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