NFC hal : Change for nfc backend operation
[platform/adaptation/emulator/nfc-plugin-emul.git] / src / oem_emul.c
1 /*
2 * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <unistd.h>
19 #include <dirent.h>
20 #include <fcntl.h>
21 #include <grp.h>
22 #include <pthread.h>
23 #include <pwd.h>
24 #include <stdbool.h>
25 #include <sys/stat.h>
26 #include <sys/epoll.h>
27 #include <vconf.h>
28
29 #include <arpa/inet.h>
30 #include <hal-nfc-interface.h>
31 #include "nfc_debug_private.h"
32
33 #ifndef EXPORT
34 #define EXPORT __attribute__ ((visibility("default")))
35 #endif
36
37 #define NET_NFC_NDEF_RECORD_MASK_MB             0x80    /**< This marks the begin of a NDEF Message. */
38 #define NET_NFC_NDEF_RECORD_MASK_ME             0x40    /**< Set if the record is at the Message End. */
39 #define NET_NFC_NDEF_RECORD_MASK_CF             0x20    /**< Chunk Flag: The record is a record chunk only. */
40 #define NET_NFC_NDEF_RECORD_MASK_SR             0x10    /**< Short Record: Payload Length is encoded in ONE byte only. */
41 #define NET_NFC_NDEF_RECORD_MASK_IL             0x08    /**< The ID Length Field is present. */
42 #define NET_NFC_NDEF_RECORD_MASK_TNF            0x07    /**< Type Name Format. */
43
44 typedef enum {
45         EMUL_NFC_TAG_DISCOVERED = 100,
46         EMUL_NFC_TAG_DETACHED,
47         EMUL_NFC_P2P_DISCOVERED,
48         EMUL_NFC_P2P_DETACHED,
49         EMUL_NFC_P2P_SEND,
50         EMUL_NFC_UNKNOWN_MSG
51 } emul_message_id;
52
53 typedef enum {
54         EMUL_TAG_TOPAZ_JEWEL = 1,
55         EMUL_TAG_MIFARE_UL,
56         EMUL_TAG_FELICA,
57         EMUL_TAG_MIFARE_DESFIRE,
58         EMUL_NFC_TARGET,
59         EMUL_TARGET_TYPE_MAX
60 } emul_target_type;
61
62 typedef enum {
63         EMUL_NDEF_TNF_EMPTY = 0,
64         EMUL_NDEF_TNF_WELL_KNOWN,
65         EMUL_NDEF_TNF_MIME_MEDIA,
66         EMUL_NDEF_TNF_ABSOLUTE_URI,
67         EMUL_NDEF_TNF_EXTERNAL,
68         EMUL_NDEF_TNF_UNKNOWN,
69         EMUL_NDEF_TNF_UNCHANGED
70 } emul_ndef_name_format;
71
72 typedef enum {
73         NET_NFC_STATE_EXCHANGER_SERVER = 0x00,
74         NET_NFC_STATE_EXCHANGER_SERVER_NPP,
75
76         NET_NFC_STATE_EXCHANGER_CLIENT,
77         NET_NFC_STATE_CONN_HANDOVER_REQUEST,
78         NET_NFC_STATE_CONN_HANDOVER_SELECT,
79         NET_NFC_STATE_UNKNOWN
80 } llcp_state_e;
81
82 typedef enum {
83         SNEP_REQ_CONTINUE = 0x00,
84         SNEP_REQ_GET = 0x01,
85         SNEP_REQ_PUT = 0x02,
86         SNEP_REQ_REJECT = 0x7F,
87         SNEP_RESP_CONT = 0x80,
88         SNEP_RESP_SUCCESS = 0x81,
89         SNEP_RESP_NOT_FOUND = 0xC0,
90         SNEP_RESP_EXCESS_DATA = 0xC1,
91         SNEP_RESP_BAD_REQ = 0xC2,
92         SNEP_RESP_NOT_IMPLEMENT = 0xE0,
93         SNEP_RESP_UNSUPPORTED_VER = 0xE1,
94         SNEP_RESP_REJECT = 0xFF,
95 } snep_command_field_e;
96
97 typedef struct _socket_info_s {
98         net_nfc_llcp_socket_t socket_handle;
99         bool isValid;
100         void *user_context;
101 } socket_info_s;
102
103 typedef struct _net_nfc_oem_llcp_state_t {
104         int client_fd;
105         unsigned int step;
106         unsigned int fragment_offset;
107         llcp_state_e state;
108         net_nfc_llcp_socket_t socket;
109         uint16_t max_capability;
110         net_nfc_target_handle_s *handle;
111         net_nfc_error_e prev_result;
112         net_nfc_llcp_socket_t incomming_socket;
113         ndef_message_s *requester;
114         ndef_message_s *selector;
115         bool low_power;
116         void *user_data;
117         void *payload;
118
119         llcp_app_protocol_e type_app_protocol;
120         net_nfc_conn_handover_carrier_type_e type;
121
122 } net_nfc_oem_llcp_state_t;
123
124 typedef struct _snep_msg_s {
125         data_s *data;
126         int offset;
127         bool isSegment;
128
129         /* Members below are used for snep msg of client */
130         bool firstTime;
131         bool RespContinue;
132 } snep_msg_s;
133
134 typedef struct _emulMsg_data_s {
135         net_nfc_record_tnf_e tnf;
136         data_s typeName;
137         data_s id;
138         data_s payload;
139
140         bool realRecord;
141 } emulMsg_data_s;
142
143 typedef struct _emulMsg_s {
144         emul_message_id message_id;
145         emul_target_type target_type;
146         int record_count;
147         uint8_t *file_data;
148 } emulMsg_s;
149
150 typedef void* (*emul_Nfc_thread_handler_t)(void *pParam);
151
152 #define __USE_EPOLL_FOR_FILE__ 1
153
154 /* for emulator management */
155 #define NET_NFC_EMUL_DATA_PATH                          "/dev"
156 #define NET_NFC_EMUL_MESSAGE_FILE_NAME          "nfc0"
157 #define NET_NFC_EMUL_MSG_ID_SEPERATOR                   "\n:"
158 #define NET_NFC_EMUL_MSG_DATA_SEPERATOR         "\n\0"
159 #define NET_NFC_EMUL_MSG_RECORD_SEPERATOR       "\n,"
160 #define NET_NFC_EMUL_TAG_DISCOVERED_DATA_FORMAT "%d,%d,%[^\n]"
161 #define NET_NFC_EMUL_HEADER_LENGTH              8
162
163 #ifdef __USE_EPOLL_FOR_FILE__
164 #define EPOLL_SIZE 128
165 #endif
166
167 /* for llcp functionality */
168 #define LLCP_NB_SOCKET_MAX 5
169 #define NET_NFC_EMUL_SNEP_SERVER_SOCKET_NUMBER 0
170 #define NET_NFC_EMUL_INCOMING_SOCKET_NUMBER 0
171 #define NET_NFC_EMUL_NPP_SERVER_SOCKET_NUMBER 1
172 #define NET_NFC_EMUL_CLIENT_SOCKET_NUMBER 2
173 #define NET_NFC_EMUL_HANDOVER_REQUEST_SOCKET_NUMBER 3
174 #define NET_NFC_EMUL_HANDOVER_SELECT_SOCKET_NUMBER 4
175
176 #define SNEP_MAX_BUFFER 128
177 #define SNEP_MAJOR_VER 1
178 #define SNEP_MINOR_VER 0
179
180 /* static variable */
181 #define BUFFER_LENGTH_MAX 4096
182 #define READ_BUFFER_LENGTH_MAX BUFFER_LENGTH_MAX
183 #define WRITE_BUFFER_LENGTH_MAX BUFFER_LENGTH_MAX
184 #define NET_NFC_MAX_LLCP_SOCKET_BUFFER BUFFER_LENGTH_MAX
185
186 /* listener callback */
187 static target_detection_listener_cb g_emul_controller_target_cb;
188 static se_transaction_listener_cb g_emul_controller_se_cb;
189 static llcp_event_listener_cb g_emul_controller_llcp_cb;
190 static hce_apdu_listener_cb g_emul_controller_hce_cb;
191
192 /* for emulator management */
193 static pthread_t gEmulThread;
194
195 /* for stack management */
196 static net_nfc_target_handle_s *current_working_handle = NULL;
197 static bool g_stack_init_successful = 0;
198 static bool g_tag_attached = 0;
199 static bool g_p2p_attached = 0;
200 static int current_connection_id = 1;
201 /* for llcp functionality */
202
203 static socket_info_s socket_info_array[LLCP_NB_SOCKET_MAX] = {{0}};
204
205 static snep_msg_s *Snep_Server_msg;
206 static data_s *llcp_server_data = NULL;
207
208 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
209
210
211 /* Memory utils */
212 /* free memory, after free given memory it set NULL. Before proceed free, this function also check NULL */
213 static void __nfc_emul_util_free_mem(void **mem, char *filename, unsigned int line);
214 /* allocation memory */
215 static void __nfc_emul_util_alloc_mem(void **mem, int size, char *filename, unsigned int line);
216 #define _nfc_emul_util_alloc_mem(mem, size) __nfc_emul_util_alloc_mem((void**)&mem, size, __FILE__, __LINE__)
217 #define _nfc_emul_util_free_mem(mem) __nfc_emul_util_free_mem((void**)&mem, __FILE__, __LINE__)
218
219
220 static void __nfc_emul_util_free_mem(void **mem, char *filename, unsigned int line)
221 {
222         if (mem == NULL || *mem == NULL) {
223                 LOGE("FILE: %s, LINE:%d, Invalid parameter in mem free util (pinter is NULL)", filename, line);
224                 return;
225         }
226         free(*mem);
227         *mem = NULL;
228 }
229
230 static void __nfc_emul_util_alloc_mem(void **mem, int size, char *filename, unsigned int line)
231 {
232         if (mem == NULL || size <= 0) {
233                 LOGE("FILE: %s, LINE:%d, Invalid parameter in mem alloc util", filename, line);
234                 return;
235         }
236
237         DEBUG_MSG("size to malloc() = [%d]", size);
238
239         if (*mem != NULL)
240                 LOGE("FILE: %s, LINE:%d, WARNING: Pointer is already allocated or it was not initialized with NULL", filename, line);
241
242         *mem = calloc(1, size);
243
244         if (NULL == *mem)
245                 LOGE("FILE: %s, LINE:%d, Allocation is failed", filename, line);
246 }
247
248 static bool _net_nfc_util_init_data(data_s * data, uint32_t length)
249 {
250         if (data == NULL || length == 0)
251                 return false;
252
253         _nfc_emul_util_alloc_mem(data->buffer, length);
254         if (data->buffer == NULL)
255                 return false;
256
257         data->length = length;
258
259         return true;
260 }
261
262 static void _net_nfc_util_clear_data(data_s * data)
263 {
264         if (data == NULL)
265                 return;
266
267         if (data->buffer != NULL) {
268                 _nfc_emul_util_free_mem(data->buffer);
269                 data->buffer = NULL;
270         }
271
272         data->length = 0;
273 }
274
275 static net_nfc_error_e _net_nfc_util_free_record(ndef_record_s * record)
276 {
277         if (record == NULL)
278                 return NET_NFC_NULL_PARAMETER;
279
280         _net_nfc_util_clear_data(&record->type_s);
281         _net_nfc_util_clear_data(&record->id_s);
282         _net_nfc_util_clear_data(&record->payload_s);
283
284         _nfc_emul_util_free_mem(record);
285
286         return NET_NFC_OK;
287 }
288
289 static net_nfc_error_e _net_nfc_util_free_ndef_message(ndef_message_s * msg)
290 {
291         int idx = 0;
292         ndef_record_s *prev, *current;
293
294         if (msg == NULL)
295                 return NET_NFC_NULL_PARAMETER;
296
297         current = msg->records;
298
299         for (idx = 0; idx < msg->recordCount; idx++) {
300                 if (current == NULL)
301                         break;
302
303                 prev = current;
304                 current = current->next;
305
306                 _net_nfc_util_free_record(prev);
307         }
308
309         _nfc_emul_util_free_mem(msg);
310
311         return NET_NFC_OK;
312 }
313
314 static net_nfc_error_e _net_nfc_util_create_record(net_nfc_record_tnf_e recordType, data_s * typeName, data_s * id, data_s * payload, ndef_record_s ** record)
315 {
316         ndef_record_s *record_temp = NULL;
317
318         if (record == NULL)
319                 return NET_NFC_NULL_PARAMETER;
320
321         if (recordType < NET_NFC_RECORD_EMPTY || recordType > NET_NFC_RECORD_UNCHAGNED)
322                 return NET_NFC_OUT_OF_BOUND;
323
324         /* empty_tag */
325         if (recordType == NET_NFC_RECORD_EMPTY) {
326                 if ((typeName != NULL && typeName->length > 0) || (payload != NULL && payload->length > 0) || (id != NULL && id->length > 0))
327                         return NET_NFC_INVALID_PARAM;
328         } else {
329                 if (typeName == NULL || typeName->buffer == NULL || typeName->length == 0)
330                         return NET_NFC_INVALID_PARAM;
331         }
332
333         _nfc_emul_util_alloc_mem(record_temp, sizeof(ndef_record_s));
334         if (record_temp == NULL)
335                 return NET_NFC_ALLOC_FAIL;
336         // set type name and length and  TNF field
337         record_temp->TNF = recordType;
338
339         if (typeName != NULL && typeName->length > 0) {
340                 if (_net_nfc_util_init_data(&record_temp->type_s, typeName->length) == false) {
341                         _nfc_emul_util_free_mem(record_temp);
342
343                         return NET_NFC_ALLOC_FAIL;
344                 }
345
346                 memcpy(record_temp->type_s.buffer, typeName->buffer, record_temp->type_s.length);
347         } else {
348                 record_temp->type_s.buffer = NULL;
349                 record_temp->type_s.length = 0;
350         }
351
352         record_temp->SR = 1;
353
354         // set payload
355         if (payload != NULL && payload->length > 0) {
356                 if (_net_nfc_util_init_data(&record_temp->payload_s, payload->length) == false) {
357                         _net_nfc_util_clear_data(&record_temp->type_s);
358                         _nfc_emul_util_free_mem(record_temp);
359
360                         return NET_NFC_ALLOC_FAIL;
361                 }
362
363                 memcpy(record_temp->payload_s.buffer, payload->buffer, record_temp->payload_s.length);
364
365                 if (payload->length > 255)
366                         record_temp->SR = 0;
367         } else {
368                 record_temp->payload_s.buffer = NULL;
369                 record_temp->payload_s.length = 0;
370         }
371
372         // set id and id length and IL field
373         if (id != NULL && id->buffer != NULL && id->length > 0) {
374                 if (_net_nfc_util_init_data(&record_temp->id_s, id->length) == false) {
375                         _net_nfc_util_clear_data(&record_temp->payload_s);
376                         _net_nfc_util_clear_data(&record_temp->type_s);
377                         _nfc_emul_util_free_mem(record_temp);
378
379                         return NET_NFC_ALLOC_FAIL;
380                 }
381
382                 memcpy(record_temp->id_s.buffer, id->buffer, record_temp->id_s.length);
383                 record_temp->IL = 1;
384         } else {
385                 record_temp->IL = 0;
386                 record_temp->id_s.buffer = NULL;
387                 record_temp->id_s.length = 0;
388         }
389
390         // this is default value
391         record_temp->MB = 1;
392         record_temp->ME = 1;
393
394         record_temp->next = NULL;
395
396         *record = record_temp;
397
398         return NET_NFC_OK;
399 }
400
401 static net_nfc_error_e _net_nfc_util_append_record(ndef_message_s * msg, ndef_record_s * record)
402 {
403         if (msg == NULL || record == NULL)
404                 return NET_NFC_NULL_PARAMETER;
405
406         if (msg->recordCount == 0) {
407                 // set short message and append
408                 record->MB = 1;
409                 record->ME = 1;
410                 record->next = NULL;
411
412                 msg->records = record;
413
414                 msg->recordCount++;
415
416                 DEBUG_MSG("record is added to NDEF message :: count [%d]", msg->recordCount);
417         } else {
418                 ndef_record_s *current = NULL;
419                 ndef_record_s *prev = NULL;
420
421                 // set flag :: this record is FIRST
422                 current = msg->records;
423
424                 if (current != NULL) {
425                         // first node
426                         current->MB = 1;
427                         current->ME = 0;
428
429                         prev = current;
430
431                         // second node
432                         current = current->next;
433
434                         while (current != NULL) {
435                                 current->MB = 0;
436                                 current->ME = 0;
437                                 prev = current;
438                                 current = current->next;
439                         }
440
441                         // set flag :: this record is END
442                         record->MB = 0;
443                         record->ME = 1;
444
445                         prev->next = record;
446                         msg->recordCount++;
447                 }
448         }
449
450         return NET_NFC_OK;
451 }
452
453 static uint32_t _net_nfc_util_get_record_length(ndef_record_s * Record)
454 {
455         uint32_t RecordLength = 1;
456
457         if (Record == NULL)
458                 return 0;
459
460         /* Type length is present only for following TNF
461            NET_NFC_TNF_NFCWELLKNOWN
462            NET_NFC_TNF_MEDIATYPE
463            SLP_FRINET_NFC_NDEFRECORD_TNF_ABSURI
464            SLP_FRINET_NFC_NDEFRECORD_TNF_NFCEXT
465          */
466
467         /* ++ is for the Type Length Byte */
468         RecordLength++;
469         if (Record->TNF != EMUL_NDEF_TNF_EMPTY && Record->TNF != EMUL_NDEF_TNF_UNKNOWN && Record->TNF != EMUL_NDEF_TNF_UNCHANGED)
470                 RecordLength += Record->type_s.length;
471
472         /* to check if payloadlength is 8bit or 32bit */
473         if (Record->SR != 0) {
474                 /* ++ is for the Payload Length Byte */
475                 RecordLength++;                 /* for short record */
476         } else {
477                 /* + NET_NFC_NDEF_NORMAL_RECORD_BYTE is for the Payload Length Byte */
478                 RecordLength += 4;
479         }
480
481         /* for non empty record */
482         if (Record->TNF != EMUL_NDEF_TNF_EMPTY)
483                 RecordLength += Record->payload_s.length;
484
485         /* ID and IDlength are present only if IL flag is set */
486         if (Record->IL != 0) {
487                 RecordLength += Record->id_s.length;
488                 /* ++ is for the ID Length Byte */
489                 RecordLength++;
490         }
491
492         return RecordLength;
493 }
494
495 static uint32_t _net_nfc_util_get_ndef_message_length(ndef_message_s * message)
496 {
497         ndef_record_s *current;
498         int total = 0;
499
500         if (message == NULL)
501                 return 0;
502
503         current = message->records;
504
505         while (current != NULL) {
506                 total += _net_nfc_util_get_record_length(current);
507                 current = current->next;
508         }
509
510         return total;
511 }
512
513 static net_nfc_error_e _net_nfc_util_create_ndef_message(ndef_message_s ** ndef_message)
514 {
515         if (ndef_message == NULL)
516                 return NET_NFC_NULL_PARAMETER;
517
518         _nfc_emul_util_alloc_mem(*ndef_message, sizeof(ndef_message_s));
519         if (*ndef_message == NULL)
520                 return NET_NFC_ALLOC_FAIL;
521
522         return NET_NFC_OK;
523 }
524
525 static net_nfc_error_e _net_nfc_util_convert_ndef_message_to_rawdata(ndef_message_s * ndef, data_s * rawdata)
526 {
527         ndef_record_s *record = NULL;
528         uint8_t *current = NULL;
529         uint8_t ndef_header;
530
531         if (rawdata == NULL || ndef == NULL)
532                 return NET_NFC_NULL_PARAMETER;
533
534         record = ndef->records;
535         current = rawdata->buffer;
536
537         while (record) {
538                 ndef_header = 0x00;
539
540                 if (record->MB)
541                         ndef_header |= NET_NFC_NDEF_RECORD_MASK_MB;
542                 if (record->ME)
543                         ndef_header |= NET_NFC_NDEF_RECORD_MASK_ME;
544                 if (record->CF)
545                         ndef_header |= NET_NFC_NDEF_RECORD_MASK_CF;
546                 if (record->SR)
547                         ndef_header |= NET_NFC_NDEF_RECORD_MASK_SR;
548                 if (record->IL)
549                         ndef_header |= NET_NFC_NDEF_RECORD_MASK_IL;
550
551                 ndef_header |= record->TNF;
552
553                 *current++ = ndef_header;
554
555                 /* check empty record */
556                 if (record->TNF == EMUL_NDEF_TNF_EMPTY) {
557                         /* set type length to zero */
558                         *current++ = 0x00;
559
560                         /* set payload length to zero */
561                         *current++ = 0x00;
562
563                         /* set ID length to zero */
564                         if (record->IL)
565                                 *current++ = 0x00;
566
567                         record = record->next;
568
569                         continue;
570                 }
571
572
573                 /* set type length */
574                 if (record->TNF == EMUL_NDEF_TNF_UNKNOWN || record->TNF == EMUL_NDEF_TNF_UNCHANGED)
575                         *current++ = 0x00;
576                 else
577                         *current++ = record->type_s.length;
578
579                 /* set payload length */
580                 if (record->SR) {
581                         *current++ = (uint8_t) (record->payload_s.length & 0x000000FF);
582                 } else {
583                         *current++ = (uint8_t) ((record->payload_s.length & 0xFF000000) >> 24);
584                         *current++ = (uint8_t) ((record->payload_s.length & 0x00FF0000) >> 16);
585                         *current++ = (uint8_t) ((record->payload_s.length & 0x0000FF00) >> 8);
586                         *current++ = (uint8_t) (record->payload_s.length & 0x000000FF);
587                 }
588
589                 /* set ID length */
590                 if (record->IL)
591                         *current++ = record->id_s.length;
592
593                 /* set type buffer */
594                 if ((record->TNF != EMUL_NDEF_TNF_UNKNOWN) && (record->TNF != EMUL_NDEF_TNF_UNCHANGED)) {
595                         memcpy(current, record->type_s.buffer, record->type_s.length);
596                         current += record->type_s.length;
597                 }
598
599                 /* set ID buffer */
600                 memcpy(current, record->id_s.buffer, record->id_s.length);
601                 current += record->id_s.length;
602
603                 /* set payload buffer */
604                 memcpy(current, record->payload_s.buffer, record->payload_s.length);
605                 current += record->payload_s.length;
606
607                 record = record->next;
608         }
609
610         return NET_NFC_OK;
611 }
612
613 static bool __net_nfc_is_valide_target_handle(net_nfc_target_handle_s *handle)
614 {
615         bool result = (current_working_handle == handle);
616         if (!result)
617                 DEBUG_MSG("[WARNING]: INVALID HANDLE IS DETECTED!");
618
619         return result;
620 }
621
622 static void __net_nfc_make_valid_target_handle(net_nfc_target_handle_s **handle)
623 {
624         if (current_working_handle != NULL)
625                 DEBUG_MSG("[WARNING]: HANDLE WAS ALLOCATED ALREADY!");
626
627         _nfc_emul_util_alloc_mem(*handle, sizeof(net_nfc_target_handle_s));
628         if (*handle != NULL)
629                 current_working_handle = *handle;
630 }
631
632 static void __net_nfc_make_invalid_target_handle()
633 {
634         if (current_working_handle != NULL) {
635                 _nfc_emul_util_free_mem(current_working_handle);
636                 current_working_handle = NULL;
637         }
638 }
639
640 static socket_info_s* _net_nfc_get_available_socket_slot()
641 {
642         int idx = 0;
643         for (; idx < LLCP_NB_SOCKET_MAX; idx++) {
644                 if (socket_info_array[idx].isValid == false) {
645                         memset(&(socket_info_array[idx]), 0x00, sizeof(socket_info_s));
646                         socket_info_array[idx].isValid = true;
647                         return &(socket_info_array[idx]);
648                 }
649         }
650
651         DEBUG_ERR_MSG("_net_nfc_get_available_socket_slot is failed");
652         return NULL;
653 }
654
655 static void _net_nfc_remove_socket_slot(net_nfc_llcp_socket_t socket)
656 {
657         int idx = 0;
658
659         for (; idx < LLCP_NB_SOCKET_MAX; idx++) {
660                 if (socket_info_array[idx].isValid == true &&
661                         socket_info_array[idx].socket_handle == socket) {
662                         socket_info_array[idx].isValid = false;
663                         socket_info_array[idx].socket_handle = 0;
664                         socket_info_array[idx].user_context = NULL;
665                 }
666         }
667 }
668
669 static socket_info_s* _net_nfc_find_server_socket(net_nfc_llcp_socket_t socket)
670 {
671         int idx = 0;
672         for (; idx < LLCP_NB_SOCKET_MAX; idx++) {
673                 if (socket_info_array[idx].socket_handle == socket && socket_info_array[idx].isValid == true)
674                         return &(socket_info_array[idx]);
675         }
676
677         DEBUG_ERR_MSG("_net_nfc_find_server_socket is failed");
678         return NULL;
679 }
680
681 static void _net_nfc_initialize_llcp(void)
682 {
683         DEBUG_EMUL_BEGIN();
684
685         if (Snep_Server_msg == NULL) {
686                 Snep_Server_msg = calloc(1, sizeof(snep_msg_s));
687                 if (Snep_Server_msg == NULL)
688                         return;
689
690                 Snep_Server_msg->data = calloc(1, sizeof(data_s));
691                 if (Snep_Server_msg->data == NULL)
692                         return;
693
694                 Snep_Server_msg->data->buffer = calloc(1, sizeof(uint8_t) * BUFFER_LENGTH_MAX);
695         }
696
697         DEBUG_EMUL_END();
698 }
699
700 static void _net_nfc_deinitialize_llcp(void)
701 {
702         DEBUG_EMUL_BEGIN();
703
704         if (Snep_Server_msg != NULL) {
705                 if (Snep_Server_msg->data != NULL) {
706                         if (Snep_Server_msg->data->buffer != NULL) {
707                                 free(Snep_Server_msg->data->buffer);
708                                 Snep_Server_msg->data->buffer = NULL;
709                         }
710
711                         free(Snep_Server_msg->data);
712                         Snep_Server_msg->data = NULL;
713                 }
714
715                 free(Snep_Server_msg);
716                 Snep_Server_msg = NULL;
717         }
718
719         llcp_server_data = NULL;
720
721         DEBUG_EMUL_END();
722 }
723
724 static bool _net_nfc_emul_get_is_tag_attached(void)
725 {
726         return g_tag_attached;
727 }
728
729 static void _net_nfc_emul_set_is_tag_attached(bool is_detached)
730 {
731         g_tag_attached = is_detached;
732         DEBUG_MSG("set g_tag_attached [%d]", g_tag_attached);
733 }
734
735 static bool _net_nfc_emul_get_is_p2p_attached(void)
736 {
737         return g_p2p_attached;
738 }
739
740 static void _net_nfc_emul_set_is_p2p_attached(bool is_detached)
741 {
742         g_p2p_attached = is_detached;
743         DEBUG_MSG("set g_p2p_attached [%d]", g_p2p_attached);
744 }
745
746 static bool _net_nfc_is_data_emulMsgData(emul_message_id messageId)
747 {
748         DEBUG_EMUL_BEGIN();
749
750         bool retval = false;
751
752         switch (messageId) {
753         case EMUL_NFC_TAG_DISCOVERED:
754                 retval = true;
755                 break;
756
757         case EMUL_NFC_P2P_SEND:
758                 retval = true;
759                 break;
760
761         case EMUL_NFC_TAG_DETACHED:
762         case EMUL_NFC_P2P_DISCOVERED:
763         case EMUL_NFC_P2P_DETACHED:
764         default:
765                 break;
766
767         }
768
769         DEBUG_MSG("retval [%d]", retval);
770
771         DEBUG_EMUL_END();
772
773         return retval;
774 }
775
776 static net_nfc_record_tnf_e _net_nfc_get_tnf_type(int name_format)
777 {
778         DEBUG_EMUL_BEGIN();
779
780         net_nfc_record_tnf_e tnf = NET_NFC_RECORD_EMPTY;
781
782         switch (name_format) {
783         case EMUL_NDEF_TNF_EMPTY:
784                 tnf = NET_NFC_RECORD_EMPTY;
785                 break;
786
787         case EMUL_NDEF_TNF_WELL_KNOWN:
788                 tnf = NET_NFC_RECORD_WELL_KNOWN_TYPE;
789                 break;
790
791         case EMUL_NDEF_TNF_MIME_MEDIA:
792                 tnf = NET_NFC_RECORD_MIME_TYPE;
793                 break;
794
795         case EMUL_NDEF_TNF_ABSOLUTE_URI:
796                 tnf = NET_NFC_RECORD_URI;
797                 break;
798
799         case EMUL_NDEF_TNF_EXTERNAL:
800                 tnf = NET_NFC_RECORD_EXTERNAL_RTD;
801                 break;
802
803         case EMUL_NDEF_TNF_UNKNOWN:
804                 tnf = NET_NFC_RECORD_UNKNOWN;
805                 break;
806
807         default:
808                 tnf = NET_NFC_RECORD_UNKNOWN;
809                 DEBUG_MSG("data is currupted");
810                 break;
811         }
812
813         DEBUG_MSG("tnf [%d]", tnf);
814
815         DEBUG_EMUL_END();
816
817         return tnf;
818
819 }
820
821 static inline int _create_records_well_known(emulMsg_data_s *record, char *record_payload, ndef_record_h *new_record)
822 {
823         if (!strncmp((char *)record->typeName.buffer, "U", 1)) {
824                 DEBUG_MSG("URI Type ");
825
826                 data_s payload_data = {NULL, 0};
827
828                 if (record->payload.buffer != NULL) {
829                         payload_data.length = strlen(record_payload) + 1;
830
831                         _nfc_emul_util_alloc_mem(payload_data.buffer, payload_data.length);
832                         if (payload_data.buffer == NULL) {
833                                 DEBUG_MSG("_nfc_emul_util_alloc_mem failed");
834                                 return -1;
835                         }
836
837                         payload_data.buffer[0] = NET_NFC_SCHEMA_FULL_URI;       /* first byte of payload is protocol scheme */
838                         memcpy(payload_data.buffer + 1, record->payload.buffer, payload_data.length - 1);
839                 }
840
841                 if (_net_nfc_util_create_record(record->tnf, &record->typeName, &record->id, &payload_data, (ndef_record_s**)new_record) != NET_NFC_OK) {
842                         DEBUG_ERR_MSG("net_nfc_util_create_record is failed");
843                         return -1;
844                 }
845
846                 if (payload_data.buffer != NULL)
847                         _nfc_emul_util_free_mem(payload_data.buffer);
848         } else if (!strncmp((char *)record->typeName.buffer, "T", 1)) {
849                 DEBUG_MSG("TEXT Type ");
850
851                 data_s payload_data = {NULL, 0};
852                 int offset = 0;
853                 int controll_byte;
854
855                 if (record->payload.buffer != NULL) {
856                         payload_data.length = strlen((char *)record_payload) + strlen("en-US") + 1;
857
858                         _nfc_emul_util_alloc_mem(payload_data.buffer, payload_data.length);
859                         if (payload_data.buffer == NULL) {
860                                 DEBUG_MSG("_nfc_emul_util_alloc_mem failed");
861                                 return -1;
862                         }
863
864                         controll_byte = strlen("en-US") & 0x3F;
865
866                         payload_data.buffer[0] = controll_byte;
867
868                         offset = 1;
869                         memcpy(payload_data.buffer + offset, "en-US", strlen("en-US"));
870
871                         offset = offset + strlen("en-US");
872                         memcpy(payload_data.buffer + offset, record->payload.buffer, strlen(record_payload));
873                 }
874
875                 if (_net_nfc_util_create_record(record->tnf, &record->typeName, &record->id, &payload_data, (ndef_record_s**)new_record) != NET_NFC_OK) {
876                         DEBUG_ERR_MSG("net_nfc_util_create_record is failed");
877                         return -1;
878                 }
879
880                 if (payload_data.buffer != NULL)
881                         _nfc_emul_util_free_mem(payload_data.buffer);
882         } else {
883                 DEBUG_ERR_MSG("NET_NFC_RECORD_WELL_KNOWN_TYPE >> typeName is wrong");
884                 return -1;
885         }
886         return 0;
887 }
888
889 static inline int _create_records_mime(emulMsg_data_s *record, char *record_payload, ndef_record_h *new_record)
890 {
891         FILE *file = NULL;
892         data_s filePayload = {NULL, 0};
893
894         /* open file : size limit? 10k? */
895         file = fopen(record_payload, "r");
896         if (file != NULL) {
897                 long int file_len = 0, read_count = 0, read_total = 0;
898                 uint8_t *file_data = NULL;
899
900                 fseek(file, 0, SEEK_END);
901                 file_len = ftell(file);
902                 fseek(file, 0, SEEK_SET);
903
904                 _nfc_emul_util_alloc_mem(file_data, file_len);
905
906                 if (file_data == NULL) {
907                         DEBUG_MSG("_nfc_emul_util_alloc_mem failed");
908                         fclose(file);
909                         return -1;
910                 }
911
912                 /* create payload */
913                 do {
914                         read_count = fread(file_data + read_total, 1, file_len - read_total, file);
915                         read_total += read_count;
916                 } while (read_count != 0 && read_total < file_len);
917
918                 fclose(file);
919
920                 DEBUG_MSG("fread(%s) success, size %ld", record->payload.buffer, file_len);
921
922                 filePayload.length = file_len;
923                 _nfc_emul_util_alloc_mem(filePayload.buffer, filePayload.length);
924
925                 if (filePayload.buffer == NULL) {
926                         DEBUG_MSG("_nfc_emul_util_alloc_mem failed");
927                         _nfc_emul_util_free_mem(file_data);
928                         return -1;
929                 }
930                 memcpy(filePayload.buffer, file_data, filePayload.length);
931
932                 _nfc_emul_util_free_mem(file_data);
933
934                 /* set id */
935                 if (record->id.buffer == NULL) {
936                         char *file_name = NULL;
937
938                         file_name = strrchr(record_payload, '/');
939                         if (file_name == NULL)
940                                 file_name = (char *)record_payload;
941                         else
942                                 file_name++;
943
944                         record->id.length = strlen(file_name);
945                         _nfc_emul_util_alloc_mem(record->id.buffer, record->id.length + 1);
946
947                         if (record->id.buffer == NULL) {
948                                 DEBUG_MSG("_nfc_emul_util_alloc_mem failed");
949                                 _nfc_emul_util_free_mem(filePayload.buffer);
950                                 return -1;
951                         }
952                         memcpy(record->id.buffer, file_name, sizeof(record->id.buffer) - 1);
953                 }
954         } else {
955                 DEBUG_MSG("file open error");
956                 return -1;
957         }
958
959         /* create record */
960         int ret = _net_nfc_util_create_record(record->tnf, &record->typeName, &record->id,
961                 &filePayload, (ndef_record_s **)new_record);
962         _nfc_emul_util_free_mem(filePayload.buffer);
963         if (ret != NET_NFC_OK) {
964                 DEBUG_MSG("net_nfc_create_record failed[%d]", ret);
965                 return -1;
966         } else {
967                 return 0;
968         }
969 }
970
971 static void _create_records_free(emulMsg_data_s *record)
972 {
973         if (record->typeName.buffer)
974                 _nfc_emul_util_free_mem(record->typeName.buffer);
975
976         if (record->id.buffer)
977                 _nfc_emul_util_free_mem(record->id.buffer);
978
979         if (record->payload.buffer)
980                 _nfc_emul_util_free_mem(record->payload.buffer);
981 }
982
983 static int _net_nfc_create_records_from_emulMsg(emulMsg_s *emul_msg, ndef_message_s **ndef_message, int record_count)
984 {
985         DEBUG_EMUL_BEGIN();
986
987         int index;
988         int create_record_count = 0;
989         char emulMsg[BUFFER_LENGTH_MAX] = {0};
990
991         memcpy(emulMsg, emul_msg->file_data, sizeof(emulMsg) - 1);
992
993         /* parsing data and create record to record structure */
994         for (index = 0; index < record_count; index++) {
995                 char *name_format;
996                 char *type_name;
997                 char *record_id;
998                 char *record_payload;
999
1000                 emulMsg_data_s record = {0};
1001
1002                 /* parse string */
1003                 if (index == 0)
1004                         name_format = strtok((char *)emulMsg, NET_NFC_EMUL_MSG_RECORD_SEPERATOR);
1005                 else
1006                         name_format = strtok(NULL, NET_NFC_EMUL_MSG_RECORD_SEPERATOR);
1007
1008                 type_name = strtok(NULL, NET_NFC_EMUL_MSG_RECORD_SEPERATOR);
1009                 record_id = strtok(NULL, NET_NFC_EMUL_MSG_RECORD_SEPERATOR);
1010                 if (index == record_count - 1) {
1011                         /* the last payload : we have to read sentence fully */
1012                         record_payload = strtok(NULL, "\n");
1013                 } else {
1014                         record_payload = strtok(NULL, NET_NFC_EMUL_MSG_RECORD_SEPERATOR);
1015                 }
1016
1017                 /* assign data to record structure */
1018                 record.tnf = _net_nfc_get_tnf_type(atoi(name_format));
1019
1020                 DEBUG_MSG("Data : type_name ");
1021
1022                 record.typeName.length = strlen(type_name);
1023                 _nfc_emul_util_alloc_mem(record.typeName.buffer, record.typeName.length + 1);
1024
1025                 if (record.typeName.buffer == NULL) {
1026                         DEBUG_MSG("_nfc_emul_util_alloc_mem failed");
1027                         return create_record_count;
1028                 }
1029                 memcpy(record.typeName.buffer, type_name, sizeof(record.typeName.buffer) - 1);
1030
1031                 DEBUG_MSG("Data : record_id ");
1032
1033                 record.id.length = strlen(record_id);
1034                 _nfc_emul_util_alloc_mem(record.id.buffer, record.id.length + 1);
1035
1036                 if (record.id.buffer == NULL) {
1037                         DEBUG_MSG("_nfc_emul_util_alloc_mem failed");
1038                         _nfc_emul_util_free_mem(record.typeName.buffer);
1039                         return create_record_count;
1040                 }
1041                 memcpy(record.id.buffer, record_id, sizeof(record.id.buffer) - 1);
1042
1043                 DEBUG_MSG("Data : record_payload ");
1044
1045                 record.payload.length = strlen(record_payload);
1046                 _nfc_emul_util_alloc_mem(record.payload.buffer, record.payload.length + 1);
1047
1048                 if (record.payload.buffer == NULL) {
1049                         DEBUG_MSG("_nfc_emul_util_alloc_mem failed");
1050                         _nfc_emul_util_free_mem(record.typeName.buffer);
1051                         _nfc_emul_util_free_mem(record.id.buffer);
1052                         return create_record_count;
1053                 }
1054                 memcpy(record.payload.buffer, record_payload, sizeof(record.payload.buffer) - 1);
1055
1056 #ifndef __EMUL_DEBUG__
1057                 DEBUG_ERR_MSG("RECORD DATA START >>>>>>>>>>>>>>>>>>>>>>>>");
1058                 DEBUG_MSG("TNF >>>>[%d]", record.tnf);
1059                 LOGE("type_name >>>>[%s]", type_name);
1060                 LOGE("record_id >>>>[%s]", record_id);
1061                 DEBUG_MSG("record_payload >>>>[%s]", record_payload);
1062                 DEBUG_ERR_MSG("RECORD DATA END >>>>>>>>>>>>>>>>>>>>>>>>");
1063 #endif
1064
1065                 /* create record */
1066                 ndef_record_h new_record = NULL;
1067                 net_nfc_error_e result = NET_NFC_OK;
1068
1069                 switch (record.tnf) {
1070                 case NET_NFC_RECORD_EMPTY:
1071                         result = _net_nfc_util_create_record(NET_NFC_RECORD_EMPTY, &record.typeName,
1072                                 &record.id, &record.payload, (ndef_record_s **)&new_record);
1073                         if (NET_NFC_OK != result) {
1074                                 DEBUG_MSG("net_nfc_create_record failed[%d]", result);
1075                                 _create_records_free(&record);
1076                                 return create_record_count;
1077                         }
1078                         break;
1079                 case NET_NFC_RECORD_UNKNOWN:
1080                         result = _net_nfc_util_create_record(NET_NFC_RECORD_UNKNOWN, &record.typeName,
1081                                 &record.id, &record.payload, (ndef_record_s **)&new_record);
1082                         if (NET_NFC_OK != result) {
1083                                 DEBUG_MSG("net_nfc_create_record failed[%d]", result);
1084                                 _create_records_free(&record);
1085                                 return create_record_count;
1086                         }
1087                         break;
1088                 case NET_NFC_RECORD_WELL_KNOWN_TYPE:
1089                         if (_create_records_well_known(&record, record_payload, &new_record) < 0) {
1090                                 _create_records_free(&record);
1091                                 return create_record_count;
1092                         }
1093                         break;
1094                 case NET_NFC_RECORD_MIME_TYPE:
1095                         if (_create_records_mime(&record, record_payload, &new_record) < 0) {
1096                                 _create_records_free(&record);
1097                                 return create_record_count;
1098                         }
1099                         break;
1100                 default:
1101                         /* NET_NFC_RECORD_URI or NET_NFC_RECORD_EXTERNAL_RTD */
1102                         result = _net_nfc_util_create_record(record.tnf, &record.typeName,
1103                                 &record.id, &record.payload, (ndef_record_s **)&new_record);
1104                         if (NET_NFC_OK != result) {
1105                                 DEBUG_MSG("net_nfc_create_record failed[%d]", result);
1106                                 _create_records_free(&record);
1107                                 return create_record_count;
1108                         }
1109                         break;
1110                 }
1111
1112                 /* append record to ndef msg */
1113                 if ((result = _net_nfc_util_append_record((ndef_message_s*)*ndef_message, (ndef_record_s *)new_record)) != NET_NFC_OK) {
1114                         DEBUG_MSG("net_nfc_util_append_record failed[%d]", result);
1115                         _create_records_free(&record);
1116                         return create_record_count;
1117                 }
1118
1119                 create_record_count++;
1120                 DEBUG_MSG("Create Record Sucess. Create Record Count[%d]", create_record_count);
1121
1122                 _create_records_free(&record);
1123                 DEBUG_MSG("Create Record Loop End");
1124         }
1125
1126         DEBUG_EMUL_END();
1127
1128         return create_record_count;
1129 }
1130
1131 static bool _net_nfc_change_file_owner_permission(FILE *file)
1132 {
1133         char *buffer = NULL;
1134         size_t buffer_len = 0;
1135         struct passwd pwd = {0};
1136         struct passwd *pw_inhouse = NULL;
1137         struct group grp = {0};
1138         struct group *gr_inhouse = NULL;
1139
1140         if (file == NULL)
1141                 return false;
1142
1143         /* change permission */
1144         fchmod(fileno(file), 0644);
1145
1146         /* change owner */
1147         /* get passwd id */
1148         buffer_len = sysconf(_SC_GETPW_R_SIZE_MAX);
1149         if (buffer_len == -1)
1150                 buffer_len = 16384;
1151
1152         _nfc_emul_util_alloc_mem(buffer, buffer_len);
1153         if (buffer == NULL)
1154                 return false;
1155
1156         getpwnam_r("inhouse", &pwd, buffer, buffer_len, &pw_inhouse);
1157         _nfc_emul_util_free_mem(buffer);
1158
1159         /* get group id */
1160         buffer_len = sysconf(_SC_GETGR_R_SIZE_MAX);
1161         if (buffer_len == -1)
1162                 buffer_len = 16384;
1163
1164         _nfc_emul_util_alloc_mem(buffer, buffer_len);
1165         if (buffer == NULL)
1166                 return false;
1167
1168         getgrnam_r("inhouse", &grp, buffer, buffer_len, &gr_inhouse);
1169         _nfc_emul_util_free_mem(buffer);
1170
1171         if ((pw_inhouse != NULL) && (gr_inhouse != NULL)) {
1172                 if (fchown(fileno(file), pw_inhouse->pw_uid, gr_inhouse->gr_gid) < 0)
1173                         DEBUG_MSG("failed to change owner");
1174         }
1175
1176         return true;
1177 }
1178
1179 static void _net_nfc_store_ndef_message(data_s *data)
1180 {
1181         char file_name[1024] = {0};
1182         FILE *fp = NULL;
1183
1184         if (data == NULL)
1185                 return;
1186
1187         /* create file */
1188         snprintf(file_name, sizeof(file_name), "%s/%s/%s", "/opt/usr/share/nfc-manager-daemon",
1189                 "message", "emul-ndef-message.txt");
1190
1191         unlink(file_name);
1192
1193         if ((fp = fopen(file_name, "w")) != NULL) {
1194                 int length = 0;
1195
1196                 if ((length = fwrite(data->buffer, 1, data->length, fp)) > 0) {
1197                         DEBUG_MSG("[%d] bytes is written", length);
1198
1199                         _net_nfc_change_file_owner_permission(fp);
1200                         fflush(fp);
1201                         fsync(fileno(fp));
1202                 } else {
1203                         DEBUG_MSG("write is failed = [%d]", data->length);
1204                 }
1205
1206                 fclose(fp);
1207         }
1208 }
1209
1210 static net_nfc_error_e _net_nfc_retrieve_ndef_message(data_s *data)
1211 {
1212         char file_name[1024] = {0};
1213         FILE *fp = NULL;
1214         net_nfc_error_e result = NET_NFC_OK;
1215
1216         if (data == NULL)
1217                 return NET_NFC_NULL_PARAMETER;
1218
1219         /* retreive file */
1220         snprintf(file_name, sizeof(file_name), "%s/%s/%s", "/opt/usr/share/nfc-manager-daemon",
1221                 "message", "emul-ndef-message.txt");
1222
1223         if ((fp = fopen(file_name, "r")) != NULL) {
1224                 long int size = 0;
1225
1226                 /* rewind to start of file */
1227                 fseek(fp, 0, SEEK_END);
1228                 size = ftell(fp);
1229                 fseek(fp, 0, SEEK_SET);
1230
1231                 DEBUG_MSG("message length = [%ld]", size);
1232
1233                 if (size > 0) {
1234                         if (_net_nfc_util_init_data(data, size) == true) {
1235                                 int current;
1236                                 size_t offset = 0;
1237
1238                                 /* read fully */
1239                                 do {
1240                                         current = fread(data->buffer + offset, 1,
1241                                                 data->length - offset, fp);
1242                                         if (current > 0)
1243                                                 offset += current;
1244                                         else
1245                                                 break;
1246                                 } while (offset < data->length);
1247
1248                                 if (offset == data->length)
1249                                         result = NET_NFC_OK;
1250                                 else
1251                                         result = NET_NFC_NO_NDEF_MESSAGE;
1252                         } else {
1253                                 result = NET_NFC_ALLOC_FAIL;
1254                         }
1255                 } else {
1256                         result = NET_NFC_OPERATION_FAIL;
1257                 }
1258
1259                 fclose(fp);
1260         } else {
1261                 result = NET_NFC_NO_DATA_FOUND;
1262         }
1263
1264         return result;
1265 }
1266
1267 static bool _net_nfc_create_ndef_from_emulMsg(emulMsg_s *emul_msg)
1268 {
1269         DEBUG_EMUL_BEGIN();
1270
1271         if (emul_msg->file_data == NULL)
1272                 return false;
1273
1274         int retval = true;
1275         net_nfc_error_e result = NET_NFC_OK;
1276         data_s rawdata = {NULL, 0};
1277
1278         int record_count = emul_msg->record_count;
1279
1280         if (record_count == 0)
1281                 return false;
1282
1283         /* create ndef msg */
1284         ndef_message_h ndef_message = NULL;
1285         int ndef_length = 0;
1286
1287         if ((result = _net_nfc_util_create_ndef_message((ndef_message_s **)&ndef_message)) != NET_NFC_OK)
1288                 DEBUG_MSG("failed to create ndef message [%d]", result);
1289
1290         /* create records and append it to ndef_msg*/
1291         emul_msg->record_count = _net_nfc_create_records_from_emulMsg(emul_msg, (ndef_message_s **)&ndef_message, record_count);
1292
1293         /* convert ndef msg to raw data */
1294         ndef_length = _net_nfc_util_get_ndef_message_length((ndef_message_s *)ndef_message);
1295
1296         if (!ndef_length)
1297                 DEBUG_MSG("ndef_message size is zero!");
1298
1299         rawdata.length = ndef_length;
1300         _nfc_emul_util_alloc_mem(rawdata.buffer, ndef_length);
1301
1302         if ((result = _net_nfc_util_convert_ndef_message_to_rawdata((ndef_message_s*)ndef_message, &rawdata)) != NET_NFC_OK)
1303                 DEBUG_MSG("net_nfc_util_convert_ndef_message_to_rawdata is failed![%d]", result);
1304
1305         _net_nfc_store_ndef_message(&rawdata);
1306
1307         _nfc_emul_util_free_mem(rawdata.buffer);
1308         _net_nfc_util_free_ndef_message(ndef_message);
1309
1310         DEBUG_EMUL_END();
1311
1312         return retval;
1313 }
1314
1315 static bool _net_nfc_create_emulMsg(emulMsg_s **emul_msg, uint8_t *data, long int size)
1316 {
1317         DEBUG_EMUL_BEGIN();
1318
1319         char *emulMsgID;
1320         char *emulMsgData;
1321
1322         *emul_msg = calloc(1, sizeof(emulMsg_s));
1323         if (*emul_msg == NULL)
1324                 return false;
1325
1326         /* emulData => ID : MSG ex) 100:1,1,1,U,samsung,http://www.naver.com */
1327         emulMsgID = strtok((char *)data, NET_NFC_EMUL_MSG_ID_SEPERATOR);
1328         if (emulMsgID != NULL)
1329                 (*emul_msg)->message_id = (emul_message_id)(atoi(emulMsgID));
1330
1331         DEBUG_MSG("emul_msg->message_id >>>>[%d]", (*emul_msg)->message_id);
1332
1333         if (_net_nfc_is_data_emulMsgData((*emul_msg)->message_id)) {
1334
1335                 emulMsgData = strtok(NULL, NET_NFC_EMUL_MSG_DATA_SEPERATOR);
1336                 DEBUG_MSG("emulMsgData >>>>[%s]", emulMsgData);
1337
1338                 switch ((*emul_msg)->message_id) {
1339                 case EMUL_NFC_TAG_DISCOVERED:
1340                 case EMUL_NFC_P2P_SEND:
1341                 {
1342                         /* get message : Tag Type, Record Count, Records */
1343                         int target_type = -1;
1344                         char file_data[BUFFER_LENGTH_MAX] = {0};
1345                         int length = 0;
1346
1347                         sscanf(emulMsgData, NET_NFC_EMUL_TAG_DISCOVERED_DATA_FORMAT, &target_type, &((*emul_msg)->record_count), file_data);
1348
1349                         (*emul_msg)->target_type = (emul_target_type)target_type;
1350
1351                         length = strlen(file_data) + 1;
1352                         _nfc_emul_util_alloc_mem((*emul_msg)->file_data, length);
1353                         memcpy((*emul_msg)->file_data, file_data, length);
1354
1355                         DEBUG_ERR_MSG("EMUL MESSAGE DATA START >>>>>>>>>>>>>>>>>>>>>>>>");
1356                         DEBUG_MSG("message_id >>>>[%d]", (*emul_msg)->message_id);
1357                         DEBUG_MSG("target_type >>>>[%d]", (*emul_msg)->target_type);
1358                         DEBUG_MSG("record_count >>>>[%d]", (*emul_msg)->record_count);
1359                         DEBUG_MSG("file_data >>>>[%s]", (char *)(*emul_msg)->file_data);
1360                         DEBUG_ERR_MSG("EMUL MESSAGE DATA END >>>>>>>>>>>>>>>>>>>>>>>>");
1361
1362                         if (!_net_nfc_create_ndef_from_emulMsg((*emul_msg)))
1363                                 DEBUG_ERR_MSG("read ndef_msg is failed >>>");
1364
1365                         DEBUG_ERR_MSG("_net_nfc_create_ndef_from_emulMsg end");
1366                         break;
1367                 }
1368
1369                 default:
1370                         /* exception case */
1371                         DEBUG_ERR_MSG("_net_nfc_set_emulMsg error. Data is currupted");
1372                         return false;
1373                         break;
1374                 }
1375         } else {
1376                 switch ((*emul_msg)->message_id) {
1377                 case EMUL_NFC_P2P_DISCOVERED:
1378                         (*emul_msg)->target_type = EMUL_NFC_TARGET;
1379                         break;
1380
1381                 case EMUL_NFC_TAG_DETACHED:
1382                         DEBUG_MSG("TAG DETACHED");
1383                         if (!_net_nfc_emul_get_is_tag_attached()) {
1384                                 DEBUG_ERR_MSG("tag is not attached!!");
1385                                 return false;
1386                         }
1387                         break;
1388
1389                 case EMUL_NFC_P2P_DETACHED:
1390                         DEBUG_MSG("P2P DETACHED");
1391                         if (!_net_nfc_emul_get_is_p2p_attached()) {
1392                                 DEBUG_ERR_MSG("tag is not attached!!");
1393                                 return false;
1394                         }
1395                         break;
1396
1397                 default:
1398                         /* exception case */
1399                         DEBUG_ERR_MSG("_net_nfc_set_emulMsg error. Data is currupted");
1400                         return false;
1401                         break;
1402                 }
1403         }
1404
1405         DEBUG_EMUL_END();
1406
1407         return true;
1408 }
1409
1410 static void _net_nfc_destroy_emulMsg(emulMsg_s *emul_msg)
1411 {
1412         DEBUG_EMUL_BEGIN();
1413
1414         if (emul_msg != NULL) {
1415                 if (emul_msg->file_data != NULL)
1416                         free(emul_msg->file_data);
1417
1418                 free(emul_msg);
1419         }
1420
1421         DEBUG_EMUL_END();
1422 }
1423
1424 static int _net_nfc_emul_convert_target_type(emul_target_type targetType)
1425 {
1426         DEBUG_EMUL_BEGIN();
1427
1428         int covert = 0;
1429
1430         switch (targetType) {
1431         case EMUL_TAG_TOPAZ_JEWEL:
1432                 covert = NET_NFC_JEWEL_PICC;
1433                 break;
1434
1435         case EMUL_TAG_MIFARE_UL:
1436                 covert = NET_NFC_MIFARE_ULTRA_PICC;
1437                 break;
1438
1439         case EMUL_TAG_FELICA:
1440                 covert = NET_NFC_FELICA_PICC;
1441                 break;
1442
1443         case EMUL_TAG_MIFARE_DESFIRE:
1444                 covert = NET_NFC_MIFARE_DESFIRE_PICC;
1445                 break;
1446
1447         case EMUL_NFC_TARGET:
1448                 covert = NET_NFC_NFCIP1_TARGET;
1449                 break;
1450
1451         case EMUL_TARGET_TYPE_MAX:
1452         default:
1453                 /* exception case */
1454                 DEBUG_ERR_MSG("_net_nfc_emul_convert_target_type error. Target type is unknown");
1455                 break;
1456         }
1457
1458         DEBUG_MSG("covert [%d]", covert);
1459
1460         DEBUG_EMUL_END();
1461
1462         return covert;
1463 }
1464
1465 static void _net_nfc_target_discovered_cb(emulMsg_s *emul_msg)
1466 {
1467         DEBUG_EMUL_BEGIN();
1468
1469         /* make handle */
1470         net_nfc_target_handle_s *handle = NULL;
1471         int length = 0;
1472
1473         __net_nfc_make_valid_target_handle(&handle);
1474         if (handle == NULL)
1475                 return;
1476
1477         /* make msg */
1478         net_nfc_request_target_detected_t *target_detected = NULL;
1479         uint8_t device_info[] = {0x03, 0x55, 0x49, 0x44, 0x07, 0x04, 0x93, 0xB7, 0xD9, 0x5B, 0x02, 0x80, \
1480                 0x08, 0x41, 0x50, 0x50, 0x5F, 0x44, 0x41, 0x54, 0x41, 0x00, 0x03, 0x53, 0x41, 0x4B, \
1481                 0x01, 0x00, 0x04, 0x41, 0x54, 0x51, 0x41, 0x02, 0x44, 0x00, 0x0D, 0x4D, 0x41, 0x58, \
1482                 0x5F, 0x44, 0x41, 0x54, 0x41, 0x5F, 0x52, 0x41, 0x54, 0x45, 0x01, 0x00, 0x08, 0x46, \
1483                 0x57, 0x49, 0x5F, 0x53, 0x46, 0x47, 0x54, 0x01, 0x03, 0x49, 0x44, 0x6D, 0x07, 0x04, \
1484                 0x93, 0xB7, 0xD9, 0x5B, 0x02, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
1485
1486
1487         length = sizeof(net_nfc_request_target_detected_t) + sizeof(device_info);
1488         _nfc_emul_util_alloc_mem(target_detected, length);
1489         if (target_detected == NULL)
1490                 return;
1491
1492         target_detected->length = length;
1493         target_detected->request_type = NET_NFC_MESSAGE_SERVICE_STANDALONE_TARGET_DETECTED;
1494         target_detected->handle = handle;
1495
1496         target_detected->devType = _net_nfc_emul_convert_target_type(emul_msg->target_type);
1497         if (!target_detected->devType) {
1498                 DEBUG_MSG("target_detected->devType is unknown");
1499                 _nfc_emul_util_free_mem(target_detected);
1500                 return;
1501         }
1502
1503         target_detected->handle->connection_id = current_connection_id++;
1504
1505         if (target_detected->devType == NET_NFC_NFCIP1_TARGET) {
1506                 DEBUG_MSG("set llcp connection type. remote device is target");
1507                 handle->connection_type = NET_NFC_P2P_CONNECTION_TARGET;
1508                 _net_nfc_emul_set_is_p2p_attached(true);
1509         } else if (target_detected->devType == NET_NFC_NFCIP1_INITIATOR) {
1510                 DEBUG_MSG("set llcp connection type. remote device is initiator");
1511                 handle->connection_type = NET_NFC_P2P_CONNECTION_INITIATOR;
1512                 _net_nfc_emul_set_is_p2p_attached(true);
1513         } else {
1514                 DEBUG_MSG("set tag connection");
1515                 handle->connection_type = NET_NFC_TAG_CONNECTION;
1516                 _net_nfc_emul_set_is_tag_attached(true);
1517         }
1518
1519         target_detected->number_of_keys = 7;
1520         target_detected->target_info_values.length = sizeof(device_info);
1521         memcpy(&target_detected->target_info_values.buffer, device_info, target_detected->target_info_values.length);
1522
1523         /* call target_cb */
1524         if (g_emul_controller_target_cb != NULL) {
1525                 DEBUG_MSG("discovered callback is called");
1526                 g_emul_controller_target_cb(target_detected, NULL);
1527         }
1528         DEBUG_EMUL_END();
1529 }
1530
1531 static void _net_nfc_tag_detached_cb(void)
1532 {
1533         DEBUG_EMUL_BEGIN();
1534
1535         _net_nfc_emul_set_is_tag_attached(false);
1536
1537         DEBUG_EMUL_END();
1538 }
1539
1540 static void _net_nfc_target_detached_cb(void)
1541 {
1542         DEBUG_EMUL_BEGIN();
1543
1544         _net_nfc_emul_set_is_p2p_attached(false);
1545
1546         /* For P2P, we send msg to manager */
1547         net_nfc_request_llcp_msg_t *req_msg = NULL;
1548
1549         _nfc_emul_util_alloc_mem(req_msg, sizeof(net_nfc_request_llcp_msg_t));
1550
1551         if (req_msg != NULL) {
1552                 req_msg->length = sizeof(net_nfc_request_llcp_msg_t);
1553                 req_msg->request_type = NET_NFC_MESSAGE_SERVICE_LLCP_DEACTIVATED;
1554
1555                 DEBUG_MSG("deactivated callback is called");
1556                 g_emul_controller_llcp_cb(req_msg, NULL);
1557         }
1558 }
1559
1560 static void _net_nfc_llcp_data_receive_cb(void *pContext)
1561 {
1562         DEBUG_EMUL_BEGIN();
1563
1564         net_nfc_request_llcp_msg_t *req_msg = NULL;
1565
1566         _nfc_emul_util_alloc_mem(req_msg, sizeof(net_nfc_request_llcp_msg_t));
1567
1568         if (req_msg != NULL) {
1569                 req_msg->length = sizeof(net_nfc_request_llcp_msg_t);
1570                 req_msg->request_type = NET_NFC_MESSAGE_SERVICE_LLCP_RECEIVE;
1571                 req_msg->result = NET_NFC_OK;
1572
1573                 DEBUG_MSG("receive callback is called >>");
1574                 g_emul_controller_llcp_cb(req_msg, pContext);
1575         }
1576
1577         DEBUG_EMUL_END();
1578 }
1579
1580 static void _net_nfc_llcp_data_receive_from_cb(void *pContext)
1581 {
1582         DEBUG_EMUL_BEGIN();
1583
1584         net_nfc_request_llcp_msg_t *req_msg = NULL;
1585
1586         _nfc_emul_util_alloc_mem(req_msg, sizeof(net_nfc_request_llcp_msg_t));
1587
1588         if (req_msg != NULL) {
1589                 req_msg->length = sizeof(net_nfc_request_llcp_msg_t);
1590                 req_msg->request_type = NET_NFC_MESSAGE_SERVICE_LLCP_RECEIVE_FROM;
1591                 req_msg->result = NET_NFC_OK;
1592
1593                 DEBUG_MSG("receive_from callback is called >>");
1594                 g_emul_controller_llcp_cb(req_msg, pContext);
1595         }
1596
1597         DEBUG_EMUL_END();
1598 }
1599
1600 static void _net_nfc_llcp_create_snep_server_msg(snep_command_field_e resp_field)
1601 {
1602         DEBUG_EMUL_BEGIN();
1603
1604         uint8_t *temp = Snep_Server_msg->data->buffer;
1605         uint8_t response = (uint8_t)resp_field;
1606         uint8_t version = 0;
1607         uint32_t length_field = 0;
1608         data_s rawdata = {NULL, 0};
1609
1610         version = SNEP_MAJOR_VER;
1611         version = (((version << 4) & 0xf0) | (SNEP_MINOR_VER & 0x0f));
1612
1613         _net_nfc_retrieve_ndef_message(&rawdata);
1614
1615         /* version response length payload*/
1616         Snep_Server_msg->data->length = sizeof(uint8_t) + sizeof(uint8_t) + sizeof(uint32_t) + rawdata.length;
1617
1618         /* copy version */
1619         *temp = version;
1620         temp++;
1621
1622         /* copy response */
1623         *temp = response;
1624         temp++;
1625
1626         length_field = htonl(rawdata.length);
1627
1628         /* length will be se 0. so we don't need to copy value */
1629         memcpy(temp, &length_field, sizeof(uint32_t));
1630         temp += sizeof(uint32_t);
1631
1632         /* copy ndef information to response msg */
1633         if (rawdata.length > 0)
1634                 memcpy(temp, rawdata.buffer, rawdata.length);
1635
1636         _nfc_emul_util_free_mem(rawdata.buffer);
1637
1638         DEBUG_EMUL_END();
1639 }
1640
1641 static llcp_state_e _net_nfc_get_llcp_state(void *pContext)
1642 {
1643         DEBUG_EMUL_BEGIN();
1644         llcp_state_e state;
1645
1646         state = NET_NFC_STATE_EXCHANGER_SERVER;
1647
1648         DEBUG_EMUL_END();
1649
1650         return state;
1651 }
1652
1653 static bool _net_nfc_make_llcp_data(emulMsg_s *emul_msg)
1654 {
1655         DEBUG_EMUL_BEGIN();
1656         data_s rawdata = {NULL, 0};
1657
1658         _net_nfc_retrieve_ndef_message(&rawdata);
1659         _nfc_emul_util_free_mem(rawdata.buffer);
1660
1661         if (emul_msg->record_count == 0 || rawdata.length == 0) {
1662                 DEBUG_ERR_MSG("data is zero >>>");
1663                 return false;
1664         }
1665
1666         /* For SNEP, we should create snep msg, and then copy it to llcp_server_data */
1667         _net_nfc_llcp_create_snep_server_msg(SNEP_REQ_PUT);
1668
1669         /* copy rawdata to llcp_server_data->buffer */
1670         if (Snep_Server_msg->data->length <= SNEP_MAX_BUFFER) {
1671                 DEBUG_MSG("The snep msg size is small than SNEP_MAX_BUFFER >>>");
1672
1673                 if (llcp_server_data == NULL)
1674                         return false;
1675
1676                 llcp_server_data->length = Snep_Server_msg->data->length;
1677                 memcpy(llcp_server_data->buffer, Snep_Server_msg->data->buffer, Snep_Server_msg->data->length);
1678         } else {
1679                 DEBUG_MSG("send first segment >>>");
1680
1681                 if (llcp_server_data == NULL)
1682                         return false;
1683
1684                 llcp_server_data->length = SNEP_MAX_BUFFER;
1685                 memcpy(llcp_server_data->buffer, Snep_Server_msg->data->buffer, SNEP_MAX_BUFFER);
1686
1687                 Snep_Server_msg->isSegment = true;
1688                 Snep_Server_msg->offset = SNEP_MAX_BUFFER;
1689         }
1690
1691         DEBUG_EMUL_END();
1692
1693         return true;
1694 }
1695
1696 static void _net_nfc_send_emulMsg_to_nfc_manager(emulMsg_s *emul_msg)
1697 {
1698         DEBUG_EMUL_BEGIN();
1699
1700         switch (emul_msg->message_id) {
1701         case EMUL_NFC_TAG_DISCOVERED:
1702                 _net_nfc_target_discovered_cb(emul_msg);
1703                 break;
1704
1705         case EMUL_NFC_TAG_DETACHED:
1706                 _net_nfc_tag_detached_cb();
1707                 break;
1708
1709         case EMUL_NFC_P2P_DISCOVERED:
1710                 _net_nfc_initialize_llcp();
1711                 _net_nfc_target_discovered_cb(emul_msg);
1712                 break;
1713
1714         case EMUL_NFC_P2P_SEND:
1715                 if (!_net_nfc_emul_get_is_p2p_attached()) {
1716                         DEBUG_ERR_MSG("target is not attached!!");
1717                         return;
1718                 }
1719
1720                 if (_net_nfc_make_llcp_data(emul_msg)) {
1721                         /* find snep server*/
1722                         socket_info_s *socket_info = _net_nfc_find_server_socket(NET_NFC_EMUL_SNEP_SERVER_SOCKET_NUMBER);
1723                         if (socket_info == NULL) {
1724                                 DEBUG_ERR_MSG("socket_info is NULL");
1725                                 return;
1726                         }
1727
1728                         _net_nfc_llcp_data_receive_cb(socket_info->user_context); /* call callback */
1729                 } else {
1730                         DEBUG_ERR_MSG("make_p2p_data is fail!!");
1731                         return;
1732                 }
1733                 break;
1734
1735         case EMUL_NFC_P2P_DETACHED:
1736                 if (!_net_nfc_emul_get_is_p2p_attached()) {
1737                         DEBUG_ERR_MSG("target is not attached!!");
1738                         return;
1739                 }
1740
1741                 _net_nfc_target_detached_cb();
1742                 _net_nfc_deinitialize_llcp();
1743                 break;
1744
1745         default:
1746                 DEBUG_ERR_MSG("message_id is wrong!!");
1747                 break;
1748         }
1749
1750         DEBUG_EMUL_END();
1751 }
1752
1753 static void _net_nfc_process_emulMsg(uint8_t *data, long int size)
1754 {
1755         DEBUG_EMUL_BEGIN();
1756
1757         emulMsg_s *emul_msg = NULL;
1758
1759         /* create emul_msg */
1760         _net_nfc_create_emulMsg(&emul_msg, data, size);
1761
1762         DEBUG_MSG("emul_msg->message_id >>>>[%d]", emul_msg->message_id);
1763
1764         /* processing emul_msg */
1765         _net_nfc_send_emulMsg_to_nfc_manager(emul_msg);
1766
1767         /* destroy emul_msg */
1768         _net_nfc_destroy_emulMsg(emul_msg);
1769
1770         DEBUG_EMUL_END();
1771 }
1772
1773 #ifdef __USE_EPOLL_FOR_FILE__
1774 static void emul_ReaderThread(void *pArg)
1775 {
1776         DEBUG_MSG("emul_ReaderThread START >>>>");
1777
1778         char file_name[1024] = {0};
1779         bool condition = true;
1780
1781         int emulMsg_file_fd = -1;
1782         int emulMsg_poll_fd = -1;
1783         struct epoll_event *emulMsg_poll_events = NULL;
1784
1785         /* make file name */
1786         snprintf(file_name, sizeof(file_name), "%s/%s", NET_NFC_EMUL_DATA_PATH, NET_NFC_EMUL_MESSAGE_FILE_NAME);
1787         LOGE("file path : %s", file_name);
1788
1789         /* open file for poll */
1790         emulMsg_file_fd = open(file_name, O_RDONLY | O_NONBLOCK);
1791         if (emulMsg_file_fd < 0) {
1792                 DEBUG_MSG("file open error !!!!");
1793                 return;
1794         }
1795
1796         /* create epoll */
1797         if ((emulMsg_poll_fd = epoll_create1(EPOLL_CLOEXEC)) == -1) {
1798                 DEBUG_MSG("epoll_create1 is occured");
1799                 close(emulMsg_file_fd);
1800                 return;
1801         }
1802
1803         if ((emulMsg_poll_events = calloc(1, sizeof(struct epoll_event) * EPOLL_SIZE)) == NULL) {
1804                 DEBUG_MSG("calloc is occured");
1805                 close(emulMsg_file_fd);
1806                 return;
1807         }
1808
1809         /* set event */
1810         struct epoll_event ev;
1811
1812         ev.events = EPOLLIN | EPOLLET | EPOLLHUP | EPOLLERR;
1813         ev.data.fd = emulMsg_file_fd;
1814
1815         /* add file fd to epoll */
1816         epoll_ctl(emulMsg_poll_fd, EPOLL_CTL_ADD, emulMsg_file_fd, &ev);
1817
1818         while (condition == true) {
1819
1820                 int num_of_files = 0;
1821                 int index = 0;
1822
1823                 /* to do : I will add mutex in the future */
1824                 /* lock mutex */
1825
1826                 DEBUG_MSG("epoll wait >>>>");
1827
1828                 while ((num_of_files = epoll_wait(emulMsg_poll_fd, emulMsg_poll_events, EPOLL_SIZE, -1)) == 0)
1829                         DEBUG_MSG("no data is changed ");
1830
1831                 for (index = 0; index < num_of_files; index++) {
1832                         if ((emulMsg_poll_events[index].events & (EPOLLHUP)) || (emulMsg_poll_events[index].events & (EPOLLERR))) {
1833                                 DEBUG_MSG("connection is closed");
1834
1835                                 condition = 0;
1836                         } else if (emulMsg_poll_events[index].events & EPOLLIN) {
1837                                 if (emulMsg_poll_events[index].data.fd == emulMsg_file_fd) {
1838                                         char readbuffer[READ_BUFFER_LENGTH_MAX + 1];
1839                                         int readcnt = 0;
1840
1841                                         DEBUG_MSG("precess POLLIN ");
1842
1843                                         memset(readbuffer, 0x00, sizeof(READ_BUFFER_LENGTH_MAX) + 1);
1844
1845                                         readcnt = read(emulMsg_file_fd, readbuffer, READ_BUFFER_LENGTH_MAX);
1846
1847                                         DEBUG_MSG("message readcnt= [%d] ", readcnt);
1848                                         DEBUG_MSG("message = [%s] ", readbuffer);
1849
1850                                         /* remove header */
1851                                         _net_nfc_process_emulMsg((uint8_t *)readbuffer + NET_NFC_EMUL_HEADER_LENGTH, (long int)readcnt - NET_NFC_EMUL_HEADER_LENGTH);
1852                                 } else {
1853                                         DEBUG_MSG("not expected socket connection");
1854                                         condition = 0;
1855                                 }
1856                         } else {
1857                                 if (num_of_files == index) {
1858                                         DEBUG_MSG("unknown event");
1859                                         condition = 0;
1860                                 }
1861                         }
1862
1863
1864                 }
1865
1866                 /* unlock mutex */
1867
1868         }
1869
1870         close(emulMsg_file_fd);
1871
1872         if (emulMsg_poll_events != NULL)
1873                 free(emulMsg_poll_events);
1874
1875         DEBUG_MSG("emul_ReaderThread END >>>>");
1876 }
1877
1878 #else
1879
1880 static void emul_ReaderThread(void *pArg)
1881 {
1882         DEBUG_MSG("emul_ReaderThread START >>>>");
1883
1884         FILE *fp = NULL;
1885         char file_name[1024] = {0};
1886
1887         char readBuffer[READ_BUFFER_LENGTH_MAX];
1888
1889         struct stat st;
1890
1891         time_t curTime;
1892
1893         bool condition = true;
1894
1895         /* make file name */
1896         snprintf(file_name, sizeof(file_name), "%s/%s", NET_NFC_EMUL_DATA_PATH, NET_NFC_EMUL_MESSAGE_FILE_NAME);
1897         LOGE("file path : %s", file_name);
1898
1899         time(&curTime);
1900         DEBUG_MSG("Start Current Time [%ld]", (unsigned long)curTime);
1901
1902         /* polling 500 ms */
1903
1904         while (condition != 0) {
1905                 usleep(500 * 1000);
1906
1907                 /* open file */
1908                 if ((fp = fopen(file_name, "r")) == NULL) {
1909                         DEBUG_MSG("file open error");
1910                         condition = false;
1911                 } else {
1912                         /* get the modified time of the file */
1913                         if (stat(file_name, &st) == 0) {
1914                                 if ((unsigned long)st.st_mtime <= (unsigned long)curTime) {
1915                                         fclose(fp);
1916                                         continue;
1917                                 } else {
1918                                         DEBUG_MSG("FILE Modified Time [%ld]", (unsigned long)st.st_mtime);
1919                                         /* update current time */
1920                                         time(&curTime);
1921                                         DEBUG_MSG("Get New Current Time [%ld]", (unsigned long)curTime);
1922                                 }
1923
1924                         } else {
1925                                 DEBUG_MSG("stat error");
1926                                 fclose(fp);
1927                                 continue;
1928                         }
1929
1930                         /* read data */
1931                         memset(readBuffer, 0x00, READ_BUFFER_LENGTH_MAX);
1932
1933                         if (fscanf(fp, "%[^\n]", readBuffer))
1934                                 DEBUG_MSG("get DATA >>>> readBuffer [%s]", readBuffer);
1935
1936                         /* process message */
1937                         _net_nfc_process_emulMsg((uint8_t *)readBuffer, (long int)strlen(readBuffer));
1938
1939                         fclose(fp);
1940                 }
1941
1942                 DEBUG_MSG("LOOP END >>>>");
1943
1944         }
1945
1946         DEBUG_MSG("emul_ReaderThread END >>>>");
1947 }
1948 #endif
1949
1950 static bool _net_nfc_emul_controller_start_thread(void)
1951 {
1952         bool ret = true;
1953
1954         DEBUG_EMUL_BEGIN();
1955
1956         ret = pthread_create(&gEmulThread, NULL, (emul_Nfc_thread_handler_t)emul_ReaderThread, (void*)"emul_read_thread");
1957
1958         if (ret != 0)
1959                 return false;
1960
1961         DEBUG_EMUL_END();
1962
1963         return true;
1964 }
1965
1966 static void _net_nfc_emul_controller_stop_thread(void)
1967 {
1968         DEBUG_EMUL_BEGIN();
1969
1970         pthread_cancel(gEmulThread);
1971
1972         usleep(500 * 1000);
1973
1974         pthread_cond_signal(&cond);
1975
1976         pthread_join(gEmulThread, NULL);
1977
1978         DEBUG_EMUL_END();
1979 }
1980
1981 static bool net_nfc_emul_controller_init(net_nfc_error_e *result)
1982 {
1983         bool ret = true;
1984
1985         if (result == NULL)
1986                 return false;
1987
1988         DEBUG_EMUL_BEGIN();
1989
1990         DEBUG_MSG("start stack init ");
1991
1992         if (g_stack_init_successful == true) {
1993                 DEBUG_MSG("Already statck is initialized");
1994                 return true;
1995         }
1996
1997
1998         /* start reader thread : to get event from Inject */
1999         if (!_net_nfc_emul_controller_start_thread()) {
2000                 DEBUG_ERR_MSG("Failed to create emul thread");
2001                 return false;
2002         }
2003
2004         DEBUG_MSG("Stack init finished");
2005
2006         g_stack_init_successful = true;
2007         *result = NET_NFC_OK;
2008
2009         DEBUG_EMUL_END();
2010
2011         return ret;
2012 }
2013
2014 static bool net_nfc_emul_controller_deinit(void)
2015 {
2016         DEBUG_EMUL_BEGIN();
2017
2018         /* End thread */
2019         if (g_stack_init_successful == false) {
2020                 DEBUG_MSG("Already statck is deinitialized");
2021                 return true;
2022         }
2023
2024         _net_nfc_emul_controller_stop_thread();
2025
2026         g_stack_init_successful = false;
2027
2028         DEBUG_EMUL_END();
2029
2030         return true;
2031 }
2032 static bool net_nfc_emul_controller_register_listener(
2033         target_detection_listener_cb target_detection_listener,
2034         se_transaction_listener_cb se_transaction_listener,
2035         llcp_event_listener_cb llcp_event_listener,
2036         hce_apdu_listener_cb hce_apdu_listener,
2037         net_nfc_error_e *result)
2038 {
2039         if (result == NULL)
2040                 return false;
2041
2042         *result = NET_NFC_OK;
2043
2044         DEBUG_EMUL_BEGIN();
2045
2046         g_emul_controller_target_cb = target_detection_listener;
2047         g_emul_controller_se_cb = se_transaction_listener;
2048         g_emul_controller_llcp_cb = llcp_event_listener;
2049         g_emul_controller_hce_cb = hce_apdu_listener;
2050
2051         DEBUG_EMUL_END();
2052
2053         return true;
2054 }
2055
2056 static bool net_nfc_emul_controller_unregister_listener()
2057 {
2058         DEBUG_EMUL_BEGIN();
2059
2060         g_emul_controller_target_cb = NULL;
2061         g_emul_controller_se_cb = NULL;
2062         g_emul_controller_llcp_cb = NULL;
2063
2064         DEBUG_EMUL_END();
2065
2066         return true;
2067 }
2068
2069 static bool net_nfc_emul_controller_get_firmware_version(data_s **data, net_nfc_error_e *result)
2070 {
2071         if (data == NULL || result == NULL)
2072                 return false;
2073
2074         *result = NET_NFC_OK;
2075
2076         DEBUG_EMUL_BEGIN();
2077
2078         *data = calloc(1, sizeof(data_s));
2079
2080         if (*data == NULL)
2081                 return false;
2082
2083         (*data)->length = 10;
2084         (*data)->buffer = calloc(1, (*data)->length);
2085
2086         if ((*data)->buffer == NULL)
2087                 return false;
2088
2089         snprintf((char *)(*data)->buffer, (*data)->length, "%d.%d", 1, 0);
2090
2091         DEBUG_EMUL_END();
2092
2093         return true;
2094 }
2095
2096 static bool net_nfc_emul_controller_check_firmware_version(net_nfc_error_e *result)
2097 {
2098         if (result == NULL)
2099                 return false;
2100
2101         *result = NET_NFC_OK;
2102
2103         DEBUG_EMUL_BEGIN();
2104
2105         DEBUG_EMUL_END();
2106
2107         return true;
2108 }
2109
2110 static bool net_nfc_emul_controller_update_firmware(net_nfc_error_e *result)
2111 {
2112         if (result == NULL)
2113                 return false;
2114
2115         *result = NET_NFC_OK;
2116
2117         DEBUG_EMUL_BEGIN();
2118
2119         DEBUG_EMUL_END();
2120
2121         return true;
2122 }
2123
2124 static bool net_nfc_emul_controller_get_stack_information(net_nfc_stack_information_s *stack_info, net_nfc_error_e *result)
2125 {
2126         if (result == NULL)
2127                 return false;
2128
2129         *result = NET_NFC_OK;
2130
2131         DEBUG_EMUL_BEGIN();
2132
2133         DEBUG_EMUL_END();
2134
2135         return true;
2136 }
2137
2138 static bool net_nfc_emul_controller_configure_discovery(net_nfc_discovery_mode_e mode, net_nfc_event_filter_e config, net_nfc_error_e *result)
2139 {
2140         int idx;
2141         bool ret = true;
2142
2143         if (result == NULL)
2144                 return false;
2145
2146         *result = NET_NFC_OK;
2147
2148         if (mode == NET_NFC_DISCOVERY_MODE_START) {
2149                 mode = NET_NFC_DISCOVERY_MODE_CONFIG;
2150         } else if (mode == NET_NFC_DISCOVERY_MODE_STOP) {
2151                 mode = NET_NFC_DISCOVERY_MODE_CONFIG;
2152                 config = NET_NFC_ALL_DISABLE;
2153         }
2154
2155         DEBUG_EMUL_BEGIN();
2156
2157         if ((mode == NET_NFC_DISCOVERY_MODE_CONFIG)) {
2158                 if (config == NET_NFC_ALL_DISABLE) {
2159                         /* This handle is not useful anymore */
2160                         __net_nfc_make_invalid_target_handle();
2161
2162                         /* reset socket_info */
2163                         for (idx = 0; idx < LLCP_NB_SOCKET_MAX; idx++)
2164                                 _net_nfc_remove_socket_slot((net_nfc_llcp_socket_t)idx);
2165
2166                         DEBUG_MSG("Kill Thread");
2167
2168                         ret = net_nfc_emul_controller_deinit();
2169                 } else if (config == NET_NFC_ALL_ENABLE) {
2170                         net_nfc_error_e err;
2171
2172                         DEBUG_MSG("Create Thread");
2173                         ret = net_nfc_emul_controller_init(&err);
2174                 }
2175         }
2176
2177         DEBUG_EMUL_END();
2178
2179         return ret;
2180 }
2181
2182 static bool net_nfc_emul_controller_get_secure_element_list(net_nfc_secure_element_info_s *list, int *count, net_nfc_error_e *result)
2183 {
2184         if (result == NULL)
2185                 return false;
2186
2187         *result = NET_NFC_OK;
2188
2189         DEBUG_EMUL_BEGIN();
2190
2191         DEBUG_EMUL_END();
2192
2193         return true;
2194 }
2195
2196 static bool net_nfc_emul_controller_set_secure_element_mode(net_nfc_secure_element_type_e element_type, net_nfc_secure_element_mode_e mode, net_nfc_error_e *result)
2197 {
2198         if (result == NULL)
2199                 return false;
2200
2201         *result = NET_NFC_OK;
2202
2203         DEBUG_EMUL_BEGIN();
2204
2205         DEBUG_EMUL_END();
2206
2207         return true;
2208 }
2209
2210 static bool net_nfc_emul_controller_check_target_presence(net_nfc_target_handle_s *handle, net_nfc_error_e *result)
2211 {
2212         if (result == NULL)
2213                 return false;
2214
2215         *result = NET_NFC_OK;
2216
2217         usleep(300 * 1000);
2218
2219         if (_net_nfc_emul_get_is_tag_attached()) {
2220                 return true;
2221         } else {
2222                 DEBUG_MSG("TAG Detached");
2223                 return false;
2224         }
2225 }
2226
2227 static bool net_nfc_emul_controller_connect(net_nfc_target_handle_s *handle, net_nfc_error_e *result)
2228 {
2229         if (result == NULL)
2230                 return false;
2231
2232         *result = NET_NFC_OK;
2233
2234         if (!__net_nfc_is_valide_target_handle(handle)) {
2235                 *result = NET_NFC_INVALID_HANDLE;
2236                 return false;
2237         }
2238
2239         DEBUG_EMUL_BEGIN();
2240
2241         DEBUG_EMUL_END();
2242
2243         return true;
2244 }
2245
2246 static bool net_nfc_emul_controller_disconnect(net_nfc_target_handle_s *handle, net_nfc_error_e *result)
2247 {
2248         if (result == NULL)
2249                 return false;
2250
2251         *result = NET_NFC_OK;
2252
2253         DEBUG_EMUL_BEGIN();
2254
2255         if (!__net_nfc_is_valide_target_handle(handle)) {
2256                 *result = NET_NFC_INVALID_HANDLE;
2257                 return false;
2258         }
2259
2260         // This handle is not useful anymore
2261         __net_nfc_make_invalid_target_handle();
2262
2263         /* reset socket_info */
2264         int idx = 0;
2265         for (; idx < LLCP_NB_SOCKET_MAX; idx++)
2266                 _net_nfc_remove_socket_slot((net_nfc_llcp_socket_t)idx);
2267
2268         DEBUG_EMUL_END();
2269
2270         return true;
2271 }
2272
2273 static bool net_nfc_emul_controller_check_ndef(net_nfc_target_handle_s *handle, uint8_t *ndef_card_state, int *max_data_size, int *real_data_size, net_nfc_error_e *result)
2274 {
2275         data_s rawdata = {NULL, 0};
2276
2277         if (result == NULL)
2278                 return false;
2279
2280         *result = NET_NFC_OK;
2281
2282         if (handle == NULL || ndef_card_state == NULL || max_data_size == NULL || real_data_size == NULL || result == NULL) {
2283                 *result = NET_NFC_NULL_PARAMETER;
2284                 return false;
2285         }
2286
2287         if (!__net_nfc_is_valide_target_handle(handle)) {
2288                 *result = NET_NFC_INVALID_HANDLE;
2289                 return false;
2290         }
2291
2292         DEBUG_EMUL_BEGIN();
2293
2294         _net_nfc_retrieve_ndef_message(&rawdata);
2295         _nfc_emul_util_free_mem(rawdata.buffer);
2296
2297         if (_net_nfc_emul_get_is_tag_attached()) {
2298                 *ndef_card_state = NET_NFC_NDEF_CARD_READ_WRITE;
2299                 *max_data_size = BUFFER_LENGTH_MAX;
2300                 *real_data_size = rawdata.length;
2301                 DEBUG_MSG("Card State : [%d] MAX data size :[%d] actual data size = [%d]", *ndef_card_state, *max_data_size, *real_data_size);
2302         } else {
2303                 DEBUG_MSG("target detached");
2304         }
2305
2306         DEBUG_EMUL_END();
2307
2308         return true;
2309 }
2310
2311 static bool net_nfc_emul_controller_make_read_only_ndef(net_nfc_target_handle_s *handle, net_nfc_error_e *result)
2312 {
2313         if (result == NULL)
2314                 return false;
2315
2316         *result = NET_NFC_OK;
2317
2318         if (!__net_nfc_is_valide_target_handle(handle)) {
2319                 *result = NET_NFC_INVALID_HANDLE;
2320                 return false;
2321         }
2322
2323         DEBUG_EMUL_BEGIN();
2324
2325         DEBUG_EMUL_END();
2326
2327         return true;
2328 }
2329
2330 static bool net_nfc_emul_controller_read_ndef(net_nfc_target_handle_s *handle, data_s **data, net_nfc_error_e *result)
2331 {
2332         int real_data_size = 0;
2333         data_s rawdata = {NULL, 0};
2334
2335         DEBUG_EMUL_BEGIN();
2336
2337         if (result == NULL)
2338                 return false;
2339
2340         *result = NET_NFC_OK;
2341
2342         if (handle == NULL || data == NULL || result == NULL) {
2343                 DEBUG_ERR_MSG("NET_NFC_NULL_PARAMETER >>>");
2344                 *result = NET_NFC_NULL_PARAMETER;
2345                 return false;
2346         }
2347
2348         if (!__net_nfc_is_valide_target_handle(handle)) {
2349                 DEBUG_ERR_MSG("NET_NFC_INVALID_HANDLE >>>");
2350                 *result = NET_NFC_INVALID_HANDLE;
2351                 return false;
2352         }
2353
2354         if (!_net_nfc_emul_get_is_tag_attached()) {
2355                 DEBUG_ERR_MSG("NET_NFC_NOT_ALLOWED_OPERATION >>>");
2356                 *result = NET_NFC_NOT_ALLOWED_OPERATION;
2357                 return false;
2358         }
2359
2360         *result = _net_nfc_retrieve_ndef_message(&rawdata);
2361         if (*result != NET_NFC_OK) {
2362                 _nfc_emul_util_free_mem(rawdata.buffer);
2363                 return false;
2364         }
2365
2366         real_data_size = rawdata.length;
2367
2368         if (real_data_size == 0) {
2369                 DEBUG_ERR_MSG("read ndef_msg is failed >>> real_data_size is zero");
2370                 *result = NET_NFC_NO_NDEF_MESSAGE;
2371                 _nfc_emul_util_free_mem(rawdata.buffer);
2372                 return false;
2373         }
2374
2375         *data = calloc(1, sizeof(data_s));
2376
2377         if (*data == NULL) {
2378                 *result = NET_NFC_ALLOC_FAIL;
2379                 _nfc_emul_util_free_mem(rawdata.buffer);
2380                 return false;
2381         }
2382
2383         (*data)->length = real_data_size;
2384         (*data)->buffer = calloc(1, real_data_size);
2385
2386         if ((*data)->buffer == NULL) {
2387                 free(*data);
2388                 *result = NET_NFC_ALLOC_FAIL;
2389                 _nfc_emul_util_free_mem(rawdata.buffer);
2390                 return false;
2391         }
2392
2393         /* copy rawdata to data->buffer */
2394         memcpy((*data)->buffer, rawdata.buffer, real_data_size);
2395         _nfc_emul_util_free_mem(rawdata.buffer);
2396
2397         DEBUG_EMUL_END();
2398
2399         return true;
2400 }
2401
2402 static bool net_nfc_emul_controller_write_ndef(net_nfc_target_handle_s *handle, data_s *data, net_nfc_error_e *result)
2403 {
2404         if (result == NULL)
2405                 return false;
2406
2407         *result = NET_NFC_OK;
2408
2409         if (!__net_nfc_is_valide_target_handle(handle)) {
2410                 *result = NET_NFC_INVALID_HANDLE;
2411                 return false;
2412         }
2413
2414         DEBUG_EMUL_BEGIN();
2415
2416         DEBUG_MSG("net_nfc_emul_controller_write_ndef success >>>");
2417
2418         _net_nfc_store_ndef_message(data);
2419
2420         DEBUG_EMUL_END();
2421
2422         return true;
2423 }
2424
2425
2426 static bool net_nfc_emul_controller_transceive(net_nfc_target_handle_s *handle,
2427         net_nfc_transceive_info_s *info, data_s **data, net_nfc_error_e *result)
2428 {
2429         bool ret = false;
2430
2431         if (result == NULL)
2432                 return ret;
2433
2434         if (info == NULL || info->trans_data.buffer == NULL ||
2435                 info->trans_data.length == 0) {
2436                 *result = NET_NFC_INVALID_PARAM;
2437                 return ret;
2438         }
2439
2440         *result = NET_NFC_OK;
2441         *data = NULL;
2442
2443         if (!__net_nfc_is_valide_target_handle(handle)) {
2444                 *result = NET_NFC_INVALID_HANDLE;
2445                 return ret;
2446         }
2447
2448         DEBUG_EMUL_BEGIN();
2449
2450         /* This implementation is not needed on Emulator environment */
2451
2452         DEBUG_EMUL_END();
2453
2454         return ret;
2455 }
2456
2457 static bool net_nfc_emul_controller_format_ndef(net_nfc_target_handle_s *handle, data_s *secure_key, net_nfc_error_e *result)
2458 {
2459         if (result == NULL)
2460                 return false;
2461
2462         *result = NET_NFC_OK;
2463
2464         if (!__net_nfc_is_valide_target_handle(handle)) {
2465                 *result = NET_NFC_INVALID_HANDLE;
2466                 return false;
2467         }
2468
2469         DEBUG_EMUL_BEGIN();
2470
2471         DEBUG_EMUL_END();
2472
2473         return true;
2474 }
2475
2476 static bool net_nfc_emul_controller_exception_handler(void)
2477 {
2478         DEBUG_EMUL_BEGIN();
2479
2480         net_nfc_error_e error;
2481
2482         if (net_nfc_emul_controller_init(&error) == false) {
2483                 DEBUG_ERR_MSG("exception handler is failed!!");
2484                 exit(0xff);
2485         }
2486
2487         DEBUG_EMUL_END();
2488
2489         return true;
2490 }
2491
2492 static bool net_nfc_emul_controller_is_ready(net_nfc_error_e *result)
2493 {
2494         if (result == NULL)
2495                 return false;
2496
2497         *result = NET_NFC_OK;
2498
2499         DEBUG_EMUL_BEGIN();
2500
2501         DEBUG_EMUL_END();
2502
2503         return g_stack_init_successful;
2504 }
2505
2506
2507 /*******************
2508 *       LLCP definition     *
2509 ********************/
2510
2511 static bool net_nfc_emul_controller_llcp_config(net_nfc_llcp_config_info_s *config, net_nfc_error_e *result)
2512 {
2513         if (result == NULL)
2514                 return false;
2515
2516         *result = NET_NFC_OK;
2517
2518         DEBUG_EMUL_BEGIN();
2519
2520         DEBUG_EMUL_END();
2521
2522         return true;
2523 }
2524
2525 static bool net_nfc_emul_controller_llcp_check_llcp(net_nfc_target_handle_s *handle, net_nfc_error_e *result)
2526 {
2527         if (result == NULL)
2528                 return false;
2529
2530         *result = NET_NFC_OK;
2531
2532         if (!__net_nfc_is_valide_target_handle(handle)) {
2533                 *result = NET_NFC_INVALID_HANDLE;
2534                 return false;
2535         }
2536
2537         DEBUG_EMUL_BEGIN();
2538
2539         DEBUG_EMUL_END();
2540
2541         return true;
2542 }
2543
2544 static bool net_nfc_emul_controller_llcp_activate_llcp(net_nfc_target_handle_s *handle, net_nfc_error_e *result)
2545 {
2546         if (result == NULL)
2547                 return false;
2548
2549         *result = NET_NFC_OK;
2550
2551         if (!__net_nfc_is_valide_target_handle(handle)) {
2552                 *result = NET_NFC_INVALID_HANDLE;
2553                 return false;
2554         }
2555
2556         DEBUG_EMUL_BEGIN();
2557
2558         DEBUG_EMUL_END();
2559
2560         return true;
2561 }
2562
2563 static bool net_nfc_emul_controller_llcp_create_socket(net_nfc_llcp_socket_t *socket, net_nfc_socket_type_e socketType, uint16_t miu, uint8_t rw, net_nfc_error_e *result, void *user_param)
2564 {
2565         if (result == NULL)
2566                 return false;
2567
2568         *result = NET_NFC_OK;
2569
2570         DEBUG_EMUL_BEGIN();
2571
2572         /* get available socket */
2573         socket_info_s *socket_info = _net_nfc_get_available_socket_slot();
2574         if (socket_info == NULL) {
2575                 DEBUG_ERR_MSG("The available socket is nothing!!");
2576                 return false;
2577         }
2578
2579         /* get llcp state */
2580         llcp_state_e llcp_state = NET_NFC_STATE_UNKNOWN;
2581         llcp_state = _net_nfc_get_llcp_state(user_param);
2582         if (llcp_state == NET_NFC_STATE_UNKNOWN) {
2583                 DEBUG_ERR_MSG("llcp_state is NET_NFC_STATE_UNKNOWN!!");
2584                 return false;
2585         }
2586
2587         if (llcp_state == NET_NFC_STATE_EXCHANGER_SERVER) {
2588                 DEBUG_MSG("NET_NFC_STATE_EXCHANGER_SERVER");
2589
2590                 *socket = NET_NFC_EMUL_SNEP_SERVER_SOCKET_NUMBER;
2591         } else if (llcp_state == NET_NFC_STATE_EXCHANGER_SERVER_NPP) {
2592                 DEBUG_MSG("NET_NFC_STATE_EXCHANGER_SERVER_NPP");
2593
2594                 *socket = NET_NFC_EMUL_NPP_SERVER_SOCKET_NUMBER;
2595         } else if (llcp_state == NET_NFC_STATE_EXCHANGER_CLIENT) {
2596                 DEBUG_MSG("NET_NFC_STATE_EXCHANGER_CLIENT");
2597
2598                 *socket = NET_NFC_EMUL_CLIENT_SOCKET_NUMBER;
2599         } else if (llcp_state == NET_NFC_STATE_CONN_HANDOVER_REQUEST) {
2600                 DEBUG_MSG("NET_NFC_STATE_CONN_HANDOVER_REQUEST");
2601
2602                 *socket = NET_NFC_EMUL_HANDOVER_REQUEST_SOCKET_NUMBER;
2603         } else if (llcp_state == NET_NFC_STATE_CONN_HANDOVER_SELECT) {
2604                 DEBUG_MSG("NET_NFC_STATE_CONN_HANDOVER_SELECT");
2605
2606                 *socket = NET_NFC_EMUL_HANDOVER_SELECT_SOCKET_NUMBER;
2607         } else {
2608                 DEBUG_MSG("we doesn't support..");
2609
2610                 return false;
2611         }
2612
2613         DEBUG_MSG("socket is created = [0x%x]", *socket);
2614
2615         socket_info->socket_handle = *socket;
2616         socket_info->user_context = (void *)user_param;
2617
2618         DEBUG_EMUL_END();
2619
2620         return true;
2621 }
2622
2623 static bool net_nfc_emul_controller_llcp_bind(net_nfc_llcp_socket_t socket, uint8_t service_access_point, net_nfc_error_e *result)
2624 {
2625         if (result == NULL)
2626                 return false;
2627
2628         *result = NET_NFC_OK;
2629
2630         DEBUG_EMUL_BEGIN();
2631
2632         DEBUG_EMUL_END();
2633
2634         return true;
2635 }
2636
2637 static bool net_nfc_emul_controller_llcp_listen(net_nfc_target_handle_s *handle, uint8_t *service_access_name, net_nfc_llcp_socket_t socket, net_nfc_error_e *result, void *user_param)
2638 {
2639         if (result == NULL)
2640                 return false;
2641
2642         *result = NET_NFC_OK;
2643
2644         if (!__net_nfc_is_valide_target_handle(handle)) {
2645                 *result = NET_NFC_INVALID_HANDLE;
2646                 return false;
2647         }
2648
2649         DEBUG_EMUL_BEGIN();
2650
2651         /* Emul don't create real socket. So, we don't need to wait accept from remote socket */
2652         /* In here, send accept event for only snep */
2653         net_nfc_request_listen_socket_t *detail = NULL;
2654
2655         socket_info_s *socket_info = _net_nfc_find_server_socket(socket);
2656         if (socket_info == NULL) {
2657                 DEBUG_ERR_MSG("socket_info is NULL");
2658                 return false;
2659         }
2660
2661         llcp_state_e llcp_state = NET_NFC_STATE_UNKNOWN;
2662         llcp_state = _net_nfc_get_llcp_state(socket_info->user_context);
2663         if (llcp_state == NET_NFC_STATE_UNKNOWN) {
2664                 DEBUG_ERR_MSG("llcp_state is NET_NFC_STATE_UNKNOWN");
2665                 return false;
2666         }
2667
2668         if (llcp_state == NET_NFC_STATE_EXCHANGER_SERVER) {
2669                 _nfc_emul_util_alloc_mem(detail, sizeof(*detail));
2670
2671                 if (detail != NULL) {
2672                         detail->length = sizeof(*detail);
2673                         detail->request_type = NET_NFC_MESSAGE_SERVICE_LLCP_LISTEN;
2674
2675                         socket_info->user_context = user_param;
2676
2677                         detail->handle = handle;
2678                         detail->client_socket = NET_NFC_EMUL_INCOMING_SOCKET_NUMBER;
2679                         detail->trans_param = socket_info->user_context;
2680                         detail->result = NET_NFC_OK;
2681
2682                         DEBUG_MSG("accept callback is called");
2683                         g_emul_controller_llcp_cb(detail, socket_info->user_context);
2684                 }
2685         } else {
2686                 DEBUG_MSG("llcp_state is [%d]", llcp_state);
2687         }
2688
2689         DEBUG_EMUL_END();
2690
2691         return true;
2692 }
2693
2694 /* below accept function does not used. */
2695 static bool net_nfc_emul_controller_llcp_accept(net_nfc_llcp_socket_t socket, net_nfc_error_e *result, void *user_param)
2696 {
2697         if (result == NULL)
2698                 return false;
2699
2700         *result = NET_NFC_OK;
2701
2702         DEBUG_EMUL_BEGIN();
2703
2704         DEBUG_EMUL_END();
2705
2706         return true;
2707 }
2708
2709 static bool net_nfc_emul_controller_llcp_connect(net_nfc_target_handle_s *handle, net_nfc_llcp_socket_t socket, uint8_t service_access_point, net_nfc_error_e *result, void *user_param)
2710 {
2711         if (result == NULL)
2712                 return false;
2713
2714         *result = NET_NFC_OK;
2715
2716         if (!__net_nfc_is_valide_target_handle(handle)) {
2717                 *result = NET_NFC_INVALID_HANDLE;
2718                 return false;
2719         }
2720
2721         DEBUG_EMUL_BEGIN();
2722
2723         socket_info_s *socket_info = _net_nfc_find_server_socket(socket);
2724         if (socket_info == NULL) {
2725                 DEBUG_ERR_MSG("socket_info is NULL");
2726                 return false;
2727         }
2728
2729         llcp_state_e llcp_state = NET_NFC_STATE_UNKNOWN;
2730         llcp_state = _net_nfc_get_llcp_state(socket_info->user_context);
2731         if (llcp_state == NET_NFC_STATE_UNKNOWN) {
2732                 DEBUG_ERR_MSG("llcp_state is NET_NFC_STATE_UNKNOWN");
2733                 return false;
2734         }
2735
2736         DEBUG_EMUL_END();
2737
2738         return true;
2739 }
2740
2741 static bool net_nfc_emul_controller_llcp_connect_by_url(net_nfc_target_handle_s *handle, net_nfc_llcp_socket_t  socket, uint8_t *service_access_name, net_nfc_error_e *result, void *user_param)
2742 {
2743         if (result == NULL)
2744                 return false;
2745
2746         *result = NET_NFC_OK;
2747
2748         if (!__net_nfc_is_valide_target_handle(handle)) {
2749                 *result = NET_NFC_INVALID_HANDLE;
2750                 return false;
2751         }
2752
2753         DEBUG_EMUL_BEGIN();
2754
2755         socket_info_s *socket_info = _net_nfc_find_server_socket(socket);
2756         if (socket_info == NULL) {
2757                 DEBUG_ERR_MSG("socket_info is NULL");
2758                 return false;
2759         }
2760
2761         llcp_state_e llcp_state = NET_NFC_STATE_UNKNOWN;
2762         llcp_state = _net_nfc_get_llcp_state(socket_info->user_context);
2763         if (llcp_state == NET_NFC_STATE_UNKNOWN) {
2764                 DEBUG_ERR_MSG("llcp_state is NET_NFC_STATE_UNKNOWN");
2765                 return false;
2766         }
2767
2768         DEBUG_EMUL_END();
2769
2770         return true;
2771 }
2772
2773 static bool net_nfc_emul_controller_llcp_send(net_nfc_target_handle_s *handle, net_nfc_llcp_socket_t    socket, data_s *data, net_nfc_error_e *result, void *user_param)
2774 {
2775         if (result == NULL)
2776                 return false;
2777
2778         *result = NET_NFC_OK;
2779
2780         if (!__net_nfc_is_valide_target_handle(handle)) {
2781                 *result = NET_NFC_INVALID_HANDLE;
2782                 return false;
2783         }
2784
2785         DEBUG_EMUL_BEGIN();
2786
2787         socket_info_s *socket_info = _net_nfc_find_server_socket(socket);
2788         if (socket_info == NULL) {
2789                 DEBUG_ERR_MSG("socket_info is NULL");
2790                 return false;
2791         }
2792
2793         llcp_state_e llcp_state = NET_NFC_STATE_UNKNOWN;
2794         llcp_state = _net_nfc_get_llcp_state(socket_info->user_context);
2795         if (llcp_state == NET_NFC_STATE_UNKNOWN) {
2796                 DEBUG_ERR_MSG("llcp_state is NET_NFC_STATE_UNKNOWN");
2797                 return false;
2798         }
2799
2800         if (llcp_state == NET_NFC_STATE_EXCHANGER_SERVER) {
2801                 net_nfc_request_llcp_msg_t *req_msg = NULL;
2802
2803                 _nfc_emul_util_alloc_mem(req_msg, sizeof(net_nfc_request_llcp_msg_t));
2804
2805                 socket_info->user_context = user_param;
2806
2807                 if (req_msg != NULL) {
2808                         req_msg->length = sizeof(net_nfc_request_llcp_msg_t);
2809                         req_msg->request_type = NET_NFC_MESSAGE_SERVICE_LLCP_SEND;
2810                         req_msg->result = NET_NFC_OK;
2811
2812                         DEBUG_MSG("send callback is called");
2813                         g_emul_controller_llcp_cb(req_msg, socket_info->user_context);
2814                 }
2815         }
2816
2817         DEBUG_EMUL_END();
2818
2819         return true;
2820 }
2821
2822 static bool net_nfc_emul_controller_llcp_send_to(net_nfc_target_handle_s *handle, net_nfc_llcp_socket_t socket, data_s *data, uint8_t service_access_point, net_nfc_error_e *result, void *user_param)
2823 {
2824         if (result == NULL)
2825                 return false;
2826
2827         *result = NET_NFC_OK;
2828
2829         if (!__net_nfc_is_valide_target_handle(handle)) {
2830                 *result = NET_NFC_INVALID_HANDLE;
2831                 return false;
2832         }
2833
2834         DEBUG_EMUL_BEGIN();
2835
2836         socket_info_s *socket_info = _net_nfc_find_server_socket(socket);
2837         if (socket_info == NULL) {
2838                 DEBUG_ERR_MSG("socket_info is NULL");
2839                 return false;
2840         }
2841
2842         llcp_state_e llcp_state = NET_NFC_STATE_UNKNOWN;
2843         llcp_state = _net_nfc_get_llcp_state(socket_info->user_context);
2844         if (llcp_state == NET_NFC_STATE_UNKNOWN) {
2845                 DEBUG_ERR_MSG("llcp_state is NET_NFC_STATE_UNKNOWN");
2846                 return false;
2847         }
2848
2849         if (llcp_state == NET_NFC_STATE_EXCHANGER_SERVER) {
2850                 net_nfc_request_llcp_msg_t *req_msg = NULL;
2851
2852                 _nfc_emul_util_alloc_mem(req_msg, sizeof(net_nfc_request_llcp_msg_t));
2853
2854                 socket_info->user_context = user_param;
2855
2856                 if (req_msg != NULL) {
2857                         req_msg->length = sizeof(net_nfc_request_llcp_msg_t);
2858                         req_msg->request_type = NET_NFC_MESSAGE_SERVICE_LLCP_SEND_TO;
2859                         req_msg->result = NET_NFC_OK;
2860
2861                         DEBUG_MSG("send_to callback is called");
2862                         g_emul_controller_llcp_cb(req_msg, socket_info->user_context);
2863                 }
2864         }
2865
2866         DEBUG_EMUL_END();
2867
2868         return true;
2869 }
2870
2871
2872 static bool net_nfc_emul_controller_llcp_recv(net_nfc_target_handle_s *handle, net_nfc_llcp_socket_t socket, data_s *data, net_nfc_error_e *result, void *user_param)
2873 {
2874         if (result == NULL || data == NULL)
2875                 return false;
2876
2877         *result = NET_NFC_OK;
2878
2879         if (!__net_nfc_is_valide_target_handle(handle)) {
2880                 *result = NET_NFC_INVALID_HANDLE;
2881                 return false;
2882         }
2883
2884         DEBUG_EMUL_BEGIN();
2885
2886         socket_info_s *socket_info = _net_nfc_find_server_socket(socket);
2887         if (socket_info == NULL) {
2888                 DEBUG_ERR_MSG("socket_info is NULL");
2889                 return false;
2890         }
2891
2892         llcp_state_e llcp_state = NET_NFC_STATE_UNKNOWN;
2893         llcp_state = _net_nfc_get_llcp_state(socket_info->user_context);
2894         if (llcp_state == NET_NFC_STATE_UNKNOWN) {
2895                 DEBUG_ERR_MSG("llcp_state is NET_NFC_STATE_UNKNOWN");
2896                 return false;
2897         }
2898
2899         if (llcp_state == NET_NFC_STATE_EXCHANGER_SERVER) {
2900
2901                 DEBUG_MSG("NET_NFC_STATE_EXCHANGER_SERVER");
2902                 socket_info->user_context = user_param;
2903
2904                 if (Snep_Server_msg->isSegment) {
2905                         /* send snep msg continueosly  ..*/
2906                         DEBUG_MSG("send segments for snep msg");
2907
2908                         int remained_size = 0;
2909
2910                         remained_size = Snep_Server_msg->data->length - Snep_Server_msg->offset;
2911                         DEBUG_MSG("remained_size[%d]", remained_size);
2912
2913                         /* copy rawdata to llcp_server_data->buffer */
2914                         if (remained_size <= SNEP_MAX_BUFFER) {
2915                                 DEBUG_MSG("send last segment >>>");
2916
2917                                 llcp_server_data->length = remained_size;
2918                                 memcpy(llcp_server_data->buffer, Snep_Server_msg->data->buffer + Snep_Server_msg->offset, remained_size);
2919
2920                                 Snep_Server_msg->isSegment = false;
2921                         } else {
2922                                 DEBUG_MSG("send continue segment >>>");
2923
2924                                 llcp_server_data->length = SNEP_MAX_BUFFER;
2925                                 memcpy(llcp_server_data->buffer, Snep_Server_msg->data->buffer + Snep_Server_msg->offset, SNEP_MAX_BUFFER);
2926
2927                                 Snep_Server_msg->offset += SNEP_MAX_BUFFER;
2928                         }
2929
2930                         _net_nfc_llcp_data_receive_cb(socket_info->user_context); /* call callback */
2931                 } else {
2932                         /* In here, we dosen't call _net_nfc_llcp_data_receive_cb. just wait event from emulator */
2933                         /*After copying data address, we will return it, immediately */
2934                         DEBUG_MSG("data address is set");
2935                         llcp_server_data = data;
2936                 }
2937         } else {
2938                 DEBUG_MSG("we don't support..");
2939         }
2940
2941         DEBUG_EMUL_END();
2942
2943         return true;
2944 }
2945
2946 static bool net_nfc_emul_controller_llcp_recv_from(net_nfc_target_handle_s *handle, net_nfc_llcp_socket_t socket, data_s *data, net_nfc_error_e *result, void *user_param)
2947 {
2948         if (result == NULL)
2949                 return false;
2950
2951         *result = NET_NFC_OK;
2952
2953         if (!__net_nfc_is_valide_target_handle(handle)) {
2954                 *result = NET_NFC_INVALID_HANDLE;
2955                 return false;
2956         }
2957
2958         DEBUG_EMUL_BEGIN();
2959
2960         socket_info_s *socket_info = _net_nfc_find_server_socket(socket);
2961         if (socket_info == NULL) {
2962                 DEBUG_ERR_MSG("socket_info is NULL");
2963                 return false;
2964         }
2965
2966         llcp_state_e llcp_state = NET_NFC_STATE_UNKNOWN;
2967         llcp_state = _net_nfc_get_llcp_state(socket_info->user_context);
2968         if (llcp_state == NET_NFC_STATE_UNKNOWN) {
2969                 DEBUG_ERR_MSG("llcp_state is NET_NFC_STATE_UNKNOWN");
2970                 return false;
2971         }
2972
2973         if (llcp_state == NET_NFC_STATE_EXCHANGER_SERVER) {
2974
2975                 DEBUG_MSG("NET_NFC_STATE_EXCHANGER_SERVER");
2976                 socket_info->user_context = user_param;
2977
2978                 if (Snep_Server_msg->isSegment) {
2979                         /* send snep msg continueosly  ..*/
2980                         DEBUG_MSG("send segments for snep msg");
2981
2982                         int remained_size = 0;
2983
2984                         remained_size = Snep_Server_msg->data->length - Snep_Server_msg->offset;
2985                         DEBUG_MSG("remained_size[%d]", remained_size);
2986
2987                         /* copy rawdata to llcp_server_data->buffer */
2988                         if (remained_size <= SNEP_MAX_BUFFER) {
2989                                 DEBUG_MSG("send last segment >>>");
2990
2991                                 llcp_server_data->length = remained_size;
2992                                 memcpy(llcp_server_data->buffer, Snep_Server_msg->data->buffer + Snep_Server_msg->offset, remained_size);
2993
2994                                 Snep_Server_msg->isSegment = false;
2995                         } else {
2996                                 DEBUG_MSG("send continue segment >>>");
2997
2998                                 llcp_server_data->length = SNEP_MAX_BUFFER;
2999                                 memcpy(llcp_server_data->buffer, Snep_Server_msg->data->buffer + Snep_Server_msg->offset, SNEP_MAX_BUFFER);
3000
3001                                 Snep_Server_msg->offset += SNEP_MAX_BUFFER;
3002                         }
3003
3004                         _net_nfc_llcp_data_receive_from_cb(socket_info->user_context); /* call callback */
3005                 } else {
3006                         /* In here, we dosen't call _net_nfc_llcp_data_receive_cb. just wait event from emulator */
3007                         /*After copying data address, we will return it, immediately */
3008                         if (data != NULL) {
3009                                 DEBUG_MSG("data address is set");
3010                                 llcp_server_data = data;
3011                         } else {
3012                                 DEBUG_ERR_MSG("data address is NULL");
3013                                 return false;
3014                         }
3015                 }
3016         } else {
3017                 DEBUG_MSG("we donen't support..");
3018         }
3019
3020         DEBUG_EMUL_END();
3021
3022         return true;
3023 }
3024
3025 static bool net_nfc_emul_controller_llcp_disconnect(net_nfc_target_handle_s *handle, net_nfc_llcp_socket_t      socket, net_nfc_error_e *result, void *user_param)
3026 {
3027         if (result == NULL)
3028                 return false;
3029
3030         *result = NET_NFC_OK;
3031
3032         if (!__net_nfc_is_valide_target_handle(handle)) {
3033                 *result = NET_NFC_INVALID_HANDLE;
3034                 return false;
3035         }
3036
3037         DEBUG_EMUL_BEGIN();
3038
3039         socket_info_s *socket_info = _net_nfc_find_server_socket(socket);
3040         if (socket_info == NULL) {
3041                 DEBUG_ERR_MSG("socket_info is NULL");
3042                 return false;
3043         }
3044
3045         llcp_state_e llcp_state = NET_NFC_STATE_UNKNOWN;
3046         llcp_state = _net_nfc_get_llcp_state(socket_info->user_context);
3047         if (llcp_state == NET_NFC_STATE_UNKNOWN) {
3048                 DEBUG_ERR_MSG("llcp_state is NET_NFC_STATE_UNKNOWN");
3049                 return false;
3050         }
3051
3052         /* send msg to framework */
3053         net_nfc_request_llcp_msg_t *req_msg = NULL;
3054
3055         _nfc_emul_util_alloc_mem(req_msg, sizeof(net_nfc_request_llcp_msg_t));
3056
3057         socket_info->user_context = user_param;
3058
3059         if (req_msg != NULL) {
3060                 req_msg->length = sizeof(net_nfc_request_llcp_msg_t);
3061                 req_msg->request_type = NET_NFC_MESSAGE_SERVICE_LLCP_DISCONNECT;
3062                 req_msg->result = NET_NFC_OK;
3063
3064                 DEBUG_MSG("disconnect callback is called");
3065                 g_emul_controller_llcp_cb(req_msg, user_param);
3066         }
3067
3068         /* reset socket_info */
3069         _net_nfc_remove_socket_slot((net_nfc_llcp_socket_t)socket);
3070
3071         DEBUG_EMUL_END();
3072
3073         return true;
3074 }
3075
3076 static bool net_nfc_emul_controller_llcp_socket_close(net_nfc_llcp_socket_t socket,
3077         net_nfc_error_e *result)
3078 {
3079         if (result == NULL)
3080                 return false;
3081
3082         *result = NET_NFC_OK;
3083
3084         DEBUG_EMUL_BEGIN();
3085
3086         DEBUG_EMUL_END();
3087
3088         return true;
3089 }
3090
3091 static bool net_nfc_emul_controller_llcp_reject(net_nfc_target_handle_s *handle, net_nfc_llcp_socket_t  socket, net_nfc_error_e *result)
3092 {
3093         if (result == NULL)
3094                 return false;
3095
3096         *result = NET_NFC_OK;
3097
3098         if (!__net_nfc_is_valide_target_handle(handle)) {
3099                 *result = NET_NFC_INVALID_HANDLE;
3100                 return false;
3101         }
3102
3103         DEBUG_EMUL_BEGIN();
3104
3105         DEBUG_EMUL_END();
3106
3107         return true;
3108 }
3109
3110 static bool net_nfc_emul_controller_llcp_get_remote_config(net_nfc_target_handle_s *handle, net_nfc_llcp_config_info_s *config, net_nfc_error_e *result)
3111 {
3112         /* In llcp specification ver 1.1, default miu size is 128 */
3113         const uint16_t default_miu = 128;
3114
3115         if (result == NULL)
3116                 return false;
3117
3118         *result = NET_NFC_OK;
3119
3120         if (!__net_nfc_is_valide_target_handle(handle)) {
3121                 *result = NET_NFC_INVALID_HANDLE;
3122                 return false;
3123         }
3124
3125         DEBUG_EMUL_BEGIN();
3126
3127         config->miu = default_miu;
3128
3129         DEBUG_EMUL_END();
3130
3131         return true;
3132 }
3133
3134 static bool net_nfc_emul_controller_llcp_get_remote_socket_info(net_nfc_target_handle_s *handle, net_nfc_llcp_socket_t socket, net_nfc_llcp_socket_option_s *option, net_nfc_error_e *result)
3135 {
3136         /* In llcp specification ver 1.1, default miu size is 128 */
3137         const uint16_t default_miu = 128;
3138
3139         if (result == NULL)
3140                 return false;
3141
3142         *result = NET_NFC_OK;
3143
3144         if (!__net_nfc_is_valide_target_handle(handle)) {
3145                 *result = NET_NFC_INVALID_HANDLE;
3146                 return false;
3147         }
3148
3149         DEBUG_EMUL_BEGIN();
3150
3151         option->miu = default_miu;
3152
3153         DEBUG_EMUL_END();
3154
3155         return true;
3156 }
3157
3158
3159 static bool net_nfc_emul_controller_support_nfc(net_nfc_error_e *result)
3160 {
3161         bool ret = false;
3162         struct stat st = {0};
3163
3164         if (stat("/dev/nfc0", &st) == -1) {
3165                 if (result)
3166                         *result = NET_NFC_NOT_SUPPORTED;
3167                 return ret;
3168         }
3169
3170         if (result)
3171                 *result = NET_NFC_OK;
3172
3173         ret = true;
3174
3175         return ret;
3176 }
3177 static bool net_nfc_emul_controller_secure_element_open(net_nfc_secure_element_type_e element_type, net_nfc_target_handle_s **handle, net_nfc_error_e *result)
3178 {
3179         DEBUG_ERR_MSG("se open start");
3180         *result = NET_NFC_OK;
3181         __net_nfc_make_valid_target_handle(handle);
3182
3183         DEBUG_ERR_MSG("se_open end");
3184         return true;
3185 }
3186 static bool net_nfc_emul_controller_secure_element_get_atr(net_nfc_target_handle_s *handle,
3187         data_s **atr, net_nfc_error_e *result)
3188 {
3189         bool ret = true;
3190         char buffer[1024];
3191         int length = sizeof(buffer);
3192         data_s *temp;
3193
3194         DEBUG_ERR_MSG("se get_atr start");
3195         *result = NET_NFC_OK;
3196         *atr = NULL;
3197
3198         strncpy(buffer, "getatr", sizeof(buffer));
3199
3200         temp = calloc(1, sizeof(*temp));
3201         if (temp != NULL) {
3202                 temp->buffer = calloc(1, length);
3203                 if (temp->buffer != NULL) {
3204                         memcpy(temp->buffer, buffer, length);
3205                         temp->length = length;
3206                         *atr = temp;
3207                         *result = NET_NFC_OK;
3208                 } else {
3209                         free(temp);
3210                         *result = NET_NFC_ALLOC_FAIL;
3211                         ret = false;
3212                 }
3213         } else {
3214                 *result = NET_NFC_ALLOC_FAIL;
3215                 ret = false;
3216         }
3217         DEBUG_ERR_MSG("se get atr end");
3218         return ret;
3219 }
3220 static bool net_nfc_emul_controller_secure_element_send_apdu(net_nfc_target_handle_s *handle,
3221         data_s *command, data_s **response, net_nfc_error_e *result)
3222 {
3223         bool ret = true;
3224         char buffer[1024];
3225         int length;
3226         data_s *temp;
3227
3228         DEBUG_ERR_MSG("se send apdu start");
3229
3230         *result = NET_NFC_OK;
3231         *response = NULL;
3232
3233         //buffer response
3234         strncpy(buffer, "response", sizeof(buffer));
3235
3236         length = strlen(buffer);
3237
3238         temp = calloc(1, sizeof(*temp));
3239         if (temp != NULL) {
3240                 temp->buffer = calloc(1, length + 1);
3241                 if (temp->buffer != NULL) {
3242                         memcpy(temp->buffer, buffer, sizeof(temp->buffer) - 1);
3243                         temp->length = length;
3244
3245                         *response = temp;
3246                         *result = NET_NFC_OK;
3247                 } else {
3248                         free(temp);
3249                         *result = NET_NFC_ALLOC_FAIL;
3250                         ret = false;
3251                 }
3252         } else {
3253                 *result = NET_NFC_ALLOC_FAIL;
3254                 ret = false;
3255         }
3256
3257         DEBUG_ERR_MSG("se send apdu end");
3258         return ret;
3259 }
3260 static bool net_nfc_emul_controller_secure_element_close(net_nfc_target_handle_s *handle,
3261         net_nfc_error_e *result)
3262 {
3263         DEBUG_ERR_MSG("se close start");
3264         *result = NET_NFC_OK;
3265         return true;
3266         DEBUG_ERR_MSG("se close end");
3267 }
3268
3269 static int nfc_backend_init(void **data)
3270 {
3271         hal_backend_nfc_funcs *nfc_funcs;
3272
3273         nfc_funcs = calloc(1, sizeof(hal_backend_nfc_funcs));
3274         if (!nfc_funcs)
3275                 return -ENOMEM;
3276
3277         nfc_funcs->start = net_nfc_emul_controller_init;
3278         nfc_funcs->stop = net_nfc_emul_controller_deinit;
3279         nfc_funcs->register_listener = net_nfc_emul_controller_register_listener;
3280         nfc_funcs->unregister_listener = net_nfc_emul_controller_unregister_listener;
3281         nfc_funcs->get_firmware_version = net_nfc_emul_controller_get_firmware_version;
3282         nfc_funcs->check_firmware_version = net_nfc_emul_controller_check_firmware_version;
3283         nfc_funcs->update_firmware = net_nfc_emul_controller_update_firmware;
3284         nfc_funcs->get_stack_information = net_nfc_emul_controller_get_stack_information;
3285         nfc_funcs->configure_discovery = net_nfc_emul_controller_configure_discovery;
3286         nfc_funcs->get_secure_element_list = net_nfc_emul_controller_get_secure_element_list;
3287         nfc_funcs->set_secure_element_mode = net_nfc_emul_controller_set_secure_element_mode;
3288         nfc_funcs->connect = net_nfc_emul_controller_connect;
3289         nfc_funcs->disconnect = net_nfc_emul_controller_disconnect;
3290         nfc_funcs->check_ndef = net_nfc_emul_controller_check_ndef;
3291         nfc_funcs->check_target_presence = net_nfc_emul_controller_check_target_presence;
3292         nfc_funcs->read_ndef = net_nfc_emul_controller_read_ndef;
3293         nfc_funcs->write_ndef = net_nfc_emul_controller_write_ndef;
3294         nfc_funcs->make_read_only_ndef = net_nfc_emul_controller_make_read_only_ndef;
3295         nfc_funcs->transceive = net_nfc_emul_controller_transceive;
3296         nfc_funcs->format_ndef = net_nfc_emul_controller_format_ndef;
3297         nfc_funcs->exception_handler = net_nfc_emul_controller_exception_handler;
3298         nfc_funcs->is_ready = net_nfc_emul_controller_is_ready;
3299
3300         nfc_funcs->llcp_config = net_nfc_emul_controller_llcp_config;
3301         nfc_funcs->llcp_check_llcp = net_nfc_emul_controller_llcp_check_llcp;
3302         nfc_funcs->llcp_activate_llcp = net_nfc_emul_controller_llcp_activate_llcp;
3303         nfc_funcs->llcp_create_socket = net_nfc_emul_controller_llcp_create_socket;
3304         nfc_funcs->llcp_bind = net_nfc_emul_controller_llcp_bind;
3305         nfc_funcs->llcp_listen = net_nfc_emul_controller_llcp_listen;
3306         nfc_funcs->llcp_accept = net_nfc_emul_controller_llcp_accept;
3307         nfc_funcs->llcp_connect_by_url = net_nfc_emul_controller_llcp_connect_by_url;
3308         nfc_funcs->llcp_connect = net_nfc_emul_controller_llcp_connect;
3309         nfc_funcs->llcp_disconnect = net_nfc_emul_controller_llcp_disconnect;
3310         nfc_funcs->llcp_socket_close = net_nfc_emul_controller_llcp_socket_close;
3311         nfc_funcs->llcp_recv = net_nfc_emul_controller_llcp_recv;
3312         nfc_funcs->llcp_send = net_nfc_emul_controller_llcp_send;
3313         nfc_funcs->llcp_recv_from = net_nfc_emul_controller_llcp_recv_from;
3314         nfc_funcs->llcp_send_to = net_nfc_emul_controller_llcp_send_to;
3315         nfc_funcs->llcp_reject = net_nfc_emul_controller_llcp_reject;
3316         nfc_funcs->llcp_get_remote_config = net_nfc_emul_controller_llcp_get_remote_config;
3317         nfc_funcs->llcp_get_remote_socket_info = net_nfc_emul_controller_llcp_get_remote_socket_info;
3318
3319         nfc_funcs->support_nfc = net_nfc_emul_controller_support_nfc;
3320
3321         nfc_funcs->secure_element_open = net_nfc_emul_controller_secure_element_open;
3322         nfc_funcs->secure_element_get_atr = net_nfc_emul_controller_secure_element_get_atr;
3323         nfc_funcs->secure_element_send_apdu = net_nfc_emul_controller_secure_element_send_apdu;
3324         nfc_funcs->secure_element_close = net_nfc_emul_controller_secure_element_close;
3325
3326         nfc_funcs->hce_response_apdu = NULL;
3327         nfc_funcs->secure_element_route_aid = NULL;
3328         nfc_funcs->secure_element_unroute_aid = NULL;
3329         nfc_funcs->secure_element_commit_routing = NULL;
3330         nfc_funcs->secure_element_set_default_route = NULL;
3331         nfc_funcs->secure_element_clear_aid_table = NULL;
3332         nfc_funcs->secure_element_get_aid_table_size = NULL;
3333
3334         DEBUG_EMUL_END();
3335
3336         *data = (void *)nfc_funcs;
3337
3338         return 0;
3339 }
3340
3341 static int nfc_backend_exit(void *data)
3342 {
3343         if (!data)
3344                 return -EINVAL;
3345         free(data);
3346
3347         return 0;
3348 }
3349
3350 EXPORT hal_backend hal_backend_nfc_data = {
3351         .name = "nfc-backend",
3352         .vendor = "Emulator",
3353         .abi_version = HAL_ABI_VERSION_TIZEN_6_5,
3354         .init = nfc_backend_init,
3355         .exit = nfc_backend_exit,
3356 };