enable ethernet setting
authorsy1124.kim <sy1124.kim@samsung.com>
Wed, 27 Dec 2017 10:09:15 +0000 (19:09 +0900)
committersy1124.kim <sy1124.kim@samsung.com>
Wed, 27 Dec 2017 10:09:15 +0000 (19:09 +0900)
Signed-off-by: sy1124.kim <sy1124.kim@samsung.com>
src/input_file.c
src/setup_network.c

index 38759e1553e59e9b4d6fbca7082624dc90797ea2..58b95870d0b8fefc70e92d755b55c71621ddebc6 100644 (file)
@@ -141,7 +141,7 @@ static int json_get_int_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_int(bodyObj);
-               } 
+               }
        }
        return ret;
 }
@@ -157,7 +157,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);
-               } 
+               }
        }
        return ret;
 }
@@ -231,7 +231,7 @@ static void print_network_config(sa_network_s *network)
                        _D("Network::eth::dhcpEnabled[%d]", eth->dhcpEnabled);
 
                        if (eth->dhcpEnabled == 0)
-                               print_config_network_static_info(wifi->staticInfo);
+                               print_config_network_static_info(eth->staticInfo);
                }
        }
 }
@@ -279,15 +279,17 @@ static int __parse_network_static_info(json_object *inputObj, sa_network_static_
        //ipAddress
        ipAddress = json_get_string_from_obj(inputObj, SA_CONFIG_NETWORKDATA_IPADDRESS);
        if (ipAddress != NULL) {
-               memcpy(staticInfo->ipAddress, ipAddress, MIN(strlen(ipAddress), sizeof(staticInfo->ipAddress)-1));
+               _D("ipaddress = %s", ipAddress);
+               memcpy(staticInfo->ipAddress, ipAddress, strlen(ipAddress));
                free(ipAddress);
                ipAddress = NULL;
+
        }
 
        //netmask
        netmask = json_get_string_from_obj(inputObj, SA_CONFIG_NETWORKDATA_NETMASK);
        if (netmask != NULL) {
-               memcpy(staticInfo->netmask, netmask, MIN(strlen(netmask), sizeof(staticInfo->netmask)-1));
+               memcpy(staticInfo->netmask, netmask,strlen(netmask));
                free(netmask);
                netmask = NULL;
        }
@@ -295,23 +297,23 @@ static int __parse_network_static_info(json_object *inputObj, sa_network_static_
        //defaultGateway
        defaultGateway = json_get_string_from_obj(inputObj, SA_CONFIG_NETWORKDATA_DEFAULTGATEWAY);
        if (defaultGateway != NULL) {
-               memcpy(staticInfo->defaultGateway, defaultGateway, MIN(strlen(defaultGateway), sizeof(staticInfo->defaultGateway)-1));
+               memcpy(staticInfo->defaultGateway, defaultGateway, strlen(defaultGateway));
                free(defaultGateway);
                defaultGateway = NULL;
        }
-       
+
        //primaryDnsServer
        primaryDnsServer = json_get_string_from_obj(inputObj, SA_CONFIG_NETWORKDATA_PRIMARYDNSSERVER);
        if (primaryDnsServer != NULL) {
-               memcpy(staticInfo->primaryDnsServer, primaryDnsServer, MIN(strlen(primaryDnsServer), sizeof(staticInfo->primaryDnsServer)-1));
+               memcpy(staticInfo->primaryDnsServer, primaryDnsServer, strlen(primaryDnsServer));
                free(primaryDnsServer);
                primaryDnsServer = NULL;
        }
-       
+
        //secondaryDnsServer
        secondaryDnsServer = json_get_string_from_obj(inputObj, SA_CONFIG_NETWORKDATA_SECONDARYDNSSERVER);
        if (secondaryDnsServer != NULL) {
-               memcpy(staticInfo->secondaryDnsServer, secondaryDnsServer, MIN(strlen(secondaryDnsServer), sizeof(staticInfo->secondaryDnsServer)-1));
+               memcpy(staticInfo->secondaryDnsServer, secondaryDnsServer, strlen(secondaryDnsServer));
                free(secondaryDnsServer);
                secondaryDnsServer = NULL;
        }
@@ -336,6 +338,7 @@ static int __parse_network_eth(json_object *inputObj, sa_eth_s *eth)
        if (eth->dhcpEnabled == FALSE) {
                eth->staticInfo = (sa_network_static_s *)malloc(sizeof(sa_network_static_s));
                if (eth->staticInfo != NULL) {
+                       _D("parse_network eth!!!\n");
                        ret = __parse_network_static_info(inputObj, eth->staticInfo);
                } else {
                        ret = -1;
@@ -387,7 +390,7 @@ static int __parse_network_wifi(json_object *inputObj, sa_wifi_s *wifi)
                } else {
                        ret = -1;
                }
-       } 
+       }
 
        return ret;
 }
