From 88c909cb0e8def273f701d214692501d0e503df2 Mon Sep 17 00:00:00 2001 From: "jiehwan.park" Date: Wed, 27 Dec 2017 15:43:01 +0900 Subject: [PATCH] enable wifi setting Signed-off-by: jiehwan.park --- include/setup_network.h | 63 ---- packaging/setup-adaptor.spec | 13 - src/adaptor.c | 383 ++++------------------ src/input_file.c | 211 +++++++++--- {include => src}/input_file.h | 16 +- {include => src}/sa_common.h | 11 + {include => src}/sa_types.h | 0 src/setup_network.c | 559 ++++++++++++-------------------- src/setup_network.h | 30 ++ src/setup_system.c | 47 ++- {include => src}/setup_system.h | 19 +- 11 files changed, 541 insertions(+), 811 deletions(-) delete mode 100644 include/setup_network.h rename {include => src}/input_file.h (74%) rename {include => src}/sa_common.h (82%) rename {include => src}/sa_types.h (100%) create mode 100644 src/setup_network.h rename {include => src}/setup_system.h (56%) diff --git a/include/setup_network.h b/include/setup_network.h deleted file mode 100644 index 8be4b2e..0000000 --- a/include/setup_network.h +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd. - * - * 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 __SETUP_NETWORK_H__ -#define __SETUP_NETWORK_H__ - -typedef enum { - NETWORK_WIFI_STATE_DEINIT, - NETWORK_WIFI_STATE_DEACTIVATING, - NETWORK_WIFI_STATE_DEACTIVATED, - NETWORK_WIFI_STATE_ACTIVATING, - NETWORK_WIFI_STATE_ACTIVATED, - NETWORK_WIFI_STATE_SCANNING, - NETWORK_WIFI_STATE_SCANNED, - NETWORK_WIFI_STATE_CONNECTING, - NETWORK_WIFI_STATE_CONNECTED, -} network_wifi_state_e; - -/** - * @fn int sa_network_get_state(int conn_state) - * @brief This function to get connection state - * @param conn_state [out] state of connection - * @param conn_type [out] type of connection - * @return int return of function - */ -sa_error_e sa_network_get_state(sa_network_state_e *conn_state, sa_network_type_e *conn_type); - -/** - * @fn int sa_network_set_connection(sa_network_s *info) - * @brief This function to set connection with info - @param conn_type [in] type of connection - * @return sa_error_e return of function - */ -sa_error_e sa_network_set_connection(sa_network_s *info); - -/** - * @fn int sa_network_get_connection(sa_network_s *info) - * @brief This function to get connection with info - * @param conn_type [out] connection info - * @return sa_error_e return of function - */ -sa_error_e sa_network_get_connection(sa_network_s *info); - -/** - * @fn int sa_network_deactivate(void) - * @brief This function to deactivate current connection - * @return sa_error_e return of function - */ -sa_error_e sa_network_deactivate(void); - -#endif/* __SETUP_NETWORK_H__ */ diff --git a/packaging/setup-adaptor.spec b/packaging/setup-adaptor.spec index 3d8e2c4..db0d47a 100644 --- a/packaging/setup-adaptor.spec +++ b/packaging/setup-adaptor.spec @@ -46,23 +46,10 @@ install -m 0644 %SOURCE2 %{buildroot}%{_unitdir}/%{name}.service %install_service multi-user.target.wants %{name}.service %post -systemctl daemon-reload -if [ $1 == 1 ]; then - # install - systemctl start %{name}.service -elif [ $1 == 2 ]; then - # upgrade - systemctl restart %{name}.service -fi %preun -if [ $1 == 0 ]; then - # uninstall - systemctl stop %{name}.service -fi %postun -systemctl daemon-reload %files %manifest %{name}.manifest diff --git a/src/adaptor.c b/src/adaptor.c index 400c1fa..75161ff 100644 --- a/src/adaptor.c +++ b/src/adaptor.c @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include #include #include #include @@ -23,356 +24,100 @@ #include "setup_network.h" #include "input_file.h" -#define NETWORK_CHECK_RETRY_MAX 10 -static char *__print_connection_state(sa_network_state_e state) +static gboolean prepare(GSource *source, gint *timeout) { - switch (state) { - case SA_NETWORK_STATE_DISCONNECTED: - return "Disconnected"; - case SA_NETWORK_STATE_PROGRESSING: - return "Progressing"; - case SA_NETWORK_STATE_CONNECTED: - return "connected"; - default: - return "Unknown"; - } -} - -static char *__print_connection_type(sa_network_type_e type) -{ - switch (type) { - case SA_NETWORK_TYPE_WIFI: - return "wifi"; - case SA_NETWORK_TYPE_ETH: - return "ethernet"; - default: - return "Unknown"; - } -} - -static void __print_config_network_static_info(sa_network_static_s *staticInfo) -{ - if (staticInfo != NULL) { - if (staticInfo->ipAddress != NULL) - _D("static::ipAddress[%s]"); - if (staticInfo->netmask != NULL) - _D("static::netmask[%s]"); - if (staticInfo->defaultGateway != NULL) - _D("static::defaultGateway[%s]"); - if (staticInfo->primaryDnsServer != NULL) - _D("static::primaryDnsServer[%s]"); - if (staticInfo->secondaryDnsServer != NULL) - _D("static::secondaryDnsServer[%s]"); - } -} - -static void __print_config_info(sa_config_s *config) -{ - sa_network_s *network = NULL; - sa_system_s *systemData = NULL; - sa_wifi_s *wifi = NULL; - sa_eth_s *eth = NULL; - - if (config != NULL) { - if (config->version != NULL) - _D("Version [%s]", config->version); - network = config->networkData; - if (network != NULL) { - wifi = network->wifi; - if (wifi != NULL) { - _D("Network::wifi::enabled[%d]", wifi->enabled); - _D("Network::wifi::dhcpEnabled[%d]", wifi->dhcpEnabled); - if (wifi->ssid != NULL) - _D("Network::wifi::ssid[%s]", wifi->ssid); - if (wifi->password != NULL) - _D("Network::wifi::password[%s]", wifi->password); - - if (wifi->dhcpEnabled == 0) - __print_config_network_static_info(wifi->staticInfo); - } - - eth = network->eth; - if (eth != NULL) { - _D("Network::eth::enabled[%d]", eth->enabled); - _D("Network::eth::dhcpEnabled[%d]", eth->dhcpEnabled); - - if (eth->dhcpEnabled == 0) - __print_config_network_static_info(wifi->staticInfo); - } - } - - systemData = config->systemData; - if (systemData != NULL) { - if (systemData->proxy != NULL) { - _D("systemData::httpProxyHost [%s]", systemData->proxy->httpProxyHost); - _D("systemData::HttpProxyPort [%d]", systemData->proxy->httpProxyPort); - } - } - } -} - -static void __release_network_resource(sa_network_s *network) -{ - if (network != NULL) { - if (network->wifi != NULL) { - if (network->wifi->staticInfo != NULL) - free(network->wifi->staticInfo); - - free(network->wifi); - } - - if (network->eth != NULL) { - if (network->eth->staticInfo != NULL) - free(network->eth->staticInfo); - - free(network->eth); - } - - free(network); - } + return TRUE; } -static void __release_system_resource(sa_system_s *system) -{ - if (system != NULL) { - if (system->proxy != NULL) - free(system->proxy); - - free(system); - } -} - -static void __release_config_resource(sa_config_s *config) -{ - - if (config != NULL) { - if (config->networkData != NULL) - __release_network_resource(config->networkData); - - if (config->systemData != NULL) - __release_system_resource(config->systemData); - } -} - -static int __parsing_config(sa_config_s *config) +gboolean setup(gpointer data) { sa_error_e ret = SA_ERROR_NONE; - // memory will be allocated from interface layer - ret = sa_inputfile_get_config_info(config); - if (ret == SA_ERROR_NONE) { - _D("Success to get config"); - // check config info - if (config != NULL) - __print_config_info(config); - - } else { - _E("sa_inputfile_get_config_info return error [%d]", ret); - } - - return ret; -} - -static int __compare_network_info(sa_network_s *devNetwork, sa_network_s *newNetwork) -{ - int needChange = FALSE; - - if (devNetwork == NULL || newNetwork == NULL) { - return -1; - } - - // compare wifi - // wifi has higher priority to compare. - // If wifi info is changed, it doesn't have to compare ethernet info - if (newNetwork->wifi != NULL) { - if (newNetwork->wifi->enabled == TRUE) { - // in case of using wifi / check dev wifi - if (devNetwork->wifi != NULL) { - if (newNetwork->wifi->enabled == TRUE) { - // check ssid/password only - if (strcmp(devNetwork->wifi->ssid, newNetwork->wifi->ssid)) { - needChange = TRUE; - } - - if (needChange == FALSE && strcmp(devNetwork->wifi->password, newNetwork->wifi->password)) { - needChange = TRUE; - } - // TBD for checking other members such as, ipaddress, dns, gateway.... - } - } else { - needChange = TRUE; + sa_config_s config = {0,}; + sa_error_e file_read = SA_ERROR_NONE; + + _D("callback>>>setup start !!!"); + + file_read = sa_inputfile_get_config_info(&config); + if (file_read == SA_ERROR_NONE) { + if (config.systemData != NULL) { + ret = sa_setup_system(config.systemData); + if(ret != SA_ERROR_NONE) { + _E("sa_setup_system return error(%d)", ret); } } - } - - if (needChange == FALSE && newNetwork->eth != NULL) { - if (newNetwork->eth->enabled == TRUE) { - if (devNetwork->eth != NULL) { - // check ip address only - if (devNetwork->eth->staticInfo != NULL && newNetwork->eth->staticInfo != NULL) { - if (strcmp(devNetwork->eth->staticInfo->ipAddress, newNetwork->eth->staticInfo->ipAddress)) { - needChange = TRUE; - } - } - } else { - needChange = TRUE; + if (config.networkData != NULL) { + ret = sa_setup_network(config.networkData); + if(ret != SA_ERROR_NONE) { + _E("sa_setup_network return error(%d)", ret); } } } - - return needChange; + + sa_inputfile_release_resource(&config); } -static int __set_network(sa_network_s *network) +static GSourceFuncs SourceFuncs = { - sa_network_s currentNetwork = {0,}; - sa_error_e ret = SA_ERROR_NONE; - sa_network_type_e conn_type; - sa_network_state_e conn_state; - - if (network == NULL) { - _E("__set_network is null"); - return -1; - } - // 1. check network state - // 2. if it is connected, read detail info - ret = sa_network_get_state(&conn_state, &conn_type); + prepare, // prepare function + NULL, // function check + sa_inputfile_thread, // callback start + NULL +}; + +// config for main thread & callback function +static gpointer adder_thread_output (gpointer data) +{ + GSource* src = NULL; - if (ret == SA_ERROR_NONE) { - _D("Device Network : T[%s] S[%s]", __print_connection_type(conn_type), __print_connection_state(conn_state)); - if (conn_state == SA_NETWORK_STATE_CONNECTED) { - // read current network info - ret = sa_network_get_connection(¤tNetwork); - if (ret == SA_ERROR_NONE) { - // compare network info - if (__compare_network_info(¤tNetwork, network)) { - // set network connection - _D("Set Network Connection !!"); - ret = sa_network_set_connection(network); - } - } else { - _E("__get_current_network_info return error"); - } + _D("adder_thread_output ~~~"); - if (currentNetwork.wifi != NULL) { - if (currentNetwork.wifi->staticInfo != NULL) - free(currentNetwork.wifi->staticInfo); - - free(currentNetwork.wifi); - } + GMainLoop* main_loop; + GMainContext* context = NULL; - if (currentNetwork.eth != NULL) { - if (currentNetwork.eth->staticInfo != NULL) - free(currentNetwork.eth->staticInfo); + context = g_main_context_new (); + main_loop = g_main_loop_new (context, FALSE); - free(currentNetwork.eth); - } - } else { - // in case of disconnect state - _D("Set Network Connection !!"); - ret = sa_network_set_connection(network); - } - } + src = g_source_new(&SourceFuncs, sizeof(GSource)); + g_source_set_name (src, "setup-adaptor-func"); + g_source_set_priority (src, G_PRIORITY_DEFAULT); + g_source_set_callback(src, (GSourceFunc) setup, (gpointer) main_loop, NULL); // set callback function + g_source_attach (src, context); - return ret; -} - -static int compare_proxy_info(sa_proxy_s *devProxy, sa_proxy_s *newProxy) -{ - // comapre proxy - if (devProxy != NULL && newProxy != NULL) { - if (strlen(devProxy->httpProxyHost) > 0) { - if (strcmp(devProxy->httpProxyHost, newProxy->httpProxyHost)) { - // proxy info is different - return 1; - } else { - // Host address is the same, but port is different - if (devProxy->httpProxyPort != newProxy->httpProxyPort) { - return 1; - } - } - } - } - - return 0; -} - -static int __set_system(sa_system_s *system) -{ - sa_proxy_s *devProxy = NULL; - sa_error_e ret = SA_ERROR_NONE; + g_main_loop_run(main_loop); - if (system == NULL) { - _E("__set_system is null"); - return -1; - } - - if (system->proxy != NULL) { - ret = sa_system_get_proxy(devProxy); - if (ret == SA_ERROR_NONE && compare_proxy_info(devProxy, system->proxy)) { - // set proxy - ret = sa_system_set_proxy(system->proxy); - } + g_main_loop_unref(main_loop); + if(data) { + g_main_loop_quit( (GMainLoop*)data ); } - return ret; + return NULL; } -void __monitor_file_state_cb(sa_file_state_e state, void *param1, void *param2) -{ - sa_config_s config = {0,}; - - switch (state) { - case SA_FILE_STATE_REGISTERED: - _D("SA_FILE_STATE_REGISTERED"); - if (SA_ERROR_NONE == __parsing_config(&config)) { - if (config.systemData != NULL) { - _D("Set system data"); - __set_system(config.systemData); - } +int main(int argc, char *argv[]) +{ + sa_file_state_e file_state = SA_FILE_STATE_NOT_EXISTED; + sa_config_s config = {0,}; - if (config.networkData != NULL) { - _D("Set nework data"); - __set_network(config.networkData); - } - __release_config_resource(&config); - } - break; - case SA_FILE_STATE_CHANGED: - _D("SA_FILE_STATE_CHANGED"); - break; - default: - _W("Not Support"); - break; + _D("main function~~~"); + // check if exists config file + file_state = sa_inputfile_get_config_state(); + _D("first check for config enable(%d)", file_state); + if (SA_FILE_STATE_NOT_EXISTED == file_state) { + _D("config file is not existed, disable setup-adaptor"); + return 0; } - _D("callback finshied"); -} + GMainLoop *main_loop; -int main(int argc, char *argv[]) -{ - sa_error_e ret = SA_ERROR_NONE; + main_loop = g_main_loop_new(NULL, FALSE); - _D("start setup-adaptor"); - //Check Config file - if (SA_FILE_STATE_EXIST == sa_inputfile_get_config_state()) { - // Waiting for booting - sleep(10); - // Register callback to receive file change event - ret = sa_inputfile_register_cb(__monitor_file_state_cb); - if (ret == SA_ERROR_NONE) { - // Keep checking callback from input - while(1) { - sleep(1); - } - } else { - _E("__init_event_listener error [%d]", ret); - } - } else { - _D("config file is not existed, setup-adaptor exit"); - } + g_thread_create((GThreadFunc) adder_thread_output, main_loop, TRUE, NULL); + g_main_loop_run(main_loop); + g_main_loop_unref(main_loop); + return 0; } \ No newline at end of file diff --git a/src/input_file.c b/src/input_file.c index 4eb3513..38759e1 100644 --- a/src/input_file.c +++ b/src/input_file.c @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - +#include #include #include #include @@ -44,14 +44,14 @@ static void *__config_main_loop(void *arg) callback = (file_state_cb)arg; if (callback == NULL) { - _E("Ccallback is null for event"); + _D("Ccallback is null for event"); return NULL; } fd = inotify_init(); if (fd < 0) { - _E("inotify_init error"); + _D("inotify_init error"); return NULL; } @@ -71,8 +71,9 @@ static void *__config_main_loop(void *arg) _D("[debug] wd=%d mask=%d cookie=%d len=%d dir=%s", event->wd, event->mask, event->cookie, event->len, (event->mask & IN_ISDIR) ? "yes" : "no"); if (event->len) { if (event->mask & IN_CREATE) { - if (event->mask & IN_ISDIR) + if (event->mask & IN_ISDIR) { _D("The directory %s was created", event->name); + } else { _D("The file %s was create.", event->name); if (!strcmp(event->name, CONFIG_NAME)) { @@ -80,8 +81,9 @@ static void *__config_main_loop(void *arg) } } } else if (event->mask & IN_DELETE) { - if (event->mask & IN_ISDIR) + if (event->mask & IN_ISDIR) { _D("The directory %s was deleted.", event->name); + } else { _D("The file %s was deleted", event->name); if (!strcmp(event->name, CONFIG_NAME)) { @@ -108,20 +110,22 @@ static void *__config_main_loop(void *arg) inotify_rm_watch(fd, wd); close(fd); - pthread_exit((void *)0); - return NULL; } static int __init_event_listener(file_state_cb callback) { +#if 0 pthread_t p_thread; // Start thread to create in order to receive event if (pthread_create(&p_thread, NULL, &__config_main_loop, callback) < 0) { - _E("__init_event_listener create error"); + _D("__init_event_listener create error"); return -1; } +#else + __config_main_loop(callback); +#endif return 0; } @@ -185,34 +189,78 @@ static char *json_get_string_from_obj(json_object *inputObj, char *key) _D("string object [%s]", ret_buf); return ret_buf; } -static int __parse_system_proxy(json_object *inputObj, sa_system_s *system) + +static void print_config_network_static_info(sa_network_static_s *staticInfo) { - char *httpProxyHost = NULL; - int httpProxyPort; - int ret = 0; + if (staticInfo != NULL) { + if (staticInfo->ipAddress != NULL) + _D("static::ipAddress[%s]"); + if (staticInfo->netmask != NULL) + _D("static::netmask[%s]"); + if (staticInfo->defaultGateway != NULL) + _D("static::defaultGateway[%s]"); + if (staticInfo->primaryDnsServer != NULL) + _D("static::primaryDnsServer[%s]"); + if (staticInfo->secondaryDnsServer != NULL) + _D("static::secondaryDnsServer[%s]"); + } +} - if (inputObj == NULL || system == NULL) { - _E("__parse_system_proxy input error"); - return -1; +static void print_network_config(sa_network_s *network) +{ + sa_wifi_s *wifi = NULL; + sa_eth_s *eth = NULL; + + if (network != NULL) { + wifi = network->wifi; + if (wifi != NULL) { + _D("Network::wifi::enabled[%d]", wifi->enabled); + _D("Network::wifi::dhcpEnabled[%d]", wifi->dhcpEnabled); + if (wifi->ssid != NULL) + _D("Network::wifi::ssid[%s]", wifi->ssid); + if (wifi->password != NULL) + _D("Network::wifi::password[%s]", wifi->password); + + if (wifi->dhcpEnabled == 0) + print_config_network_static_info(wifi->staticInfo); + } + + eth = network->eth; + if (eth != NULL) { + _D("Network::eth::enabled[%d]", eth->enabled); + _D("Network::eth::dhcpEnabled[%d]", eth->dhcpEnabled); + + if (eth->dhcpEnabled == 0) + print_config_network_static_info(wifi->staticInfo); + } } +} - system->proxy = (sa_proxy_s *)malloc(sizeof(sa_proxy_s)); - if (system->proxy != NULL) { - //httpProxyHost - httpProxyHost = json_get_string_from_obj(inputObj, SA_CONFIG_NETWORKDATA_HTTPPROXYHOST); - if (httpProxyHost != NULL) { - memcpy(system->proxy->httpProxyHost, httpProxyHost, MIN(strlen(httpProxyHost), sizeof(system->proxy->httpProxyHost)-1)); - free(httpProxyHost); - httpProxyHost = NULL; +static void print_system_config(sa_system_s *systemData) +{ + if (systemData != NULL) { + if (systemData->proxy != NULL) { + _D("systemData::httpProxyHost [%s]", systemData->proxy->httpProxyHost); + _D("systemData::HttpProxyPort [%d]", systemData->proxy->httpProxyPort); } - //httpProxyPort - httpProxyPort = json_get_int_from_obj(inputObj, SA_CONFIG_NETWORKDATA_HTTPPROXYPORT); - } else { - ret = -1; - _E("system->proxy malloc fail"); } +} - return ret; +static void print_config_info(sa_config_s *config) +{ + if (config != NULL) { + if (config->version != NULL) { + _D("Version [%s]", config->version); + } + if(config->networkData) + { + print_network_config(config->networkData); + } + if(config->networkData) + { + print_system_config(config->systemData); + } + } } static int __parse_network_static_info(json_object *inputObj, sa_network_static_s *staticInfo) @@ -224,7 +272,7 @@ static int __parse_network_static_info(json_object *inputObj, sa_network_static_ char *secondaryDnsServer = NULL; if (inputObj == NULL || staticInfo == NULL) { - _E("__parse_network_static_info input error"); + _D("__parse_network_static_info input error"); return -1; } @@ -276,7 +324,7 @@ static int __parse_network_eth(json_object *inputObj, sa_eth_s *eth) int ret = 0; if (inputObj == NULL || eth == NULL) { - _E("__parse_network_eth input error"); + _D("__parse_network_eth input error"); return -1; } @@ -292,8 +340,6 @@ static int __parse_network_eth(json_object *inputObj, sa_eth_s *eth) } else { ret = -1; } - } else { - _D("dhcp is true"); } return ret; @@ -306,7 +352,7 @@ static int __parse_network_wifi(json_object *inputObj, sa_wifi_s *wifi) int ret = 0; if (inputObj == NULL || wifi == NULL) { - _E("__parse_network_wifi input error"); + _D("__parse_network_wifi input error"); return -1; } // enabled @@ -341,9 +387,7 @@ static int __parse_network_wifi(json_object *inputObj, sa_wifi_s *wifi) } else { ret = -1; } - } else { - _D("dhcp is true"); - } + } return ret; } @@ -355,7 +399,7 @@ static int __parse_network_data(json_object *inputObj, sa_config_s *setupConfig) int wifiRet = 0, ethRet = 0; if (inputObj == NULL) { - _E("__parse_network_data input error"); + _D("__parse_network_data input error"); return -1; } @@ -369,7 +413,7 @@ static int __parse_network_data(json_object *inputObj, sa_config_s *setupConfig) if (setupConfig->networkData->wifi != NULL) { wifiRet = __parse_network_wifi(wifiObj, setupConfig->networkData->wifi); } else { - _E("network->wifi malloc fail"); + _D("network->wifi malloc fail"); } } } @@ -382,17 +426,17 @@ static int __parse_network_data(json_object *inputObj, sa_config_s *setupConfig) if (setupConfig->networkData->eth != NULL) { ethRet = __parse_network_eth(ethernetObj, setupConfig->networkData->eth); } else { - _E("network->eth malloc fail"); + _D("network->eth malloc fail"); } } } } else { - _E("malloc fail etupConfig->networkData"); + _D("malloc fail etupConfig->networkData"); } // if both of network interfaces are failed, it would return -1 if (wifiRet != 0 && ethRet != 0) { - _E("__parse_network_data fail"); + _D("__parse_network_data fail"); return -1; } @@ -405,7 +449,7 @@ static int __parse_system_data(json_object *inputObj, sa_config_s *setupConfig) int ret = 0; char *httpProxyHost = NULL; if (inputObj == NULL) { - _E("__parse_system_data input error"); + _D("__parse_system_data input error"); return -1; } @@ -425,11 +469,11 @@ static int __parse_system_data(json_object *inputObj, sa_config_s *setupConfig) } else { ret = -1; - _E("malloc fail setupConfig->systemData->proxy"); + _D("malloc fail setupConfig->systemData->proxy"); } } else { ret = -1; - _E("malloc fail setupConfig->systemData"); + _D("malloc fail setupConfig->systemData"); } } @@ -442,7 +486,7 @@ static int __parse_version(json_object *inputObj, sa_config_s *setupConfig) char *version = NULL; if (inputObj == NULL) { - _E("__parse_version input error"); + _D("__parse_version input error"); return -1; } @@ -480,7 +524,7 @@ static int __parse_config(char *file, sa_config_s *setupConfig) configObj = json_tokener_parse(jsonData); - if (configObj != NULL) { + if (configObj != NULL && readLen > 0) { // parse version ret = __parse_version(configObj, setupConfig); if (ret == 0) { @@ -492,21 +536,21 @@ static int __parse_config(char *file, sa_config_s *setupConfig) } } else { ret = -1; - _E("ConfigObj is not existed"); + _D("ConfigObj is not existed"); } json_object_put(configObj); free(jsonData); } else { ret = -1; - _E("memory allocation fail for jsonData"); + _D("memory allocation fail for jsonData"); } } close(fd); } else { ret = -1; - _E("config file can't be opened"); + _D("config file can't be opened"); } return ret; @@ -522,10 +566,12 @@ sa_error_e sa_inputfile_get_config_info(sa_config_s *config) // parsing and fill into struct ret = __parse_config(CONFIG_FILE, config); if (ret != 0) { - _E("Config parsing error"); + _D("Config parsing error"); return SA_ERROR_PARSING; } + print_config_info(config); + return ret; } @@ -542,16 +588,75 @@ sa_file_state_e sa_inputfile_get_config_state(void) return ret; } -sa_error_e sa_inputfile_register_cb(file_state_cb callback) +static void release_system_resource(sa_system_s *system) +{ + if (system != NULL) { + if (system->proxy != NULL) + free(system->proxy); + + free(system); + } +} + +static void release_network_resource(sa_network_s *network) { + if (network != NULL) { + if (network->wifi != NULL) { + if (network->wifi->staticInfo != NULL) + free(network->wifi->staticInfo); + + free(network->wifi); + } + + if (network->eth != NULL) { + if (network->eth->staticInfo != NULL) + free(network->eth->staticInfo); + + free(network->eth); + } + + free(network); + } +} + +gboolean sa_inputfile_thread(GSource *source, GSourceFunc callbackFuntion, gpointer user_data) +{ + GMainLoop* loop = user_data; + GSource* src = NULL; + + _D("GSourceFuncs>>>Execution !!!"); + +#if 0 sa_error_e ret = SA_ERROR_NONE; if (__init_event_listener(callback)) { ret = SA_ERROR_UNKNOWN; } +#else + callbackFuntion(NULL); +#endif + + if(user_data) { + g_main_loop_quit( (GMainLoop*)user_data ); + } + + // if return value is "FALSE", function stop + // if return value is "TRUE", function restart + return FALSE; +} - return ret; +void sa_inputfile_release_resource(sa_config_s *config) +{ + + if (config != NULL) { + if (config->networkData != NULL) + release_network_resource(config->networkData); + + if (config->systemData != NULL) + release_system_resource(config->systemData); + } } + /* Sample Config */ /*{"version":"0.1","wifi":{"ssid":"XXXX","password":"XXXXXX","enabled":true,"dhcpEnabled":true,"ipAddress":"127.0.0.1","netmask":"127.0.0.1","defaultGateway":"127.0.0.1","primaryDNSServer":"127.0.0.1","secondaryDNSServer":"127.0.0.1"},"ethernet":{"enabled":false,"dhcpEnabled":true,"ipAddress":"127.0.0.1","netmask":"127.0.0.1","defaultGateway":"127.0.0.1","primaryDnsServer":"127.0.0.1","secondaryDnsServer":"127.0.0.1"},"httpProxyHost":"127.0.0.1","httpProxyPort":8080}*/ \ No newline at end of file diff --git a/include/input_file.h b/src/input_file.h similarity index 74% rename from include/input_file.h rename to src/input_file.h index 4d41641..bdc4e09 100644 --- a/include/input_file.h +++ b/src/input_file.h @@ -16,6 +16,9 @@ #ifndef __INPUT_FILE_H__ #define __INPUT_FILE_H__ +#include +#include "sa_types.h" + /** * @fn int sa_inputfile_get_config_info(sa_config_s *config) * @brief This function to get config info @@ -32,11 +35,14 @@ sa_error_e sa_inputfile_get_config_info(sa_config_s *config); sa_file_state_e sa_inputfile_get_config_state(void); /** - * @fn int sa_inputfile_register_cb(file_status_cb callback) - * @brief This function to register callback for config file - * @param file_state_cb [inout] callback function - * @return sa_error_e return of function + * @fn int sa_inputfile_thread + * @brief This function to call callback at the condition of file + * @param GSource parameter + * @return gboolean FALSE */ -sa_error_e sa_inputfile_register_cb(file_state_cb callback); +gboolean sa_inputfile_thread(GSource *source, GSourceFunc callbackFuntion, gpointer user_data); + + +void sa_inputfile_release_resource(sa_config_s *config); #endif/* __INPUT_FILE_H__ */ \ No newline at end of file diff --git a/include/sa_common.h b/src/sa_common.h similarity index 82% rename from include/sa_common.h rename to src/sa_common.h index d169280..ad762d1 100644 --- a/include/sa_common.h +++ b/src/sa_common.h @@ -25,6 +25,10 @@ #define LOG_TAG "SETUP-ADAPTOR" +#ifdef MIN +#undef MIN +#endif + #define MIN(X, Y) ((X) < (Y) ? (X) : (Y)) #ifndef TRUE #define TRUE 1 @@ -32,8 +36,15 @@ #ifndef FALSE #define FALSE 0 #endif + +#if 1 #define _E(fmt, arg...) LOGE(fmt, ##arg) #define _D(fmt, arg...) LOGD(fmt, ##arg) #define _W(fmt, arg...) LOGW(fmt, ##arg) +#else +#define _E(fmt, arg...) {printf(fmt, ##arg);printf("\n");} +#define _D(fmt, arg...) {printf(fmt, ##arg);printf("\n");} +#define _W(fmt, arg...) {printf(fmt, ##arg);printf("\n");} +#endif #endif /* __SA_COMMON_H__ */ diff --git a/include/sa_types.h b/src/sa_types.h similarity index 100% rename from include/sa_types.h rename to src/sa_types.h diff --git a/src/setup_network.c b/src/setup_network.c index c2c16ef..af51179 100644 --- a/src/setup_network.c +++ b/src/setup_network.c @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include #include #include #include @@ -22,50 +23,28 @@ #include "net_connection.h" #include "wifi-manager.h" +typedef struct { + wifi_manager_h wifi_h; + GMainLoop *loop; + char *ssid; + char *password; +} _wifi_data_s; -char wifi_password[99+1]; -int initialize_setup_network_flag; -sa_network_state_e setup_network_wifi_state; -connection_h connection_handle = NULL; -wifi_manager_h wifi_handle = NULL; -static int __get_setup_network_initialized(void) +static char *__print_connection_state(sa_network_state_e state) { - return initialize_setup_network_flag; -} - -static int __set_setup_network_initialized(int val) -{ - if (val != __get_setup_network_initialized()) { - initialize_setup_network_flag = val; - } -} - -static int __get_setup_network_wifi_state(void) -{ - return setup_network_wifi_state; -} - -static void __set_setup_network_wifi_state(sa_network_state_e val) -{ - if (val != __get_setup_network_wifi_state()) { - setup_network_wifi_state = val; - } -} - -static void __set_setup_network_wifi_password(char *password) -{ - if (strcmp(wifi_password, password)) { - memset(wifi_password, 0x00, sizeof(wifi_password)); - memcpy(wifi_password, password, MIN(strlen(password), sizeof(wifi_password)-1)); + switch (state) { + case SA_NETWORK_STATE_DISCONNECTED: + return "Disconnected"; + case SA_NETWORK_STATE_PROGRESSING: + return "Progressing"; + case SA_NETWORK_STATE_CONNECTED: + return "connected"; + default: + return "Unknown"; } } -static char *__get_setup_network_wifi_password(void) -{ - return wifi_password; -} - static const char *__print_wifi_state(connection_wifi_state_e state) { @@ -141,6 +120,18 @@ static const char *__print_connection_type(connection_type_e type) } } +static const char *__print_network_type(sa_network_type_e type) +{ + switch (type) { + case SA_NETWORK_TYPE_ETH: + return "Ethernet"; + case SA_NETWORK_TYPE_WIFI: + return "Wifi"; + default: + return "Unknown"; + } +} + static const char *__print_wifi_error(wifi_manager_error_e err_type) { switch (err_type) { @@ -200,258 +191,191 @@ static const char *__print_wifi_error(wifi_manager_error_e err_type) } -static int __check_wifi_state(connection_h connection, connection_wifi_state_e wifi_state) +static void __wifi_exit_loop(_wifi_data_s *wifi_data) { - int rv = 0; - - if (connection == NULL) { - return -1; - } - - rv = connection_get_wifi_state(connection, &wifi_state); - if (rv != CONNECTION_ERROR_NONE) { - _D("Fail to get WiFi state [%s]", __print_error(rv)); - } else { - _D("Retval = [%s] WiFi state [%s]", __print_error(rv), __print_wifi_state(wifi_state)); + _D("__wifi_exit_loop !!!"); + if( wifi_data ) { + g_main_loop_quit( (GMainLoop*)wifi_data->loop ); } - - return 0; } -static void __activated_callback(wifi_manager_error_e result, void *user_data) +static void __wifi_connected_cb(wifi_manager_error_e result, void *user_data) { - if (result == WIFI_MANAGER_ERROR_NONE) { - __set_setup_network_wifi_state(NETWORK_WIFI_STATE_ACTIVATED); - _D("Wi-Fi Activation Succeeded"); - } - else - _D("Wi-Fi Activation Failed! error : %s", __print_wifi_error(result)); -} - -static void __scan_request_callback(wifi_manager_error_e error_code, void* user_data) -{ - if (user_data != NULL) - _D("user_data : %s", (char *)user_data); - - _D("Scan Completed from scan request [%s]", __print_wifi_error(error_code)); -} + _wifi_data_s *wifi_data = (_wifi_data_s*)user_data; -static void __deactivated_callback(wifi_manager_error_e result, void* user_data) -{ - if (result == WIFI_MANAGER_ERROR_NONE) { - __set_setup_network_wifi_state(NETWORK_WIFI_STATE_DEACTIVATED); - _D("Wi-Fi Deactivation Succeeded"); - } else - _D("Wi-Fi Deactivation Failed! error : %s", __print_wifi_error(result)); + if (result == WIFI_MANAGER_ERROR_NONE){ + _D("__wifi_connected_cb() connected Successfully !!!"); + } + else { + _D("__wifi_connected_cb() connection Failed!(%s)", __print_wifi_error(result)); + } + __wifi_exit_loop(wifi_data); } -static int __compare_ap_name(const char *ap_name, const char *ap_name_part) -{ - int ap_name_len = strlen(ap_name); - int ap_name_part_len = strlen(ap_name_part); - - if (strncmp(ap_name, ap_name_part, - ap_name_len > ap_name_part_len ? ap_name_len : ap_name_part_len) == 0) - return TRUE; - else - return FALSE; -} -static void __connected_callback(wifi_manager_error_e result, void *user_data) +// CAUTION : this callback function should be boolean type. +// return true : continue search ap (not found) +// return false : stop search ap (found) +static bool __wifi_connect_ap_cb(wifi_manager_ap_h ap, void *user_data) { - if (result == WIFI_MANAGER_ERROR_NONE) - _D("Wi-Fi Connection Succeeded"); - else - _D("Wi-Fi Connection Failed! error : %s", __print_wifi_error(result)); -} + _wifi_data_s *wifi_data = (_wifi_data_s*)user_data; -static void __scan_changed_callback(wifi_manager_scan_state_e state, void* user_data) -{ - _D("Scan changed, scan state : %d", state); - if (state == WIFI_MANAGER_SCAN_STATE_NOT_SCANNING) { - __set_setup_network_wifi_state(NETWORK_WIFI_STATE_SCANNED); - } -} + _D("__wifi_connect_ap_cb()"); -static void __found_connect_ap_callback(wifi_manager_ap_h ap, void *user_data) -{ int rv = 0; char *ap_name = NULL; - char *ap_name_part = (char*)user_data; rv = wifi_manager_ap_get_essid(ap, &ap_name); if (rv != WIFI_MANAGER_ERROR_NONE) { - _D("Fail to get AP name [%s]\n", __print_wifi_error(rv)); - return; + _D("Fail to get AP name [%s]", __print_wifi_error((wifi_manager_error_e)rv)); + g_free(ap_name); + return false; + } else { + _D("AP name = [%s]/[%s]", ap_name, wifi_data->ssid); } - if (__compare_ap_name(ap_name, ap_name_part)) { + if (g_strcmp0(ap_name, wifi_data->ssid) == 0) { int required = FALSE; + _D("OK!!! connect AP [%s]", ap_name); + rv = wifi_manager_ap_is_passphrase_required(ap, &required); if (rv == WIFI_MANAGER_ERROR_NONE) { if (required) { - char *passphrase = __get_setup_network_wifi_password(); - _D("password : [%s]", passphrase); - rv = wifi_manager_ap_set_passphrase(ap, passphrase); + _D(" passphrase1 : [%s]", wifi_data->password); + rv = wifi_manager_ap_set_passphrase(ap, wifi_data->password); if (rv != WIFI_MANAGER_ERROR_NONE) { _D("Fail to set passphrase : %s", __print_wifi_error(rv)); - if (ap_name != NULL) - free(ap_name);; - return ; + __wifi_exit_loop(wifi_data); } } - - //rv = wifi_manager_connect(wifi, ap, __connected_callback, NULL); - if (rv != WIFI_MANAGER_ERROR_NONE) - _D("Fail to connection request [%s] : %s", ap_name, __print_wifi_error(rv)); - else { - _D("Success to connection request [%s]", ap_name); - __set_setup_network_wifi_state(NETWORK_WIFI_STATE_CONNECTED); + if (rv == WIFI_MANAGER_ERROR_NONE) + { + rv = wifi_manager_connect(wifi_data->wifi_h, ap, __wifi_connected_cb, wifi_data); + if (rv != WIFI_MANAGER_ERROR_NONE) { + _D("Fail to connection request [%s] : %s", ap_name, __print_wifi_error(rv)); + __wifi_exit_loop(wifi_data); + } + else { + _D("Success to connection request [%s]", ap_name); + } } } - - } + else { + _D("Fail to passphrase_required request [%s]", __print_wifi_error(rv)); + __wifi_exit_loop(wifi_data); + } - if (ap_name != NULL) - free(ap_name); + g_free(ap_name); + return false; + } - return; + g_free(ap_name); + return true; } -static int __start_setup_network(void) +static void __wifi_scan_finished_cb(wifi_manager_error_e result, void *user_data) { - int err_code = 0; - - err_code = wifi_manager_initialize(&wifi_handle); - if (err_code == WIFI_MANAGER_ERROR_NONE || - err_code == WIFI_MANAGER_ERROR_ALREADY_INITIALIZED) { - if (connection_handle != NULL) { - err_code = connection_destroy(connection_handle); - _D("connection handle was already created [%s]", __print_error(err_code)); - } else { - err_code = connection_create(&connection_handle); - if (err_code != CONNECTION_ERROR_NONE) { - _E("create connection handle error [%s]", __print_error(err_code)); - return -1; - } - } - __set_setup_network_initialized(TRUE); - } else { - _E("create wifi handle error [%s]", __print_wifi_error(err_code)); - return -1; - } + _wifi_data_s *wifi_data = (_wifi_data_s*)user_data; - return 0; + _D("__wifi_scan_finished_cb() result=[%s]", __print_wifi_error(result)); + + wifi_manager_foreach_found_ap(wifi_data->wifi_h, __wifi_connect_ap_cb, wifi_data); } -static int __init_wifi_handle(wifi_manager_h *wifi) { +static void __wifi_state_changed_cb(wifi_manager_device_state_e state, void *user_data) +{ + _wifi_data_s *wifi_data = (_wifi_data_s*)user_data; - int err_code = 0; + _D("__wifi_state_changed_cb() state=[%d]", state); - err_code = wifi_manager_initialize(&wifi); - if (err_code == WIFI_MANAGER_ERROR_NONE || - err_code == WIFI_MANAGER_ERROR_ALREADY_INITIALIZED) { - _D(" Create handle for wiri"); - } else { - _E("create wifi handle error [%s]", __print_wifi_error(err_code)); - return -1; + if (state == WIFI_MANAGER_DEVICE_STATE_ACTIVATED) { + wifi_manager_scan(wifi_data->wifi_h, __wifi_scan_finished_cb, wifi_data); + wifi_manager_unset_device_state_changed_cb(wifi_data->wifi_h); } +} - return 0; +static void __wifi_activate_cb(wifi_manager_error_e result, void *user_data) +{ + _D("__wifi_activate_cb() result=[%s]", __print_wifi_error(result)); } -static int __connect_wifi(sa_wifi_s *info) +static int __wifi_connect_main(sa_wifi_s *info) { + GMainLoop *gmain_loop = NULL; + GMainContext *context; + + _wifi_data_s wifi_data = {0,}; + int rv = 0; - bool state = false; sa_error_e ret = SA_ERROR_UNKNOWN; - sa_network_state_e file_state = NETWORK_WIFI_STATE_DEACTIVATED; + wifi_manager_h wifi_h = NULL; + + _D("__wifi_connect_main (ssid=%s, pw=%s)", info->ssid, info->password); if (info == NULL) { + _E("invalid input (sa_wifi_s*)"); return -1; } - // if wifi is already connected, it would be diconnected - __set_setup_network_wifi_state(NETWORK_WIFI_STATE_DEACTIVATED); - - while(NETWORK_WIFI_STATE_CONNECTED != __get_setup_network_wifi_state()) { - - _D("Enter while [%d]", __get_setup_network_wifi_state()); - // check wifi state whether it is activated or not - switch(__get_setup_network_wifi_state()) { - case NETWORK_WIFI_STATE_DEACTIVATED: - _D("deativated state"); - rv = wifi_manager_is_activated(wifi_handle, &state); - if (rv != WIFI_MANAGER_ERROR_NONE) { - _D("Fail to get Wi-Fi device state [%s]", __print_wifi_error(rv)); - ret = SA_ERROR_UNKNOWN; - break; - } - - if (state == FALSE) { - _D("ativating state"); - rv = wifi_manager_activate(wifi_handle, __activated_callback, NULL); - __set_setup_network_wifi_state(NETWORK_WIFI_STATE_ACTIVATING); - if (rv == WIFI_MANAGER_ERROR_NONE || - rv == CONNECTION_ERROR_NOW_IN_PROGRESS || - rv == WIFI_MANAGER_ERROR_ALREADY_EXISTS) { - ; - } else { - _D("First activate Wi-Fi device [%s]", __print_wifi_error(rv)); - break; - } - } else { - _D("Already Activated"); - __set_setup_network_wifi_state(NETWORK_WIFI_STATE_ACTIVATED); - } - break; - case NETWORK_WIFI_STATE_ACTIVATED: - _D("ativated state"); - - rv = wifi_manager_scan(wifi_handle, __scan_request_callback, NULL); - __set_setup_network_wifi_state(NETWORK_WIFI_STATE_SCANNING); - _D("scanning state"); - if (rv != WIFI_MANAGER_ERROR_NONE) { - _D("Scan request failed [%s]", __print_wifi_error(rv)); - ret = SA_ERROR_UNKNOWN; - break; - } else { - _D("scan request succeeded"); - } - break; - case NETWORK_WIFI_STATE_SCANNED: - // connecting - _D("scanned state"); - __set_setup_network_wifi_state(NETWORK_WIFI_STATE_CONNECTING); - __set_setup_network_wifi_password(info->password); - _D("connecting state"); - rv = wifi_manager_foreach_found_ap(wifi_handle, __found_connect_ap_callback, info->ssid); - - if (rv != WIFI_MANAGER_ERROR_NONE) { - _D("Fail to connect (can't get AP list) [%s]", __print_wifi_error(rv)); - ret = SA_ERROR_UNKNOWN; - break; - } - break; - default: - break; + + context = g_main_context_new (); + g_main_context_push_thread_default (context); // should be.. to get mainloop message in thread + gmain_loop = g_main_loop_new (context, FALSE); + + rv = wifi_manager_initialize(&wifi_h); + + if (rv == WIFI_MANAGER_ERROR_NONE) { + // fill the token data + wifi_data.ssid = info->ssid; + wifi_data.password = info->password; + wifi_data.loop = gmain_loop; + wifi_data.wifi_h = wifi_h; + + rv = wifi_manager_set_device_state_changed_cb(wifi_h, __wifi_state_changed_cb, (void *)&wifi_data); + rv = wifi_manager_activate(wifi_h, __wifi_activate_cb, NULL); + if (rv != WIFI_MANAGER_ERROR_NONE) { + _E("wifi_manager_activated failed.[%s]", __print_wifi_error(rv)); + return SA_ERROR_UNKNOWN; } + } + else { + _E("Wifi init failed [%s]", __print_wifi_error(rv)); + return SA_ERROR_UNKNOWN; + } + + g_main_loop_run (gmain_loop); + + gmain_loop = NULL; + + wifi_manager_unset_scan_state_changed_cb(wifi_h); + wifi_manager_unset_connection_state_changed_cb(wifi_h); - sleep(1); + rv = wifi_manager_deinitialize(wifi_h); + + if (rv != WIFI_MANAGER_ERROR_NONE) { + _D("Fail to deinitialize."); + ret = SA_ERROR_UNKNOWN; } - + + _D("__wifi_connect_main completed."); return ret; } -static int __connect_ethernet(sa_eth_s *info) -{ - int rv = 0; - int err = 0; - int ret = 0; - connection_h connection = NULL; + + + + + + + + + + +static int __ethernet_connect_main(sa_eth_s *info) +{ if (info == NULL) { return -1; } @@ -463,82 +387,60 @@ static int __connect_ethernet(sa_eth_s *info) return 0; } -static int __get_wifi_info(sa_wifi_s *info) + + + + + + + + + +static int __network_get_wifi_state(connection_h connection, connection_wifi_state_e *wifi_state) { int rv = 0; - char *ap_name = NULL; - wifi_manager_ap_h ap_h; - - rv = wifi_manager_get_connected_ap(wifi_handle, &ap_h); - if (rv != WIFI_MANAGER_ERROR_NONE) { - _D("Fail to get connected AP [%s]", __print_wifi_error(rv)); - return -1; - } - rv = wifi_manager_ap_get_essid(ap_h, &ap_name); - if (rv != WIFI_MANAGER_ERROR_NONE) { - _D("Fail to get essid [%s]", __print_wifi_error(rv)); - wifi_manager_ap_destroy(ap_h); + if (connection == NULL) { return -1; } - _D("Connected AP : %s\n", ap_name); - memcpy(info->ssid, ap_name, MIN(strlen(ap_name), sizeof(info->ssid)-1)); - free(ap_name); - wifi_manager_ap_destroy(ap_h); - - return 0; -} - -static int __get_ethernet_info(sa_eth_s *info) { - char *ip_addr = NULL; - int input; - int rv; - - if (info != NULL) { - // only for ipv4 - rv = connection_get_ip_address(connection_handle, CONNECTION_ADDRESS_FAMILY_IPV4, &ip_addr); - if (ip_addr == NULL) { - _D("IPv4 address does not exist"); - return -1; - } - _D("IPv4 address : %s", ip_addr); - info->staticInfo = (sa_network_static_s *)malloc(sizeof(sa_network_static_s)); - if (info->staticInfo != NULL) { - memcpy(info->staticInfo->ipAddress, ip_addr, MIN(strlen(ip_addr), sizeof(info->staticInfo->ipAddress)-1)); - } - free(ip_addr); + rv = connection_get_wifi_state(connection, wifi_state); + if (rv != CONNECTION_ERROR_NONE) { + _D("Fail to get WiFi state [%s]", __print_error(rv)); + } else { + _D("Retval = [%s] WiFi state [%s]", __print_error(rv), __print_wifi_state(*wifi_state)); } return 0; } -sa_error_e sa_network_get_state(sa_network_state_e *conn_state, sa_network_type_e *conn_type) +static sa_error_e __network_get_state(sa_network_state_e *conn_state, sa_network_type_e *conn_type) { + connection_h connection = NULL; sa_error_e ret = SA_ERROR_UNKNOWN; connection_type_e net_state; connection_wifi_state_e wifi_state; int rv = 0; - if (!__get_setup_network_initialized()) { - if(__start_setup_network()) { - _E("not ready to use network api due to async reply from network api"); - return SA_ERROR_NOT_AVAILABLE; - } + rv = connection_create(&connection); + if (rv != CONNECTION_ERROR_NONE) { + _E("create connection handle error [%s]", __print_error(rv)); + return -1; } + // check network state(eth/wifi) - rv = connection_get_type(connection_handle, &net_state); + rv = connection_get_type(connection, &net_state); if (rv != CONNECTION_ERROR_NONE) { - printf("Fail to get network state [%s]\n", __print_error(rv)); + _E("Fail to get network state [%s]", __print_error(rv)); } else { ret = SA_ERROR_NONE; - _D("Retval = [%s] network connection state [%s]\n", __print_error(rv), __print_connection_type(net_state)); + _D("Retval = [%s] network connection state [%s]", __print_error(rv), __print_connection_type(net_state)); if (CONNECTION_TYPE_DISCONNECTED == net_state) { *conn_state = SA_NETWORK_STATE_DISCONNECTED; } else if (CONNECTION_TYPE_WIFI == net_state) { *conn_type = SA_NETWORK_TYPE_WIFI; // check wifi state - if (!__check_wifi_state(connection_handle, wifi_state)) { + if (!__network_get_wifi_state(connection, &wifi_state)) { if (wifi_state == CONNECTION_WIFI_STATE_CONNECTED) { *conn_state = SA_NETWORK_STATE_CONNECTED; } else { @@ -554,98 +456,67 @@ sa_error_e sa_network_get_state(sa_network_state_e *conn_state, sa_network_type_ } } + rv = connection_destroy(connection); + if (rv != CONNECTION_ERROR_NONE) { + _E("connection destroy fail [%s]", __print_error(rv)); + return -1; + } + return ret; } -sa_error_e sa_network_set_connection(sa_network_s *info) + +static sa_error_e __network_connect(sa_network_s *info) { sa_error_e ret = SA_ERROR_NONE; - connection_wifi_state_e wifi_state; int retWifi = 0; int retEth = 0; if (info == NULL) { return SA_ERROR_INVALID_PARAMETER; } - - if (!__get_setup_network_initialized()) { - if(__start_setup_network()) { - _E("not ready to use network api due to async reply from network api"); - } - } - - // first priority is wifi + if (info->wifi != NULL) { if (info->wifi->enabled == TRUE) { - // wifi setting and waiting to get response - retWifi = __connect_wifi(info->wifi); + retWifi = __wifi_connect_main(info->wifi); _D("return wifi [%d]", retWifi); - // or if wifi is enabled but fail to connect, try to connect eth - - /* - if (info->eth != NULL) { - if (wifi_state != CONNECTION_WIFI_STATE_CONNECTED && info->eth->enabled == TRUE) { - retEth = __connect_ethernet(info->eth); - } - }*/ + } else { _D("wifi enabled flag is false"); } } - _D("End setting"); - + if (info->eth != NULL) { + // decide whether it will be set according to policy + if (info->eth->enabled == TRUE){ + retEth = __ethernet_connect_main(info->eth); + _D("return ethernet [%d]", retEth); + } + } + return ret; } -sa_error_e sa_network_get_connection(sa_network_s *info) +sa_error_e sa_setup_network(sa_network_s *network) { sa_error_e ret = SA_ERROR_NONE; - connection_type_e net_state; - connection_wifi_state_e wifi_state; - sa_network_state_e conn_state; sa_network_type_e conn_type; - int rv = 0; + sa_network_state_e conn_state; - if (!__get_setup_network_initialized()) { - if(__start_setup_network()) { - _E("not ready to use network api due to async reply from network api"); - } - } - // get network state - if (connection_handle != NULL) { - rv = connection_get_type(connection_handle, &net_state); - if (rv == CONNECTION_ERROR_NONE) { - // get detail information - if (CONNECTION_TYPE_WIFI == net_state) { - // check wifi state - if (!__check_wifi_state(connection_handle, wifi_state)) { - if (wifi_state == CONNECTION_WIFI_STATE_CONNECTED) { - // detail info for wifi - info->wifi = (sa_wifi_s *)malloc(sizeof(sa_wifi_s)); - if (info->wifi != NULL) { - ret = __get_wifi_info(info->wifi); - } - } - } - } else if (CONNECTION_TYPE_ETHERNET == net_state) { - // detail info for ethernet - info->eth = (sa_eth_s *)malloc(sizeof(sa_eth_s)); - if (info->eth != NULL) { - ret = __get_ethernet_info(info->eth); - } - } - } - } else { - _E("Connection handle is null"); + if (network == NULL) { + _E("__set_network is null"); + return SA_ERROR_INVALID_PARAMETER; } + // 1. check network state + // 2. if it is connected, read detail info + ret = __network_connect(network); - return ret; -} - -sa_error_e sa_network_deactivate(void) -{ - sa_error_e ret = SA_ERROR_NONE; + // need to delay to sync with setting... + sleep(2); + ret = __network_get_state(&conn_state, &conn_type); + if (ret == SA_ERROR_NONE) { + _D("Device Network : T[%s] S[%s]", __print_network_type(conn_type), __print_connection_state(conn_state)); + } return ret; -} +} \ No newline at end of file diff --git a/src/setup_network.h b/src/setup_network.h new file mode 100644 index 0000000..07afe87 --- /dev/null +++ b/src/setup_network.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd. + * + * 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 __SETUP_NETWORK_H__ +#define __SETUP_NETWORK_H__ + +#include "sa_types.h" + +/** + * @fn int sa_setup_network(sa_network_s *info) + * @brief This function to set network connection + @param sa_network_s [in] network info struture + * @return sa_error_e return of function + */ +sa_error_e sa_setup_network(sa_network_s *network); + + +#endif/* __SETUP_NETWORK_H__ */ diff --git a/src/setup_system.c b/src/setup_system.c index 209b451..539398f 100644 --- a/src/setup_system.c +++ b/src/setup_system.c @@ -18,12 +18,55 @@ #include "sa_types.h" #include "setup_system.h" -sa_error_e sa_system_set_proxy(sa_proxy_s *info) +static sa_error_e system_set_proxy(sa_proxy_s *info) { return SA_ERROR_NOT_SUPPORTED; } -sa_error_e sa_system_get_proxy(sa_proxy_s *info) +static sa_error_e system_get_proxy(sa_proxy_s *info) { return SA_ERROR_NOT_SUPPORTED; +} + +static int compare_proxy_info(sa_proxy_s *devProxy, sa_proxy_s *newProxy) +{ + // comapre proxy + if (devProxy != NULL && newProxy != NULL) { + if (strlen(devProxy->httpProxyHost) > 0) { + if (strcmp(devProxy->httpProxyHost, newProxy->httpProxyHost)) { + // proxy info is different + return 1; + } else { + // Host address is the same, but port is different + if (devProxy->httpProxyPort != newProxy->httpProxyPort) { + return 1; + } + } + } + } + + return 0; +} + + + +sa_error_e sa_setup_system(sa_system_s *system) +{ + sa_proxy_s *devProxy = NULL; + sa_error_e ret = SA_ERROR_NONE; + + if (system == NULL) { + _E("__set_system is null"); + return -1; + } + + if (system->proxy != NULL) { + ret = system_get_proxy(devProxy); + if (ret == SA_ERROR_NONE && compare_proxy_info(devProxy, system->proxy)) { + // set proxy + ret = system_set_proxy(system->proxy); + } + } + + return ret; } \ No newline at end of file diff --git a/include/setup_system.h b/src/setup_system.h similarity index 56% rename from include/setup_system.h rename to src/setup_system.h index 4d72df0..4e683ba 100644 --- a/include/setup_system.h +++ b/src/setup_system.h @@ -16,20 +16,15 @@ #ifndef __SETUP_SYSTEMDATA_H__ #define __SETUP_SYSTEMDATA_H__ -/** - * @fn int sa_system_set_proxy(sa_network_s *info) - * @brief This function to set connection with info - @param conn_type [in] proxy info to set - * @return sa_error_e return of function - */ -sa_error_e sa_system_set_proxy(sa_proxy_s *info); +#include "sa_types.h" /** - * @fn int sa_system_get_proxy(sa_network_s *info) - * @brief This function to get connection with info - * @param conn_type [out] proxy info to get - * @return sa_error_e return of function + * @fn int sa_setup_system(sa_network_s *info) + * @brief This function to set system info info + @param sa_system_s [in] system info + * @return sa_error_e return of function */ -sa_error_e sa_system_get_proxy(sa_proxy_s *info); +sa_error_e sa_setup_system(sa_system_s *system); + #endif/* __SETUP_SYSTEMDATA_H__ */ -- 2.34.1