Merge branch 'tizen', remote-tracking branch 'upstream/master'
[platform/core/system/setup-adaptor.git] / src / setup_network.c
1 /*
2  * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include <glib.h>
17 #include <stdio.h>
18 #include <string.h>
19 #include <stdlib.h>
20 #include "sa_common.h"
21 #include "sa_types.h"
22 #include "setup_network.h"
23 #include "input_file.h"
24 #include "net_connection.h"
25 #include "wifi-manager.h"
26
27 typedef struct {
28         wifi_manager_h wifi_h;
29         GMainLoop *loop;
30         char *ssid;
31         char *password;
32 } _wifi_data_s;
33 /*
34 static char *__print_connection_state(sa_network_state_e state)
35 {
36         switch (state) {
37         case SA_NETWORK_STATE_DISCONNECTED:
38                 return "Disconnected";
39         case SA_NETWORK_STATE_PROGRESSING:
40                 return "Progressing";
41         case SA_NETWORK_STATE_CONNECTED:
42                 return "connected";
43         default:
44                 return "Unknown";
45         }
46 }
47
48 static const char *__print_wifi_state(connection_wifi_state_e state)
49 {
50         switch (state) {
51         case CONNECTION_WIFI_STATE_DEACTIVATED:
52                 return "Deactivated";
53         case CONNECTION_WIFI_STATE_DISCONNECTED:
54                 return "Disconnected";
55         case CONNECTION_WIFI_STATE_CONNECTED:
56                 return "Connected";
57         default:
58                 return "Unknown";
59         }
60 }
61
62 static const char *__print_error(connection_error_e error)
63 {
64         switch (error) {
65         case CONNECTION_ERROR_NONE:
66                 return "CONNECTION_ERROR_NONE";
67         case CONNECTION_ERROR_INVALID_PARAMETER:
68                 return "CONNECTION_ERROR_INVALID_PARAMETER";
69         case CONNECTION_ERROR_OUT_OF_MEMORY:
70                 return "CONNECTION_ERROR_OUT_OF_MEMORY";
71         case CONNECTION_ERROR_INVALID_OPERATION:
72                 return "CONNECTION_ERROR_INVALID_OPERATION";
73         case CONNECTION_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED:
74                 return "CONNECTION_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED";
75         case CONNECTION_ERROR_OPERATION_FAILED:
76                 return "CONNECTION_ERROR_OPERATION_FAILED";
77         case CONNECTION_ERROR_ITERATOR_END:
78                 return "CONNECTION_ERROR_ITERATOR_END";
79         case CONNECTION_ERROR_NO_CONNECTION:
80                 return "CONNECTION_ERROR_NO_CONNECTION";
81         case CONNECTION_ERROR_NOW_IN_PROGRESS:
82                 return "CONNECTION_ERROR_NOW_IN_PROGRESS";
83         case CONNECTION_ERROR_ALREADY_EXISTS:
84                 return "CONNECTION_ERROR_ALREADY_EXISTS";
85         case CONNECTION_ERROR_OPERATION_ABORTED:
86                 return "CONNECTION_ERROR_OPERATION_ABORTED";
87         case CONNECTION_ERROR_DHCP_FAILED:
88                 return "CONNECTION_ERROR_DHCP_FAILED";
89         case CONNECTION_ERROR_INVALID_KEY:
90                 return "CONNECTION_ERROR_INVALID_KEY";
91         case CONNECTION_ERROR_NO_REPLY:
92                 return "CONNECTION_ERROR_NO_REPLY";
93         case CONNECTION_ERROR_PERMISSION_DENIED:
94                 return "CONNECTION_ERROR_PERMISSION_DENIED";
95         case CONNECTION_ERROR_NOT_SUPPORTED:
96                 return "CONNECTION_ERROR_NOT_SUPPORTED";
97         default:
98                 return "CONNECTION_ERROR_UNKNOWN";
99         }
100 }
101
102 static const char *__print_connection_type(connection_type_e type)
103 {
104         switch (type) {
105         case CONNECTION_TYPE_DISCONNECTED:
106                 return "Disconnected";
107         case CONNECTION_TYPE_WIFI:
108                 return "Wifi";
109         case CONNECTION_TYPE_CELLULAR:
110                 return "Cellular";
111         case CONNECTION_TYPE_ETHERNET:
112                 return "Ethernet";
113         case CONNECTION_TYPE_BT:
114                 return "BT";
115         case CONNECTION_TYPE_NET_PROXY:
116                 return "Net_Proxy";
117         default:
118                 return "Unknown";
119         }
120 }
121
122 static const char *__print_network_type(sa_network_type_e type)
123 {
124         switch (type) {
125         case SA_NETWORK_TYPE_ETH:
126                 return "Ethernet";
127         case SA_NETWORK_TYPE_WIFI:
128                 return "Wifi";
129         default:
130                 return "Unknown";
131         }
132 }
133 */
134 static const char *__print_wifi_error(wifi_manager_error_e err_type)
135 {
136         switch (err_type) {
137         case WIFI_MANAGER_ERROR_NONE:
138                 return "NONE";
139         case WIFI_MANAGER_ERROR_INVALID_PARAMETER:
140                 return "INVALID_PARAMETER";
141         case WIFI_MANAGER_ERROR_OUT_OF_MEMORY:
142                 return "OUT_OF_MEMORY";
143         case WIFI_MANAGER_ERROR_INVALID_OPERATION:
144                 return "INVALID_OPERATION";
145         case WIFI_MANAGER_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED:
146                 return "ADDRESS_FAMILY_NOT_SUPPORTED";
147         case WIFI_MANAGER_ERROR_OPERATION_FAILED:
148                 return "OPERATION_FAILED";
149         case WIFI_MANAGER_ERROR_NO_CONNECTION:
150                 return "NO_CONNECTION";
151         case WIFI_MANAGER_ERROR_NOW_IN_PROGRESS:
152                 return "NOW_IN_PROGRESS";
153         case WIFI_MANAGER_ERROR_ALREADY_EXISTS:
154                 return "ALREADY_EXISTS";
155         case WIFI_MANAGER_ERROR_OPERATION_ABORTED:
156                 return "OPERATION_ABORTED";
157         case WIFI_MANAGER_ERROR_DHCP_FAILED:
158                 return "DHCP_FAILED";
159         case WIFI_MANAGER_ERROR_INVALID_KEY:
160                 return "INVALID_KEY";
161         case WIFI_MANAGER_ERROR_OUT_OF_RANGE:
162                 return "OUT_OF_RANGE";
163         case WIFI_MANAGER_ERROR_PIN_MISSING:
164                 return "PIN_MISSING";
165         case WIFI_MANAGER_ERROR_CONNECT_FAILED:
166                 return "CONNECT_FAILED";
167         case WIFI_MANAGER_ERROR_LOGIN_FAILED:
168                 return "LOGIN_FAILED";
169         case WIFI_MANAGER_ERROR_AUTHENTICATION_FAILED:
170                 return "AUTH_FAILED";
171         case WIFI_MANAGER_ERROR_NO_REPLY:
172                 return "NO_REPLY";
173         case WIFI_MANAGER_ERROR_SECURITY_RESTRICTED:
174                 return "SECURITY_RESTRICTED";
175         case WIFI_MANAGER_ERROR_ALREADY_INITIALIZED:
176                 return "ALREADY_INITIALIZED";
177         case WIFI_MANAGER_ERROR_PERMISSION_DENIED:
178                 return "PERMISSION_DENIED";
179         case WIFI_MANAGER_ERROR_NOT_SUPPORTED:
180                 return "NOT_SUPPORTED";
181         case WIFI_MANAGER_ERROR_WPS_OVERLAP:
182                 return "WPS_OVERLAP";
183         case WIFI_MANAGER_ERROR_WPS_TIMEOUT:
184                 return "WPS_TIMEOUT";
185         case WIFI_MANAGER_ERROR_WPS_WEP_PROHIBITED:
186                 return "WPS_WEP_PROHIBITED";
187         default:
188                 return "UNKNOWN";
189         }
190 }
191
192 static void __wifi_exit_loop(_wifi_data_s * wifi_data)
193 {
194         _D("__wifi_exit_loop !!!");
195         if (wifi_data)
196                 g_main_loop_quit((GMainLoop *) wifi_data->loop);
197 }
198
199 static void __wifi_connected_cb(wifi_manager_error_e result, void *user_data)
200 {
201         _wifi_data_s *wifi_data = (_wifi_data_s *) user_data;
202
203         if (result == WIFI_MANAGER_ERROR_NONE)
204                 _D("__wifi_connected_cb() connected Successfully !!!");
205         else
206                 _D("__wifi_connected_cb() connection Failed!(%s)", __print_wifi_error(result));
207
208         __wifi_exit_loop(wifi_data);
209 }
210
211 // CAUTION : this callback function should be boolean type.
212 // return true : continue search ap (not found)
213 // return false : stop search ap (found)
214 static bool __wifi_connect_ap_cb(wifi_manager_ap_h ap, void *user_data)
215 {
216         _wifi_data_s *wifi_data = (_wifi_data_s *) user_data;
217
218         _D("__wifi_connect_ap_cb()");
219
220         int rv = 0;
221         char *ap_name = NULL;
222
223         rv = wifi_manager_ap_get_essid(ap, &ap_name);
224         if (rv != WIFI_MANAGER_ERROR_NONE) {
225                 _D("Fail to get AP name [%s]", __print_wifi_error((wifi_manager_error_e) rv));
226                 g_free(ap_name);
227                 return false;
228         } else {
229                 _D("AP name = [%s]/[%s]", ap_name, wifi_data->ssid);
230         }
231
232         if (g_strcmp0(ap_name, wifi_data->ssid) == 0) {
233                 bool required = FALSE;
234
235                 _D("OK!!! connect AP [%s]", ap_name);
236
237                 rv = wifi_manager_ap_is_passphrase_required(ap, &required);
238
239                 if (rv == WIFI_MANAGER_ERROR_NONE) {
240                         if (required) {
241                                 _D(" passphrase1 : [%s]", wifi_data->password);
242                                 rv = wifi_manager_ap_set_passphrase(ap, wifi_data->password);
243                                 if (rv != WIFI_MANAGER_ERROR_NONE) {
244                                         _D("Fail to set passphrase : %s", __print_wifi_error(rv));
245                                         __wifi_exit_loop(wifi_data);
246                                 }
247                         }
248                         if (rv == WIFI_MANAGER_ERROR_NONE) {
249                                 rv = wifi_manager_connect(wifi_data->wifi_h, ap, __wifi_connected_cb, wifi_data);
250                                 if (rv != WIFI_MANAGER_ERROR_NONE) {
251                                         _D("Fail to connection request [%s] : %s", ap_name, __print_wifi_error(rv));
252                                         __wifi_exit_loop(wifi_data);
253                                 } else {
254                                         _D("Success to connection request [%s]", ap_name);
255                                 }
256                         }
257                 } else {
258                         _D("Fail to passphrase_required request [%s]", __print_wifi_error(rv));
259                         __wifi_exit_loop(wifi_data);
260                 }
261
262                 g_free(ap_name);
263                 return false;
264         }
265
266         g_free(ap_name);
267         return true;
268 }
269
270 static void __wifi_scan_finished_cb(wifi_manager_error_e result, void *user_data)
271 {
272         int rv = 0;
273         _wifi_data_s *wifi_data = (_wifi_data_s *) user_data;
274
275         _D("__wifi_scan_finished_cb() result=[%s]", __print_wifi_error(result));
276
277         rv = wifi_manager_foreach_found_ap(wifi_data->wifi_h, __wifi_connect_ap_cb, wifi_data);
278
279         if (rv != WIFI_MANAGER_ERROR_NONE)
280                 _E("Fail to connect (can't get AP list) [%s]", __print_wifi_error(rv));
281
282         _D("Connection step finished");
283         // If ssid is not able to find, setup-adaptor service will be completed by systemd
284 }
285
286 static void __wifi_state_changed_cb(wifi_manager_device_state_e state, void *user_data)
287 {
288         _wifi_data_s *wifi_data = (_wifi_data_s *) user_data;
289
290         _D("__wifi_state_changed_cb() state=[%d]", state);
291
292         if (state == WIFI_MANAGER_DEVICE_STATE_ACTIVATED) {
293                 wifi_manager_scan(wifi_data->wifi_h, __wifi_scan_finished_cb, wifi_data);
294                 wifi_manager_unset_device_state_changed_cb(wifi_data->wifi_h);
295         }
296 }
297
298 static void __wifi_activate_cb(wifi_manager_error_e result, void *user_data)
299 {
300         _D("__wifi_activate_cb() result=[%s]", __print_wifi_error(result));
301 }
302
303 static int __wifi_connect_main(sa_wifi_s * info)
304 {
305         GMainLoop *gmain_loop = NULL;
306         GMainContext *context;
307
308         _wifi_data_s wifi_data = { 0, };
309
310         int rv = 0;
311         sa_error_e ret = SA_ERROR_NONE;
312         wifi_manager_h wifi_h = NULL;
313
314         _D("__wifi_connect_main (ssid=%s, pw=%s)", info->ssid, info->password);
315
316         if (info == NULL) {
317                 _E("invalid input (sa_wifi_s*)");
318                 return -1;
319         }
320
321         context = g_main_context_new();
322
323         if (context != NULL) {
324                 g_main_context_push_thread_default(context);    // should be.. to get mainloop message in thread
325                 gmain_loop = g_main_loop_new(context, FALSE);
326
327                 if (gmain_loop != NULL) {
328                         rv = wifi_manager_initialize(&wifi_h);
329
330                         if (rv == WIFI_MANAGER_ERROR_NONE) {
331                                 // fill the token data
332                                 wifi_data.ssid = info->ssid;
333                                 wifi_data.password = info->password;
334                                 wifi_data.loop = gmain_loop;
335                                 wifi_data.wifi_h = wifi_h;
336
337                                 rv = wifi_manager_set_device_state_changed_cb(wifi_h, __wifi_state_changed_cb, (void *)&wifi_data);
338                                 if (rv == WIFI_MANAGER_ERROR_NONE) {
339                                         rv = wifi_manager_activate(wifi_h, __wifi_activate_cb, NULL);
340                                         if (rv == WIFI_MANAGER_ERROR_ALREADY_EXISTS) {
341                                                 _D("wifi_manager_active alreay exists");
342                                                 wifi_manager_scan(wifi_h, __wifi_scan_finished_cb, (void *)&wifi_data);
343                                         } else if (rv != WIFI_MANAGER_ERROR_NONE) {
344                                                 _E("wifi_manager_activated failed.[%s]", __print_wifi_error(rv));
345                                                 ret = SA_ERROR_UNKNOWN;
346                                         }
347                                 } else {
348                                         ret = SA_ERROR_UNKNOWN;
349                                         _E("wifi_manager_set_device_state_changed_cb error");
350                                 }
351                         } else {
352                                 ret = SA_ERROR_UNKNOWN;
353                                 _E("Wifi init failed [%s]", __print_wifi_error(rv));
354                         }
355
356                         if (ret == SA_ERROR_NONE)
357                                 g_main_loop_run(gmain_loop);
358
359                         g_main_loop_unref(gmain_loop);
360                         g_main_context_unref(context);
361
362                         if (wifi_h != NULL) {
363                                 wifi_manager_unset_scan_state_changed_cb(wifi_h);
364                                 wifi_manager_unset_connection_state_changed_cb(wifi_h);
365                         }
366
367                         gmain_loop = NULL;
368                         context = NULL;
369
370                         if (wifi_h != NULL) {
371                         rv = wifi_manager_deinitialize(wifi_h);
372                                 if (rv != WIFI_MANAGER_ERROR_NONE) {
373                                         _D("Fail to deinitialize.");
374                                         ret = SA_ERROR_UNKNOWN;
375                                 }
376                         }
377                 } else {
378                         g_main_context_unref(context);
379                         _E("Fail to create g_main_loop");
380                 }
381         } else {
382                 _E("Fail to create g_main_context");
383         }
384
385         _D("__wifi_connect_main completed.");
386
387         return ret;
388 }
389
390 connection_h eth_connect;
391
392 static bool __eth_get_user_selected_profile(connection_profile_h * profile, bool select)
393 {
394         int rv = 0;
395         char *profile_name;
396         connection_profile_type_e profile_type;
397         connection_profile_state_e profile_state;
398         connection_profile_iterator_h profile_iter;
399         connection_profile_h profile_h;
400
401         rv = connection_get_profile_iterator(eth_connect, CONNECTION_ITERATOR_TYPE_REGISTERED, &profile_iter);
402         if (rv != CONNECTION_ERROR_NONE) {
403                 printf("Fail to get profile iterator error\n");
404                 return false;
405         }
406
407         while (connection_profile_iterator_has_next(profile_iter)) {
408                 if (connection_profile_iterator_next(profile_iter, &profile_h) != CONNECTION_ERROR_NONE) {
409                         printf("Fail to get profile handle\n");
410                         return false;
411                 }
412
413                 if (connection_profile_get_name(profile_h, &profile_name) != CONNECTION_ERROR_NONE) {
414                         printf("Fail to get profile name\n");
415                         return false;
416                 }
417
418                 if (connection_profile_get_type(profile_h, &profile_type) != CONNECTION_ERROR_NONE) {
419                         printf("Fail to get profile type\n");
420                         g_free(profile_name);
421                         return false;
422                 }
423
424                 if (connection_profile_get_state(profile_h, &profile_state) != CONNECTION_ERROR_NONE) {
425                         printf("Fail to get profile state\n");
426                         g_free(profile_name);
427                         return false;
428                 }
429
430                 if (!strncmp(profile_name, "eth0", strlen("eth0"))) {
431                         printf("profile > %s\n", profile_name);
432                         if (profile)
433                                 *profile = profile_h;
434                 }
435                 g_free(profile_name);
436         }
437
438         return true;
439 }
440
441 static int __eth_update_ip_info(sa_eth_s * info, connection_profile_h profile, connection_address_family_e address_family)
442 {
443         int rv = 0;
444
445         _D("ipaddress =%s", info->staticInfo->ipAddress);
446         if (strlen(info->staticInfo->ipAddress) > 0) {
447                 rv = connection_profile_set_ip_address(profile, address_family, info->staticInfo->ipAddress);
448                 if (rv != CONNECTION_ERROR_NONE)
449                         return -1;
450         }
451
452         _D("netmask =%s", info->staticInfo->netmask);
453         if (strlen(info->staticInfo->netmask) > 0) {
454                 rv = connection_profile_set_subnet_mask(profile, address_family, info->staticInfo->netmask);
455                 if (rv != CONNECTION_ERROR_NONE)
456                         return -1;
457
458         }
459
460         if (strlen(info->staticInfo->defaultGateway) > 0) {
461                 rv = connection_profile_set_gateway_address(profile, address_family, info->staticInfo->defaultGateway);
462                 if (rv != CONNECTION_ERROR_NONE)
463                         return -1;
464                 _D("gateway =%s", info->staticInfo->defaultGateway);
465         }
466         if (strlen(info->staticInfo->primaryDnsServer) > 0) {
467                 rv = connection_profile_set_dns_address(profile, 1, address_family, info->staticInfo->primaryDnsServer);
468                 if (rv != CONNECTION_ERROR_NONE)
469                         return -1;
470
471                 _D("DNS 1 =%s", info->staticInfo->primaryDnsServer);
472
473                 if (strlen(info->staticInfo->secondaryDnsServer) > 0) {
474                         rv = connection_profile_set_dns_address(profile, 2, address_family, info->staticInfo->secondaryDnsServer);
475                         if (rv != CONNECTION_ERROR_NONE)
476                                 return -1;
477                         _D("DNS 2 =%s", info->staticInfo->secondaryDnsServer);
478                 }
479         }
480
481         return 1;
482 }
483
484 static int __eth_update_ip(sa_eth_s * info)
485 {
486         int ret = 0;
487
488         connection_profile_type_e prof_type;
489         connection_profile_h profile = NULL;
490         int address_family = 0;         // IPv4
491
492         //printf("\n** Choose a profile to update. **\n");
493         if (__eth_get_user_selected_profile(&profile, true) == false) {
494                 _D("profile failed \n");
495                 return -1;
496         }
497
498         // If ethernet cable is detached, it should be retried after rebooting.
499         // return 1 is retry case, 0 is non-error and -1 is error to be completed
500         ret = connection_profile_get_type(profile, &prof_type);
501         if (ret == CONNECTION_ERROR_INVALID_PARAMETER) {
502                 _D("profile connection error!! [%d]", ret);
503                 return 1;
504         } else if (ret != CONNECTION_ERROR_NONE) {
505                 return -1;
506         }
507
508         if (connection_profile_set_ip_config_type(profile, address_family, CONNECTION_IP_CONFIG_TYPE_STATIC) != CONNECTION_ERROR_NONE) {
509                 _D("setting error !!\n");
510                 return -1;
511         }
512         if (__eth_update_ip_info(info, profile, address_family) != 1) {
513                 _D("ip setting error!!\n");
514                 return -1;
515         }
516
517         if (connection_profile_set_proxy_type(profile, CONNECTION_PROXY_TYPE_DIRECT) != CONNECTION_ERROR_NONE)
518                 return -1;
519
520         if (connection_update_profile(eth_connect, profile) != CONNECTION_ERROR_NONE) {
521                 _D("profile update error !!\n");
522                 return -1;
523         }
524
525         _D("operation sucess\n");
526         return ret;
527 }
528
529 static int __eth_register_client(void)
530 {
531
532         if (CONNECTION_ERROR_NONE != connection_create(&eth_connect)) {
533                 _D("Client registration failed \n");
534                 return -1;
535         }
536
537         _D("Client registration success\n");
538         return 1;
539 }
540
541 static int __eth_deregister_client(void)
542 {
543         int rv = 0;
544
545         if (eth_connect != NULL)
546                 rv = connection_destroy(eth_connect);
547         else {
548                 _D("Cannot deregister : Handle is NULL\n");
549                 rv = CONNECTION_ERROR_INVALID_OPERATION;
550         }
551
552         if (rv != CONNECTION_ERROR_NONE) {
553                 _D("Client deregistration fail\n");
554                 return -1;
555         }
556
557         eth_connect = NULL;
558         _D("Client deregistration success\n");
559
560         return 1;
561 }
562
563 static int __ethernet_connect_main(sa_eth_s * info)
564 {
565         int ret = 0;
566
567         if (info == NULL)
568                 return -1;
569
570         if (__eth_register_client() == 1) {
571                 ret = __eth_update_ip(info);
572
573                 __eth_deregister_client();
574         } else
575                 _D("connection error!!!\n");
576
577         return ret;
578 }
579 /*
580 static int __network_get_wifi_state(connection_h connection, connection_wifi_state_e * wifi_state)
581 {
582         int rv = 0;
583
584         if (connection == NULL)
585                 return -1;
586
587         rv = connection_get_wifi_state(connection, wifi_state);
588         if (rv != CONNECTION_ERROR_NONE)
589                 _D("Fail to get WiFi state [%s]", __print_error(rv));
590         else
591                 _D("Retval = [%s] WiFi state [%s]", __print_error(rv), __print_wifi_state(*wifi_state));
592
593         return 0;
594 }
595
596 static sa_error_e __network_get_state(sa_network_state_e * conn_state, sa_network_type_e * conn_type)
597 {
598         connection_h connection = NULL;
599         sa_error_e ret = SA_ERROR_UNKNOWN;
600         connection_type_e net_state;
601         connection_wifi_state_e wifi_state;
602         int rv = 0;
603
604         rv = connection_create(&connection);
605         if (rv != CONNECTION_ERROR_NONE) {
606                 _E("create connection handle error [%s]", __print_error(rv));
607                 return -1;
608         }
609         // check network state(eth/wifi)
610         rv = connection_get_type(connection, &net_state);
611         if (rv != CONNECTION_ERROR_NONE) {
612                 _E("Fail to get network state [%s]", __print_error(rv));
613         } else {
614                 ret = SA_ERROR_NONE;
615                 _D("Retval = [%s] network connection state [%s]", __print_error(rv), __print_connection_type(net_state));
616                 if (CONNECTION_TYPE_DISCONNECTED == net_state) {
617                         *conn_state = SA_NETWORK_STATE_DISCONNECTED;
618                 } else if (CONNECTION_TYPE_WIFI == net_state) {
619                         *conn_type = SA_NETWORK_TYPE_WIFI;
620                         // check wifi state
621                         if (!__network_get_wifi_state(connection, &wifi_state)) {
622                                 if (wifi_state == CONNECTION_WIFI_STATE_CONNECTED)
623                                         *conn_state = SA_NETWORK_STATE_CONNECTED;
624                                 else
625                                         *conn_state = SA_NETWORK_STATE_DISCONNECTED;
626                         }
627                 } else if (CONNECTION_TYPE_ETHERNET == net_state) {
628                         *conn_type = SA_NETWORK_TYPE_ETH;
629                         *conn_state = SA_NETWORK_STATE_CONNECTED;
630                 } else {
631                         *conn_state = SA_NETWORK_STATE_UNKNOWN;
632                         *conn_type = SA_NETWORK_TYPE_NONE;
633                 }
634         }
635
636         rv = connection_destroy(connection);
637         if (rv != CONNECTION_ERROR_NONE) {
638                 _E("connection destroy fail [%s]", __print_error(rv));
639                 return -1;
640         }
641
642         return ret;
643 }
644 */
645 extern void sa_inputfile_flag(void);
646
647 static sa_error_e __network_connect(sa_network_s * info)
648 {
649         sa_error_e ret = SA_ERROR_NONE;
650         int retWifi = 0;
651         int retEth = 0;
652
653         if (info == NULL) {
654                 return SA_ERROR_INVALID_PARAMETER;
655         } else {
656                 if (info->wifi != NULL) {
657                         _D("----------- wifi info -----------");
658                         _D("info|wifi|enabled(%d)", info->wifi->enabled);
659                         _D("info|wifi|dhcpEnabled(%d)", info->wifi->dhcpEnabled);
660                         _D("info|wifi|ssid(%s)", info->wifi->ssid);
661                         _D("info|wifi|password(%s)", info->wifi->password);
662                         if (info->wifi->staticInfo != NULL) {
663                                 _D("info|wifi|staticInfo|ipAddress(%s)", info->wifi->staticInfo->ipAddress);
664                                 _D("info|wifi|staticInfo|netmask(%s)", info->wifi->staticInfo->netmask);
665                                 _D("info|wifi|staticInfo|defaultGateway(%s)", info->wifi->staticInfo->defaultGateway);
666                                 _D("info|wifi|staticInfo|primaryDnsServer(%s)", info->wifi->staticInfo->primaryDnsServer);
667                                 _D("info|wifi|staticInfo|secondaryDnsServer(%s)", info->wifi->staticInfo->secondaryDnsServer);
668                         }
669                 }
670
671                 if (info->eth != NULL) {
672                         _D("----------- ethernet info -----------");
673                         _D("info|eth|enabled(%d)", info->eth->enabled);
674                         _D("info|eth|dhcpEnabled(%d)", info->eth->dhcpEnabled);
675                         if (info->eth->staticInfo != NULL) {
676                                 _D("info|eth|staticInfo|ipAddress(%s)", info->eth->staticInfo->ipAddress);
677                                 _D("info|eth|staticInfo|netmask(%s)", info->eth->staticInfo->netmask);
678                                 _D("info|eth|staticInfo|defaultGateway(%s)", info->eth->staticInfo->defaultGateway);
679                                 _D("info|eth|staticInfo|primaryDnsServer(%s)", info->eth->staticInfo->primaryDnsServer);
680                                 _D("info|eth|staticInfo|secondaryDnsServer(%s)", info->eth->staticInfo->secondaryDnsServer);
681                         }
682                 }
683         }
684         if (info->eth != NULL) {
685                 // decide whether it will be set according to policy
686                 if ((info->eth->enabled == TRUE) && !sa_inputfile_get_completion_flag(SA_FILE_CONFIG_ETHERNET)) {
687                         retEth = __ethernet_connect_main(info->eth);
688                         _D("return ethernet [%d]", retEth);
689                         if (retEth == 1)
690                                 _D("retry after rebooting, do not remove completion flag");
691                         else
692                                 sa_inputfile_set_completion_flag(SA_FILE_CONFIG_ETHERNET);
693                 } else {
694                         // if eth info is null, ethernet completion flag is set
695                         sa_inputfile_set_completion_flag(SA_FILE_CONFIG_ETHERNET);
696                 }
697         } else {
698                 // if eth info is null, ethernet completion flag is set
699                 sa_inputfile_set_completion_flag(SA_FILE_CONFIG_ETHERNET);
700         }
701
702         if (info->wifi != NULL) {
703                 if ((info->wifi->enabled == TRUE) && !sa_inputfile_get_completion_flag(SA_FILE_CONFIG_WIFI)) {
704                         retWifi = __wifi_connect_main(info->wifi);
705
706                         _D("return wifi [%d]", retWifi);
707                 } else {
708                         _D("wifi enabled flag is false");
709                 }
710         }
711         sa_inputfile_set_completion_flag(SA_FILE_CONFIG_WIFI);
712
713         return ret;
714 }
715
716 sa_error_e sa_setup_network(sa_network_s * network)
717 {
718         sa_error_e ret = SA_ERROR_NONE;
719
720         if (network == NULL) {
721                 _E("__set_network is null");
722                 return SA_ERROR_INVALID_PARAMETER;
723         }
724         // 1. check network state
725         // 2. if it is connected, read detail info
726         ret = __network_connect(network);
727
728         return ret;
729 }