@@ -439,7 +442,7 @@ static int __parse_network_data(json_object *inputObj, sa_config_s *setupConfig)
                _D("__parse_network_data fail");
                return -1;
        }
-       
+
        return 0;
 }
 
@@ -452,7 +455,7 @@ static int __parse_system_data(json_object *inputObj, sa_config_s *setupConfig)
                _D("__parse_system_data input error");
                return -1;
        }
-       
+
        //httpProxyHost
        httpProxyHost = json_get_string_from_obj(inputObj, SA_CONFIG_NETWORKDATA_HTTPPROXYHOST);
        if (httpProxyHost != NULL) {
@@ -463,7 +466,7 @@ static int __parse_system_data(json_object *inputObj, sa_config_s *setupConfig)
                                memcpy(setupConfig->systemData->proxy->httpProxyHost, httpProxyHost, MIN(strlen(httpProxyHost), sizeof(setupConfig->systemData->proxy->httpProxyHost)-1));
                                free(httpProxyHost);
                                httpProxyHost = NULL;
-                               
+
                                //httpProxyPort
                                setupConfig->systemData->proxy->httpProxyPort = json_get_int_from_obj(inputObj, SA_CONFIG_NETWORKDATA_HTTPPROXYPORT);
 
@@ -530,7 +533,7 @@ static int __parse_config(char *file, sa_config_s *setupConfig)
                                        if (ret == 0) {
                                                ret = __parse_system_data(configObj, setupConfig);
                                        }
-                                       
+
                                        if (ret == 0) {
                                                ret = __parse_network_data(configObj, setupConfig);
                                        }
@@ -538,7 +541,7 @@ static int __parse_config(char *file, sa_config_s *setupConfig)
                                        ret = -1;
                                        _D("ConfigObj is not existed");
                                }
-                               
+
                                json_object_put(configObj);
                                free(jsonData);
                        } else {
@@ -571,7 +574,7 @@ sa_error_e sa_inputfile_get_config_info(sa_config_s *config)
        }
 
        print_config_info(config);
-       
+
        return ret;
 }
 
@@ -603,8 +606,8 @@ 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->staticInfo);
+
                        free(network->wifi);
                }
 
