From: MyoungHoon chae Date: Fri, 6 Nov 2015 00:33:16 +0000 (+0900) Subject: daemonizing phonenumber-utils X-Git-Tag: submit/tizen/20151125.062823^0 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F55%2F51255%2F12;p=platform%2Fcore%2Ftelephony%2Fphonenumber-utils.git daemonizing phonenumber-utils Change-Id: Id80475abaad362c89f45d1358516620bb300ade6 Signed-off-by: MyoungHoon chae --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 02a2ee6..8b3513c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,33 +1,18 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.6) - PROJECT(phonenumber-utils C CXX) -SET(COMMON_CFLAGS "-fvisibility=hidden -fdata-sections -ffunction-sections -Wl,--gc-sections -fPIC") -SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${COMMON_CFLAGS} ${EXTRA_CFLAGS}") -SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_CXXFLAGS} -Wall -fPIC -std=c++0x -fvisibility=hidden") - -SET(SRC_FILE - src/phn.c - src/phn_location.c - src/phn_region_data.c - src/phn_phonenumber_wrapper.cpp -) INCLUDE(FindPkgConfig) -pkg_check_modules(PN_pkgs REQUIRED dlog icu-i18n glib-2.0 capi-base-common capi-system-system-settings tapi capi-system-info) -INCLUDE_DIRECTORIES(${PN_pkgs_INCLUDE_DIRS}) -LINK_DIRECTORIES(${PN_pkgs_LIBRARY_DIRS}) -INCLUDE_DIRECTORIES(include) -ADD_LIBRARY(${PROJECT_NAME} SHARED ${SRC_FILE}) -TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${PN_pkgs_LIBRARIES} m phonenumber geocoding) -SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES VERSION ${FULLVER} SOVERSION ${MAJORVER}) -INSTALL(TARGETS ${PROJECT_NAME} DESTINATION ${LIB_INSTALL_DIR}) +SET(EXTRA_CFLAGS "-fdata-sections -ffunction-sections -Wl,--gc-sections") +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -fvisibility=hidden ") +SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=c++0x -fvisibility=hidden") + +SET(CLIENT ${PROJECT_NAME}) +SET(DAEMON "${PROJECT_NAME}-daemon") +SET(DBUS_INTERFACE "org.tizen.PhonenumberUtils.dbus") -SET(PREFIX ${CMAKE_INSTALL_PREFIX}) -CONFIGURE_FILE(${PROJECT_NAME}.pc.in ${PROJECT_NAME}.pc @ONLY) -INSTALL(FILES ${PROJECT_NAME}.pc DESTINATION ${LIB_INSTALL_DIR}/pkgconfig) +ADD_SUBDIRECTORY(common) +ADD_SUBDIRECTORY(daemon) +ADD_SUBDIRECTORY(client) +ADD_SUBDIRECTORY(test) -FILE(GLOB HEADER_FILES include/*.h) -INSTALL(FILES ${HEADER_FILES} DESTINATION ${INCLUDE_INSTALL_DIR}/${PROJECT_NAME}) -INSTALL(DIRECTORY DESTINATION /opt/usr/data/${PROJECT_NAME}) -INSTALL(DIRECTORY DESTINATION /opt/usr/data/${PROJECT_NAME}/downloads) diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt new file mode 100644 index 0000000..afbb062 --- /dev/null +++ b/client/CMakeLists.txt @@ -0,0 +1,32 @@ +INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/common) +INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include) + +FILE(GLOB CLIENT_SRCS *.c) +SET(CLIENT_SRCS ${CLIENT_SRCS} ${CMAKE_SOURCE_DIR}/common/phn-dbus.c) + +SET_SOURCE_FILES_PROPERTIES(${CMAKE_SOURCE_DIR}/common/phn-dbus.c + PROPERTIES GENERATED TRUE) + +pkg_check_modules(client_pkgs REQUIRED + dlog glib-2.0 + capi-base-common + gio-2.0 + gio-unix-2.0 + capi-system-info) +INCLUDE_DIRECTORIES(${client_pkgs_INCLUDE_DIRS}) +LINK_DIRECTORIES(${client_pkgs_LIBRARY_DIRS}) + +ADD_DEFINITIONS("-DPHN_DBUS_INTERFACE=\"${DBUS_INTERFACE}\"") + +ADD_LIBRARY(${CLIENT} SHARED ${CLIENT_SRCS}) +ADD_DEPENDENCIES(${CLIENT} GENERATED_DBUS_CODE) +TARGET_LINK_LIBRARIES(${CLIENT} ${client_pkgs_LIBRARIES}) +SET_TARGET_PROPERTIES(${CLIENT} PROPERTIES VERSION ${FULLVER} SOVERSION ${MAJORVER}) +INSTALL(TARGETS ${CLIENT} DESTINATION ${LIB_INSTALL_DIR}) + +SET(PREFIX ${CMAKE_INSTALL_PREFIX}) +CONFIGURE_FILE(${CLIENT}.pc.in ${CLIENT}.pc @ONLY) +INSTALL(FILES ${CLIENT}.pc DESTINATION ${LIB_INSTALL_DIR}/pkgconfig) + +FILE(GLOB HEADER_FILES ${CMAKE_SOURCE_DIR}/include/*.h) +INSTALL(FILES ${HEADER_FILES} DESTINATION ${INCLUDE_INSTALL_DIR}/${PROJECT_NAME}) diff --git a/client/phnc.c b/client/phnc.c new file mode 100644 index 0000000..94c7aaa --- /dev/null +++ b/client/phnc.c @@ -0,0 +1,258 @@ +/* + * Phonenumber Utils + * + * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include +#include +#include +#include +#include +#include + +#include "phone_number.h" +#include "phone_number_errors.h" +#include "phone_number_types.h" + +#include "phn-dbus.h" +#include "phnc.h" + +#define PHN_FEATURE_TELEPHONY "http://tizen.org/feature/network.telephony" + +#define PHN_FEATURE_TELEPHONY_NOT_CHECKED -1 +#define PHN_FEATURE_TELEPHONY_NOT_SUPPORTED 0 +#define PHN_FEATURE_TELEPHONY_SUPPORTED 1 + +static phnDbus *phn_client_dbus_object; + +int _phn_client_dbus_start() +{ + GError *error = NULL; + FN_CALL; + + if (phn_client_dbus_object) { + DBG("phn_client_dbus_object is already created"); + return PHONE_NUMBER_ERROR_NONE; + } + + phn_client_dbus_object = phn_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SYSTEM, + G_DBUS_PROXY_FLAGS_NONE, + PHN_DBUS_INTERFACE, + PHN_DBUS_OBJPATH, + NULL, + &error); + if (NULL == phn_client_dbus_object) { + ERR("phn_client_dbus_proxy_new_for_bus_sync() Fail(%s)", error->message); + g_error_free(error); + return PHONE_NUMBER_ERROR_SYSTEM; + } + + return PHONE_NUMBER_ERROR_NONE; +} + + +static int _phn_is_support_telephony_feature() +{ + int err; + bool is_support; + static int _phn_telephony_feature_support = PHN_FEATURE_TELEPHONY_NOT_CHECKED; + + if (PHN_FEATURE_TELEPHONY_NOT_CHECKED != _phn_telephony_feature_support) + return _phn_telephony_feature_support; + + err = system_info_get_platform_bool(PHN_FEATURE_TELEPHONY, &is_support); + if (SYSTEM_INFO_ERROR_NONE != err) { + DBG("Error system_info_get_platform_bool : %d", err); + return PHONE_NUMBER_ERROR_SYSTEM; + } + + if (is_support) + _phn_telephony_feature_support = PHN_FEATURE_TELEPHONY_SUPPORTED; + else + _phn_telephony_feature_support = PHN_FEATURE_TELEPHONY_NOT_SUPPORTED; + + return _phn_telephony_feature_support; +} + + +API int phone_number_connect(void) +{ + int ret; + FN_CALL; + +#if !GLIB_CHECK_VERSION(2,35,0) + g_type_init(); +#endif + + ret = _phn_client_dbus_start(); + if (PHONE_NUMBER_ERROR_NONE != ret) + ERR("_phn_client_dbus_start() Fail(%d)", ret); + + return ret; +} + + +API int phone_number_disconnect(void) +{ + if (NULL == phn_client_dbus_object) { + ERR("dbus not initialized"); + return PHONE_NUMBER_ERROR_SYSTEM; + } + + g_object_unref(phn_client_dbus_object); + phn_client_dbus_object = NULL; + DBG("client closed"); + + return PHONE_NUMBER_ERROR_NONE; +} + + +API int phone_number_get_location_from_number(const char *number, + phone_number_region_e region, phone_number_lang_e lang, char **location) +{ + GError *error = NULL; + char *out_loc = NULL; + int ret; + FN_CALL; + + RETVM_IF(NULL == number || '\0' == *number, PHONE_NUMBER_ERROR_INVALID_PARAMETER, + "Invalid parameter (number is NULL)"); + RETVM_IF(region < 0 || PHONE_NUMBER_REGION_MAX <= region, + PHONE_NUMBER_ERROR_INVALID_PARAMETER, "Invalid parameter (region:%d)", region); + RETVM_IF(lang < 0 || PHONE_NUMBER_LANG_MAX <= lang, PHONE_NUMBER_ERROR_INVALID_PARAMETER, + "Invalid parameter (lang:%d)", lang); + RETVM_IF(NULL == location, PHONE_NUMBER_ERROR_INVALID_PARAMETER, + "Invalid parameter (location is NULL)"); + + if (NULL == phn_client_dbus_object) { + phone_number_connect(); + phn_dbus_call_get_location_sync ( + phn_client_dbus_object, number, region, lang, &out_loc, &ret, NULL, &error); + phone_number_disconnect(); + } + else { + phn_dbus_call_get_location_sync ( + phn_client_dbus_object, number, region, lang, &out_loc, &ret, NULL, &error); + } + + if (NULL != error) { + ERR("dbus sync error : %s", error->message); + free(out_loc); + return PHONE_NUMBER_ERROR_SYSTEM; + } + + if (PHONE_NUMBER_ERROR_NONE != ret) { + ERR("Phonenumber utils error : %d", ret); + free(out_loc); + return ret; + } + + *location = out_loc; + + return ret; +} + +API int phone_number_get_formatted_number(const char *number, + phone_number_region_e region, char **formatted_number) +{ + GError *error = NULL; + char *out_num = NULL; + int ret; + FN_CALL; + + RETVM_IF(NULL == number || '\0' == *number, PHONE_NUMBER_ERROR_INVALID_PARAMETER, + "Invalid parameter (number is NULL)"); + RETVM_IF((int)region < 0 || PHONE_NUMBER_REGION_MAX <= region, + PHONE_NUMBER_ERROR_INVALID_PARAMETER, "Invalid parameter (region:%d)", region); + RETVM_IF(NULL == formatted_number, PHONE_NUMBER_ERROR_INVALID_PARAMETER, + "Invalid parameter (formatted_number is NULL)"); + + + if (NULL == phn_client_dbus_object) { + phone_number_connect(); + phn_dbus_call_get_number_sync ( + phn_client_dbus_object, number, region, &out_num, &ret, NULL, &error); + phone_number_disconnect(); + } + else { + phn_dbus_call_get_number_sync ( + phn_client_dbus_object, number, region, &out_num, &ret, NULL, &error); + } + + if (NULL != error) { + ERR("dbus sync error : %s", error->message); + free(out_num); + return PHONE_NUMBER_ERROR_SYSTEM; + } + + if (PHONE_NUMBER_ERROR_NONE != ret) { + ERR("Phonenumber utils error : %d", ret); + free(out_num); + return ret; + } + + *formatted_number = out_num; + return ret; +} + + +API int phone_number_get_normalized_number(const char *number, char **normalized_number) +{ + GError *error = NULL; + char *out_num = NULL; + int ret; + FN_CALL; + + ret = _phn_is_support_telephony_feature(); + + RETVM_IF(PHN_FEATURE_TELEPHONY_NOT_SUPPORTED == ret, + PHONE_NUMBER_ERROR_NOT_SUPPORTED, "Telephony feature is disabled"); + RETVM_IF(PHONE_NUMBER_ERROR_SYSTEM == ret, + PHONE_NUMBER_ERROR_SYSTEM, "Gerring system info error"); + + RETVM_IF(NULL == number || '\0' == *number, PHONE_NUMBER_ERROR_INVALID_PARAMETER, + "Invalid parameter (number is NULL)"); + RETVM_IF(NULL == normalized_number, PHONE_NUMBER_ERROR_INVALID_PARAMETER, + "Invalid parameter (normalized_number is NULL)"); + + if (NULL == phn_client_dbus_object) { + phone_number_connect(); + phn_dbus_call_get_normalized_number_sync ( + phn_client_dbus_object, number, &out_num, &ret, NULL, &error); + phone_number_disconnect(); + } + else { + phn_dbus_call_get_normalized_number_sync ( + phn_client_dbus_object, number, &out_num, &ret, NULL, &error); + } + + if (NULL != error) { + ERR("dbus sync error : %s", error->message); + free(out_num); + return PHONE_NUMBER_ERROR_SYSTEM; + } + + if (PHONE_NUMBER_ERROR_NONE != ret) { + ERR("Phonenumber utils error : %d", ret); + free(out_num); + return ret; + } + + *normalized_number = out_num; + return ret; +} + diff --git a/client/phnc.h b/client/phnc.h new file mode 100644 index 0000000..0aed10a --- /dev/null +++ b/client/phnc.h @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef __PHONENUMBER_UTILS_CLIENT_H__ +#define __PHONENUMBER_UTILS_CLIENT_H__ + +#include "phn-common.h" +#include "phn-log.h" + +#ifdef API +#undef API +#endif +#define API __attribute__((visibility("default"))) + +#endif /*__PHONENUMBER_UTILS_CLIENT_H__*/ + diff --git a/client/phonenumber-utils.pc.in b/client/phonenumber-utils.pc.in new file mode 100644 index 0000000..ba87bea --- /dev/null +++ b/client/phonenumber-utils.pc.in @@ -0,0 +1,11 @@ +prefix=@PREFIX@ +exec_prefix=${prefix}/bin +libdir=@LIB_INSTALL_DIR@ +includedir=@INCLUDE_INSTALL_DIR@/@PROJECT_NAME@ + +Name: @PROJECT_NAME@ +Description: @PROJECT_NAME@ library +Version: @FULLVER@ +Requires: +Libs: -L${libdir} -l@PROJECT_NAME@ +Cflags: -I${includedir} diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt new file mode 100644 index 0000000..57b96a3 --- /dev/null +++ b/common/CMakeLists.txt @@ -0,0 +1,13 @@ +SET(PHN_DBUS "phn-dbus") + +FIND_PROGRAM(GDBUS_CODEGEN NAMES gdbus-codegen) + +ADD_CUSTOM_COMMAND( + OUTPUT dbus + COMMAND ${GDBUS_CODEGEN} --generate-c-code ${PHN_DBUS} + --interface-prefix org.tizen.PhonenumberUtils. + --c-namespace phn + ${PHN_DBUS}.xml + DEPENDS ${PHN_DBUS}.xml) + +ADD_CUSTOM_TARGET(GENERATED_DBUS_CODE DEPENDS dbus) diff --git a/common/phn-common.h b/common/phn-common.h new file mode 100644 index 0000000..435aa60 --- /dev/null +++ b/common/phn-common.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef __PHONENUMBER_UTILS_COMMON_H__ +#define __PHONENUMBER_UTILS_COMMON_H__ + +#include "phone_number_errors.h" + +#ifndef PHN_DBUS_INTERFACE +#define PHN_DBUS_INTERFACE "org.tizen.PhonenumberUtils.dbus" +#endif + +#define PHN_DBUS_OBJPATH "/org/tizen/phonenumberutils/dbus" +#define PHN_DBUS_SIGNAL_LENGTH 20 +#define PHN_STR_SHORT_LEN 1024 +#define STRING_EQUAL 0 + +#define PHN_DBUS_SIGNAL_REQUEST_HANDLER "REQ" +#define PHN_DBUS_SIGNAL_FOUND_RESOURCE "RES" +#define PHN_DBUS_SIGNAL_GET "GET" +#define PHN_DBUS_SIGNAL_PUT "PUT" +#define PHN_DBUS_SIGNAL_POST "POST" +#define PHN_DBUS_SIGNAL_DELETE "DELETE" +#define PHN_DBUS_SIGNAL_OBSERVE "OBSERVE" +#define PHN_DBUS_SIGNAL_DEVICE "DEVICE" +#define PHN_DBUS_SIGNAL_PLATFORM "PLATFORM" +#define PHN_DBUS_SIGNAL_PRESENCE "PRESENCE" + +#endif /* __PHONENUMBER_UTILS_COMMON_H__ */ + diff --git a/common/phn-dbus.xml b/common/phn-dbus.xml new file mode 100644 index 0000000..2b1e983 --- /dev/null +++ b/common/phn-dbus.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/common/phn-log.h b/common/phn-log.h new file mode 100644 index 0000000..84d45e2 --- /dev/null +++ b/common/phn-log.h @@ -0,0 +1,139 @@ +/* + * Phonenumber Utils + * + * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +#ifndef __PHONENUMBER_COMMON_LOG_H__ +#define __PHONENUMBER_COMMON_LOG_H__ + +#ifdef PHN_CONSOLE_COLOR_LOG +#define PHN_LOG_RED "\033[0;31m" +#define PHN_LOG_GREEN "\033[0;32m" +#define PHN_LOG_BROWN "\033[0;33m" +#define PHN_LOG_BLUE "\033[0;34m" +#define PHN_LOG_END "\033[0;m" +#else +#define PHN_LOG_RED +#define PHN_LOG_GREEN +#define PHN_LOG_BROWN +#define PHN_LOG_BLUE +#define PHN_LOG_END +#endif + +#undef _DBG +#undef _INFO +#undef _WARN +#undef _ERR + +#undef DBG +#undef INFO +#undef WARN +#undef ERR + +#define LOG_TAG "PHN_UTILS" +#include + +#ifdef PHN_DAEMON_LOG + +#define _DBG(fmt, arg...) SLOGD(PHN_LOG_GREEN " " PHN_LOG_END fmt, ##arg) +#define _INFO(fmt, arg...) SLOGI(PHN_LOG_GREEN " " PHN_LOG_END fmt, ##arg) +#define _WARN(fmt, arg...) SLOGW(PHN_LOG_GREEN " " PHN_LOG_END fmt, ##arg) +#define _ERR(fmt, arg...) SLOGE(PHN_LOG_GREEN " " PHN_LOG_END fmt, ##arg) + +#else /* PHN_DAEMON_LOG */ + +#define _DBG(fmt, arg...) SLOGD(fmt, ##arg) +#define _INFO(fmt, arg...) SLOGI(fmt, ##arg) +#define _WARN(fmt, arg...) SLOGW(fmt, ##arg) +#define _ERR(fmt, arg...) SLOGE(fmt, ##arg) + +#endif /* PHN_DAEMON_LOG */ + + +#define PHN_DEBUG_ON + +#ifdef PHN_DEBUG_ON + +#define FN_CALL _INFO(">>>>>>>> called") +#define FN_END _INFO("<<<<<<<< ended") +#define DBG(fmt, arg...) _DBG(fmt, ##arg) +#define WARN(fmt, arg...) _WARN(PHN_LOG_BROWN fmt PHN_LOG_END, ##arg) +#define ERR(fmt, arg...) _ERR(PHN_LOG_RED fmt PHN_LOG_END, ##arg) +#define INFO(fmt, arg...) _INFO(PHN_LOG_BLUE fmt PHN_LOG_END, ##arg) +#define SECURE_DBG(fmt, arg...) SECURE_SLOGD(fmt, ##arg) +#define SECURE_ERR(fmt, arg...) SECURE_SLOGE(fmt, ##arg) + +#else /* PHN_DEBUG_ON */ + +#define FN_CALL +#define FN_END +#define DBG(fmt, arg...) +#define WARN(fmt, arg...) +#define ERR(fmt, arg...) _ERR(fmt, ##arg) +#define INFO(fmt, arg...) +#define SECURE_DBG(fmt, arg...) +#define SECURE_ERR(fmt, arg...) SECURE_SLOGE(fmt, ##arg) + +#endif /* PHN_DEBUG_ON */ + + +#define RET_IF(expr) \ + do { \ + if (expr) { \ + ERR("(%s)", #expr); \ + return; \ + }\ + } while(0) + +#define RETV_IF(expr, val) \ + do {\ + if (expr) { \ + ERR("(%s)", #expr); \ + return (val); \ + } \ + } while(0) + +#define RETM_IF(expr, fmt, arg...) \ + do {\ + if (expr) { \ + ERR(fmt, ##arg); \ + return; \ + }\ + } while(0) + +#define RETVM_IF(expr, val, fmt, arg...) \ + do {\ + if (expr) { \ + ERR(fmt, ##arg); \ + return (val); \ + } \ + } while(0) + +#define ERR_IF(expr) \ + do { \ + if (expr) { \ + ERR("(%s)", #expr); \ + } \ + } while (0) + +#define WARN_IF(expr, fmt, arg...) \ + do { \ + if (expr) { \ + WARN(fmt, ##arg); \ + } \ + } while (0) + +#endif /* __PHONENUMBER_COMMON_LOG_H__ */ diff --git a/daemon/CMakeLists.txt b/daemon/CMakeLists.txt new file mode 100644 index 0000000..915bf39 --- /dev/null +++ b/daemon/CMakeLists.txt @@ -0,0 +1,40 @@ +INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/common) +INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include) + +FILE(GLOB DAEMON_SRCS *.c *.cpp) +SET(DAEMON_SRCS ${DAEMON_SRCS} ${CMAKE_SOURCE_DIR}/common/phn-dbus.c) + +SET_SOURCE_FILES_PROPERTIES(${CMAKE_SOURCE_DIR}/common/phn-dbus.c + PROPERTIES GENERATED TRUE) + +pkg_check_modules(daemon_pkgs REQUIRED + dlog + icu-i18n + glib-2.0 + gio-2.0 + gio-unix-2.0 + capi-base-common + capi-system-system-settings + tapi +) + +INCLUDE_DIRECTORIES(${daemon_pkgs_INCLUDE_DIRS}) +LINK_DIRECTORIES(${daemon_pkgs_LIBRARY_DIRS}) + +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIE") +SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIE") +SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--hash-style=both -pie") +ADD_DEFINITIONS("-DPHN_DBUS_INTERFACE=\"${DBUS_INTERFACE}\"") + +ADD_EXECUTABLE(${DAEMON} ${DAEMON_SRCS}) +ADD_DEPENDENCIES(${DAEMON} GENERATED_DBUS_CODE) +TARGET_LINK_LIBRARIES(${DAEMON} ${daemon_pkgs_LIBRARIES} m phonenumber geocoding) + +INSTALL(TARGETS ${DAEMON} DESTINATION ${BIN_INSTALL_DIR}) + +CONFIGURE_FILE(${DBUS_INTERFACE}.service.in ${DBUS_INTERFACE}.service @ONLY) +INSTALL(FILES ${DBUS_INTERFACE}.service DESTINATION ${SHARE_INSTALL_PREFIX}/dbus-1/system-services) +INSTALL(FILES ${DBUS_INTERFACE}.conf DESTINATION ${SYSCONF_INSTALL_DIR}/dbus-1/system.d) + +INSTALL(DIRECTORY DESTINATION /opt/usr/data/${PROJECT_NAME}) +INSTALL(DIRECTORY DESTINATION /opt/usr/data/${PROJECT_NAME}/downloads) diff --git a/daemon/org.tizen.PhonenumberUtils.dbus.conf b/daemon/org.tizen.PhonenumberUtils.dbus.conf new file mode 100644 index 0000000..a4d5136 --- /dev/null +++ b/daemon/org.tizen.PhonenumberUtils.dbus.conf @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/daemon/org.tizen.PhonenumberUtils.dbus.service.in b/daemon/org.tizen.PhonenumberUtils.dbus.service.in new file mode 100644 index 0000000..e7e7440 --- /dev/null +++ b/daemon/org.tizen.PhonenumberUtils.dbus.service.in @@ -0,0 +1,5 @@ +[D-BUS Service] +Name=@DBUS_INTERFACE@ +Exec=/bin/false +SystemdService=@PROJECT_NAME@.service +User=root \ No newline at end of file diff --git a/daemon/phnd-dbus.c b/daemon/phnd-dbus.c new file mode 100644 index 0000000..2eb273e --- /dev/null +++ b/daemon/phnd-dbus.c @@ -0,0 +1,257 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include + +#include "phn-dbus.h" +#include "phn-log.h" +#include "phn-common.h" +#include "phnd.h" +#include "phnd-utils.h" +#include "phnd-libphonenumber.h" +#include "phnd-location.h" +#include "phnd-region-data.h" + +int _get_location_from_libphonenumber(const char *number, + phone_number_region_e region, phone_number_lang_e lang, char **location) +{ + int ret = PHONE_NUMBER_ERROR_NONE; + char *region_str = NULL; + char *lang_str = NULL; + char *location_file = NULL; + + ret = phn_region_data_get_region_str(region, ®ion_str); + if (PHONE_NUMBER_ERROR_NONE != ret) { + ERR("phn_region_data_get_region_str() Fail(%d)", ret); + goto FREE_N_RETURN; + } + + ret = phn_region_data_get_lang_str(lang, &lang_str); + if (PHONE_NUMBER_ERROR_NONE != ret) { + ERR("phn_region_data_get_lang_str() Fail(%d)", ret); + goto FREE_N_RETURN; + } + + ret = phn_location_find_extra_data(region_str, &location_file); + if (PHONE_NUMBER_ERROR_NONE == ret && location_file) { + ret = phn_location_get_location_from_extra_data(location_file, number, + region_str, lang_str, location); + if (PHONE_NUMBER_ERROR_NONE == ret && *location) { + goto FREE_N_RETURN; + } + } + + bool exist = phn_region_data_find_match_info(region, lang); + if (false == exist) { + INFO("Language not matched with Region. Set to defualt language."); + free(lang_str); + lang_str = strdup(PHN_REGION_DEFAULT_LANG); + } + + ret = phn_get_location_from_number(number, region_str, lang_str, location); + if (PHONE_NUMBER_ERROR_NONE != ret) { + ERR("phn_get_location_from_number() Fail(%d)", ret); + goto FREE_N_RETURN; + } + +FREE_N_RETURN: + free(region_str); + free(lang_str); + free(location_file); + phnd_utils_start_timeout(); + return ret; +} + +int _get_number_from_libphonenumber(const char *number, phone_number_region_e region, + char **formatted_number) +{ + int ret = PHONE_NUMBER_ERROR_NONE; + char *region_str = NULL; + + ret = phn_region_data_get_region_str(region, ®ion_str); + if (PHONE_NUMBER_ERROR_NONE != ret) { + ERR("phn_region_data_get_region_str() Fail(%d)", ret); + goto FREE_N_RETURN; + } + + ret = phn_get_formatted_number(number, region_str, formatted_number); + if (PHONE_NUMBER_ERROR_NONE != ret) { + ERR("phn_get_formatted_number() Fail(%d)", ret); + goto FREE_N_RETURN; + } + +FREE_N_RETURN: + free(region_str); + phnd_utils_start_timeout(); + return ret; +} + + +int _get_normalized_number_from_libphonenumber(const char *number, + char **normalized_number) +{ + int ret = PHONE_NUMBER_ERROR_NONE; + + ret = phn_get_normalized_number(number, normalized_number); + if (PHONE_NUMBER_ERROR_NONE != ret) + ERR("phn_get_normalized_number() Fail(%d)", ret); + + phnd_utils_start_timeout(); + return ret; +} + + +static gboolean _dbus_handle_get_location(phnDbus *object, + GDBusMethodInvocation *invocation, + gchar *number, + gint region, + gint lang, + guint signal_number) +{ + FN_CALL; + char *location; + int ret; + + DBG("number = %s, region = %d, lang = %d", number, region, lang); + + ret = _get_location_from_libphonenumber(number, region, lang, &location); + if (PHONE_NUMBER_ERROR_NONE != ret) { + ERR("err ret = %d", ret); + location = ""; + } + + phn_dbus_complete_get_location(object, invocation, location, ret); + + return TRUE; +} + +static gboolean _dbus_handle_get_number(phnDbus *object, + GDBusMethodInvocation *invocation, + gchar *number, + gint region, + guint signal_number) +{ + FN_CALL; + char *formatted_number; + int ret; + + DBG("number = %s, region = %d", number, region); + + ret = _get_number_from_libphonenumber(number, region, &formatted_number); + + if (PHONE_NUMBER_ERROR_NONE != ret) { + ERR("err ret = %d", ret); + formatted_number = ""; + } + + phn_dbus_complete_get_number(object, invocation, formatted_number, ret); + + + return TRUE; +} + +static gboolean _dbus_handle_get_normalized_number(phnDbus *object, + GDBusMethodInvocation *invocation, + gchar *number, + guint signal_number) +{ + FN_CALL; + char *normalized_number; + int ret; + + DBG("number = %s", number); + + ret = _get_normalized_number_from_libphonenumber(number, &normalized_number); + if (PHONE_NUMBER_ERROR_NONE != ret) { + ERR("err ret = %d", ret); + normalized_number = ""; + } + + phn_dbus_complete_get_normalized_number(object, invocation, normalized_number, ret); + + return TRUE; +} + +static void _dbus_bus_acquired_handler(GDBusConnection *conn, const gchar *name, + gpointer user_data) +{ + + gboolean ret; + phnDbus *dbus_object; + GError *error = NULL; + + DBG("Acquired the bus %s", name); + + dbus_object = phn_dbus_skeleton_new(); + + g_signal_connect(dbus_object, "handle-get-location", + G_CALLBACK(_dbus_handle_get_location), NULL); + g_signal_connect(dbus_object, "handle-get-number", + G_CALLBACK(_dbus_handle_get_number), NULL); + g_signal_connect(dbus_object, "handle-get-normalized-number", + G_CALLBACK(_dbus_handle_get_normalized_number), NULL); + + ret = g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(dbus_object), conn, + PHN_DBUS_OBJPATH, &error); + + if (FALSE == ret) { + ERR("g_dbus_interface_skeleton_export() Fail(%s)", error->message); + g_error_free(error); + } +} + +static void _dbus_name_lost_handler(GDBusConnection *connection, const gchar *name, + gpointer user_data) +{ + DBG("Lost the name %s", name); +} + + +static void _dbus_name_acquired_handler(GDBusConnection *connection, const gchar *name, + gpointer user_data) +{ + DBG("Acquired the name %s", name); +} + +unsigned int phnd_dbus_init() +{ + guint id; + DBG("phnd_dbus_init"); + + id = g_bus_own_name(G_BUS_TYPE_SYSTEM, + PHN_DBUS_INTERFACE, + G_BUS_NAME_OWNER_FLAGS_REPLACE, + _dbus_bus_acquired_handler, + _dbus_name_acquired_handler, + _dbus_name_lost_handler, + NULL, + NULL); + + phnd_utils_start_timeout(); + + if (0 == id) { + ERR("g_bus_own_name() Fail"); + return 0; + } + + return id; +} + + +void phnd_dbus_deinit(unsigned int id) +{ + g_bus_unown_name(id); +} diff --git a/daemon/phnd-dbus.h b/daemon/phnd-dbus.h new file mode 100644 index 0000000..9e51a77 --- /dev/null +++ b/daemon/phnd-dbus.h @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef __PHONENUMBER_UTILS_DAEMON_DBUS_H__ +#define __PHONENUMBER_UTILS_DAEMON_DBUS_H__ + +unsigned int phnd_dbus_init(); +void phnd_dbus_deinit(unsigned int id); + + +#endif /*__PHONENUMBER_UTILS_DAEMON_DBUS_H__*/ + diff --git a/daemon/phnd-libphonenumber.cpp b/daemon/phnd-libphonenumber.cpp new file mode 100644 index 0000000..e56de8f --- /dev/null +++ b/daemon/phnd-libphonenumber.cpp @@ -0,0 +1,513 @@ +/* + * Phonenumber Utils + * + * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "phone_number_errors.h" +#include "phn-log.h" +#include "phnd.h" +#include "phnd-libphonenumber.h" + +#define MCC_LEN 3 +using namespace i18n::phonenumbers; + +typedef struct { + int mcc; + int cc; +} mcc_cc_map_s; + +static int _cc = 0; +static TapiHandle **_tapi_handle = NULL; +static int _modem_num = 0; +static int _phn_get_cc(bool reload, int *out_cc); + +int phn_get_location_from_number(const char *number, const char *region, const char *language, char **location) +{ + PhoneNumber phNumber; + const PhoneNumberUtil& pn_instance = *PhoneNumberUtil::GetInstance(); + const PhoneNumberUtil::ErrorType status = pn_instance.Parse( + number, region, &phNumber); + RETVM_IF(status != PhoneNumberUtil::NO_PARSING_ERROR, PHONE_NUMBER_ERROR_NO_DATA, "parse() failed(%d)", status); + + const std::string description = + PhoneNumberOfflineGeocoder().GetDescriptionForNumber( + phNumber, icu::Locale(language)); + *location = g_strdup((gchar *)description.c_str()); + + return PHONE_NUMBER_ERROR_NONE; +} + +int phn_get_formatted_number(const char *number, const char *region, char **formatted_number) +{ + const PhoneNumberUtil& pn_instance = *PhoneNumberUtil::GetInstance(); + AsYouTypeFormatter *formatter = pn_instance.GetAsYouTypeFormatter(region); + + int i = 0; + string result; + while (number[i] && '\0' != number[i]) { + formatter->InputDigit(number[i++], &result); + } + delete formatter; + *formatted_number = g_strdup((gchar *)result.c_str()); + + return PHONE_NUMBER_ERROR_NONE; +} + +static void _phn_cc_changed_cb(TapiHandle *handle, const char *noti_id, void *data, void *user_data) +{ + int ret; + ret = _phn_get_cc(true, NULL); + if (PHONE_NUMBER_ERROR_NONE != ret) { + ERR("_phn_get_cc() Fail(%d)", ret); + return; + } +} + +static int _phn_get_tapi_handle() +{ + int i; + int ret; + char **cp_list = NULL; + _modem_num = 0; + + cp_list = tel_get_cp_name_list(); + if (NULL == cp_list) { + ERR("tel_get_cp_name_list() Fail(NULL)"); + return PHONE_NUMBER_ERROR_SYSTEM; + } + + while (cp_list[_modem_num]) + _modem_num++; + DBG("_modem_num=[%d]", _modem_num); + + _tapi_handle = (TapiHandle **)calloc(_modem_num, sizeof(TapiHandle *)); + if (NULL == _tapi_handle) { + ERR("calloc() Fail"); + g_strfreev(cp_list); + return PHONE_NUMBER_ERROR_OUT_OF_MEMORY; + } + + for (i = 0; i < _modem_num; i++) { + _tapi_handle[i] = tel_init(cp_list[i]); + if (NULL == _tapi_handle[i]) { + ERR("tel_init() for _tapi_handle[%d] Fail", i); + g_strfreev(cp_list); + return PHONE_NUMBER_ERROR_SYSTEM; + } + + ret = tel_register_noti_event(_tapi_handle[i], TAPI_PROP_NETWORK_PLMN, + _phn_cc_changed_cb, NULL); + if (TAPI_API_SUCCESS != ret) { + if (TAPI_API_ACCESS_DENIED == ret) { + ERR("tel_register_noti_event() Fail(%d)", ret); + g_strfreev(cp_list); + return PHONE_NUMBER_ERROR_PERMISSION_DENIED; + } else { + ERR("tel_register_noti_event() for _tapi_handle[%d] Fail(%d)", i, ret); + g_strfreev(cp_list); + return PHONE_NUMBER_ERROR_SYSTEM; + } + } + } + + g_strfreev(cp_list); + return PHONE_NUMBER_ERROR_NONE; +} + +static int _phn_get_cc(bool reload, int *out_cc) +{ + int i = 0; + int state; + int ret; + int mcc = 0; + char *temp = NULL; + static bool cc_loaded = false; + + mcc_cc_map_s _mcc_cc_list[] = { + {0, 1}, + {202, 30}, + {204, 31}, + {206, 32}, + {208, 33}, + {212, 377}, + {213, 376}, + {214, 34}, + {216, 36}, + {218, 387}, + {219, 385}, + {220, 381}, + {222, 39}, + {225, 39}, + {226, 40}, + {228, 41}, + {230, 420}, + {231, 421}, + {232, 43}, + {234, 44}, + {235, 44}, + {238, 45}, + {240, 46}, + {242, 47}, + {244, 358}, + {246, 370}, + {247, 371}, + {248, 372}, + {250, 7}, + {255, 380}, + {257, 375}, + {259, 373}, + {260, 48}, + {262, 49}, + {266, 350}, + {268, 351}, + {270, 352}, + {272, 353}, + {274, 354}, + {276, 355}, + {278, 356}, + {280, 357}, + {282, 995}, + {283, 374}, + {284, 359}, + {286, 90}, + {288, 298}, + {290, 299}, + {292, 378}, + {293, 386}, + {294, 389}, + {295, 423}, + {297, 382}, + {302, 1}, + {308, 508}, + {310, 1}, + {311, 1}, + {312, 1}, + {313, 1}, + {314, 1}, + {315, 1}, + {316, 1}, + {330, 1}, + {332, 1}, + {334, 52}, + {338, 1}, + {340, 590}, + {340, 596}, + {342, 1}, + {344, 1}, + {346, 1}, + {348, 1}, + {350, 1}, + {352, 1}, + {354, 1}, + {356, 1}, + {358, 1}, + {360, 1}, + {362, 599}, + {363, 297}, + {364, 1}, + {365, 1}, + {366, 1}, + {368, 53}, + {370, 1}, + {372, 509}, + {374, 1}, + {376, 1}, + {400, 994}, + {401, 7}, + {402, 975}, + {404, 91}, + {405, 91}, + {406, 91}, + {410, 92}, + {412, 93}, + {413, 94}, + {414, 95}, + {415, 961}, + {416, 962}, + {417, 963}, + {418, 964}, + {419, 965}, + {420, 966}, + {421, 967}, + {422, 968}, + {424, 971}, + {425, 972}, + {426, 973}, + {427, 974}, + {428, 976}, + {429, 977}, + {430, 971}, + {431, 971}, + {432, 98}, + {434, 998}, + {436, 992}, + {437, 996}, + {438, 993}, + {440, 81}, + {441, 81}, + {450, 82}, + {452, 84}, + {454, 852}, + {455, 853}, + {456, 855}, + {457, 856}, + {460, 86}, + {461, 86}, + {466, 886}, + {467, 850}, + {470, 880}, + {472, 960}, + {502, 60}, + {505, 61}, + {510, 62}, + {514, 670}, + {515, 63}, + {520, 66}, + {525, 65}, + {528, 673}, + {530, 64}, + {536, 674}, + {537, 675}, + {539, 676}, + {540, 677}, + {541, 678}, + {542, 679}, + {543, 681}, + {544, 1}, + {545, 686}, + {546, 687}, + {547, 689}, + {548, 682}, + {549, 685}, + {550, 691}, + {551, 692}, + {552, 680}, + {602, 20}, + {603, 213}, + {604, 212}, + {605, 216}, + {606, 218}, + {607, 220}, + {608, 221}, + {609, 222}, + {610, 223}, + {611, 224}, + {612, 225}, + {613, 226}, + {614, 227}, + {615, 228}, + {616, 229}, + {617, 230}, + {618, 231}, + {619, 232}, + {620, 233}, + {621, 234}, + {622, 235}, + {623, 236}, + {624, 237}, + {625, 238}, + {626, 239}, + {627, 240}, + {628, 241}, + {629, 242}, + {630, 243}, + {631, 244}, + {632, 245}, + {633, 248}, + {634, 249}, + {635, 250}, + {636, 251}, + {637, 252}, + {638, 253}, + {639, 254}, + {640, 255}, + {641, 256}, + {642, 257}, + {643, 258}, + {645, 260}, + {646, 261}, + {647, 262}, + {648, 263}, + {649, 264}, + {650, 265}, + {651, 266}, + {652, 267}, + {653, 268}, + {654, 269}, + {655, 27}, + {657, 291}, + {702, 501}, + {704, 502}, + {706, 503}, + {708, 504}, + {710, 505}, + {712, 506}, + {714, 507}, + {716, 51}, + {722, 54}, + {724, 55}, + {730, 56}, + {732, 57}, + {734, 58}, + {736, 591}, + {738, 592}, + {740, 593}, + {742, 594}, + {744, 595}, + {746, 597}, + {748, 598}, + {750, 500}, + }; + + if (cc_loaded && false == reload) { + if (NULL == out_cc) { + ERR("Invalid parameter (out_cc is NULL)"); + return PHONE_NUMBER_ERROR_INVALID_PARAMETER; + } + + *out_cc = _cc; + DBG("cc=[%d]", *out_cc); + return PHONE_NUMBER_ERROR_NONE; + } + + _cc = 0; + cc_loaded = true; + + if (NULL == _tapi_handle) { + ret = _phn_get_tapi_handle(); + if (PHONE_NUMBER_ERROR_NONE != ret) { + ERR("_phn_get_tapi_handle() Fail(%d)", ret); + return ret; + } + } + + do { + WARN("Using multi SIM"); + free(temp); + temp = NULL; + + ret = tel_get_property_int(_tapi_handle[i], TAPI_PROP_NETWORK_SERVICE_TYPE, &state); + if (TAPI_API_SUCCESS != ret) { + if (TAPI_API_ACCESS_DENIED == ret) { + ERR("tel_get_property_int() Fail(%d)", ret); + return PHONE_NUMBER_ERROR_PERMISSION_DENIED; + } else { + ERR("tel_get_property_int() Fail(%d)", ret); + return PHONE_NUMBER_ERROR_SYSTEM; + } + } + + if (TAPI_NETWORK_SERVICE_TYPE_UNKNOWN == state + || TAPI_NETWORK_SERVICE_TYPE_NO_SERVICE == state + || TAPI_NETWORK_SERVICE_TYPE_EMERGENCY == state + || TAPI_NETWORK_SERVICE_TYPE_SEARCH == state) { + WARN("network service is not working : state(%d)", state); + } + + ret = tel_get_property_string(_tapi_handle[i], TAPI_PROP_NETWORK_PLMN, &temp); + if (TAPI_API_SUCCESS != ret) { + if (TAPI_API_ACCESS_DENIED == ret) { + ERR("tel_get_property_string() Fail(%d)", ret); + return PHONE_NUMBER_ERROR_PERMISSION_DENIED; + } else { + ERR("tel_get_property_string() Fail(%d)", ret); + return PHONE_NUMBER_ERROR_SYSTEM; + } + } + DBG("temp=[%s] from _tapi_handle[%d]", temp, i); + + i++; + if (_modem_num <= i) break; + + } while (NULL == temp || '\0' == temp[0]); + + if (NULL == temp || '\0' == temp[0]) { + ERR("get NETWORK_PLMN Fail"); + free(temp); + return PHONE_NUMBER_ERROR_SYSTEM; + } + + if (temp && MCC_LEN < strlen(temp)) + temp[MCC_LEN] = '\0'; + mcc = atoi(temp); + DBG("mcc=[%d]", mcc); + + for (i = 0; i < (int)(sizeof(_mcc_cc_list)/sizeof(mcc_cc_map_s)); i++) { + if (_mcc_cc_list[i].mcc == mcc) { + _cc = _mcc_cc_list[i].cc; + break; + } + } + + if (0 == _cc) { + ERR("No data for current mcc(%s)", mcc); + free(temp); + return PHONE_NUMBER_ERROR_NO_DATA; + } + + if (out_cc) { + *out_cc = _cc; + DBG("cc=[%d]", *out_cc); + } + + free(temp); + return PHONE_NUMBER_ERROR_NONE; +} + +int phn_get_normalized_number(const char *number, char **out_e164) +{ + int ret; + int cc = 0; + PhoneNumber pn; + string number_e164; + string region_code; + const PhoneNumberUtil& pnu = *PhoneNumberUtil::GetInstance(); + + ret = _phn_get_cc(false, &cc); + if (PHONE_NUMBER_ERROR_NONE != ret) { + ERR("_phn_get_cc() Fail(%d)", ret); + return ret; + } + pnu.GetRegionCodeForCountryCode(cc, ®ion_code); + + DBG("cc=[%d], number=[%s], region_code=[%s]", cc, number, region_code.c_str()); + + const PhoneNumberUtil::ErrorType status = pnu.Parse(number, region_code, &pn); + if (PhoneNumberUtil::NO_PARSING_ERROR != status) { + ERR("pnu.Parse() Fail(%d), cc(%d), number(%s), region_code(%s)", + status, cc, number, region_code.c_str()); + return PHONE_NUMBER_ERROR_SYSTEM; + } + + pnu.Format(pn, PhoneNumberUtil::E164, &number_e164); + + *out_e164 = g_strdup((gchar *)number_e164.c_str()); + DBG("number_e164=[%s]", *out_e164); + + return PHONE_NUMBER_ERROR_NONE; + +} + + diff --git a/daemon/phnd-libphonenumber.h b/daemon/phnd-libphonenumber.h new file mode 100644 index 0000000..bfbb2b9 --- /dev/null +++ b/daemon/phnd-libphonenumber.h @@ -0,0 +1,37 @@ +/* + * Phonenumber Utils + * + * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +#ifndef __PHONENUMBER_LIBPHONENUMBER_H__ +#define __PHONENUMBER_LIBPHONENUMBER_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +int phn_get_formatted_number(const char *number, const char *region, + char **formatted_number); +int phn_get_location_from_number(const char *number, const char *region, + const char *language, char **location); +int phn_get_normalized_number(const char *number, char **out_e164); + + +#ifdef __cplusplus +} +#endif + +#endif /* __PHONENUMBER_LIBPHONENUMBER_H__ */ diff --git a/daemon/phnd-location.c b/daemon/phnd-location.c new file mode 100644 index 0000000..a4164e4 --- /dev/null +++ b/daemon/phnd-location.c @@ -0,0 +1,403 @@ +/* + * Phonenumber Utils + * + * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "phone_number_errors.h" +#include "phn-log.h" +#include "phn-common.h" +#include "phnd.h" +#include "phnd-region-data.h" +#include "phnd-location.h" + +#define PHN_LOCATION_DIR "/opt/usr/data/phonenumber-utils" +#define PHN_LOCATION_FILE_PREFIX "location" + +#define PHN_LOCATION_CHINA_MOBILE_SUFFIX_OFFSET 10000 +#define PHN_LOCATION_CHINA_MOBILE_PREFIX_LEN 3 +#define PHN_LOCATION_CHINA_MOBILE_SUFFIX_LEN 4 +#define PHN_LOCATION_CHINA_MOBILE_NUMBER_MIN_LEN 7 + +struct phn_location_header { + gunichar2 version[16]; + gunichar2 version_date[16]; + int province_count; + int telephone_city_count; + int mobile_city_count; + int mobile_prefix_index_count; + int province_name_len[3]; + int province_id_len; + int telephone_number_len; + int telephone_city_len[3]; + int mobile_city_len[3]; + int mobile_prefix_len; +}; + +int phn_location_find_extra_data(char *region_str, char **p_location_file) +{ + DIR *dirp = NULL; + struct dirent **dir_list; + char *location_file = NULL; + + dirp = opendir(PHN_LOCATION_DIR); + if (NULL == dirp) { + ERR("opendir() return NULL"); + return PHONE_NUMBER_ERROR_NO_DATA; + } + + int count = scandir(PHN_LOCATION_DIR, &dir_list, 0, alphasort); + if (count) { + int idx = 0; + char location_prefix[PHN_STR_SHORT_LEN] = {0}; + + snprintf(location_prefix, sizeof(location_prefix), "%s-%s", + PHN_LOCATION_FILE_PREFIX, region_str); + while (idx != count) { + const char *file_name = dir_list[idx]->d_name; + if (0 == strncmp(file_name, location_prefix, strlen(location_prefix))) { + location_file = g_strdup(file_name); + break; + } + idx++; + } + } + closedir(dirp); + + if (location_file) { + *p_location_file = location_file; + return PHONE_NUMBER_ERROR_NONE; + } + + return PHONE_NUMBER_ERROR_NO_DATA; +} + +int phn_location_get_location_from_extra_data(const char *file, const char *number, + char *region_str, char *lang_str, char **p_location) +{ + int ret = 0; + int city_str_len = 0; + int province_str_len = 0; + gchar *city_temp = NULL; + gchar *province_temp = NULL; + const char *real_number = number; + const gunichar2 *city_str = NULL; + const gunichar2 *province_str = NULL; + char file_path[PHN_STR_SHORT_LEN] = {0}; + + /* support region - CN, support lang - zh,en,ko */ + RETV_IF(NULL == region_str, PHONE_NUMBER_ERROR_INVALID_PARAMETER); + RETV_IF(NULL == lang_str, PHONE_NUMBER_ERROR_INVALID_PARAMETER); + + while (real_number && real_number[0] == '0') + real_number++; + RETVM_IF(NULL == real_number, PHONE_NUMBER_ERROR_INVALID_PARAMETER, "number=%s", + number); + + char lang_region[PHN_STR_SHORT_LEN] = {0}; + snprintf(lang_region, sizeof(lang_region), "%s_%s", lang_str, region_str); + + int lang_index = 0; + if (STRING_EQUAL == strcmp(lang_region, PHN_LOCATION_SUPPORT_ZH_CN)) { + lang_index = 0; + } else if (STRING_EQUAL == strcmp(lang_region, PHN_LOCATION_SUPPORT_EN_CN)) { + lang_index = 1; + } else if (STRING_EQUAL == strcmp(lang_region, PHN_LOCATION_SUPPORT_KO_CN)) { + lang_index = 2; + } else { + ERR("Not supported(%s)", lang_region); + return PHONE_NUMBER_ERROR_NOT_SUPPORTED; + } + + snprintf(file_path, sizeof(file_path), "%s/%s", PHN_LOCATION_DIR, file); + + int fd = open(file_path, O_RDONLY); + RETVM_IF(fd < 0, PHONE_NUMBER_ERROR_NOT_SUPPORTED, "open() Fail(%d)", errno); + + ret = lseek(fd, sizeof(int), SEEK_CUR); /* start_mark */ + if (ret <= 0) { + ERR("lseek() Fail(%d)", errno); + close(fd); + return PHONE_NUMBER_ERROR_NOT_SUPPORTED; + } + + struct phn_location_header header; + ret = read(fd, &header, sizeof(struct phn_location_header)); + if (ret <= 0) { + ERR("read() Fail(%d)", errno); + close(fd); + return PHONE_NUMBER_ERROR_NOT_SUPPORTED; + } + + struct phn_province_info { + gunichar2 name1[header.province_name_len[0]/2]; + gunichar2 name2[header.province_name_len[1]/2]; + gunichar2 name3[header.province_name_len[2]/2]; + }; + +#pragma pack(1) + struct phn_telephone_city_info { + gint8 province_index; + gunichar2 city1[header.telephone_city_len[0]/2]; + gunichar2 city2[header.telephone_city_len[1]/2]; + gunichar2 city3[header.telephone_city_len[2]/2]; + gint16 prefix; + }; + +#pragma pack(1) + struct phn_mobile_city_info { + gint8 province_index; + gunichar2 city1[header.mobile_city_len[0]/2]; + gunichar2 city2[header.mobile_city_len[1]/2]; + gunichar2 city3[header.mobile_city_len[2]/2]; + }; + + if (header.province_count <= 0) { + ERR("Invalid provice_count(%d)", header.province_count); + close(fd); + return PHONE_NUMBER_ERROR_NOT_SUPPORTED; + } + struct phn_province_info province_info[header.province_count]; + ret = read(fd, &province_info, sizeof(province_info)); + if (ret <= 0) { + ERR("read() Fail(%d)", errno); + close(fd); + return PHONE_NUMBER_ERROR_NOT_SUPPORTED; + } + + if (header.telephone_city_count <= 0) { + ERR("Invalid telephone_city_count(%d)", header.telephone_city_count); + close(fd); + return PHONE_NUMBER_ERROR_NOT_SUPPORTED; + } + struct phn_telephone_city_info telephone_city_info[header.telephone_city_count]; + ret = read(fd, &telephone_city_info, sizeof(telephone_city_info)); + if (ret <= 0) { + ERR("read() Fail(%d)", errno); + close(fd); + return PHONE_NUMBER_ERROR_NOT_SUPPORTED; + } + + int i; + int telephone_city_count = + sizeof(telephone_city_info) / sizeof(struct phn_telephone_city_info); + for (i = 0; i < telephone_city_count; i++) { + gint8 provice_idx; + gint16 prefix = telephone_city_info[i].prefix; + char prefix_str[PHN_STR_SHORT_LEN] = {0}; + + snprintf(prefix_str, sizeof(prefix_str), "%u", prefix); + if (0 == strncmp(real_number, prefix_str, strlen(prefix_str))) { + switch (lang_index) { + case 0: + city_str = telephone_city_info[i].city1; + city_str_len = header.telephone_city_len[0] / 2; + break; + case 1: + city_str = telephone_city_info[i].city2; + city_str_len = header.telephone_city_len[1] / 2; + break; + case 2: + city_str = telephone_city_info[i].city3; + city_str_len = header.telephone_city_len[2] / 2; + break; + default: + ERR("Invalid lang_index(%d)", lang_index); + break; + } + + provice_idx = telephone_city_info[i].province_index; + if (0 < provice_idx && provice_idx <= header.province_count) { + switch (lang_index) { + case 0: + province_str = province_info[provice_idx-1].name1; + province_str_len = header.province_name_len[0] / 2; + break; + case 1: + province_str = province_info[provice_idx-1].name2; + province_str_len = header.province_name_len[1] / 2; + break; + case 2: + province_str = province_info[provice_idx-1].name3; + province_str_len = header.province_name_len[2] / 2; + break; + default: + ERR("Invalid lang_index(%d)", lang_index); + break; + } + } + + province_temp = g_utf16_to_utf8(province_str, province_str_len, NULL, NULL, + NULL); + city_temp = g_utf16_to_utf8(city_str, city_str_len, NULL, NULL, NULL); + + if (city_temp && province_temp) { + int size = strlen(city_temp) + strlen(province_temp); + char *location = NULL; + location = calloc(size + 3, sizeof(char)); + if (location) + snprintf(location, size + 3, "%s, %s", city_temp, province_temp); + *p_location = location; + } else if (city_temp) { + int size = strlen(city_temp); + char *location = NULL; + location = calloc(size + 1, sizeof(char)); + if (location) + snprintf(location, size + 1, "%s", city_temp); + *p_location = location; + } + + close(fd); + free(city_temp); + free(province_temp); + if (NULL == p_location) + return PHONE_NUMBER_ERROR_NOT_SUPPORTED; + return PHONE_NUMBER_ERROR_NONE; + } + } + + if (strlen(number) < PHN_LOCATION_CHINA_MOBILE_NUMBER_MIN_LEN) { + ERR("Invalid number(%s)", number); + close(fd); + return PHONE_NUMBER_ERROR_INVALID_PARAMETER; + } + + if (header.mobile_city_count <= 0) { + ERR("Invalid mobile_city_count(%d)", header.mobile_city_count); + close(fd); + return PHONE_NUMBER_ERROR_NOT_SUPPORTED; + } + struct phn_mobile_city_info mobile_city_info[header.mobile_city_count]; + ret = read(fd, &mobile_city_info, sizeof(mobile_city_info)); + if (ret <= 0) { + ERR("read() Fail(%d)", errno); + close(fd); + return PHONE_NUMBER_ERROR_NOT_SUPPORTED; + } + + if (header.mobile_prefix_index_count <= 0) { + ERR("Invalid mobile_prefix_index_count(%d)", header.mobile_prefix_index_count); + close(fd); + return PHONE_NUMBER_ERROR_NOT_SUPPORTED; + } + gint16 mobile_prefix_info[header.mobile_prefix_index_count]; + ret = read(fd, &mobile_prefix_info, sizeof(mobile_prefix_info)); + if (ret <= 0) { + ERR("read() Fail(%d)", errno); + close(fd); + return PHONE_NUMBER_ERROR_NOT_SUPPORTED; + } + + char num_prefix_str[PHN_LOCATION_CHINA_MOBILE_PREFIX_LEN + 1] = {0}; + char num_suffix_str[PHN_LOCATION_CHINA_MOBILE_SUFFIX_LEN + 1] = {0}; + snprintf(num_prefix_str, sizeof(num_prefix_str), "%s", number); + snprintf(num_suffix_str, sizeof(num_suffix_str), "%s", number + 3); + int num_prefix = atoi(num_prefix_str); + int num_suffix = atoi(num_suffix_str); + + int mobile_prefix_index_count = sizeof(mobile_prefix_info) / sizeof(gint16); + for (i = 0; i < mobile_prefix_index_count; i++) { + if (num_prefix == mobile_prefix_info[i]) { + gint16 mobile_prefix = 0; + ret = lseek(fd, (PHN_LOCATION_CHINA_MOBILE_SUFFIX_OFFSET * sizeof(gint16) * i) + + (num_suffix * sizeof(gint16)), SEEK_CUR); + ret = read(fd, &mobile_prefix, sizeof(gint16)); + WARN_IF(ret < 0, "read() Fail(%d)", errno); + if (0 == mobile_prefix) { + ERR("Invalid value:mobile_prefix is 0"); + continue; + } + + switch (lang_index) { + case 0: + city_str = mobile_city_info[mobile_prefix - 1].city1; + city_str_len = header.mobile_city_len[0] / 2; + break; + case 1: + city_str = mobile_city_info[mobile_prefix - 1].city2; + city_str_len = header.mobile_city_len[1] / 2; + break; + case 2: + city_str = mobile_city_info[mobile_prefix - 1].city3; + city_str_len = header.mobile_city_len[2] / 2; + break; + default: + ERR("Invalid lang_index(%d)", lang_index); + break; + } + + int province_index = mobile_city_info[mobile_prefix - 1].province_index; + + if (0 < province_index && province_index <= header.province_count) { + switch (lang_index) { + case 0: + province_str = province_info[province_index - 1].name1; + province_str_len = header.province_name_len[0] / 2; + break; + case 1: + province_str = province_info[province_index - 1].name2; + province_str_len = header.province_name_len[1] / 2; + break; + case 2: + province_str = province_info[province_index - 1].name3; + province_str_len = header.province_name_len[2] / 2; + break; + default: + ERR("Invalid lang_index(%d)", lang_index); + break; + } + } + + province_temp = g_utf16_to_utf8(province_str, province_str_len, NULL, NULL, + NULL); + city_temp = g_utf16_to_utf8(city_str, city_str_len, NULL, NULL, NULL); + + if (city_temp && province_temp) { + int size = strlen(city_temp) + strlen(province_temp); + char *location = NULL; + location = calloc(size + 3, sizeof(char)); + if (location) + snprintf(location, size + 3, "%s, %s", city_temp, province_temp); + *p_location = location; + } else if (city_temp) { + int size = strlen(city_temp); + char *location = NULL; + location = calloc(size + 1, sizeof(char)); + if (location) + snprintf(location, size + 1, "%s", city_temp); + *p_location = location; + } + close(fd); + free(city_temp); + free(province_temp); + if (NULL == p_location) + return PHONE_NUMBER_ERROR_NOT_SUPPORTED; + return PHONE_NUMBER_ERROR_NONE; + } + } + + close(fd); + return PHONE_NUMBER_ERROR_NONE; +} + diff --git a/daemon/phnd-location.h b/daemon/phnd-location.h new file mode 100644 index 0000000..bf7b18e --- /dev/null +++ b/daemon/phnd-location.h @@ -0,0 +1,32 @@ +/* + * Phonenumber Utils + * + * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +#ifndef __PHN_LOCATION_H__ +#define __PHN_LOCATION_H__ + +#include "phone_number_types.h" + +#define PHN_LOCATION_SUPPORT_ZH_CN "zh_CN" +#define PHN_LOCATION_SUPPORT_EN_CN "en_CN" +#define PHN_LOCATION_SUPPORT_KO_CN "ko_CN" + +int phn_location_find_extra_data(char *region_str, char **p_location_file); +int phn_location_get_location_from_extra_data(const char *file, const char *number, + char *region_str, char *lang_str, char **p_location); + +#endif /* __PHN_LOCATION_H__ */ diff --git a/daemon/phnd-region-data.c b/daemon/phnd-region-data.c new file mode 100644 index 0000000..2b57efe --- /dev/null +++ b/daemon/phnd-region-data.c @@ -0,0 +1,350 @@ +/* + * Phonenumber Utils + * + * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +#include +#include +#include + +#include "phn-log.h" +#include "phn-common.h" +#include "phnd-region-data.h" + +struct phn_lang_info { + const char *lang_str; + phone_number_lang_e lang; +}; + +struct phn_match_info { + phone_number_region_e region; + phone_number_lang_e lang; +}; + +struct phn_region_info { + const char *region_str; + phone_number_region_e region; +}; + +const struct phn_match_info phn_match_info_table[] = { + {PHONE_NUMBER_REGION_WESTERN_SAHARA, PHONE_NUMBER_LANG_FRENCH}, + {PHONE_NUMBER_REGION_MOROCCO, PHONE_NUMBER_LANG_FRENCH}, + {PHONE_NUMBER_REGION_MAURITANIA, PHONE_NUMBER_LANG_FRENCH}, + {PHONE_NUMBER_REGION_COTE_D_IVOIRE, PHONE_NUMBER_LANG_FRENCH}, + {PHONE_NUMBER_REGION_TOGO, PHONE_NUMBER_LANG_SPANISH}, + {PHONE_NUMBER_REGION_TOGO, PHONE_NUMBER_LANG_FRENCH}, + {PHONE_NUMBER_REGION_BENIN, PHONE_NUMBER_LANG_FRENCH}, + {PHONE_NUMBER_REGION_MAURITIUS, PHONE_NUMBER_LANG_SPANISH}, + {PHONE_NUMBER_REGION_MAURITIUS, PHONE_NUMBER_LANG_FRENCH}, + {PHONE_NUMBER_REGION_CABO_VERDE, PHONE_NUMBER_LANG_PORTUGUESE}, + {PHONE_NUMBER_REGION_SAO_TOME_AND_PRINCIPE, PHONE_NUMBER_LANG_PORTUGUESE}, + {PHONE_NUMBER_REGION_CONGO, PHONE_NUMBER_LANG_FRENCH}, + {PHONE_NUMBER_REGION_DEMOCRATIC_REPUBLIC_OF_THE_CONGO, PHONE_NUMBER_LANG_FRENCH}, + {PHONE_NUMBER_REGION_ANGOLA, PHONE_NUMBER_LANG_PORTUGUESE}, + {PHONE_NUMBER_REGION_GUINEA_BISSAU, PHONE_NUMBER_LANG_PORTUGUESE}, + {PHONE_NUMBER_REGION_MOZAMBIQUE, PHONE_NUMBER_LANG_PORTUGUESE}, + {PHONE_NUMBER_REGION_COMOROS, PHONE_NUMBER_LANG_FRENCH}, + {PHONE_NUMBER_REGION_SAINT_HELENA, PHONE_NUMBER_LANG_FRENCH}, + {PHONE_NUMBER_REGION_TRISTAN_DA_CUNHA, PHONE_NUMBER_LANG_FRENCH}, + {PHONE_NUMBER_REGION_GREECE, PHONE_NUMBER_LANG_GREEK}, + {PHONE_NUMBER_REGION_NETHERLANDS, PHONE_NUMBER_LANG_DUTCH}, + {PHONE_NUMBER_REGION_BELGIUM, PHONE_NUMBER_LANG_GERMAN}, + {PHONE_NUMBER_REGION_BELGIUM, PHONE_NUMBER_LANG_FRENCH}, + {PHONE_NUMBER_REGION_BELGIUM, PHONE_NUMBER_LANG_DUTCH}, + {PHONE_NUMBER_REGION_FRANCE, PHONE_NUMBER_LANG_FRENCH}, + {PHONE_NUMBER_REGION_SPAIN, PHONE_NUMBER_LANG_SPANISH}, + {PHONE_NUMBER_REGION_PORTUGAL, PHONE_NUMBER_LANG_PORTUGUESE}, + {PHONE_NUMBER_REGION_LUXEMBOURG, PHONE_NUMBER_LANG_GERMAN}, + {PHONE_NUMBER_REGION_LUXEMBOURG, PHONE_NUMBER_LANG_FRENCH}, + {PHONE_NUMBER_REGION_ALAND_ISLANDS, PHONE_NUMBER_LANG_FINNISH}, + {PHONE_NUMBER_REGION_ALAND_ISLANDS, PHONE_NUMBER_LANG_NORTHERN_SAMI}, + {PHONE_NUMBER_REGION_FINLAND, PHONE_NUMBER_LANG_FINNISH}, + {PHONE_NUMBER_REGION_BULGARIA, PHONE_NUMBER_LANG_BULGARIAN}, + {PHONE_NUMBER_REGION_HUNGARY, PHONE_NUMBER_LANG_HUNGARIAN}, + {PHONE_NUMBER_REGION_REPUBLIC_OF_MOLDOVA, PHONE_NUMBER_LANG_ROMANIAN}, + {PHONE_NUMBER_REGION_REPUBLIC_OF_MOLDOVA, PHONE_NUMBER_LANG_RUSSIAN}, + {PHONE_NUMBER_REGION_ARMENIA, PHONE_NUMBER_LANG_AMHARIC}, + {PHONE_NUMBER_REGION_ARMENIA, PHONE_NUMBER_LANG_RUSSIAN}, + {PHONE_NUMBER_REGION_BELARUS, PHONE_NUMBER_LANG_BELARUSIAN}, + {PHONE_NUMBER_REGION_BELARUS, PHONE_NUMBER_LANG_RUSSIAN}, + {PHONE_NUMBER_REGION_SERBIA, PHONE_NUMBER_LANG_SERBIAN}, + {PHONE_NUMBER_REGION_ITALY, PHONE_NUMBER_LANG_ITALIAN}, + {PHONE_NUMBER_REGION_ROMANIA, PHONE_NUMBER_LANG_ROMANIAN}, + {PHONE_NUMBER_REGION_SWITZERLAND, PHONE_NUMBER_LANG_GERMAN}, + {PHONE_NUMBER_REGION_SWITZERLAND, PHONE_NUMBER_LANG_FRENCH}, + {PHONE_NUMBER_REGION_SWITZERLAND, PHONE_NUMBER_LANG_ITALIAN}, + {PHONE_NUMBER_REGION_AUSTRIA, PHONE_NUMBER_LANG_GERMAN}, + {PHONE_NUMBER_REGION_SWEDEN, PHONE_NUMBER_LANG_SWEDISH}, + {PHONE_NUMBER_REGION_POLAND, PHONE_NUMBER_LANG_POLISH}, + {PHONE_NUMBER_REGION_GERMANY, PHONE_NUMBER_LANG_GERMAN}, + {PHONE_NUMBER_REGION_BRAZIL, PHONE_NUMBER_LANG_PORTUGUESE}, + {PHONE_NUMBER_REGION_CHILE, PHONE_NUMBER_LANG_SPANISH}, + {PHONE_NUMBER_REGION_COLOMBIA, PHONE_NUMBER_LANG_SPANISH}, + {PHONE_NUMBER_REGION_VENEZUELA, PHONE_NUMBER_LANG_SPANISH}, + {PHONE_NUMBER_REGION_INDONESIA, PHONE_NUMBER_LANG_INDONESIAN}, + {PHONE_NUMBER_REGION_THAILAND, PHONE_NUMBER_LANG_THAI}, + {PHONE_NUMBER_REGION_KAZAKHSTAN, PHONE_NUMBER_LANG_RUSSIAN}, + {PHONE_NUMBER_REGION_RUSSIAN_FEDERATION, PHONE_NUMBER_LANG_RUSSIAN}, + {PHONE_NUMBER_REGION_JAPAN, PHONE_NUMBER_LANG_JAPANESE}, + {PHONE_NUMBER_REGION_REPUBLIC_OF_KOREA, PHONE_NUMBER_LANG_ARABIC}, + {PHONE_NUMBER_REGION_REPUBLIC_OF_KOREA, PHONE_NUMBER_LANG_CZECH}, + {PHONE_NUMBER_REGION_REPUBLIC_OF_KOREA, PHONE_NUMBER_LANG_FRENCH}, + {PHONE_NUMBER_REGION_REPUBLIC_OF_KOREA, PHONE_NUMBER_LANG_KOREAN}, + {PHONE_NUMBER_REGION_REPUBLIC_OF_KOREA, PHONE_NUMBER_LANG_CHINESE}, + {PHONE_NUMBER_REGION_REPUBLIC_OF_KOREA, PHONE_NUMBER_LANG_CHINESE_TRADITIONAL}, + {PHONE_NUMBER_REGION_VIET_NAM, PHONE_NUMBER_LANG_VIETNAMESE}, + {PHONE_NUMBER_REGION_CHINA, PHONE_NUMBER_LANG_CHINESE}, + {PHONE_NUMBER_REGION_CHINA, PHONE_NUMBER_LANG_CHINESE_TRADITIONAL}, + {PHONE_NUMBER_REGION_TAIWAN_PROVINCE_OF_CHINA, PHONE_NUMBER_LANG_CHINESE}, + {PHONE_NUMBER_REGION_TAIWAN_PROVINCE_OF_CHINA, PHONE_NUMBER_LANG_CHINESE_TRADITIONAL}, + {PHONE_NUMBER_REGION_TURKEY, PHONE_NUMBER_LANG_TURKISH}, + {PHONE_NUMBER_REGION_SAUDI_ARABIA, PHONE_NUMBER_LANG_ARABIC}, + {PHONE_NUMBER_REGION_ISLAMIC_REPUBLIC_OF_IRAN, PHONE_NUMBER_LANG_PERSIAN}, + {PHONE_NUMBER_REGION_SYSTEM, PHONE_NUMBER_LANG_SYSTEM}, +}; + +const struct phn_lang_info phn_lang_info_table[] = { + {"am", PHONE_NUMBER_LANG_AMHARIC}, + {"ar", PHONE_NUMBER_LANG_ARABIC}, + {"be", PHONE_NUMBER_LANG_BELARUSIAN}, + {"bg", PHONE_NUMBER_LANG_BULGARIAN}, + {"zh", PHONE_NUMBER_LANG_CHINESE}, + {"zh_Hant", PHONE_NUMBER_LANG_CHINESE_TRADITIONAL}, + {"cs", PHONE_NUMBER_LANG_CZECH}, + {"nl", PHONE_NUMBER_LANG_DUTCH}, + {"en", PHONE_NUMBER_LANG_ENGLISH}, + {"fi", PHONE_NUMBER_LANG_FINNISH}, + {"fr", PHONE_NUMBER_LANG_FRENCH}, + {"de", PHONE_NUMBER_LANG_GERMAN}, + {"el", PHONE_NUMBER_LANG_GREEK}, + {"hu", PHONE_NUMBER_LANG_HUNGARIAN}, + {"id", PHONE_NUMBER_LANG_INDONESIAN}, + {"it", PHONE_NUMBER_LANG_ITALIAN}, + {"ja", PHONE_NUMBER_LANG_JAPANESE}, + {"ko", PHONE_NUMBER_LANG_KOREAN}, + {"se", PHONE_NUMBER_LANG_NORTHERN_SAMI}, + {"fa", PHONE_NUMBER_LANG_PERSIAN}, + {"pl", PHONE_NUMBER_LANG_POLISH}, + {"pt", PHONE_NUMBER_LANG_PORTUGUESE}, + {"ro", PHONE_NUMBER_LANG_ROMANIAN}, + {"ru", PHONE_NUMBER_LANG_RUSSIAN}, + {"sr", PHONE_NUMBER_LANG_SERBIAN}, + {"se", PHONE_NUMBER_LANG_SPANISH}, + {"sv", PHONE_NUMBER_LANG_SWEDISH}, + {"th", PHONE_NUMBER_LANG_THAI}, + {"tr", PHONE_NUMBER_LANG_TURKISH}, + {"vi", PHONE_NUMBER_LANG_VIETNAMESE}, +}; + +const struct phn_region_info phn_region_info_table[] = { + {"AX", PHONE_NUMBER_REGION_ALAND_ISLANDS}, + {"AL", PHONE_NUMBER_REGION_ALBANIA}, + {"DZ", PHONE_NUMBER_REGION_ALGERIA}, + {"AS", PHONE_NUMBER_REGION_AMERICAN_SAMOA}, + {"AO", PHONE_NUMBER_REGION_ANGOLA}, + {"AI", PHONE_NUMBER_REGION_ANGUILLA}, + {"AR", PHONE_NUMBER_REGION_ARGENTINA}, + {"AM", PHONE_NUMBER_REGION_ARMENIA}, + {"AC", PHONE_NUMBER_REGION_ASCENSION_ISLAND}, + {"AG", PHONE_NUMBER_REGION_ATIGUA_AND_BARBUDA}, + {"AT", PHONE_NUMBER_REGION_AUSTRIA}, + {"BS", PHONE_NUMBER_REGION_BAHAMAS}, + {"BB", PHONE_NUMBER_REGION_BARBADOS}, + {"BY", PHONE_NUMBER_REGION_BELARUS}, + {"BE", PHONE_NUMBER_REGION_BELGIUM}, + {"BJ", PHONE_NUMBER_REGION_BENIN}, + {"BM", PHONE_NUMBER_REGION_BERMUDA}, + {"BQ", PHONE_NUMBER_REGION_BONAIRE_SINT_EUSTATIUS_AND_SABA}, + {"BW", PHONE_NUMBER_REGION_BOTSWANA}, + {"BR", PHONE_NUMBER_REGION_BRAZIL}, + {"BG", PHONE_NUMBER_REGION_BULGARIA}, + {"BF", PHONE_NUMBER_REGION_BURKINA_FASO}, + {"BI", PHONE_NUMBER_REGION_BURUNDI}, + {"CV", PHONE_NUMBER_REGION_CABO_VERDE}, + {"CM", PHONE_NUMBER_REGION_CAMEROON}, + {"CA", PHONE_NUMBER_REGION_CANADA}, + {"KY", PHONE_NUMBER_REGION_CAYMAN_ISLAND}, + {"CL", PHONE_NUMBER_REGION_CHILE}, + {"CN", PHONE_NUMBER_REGION_CHINA}, + {"CO", PHONE_NUMBER_REGION_COLOMBIA}, + {"KM", PHONE_NUMBER_REGION_COMOROS}, + {"CG", PHONE_NUMBER_REGION_CONGO}, + {"CI", PHONE_NUMBER_REGION_COTE_D_IVOIRE}, + {"CU", PHONE_NUMBER_REGION_CUBA}, + {"CW", PHONE_NUMBER_REGION_CURACAO}, + {"CZ", PHONE_NUMBER_REGION_CZECH_REPUBLIC}, + {"CD", PHONE_NUMBER_REGION_DEMOCRATIC_REPUBLIC_OF_THE_CONGO}, + {"DM", PHONE_NUMBER_REGION_DOMINICA}, + {"EG", PHONE_NUMBER_REGION_EGYPT}, + {"EE", PHONE_NUMBER_REGION_ESTONIA}, + {"ET", PHONE_NUMBER_REGION_ETHIOPIA}, + {"FI", PHONE_NUMBER_REGION_FINLAND}, + {"FR", PHONE_NUMBER_REGION_FRANCE}, + {"GA", PHONE_NUMBER_REGION_GABON}, + {"GM", PHONE_NUMBER_REGION_GAMBIA}, + {"DE", PHONE_NUMBER_REGION_GERMANY}, + {"GH", PHONE_NUMBER_REGION_GHANA}, + {"GR", PHONE_NUMBER_REGION_GREECE}, + {"GL", PHONE_NUMBER_REGION_GREENLAND}, + {"GD", PHONE_NUMBER_REGION_GRENADA}, + {"GU", PHONE_NUMBER_REGION_GUAM}, + {"GG", PHONE_NUMBER_REGION_GUERNSEY}, + {"GN", PHONE_NUMBER_REGION_GUINEA}, + {"GW", PHONE_NUMBER_REGION_GUINEA_BISSAU}, + {"HU", PHONE_NUMBER_REGION_HUNGARY}, + {"IS", PHONE_NUMBER_REGION_ICELAND}, + {"IN", PHONE_NUMBER_REGION_INDIA}, + {"ID", PHONE_NUMBER_REGION_INDONESIA}, + {"IE", PHONE_NUMBER_REGION_IRELAND}, + {"IR", PHONE_NUMBER_REGION_ISLAMIC_REPUBLIC_OF_IRAN}, + {"IM", PHONE_NUMBER_REGION_ISLE_OF_MAN}, + {"IT", PHONE_NUMBER_REGION_ITALY}, + {"JM", PHONE_NUMBER_REGION_JAMAICA}, + {"JP", PHONE_NUMBER_REGION_JAPAN}, + {"JE", PHONE_NUMBER_REGION_JERSEY}, + {"JO", PHONE_NUMBER_REGION_JORDAN}, + {"KZ", PHONE_NUMBER_REGION_KAZAKHSTAN}, + {"KE", PHONE_NUMBER_REGION_KENYA}, + {"KI", PHONE_NUMBER_REGION_KIRIBATI}, + {"LV", PHONE_NUMBER_REGION_LATVIA}, + {"LS", PHONE_NUMBER_REGION_LESOTHO}, + {"LT", PHONE_NUMBER_REGION_LITHUANIA }, + {"LU", PHONE_NUMBER_REGION_LUXEMBOURG}, + {"MG", PHONE_NUMBER_REGION_MADAGASCAR}, + {"MR", PHONE_NUMBER_REGION_MAURITANIA}, + {"MU", PHONE_NUMBER_REGION_MAURITIUS }, + {"DO", PHONE_NUMBER_REGION_DOMINICAN_REPUBLIC }, + {"MS", PHONE_NUMBER_REGION_MONTSERRAT}, + {"MA", PHONE_NUMBER_REGION_MOROCCO}, + {"MZ", PHONE_NUMBER_REGION_MOZAMBIQUE}, + {"NA", PHONE_NUMBER_REGION_NAMIBIA}, + {"NL", PHONE_NUMBER_REGION_NETHERLANDS}, + {"NG", PHONE_NUMBER_REGION_NIGERIA}, + {"MP", PHONE_NUMBER_REGION_NORTHERN_MARIANA_ISLANDS}, + {"NO", PHONE_NUMBER_REGION_NORWAY}, + {"PE", PHONE_NUMBER_REGION_PERU}, + {"PL", PHONE_NUMBER_REGION_POLAND}, + {"PT", PHONE_NUMBER_REGION_PORTUGAL}, + {"PR", PHONE_NUMBER_REGION_PUERTO_RICO}, + {"KR", PHONE_NUMBER_REGION_REPUBLIC_OF_KOREA}, + {"MD", PHONE_NUMBER_REGION_REPUBLIC_OF_MOLDOVA}, + {"RO", PHONE_NUMBER_REGION_ROMANIA}, + {"RU", PHONE_NUMBER_REGION_RUSSIAN_FEDERATION}, + {"SH", PHONE_NUMBER_REGION_SAINT_HELENA}, + {"KN", PHONE_NUMBER_REGION_SAINT_KITTS_AND_NEVIS}, + {"LC", PHONE_NUMBER_REGION_SAINT_LUCIA}, + {"VC", PHONE_NUMBER_REGION_SAINT_VINCENT_AND_THE_GRENADINES}, + {"ST", PHONE_NUMBER_REGION_SAO_TOME_AND_PRINCIPE}, + {"SA", PHONE_NUMBER_REGION_SAUDI_ARABIA}, + {"SN", PHONE_NUMBER_REGION_SENEGAL}, + {"RS", PHONE_NUMBER_REGION_SERBIA}, + {"SL", PHONE_NUMBER_REGION_SIERRA_LEONE}, + {"SX", PHONE_NUMBER_REGION_SINT_MAARTEN}, + {"SK", PHONE_NUMBER_REGION_SLOVAKIA}, + {"SO", PHONE_NUMBER_REGION_SOMALIA}, + {"ZA", PHONE_NUMBER_REGION_SOUTH_AFRICA}, + {"ES", PHONE_NUMBER_REGION_SPAIN}, + {"LK", PHONE_NUMBER_REGION_SRI_LANKA}, + {"SD", PHONE_NUMBER_REGION_SUDAN}, + {"SJ", PHONE_NUMBER_REGION_SVALBARD_AND_JAN_MAYEN}, + {"SZ", PHONE_NUMBER_REGION_SWAZILAND}, + {"SE", PHONE_NUMBER_REGION_SWEDEN}, + {"CH", PHONE_NUMBER_REGION_SWITZERLAND}, + {"TW", PHONE_NUMBER_REGION_TAIWAN_PROVINCE_OF_CHINA}, + {"TH", PHONE_NUMBER_REGION_THAILAND}, + {"MK", PHONE_NUMBER_REGION_THE_FORMER_YUGOSLAV_REPUBLIC_OF_MACEDONIA}, + {"TG", PHONE_NUMBER_REGION_TOGO}, + {"TT", PHONE_NUMBER_REGION_TRINIDAD_AND_TOBAGO}, + {"TA", PHONE_NUMBER_REGION_TRISTAN_DA_CUNHA}, + {"TN", PHONE_NUMBER_REGION_TUNISIA}, + {"TR", PHONE_NUMBER_REGION_TURKEY}, + {"TC", PHONE_NUMBER_REGION_TURKS_AND_CAICOS_ISLANDS}, + {"UG", PHONE_NUMBER_REGION_UGANDA}, + {"GB", PHONE_NUMBER_REGION_UNITED_KINGDOM}, + {"US", PHONE_NUMBER_REGION_UNITED_STATES_OF_AMERICA}, + {"VE", PHONE_NUMBER_REGION_VENEZUELA}, + {"VN", PHONE_NUMBER_REGION_VIET_NAM}, + {"VG", PHONE_NUMBER_REGION_VIRGIN_ISLAND_BRITISH}, + {"VI", PHONE_NUMBER_REGION_VIRGIN_ISLAND_US}, + {"EH", PHONE_NUMBER_REGION_WESTERN_SAHARA}, + {"ZM", PHONE_NUMBER_REGION_ZAMBIA}, + {"ZW", PHONE_NUMBER_REGION_ZIMBABWE}, +}; + +int phn_region_data_get_region_str(phone_number_region_e region, char **region_str) +{ + int ret, i; + + if (PHONE_NUMBER_REGION_SYSTEM == region) { + char *str = NULL; + ret = system_settings_get_value_string(SYSTEM_SETTINGS_KEY_LOCALE_COUNTRY, &str); + if (str) + *region_str = g_strdup(strchr(str, '_') + 1); + free(str); + if (SYSTEM_SETTINGS_ERROR_NONE != ret) { + ERR("system_settings_get_value_string() Fail(%d)", ret); + return PHONE_NUMBER_ERROR_NOT_SUPPORTED; + } + return PHONE_NUMBER_ERROR_NONE; + } + + for (i = 0; i < sizeof(phn_region_info_table)/sizeof(struct phn_region_info); i++) { + if (phn_region_info_table[i].region == region) { + *region_str = g_strdup(phn_region_info_table[i].region_str); + return PHONE_NUMBER_ERROR_NONE; + } + } + return PHONE_NUMBER_ERROR_NOT_SUPPORTED; +} + +int phn_region_data_get_lang_str(phone_number_lang_e lang, char **lang_str) +{ + int ret, i; + + if (PHONE_NUMBER_LANG_SYSTEM == lang) { + char *str = NULL; + ret = system_settings_get_value_string(SYSTEM_SETTINGS_KEY_LOCALE_LANGUAGE, &str); + if (str) + *lang_str = g_strdup(strtok(str, "_")); + free(str); + if (SYSTEM_SETTINGS_ERROR_NONE != ret) { + ERR("system_settings_get_value_string() Fail(%d)", ret); + return PHONE_NUMBER_ERROR_NOT_SUPPORTED; + } + return PHONE_NUMBER_ERROR_NONE; + } + + for (i = 0; i < sizeof(phn_lang_info_table)/sizeof(struct phn_lang_info); i++) { + if (phn_lang_info_table[i].lang == lang) { + *lang_str = g_strdup(phn_lang_info_table[i].lang_str); + return PHONE_NUMBER_ERROR_NONE; + } + } + return PHONE_NUMBER_ERROR_NOT_SUPPORTED; +} + +bool phn_region_data_find_match_info(phone_number_region_e region, + phone_number_lang_e lang) +{ + int i; + for (i = 0; i < sizeof(phn_match_info_table)/sizeof(struct phn_match_info); i++) { + if (phn_match_info_table[i].region == region + && phn_match_info_table[i].lang == lang) { + return true; + } + } + return false; +} diff --git a/daemon/phnd-region-data.h b/daemon/phnd-region-data.h new file mode 100644 index 0000000..9580018 --- /dev/null +++ b/daemon/phnd-region-data.h @@ -0,0 +1,32 @@ +/* + * Phonenumber Utils + * + * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +#ifndef __PHN_REGION_DATA_H__ +#define __PHN_REGION_DATA_H__ + +#include +#include "phone_number_types.h" + +#define PHN_REGION_DEFAULT_LANG "en" + +int phn_region_data_get_region_str(phone_number_region_e region, char **region_str); +int phn_region_data_get_lang_str(phone_number_lang_e lang, char **lang_str); +bool phn_region_data_find_match_info(phone_number_region_e region, + phone_number_lang_e lang); + +#endif /* __PHN_REGION_DATA_H__ */ diff --git a/daemon/phnd-utils.c b/daemon/phnd-utils.c new file mode 100644 index 0000000..cea3f9c --- /dev/null +++ b/daemon/phnd-utils.c @@ -0,0 +1,56 @@ +/* + * Phonenumber Utils + * + * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include +#include "phn-log.h" +#include "phnd.h" +#include "phnd-utils.h" + +#define PHN_DAEMON_DEFAULT_TIMEOUT 90 + +static int _phnd_timeout_sec = PHN_DAEMON_DEFAULT_TIMEOUT; +static guint _phnd_timeout_handle = 0; + +static gboolean _phnd_timeout_cb(gpointer user_data) +{ + phnd_daemon_quit(); + return FALSE; +} + +void phnd_utils_start_timeout() +{ + DBG("start timeout = %d", _phnd_timeout_sec); + if (_phnd_timeout_sec < 1) + return; + + if (_phnd_timeout_handle) + g_source_remove(_phnd_timeout_handle); + + _phnd_timeout_handle = g_timeout_add_seconds(_phnd_timeout_sec, _phnd_timeout_cb, NULL); +} + +void phnd_utils_stop_timeout() +{ + if (_phnd_timeout_sec < 1) + return; + + if (_phnd_timeout_handle) + g_source_remove(_phnd_timeout_handle); + _phnd_timeout_handle = 0; +} diff --git a/daemon/phnd-utils.h b/daemon/phnd-utils.h new file mode 100644 index 0000000..71692b2 --- /dev/null +++ b/daemon/phnd-utils.h @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef __PHONENUMBER_UTILS_DAEMON_UTIL_H__ +#define __PHONENUMBER_UTILS_DAEMON_UTIL_H__ + +void phnd_utils_start_timeout(); +void phnd_utils_stop_timeout(); + +#endif /*__PHONENUMBER_UTILS_DAEMON_UTIL_H__*/ + diff --git a/daemon/phnd.c b/daemon/phnd.c new file mode 100644 index 0000000..488b52f --- /dev/null +++ b/daemon/phnd.c @@ -0,0 +1,57 @@ +/* + * Phonenumber Utils + * + * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +#include +#include +#include + +#include "phnd.h" +#include "phn-common.h" +#include "phn-log.h" +#include "phnd-dbus.h" + +static GMainLoop *_main_loop; + +void phnd_daemon_quit() +{ + INFO("phonenumber-utils daemon is quit by timeout."); + g_main_loop_quit(_main_loop); + _main_loop = NULL; +} + + +int main(int argc, char **argv) +{ + guint id; + + INFO("start phonenumber utils daemon"); + + _main_loop = g_main_loop_new(NULL, FALSE); + + id = phnd_dbus_init(); + + g_main_loop_run(_main_loop); + + phnd_dbus_deinit(id); + g_main_loop_unref(_main_loop); + + return 0; +} + + + diff --git a/daemon/phnd.h b/daemon/phnd.h new file mode 100644 index 0000000..8e343cb --- /dev/null +++ b/daemon/phnd.h @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef __PHONENUMBER_UTILS_DAEMON_H__ +#define __PHONENUMBER_UTILS_DAEMON_H__ + +#define PHN_DAEMON_LOG + +void phnd_daemon_quit(); + +#endif /*__PHONENUMBER_UTILS_DAEMON_H__*/ + diff --git a/include/phone_number.h b/include/phone_number.h index 67dd815..d8df935 100644 --- a/include/phone_number.h +++ b/include/phone_number.h @@ -38,11 +38,45 @@ extern "C" */ /** - * @brief This function gets the location string from number, region and language. + * @brief Connects to the phonenumber-utils service. + * + * @since_tizen 3.0 + * + * @return @c 0 on success, + * otherwise a negative error value + * + * @retval #PHONE_NUMBER_ERROR_NONE Successful + * @retval #PHONE_NUMBER_ERROR_SYSTEM Internal error + * + * @see phone_number_disconnect() + */ +int phone_number_connect(void); + + +/** + * @brief Disconnects from the phonenumber-utils service. + * + * @since_tizen 3.0 + * + * @return @c 0 on success, + * otherwise a negative error value + * + * @retval #PHONE_NUMBER_ERROR_NONE Successful + * @retval #PHONE_NUMBER_ERROR_SYSTEM Internal error + * + * @see phone_number_connect() + */ +int phone_number_disconnect(void); + + +/** + * @brief Gets the location string from number, region and language. * * @since_tizen 2.4 * * @remarks You must release @a location using free(). + * @remarks The phonenumber-utils is changed to client/server architecture for performance improvement since 3.0. + * phone_number_connect()/phone_number_disconnect() should be called. * * @param[in] number The number * @param[in] region The region of number @@ -60,16 +94,21 @@ extern "C" * @retval #PHONE_NUMBER_ERROR_NOT_SUPPORTED Not supported * @retval #PHONE_NUMBER_ERROR_NO_DATA Requested data does not exist * + * @pre phone_number_connect() should be called to open a connection to the phonenumber-utils service. (Since 3.0) + * @post phone_number_disconnect() should be called to close a connection to the phonenumber-utils service. (Since 3.0) + * */ int phone_number_get_location_from_number(const char *number, phone_number_region_e region, phone_number_lang_e language, char **location); /** - * @brief This function gets the formatted number. + * @brief Gets the formatted number. * * @since_tizen 2.4 * * @remarks You must release @a formatted_number using free(). + * @remarks The phonenumber-utils is changed to client/server architecture for performance improvement since 3.0. + * phone_number_connect()/phone_number_disconnect() should be called. * * @param[in] number The number * @param[in] region The region of number @@ -86,6 +125,9 @@ int phone_number_get_location_from_number(const char *number, * @retval #PHONE_NUMBER_ERROR_NOT_SUPPORTED Not supported * @retval #PHONE_NUMBER_ERROR_NO_DATA Requested data does not exist * + * @pre phone_number_connect() should be called to open a connection to the phonenumber-utils service. (Since 3.0) + * @post phone_number_disconnect() should be called to close a connection to the phonenumber-utils service. (Since 3.0) + * */ int phone_number_get_formatted_number(const char *number, phone_number_region_e region, char **formatted_number); @@ -100,6 +142,8 @@ int phone_number_get_formatted_number(const char *number, phone_number_region_e * @privilege %http://tizen.org/privilege/telephony * * @remarks You must release @a normalized_number using free(). + * @remarks The phonenumber-utils is changed to client/server architecture for performance improvement since 3.0. + * phone_number_connect()/phone_number_disconnect() should be called. * * @param[in] number The number * @param[out] normalized_number The normalized number string to be returned @@ -113,7 +157,10 @@ int phone_number_get_formatted_number(const char *number, phone_number_region_e * @retval #PHONE_NUMBER_ERROR_INVALID_PARAMETER Invalid parameter * @retval #PHONE_NUMBER_ERROR_PERMISSION_DENIED Permission denied * @retval #PHONE_NUMBER_ERROR_NO_DATA Requested data does not exist - * @retval #PHONE_NUMBER_ERROR_SYSTEM Error from another modules + * @retval #PHONE_NUMBER_ERROR_SYSTEM Internal error + * + * @pre phone_number_connect() should be called to open a connection to the phonenumber-utils service. (Since 3.0) + * @post phone_number_disconnect() should be called to close a connection to the phonenumber-utils service. (Since 3.0) * */ int phone_number_get_normalized_number(const char *number, char **normalized_number); diff --git a/packaging/phonenumber-utils-test.manifest b/packaging/phonenumber-utils-test.manifest new file mode 100644 index 0000000..97e8c31 --- /dev/null +++ b/packaging/phonenumber-utils-test.manifest @@ -0,0 +1,5 @@ + + + + + diff --git a/packaging/phonenumber-utils.service b/packaging/phonenumber-utils.service new file mode 100644 index 0000000..9ca1615 --- /dev/null +++ b/packaging/phonenumber-utils.service @@ -0,0 +1,12 @@ +[Unit] +Description=Phonenumber Util +After=dbus.socket dbus.service +Requires=dbus.socket + +[Service] +Type=dbus +BusName=org.tizen.PhonenumberUtils.dbus +ExecStart=/usr/bin/phonenumber-utils-daemon + +[Install] +WantedBy=multi-user.target diff --git a/packaging/phonenumber-utils.spec b/packaging/phonenumber-utils.spec index a5b4871..d4cfd03 100644 --- a/packaging/phonenumber-utils.spec +++ b/packaging/phonenumber-utils.spec @@ -5,7 +5,9 @@ Release: 0 Group: Telephony/Utilities License: Apache-2.0 Source0: %{name}-%{version}.tar.gz +Source1: %{name}.service Source1001: %{name}.manifest +Source1002: %{name}-test.manifest BuildRequires: cmake BuildRequires: gettext-devel BuildRequires: pkgconfig(glib-2.0) @@ -18,6 +20,9 @@ BuildRequires: pkgconfig(tapi) BuildRequires: pkgconfig(capi-system-info) BuildRequires: libphonenumber-devel +%define _unitdir /usr/lib/systemd/system +%define _debus_name org.tizen.PhonenumberUtils.dbus + %description Phone Number Utilities(location, formatted number, normalized number) @@ -29,36 +34,79 @@ Requires: %{name} = %{version}-%{release} %description devel Phone Number Utilities development kit +%package test +Summary: Phone Number Utilities tester +Group: Telephony/Utilities +Requires: %{name} = %{version} + +%description test +Tizen Phone Number Utilities tester + %prep %setup -q cp %{SOURCE1001} . +cp %{SOURCE1002} . %build +%if 0%{?tizen_build_binary_release_type_eng} +export CFLAGS="$CFLAGS -DTIZEN_DEBUG_ENABLE" +export CXXFLAGS="$CXXFLAGS -DTIZEN_DEBUG_ENABLE" +export FFLAGS="$FFLAGS -DTIZEN_DEBUG_ENABLE" +%endif + MAJORVER=`echo %{version} | awk 'BEGIN {FS="."}{print $1}'` -%cmake . -DMAJORVER=${MAJORVER} -DFULLVER=%{version} +%cmake . -DMAJORVER=${MAJORVER} -DFULLVER=%{version} -DBIN_INSTALL_DIR:PATH=%{_bindir} %install rm -rf %{buildroot} %make_install +mkdir -p %{buildroot}%{_unitdir}/multi-user.target.wants +install -m 0644 %{SOURCE1} %{buildroot}%{_unitdir}/%{name}.service + +mkdir -p %{buildroot}/usr/share/license +cp LICENSE.APLv2 %{buildroot}/usr/share/license/%{name} + + %post /sbin/ldconfig +systemctl daemon-reload +if [ $1 == 1 ]; then + systemctl restart %{name}.service +fi chown :5000 -R /opt/usr/data/%{name} chmod 775 -R /opt/usr/data/%{name} -%postun -p /sbin/ldconfig +%postun +/sbin/ldconfig +if [ $1 == 0 ]; then + systemctl stop %{name}.service +fi +systemctl daemon-reload + %files -%manifest %{name}.manifest %defattr(-,root,root,-) -%{_libdir}/lib%{name}.so.* +%manifest %{name}.manifest +%{_unitdir}/%{name}.service /opt/usr/data/%{name} -%license LICENSE.APLv2 +/opt/usr/data/%{name}/downloads +%{_datadir}/license/%{name} +%{_bindir}/%{name}-daemon +%{_datadir}/dbus-1/system-services/%{_debus_name}.service +%config %{_sysconfdir}/dbus-1/system.d/%{_debus_name}.conf +%{_libdir}/lib%{name}.so.* %files devel %defattr(-,root,root,-) %{_libdir}/lib%{name}.so %{_libdir}/pkgconfig/%{name}.pc %{_includedir}/%{name}/*.h -%license LICENSE.APLv2 + + +%files test +%defattr(-,root,root,-) +%manifest %{name}-test.manifest +%{_bindir}/%{name}-test + diff --git a/phonenumber-utils.pc.in b/phonenumber-utils.pc.in deleted file mode 100644 index ba87bea..0000000 --- a/phonenumber-utils.pc.in +++ /dev/null @@ -1,11 +0,0 @@ -prefix=@PREFIX@ -exec_prefix=${prefix}/bin -libdir=@LIB_INSTALL_DIR@ -includedir=@INCLUDE_INSTALL_DIR@/@PROJECT_NAME@ - -Name: @PROJECT_NAME@ -Description: @PROJECT_NAME@ library -Version: @FULLVER@ -Requires: -Libs: -L${libdir} -l@PROJECT_NAME@ -Cflags: -I${includedir} diff --git a/src/phn.c b/src/phn.c deleted file mode 100644 index bd3b4dc..0000000 --- a/src/phn.c +++ /dev/null @@ -1,131 +0,0 @@ -/* - * Phonenumber Utils - * - * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#include - -#include "phone_number.h" -#include "phn_common.h" -#include "phn_phonenumber_wrapper.h" -#include "phn_location.h" -#include "phn_region_data.h" - - -API int phone_number_get_location_from_number(const char *number, - phone_number_region_e region, phone_number_lang_e lang, char **location) -{ - int ret; - char *region_str = NULL; - char *lang_str = NULL; - - RETVM_IF(NULL == number || '\0' == *number, PHONE_NUMBER_ERROR_INVALID_PARAMETER, - "Invalid parameter (number is NULL)"); - RETVM_IF(region < 0 || PHONE_NUMBER_REGION_MAX <= region, PHONE_NUMBER_ERROR_INVALID_PARAMETER, - "Invalid parameter (region:%d)", region); - RETVM_IF(lang < 0 || PHONE_NUMBER_LANG_MAX <= lang, PHONE_NUMBER_ERROR_INVALID_PARAMETER, - "Invalid parameter (lang:%d)", lang); - RETVM_IF(NULL == location, PHONE_NUMBER_ERROR_INVALID_PARAMETER, - "Invalid parameter (location is NULL)"); - - ret = phn_region_data_get_region_str(region, ®ion_str); - if (PHONE_NUMBER_ERROR_NONE != ret) { - ERR("phn_region_data_get_region_str() Fail(%d)", ret); - return ret; - } - - ret = phn_region_data_get_lang_str(lang, &lang_str); - if (PHONE_NUMBER_ERROR_NONE != ret) { - ERR("phn_region_data_get_lang_str() Fail(%d)", ret); - free(region_str); - return ret; - } - - char *location_file = NULL; - ret = phn_location_find_extra_data(region_str, &location_file); - if (PHONE_NUMBER_ERROR_NONE == ret && location_file) { - ret = phn_location_get_location_from_extra_data(location_file, number, - region_str, lang_str, location); - free(location_file); - if (PHONE_NUMBER_ERROR_NONE == ret && *location) { - free(region_str); - free(lang_str); - return PHONE_NUMBER_ERROR_NONE; - } - } - - bool exist = phn_region_data_find_match_info(region, lang); - if (false == exist) { - INFO("Language not matched with Region. Set to defualt language."); - free(lang_str); - lang_str = strdup(PHN_REGION_DEFAULT_LANG); - } - - ret = phn_get_location_from_number(number, region_str, lang_str, location); - if (PHONE_NUMBER_ERROR_NONE != ret) { - ERR("phn_get_location_from_number() Fail(%d)", ret); - free(region_str); - free(lang_str); - return ret; - } - - free(region_str); - free(lang_str); - return PHONE_NUMBER_ERROR_NONE; -} - -API int phone_number_get_formatted_number(const char *number, phone_number_region_e region, char **formatted_number) -{ - int ret; - char *region_str = NULL; - - RETVM_IF(NULL == number || '\0' == *number, PHONE_NUMBER_ERROR_INVALID_PARAMETER, - "Invalid parameter (number is NULL)"); - RETVM_IF((int)region < 0 || PHONE_NUMBER_REGION_MAX <= region, PHONE_NUMBER_ERROR_INVALID_PARAMETER, - "Invalid parameter (region:%d)", region); - RETVM_IF(NULL == formatted_number, PHONE_NUMBER_ERROR_INVALID_PARAMETER, "Invalid parameter (formatted_number is NULL)"); - - ret = phn_region_data_get_region_str(region, ®ion_str); - if (PHONE_NUMBER_ERROR_NONE != ret) { - ERR("phn_region_data_get_region_str() Fail(%d)", ret); - return ret; - } - - ret = phn_get_formatted_number(number, region_str, formatted_number); - if (PHONE_NUMBER_ERROR_NONE != ret) { - ERR("phn_get_formatted_number() Fail(%d)", ret); - free(region_str); - return ret; - } - - free(region_str); - return PHONE_NUMBER_ERROR_NONE; -} - -API int phone_number_get_normalized_number(const char *number, char **normalized_number) -{ - int ret; - - ret = phn_get_normalized_number(number, normalized_number); - if (PHONE_NUMBER_ERROR_NONE != ret) { - ERR("phn_get_normalized_number() Fail(%d)", ret); - return ret; - } - - return PHONE_NUMBER_ERROR_NONE; -} - diff --git a/src/phn_common.h b/src/phn_common.h deleted file mode 100644 index d550140..0000000 --- a/src/phn_common.h +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Phonenumber Utils - * - * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ -#ifndef __PHN_COMMON_H__ -#define __PHN_COMMON_H__ - -#include "phone_number_errors.h" - -#define PHN_STR_SHORT_LEN 1024 -#define STRING_EQUAL 0 - -#define LOG_TAG "PHONE_NUMBER_UTILS" -#include - -#ifndef API -#define API __attribute__ ((visibility("default"))) -#endif - -#define DLOG(prio, fmt, arg...) \ - do { SLOG(prio, LOG_TAG, fmt, ##arg); } while (0); -#define INFO(fmt, arg...) SLOGI(fmt, ##arg) -#define WARN(fmt, arg...) SLOGW("%s:%d " fmt, __FUNCTION__, __LINE__, ##arg) -#define ERR(fmt, arg...) SLOGE("%s:%d " fmt, __FUNCTION__, __LINE__, ##arg) -#define DBG(fmt, arg...) SLOGD("%s:" fmt, __FUNCTION__, ##arg) -#define WARN_IF(expr, fmt, arg...) do { \ - if (expr) { \ - WARN(fmt, ##arg); \ - } \ -} while (0) - -#define RET_IF(expr) do { \ - if (expr) { \ - ERR("(%s)", #expr); \ - return; \ - } \ -} while (0) -#define RETV_IF(expr, val) do { \ - if (expr) { \ - ERR("(%s)", #expr); \ - return (val); \ - } \ -} while (0) -#define RETM_IF(expr, fmt, arg...) do { \ - if (expr) { \ - ERR(fmt, ##arg); \ - return; \ - } \ -} while (0) -#define RETVM_IF(expr, val, fmt, arg...) do { \ - if (expr) { \ - ERR(fmt, ##arg); \ - return (val); \ - } \ -} while (0) - -#endif /* __PHN_COMMON_H__ */ diff --git a/src/phn_location.c b/src/phn_location.c deleted file mode 100644 index f56283f..0000000 --- a/src/phn_location.c +++ /dev/null @@ -1,400 +0,0 @@ -/* - * Phonenumber Utils - * - * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ -#include -#include -#include -#include -#include -#include -#include -#include - -#include "phn_common.h" -#include "phn_region_data.h" -#include "phn_location.h" - -#define PHN_LOCATION_DIR "/opt/usr/data/phonenumber-utils" -#define PHN_LOCATION_FILE_PREFIX "location" - -#define PHN_LOCATION_CHINA_MOBILE_SUFFIX_OFFSET 10000 -#define PHN_LOCATION_CHINA_MOBILE_PREFIX_LEN 3 -#define PHN_LOCATION_CHINA_MOBILE_SUFFIX_LEN 4 -#define PHN_LOCATION_CHINA_MOBILE_NUMBER_MIN_LEN 7 - -struct phn_location_header { - gunichar2 version[16]; - gunichar2 version_date[16]; - int province_count; - int telephone_city_count; - int mobile_city_count; - int mobile_prefix_index_count; - int province_name_len[3]; - int province_id_len; - int telephone_number_len; - int telephone_city_len[3]; - int mobile_city_len[3]; - int mobile_prefix_len; -}; - -int phn_location_find_extra_data(char *region_str, char **p_location_file) -{ - DIR *dirp = NULL; - struct dirent **dir_list; - char *location_file = NULL; - - dirp = opendir(PHN_LOCATION_DIR); - if (NULL == dirp) { - ERR("opendir() return NULL"); - return PHONE_NUMBER_ERROR_NO_DATA; - } - - int count = scandir(PHN_LOCATION_DIR, &dir_list, 0, alphasort); - if (count) { - int idx = 0; - char location_prefix[PHN_STR_SHORT_LEN] = {0}; - - snprintf(location_prefix, sizeof(location_prefix), "%s-%s", - PHN_LOCATION_FILE_PREFIX, region_str); - while (idx != count) { - const char *file_name = dir_list[idx]->d_name; - if (0 == strncmp(file_name, location_prefix, strlen(location_prefix))) { - location_file = g_strdup(file_name); - break; - } - idx++; - } - } - closedir(dirp); - - if (location_file) { - *p_location_file = location_file; - return PHONE_NUMBER_ERROR_NONE; - } - - return PHONE_NUMBER_ERROR_NO_DATA; -} - -int phn_location_get_location_from_extra_data(const char *file, const char *number, - char *region_str, char *lang_str, char **p_location) -{ - int ret = 0; - int city_str_len = 0; - int province_str_len = 0; - gchar *city_temp = NULL; - gchar *province_temp = NULL; - const char *real_number = number; - const gunichar2 *city_str = NULL; - const gunichar2 *province_str = NULL; - char file_path[PHN_STR_SHORT_LEN] = {0}; - - /* support region - CN, support lang - zh,en,ko */ - RETV_IF(NULL == region_str, PHONE_NUMBER_ERROR_INVALID_PARAMETER); - RETV_IF(NULL == lang_str, PHONE_NUMBER_ERROR_INVALID_PARAMETER); - - while (real_number && real_number[0] == '0') - real_number++; - RETVM_IF(NULL == real_number, PHONE_NUMBER_ERROR_INVALID_PARAMETER, "number=%s", - number); - - char lang_region[PHN_STR_SHORT_LEN] = {0}; - snprintf(lang_region, sizeof(lang_region), "%s_%s", lang_str, region_str); - - int lang_index = 0; - if (STRING_EQUAL == strcmp(lang_region, PHN_LOCATION_SUPPORT_ZH_CN)) { - lang_index = 0; - } else if (STRING_EQUAL == strcmp(lang_region, PHN_LOCATION_SUPPORT_EN_CN)) { - lang_index = 1; - } else if (STRING_EQUAL == strcmp(lang_region, PHN_LOCATION_SUPPORT_KO_CN)) { - lang_index = 2; - } else { - ERR("Not supported(%s)", lang_region); - return PHONE_NUMBER_ERROR_NOT_SUPPORTED; - } - - snprintf(file_path, sizeof(file_path), "%s/%s", PHN_LOCATION_DIR, file); - - int fd = open(file_path, O_RDONLY); - RETVM_IF(fd < 0, PHONE_NUMBER_ERROR_NOT_SUPPORTED, "open() Fail(%d)", errno); - - ret = lseek(fd, sizeof(int), SEEK_CUR); /* start_mark */ - if (ret <= 0) { - ERR("lseek() Fail(%d)", errno); - close(fd); - return PHONE_NUMBER_ERROR_NOT_SUPPORTED; - } - - struct phn_location_header header; - ret = read(fd, &header, sizeof(struct phn_location_header)); - if (ret <= 0) { - ERR("read() Fail(%d)", errno); - close(fd); - return PHONE_NUMBER_ERROR_NOT_SUPPORTED; - } - - struct phn_province_info { - gunichar2 name1[header.province_name_len[0]/2]; - gunichar2 name2[header.province_name_len[1]/2]; - gunichar2 name3[header.province_name_len[2]/2]; - }; - -#pragma pack(1) - struct phn_telephone_city_info { - gint8 province_index; - gunichar2 city1[header.telephone_city_len[0]/2]; - gunichar2 city2[header.telephone_city_len[1]/2]; - gunichar2 city3[header.telephone_city_len[2]/2]; - gint16 prefix; - }; - -#pragma pack(1) - struct phn_mobile_city_info { - gint8 province_index; - gunichar2 city1[header.mobile_city_len[0]/2]; - gunichar2 city2[header.mobile_city_len[1]/2]; - gunichar2 city3[header.mobile_city_len[2]/2]; - }; - - if (header.province_count <= 0) { - ERR("Invalid provice_count(%d)", header.province_count); - close(fd); - return PHONE_NUMBER_ERROR_NOT_SUPPORTED; - } - struct phn_province_info province_info[header.province_count]; - ret = read(fd, &province_info, sizeof(province_info)); - if (ret <= 0) { - ERR("read() Fail(%d)", errno); - close(fd); - return PHONE_NUMBER_ERROR_NOT_SUPPORTED; - } - - if (header.telephone_city_count <= 0) { - ERR("Invalid telephone_city_count(%d)", header.telephone_city_count); - close(fd); - return PHONE_NUMBER_ERROR_NOT_SUPPORTED; - } - struct phn_telephone_city_info telephone_city_info[header.telephone_city_count]; - ret = read(fd, &telephone_city_info, sizeof(telephone_city_info)); - if (ret <= 0) { - ERR("read() Fail(%d)", errno); - close(fd); - return PHONE_NUMBER_ERROR_NOT_SUPPORTED; - } - - int i; - int telephone_city_count = - sizeof(telephone_city_info) / sizeof(struct phn_telephone_city_info); - for (i = 0; i < telephone_city_count; i++) { - gint8 provice_idx; - gint16 prefix = telephone_city_info[i].prefix; - char prefix_str[PHN_STR_SHORT_LEN] = {0}; - - snprintf(prefix_str, sizeof(prefix_str), "%u", prefix); - if (0 == strncmp(real_number, prefix_str, strlen(prefix_str))) { - switch (lang_index) { - case 0: - city_str = telephone_city_info[i].city1; - city_str_len = header.telephone_city_len[0] / 2; - break; - case 1: - city_str = telephone_city_info[i].city2; - city_str_len = header.telephone_city_len[1] / 2; - break; - case 2: - city_str = telephone_city_info[i].city3; - city_str_len = header.telephone_city_len[2] / 2; - break; - default: - ERR("Invalid lang_index(%d)", lang_index); - break; - } - - provice_idx = telephone_city_info[i].province_index; - if (0 < provice_idx && provice_idx <= header.province_count) { - switch (lang_index) { - case 0: - province_str = province_info[provice_idx-1].name1; - province_str_len = header.province_name_len[0] / 2; - break; - case 1: - province_str = province_info[provice_idx-1].name2; - province_str_len = header.province_name_len[1] / 2; - break; - case 2: - province_str = province_info[provice_idx-1].name3; - province_str_len = header.province_name_len[2] / 2; - break; - default: - ERR("Invalid lang_index(%d)", lang_index); - break; - } - } - - province_temp = g_utf16_to_utf8(province_str, province_str_len, NULL, NULL, - NULL); - city_temp = g_utf16_to_utf8(city_str, city_str_len, NULL, NULL, NULL); - - if (city_temp && province_temp) { - int size = strlen(city_temp) + strlen(province_temp); - char *location = NULL; - location = calloc(size + 3, sizeof(char)); - if (location) - snprintf(location, size + 3, "%s, %s", city_temp, province_temp); - *p_location = location; - } else if (city_temp) { - int size = strlen(city_temp); - char *location = NULL; - location = calloc(size + 1, sizeof(char)); - if (location) - snprintf(location, size + 1, "%s", city_temp); - *p_location = location; - } - - close(fd); - free(city_temp); - free(province_temp); - if (NULL == p_location) - return PHONE_NUMBER_ERROR_NOT_SUPPORTED; - return PHONE_NUMBER_ERROR_NONE; - } - } - - if (strlen(number) < PHN_LOCATION_CHINA_MOBILE_NUMBER_MIN_LEN) { - ERR("Invalid number(%s)", number); - close(fd); - return PHONE_NUMBER_ERROR_INVALID_PARAMETER; - } - - if (header.mobile_city_count <= 0) { - ERR("Invalid mobile_city_count(%d)", header.mobile_city_count); - close(fd); - return PHONE_NUMBER_ERROR_NOT_SUPPORTED; - } - struct phn_mobile_city_info mobile_city_info[header.mobile_city_count]; - ret = read(fd, &mobile_city_info, sizeof(mobile_city_info)); - if (ret <= 0) { - ERR("read() Fail(%d)", errno); - close(fd); - return PHONE_NUMBER_ERROR_NOT_SUPPORTED; - } - - if (header.mobile_prefix_index_count <= 0) { - ERR("Invalid mobile_prefix_index_count(%d)", header.mobile_prefix_index_count); - close(fd); - return PHONE_NUMBER_ERROR_NOT_SUPPORTED; - } - gint16 mobile_prefix_info[header.mobile_prefix_index_count]; - ret = read(fd, &mobile_prefix_info, sizeof(mobile_prefix_info)); - if (ret <= 0) { - ERR("read() Fail(%d)", errno); - close(fd); - return PHONE_NUMBER_ERROR_NOT_SUPPORTED; - } - - char num_prefix_str[PHN_LOCATION_CHINA_MOBILE_PREFIX_LEN + 1] = {0}; - char num_suffix_str[PHN_LOCATION_CHINA_MOBILE_SUFFIX_LEN + 1] = {0}; - snprintf(num_prefix_str, sizeof(num_prefix_str), "%s", number); - snprintf(num_suffix_str, sizeof(num_suffix_str), "%s", number + 3); - int num_prefix = atoi(num_prefix_str); - int num_suffix = atoi(num_suffix_str); - - int mobile_prefix_index_count = sizeof(mobile_prefix_info) / sizeof(gint16); - for (i = 0; i < mobile_prefix_index_count; i++) { - if (num_prefix == mobile_prefix_info[i]) { - gint16 mobile_prefix = 0; - ret = lseek(fd, (PHN_LOCATION_CHINA_MOBILE_SUFFIX_OFFSET * sizeof(gint16) * i) - + (num_suffix * sizeof(gint16)), SEEK_CUR); - ret = read(fd, &mobile_prefix, sizeof(gint16)); - WARN_IF(ret < 0, "read() Fail(%d)", errno); - if (0 == mobile_prefix) { - ERR("Invalid value:mobile_prefix is 0"); - continue; - } - - switch (lang_index) { - case 0: - city_str = mobile_city_info[mobile_prefix - 1].city1; - city_str_len = header.mobile_city_len[0] / 2; - break; - case 1: - city_str = mobile_city_info[mobile_prefix - 1].city2; - city_str_len = header.mobile_city_len[1] / 2; - break; - case 2: - city_str = mobile_city_info[mobile_prefix - 1].city3; - city_str_len = header.mobile_city_len[2] / 2; - break; - default: - ERR("Invalid lang_index(%d)", lang_index); - break; - } - - int province_index = mobile_city_info[mobile_prefix - 1].province_index; - - if (0 < province_index && province_index <= header.province_count) { - switch (lang_index) { - case 0: - province_str = province_info[province_index - 1].name1; - province_str_len = header.province_name_len[0] / 2; - break; - case 1: - province_str = province_info[province_index - 1].name2; - province_str_len = header.province_name_len[1] / 2; - break; - case 2: - province_str = province_info[province_index - 1].name3; - province_str_len = header.province_name_len[2] / 2; - break; - default: - ERR("Invalid lang_index(%d)", lang_index); - break; - } - } - - province_temp = g_utf16_to_utf8(province_str, province_str_len, NULL, NULL, - NULL); - city_temp = g_utf16_to_utf8(city_str, city_str_len, NULL, NULL, NULL); - - if (city_temp && province_temp) { - int size = strlen(city_temp) + strlen(province_temp); - char *location = NULL; - location = calloc(size + 3, sizeof(char)); - if (location) - snprintf(location, size + 3, "%s, %s", city_temp, province_temp); - *p_location = location; - } else if (city_temp) { - int size = strlen(city_temp); - char *location = NULL; - location = calloc(size + 1, sizeof(char)); - if (location) - snprintf(location, size + 1, "%s", city_temp); - *p_location = location; - } - close(fd); - free(city_temp); - free(province_temp); - if (NULL == p_location) - return PHONE_NUMBER_ERROR_NOT_SUPPORTED; - return PHONE_NUMBER_ERROR_NONE; - } - } - - close(fd); - return PHONE_NUMBER_ERROR_NONE; -} - diff --git a/src/phn_location.h b/src/phn_location.h deleted file mode 100644 index bf7b18e..0000000 --- a/src/phn_location.h +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Phonenumber Utils - * - * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ -#ifndef __PHN_LOCATION_H__ -#define __PHN_LOCATION_H__ - -#include "phone_number_types.h" - -#define PHN_LOCATION_SUPPORT_ZH_CN "zh_CN" -#define PHN_LOCATION_SUPPORT_EN_CN "en_CN" -#define PHN_LOCATION_SUPPORT_KO_CN "ko_CN" - -int phn_location_find_extra_data(char *region_str, char **p_location_file); -int phn_location_get_location_from_extra_data(const char *file, const char *number, - char *region_str, char *lang_str, char **p_location); - -#endif /* __PHN_LOCATION_H__ */ diff --git a/src/phn_phonenumber_wrapper.cpp b/src/phn_phonenumber_wrapper.cpp deleted file mode 100644 index 7e1b56b..0000000 --- a/src/phn_phonenumber_wrapper.cpp +++ /dev/null @@ -1,554 +0,0 @@ -/* - * Phonenumber Utils - * - * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "phn_common.h" -#include "phn_phonenumber_wrapper.h" - -#define MCC_LEN 3 -#define PHN_FEATURE_TELEPHONY "http://tizen.org/feature/network.telephony" - -#define PHN_FEATURE_TELEPHONY_INIT_STATE -1 -#define PHN_FEATURE_TELEPHONY_NOT_SUPPORTED 0 -#define PHN_FEATURE_TELEPHONY_SUPPORTED 1 - -using namespace i18n::phonenumbers; - -typedef struct { - int mcc; - int cc; -} mcc_cc_map_s; - -static int _cc = 0; -static TapiHandle **_tapi_handle = NULL; -static int _modem_num = 0; -static int _phn_have_telephony_feature = -1; - -static int _phn_get_cc(bool reload, int *out_cc); - -int phn_get_location_from_number(const char *number, const char *region, const char *language, char **location) -{ - PhoneNumber phNumber; - const PhoneNumberUtil& pn_instance = *PhoneNumberUtil::GetInstance(); - const PhoneNumberUtil::ErrorType status = pn_instance.Parse( - number, region, &phNumber); - RETVM_IF(status != PhoneNumberUtil::NO_PARSING_ERROR, PHONE_NUMBER_ERROR_NO_DATA, "parse() failed(%d)", status); - - const std::string description = - PhoneNumberOfflineGeocoder().GetDescriptionForNumber( - phNumber, icu::Locale(language)); - *location = g_strdup((gchar *)description.c_str()); - - return PHONE_NUMBER_ERROR_NONE; -} - -int phn_get_formatted_number(const char *number, const char *region, char **formatted_number) -{ - const PhoneNumberUtil& pn_instance = *PhoneNumberUtil::GetInstance(); - AsYouTypeFormatter *formatter = pn_instance.GetAsYouTypeFormatter(region); - - int i = 0; - string result; - while (number[i] && '\0' != number[i]) { - formatter->InputDigit(number[i++], &result); - } - delete formatter; - *formatted_number = g_strdup((gchar *)result.c_str()); - - return PHONE_NUMBER_ERROR_NONE; -} - -static void _phn_cc_changed_cb(TapiHandle *handle, const char *noti_id, void *data, void *user_data) -{ - int ret; - ret = _phn_get_cc(true, NULL); - if (PHONE_NUMBER_ERROR_NONE != ret) { - ERR("_phn_get_cc() Fail(%d)", ret); - return; - } -} - -static int _phn_get_tapi_handle() -{ - int i; - int ret; - char **cp_list = NULL; - _modem_num = 0; - - cp_list = tel_get_cp_name_list(); - if (NULL == cp_list) { - ERR("tel_get_cp_name_list() Fail(NULL)"); - return PHONE_NUMBER_ERROR_SYSTEM; - } - - while (cp_list[_modem_num]) - _modem_num++; - DBG("_modem_num=[%d]", _modem_num); - - _tapi_handle = (TapiHandle **)calloc(_modem_num, sizeof(TapiHandle *)); - if (NULL == _tapi_handle) { - ERR("calloc() Fail"); - g_strfreev(cp_list); - return PHONE_NUMBER_ERROR_OUT_OF_MEMORY; - } - - for (i = 0; i < _modem_num; i++) { - _tapi_handle[i] = tel_init(cp_list[i]); - if (NULL == _tapi_handle[i]) { - ERR("tel_init() for _tapi_handle[%d] Fail", i); - g_strfreev(cp_list); - return PHONE_NUMBER_ERROR_SYSTEM; - } - - ret = tel_register_noti_event(_tapi_handle[i], TAPI_PROP_NETWORK_PLMN, - _phn_cc_changed_cb, NULL); - if (TAPI_API_SUCCESS != ret) { - if (TAPI_API_ACCESS_DENIED == ret) { - ERR("tel_register_noti_event() Fail(%d)", ret); - g_strfreev(cp_list); - return PHONE_NUMBER_ERROR_PERMISSION_DENIED; - } else { - ERR("tel_register_noti_event() for _tapi_handle[%d] Fail(%d)", i, ret); - g_strfreev(cp_list); - return PHONE_NUMBER_ERROR_SYSTEM; - } - } - } - - g_strfreev(cp_list); - return PHONE_NUMBER_ERROR_NONE; -} - -static int _phn_get_cc(bool reload, int *out_cc) -{ - int i = 0; - int state; - int ret; - int mcc = 0; - char *temp = NULL; - static bool cc_loaded = false; - - mcc_cc_map_s _mcc_cc_list[] = { - {0, 1}, - {202, 30}, - {204, 31}, - {206, 32}, - {208, 33}, - {212, 377}, - {213, 376}, - {214, 34}, - {216, 36}, - {218, 387}, - {219, 385}, - {220, 381}, - {222, 39}, - {225, 39}, - {226, 40}, - {228, 41}, - {230, 420}, - {231, 421}, - {232, 43}, - {234, 44}, - {235, 44}, - {238, 45}, - {240, 46}, - {242, 47}, - {244, 358}, - {246, 370}, - {247, 371}, - {248, 372}, - {250, 7}, - {255, 380}, - {257, 375}, - {259, 373}, - {260, 48}, - {262, 49}, - {266, 350}, - {268, 351}, - {270, 352}, - {272, 353}, - {274, 354}, - {276, 355}, - {278, 356}, - {280, 357}, - {282, 995}, - {283, 374}, - {284, 359}, - {286, 90}, - {288, 298}, - {290, 299}, - {292, 378}, - {293, 386}, - {294, 389}, - {295, 423}, - {297, 382}, - {302, 1}, - {308, 508}, - {310, 1}, - {311, 1}, - {312, 1}, - {313, 1}, - {314, 1}, - {315, 1}, - {316, 1}, - {330, 1}, - {332, 1}, - {334, 52}, - {338, 1}, - {340, 590}, - {340, 596}, - {342, 1}, - {344, 1}, - {346, 1}, - {348, 1}, - {350, 1}, - {352, 1}, - {354, 1}, - {356, 1}, - {358, 1}, - {360, 1}, - {362, 599}, - {363, 297}, - {364, 1}, - {365, 1}, - {366, 1}, - {368, 53}, - {370, 1}, - {372, 509}, - {374, 1}, - {376, 1}, - {400, 994}, - {401, 7}, - {402, 975}, - {404, 91}, - {405, 91}, - {406, 91}, - {410, 92}, - {412, 93}, - {413, 94}, - {414, 95}, - {415, 961}, - {416, 962}, - {417, 963}, - {418, 964}, - {419, 965}, - {420, 966}, - {421, 967}, - {422, 968}, - {424, 971}, - {425, 972}, - {426, 973}, - {427, 974}, - {428, 976}, - {429, 977}, - {430, 971}, - {431, 971}, - {432, 98}, - {434, 998}, - {436, 992}, - {437, 996}, - {438, 993}, - {440, 81}, - {441, 81}, - {450, 82}, - {452, 84}, - {454, 852}, - {455, 853}, - {456, 855}, - {457, 856}, - {460, 86}, - {461, 86}, - {466, 886}, - {467, 850}, - {470, 880}, - {472, 960}, - {502, 60}, - {505, 61}, - {510, 62}, - {514, 670}, - {515, 63}, - {520, 66}, - {525, 65}, - {528, 673}, - {530, 64}, - {536, 674}, - {537, 675}, - {539, 676}, - {540, 677}, - {541, 678}, - {542, 679}, - {543, 681}, - {544, 1}, - {545, 686}, - {546, 687}, - {547, 689}, - {548, 682}, - {549, 685}, - {550, 691}, - {551, 692}, - {552, 680}, - {602, 20}, - {603, 213}, - {604, 212}, - {605, 216}, - {606, 218}, - {607, 220}, - {608, 221}, - {609, 222}, - {610, 223}, - {611, 224}, - {612, 225}, - {613, 226}, - {614, 227}, - {615, 228}, - {616, 229}, - {617, 230}, - {618, 231}, - {619, 232}, - {620, 233}, - {621, 234}, - {622, 235}, - {623, 236}, - {624, 237}, - {625, 238}, - {626, 239}, - {627, 240}, - {628, 241}, - {629, 242}, - {630, 243}, - {631, 244}, - {632, 245}, - {633, 248}, - {634, 249}, - {635, 250}, - {636, 251}, - {637, 252}, - {638, 253}, - {639, 254}, - {640, 255}, - {641, 256}, - {642, 257}, - {643, 258}, - {645, 260}, - {646, 261}, - {647, 262}, - {648, 263}, - {649, 264}, - {650, 265}, - {651, 266}, - {652, 267}, - {653, 268}, - {654, 269}, - {655, 27}, - {657, 291}, - {702, 501}, - {704, 502}, - {706, 503}, - {708, 504}, - {710, 505}, - {712, 506}, - {714, 507}, - {716, 51}, - {722, 54}, - {724, 55}, - {730, 56}, - {732, 57}, - {734, 58}, - {736, 591}, - {738, 592}, - {740, 593}, - {742, 594}, - {744, 595}, - {746, 597}, - {748, 598}, - {750, 500}, - }; - - if (cc_loaded && false == reload) { - if (NULL == out_cc) { - ERR("Invalid parameter (out_cc is NULL)"); - return PHONE_NUMBER_ERROR_INVALID_PARAMETER; - } - - *out_cc = _cc; - DBG("cc=[%d]", *out_cc); - return PHONE_NUMBER_ERROR_NONE; - } - - _cc = 0; - cc_loaded = true; - - if (NULL == _tapi_handle) { - ret = _phn_get_tapi_handle(); - if (PHONE_NUMBER_ERROR_NONE != ret) { - ERR("_phn_get_tapi_handle() Fail(%d)", ret); - return ret; - } - } - - do { - WARN("Using multi SIM"); - free(temp); - temp = NULL; - - ret = tel_get_property_int(_tapi_handle[i], TAPI_PROP_NETWORK_SERVICE_TYPE, &state); - if (TAPI_API_SUCCESS != ret) { - if (TAPI_API_ACCESS_DENIED == ret) { - ERR("tel_get_property_int() Fail(%d)", ret); - return PHONE_NUMBER_ERROR_PERMISSION_DENIED; - } else { - ERR("tel_get_property_int() Fail(%d)", ret); - return PHONE_NUMBER_ERROR_SYSTEM; - } - } - - if (TAPI_NETWORK_SERVICE_TYPE_UNKNOWN == state - || TAPI_NETWORK_SERVICE_TYPE_NO_SERVICE == state - || TAPI_NETWORK_SERVICE_TYPE_EMERGENCY == state - || TAPI_NETWORK_SERVICE_TYPE_SEARCH == state) { - WARN("network service is not working : state(%d)", state); - } - - ret = tel_get_property_string(_tapi_handle[i], TAPI_PROP_NETWORK_PLMN, &temp); - if (TAPI_API_SUCCESS != ret) { - if (TAPI_API_ACCESS_DENIED == ret) { - ERR("tel_get_property_string() Fail(%d)", ret); - return PHONE_NUMBER_ERROR_PERMISSION_DENIED; - } else { - ERR("tel_get_property_string() Fail(%d)", ret); - return PHONE_NUMBER_ERROR_SYSTEM; - } - } - DBG("temp=[%s] from _tapi_handle[%d]", temp, i); - - i++; - if (_modem_num <= i) break; - - } while (NULL == temp || '\0' == temp[0]); - - if (NULL == temp || '\0' == temp[0]) { - ERR("get NETWORK_PLMN Fail"); - free(temp); - return PHONE_NUMBER_ERROR_SYSTEM; - } - - if (temp && MCC_LEN < strlen(temp)) - temp[MCC_LEN] = '\0'; - mcc = atoi(temp); - DBG("mcc=[%d]", mcc); - - for (i = 0; i < (int)(sizeof(_mcc_cc_list)/sizeof(mcc_cc_map_s)); i++) { - if (_mcc_cc_list[i].mcc == mcc) { - _cc = _mcc_cc_list[i].cc; - break; - } - } - - if (0 == _cc) { - ERR("No data for current mcc(%s)", mcc); - free(temp); - return PHONE_NUMBER_ERROR_NO_DATA; - } - - if (out_cc) { - *out_cc = _cc; - DBG("cc=[%d]", *out_cc); - } - - free(temp); - return PHONE_NUMBER_ERROR_NONE; -} - -static int _phn_get_telephony_feature(void) -{ - int ret; - bool telephony_feature = false; - - if (PHN_FEATURE_TELEPHONY_INIT_STATE != _phn_have_telephony_feature) - return _phn_have_telephony_feature; - - ret = system_info_get_platform_bool(PHN_FEATURE_TELEPHONY, &telephony_feature); - if (SYSTEM_INFO_ERROR_NONE != ret) { - ERR("system_info_get_platform_bool() Fail(%d)", ret); - return PHN_FEATURE_TELEPHONY_INIT_STATE; - } - - if (false == telephony_feature) - _phn_have_telephony_feature = PHN_FEATURE_TELEPHONY_NOT_SUPPORTED; - else - _phn_have_telephony_feature = PHN_FEATURE_TELEPHONY_SUPPORTED; - - return _phn_have_telephony_feature; -} - -int phn_get_normalized_number(const char *number, char **out_e164) -{ - int ret; - int cc = 0; - PhoneNumber pn; - string number_e164; - string region_code; - const PhoneNumberUtil& pnu = *PhoneNumberUtil::GetInstance(); - - ret = _phn_get_telephony_feature(); - if (PHN_FEATURE_TELEPHONY_INIT_STATE == ret) { - ERR("System info error"); - return PHONE_NUMBER_ERROR_SYSTEM; - } else if (PHN_FEATURE_TELEPHONY_NOT_SUPPORTED == ret) { - ERR("Telephony feature disabled"); - return PHONE_NUMBER_ERROR_NOT_SUPPORTED; - } - - RETVM_IF(NULL == number || '\0' == *number, PHONE_NUMBER_ERROR_INVALID_PARAMETER, - "Invalid parameter (number is NULL)"); - RETVM_IF(NULL == out_e164, PHONE_NUMBER_ERROR_INVALID_PARAMETER, "Invalid parameter (normalized_number is NULL)"); - - ret = _phn_get_cc(false, &cc); - if (PHONE_NUMBER_ERROR_NONE != ret) { - ERR("_phn_get_cc() Fail(%d)", ret); - return ret; - } - pnu.GetRegionCodeForCountryCode(cc, ®ion_code); - - DBG("cc=[%d], number=[%s], region_code=[%s]", cc, number, region_code.c_str()); - - const PhoneNumberUtil::ErrorType status = pnu.Parse(number, region_code, &pn); - if (PhoneNumberUtil::NO_PARSING_ERROR != status) { - ERR("pnu.Parse() Fail(%d), cc(%d), number(%s), region_code(%s)", - status, cc, number, region_code.c_str()); - return PHONE_NUMBER_ERROR_SYSTEM; - } - - pnu.Format(pn, PhoneNumberUtil::E164, &number_e164); - - *out_e164 = g_strdup((gchar *)number_e164.c_str()); - DBG("number_e164=[%s]", *out_e164); - - return PHONE_NUMBER_ERROR_NONE; - -} - - diff --git a/src/phn_phonenumber_wrapper.h b/src/phn_phonenumber_wrapper.h deleted file mode 100644 index b71db3c..0000000 --- a/src/phn_phonenumber_wrapper.h +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Phonenumber Utils - * - * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ -#ifndef __PHN_PHONENUMBER_WRAPPER_H__ -#define __PHN_PHONENUMBER_WRAPPER_H__ - -#ifdef __cplusplus -extern "C" { -#endif - -int phn_get_formatted_number(const char *number, const char *region, - char **formatted_number); -int phn_get_location_from_number(const char *number, const char *region, - const char *language, char **location); -int phn_get_normalized_number(const char *number, char **out_e164); - -#ifdef __cplusplus -} -#endif - -#endif /* __PHN_PHONENUMBER_WRAPPER_H__ */ diff --git a/src/phn_region_data.c b/src/phn_region_data.c deleted file mode 100644 index 1a70305..0000000 --- a/src/phn_region_data.c +++ /dev/null @@ -1,350 +0,0 @@ -/* - * Phonenumber Utils - * - * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ -#include -#include -#include - -#include "phn_common.h" -#include "phn_region_data.h" - -struct phn_lang_info { - const char *lang_str; - phone_number_lang_e lang; -}; - -struct phn_match_info { - phone_number_region_e region; - phone_number_lang_e lang; -}; - -struct phn_region_info { - const char *region_str; - phone_number_region_e region; -}; - -const struct phn_match_info phn_match_info_table[] = { - {PHONE_NUMBER_REGION_WESTERN_SAHARA, PHONE_NUMBER_LANG_FRENCH}, - {PHONE_NUMBER_REGION_MOROCCO, PHONE_NUMBER_LANG_FRENCH}, - {PHONE_NUMBER_REGION_MAURITANIA, PHONE_NUMBER_LANG_FRENCH}, - {PHONE_NUMBER_REGION_COTE_D_IVOIRE, PHONE_NUMBER_LANG_FRENCH}, - {PHONE_NUMBER_REGION_TOGO, PHONE_NUMBER_LANG_SPANISH}, - {PHONE_NUMBER_REGION_TOGO, PHONE_NUMBER_LANG_FRENCH}, - {PHONE_NUMBER_REGION_BENIN, PHONE_NUMBER_LANG_FRENCH}, - {PHONE_NUMBER_REGION_MAURITIUS, PHONE_NUMBER_LANG_SPANISH}, - {PHONE_NUMBER_REGION_MAURITIUS, PHONE_NUMBER_LANG_FRENCH}, - {PHONE_NUMBER_REGION_CABO_VERDE, PHONE_NUMBER_LANG_PORTUGUESE}, - {PHONE_NUMBER_REGION_SAO_TOME_AND_PRINCIPE, PHONE_NUMBER_LANG_PORTUGUESE}, - {PHONE_NUMBER_REGION_CONGO, PHONE_NUMBER_LANG_FRENCH}, - {PHONE_NUMBER_REGION_DEMOCRATIC_REPUBLIC_OF_THE_CONGO, PHONE_NUMBER_LANG_FRENCH}, - {PHONE_NUMBER_REGION_ANGOLA, PHONE_NUMBER_LANG_PORTUGUESE}, - {PHONE_NUMBER_REGION_GUINEA_BISSAU, PHONE_NUMBER_LANG_PORTUGUESE}, - {PHONE_NUMBER_REGION_MOZAMBIQUE, PHONE_NUMBER_LANG_PORTUGUESE}, - {PHONE_NUMBER_REGION_COMOROS, PHONE_NUMBER_LANG_FRENCH}, - {PHONE_NUMBER_REGION_SAINT_HELENA, PHONE_NUMBER_LANG_FRENCH}, - {PHONE_NUMBER_REGION_TRISTAN_DA_CUNHA, PHONE_NUMBER_LANG_FRENCH}, - {PHONE_NUMBER_REGION_GREECE, PHONE_NUMBER_LANG_GREEK}, - {PHONE_NUMBER_REGION_NETHERLANDS, PHONE_NUMBER_LANG_DUTCH}, - {PHONE_NUMBER_REGION_BELGIUM, PHONE_NUMBER_LANG_GERMAN}, - {PHONE_NUMBER_REGION_BELGIUM, PHONE_NUMBER_LANG_FRENCH}, - {PHONE_NUMBER_REGION_BELGIUM, PHONE_NUMBER_LANG_DUTCH}, - {PHONE_NUMBER_REGION_FRANCE, PHONE_NUMBER_LANG_FRENCH}, - {PHONE_NUMBER_REGION_SPAIN, PHONE_NUMBER_LANG_SPANISH}, - {PHONE_NUMBER_REGION_PORTUGAL, PHONE_NUMBER_LANG_PORTUGUESE}, - {PHONE_NUMBER_REGION_LUXEMBOURG, PHONE_NUMBER_LANG_GERMAN}, - {PHONE_NUMBER_REGION_LUXEMBOURG, PHONE_NUMBER_LANG_FRENCH}, - {PHONE_NUMBER_REGION_ALAND_ISLANDS, PHONE_NUMBER_LANG_FINNISH}, - {PHONE_NUMBER_REGION_ALAND_ISLANDS, PHONE_NUMBER_LANG_NORTHERN_SAMI}, - {PHONE_NUMBER_REGION_FINLAND, PHONE_NUMBER_LANG_FINNISH}, - {PHONE_NUMBER_REGION_BULGARIA, PHONE_NUMBER_LANG_BULGARIAN}, - {PHONE_NUMBER_REGION_HUNGARY, PHONE_NUMBER_LANG_HUNGARIAN}, - {PHONE_NUMBER_REGION_REPUBLIC_OF_MOLDOVA, PHONE_NUMBER_LANG_ROMANIAN}, - {PHONE_NUMBER_REGION_REPUBLIC_OF_MOLDOVA, PHONE_NUMBER_LANG_RUSSIAN}, - {PHONE_NUMBER_REGION_ARMENIA, PHONE_NUMBER_LANG_AMHARIC}, - {PHONE_NUMBER_REGION_ARMENIA, PHONE_NUMBER_LANG_RUSSIAN}, - {PHONE_NUMBER_REGION_BELARUS, PHONE_NUMBER_LANG_BELARUSIAN}, - {PHONE_NUMBER_REGION_BELARUS, PHONE_NUMBER_LANG_RUSSIAN}, - {PHONE_NUMBER_REGION_SERBIA, PHONE_NUMBER_LANG_SERBIAN}, - {PHONE_NUMBER_REGION_ITALY, PHONE_NUMBER_LANG_ITALIAN}, - {PHONE_NUMBER_REGION_ROMANIA, PHONE_NUMBER_LANG_ROMANIAN}, - {PHONE_NUMBER_REGION_SWITZERLAND, PHONE_NUMBER_LANG_GERMAN}, - {PHONE_NUMBER_REGION_SWITZERLAND, PHONE_NUMBER_LANG_FRENCH}, - {PHONE_NUMBER_REGION_SWITZERLAND, PHONE_NUMBER_LANG_ITALIAN}, - {PHONE_NUMBER_REGION_AUSTRIA, PHONE_NUMBER_LANG_GERMAN}, - {PHONE_NUMBER_REGION_SWEDEN, PHONE_NUMBER_LANG_SWEDISH}, - {PHONE_NUMBER_REGION_POLAND, PHONE_NUMBER_LANG_POLISH}, - {PHONE_NUMBER_REGION_GERMANY, PHONE_NUMBER_LANG_GERMAN}, - {PHONE_NUMBER_REGION_BRAZIL, PHONE_NUMBER_LANG_PORTUGUESE}, - {PHONE_NUMBER_REGION_CHILE, PHONE_NUMBER_LANG_SPANISH}, - {PHONE_NUMBER_REGION_COLOMBIA, PHONE_NUMBER_LANG_SPANISH}, - {PHONE_NUMBER_REGION_VENEZUELA, PHONE_NUMBER_LANG_SPANISH}, - {PHONE_NUMBER_REGION_INDONESIA, PHONE_NUMBER_LANG_INDONESIAN}, - {PHONE_NUMBER_REGION_THAILAND, PHONE_NUMBER_LANG_THAI}, - {PHONE_NUMBER_REGION_KAZAKHSTAN, PHONE_NUMBER_LANG_RUSSIAN}, - {PHONE_NUMBER_REGION_RUSSIAN_FEDERATION, PHONE_NUMBER_LANG_RUSSIAN}, - {PHONE_NUMBER_REGION_JAPAN, PHONE_NUMBER_LANG_JAPANESE}, - {PHONE_NUMBER_REGION_REPUBLIC_OF_KOREA, PHONE_NUMBER_LANG_ARABIC}, - {PHONE_NUMBER_REGION_REPUBLIC_OF_KOREA, PHONE_NUMBER_LANG_CZECH}, - {PHONE_NUMBER_REGION_REPUBLIC_OF_KOREA, PHONE_NUMBER_LANG_FRENCH}, - {PHONE_NUMBER_REGION_REPUBLIC_OF_KOREA, PHONE_NUMBER_LANG_KOREAN}, - {PHONE_NUMBER_REGION_REPUBLIC_OF_KOREA, PHONE_NUMBER_LANG_CHINESE}, - {PHONE_NUMBER_REGION_REPUBLIC_OF_KOREA, PHONE_NUMBER_LANG_CHINESE_TRADITIONAL}, - {PHONE_NUMBER_REGION_VIET_NAM, PHONE_NUMBER_LANG_VIETNAMESE}, - {PHONE_NUMBER_REGION_CHINA, PHONE_NUMBER_LANG_CHINESE}, - {PHONE_NUMBER_REGION_CHINA, PHONE_NUMBER_LANG_CHINESE_TRADITIONAL}, - {PHONE_NUMBER_REGION_TAIWAN_PROVINCE_OF_CHINA, PHONE_NUMBER_LANG_CHINESE}, - {PHONE_NUMBER_REGION_TAIWAN_PROVINCE_OF_CHINA, PHONE_NUMBER_LANG_CHINESE_TRADITIONAL}, - {PHONE_NUMBER_REGION_TURKEY, PHONE_NUMBER_LANG_TURKISH}, - {PHONE_NUMBER_REGION_SAUDI_ARABIA, PHONE_NUMBER_LANG_ARABIC}, - {PHONE_NUMBER_REGION_ISLAMIC_REPUBLIC_OF_IRAN, PHONE_NUMBER_LANG_PERSIAN}, - {PHONE_NUMBER_REGION_SYSTEM, PHONE_NUMBER_LANG_SYSTEM}, -}; - -const struct phn_lang_info phn_lang_info_table[] = { - {"am", PHONE_NUMBER_LANG_AMHARIC}, - {"ar", PHONE_NUMBER_LANG_ARABIC}, - {"be", PHONE_NUMBER_LANG_BELARUSIAN}, - {"bg", PHONE_NUMBER_LANG_BULGARIAN}, - {"zh", PHONE_NUMBER_LANG_CHINESE}, - {"zh_Hant", PHONE_NUMBER_LANG_CHINESE_TRADITIONAL}, - {"cs", PHONE_NUMBER_LANG_CZECH}, - {"nl", PHONE_NUMBER_LANG_DUTCH}, - {"en", PHONE_NUMBER_LANG_ENGLISH}, - {"fi", PHONE_NUMBER_LANG_FINNISH}, - {"fr", PHONE_NUMBER_LANG_FRENCH}, - {"de", PHONE_NUMBER_LANG_GERMAN}, - {"el", PHONE_NUMBER_LANG_GREEK}, - {"hu", PHONE_NUMBER_LANG_HUNGARIAN}, - {"id", PHONE_NUMBER_LANG_INDONESIAN}, - {"it", PHONE_NUMBER_LANG_ITALIAN}, - {"ja", PHONE_NUMBER_LANG_JAPANESE}, - {"ko", PHONE_NUMBER_LANG_KOREAN}, - {"se", PHONE_NUMBER_LANG_NORTHERN_SAMI}, - {"fa", PHONE_NUMBER_LANG_PERSIAN}, - {"pl", PHONE_NUMBER_LANG_POLISH}, - {"pt", PHONE_NUMBER_LANG_PORTUGUESE}, - {"ro", PHONE_NUMBER_LANG_ROMANIAN}, - {"ru", PHONE_NUMBER_LANG_RUSSIAN}, - {"sr", PHONE_NUMBER_LANG_SERBIAN}, - {"se", PHONE_NUMBER_LANG_SPANISH}, - {"sv", PHONE_NUMBER_LANG_SWEDISH}, - {"th", PHONE_NUMBER_LANG_THAI}, - {"tr", PHONE_NUMBER_LANG_TURKISH}, - {"vi", PHONE_NUMBER_LANG_VIETNAMESE}, -}; - -const struct phn_region_info phn_region_info_table[] = { - {"AX", PHONE_NUMBER_REGION_ALAND_ISLANDS}, - {"AL", PHONE_NUMBER_REGION_ALBANIA}, - {"DZ", PHONE_NUMBER_REGION_ALGERIA}, - {"AS", PHONE_NUMBER_REGION_AMERICAN_SAMOA}, - {"AO", PHONE_NUMBER_REGION_ANGOLA}, - {"AI", PHONE_NUMBER_REGION_ANGUILLA}, - {"AR", PHONE_NUMBER_REGION_ARGENTINA}, - {"AM", PHONE_NUMBER_REGION_ARMENIA}, - {"AC", PHONE_NUMBER_REGION_ASCENSION_ISLAND}, - {"AG", PHONE_NUMBER_REGION_ATIGUA_AND_BARBUDA}, - {"AT", PHONE_NUMBER_REGION_AUSTRIA}, - {"BS", PHONE_NUMBER_REGION_BAHAMAS}, - {"BB", PHONE_NUMBER_REGION_BARBADOS}, - {"BY", PHONE_NUMBER_REGION_BELARUS}, - {"BE", PHONE_NUMBER_REGION_BELGIUM}, - {"BJ", PHONE_NUMBER_REGION_BENIN}, - {"BM", PHONE_NUMBER_REGION_BERMUDA}, - {"BQ", PHONE_NUMBER_REGION_BONAIRE_SINT_EUSTATIUS_AND_SABA}, - {"BW", PHONE_NUMBER_REGION_BOTSWANA}, - {"BR", PHONE_NUMBER_REGION_BRAZIL}, - {"BG", PHONE_NUMBER_REGION_BULGARIA}, - {"BF", PHONE_NUMBER_REGION_BURKINA_FASO}, - {"BI", PHONE_NUMBER_REGION_BURUNDI}, - {"CV", PHONE_NUMBER_REGION_CABO_VERDE}, - {"CM", PHONE_NUMBER_REGION_CAMEROON}, - {"CA", PHONE_NUMBER_REGION_CANADA}, - {"KY", PHONE_NUMBER_REGION_CAYMAN_ISLAND}, - {"CL", PHONE_NUMBER_REGION_CHILE}, - {"CN", PHONE_NUMBER_REGION_CHINA}, - {"CO", PHONE_NUMBER_REGION_COLOMBIA}, - {"KM", PHONE_NUMBER_REGION_COMOROS}, - {"CG", PHONE_NUMBER_REGION_CONGO}, - {"CI", PHONE_NUMBER_REGION_COTE_D_IVOIRE}, - {"CU", PHONE_NUMBER_REGION_CUBA}, - {"CW", PHONE_NUMBER_REGION_CURACAO}, - {"CZ", PHONE_NUMBER_REGION_CZECH_REPUBLIC}, - {"CD", PHONE_NUMBER_REGION_DEMOCRATIC_REPUBLIC_OF_THE_CONGO}, - {"DM", PHONE_NUMBER_REGION_DOMINICA}, - {"EG", PHONE_NUMBER_REGION_EGYPT}, - {"EE", PHONE_NUMBER_REGION_ESTONIA}, - {"ET", PHONE_NUMBER_REGION_ETHIOPIA}, - {"FI", PHONE_NUMBER_REGION_FINLAND}, - {"FR", PHONE_NUMBER_REGION_FRANCE}, - {"GA", PHONE_NUMBER_REGION_GABON}, - {"GM", PHONE_NUMBER_REGION_GAMBIA}, - {"DE", PHONE_NUMBER_REGION_GERMANY}, - {"GH", PHONE_NUMBER_REGION_GHANA}, - {"GR", PHONE_NUMBER_REGION_GREECE}, - {"GL", PHONE_NUMBER_REGION_GREENLAND}, - {"GD", PHONE_NUMBER_REGION_GRENADA}, - {"GU", PHONE_NUMBER_REGION_GUAM}, - {"GG", PHONE_NUMBER_REGION_GUERNSEY}, - {"GN", PHONE_NUMBER_REGION_GUINEA}, - {"GW", PHONE_NUMBER_REGION_GUINEA_BISSAU}, - {"HU", PHONE_NUMBER_REGION_HUNGARY}, - {"IS", PHONE_NUMBER_REGION_ICELAND}, - {"IN", PHONE_NUMBER_REGION_INDIA}, - {"ID", PHONE_NUMBER_REGION_INDONESIA}, - {"IE", PHONE_NUMBER_REGION_IRELAND}, - {"IR", PHONE_NUMBER_REGION_ISLAMIC_REPUBLIC_OF_IRAN}, - {"IM", PHONE_NUMBER_REGION_ISLE_OF_MAN}, - {"IT", PHONE_NUMBER_REGION_ITALY}, - {"JM", PHONE_NUMBER_REGION_JAMAICA}, - {"JP", PHONE_NUMBER_REGION_JAPAN}, - {"JE", PHONE_NUMBER_REGION_JERSEY}, - {"JO", PHONE_NUMBER_REGION_JORDAN}, - {"KZ", PHONE_NUMBER_REGION_KAZAKHSTAN}, - {"KE", PHONE_NUMBER_REGION_KENYA}, - {"KI", PHONE_NUMBER_REGION_KIRIBATI}, - {"LV", PHONE_NUMBER_REGION_LATVIA}, - {"LS", PHONE_NUMBER_REGION_LESOTHO}, - {"LT", PHONE_NUMBER_REGION_LITHUANIA }, - {"LU", PHONE_NUMBER_REGION_LUXEMBOURG}, - {"MG", PHONE_NUMBER_REGION_MADAGASCAR}, - {"MR", PHONE_NUMBER_REGION_MAURITANIA}, - {"MU", PHONE_NUMBER_REGION_MAURITIUS }, - {"DO", PHONE_NUMBER_REGION_DOMINICAN_REPUBLIC }, - {"MS", PHONE_NUMBER_REGION_MONTSERRAT}, - {"MA", PHONE_NUMBER_REGION_MOROCCO}, - {"MZ", PHONE_NUMBER_REGION_MOZAMBIQUE}, - {"NA", PHONE_NUMBER_REGION_NAMIBIA}, - {"NL", PHONE_NUMBER_REGION_NETHERLANDS}, - {"NG", PHONE_NUMBER_REGION_NIGERIA}, - {"MP", PHONE_NUMBER_REGION_NORTHERN_MARIANA_ISLANDS}, - {"NO", PHONE_NUMBER_REGION_NORWAY}, - {"PE", PHONE_NUMBER_REGION_PERU}, - {"PL", PHONE_NUMBER_REGION_POLAND}, - {"PT", PHONE_NUMBER_REGION_PORTUGAL}, - {"PR", PHONE_NUMBER_REGION_PUERTO_RICO}, - {"KR", PHONE_NUMBER_REGION_REPUBLIC_OF_KOREA}, - {"MD", PHONE_NUMBER_REGION_REPUBLIC_OF_MOLDOVA}, - {"RO", PHONE_NUMBER_REGION_ROMANIA}, - {"RU", PHONE_NUMBER_REGION_RUSSIAN_FEDERATION}, - {"SH", PHONE_NUMBER_REGION_SAINT_HELENA}, - {"KN", PHONE_NUMBER_REGION_SAINT_KITTS_AND_NEVIS}, - {"LC", PHONE_NUMBER_REGION_SAINT_LUCIA}, - {"VC", PHONE_NUMBER_REGION_SAINT_VINCENT_AND_THE_GRENADINES}, - {"ST", PHONE_NUMBER_REGION_SAO_TOME_AND_PRINCIPE}, - {"SA", PHONE_NUMBER_REGION_SAUDI_ARABIA}, - {"SN", PHONE_NUMBER_REGION_SENEGAL}, - {"RS", PHONE_NUMBER_REGION_SERBIA}, - {"SL", PHONE_NUMBER_REGION_SIERRA_LEONE}, - {"SX", PHONE_NUMBER_REGION_SINT_MAARTEN}, - {"SK", PHONE_NUMBER_REGION_SLOVAKIA}, - {"SO", PHONE_NUMBER_REGION_SOMALIA}, - {"ZA", PHONE_NUMBER_REGION_SOUTH_AFRICA}, - {"ES", PHONE_NUMBER_REGION_SPAIN}, - {"LK", PHONE_NUMBER_REGION_SRI_LANKA}, - {"SD", PHONE_NUMBER_REGION_SUDAN}, - {"SJ", PHONE_NUMBER_REGION_SVALBARD_AND_JAN_MAYEN}, - {"SZ", PHONE_NUMBER_REGION_SWAZILAND}, - {"SE", PHONE_NUMBER_REGION_SWEDEN}, - {"CH", PHONE_NUMBER_REGION_SWITZERLAND}, - {"TW", PHONE_NUMBER_REGION_TAIWAN_PROVINCE_OF_CHINA}, - {"TH", PHONE_NUMBER_REGION_THAILAND}, - {"MK", PHONE_NUMBER_REGION_THE_FORMER_YUGOSLAV_REPUBLIC_OF_MACEDONIA}, - {"TG", PHONE_NUMBER_REGION_TOGO}, - {"TT", PHONE_NUMBER_REGION_TRINIDAD_AND_TOBAGO}, - {"TA", PHONE_NUMBER_REGION_TRISTAN_DA_CUNHA}, - {"TN", PHONE_NUMBER_REGION_TUNISIA}, - {"TR", PHONE_NUMBER_REGION_TURKEY}, - {"TC", PHONE_NUMBER_REGION_TURKS_AND_CAICOS_ISLANDS}, - {"UG", PHONE_NUMBER_REGION_UGANDA}, - {"GB", PHONE_NUMBER_REGION_UNITED_KINGDOM}, - {"US", PHONE_NUMBER_REGION_UNITED_STATES_OF_AMERICA}, - {"VE", PHONE_NUMBER_REGION_VENEZUELA}, - {"VN", PHONE_NUMBER_REGION_VIET_NAM}, - {"VG", PHONE_NUMBER_REGION_VIRGIN_ISLAND_BRITISH}, - {"VI", PHONE_NUMBER_REGION_VIRGIN_ISLAND_US}, - {"EH", PHONE_NUMBER_REGION_WESTERN_SAHARA}, - {"ZM", PHONE_NUMBER_REGION_ZAMBIA}, - {"ZW", PHONE_NUMBER_REGION_ZIMBABWE}, -}; - -int phn_region_data_get_region_str(phone_number_region_e region, char **region_str) -{ - int ret, i; - - if (PHONE_NUMBER_REGION_SYSTEM == region) { - char *str = NULL; - ret = system_settings_get_value_string(SYSTEM_SETTINGS_KEY_LOCALE_COUNTRY, &str); - if (str) - *region_str = g_strdup(strchr(str, '_') + 1); - free(str); - if (SYSTEM_SETTINGS_ERROR_NONE != ret) { - ERR("system_settings_get_value_string() Fail(%d)", ret); - return PHONE_NUMBER_ERROR_NOT_SUPPORTED; - } - return PHONE_NUMBER_ERROR_NONE; - } - - for (i = 0; i < sizeof(phn_region_info_table) / sizeof(struct phn_region_info); i++) { - if (phn_region_info_table[i].region == region) { - *region_str = g_strdup(phn_region_info_table[i].region_str); - return PHONE_NUMBER_ERROR_NONE; - } - } - return PHONE_NUMBER_ERROR_NOT_SUPPORTED; -} - -int phn_region_data_get_lang_str(phone_number_lang_e lang, char **lang_str) -{ - int ret, i; - - if (PHONE_NUMBER_LANG_SYSTEM == lang) { - char *str = NULL; - ret = system_settings_get_value_string(SYSTEM_SETTINGS_KEY_LOCALE_LANGUAGE, &str); - if (str) - *lang_str = g_strdup(strtok(str, "_")); - free(str); - if (SYSTEM_SETTINGS_ERROR_NONE != ret) { - ERR("system_settings_get_value_string() Fail(%d)", ret); - return PHONE_NUMBER_ERROR_NOT_SUPPORTED; - } - return PHONE_NUMBER_ERROR_NONE; - } - - for (i = 0; i < sizeof(phn_lang_info_table) / sizeof(struct phn_lang_info); i++) { - if (phn_lang_info_table[i].lang == lang) { - *lang_str = g_strdup(phn_lang_info_table[i].lang_str); - return PHONE_NUMBER_ERROR_NONE; - } - } - return PHONE_NUMBER_ERROR_NOT_SUPPORTED; -} - -bool phn_region_data_find_match_info(phone_number_region_e region, - phone_number_lang_e lang) -{ - int i; - for (i = 0; i < sizeof(phn_match_info_table) / sizeof(struct phn_match_info); i++) { - if (phn_match_info_table[i].region == region - && phn_match_info_table[i].lang == lang) { - return true; - } - } - return false; -} - diff --git a/src/phn_region_data.h b/src/phn_region_data.h deleted file mode 100644 index 3c674df..0000000 --- a/src/phn_region_data.h +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Phonenumber Utils - * - * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ -#ifndef __PHN_REGION_DATA_H__ -#define __PHN_REGION_DATA_H__ - -#include "phone_number_types.h" - -#define PHN_REGION_DEFAULT_LANG "en" - -int phn_region_data_get_region_str(phone_number_region_e region, char **region_str); -int phn_region_data_get_lang_str(phone_number_lang_e lang, char **lang_str); -bool phn_region_data_find_match_info(phone_number_region_e region, - phone_number_lang_e lang); - -#endif /* __PHN_REGION_DATA_H__ */ diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..870915a --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,20 @@ +INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/common) +INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include) +LINK_DIRECTORIES(${CMAKE_BINARY_DIR}) + +SET(TEST_TARGET "${PROJECT_NAME}-test") + +FILE(GLOB TEST_SRCS *.c) + +pkg_check_modules(test_pkgs REQUIRED dlog) +INCLUDE_DIRECTORIES(${test_pkgs_INCLUDE_DIRS}) +LINK_DIRECTORIES(${test_pkgs_LIBRARY_DIRS}) + +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIE") +SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIE") +SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--hash-style=both -pie") + +ADD_EXECUTABLE(${TEST_TARGET} ${TEST_SRCS}) +ADD_DEPENDENCIES(${TEST_TARGET} GENERATED_DBUS_CODE) +TARGET_LINK_LIBRARIES(${TEST_TARGET} ${test_pkgs_LIBRARIES} ${CLIENT}) +INSTALL(TARGETS ${TEST_TARGET} DESTINATION bin) \ No newline at end of file diff --git a/test/phn-test.c b/test/phn-test.c new file mode 100644 index 0000000..345f30c --- /dev/null +++ b/test/phn-test.c @@ -0,0 +1,103 @@ +/* + * Phonenumber Utils + * + * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include "phone_number.h" +#include "phn-log.h" + +int main(int argc, char **argv) +{ + + DBG("start test main !!!"); + + char *location = NULL; + char *formatted_number = NULL; + char *normalized_number = NULL; + char test_number[11] = {0}; + int region = PHONE_NUMBER_REGION_REPUBLIC_OF_KOREA; + int lang = PHONE_NUMBER_LANG_KOREAN; + int count = 0; + int ret; + + ret = phone_number_connect(); + + if (PHONE_NUMBER_ERROR_NONE != ret) { + ERR("client open error!!"); + return 0; + } + + while(1) { + + if (count > 5) { + count = 0; + break; + } + + /* test number start from 0212340000 to 0212340005*/ + snprintf(test_number, sizeof(test_number), "021234%04d", count); + DBG("========== start test with number[%s], region[%d], lang[%d] =====", test_number, region, lang); + + DBG("=========== Call get location ======================="); + ret = phone_number_get_location_from_number(test_number, region, lang, &location); + if (PHONE_NUMBER_ERROR_NONE != ret) { + ERR("phone_number_get_location_from_number() Fail(%d)", ret); + phone_number_disconnect(); + return -1; + } + /* location - 'seoul' */ + DBG("location = %s", location); + free(location); + location = NULL; + + + DBG("=========== Call get formatted number ======================="); + ret = phone_number_get_formatted_number(test_number, region, &formatted_number); + if (PHONE_NUMBER_ERROR_NONE != ret) { + ERR("phone_number_get_formatted_number() Fail(%d)", ret); + phone_number_disconnect(); + return -1; + } + + /* formatted_number - '02-1234-0001' */ + DBG("formatted_number = %s", formatted_number); + free(formatted_number); + formatted_number = NULL; + + DBG("=========== Call get normalized number ======================="); + ret = phone_number_get_normalized_number(test_number, &normalized_number); + if (PHONE_NUMBER_ERROR_NONE != ret) { + ERR("phone_number_get_normalized_number() Fail(%d)", ret); + phone_number_disconnect(); + return -1; + } + + /* normalized_number - '+82212340001' */ + DBG("normalized_number = %s", normalized_number); + free(normalized_number); + normalized_number = NULL; + + count++; + } + phone_number_disconnect(); + + DBG("end test main !!!"); + return 0; +} + + +