1baa2ce295545bad61bd77a7b5324139067f985c
[platform/core/api/wifi.git] / src / libnetwork.c
1 /*
2  * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd All Rights Reserved
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
17 #include <glib.h>
18 #include <ctype.h>
19 #include <stdio.h>
20 #include <string.h>
21
22 #include "wifi_dbus_private.h"
23 #include "net_wifi_private.h"
24
25 static __thread bool is_init = false;
26 static __thread GSList *ap_handle_list = NULL;
27
28 struct _wifi_cb_s {
29         wifi_device_state_changed_cb device_state_cb;
30         void *device_state_user_data;
31         wifi_scan_finished_cb bg_scan_cb;
32         void *bg_scan_user_data;
33         wifi_scan_finished_cb scan_request_cb;
34         void *scan_request_user_data;
35         wifi_scan_finished_cb specific_scan_cb;
36         void *specific_scan_user_data;
37         wifi_connection_state_changed_cb connection_state_cb;
38         void *connection_state_user_data;
39         wifi_activated_cb activated_cb;
40         void *activated_user_data;
41         wifi_deactivated_cb deactivated_cb;
42         void *deactivated_user_data;
43         wifi_connected_cb connected_cb;
44         void *connected_user_data;
45         wifi_disconnected_cb disconnected_cb;
46         void *disconnected_user_data;
47 };
48
49 struct _profile_list_s {
50         int count;
51         net_profile_info_t *profiles;
52 };
53
54 struct _wifi_state_notify {
55         net_profile_info_t *ap_info;
56         wifi_connection_state_e state;
57 };
58
59 struct managed_idle_data {
60         GSourceFunc func;
61         gpointer user_data;
62         guint id;
63 };
64
65 static __thread struct _wifi_cb_s wifi_callbacks = { 0, };
66 static __thread struct _profile_list_s profile_iterator = { 0, NULL };
67 static __thread struct _profile_list_s specific_profile_iterator = {0, NULL};
68 static __thread char specific_profile_essid[NET_WLAN_ESSID_LEN + 1] = { 0, };
69 static __thread bool is_feature_checked = false;
70 static __thread bool feature_supported = false;
71 static __thread GSList *managed_idler_list = NULL;
72 static __thread bool wifi_is_feature_checked[WIFI_SUPPORTED_FEATURE_MAX] = {0, };
73 static __thread bool wifi_feature_supported[WIFI_SUPPORTED_FEATURE_MAX] = {0, };
74
75 wifi_dbus *g_dbus_h = NULL;
76
77 bool _wifi_is_init(void)
78 {
79         return is_init;
80 }
81
82 static void __wifi_set_init(bool tag)
83 {
84         is_init = tag;
85 }
86
87 static wifi_error_e __libnet_convert_to_ap_error_type(net_err_t err_type)
88 {
89         switch (err_type) {
90         case NET_ERR_NONE:
91                 return WIFI_ERROR_NONE;
92         case NET_ERR_APP_ALREADY_REGISTERED:
93                 return WIFI_ERROR_INVALID_OPERATION;
94         case NET_ERR_APP_NOT_REGISTERED:
95                 return WIFI_ERROR_INVALID_OPERATION;
96         case NET_ERR_NO_ACTIVE_CONNECTIONS:
97                 return WIFI_ERROR_NO_CONNECTION;
98         case NET_ERR_ACTIVE_CONNECTION_EXISTS:
99                 return WIFI_ERROR_ALREADY_EXISTS;
100         case NET_ERR_CONNECTION_DHCP_FAILED:
101                 return WIFI_ERROR_DHCP_FAILED;
102         case NET_ERR_CONNECTION_INVALID_KEY:
103                 return WIFI_ERROR_INVALID_KEY;
104         case NET_ERR_IN_PROGRESS:
105                 return WIFI_ERROR_NOW_IN_PROGRESS;
106         case NET_ERR_OPERATION_ABORTED:
107                 return WIFI_ERROR_OPERATION_ABORTED;
108         case NET_ERR_TIME_OUT:
109                 return WIFI_ERROR_NO_REPLY;
110         case NET_ERR_ACCESS_DENIED:
111                 return WIFI_ERROR_PERMISSION_DENIED;
112         default:
113                 return WIFI_ERROR_OPERATION_FAILED;
114         }
115 }
116
117 static const char *__libnet_convert_ap_error_type_to_string(wifi_error_e err_type)
118 {
119         switch (err_type) {
120         case WIFI_ERROR_NONE:
121                 return "NONE";
122         case WIFI_ERROR_INVALID_PARAMETER:
123                 return "INVALID_PARAMETER";
124         case WIFI_ERROR_OUT_OF_MEMORY:
125                 return "OUT_OF_MEMORY";
126         case WIFI_ERROR_INVALID_OPERATION:
127                 return "INVALID_OPERATION";
128         case WIFI_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED:
129                 return "ADDRESS_FAMILY_NOT_SUPPORTED";
130         case WIFI_ERROR_OPERATION_FAILED:
131                 return "OPERATION_FAILED";
132         case WIFI_ERROR_NO_CONNECTION:
133                 return "NO_CONNECTION";
134         case WIFI_ERROR_NOW_IN_PROGRESS:
135                 return "NOW_IN_PROGRESS";
136         case WIFI_ERROR_ALREADY_EXISTS:
137                 return "ALREADY_EXISTS";
138         case WIFI_ERROR_OPERATION_ABORTED:
139                 return "OPERATION_ABORTED";
140         case WIFI_ERROR_DHCP_FAILED:
141                 return "DHCP_FAILED";
142         case WIFI_ERROR_INVALID_KEY:
143                 return "INVALID_KEY";
144         case WIFI_ERROR_NO_REPLY:
145                 return "NO_REPLY";
146         case WIFI_ERROR_SECURITY_RESTRICTED:
147                 return "SECURITY_RESTRICTED";
148         case WIFI_ERROR_PERMISSION_DENIED:
149                 return "PERMISSION_DENIED";
150         case WIFI_ERROR_NOT_SUPPORTED:
151                 return "NOT_SUPPROTED";
152         }
153
154         return "UNKNOWN";
155 }
156
157 static const char *__libnet_convert_ap_state_to_string(wifi_connection_state_e state)
158 {
159         switch (state) {
160         case WIFI_CONNECTION_STATE_FAILURE:
161                 return "FAILURE";
162         case WIFI_CONNECTION_STATE_DISCONNECTED:
163                 return "DISCONNECTED";
164         case WIFI_CONNECTION_STATE_ASSOCIATION:
165                 return "ASSOCIATION";
166         case WIFI_CONNECTION_STATE_CONFIGURATION:
167                 return "CONFIGURATION";
168         case WIFI_CONNECTION_STATE_CONNECTED:
169                 return "CONNECTED";
170         default:
171                 return "UNKNOWN";
172         }
173 }
174
175 static void __libnet_clear_profile_list(struct _profile_list_s *profile_list)
176 {
177         if (profile_list->count > 0)
178                 g_free(profile_list->profiles);
179
180         profile_list->count = 0;
181         profile_list->profiles = NULL;
182 }
183
184 static int __libnet_update_profile_iterator(void)
185 {
186         int rv;
187         struct _profile_list_s wifi_profiles = { 0, NULL };
188
189         __libnet_clear_profile_list(&profile_iterator);
190
191         rv = net_get_profile_list(NET_DEVICE_WIFI, &wifi_profiles.profiles, &wifi_profiles.count);
192         WIFI_LOG(WIFI_INFO, "Wi-Fi profile count: %d", wifi_profiles.count);
193
194         if (rv == NET_ERR_ACCESS_DENIED) {
195                 WIFI_LOG(WIFI_ERROR, "Access denied");
196                 return WIFI_ERROR_PERMISSION_DENIED;
197         }
198
199         if (wifi_profiles.count == 0)
200                 return WIFI_ERROR_NONE;
201
202         profile_iterator.count = wifi_profiles.count;
203         profile_iterator.profiles = wifi_profiles.profiles;
204
205         return WIFI_ERROR_NONE;
206 }
207
208 static void __libnet_update_specific_profile_iterator(GSList *ap_list)
209 {
210         int count = 0;
211         GSList *list = ap_list;
212
213         for (count = 0; list; list = list->next)
214                 count++;
215
216         if (count == 0) {
217                 WIFI_LOG(WIFI_INFO, "No hidden AP found\n");
218                 return;
219         }
220
221         specific_profile_iterator.count = count;
222         specific_profile_iterator.profiles = g_try_new0(net_profile_info_t, count);
223
224         list = ap_list;
225         for (count = 0; list; list = list->next) {
226                 struct ssid_scan_bss_info_t *ap = (struct ssid_scan_bss_info_t *)list->data;
227                 net_profile_info_t *profile = &specific_profile_iterator.profiles[count];
228
229                 g_strlcpy(profile->ProfileInfo.Wlan.essid, ap->ssid, NET_WLAN_ESSID_LEN+1);
230                 profile->ProfileInfo.Wlan.security_info.sec_mode = ap->security;
231
232                 count++;
233         }
234
235         WIFI_LOG(WIFI_INFO, "Specific AP count : %d\n", count);
236 }
237
238 static void __libnet_convert_profile_info_to_wifi_info(net_wifi_connection_info_t *wifi_info,
239                                                                 net_profile_info_t *ap_info)
240 {
241         g_strlcpy(wifi_info->essid, ap_info->ProfileInfo.Wlan.essid, NET_WLAN_ESSID_LEN+1);
242         wifi_info->wlan_mode = ap_info->ProfileInfo.Wlan.wlan_mode;
243         memcpy(&wifi_info->security_info, &ap_info->ProfileInfo.Wlan.security_info, sizeof(wlan_security_info_t));
244         wifi_info->is_hidden = ap_info->ProfileInfo.Wlan.is_hidden;
245 }
246
247 static int __libnet_connect_with_wifi_info(net_profile_info_t *ap_info)
248 {
249         int rv;
250         net_wifi_connection_info_t wifi_info;
251         memset(&wifi_info, 0, sizeof(net_wifi_connection_info_t));
252
253         __libnet_convert_profile_info_to_wifi_info(&wifi_info, ap_info);
254
255         rv = net_open_connection_with_wifi_info(&wifi_info);
256         if (rv == NET_ERR_ACCESS_DENIED) {
257                 WIFI_LOG(WIFI_ERROR, "Access denied");
258                 return WIFI_ERROR_PERMISSION_DENIED;
259         } else if (rv != NET_ERR_NONE)
260                 return WIFI_ERROR_OPERATION_FAILED;
261
262         return WIFI_ERROR_NONE;
263 }
264
265 static gboolean __wifi_state_changed_cb(gpointer data)
266 {
267         wifi_ap_h ap_info;
268         struct _wifi_state_notify *notify = (struct _wifi_state_notify *)data;
269
270         if (notify == NULL)
271                 return FALSE;
272
273         if (notify->ap_info == NULL) {
274                 g_free(notify);
275                 return FALSE;
276         }
277
278         ap_info = (wifi_ap_h)notify->ap_info;
279
280         _wifi_libnet_add_to_ap_list(ap_info);
281
282         if (wifi_callbacks.connection_state_cb != NULL)
283                 wifi_callbacks.connection_state_cb(notify->state, ap_info,
284                                                 wifi_callbacks.connection_state_user_data);
285
286         _wifi_libnet_remove_from_ap_list(ap_info);
287
288         g_free(notify);
289
290         return FALSE;
291 }
292
293 static void __libnet_state_changed_cb(char *profile_name, net_profile_info_t *profile_info,
294                                                         wifi_connection_state_e state)
295 {
296         guint id;
297         net_profile_info_t *ap_info = NULL;
298         struct _wifi_state_notify *notify = NULL;
299
300         if (_wifi_is_init() != true) {
301                 WIFI_LOG(WIFI_ERROR, "Application is not registered"
302                                 "If multi-threaded, thread integrity be broken.");
303                 return;
304         }
305
306         if (wifi_callbacks.connection_state_cb == NULL)
307                 return;
308
309         if (profile_name == NULL)
310                 return;
311
312         if (profile_info == NULL) {
313                 SECURE_WIFI_LOG(WIFI_ERROR, "Failed to find: %s", profile_name);
314                 return;
315         }
316
317         ap_info = g_try_malloc0(sizeof(net_profile_info_t));
318         if (ap_info == NULL) {
319                 WIFI_LOG(WIFI_ERROR, "Memory allocation error");
320                 return;
321         }
322
323         memcpy(ap_info, profile_info, sizeof(net_profile_info_t));
324
325         notify = g_try_new0(struct _wifi_state_notify, 1);
326         if (notify == NULL) {
327                 g_free(ap_info);
328                 return;
329         }
330
331         notify->ap_info = ap_info;
332         notify->state = state;
333
334         id = _wifi_callback_add(__wifi_state_changed_cb, (gpointer)notify);
335         if (!id)
336                 g_free(notify);
337 }
338
339 static void __libnet_set_activated_cb(wifi_activated_cb user_cb, void *user_data)
340 {
341         if (user_cb != NULL) {
342                 wifi_callbacks.activated_cb = user_cb;
343                 wifi_callbacks.activated_user_data = user_data;
344         }
345 }
346
347 static gboolean __activated_cb_idle(gpointer data)
348 {
349         wifi_error_e result = (wifi_error_e)data;
350
351         if (wifi_callbacks.activated_cb != NULL)
352                 wifi_callbacks.activated_cb(result, wifi_callbacks.activated_user_data);
353
354         wifi_callbacks.activated_cb = NULL;
355         wifi_callbacks.activated_user_data = NULL;
356
357         return FALSE;
358 }
359
360 static void __libnet_set_deactivated_cb(wifi_deactivated_cb user_cb, void *user_data)
361 {
362         if (user_cb != NULL) {
363                 wifi_callbacks.deactivated_cb = user_cb;
364                 wifi_callbacks.deactivated_user_data = user_data;
365         }
366 }
367
368 static gboolean __deactivated_cb_idle(gpointer data)
369 {
370         wifi_error_e result = (wifi_error_e)data;
371
372         if (wifi_callbacks.deactivated_cb != NULL)
373                 wifi_callbacks.deactivated_cb(result, wifi_callbacks.deactivated_user_data);
374
375         wifi_callbacks.deactivated_cb = NULL;
376         wifi_callbacks.deactivated_user_data = NULL;
377
378         return FALSE;
379 }
380
381 static gboolean __device_state_cb_idle(gpointer data)
382 {
383         wifi_device_state_e state = (wifi_device_state_e)data;
384
385         if (wifi_callbacks.device_state_cb != NULL)
386                 wifi_callbacks.device_state_cb(state, wifi_callbacks.device_state_user_data);
387
388         return FALSE;
389 }
390
391 static void __libnet_power_on_off_cb(net_event_info_t *event_cb, bool is_requested)
392 {
393         if (_wifi_is_init() != true) {
394                 WIFI_LOG(WIFI_ERROR, "Application is not registered"
395                                 "If multi-threaded, thread integrity be broken.");
396                 return;
397         }
398
399         if (wifi_callbacks.device_state_cb == NULL &&
400                         wifi_callbacks.activated_cb == NULL &&
401                         wifi_callbacks.deactivated_cb == NULL)
402                 return;
403
404         wifi_error_e error_code = WIFI_ERROR_NONE;
405         wifi_device_state_e state = WIFI_DEVICE_STATE_DEACTIVATED;
406         net_wifi_state_t *wifi_state = (net_wifi_state_t *)event_cb->Data;
407
408         if (event_cb->Error == NET_ERR_NONE &&
409                         event_cb->Datalength == sizeof(net_wifi_state_t)) {
410                 if (*wifi_state == WIFI_ON) {
411                         WIFI_LOG(WIFI_INFO, "Wi-Fi power on");
412                         state = WIFI_DEVICE_STATE_ACTIVATED;
413                 } else if (*wifi_state == WIFI_OFF) {
414                         WIFI_LOG(WIFI_INFO, "Wi-Fi power off");
415                         state = WIFI_DEVICE_STATE_DEACTIVATED;
416                         __libnet_clear_profile_list(&profile_iterator);
417                 } else {
418                         WIFI_LOG(WIFI_ERROR, "Error Wi-Fi state %d", *wifi_state);
419                         error_code = WIFI_ERROR_OPERATION_FAILED;
420                         state = WIFI_DEVICE_STATE_DEACTIVATED;
421                 }
422         } else {
423                 WIFI_LOG(WIFI_ERROR, "Wi-Fi power request failed(%d)", event_cb->Error);
424
425                 if (event_cb->Error == NET_ERR_SECURITY_RESTRICTED)
426                         error_code = WIFI_ERROR_SECURITY_RESTRICTED;
427                 else
428                         error_code = WIFI_ERROR_OPERATION_FAILED;
429
430                 state = WIFI_DEVICE_STATE_DEACTIVATED;
431         }
432
433         if (wifi_callbacks.activated_cb != NULL)
434                 _wifi_callback_add(__activated_cb_idle, (gpointer)error_code);
435
436         if (wifi_callbacks.deactivated_cb != NULL)
437                 _wifi_callback_add(__deactivated_cb_idle, (gpointer)error_code);
438
439         if (wifi_callbacks.device_state_cb != NULL)
440                 _wifi_callback_add(__device_state_cb_idle, (gpointer)state);
441 }
442
443 static gboolean __scan_request_cb_idle(gpointer data)
444 {
445         wifi_error_e error_code = (wifi_error_e)data;
446
447         if (wifi_callbacks.scan_request_cb != NULL)
448                 wifi_callbacks.scan_request_cb(error_code, wifi_callbacks.scan_request_user_data);
449
450         wifi_callbacks.scan_request_cb = NULL;
451         wifi_callbacks.scan_request_user_data = NULL;
452
453         return FALSE;
454 }
455
456 static void __libnet_set_specific_scan_cb(wifi_scan_finished_cb user_cb, void *user_data)
457 {
458         if (user_cb != NULL) {
459                 wifi_callbacks.specific_scan_cb = user_cb;
460                 wifi_callbacks.specific_scan_user_data = user_data;
461         }
462 }
463
464 static gboolean __specific_scan_cb_idle(gpointer data)
465 {
466         wifi_error_e error_code = (wifi_error_e)data;
467
468         if (wifi_callbacks.specific_scan_cb != NULL)
469                 wifi_callbacks.specific_scan_cb(error_code, wifi_callbacks.specific_scan_user_data);
470
471         wifi_callbacks.specific_scan_cb = NULL;
472         wifi_callbacks.specific_scan_user_data = NULL;
473
474         return FALSE;
475 }
476
477 static gboolean __bgscan_cb_idle(gpointer data)
478 {
479         wifi_error_e error_code = (wifi_error_e)data;
480
481         if (wifi_callbacks.bg_scan_cb != NULL)
482                 wifi_callbacks.bg_scan_cb(error_code, wifi_callbacks.bg_scan_user_data);
483
484         return FALSE;
485 }
486
487 static void __libnet_scan_cb(net_event_info_t *event_cb, bool is_requested)
488 {
489         wifi_error_e error_code = WIFI_ERROR_NONE;
490
491         if (_wifi_is_init() != true) {
492                 WIFI_LOG(WIFI_ERROR, "Application is not registered"
493                                 "If multi-threaded, thread integrity be broken.");
494                 return;
495         }
496
497         if (event_cb->Error != NET_ERR_NONE) {
498                 WIFI_LOG(WIFI_ERROR, "Scan failed[%d]", event_cb->Error);
499                 error_code = WIFI_ERROR_OPERATION_FAILED;
500         }
501
502         if (wifi_callbacks.scan_request_cb != NULL) {
503                 _wifi_callback_add(__scan_request_cb_idle, (gpointer)error_code);
504                 return;
505         }
506
507         if (wifi_callbacks.bg_scan_cb != NULL)
508                 _wifi_callback_add(__bgscan_cb_idle, (gpointer)error_code);
509 }
510
511 static void __libnet_specific_scan_cb(net_event_info_t *event_cb)
512 {
513         wifi_error_e error_code = WIFI_ERROR_NONE;
514
515         __libnet_clear_profile_list(&specific_profile_iterator);
516
517         if (event_cb->Error != NET_ERR_NONE) {
518                 WIFI_LOG(WIFI_ERROR, "Specific scan failed!, Error [%d]\n", event_cb->Error);
519                 error_code = WIFI_ERROR_OPERATION_FAILED;
520         } else if (event_cb->Data) {
521                 GSList *ap_list = (GSList *)event_cb->Data;
522                 __libnet_update_specific_profile_iterator(ap_list);
523         }
524
525         if (wifi_callbacks.specific_scan_cb != NULL)
526                 _wifi_callback_add(__specific_scan_cb_idle, (gpointer)error_code);
527 }
528
529 static void __libnet_set_connected_cb(wifi_connected_cb user_cb, void *user_data)
530 {
531         if (user_cb != NULL) {
532                 wifi_callbacks.connected_cb = user_cb;
533                 wifi_callbacks.connected_user_data = user_data;
534         }
535 }
536
537 static gboolean __connected_cb_idle(gpointer data)
538 {
539         wifi_error_e result = (wifi_error_e)data;
540
541         if (wifi_callbacks.connected_cb != NULL)
542                 wifi_callbacks.connected_cb(result, wifi_callbacks.connected_user_data);
543
544         wifi_callbacks.connected_cb = NULL;
545         wifi_callbacks.connected_user_data = NULL;
546
547         return FALSE;
548 }
549
550 static void __libnet_connected_cb(wifi_error_e result)
551 {
552         if (_wifi_is_init() != true) {
553                 WIFI_LOG(WIFI_ERROR, "Application is not registered"
554                                 "If multi-threaded, thread integrity be broken.");
555                 return;
556         }
557
558         if (wifi_callbacks.connected_cb != NULL)
559                 _wifi_callback_add(__connected_cb_idle, (gpointer)result);
560 }
561
562 static void __libnet_set_disconnected_cb(wifi_disconnected_cb user_cb, void *user_data)
563 {
564         if (user_cb != NULL) {
565                 wifi_callbacks.disconnected_cb = user_cb;
566                 wifi_callbacks.disconnected_user_data = user_data;
567         }
568 }
569
570 static gboolean __disconnected_cb_idle(gpointer data)
571 {
572         wifi_error_e result = (wifi_error_e)data;
573
574         if (wifi_callbacks.disconnected_cb != NULL)
575                 wifi_callbacks.disconnected_cb(result, wifi_callbacks.disconnected_user_data);
576
577         wifi_callbacks.disconnected_cb = NULL;
578         wifi_callbacks.disconnected_user_data = NULL;
579
580         return FALSE;
581 }
582
583 static void __libnet_disconnected_cb(wifi_error_e result)
584 {
585         if (_wifi_is_init() != true) {
586                 WIFI_LOG(WIFI_ERROR, "Application is not registered"
587                                 "If multi-threaded, thread integrity be broken.");
588                 return;
589         }
590
591         if (wifi_callbacks.disconnected_cb != NULL)
592                 _wifi_callback_add(__disconnected_cb_idle, (gpointer)result);
593 }
594
595 static void __libnet_evt_cb(net_event_info_t *event_cb, void *user_data)
596 {
597         bool is_requested = false;
598         net_profile_info_t *prof_info_p = NULL;
599         net_profile_info_t prof_info;
600         wifi_error_e result = WIFI_ERROR_NONE;
601         int i = 0;
602         bool is_profile_exists = false;
603
604         switch (event_cb->Event) {
605         case NET_EVENT_OPEN_RSP:
606         case NET_EVENT_WIFI_WPS_RSP:
607                 is_requested = true;
608                 /* fall through */
609         case NET_EVENT_OPEN_IND:
610                 if (_wifi_libnet_check_profile_name_validity(event_cb->ProfileName) != true)
611                         return;
612
613                 result = __libnet_convert_to_ap_error_type(event_cb->Error);
614                 WIFI_LOG(WIFI_INFO, "Connection open error %s",
615                                 __libnet_convert_ap_error_type_to_string(result));
616
617                 if (is_requested)
618                         __libnet_connected_cb(result);
619
620                 switch (event_cb->Error) {
621                 case NET_ERR_NONE:
622                         if (event_cb->Datalength == sizeof(net_profile_info_t))
623                                 prof_info_p = (net_profile_info_t *)event_cb->Data;
624
625                         __libnet_state_changed_cb(event_cb->ProfileName, prof_info_p,
626                                                         WIFI_CONNECTION_STATE_CONNECTED);
627                         return;
628                 case NET_ERR_ACTIVE_CONNECTION_EXISTS:
629                         return;
630                 default:
631                         break;
632                 }
633
634                 if (net_get_profile_info(event_cb->ProfileName, &prof_info) == NET_ERR_NONE)
635                         __libnet_state_changed_cb(event_cb->ProfileName, &prof_info,
636                                                 WIFI_CONNECTION_STATE_DISCONNECTED);
637                 else
638                         __libnet_state_changed_cb(event_cb->ProfileName, NULL,
639                                                 WIFI_CONNECTION_STATE_DISCONNECTED);
640
641                 break;
642         case NET_EVENT_CLOSE_RSP:
643                 is_requested = true;
644                 /* fall through */
645         case NET_EVENT_CLOSE_IND:
646                 if (_wifi_libnet_check_profile_name_validity(event_cb->ProfileName) != true)
647                         return;
648
649                 result = __libnet_convert_to_ap_error_type(event_cb->Error);
650                 WIFI_LOG(WIFI_ERROR, "Connection close error %s",
651                                 __libnet_convert_ap_error_type_to_string(result));
652
653                 if (is_requested)
654                         __libnet_disconnected_cb(result);
655
656                 switch (event_cb->Error) {
657                 case NET_ERR_NONE:
658                         if (net_get_profile_info(event_cb->ProfileName, &prof_info) == NET_ERR_NONE)
659                                 __libnet_state_changed_cb(event_cb->ProfileName, &prof_info,
660                                                 WIFI_CONNECTION_STATE_DISCONNECTED);
661                         else
662                                 __libnet_state_changed_cb(event_cb->ProfileName, NULL,
663                                                 WIFI_CONNECTION_STATE_DISCONNECTED);
664                         return;
665                 default:
666                         break;
667                 }
668
669                 break;
670         case NET_EVENT_NET_STATE_IND:
671                 if (_wifi_libnet_check_profile_name_validity(event_cb->ProfileName) != true)
672                         return;
673
674                 if (event_cb->Datalength != sizeof(net_state_type_t))
675                         return;
676
677                 net_state_type_t *profile_state = (net_state_type_t *)event_cb->Data;
678                 wifi_connection_state_e ap_state = _wifi_convert_to_ap_state(*profile_state);
679
680                 WIFI_LOG(WIFI_INFO, "state: %s", __libnet_convert_ap_state_to_string(ap_state));
681                 SECURE_WIFI_LOG(WIFI_INFO, "profile name: %s", event_cb->ProfileName);
682
683                 if (net_get_profile_info(event_cb->ProfileName, &prof_info) == NET_ERR_NONE)
684                         __libnet_state_changed_cb(event_cb->ProfileName, &prof_info, ap_state);
685                 else if (ap_state == WIFI_CONNECTION_STATE_DISCONNECTED) {
686                         for (i = 0; i < profile_iterator.count; i++) {
687                                 if (!g_strcmp0(event_cb->ProfileName,
688                                                 profile_iterator.profiles[i].ProfileName)) {
689                                         is_profile_exists = true;
690                                         break;
691                                 }
692                         }
693
694                         if (is_profile_exists == true) {
695                                 profile_iterator.profiles[i].ProfileState = *profile_state;
696                                 __libnet_state_changed_cb(event_cb->ProfileName,
697                                                         &profile_iterator.profiles[i], ap_state);
698                         } else
699                                 __libnet_state_changed_cb(event_cb->ProfileName,
700                                                         NULL, ap_state);
701                 } else
702                         __libnet_state_changed_cb(event_cb->ProfileName, NULL, ap_state);
703
704                 break;
705         case NET_EVENT_WIFI_SCAN_RSP:
706         case NET_EVENT_WIFI_SCAN_IND:
707                 __libnet_scan_cb(event_cb, is_requested);
708                 break;
709         case NET_EVENT_SPECIFIC_SCAN_RSP:
710                 WIFI_LOG(WIFI_INFO, "Got Wi-Fi specific scan RSP\n");
711                 break;
712         case NET_EVENT_SPECIFIC_SCAN_IND:
713                 WIFI_LOG(WIFI_INFO, "Got Wi-Fi specific scan IND\n");
714                 __libnet_specific_scan_cb(event_cb);
715                 break;
716         case NET_EVENT_WIFI_POWER_RSP:
717                 is_requested = true;
718                 /* fall through */
719         case NET_EVENT_WIFI_POWER_IND:
720                 __libnet_power_on_off_cb(event_cb, is_requested);
721                 break;
722         default:
723                 break;
724         }
725 }
726
727 int _wifi_libnet_init(void)
728 {
729         int rv;
730
731         rv = net_register_client_ext((net_event_cb_t)__libnet_evt_cb, NET_DEVICE_WIFI, NULL);
732         if (rv != NET_ERR_NONE)
733                 return rv;
734
735         __wifi_set_init(true);
736
737         return NET_ERR_NONE;
738 }
739
740 bool _wifi_libnet_deinit(void)
741 {
742         if (net_deregister_client_ext(NET_DEVICE_WIFI) != NET_ERR_NONE)
743                 return false;
744
745         __libnet_clear_profile_list(&profile_iterator);
746         g_slist_free_full(ap_handle_list, g_free);
747         ap_handle_list = NULL;
748         memset(&wifi_callbacks, 0, sizeof(struct _wifi_cb_s));
749
750         __wifi_set_init(false);
751
752         return true;
753 }
754
755 int _wifi_activate(wifi_activated_cb callback, gboolean wifi_picker_test,
756                                         void *user_data)
757 {
758         int rv = NET_ERR_NONE;
759
760         rv = net_wifi_power_on(wifi_picker_test);
761         if (rv == NET_ERR_NONE) {
762                 __libnet_set_activated_cb(callback, user_data);
763                 return WIFI_ERROR_NONE;
764         } else if (rv == NET_ERR_ACCESS_DENIED) {
765                 WIFI_LOG(WIFI_ERROR, "Access denied");
766                 return WIFI_ERROR_PERMISSION_DENIED;
767         } else if (rv == NET_ERR_INVALID_OPERATION)
768                 return WIFI_ERROR_INVALID_OPERATION;
769         else if (rv == NET_ERR_ALREADY_EXISTS)
770                 return WIFI_ERROR_ALREADY_EXISTS;
771         else if (rv == NET_ERR_IN_PROGRESS)
772                 return WIFI_ERROR_NOW_IN_PROGRESS;
773         else if (rv == NET_ERR_SECURITY_RESTRICTED)
774                 return WIFI_ERROR_SECURITY_RESTRICTED;
775
776         return WIFI_ERROR_OPERATION_FAILED;
777 }
778
779 int _wifi_deactivate(wifi_deactivated_cb callback, void *user_data)
780 {
781         int rv = NET_ERR_NONE;
782
783         rv = net_wifi_power_off();
784         if (rv == NET_ERR_NONE) {
785                 __libnet_set_deactivated_cb(callback, user_data);
786                 return WIFI_ERROR_NONE;
787         } else if (rv == NET_ERR_ACCESS_DENIED) {
788                 WIFI_LOG(WIFI_ERROR, "Access denied");
789                 return WIFI_ERROR_PERMISSION_DENIED;
790         } else if (rv == NET_ERR_INVALID_OPERATION)
791                 return WIFI_ERROR_INVALID_OPERATION;
792         else if (rv == NET_ERR_ALREADY_EXISTS)
793                 return WIFI_ERROR_ALREADY_EXISTS;
794         else if (rv == NET_ERR_IN_PROGRESS)
795                 return WIFI_ERROR_NOW_IN_PROGRESS;
796         else if (rv == NET_ERR_SECURITY_RESTRICTED)
797                 return WIFI_ERROR_SECURITY_RESTRICTED;
798
799         return WIFI_ERROR_OPERATION_FAILED;
800 }
801
802 bool _wifi_libnet_check_ap_validity(wifi_ap_h ap_h)
803 {
804         int i;
805         GSList *list = NULL;
806
807         if (ap_h == NULL)
808                 return false;
809
810         for (list = ap_handle_list; list; list = list->next)
811                 if (ap_h == list->data) return true;
812
813         for (i = 0; i < profile_iterator.count; i++)
814                 if (ap_h == &profile_iterator.profiles[i]) return true;
815
816         for (i = 0; i < specific_profile_iterator.count; i++)
817                 if (ap_h == &specific_profile_iterator.profiles[i]) return true;
818
819         return false;
820 }
821
822 void _wifi_libnet_add_to_ap_list(wifi_ap_h ap_h)
823 {
824         ap_handle_list = g_slist_append(ap_handle_list, ap_h);
825 }
826
827 void _wifi_libnet_remove_from_ap_list(wifi_ap_h ap_h)
828 {
829         ap_handle_list = g_slist_remove(ap_handle_list, ap_h);
830         g_free(ap_h);
831 }
832
833 bool _wifi_libnet_check_profile_name_validity(const char *profile_name)
834 {
835         const char *profile_prefix = "/net/connman/service/wifi_";
836         int i = 0;
837
838         if (profile_name == NULL ||
839                         g_str_has_prefix(profile_name, profile_prefix) != TRUE) {
840                 WIFI_LOG(WIFI_INFO, "The profile is a hidden or not a regular Wi-Fi profile");
841                 return false;
842         }
843
844         while (profile_name[i] != '\0') {
845                 if (isgraph(profile_name[i]) == 0) {
846                         WIFI_LOG(WIFI_INFO, "Invalid format: %s", profile_name);
847                         return false;
848                 }
849                 i++;
850         }
851
852         return true;
853 }
854
855 int _wifi_libnet_get_wifi_device_state(wifi_device_state_e *device_state)
856 {
857         net_tech_info_t tech_info;
858
859         int rv = NET_ERR_NONE;
860         rv = net_get_technology_properties(NET_DEVICE_WIFI, &tech_info);
861         if (rv == NET_ERR_ACCESS_DENIED) {
862                 WIFI_LOG(WIFI_ERROR, "Access denied");
863                 return WIFI_ERROR_PERMISSION_DENIED;
864         } else if (rv != NET_ERR_NONE) {
865                 WIFI_LOG(WIFI_ERROR, "Failed to get technology properties");
866                 return WIFI_ERROR_OPERATION_FAILED;
867         }
868
869         if (tech_info.powered)
870                 *device_state = WIFI_DEVICE_STATE_ACTIVATED;
871         else
872                 *device_state = WIFI_DEVICE_STATE_DEACTIVATED;
873
874         return WIFI_ERROR_NONE;
875 }
876
877 int _wifi_libnet_get_wifi_state(wifi_connection_state_e *connection_state)
878 {
879         wifi_dbus *dbus_h = NULL;
880         GError *error = NULL;
881         GVariant *result = NULL;
882         gint state = 0;
883
884         dbus_h = _wifi_get_dbus_handle();
885         if (dbus_h == NULL) {
886                 WIFI_LOG(WIFI_ERROR, "Not initialized for wifi dbus connection");
887                 return WIFI_ERROR_INVALID_OPERATION;
888         }
889
890         result = g_dbus_connection_call_sync(dbus_h->dbus_conn,
891                                              NETCONFIG_SERVICE,
892                                              NETCONFIG_WIFI_PATH,
893                                              NETCONFIG_IWIFI,
894                                              "GetWifiState",
895                                              g_variant_new("()"),
896                                              NULL, G_DBUS_CALL_FLAGS_NONE,
897                                              DBUS_REPLY_TIMEOUT, dbus_h->ca,
898                                              &error);
899
900         if (error) {
901                 WIFI_LOG(WIFI_ERROR, "Fail to GetWifiState [%d: %s]", error->code, error->message);
902                 g_error_free(error);
903                 return WIFI_ERROR_OPERATION_FAILED;
904         }
905
906         if (result != NULL) {
907                 g_variant_get(result, "(i)", &state);
908                 g_variant_unref(result);
909         }
910
911         *connection_state = state;
912
913         return WIFI_ERROR_NONE;
914 }
915
916 int _wifi_libnet_get_intf_name(char** name)
917 {
918         int rv;
919
920         if (profile_iterator.count == 0) {
921                 rv = __libnet_update_profile_iterator();
922                 if (rv == NET_ERR_ACCESS_DENIED) {
923                         WIFI_LOG(WIFI_ERROR, "Access denied");
924                         return WIFI_ERROR_PERMISSION_DENIED;
925                 }
926         }
927
928         if (profile_iterator.count == 0) {
929                 WIFI_LOG(WIFI_ERROR, "There is no AP");
930                 return WIFI_ERROR_OPERATION_FAILED;
931         }
932
933         *name = g_strdup(profile_iterator.profiles->ProfileInfo.Wlan.net_info.DevName);
934         if (*name == NULL)
935                 return WIFI_ERROR_OUT_OF_MEMORY;
936
937         return WIFI_ERROR_NONE;
938 }
939
940 int _wifi_libnet_scan_request(wifi_scan_finished_cb callback, void *user_data)
941 {
942         int rv;
943         rv = net_scan_wifi();
944
945         if (rv == NET_ERR_NONE) {
946                 wifi_callbacks.scan_request_cb = callback;
947                 wifi_callbacks.scan_request_user_data = user_data;
948                 return WIFI_ERROR_NONE;
949         } else if (rv == NET_ERR_ACCESS_DENIED) {
950                 WIFI_LOG(WIFI_ERROR, "Access denied");
951                 return WIFI_ERROR_PERMISSION_DENIED;
952         } else if (rv == NET_ERR_INVALID_OPERATION)
953                 return WIFI_ERROR_INVALID_OPERATION;
954
955         return WIFI_ERROR_OPERATION_FAILED;
956 }
957
958 int _wifi_libnet_scan_specific_ap(const char *essid,
959                                                         wifi_scan_finished_cb callback, void *user_data)
960 {
961         int rv;
962         rv = net_specific_scan_wifi(essid);
963
964         if (rv == NET_ERR_NONE) {
965                 g_strlcpy(specific_profile_essid, essid, NET_WLAN_ESSID_LEN+1);
966                 __libnet_set_specific_scan_cb(callback, user_data);
967                 return WIFI_ERROR_NONE;
968         } else if (rv == NET_ERR_ACCESS_DENIED) {
969                 WIFI_LOG(WIFI_ERROR, "Access denied");
970                 return WIFI_ERROR_PERMISSION_DENIED;
971         } else if (rv == NET_ERR_INVALID_OPERATION)
972                 return WIFI_ERROR_INVALID_OPERATION;
973
974         return WIFI_ERROR_OPERATION_FAILED;
975 }
976
977 int _wifi_libnet_get_connected_profile(wifi_ap_h *ap)
978 {
979         int i, rv;
980         wifi_ap_h ap_h = NULL;
981
982         rv = __libnet_update_profile_iterator();
983         if (rv == NET_ERR_ACCESS_DENIED) {
984                 WIFI_LOG(WIFI_ERROR, "Access denied");
985                 return WIFI_ERROR_PERMISSION_DENIED;
986         }
987
988         for (i = 0; i < profile_iterator.count; i++) {
989                 if (profile_iterator.profiles[i].ProfileState == NET_STATE_TYPE_ONLINE ||
990                     profile_iterator.profiles[i].ProfileState == NET_STATE_TYPE_READY) {
991                         ap_h = (wifi_ap_h)(&profile_iterator.profiles[i]);
992                         break;
993                 }
994         }
995
996         if (ap_h == NULL) {
997                 WIFI_LOG(WIFI_ERROR, "There is no connected AP");
998                 return WIFI_ERROR_NO_CONNECTION;
999         }
1000
1001         *ap = g_try_malloc0(sizeof(net_profile_info_t));
1002         if (*ap == NULL)
1003                 return WIFI_ERROR_OUT_OF_MEMORY;
1004
1005         memcpy(*ap, ap_h, sizeof(net_profile_info_t));
1006
1007         _wifi_libnet_add_to_ap_list(*ap);
1008
1009         return WIFI_ERROR_NONE;
1010 }
1011
1012 int _wifi_libnet_foreach_found_aps(wifi_found_ap_cb callback, void *user_data)
1013 {
1014         int i, rv;
1015
1016         rv = __libnet_update_profile_iterator();
1017         if (rv == NET_ERR_ACCESS_DENIED) {
1018                 WIFI_LOG(WIFI_ERROR, "Access denied");
1019                 return WIFI_ERROR_PERMISSION_DENIED;
1020         }
1021
1022         if (profile_iterator.count == 0) {
1023                 WIFI_LOG(WIFI_WARN, "There is no AP");
1024                 return WIFI_ERROR_NONE;
1025         }
1026
1027         for (i = 0; i < profile_iterator.count; i++) {
1028                 if (profile_iterator.profiles[i].ProfileInfo.Wlan.is_hidden == TRUE)
1029                         continue;
1030
1031                 rv = callback((wifi_ap_h)(&profile_iterator.profiles[i]), user_data);
1032                 if (rv == false) break;
1033         }
1034
1035         return WIFI_ERROR_NONE;
1036 }
1037
1038 int _wifi_libnet_foreach_found_specific_aps(wifi_found_ap_cb callback, void *user_data)
1039 {
1040         int i, rv;
1041
1042         if (specific_profile_iterator.count == 0) {
1043                 WIFI_LOG(WIFI_WARN, "There is no specific APs");
1044
1045                 rv = __libnet_update_profile_iterator();
1046                 if (rv == NET_ERR_ACCESS_DENIED) {
1047                         WIFI_LOG(WIFI_ERROR, "Access denied");
1048                         return WIFI_ERROR_PERMISSION_DENIED;
1049                 }
1050
1051                 if (profile_iterator.count == 0) {
1052                         WIFI_LOG(WIFI_WARN, "There is no APs");
1053                         return WIFI_ERROR_NONE;
1054                 }
1055
1056                 for (i = 0; i < profile_iterator.count; i++) {
1057                         if (!g_strcmp0(specific_profile_essid,
1058                                                 profile_iterator.profiles[i].ProfileInfo.Wlan.essid)) {
1059                                 rv = callback((wifi_ap_h)(&profile_iterator.profiles[i]), user_data);
1060                                 if (rv == false) break;
1061                         }
1062                 }
1063                 return WIFI_ERROR_NONE;
1064         }
1065
1066         for (i = 0; i < specific_profile_iterator.count; i++) {
1067                 rv = callback((wifi_ap_h)(&specific_profile_iterator.profiles[i]), user_data);
1068                 if (rv == false) break;
1069         }
1070
1071         return WIFI_ERROR_NONE;
1072 }
1073
1074 int _wifi_libnet_open_profile(wifi_ap_h ap_h, wifi_connected_cb callback, void *user_data)
1075 {
1076         int rv;
1077         bool valid_profile;
1078         net_profile_info_t *ap_info = ap_h;
1079
1080         valid_profile =
1081                         _wifi_libnet_check_profile_name_validity(ap_info->ProfileName);
1082
1083         if (valid_profile == true && ap_info->Favourite)
1084                 rv = net_open_connection_with_profile(ap_info->ProfileName);
1085         else if (valid_profile == true &&
1086                         ap_info->ProfileInfo.Wlan.is_hidden != TRUE &&
1087                         ap_info->ProfileInfo.Wlan.security_info.sec_mode ==
1088                                                                                                 WLAN_SEC_MODE_NONE)
1089                 rv = net_open_connection_with_profile(ap_info->ProfileName);
1090         else
1091                 rv = __libnet_connect_with_wifi_info(ap_info);
1092
1093         rv = __libnet_convert_to_ap_error_type(rv);
1094         if (rv == WIFI_ERROR_NONE)
1095                 __libnet_set_connected_cb(callback, user_data);
1096
1097         return rv;
1098 }
1099
1100 int _wifi_libnet_close_profile(wifi_ap_h ap_h, wifi_disconnected_cb callback, void *user_data)
1101 {
1102         int rv;
1103         net_profile_info_t *ap_info = ap_h;
1104
1105         rv = net_close_connection(ap_info->ProfileName);
1106         if (rv == NET_ERR_ACCESS_DENIED) {
1107                 WIFI_LOG(WIFI_ERROR, "Access denied");
1108                 return WIFI_ERROR_PERMISSION_DENIED;
1109         } else if (rv != NET_ERR_NONE)
1110                 return WIFI_ERROR_OPERATION_FAILED;
1111
1112         __libnet_set_disconnected_cb(callback, user_data);
1113
1114         return WIFI_ERROR_NONE;
1115 }
1116
1117 int _wifi_libnet_connect_with_wps_pbc(wifi_ap_h ap_h, wifi_connected_cb callback, void *user_data)
1118 {
1119         int rv;
1120         net_profile_info_t *ap_info = ap_h;
1121         net_wifi_wps_info_t wps_info;
1122         memset(&wps_info, 0, sizeof(net_wifi_wps_info_t));
1123
1124         wps_info.type = WIFI_WPS_PBC;
1125
1126         rv = net_wifi_enroll_wps(ap_info->ProfileName, &wps_info);
1127         if (rv == NET_ERR_ACCESS_DENIED) {
1128                 WIFI_LOG(WIFI_ERROR, "Access denied");
1129                 return WIFI_ERROR_PERMISSION_DENIED;
1130         } else if (rv != NET_ERR_NONE)
1131                 return WIFI_ERROR_OPERATION_FAILED;
1132
1133         __libnet_set_connected_cb(callback, user_data);
1134
1135         return WIFI_ERROR_NONE;
1136 }
1137
1138 int _wifi_libnet_connect_with_wps_pin(wifi_ap_h ap_h, const char *pin,
1139                 wifi_connected_cb callback, void *user_data)
1140 {
1141         int rv;
1142         net_profile_info_t *ap_info = ap_h;
1143         net_wifi_wps_info_t wps_info;
1144
1145         if (ap_info == NULL) {
1146                 WIFI_LOG(WIFI_ERROR, "Invalid parameter");
1147                 return WIFI_ERROR_INVALID_PARAMETER;
1148         }
1149
1150         wps_info.type = WIFI_WPS_PIN;
1151         g_strlcpy(wps_info.pin, pin, NET_WLAN_MAX_WPSPIN_LEN + 1);
1152
1153         rv = net_wifi_enroll_wps(ap_info->ProfileName, &wps_info);
1154         if (rv == NET_ERR_ACCESS_DENIED) {
1155                 WIFI_LOG(WIFI_ERROR, "Access denied");
1156                 return WIFI_ERROR_PERMISSION_DENIED;
1157         } else if (rv != NET_ERR_NONE)
1158                 return WIFI_ERROR_OPERATION_FAILED;
1159
1160         __libnet_set_connected_cb(callback, user_data);
1161
1162         return WIFI_ERROR_NONE;
1163 }
1164
1165 int _wifi_libnet_forget_ap(wifi_ap_h ap)
1166 {
1167         int rv = 0;
1168         net_profile_info_t *ap_info = ap;
1169
1170         if (ap_info == NULL) {
1171                 WIFI_LOG(WIFI_ERROR, "Invalid parameter");
1172                 return WIFI_ERROR_INVALID_PARAMETER;
1173         }
1174
1175         rv = net_delete_profile(ap_info->ProfileName);
1176         if (rv == NET_ERR_ACCESS_DENIED) {
1177                 WIFI_LOG(WIFI_ERROR, "Access denied");
1178                 return WIFI_ERROR_PERMISSION_DENIED;
1179         } else if (rv != NET_ERR_NONE)
1180                 return WIFI_ERROR_OPERATION_FAILED;
1181
1182         ap_info->Favourite = (char)FALSE;
1183
1184         return WIFI_ERROR_NONE;
1185 }
1186
1187 int _wifi_set_power_on_off_cb(wifi_device_state_changed_cb callback, void *user_data)
1188 {
1189         if (wifi_callbacks.device_state_cb != NULL)
1190                 return WIFI_ERROR_INVALID_OPERATION;
1191
1192         wifi_callbacks.device_state_cb = callback;
1193         wifi_callbacks.device_state_user_data = user_data;
1194
1195         WIFI_LOG(WIFI_INFO, "Wi-Fi registered device state changed callback");
1196
1197         return WIFI_ERROR_NONE;
1198 }
1199
1200 int _wifi_unset_power_on_off_cb(void)
1201 {
1202         if (wifi_callbacks.device_state_cb == NULL)
1203                 return WIFI_ERROR_INVALID_OPERATION;
1204
1205         wifi_callbacks.device_state_cb = NULL;
1206         wifi_callbacks.device_state_user_data = NULL;
1207
1208         return WIFI_ERROR_NONE;
1209 }
1210
1211 int _wifi_set_background_scan_cb(wifi_scan_finished_cb callback, void *user_data)
1212 {
1213         if (wifi_callbacks.bg_scan_cb != NULL)
1214                 return WIFI_ERROR_INVALID_OPERATION;
1215
1216         wifi_callbacks.bg_scan_cb = callback;
1217         wifi_callbacks.bg_scan_user_data = user_data;
1218
1219         return WIFI_ERROR_NONE;
1220 }
1221
1222 int _wifi_unset_background_scan_cb(void)
1223 {
1224         if (wifi_callbacks.bg_scan_cb == NULL)
1225                 return WIFI_ERROR_INVALID_OPERATION;
1226
1227         wifi_callbacks.bg_scan_cb = NULL;
1228         wifi_callbacks.bg_scan_user_data = NULL;
1229
1230         return WIFI_ERROR_NONE;
1231 }
1232
1233 int _wifi_set_connection_state_cb(wifi_connection_state_changed_cb callback, void *user_data)
1234 {
1235         if (wifi_callbacks.connection_state_cb != NULL)
1236                 return WIFI_ERROR_INVALID_OPERATION;
1237
1238         wifi_callbacks.connection_state_cb = callback;
1239         wifi_callbacks.connection_state_user_data = user_data;
1240
1241         return WIFI_ERROR_NONE;
1242 }
1243
1244 int _wifi_unset_connection_state_cb()
1245 {
1246         if (wifi_callbacks.connection_state_cb == NULL)
1247                 return WIFI_ERROR_INVALID_OPERATION;
1248
1249         wifi_callbacks.connection_state_cb = NULL;
1250         wifi_callbacks.connection_state_user_data = NULL;
1251
1252         return WIFI_ERROR_NONE;
1253 }
1254
1255 int _wifi_update_ap_info(net_profile_info_t *ap_info)
1256 {
1257         int rv = NET_ERR_NONE;
1258         rv = net_modify_profile(ap_info->ProfileName, ap_info);
1259
1260         if (rv == NET_ERR_ACCESS_DENIED) {
1261                 WIFI_LOG(WIFI_ERROR, "Access denied");
1262                 return WIFI_ERROR_PERMISSION_DENIED;
1263         } else if (rv != NET_ERR_NONE)
1264                 return WIFI_ERROR_OPERATION_FAILED;
1265
1266         return WIFI_ERROR_NONE;
1267 }
1268
1269 static void __wifi_idle_destroy_cb(gpointer data)
1270 {
1271         if (!data)
1272                 return;
1273
1274         managed_idler_list = g_slist_remove(managed_idler_list, data);
1275         g_free(data);
1276 }
1277
1278 static gboolean __wifi_idle_cb(gpointer user_data)
1279 {
1280         struct managed_idle_data *data = (struct managed_idle_data *)user_data;
1281
1282         if (!data)
1283                 return FALSE;
1284
1285         return data->func(data->user_data);
1286 }
1287
1288 guint _wifi_callback_add(GSourceFunc func, gpointer user_data)
1289 {
1290         guint id;
1291         struct managed_idle_data *data;
1292         GMainContext *context;
1293         GSource *src;
1294
1295         if (!func)
1296                 return 0;
1297
1298         data = g_try_new0(struct managed_idle_data, 1);
1299         if (!data)
1300                 return 0;
1301
1302         data->func = func;
1303         data->user_data = user_data;
1304
1305         context = g_main_context_get_thread_default();
1306         src = g_idle_source_new();
1307         g_source_set_callback(src, __wifi_idle_cb, data, __wifi_idle_destroy_cb);
1308         id = g_source_attach(src, context);
1309         g_source_unref(src);
1310
1311         if (!id) {
1312                 g_free(data);
1313                 return id;
1314         }
1315
1316         data->id = id;
1317
1318         managed_idler_list = g_slist_append(managed_idler_list, data);
1319
1320         return id;
1321 }
1322
1323 void _wifi_callback_cleanup(void)
1324 {
1325         GSList *cur = managed_idler_list;
1326         GSource *src;
1327         struct managed_idle_data *data;
1328
1329         while (cur) {
1330                 GSList *next = cur->next;
1331                 data = (struct managed_idle_data *)cur->data;
1332
1333                 src = g_main_context_find_source_by_id(g_main_context_default(), data->id);
1334                 if (src) {
1335                         g_source_destroy(src);
1336                         cur = managed_idler_list;
1337                 } else
1338                         cur = next;
1339         }
1340
1341         g_slist_free(managed_idler_list);
1342         managed_idler_list = NULL;
1343 }
1344
1345 bool __libnet_check_feature_supported(const char *key, wifi_supported_feature_e feature)
1346 {
1347         if (!wifi_is_feature_checked[feature]) {
1348                 if (system_info_get_platform_bool(key, &wifi_feature_supported[feature]) < 0) {
1349                         WIFI_LOG(WIFI_ERROR, "Error - Feature getting from System Info");
1350                         set_last_result(WIFI_ERROR_OPERATION_FAILED);
1351                         return WIFI_ERROR_OPERATION_FAILED;
1352                 }
1353                 wifi_is_feature_checked[feature] = true;
1354         }
1355         return wifi_feature_supported[feature];
1356 }
1357
1358 int _wifi_check_feature_supported(const char *feature_name, ...)
1359 {
1360         va_list list;
1361         const char *key;
1362         bool value = false;
1363         bool feature_supported = false;
1364
1365         va_start(list, feature_name);
1366         key = feature_name;
1367         while (1) {
1368                 if (strcmp(key, WIFI_FEATURE) == 0)
1369                         value = __libnet_check_feature_supported(key, WIFI_SUPPORTED_FEATURE_WIFI);
1370                 else if (strcmp(key, WIFI_TDLS_FEATURE) == 0)
1371                         value = __libnet_check_feature_supported(key, WIFI_SUPPORTED_FEATURE_WIFI_TDLS);
1372
1373                 SECURE_WIFI_LOG(WIFI_INFO, "%s feature is %s", key, (value ? "true" : "false"));
1374                 feature_supported |= value;
1375                 key = va_arg(list, const char *);
1376                 if (!key) break;
1377         }
1378         if (!feature_supported) {
1379                 WIFI_LOG(WIFI_ERROR, "Error - Feature is not supported");
1380                 set_last_result(WIFI_ERROR_NOT_SUPPORTED);
1381                 va_end(list);
1382                 return WIFI_ERROR_NOT_SUPPORTED;
1383         }
1384
1385         va_end(list);
1386         set_last_result(WIFI_ERROR_NONE);
1387         return WIFI_ERROR_NONE;
1388 }
1389
1390 int _wifi_dbus_init(void)
1391 {
1392         int rv;
1393
1394         rv = wifi_dbus_init(&g_dbus_h);
1395         if (rv != NET_ERR_NONE)
1396                 return rv;
1397
1398         return NET_ERR_NONE;
1399 }
1400
1401 int _wifi_dbus_deinit(void)
1402 {
1403         wifi_dbus_deinit(g_dbus_h);
1404         g_dbus_h = NULL;
1405
1406         return NET_ERR_NONE;
1407 }
1408
1409 wifi_dbus *_wifi_get_dbus_handle(void)
1410 {
1411         if (g_dbus_h == NULL) {
1412                 WIFI_LOG(WIFI_ERROR, "g_dbus_h is NULL");
1413                 return NULL;
1414         }
1415
1416         return g_dbus_h;
1417 }