@@ -623,23 +626,23 @@ gboolean sa_inputfile_thread(GSource *source, GSourceFunc callbackFuntion, gpoin
 {
        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;
@@ -659,4 +662,4 @@ void sa_inputfile_release_resource(sa_config_s *config)
 
 
 /* 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
+/*{"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}*/
index af5117991cfb55c101fa31fe578519b7244460be..eb2306fd709b59cb4c2f62dc84997667fb2fb174 100644 (file)
@@ -240,7 +240,7 @@ static bool __wifi_connect_ap_cb(wifi_manager_ap_h ap, void *user_data)
                _D("OK!!! connect AP [%s]", ap_name);
 
                rv = wifi_manager_ap_is_passphrase_required(ap, &required);
-               
+
                if (rv == WIFI_MANAGER_ERROR_NONE) {
                        if (required) {
                                _D(" passphrase1 : [%s]", wifi_data->password);
@@ -311,7 +311,7 @@ static int __wifi_connect_main(sa_wifi_s *info)
        int rv = 0;
        sa_error_e ret = SA_ERROR_UNKNOWN;
        wifi_manager_h wifi_h = NULL;
-       
+
        _D("__wifi_connect_main (ssid=%s, pw=%s)", info->ssid, info->password);
 
        if (info == NULL) {
@@ -322,7 +322,7 @@ static int __wifi_connect_main(sa_wifi_s *info)
        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) {
@@ -338,7 +338,7 @@ static int __wifi_connect_main(sa_wifi_s *info)
                        _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;
@@ -364,36 +364,211 @@ static int __wifi_connect_main(sa_wifi_s *info)
 }
 
 
+connection_h eth_connect;
+
+static bool __eth_get_user_selected_profile(connection_profile_h *profile, bool select)
+{
+       int rv = 0;
+       int input = 0;
+       char *profile_name;
+       connection_profile_type_e profile_type;
+       connection_profile_state_e profile_state;
+       connection_profile_iterator_h profile_iter;
+       connection_profile_h profile_h;
+
+       rv = connection_get_profile_iterator(eth_connect, CONNECTION_ITERATOR_TYPE_REGISTERED, &profile_iter);
+       if (rv != CONNECTION_ERROR_NONE) {
+               printf("Fail to get profile iterator error\n" );
+               return false;
+       }
 
+       while (connection_profile_iterator_has_next(profile_iter)) {
+               if (connection_profile_iterator_next(profile_iter, &profile_h) != CONNECTION_ERROR_NONE) {
+                       printf("Fail to get profile handle\n");
+                       return false;
+               }
 
+               if (connection_profile_get_name(profile_h, &profile_name) != CONNECTION_ERROR_NONE) {
+                       printf("Fail to get profile name\n");
+                       return false;
+               }
 
+               if (connection_profile_get_type(profile_h, &profile_type) != CONNECTION_ERROR_NONE) {
+                       printf("Fail to get profile type\n");
+                       g_free(profile_name);
+                       return false;
+               }
 
+               if (connection_profile_get_state(profile_h, &profile_state) != CONNECTION_ERROR_NONE) {
+                       printf("Fail to get profile state\n");
+                       g_free(profile_name);
+                       return false;
+               }
 
+               if (!strncmp(profile_name, "eth0", strlen("eth0"))) {
+                       printf("profile > %s\n", profile_name);
+                       if (profile) {
+                               *profile = profile_h;
+                       }
+               }
+               g_free(profile_name);
+       }
 
+       return true;
+}
 
+static int __eth_update_ip_info(sa_eth_s *info, connection_profile_h profile, connection_address_family_e address_family)
+{
+       int rv = 0;
+       int input_int = 0;
+       char input_str[100] = {0,};
 
+       _D("ipaddress =%s", info->staticInfo->ipAddress);
 
+       if (info->staticInfo->ipAddress != NULL) {
+               rv = connection_profile_set_ip_address(profile,
+                                                       address_family,
+                                                       info->staticInfo->ipAddress);
+               if (rv != CONNECTION_ERROR_NONE)
+                       return -1;
+       }
+#if 0
+       _D("netmask =%s", info->staticInfo->netmask);
+       if (info->staticInfo->netmask != NULL) {
+               rv = connection_profile_set_subnet_mask(profile,
+                                                       address_family,
+                                                       info->staticInfo->netmask);
+               if (rv != CONNECTION_ERROR_NONE)
+                       return -1;
 
-static int __ethernet_connect_main(sa_eth_s *info)
+       }
+
+       if (info->staticInfo->defaultGateway != NULL) {
+               rv = connection_profile_set_gateway_address(profile,
+                                                       address_family,
+                                                       info->staticInfo->defaultGateway);
+               if (rv != CONNECTION_ERROR_NONE)
+                       return -1;
+               _D("gateway =%s", info->staticInfo->defaultGateway);
+       }
+       if (info->staticInfo->primaryDnsServer != NULL ) {
+               rv = connection_profile_set_dns_address(profile,
+                               1,
+                               address_family,
+                               info->staticInfo->primaryDnsServer);
+               if (rv != CONNECTION_ERROR_NONE)
+                       return -1;
+
+               _D("DNS 1 =%s", info->staticInfo->primaryDnsServer);
+
+               if (info->staticInfo->secondaryDnsServer != NULL ) {
+                       rv = connection_profile_set_dns_address(profile,
+                                       2,
+                                       address_family,
+                                       info->staticInfo->secondaryDnsServer);
+                       if (rv != CONNECTION_ERROR_NONE)
+                               return -1;
+                       _D("DNS 2 =%s", info->staticInfo->secondaryDnsServer);
+               }
+       }
+#endif
+
+       return 1;
+}
+
+static int __eth_update_ip(sa_eth_s *info)
 {
-       if (info == NULL) {
+       int rv = 0;
+
+       connection_profile_type_e prof_type;
+       connection_profile_h profile;
+       int address_family = 0; // IPv4
+
+       //printf("\n** Choose a profile to update. **\n");
+       if (__eth_get_user_selected_profile(&profile, true) == false) {
+               _D("profile failed \n");
                return -1;
        }
-       
-       // check network cable
-       // check dhcp / static
-       // set ip
 
-       return 0;
+       if (connection_profile_get_type(profile, &prof_type) != CONNECTION_ERROR_NONE) {
+               _D("profile connection error!! \n");
+               return -1;
+       }
+
+       if (connection_profile_set_ip_config_type(profile, address_family, CONNECTION_IP_CONFIG_TYPE_STATIC) != CONNECTION_ERROR_NONE) {
+               _D("setting error !!\n");
+               return -1;
+       }
+       if (__eth_update_ip_info(info, profile, address_family) != 1) {
+               _D("ip setting error!!\n");
+               return -1;
+       }
+
+       if(connection_profile_set_proxy_type(profile,CONNECTION_PROXY_TYPE_DIRECT) != CONNECTION_ERROR_NONE)
+               return -1;
+
+       if (connection_update_profile(eth_connect, profile) != CONNECTION_ERROR_NONE) {
+               _D("profile update error !!\n");
+               return -1;
+       }
+
+       _D("operation sucess\n");
+       return 1;
+}
+
+static int __eth_register_client(void)
+{
+
+       if (CONNECTION_ERROR_NONE != connection_create(&eth_connect)) {
+               _D("Client registration failed \n");
+               return -1;
+       }
+
+       _D("Client registration success\n");
+       return 1;
 }
 
+static int __eth_deregister_client(void)
+{
+       int rv = 0;
+       GSList *list;
+       connection_profile_h profile;
 
+       if (eth_connect != NULL)
+               rv = connection_destroy(eth_connect);
+       else {
+               _D("Cannot deregister : Handle is NULL\n");
+               rv = CONNECTION_ERROR_INVALID_OPERATION;
+       }
 
+       if (rv != CONNECTION_ERROR_NONE) {
+               _D("Client deregistration fail\n");
+               return -1;
+       }
 
+       eth_connect = NULL;
+       _D("Client deregistration success\n");
 
+       return 1;
+}
 
+static int __ethernet_connect_main(sa_eth_s *info)
+{
+       if (info == NULL) {
+               return -1;
+       }
 
+       if(__eth_register_client() == 1) {
+               __eth_update_ip(info);
 
+               __eth_deregister_client();
+       }
+       else
+               _D("connection error!!!\n");
+
+
+       return 0;
+}
 
 
 static int __network_get_wifi_state(connection_h connection, connection_wifi_state_e *wifi_state)
@@ -427,14 +602,14 @@ static sa_error_e __network_get_state(sa_network_state_e *conn_state, sa_network
                _E("create connection handle error [%s]", __print_error(rv));
                return -1;
        }
-       
+
        // check network state(eth/wifi)
        rv = connection_get_type(connection, &net_state);
        if (rv != CONNECTION_ERROR_NONE) {
                _E("Fail to get network state [%s]", __print_error(rv));
        } else {
                ret = SA_ERROR_NONE;
-               _D("Retval = [%s] network connection state [%s]", __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) {
@@ -475,7 +650,15 @@ static sa_error_e __network_connect(sa_network_s *info)
        if (info == NULL) {
                return SA_ERROR_INVALID_PARAMETER;
        }
-       
+
+       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);
+               }
+       }
+
        if (info->wifi != NULL) {
                if (info->wifi->enabled == TRUE) {
                        retWifi = __wifi_connect_main(info->wifi);
@@ -486,13 +669,7 @@ static sa_error_e __network_connect(sa_network_s *info)
                }
        }
 
-       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;
 }
@@ -516,7 +693,7 @@ sa_error_e sa_setup_network(sa_network_s *network)
        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
+}