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