PHDC P2P Handling
[platform/core/connectivity/nfc-manager-neard.git] / daemon / net_nfc_server_process_phdc.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 <glib.h>
18 #include "net_nfc_debug_internal.h"
19 #include "net_nfc_server_tag.h"
20 #include "net_nfc_util_defines.h"
21 #include "net_nfc_util_internal.h"
22 #include "net_nfc_util_gdbus_internal.h"
23 #include "net_nfc_server_llcp.h"
24 #include "net_nfc_server_util.h"
25 #include "net_nfc_server_process_phdc.h"
26 #include "net_nfc_server_controller.h"
27 #include "net_nfc_server_phdc.h"
28
29
30
31 typedef struct _net_nfc_server_phdc_pdu_t
32 {
33         uint8_t data[128];
34 }
35 __attribute__ ((packed)) net_nfc_server_phdc_pdu_t;
36
37 //callback for internal send/receive
38 typedef void (*_net_nfc_server_phdc_operation_cb)(
39                 net_nfc_error_e result,
40                 data_s *data,
41                 void *user_param);
42
43 static void _net_nfc_server_phdc_agent_connected_cb(net_nfc_error_e result,
44                 net_nfc_target_handle_s *handle,
45                 net_nfc_llcp_socket_t socket,
46                 data_s *data,
47                 void *user_param);
48
49 typedef struct _net_nfc_server_phdc_op_context_t
50 {
51         net_nfc_target_handle_s *handle;
52         net_nfc_error_e result;
53         int socket;
54         uint32_t state;
55         uint32_t current;
56         uint16_t miu;
57         data_s data;
58         _net_nfc_server_phdc_operation_cb cb;
59         void *user_param;
60 }net_nfc_server_phdc_op_context_t;
61
62
63 typedef struct _net_nfc_server_phdc_job_t
64 {
65         net_nfc_server_phdc_context_t *context;
66         net_nfc_target_handle_s *handle;
67         net_nfc_error_e result;
68         net_nfc_llcp_socket_t socket;
69         uint32_t state;
70         data_s data;
71         net_nfc_server_phdc_send_cb cb;
72         void *user_param;
73 }net_nfc_server_phdc_job_t;
74
75 static void _net_nfc_server_phdc_agent_process(
76                 net_nfc_server_phdc_job_t *job);
77
78 static void _net_nfc_server_phdc_destory_context(
79                 net_nfc_server_phdc_op_context_t *context);
80
81 static void _net_nfc_server_phdc_clear_queue(
82                 gpointer data,
83                 gpointer user_data);
84
85 static void _net_nfc_server_phdc_socket_error_cb(net_nfc_error_e result,
86                 net_nfc_target_handle_s *handle,
87                 net_nfc_llcp_socket_t socket,
88                 data_s *data,
89                 void *user_param);
90
91 static void _net_nfc_server_phdc_recv_cb(
92                 net_nfc_llcp_socket_t socket,
93                 net_nfc_error_e result,
94                 data_s *data,
95                 void *extra,
96                 void *user_param);
97
98 static void _net_nfc_server_phdc_recv(
99                 net_nfc_server_phdc_op_context_t *context);
100
101 static net_nfc_server_phdc_op_context_t* _net_nfc_server_phdc_create_recv_context(
102                 net_nfc_target_handle_s *handle,
103                 net_nfc_llcp_socket_t socket,
104                 void *cb,
105                 void *user_param);
106
107 static void _net_nfc_server_phdc_manager_op_cb(
108                 net_nfc_error_e result,
109                 data_s *data,
110                 void *user_param);
111
112
113 static net_nfc_error_e net_nfc_server_phdc_recv(
114                 net_nfc_target_handle_s *handle,
115                 net_nfc_llcp_socket_t socket,
116                 _net_nfc_server_phdc_operation_cb cb,
117                 void *user_param);
118
119 static void _net_nfc_server_phdc_manager_process(
120                 net_nfc_server_phdc_context_t *context);
121
122 static void _net_nfc_server_phdc_incoming_cb(net_nfc_error_e result,
123                 net_nfc_target_handle_s *handle,
124                 net_nfc_llcp_socket_t socket,
125                 data_s *data,
126                 void *user_param);
127
128
129 static void _net_nfc_server_phdc_destory_context(net_nfc_server_phdc_op_context_t *context)
130 {
131         if (context != NULL)
132         {
133                 if (context->data.buffer != NULL)
134                         net_nfc_util_free_data(&context->data);
135
136                 _net_nfc_util_free_mem(context);
137         }
138 }
139
140 static void _net_nfc_server_phdc_clear_queue(gpointer data,gpointer user_data)
141 {
142         net_nfc_server_phdc_job_t *job = (net_nfc_server_phdc_job_t *)data;
143
144         if (job != NULL)
145         {
146                 if (job->cb != NULL)
147                 {
148                         job->cb((net_nfc_phdc_handle_h)job->context,NET_NFC_OPERATION_FAIL,
149                                 NET_NFC_PHDC_OPERATION_FAILED, job->user_param);
150                 }
151
152                 if (job->data.buffer != NULL)
153                         net_nfc_util_free_data(&job->data);
154
155                 _net_nfc_util_free_mem(job);
156         }
157 }
158
159 static void _net_nfc_server_phdc_socket_error_cb(net_nfc_error_e result,
160                 net_nfc_target_handle_s *handle, net_nfc_llcp_socket_t socket, data_s *data,
161                 void *user_param)
162 {
163         net_nfc_server_phdc_context_t *context = (net_nfc_server_phdc_context_t *)user_param;
164
165         NFC_DBG("_net_nfc_server_phdc_socket_error_cb socket [%x], result [%d]",
166                         socket, result);
167
168         RET_IF(NULL == context);
169
170         if (context->data.buffer != NULL)
171                 net_nfc_util_free_data(&context->data);
172
173         g_queue_foreach(&context->queue, _net_nfc_server_phdc_clear_queue, NULL);
174
175         _net_nfc_util_free_mem(context);
176 }
177
178
179 static void _net_nfc_server_phdc_recv_cb(net_nfc_llcp_socket_t socket,
180                 net_nfc_error_e result, data_s *data, void *extra, void *user_param)
181 {
182         net_nfc_server_phdc_op_context_t *context =
183                 (net_nfc_server_phdc_op_context_t *)user_param;
184
185         uint8_t *buffer = NULL;
186         uint16_t pdu_length = 0;
187
188         NFC_DBG("_net_nfc_server_phdc_recv_cb, socket[%x], result[%d]", socket, result);
189
190         RET_IF(NULL == context);
191
192         context->result = result;
193
194         if (result != NET_NFC_OK)
195         {
196                 NFC_ERR("error [%d]", result);
197                 context->state = NET_NFC_STATE_ERROR;
198                 goto END;
199         }
200
201         if (NULL == data || NULL == data->buffer || 0 == data->length)
202         {
203                 NFC_ERR("invalid response");
204                 context->state = NET_NFC_STATE_ERROR;
205                 goto END;
206         }
207
208         //first 2 bytes is phdc header length
209         memcpy(&pdu_length,data->buffer,PHDC_HEADER_LEN);
210
211         pdu_length = ntohs(pdu_length);
212         NFC_INFO("pdu_legth [%d]", pdu_length);
213
214         if(pdu_length > 0)
215         {
216                 /* buffer create */
217                 net_nfc_util_alloc_data(&context->data,pdu_length);
218                 if (NULL == context->data.buffer)
219                 {
220                         NFC_ERR("net_nfc_util_alloc_data failed");
221                         context->result = NET_NFC_ALLOC_FAIL;
222                         goto END;
223                 }
224                 buffer =data->buffer;
225                 context->data.length = data->length - PHDC_HEADER_LEN;
226
227                 /* copy data */
228                 if(context->data.length > 0)
229                 {
230                         memcpy(context->data.buffer ,buffer, context->data.length);
231                         NFC_DBG("receive progress... [%d", context->data.length);
232                 }
233         }
234 END:
235         context->cb(context->result, &context->data, context->user_param);
236 }
237
238
239 static void _net_nfc_server_phdc_recv(net_nfc_server_phdc_op_context_t *context)
240 {
241         RET_IF(NULL == context);
242
243         bool ret;
244         net_nfc_error_e result;
245
246         NFC_DBG("socket [%x] waiting data.....", context->socket);
247
248         ret = net_nfc_controller_llcp_recv(context->handle, context->socket, context->miu,
249                         &result, _net_nfc_server_phdc_recv_cb, context);
250
251         if(false == ret)
252         {
253                 NFC_ERR("net_nfc_controller_llcp_recv failed, [%d]", result);
254
255                 context->result = result;
256                 context->cb(context->result, &context->data, context->user_param);
257
258                 _net_nfc_server_phdc_destory_context(context);
259         }
260
261 }
262
263
264 static net_nfc_server_phdc_op_context_t* _net_nfc_server_phdc_create_recv_context(
265                 net_nfc_target_handle_s *handle, net_nfc_llcp_socket_t socket, void *cb,
266                 void *user_param)
267 {
268
269         bool ret;
270         net_nfc_error_e result;
271         net_nfc_server_phdc_op_context_t *context = NULL;
272         net_nfc_llcp_config_info_s config;
273
274         ret = net_nfc_controller_llcp_get_remote_config(handle, &config, &result);
275
276         if(false == ret)
277         {
278                 NFC_ERR("net_nfc_controller_llcp_get_remote_config failed, [%d]",       result);
279                 return NULL;
280         }
281
282         _net_nfc_util_alloc_mem(context, sizeof(*context));
283
284         RETV_IF(NULL == context, NULL);
285
286         context->handle = handle;
287         context->state = NET_NFC_LLCP_STEP_01;
288         context->socket = socket;
289         context->cb = cb;
290         context->user_param = user_param;
291         context->miu = MIN(config.miu, net_nfc_server_llcp_get_miu());
292
293         return context;
294 }
295
296
297 static net_nfc_error_e net_nfc_server_phdc_recv(net_nfc_target_handle_s *handle,
298                 net_nfc_llcp_socket_t socket, _net_nfc_server_phdc_operation_cb cb,
299                 void *user_param)
300 {
301         net_nfc_error_e result;
302         net_nfc_server_phdc_op_context_t *context = NULL;
303
304         /* create context */
305         context = _net_nfc_server_phdc_create_recv_context(handle, socket, cb, user_param);
306
307         if (context != NULL)
308         {
309                 _net_nfc_server_phdc_recv(context);
310                 result = NET_NFC_OK;
311         }
312         else
313         {
314                 result = NET_NFC_ALLOC_FAIL;
315
316                 if (cb != NULL)
317                         cb(result, NULL, user_param);
318         }
319
320         return result;
321 }
322
323
324 static void _net_nfc_server_phdc_agent_connected_cb(net_nfc_error_e result,
325                 net_nfc_target_handle_s *handle, net_nfc_llcp_socket_t socket, data_s *data,
326                 void *user_param)
327 {
328         net_nfc_server_phdc_context_t *context =
329                 (net_nfc_server_phdc_context_t *)user_param;
330
331         RET_IF(NULL == context);
332
333         context->socket = socket;
334
335         if (NET_NFC_OK == result)
336                 NFC_DBG("socket [%x] connected. send message", socket);
337         else
338                 NFC_ERR("connect socket failed, [%d]", result);
339
340         net_nfc_server_phdc_recv(handle, socket,
341                 _net_nfc_server_phdc_manager_op_cb,
342                 context);
343
344         if (context->cb != NULL)
345                 context->cb((net_nfc_phdc_handle_h)context, result,
346                         NET_NFC_PHDC_TARGET_CONNECTED, NULL);
347
348 }
349
350 net_nfc_error_e net_nfc_server_phdc_agent_connect(net_nfc_target_handle_s *handle,
351                 const char *san, sap_t sap,     net_nfc_server_phdc_cb cb, void *user_param)
352 {
353         bool ret;
354         net_nfc_error_e result;
355         net_nfc_server_phdc_context_t *context = NULL;
356
357         RETV_IF(NULL == handle, NET_NFC_NULL_PARAMETER);
358
359         ret = net_nfc_server_target_connected(handle);
360
361         if(FALSE == ret)
362                 return NET_NFC_NOT_CONNECTED;
363
364         _net_nfc_util_alloc_mem(context, sizeof(*context));
365
366         if (NULL == context)
367         {
368                 NFC_ERR("_net_nfc_util_alloc_mem failed");
369                 result = NET_NFC_ALLOC_FAIL;
370                 goto ERROR;
371         }
372
373         context->handle = handle;
374         context->cb = cb;
375         context->user_param = user_param;
376
377         result = net_nfc_server_llcp_simple_client(handle, san,         sap,
378                         _net_nfc_server_phdc_agent_connected_cb,
379                         _net_nfc_server_phdc_socket_error_cb,
380                         context);
381
382         if (result != NET_NFC_OK)
383         {
384                 NFC_ERR("net_nfc_server_llcp_simple_client failed, [%d]", result);
385                 goto ERROR;
386         }
387
388         if (san != NULL)
389                 NFC_DBG("start phdc agent san [%s]", san);
390         else
391                 NFC_DBG("start phdc agent, sap [%d]", sap);
392
393         return result;
394
395 ERROR :
396         if (context != NULL)
397         {
398                 if (context->data.buffer != NULL)
399                 {
400                         net_nfc_util_free_data(&context->data);
401                 }
402                 _net_nfc_util_free_mem(context);
403         }
404
405         return result;
406 }
407
408
409 static void _net_nfc_server_phdc_agent_do_job(
410                 net_nfc_server_phdc_context_t *context)
411 {
412         if (context->state == NET_NFC_LLCP_IDLE && g_queue_is_empty(&context->queue) == false)
413         {
414                 net_nfc_server_phdc_job_t *job;
415
416                 job = g_queue_pop_head(&context->queue);
417                 if (job != NULL)
418                 {
419                         context->state = NET_NFC_LLCP_STEP_01;
420                         job->state = NET_NFC_LLCP_STEP_01;
421                         _net_nfc_server_phdc_agent_process(job);
422                 }
423         }
424 }
425
426 static net_nfc_server_phdc_op_context_t* _net_nfc_server_phdc_create_send_context(
427                 net_nfc_target_handle_s *handle, net_nfc_llcp_socket_t socket, data_s *data,
428                 void *cb, void *user_param)
429 {
430         bool ret;
431         uint32_t data_len = 0;
432         uint16_t length;
433         uint8_t *buffer = NULL;
434         net_nfc_server_phdc_op_context_t *context = NULL;
435         net_nfc_llcp_config_info_s config;
436         net_nfc_error_e result;
437
438         ret = net_nfc_controller_llcp_get_remote_config(handle,
439                         &config, &result);
440
441         if(FALSE == ret)
442         {
443                 NFC_ERR("net_nfc_controller_llcp_get_remote_config failed, [%d]",       result);
444                 return NULL;
445         }
446
447         _net_nfc_util_alloc_mem(context, sizeof(*context));
448         RETV_IF(NULL == context, NULL);
449
450         if (data != NULL)
451                 data_len =data->length;
452
453         if (data_len > 0)
454         {
455                 net_nfc_util_alloc_data(&context->data,data_len+PHDC_HEADER_LEN);
456                 if (NULL == context->data.buffer)
457                 {
458                         _net_nfc_util_free_mem(context);
459                         return NULL;
460                 }
461
462                 length = (uint16_t)data->length;
463                 length +=PHDC_HEADER_LEN;
464                 buffer = context->data.buffer;
465
466                 if(data->buffer != NULL)
467                 {
468                         uint16_t network_length = htons(length);
469                         memcpy(buffer,&network_length,PHDC_HEADER_LEN);
470                         buffer+=PHDC_HEADER_LEN;
471                         memcpy(buffer,data->buffer,data->length);
472                         context->data.length = length;
473                 }
474         }
475
476         context->handle = handle;
477         context->socket = socket;
478         context->cb = cb;
479         context->user_param = user_param;
480         context->miu = MIN(config.miu, net_nfc_server_llcp_get_miu());
481
482         return context;
483 }
484
485 static void _net_nfc_server_phdc_send_cb(net_nfc_llcp_socket_t socket,
486                 net_nfc_error_e result, data_s *data, void *extra, void *user_param)
487 {
488         net_nfc_server_phdc_op_context_t *context =
489                 (net_nfc_server_phdc_op_context_t *)user_param;
490
491         NFC_DBG("_net_nfc_server_phdc_send_cb, socket[%x], result[%d]", socket, result);
492
493         RET_IF(NULL == context);
494
495         context->result = result;
496
497         /* complete and invoke callback */
498         context->cb(context->result, NULL, context->user_param);
499
500         _net_nfc_server_phdc_destory_context(context);
501 }
502
503
504 static void _net_nfc_server_phdc_send(net_nfc_server_phdc_op_context_t *context)
505 {
506         bool ret;
507         data_s req_msg;
508         net_nfc_error_e result;
509
510         RET_IF(NULL == context);
511
512         req_msg.length = context->data.length;
513         req_msg.buffer = context->data.buffer;
514
515         NFC_DBG("try to send data, socket [%x]",context->socket);
516
517         ret= net_nfc_controller_llcp_send(context->handle, context->socket,
518                         &req_msg, &result, _net_nfc_server_phdc_send_cb, context);
519
520         if(FALSE == ret)
521         {
522                 NFC_ERR("net_nfc_controller_llcp_send failed, [%d]",result);
523                 context->result = result;
524
525                 context->cb(context->result, NULL, context->user_param);
526                 _net_nfc_server_phdc_destory_context(context);
527         }
528 }
529
530 static void _net_nfc_server_phdc_agent_send_cb(net_nfc_error_e result,data_s *data,
531                 void *user_param)
532 {
533         net_nfc_server_phdc_job_t*job = (net_nfc_server_phdc_job_t *)user_param;
534
535         RET_IF(NULL == job);
536
537         job->result = result;
538
539         if (NET_NFC_OK ==result)
540         {
541                 job->state = NET_NFC_LLCP_STEP_RETURN;
542                 net_nfc_util_free_data(&job->data);
543         }
544         else
545         {
546                 NFC_ERR("net_nfc_server_phdc_send failed, [%d]", result);
547                 job->state = NET_NFC_STATE_ERROR;
548         }
549
550         _net_nfc_server_phdc_agent_process(job);
551 }
552
553
554 net_nfc_error_e net_nfc_server_phdc_send(net_nfc_target_handle_s *handle,
555                 net_nfc_llcp_socket_t socket, data_s *data, _net_nfc_server_phdc_operation_cb cb,
556                 void *user_param)
557 {
558         net_nfc_error_e result;
559         net_nfc_server_phdc_op_context_t *context = NULL;
560
561         /* create context */
562         context = _net_nfc_server_phdc_create_send_context(handle, socket,data,
563                         cb, user_param);
564
565         if (context != NULL)
566         {
567                 /* send data */
568                 _net_nfc_server_phdc_send(context);
569                 result = NET_NFC_OK;
570         }
571         else
572         {
573                 NFC_ERR("_net_nfc_server_phdc_create_send_context failed");
574                 result = NET_NFC_ALLOC_FAIL;
575
576                 if (cb != NULL)
577                         cb(result, NULL, user_param);
578         }
579
580         return result;
581 }
582
583 static void _net_nfc_server_phdc_agent_process(net_nfc_server_phdc_job_t *job)
584 {
585         bool finish = false;
586
587         RET_IF(NULL == job);
588
589         switch (job->state)
590         {
591         case NET_NFC_LLCP_STEP_01 :
592                 NFC_DBG("NET_NFC_LLCP_STEP_01");
593
594                 /* send request */
595                 net_nfc_server_phdc_send(job->handle, job->socket,      &job->data,
596                                 _net_nfc_server_phdc_agent_send_cb, job);
597                 break;
598
599         case NET_NFC_LLCP_STEP_RETURN :
600                 NFC_DBG("NET_NFC_LLCP_STEP_RETURN");
601
602                 /* complete and invoke callback */
603                 if (job->cb != NULL)
604                 {
605                         job->cb((net_nfc_phdc_handle_h)job->context, NET_NFC_OK,
606                                 NET_NFC_PHDC_DATA_RECEIVED, job->user_param);
607                 }
608                 /* finish job */
609                 finish = true;
610                 break;
611
612         case NET_NFC_STATE_ERROR :
613         default :
614                 NFC_ERR("NET_NFC_STATE_ERROR");
615
616                 /* error, invoke callback */
617                 NFC_ERR("phdc_agent_send failed, [%d]", job->result);
618                 if (job->cb != NULL)
619                 {
620                         job->cb((net_nfc_phdc_handle_h)job->context, job->result,
621                                 NET_NFC_PHDC_OPERATION_FAILED, job->user_param);
622                 }
623                 /* finish job */
624                 finish = TRUE;
625                 break;
626         }
627
628         if (TRUE == finish)
629         {
630                 net_nfc_server_phdc_context_t *context = job->context;
631
632                 if (job->data.buffer != NULL)
633                         net_nfc_util_free_data(&job->data);
634
635                 _net_nfc_util_free_mem(job);
636                 context->state = NET_NFC_LLCP_IDLE;
637                 _net_nfc_server_phdc_agent_do_job(context);
638         }
639 }
640
641
642 net_nfc_error_e net_nfc_server_phdc_agent_request(net_nfc_phdc_handle_h handle,
643                 data_s *data, net_nfc_server_phdc_send_cb cb, void *user_param)
644 {
645         bool ret;
646         net_nfc_error_e result = NET_NFC_OK;
647         net_nfc_server_phdc_job_t *job = NULL;
648         net_nfc_server_phdc_context_t *context = (net_nfc_server_phdc_context_t *)handle;
649
650         RETV_IF(NULL == handle, NET_NFC_NULL_PARAMETER);
651         RETV_IF(NULL == data, NET_NFC_NULL_PARAMETER);
652         RETV_IF(NULL == data->buffer, NET_NFC_NULL_PARAMETER);
653
654         ret =net_nfc_server_target_connected(context->handle);
655
656         if(FALSE == ret)
657                 return NET_NFC_NOT_CONNECTED;
658
659         /* check type */
660         _net_nfc_util_alloc_mem(job, sizeof(*job));
661         if (job != NULL)
662         {
663                 net_nfc_util_alloc_data(&job->data, data->length);
664                 if (job->data.buffer != NULL)
665                         memcpy(job->data.buffer, data->buffer, data->length);
666
667                 job->cb = cb;
668                 job->user_param = user_param;
669                 job->context = context;
670                 job->handle = context->handle;
671                 job->socket = context->socket;
672                 g_queue_push_tail(&context->queue, job);
673         }
674         else
675                 return NET_NFC_ALLOC_FAIL;
676
677         NFC_INFO("enqueued jobs [%d]", g_queue_get_length(&context->queue));
678
679         /* if agent is idle, starts sending request */
680         if (context->state == NET_NFC_LLCP_IDLE)
681                 _net_nfc_server_phdc_agent_do_job(context);
682         else
683                 NFC_INFO("agent is working. this job will be enqueued");
684
685         return result;
686 }
687
688
689 net_nfc_error_e net_nfc_server_phdc_agent_start(net_nfc_target_handle_s *handle,
690                 const char *san, sap_t sap, net_nfc_server_phdc_cb cb, void *user_param)
691 {
692         net_nfc_error_e result;
693
694         RETV_IF(NULL == handle, NET_NFC_NULL_PARAMETER);
695         RETV_IF(NULL == san, NET_NFC_NULL_PARAMETER);
696
697                 /* start PHDC Agent, register your callback */
698                 result =  net_nfc_server_phdc_agent_connect(handle, san, sap,
699                                 cb, user_param);
700
701                 if (result != NET_NFC_OK)
702                 {
703                         NFC_ERR("net_nfc_server_phdc_agent_connect failed, [%d]",result);
704                 }
705                 return result;
706 }
707
708 static void _net_nfc_server_phdc_manager_op_cb( net_nfc_error_e result, data_s *data,
709                 void *user_param)
710 {
711         net_nfc_server_phdc_context_t *context =
712                 (net_nfc_server_phdc_context_t *)user_param;
713
714         NFC_DBG("_net_nfc_server_phdc_manager_op_cb result [%d]", result);
715
716         RET_IF(NULL == context);
717
718         context->result = result;
719         if (NET_NFC_OK == result && data != NULL && data->buffer != NULL)
720         {
721                 NFC_DBG("received message, length [%d]", data->length);
722
723                 net_nfc_util_alloc_data(&context->data, data->length);
724                 if (context->data.buffer != NULL)
725                 {
726                         memcpy(context->data.buffer,data->buffer, data->length);
727                         context->state = NET_NFC_LLCP_STEP_02;
728                 }
729                 else
730                 {
731                         NFC_ERR("net_nfc_util_alloc_data failed");
732                         context->state = NET_NFC_STATE_ERROR;
733                         context->result = NET_NFC_ALLOC_FAIL;
734                 }
735         }
736         else
737         {
738                 NFC_ERR("net_nfc_server_phdc_recv failed, [%d]", result);
739                 context->state = NET_NFC_STATE_ERROR;
740         }
741
742         _net_nfc_server_phdc_manager_process(context);
743
744 }
745
746
747 static void _net_nfc_server_phdc_manager_process(
748                 net_nfc_server_phdc_context_t *context)
749 {
750         bool finish = false;
751         RET_IF(NULL == context);
752
753         switch (context->state)
754         {
755         case NET_NFC_LLCP_STEP_01 :
756                 NFC_INFO("NET_NFC_LLCP_STEP_01 ");
757                 /*receive the Agent data*/
758                 net_nfc_server_phdc_recv(       context->handle,context->socket,
759                                 _net_nfc_server_phdc_manager_op_cb, context);
760                 break;
761         case NET_NFC_LLCP_STEP_02 :
762                 NFC_INFO("NET_NFC_LLCP_STEP_02");
763                 if (context->cb != NULL)
764                 {
765                         /* complete operation and invoke the callback*/
766                         context->cb((net_nfc_phdc_handle_h)context,NET_NFC_OK,
767                                         NET_NFC_PHDC_DATA_RECEIVED, &context->data);
768                 }
769                 finish = true;
770                 break;
771         case NET_NFC_STATE_ERROR :
772                 NFC_INFO("NET_NFC_STATE_ERROR");
773         default :
774                 if (context->cb != NULL)
775                 {
776                         context->cb((net_nfc_phdc_handle_h)context,context->result,
777                                         NET_NFC_PHDC_OPERATION_FAILED,&context->data);
778                 }
779                 return;
780                 break;
781         }
782
783         if(true == finish)
784         {
785                 context->state = NET_NFC_LLCP_IDLE;
786
787                 net_nfc_server_phdc_recv(context->handle,
788                         context->socket,
789                         _net_nfc_server_phdc_manager_op_cb,
790                         context);
791
792         }
793 }
794
795
796 static void _net_nfc_server_phdc_incoming_cb(net_nfc_error_e result,
797                 net_nfc_target_handle_s *handle, net_nfc_llcp_socket_t socket, data_s *data,
798                 void *user_param)
799 {
800         net_nfc_server_phdc_context_t *context =
801                 (net_nfc_server_phdc_context_t *)user_param;
802
803         net_nfc_server_phdc_context_t *accept_context = NULL;
804
805         RET_IF(NULL == context);
806
807         NFC_DBG("phdc incoming socket [%x], result [%d]", socket, result);
808
809         if (result != NET_NFC_OK)
810         {
811                 NFC_ERR("listen socket failed, [%d]", result);
812                 goto ERROR;
813         }
814
815         _net_nfc_util_alloc_mem(accept_context, sizeof(*accept_context));
816
817         if (NULL == accept_context)
818         {
819                 NFC_ERR("_net_nfc_util_alloc_mem failed");
820                 result = NET_NFC_ALLOC_FAIL;
821                 goto ERROR;
822         }
823
824         accept_context->handle = context->handle;
825         accept_context->socket = socket;
826         accept_context->cb = context->cb;
827         accept_context->user_param = context->user_param;
828         accept_context->state = NET_NFC_LLCP_STEP_01;
829
830         result = net_nfc_server_llcp_simple_accept(handle, socket,
831                         _net_nfc_server_phdc_socket_error_cb, accept_context);
832
833         if (result != NET_NFC_OK)
834         {
835                 NFC_ERR("net_nfc_server_llcp_simple_accept failed, [%d]",result);
836                 goto ERROR;
837         }
838
839         NFC_DBG("socket [%x] accepted.. waiting for request message", socket);
840
841         accept_context->cb((net_nfc_phdc_handle_h)accept_context, result,
842                         NET_NFC_PHDC_TARGET_CONNECTED, data);
843
844         _net_nfc_server_phdc_manager_process(accept_context);
845
846         return;
847 ERROR :
848         if (accept_context != NULL)
849         {
850                 _net_nfc_util_free_mem(accept_context);
851         }
852 }
853
854 net_nfc_error_e net_nfc_server_phdc_manager_start(net_nfc_target_handle_s *handle,
855                 const char *san, sap_t sap,     net_nfc_server_phdc_cb cb,      void *user_param)
856 {
857         bool ret;
858         net_nfc_error_e result;
859         net_nfc_server_phdc_context_t *context = NULL;
860
861         RETV_IF(NULL == handle, NET_NFC_NULL_PARAMETER);
862         RETV_IF(NULL == san, NET_NFC_NULL_PARAMETER);
863
864         ret = net_nfc_server_target_connected(handle);
865
866         if(FALSE == ret)
867                 return NET_NFC_NOT_CONNECTED;
868
869         _net_nfc_util_alloc_mem(context, sizeof(*context));
870         if(NULL == context)
871         {
872                 NFC_ERR("_create_phdc_context failed");
873                 result = NET_NFC_ALLOC_FAIL;
874                 goto ERROR;
875         }
876
877         context->handle = handle;
878         context->cb = cb;
879         context->user_param = user_param;
880         context->state = NET_NFC_LLCP_STEP_01;
881
882         if(!strcmp(san,PHDC_SAN))
883         {
884                 /*Handle the default PHDC SAN*/
885                 result = net_nfc_server_llcp_simple_server(handle,      san,sap,
886                                 _net_nfc_server_phdc_incoming_cb,_net_nfc_server_phdc_socket_error_cb,
887                                 context);
888         }
889         else
890         {
891                 /*Handle other SAN, Implement as and when needed.*/
892                 result = NET_NFC_NOT_SUPPORTED;
893         }
894
895         if (result != NET_NFC_OK)
896         {
897                 NFC_ERR("net_nfc_server_llcp_simple_server failed, [%d]",result);
898                 goto ERROR;
899         }
900
901         NFC_DBG("start phdc manager, san [%s], sap [%d]", san, sap);
902         return result;
903
904 ERROR :
905         if (context != NULL)
906                 _net_nfc_util_free_mem(context);
907
908         return result;
909 }
910
911 ///////////////////////////////////////////////////////////////////
912