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