Tizen 2.1 base
[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 <stdio.h>
18 #include <string.h>
19 #include <ctype.h>
20 #include <glib.h>
21 #include "net_wifi_private.h"
22
23 static GSList *ap_handle_list = NULL;
24
25 struct _wifi_cb_s {
26         wifi_device_state_changed_cb device_state_cb;
27         void *device_state_user_data;
28         wifi_scan_finished_cb bg_scan_cb;
29         void *bg_scan_user_data;
30         wifi_scan_finished_cb scan_request_cb;
31         void *scan_request_user_data;
32         wifi_connection_state_changed_cb connection_state_cb;
33         void *connection_state_user_data;
34         wifi_activated_cb activated_cb;
35         void *activated_user_data;
36         wifi_deactivated_cb deactivated_cb;
37         void *deactivated_user_data;
38         wifi_connected_cb connected_cb;
39         void *connected_user_data;
40         wifi_disconnected_cb disconnected_cb;
41         void *disconnected_user_data;
42 };
43
44 struct _profile_list_s {
45         int count;
46         net_profile_info_t *profiles;
47 };
48
49 static struct _wifi_cb_s wifi_callbacks = {0,};
50 static struct _profile_list_s profile_iterator = {0, NULL};
51
52
53 static wifi_error_e __libnet_convert_to_ap_error_type(net_err_t err_type)
54 {
55         switch (err_type) {
56         case NET_ERR_NONE:
57                 return WIFI_ERROR_NONE;
58         case NET_ERR_APP_ALREADY_REGISTERED:
59                 return WIFI_ERROR_INVALID_OPERATION;
60         case NET_ERR_APP_NOT_REGISTERED:
61                 return WIFI_ERROR_INVALID_OPERATION;
62         case NET_ERR_NO_ACTIVE_CONNECTIONS:
63                 return WIFI_ERROR_NO_CONNECTION;
64         case NET_ERR_ACTIVE_CONNECTION_EXISTS:
65                 return WIFI_ERROR_ALREADY_EXISTS;
66         case NET_ERR_CONNECTION_DHCP_FAILED:
67                 return WIFI_ERROR_DHCP_FAILED;
68         case NET_ERR_CONNECTION_INVALID_KEY:
69                 return WIFI_ERROR_INVALID_KEY;
70         case NET_ERR_IN_PROGRESS:
71                 return WIFI_ERROR_NOW_IN_PROGRESS;
72         case NET_ERR_OPERATION_ABORTED:
73                 return WIFI_ERROR_OPERATION_ABORTED;
74         case NET_ERR_TIME_OUT:
75                 return WIFI_ERROR_NO_REPLY;
76         default:
77                 return WIFI_ERROR_OPERATION_FAILED;
78         }
79 }
80
81 static const char *__libnet_convert_ap_error_type_to_string(wifi_error_e err_type)
82 {
83         switch (err_type) {
84         case WIFI_ERROR_NONE:
85                 return "NONE";
86         case WIFI_ERROR_INVALID_PARAMETER:
87                 return "INVALID_PARAMETER";
88         case WIFI_ERROR_OUT_OF_MEMORY:
89                 return "OUT_OF_MEMORY";
90         case WIFI_ERROR_INVALID_OPERATION:
91                 return "INVALID_OPERATION";
92         case WIFI_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED:
93                 return "ADDRESS_FAMILY_NOT_SUPPORTED";
94         case WIFI_ERROR_OPERATION_FAILED:
95                 return "OPERATION_FAILED";
96         case WIFI_ERROR_NO_CONNECTION:
97                 return "NO_CONNECTION";
98         case WIFI_ERROR_NOW_IN_PROGRESS:
99                 return "NOW_IN_PROGRESS";
100         case WIFI_ERROR_ALREADY_EXISTS:
101                 return "ALREADY_EXISTS";
102         case WIFI_ERROR_OPERATION_ABORTED:
103                 return "OPERATION_ABORTED";
104         case WIFI_ERROR_DHCP_FAILED:
105                 return "DHCP_FAILED";
106         case WIFI_ERROR_INVALID_KEY:
107                 return "INVALID_KEY";
108         case WIFI_ERROR_NO_REPLY:
109                 return "NO_REPLY";
110         case WIFI_ERROR_SECURITY_RESTRICTED:
111                 return "SECURITY_RESTRICTED";
112         }
113
114         return "UNKNOWN";
115 }
116
117 static const char *__libnet_convert_ap_state_to_string(wifi_connection_state_e state)
118 {
119         switch (state) {
120         case WIFI_CONNECTION_STATE_DISCONNECTED:
121                 return "DISCONNECTED";
122         case WIFI_CONNECTION_STATE_ASSOCIATION:
123                 return "ASSOCIATION";
124         case WIFI_CONNECTION_STATE_CONFIGURATION:
125                 return "CONFIGURATION";
126         case WIFI_CONNECTION_STATE_CONNECTED:
127                 return "CONNECTED";
128         default:
129                 return "UNKNOWN";
130         }
131 }
132
133 static void __libnet_clear_profile_list(struct _profile_list_s *profile_list)
134 {
135         if (profile_list->count > 0)
136                 g_free(profile_list->profiles);
137
138         profile_list->count = 0;
139         profile_list->profiles = NULL;
140 }
141
142 static void __libnet_update_profile_iterator(void)
143 {
144         struct _profile_list_s wifi_profiles = {0, NULL};
145
146         __libnet_clear_profile_list(&profile_iterator);
147
148         net_get_profile_list(NET_DEVICE_WIFI, &wifi_profiles.profiles, &wifi_profiles.count);
149         WIFI_LOG(WIFI_INFO, "Wifi profile count : %d\n", wifi_profiles.count);
150
151         if (wifi_profiles.count == 0)
152                 return;
153
154         profile_iterator.count = wifi_profiles.count;
155         profile_iterator.profiles = wifi_profiles.profiles;
156 }
157
158 static void __libnet_convert_profile_info_to_wifi_info(net_wifi_connection_info_t *wifi_info,
159                                                                 net_profile_info_t *ap_info)
160 {
161         g_strlcpy(wifi_info->essid, ap_info->ProfileInfo.Wlan.essid, NET_WLAN_ESSID_LEN+1);
162         wifi_info->wlan_mode = ap_info->ProfileInfo.Wlan.wlan_mode;
163         memcpy(&wifi_info->security_info, &ap_info->ProfileInfo.Wlan.security_info, sizeof(wlan_security_info_t));
164 }
165
166 static int __libnet_connect_with_wifi_info(net_profile_info_t *ap_info)
167 {
168         net_wifi_connection_info_t wifi_info;
169         memset(&wifi_info, 0, sizeof(net_wifi_connection_info_t));
170
171         __libnet_convert_profile_info_to_wifi_info(&wifi_info, ap_info);
172
173         if (net_open_connection_with_wifi_info(&wifi_info) != NET_ERR_NONE)
174                 return WIFI_ERROR_OPERATION_FAILED;
175
176         return WIFI_ERROR_NONE;
177 }
178
179 static void __libnet_state_changed_cb(char *profile_name, net_profile_info_t *profile_info,
180                                                         wifi_connection_state_e state)
181 {
182         if (profile_name == NULL)
183                 return;
184
185         if (profile_info == NULL) {
186                 WIFI_LOG(WIFI_ERROR, "Error!! Profile info not found! : %s\n", profile_name);
187                 return;
188         }
189
190         ap_handle_list = g_slist_append(ap_handle_list, (wifi_ap_h)profile_info);
191
192         if (wifi_callbacks.connection_state_cb)
193                 wifi_callbacks.connection_state_cb(state, (wifi_ap_h)profile_info,
194                                         wifi_callbacks.connection_state_user_data);
195
196         ap_handle_list = g_slist_remove(ap_handle_list, (wifi_ap_h)profile_info);
197 }
198
199 static void __libnet_set_activated_cb(wifi_activated_cb user_cb, void *user_data)
200 {
201         if (user_cb) {
202                 wifi_callbacks.activated_cb = user_cb;
203                 wifi_callbacks.activated_user_data = user_data;
204         }
205 }
206
207 static void __libnet_activated_cb(wifi_error_e result)
208 {
209         if (wifi_callbacks.activated_cb)
210                 wifi_callbacks.activated_cb(result, wifi_callbacks.activated_user_data);
211
212         wifi_callbacks.activated_cb = NULL;
213         wifi_callbacks.activated_user_data = NULL;
214 }
215
216 static void __libnet_set_deactivated_cb(wifi_disconnected_cb user_cb, void *user_data)
217 {
218         if (user_cb) {
219                 wifi_callbacks.deactivated_cb = user_cb;
220                 wifi_callbacks.deactivated_user_data = user_data;
221         }
222 }
223
224 static void __libnet_deactivated_cb(wifi_error_e result)
225 {
226         if (wifi_callbacks.deactivated_cb)
227                 wifi_callbacks.deactivated_cb(result, wifi_callbacks.deactivated_user_data);
228
229         wifi_callbacks.deactivated_cb = NULL;
230         wifi_callbacks.deactivated_user_data = NULL;
231 }
232
233 static void __libnet_power_on_off_cb(net_event_info_t *event_cb, bool is_requested)
234 {
235         if (wifi_callbacks.device_state_cb == NULL &&
236             wifi_callbacks.activated_cb == NULL &&
237             wifi_callbacks.deactivated_cb == NULL)
238                 return;
239
240         wifi_error_e error_code = WIFI_ERROR_NONE;
241         wifi_device_state_e state;
242         net_wifi_state_t *wifi_state = (net_wifi_state_t*)event_cb->Data;
243
244         if (event_cb->Error == NET_ERR_NONE &&
245             event_cb->Datalength == sizeof(net_wifi_state_t)) {
246
247                 if (*wifi_state == WIFI_ON) {
248                         WIFI_LOG(WIFI_INFO, "Wi-Fi State : Power ON\n");
249                         state = WIFI_DEVICE_STATE_ACTIVATED;
250                 } else if (*wifi_state == WIFI_OFF) {
251                         WIFI_LOG(WIFI_INFO, "Wi-Fi State : Power OFF\n");
252                         state = WIFI_DEVICE_STATE_DEACTIVATED;
253                         __libnet_clear_profile_list(&profile_iterator);
254                 } else {
255                         WIFI_LOG(WIFI_INFO, "Wi-Fi State : Unknown\n");
256                         error_code = WIFI_ERROR_OPERATION_FAILED;
257                         state = WIFI_DEVICE_STATE_DEACTIVATED;
258                 }
259         } else {
260                 WIFI_LOG(WIFI_ERROR, "Wi-Fi Power on/off request failed! Error [%d]\n", event_cb->Error);
261                 error_code = WIFI_ERROR_OPERATION_FAILED;
262                 state = WIFI_DEVICE_STATE_DEACTIVATED;
263         }
264
265         __libnet_activated_cb(error_code);
266         __libnet_deactivated_cb(error_code);
267
268         if (wifi_callbacks.device_state_cb)
269                 wifi_callbacks.device_state_cb(state, wifi_callbacks.device_state_user_data);
270 }
271
272 static void __libnet_scan_cb(net_event_info_t *event_cb, bool is_requested)
273 {
274         wifi_error_e error_code = WIFI_ERROR_NONE;
275
276         if (event_cb->Error != NET_ERR_NONE) {
277                 WIFI_LOG(WIFI_ERROR, "Scan failed!, Error [%d]\n", event_cb->Error);
278                 error_code = WIFI_ERROR_OPERATION_FAILED;
279         }
280
281         if (wifi_callbacks.scan_request_cb) {
282                 wifi_callbacks.scan_request_cb(error_code, wifi_callbacks.scan_request_user_data);
283                 wifi_callbacks.scan_request_cb = NULL;
284                 wifi_callbacks.scan_request_user_data = NULL;
285                 return;
286         }
287
288         if (wifi_callbacks.bg_scan_cb != NULL)
289                 wifi_callbacks.bg_scan_cb(error_code, wifi_callbacks.bg_scan_user_data);
290 }
291
292 static void __libnet_set_connected_cb(wifi_connected_cb user_cb, void *user_data)
293 {
294         if (user_cb) {
295                 wifi_callbacks.connected_cb = user_cb;
296                 wifi_callbacks.connected_user_data = user_data;
297         }
298 }
299
300 static void __libnet_connected_cb(wifi_error_e result)
301 {
302         if (wifi_callbacks.connected_cb)
303                 wifi_callbacks.connected_cb(result, wifi_callbacks.connected_user_data);
304
305         wifi_callbacks.connected_cb = NULL;
306         wifi_callbacks.connected_user_data = NULL;
307 }
308
309 static void __libnet_set_disconnected_cb(wifi_disconnected_cb user_cb, void *user_data)
310 {
311         if (user_cb) {
312                 wifi_callbacks.disconnected_cb = user_cb;
313                 wifi_callbacks.disconnected_user_data = user_data;
314         }
315 }
316
317 static void __libnet_disconnected_cb(wifi_error_e result)
318 {
319         if (wifi_callbacks.disconnected_cb)
320                 wifi_callbacks.disconnected_cb(result, wifi_callbacks.disconnected_user_data);
321
322         wifi_callbacks.disconnected_cb = NULL;
323         wifi_callbacks.disconnected_user_data = NULL;
324 }
325
326 static void __libnet_evt_cb(net_event_info_t *event_cb, void *user_data)
327 {
328         bool is_requested = false;
329         net_profile_info_t *prof_info_p = NULL;
330         net_profile_info_t prof_info;
331         wifi_error_e result = WIFI_ERROR_NONE;
332
333         switch (event_cb->Event) {
334         case NET_EVENT_OPEN_RSP:
335         case NET_EVENT_WIFI_WPS_RSP:
336                 is_requested = true;
337                 /* fall through */
338         case NET_EVENT_OPEN_IND:
339                 if (strstr(event_cb->ProfileName, "/wifi_") == NULL) return;
340
341                 result = __libnet_convert_to_ap_error_type(event_cb->Error);
342                 WIFI_LOG(WIFI_INFO, "Got Open RSP/IND : %s\n",
343                         __libnet_convert_ap_error_type_to_string(result));
344
345                 if (is_requested)
346                         __libnet_connected_cb(result);
347
348                 switch (event_cb->Error) {
349                 case NET_ERR_NONE:
350                         WIFI_LOG(WIFI_INFO, "Connection open succeeded\n");
351
352                         if (event_cb->Datalength == sizeof(net_profile_info_t))
353                                 prof_info_p = (net_profile_info_t*)event_cb->Data;
354
355                         __libnet_state_changed_cb(event_cb->ProfileName, prof_info_p,
356                                                         WIFI_CONNECTION_STATE_CONNECTED);
357                         return;
358                 default :
359                         WIFI_LOG(WIFI_ERROR, "Connection open failed!\n");
360                         break;
361                 }
362
363                 if (net_get_profile_info(event_cb->ProfileName, &prof_info) == NET_ERR_NONE)
364                         __libnet_state_changed_cb(event_cb->ProfileName, &prof_info,
365                                                 WIFI_CONNECTION_STATE_DISCONNECTED);
366                 else
367                         __libnet_state_changed_cb(event_cb->ProfileName, NULL,
368                                                 WIFI_CONNECTION_STATE_DISCONNECTED);
369
370                 break;
371         case NET_EVENT_CLOSE_RSP:
372                 is_requested = true;
373                 /* fall through */
374         case NET_EVENT_CLOSE_IND:
375                 if (strstr(event_cb->ProfileName, "/wifi_") == NULL) return;
376
377                 result = __libnet_convert_to_ap_error_type(event_cb->Error);
378                 WIFI_LOG(WIFI_INFO, "Got Close RSP/IND : %s\n",
379                         __libnet_convert_ap_error_type_to_string(result));
380
381                 if (is_requested)
382                         __libnet_disconnected_cb(result);
383
384                 switch (event_cb->Error) {
385                 case NET_ERR_NONE:
386                         /* Successful PDP Deactivation */
387                         WIFI_LOG(WIFI_INFO, "Connection close succeeded!\n");
388                         if (net_get_profile_info(event_cb->ProfileName, &prof_info) == NET_ERR_NONE)
389                                 __libnet_state_changed_cb(event_cb->ProfileName, &prof_info,
390                                                         WIFI_CONNECTION_STATE_DISCONNECTED);
391                         else
392                                 __libnet_state_changed_cb(event_cb->ProfileName, NULL,
393                                                         WIFI_CONNECTION_STATE_DISCONNECTED);
394                         return;
395                 default:
396                         WIFI_LOG(WIFI_ERROR, "Connection close failed!\n");
397                         break;
398                 }
399
400                 break;
401         case NET_EVENT_NET_STATE_IND:
402                 if (strstr(event_cb->ProfileName, "/wifi_") == NULL) return;
403
404                 WIFI_LOG(WIFI_INFO, "Got State changed IND\n");
405
406                 if (event_cb->Datalength != sizeof(net_state_type_t))
407                         return;
408
409                 net_state_type_t *profile_state = (net_state_type_t*)event_cb->Data;
410                 wifi_connection_state_e ap_state = _wifi_convert_to_ap_state(*profile_state);
411
412                 WIFI_LOG(WIFI_INFO,
413                         "Profile State : %s, profile name : %s\n",
414                         __libnet_convert_ap_state_to_string(ap_state),
415                         event_cb->ProfileName);
416
417                 if (net_get_profile_info(event_cb->ProfileName, &prof_info) == NET_ERR_NONE)
418                         __libnet_state_changed_cb(event_cb->ProfileName, &prof_info, ap_state);
419                 else
420                         __libnet_state_changed_cb(event_cb->ProfileName, NULL, ap_state);
421
422
423                 break;
424         case NET_EVENT_WIFI_SCAN_RSP:
425         case NET_EVENT_WIFI_SCAN_IND:
426                 WIFI_LOG(WIFI_ERROR, "Got wifi scan IND\n");
427                 __libnet_scan_cb(event_cb, is_requested);
428                 break;
429         case NET_EVENT_WIFI_POWER_RSP:
430                 is_requested = true;
431                 /* fall through */
432         case NET_EVENT_WIFI_POWER_IND:
433                 WIFI_LOG(WIFI_ERROR, "Got wifi power IND\n");
434                 __libnet_power_on_off_cb(event_cb, is_requested);
435                 break;
436         default :
437                 WIFI_LOG(WIFI_ERROR, "Error! Unknown Event\n\n");
438         }
439 }
440
441 bool _wifi_libnet_init(void)
442 {
443         int rv;
444
445         rv = net_register_client_ext((net_event_cb_t)__libnet_evt_cb, NET_DEVICE_WIFI, NULL);
446         if (rv != NET_ERR_NONE)
447                 return false;
448
449         return true;
450 }
451
452 bool _wifi_libnet_deinit(void)
453 {
454         if (net_deregister_client_ext(NET_DEVICE_WIFI) != NET_ERR_NONE)
455                 return false;
456
457         __libnet_clear_profile_list(&profile_iterator);
458         g_slist_free_full(ap_handle_list, g_free);
459         ap_handle_list = NULL;
460         memset(&wifi_callbacks, 0, sizeof(struct _wifi_cb_s));
461
462         return true;
463 }
464
465 int _wifi_activate(wifi_activated_cb callback, void* user_data)
466 {
467         int rv;
468
469         rv = net_wifi_power_on();
470         if (rv == NET_ERR_NONE) {
471                 __libnet_set_activated_cb(callback, user_data);
472                 return WIFI_ERROR_NONE;
473         } else if (rv == NET_ERR_INVALID_OPERATION)
474                 return WIFI_ERROR_INVALID_OPERATION;
475
476         return WIFI_ERROR_OPERATION_FAILED;
477 }
478
479 int _wifi_deactivate(wifi_deactivated_cb callback, void* user_data)
480 {
481         int rv;
482
483         rv = net_wifi_power_off();
484         if (rv == NET_ERR_NONE) {
485                 __libnet_set_deactivated_cb(callback, user_data);
486                 return WIFI_ERROR_NONE;
487         } else if (rv == NET_ERR_INVALID_OPERATION)
488                 return WIFI_ERROR_INVALID_OPERATION;
489
490         return WIFI_ERROR_OPERATION_FAILED;
491 }
492
493 bool _wifi_libnet_check_ap_validity(wifi_ap_h ap_h)
494 {
495         GSList *list;
496         int i = 0;
497
498         for (list = ap_handle_list; list; list = list->next)
499                 if (ap_h == list->data) return true;
500
501         for (;i < profile_iterator.count;i++)
502                 if (ap_h == &profile_iterator.profiles[i]) return true;
503
504         return false;
505 }
506
507 void _wifi_libnet_add_to_ap_list(wifi_ap_h ap_h)
508 {
509         ap_handle_list = g_slist_append(ap_handle_list, ap_h);
510 }
511
512 void _wifi_libnet_remove_from_ap_list(wifi_ap_h ap_h)
513 {
514         ap_handle_list = g_slist_remove(ap_handle_list, ap_h);
515         g_free(ap_h);
516 }
517
518 bool _wifi_libnet_check_profile_name_validity(const char *profile_name)
519 {
520         const char *profile_header = "/net/connman/service/wifi_";
521         int i = 0;
522         int string_len = 0;
523
524         if (profile_name == NULL || strlen(profile_name) <= strlen(profile_header)) {
525                 WIFI_LOG(WIFI_ERROR, "Error!!! Profile name is invalid\n");
526                 return false;
527         }
528
529         string_len = strlen(profile_name);
530
531         if (strncmp(profile_header, profile_name, strlen(profile_header)) == 0) {
532                 for (;i < string_len;i++) {
533                         if (isgraph(profile_name[i]) == 0) {
534                                 WIFI_LOG(WIFI_ERROR, "Error!!! Profile name is invalid\n");
535                                 return false;
536                         }
537                 }
538         } else {
539                 WIFI_LOG(WIFI_ERROR, "Error!!! Profile name is invalid\n");
540                 return false;
541         }
542
543         return true;
544 }
545
546 bool _wifi_libnet_get_wifi_device_state(wifi_device_state_e *device_state)
547 {
548         net_wifi_state_t wlan_state;
549         net_profile_name_t profile_name;
550
551         if (net_get_wifi_state(&wlan_state, &profile_name) != NET_ERR_NONE) {
552                 WIFI_LOG(WIFI_ERROR, "Error!! net_get_wifi_state() failed.\n");
553                 return false;
554         }
555
556         switch (wlan_state) {
557         case WIFI_OFF:
558                 *device_state = WIFI_DEVICE_STATE_DEACTIVATED;
559                 break;
560         case WIFI_ON:
561         case WIFI_CONNECTING:
562         case WIFI_CONNECTED:
563         case WIFI_DISCONNECTING:
564                 *device_state = WIFI_DEVICE_STATE_ACTIVATED;
565                 break;
566         default :
567                 WIFI_LOG(WIFI_ERROR, "Error!! Unknown state\n");
568                 return false;
569         }
570
571         return true;
572 }
573
574 bool _wifi_libnet_get_wifi_state(wifi_connection_state_e* connection_state)
575 {
576         net_wifi_state_t wlan_state = 0;
577         net_profile_name_t profile_name;
578
579         if (net_get_wifi_state(&wlan_state, &profile_name) != NET_ERR_NONE) {
580                 WIFI_LOG(WIFI_ERROR, "Error!! net_get_wifi_state() failed.\n");
581                 return false;
582         }
583
584         switch (wlan_state) {
585         case WIFI_OFF:
586         case WIFI_ON:
587                 *connection_state = WIFI_CONNECTION_STATE_DISCONNECTED;
588                 break;
589         case WIFI_CONNECTING:
590                 *connection_state = WIFI_CONNECTION_STATE_ASSOCIATION;
591                 break;
592         case WIFI_CONNECTED:
593                 *connection_state = WIFI_CONNECTION_STATE_CONNECTED;
594                 break;
595         case WIFI_DISCONNECTING:
596                 *connection_state = WIFI_CONNECTION_STATE_CONNECTED;
597                 break;
598         default :
599                 WIFI_LOG(WIFI_ERROR, "Error!! Unknown state\n");
600                 return false;
601         }
602
603         return true;
604 }
605
606 int _wifi_libnet_get_intf_name(char** name)
607 {
608         if (profile_iterator.count == 0)
609                 __libnet_update_profile_iterator();
610
611         if (profile_iterator.count == 0) {
612                 WIFI_LOG(WIFI_ERROR, "Error!! There is no AP\n");
613                 return WIFI_ERROR_OPERATION_FAILED;
614         }
615
616         *name = g_strdup(profile_iterator.profiles->ProfileInfo.Wlan.net_info.DevName);
617         if (*name == NULL)
618                 return WIFI_ERROR_OUT_OF_MEMORY;
619
620         return WIFI_ERROR_NONE;
621 }
622
623 int _wifi_libnet_scan_request(wifi_scan_finished_cb callback, void* user_data)
624 {
625         int rv;
626         rv = net_scan_wifi();
627
628         if (rv == NET_ERR_NONE) {
629                 wifi_callbacks.scan_request_cb = callback;
630                 wifi_callbacks.scan_request_user_data = user_data;
631                 return WIFI_ERROR_NONE;
632         } else if (rv == NET_ERR_INVALID_OPERATION)
633                 return WIFI_ERROR_INVALID_OPERATION;
634
635         return WIFI_ERROR_OPERATION_FAILED;
636 }
637
638 int _wifi_libnet_get_connected_profile(wifi_ap_h *ap)
639 {
640         int i = 0;
641         wifi_ap_h ap_h = NULL;
642
643         __libnet_update_profile_iterator();
644
645         for (;i < profile_iterator.count;i++) {
646                 if (profile_iterator.profiles[i].ProfileState == NET_STATE_TYPE_ONLINE ||
647                     profile_iterator.profiles[i].ProfileState == NET_STATE_TYPE_READY) {
648                         ap_h = (wifi_ap_h)(&profile_iterator.profiles[i]);
649                         break;
650                 }
651         }
652
653         if (ap_h == NULL) {
654                 WIFI_LOG(WIFI_ERROR, "Error!! There is no connected AP.\n");
655                 return WIFI_ERROR_NO_CONNECTION;
656         }
657
658         *ap = g_try_malloc0(sizeof(net_profile_info_t));
659         if (*ap == NULL)
660                 return WIFI_ERROR_OUT_OF_MEMORY;
661
662         memcpy(*ap, ap_h, sizeof(net_profile_info_t));
663
664         _wifi_libnet_add_to_ap_list(*ap);
665
666         return WIFI_ERROR_NONE;
667 }
668
669 bool _wifi_libnet_foreach_found_aps(wifi_found_ap_cb callback, void *user_data)
670 {
671         int i = 0;
672         bool rv = true;
673
674         __libnet_update_profile_iterator();
675
676         if (profile_iterator.count == 0) {
677                 WIFI_LOG(WIFI_INFO, "There is no APs.\n");
678                 return true;
679         }
680
681         for (;i < profile_iterator.count;i++) {
682                 rv = callback((wifi_ap_h)(&profile_iterator.profiles[i]), user_data);
683                 if (rv == false) break;
684         }
685
686         return true;
687 }
688
689 int _wifi_libnet_open_profile(wifi_ap_h ap_h, wifi_connected_cb callback, void* user_data)
690 {
691         net_profile_info_t *ap_info = ap_h;
692         net_profile_name_t profile_name;
693         int rv;
694
695         g_strlcpy(profile_name.ProfileName, ap_info->ProfileName, NET_PROFILE_NAME_LEN_MAX+1);
696
697         if (ap_info->ProfileInfo.Wlan.security_info.sec_mode == WLAN_SEC_MODE_IEEE8021X)
698                 rv = __libnet_connect_with_wifi_info(ap_info);
699         else if (_wifi_libnet_check_profile_name_validity(ap_info->ProfileName) == false)
700                 rv = __libnet_connect_with_wifi_info(ap_info);
701         else
702                 rv = net_open_connection_with_profile(profile_name.ProfileName);
703
704         if (rv != NET_ERR_NONE)
705                 return WIFI_ERROR_OPERATION_FAILED;
706
707         __libnet_set_connected_cb(callback, user_data);
708
709         return WIFI_ERROR_NONE;
710 }
711
712 int _wifi_libnet_close_profile(wifi_ap_h ap_h, wifi_disconnected_cb callback, void* user_data)
713 {
714         net_profile_info_t *ap_info = ap_h;
715         net_profile_name_t profile_name;
716
717         g_strlcpy(profile_name.ProfileName, ap_info->ProfileName, NET_PROFILE_NAME_LEN_MAX+1);
718
719         if (net_close_connection(profile_name.ProfileName) != NET_ERR_NONE)
720                 return WIFI_ERROR_OPERATION_FAILED;
721
722         __libnet_set_disconnected_cb(callback, user_data);
723
724         return WIFI_ERROR_NONE;
725 }
726
727 int _wifi_libnet_connect_with_wps(wifi_ap_h ap_h, wifi_connected_cb callback, void* user_data)
728 {
729         net_profile_info_t *ap_info = ap_h;
730         net_wifi_wps_info_t wps_info;
731         net_profile_name_t profile_name;
732
733         memset(&wps_info, 0 , sizeof(net_wifi_wps_info_t));
734         g_strlcpy(profile_name.ProfileName, ap_info->ProfileName, NET_PROFILE_NAME_LEN_MAX+1);
735
736         wps_info.type = WIFI_WPS_PBC;
737
738         if (net_wifi_enroll_wps(profile_name.ProfileName, &wps_info) != NET_ERR_NONE)
739                 return WIFI_ERROR_OPERATION_FAILED;
740
741         __libnet_set_connected_cb(callback, user_data);
742
743         return WIFI_ERROR_NONE;
744 }
745
746 int _wifi_libnet_forget_ap(wifi_ap_h ap)
747 {
748         int rv = 0;
749         net_profile_name_t profile_name;
750         net_profile_info_t *ap_info = ap;
751
752         g_strlcpy(profile_name.ProfileName, ap_info->ProfileName, NET_PROFILE_NAME_LEN_MAX+1);
753
754         rv = net_delete_profile(profile_name.ProfileName);
755         if (rv != NET_ERR_NONE)
756                 return WIFI_ERROR_OPERATION_FAILED;
757
758         return WIFI_ERROR_NONE;
759 }
760
761 int _wifi_set_power_on_off_cb(wifi_device_state_changed_cb callback, void *user_data)
762 {
763         if (wifi_callbacks.device_state_cb)
764                 return WIFI_ERROR_INVALID_OPERATION;
765
766         wifi_callbacks.device_state_cb = callback;
767         wifi_callbacks.device_state_user_data = user_data;
768
769         return WIFI_ERROR_NONE;
770 }
771
772 int _wifi_unset_power_on_off_cb(void)
773 {
774         if (wifi_callbacks.device_state_cb == NULL)
775                 return WIFI_ERROR_INVALID_OPERATION;
776
777         wifi_callbacks.device_state_cb = NULL;
778         wifi_callbacks.device_state_user_data = NULL;
779
780         return WIFI_ERROR_NONE;
781 }
782
783 int _wifi_set_background_scan_cb(wifi_scan_finished_cb callback, void *user_data)
784 {
785         if (wifi_callbacks.bg_scan_cb)
786                 return WIFI_ERROR_INVALID_OPERATION;
787
788         wifi_callbacks.bg_scan_cb = callback;
789         wifi_callbacks.bg_scan_user_data = user_data;
790
791         return WIFI_ERROR_NONE;
792 }
793
794 int _wifi_unset_background_scan_cb(void)
795 {
796         if (wifi_callbacks.bg_scan_cb == NULL)
797                 return WIFI_ERROR_INVALID_OPERATION;
798
799         wifi_callbacks.bg_scan_cb = NULL;
800         wifi_callbacks.bg_scan_user_data = NULL;
801
802         return WIFI_ERROR_NONE;
803 }
804
805 int _wifi_set_connection_state_cb(wifi_connection_state_changed_cb callback, void *user_data)
806 {
807         if (wifi_callbacks.connection_state_cb)
808                 return WIFI_ERROR_INVALID_OPERATION;
809
810         wifi_callbacks.connection_state_cb = callback;
811         wifi_callbacks.connection_state_user_data = user_data;
812
813         return WIFI_ERROR_NONE;
814 }
815
816 int _wifi_unset_connection_state_cb()
817 {
818         if (wifi_callbacks.connection_state_cb == NULL)
819                 return WIFI_ERROR_INVALID_OPERATION;
820
821         wifi_callbacks.connection_state_cb = NULL;
822         wifi_callbacks.connection_state_user_data = NULL;
823
824         return WIFI_ERROR_NONE;
825 }
826
827 int _wifi_update_ap_info(net_profile_info_t *ap_info)
828 {
829         if (net_modify_profile(ap_info->ProfileName, ap_info) != NET_ERR_NONE)
830                 return WIFI_ERROR_OPERATION_FAILED;
831
832         return WIFI_ERROR_NONE;
833 }
834