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