Wifi Handover implementation
[platform/core/connectivity/nfc-manager-neard.git] / daemon / net_nfc_server_process_handover.c
1 /*
2  * Copyright (c) 2012, 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
17 #include "bluetooth-api.h"
18 #include "net_nfc_server_handover.h"
19 #include "net_nfc_debug_internal.h"
20 #include "net_nfc_util_internal.h"
21 #include "net_nfc_util_ndef_message.h"
22 #include "net_nfc_util_ndef_record.h"
23 #include "net_nfc_util_handover.h"
24 #include "net_nfc_server_controller.h"
25 #include "net_nfc_server_llcp.h"
26 #include "net_nfc_server_process_handover.h"
27 #include "net_nfc_server_process_snep.h"
28 #include "net_nfc_util_gdbus_internal.h"
29 #include "net_nfc_server_handover_bss.h"
30 #include "net_nfc_server_handover_bt.h"
31
32 typedef void (*_net_nfc_server_handover_create_carrier_msg_cb)(
33                 net_nfc_error_e result,
34                 ndef_message_s *selector,
35                 void *user_param);
36
37 typedef struct _net_nfc_handover_context_t
38 {
39         net_nfc_target_handle_s *handle;
40         net_nfc_error_e result;
41         net_nfc_llcp_socket_t socket;
42         uint32_t state;
43         net_nfc_conn_handover_carrier_type_e type;
44         data_s data;
45         ndef_record_s *record;
46         void *user_param;
47 }
48 net_nfc_handover_context_t;
49
50 #define NET_NFC_CH_CONTEXT net_nfc_target_handle_s *handle;\
51         net_nfc_llcp_socket_t socket;\
52         net_nfc_error_e result;\
53         int step;\
54         net_nfc_conn_handover_carrier_type_e type;\
55         void *user_param;
56
57 typedef struct _net_nfc_server_handover_create_config_context_t
58 {
59         NET_NFC_CH_CONTEXT;
60
61         _net_nfc_server_handover_create_carrier_msg_cb cb;
62         net_nfc_conn_handover_carrier_type_e current_type;
63         ndef_message_s *ndef_message;
64         ndef_message_s *requester; /* for low power selector */
65
66         ndef_record_s *record;
67 }
68 net_nfc_server_handover_create_config_context_t;
69
70 typedef struct _net_nfc_server_handover_process_config_context_t
71 {
72         NET_NFC_CH_CONTEXT;
73
74         net_nfc_server_handover_process_carrier_record_cb cb;
75 }net_nfc_server_handover_process_config_context_t;
76
77
78
79 static void _net_nfc_server_handover_send_response(
80                 net_nfc_error_e result,
81                 net_nfc_conn_handover_carrier_type_e carrier,
82                 data_s *ac_data,
83                 void *user_param);
84
85 static void _net_nfc_server_handover_client_process(
86                 net_nfc_handover_context_t *context);
87
88 static void _net_nfc_server_handover_client_error_cb(
89                 net_nfc_error_e result,
90                 net_nfc_target_handle_s *handle,
91                 net_nfc_llcp_socket_t socket,
92                 data_s *data,
93                 void *user_param);
94
95 static void _net_nfc_server_handover_client_connected_cb(
96                 net_nfc_error_e result,
97                 net_nfc_target_handle_s *handle,
98                 net_nfc_llcp_socket_t socket,
99                 data_s *data,
100                 void *user_param);
101
102 static void _net_nfc_server_handover_bss_get_carrier_record_cb(
103                 net_nfc_error_e result,
104                 net_nfc_conn_handover_carrier_state_e cps,
105                 ndef_record_s *carrier,
106                 uint32_t aux_data_count,
107                 ndef_record_s *aux_data,
108                 void *user_param);
109
110 static void _net_nfc_server_handover_bss_process_carrier_record_cb(
111                 net_nfc_error_e result,
112                 net_nfc_conn_handover_carrier_type_e type,
113                 data_s *data,
114                 void *user_param);
115 static void _net_nfc_server_handover_get_response_process(
116                 net_nfc_handover_context_t *context);
117
118 static net_nfc_error_e
119 _net_nfc_server_handover_create_requester_from_rawdata(
120                 ndef_message_s **requestor,
121                 data_s *data);
122
123 static net_nfc_error_e
124 _net_nfc_server_handover_create_requester_carrier_configs(
125                 net_nfc_conn_handover_carrier_type_e type,
126                 void *cb,
127                 void *user_param);
128
129 static bool _net_nfc_server_handover_check_hr_record_validation(
130                 ndef_message_s *message);
131
132 static bool _net_nfc_server_handover_check_hs_record_validation(
133                 ndef_message_s *message);
134
135 static net_nfc_error_e
136 _net_nfc_server_handover_create_selector_carrier_configs(
137                 net_nfc_conn_handover_carrier_type_e type,
138                 void *cb,
139                 void *user_param);
140
141 static int _net_nfc_server_handover_iterate_create_carrier_configs(
142                 net_nfc_server_handover_create_config_context_t *context);
143
144 static int _net_nfc_server_handover_iterate_carrier_configs_to_next(
145                 net_nfc_server_handover_create_config_context_t *context);
146
147 static int _net_nfc_server_handover_iterate_carrier_configs_step(
148                 net_nfc_server_handover_create_config_context_t *context);
149
150 static void _net_nfc_server_handover_server_process(
151                 net_nfc_handover_context_t *context);
152
153 ////////////////////////////////////////////////////////////////////////////
154
155 static void _net_nfc_server_handover_send_response(
156                 net_nfc_error_e result,
157                 net_nfc_conn_handover_carrier_type_e carrier,
158                 data_s *ac_data,
159                 void *user_param)
160 {
161         HandoverRequestData *handover_data = (HandoverRequestData *)user_param;
162
163         g_assert(handover_data != NULL);
164         g_assert(handover_data->invocation != NULL);
165         g_assert(handover_data->handoverobj != NULL);
166
167         net_nfc_gdbus_handover_complete_request(
168                         handover_data->handoverobj,
169                         handover_data->invocation,
170                         result,
171                         carrier,
172                         net_nfc_util_gdbus_data_to_variant(ac_data));
173
174         if (handover_data->data)
175         {
176                 g_free(handover_data->data->buffer);
177                 g_free(handover_data->data);
178         }
179
180         g_object_unref(handover_data->invocation);
181         g_object_unref(handover_data->handoverobj);
182
183         g_free(handover_data);
184 }
185
186 static net_nfc_error_e _net_nfc_server_handover_convert_ndef_message_to_data(
187                 ndef_message_s *msg,
188                 data_s *data)
189 {
190         net_nfc_error_e result;
191         uint32_t length;
192
193         if (msg == NULL || data == NULL)
194         {
195                 return NET_NFC_INVALID_PARAM;
196         }
197
198         length = net_nfc_util_get_ndef_message_length(msg);
199         if (length > 0)
200         {
201                 net_nfc_util_alloc_data(data, length);
202                 result = net_nfc_util_convert_ndef_message_to_rawdata(msg, data);
203         }
204         else
205         {
206                 result = NET_NFC_INVALID_PARAM;
207         }
208
209         return result;
210 }
211
212 static void _net_nfc_server_handover_bt_get_carrier_record_cb(
213                 net_nfc_error_e result,
214                 net_nfc_conn_handover_carrier_state_e cps,
215                 ndef_record_s *carrier,
216                 uint32_t aux_data_count,
217                 ndef_record_s *aux_data,
218                 void *user_param)
219 {
220         net_nfc_server_handover_create_config_context_t *context =
221                 (net_nfc_server_handover_create_config_context_t *)user_param;
222
223         /* append record to ndef message */
224         if (result == NET_NFC_OK)
225         {
226                 ndef_record_s *record;
227
228                 /* FIXME : copy record and append */
229                 net_nfc_util_create_record(carrier->TNF,
230                                 &carrier->type_s,
231                                 &carrier->id_s,
232                                 &carrier->payload_s,
233                                 &record);
234
235                 if ((result = net_nfc_util_append_carrier_config_record(
236                                                 context->ndef_message,
237                                                 record,
238                                                 cps)) == NET_NFC_OK)
239                 {
240                         DEBUG_SERVER_MSG("net_nfc_util_append_carrier"
241                                         "_config_record success");
242                 }
243                 else
244                 {
245                         DEBUG_ERR_MSG("net_nfc_util_append_carrier"
246                                         "_config_record failed [%d]",
247                                         result);
248                         net_nfc_util_free_record(record);
249                 }
250
251                 g_idle_add(
252                                 (GSourceFunc)_net_nfc_server_handover_iterate_carrier_configs_to_next,
253                                 (gpointer)context);
254         }
255
256         /* don't free context */
257 }
258 static void _net_nfc_server_handover_bss_get_carrier_record_cb(
259                 net_nfc_error_e result,
260                 net_nfc_conn_handover_carrier_state_e cps,
261                 ndef_record_s *carrier,
262                 uint32_t aux_data_count,
263                 ndef_record_s *aux_data,
264                 void *user_param)
265 {
266         net_nfc_server_handover_create_config_context_t *context =
267                 (net_nfc_server_handover_create_config_context_t *)user_param;
268
269         /* append record to ndef message */
270         if (result == NET_NFC_OK)
271         {
272                 ndef_record_s *record;
273
274                 net_nfc_util_create_record(carrier->TNF,
275                                 &carrier->type_s,
276                                 &carrier->id_s,
277                                 &carrier->payload_s,
278                                 &record);
279
280                 if ((result = net_nfc_util_append_carrier_config_record(
281                                                 context->ndef_message,
282                                                 record,
283                                                 cps)) == NET_NFC_OK)
284                 {
285                         DEBUG_SERVER_MSG("net_nfc_util_append_carrier"
286                                         "_config_record success");
287                 }
288                 else
289                 {
290                         DEBUG_ERR_MSG("net_nfc_util_append_carrier"
291                                         "_config_record failed [%d]",
292                                         result);
293                         net_nfc_util_free_record(record);
294                 }
295
296                 g_idle_add(
297                                 (GSourceFunc)_net_nfc_server_handover_iterate_carrier_configs_to_next,
298                                 (gpointer)context);
299         }
300
301         /* don't free context */
302 }
303 static void _net_nfc_server_handover_bt_process_carrier_record_cb(
304                 net_nfc_error_e result,
305                 net_nfc_conn_handover_carrier_type_e type,
306                 data_s *data,
307                 void *user_param)
308 {
309         net_nfc_server_handover_process_config_context_t *context =
310                 (net_nfc_server_handover_process_config_context_t *)user_param;
311
312         if (result == NET_NFC_OK)
313         {
314                 if (context->cb != NULL)
315                 {
316                         context->cb(result, type, data, context->user_param);
317                 }
318         }
319         else
320         {
321                 DEBUG_ERR_MSG("_handover_bt_process_carrier_record failed [%d]",
322                                 result);
323         }
324
325         _net_nfc_util_free_mem(context);
326 }
327
328 static void _net_nfc_server_handover_bss_process_carrier_record_cb(
329                 net_nfc_error_e result,
330                 net_nfc_conn_handover_carrier_type_e type,
331                 data_s *data,
332                 void *user_param)
333 {
334
335         net_nfc_server_handover_process_config_context_t *context =
336                 (net_nfc_server_handover_process_config_context_t *)user_param;
337
338         if(context)
339         {
340                 if (context->cb != NULL)
341                 {
342                         context->cb(result, type, data, context->user_param);
343                 }
344                 else
345                         DEBUG_ERR_MSG("Invalid Callback");
346
347                 _net_nfc_util_free_mem(context);
348         }
349 }
350 net_nfc_error_e
351 net_nfc_server_handover_get_carrier_record_by_priority_order(
352                 ndef_message_s *request,
353                 ndef_record_s **record)
354 {
355         net_nfc_error_e result;
356         unsigned int carrier_count = 0;
357
358         LOGD("[%s] START", __func__);
359
360         if (request == NULL || record == NULL)
361         {
362                 return NET_NFC_NULL_PARAMETER;
363         }
364
365         *record = NULL;
366
367         if ((result = net_nfc_util_get_alternative_carrier_record_count(
368                                         request,
369                                         &carrier_count)) == NET_NFC_OK)
370         {
371                 int idx, priority;
372                 net_nfc_conn_handover_carrier_type_e carrier_type =
373                         NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN;
374
375                 for (priority = NET_NFC_CONN_HANDOVER_CARRIER_BT;*record == NULL
376                                 && priority < NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN;priority++)
377                 {
378                         /* check each carrier record and create matched record */
379                         for (idx = 0; idx < carrier_count; idx++)
380                         {
381                                 if ((net_nfc_util_get_alternative_carrier_type(
382                                                                 request,
383                                                                 idx,
384                                                                 &carrier_type)== NET_NFC_OK)
385                                                 && (carrier_type == priority))
386                                 {
387                                         DEBUG_SERVER_MSG("selected carrier type"
388                                                         " = [%d]", carrier_type);
389                                         net_nfc_util_get_carrier_config_record(
390                                                         request,
391                                                         idx,
392                                                         record);
393                                         result = NET_NFC_OK;
394                                         break;
395                                 }
396                         }
397                 }
398         }
399         else
400         {
401                 DEBUG_ERR_MSG("net_nfc_util_get_alternative_carrier"
402                                 "_record_count failed");
403         }
404
405         LOGD("[%s] END", __func__);
406
407         return result;
408 }
409
410 static net_nfc_error_e _net_nfc_server_handover_create_requester_from_rawdata(
411                 ndef_message_s **requestor,
412                 data_s *data)
413 {
414         net_nfc_error_e result;
415
416         if (requestor == NULL)
417                 return NET_NFC_NULL_PARAMETER;
418
419         *requestor = NULL;
420
421         result = net_nfc_util_create_ndef_message(requestor);
422         if (result == NET_NFC_OK)
423         {
424                 result = net_nfc_util_convert_rawdata_to_ndef_message(data,
425                                 *requestor);
426
427                 if (result == NET_NFC_OK)
428                 {
429                         if (_net_nfc_server_handover_check_hr_record_validation(
430                                                 *requestor) == true)
431                         {
432                                 result = NET_NFC_OK;
433                         }
434                         else
435                         {
436                                 DEBUG_ERR_MSG("record is not valid or"
437                                                 " is not available");
438                                 net_nfc_util_free_ndef_message(*requestor);
439                                 result = NET_NFC_INVALID_PARAM;
440                         }
441                 }
442                 else
443                 {
444                         DEBUG_ERR_MSG("_net_nfc_ndef_rawdata_to_ndef"
445                                         "failed [%d]",result);
446                 }
447         } else {
448                 DEBUG_ERR_MSG("net_nfc_util_create_ndef_message failed [%d]",
449                                 result);
450         }
451
452         return result;
453 }
454
455 net_nfc_error_e net_nfc_server_handover_create_selector_from_rawdata(
456                 ndef_message_s **selector, data_s *data)
457 {
458         net_nfc_error_e result;
459
460         if (selector == NULL)
461                 return NET_NFC_NULL_PARAMETER;
462
463         *selector = NULL;
464
465         if ((result = net_nfc_util_create_ndef_message(selector))
466                         == NET_NFC_OK)
467         {
468                 if ((result = net_nfc_util_convert_rawdata_to_ndef_message(data,
469                                                 *selector)) == NET_NFC_OK)
470                 {
471
472                         /* if record is not Hs record, then */
473                         if (_net_nfc_server_handover_check_hs_record_validation(
474                                                 *selector) == true)
475                         {
476                                 result = NET_NFC_OK;
477                         }
478                         else
479                         {
480                                 DEBUG_ERR_MSG("record is not valid or is "
481                                                 "not available");
482                                 net_nfc_util_free_ndef_message(*selector);
483                                 result = NET_NFC_INVALID_PARAM;
484                         }
485                 }
486                 else
487                 {
488                         DEBUG_ERR_MSG("_net_nfc_ndef_rawdata_to_ndef"
489                                         " failed [%d]",result);
490                 }
491         }
492         else
493         {
494                 DEBUG_ERR_MSG("_net_nfc_util_create_ndef_message"
495                                 " failed [%d]",result);
496         }
497
498         return result;
499 }
500
501 static bool _net_nfc_server_handover_check_hr_record_validation(
502                 ndef_message_s *message)
503 {
504         unsigned int count;
505         ndef_record_s *rec;
506
507         LOGD("[%s] START", __func__);
508
509         if (message == NULL)
510                 return false;
511
512         rec = (ndef_record_s *)message->records;
513
514         if (memcmp(rec->type_s.buffer, CH_REQ_RECORD_TYPE,
515                                 rec->type_s.length) != 0)
516         {
517                 DEBUG_ERR_MSG("This is not connection handover"
518                                 " request message");
519                 goto ERROR;
520         }
521
522         if (rec->payload_s.buffer[0] != CH_VERSION)
523         {
524                 DEBUG_ERR_MSG("connection handover version is not matched");
525                 goto ERROR;
526         }
527
528         if (net_nfc_util_get_alternative_carrier_record_count(message, &count)
529                         != NET_NFC_OK || count == 0)
530         {
531                 DEBUG_ERR_MSG("there is no carrier reference");
532                 goto ERROR;
533         }
534
535         LOGD("[%s] END", __func__);
536
537         return true;
538
539 ERROR :
540         LOGD("[%s] END", __func__);
541
542         return false;
543 }
544
545 static bool _net_nfc_server_handover_check_hs_record_validation(
546                 ndef_message_s *message)
547 {
548         unsigned int count;
549         ndef_record_s *rec;
550
551         LOGD("[%s] START", __func__);
552
553         if (message == NULL)
554                 return false;
555
556         rec = (ndef_record_s *)message->records;
557
558         if (memcmp(rec->type_s.buffer, CH_SEL_RECORD_TYPE,
559                                 rec->type_s.length) != 0)
560         {
561                 DEBUG_ERR_MSG("This is not connection handover"
562                                 " request message");
563                 goto ERROR;
564         }
565
566         if (net_nfc_util_get_alternative_carrier_record_count(
567                                 message,&count)!= NET_NFC_OK || count == 0)
568         {
569                 DEBUG_ERR_MSG("there is no carrrier reference");
570                 goto ERROR;
571         }
572
573         /*      check version */
574         if (rec->payload_s.buffer[0] != CH_VERSION)
575         {
576                 DEBUG_ERR_MSG("connection handover version"
577                                 " is not matched");
578                 goto ERROR;
579         }
580
581         LOGD("[%s] END", __func__);
582
583         return true;
584
585 ERROR :
586         LOGD("[%s] END", __func__);
587
588         return false;
589 }
590
591 static int _net_nfc_server_handover_iterate_carrier_configs_step(
592                 net_nfc_server_handover_create_config_context_t *context)
593 {
594         LOGD("[%s:%d] START", __func__, __LINE__);
595
596         if (context == NULL)
597         {
598                 return 0;
599         }
600
601         if (context->cb != NULL)
602         {
603                 context->cb(NET_NFC_OK, context->ndef_message,
604                                 context->user_param);
605         }
606
607         if (context->ndef_message != NULL)
608         {
609                 net_nfc_util_free_ndef_message(context->ndef_message);
610         }
611
612         _net_nfc_util_free_mem(context);
613
614         LOGD("[%s:%d] END", __func__, __LINE__);
615
616         return 0;
617 }
618
619 static int _net_nfc_server_handover_iterate_carrier_configs_to_next(
620                 net_nfc_server_handover_create_config_context_t *context)
621 {
622         if (context->result == NET_NFC_OK || context->result == NET_NFC_BUSY)
623         {
624                 if (context->type == NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN)
625                 {
626                         if (context->current_type <
627                                         NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN)
628                         {
629                                 context->current_type++;
630                         }
631                 }
632                 else
633                 {
634                         context->current_type =
635                                 NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN;
636                 }
637
638                 g_idle_add(
639                                 (GSourceFunc)_net_nfc_server_handover_iterate_create_carrier_configs,
640                                 (gpointer)context);
641         }
642         else
643         {
644                 DEBUG_ERR_MSG("context->result is error [%d]", context->result);
645
646                 g_idle_add(
647                                 (GSourceFunc)_net_nfc_server_handover_iterate_carrier_configs_step,
648                                 (gpointer)context);
649         }
650
651         return 0;
652 }
653
654 static int _net_nfc_server_handover_iterate_create_carrier_configs(
655                 net_nfc_server_handover_create_config_context_t *context)
656 {
657         LOGD("[%s:%d] START", __func__, __LINE__);
658         net_nfc_error_e result = NET_NFC_OK;
659
660         switch (context->current_type)
661         {
662         case NET_NFC_CONN_HANDOVER_CARRIER_BT :
663                 DEBUG_MSG("[NET_NFC_CONN_HANDOVER_CARRIER_BT]");
664                 net_nfc_server_handover_bt_get_carrier_record(
665                                 _net_nfc_server_handover_bt_get_carrier_record_cb,
666                                 context);
667                 break;
668
669         case NET_NFC_CONN_HANDOVER_CARRIER_WIFI_BSS :
670                 DEBUG_MSG("[NET_NFC_CONN_HANDOVER_CARRIER_WIFI_BSS]");
671
672 #ifdef TARGET
673                 if(memcmp(context->ndef_message->records->type_s.buffer ,CH_SEL_RECORD_TYPE,
674                                         context->ndef_message->records->type_s.length)==0)
675                 {
676                         result = net_nfc_server_handover_bss_wfd_get_carrier_record(
677                                         _net_nfc_server_handover_bss_get_carrier_record_cb,
678                                         context);
679                 }
680                 else
681 #endif
682                 {
683                         result = net_nfc_server_handover_bss_get_carrier_record(
684                                         _net_nfc_server_handover_bss_get_carrier_record_cb,
685                                         context);
686                 }
687                 DEBUG_MSG("[%d]",result);
688                 break;
689
690                 //      case NET_NFC_CONN_HANDOVER_CARRIER_WIFI_IBSS :
691                 //              DEBUG_MSG("[NET_NFC_CONN_HANDOVER_CARRIER_WIFI_BSS]");
692                 //              g_idle_add(
693                 //              (GSourceFunc)_net_nfc_server_handover_append_wifi_carrier_config,
694                 //                      context);
695                 //              break;
696
697         case NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN :
698                 DEBUG_MSG("[NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN]");
699                 g_idle_add(
700                                 (GSourceFunc)_net_nfc_server_handover_iterate_carrier_configs_step,
701                                 (gpointer)context);
702                 break;
703
704         default :
705                 DEBUG_MSG("[unknown : %d]", context->current_type);
706                 g_idle_add(
707                                 (GSourceFunc)_net_nfc_server_handover_iterate_carrier_configs_step,
708                                 (gpointer)context);
709                 break;
710         }
711
712         LOGD("[%s:%d] END", __func__, __LINE__);
713
714         return 0;
715 }
716
717 static net_nfc_error_e
718 _net_nfc_server_handover_create_requester_carrier_configs(
719                 net_nfc_conn_handover_carrier_type_e type,
720                 void *cb,
721                 void *user_param)
722 {
723         net_nfc_error_e result = NET_NFC_OK;
724         net_nfc_server_handover_create_config_context_t *context = NULL;
725
726         LOGD("[%s:%d] START", __func__, __LINE__);
727
728         _net_nfc_util_alloc_mem(context, sizeof(*context));
729         if (context != NULL)
730         {
731                 context->type = type;
732                 if (type == NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN)
733                 {
734                         context->current_type =
735                                 NET_NFC_CONN_HANDOVER_CARRIER_BT;
736                 }
737                 else
738                         context->current_type = context->type;
739
740                 context->cb = cb;
741                 context->user_param = user_param;
742                 net_nfc_util_create_handover_request_message(
743                                 &context->ndef_message);
744
745                 /* append carrier record */
746                 g_idle_add(
747                                 (GSourceFunc)_net_nfc_server_handover_iterate_create_carrier_configs,
748                                 (gpointer)context);
749         }
750         else
751         {
752                 DEBUG_ERR_MSG("alloc failed");
753                 result = NET_NFC_ALLOC_FAIL;
754         }
755
756         LOGD("[%s:%d] END", __func__, __LINE__);
757
758         return result;
759 }
760
761 static net_nfc_error_e
762 _net_nfc_server_handover_create_selector_carrier_configs(
763                 net_nfc_conn_handover_carrier_type_e type,
764                 void *cb,
765                 void *user_param)
766 {
767         net_nfc_error_e result = NET_NFC_OK;
768         net_nfc_server_handover_create_config_context_t *context = NULL;
769
770         LOGD("[%s:%d] START", __func__, __LINE__);
771
772         _net_nfc_util_alloc_mem(context, sizeof(net_nfc_server_handover_create_config_context_t));
773         if (context != NULL)
774         {
775                 context->type = type;
776                 if (type == NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN)
777                 {
778                         context->current_type = NET_NFC_CONN_HANDOVER_CARRIER_BT;
779                 }
780                 else
781                         context->current_type = context->type;
782
783                 context->cb = cb;
784                 context->user_param = user_param;
785                 net_nfc_util_create_handover_select_message(
786                                 &context->ndef_message);
787
788                 /* append carrier record */
789                 g_idle_add(
790                                 (GSourceFunc)_net_nfc_server_handover_iterate_create_carrier_configs,
791                                 (gpointer)context);
792         }
793         else
794         {
795                 DEBUG_ERR_MSG("alloc failed");
796                 result = NET_NFC_ALLOC_FAIL;
797         }
798
799         LOGD("[%s:%d] END", __func__, __LINE__);
800
801         return result;
802 }
803
804 net_nfc_error_e net_nfc_server_handover_process_carrier_record(
805                 ndef_record_s *carrier, void *cb, void *user_param)
806 {
807         net_nfc_error_e result = NET_NFC_OK;
808         net_nfc_server_handover_process_config_context_t *context = NULL;
809
810         LOGD("[%s:%d] START", __func__, __LINE__);
811
812         _net_nfc_util_alloc_mem(context, sizeof(*context));
813         if (context != NULL)
814         {
815                 net_nfc_conn_handover_carrier_type_e type;
816
817                 net_nfc_util_get_alternative_carrier_type_from_record(
818                                 carrier,
819                                 &type);
820
821                 context->type = type;
822                 context->user_param = user_param;
823                 context->cb = cb;
824                 context->step = NET_NFC_LLCP_STEP_01;
825
826                 /* process carrier record */
827                 switch (type)
828                 {
829                 case NET_NFC_CONN_HANDOVER_CARRIER_BT :
830                         DEBUG_MSG("[NET_NFC_CONN_HANDOVER_CARRIER_BT]");
831                         net_nfc_server_handover_bt_process_carrier_record(
832                                         carrier,
833                                         _net_nfc_server_handover_bt_process_carrier_record_cb,
834                                         context);
835                         break;
836
837                 case NET_NFC_CONN_HANDOVER_CARRIER_WIFI_BSS :
838                         DEBUG_MSG("[NET_NFC_CONN_HANDOVER_CARRIER_WIFI_BSS]");
839                         net_nfc_server_handover_bss_process_carrier_record(
840                                         carrier,
841                                         _net_nfc_server_handover_bss_process_carrier_record_cb,
842                                         context);
843                         break;
844
845                 case NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN :
846                         DEBUG_MSG("[NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN]");
847                         _net_nfc_util_free_mem(context);
848                         break;
849
850                 default :
851                         DEBUG_MSG("[unknown]");
852                         _net_nfc_util_free_mem(context);
853                         break;
854                 }
855         }
856         else
857         {
858                 DEBUG_ERR_MSG("alloc failed");
859                 result = NET_NFC_ALLOC_FAIL;
860         }
861
862         LOGD("[%s:%d] END", __func__, __LINE__);
863
864         return result;
865 }
866
867 static net_nfc_error_e _net_nfc_server_handover_select_carrier_record(
868                 ndef_message_s *request,
869                 net_nfc_conn_handover_carrier_type_e *type,
870                 ndef_record_s **record)
871 {
872         net_nfc_error_e result;
873         uint32_t count;
874
875         *record = NULL;
876         *type = NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN;
877
878         /* get requester message */
879         if ((result = net_nfc_util_get_alternative_carrier_record_count(request,
880                                         &count)) == NET_NFC_OK)
881         {
882                 if (1/* power state */ || count == 1)
883                 {
884                         ndef_record_s *temp;
885
886                         /* fill alternative carrier information */
887                         if ((result =
888                                                 net_nfc_server_handover_get_carrier_record_by_priority_order(
889                                                         request,
890                                                         &temp)) == NET_NFC_OK)
891                         {
892                                 if ((result =
893                                                         net_nfc_util_get_alternative_carrier_type_from_record(
894                                                                 temp,
895                                                                 type)) == NET_NFC_OK)
896                                 {
897                                         net_nfc_util_create_record(temp->TNF,
898                                                         &temp->type_s, &temp->id_s,
899                                                         &temp->payload_s,
900                                                         record);
901                                 }
902                                 else
903                                 {
904                                         DEBUG_ERR_MSG("net_nfc_util_get_alternative"
905                                                         "_carrier_type_from_record failed [%d]", result);
906                                 }
907                         }
908                         else
909                         {
910                                 DEBUG_ERR_MSG("_handover_get_carrier_record"
911                                                 "_by_priority_order failed [%d]", result);
912                         }
913                 }
914                 else /* low power && count > 1 */
915                 {
916                         result = NET_NFC_INVALID_STATE;
917                 }
918         }
919         else
920         {
921                 DEBUG_ERR_MSG("net_nfc_util_get_alternative_carrier_record_count "
922                                 "failed [%d]", result);
923         }
924
925         return result;
926 }
927
928 static void _net_nfc_server_handover_create_carrier_configs_2_cb(
929                 net_nfc_error_e result,
930                 ndef_message_s *selector,
931                 void *user_param)
932 {
933         net_nfc_handover_context_t *context =
934                 (net_nfc_handover_context_t *)user_param;
935
936         DEBUG_SERVER_MSG("_net_nfc_server_handover_server_create_carrier_config_cb"
937                         "result [%d]",result);
938
939         if (context == NULL)
940         {
941                 return;
942         }
943
944         context->result = result;
945
946         if (result == NET_NFC_OK) {
947                 result = _net_nfc_server_handover_convert_ndef_message_to_data(
948                                 selector,
949                                 &context->data);
950
951                 DEBUG_SERVER_MSG("selector message created, length [%d]",
952                                 context->data.length);
953
954                 context->state = NET_NFC_LLCP_STEP_03;
955         }
956         else
957         {
958                 DEBUG_ERR_MSG("_net_nfc_server_handover_create_selector_msg failed [%d]",
959                                 result);
960                 context->state = NET_NFC_MESSAGE_LLCP_ERROR;
961         }
962
963         _net_nfc_server_handover_get_response_process(context);
964 }
965
966 static void _net_nfc_server_handover_process_carrier_record_2_cb(
967                 net_nfc_error_e result,
968                 net_nfc_conn_handover_carrier_type_e type,
969                 data_s *data,
970                 void *user_param)
971 {
972         net_nfc_handover_context_t *context =
973                 (net_nfc_handover_context_t *)user_param;
974
975         DEBUG_SERVER_MSG("_net_nfc_server_handover_server_process_carrier_record_cb"
976                         "result [%d]",result);
977
978         context->result = result;
979         if (result == NET_NFC_OK)
980         {
981                 context->state = NET_NFC_LLCP_STEP_04;
982         }
983         else
984         {
985                 context->state = NET_NFC_MESSAGE_LLCP_ERROR;
986         }
987
988         context->data.length = data->length;
989         _net_nfc_util_alloc_mem(context->data.buffer, context->data.length);
990         memcpy(context->data.buffer, data->buffer, context->data.length);
991
992
993         _net_nfc_server_handover_server_process(context);
994 }
995
996 static void _net_nfc_server_handover_get_response_process(
997                 net_nfc_handover_context_t *context)
998 {
999         net_nfc_error_e result;
1000
1001         if (context == NULL)
1002         {
1003                 return;
1004         }
1005
1006         switch (context->state)
1007         {
1008         case NET_NFC_LLCP_STEP_02 :
1009                 DEBUG_SERVER_MSG("NET_NFC_LLCP_STEP_02");
1010
1011                 result = _net_nfc_server_handover_create_selector_carrier_configs(
1012                                 context->type,
1013                                 _net_nfc_server_handover_create_carrier_configs_2_cb,
1014                                 context);
1015
1016                 if (result != NET_NFC_OK)
1017                 {
1018                         DEBUG_ERR_MSG("_net_nfc_server_handover_create_"
1019                                         "selector_carrier_config failed [%d]",result);
1020                 }
1021                 break;
1022
1023         case NET_NFC_LLCP_STEP_03 :
1024                 DEBUG_SERVER_MSG("NET_NFC_LLCP_STEP_03");
1025
1026                 result = net_nfc_server_handover_process_carrier_record(
1027                                 context->record,
1028                                 _net_nfc_server_handover_process_carrier_record_2_cb,
1029                                 context);
1030
1031                 if (result != NET_NFC_OK)
1032                 {
1033                         DEBUG_ERR_MSG("net_nfc_server_handover_process_carrier_record"
1034                                         "failed [%d]",result);
1035                 }
1036                 break;
1037
1038         case NET_NFC_LLCP_STEP_04 :
1039                 DEBUG_SERVER_MSG("NET_NFC_LLCP_STEP_04");
1040
1041                 /* response select message */
1042                 result = net_nfc_server_snep_server_send_get_response(
1043                                 (net_nfc_snep_handle_h)context->handle,
1044                                 &context->data);
1045                 break;
1046
1047         case NET_NFC_STATE_ERROR :
1048                 DEBUG_SERVER_MSG("NET_NFC_STATE_ERROR");
1049                 break;
1050
1051         default :
1052                 DEBUG_ERR_MSG("NET_NFC_LLCP_STEP_??");
1053                 /* TODO */
1054                 break;
1055         }
1056 }
1057
1058 static bool _net_nfc_server_handover_get_response_cb(
1059                 net_nfc_snep_handle_h handle,
1060                 uint32_t type,
1061                 uint32_t max_len,
1062                 data_s *data,
1063                 void *user_param)
1064 {
1065         net_nfc_error_e result;
1066         ndef_message_s *request;
1067
1068         DEBUG_SERVER_MSG("type [%d], data [%p], user_param [%p]",
1069                         type, data, user_param);
1070
1071         if (data == NULL || data->buffer == NULL)
1072         {
1073                 /* restart */
1074                 return false;
1075         }
1076
1077         /* TODO : send select response to requester */
1078         result = _net_nfc_server_handover_create_requester_from_rawdata(
1079                         &request,
1080                         data);
1081
1082         if (result == NET_NFC_OK)
1083         {
1084                 net_nfc_conn_handover_carrier_type_e type;
1085                 ndef_record_s *record;
1086
1087                 if (_net_nfc_server_handover_select_carrier_record(
1088                                         request,
1089                                         &type,
1090                                         &record) == NET_NFC_OK)
1091                 {
1092                         net_nfc_handover_context_t *context = NULL;
1093
1094                         _net_nfc_util_alloc_mem(context, sizeof(*context));
1095
1096                         if (context != NULL)
1097                         {
1098                                 context->handle = (void *)handle;
1099                                 context->type = type;
1100                                 context->user_param = user_param;
1101                                 context->state = NET_NFC_LLCP_STEP_02;
1102
1103                                 net_nfc_util_create_record(record->TNF,
1104                                                 &record->type_s,
1105                                                 &record->id_s,
1106                                                 &record->payload_s,
1107                                                 &context->record);
1108
1109                                 _net_nfc_server_handover_get_response_process(context);
1110                         }
1111                         else
1112                         {
1113                                 DEBUG_ERR_MSG("_net_nfc_util_alloc_mem failed");
1114                                 result = NET_NFC_ALLOC_FAIL;
1115                         }
1116                 }
1117                 else
1118                 {
1119                         /* low power */
1120                 }
1121
1122                 net_nfc_util_free_ndef_message(request);
1123         }
1124         else
1125         {
1126                 DEBUG_SERVER_MSG("it is not handover requester message, [%d]",
1127                                 result);
1128         }
1129
1130         return (result == NET_NFC_OK);
1131 }
1132
1133 ////////////////////////////////////////////////////////////////////////////////
1134 static void _net_nfc_server_handover_server_process_carrier_record_cb(
1135                 net_nfc_error_e result,
1136                 net_nfc_conn_handover_carrier_type_e type,
1137                 data_s *data,
1138                 void *user_param)
1139 {
1140         net_nfc_handover_context_t *context =
1141                 (net_nfc_handover_context_t *)user_param;
1142
1143         DEBUG_SERVER_MSG("_net_nfc_server_handover_server_process"
1144                         "_carrier_record_cb result [%d]",result);
1145
1146         context->result = result;
1147
1148         if (result == NET_NFC_OK)
1149                 context->state = NET_NFC_LLCP_STEP_04;
1150         else
1151                 context->state = NET_NFC_MESSAGE_LLCP_ERROR;
1152
1153         _net_nfc_server_handover_server_process(context);
1154 }
1155
1156 static void _net_nfc_server_handover_server_create_carrier_config_cb(
1157                 net_nfc_error_e result,
1158                 ndef_message_s *selector,
1159                 void *user_param)
1160 {
1161         net_nfc_handover_context_t *context =
1162                 (net_nfc_handover_context_t *)user_param;
1163
1164         DEBUG_SERVER_MSG("_net_nfc_server_handover_server_create"
1165                         "_carrier_config_cb,result [%d]",result);
1166
1167         if (context == NULL)
1168         {
1169                 return;
1170         }
1171
1172         context->result = result;
1173
1174         net_nfc_conn_handover_carrier_type_e type;
1175
1176         net_nfc_util_get_alternative_carrier_type_from_record(
1177                         context->record,
1178                         &type);
1179         if (result == NET_NFC_OK)
1180         {
1181                 result = _net_nfc_server_handover_convert_ndef_message_to_data(
1182                                 selector,
1183                                 &context->data);
1184                 DEBUG_SERVER_MSG("selector message created, length [%d]",
1185                                 context->data.length);
1186                 if(type == NET_NFC_CONN_HANDOVER_CARRIER_WIFI_BSS)
1187                         context->state = NET_NFC_LLCP_STEP_04;
1188                 else
1189                         context->state = NET_NFC_LLCP_STEP_03;
1190         }
1191         else
1192         {
1193                 DEBUG_ERR_MSG("_net_nfc_server_handover_create_selector_msg"
1194                                 " failed [%d]", result);
1195                 context->state = NET_NFC_MESSAGE_LLCP_ERROR;
1196         }
1197
1198         _net_nfc_server_handover_server_process(context);
1199 }
1200
1201 static void _net_nfc_server_handover_server_recv_cb(
1202                 net_nfc_error_e result,
1203                 net_nfc_target_handle_s *handle,
1204                 net_nfc_llcp_socket_t socket,
1205                 data_s *data,
1206                 void *user_param)
1207 {
1208         net_nfc_handover_context_t *context =
1209                 (net_nfc_handover_context_t *)user_param;
1210         ndef_message_s *request;
1211
1212         DEBUG_SERVER_MSG("_net_nfc_server_handover_server_recv_cb,"
1213                         " socket [%x], result [%d]", socket, result);
1214
1215         context->result = result;
1216
1217         if (result == NET_NFC_OK)
1218         {
1219                 result = _net_nfc_server_handover_create_requester_from_rawdata(
1220                                 &request,
1221                                 data);
1222
1223                 if (result == NET_NFC_OK)
1224                 {
1225                         ndef_record_s *record;
1226
1227                         if (_net_nfc_server_handover_select_carrier_record(
1228                                                 request,
1229                                                 &context->type,
1230                                                 &record) == NET_NFC_OK)
1231                         {
1232                                 net_nfc_util_create_record(record->TNF,
1233                                                 &record->type_s, &record->id_s,
1234                                                 &record->payload_s,
1235                                                 &context->record);
1236
1237                                 context->state = NET_NFC_LLCP_STEP_02;
1238                         }
1239                         else
1240                         {
1241                                 /* low power */
1242                                 context->state = NET_NFC_LLCP_STEP_06;
1243                         }
1244
1245                         net_nfc_util_free_ndef_message(request);
1246                 }
1247                 else
1248                 {
1249                         DEBUG_ERR_MSG("_net_nfc_server_handover_create"
1250                                         "_requester_from_rawdata failed [%d]",result);
1251                         context->state = NET_NFC_MESSAGE_LLCP_ERROR;
1252                 }
1253         }
1254         else
1255         {
1256                 DEBUG_ERR_MSG("net_nfc_server_llcp_simple_receive failed [%d]",
1257                                 result);
1258                 context->state = NET_NFC_MESSAGE_LLCP_ERROR;
1259         }
1260
1261         _net_nfc_server_handover_server_process(context);
1262 }
1263
1264 static void _net_nfc_server_handover_server_send_cb(net_nfc_error_e result,
1265                 net_nfc_target_handle_s *handle,
1266                 net_nfc_llcp_socket_t socket,
1267                 data_s *data,
1268                 void *user_param)
1269 {
1270         net_nfc_handover_context_t *context =
1271                 (net_nfc_handover_context_t *)user_param;
1272
1273         DEBUG_SERVER_MSG("_net_nfc_server_handover_server_send_cb"
1274                         " socket [%x], result [%d]", socket, result);
1275
1276         context->result = result;
1277
1278         if (result == NET_NFC_OK)
1279         {
1280                 context->state = NET_NFC_LLCP_STEP_01;
1281                 net_nfc_util_free_data(&context->data);
1282                 net_nfc_util_free_record(context->record);
1283                 context->record = NULL;
1284         }
1285         else
1286         {
1287                 DEBUG_ERR_MSG("net_nfc_server_llcp_simple_send failed [%d]",
1288                                 result);
1289                 context->state = NET_NFC_MESSAGE_LLCP_ERROR;
1290         }
1291
1292         _net_nfc_server_handover_server_process(context);
1293 }
1294
1295 static void _net_nfc_server_handover_server_process(
1296                 net_nfc_handover_context_t *context)
1297 {
1298         if (context == NULL)
1299         {
1300                 return;
1301         }
1302
1303         switch (context->state)
1304         {
1305         case NET_NFC_LLCP_STEP_01 :
1306                 DEBUG_SERVER_MSG("NET_NFC_LLCP_STEP_01");
1307
1308                 /* receive request message */
1309                 net_nfc_server_llcp_simple_receive(context->handle,
1310                                 context->socket,
1311                                 _net_nfc_server_handover_server_recv_cb,
1312                                 context);
1313                 break;
1314
1315         case NET_NFC_LLCP_STEP_02 :
1316                 DEBUG_SERVER_MSG("NET_NFC_LLCP_STEP_02");
1317
1318                 context->result = _net_nfc_server_handover_create_selector_carrier_configs(
1319                                 context->type,
1320                                 _net_nfc_server_handover_server_create_carrier_config_cb,
1321                                 context);
1322
1323                 if (context->result != NET_NFC_OK)
1324                 {
1325                         DEBUG_ERR_MSG("_net_nfc_server_handover_create_selector"
1326                                         "_carrier_configs failed [%d]", context->result);
1327                 }
1328                 break;
1329
1330         case NET_NFC_LLCP_STEP_03 :
1331                 DEBUG_SERVER_MSG("NET_NFC_LLCP_STEP_03");
1332
1333                 context->result = net_nfc_server_handover_process_carrier_record(
1334                                 context->record,
1335                                 _net_nfc_server_handover_server_process_carrier_record_cb,
1336                                 context);
1337                 if (context->result != NET_NFC_OK)
1338                 {
1339                         DEBUG_ERR_MSG("_net_nfc_server_handover_process_carrier_"
1340                                         "record failed [%d]",context->result);
1341                 }
1342                 break;
1343
1344         case NET_NFC_LLCP_STEP_04 :
1345                 DEBUG_SERVER_MSG("NET_NFC_LLCP_STEP_04");
1346
1347                 /* send select message */
1348                 net_nfc_server_llcp_simple_send(
1349                                 context->handle,
1350                                 context->socket,
1351                                 &context->data,
1352                                 _net_nfc_server_handover_server_send_cb,
1353                                 context);
1354                 break;
1355
1356         case NET_NFC_STATE_ERROR :
1357                 DEBUG_SERVER_MSG("NET_NFC_STATE_ERROR");
1358
1359                 /* error, invoke callback */
1360                 DEBUG_ERR_MSG("handover_server failed, [%d]",
1361                                 context->result);
1362
1363                 /* restart?? */
1364                 break;
1365
1366         default :
1367                 DEBUG_ERR_MSG("NET_NFC_LLCP_STEP_??");
1368                 /* TODO */
1369                 break;
1370         }
1371 }
1372
1373 static void _net_nfc_server_handover_server_error_cb(
1374                 net_nfc_error_e result,
1375                 net_nfc_target_handle_s *handle,
1376                 net_nfc_llcp_socket_t socket,
1377                 data_s *data,
1378                 void *user_param)
1379 {
1380         net_nfc_handover_context_t *context =
1381                 (net_nfc_handover_context_t *)user_param;
1382
1383         DEBUG_ERR_MSG("result [%d], socket [%x], user_param [%p]",
1384                         result, socket, user_param);
1385
1386         if (context == NULL)
1387         {
1388                 return;
1389         }
1390
1391         net_nfc_controller_llcp_socket_close(socket, &result);
1392
1393         net_nfc_util_free_record(context->record);
1394         net_nfc_util_free_data(&context->data);
1395         _net_nfc_util_free_mem(user_param);
1396 }
1397
1398 static void _net_nfc_server_handover_server_incomming_cb(
1399                 net_nfc_error_e result,
1400                 net_nfc_target_handle_s *handle,
1401                 net_nfc_llcp_socket_t socket,
1402                 data_s *data,
1403                 void *user_param)
1404 {
1405         DEBUG_SERVER_MSG("result [%d], socket [%x], user_param [%p]",
1406                         result, socket, user_param);
1407
1408         net_nfc_handover_context_t *accept_context = NULL;
1409
1410         _net_nfc_util_alloc_mem(accept_context, sizeof(*accept_context));
1411         if (accept_context == NULL)
1412         {
1413                 DEBUG_ERR_MSG("_net_nfc_util_alloc_mem failed");
1414
1415                 result = NET_NFC_ALLOC_FAIL;
1416                 goto ERROR;
1417         }
1418
1419         accept_context->handle = handle;
1420         accept_context->socket = socket;
1421         accept_context->state = NET_NFC_LLCP_STEP_01;
1422
1423         result = net_nfc_server_llcp_simple_accept(
1424                         handle,
1425                         socket,
1426                         _net_nfc_server_handover_server_error_cb,
1427                         accept_context);
1428
1429         if (result != NET_NFC_OK)
1430         {
1431                 DEBUG_ERR_MSG("net_nfc_server_llcp_simple_accept failed, [%d]",
1432                                 result);
1433
1434                 goto ERROR;
1435         }
1436
1437         _net_nfc_server_handover_server_process(accept_context);
1438
1439         return;
1440
1441 ERROR :
1442         if (accept_context != NULL)
1443         {
1444                 _net_nfc_util_free_mem(accept_context);
1445         }
1446
1447         net_nfc_controller_llcp_socket_close(socket, &result);
1448
1449         /* TODO : restart ?? */
1450 }
1451
1452 net_nfc_error_e net_nfc_server_handover_default_server_start(
1453                 net_nfc_target_handle_s *handle)
1454 {
1455         net_nfc_error_e result;
1456
1457         /* start default handover server using snep */
1458         result =
1459                 net_nfc_server_snep_default_server_register_get_response_cb(
1460                                 _net_nfc_server_handover_get_response_cb,
1461                                 NULL);
1462
1463         /* start default handover server */
1464         result = net_nfc_server_llcp_simple_server(
1465                         handle,
1466                         CH_SAN,
1467                         CH_SAP,
1468                         _net_nfc_server_handover_server_incomming_cb,
1469                         _net_nfc_server_handover_server_error_cb,
1470                         NULL);
1471
1472         if (result == NET_NFC_OK)
1473         {
1474                 DEBUG_SERVER_MSG("start handover server, san [%s], sap [%d]",
1475                                 CH_SAN,CH_SAP);
1476         }
1477         else
1478         {
1479                 DEBUG_ERR_MSG("net_nfc_server_llcp_simple_server failed, [%d]",
1480                                 result);
1481         }
1482
1483         return result;
1484 }
1485
1486 static void _handover_default_activate_cb(int event,
1487                 net_nfc_target_handle_s *handle,uint32_t sap, const char *san, void *user_param)
1488 {
1489         net_nfc_error_e result;
1490
1491         DEBUG_SERVER_MSG("event [%d], handle [%p], sap [%d], san [%s]",
1492                         event, handle, sap, san);
1493
1494         if (event == NET_NFC_LLCP_START) {
1495                 /* start default handover server using snep */
1496                 result = net_nfc_server_snep_default_server_register_get_response_cb(
1497                                 _net_nfc_server_handover_get_response_cb, NULL);
1498
1499                 /* start default handover server */
1500                 result = net_nfc_server_llcp_simple_server(handle,
1501                                 CH_SAN, CH_SAP,
1502                                 _net_nfc_server_handover_server_incomming_cb,
1503                                 _net_nfc_server_handover_server_error_cb, NULL);
1504
1505                 if (result == NET_NFC_OK) {
1506                         DEBUG_SERVER_MSG("start handover server, san [%s], sap [%d]",
1507                                         CH_SAN, CH_SAP);
1508                 } else {
1509                         DEBUG_ERR_MSG("net_nfc_service_llcp_server failed, [%d]",
1510                                         result);
1511                 }
1512         } else if (event == NET_NFC_LLCP_UNREGISTERED) {
1513                 /* unregister server, do nothing */
1514         }
1515 }
1516
1517 net_nfc_error_e net_nfc_server_handover_default_server_register()
1518 {
1519         char id[20];
1520
1521         /* TODO : make id, */
1522         snprintf(id, sizeof(id), "%d", getpid());
1523
1524         /* start default snep server */
1525         return net_nfc_server_llcp_register_service(id,
1526                         CH_SAP,
1527                         CH_SAN,
1528                         _handover_default_activate_cb,
1529                         NULL);
1530 }
1531
1532 net_nfc_error_e net_nfc_server_handover_default_server_unregister()
1533 {
1534         char id[20];
1535
1536         /* TODO : make id, */
1537         snprintf(id, sizeof(id), "%d", getpid());
1538
1539         /* start default snep server */
1540         return net_nfc_server_llcp_unregister_service(id,
1541                         CH_SAP,
1542                         CH_SAN);
1543 }
1544
1545 ////////////////////////////////////////////////////////////////////////////////
1546
1547 static void _net_nfc_server_handover_client_post_process_cb(
1548                 net_nfc_error_e result,
1549                 net_nfc_conn_handover_carrier_type_e type,
1550                 data_s *data,
1551                 void *user_param)
1552 {
1553         net_nfc_handover_context_t *context =
1554                 (net_nfc_handover_context_t *)user_param;
1555
1556         if (context == NULL)
1557         {
1558                 return;
1559         }
1560
1561         context->state = NET_NFC_LLCP_STEP_RETURN;
1562
1563         _net_nfc_server_handover_client_process(context);
1564 }
1565
1566 static void _net_nfc_server_handover_client_process_carrier_record_cb(
1567                 net_nfc_error_e result,
1568                 net_nfc_conn_handover_carrier_type_e type,
1569                 data_s *data,
1570                 void *user_param)
1571 {
1572         net_nfc_handover_context_t *context =
1573                 (net_nfc_handover_context_t *)user_param;
1574
1575         DEBUG_SERVER_MSG("_net_nfc_server_handover_server_process"
1576                         "_carrier_record_cb,result [%d]",result);
1577
1578         context->result = result;
1579
1580         net_nfc_util_free_data(&context->data);
1581
1582         if (result == NET_NFC_OK)
1583         {
1584                 if(context->type == NET_NFC_CONN_HANDOVER_CARRIER_WIFI_BSS)
1585                         context->state = NET_NFC_LLCP_STEP_RETURN;
1586                 else
1587                 {
1588                         net_nfc_util_alloc_data(&context->data, data->length);
1589                         memcpy(context->data.buffer, data->buffer, data->length);
1590
1591                         context->state = NET_NFC_LLCP_STEP_05;
1592                 }
1593         }
1594         else
1595         {
1596                 context->state = NET_NFC_MESSAGE_LLCP_ERROR;
1597         }
1598
1599         _net_nfc_server_handover_client_process(context);
1600 }
1601
1602 static void _net_nfc_server_handover_client_recv_cb(
1603                 net_nfc_error_e result,
1604                 net_nfc_target_handle_s *handle,
1605                 net_nfc_llcp_socket_t socket,
1606                 data_s *data,
1607                 void *user_param)
1608 {
1609         net_nfc_handover_context_t *context =
1610                 (net_nfc_handover_context_t *)user_param;
1611
1612         if (context == NULL)
1613         {
1614                 return;
1615         }
1616
1617         context->result = result;
1618
1619         if (result == NET_NFC_OK)
1620         {
1621                 ndef_message_s *selector;
1622                 ndef_record_s *record;
1623
1624                 result = net_nfc_server_handover_create_selector_from_rawdata(
1625                                 &selector,
1626                                 data);
1627
1628                 if (result == NET_NFC_OK)
1629                 {
1630                         result =
1631                                 net_nfc_server_handover_get_carrier_record_by_priority_order(
1632                                                 selector,
1633                                                 &record);
1634
1635                         if (result == NET_NFC_OK)
1636                         {
1637                                 net_nfc_util_create_record(
1638                                                 record->TNF,
1639                                                 &record->type_s, &record->id_s,
1640                                                 &record->payload_s,
1641                                                 &context->record);
1642
1643                                 context->state = NET_NFC_LLCP_STEP_04;
1644                         }
1645                         else
1646                         {
1647                                 DEBUG_ERR_MSG("_get_carrier_record_by_priority_order"
1648                                                 " failed, [%d]",result);
1649                                 context->state = NET_NFC_STATE_ERROR;
1650                         }
1651                 }
1652                 else
1653                 {
1654                         DEBUG_ERR_MSG("_net_nfc_server_handover_create"
1655                                         "_selector_from_rawdata failed, [%d]",result);
1656                         context->state = NET_NFC_STATE_ERROR;
1657                 }
1658         }
1659         else
1660         {
1661                 DEBUG_ERR_MSG("net_nfc_server_llcp_simple_receive failed, [%d]",
1662                                 result);
1663                 context->state = NET_NFC_STATE_ERROR;
1664         }
1665
1666         _net_nfc_server_handover_client_process(context);
1667 }
1668
1669 static void _net_nfc_server_handover_client_send_cb(
1670                 net_nfc_error_e result,
1671                 net_nfc_target_handle_s *handle,
1672                 net_nfc_llcp_socket_t socket,
1673                 data_s *data,
1674                 void *user_param)
1675 {
1676         net_nfc_handover_context_t *context =
1677                 (net_nfc_handover_context_t *)user_param;
1678
1679         if (context == NULL)
1680         {
1681                 return;
1682         }
1683
1684         context->result = result;
1685
1686         net_nfc_util_free_data(&context->data);
1687
1688         if (result == NET_NFC_OK)
1689         {
1690                 context->state = NET_NFC_LLCP_STEP_03;
1691         }
1692         else
1693         {
1694                 DEBUG_ERR_MSG("net_nfc_server_llcp_simple_client failed, [%d]",
1695                                 result);
1696                 context->state = NET_NFC_STATE_ERROR;
1697         }
1698
1699         _net_nfc_server_handover_client_process(context);
1700 }
1701
1702 static void _net_nfc_server_handover_client_create_carrier_configs_cb(
1703                 net_nfc_error_e result,
1704                 ndef_message_s *msg,
1705                 void *user_param)
1706 {
1707         net_nfc_handover_context_t *context =
1708                 (net_nfc_handover_context_t *)user_param;
1709
1710         if (context == NULL) {
1711                 return;
1712         }
1713
1714         context->result = result;
1715
1716         if (msg != NULL)
1717         {
1718                 if ((result =
1719                                         _net_nfc_server_handover_convert_ndef_message_to_data(
1720                                                 msg,
1721                                                 &context->data)) == NET_NFC_OK)
1722                 {
1723                         context->state = NET_NFC_LLCP_STEP_02;
1724                 }
1725                 else
1726                 {
1727                         DEBUG_ERR_MSG("_net_nfc_server_handover_convert_ndef_"
1728                                         "message_to_data failed [%d]",result);
1729                         context->state = NET_NFC_STATE_ERROR;
1730                         context->result = result;
1731                 }
1732         }
1733         else
1734         {
1735                 DEBUG_ERR_MSG("null param, [%d]", result);
1736                 context->state = NET_NFC_STATE_ERROR;
1737                 context->result = result;
1738         }
1739
1740         _net_nfc_server_handover_client_process(context);
1741 }
1742
1743
1744 ////////////////////////////////////////////////////////////////////////////////
1745 static void _net_nfc_server_handover_client_process(
1746                 net_nfc_handover_context_t *context)
1747 {
1748         net_nfc_error_e result;
1749
1750         if (context == NULL)
1751         {
1752                 return;
1753         }
1754
1755         switch (context->state)
1756         {
1757         case NET_NFC_LLCP_STEP_01 :
1758                 DEBUG_SERVER_MSG("NET_NFC_LLCP_STEP_01");
1759
1760                 if ((result = _net_nfc_server_handover_create_requester_carrier_configs(
1761                                                 context->type,
1762                                                 _net_nfc_server_handover_client_create_carrier_configs_cb,
1763                                                 context)) != NET_NFC_OK)
1764                 {
1765                         DEBUG_ERR_MSG("_net_nfc_server_handover_create_requester"
1766                                         "_carrier_configs failed [%d]",result);
1767                 }
1768                 break;
1769
1770         case NET_NFC_LLCP_STEP_02 :
1771                 DEBUG_SERVER_MSG("NET_NFC_LLCP_STEP_02");
1772
1773                 /* send request */
1774                 net_nfc_server_llcp_simple_send(
1775                                 context->handle,
1776                                 context->socket,
1777                                 &context->data,
1778                                 _net_nfc_server_handover_client_send_cb,
1779                                 context);
1780                 break;
1781
1782         case NET_NFC_LLCP_STEP_03 :
1783                 DEBUG_SERVER_MSG("NET_NFC_LLCP_STEP_03");
1784
1785                 /* receive response */
1786                 net_nfc_server_llcp_simple_receive(context->handle,
1787                                 context->socket,
1788                                 _net_nfc_server_handover_client_recv_cb,
1789                                 context);
1790                 break;
1791
1792         case NET_NFC_LLCP_STEP_04 :
1793                 DEBUG_SERVER_MSG("NET_NFC_LLCP_STEP_04");
1794
1795                 result = net_nfc_server_handover_process_carrier_record(
1796                                 context->record,
1797                                 _net_nfc_server_handover_client_process_carrier_record_cb,
1798                                 context);
1799
1800                 if (result != NET_NFC_OK)
1801                 {
1802                         DEBUG_ERR_MSG("net_nfc_server_handover_process_carrier_record"
1803                                         "failed [%d]",result);
1804                 }
1805                 break;
1806
1807         case NET_NFC_LLCP_STEP_05 :
1808                 DEBUG_SERVER_MSG("NET_NFC_LLCP_STEP_05");
1809
1810                 /* start post process */
1811                 if (context->type == NET_NFC_CONN_HANDOVER_CARRIER_BT)
1812                 {
1813                         net_nfc_server_handover_bt_post_process(
1814                                         &context->data,
1815                                         _net_nfc_server_handover_client_post_process_cb,
1816                                         context);
1817                 }
1818                 else
1819                 {
1820                         DEBUG_ERR_MSG("not supported...");
1821                 }
1822                 break;
1823
1824         case NET_NFC_LLCP_STEP_RETURN :
1825                 DEBUG_SERVER_MSG("NET_NFC_LLCP_STEP_RETURN");
1826
1827                 /* complete and invoke callback */
1828                 _net_nfc_server_handover_send_response(
1829                                 context->result,
1830                                 context->type,
1831                                 &context->data,
1832                                 context->user_param);
1833
1834                 net_nfc_util_free_data(&context->data);
1835                 net_nfc_util_free_record(context->record);
1836                 _net_nfc_util_free_mem(context);
1837                 break;
1838
1839         case NET_NFC_STATE_ERROR :
1840         default :
1841                 DEBUG_ERR_MSG("NET_NFC_STATE_ERROR");
1842
1843                 _net_nfc_server_handover_send_response(
1844                                 context->result,
1845                                 context->type,
1846                                 NULL,
1847                                 context->user_param);
1848                 break;
1849         }
1850 }
1851
1852
1853
1854 static void _net_nfc_server_handover_client_connected_cb(
1855                 net_nfc_error_e result,
1856                 net_nfc_target_handle_s *handle,
1857                 net_nfc_llcp_socket_t socket,
1858                 data_s *data,
1859                 void *user_param)
1860 {
1861         DEBUG_SERVER_MSG("result [%d], socket [%x], user_param [%p]",
1862                         result, socket, user_param);
1863
1864         HandoverRequestData *handover_data = NULL;
1865         handover_data = (HandoverRequestData *)user_param;
1866         if (result == NET_NFC_OK)
1867         {
1868                 net_nfc_handover_context_t *context = NULL;
1869                 _net_nfc_util_alloc_mem(context, sizeof(*context));
1870
1871                 if (context != NULL)
1872                 {
1873                         context->handle = handle;
1874                         context->socket = socket;
1875                         context->state = NET_NFC_LLCP_STEP_01;
1876                         context->type = handover_data->type;
1877                         context->user_param = user_param;
1878                         _net_nfc_server_handover_client_process(context);
1879                 }
1880                 else
1881                 {
1882                         DEBUG_ERR_MSG("_net_nfc_util_alloc_mem failed");
1883                         result = NET_NFC_ALLOC_FAIL;
1884                 }
1885         }
1886         else
1887         {
1888                 DEBUG_ERR_MSG("net_nfc_server_llcp_simple_client"
1889                                 " failed, [%d]", result);
1890         }
1891 }
1892
1893 static void _net_nfc_server_handover_client_error_cb(
1894                 net_nfc_error_e result,
1895                 net_nfc_target_handle_s *handle,
1896                 net_nfc_llcp_socket_t socket,
1897                 data_s *data,
1898                 void *user_param)
1899 {
1900
1901         DEBUG_ERR_MSG("result [%d], socket [%x], user_param [%p]",
1902                         result, socket, user_param);
1903
1904         if (false)
1905         {
1906                 _net_nfc_server_handover_send_response(
1907                                 result,
1908                                 NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN,
1909                                 NULL,
1910                                 user_param);
1911         }
1912
1913         net_nfc_controller_llcp_socket_close(socket, &result);
1914 }
1915
1916
1917 net_nfc_error_e net_nfc_server_handover_default_client_start(
1918                 net_nfc_target_handle_s *handle, void *user_data)
1919 {
1920         net_nfc_error_e result;
1921
1922         result = net_nfc_server_llcp_simple_client(
1923                         handle,
1924                         CH_SAN,
1925                         CH_SAP,
1926                         _net_nfc_server_handover_client_connected_cb,
1927                         _net_nfc_server_handover_client_error_cb,
1928                         user_data);
1929
1930         if (result != NET_NFC_OK)
1931         {
1932                 DEBUG_ERR_MSG("net_nfc_server_llcp_simple_client"
1933                                 " failed, [%d]",result);
1934         }
1935
1936         return result;
1937 }