2 * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include "net_wifi_private.h"
23 static __thread bool is_init = false;
24 static __thread GSList *ap_handle_list = NULL;
27 wifi_device_state_changed_cb device_state_cb;
28 void *device_state_user_data;
29 wifi_scan_finished_cb bg_scan_cb;
30 void *bg_scan_user_data;
31 wifi_scan_finished_cb scan_request_cb;
32 void *scan_request_user_data;
33 wifi_scan_finished_cb specific_scan_cb;
34 void *specific_scan_user_data;
35 wifi_connection_state_changed_cb connection_state_cb;
36 void *connection_state_user_data;
37 wifi_activated_cb activated_cb;
38 void *activated_user_data;
39 wifi_deactivated_cb deactivated_cb;
40 void *deactivated_user_data;
41 wifi_connected_cb connected_cb;
42 void *connected_user_data;
43 wifi_disconnected_cb disconnected_cb;
44 void *disconnected_user_data;
47 struct _profile_list_s {
49 net_profile_info_t *profiles;
52 struct _wifi_state_notify {
53 net_profile_info_t *ap_info;
54 wifi_connection_state_e state;
57 struct managed_idle_data {
63 static __thread struct _wifi_cb_s wifi_callbacks = { 0, };
64 static __thread struct _profile_list_s profile_iterator = { 0, NULL };
65 static __thread struct _profile_list_s specific_profile_iterator = {0, NULL};
66 static __thread char specific_profile_essid[NET_WLAN_ESSID_LEN + 1] = { 0, };
67 static __thread GSList *managed_idler_list = NULL;
69 bool _wifi_is_init(void)
74 static void __wifi_set_init(bool tag)
79 static wifi_error_e __libnet_convert_to_ap_error_type(net_err_t err_type)
83 return WIFI_ERROR_NONE;
84 case NET_ERR_APP_ALREADY_REGISTERED:
85 return WIFI_ERROR_INVALID_OPERATION;
86 case NET_ERR_APP_NOT_REGISTERED:
87 return WIFI_ERROR_INVALID_OPERATION;
88 case NET_ERR_NO_ACTIVE_CONNECTIONS:
89 return WIFI_ERROR_NO_CONNECTION;
90 case NET_ERR_ACTIVE_CONNECTION_EXISTS:
91 return WIFI_ERROR_ALREADY_EXISTS;
92 case NET_ERR_CONNECTION_DHCP_FAILED:
93 return WIFI_ERROR_DHCP_FAILED;
94 case NET_ERR_CONNECTION_INVALID_KEY:
95 return WIFI_ERROR_INVALID_KEY;
96 case NET_ERR_IN_PROGRESS:
97 return WIFI_ERROR_NOW_IN_PROGRESS;
98 case NET_ERR_OPERATION_ABORTED:
99 return WIFI_ERROR_OPERATION_ABORTED;
100 case NET_ERR_TIME_OUT:
101 return WIFI_ERROR_NO_REPLY;
102 case NET_ERR_ACCESS_DENIED:
103 return WIFI_ERROR_PERMISSION_DENIED;
105 return WIFI_ERROR_OPERATION_FAILED;
109 static const char *__libnet_convert_ap_error_type_to_string(wifi_error_e err_type)
112 case WIFI_ERROR_NONE:
114 case WIFI_ERROR_INVALID_PARAMETER:
115 return "INVALID_PARAMETER";
116 case WIFI_ERROR_OUT_OF_MEMORY:
117 return "OUT_OF_MEMORY";
118 case WIFI_ERROR_INVALID_OPERATION:
119 return "INVALID_OPERATION";
120 case WIFI_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED:
121 return "ADDRESS_FAMILY_NOT_SUPPORTED";
122 case WIFI_ERROR_OPERATION_FAILED:
123 return "OPERATION_FAILED";
124 case WIFI_ERROR_NO_CONNECTION:
125 return "NO_CONNECTION";
126 case WIFI_ERROR_NOW_IN_PROGRESS:
127 return "NOW_IN_PROGRESS";
128 case WIFI_ERROR_ALREADY_EXISTS:
129 return "ALREADY_EXISTS";
130 case WIFI_ERROR_OPERATION_ABORTED:
131 return "OPERATION_ABORTED";
132 case WIFI_ERROR_DHCP_FAILED:
133 return "DHCP_FAILED";
134 case WIFI_ERROR_INVALID_KEY:
135 return "INVALID_KEY";
136 case WIFI_ERROR_NO_REPLY:
138 case WIFI_ERROR_SECURITY_RESTRICTED:
139 return "SECURITY_RESTRICTED";
140 case WIFI_ERROR_PERMISSION_DENIED:
141 return "PERMISSION_DENIED";
142 case WIFI_ERROR_NOT_SUPPORTED:
143 return "NOT_SUPPROTED";
149 static const char *__libnet_convert_ap_state_to_string(wifi_connection_state_e state)
152 case WIFI_CONNECTION_STATE_FAILURE:
154 case WIFI_CONNECTION_STATE_DISCONNECTED:
155 return "DISCONNECTED";
156 case WIFI_CONNECTION_STATE_ASSOCIATION:
157 return "ASSOCIATION";
158 case WIFI_CONNECTION_STATE_CONFIGURATION:
159 return "CONFIGURATION";
160 case WIFI_CONNECTION_STATE_CONNECTED:
167 static void __libnet_clear_profile_list(struct _profile_list_s *profile_list)
169 if (profile_list->count > 0)
170 g_free(profile_list->profiles);
172 profile_list->count = 0;
173 profile_list->profiles = NULL;
176 static int __libnet_update_profile_iterator(void)
179 struct _profile_list_s wifi_profiles = { 0, NULL };
181 __libnet_clear_profile_list(&profile_iterator);
183 rv = net_get_profile_list(NET_DEVICE_WIFI, &wifi_profiles.profiles, &wifi_profiles.count);
184 WIFI_LOG(WIFI_INFO, "Wi-Fi profile count: %d", wifi_profiles.count);
186 if (rv == NET_ERR_ACCESS_DENIED) {
187 WIFI_LOG(WIFI_ERROR, "Access denied");
188 return WIFI_ERROR_PERMISSION_DENIED;
191 if (wifi_profiles.count == 0)
192 return WIFI_ERROR_NONE;
194 profile_iterator.count = wifi_profiles.count;
195 profile_iterator.profiles = wifi_profiles.profiles;
197 return WIFI_ERROR_NONE;
200 static void __libnet_update_specific_profile_iterator(GSList *ap_list)
203 GSList *list = ap_list;
205 for (count = 0; list; list = list->next) {
210 WIFI_LOG(WIFI_INFO, "No hidden AP found\n");
214 specific_profile_iterator.count = count;
215 specific_profile_iterator.profiles = g_try_new0(net_profile_info_t, count);
218 for (count = 0; list; list = list->next) {
219 struct ssid_scan_bss_info_t *ap = (struct ssid_scan_bss_info_t *)list->data;
220 net_profile_info_t *profile = &specific_profile_iterator.profiles[count];
222 g_strlcpy(profile->ProfileInfo.Wlan.essid, ap->ssid, NET_WLAN_ESSID_LEN+1);
223 profile->ProfileInfo.Wlan.security_info.sec_mode = ap->security;
228 WIFI_LOG(WIFI_INFO, "Specific AP count : %d\n", count);
231 static void __libnet_convert_profile_info_to_wifi_info(net_wifi_connection_info_t *wifi_info,
232 net_profile_info_t *ap_info)
234 g_strlcpy(wifi_info->essid, ap_info->ProfileInfo.Wlan.essid, NET_WLAN_ESSID_LEN+1);
235 wifi_info->wlan_mode = ap_info->ProfileInfo.Wlan.wlan_mode;
236 memcpy(&wifi_info->security_info, &ap_info->ProfileInfo.Wlan.security_info, sizeof(wlan_security_info_t));
237 wifi_info->is_hidden = ap_info->ProfileInfo.Wlan.is_hidden;
240 static int __libnet_connect_with_wifi_info(net_profile_info_t *ap_info)
243 net_wifi_connection_info_t wifi_info;
244 memset(&wifi_info, 0, sizeof(net_wifi_connection_info_t));
246 __libnet_convert_profile_info_to_wifi_info(&wifi_info, ap_info);
248 rv = net_open_connection_with_wifi_info(&wifi_info);
249 if (rv == NET_ERR_ACCESS_DENIED) {
250 WIFI_LOG(WIFI_ERROR, "Access denied");
251 return WIFI_ERROR_PERMISSION_DENIED;
252 } else if (rv != NET_ERR_NONE)
253 return WIFI_ERROR_OPERATION_FAILED;
255 return WIFI_ERROR_NONE;
258 static void __libnet_state_changed_cb(char *profile_name, net_profile_info_t *profile_info,
259 wifi_connection_state_e state)
261 if (profile_name == NULL)
264 if (profile_info == NULL) {
265 WIFI_LOG(WIFI_ERROR, "Error!! Profile info not found! : %s\n", profile_name);
269 ap_handle_list = g_slist_append(ap_handle_list, (wifi_ap_h)profile_info);
271 if (wifi_callbacks.connection_state_cb)
272 wifi_callbacks.connection_state_cb(state, (wifi_ap_h)profile_info,
273 wifi_callbacks.connection_state_user_data);
275 ap_handle_list = g_slist_remove(ap_handle_list, (wifi_ap_h)profile_info);
278 static void __libnet_set_activated_cb(wifi_activated_cb user_cb, void *user_data)
281 wifi_callbacks.activated_cb = user_cb;
282 wifi_callbacks.activated_user_data = user_data;
286 static void __libnet_activated_cb(wifi_error_e result)
288 if (wifi_callbacks.activated_cb)
289 wifi_callbacks.activated_cb(result, wifi_callbacks.activated_user_data);
291 wifi_callbacks.activated_cb = NULL;
292 wifi_callbacks.activated_user_data = NULL;
295 static void __libnet_set_deactivated_cb(wifi_disconnected_cb user_cb, void *user_data)
297 if (user_cb != NULL) {
298 wifi_callbacks.deactivated_cb = user_cb;
299 wifi_callbacks.deactivated_user_data = user_data;
303 static void __libnet_deactivated_cb(wifi_error_e result)
305 if (wifi_callbacks.deactivated_cb)
306 wifi_callbacks.deactivated_cb(result, wifi_callbacks.deactivated_user_data);
308 wifi_callbacks.deactivated_cb = NULL;
309 wifi_callbacks.deactivated_user_data = NULL;
312 static void __libnet_power_on_off_cb(net_event_info_t *event_cb, bool is_requested)
314 if (_wifi_is_init() != true) {
315 WIFI_LOG(WIFI_ERROR, "Application is not registered"
316 "If multi-threaded, thread integrity be broken.");
320 if (wifi_callbacks.device_state_cb == NULL &&
321 wifi_callbacks.activated_cb == NULL &&
322 wifi_callbacks.deactivated_cb == NULL)
325 wifi_error_e error_code = WIFI_ERROR_NONE;
326 wifi_device_state_e state = WIFI_DEVICE_STATE_DEACTIVATED;
327 net_wifi_state_t *wifi_state = (net_wifi_state_t *)event_cb->Data;
329 if (event_cb->Error == NET_ERR_NONE &&
330 event_cb->Datalength == sizeof(net_wifi_state_t)) {
331 if (*wifi_state == WIFI_ON) {
332 WIFI_LOG(WIFI_INFO, "Wi-Fi power on");
333 state = WIFI_DEVICE_STATE_ACTIVATED;
334 } else if (*wifi_state == WIFI_OFF) {
335 WIFI_LOG(WIFI_INFO, "Wi-Fi power off");
336 state = WIFI_DEVICE_STATE_DEACTIVATED;
337 __libnet_clear_profile_list(&profile_iterator);
338 __libnet_clear_profile_list(&specific_profile_iterator);
340 WIFI_LOG(WIFI_ERROR, "Error Wi-Fi state %d", *wifi_state);
341 error_code = WIFI_ERROR_OPERATION_FAILED;
342 state = WIFI_DEVICE_STATE_DEACTIVATED;
345 WIFI_LOG(WIFI_ERROR, "Wi-Fi power request failed(%d)", event_cb->Error);
347 if (event_cb->Error == NET_ERR_SECURITY_RESTRICTED)
348 error_code = WIFI_ERROR_SECURITY_RESTRICTED;
350 error_code = WIFI_ERROR_OPERATION_FAILED;
352 state = WIFI_DEVICE_STATE_DEACTIVATED;
355 __libnet_activated_cb(error_code);
356 __libnet_deactivated_cb(error_code);
358 if (wifi_callbacks.device_state_cb)
359 wifi_callbacks.device_state_cb(state, wifi_callbacks.device_state_user_data);
362 static void __libnet_scan_cb(net_event_info_t *event_cb)
364 wifi_error_e error_code = WIFI_ERROR_NONE;
366 if (event_cb->Error != NET_ERR_NONE) {
367 WIFI_LOG(WIFI_ERROR, "Scan failed!, Error [%d]\n", event_cb->Error);
368 error_code = WIFI_ERROR_OPERATION_FAILED;
371 if (wifi_callbacks.scan_request_cb) {
372 wifi_callbacks.scan_request_cb(error_code, wifi_callbacks.scan_request_user_data);
373 wifi_callbacks.scan_request_cb = NULL;
374 wifi_callbacks.scan_request_user_data = NULL;
378 if (wifi_callbacks.bg_scan_cb != NULL)
379 wifi_callbacks.bg_scan_cb(error_code, wifi_callbacks.bg_scan_user_data);
382 static void __libnet_specific_scan_cb(net_event_info_t *event_cb)
384 wifi_error_e error_code = WIFI_ERROR_NONE;
386 __libnet_clear_profile_list(&specific_profile_iterator);
388 if (event_cb->Error != NET_ERR_NONE) {
389 WIFI_LOG(WIFI_ERROR, "Specific scan failed!, Error [%d]\n", event_cb->Error);
390 error_code = WIFI_ERROR_OPERATION_FAILED;
391 } else if (event_cb->Data) {
392 GSList *ap_list = (GSList *)event_cb->Data;
393 __libnet_update_specific_profile_iterator(ap_list);
396 if (wifi_callbacks.specific_scan_cb) {
397 wifi_callbacks.specific_scan_cb(error_code, wifi_callbacks.specific_scan_user_data);
398 wifi_callbacks.specific_scan_cb = NULL;
399 wifi_callbacks.specific_scan_user_data = NULL;
403 static void __libnet_set_connected_cb(wifi_connected_cb user_cb, void *user_data)
406 wifi_callbacks.connected_cb = user_cb;
407 wifi_callbacks.connected_user_data = user_data;
411 static void __libnet_connected_cb(wifi_error_e result)
413 if (wifi_callbacks.connected_cb)
414 wifi_callbacks.connected_cb(result, wifi_callbacks.connected_user_data);
416 wifi_callbacks.connected_cb = NULL;
417 wifi_callbacks.connected_user_data = NULL;
420 static void __libnet_set_disconnected_cb(wifi_disconnected_cb user_cb, void *user_data)
423 wifi_callbacks.disconnected_cb = user_cb;
424 wifi_callbacks.disconnected_user_data = user_data;
428 static void __libnet_disconnected_cb(wifi_error_e result)
430 if (wifi_callbacks.disconnected_cb)
431 wifi_callbacks.disconnected_cb(result, wifi_callbacks.disconnected_user_data);
433 wifi_callbacks.disconnected_cb = NULL;
434 wifi_callbacks.disconnected_user_data = NULL;
437 static void __libnet_evt_cb(net_event_info_t *event_cb, void *user_data)
439 bool is_requested = false;
440 net_profile_info_t *prof_info_p = NULL;
441 net_profile_info_t prof_info;
442 wifi_error_e result = WIFI_ERROR_NONE;
444 switch (event_cb->Event) {
445 case NET_EVENT_OPEN_RSP:
446 case NET_EVENT_WIFI_WPS_RSP:
449 case NET_EVENT_OPEN_IND:
450 if (_wifi_libnet_check_profile_name_validity(event_cb->ProfileName) != true)
453 result = __libnet_convert_to_ap_error_type(event_cb->Error);
454 WIFI_LOG(WIFI_INFO, "Got Open RSP/IND : %s\n",
455 __libnet_convert_ap_error_type_to_string(result));
458 __libnet_connected_cb(result);
460 switch (event_cb->Error) {
462 if (event_cb->Datalength == sizeof(net_profile_info_t))
463 prof_info_p = (net_profile_info_t *)event_cb->Data;
465 __libnet_state_changed_cb(event_cb->ProfileName, prof_info_p,
466 WIFI_CONNECTION_STATE_CONNECTED);
468 case NET_ERR_ACTIVE_CONNECTION_EXISTS:
474 if (net_get_profile_info(event_cb->ProfileName, &prof_info) == NET_ERR_NONE)
475 __libnet_state_changed_cb(event_cb->ProfileName, &prof_info,
476 WIFI_CONNECTION_STATE_DISCONNECTED);
478 __libnet_state_changed_cb(event_cb->ProfileName, NULL,
479 WIFI_CONNECTION_STATE_DISCONNECTED);
482 case NET_EVENT_CLOSE_RSP:
485 case NET_EVENT_CLOSE_IND:
486 if (_wifi_libnet_check_profile_name_validity(event_cb->ProfileName) != true)
489 result = __libnet_convert_to_ap_error_type(event_cb->Error);
490 WIFI_LOG(WIFI_INFO, "Got Close RSP/IND : %s\n",
491 __libnet_convert_ap_error_type_to_string(result));
494 __libnet_disconnected_cb(result);
496 switch (event_cb->Error) {
498 if (net_get_profile_info(event_cb->ProfileName, &prof_info) == NET_ERR_NONE)
499 __libnet_state_changed_cb(event_cb->ProfileName, &prof_info,
500 WIFI_CONNECTION_STATE_DISCONNECTED);
502 __libnet_state_changed_cb(event_cb->ProfileName, NULL,
503 WIFI_CONNECTION_STATE_DISCONNECTED);
510 case NET_EVENT_NET_STATE_IND:
511 if (_wifi_libnet_check_profile_name_validity(event_cb->ProfileName) != true)
514 if (event_cb->Datalength != sizeof(net_state_type_t))
517 net_state_type_t *profile_state = (net_state_type_t *)event_cb->Data;
518 wifi_connection_state_e ap_state = _wifi_convert_to_ap_state(*profile_state);
521 "Profile State : %s, profile name : %s\n",
522 __libnet_convert_ap_state_to_string(ap_state),
523 event_cb->ProfileName);
525 if (net_get_profile_info(event_cb->ProfileName, &prof_info) == NET_ERR_NONE)
526 __libnet_state_changed_cb(event_cb->ProfileName, &prof_info, ap_state);
528 __libnet_state_changed_cb(event_cb->ProfileName, NULL, ap_state);
531 case NET_EVENT_WIFI_SCAN_RSP:
532 case NET_EVENT_WIFI_SCAN_IND:
533 WIFI_LOG(WIFI_INFO, "Got Wi-Fi scan IND\n");
534 __libnet_scan_cb(event_cb);
536 case NET_EVENT_SPECIFIC_SCAN_RSP:
537 WIFI_LOG(WIFI_INFO, "Got Wi-Fi specific scan RSP\n");
539 case NET_EVENT_SPECIFIC_SCAN_IND:
540 WIFI_LOG(WIFI_INFO, "Got Wi-Fi specific scan IND\n");
541 __libnet_specific_scan_cb(event_cb);
543 case NET_EVENT_WIFI_POWER_RSP:
546 case NET_EVENT_WIFI_POWER_IND:
547 __libnet_power_on_off_cb(event_cb, is_requested);
554 int _wifi_libnet_init(void)
558 rv = net_register_client_ext((net_event_cb_t)__libnet_evt_cb, NET_DEVICE_WIFI, NULL);
559 if (rv != NET_ERR_NONE)
562 __wifi_set_init(true);
567 bool _wifi_libnet_deinit(void)
569 if (net_deregister_client_ext(NET_DEVICE_WIFI) != NET_ERR_NONE)
572 __libnet_clear_profile_list(&profile_iterator);
573 __libnet_clear_profile_list(&specific_profile_iterator);
574 g_slist_free_full(ap_handle_list, g_free);
575 ap_handle_list = NULL;
576 memset(&wifi_callbacks, 0, sizeof(struct _wifi_cb_s));
578 __wifi_set_init(false);
583 int _wifi_activate(wifi_activated_cb callback, gboolean wifi_picker_test,
586 int rv = NET_ERR_NONE;
588 rv = net_wifi_power_on();
589 if (rv == NET_ERR_NONE) {
590 __libnet_set_activated_cb(callback, user_data);
591 return WIFI_ERROR_NONE;
592 } else if (rv == NET_ERR_ACCESS_DENIED) {
593 WIFI_LOG(WIFI_ERROR, "Access denied");
594 return WIFI_ERROR_PERMISSION_DENIED;
595 } else if (rv == NET_ERR_INVALID_OPERATION)
596 return WIFI_ERROR_INVALID_OPERATION;
597 else if (rv == NET_ERR_ALREADY_EXISTS)
598 return WIFI_ERROR_ALREADY_EXISTS;
599 else if (rv == NET_ERR_IN_PROGRESS)
600 return WIFI_ERROR_NOW_IN_PROGRESS;
601 else if (rv == NET_ERR_SECURITY_RESTRICTED)
602 return WIFI_ERROR_SECURITY_RESTRICTED;
604 return WIFI_ERROR_OPERATION_FAILED;
607 int _wifi_deactivate(wifi_deactivated_cb callback, void *user_data)
609 int rv = NET_ERR_NONE;
611 rv = net_wifi_power_off();
612 if (rv == NET_ERR_NONE) {
613 __libnet_set_deactivated_cb(callback, user_data);
614 return WIFI_ERROR_NONE;
615 } else if (rv == NET_ERR_ACCESS_DENIED) {
616 WIFI_LOG(WIFI_ERROR, "Access denied");
617 return WIFI_ERROR_PERMISSION_DENIED;
618 } else if (rv == NET_ERR_INVALID_OPERATION)
619 return WIFI_ERROR_INVALID_OPERATION;
620 else if (rv == NET_ERR_ALREADY_EXISTS)
621 return WIFI_ERROR_ALREADY_EXISTS;
622 else if (rv == NET_ERR_IN_PROGRESS)
623 return WIFI_ERROR_NOW_IN_PROGRESS;
624 else if (rv == NET_ERR_SECURITY_RESTRICTED)
625 return WIFI_ERROR_SECURITY_RESTRICTED;
627 return WIFI_ERROR_OPERATION_FAILED;
630 bool _wifi_libnet_check_ap_validity(wifi_ap_h ap_h)
638 for (list = ap_handle_list; list; list = list->next)
639 if (ap_h == list->data) return true;
641 for (i = 0; i < profile_iterator.count; i++)
642 if (ap_h == &profile_iterator.profiles[i]) return true;
644 for (i = 0; i < specific_profile_iterator.count; i++)
645 if (ap_h == &specific_profile_iterator.profiles[i]) return true;
650 void _wifi_libnet_add_to_ap_list(wifi_ap_h ap_h)
652 ap_handle_list = g_slist_append(ap_handle_list, ap_h);
655 void _wifi_libnet_remove_from_ap_list(wifi_ap_h ap_h)
657 ap_handle_list = g_slist_remove(ap_handle_list, ap_h);
661 bool _wifi_libnet_check_profile_name_validity(const char *profile_name)
663 const char *profile_prefix = "/net/connman/service/wifi_";
666 if (profile_name == NULL ||
667 g_str_has_prefix(profile_name, profile_prefix) != TRUE) {
668 WIFI_LOG(WIFI_INFO, "The profile is a hidden or not a regular Wi-Fi profile");
672 while (profile_name[i] != '\0') {
673 if (isgraph(profile_name[i]) == 0) {
674 WIFI_LOG(WIFI_INFO, "Invalid format: %s", profile_name);
683 int _wifi_libnet_get_wifi_device_state(wifi_device_state_e *device_state)
685 net_tech_info_t tech_info;
687 int rv = NET_ERR_NONE;
688 rv = net_get_technology_properties(NET_DEVICE_WIFI, &tech_info);
689 if (rv == NET_ERR_ACCESS_DENIED) {
690 WIFI_LOG(WIFI_ERROR, "Access denied");
691 return WIFI_ERROR_PERMISSION_DENIED;
692 } else if (rv != NET_ERR_NONE) {
693 WIFI_LOG(WIFI_ERROR, "Failed to get technology properties");
694 return WIFI_ERROR_OPERATION_FAILED;
697 if (tech_info.powered)
698 *device_state = WIFI_DEVICE_STATE_ACTIVATED;
700 *device_state = WIFI_DEVICE_STATE_DEACTIVATED;
702 return WIFI_ERROR_NONE;
705 int _wifi_libnet_get_wifi_state(wifi_connection_state_e* connection_state)
708 net_wifi_state_t wlan_state = 0;
709 net_profile_name_t profile_name;
711 rv = net_get_wifi_state(&wlan_state, &profile_name);
712 if (rv == NET_ERR_ACCESS_DENIED) {
713 WIFI_LOG(WIFI_ERROR, "Access denied");
714 return WIFI_ERROR_PERMISSION_DENIED;
715 } else if (rv != NET_ERR_NONE) {
716 WIFI_LOG(WIFI_ERROR, "Failed to get Wi-Fi state");
717 return WIFI_ERROR_OPERATION_FAILED;
720 switch (wlan_state) {
723 *connection_state = WIFI_CONNECTION_STATE_DISCONNECTED;
726 *connection_state = WIFI_CONNECTION_STATE_CONNECTED;
728 case WIFI_DISCONNECTING:
729 *connection_state = WIFI_CONNECTION_STATE_CONNECTED;
732 WIFI_LOG(WIFI_ERROR, "Unknown state");
733 return WIFI_ERROR_OPERATION_FAILED;
736 return WIFI_ERROR_NONE;
739 int _wifi_libnet_get_intf_name(char** name)
743 if (profile_iterator.count == 0) {
744 rv = __libnet_update_profile_iterator();
745 if (rv == NET_ERR_ACCESS_DENIED) {
746 WIFI_LOG(WIFI_ERROR, "Access denied");
747 return WIFI_ERROR_PERMISSION_DENIED;
751 if (profile_iterator.count == 0) {
752 WIFI_LOG(WIFI_ERROR, "There is no AP");
753 return WIFI_ERROR_OPERATION_FAILED;
756 *name = g_strdup(profile_iterator.profiles->ProfileInfo.Wlan.net_info.DevName);
758 return WIFI_ERROR_OUT_OF_MEMORY;
760 return WIFI_ERROR_NONE;
763 int _wifi_libnet_scan_request(wifi_scan_finished_cb callback, void *user_data)
766 rv = net_scan_wifi();
768 if (rv == NET_ERR_NONE) {
769 wifi_callbacks.scan_request_cb = callback;
770 wifi_callbacks.scan_request_user_data = user_data;
771 return WIFI_ERROR_NONE;
772 } else if (rv == NET_ERR_ACCESS_DENIED) {
773 WIFI_LOG(WIFI_ERROR, "Access denied");
774 return WIFI_ERROR_PERMISSION_DENIED;
775 } else if (rv == NET_ERR_INVALID_OPERATION)
776 return WIFI_ERROR_INVALID_OPERATION;
778 return WIFI_ERROR_OPERATION_FAILED;
781 int _wifi_libnet_scan_specific_ap(const char *essid,
782 wifi_scan_finished_cb callback, void *user_data)
785 rv = net_specific_scan_wifi(essid);
787 if (rv == NET_ERR_NONE) {
788 g_strlcpy(specific_profile_essid, essid, NET_WLAN_ESSID_LEN+1);
789 wifi_callbacks.specific_scan_cb = callback;
790 wifi_callbacks.specific_scan_user_data = user_data;
791 return WIFI_ERROR_NONE;
792 } else if (rv == NET_ERR_ACCESS_DENIED) {
793 WIFI_LOG(WIFI_ERROR, "Access denied");
794 return WIFI_ERROR_PERMISSION_DENIED;
795 } else if (rv == NET_ERR_INVALID_OPERATION)
796 return WIFI_ERROR_INVALID_OPERATION;
798 return WIFI_ERROR_OPERATION_FAILED;
801 int _wifi_libnet_get_connected_profile(wifi_ap_h *ap)
804 wifi_ap_h ap_h = NULL;
806 rv = __libnet_update_profile_iterator();
807 if (rv == NET_ERR_ACCESS_DENIED) {
808 WIFI_LOG(WIFI_ERROR, "Access denied");
809 return WIFI_ERROR_PERMISSION_DENIED;
812 for (i = 0; i < profile_iterator.count; i++) {
813 if (profile_iterator.profiles[i].ProfileState == NET_STATE_TYPE_ONLINE ||
814 profile_iterator.profiles[i].ProfileState == NET_STATE_TYPE_READY) {
815 ap_h = (wifi_ap_h)(&profile_iterator.profiles[i]);
821 WIFI_LOG(WIFI_ERROR, "There is no connected AP");
822 return WIFI_ERROR_NO_CONNECTION;
825 *ap = g_try_malloc0(sizeof(net_profile_info_t));
827 return WIFI_ERROR_OUT_OF_MEMORY;
829 memcpy(*ap, ap_h, sizeof(net_profile_info_t));
831 _wifi_libnet_add_to_ap_list(*ap);
833 return WIFI_ERROR_NONE;
836 int _wifi_libnet_foreach_found_aps(wifi_found_ap_cb callback, void *user_data)
840 rv = __libnet_update_profile_iterator();
841 if (rv == NET_ERR_ACCESS_DENIED) {
842 WIFI_LOG(WIFI_ERROR, "Access denied");
843 return WIFI_ERROR_PERMISSION_DENIED;
846 if (profile_iterator.count == 0) {
847 WIFI_LOG(WIFI_WARN, "There is no AP");
848 return WIFI_ERROR_NONE;
851 for (i = 0; i < profile_iterator.count; i++) {
852 if (profile_iterator.profiles[i].ProfileInfo.Wlan.is_hidden == TRUE)
855 rv = callback((wifi_ap_h)(&profile_iterator.profiles[i]), user_data);
856 if (rv == false) break;
859 return WIFI_ERROR_NONE;
862 int _wifi_libnet_foreach_found_specific_aps(wifi_found_ap_cb callback, void *user_data)
866 if (specific_profile_iterator.count == 0) {
867 WIFI_LOG(WIFI_WARN, "There is no specific APs");
869 rv = __libnet_update_profile_iterator();
870 if (rv == NET_ERR_ACCESS_DENIED) {
871 WIFI_LOG(WIFI_ERROR, "Access denied");
872 return WIFI_ERROR_PERMISSION_DENIED;
875 if (profile_iterator.count == 0) {
876 WIFI_LOG(WIFI_WARN, "There is no APs");
877 return WIFI_ERROR_NONE;
880 for (i = 0; i < profile_iterator.count; i++) {
881 if (!g_strcmp0(specific_profile_essid,
882 profile_iterator.profiles[i].ProfileInfo.Wlan.essid)) {
883 rv = callback((wifi_ap_h)(&profile_iterator.profiles[i]), user_data);
884 if (rv == false) break;
887 return WIFI_ERROR_NONE;
890 for (i = 0; i < specific_profile_iterator.count; i++) {
891 rv = callback((wifi_ap_h)(&specific_profile_iterator.profiles[i]), user_data);
892 if (rv == false) break;
895 return WIFI_ERROR_NONE;
898 int _wifi_libnet_open_profile(wifi_ap_h ap_h, wifi_connected_cb callback, void *user_data)
902 net_profile_info_t *ap_info = ap_h;
905 _wifi_libnet_check_profile_name_validity(ap_info->ProfileName);
907 if (valid_profile == true && ap_info->Favourite)
908 rv = net_open_connection_with_profile(ap_info->ProfileName);
909 else if (valid_profile == true &&
910 ap_info->ProfileInfo.Wlan.is_hidden != TRUE &&
911 ap_info->ProfileInfo.Wlan.security_info.sec_mode == WLAN_SEC_MODE_NONE)
912 rv = net_open_connection_with_profile(ap_info->ProfileName);
914 rv = __libnet_connect_with_wifi_info(ap_info);
916 rv = __libnet_convert_to_ap_error_type(rv);
917 if (rv == WIFI_ERROR_NONE)
918 __libnet_set_connected_cb(callback, user_data);
923 int _wifi_libnet_close_profile(wifi_ap_h ap_h, wifi_disconnected_cb callback, void *user_data)
926 net_profile_info_t *ap_info = ap_h;
928 rv = net_close_connection(ap_info->ProfileName);
929 if (rv == NET_ERR_ACCESS_DENIED) {
930 WIFI_LOG(WIFI_ERROR, "Access denied");
931 return WIFI_ERROR_PERMISSION_DENIED;
932 } else if (rv != NET_ERR_NONE)
933 return WIFI_ERROR_OPERATION_FAILED;
935 __libnet_set_disconnected_cb(callback, user_data);
937 return WIFI_ERROR_NONE;
940 int _wifi_libnet_connect_with_wps_pbc(wifi_ap_h ap_h, wifi_connected_cb callback, void *user_data)
943 net_profile_info_t *ap_info = ap_h;
944 net_wifi_wps_info_t wps_info;
945 memset(&wps_info, 0, sizeof(net_wifi_wps_info_t));
947 wps_info.type = WIFI_WPS_PBC;
949 rv = net_wifi_enroll_wps(ap_info->ProfileName, &wps_info);
950 if (rv == NET_ERR_ACCESS_DENIED) {
951 WIFI_LOG(WIFI_ERROR, "Access denied");
952 return WIFI_ERROR_PERMISSION_DENIED;
953 } else if (rv != NET_ERR_NONE)
954 return WIFI_ERROR_OPERATION_FAILED;
956 __libnet_set_connected_cb(callback, user_data);
958 return WIFI_ERROR_NONE;
961 int _wifi_libnet_connect_with_wps_pin(wifi_ap_h ap_h, const char *pin,
962 wifi_connected_cb callback, void *user_data)
965 net_profile_info_t *ap_info = ap_h;
966 net_wifi_wps_info_t wps_info;
968 if (ap_info == NULL) {
969 WIFI_LOG(WIFI_ERROR, "Invalid parameter");
970 return WIFI_ERROR_INVALID_PARAMETER;
973 wps_info.type = WIFI_WPS_PIN;
974 g_strlcpy(wps_info.pin, pin, NET_WLAN_MAX_WPSPIN_LEN + 1);
976 rv = net_wifi_enroll_wps(ap_info->ProfileName, &wps_info);
977 if (rv == NET_ERR_ACCESS_DENIED) {
978 WIFI_LOG(WIFI_ERROR, "Access denied");
979 return WIFI_ERROR_PERMISSION_DENIED;
980 } else if (rv != NET_ERR_NONE)
981 return WIFI_ERROR_OPERATION_FAILED;
983 __libnet_set_connected_cb(callback, user_data);
985 return WIFI_ERROR_NONE;
988 int _wifi_libnet_forget_ap(wifi_ap_h ap)
991 net_profile_info_t *ap_info = ap;
993 if (ap_info == NULL) {
994 WIFI_LOG(WIFI_ERROR, "Invalid parameter");
995 return WIFI_ERROR_INVALID_PARAMETER;
998 rv = net_delete_profile(ap_info->ProfileName);
999 if (rv == NET_ERR_ACCESS_DENIED) {
1000 WIFI_LOG(WIFI_ERROR, "Access denied");
1001 return WIFI_ERROR_PERMISSION_DENIED;
1002 } else if (rv != NET_ERR_NONE)
1003 return WIFI_ERROR_OPERATION_FAILED;
1005 ap_info->Favourite = (char)FALSE;
1007 return WIFI_ERROR_NONE;
1010 int _wifi_set_power_on_off_cb(wifi_device_state_changed_cb callback, void *user_data)
1012 if (wifi_callbacks.device_state_cb != NULL)
1013 return WIFI_ERROR_INVALID_OPERATION;
1015 wifi_callbacks.device_state_cb = callback;
1016 wifi_callbacks.device_state_user_data = user_data;
1018 WIFI_LOG(WIFI_INFO, "Wi-Fi registered device state changed callback");
1020 return WIFI_ERROR_NONE;
1023 int _wifi_unset_power_on_off_cb(void)
1025 if (wifi_callbacks.device_state_cb == NULL)
1026 return WIFI_ERROR_INVALID_OPERATION;
1028 wifi_callbacks.device_state_cb = NULL;
1029 wifi_callbacks.device_state_user_data = NULL;
1031 return WIFI_ERROR_NONE;
1034 int _wifi_set_background_scan_cb(wifi_scan_finished_cb callback, void *user_data)
1036 if (wifi_callbacks.bg_scan_cb != NULL)
1037 return WIFI_ERROR_INVALID_OPERATION;
1039 wifi_callbacks.bg_scan_cb = callback;
1040 wifi_callbacks.bg_scan_user_data = user_data;
1042 return WIFI_ERROR_NONE;
1045 int _wifi_unset_background_scan_cb(void)
1047 if (wifi_callbacks.bg_scan_cb == NULL)
1048 return WIFI_ERROR_INVALID_OPERATION;
1050 wifi_callbacks.bg_scan_cb = NULL;
1051 wifi_callbacks.bg_scan_user_data = NULL;
1053 return WIFI_ERROR_NONE;
1056 int _wifi_set_connection_state_cb(wifi_connection_state_changed_cb callback, void *user_data)
1058 if (wifi_callbacks.connection_state_cb != NULL)
1059 return WIFI_ERROR_INVALID_OPERATION;
1061 wifi_callbacks.connection_state_cb = callback;
1062 wifi_callbacks.connection_state_user_data = user_data;
1064 return WIFI_ERROR_NONE;
1067 int _wifi_unset_connection_state_cb()
1069 if (wifi_callbacks.connection_state_cb == NULL)
1070 return WIFI_ERROR_INVALID_OPERATION;
1072 wifi_callbacks.connection_state_cb = NULL;
1073 wifi_callbacks.connection_state_user_data = NULL;
1075 return WIFI_ERROR_NONE;
1078 int _wifi_update_ap_info(net_profile_info_t *ap_info)
1080 int rv = NET_ERR_NONE;
1081 rv = net_modify_profile(ap_info->ProfileName, ap_info);
1083 if (rv == NET_ERR_ACCESS_DENIED) {
1084 WIFI_LOG(WIFI_ERROR, "Access denied");
1085 return WIFI_ERROR_PERMISSION_DENIED;
1086 } else if (rv != NET_ERR_NONE)
1087 return WIFI_ERROR_OPERATION_FAILED;
1089 return WIFI_ERROR_NONE;
1092 void _wifi_callback_cleanup(void)
1094 GSList *cur = managed_idler_list;
1096 struct managed_idle_data *data;
1099 GSList *next = cur->next;
1100 data = (struct managed_idle_data *)cur->data;
1102 src = g_main_context_find_source_by_id(g_main_context_default(), data->id);
1104 g_source_destroy(src);
1105 cur = managed_idler_list;
1110 g_slist_free(managed_idler_list);
1111 managed_idler_list = NULL;