2 * Copyright (c) 2013 Samsung Electronics Co., Ltd.
4 * Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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.
18 #include "net_nfc_debug_internal.h"
19 #include "net_nfc_util_internal.h"
20 #include "net_nfc_util_handover.h"
21 #include "net_nfc_util_ndef_message.h"
22 #include "net_nfc_util_ndef_record.h"
23 #include "net_nfc_server_llcp.h"
24 #include "net_nfc_server_handover_bss.h"
27 static gboolean _net_nfc_handover_bss_process_carrier_record(
30 static void _net_nfc_handover_bss_get_carrier_record(
31 net_nfc_handover_bss_create_context_t *context);
33 static net_nfc_error_e _net_nfc_handover_bss_create_carrier_record(
34 ndef_record_s **record);
37 static net_nfc_error_e _net_nfc_handover_bss_create_config_record(
38 ndef_record_s **record);
40 static int _net_nfc_handover_process_wifi_direct_setup(
41 net_nfc_handover_bss_get_context_t *context);
43 static gboolean _net_nfc_handover_bss_wfd_get_carrier_record(
44 net_nfc_handover_bss_get_context_t *context);
46 static void _net_nfc_wifi_process_error(
48 net_nfc_handover_bss_get_context_t *context);
50 static int _net_nfc_handover_process_wifi_group_setup(
51 net_nfc_handover_bss_get_context_t *context);
56 int _net_nfc_handover_bss_convert_security_type(int value)
61 case 0x0001: /* Open */
62 retval = WIFI_SECURITY_TYPE_NONE;
64 case 0x0002: /* WPA - Personal */
65 retval = WIFI_SECURITY_TYPE_WPA_PSK;
67 case 0x0004: /* WPA - Shared */
68 retval = WIFI_SECURITY_TYPE_WEP;
70 case 0x0008: /* WPA - Enterprise */
71 case 0x0010: /* WPA2 - Enterprise */
72 retval = WIFI_SECURITY_TYPE_EAP;
74 case 0x0020: /* WPA2 - Personal */
75 retval = WIFI_SECURITY_TYPE_WPA_PSK;
78 NFC_ERR("Invalid security Type");
84 int _net_nfc_handover_bss_convert_encryption_type (int enc_type)
89 case 0x0001: /* None */
90 retval = WIFI_ENCRYPTION_TYPE_NONE;
92 case 0x0002: /* WEP */
93 retval = WIFI_ENCRYPTION_TYPE_WEP;
95 case 0x0004: /* TKIP */
96 retval = WIFI_ENCRYPTION_TYPE_TKIP;
98 case 0x0008: /* AES */
99 retval = WIFI_ENCRYPTION_TYPE_AES;
101 case 0x000C: /* TKIP + AES */
102 retval = WIFI_ENCRYPTION_TYPE_TKIP_AES_MIXED;
105 NFC_ERR("Invalid Encryption type");
111 wifi_ap_h _net_nfc_handover_bss_create_ap(net_nfc_carrier_config_s *config)
113 wifi_ap_h ap_handle = NULL;
114 data_s temp = { NULL, 0 };
115 int err = WIFI_ERROR_NONE;
118 err = net_nfc_util_get_carrier_config_property(config,
119 NET_NFC_WIFI_ATTRIBUTE_SSID,(uint16_t *)&temp.length, &temp.buffer);
120 NFC_DBG("SSID = [%s] err %d",temp.buffer, err);
121 err = wifi_ap_create((char*)temp.buffer, &ap_handle);
122 if(err != WIFI_ERROR_NONE)
124 NFC_ERR("Failed to create AP information %d",err);
128 // Sets Authentication Type
129 net_nfc_util_get_carrier_config_property(config,
130 NET_NFC_WIFI_ATTRIBUTE_AUTH_TYPE,(uint16_t *)&temp.length, &temp.buffer);
133 uint16_t securitytype = temp.buffer[0] <<8 | temp.buffer[1];
134 NFC_DBG("wifi_ap_set_security_type %x", securitytype);
135 err = wifi_ap_set_security_type(ap_handle,
136 _net_nfc_handover_bss_convert_security_type(securitytype));
137 if(err != WIFI_ERROR_NONE)
139 NFC_ERR("set security type failed");
145 NFC_ERR("Invalid authentication length");
148 net_nfc_util_get_carrier_config_property(config,
149 NET_NFC_WIFI_ATTRIBUTE_NET_KEY,(uint16_t *)&temp.length, &temp.buffer);
152 err = wifi_ap_set_passphrase(ap_handle,(char*)temp.buffer);
153 if(err != WIFI_ERROR_NONE)
155 NFC_ERR("Failed to set passphrase");
158 // Sets encryption Type
159 net_nfc_util_get_carrier_config_property(config,
160 NET_NFC_WIFI_ATTRIBUTE_ENC_TYPE,(uint16_t *)&temp.length, &temp.buffer);
163 uint16_t enc_type = temp.buffer[0] <<8 | temp.buffer[1];
164 NFC_DBG("wifi_ap_set_encryption_type %x", enc_type);
165 err = wifi_ap_set_encryption_type(ap_handle,
166 _net_nfc_handover_bss_convert_encryption_type(enc_type));
170 NFC_ERR("Invalid Encryption length");
175 if(ap_handle != NULL)
177 wifi_ap_destroy(ap_handle);
182 void _net_nfc_handover_bss_on_wifi_activated(wifi_error_e errorCode,
185 net_nfc_handover_bss_process_context_t *context = user_data;
188 NFC_ERR("Invalid context");
191 if (errorCode == WIFI_ERROR_NONE)
193 NFC_ERR("WIFI activated succesfully");
194 context->result = NET_NFC_OK;
198 NFC_ERR("Failed to activate WIFI");
199 context->result = NET_NFC_OPERATION_FAIL;
203 bool _net_nfc_handover_bss_wifi_for_each_access_point_found(
204 wifi_ap_h ap_handle, void *user_data)
206 data_s temp_ssid = { NULL, 0 };
207 int err = WIFI_ERROR_NONE;
209 net_nfc_handover_bss_process_context_t *context = user_data;
213 NFC_ERR("Invalid context");
217 wifi_ap_clone(&context->ap_handle, ap_handle);
218 err = net_nfc_util_get_carrier_config_property(context->config,
219 NET_NFC_WIFI_ATTRIBUTE_SSID,(uint16_t *)&temp_ssid.length,
222 if(err != WIFI_ERROR_NONE)
224 NFC_ERR("Wifi get carrier config failed");
225 context->result = NET_NFC_OPERATION_FAIL;
226 context->step = NET_NFC_LLCP_STEP_RETURN;
227 g_idle_add(_net_nfc_handover_bss_process_carrier_record, context);
231 wifi_ap_get_essid(ap_handle, &essid);
232 NFC_DBG("Scan Result Ap essid [%s]",essid);
233 if(memcmp(temp_ssid.buffer, essid,temp_ssid.length) == 0)
239 wifi_ap_destroy(context->ap_handle);
240 context->ap_handle = NULL;
245 void _net_nfc_handover_bss_on_wifi_bgscan_completed(
246 wifi_error_e error_code, void* user_data)
249 net_nfc_handover_bss_process_context_t *context = user_data;
253 NFC_ERR("Invalid context");
256 if(error_code != WIFI_ERROR_NONE)
258 NFC_ERR("Wifi scan failed");
259 context->result = NET_NFC_OPERATION_FAIL;
260 context->step = NET_NFC_LLCP_STEP_RETURN;
262 _net_nfc_handover_bss_process_carrier_record,
267 context->result = NET_NFC_OK;
268 context->step = NET_NFC_LLCP_STEP_02;
273 void _net_nfc_handover_bss_on_wifi_scan_completed(wifi_error_e error_code,
276 int err = WIFI_ERROR_NONE;
277 net_nfc_handover_bss_process_context_t *context = user_data;
279 if(error_code != WIFI_ERROR_NONE)
281 NFC_ERR("Wifi scan failed");
282 context->result = NET_NFC_OPERATION_FAIL;
283 context->step = NET_NFC_LLCP_STEP_RETURN;
285 _net_nfc_handover_bss_process_carrier_record,
290 net_nfc_util_create_carrier_config_from_config_record(
291 &context->config, context->carrier);
292 context->ap_handle = NULL;
293 err = wifi_foreach_found_aps(
294 _net_nfc_handover_bss_wifi_for_each_access_point_found,
296 if(err != WIFI_ERROR_NONE)
298 NFC_ERR("wifi_foreach_found_aps failed Err[%x]",err);
299 context->result = NET_NFC_OPERATION_FAIL;
300 context->step = NET_NFC_LLCP_STEP_RETURN;
301 g_idle_add(_net_nfc_handover_bss_process_carrier_record, context);
303 if(context->ap_handle == NULL)
305 wifi_encryption_type_e enc_type;
306 wifi_security_type_e sec_type;
308 context->ap_handle = _net_nfc_handover_bss_create_ap(
310 wifi_ap_get_encryption_type(context->ap_handle, &enc_type);
311 NFC_DBG("Encryption type %x",enc_type);
312 wifi_ap_get_security_type(context->ap_handle, &sec_type);
313 NFC_DBG("Authentication type %x", sec_type);
317 data_s temp = { NULL, 0 };
318 wifi_encryption_type_e enc_type = WIFI_ENCRYPTION_TYPE_NONE;
319 wifi_security_type_e sec_type = WIFI_SECURITY_TYPE_NONE;
321 net_nfc_util_get_carrier_config_property(context->config,
322 NET_NFC_WIFI_ATTRIBUTE_NET_KEY,(uint16_t *)&temp.length,
325 NFC_ERR("Network Key %s",temp.buffer);
327 err = wifi_ap_set_passphrase(context->ap_handle,(char*)temp.buffer);
329 wifi_ap_get_encryption_type(context->ap_handle, &enc_type);
330 NFC_DBG("Encryption type %x",enc_type);
331 wifi_ap_get_security_type(context->ap_handle, &sec_type);
332 NFC_DBG("Authentication type %x", sec_type);
334 context->step = NET_NFC_LLCP_STEP_03;
335 g_idle_add(_net_nfc_handover_bss_process_carrier_record, context);
341 void _net_nfc_handover_bss_on_wifi_connected(wifi_error_e error_code, void* user_data)
343 net_nfc_handover_bss_process_context_t *context = user_data;
347 NFC_ERR("Invalid context");
351 if(error_code == WIFI_ERROR_NONE)
353 NFC_ERR("WIFI Connected succesfully");
354 context->result = NET_NFC_OK;
358 NFC_ERR("Failed to connect WIFI");
359 context->result = NET_NFC_OPERATION_FAIL;
361 context->step = NET_NFC_LLCP_STEP_RETURN;
362 g_idle_add(_net_nfc_handover_bss_process_carrier_record,context);
365 static gboolean _net_nfc_handover_bss_process_carrier_record(
368 NFC_DBG("[%s:%d] START", __func__, __LINE__);
370 net_nfc_handover_bss_process_context_t *context = user_data;
374 NFC_ERR("Invalid context");
375 NFC_ERR("Handover Failed");
379 if (context->result != NET_NFC_OK && context->result != NET_NFC_BUSY)
381 NFC_ERR("context->result is error [%d]", context->result);
382 context->step = NET_NFC_LLCP_STEP_RETURN;
385 switch (context->step)
387 case NET_NFC_LLCP_STEP_01:
389 int err = WIFI_ERROR_NONE;
391 err = wifi_initialize();
392 /* WIFI returns WIFI_ERROR_INVALID_OPERATION in case already
394 if (err == WIFI_ERROR_NONE || err == WIFI_ERROR_INVALID_OPERATION)
398 err = wifi_is_activated(&val);
403 context->result = NET_NFC_OK;
404 wifi_set_background_scan_cb(
405 _net_nfc_handover_bss_on_wifi_bgscan_completed, context);
407 _net_nfc_handover_bss_on_wifi_activated, context);
412 NFC_DBG("Wifi is enabled already, go next step");
413 context->result = NET_NFC_OK;
414 context->step = NET_NFC_LLCP_STEP_02;
415 g_idle_add(_net_nfc_handover_bss_process_carrier_record, context);
420 NFC_ERR("Wifi init failed");
421 context->result = NET_NFC_OPERATION_FAIL;
422 context->step = NET_NFC_LLCP_STEP_RETURN;
423 g_idle_add(_net_nfc_handover_bss_process_carrier_record, context);
428 case NET_NFC_LLCP_STEP_02:
431 int err = WIFI_ERROR_NONE;
433 err = wifi_scan(_net_nfc_handover_bss_on_wifi_scan_completed,
435 if(err != WIFI_ERROR_NONE)
437 NFC_ERR("Wifi scan failed");
438 context->result = NET_NFC_OPERATION_FAIL;
439 context->step = NET_NFC_LLCP_STEP_RETURN;
440 g_idle_add(_net_nfc_handover_bss_process_carrier_record, context);
445 case NET_NFC_LLCP_STEP_03 :
447 NFC_DBG("Connect with WIFI");
448 int err = wifi_connect(context->ap_handle,
449 _net_nfc_handover_bss_on_wifi_connected, context);
450 NFC_DBG("Connect with WIFI err [%x]",err);
451 if(err != WIFI_ERROR_NONE)
453 NFC_ERR("Wifi Connect failed");
454 context->result = NET_NFC_OPERATION_FAIL;
455 context->step = NET_NFC_LLCP_STEP_RETURN;
456 g_idle_add(_net_nfc_handover_bss_process_carrier_record, context);
460 case NET_NFC_LLCP_STEP_RETURN :
462 NFC_DBG("STEP return");
463 if(context->result == NET_NFC_OK)
465 NFC_DBG("Handover completed succesfully");
469 NFC_ERR("Handover Failed");
472 context->cb(context->result,
473 NET_NFC_CONN_HANDOVER_CARRIER_WIFI_BSS,
475 context->user_param);
483 LOGD("[%s:%d] END", __func__, __LINE__);
488 net_nfc_error_e net_nfc_server_handover_bss_process_carrier_record(
489 ndef_record_s *record,
490 net_nfc_server_handover_process_carrier_record_cb cb,
493 net_nfc_error_e result = NET_NFC_OK;
494 net_nfc_handover_bss_process_context_t *context = NULL;
496 _net_nfc_util_alloc_mem(context, sizeof(*context));
500 context->user_param = user_param;
501 context->step = NET_NFC_LLCP_STEP_01;
502 net_nfc_util_create_record(record->TNF, &record->type_s,
503 &record->id_s, &record->payload_s, &context->carrier);
505 g_idle_add(_net_nfc_handover_bss_process_carrier_record, context);
509 result = NET_NFC_ALLOC_FAIL;
516 _net_nfc_wifi_direct_power_changed(int err, wifi_direct_device_state_e device_state, void* user_data)
519 if(device_state == WIFI_DIRECT_DEVICE_STATE_ACTIVATED && err == WIFI_DIRECT_ERROR_NONE)
521 int err = wifi_direct_start_discovery(true, 0);
522 NFC_DBG("wifi direct is enabled %d",err);
526 NFC_ERR("wifi direct power state error");
527 net_nfc_handover_bss_create_context_t* context =
528 (net_nfc_handover_bss_create_context_t*)user_data;
530 context->step = NET_NFC_LLCP_STEP_RETURN;
531 context->result = NET_NFC_OPERATION_FAIL;
532 g_idle_add((GSourceFunc)
533 _net_nfc_handover_bss_wfd_get_carrier_record,
539 _net_nfc_wifi_scan_completed_cb(int err, wifi_direct_discovery_state_e discovery_state, void* user_data)
542 net_nfc_handover_bss_create_context_t* context =
543 (net_nfc_handover_bss_create_context_t*) user_data;
545 if(discovery_state == WIFI_DIRECT_ONLY_LISTEN_STARTED && err == WIFI_DIRECT_ERROR_NONE)
547 g_idle_add((GSourceFunc)
548 _net_nfc_handover_bss_wfd_get_carrier_record,
553 NFC_ERR("wifi scan error");
554 context->step = NET_NFC_LLCP_STEP_RETURN;
555 context->result = NET_NFC_OPERATION_FAIL;
556 g_idle_add((GSourceFunc)
557 _net_nfc_handover_bss_wfd_get_carrier_record,
564 _net_nfc_wifi_direct_connection_changed(wifi_direct_error_e error_code,
565 wifi_direct_connection_state_e connection_state,
566 const char* mac_address,
570 net_nfc_handover_bss_create_context_t* context =
571 (net_nfc_handover_bss_create_context_t*) user_data;
573 if(connection_state == WIFI_DIRECT_GROUP_CREATED
574 && error_code == WIFI_DIRECT_ERROR_NONE)
576 g_idle_add((GSourceFunc)
577 _net_nfc_handover_bss_wfd_get_carrier_record,
582 NFC_ERR("_net_nfc_wifi_direct_connection_changed failed"
583 "[%d] [%d]",error_code,connection_state);
585 context->step = NET_NFC_LLCP_STEP_RETURN;
586 context->result = NET_NFC_OPERATION_FAIL;
587 g_idle_add((GSourceFunc)
588 _net_nfc_handover_bss_wfd_get_carrier_record,
595 static net_nfc_error_e _net_nfc_handover_bss_create_carrier_record(
596 ndef_record_s **record)
598 net_nfc_error_e result;
600 if ((result = net_nfc_util_create_handover_carrier_record(
601 record)) == NET_NFC_OK)
603 NFC_ERR("net_nfc_util_create_ndef_record"
604 "_with_carrier_config [%d]",result);
608 NFC_ERR("net_nfc_util_create_carrier_config failed "
617 /*TO DO : This is a work around and needs to be replaced by WIFI-DIRECT API*/
618 static int _net_nfc_handover_getpassword(uint8_t** password )
624 ret_val = system("wpa_cli -g /var/run/wpa_supplicant/p2p-wlan0-0 p2p_get_passphrase "
625 "> /tmp/nfc_p2p_passphrase.txt");
627 NFC_INFO("system command returned with [%d]",ret_val);
629 FILE *f = fopen("/tmp/nfc_p2p_passphrase.txt","r");
634 readlength = fread(data, 1 , 255, f);
635 for(cnt = 0; cnt < readlength; cnt++)
637 if(data[cnt] == '\n')
642 _net_nfc_util_alloc_mem(*password,(readlength - cnt)+1);
643 memcpy(*password, &data[cnt+1], (readlength - cnt));
645 return (readlength-cnt);
652 static net_nfc_error_e _net_nfc_handover_bss_create_config_record(
653 ndef_record_s **record)
655 char* mac_address = NULL;
657 net_nfc_carrier_config_s *config = NULL;
658 net_nfc_carrier_property_s *cred_config = NULL;
659 net_nfc_error_e result;
661 char *passphrase = NULL;
663 uint8_t *password = NULL;
667 data_s version_data = {NULL,0};
668 data_s net_index_data = {NULL,0};
669 data_s ssid_data = {NULL,0};
670 data_s auth_data = {NULL,0};
671 data_s enc_type = {NULL,0};
672 data_s mac_data = {NULL,0};
674 _net_nfc_util_alloc_mem(version_data.buffer,1);
675 if(version_data.buffer)
677 version_data.length = 1;
678 *version_data.buffer = 0x01;
680 if ((result = net_nfc_util_create_carrier_config(
682 NET_NFC_CONN_HANDOVER_CARRIER_WIFI_BSS)) == NET_NFC_OK)
684 if ((result = net_nfc_util_add_carrier_config_property(
686 NET_NFC_WIFI_ATTRIBUTE_VERSION,
687 version_data.length, version_data.buffer)) != NET_NFC_OK)
689 NFC_ERR("net_nfc_util_add_carrier_config_property failed"
694 _net_nfc_util_free_mem(version_data.buffer);
697 if ((result = net_nfc_util_create_carrier_config_group(
699 NET_NFC_WIFI_ATTRIBUTE_CREDENTIAL)) == NET_NFC_OK)
702 _net_nfc_util_alloc_mem(net_index_data.buffer,1);
703 if(net_index_data.buffer)
705 net_index_data.length = 1;
706 *net_index_data.buffer = 1;
709 net_nfc_util_add_carrier_config_group_property(
711 NET_NFC_WIFI_ATTRIBUTE_NET_INDEX,
712 net_index_data.length, net_index_data.buffer);
714 _net_nfc_util_free_mem(net_index_data.buffer);
717 wifi_direct_get_ssid(&ssid);
719 _net_nfc_util_alloc_mem(ssid_data.buffer,strlen(ssid)+2);
722 ssid_data.length = strlen(ssid);
723 memcpy(ssid_data.buffer,ssid, strlen(ssid));
725 net_nfc_util_add_carrier_config_group_property(
727 NET_NFC_WIFI_ATTRIBUTE_SSID,
728 ssid_data.length, ssid_data.buffer);
730 _net_nfc_util_free_mem(version_data.buffer);
733 _net_nfc_util_alloc_mem(auth_data.buffer,2);
736 auth_data.length = 1;
737 *auth_data.buffer = 20;
739 net_nfc_util_add_carrier_config_group_property(
741 NET_NFC_WIFI_ATTRIBUTE_AUTH_TYPE,
742 auth_data.length, auth_data.buffer);
744 _net_nfc_util_free_mem(auth_data.buffer);
747 _net_nfc_util_alloc_mem(enc_type.buffer,2);
751 *enc_type.buffer = 8;
753 net_nfc_util_add_carrier_config_group_property(
755 NET_NFC_WIFI_ATTRIBUTE_ENC_TYPE,
756 enc_type.length, enc_type.buffer);
758 _net_nfc_util_free_mem(enc_type.buffer);
760 /*TO DO : This is a work around,to be replaced by WIFI-DIRECT API*/
762 pw_length = wifi_direct_get_passphrase(&passphrase);
763 NFC_DBG("wifi_direct_get_passphrase[%s]", passphrase);
765 pw_length = _net_nfc_handover_getpassword(&password);
766 NFC_DBG("_net_nfc_handover_getpassword[%s]", password);
769 net_nfc_util_add_carrier_config_group_property(
771 NET_NFC_WIFI_ATTRIBUTE_NET_KEY,
772 pw_length, password);
774 _net_nfc_util_free_mem(password);
776 wifi_direct_get_mac_address(&mac_address);
778 _net_nfc_util_alloc_mem(mac_data.buffer,strlen(mac_address)+2);
781 mac_data.length = strlen(mac_address);
782 memcpy(mac_data.buffer,mac_address,strlen(mac_address));
784 if ((result = net_nfc_util_add_carrier_config_group_property(
786 NET_NFC_WIFI_ATTRIBUTE_MAC_ADDR,
787 mac_data.length, mac_data.buffer)) != NET_NFC_OK)
789 NFC_ERR("net_nfc_util_add_carrier"
790 "_config_property failed"
795 _net_nfc_util_free_mem(mac_data.buffer);
798 net_nfc_util_append_carrier_config_group(config, cred_config);
800 result = net_nfc_util_create_ndef_record_with_carrier_config(
804 if (result != NET_NFC_OK)
806 NFC_ERR("net_nfc_util_create_ndef_record"
807 "_with_carrier_config failed"
816 static void _net_nfc_handover_bss_get_carrier_record(
817 net_nfc_handover_bss_create_context_t *context)
819 LOGD("[%s:%d] START", __func__, __LINE__);
824 if (context->result != NET_NFC_OK && context->result != NET_NFC_BUSY)
826 NFC_ERR("context->result is error [%d]", context->result);
829 context->result = NET_NFC_OK;
831 context->cps = NET_NFC_CONN_HANDOVER_CARRIER_ACTIVATE;
833 /* Create carrier record */
834 context->result =_net_nfc_handover_bss_create_carrier_record(
836 if (context->result!= NET_NFC_OK)
838 NFC_ERR("create_bss_config_record failed"
839 " [%d]", context->result);
842 /* complete and return to upper step */
843 context->cb(context->result,
848 context->user_param);
853 static void _net_nfc_wifi_process_error(
855 net_nfc_handover_bss_get_context_t *context)
857 NFC_ERR("_net_nfc_wifi_process_error - [%d]",error);
859 context->step = NET_NFC_LLCP_STEP_RETURN;
860 context->result = NET_NFC_OPERATION_FAIL;
862 g_idle_add((GSourceFunc)
863 _net_nfc_handover_bss_wfd_get_carrier_record,
869 static int _net_nfc_handover_process_wifi_direct_setup(
870 net_nfc_handover_bss_get_context_t *context)
872 int err = WIFI_DIRECT_ERROR_NONE;
873 err = wifi_direct_initialize();
874 if(err != WIFI_DIRECT_ERROR_NONE)
876 NFC_ERR("wifi_direct_initialize err value %d",err);
880 err = wifi_direct_set_device_state_changed_cb(
881 _net_nfc_wifi_direct_power_changed,
884 if(err != WIFI_DIRECT_ERROR_NONE)
886 NFC_ERR("wifi_direct_set_device_state_changed_cb err value %d",err);
890 err = wifi_direct_set_discovery_state_changed_cb(
891 _net_nfc_wifi_scan_completed_cb,
894 if(err != WIFI_DIRECT_ERROR_NONE)
896 NFC_ERR("wifi_direct_set_discovery_state_changed_cb err value %d",err);
900 err = wifi_direct_set_connection_state_changed_cb(
901 _net_nfc_wifi_direct_connection_changed,
904 if (err != WIFI_DIRECT_ERROR_NONE)
906 NFC_ERR("wifi_direct_set_connection_state_changed_cb err value %d",err);
911 context->step = NET_NFC_LLCP_STEP_02;
912 context->result = NET_NFC_OK;
914 wifi_direct_state_e status = WIFI_DIRECT_STATE_DEACTIVATED;
916 err = wifi_direct_get_state(&status);
917 NFC_ERR("status value %d",status);
919 if (status != WIFI_DIRECT_STATE_ACTIVATED)
921 wifi_direct_activate();
925 NFC_DBG("wifi direct is enabled already");
928 g_idle_add((GSourceFunc)
929 _net_nfc_handover_bss_wfd_get_carrier_record,
932 return WIFI_DIRECT_ERROR_NONE;
937 static int _net_nfc_handover_process_wifi_group_setup(
938 net_nfc_handover_bss_get_context_t *context)
940 int err = WIFI_DIRECT_ERROR_NONE;
941 bool group_owner = false;
943 err = wifi_direct_is_group_owner(&group_owner);
944 NFC_ERR("error value %d",err);
946 context->step = NET_NFC_LLCP_STEP_03;
947 context->result = NET_NFC_OK;
949 if(err == WIFI_DIRECT_ERROR_NONE)
951 if(group_owner == true)
953 NFC_DBG("Already group owner, continue next step");
954 g_idle_add((GSourceFunc)
955 _net_nfc_handover_bss_wfd_get_carrier_record,
960 err = wifi_direct_create_group();
961 if (err != WIFI_DIRECT_ERROR_NONE)
963 NFC_ERR("wifi_direct_create_group failed [%d]",err);
970 NFC_ERR("wifi_direct_is_group_owner failed [%d]",err);
977 static gboolean _net_nfc_handover_bss_wfd_get_carrier_record(
978 net_nfc_handover_bss_get_context_t *context)
980 LOGD("[%s:%d] START", __func__, __LINE__);
983 NFC_ERR("Invalid context");
989 int err = WIFI_DIRECT_ERROR_NONE;
991 if (context->result != NET_NFC_OK && context->result != NET_NFC_BUSY)
993 NFC_ERR("context->result is error"
994 " [%d]", context->result);
996 context->step = NET_NFC_LLCP_STEP_RETURN;
999 switch (context->step)
1001 case NET_NFC_LLCP_STEP_01 :
1002 NFC_ERR("NET_NFC_LLCP_STEP_01");
1003 err = _net_nfc_handover_process_wifi_direct_setup(context);
1004 if(err != WIFI_DIRECT_ERROR_NONE)
1006 NFC_ERR("_net_nfc_handover_process_wifi_direct_setup failed");
1007 _net_nfc_wifi_process_error(err,context);
1010 case NET_NFC_LLCP_STEP_02:
1011 NFC_ERR("NET_NFC_LLCP_STEP_02");
1012 err = _net_nfc_handover_process_wifi_group_setup(context);
1013 if(err != WIFI_DIRECT_ERROR_NONE)
1015 NFC_ERR("_net_nfc_handover_process_wifi_group_setup failed");
1016 _net_nfc_wifi_process_error(err,context);
1019 case NET_NFC_LLCP_STEP_03 :
1020 NFC_ERR("NET_NFC_LLCP_STEP_03");
1021 context->step = NET_NFC_LLCP_STEP_RETURN;
1023 /* append config to ndef message */
1024 context->result =_net_nfc_handover_bss_create_config_record(
1026 if (context->result != NET_NFC_OK)
1028 NFC_ERR("_net_nfc_handover_bss_create_config_record failed"
1029 "[%d]", context->result);
1032 g_idle_add((GSourceFunc)
1033 _net_nfc_handover_bss_wfd_get_carrier_record,
1037 case NET_NFC_LLCP_STEP_RETURN :
1038 NFC_ERR("NET_NFC_LLCP_STEP_RETURN");
1039 /* unregister current callback */
1040 wifi_direct_unset_connection_state_changed_cb();
1041 wifi_direct_unset_device_state_changed_cb();
1042 wifi_direct_unset_discovery_state_changed_cb();
1043 wifi_direct_deinitialize();
1045 /* complete and return to upper step */
1046 context->cb(context->result,
1049 context->aux_data_count,
1051 context->user_param);
1058 LOGD("[%s:%d] END", __func__, __LINE__);
1064 net_nfc_error_e net_nfc_server_handover_bss_get_carrier_record(
1065 net_nfc_server_handover_get_carrier_record_cb cb,
1068 net_nfc_error_e result = NET_NFC_OK;
1069 net_nfc_handover_bss_create_context_t *context = NULL;
1071 _net_nfc_util_alloc_mem(context, sizeof(*context));
1072 if (context != NULL)
1075 context->user_param = user_param;
1076 context->step = NET_NFC_LLCP_STEP_01;
1078 _net_nfc_handover_bss_get_carrier_record(context);
1083 result = NET_NFC_ALLOC_FAIL;
1090 net_nfc_error_e net_nfc_server_handover_bss_wfd_get_carrier_record(
1091 net_nfc_server_handover_get_carrier_record_cb cb,
1094 net_nfc_handover_bss_get_context_t *context = NULL;
1096 _net_nfc_util_alloc_mem(context, sizeof(*context));
1097 if (context != NULL)
1100 context->user_param = user_param;
1101 context->step = NET_NFC_LLCP_STEP_01;
1102 context->cps = NET_NFC_CONN_HANDOVER_CARRIER_ACTIVATE;
1104 g_idle_add((GSourceFunc)_net_nfc_handover_bss_wfd_get_carrier_record,
1111 return NET_NFC_ALLOC_FAIL;