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