From b24d4e39d843c9e7a51dedd332c676009431eb6b Mon Sep 17 00:00:00 2001 From: Eunyoung Lee Date: Thu, 21 Dec 2017 16:14:39 +0900 Subject: [PATCH] Added ip addr to json data sent to the web server Change-Id: I2b1fa64a6339075f17148900157b504902472e00 --- CMakeLists.txt | 1 + inc/connection_manager.h | 32 ++++ inc/webutil.h | 3 +- packaging/org.tizen.position-finder-server.spec | 1 + src/connection_manager.c | 226 ++++++++++++++++++++++++ src/connectivity.c | 14 ++ src/controller_internal.c | 6 + src/webutil.c | 10 +- 8 files changed, 289 insertions(+), 4 deletions(-) create mode 100644 inc/connection_manager.h create mode 100644 src/connection_manager.c diff --git a/CMakeLists.txt b/CMakeLists.txt index a20c2c4..4d2914d 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,6 +43,7 @@ ADD_EXECUTABLE(${PROJECT_NAME} ${PROJECT_ROOT_DIR}/src/controller_internal.c ${PROJECT_ROOT_DIR}/src/controller_util.c ${PROJECT_ROOT_DIR}/src/connectivity.c + ${PROJECT_ROOT_DIR}/src/connection_manager.c ${PROJECT_ROOT_DIR}/src/webutil.c ${PROJECT_ROOT_DIR}/src/resource.c ${PROJECT_ROOT_DIR}/src/resource/resource_illuminance_sensor.c diff --git a/inc/connection_manager.h b/inc/connection_manager.h new file mode 100644 index 0000000..90e5fab --- /dev/null +++ b/inc/connection_manager.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd. + * + * Contact: Jin Yoon + * Geunsun Lee + * Eunyoung Lee + * Junkyu Han + * Jeonghoon Park + * + * Licensed under the Flora License, Version 1.1 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://floralicense.org/license/ + * + * 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 __POSITION_FINDER_CONN_MGR_H__ +#define __POSITION_FINDER_CONN_MGR_H__ + +int connection_manager_get_ip(const char **ip); +int connection_manager_init(void); +int connection_manager_fini(void); + +#endif /* __POSITION_FINDER_CONN_MGR_H__ */ + diff --git a/inc/webutil.h b/inc/webutil.h index 0a78fdb..8947bc2 100644 --- a/inc/webutil.h +++ b/inc/webutil.h @@ -61,7 +61,8 @@ struct _web_util_sensor_data_s { int touch; int gas; web_util_sensor_type_e enabled_sensor; - char *hash; + const char *hash; + const char *ip_addr; }; int web_util_noti_init(void); diff --git a/packaging/org.tizen.position-finder-server.spec b/packaging/org.tizen.position-finder-server.spec index 8bc5343..dd4e98a 100644 --- a/packaging/org.tizen.position-finder-server.spec +++ b/packaging/org.tizen.position-finder-server.spec @@ -27,6 +27,7 @@ BuildRequires: pkgconfig(libcurl) BuildRequires: pkgconfig(glib-2.0) BuildRequires: pkgconfig(json-glib-1.0) BuildRequires: pkgconfig(capi-system-info) +BuildRequires: pkgconfig(capi-network-connection) %description Server for Position Finder diff --git a/src/connection_manager.c b/src/connection_manager.c new file mode 100644 index 0000000..ef5fd7e --- /dev/null +++ b/src/connection_manager.c @@ -0,0 +1,226 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd. + * + * Contact: Jin Yoon + * Geunsun Lee + * Eunyoung Lee + * Junkyu Han + * + * Licensed under the Flora License, Version 1.1 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://floralicense.org/license/ + * + * 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. + */ + /* + * Copyright (c) 2017 Samsung Electronics Co., Ltd. + * + * Contact: Jin Yoon + * Geunsun Lee + * Eunyoung Lee + * Junkyu Han + * Jeonghoon Park + * + * Licensed under the Flora License, Version 1.1 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://floralicense.org/license/ + * + * 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 "log.h" + +struct conn_mgr_s { + connection_h connection; + connection_type_e net_state; + connection_wifi_state_e wifi_state; + char *ip_addr; +}; + +struct conn_mgr_s conn_mgr = { 0, }; + +static const char *__connection_error_to_string(connection_error_e error) +{ + switch (error) { + case CONNECTION_ERROR_NONE: + return "CONNECTION_ERROR_NONE"; + case CONNECTION_ERROR_INVALID_PARAMETER: + return "CONNECTION_ERROR_INVALID_PARAMETER"; + case CONNECTION_ERROR_OUT_OF_MEMORY: + return "CONNECTION_ERROR_OUT_OF_MEMORY"; + case CONNECTION_ERROR_INVALID_OPERATION: + return "CONNECTION_ERROR_INVALID_OPERATION"; + case CONNECTION_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED: + return "CONNECTION_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED"; + case CONNECTION_ERROR_OPERATION_FAILED: + return "CONNECTION_ERROR_OPERATION_FAILED"; + case CONNECTION_ERROR_ITERATOR_END: + return "CONNECTION_ERROR_ITERATOR_END"; + case CONNECTION_ERROR_NO_CONNECTION: + return "CONNECTION_ERROR_NO_CONNECTION"; + case CONNECTION_ERROR_NOW_IN_PROGRESS: + return "CONNECTION_ERROR_NOW_IN_PROGRESS"; + case CONNECTION_ERROR_ALREADY_EXISTS: + return "CONNECTION_ERROR_ALREADY_EXISTS"; + case CONNECTION_ERROR_OPERATION_ABORTED: + return "CONNECTION_ERROR_OPERATION_ABORTED"; + case CONNECTION_ERROR_DHCP_FAILED: + return "CONNECTION_ERROR_DHCP_FAILED"; + case CONNECTION_ERROR_INVALID_KEY: + return "CONNECTION_ERROR_INVALID_KEY"; + case CONNECTION_ERROR_NO_REPLY: + return "CONNECTION_ERROR_NO_REPLY"; + case CONNECTION_ERROR_PERMISSION_DENIED: + return "CONNECTION_ERROR_PERMISSION_DENIED"; + case CONNECTION_ERROR_NOT_SUPPORTED: + return "CONNECTION_ERROR_NOT_SUPPORTED"; + default: + return "CONNECTION_ERROR_UNKNOWN"; + } +} + +static void __conn_mgr_ip_changed_cb(const char* ipv4_address, + const char* ipv6_address, void* user_data) +{ + _D("ip changed from[%s] to[%s]", conn_mgr.ip_addr, ipv4_address); + + if (conn_mgr.ip_addr) { + free(conn_mgr.ip_addr); + conn_mgr.ip_addr = strdup(ipv4_address); + } + return; +} + +static void __conn_mgr_connection_changed_cb(connection_type_e type, void* user_data) +{ + int ret = CONNECTION_ERROR_NONE; + _D("connection changed from[%d] to[%d]", conn_mgr.net_state, type); + + conn_mgr.net_state = type; + if (conn_mgr.net_state != CONNECTION_TYPE_WIFI) { + ret = connection_get_wifi_state(conn_mgr.connection, &conn_mgr.wifi_state); + if (CONNECTION_ERROR_NONE != ret) + _E("fail connection_get_wifi_state - [%s]", + __connection_error_to_string(ret)); + else + _D("net_state[%d] - wifi_state[%d]", + conn_mgr.net_state, conn_mgr.wifi_state); + } + + return; +} + +int connection_manager_get_ip(const char **ip) +{ + int ret = CONNECTION_ERROR_NONE; + + retv_if(conn_mgr.connection == NULL, -1); + retv_if(ip == NULL, -1); + + if (conn_mgr.ip_addr) { + *ip = conn_mgr.ip_addr; + return 0; + } + + if (conn_mgr.net_state == CONNECTION_TYPE_DISCONNECTED) { + _W("disconnected now"); + if (conn_mgr.ip_addr) { + free(conn_mgr.ip_addr); + conn_mgr.ip_addr = NULL; + } + return -1; + } + + ret = connection_get_ip_address(conn_mgr.connection, + CONNECTION_ADDRESS_FAMILY_IPV4, &conn_mgr.ip_addr); + if ((CONNECTION_ERROR_NONE != ret) || (conn_mgr.ip_addr == NULL)) { + _E("fail to connection_get_ip_address() - [%s]", + __connection_error_to_string(ret)); + return -1; + } + + return 0; +} + +int connection_manager_init(void) +{ + int ret = CONNECTION_ERROR_NONE; + if (conn_mgr.connection) { + _W("connection manager is already initialized"); + return 0; + } + + ret = connection_create(&conn_mgr.connection); + if (CONNECTION_ERROR_NONE != ret) { + _E("fail to create connection - [%s]", + __connection_error_to_string(ret)); + return -1; + } + + ret = connection_get_type(conn_mgr.connection, &conn_mgr.net_state); + if (CONNECTION_ERROR_NONE != ret) + _E("fail connection_get_type - [%s]", + __connection_error_to_string(ret)); + + if (conn_mgr.net_state != CONNECTION_TYPE_DISCONNECTED) { + ret = connection_get_ip_address(conn_mgr.connection, + CONNECTION_ADDRESS_FAMILY_IPV4, &conn_mgr.ip_addr); + if ((CONNECTION_ERROR_NONE != ret) || (conn_mgr.ip_addr == NULL)) + _E("fail to connection_get_ip_address() - [%s]", + __connection_error_to_string(ret)); + } + + ret = connection_set_type_changed_cb(conn_mgr.connection, + __conn_mgr_connection_changed_cb, NULL); + if (CONNECTION_ERROR_NONE != ret) + _E("fail connection_set_type_changed_cb - [%s]", + __connection_error_to_string(ret)); + + ret = connection_get_wifi_state(conn_mgr.connection, &conn_mgr.wifi_state); + if (CONNECTION_ERROR_NONE != ret) + _E("fail connection_get_wifi_state - [%s]", + __connection_error_to_string(ret)); + + ret = connection_set_ip_address_changed_cb(conn_mgr.connection, + __conn_mgr_ip_changed_cb, NULL); + if (CONNECTION_ERROR_NONE != ret) + _E("fail toconnection_set_ip_address_changed_cb - [%s]", + __connection_error_to_string(ret)); + + _D("net_state[%d], wifi_state[%d], ip address[%s]", + conn_mgr.net_state, conn_mgr.wifi_state, conn_mgr.ip_addr); + + return 0; +} + +int connection_manager_fini(void) +{ + if (conn_mgr.connection) { + int ret = 0; + ret = connection_destroy(conn_mgr.connection); + _D("connection_destroy - [%s]", __connection_error_to_string(ret)); + conn_mgr.connection = NULL; + } + + if (conn_mgr.ip_addr) { + free(conn_mgr.ip_addr); + conn_mgr.ip_addr = NULL; + } + return 0; +} diff --git a/src/connectivity.c b/src/connectivity.c index d1b0a5f..b9c32bb 100644 --- a/src/connectivity.c +++ b/src/connectivity.c @@ -30,6 +30,7 @@ #include "connectivity.h" #include "webutil.h" #include "controller_util.h" +#include "connection_manager.h" #define DEFAULT_RESOURCE_TYPE "org.tizen.door" #define BUFSIZE 1024 @@ -41,6 +42,7 @@ struct _connectivity_resource { char *path; char *type; + char *ip; connectivity_protocol_e protocol_type; GHashTable *value_hash; union { @@ -629,6 +631,7 @@ int connectivity_notify_bool(connectivity_resource_s *resource_info, const char web_util_json_add_string("SensorPiID", resource_info->path); web_util_json_add_string("SensorPiType", resource_info->type); + web_util_json_add_string("SensorPiIP", resource_info->ip); web_util_json_add_boolean(key, value); web_util_json_end(); @@ -681,6 +684,7 @@ int connectivity_notify_int(connectivity_resource_s *resource_info, const char * web_util_json_add_string("SensorPiID", resource_info->path); web_util_json_add_string("SensorPiType", resource_info->type); + web_util_json_add_string("SensorPiIP", resource_info->ip); web_util_json_add_int(key, value); web_util_json_end(); @@ -732,6 +736,7 @@ int connectivity_notify_double(connectivity_resource_s *resource_info, const cha web_util_json_add_string("SensorPiID", resource_info->path); web_util_json_add_string("SensorPiType", resource_info->type); + web_util_json_add_string("SensorPiIP", resource_info->ip); web_util_json_add_double(key, value); web_util_json_end(); @@ -784,6 +789,7 @@ int connectivity_notify_string(connectivity_resource_s *resource_info, const cha web_util_json_add_string("SensorPiID", resource_info->path); web_util_json_add_string("SensorPiType", resource_info->type); + web_util_json_add_string("SensorPiIP", resource_info->ip); web_util_json_add_string(key, value); web_util_json_end(); @@ -1132,6 +1138,7 @@ int connectivity_attributes_notify_all(connectivity_resource_s *resource_info) web_util_json_add_string("SensorPiID", resource_info->path); web_util_json_add_string("SensorPiType", resource_info->type); + web_util_json_add_string("SensorPiIP", resource_info->ip); g_hash_table_foreach(resource_info->value_hash, __json_add_data_iter_cb, NULL); web_util_json_end(); @@ -1170,6 +1177,7 @@ void connectivity_unset_resource(connectivity_resource_s *resource_info) if (resource_info->path) free(resource_info->path); if (resource_info->type) free(resource_info->type); + if (resource_info->ip) free(resource_info->ip); free(resource_info); return; @@ -1178,6 +1186,7 @@ void connectivity_unset_resource(connectivity_resource_s *resource_info) int connectivity_set_resource(const char *path, const char *type, connectivity_resource_s **out_resource_info) { connectivity_resource_s *resource_info = NULL; + const char *ip = NULL; int ret = -1; retv_if(!path, -1); @@ -1193,6 +1202,10 @@ int connectivity_set_resource(const char *path, const char *type, connectivity_r resource_info->type = strdup(type); goto_if(!resource_info->type, error); + connection_manager_get_ip(&ip); + resource_info->ip = strdup(ip); + goto_if(!resource_info->ip, error); + resource_info->protocol_type = ProtocolType; _D("Path[%s], Type[%s], protocol_type[%d]" , resource_info->path, resource_info->type, resource_info->protocol_type); @@ -1222,6 +1235,7 @@ int connectivity_set_resource(const char *path, const char *type, connectivity_r error: if (resource_info->path) free(resource_info->path); if (resource_info->type) free(resource_info->type); + if (resource_info->ip) free(resource_info->ip); if (resource_info) free(resource_info); return -1; diff --git a/src/controller_internal.c b/src/controller_internal.c index 97e8499..410669e 100644 --- a/src/controller_internal.c +++ b/src/controller_internal.c @@ -25,9 +25,13 @@ #include "connectivity.h" #include "resource.h" #include "controller_util.h" +#include "webutil.h" +#include "connection_manager.h" void controller_init_internal_functions(void) { + connection_manager_init(); + web_util_noti_init(); return; } @@ -35,6 +39,8 @@ void controller_fini_internal_functions(void) { _I("Terminating..."); resource_close_all(); + web_util_noti_fini(); + connection_manager_fini(); controller_util_free(); return; diff --git a/src/webutil.c b/src/webutil.c index f498071..27f8a37 100644 --- a/src/webutil.c +++ b/src/webutil.c @@ -367,6 +367,7 @@ int web_util_json_add_sensor_data(const char* sensorpi_id, web_util_sensor_data_ const char n_gas[] = "Gas"; const char n_e_sensor[] = "SensorEnabled"; const char n_hash[] = "Hash"; + const char n_ip[] = "SensorPiIP"; retv_if(!sensorpi_id, -1); retv_if(Json_h.builder == NULL, -1); @@ -377,6 +378,7 @@ int web_util_json_add_sensor_data(const char* sensorpi_id, web_util_sensor_data_ /* JSON structure : { SensorPiID: string, + SensorPiIP: string, Motion: boolean, Flame: boolean, Humidity: double, @@ -403,6 +405,11 @@ int web_util_json_add_sensor_data(const char* sensorpi_id, web_util_sensor_data_ json_builder_set_member_name(Json_h.builder, n_id); json_builder_add_string_value(Json_h.builder, sensorpi_id); + if (sensor_data->ip_addr) { + json_builder_set_member_name(Json_h.builder, n_ip); + json_builder_add_string_value(Json_h.builder, sensor_data->ip_addr); + } + if (sensor_data->enabled_sensor & WEB_UTIL_SENSOR_MOTION) { json_builder_set_member_name(Json_h.builder, n_motion); json_builder_add_int_value(Json_h.builder, sensor_data->motion); @@ -563,9 +570,6 @@ error_release_all: if (root) json_node_free(root); - if (gen) - g_object_unref(gen); - return NULL; } -- 2.7.4