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 GSList *ap_handle_list = NULL;
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;
46 struct _profile_list_s {
48 net_profile_info_t *profiles;
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};
56 static wifi_error_e __libnet_convert_to_ap_error_type(net_err_t err_type)
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;
80 return WIFI_ERROR_OPERATION_FAILED;
84 static const char *__libnet_convert_ap_error_type_to_string(wifi_error_e err_type)
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:
113 case WIFI_ERROR_SECURITY_RESTRICTED:
114 return "SECURITY_RESTRICTED";
120 static const char *__libnet_convert_ap_state_to_string(wifi_connection_state_e 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:
136 static void __libnet_clear_profile_list(struct _profile_list_s *profile_list)
138 if (profile_list->count > 0)
139 g_free(profile_list->profiles);
141 profile_list->count = 0;
142 profile_list->profiles = NULL;
145 static void __libnet_update_profile_iterator(void)
147 struct _profile_list_s wifi_profiles = {0, NULL};
149 __libnet_clear_profile_list(&profile_iterator);
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);
154 if (wifi_profiles.count == 0)
157 profile_iterator.count = wifi_profiles.count;
158 profile_iterator.profiles = wifi_profiles.profiles;
161 static void __libnet_update_hidden_profile_iterator(GSList *ap_list)
164 GSList *list = ap_list;
166 for (count = 0; list; list = list->next)
170 WIFI_LOG(WIFI_INFO, "No hidden AP found\n");
174 hidden_profile_iterator.count = count;
175 hidden_profile_iterator.profiles = g_try_new0(net_profile_info_t, count);
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];
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;
187 WIFI_LOG(WIFI_INFO, "Hidden AP count : %d\n", count);
190 static void __libnet_convert_profile_info_to_wifi_info(net_wifi_connection_info_t *wifi_info,
191 net_profile_info_t *ap_info)
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));
198 static int __libnet_connect_with_wifi_info(net_profile_info_t *ap_info)
200 net_wifi_connection_info_t wifi_info;
201 memset(&wifi_info, 0, sizeof(net_wifi_connection_info_t));
203 __libnet_convert_profile_info_to_wifi_info(&wifi_info, ap_info);
205 if (net_open_connection_with_wifi_info(&wifi_info) != NET_ERR_NONE)
206 return WIFI_ERROR_OPERATION_FAILED;
208 return WIFI_ERROR_NONE;
211 static void __libnet_state_changed_cb(char *profile_name, net_profile_info_t *profile_info,
212 wifi_connection_state_e state)
214 if (profile_name == NULL)
217 if (profile_info == NULL) {
218 WIFI_LOG(WIFI_ERROR, "Error!! Profile info not found! : %s\n", profile_name);
222 ap_handle_list = g_slist_append(ap_handle_list, (wifi_ap_h)profile_info);
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);
228 ap_handle_list = g_slist_remove(ap_handle_list, (wifi_ap_h)profile_info);
231 static void __libnet_set_activated_cb(wifi_activated_cb user_cb, void *user_data)
234 wifi_callbacks.activated_cb = user_cb;
235 wifi_callbacks.activated_user_data = user_data;
239 static void __libnet_activated_cb(wifi_error_e result)
241 if (wifi_callbacks.activated_cb)
242 wifi_callbacks.activated_cb(result, wifi_callbacks.activated_user_data);
244 wifi_callbacks.activated_cb = NULL;
245 wifi_callbacks.activated_user_data = NULL;
248 static void __libnet_set_deactivated_cb(wifi_disconnected_cb user_cb, void *user_data)
251 wifi_callbacks.deactivated_cb = user_cb;
252 wifi_callbacks.deactivated_user_data = user_data;
256 static void __libnet_deactivated_cb(wifi_error_e result)
258 if (wifi_callbacks.deactivated_cb)
259 wifi_callbacks.deactivated_cb(result, wifi_callbacks.deactivated_user_data);
261 wifi_callbacks.deactivated_cb = NULL;
262 wifi_callbacks.deactivated_user_data = NULL;
265 static void __libnet_power_on_off_cb(net_event_info_t *event_cb, bool is_requested)
267 if (wifi_callbacks.device_state_cb == NULL &&
268 wifi_callbacks.activated_cb == NULL &&
269 wifi_callbacks.deactivated_cb == NULL)
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;
276 if (event_cb->Error == NET_ERR_NONE &&
277 event_cb->Datalength == sizeof(net_wifi_state_t)) {
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);
288 WIFI_LOG(WIFI_INFO, "Wi-Fi State : Unknown\n");
289 error_code = WIFI_ERROR_OPERATION_FAILED;
290 state = WIFI_DEVICE_STATE_DEACTIVATED;
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;
298 __libnet_activated_cb(error_code);
299 __libnet_deactivated_cb(error_code);
301 if (wifi_callbacks.device_state_cb)
302 wifi_callbacks.device_state_cb(state, wifi_callbacks.device_state_user_data);
305 static void __libnet_scan_cb(net_event_info_t *event_cb)
307 wifi_error_e error_code = WIFI_ERROR_NONE;
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;
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;
321 if (wifi_callbacks.bg_scan_cb != NULL)
322 wifi_callbacks.bg_scan_cb(error_code, wifi_callbacks.bg_scan_user_data);
325 static void __libnet_hidden_scan_cb(net_event_info_t *event_cb)
327 wifi_error_e error_code = WIFI_ERROR_NONE;
329 __libnet_clear_profile_list(&hidden_profile_iterator);
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);
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;
346 static void __libnet_set_connected_cb(wifi_connected_cb user_cb, void *user_data)
349 wifi_callbacks.connected_cb = user_cb;
350 wifi_callbacks.connected_user_data = user_data;
354 static void __libnet_connected_cb(wifi_error_e result)
356 if (wifi_callbacks.connected_cb)
357 wifi_callbacks.connected_cb(result, wifi_callbacks.connected_user_data);
359 wifi_callbacks.connected_cb = NULL;
360 wifi_callbacks.connected_user_data = NULL;
363 static void __libnet_set_disconnected_cb(wifi_disconnected_cb user_cb, void *user_data)
366 wifi_callbacks.disconnected_cb = user_cb;
367 wifi_callbacks.disconnected_user_data = user_data;
371 static void __libnet_disconnected_cb(wifi_error_e result)
373 if (wifi_callbacks.disconnected_cb)
374 wifi_callbacks.disconnected_cb(result, wifi_callbacks.disconnected_user_data);
376 wifi_callbacks.disconnected_cb = NULL;
377 wifi_callbacks.disconnected_user_data = NULL;
380 static void __libnet_evt_cb(net_event_info_t *event_cb, void *user_data)
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;
387 switch (event_cb->Event) {
388 case NET_EVENT_OPEN_RSP:
389 case NET_EVENT_WIFI_WPS_RSP:
392 case NET_EVENT_OPEN_IND:
393 if (_wifi_libnet_check_profile_name_validity(event_cb->ProfileName) != true)
396 result = __libnet_convert_to_ap_error_type(event_cb->Error);
397 WIFI_LOG(WIFI_INFO, "Got Open RSP/IND : %s\n",
398 __libnet_convert_ap_error_type_to_string(result));
401 __libnet_connected_cb(result);
403 switch (event_cb->Error) {
405 WIFI_LOG(WIFI_INFO, "Connection open succeeded\n");
407 if (event_cb->Datalength == sizeof(net_profile_info_t))
408 prof_info_p = (net_profile_info_t*)event_cb->Data;
410 __libnet_state_changed_cb(event_cb->ProfileName, prof_info_p,
411 WIFI_CONNECTION_STATE_CONNECTED);
413 case NET_ERR_ACTIVE_CONNECTION_EXISTS:
414 WIFI_LOG(WIFI_INFO, "Connection already existed\n");
417 WIFI_LOG(WIFI_ERROR, "Connection open failed!\n");
421 if (net_get_profile_info(event_cb->ProfileName, &prof_info) == NET_ERR_NONE)
422 __libnet_state_changed_cb(event_cb->ProfileName, &prof_info,
423 WIFI_CONNECTION_STATE_DISCONNECTED);
425 __libnet_state_changed_cb(event_cb->ProfileName, NULL,
426 WIFI_CONNECTION_STATE_DISCONNECTED);
429 case NET_EVENT_CLOSE_RSP:
432 case NET_EVENT_CLOSE_IND:
433 if (_wifi_libnet_check_profile_name_validity(event_cb->ProfileName) != true)
436 result = __libnet_convert_to_ap_error_type(event_cb->Error);
437 WIFI_LOG(WIFI_INFO, "Got Close RSP/IND : %s\n",
438 __libnet_convert_ap_error_type_to_string(result));
441 __libnet_disconnected_cb(result);
443 switch (event_cb->Error) {
445 WIFI_LOG(WIFI_INFO, "Connection close succeeded!\n");
446 if (net_get_profile_info(event_cb->ProfileName, &prof_info) == NET_ERR_NONE)
447 __libnet_state_changed_cb(event_cb->ProfileName, &prof_info,
448 WIFI_CONNECTION_STATE_DISCONNECTED);
450 __libnet_state_changed_cb(event_cb->ProfileName, NULL,
451 WIFI_CONNECTION_STATE_DISCONNECTED);
454 WIFI_LOG(WIFI_ERROR, "Connection close failed!\n");
459 case NET_EVENT_NET_STATE_IND:
460 if (_wifi_libnet_check_profile_name_validity(event_cb->ProfileName) != true)
463 WIFI_LOG(WIFI_INFO, "Got State changed IND\n");
465 if (event_cb->Datalength != sizeof(net_state_type_t))
468 net_state_type_t *profile_state = (net_state_type_t*)event_cb->Data;
469 wifi_connection_state_e ap_state = _wifi_convert_to_ap_state(*profile_state);
472 "Profile State : %s, profile name : %s\n",
473 __libnet_convert_ap_state_to_string(ap_state),
474 event_cb->ProfileName);
476 if (net_get_profile_info(event_cb->ProfileName, &prof_info) == NET_ERR_NONE)
477 __libnet_state_changed_cb(event_cb->ProfileName, &prof_info, ap_state);
479 __libnet_state_changed_cb(event_cb->ProfileName, NULL, ap_state);
483 case NET_EVENT_WIFI_SCAN_RSP:
484 case NET_EVENT_WIFI_SCAN_IND:
485 WIFI_LOG(WIFI_INFO, "Got wifi scan IND\n");
486 __libnet_scan_cb(event_cb);
488 case NET_EVENT_SPECIFIC_SCAN_RSP:
489 WIFI_LOG(WIFI_INFO, "Got wifi hidden scan RSP\n");
491 case NET_EVENT_SPECIFIC_SCAN_IND:
492 WIFI_LOG(WIFI_INFO, "Got wifi hidden scan IND\n");
493 __libnet_hidden_scan_cb(event_cb);
495 case NET_EVENT_WIFI_POWER_RSP:
498 case NET_EVENT_WIFI_POWER_IND:
499 WIFI_LOG(WIFI_INFO, "Got wifi power IND\n");
500 __libnet_power_on_off_cb(event_cb, is_requested);
503 WIFI_LOG(WIFI_INFO, "Error! Unknown Event\n\n");
507 bool _wifi_libnet_init(void)
511 rv = net_register_client_ext((net_event_cb_t)__libnet_evt_cb, NET_DEVICE_WIFI, NULL);
512 if (rv != NET_ERR_NONE)
518 bool _wifi_libnet_deinit(void)
520 if (net_deregister_client_ext(NET_DEVICE_WIFI) != NET_ERR_NONE)
523 __libnet_clear_profile_list(&profile_iterator);
524 __libnet_clear_profile_list(&hidden_profile_iterator);
525 g_slist_free_full(ap_handle_list, g_free);
526 ap_handle_list = NULL;
527 memset(&wifi_callbacks, 0, sizeof(struct _wifi_cb_s));
532 int _wifi_activate(wifi_activated_cb callback, void* user_data)
536 rv = net_wifi_power_on();
537 if (rv == NET_ERR_NONE) {
538 __libnet_set_activated_cb(callback, user_data);
539 return WIFI_ERROR_NONE;
540 } else if (rv == NET_ERR_INVALID_OPERATION)
541 return WIFI_ERROR_INVALID_OPERATION;
543 return WIFI_ERROR_OPERATION_FAILED;
546 int _wifi_deactivate(wifi_deactivated_cb callback, void* user_data)
550 rv = net_wifi_power_off();
551 if (rv == NET_ERR_NONE) {
552 __libnet_set_deactivated_cb(callback, user_data);
553 return WIFI_ERROR_NONE;
554 } else if (rv == NET_ERR_INVALID_OPERATION)
555 return WIFI_ERROR_INVALID_OPERATION;
557 return WIFI_ERROR_OPERATION_FAILED;
560 bool _wifi_libnet_check_ap_validity(wifi_ap_h ap_h)
565 for (list = ap_handle_list; list; list = list->next)
566 if (ap_h == list->data) return true;
568 for (; i < profile_iterator.count; i++)
569 if (ap_h == &profile_iterator.profiles[i]) return true;
571 for (i = 0; i < hidden_profile_iterator.count; i++)
572 if (ap_h == &hidden_profile_iterator.profiles[i]) return true;
577 void _wifi_libnet_add_to_ap_list(wifi_ap_h ap_h)
579 ap_handle_list = g_slist_append(ap_handle_list, ap_h);
582 void _wifi_libnet_remove_from_ap_list(wifi_ap_h ap_h)
584 ap_handle_list = g_slist_remove(ap_handle_list, ap_h);
588 bool _wifi_libnet_check_profile_name_validity(const char *profile_name)
590 const char *profile_prefix = "/net/connman/service/wifi_";
593 if (profile_name == NULL ||
594 g_str_has_prefix(profile_name, profile_prefix) != TRUE) {
595 WIFI_LOG(WIFI_ERROR, "Error!!! Profile name is invalid\n");
599 while (profile_name[i] != '\0') {
600 if (isgraph(profile_name[i]) == 0) {
601 WIFI_LOG(WIFI_ERROR, "Error!!! Profile name is invalid\n");
610 bool _wifi_libnet_get_wifi_device_state(wifi_device_state_e *device_state)
612 net_tech_info_t tech_info;
614 if (net_get_technology_properties(NET_DEVICE_WIFI, &tech_info) != NET_ERR_NONE) {
615 WIFI_LOG(WIFI_ERROR, "Error!! net_get_technology_properties() failed.\n");
619 if (tech_info.powered)
620 *device_state = WIFI_DEVICE_STATE_ACTIVATED;
622 *device_state = WIFI_DEVICE_STATE_DEACTIVATED;
627 bool _wifi_libnet_get_wifi_state(wifi_connection_state_e* connection_state)
629 net_wifi_state_t wlan_state = 0;
630 net_profile_name_t profile_name;
632 if (net_get_wifi_state(&wlan_state, &profile_name) != NET_ERR_NONE) {
633 WIFI_LOG(WIFI_ERROR, "Error!! net_get_wifi_state() failed.\n");
637 switch (wlan_state) {
640 *connection_state = WIFI_CONNECTION_STATE_DISCONNECTED;
642 case WIFI_CONNECTING:
643 *connection_state = WIFI_CONNECTION_STATE_ASSOCIATION;
646 *connection_state = WIFI_CONNECTION_STATE_CONNECTED;
648 case WIFI_DISCONNECTING:
649 *connection_state = WIFI_CONNECTION_STATE_CONNECTED;
652 WIFI_LOG(WIFI_ERROR, "Error!! Unknown state\n");
659 int _wifi_libnet_get_intf_name(char** name)
661 if (profile_iterator.count == 0)
662 __libnet_update_profile_iterator();
664 if (profile_iterator.count == 0) {
665 WIFI_LOG(WIFI_ERROR, "Error!! There is no AP\n");
666 return WIFI_ERROR_OPERATION_FAILED;
669 *name = g_strdup(profile_iterator.profiles->ProfileInfo.Wlan.net_info.DevName);
671 return WIFI_ERROR_OUT_OF_MEMORY;
673 return WIFI_ERROR_NONE;
676 int _wifi_libnet_scan_request(wifi_scan_finished_cb callback, void* user_data)
679 rv = net_scan_wifi();
681 if (rv == NET_ERR_NONE) {
682 wifi_callbacks.scan_request_cb = callback;
683 wifi_callbacks.scan_request_user_data = user_data;
684 return WIFI_ERROR_NONE;
685 } else if (rv == NET_ERR_INVALID_OPERATION)
686 return WIFI_ERROR_INVALID_OPERATION;
688 return WIFI_ERROR_OPERATION_FAILED;
691 int _wifi_libnet_scan_hidden_ap(const char *essid,
692 wifi_scan_finished_cb callback, void* user_data)
695 rv = net_specific_scan_wifi(essid);
697 if (rv == NET_ERR_NONE) {
698 wifi_callbacks.scan_hidden_ap_cb = callback;
699 wifi_callbacks.scan_hidden_ap_user_data = user_data;
700 return WIFI_ERROR_NONE;
701 } else if (rv == NET_ERR_INVALID_OPERATION)
702 return WIFI_ERROR_INVALID_OPERATION;
704 return WIFI_ERROR_OPERATION_FAILED;
707 int _wifi_libnet_get_connected_profile(wifi_ap_h *ap)
710 wifi_ap_h ap_h = NULL;
712 __libnet_update_profile_iterator();
714 for (;i < profile_iterator.count;i++) {
715 if (profile_iterator.profiles[i].ProfileState == NET_STATE_TYPE_ONLINE ||
716 profile_iterator.profiles[i].ProfileState == NET_STATE_TYPE_READY) {
717 ap_h = (wifi_ap_h)(&profile_iterator.profiles[i]);
723 WIFI_LOG(WIFI_ERROR, "Error!! There is no connected AP.\n");
724 return WIFI_ERROR_NO_CONNECTION;
727 *ap = g_try_malloc0(sizeof(net_profile_info_t));
729 return WIFI_ERROR_OUT_OF_MEMORY;
731 memcpy(*ap, ap_h, sizeof(net_profile_info_t));
733 _wifi_libnet_add_to_ap_list(*ap);
735 return WIFI_ERROR_NONE;
738 bool _wifi_libnet_foreach_found_aps(wifi_found_ap_cb callback, void *user_data)
743 __libnet_update_profile_iterator();
745 if (profile_iterator.count == 0) {
746 WIFI_LOG(WIFI_INFO, "There is no APs.\n");
750 for (;i < profile_iterator.count;i++) {
751 rv = callback((wifi_ap_h)(&profile_iterator.profiles[i]), user_data);
752 if (rv == false) break;
758 bool _wifi_libnet_foreach_found_hidden_aps(wifi_found_ap_cb callback, void *user_data)
763 if (hidden_profile_iterator.count == 0) {
764 WIFI_LOG(WIFI_INFO, "There is no hidden APs.\n");
768 for (;i < hidden_profile_iterator.count;i++) {
769 rv = callback((wifi_ap_h)(&hidden_profile_iterator.profiles[i]), user_data);
770 if (rv == false) break;
776 int _wifi_libnet_open_profile(wifi_ap_h ap_h, wifi_connected_cb callback, void* user_data)
780 net_profile_name_t profile_name;
781 net_profile_info_t *ap_info = ap_h;
783 g_strlcpy(profile_name.ProfileName, ap_info->ProfileName, NET_PROFILE_NAME_LEN_MAX+1);
786 _wifi_libnet_check_profile_name_validity(profile_name.ProfileName);
788 if (valid_profile == true && ap_info->Favourite)
789 rv = net_open_connection_with_profile(profile_name.ProfileName);
790 else if (valid_profile == true &&
791 ap_info->ProfileInfo.Wlan.security_info.sec_mode == WLAN_SEC_MODE_NONE)
792 rv = net_open_connection_with_profile(profile_name.ProfileName);
794 rv = __libnet_connect_with_wifi_info(ap_info);
796 if (rv != NET_ERR_NONE)
797 return WIFI_ERROR_OPERATION_FAILED;
799 __libnet_set_connected_cb(callback, user_data);
801 return WIFI_ERROR_NONE;
804 int _wifi_libnet_close_profile(wifi_ap_h ap_h, wifi_disconnected_cb callback, void* user_data)
806 net_profile_info_t *ap_info = ap_h;
807 net_profile_name_t profile_name;
809 g_strlcpy(profile_name.ProfileName, ap_info->ProfileName, NET_PROFILE_NAME_LEN_MAX+1);
811 if (net_close_connection(profile_name.ProfileName) != NET_ERR_NONE)
812 return WIFI_ERROR_OPERATION_FAILED;
814 __libnet_set_disconnected_cb(callback, user_data);
816 return WIFI_ERROR_NONE;
819 int _wifi_libnet_connect_with_wps(wifi_ap_h ap_h, wifi_connected_cb callback, void* user_data)
821 net_profile_info_t *ap_info = ap_h;
822 net_wifi_wps_info_t wps_info;
823 net_profile_name_t profile_name;
825 memset(&wps_info, 0 , sizeof(net_wifi_wps_info_t));
826 g_strlcpy(profile_name.ProfileName, ap_info->ProfileName, NET_PROFILE_NAME_LEN_MAX+1);
828 wps_info.type = WIFI_WPS_PBC;
830 if (net_wifi_enroll_wps(profile_name.ProfileName, &wps_info) != NET_ERR_NONE)
831 return WIFI_ERROR_OPERATION_FAILED;
833 __libnet_set_connected_cb(callback, user_data);
835 return WIFI_ERROR_NONE;
838 int _wifi_libnet_forget_ap(wifi_ap_h ap)
841 net_profile_name_t profile_name;
842 net_profile_info_t *ap_info = ap;
844 g_strlcpy(profile_name.ProfileName, ap_info->ProfileName, NET_PROFILE_NAME_LEN_MAX+1);
846 rv = net_delete_profile(profile_name.ProfileName);
847 if (rv != NET_ERR_NONE)
848 return WIFI_ERROR_OPERATION_FAILED;
850 return WIFI_ERROR_NONE;
853 int _wifi_set_power_on_off_cb(wifi_device_state_changed_cb callback, void *user_data)
855 if (wifi_callbacks.device_state_cb)
856 return WIFI_ERROR_INVALID_OPERATION;
858 wifi_callbacks.device_state_cb = callback;
859 wifi_callbacks.device_state_user_data = user_data;
861 return WIFI_ERROR_NONE;
864 int _wifi_unset_power_on_off_cb(void)
866 if (wifi_callbacks.device_state_cb == NULL)
867 return WIFI_ERROR_INVALID_OPERATION;
869 wifi_callbacks.device_state_cb = NULL;
870 wifi_callbacks.device_state_user_data = NULL;
872 return WIFI_ERROR_NONE;
875 int _wifi_set_background_scan_cb(wifi_scan_finished_cb callback, void *user_data)
877 if (wifi_callbacks.bg_scan_cb)
878 return WIFI_ERROR_INVALID_OPERATION;
880 wifi_callbacks.bg_scan_cb = callback;
881 wifi_callbacks.bg_scan_user_data = user_data;
883 return WIFI_ERROR_NONE;
886 int _wifi_unset_background_scan_cb(void)
888 if (wifi_callbacks.bg_scan_cb == NULL)
889 return WIFI_ERROR_INVALID_OPERATION;
891 wifi_callbacks.bg_scan_cb = NULL;
892 wifi_callbacks.bg_scan_user_data = NULL;
894 return WIFI_ERROR_NONE;
897 int _wifi_set_connection_state_cb(wifi_connection_state_changed_cb callback, void *user_data)
899 if (wifi_callbacks.connection_state_cb)
900 return WIFI_ERROR_INVALID_OPERATION;
902 wifi_callbacks.connection_state_cb = callback;
903 wifi_callbacks.connection_state_user_data = user_data;
905 return WIFI_ERROR_NONE;
908 int _wifi_unset_connection_state_cb()
910 if (wifi_callbacks.connection_state_cb == NULL)
911 return WIFI_ERROR_INVALID_OPERATION;
913 wifi_callbacks.connection_state_cb = NULL;
914 wifi_callbacks.connection_state_user_data = NULL;
916 return WIFI_ERROR_NONE;
919 int _wifi_update_ap_info(net_profile_info_t *ap_info)
921 if (net_modify_profile(ap_info->ProfileName, ap_info) != NET_ERR_NONE)
922 return WIFI_ERROR_OPERATION_FAILED;
924 return WIFI_ERROR_NONE;