From 2125249a51b9598c7f6061707c3a92455ed7cf24 Mon Sep 17 00:00:00 2001 From: "nshero.lee" Date: Mon, 18 Dec 2017 22:10:41 +0900 Subject: [PATCH] Implement 18th Dec Signed-off-by: nshero.lee --- include/sa_common.h | 7 +- include/sa_network.h | 7 +- include/sa_systemdata.h | 2 +- include/sa_types.h | 8 +- src/sa_manager.c | 19 +- src/sa_network.c | 401 +++++++++++++++++++++++++++++++++++++++- src/sa_systemdata.c | 192 ++++--------------- 7 files changed, 456 insertions(+), 180 deletions(-) diff --git a/include/sa_common.h b/include/sa_common.h index 6a8b55c..d169280 100644 --- a/include/sa_common.h +++ b/include/sa_common.h @@ -26,7 +26,12 @@ #define LOG_TAG "SETUP-ADAPTOR" #define MIN(X, Y) ((X) < (Y) ? (X) : (Y)) - +#ifndef TRUE +#define TRUE 1 +#endif +#ifndef FALSE +#define FALSE 0 +#endif #define _E(fmt, arg...) LOGE(fmt, ##arg) #define _D(fmt, arg...) LOGD(fmt, ##arg) #define _W(fmt, arg...) LOGW(fmt, ##arg) diff --git a/include/sa_network.h b/include/sa_network.h index 7f3a0af..806366b 100644 --- a/include/sa_network.h +++ b/include/sa_network.h @@ -23,15 +23,14 @@ * @param conn_type [out] type of connection * @return int return of function */ -int sa_network_get_state(sa_network_state_e * conn_state, sa_network_type_e * conn_type); +int sa_network_get_state(sa_network_state_e *conn_state, sa_network_type_e *conn_type); /** - * @fn int sa_network_activate(sa_network_activate_pararms_s *info) + * @fn int sa_network_activate(sa_network_s *info) * @brief This function to activate connection with info - * @param conn_state [in] activation params for network * @return int return of function */ -int sa_network_activate(sa_network_activate_pararms_s * info); +int sa_network_activate(sa_network_s *info); /** * @fn int sa_network_deactivate(void) diff --git a/include/sa_systemdata.h b/include/sa_systemdata.h index b474f2b..630930a 100644 --- a/include/sa_systemdata.h +++ b/include/sa_systemdata.h @@ -23,6 +23,6 @@ * @param sa_config_s [out] fill config info * @return int return of function */ -int sa_systemdata_get_config_info(char *flle, sa_config_s * config); +int sa_systemdata_get_config_info(char *flle, sa_config_s *config); #endif /* __SA_SYSTEMDATA_H__ */ diff --git a/include/sa_types.h b/include/sa_types.h index 655ff0a..be348b5 100644 --- a/include/sa_types.h +++ b/include/sa_types.h @@ -60,9 +60,9 @@ typedef enum { * */ typedef enum { - SA_NETWORK_TYPE_WIFI = 0, /**< wifi */ - SA_NETWORK_TYPE_ETH, /**< eth */ - SA_NETWORK_TYPE_UNKNOWN, /**< UNKNOWN */ + SA_NETWORK_TYPE_NONE = 0, /**< none */ + SA_NETWORK_TYPE_ETH, /**< eth */ + SA_NETWORK_TYPE_WIFI, /**< wifi */ } sa_network_type_e; /** @@ -72,7 +72,7 @@ typedef enum { * */ typedef enum { - SA_NETWORK_STATE_DISCONNECTED = 0, /**< Successful */ + SA_NETWORK_STATE_DISCONNECTED = 0, /**< Disconnected */ SA_NETWORK_STATE_PROGRESSING, /**< Connecting/Association/Configuration */ SA_NETWORK_STATE_CONNECTED, /**< connected */ SA_NETWORK_STATE_UNKNOWN, /**< UNKNOWN */ diff --git a/src/sa_manager.c b/src/sa_manager.c index ddcf2c5..cda1c17 100644 --- a/src/sa_manager.c +++ b/src/sa_manager.c @@ -25,7 +25,7 @@ #include "sa_network.h" #define CONFIG_FILE "/etc/setup-adaptor/config.json" -#define NETWORK_CHECK_RETRY_MAX 60 +#define NETWORK_CHECK_RETRY_MAX 10 #define EVENT_SIZE (sizeof(struct inotify_event)) #define BUF_LEN (1024 * (EVENT_SIZE + 16)) @@ -67,7 +67,7 @@ static char *__print_connection_type(sa_network_type_e type) } } -static void __print_config_network_static_info(sa_network_static_s * staticInfo) +static void __print_config_network_static_info(sa_network_static_s *staticInfo) { if (staticInfo != NULL) { if (staticInfo->ipAddress != NULL) @@ -83,7 +83,7 @@ static void __print_config_network_static_info(sa_network_static_s * staticInfo) } } -static void __print_config_info(sa_config_s * config) +static void __print_config_info(sa_config_s *config) { sa_network_s *network = NULL; sa_systemdata_s *systemData = NULL; @@ -309,10 +309,9 @@ int main(int argc, char *argv[]) if (ret == SA_ERROR_NONE) { _D("Success to get config"); // check config info - if (config != NULL) { - _D("Config value"); + if (config != NULL) __print_config_info(config); - } + } else { _E("sa_systemdata_get_config_info return error [%d]", ret); } @@ -340,8 +339,12 @@ int main(int argc, char *argv[]) } // 3.Try to connect with config value - if (ret == SA_ERROR_NONE && conn_state != SA_NETWORK_STATE_CONNECTED) - ret = sa_network_activate(activateParams); + if (ret == SA_ERROR_NONE && conn_state != SA_NETWORK_STATE_CONNECTED) { + if (config->networkData != NULL) { + ret = sa_network_activate(config->networkData); + } + + } // 4.Register file change event if (ret == SA_ERROR_NONE) { // register file change event diff --git a/src/sa_network.c b/src/sa_network.c index 0f9874b..824482b 100644 --- a/src/sa_network.c +++ b/src/sa_network.c @@ -14,23 +14,414 @@ * limitations under the License. */ #include +#include +#include #include "sa_common.h" #include "sa_types.h" #include "sa_network.h" +#include "net_connection.h" +#include "wifi-manager.h" -int sa_network_get_state(sa_network_state_e * conn_state, sa_network_type_e * conn_type) + +static const char *__print_wifi_state(connection_wifi_state_e state) { - sa_error_e ret = SA_ERROR_NONE; + switch (state) { + case CONNECTION_WIFI_STATE_DEACTIVATED: + return "Deactivated"; + case CONNECTION_WIFI_STATE_DISCONNECTED: + return "Disconnected"; + case CONNECTION_WIFI_STATE_CONNECTED: + return "Connected"; + default: + return "Unknown"; + } +} + +static const char *__print_error(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 const char *__print_connection_type(connection_type_e type) +{ + switch (type) { + case CONNECTION_TYPE_DISCONNECTED: + return "Disconnected"; + case CONNECTION_TYPE_WIFI: + return "Wifi"; + case CONNECTION_TYPE_CELLULAR: + return "Cellular"; + case CONNECTION_TYPE_ETHERNET: + return "Ethernet"; + case CONNECTION_TYPE_BT: + return "BT"; + case CONNECTION_TYPE_NET_PROXY: + return "Net_Proxy"; + default: + return "Unknown"; + } +} + +static const char *__print_wifi_error(wifi_manager_error_e err_type) +{ + switch (err_type) { + case WIFI_MANAGER_ERROR_NONE: + return "NONE"; + case WIFI_MANAGER_ERROR_INVALID_PARAMETER: + return "INVALID_PARAMETER"; + case WIFI_MANAGER_ERROR_OUT_OF_MEMORY: + return "OUT_OF_MEMORY"; + case WIFI_MANAGER_ERROR_INVALID_OPERATION: + return "INVALID_OPERATION"; + case WIFI_MANAGER_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED: + return "ADDRESS_FAMILY_NOT_SUPPORTED"; + case WIFI_MANAGER_ERROR_OPERATION_FAILED: + return "OPERATION_FAILED"; + case WIFI_MANAGER_ERROR_NO_CONNECTION: + return "NO_CONNECTION"; + case WIFI_MANAGER_ERROR_NOW_IN_PROGRESS: + return "NOW_IN_PROGRESS"; + case WIFI_MANAGER_ERROR_ALREADY_EXISTS: + return "ALREADY_EXISTS"; + case WIFI_MANAGER_ERROR_OPERATION_ABORTED: + return "OPERATION_ABORTED"; + case WIFI_MANAGER_ERROR_DHCP_FAILED: + return "DHCP_FAILED"; + case WIFI_MANAGER_ERROR_INVALID_KEY: + return "INVALID_KEY"; + case WIFI_MANAGER_ERROR_OUT_OF_RANGE: + return "OUT_OF_RANGE"; + case WIFI_MANAGER_ERROR_PIN_MISSING: + return "PIN_MISSING"; + case WIFI_MANAGER_ERROR_CONNECT_FAILED: + return "CONNECT_FAILED"; + case WIFI_MANAGER_ERROR_LOGIN_FAILED: + return "LOGIN_FAILED"; + case WIFI_MANAGER_ERROR_AUTHENTICATION_FAILED: + return "AUTH_FAILED"; + case WIFI_MANAGER_ERROR_NO_REPLY: + return "NO_REPLY"; + case WIFI_MANAGER_ERROR_SECURITY_RESTRICTED: + return "SECURITY_RESTRICTED"; + case WIFI_MANAGER_ERROR_ALREADY_INITIALIZED: + return "ALREADY_INITIALIZED"; + case WIFI_MANAGER_ERROR_PERMISSION_DENIED: + return "PERMISSION_DENIED"; + case WIFI_MANAGER_ERROR_NOT_SUPPORTED: + return "NOT_SUPPORTED"; + case WIFI_MANAGER_ERROR_WPS_OVERLAP: + return "WPS_OVERLAP"; + case WIFI_MANAGER_ERROR_WPS_TIMEOUT: + return "WPS_TIMEOUT"; + case WIFI_MANAGER_ERROR_WPS_WEP_PROHIBITED: + return "WPS_WEP_PROHIBITED"; + default: + return "UNKNOWN"; + } +} + + +static int __check_wifi_state(connection_h connection, connection_wifi_state_e wifi_state) +{ + 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)); + } + + return 0; +} + +static void __activated_callback(wifi_manager_error_e result, void *user_data) +{ + if (result == WIFI_MANAGER_ERROR_NONE) + _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, error code : %s", __print_wifi_error(error_code)); +} + +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) +{ + if (result == WIFI_MANAGER_ERROR_NONE) + _D("Wi-Fi Connection Succeeded"); + else + _D("Wi-Fi Connection Failed! error : %s", __print_wifi_error(result)); +} + +static int __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 -1; + } + + if (__compare_ap_name(ap_name, ap_name_part)) { + int required = FALSE; + + rv = wifi_manager_ap_is_passphrase_required(ap, &required); + + if (rv == WIFI_MANAGER_ERROR_NONE) { + if (required) { + char passphrase[100]; + _D("Input passphrase for %s : ", ap_name); + rv = scanf("%99s", passphrase); + + rv = wifi_manager_ap_set_passphrase(ap, passphrase); + if (rv != WIFI_MANAGER_ERROR_NONE) { + _D("Fail to set passphrase : %s", __print_wifi_error(rv)); + if (ap_name != NULL) + free(ap_name);; + return false; + } + } + + //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); + } + + } + + if (ap_name != NULL) + free(ap_name); + return true; +} + + + +static int __connect_wifi(sa_wifi_s *info) +{ + int rv = 0; + wifi_manager_h wifi = NULL; + bool state = false; + + if (info == NULL) { + return -1; + } + + rv = wifi_manager_initialize(&wifi); + + if (rv == WIFI_MANAGER_ERROR_NONE) { + // check wifi + rv = wifi_manager_is_activated(wifi, &state); + if (rv != WIFI_MANAGER_ERROR_NONE) { + printf("Fail to get Wi-Fi device state [%s]", __print_wifi_error(rv)); + return -1; + } + + // if wifi is not activated + if (state == FALSE) { + rv = wifi_manager_activate(wifi, __activated_callback, NULL); + if (rv != WIFI_MANAGER_ERROR_NONE) { + printf("Fail to activate Wi-Fi device [%s]", __print_wifi_error(rv)); + return -1; + } + } + + // scan ap + rv = wifi_manager_scan(wifi, __scan_request_callback, NULL); + + if (rv != WIFI_MANAGER_ERROR_NONE) { + _D("Scan request failed [%s]", __print_wifi_error(rv)); + return -1; + } + + // connect ap + _D("AP : [%s]", info->ssid); + + rv = wifi_manager_foreach_found_ap(wifi, __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)); + return -1; + } + + } else { + _E("fail creation wifi-manager handler [%s]", __print_wifi_error(rv)); + } + + return 0; +} + +static int __connect_ethernet(sa_eth_s *info) +{ + int rv = 0; + int err = 0; + int ret = 0; + connection_h connection = NULL; + + if (info == NULL) { + return -1; + } + + err = connection_create(&connection); + if (CONNECTION_ERROR_NONE == err) { + // check network cable + // check dhcp / static + // set ip + rv = connection_destroy(connection); + if (rv != CONNECTION_ERROR_NONE) { + ret = SA_ERROR_UNKNOWN; + printf("Fail to get network state [%s]\n", __print_error(rv)); + } + + } else { + _E("fail creation connection handler [%s]\n", __print_error(err)); + } + + return 0; +} + +int sa_network_get_state(sa_network_state_e *conn_state, sa_network_type_e *conn_type) +{ + sa_error_e ret = SA_ERROR_UNKNOWN; + connection_h connection = NULL; + connection_type_e net_state; + connection_wifi_state_e wifi_state; + int rv = 0; + + int err = connection_create(&connection); + // check network state(eth/wifi) - // return state according to enum + if (CONNECTION_ERROR_NONE == err) { + rv = connection_get_type(connection, &net_state); + if (rv != CONNECTION_ERROR_NONE) { + printf("Fail to get network state [%s]\n", __print_error(rv)); + } else { + ret = SA_ERROR_NONE; + _D("Retval = [%s] network connection state [%s]\n", __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, wifi_state)) { + if (wifi_state == CONNECTION_WIFI_STATE_CONNECTED) { + *conn_state = SA_NETWORK_STATE_CONNECTED; + } else { + *conn_state = SA_NETWORK_STATE_DISCONNECTED; + } + } + } else if (CONNECTION_TYPE_ETHERNET == net_state) { + *conn_type = SA_NETWORK_TYPE_ETH; + *conn_state = SA_NETWORK_STATE_CONNECTED; + } else { + *conn_state = SA_NETWORK_STATE_UNKNOWN; + *conn_type = SA_NETWORK_TYPE_NONE; + } + } + + // destroy connection + rv = connection_destroy(connection); + if (rv != CONNECTION_ERROR_NONE) { + ret = SA_ERROR_UNKNOWN; + printf("Fail to get network state [%s]\n", __print_error(rv)); + } + + } else { + _E("fail creation connection handler [%s]\n", __print_error(err)); + } + return ret; } -int sa_network_activate(sa_network_activate_pararms_s * info) +int sa_network_activate(sa_network_s *info) { sa_error_e ret = SA_ERROR_NONE; + connection_wifi_state_e wifi_state; + int retWifi = 0; + int retEth = 0; - return ret; + if (info == NULL) { + return SA_ERROR_INVALID_PARAMETER; + } + + // first priority is wifi + if (info->wifi->enabled == TRUE) { + // wifi setting and waiting to get response + retWifi = __connect_wifi(info->wifi); + + // or if wifi is enabled but fail to connect, try to connect eth + if (wifi_state != CONNECTION_WIFI_STATE_CONNECTED && info->eth->enabled == TRUE) { + retEth = __connect_ethernet(info->eth); + } + + } else { + // second priority is eth + if (info->eth->enabled == TRUE) { + retEth = __connect_ethernet(info->eth); + } + } + + return 0; } int sa_network_deactivate(void) diff --git a/src/sa_systemdata.c b/src/sa_systemdata.c index b18b66b..cd0e69d 100644 --- a/src/sa_systemdata.c +++ b/src/sa_systemdata.c @@ -24,164 +24,21 @@ #include "sa_types.h" #include "sa_systemdata.h" -static char *json_getString(char *data, char *key) -{ - struct json_object *inputObj; - struct json_object *bodyObj; - enum json_type type; - char *ret_buf = NULL; - const char *buf = NULL; - int i, j = 0; - - inputObj = json_tokener_parse(data); - type = json_object_get_type(inputObj); - if (type == json_type_object) { - if (json_object_object_get_ex(inputObj, key, &bodyObj)) { - buf = json_object_to_json_string_ext(bodyObj, JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY); - ret_buf = malloc(strlen(buf) - 1); - if (ret_buf != NULL) { - memset(ret_buf, 0x00, strlen(buf) - 1); // "<-- exclude - for (i = 1, j = 0; i < strlen(buf) - 1; i++) { // "\ <-- exclude - if (buf[i] == '\\') - continue; - ret_buf[j++] = buf[i]; - } - } - } else { - _E("ERROR : no data"); - } - } - json_object_put(inputObj); - _D("JSON>> ret_buf = %s", ret_buf); - return ret_buf; -} - -static int json_getNumber(char *data, char *key) +static int json_get_int_from_obj(json_object *inputObj, char *key) { - struct json_object *inputObj; struct json_object *bodyObj; enum json_type type; - int ret_num = 0; - - inputObj = json_tokener_parse(data); - type = json_object_get_type(inputObj); - - if (type == json_type_object) { - if (json_object_object_get_ex(inputObj, key, &bodyObj)) { - ret_num = json_object_get_int(bodyObj); - } else { - _E("ERROR : no data"); - - } - } - json_object_put(inputObj); - return ret_num; -} - -static char *json_getObject(char *data, char *key) -{ - struct json_object *inputObj = NULL; - struct json_object *bodyObj = NULL; - const char *buf = NULL; - char *ret_buf = NULL; - - inputObj = json_tokener_parse(data); - - if (inputObj != NULL) { - if (json_object_object_get_ex(inputObj, key, &bodyObj)) { - if (json_object_get_type(bodyObj) == json_type_object) { - buf = json_object_to_json_string_ext(bodyObj, JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY); - ret_buf = (char *)malloc(strlen(buf) + 1); - if (ret_buf != NULL) { - memset(ret_buf, 0x00, strlen(buf) + 1); - memcpy(ret_buf, buf, strlen(buf)); - } - } - } - json_object_put(inputObj); - } - return ret_buf; -} - -static char *json_getArray(char *data, char *key) -{ - struct json_object *inputObj, *bodyObj; - enum json_type type; - const char *buf = NULL; - char *ret_buf = NULL; + int ret = 0; - inputObj = json_tokener_parse(data); type = json_object_get_type(inputObj); if (type == json_type_object) { if (json_object_object_get_ex(inputObj, key, &bodyObj)) { - type = json_object_get_type(bodyObj); - if (type == json_type_array) { - buf = json_object_to_json_string_ext(bodyObj, JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY); - ret_buf = malloc(strlen(buf) + 1); - if (ret_buf != NULL) { - memset(ret_buf, 0x00, strlen(buf) + 1); - memcpy(ret_buf, buf, strlen(buf)); - } - } - } + ret = json_object_get_int(bodyObj); + } } - - json_object_put(inputObj); - return ret_buf; -} - -static int json_getArrayLength(char *data) -{ - struct json_object *inputObj; - enum json_type type; - int cnt = 0; - - inputObj = json_tokener_parse(data); - type = json_object_get_type(inputObj); - - if (type == json_type_array) { - cnt = json_object_array_length(inputObj); - } - - return cnt; -} - -static char *json_getStringFromArray(char *data, char *arrayKey, int arrayIndex, char *key) -{ - struct json_object *inputObj; - struct json_object *bodyObj; - struct json_object *arrayObj; - struct json_object *elementObj; - char *ret_buf = NULL; - const char *buf = NULL; - int i, j = 0; - - inputObj = json_tokener_parse(data); - if (json_object_object_get_ex(inputObj, arrayKey, &arrayObj)) { - elementObj = json_object_array_get_idx(arrayObj, arrayIndex); - - if (json_object_object_get_ex(elementObj, key, &bodyObj)) { - buf = json_object_to_json_string_ext(bodyObj, JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY); - ret_buf = malloc(strlen(buf) - 1); - if (ret_buf != NULL) { - memset(ret_buf, 0x00, strlen(buf) - 1); // "<-- exclude - for (i = 1, j = 0; i < strlen(buf) - 1; i++) { // "\ <-- exclude - if (buf[i] == '\\') - continue; - ret_buf[j++] = buf[i]; - } - } - } else { - _E("ERROR : no data"); - } - json_object_put(inputObj); - } else { - _E("ERROR : not found(%s)", arrayKey); - } - - return ret_buf; + return ret; } static int json_get_boolean_from_obj(json_object *inputObj, char *key) @@ -195,9 +52,7 @@ static int json_get_boolean_from_obj(json_object *inputObj, char *key) if (type == json_type_object) { if (json_object_object_get_ex(inputObj, key, &bodyObj)) { ret = json_object_get_boolean(bodyObj); - } else { - _E("ERROR : no data"); - } + } } return ret; } @@ -229,6 +84,28 @@ static char *json_get_string_from_obj(json_object *inputObj, char *key) _D("string object [%s]", ret_buf); return ret_buf; } +static int __parse_network_proxy(json_object *inputObj, sa_network_s *network) +{ + char *httpProxyHost = NULL; + int httpProxyPort; + + if (inputObj == NULL || network == NULL) { + _E("__parse_network_proxy input error"); + return -1; + } + + //httpProxyHost + httpProxyHost = json_get_string_from_obj(inputObj, SA_CONFIG_NETWORKDATA_HTTPPROXYHOST); + if (httpProxyHost != NULL) { + memcpy(network->httpProxyHost, httpProxyHost, MIN(strlen(httpProxyHost), sizeof(network->httpProxyHost)-1)); + free(httpProxyHost); + httpProxyHost = NULL; + } + + //httpProxyPort + httpProxyPort = json_get_int_from_obj(inputObj, SA_CONFIG_NETWORKDATA_HTTPPROXYPORT); + +} static int __parse_network_static_info(json_object *inputObj, sa_network_static_s *staticInfo) { @@ -300,7 +177,7 @@ static int __parse_network_eth(json_object *inputObj, sa_eth_s *eth) // dhcpEnabled eth->dhcpEnabled = json_get_boolean_from_obj(inputObj, SA_CONFIG_NETWORKDATA_DHCPENABLED); - if (eth->dhcpEnabled == 0) { + if (eth->dhcpEnabled == FALSE) { eth->staticInfo = (sa_network_static_s *)malloc(sizeof(sa_network_static_s)); if (eth->staticInfo != NULL) { ret = __parse_network_static_info(inputObj, eth->staticInfo); @@ -349,7 +226,7 @@ static int __parse_network_wifi(json_object *inputObj, sa_wifi_s *wifi) // dhcpEnabled wifi->dhcpEnabled = json_get_boolean_from_obj(inputObj, SA_CONFIG_NETWORKDATA_DHCPENABLED); - if (wifi->dhcpEnabled == 0) { + if (wifi->dhcpEnabled == FALSE) { wifi->staticInfo = (sa_network_static_s *)malloc(sizeof(sa_network_static_s)); if (wifi->staticInfo != NULL) { ret = __parse_network_static_info(inputObj, wifi->staticInfo); @@ -400,6 +277,9 @@ static int __parse_network_data(json_object *inputObj, sa_network_s *network) } } + // parse proxy + __parse_network_proxy(inputObj, network); + // if both of network interfaces are failed, it would return -1 if (wifiRet != 0 && ethRet != 0) { _E("__parse_network_data fail"); @@ -513,7 +393,7 @@ static int __parse_config(char *file, sa_config_s *setupConfig) return ret; } -int sa_systemdata_get_config_info(char *file, sa_config_s * config) +int sa_systemdata_get_config_info(char *file, sa_config_s *config) { sa_error_e ret = SA_ERROR_NONE; @@ -526,10 +406,8 @@ int sa_systemdata_get_config_info(char *file, sa_config_s * config) ret = __parse_config(file, config); if (ret != 0) { _D("ret:DOCKER_STATUS_UNKNOWN --> exit for user recovery"); - return -1; + return SA_ERROR_UNKNOWN; } - // parsing - // fill the info into struct return ret; } -- 2.34.1