return ret;
}
-static int __activate_network(sa_network_s *network)
+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 (devNetwork->wifi != NULL && newNetwork->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....
+ }
+ }
+
+
+ if (needChange == FALSE && (devNetwork->eth != NULL && newNetwork->eth != NULL)) {
+ if (newNetwork->eth->enabled == TRUE) {
+ // 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;
+ }
+ }
+ }
+ }
+
+
+ return needChange;
+}
+
+static int __set_network(sa_network_s *network)
+{
+ sa_network_s *currentNetwork = NULL;
sa_error_e ret = SA_ERROR_NONE;
sa_network_type_e conn_type;
sa_network_state_e conn_state;
int index = 0;
+ if (network != NULL) {
+ return -1;
+ }
+ // 1. check network state
+ // 2. if it is connected, read detail info
+ ret = sa_network_get_state(&conn_state, &conn_type);
if (ret == SA_ERROR_NONE) {
- for (index = 0; index < NETWORK_CHECK_RETRY_MAX; index++) {
- ret = sa_network_get_state(&conn_state, &conn_type);
- if (ret == SA_ERROR_NONE) {
- if (conn_state == SA_NETWORK_STATE_CONNECTED) {
- _D("Network is connected : Type[%d][%s]", conn_type, __print_connection_type(conn_type));
- break;
- } else {
- _D("conn_state [%d][%s]", conn_state, __print_connection_state(conn_state));
+ _D("Device Network : Type[%d][%s] State[%d][%s]", conn_type, __print_connection_type(conn_type), conn_state, __print_connection_state(conn_state));
+ if (conn_state == SA_NETWORK_STATE_CONNECTED) {
+ // read current network info
+ ret = sa_network_get_connection(currentNetwork);
+ if (ret == SA_ERROR_NONE && currentNetwork != NULL) {
+ // compare network info
+ if (__compare_network_info(currentNetwork, network)) {
+ // set network connection
+ ret = sa_network_set_connection(network);
}
} else {
- _E("sa_network_get_state return error [%d]", ret);
- break;
+ _E("__get_current_network_info return error [%d]", ret);
+ }
+ } else {
+ // in case of disconnect state
+ ret = sa_network_set_connection(network);
+ }
+ }
+
+ 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;
+ }
}
- sleep(1);
}
}
- if (ret == SA_ERROR_NONE && conn_state != SA_NETWORK_STATE_CONNECTED) {
- if (network != NULL) {
- ret = sa_network_set_connection(network);
+ return 0;
+}
+
+static int __set_system(sa_system_s *system)
+{
+ sa_proxy_s *devProxy = NULL;
+ sa_error_e ret = SA_ERROR_NONE;
+
+ if (system != NULL) {
+ 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);
+ }
}
-
}
return ret;
if (SA_ERROR_NONE == __parsing_config(config)) {
// activate network
if (config != NULL) {
- __activate_network(config->networkData);
+ if (config->systemData != NULL) {
+ __set_system(config->systemData);
+ }
+
+ if (config->networkData != NULL) {
+ __set_network(config->networkData);
+ }
}
__release_config_resource(config);
}
#include "net_connection.h"
#include "wifi-manager.h"
+static int initialize_setup_network_flag;
+static sa_network_state_e setup_network_wifi_state;
+static char wifi_password[99+1];
+static connection_h connection_handle = NULL;
+static wifi_manager_h *wifi_handle = NULL;
+
+static int __get_setup_network_initialized(void)
+{
+ return initialize_setup_network_flag;
+}
+
+static void __set_setup_network_wifi_state(sa_network_state_e val)
+{
+ if (val != __get_setup_network_initialized()) {
+ setup_network_wifi_state = val;
+ }
+}
+
+static int __get_setup_network_wifi_state(void)
+{
+ return setup_network_wifi_state;
+}
+
+static void __set_setup_network_wifi_password(char *password)
+{
+ if (strcmp(wifi_password, password)) {
+ memcpy(wifi_password, password, MIN(strlen(password), sizeof(wifi_password)-1));
+ }
+}
+
+static char *__get_setup_network_wifi_password(void)
+{
+ return wifi_password;
+}
+
+
static const char *__print_wifi_state(connection_wifi_state_e state)
{
switch (state) {
static void __activated_callback(wifi_manager_error_e result, void *user_data)
{
- if (result == WIFI_MANAGER_ERROR_NONE)
+ 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);
+ __set_setup_network_wifi_state(NETWORK_WIFI_STATE_ACTIVATED);
- _D("Scan Completed from scan request, error code : %s", __print_wifi_error(error_code));
+ if (error_code == WIFI_MANAGER_ERROR_NONE) {
+ __set_setup_network_wifi_state(NETWORK_WIFI_STATE_SCANNED);
+ _D("Scan Completed from scan request");
+ }
+ else
+ _D("Wi-Fi Scan Failed! error : %s", __print_wifi_error(error_code));
+
}
static int __compare_ap_name(const char *ap_name, const char *ap_name_part)
_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)
+static void __found_connect_ap_callback(wifi_manager_ap_h ap, void *user_data)
{
int rv = 0;
char *ap_name = NULL;
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;
+ return;
}
if (__compare_ap_name(ap_name, ap_name_part)) {
if (rv == WIFI_MANAGER_ERROR_NONE) {
if (required) {
- char passphrase[100];
- _D("Input passphrase for %s : ", ap_name);
- rv = scanf("%99s", passphrase);
-
+ char *passphrase = __get_setup_network_wifi_password();
+ _D("password : [%s]", 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;
+ return ;
}
}
//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
+ else {
_D("Success to connection request [%s]", ap_name);
+ __set_setup_network_wifi_state(NETWORK_WIFI_STATE_CONNECTED);
+ }
}
}
if (ap_name != NULL)
free(ap_name);
- return true;
-}
+ return;
+}
static int __connect_wifi(sa_wifi_s *info)
{
int rv = 0;
- wifi_manager_h wifi = NULL;
bool state = false;
+ int ret = 0;
+ sa_network_state_e wifi_state = SA_NETWORK_STATE_DISCONNECTED;
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 wifi is already connected, it would be diconnected
+ while(NETWORK_WIFI_STATE_CONNECTED != __get_setup_network_wifi_state()) {
+ // check wifi state whether it is activated or not
+ if (NETWORK_WIFI_STATE_DEACTIVATED == __get_setup_network_wifi_state()) {
+ rv = wifi_manager_is_activated(wifi_handle, &state);
+ if (rv != WIFI_MANAGER_ERROR_NONE) {
+ printf("Fail to get Wi-Fi device state [%s]", __print_wifi_error(rv));
+ break;
+ }
+ __set_setup_network_wifi_state(NETWORK_WIFI_STATE_ACTIVATING);
+ rv = wifi_manager_activate(wifi_handle, __activated_callback, NULL);
if (rv != WIFI_MANAGER_ERROR_NONE) {
printf("Fail to activate Wi-Fi device [%s]", __print_wifi_error(rv));
- return -1;
+ break;
}
- }
+ }
- // scan ap
- rv = wifi_manager_scan(wifi, __scan_request_callback, NULL);
+ if (NETWORK_WIFI_STATE_ACTIVATED == __get_setup_network_wifi_state()) {
+ // scan ap
+ __set_setup_network_wifi_state(NETWORK_WIFI_STATE_SCANNING);
+ rv = wifi_manager_scan(wifi_handle, __scan_request_callback, NULL);
- if (rv != WIFI_MANAGER_ERROR_NONE) {
- _D("Scan request failed [%s]", __print_wifi_error(rv));
- return -1;
+ if (rv != WIFI_MANAGER_ERROR_NONE) {
+ _D("Scan request failed [%s]", __print_wifi_error(rv));
+ break;
+ }
}
+
+ if (NETWORK_WIFI_STATE_SCANNED == __get_setup_network_wifi_state()) {
+ // connecting
+ __set_setup_network_wifi_state(NETWORK_WIFI_STATE_CONNECTING);
+ __set_setup_network_wifi_password(info->password);
+ rv = wifi_manager_foreach_found_ap(wifi_handle, __found_connect_ap_callback, info->ssid);
- // 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;
- }
+ if (rv != WIFI_MANAGER_ERROR_NONE) {
+ _D("Fail to connect (can't get AP list) [%s]", __print_wifi_error(rv));
+ break;
+ }
+ }
- } else {
- _E("fail creation wifi-manager handler [%s]", __print_wifi_error(rv));
+ sleep(1);
}
- return 0;
+ return rv;
}
static int __connect_ethernet(sa_eth_s *info)
return 0;
}
+static int __start_setup_network(void)
+{
+ int ret = 0;
+ int err_code = 0;
+
+ err_code = wifi_manager_initialize(wifi_handle);
+ if (err_code == WIFI_MANAGER_ERROR_NONE) {
+ err_code = connection_create(&connection_handle);
+ if (err_code != CONNECTION_ERROR_NONE)
+ return -1;
+ } else {
+ return -1;
+ }
+
+ return 0;
+}
+
+static int __get_wifi_info(sa_wifi_s *info)
+{
+ 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);
+ 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 rv;
+}
+
+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);
+ memcpy(info->staticInfo->ipAddress, ip_addr, MIN(strlen(ip_addr), sizeof(info->staticInfo->ipAddress)-1));
+ free(ip_addr);
+ }
+
+ return rv;
+}
+
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);
-
+ if (!__get_setup_network_initialized()) {
+ if(__start_setup_network()) {
+ _E("not ready to use network api due to async reply from network api");
+ }
+ }
// check network state(eth/wifi)
- 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;
- }
+ rv = connection_get_type(connection_handle, &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_handle, 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;
}
+ } 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_s *info)
+sa_error_e sa_network_set_connection(sa_network_s *info)
{
sa_error_e ret = SA_ERROR_NONE;
connection_wifi_state_e wifi_state;
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);
+ if (!__get_setup_network_initialized()) {
+ if(__start_setup_network()) {
+ _E("not ready to use network api due to async reply from network api");
+ }
+ }
- // 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);
+ // 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);
+ _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);
+ }
+ }*/
}
+ }
+
+ return 0;
+}
- } else {
- // second priority is eth
- if (info->eth->enabled == TRUE) {
- retEth = __connect_ethernet(info->eth);
+sa_error_e sa_network_get_connection(sa_network_s *info)
+{
+ sa_error_e ret;
+ 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;
+
+ 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
+ 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);
+ }
+ }
+ }
- return 0;
+ return ret;
}
-int sa_network_deactivate(void)
+sa_error_e sa_network_deactivate(void)
{
sa_error_e ret = SA_ERROR_NONE;