* limitations under the License.
*/
#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
#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)
#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)
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;
}
_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)
{
// 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);
// 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);
}
}
+ // 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");
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;
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;
}