99e68fbac67f6146bba72ce98c008baba498c808
[platform/core/connectivity/nfc-manager-neard.git] / daemon / net_nfc_server_handover_bss.c
1 /*
2  * Copyright (c) 2013 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  *    http://floralicense.org/license/
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 #include <glib.h>
17
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"
25 #include <execinfo.h>
26
27 static gboolean _net_nfc_handover_bss_process_carrier_record(
28                 gpointer user_data);
29
30 static void _net_nfc_handover_bss_get_carrier_record(
31                 net_nfc_handover_bss_create_context_t *context);
32
33 static net_nfc_error_e _net_nfc_handover_bss_create_carrier_record(
34                 ndef_record_s **record);
35
36 #ifdef TARGET
37 static net_nfc_error_e _net_nfc_handover_bss_create_config_record(
38                 ndef_record_s **record);
39
40 static int _net_nfc_handover_process_wifi_direct_setup(
41                 net_nfc_handover_bss_get_context_t *context);
42
43 static gboolean _net_nfc_handover_bss_wfd_get_carrier_record(
44                 net_nfc_handover_bss_get_context_t *context);
45
46 static void _net_nfc_wifi_process_error(
47                 int error,
48                 net_nfc_handover_bss_get_context_t *context);
49
50 static int _net_nfc_handover_process_wifi_group_setup(
51                 net_nfc_handover_bss_get_context_t *context);
52
53 #endif
54
55
56 int _net_nfc_handover_bss_convert_security_type(int value)
57 {
58         int retval = 0;
59         switch (value)
60         {
61         case 0x0001:            /* Open */
62                 retval = WIFI_SECURITY_TYPE_NONE;
63                 break;
64         case 0x0002:            /* WPA - Personal */
65                 retval = WIFI_SECURITY_TYPE_WPA_PSK;
66                 break;
67         case 0x0004:            /* WPA - Shared */
68                 retval = WIFI_SECURITY_TYPE_WEP;
69                 break;
70         case 0x0008:            /* WPA - Enterprise */
71         case 0x0010:            /* WPA2 - Enterprise */
72                 retval = WIFI_SECURITY_TYPE_EAP;
73                 break;
74         case 0x0020:            /* WPA2 - Personal */
75                 retval = WIFI_SECURITY_TYPE_WPA_PSK;
76                 break;
77         default:
78                 NFC_ERR("Invalid security Type");
79                 retval = 0;
80         }
81         return retval;
82 }
83
84 int _net_nfc_handover_bss_convert_encryption_type (int enc_type)
85 {
86         int retval = 0;
87         switch (enc_type)
88         {
89         case 0x0001:            /* None */
90                 retval = WIFI_ENCRYPTION_TYPE_NONE;
91                 break;
92         case 0x0002:            /* WEP */
93                 retval = WIFI_ENCRYPTION_TYPE_WEP;
94                 break;
95         case 0x0004:            /* TKIP */
96                 retval = WIFI_ENCRYPTION_TYPE_TKIP;
97                 break;
98         case 0x0008:            /* AES */
99                 retval = WIFI_ENCRYPTION_TYPE_AES;
100                 break;
101         case 0x000C:            /* TKIP + AES */
102                 retval = WIFI_ENCRYPTION_TYPE_TKIP_AES_MIXED;
103                 break;
104         default:
105                 NFC_ERR("Invalid Encryption type");
106                 retval = 0;
107         }
108         return retval;
109 }
110
111 wifi_ap_h _net_nfc_handover_bss_create_ap(net_nfc_carrier_config_s *config)
112 {
113         wifi_ap_h ap_handle = NULL;
114         data_s temp = { NULL, 0 };
115         int err = WIFI_ERROR_NONE;
116
117         // Sets SSID
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)
123         {
124                 NFC_ERR("Failed to create AP information %d",err);
125                 goto error;
126         }
127
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);
131         if(temp.length == 2)
132         {
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)
138                 {
139                         NFC_ERR("set security type failed");
140                         goto error;
141                 }
142         }
143         else
144         {
145                 NFC_ERR("Invalid authentication length");
146                 goto error;
147         }
148         net_nfc_util_get_carrier_config_property(config,
149                         NET_NFC_WIFI_ATTRIBUTE_NET_KEY,(uint16_t *)&temp.length, &temp.buffer);
150
151         // Sets Network Key
152         err = wifi_ap_set_passphrase(ap_handle,(char*)temp.buffer);
153         if(err != WIFI_ERROR_NONE)
154         {
155                 NFC_ERR("Failed to set passphrase");
156                 goto error;
157         }
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);
161         if(temp.length == 2)
162         {
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));
167         }
168         else
169         {
170                 NFC_ERR("Invalid Encryption length");
171                 goto error;
172         }
173         return ap_handle;
174 error:
175         if(ap_handle != NULL)
176         {
177                 wifi_ap_destroy(ap_handle);
178         }
179         return NULL;
180 }
181
182 void _net_nfc_handover_bss_on_wifi_activated(wifi_error_e errorCode,
183                 void* user_data)
184 {
185         net_nfc_handover_bss_process_context_t *context = user_data;
186         if(context == NULL)
187         {
188                 NFC_ERR("Invalid context");
189                 return;
190         }
191         if (errorCode == WIFI_ERROR_NONE)
192         {
193                 NFC_ERR("WIFI activated succesfully");
194                 context->result = NET_NFC_OK;
195         }
196         else
197         {
198                 NFC_ERR("Failed to activate WIFI");
199                 context->result = NET_NFC_OPERATION_FAIL;
200         }
201 }
202
203 bool _net_nfc_handover_bss_wifi_for_each_access_point_found(
204                 wifi_ap_h ap_handle, void *user_data)
205 {
206         data_s temp_ssid = { NULL, 0 };
207         int err = WIFI_ERROR_NONE;
208         char* essid = NULL;
209         net_nfc_handover_bss_process_context_t *context = user_data;
210
211         if(context == NULL)
212         {
213                 NFC_ERR("Invalid context");
214                 return false;
215         }
216
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,
220                         &temp_ssid.buffer);
221
222         if(err != WIFI_ERROR_NONE)
223         {
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);
228                 return false;
229         }
230
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)
234         {
235                 return false;
236         }
237         else
238         {
239                 wifi_ap_destroy(context->ap_handle);
240                 context->ap_handle = NULL;
241         }
242         return true;
243 }
244
245 void _net_nfc_handover_bss_on_wifi_bgscan_completed(
246                 wifi_error_e error_code, void* user_data)
247 {
248
249         net_nfc_handover_bss_process_context_t *context = user_data;
250
251         if(context == NULL)
252         {
253                 NFC_ERR("Invalid context");
254                 return;
255         }
256         if(error_code != WIFI_ERROR_NONE)
257         {
258                 NFC_ERR("Wifi scan failed");
259                 context->result = NET_NFC_OPERATION_FAIL;
260                 context->step = NET_NFC_LLCP_STEP_RETURN;
261                 g_idle_add(
262                                 _net_nfc_handover_bss_process_carrier_record,
263                                 context);
264         }
265         else
266         {
267                 context->result = NET_NFC_OK;
268                 context->step = NET_NFC_LLCP_STEP_02;
269
270         }
271 }
272
273 void _net_nfc_handover_bss_on_wifi_scan_completed(wifi_error_e error_code,
274                 void* user_data)
275 {
276         int err = WIFI_ERROR_NONE;
277         net_nfc_handover_bss_process_context_t *context = user_data;
278
279         if(error_code != WIFI_ERROR_NONE)
280         {
281                 NFC_ERR("Wifi scan failed");
282                 context->result = NET_NFC_OPERATION_FAIL;
283                 context->step = NET_NFC_LLCP_STEP_RETURN;
284                 g_idle_add(
285                                 _net_nfc_handover_bss_process_carrier_record,
286                                 context);
287         }
288         else
289         {
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,
295                                 context);
296                 if(err != WIFI_ERROR_NONE)
297                 {
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);
302                 }
303                 if(context->ap_handle == NULL)
304                 {
305                         wifi_encryption_type_e enc_type;
306                         wifi_security_type_e sec_type;
307
308                         context->ap_handle = _net_nfc_handover_bss_create_ap(
309                                         context->config);
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);
314                 }
315                 else
316                 {
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;
320                         //set passkey
321                         net_nfc_util_get_carrier_config_property(context->config,
322                                         NET_NFC_WIFI_ATTRIBUTE_NET_KEY,(uint16_t *)&temp.length,
323                                         &temp.buffer);
324
325                         NFC_ERR("Network Key %s",temp.buffer);
326                         // Sets Network Key
327                         err = wifi_ap_set_passphrase(context->ap_handle,(char*)temp.buffer);
328
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);
333                 }
334                 context->step = NET_NFC_LLCP_STEP_03;
335                 g_idle_add(_net_nfc_handover_bss_process_carrier_record, context);
336
337         }
338
339 }
340
341 void _net_nfc_handover_bss_on_wifi_connected(wifi_error_e error_code, void* user_data)
342 {
343         net_nfc_handover_bss_process_context_t *context = user_data;
344
345         if(context == NULL)
346         {
347                 NFC_ERR("Invalid context");
348                 return;
349         }
350
351         if(error_code == WIFI_ERROR_NONE)
352         {
353                 NFC_ERR("WIFI Connected succesfully");
354                 context->result = NET_NFC_OK;
355         }
356         else
357         {
358                 NFC_ERR("Failed to connect WIFI");
359                 context->result = NET_NFC_OPERATION_FAIL;
360         }
361         context->step = NET_NFC_LLCP_STEP_RETURN;
362         g_idle_add(_net_nfc_handover_bss_process_carrier_record,context);
363 }
364
365 static gboolean _net_nfc_handover_bss_process_carrier_record(
366                 gpointer user_data)
367 {
368         NFC_DBG("[%s:%d] START", __func__, __LINE__);
369
370         net_nfc_handover_bss_process_context_t *context = user_data;
371
372         if(context == NULL)
373         {
374                 NFC_ERR("Invalid context");
375                 NFC_ERR("Handover Failed");
376                 return FALSE;
377         }
378
379         if (context->result != NET_NFC_OK && context->result != NET_NFC_BUSY)
380         {
381                 NFC_ERR("context->result is error [%d]", context->result);
382                 context->step = NET_NFC_LLCP_STEP_RETURN;
383         }
384
385         switch (context->step)
386         {
387         case NET_NFC_LLCP_STEP_01:
388                 {
389                         int err = WIFI_ERROR_NONE;
390                         NFC_DBG("STEP [1]");
391                         err = wifi_initialize();
392                         /* WIFI returns WIFI_ERROR_INVALID_OPERATION in case already
393                          *  initialized*/
394                         if (err == WIFI_ERROR_NONE || err == WIFI_ERROR_INVALID_OPERATION)
395                         {
396                                 bool val = false;
397
398                                 err = wifi_is_activated(&val);
399                                 /* next step */
400
401                                 if (val == false)
402                                 {
403                                         context->result = NET_NFC_OK;
404                                         wifi_set_background_scan_cb(
405                                                         _net_nfc_handover_bss_on_wifi_bgscan_completed, context);
406                                         wifi_activate(
407                                                         _net_nfc_handover_bss_on_wifi_activated, context);
408                                 }
409                                 else
410                                 {
411                                         /* do next step */
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);
416                                 }
417                         }
418                         else
419                         {
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);
424                         }
425                 }
426                 break;
427
428         case NET_NFC_LLCP_STEP_02:
429                 {
430
431                         int err = WIFI_ERROR_NONE;
432                         NFC_DBG("STEP [2]");
433                         err = wifi_scan(_net_nfc_handover_bss_on_wifi_scan_completed,
434                                         context);
435                         if(err != WIFI_ERROR_NONE)
436                         {
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);
441                         }
442                 }
443
444                 break;
445         case NET_NFC_LLCP_STEP_03 :
446                 {
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)
452                         {
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);
457                         }
458                 }
459                 break;
460         case NET_NFC_LLCP_STEP_RETURN :
461                 {
462                         NFC_DBG("STEP return");
463                         if(context->result == NET_NFC_OK)
464                         {
465                                 NFC_DBG("Handover completed succesfully");
466                         }
467                         else
468                         {
469                                 NFC_ERR("Handover Failed");
470                         }
471                         wifi_deinitialize();
472                         context->cb(context->result,
473                                         NET_NFC_CONN_HANDOVER_CARRIER_WIFI_BSS,
474                                         NULL,
475                                         context->user_param);
476                 }
477                 break;
478
479         default :
480                 break;
481         }
482
483         LOGD("[%s:%d] END", __func__, __LINE__);
484
485         return 0;
486 }
487
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,
491                 void *user_param)
492 {
493         net_nfc_error_e result = NET_NFC_OK;
494         net_nfc_handover_bss_process_context_t *context = NULL;
495
496         _net_nfc_util_alloc_mem(context, sizeof(*context));
497         if (context != NULL)
498         {
499                 context->cb = cb;
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);
504
505                 g_idle_add(_net_nfc_handover_bss_process_carrier_record, context);
506         }
507         else
508         {
509                 result = NET_NFC_ALLOC_FAIL;
510         }
511         return result;
512 }
513
514 #ifdef TARGET
515 void
516 _net_nfc_wifi_direct_power_changed(int err, wifi_direct_device_state_e device_state, void* user_data)
517 {
518
519         if(device_state == WIFI_DIRECT_DEVICE_STATE_ACTIVATED && err == WIFI_DIRECT_ERROR_NONE)
520         {
521                 int err = wifi_direct_start_discovery(true, 0);
522                 NFC_DBG("wifi direct is enabled %d",err);
523         }
524         else
525         {
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;
529
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,
534                                 (gpointer)context);
535         }
536 }
537
538 void
539 _net_nfc_wifi_scan_completed_cb(int err, wifi_direct_discovery_state_e discovery_state, void* user_data)
540 {
541
542         net_nfc_handover_bss_create_context_t* context =
543                 (net_nfc_handover_bss_create_context_t*) user_data;
544
545         if(discovery_state == WIFI_DIRECT_ONLY_LISTEN_STARTED && err == WIFI_DIRECT_ERROR_NONE)
546         {
547                 g_idle_add((GSourceFunc)
548                                 _net_nfc_handover_bss_wfd_get_carrier_record,
549                                 (gpointer)context);
550         }
551         else
552         {
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,
558                                 (gpointer)context);
559         }
560
561 }
562
563 void
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,
567                 void *user_data)
568 {
569
570         net_nfc_handover_bss_create_context_t* context =
571                 (net_nfc_handover_bss_create_context_t*) user_data;
572
573         if(connection_state == WIFI_DIRECT_GROUP_CREATED
574                         && error_code == WIFI_DIRECT_ERROR_NONE)
575         {
576                 g_idle_add((GSourceFunc)
577                                 _net_nfc_handover_bss_wfd_get_carrier_record,
578                                 (gpointer)context);
579         }
580         else
581         {
582                 NFC_ERR("_net_nfc_wifi_direct_connection_changed failed"
583                                 "[%d] [%d]",error_code,connection_state);
584
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,
589                                 (gpointer)context);
590         }
591 }
592
593 #endif
594
595 static net_nfc_error_e _net_nfc_handover_bss_create_carrier_record(
596                 ndef_record_s **record)
597 {
598         net_nfc_error_e result;
599
600         if ((result = net_nfc_util_create_handover_carrier_record(
601                                         record)) == NET_NFC_OK)
602         {
603                 NFC_ERR("net_nfc_util_create_ndef_record"
604                                 "_with_carrier_config [%d]",result);
605         }
606         else
607         {
608                 NFC_ERR("net_nfc_util_create_carrier_config failed "
609                                 "[%d]", result);
610         }
611
612         return result;
613 }
614
615
616 #ifdef TARGET
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 )
619 {
620
621         char data[256];
622         int ret_val;
623
624         ret_val = system("wpa_cli -g /var/run/wpa_supplicant/p2p-wlan0-0 p2p_get_passphrase "
625                         "> /tmp/nfc_p2p_passphrase.txt");
626
627         NFC_INFO("system command returned with [%d]",ret_val);
628
629         FILE *f = fopen("/tmp/nfc_p2p_passphrase.txt","r");
630         if(f != NULL)
631         {
632                 int readlength;
633                 int cnt;
634                 readlength = fread(data, 1 , 255, f);
635                 for(cnt = 0; cnt < readlength; cnt++)
636                 {
637                         if(data[cnt] == '\n')
638                         {
639                                 break;
640                         }
641                 }
642                 _net_nfc_util_alloc_mem(*password,(readlength - cnt)+1);
643                 memcpy(*password, &data[cnt+1], (readlength - cnt));
644                 fclose(f);
645                 return (readlength-cnt);
646         }
647         else
648                 return 0;
649 }
650
651
652 static net_nfc_error_e _net_nfc_handover_bss_create_config_record(
653                 ndef_record_s **record)
654 {
655         char* mac_address = NULL;
656         char* ssid = NULL;
657         net_nfc_carrier_config_s *config = NULL;
658         net_nfc_carrier_property_s *cred_config = NULL;
659         net_nfc_error_e result;
660 #if 0
661         char *passphrase = NULL;
662 #else
663         uint8_t *password = NULL;
664 #endif
665         int pw_length = 0;
666
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};
673
674         _net_nfc_util_alloc_mem(version_data.buffer,1);
675         if(version_data.buffer)
676         {
677                 version_data.length = 1;
678                 *version_data.buffer = 0x01;
679
680                 if ((result = net_nfc_util_create_carrier_config(
681                                                 &config,
682                                                 NET_NFC_CONN_HANDOVER_CARRIER_WIFI_BSS)) == NET_NFC_OK)
683                 {
684                         if ((result = net_nfc_util_add_carrier_config_property(
685                                                         config,
686                                                         NET_NFC_WIFI_ATTRIBUTE_VERSION,
687                                                         version_data.length, version_data.buffer)) != NET_NFC_OK)
688                         {
689                                 NFC_ERR("net_nfc_util_add_carrier_config_property failed"
690                                                 "[%d]", result);
691                         }
692                 }
693
694                 _net_nfc_util_free_mem(version_data.buffer);
695         }
696
697         if ((result = net_nfc_util_create_carrier_config_group(
698                                         &cred_config,
699                                         NET_NFC_WIFI_ATTRIBUTE_CREDENTIAL)) == NET_NFC_OK)
700         {
701
702                 _net_nfc_util_alloc_mem(net_index_data.buffer,1);
703                 if(net_index_data.buffer)
704                 {
705                         net_index_data.length = 1;
706                         *net_index_data.buffer = 1;
707
708
709                         net_nfc_util_add_carrier_config_group_property(
710                                         cred_config,
711                                         NET_NFC_WIFI_ATTRIBUTE_NET_INDEX,
712                                         net_index_data.length, net_index_data.buffer);
713
714                         _net_nfc_util_free_mem(net_index_data.buffer);
715                 }
716
717                 wifi_direct_get_ssid(&ssid);
718
719                 _net_nfc_util_alloc_mem(ssid_data.buffer,strlen(ssid)+2);
720                 if(ssid_data.buffer)
721                 {
722                         ssid_data.length = strlen(ssid);
723                         memcpy(ssid_data.buffer,ssid, strlen(ssid));
724
725                         net_nfc_util_add_carrier_config_group_property(
726                                         cred_config,
727                                         NET_NFC_WIFI_ATTRIBUTE_SSID,
728                                         ssid_data.length, ssid_data.buffer);
729
730                         _net_nfc_util_free_mem(version_data.buffer);
731                 }
732
733                 _net_nfc_util_alloc_mem(auth_data.buffer,2);
734                 if(auth_data.buffer)
735                 {
736                         auth_data.length = 1;
737                         *auth_data.buffer = 20;
738
739                         net_nfc_util_add_carrier_config_group_property(
740                                         cred_config,
741                                         NET_NFC_WIFI_ATTRIBUTE_AUTH_TYPE,
742                                         auth_data.length, auth_data.buffer);
743
744                         _net_nfc_util_free_mem(auth_data.buffer);
745                 }
746
747                 _net_nfc_util_alloc_mem(enc_type.buffer,2);
748                 if(enc_type.buffer)
749                 {
750                         enc_type.length = 1;
751                         *enc_type.buffer = 8;
752
753                         net_nfc_util_add_carrier_config_group_property(
754                                         cred_config,
755                                         NET_NFC_WIFI_ATTRIBUTE_ENC_TYPE,
756                                         enc_type.length, enc_type.buffer);
757
758                         _net_nfc_util_free_mem(enc_type.buffer);
759                 }
760                 /*TO DO : This is a work around,to be replaced by WIFI-DIRECT API*/
761 #if 0
762                 pw_length = wifi_direct_get_passphrase(&passphrase);
763                 NFC_DBG("wifi_direct_get_passphrase[%s]", passphrase);
764 #else
765                 pw_length = _net_nfc_handover_getpassword(&password);
766                 NFC_DBG("_net_nfc_handover_getpassword[%s]", password);
767 #endif
768
769                 net_nfc_util_add_carrier_config_group_property(
770                                 cred_config,
771                                 NET_NFC_WIFI_ATTRIBUTE_NET_KEY,
772                                 pw_length, password);
773
774                 _net_nfc_util_free_mem(password);
775
776                 wifi_direct_get_mac_address(&mac_address);
777
778                 _net_nfc_util_alloc_mem(mac_data.buffer,strlen(mac_address)+2);
779                 if(mac_data.buffer)
780                 {
781                         mac_data.length = strlen(mac_address);
782                         memcpy(mac_data.buffer,mac_address,strlen(mac_address));
783
784                         if ((result = net_nfc_util_add_carrier_config_group_property(
785                                                         cred_config,
786                                                         NET_NFC_WIFI_ATTRIBUTE_MAC_ADDR,
787                                                         mac_data.length, mac_data.buffer)) != NET_NFC_OK)
788                         {
789                                 NFC_ERR("net_nfc_util_add_carrier"
790                                                 "_config_property failed"
791                                                 "[%d]", result);
792                         }
793                 }
794
795                 _net_nfc_util_free_mem(mac_data.buffer);
796
797         }
798         net_nfc_util_append_carrier_config_group(config, cred_config);
799
800         result = net_nfc_util_create_ndef_record_with_carrier_config(
801                         record,
802                         config);
803
804         if (result != NET_NFC_OK)
805         {
806                 NFC_ERR("net_nfc_util_create_ndef_record"
807                                 "_with_carrier_config failed"
808                                 "[%d]",result);
809         }
810
811         return result;
812 }
813 #endif
814
815
816 static void _net_nfc_handover_bss_get_carrier_record(
817                 net_nfc_handover_bss_create_context_t *context)
818 {
819         LOGD("[%s:%d] START", __func__, __LINE__);
820
821         if(context != NULL)
822         {
823
824                 if (context->result != NET_NFC_OK && context->result != NET_NFC_BUSY)
825                 {
826                         NFC_ERR("context->result is error [%d]", context->result);
827                 }
828
829                 context->result = NET_NFC_OK;
830
831                 context->cps = NET_NFC_CONN_HANDOVER_CARRIER_ACTIVATE;
832
833                 /* Create carrier record */
834                 context->result =_net_nfc_handover_bss_create_carrier_record(
835                                 &context->carrier);
836                 if (context->result!= NET_NFC_OK)
837                 {
838                         NFC_ERR("create_bss_config_record failed"
839                                         " [%d]", context->result);
840                 }
841
842                 /* complete and return to upper step */
843                 context->cb(context->result,
844                                 context->cps,
845                                 context->carrier,
846                                 0,
847                                 NULL,
848                                 context->user_param);
849         }
850 }
851
852 #ifdef TARGET
853 static void _net_nfc_wifi_process_error(
854                 int error,
855                 net_nfc_handover_bss_get_context_t *context)
856 {
857         NFC_ERR("_net_nfc_wifi_process_error - [%d]",error);
858
859         context->step = NET_NFC_LLCP_STEP_RETURN;
860         context->result = NET_NFC_OPERATION_FAIL;
861
862         g_idle_add((GSourceFunc)
863                         _net_nfc_handover_bss_wfd_get_carrier_record,
864                         (gpointer)context);
865
866         return;
867 }
868
869 static int _net_nfc_handover_process_wifi_direct_setup(
870                 net_nfc_handover_bss_get_context_t *context)
871 {
872         int err = WIFI_DIRECT_ERROR_NONE;
873         err = wifi_direct_initialize();
874         if(err != WIFI_DIRECT_ERROR_NONE)
875         {
876                 NFC_ERR("wifi_direct_initialize err value %d",err);
877                 return err;
878         }
879
880         err = wifi_direct_set_device_state_changed_cb(
881                         _net_nfc_wifi_direct_power_changed,
882                         context);
883
884         if(err != WIFI_DIRECT_ERROR_NONE)
885         {
886                 NFC_ERR("wifi_direct_set_device_state_changed_cb err value %d",err);
887                 return err;
888         }
889
890         err = wifi_direct_set_discovery_state_changed_cb(
891                         _net_nfc_wifi_scan_completed_cb,
892                         context);
893
894         if(err != WIFI_DIRECT_ERROR_NONE)
895         {
896                 NFC_ERR("wifi_direct_set_discovery_state_changed_cb err value %d",err);
897                 return err;
898         }
899
900         err = wifi_direct_set_connection_state_changed_cb(
901                         _net_nfc_wifi_direct_connection_changed,
902                         context);
903
904         if (err != WIFI_DIRECT_ERROR_NONE)
905         {
906                 NFC_ERR("wifi_direct_set_connection_state_changed_cb err value %d",err);
907                 return err;
908         }
909         else
910         {
911                 context->step = NET_NFC_LLCP_STEP_02;
912                 context->result = NET_NFC_OK;
913
914                 wifi_direct_state_e status = WIFI_DIRECT_STATE_DEACTIVATED;
915
916                 err = wifi_direct_get_state(&status);
917                 NFC_ERR("status value %d",status);
918
919                 if (status != WIFI_DIRECT_STATE_ACTIVATED)
920                 {
921                         wifi_direct_activate();
922                 }
923                 else
924                 {
925                         NFC_DBG("wifi direct is enabled already");
926
927                         /* do next step */
928                         g_idle_add((GSourceFunc)
929                                         _net_nfc_handover_bss_wfd_get_carrier_record,
930                                         (gpointer)context);
931                 }
932                 return WIFI_DIRECT_ERROR_NONE;
933
934         }
935 }
936
937 static int _net_nfc_handover_process_wifi_group_setup(
938                 net_nfc_handover_bss_get_context_t *context)
939 {
940         int err = WIFI_DIRECT_ERROR_NONE;
941         bool group_owner = false;
942
943         err = wifi_direct_is_group_owner(&group_owner);
944         NFC_ERR("error value %d",err);
945
946         context->step = NET_NFC_LLCP_STEP_03;
947         context->result = NET_NFC_OK;
948
949         if(err == WIFI_DIRECT_ERROR_NONE)
950         {
951                 if(group_owner == true)
952                 {
953                         NFC_DBG("Already group owner, continue next step");
954                         g_idle_add((GSourceFunc)
955                                         _net_nfc_handover_bss_wfd_get_carrier_record,
956                                         (gpointer)context);
957                 }
958                 else
959                 {
960                         err = wifi_direct_create_group();
961                         if (err != WIFI_DIRECT_ERROR_NONE)
962                         {
963                                 NFC_ERR("wifi_direct_create_group failed [%d]",err);
964                                 return err;
965                         }
966                 }
967         }
968         else
969         {
970                 NFC_ERR("wifi_direct_is_group_owner failed [%d]",err);
971                 return err;
972         }
973
974         return err;
975 }
976
977 static gboolean _net_nfc_handover_bss_wfd_get_carrier_record(
978                 net_nfc_handover_bss_get_context_t *context)
979 {
980         LOGD("[%s:%d] START", __func__, __LINE__);
981         if(context == NULL)
982         {
983                 NFC_ERR("Invalid context");
984                 return FALSE;
985         }
986
987         if(context != NULL)
988         {
989                 int err = WIFI_DIRECT_ERROR_NONE;
990
991                 if (context->result != NET_NFC_OK && context->result != NET_NFC_BUSY)
992                 {
993                         NFC_ERR("context->result is error"
994                                         " [%d]", context->result);
995
996                         context->step = NET_NFC_LLCP_STEP_RETURN;
997                 }
998
999                 switch (context->step)
1000                 {
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)
1005                         {
1006                                 NFC_ERR("_net_nfc_handover_process_wifi_direct_setup failed");
1007                                 _net_nfc_wifi_process_error(err,context);
1008                         }
1009                         break;
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)
1014                         {
1015                                 NFC_ERR("_net_nfc_handover_process_wifi_group_setup failed");
1016                                 _net_nfc_wifi_process_error(err,context);
1017                         }
1018                         break;
1019                 case NET_NFC_LLCP_STEP_03 :
1020                         NFC_ERR("NET_NFC_LLCP_STEP_03");
1021                         context->step = NET_NFC_LLCP_STEP_RETURN;
1022
1023                         /* append config to ndef message */
1024                         context->result =_net_nfc_handover_bss_create_config_record(
1025                                         &context->carrier);
1026                         if (context->result != NET_NFC_OK)
1027                         {
1028                                 NFC_ERR("_net_nfc_handover_bss_create_config_record failed"
1029                                                 "[%d]", context->result);
1030                         }
1031
1032                         g_idle_add((GSourceFunc)
1033                                         _net_nfc_handover_bss_wfd_get_carrier_record,
1034                                         (gpointer)context);
1035
1036                         break;
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();
1044
1045                         /* complete and return to upper step */
1046                         context->cb(context->result,
1047                                         context->cps,
1048                                         context->carrier,
1049                                         context->aux_data_count,
1050                                         context->aux_data,
1051                                         context->user_param);
1052                         break;
1053
1054                 default :
1055                         break;
1056                 }
1057         }
1058         LOGD("[%s:%d] END", __func__, __LINE__);
1059
1060         return TRUE;
1061 }
1062 #endif
1063
1064 net_nfc_error_e net_nfc_server_handover_bss_get_carrier_record(
1065                 net_nfc_server_handover_get_carrier_record_cb cb,
1066                 void *user_param)
1067 {
1068         net_nfc_error_e result = NET_NFC_OK;
1069         net_nfc_handover_bss_create_context_t *context = NULL;
1070
1071         _net_nfc_util_alloc_mem(context, sizeof(*context));
1072         if (context != NULL)
1073         {
1074                 context->cb = cb;
1075                 context->user_param = user_param;
1076                 context->step = NET_NFC_LLCP_STEP_01;
1077
1078                 _net_nfc_handover_bss_get_carrier_record(context);
1079
1080         }
1081         else
1082         {
1083                 result = NET_NFC_ALLOC_FAIL;
1084         }
1085
1086         return result;
1087 }
1088
1089 #ifdef TARGET
1090 net_nfc_error_e net_nfc_server_handover_bss_wfd_get_carrier_record(
1091                 net_nfc_server_handover_get_carrier_record_cb cb,
1092                 void *user_param)
1093 {
1094         net_nfc_handover_bss_get_context_t *context = NULL;
1095
1096         _net_nfc_util_alloc_mem(context, sizeof(*context));
1097         if (context != NULL)
1098         {
1099                 context->cb = cb;
1100                 context->user_param = user_param;
1101                 context->step = NET_NFC_LLCP_STEP_01;
1102                 context->cps = NET_NFC_CONN_HANDOVER_CARRIER_ACTIVATE;
1103
1104                 g_idle_add((GSourceFunc)_net_nfc_handover_bss_wfd_get_carrier_record,
1105                                 (gpointer)context);
1106
1107                 return NET_NFC_OK;
1108         }
1109         else
1110         {
1111                 return NET_NFC_ALLOC_FAIL;
1112         }
1113
1114 }
1115 #endif