2 * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
25 #include <sys/types.h>
32 #include <sys/epoll.h>
34 #ifdef USE_GLIB_MAIN_LOOP
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"
45 #include <netinet/in.h>
47 /*************************** STRUCTURE DEFINE START ***************************************/
49 #ifndef NET_NFC_EXPORT_API
50 #define NET_NFC_EXPORT_API __attribute__((visibility("default")))
54 EMUL_NFC_TAG_DISCOVERED = 100,
55 EMUL_NFC_TAG_DETACHED,
56 EMUL_NFC_P2P_DISCOVERED,
57 EMUL_NFC_P2P_DETACHED,
63 EMUL_TAG_TOPAZ_JEWEL =1,
66 EMUL_TAG_MIFARE_DESFIRE,
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,
78 } emul_ndef_name_format;
81 NET_NFC_STATE_EXCHANGER_SERVER = 0x00,
82 NET_NFC_STATE_EXCHANGER_SERVER_NPP,
84 NET_NFC_STATE_EXCHANGER_CLIENT,
85 NET_NFC_STATE_CONN_HANDOVER_REQUEST,
86 NET_NFC_STATE_CONN_HANDOVER_SELECT,
91 SNEP_REQ_CONTINUE = 0x00,
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;
105 typedef struct _socket_info_s {
106 net_nfc_llcp_socket_t socket_handle;
111 typedef struct _net_nfc_oem_llcp_state_t{
114 unsigned int fragment_offset;
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;
127 llcp_app_protocol_e type_app_protocol;
128 net_nfc_conn_handover_carrier_type_e type;
130 } net_nfc_oem_llcp_state_t;
132 typedef struct _snep_msg_s{
137 /* Members below are used for snep msg of client */
142 typedef struct _emulMsg_data_s{
143 net_nfc_record_tnf_e tnf;
151 typedef struct _emulMsg_s{
152 emul_message_id message_id;
153 emul_target_type target_type;
158 typedef void * (*emul_Nfc_thread_handler_t) (void * pParam);
160 /*************************** STRUCTURE DEFINE START ***************************************/
162 #define __USE_EPOLL_FOR_FILE__ 1
165 /****************************** DEFINE START *******************************************/
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
176 #ifdef __USE_EPOLL_FOR_FILE__
177 #define EPOLL_SIZE 128
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
189 #define SNEP_MAX_BUFFER 128
190 #define SNEP_MAJOR_VER 1
191 #define SNEP_MINOR_VER 0
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
199 /****************************** DEFINE END *******************************************/
203 /****************************** VARIABLE START *******************************************/
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 ;
211 /* for emulator management */
212 pthread_t gEmulThread;
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;
220 /* for llcp functionality */
222 socket_info_s socket_info_array[LLCP_NB_SOCKET_MAX] = {{0,}};
224 snep_msg_s* Snep_Server_msg;
225 data_s * llcp_server_data = NULL;
227 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
229 /****************************** VARIABLE END *******************************************/
233 /*************************** STATIC FUNCTION DECLARE START ***************************************/
235 /*************************** INTERFACE START ***************************************/
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);
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);
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);
295 /*************************** INTERFACE END ***************************************/
299 /*************************** ETC Function ***************************************/
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__)
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 ();
314 /*************************** STATIC FUNCTION DECLARE END ***************************************/
318 void __nfc_emul_util_free_mem (void** mem, char * filename, unsigned int line)
320 if (mem == NULL || *mem == NULL)
322 SECURE_LOGD ("FILE: %s, LINE:%d, Invalid parameter in mem free util (pinter is NULL)", filename, line);
329 void __nfc_emul_util_alloc_mem(void** mem, int size, char * filename, unsigned int line)
331 if (mem == NULL || size <= 0)
333 SECURE_LOGD ("FILE: %s, LINE:%d, Invalid parameter in mem alloc util", filename, line);
337 DEBUG_MSG("size to malloc() = [%d]", size);
341 SECURE_LOGD("FILE: %s, LINE:%d, WARNING: Pointer is already allocated or it was not initialized with NULL", filename, line);
344 *mem = malloc (size);
348 memset (*mem, 0x0, size);
352 SECURE_LOGD("FILE: %s, LINE:%d, Allocation is failed", filename, line);
356 static bool __net_nfc_is_valide_target_handle (net_nfc_target_handle_s * handle)
358 bool result = (current_working_handle == handle);
360 DEBUG_MSG ("[WARNING]: INVALID HANDLE IS DETECTED!");
364 static void __net_nfc_make_valid_target_handle (net_nfc_target_handle_s ** handle)
366 if (current_working_handle != NULL){
367 DEBUG_MSG ("[WARNING]: HANDLE WAS ALLOCATED ALREADY!");
369 _nfc_emul_util_alloc_mem (*handle, sizeof (net_nfc_target_handle_s));
370 if (*handle != NULL) {
371 current_working_handle = *handle;
374 static void __net_nfc_make_invalid_target_handle ()
376 if (current_working_handle != NULL) {
377 _nfc_emul_util_free_mem (current_working_handle);
378 current_working_handle = NULL;
382 static socket_info_s * _net_nfc_get_available_socket_slot ()
385 for (; idx < LLCP_NB_SOCKET_MAX; idx++)
387 if (socket_info_array [idx].isValid == false)
389 memset (&(socket_info_array[idx]), 0x00, sizeof (socket_info_s));
390 socket_info_array [idx].isValid = true;
391 return &(socket_info_array[idx]);
395 DEBUG_ERR_MSG("_net_nfc_get_available_socket_slot is failed");
399 static void _net_nfc_remove_socket_slot (net_nfc_llcp_socket_t socket)
403 for (; idx < LLCP_NB_SOCKET_MAX; idx++)
405 if (socket_info_array [idx].isValid == true &&
406 socket_info_array [idx].socket_handle == socket)
408 socket_info_array [idx].isValid = false;
409 socket_info_array [idx].socket_handle = 0;
410 socket_info_array [idx].user_context= NULL;
415 static socket_info_s * _net_nfc_find_server_socket (net_nfc_llcp_socket_t socket)
418 for (; idx < LLCP_NB_SOCKET_MAX; idx++)
420 if (socket_info_array [idx].socket_handle == socket && socket_info_array [idx].isValid == true)
422 return &(socket_info_array[idx]);
426 DEBUG_ERR_MSG("_net_nfc_find_server_socket is failed");
430 ////////////// INTERFACE START //////////
432 NET_NFC_EXPORT_API bool onload(net_nfc_oem_interface_s* emul_interfaces)
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;
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;
478 emul_interfaces->support_nfc = net_nfc_emul_controller_support_nfc;
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;
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;
498 static void _net_nfc_initialize_llcp(void)
502 if(Snep_Server_msg == NULL)
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);
512 static void _net_nfc_deinitialize_llcp(void)
516 if (Snep_Server_msg->data->buffer != NULL)
518 free(Snep_Server_msg->data->buffer);
519 Snep_Server_msg->data->buffer = NULL;
522 if (Snep_Server_msg->data != NULL)
524 free(Snep_Server_msg->data);
525 Snep_Server_msg->data = NULL;
528 if(Snep_Server_msg != NULL)
530 free(Snep_Server_msg);
531 Snep_Server_msg = NULL;
534 llcp_server_data = NULL;
539 static bool _net_nfc_emul_get_is_tag_attached(void)
541 return g_tag_attached;
544 static void _net_nfc_emul_set_is_tag_attached(bool is_detached)
546 g_tag_attached = is_detached;
547 DEBUG_MSG("set g_tag_attached [%d]", g_tag_attached);
550 static bool _net_nfc_emul_get_is_p2p_attached(void)
552 return g_p2p_attached;
555 static void _net_nfc_emul_set_is_p2p_attached(bool is_detached)
557 g_p2p_attached = is_detached;
558 DEBUG_MSG("set g_p2p_attached [%d]", g_p2p_attached);
561 static bool _net_nfc_is_data_emulMsgData(emul_message_id messageId)
568 case EMUL_NFC_TAG_DISCOVERED :{
573 case EMUL_NFC_P2P_SEND :{
578 case EMUL_NFC_TAG_DETACHED :
579 case EMUL_NFC_P2P_DISCOVERED :
580 case EMUL_NFC_P2P_DETACHED :
586 DEBUG_MSG("retval [%d]", retval);
593 static net_nfc_record_tnf_e _net_nfc_get_tnf_type(int name_format)
597 net_nfc_record_tnf_e tnf = NET_NFC_RECORD_EMPTY;
599 switch (name_format) {
600 case EMUL_NDEF_TNF_EMPTY :
601 tnf = NET_NFC_RECORD_EMPTY;
604 case EMUL_NDEF_TNF_WELL_KNOWN :
605 tnf = NET_NFC_RECORD_WELL_KNOWN_TYPE;
608 case EMUL_NDEF_TNF_MIME_MEDIA :
609 tnf = NET_NFC_RECORD_MIME_TYPE;
612 case EMUL_NDEF_TNF_ABSOLUTE_URI :
613 tnf = NET_NFC_RECORD_URI;
616 case EMUL_NDEF_TNF_EXTERNAL :
617 tnf = NET_NFC_RECORD_EXTERNAL_RTD;
620 case EMUL_NDEF_TNF_UNKNOWN :
621 tnf = NET_NFC_RECORD_UNKNOWN;
625 tnf = NET_NFC_RECORD_UNKNOWN;
626 DEBUG_MSG("data is currupted");
631 DEBUG_MSG("tnf [%d]", tnf);
639 static int _net_nfc_create_records_from_emulMsg(emulMsg_s *emul_msg, ndef_message_s **ndef_message, int record_count)
644 int create_record_count = 0;
645 char emulMsg[BUFFER_LENGTH_MAX] = { 0, };
647 memcpy(emulMsg, emul_msg->file_data, strlen((char*)emul_msg->file_data));
649 /* parsing data and create record to record structure */
650 for (index = 0; index < record_count ; index ++) {
654 char *record_payload;
656 emulMsg_data_s record = { 0, };
662 name_format = strtok((char *) emulMsg, NET_NFC_EMUL_MSG_RECORD_SEPERATOR);
666 name_format = strtok(NULL, NET_NFC_EMUL_MSG_RECORD_SEPERATOR);
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");
675 record_payload = strtok(NULL, NET_NFC_EMUL_MSG_RECORD_SEPERATOR);
678 /* assign data to record structure */
679 record.tnf = _net_nfc_get_tnf_type(atoi(name_format));
681 if (strcmp(type_name, "Null")) {
682 DEBUG_MSG("Data : type_name ");
684 record.typeName.length = strlen(type_name);
685 _nfc_emul_util_alloc_mem(record.typeName.buffer, record.typeName.length);
687 if (record.typeName.buffer == NULL) {
688 DEBUG_MSG("_nfc_emul_util_alloc_mem failed");
691 memcpy(record.typeName.buffer, type_name, record.typeName.length);
694 if (strcmp(record_id, "Null")) {
695 DEBUG_MSG("Data : record_id ");
697 record.id.length = strlen(record_id);
698 _nfc_emul_util_alloc_mem(record.id.buffer, record.id.length);
700 if (record.id.buffer == NULL) {
701 DEBUG_MSG("_nfc_emul_util_alloc_mem failed");
704 memcpy(record.id.buffer, record_id, record.id.length);
707 if (strcmp(record_payload, "Null")) {
708 DEBUG_MSG("Data : record_payload ");
710 record.payload.length = strlen(record_payload);
711 _nfc_emul_util_alloc_mem(record.payload.buffer, record.payload.length);
713 if (record.payload.buffer == NULL) {
714 DEBUG_MSG("_nfc_emul_util_alloc_mem failed");
717 memcpy(record.payload.buffer, record_payload, record.payload.length);
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 >>>>>>>>>>>>>>>>>>>>>>>>");
730 ndef_record_h new_record = NULL;
731 net_nfc_error_e result = NET_NFC_OK;
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);
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);
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 ");
749 data_s payload_data = { NULL, 0 };
751 if (record.payload.buffer != NULL )
753 payload_data.length = strlen((char *)record_payload) + 1;
755 _nfc_emul_util_alloc_mem(payload_data.buffer, payload_data.length);
756 if (payload_data.buffer == NULL)
758 DEBUG_MSG("_nfc_emul_util_alloc_mem failed");
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);
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");
771 if (payload_data.buffer != NULL )
772 _nfc_emul_util_free_mem(payload_data.buffer);
774 else if (!strncmp((char *)record.typeName.buffer, "T", 1)) {
775 DEBUG_MSG("TEXT Type ");
777 data_s payload_data = { NULL, 0 };
781 if (record.payload.buffer != NULL )
783 payload_data.length = strlen((char *)record_payload) + strlen("en-US") + 1;
785 _nfc_emul_util_alloc_mem(payload_data.buffer, payload_data.length);
786 if (payload_data.buffer == NULL)
788 DEBUG_MSG("_nfc_emul_util_alloc_mem failed");
792 controll_byte = strlen("en-US") & 0x3F;
794 payload_data.buffer[0] = controll_byte;
797 memcpy(payload_data.buffer + offset, "en-US", strlen("en-US"));
799 offset = offset + strlen("en-US");
800 memcpy(payload_data.buffer + offset, record.payload.buffer, strlen(record_payload));
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");
809 if (payload_data.buffer != NULL )
810 _nfc_emul_util_free_mem(payload_data.buffer);
813 DEBUG_ERR_MSG("NET_NFC_RECORD_WELL_KNOWN_TYPE >> typeName is wrong");
817 else if ((record.tnf == NET_NFC_RECORD_MIME_TYPE)) {
821 /* open file : size limit? 10k? */
822 file = fopen(record_payload, "r");
824 long int file_len = 0, read_count = 0, read_total = 0;
825 uint8_t *file_data = NULL;
827 fseek(file, 0, SEEK_END);
828 file_len = ftell(file);
829 fseek(file, 0, SEEK_SET);
831 _nfc_emul_util_alloc_mem(file_data, file_len);
833 if (file_data == NULL)
835 DEBUG_MSG("_nfc_emul_util_alloc_mem failed");
843 read_count = fread(file_data + read_total, 1, file_len - read_total, file);
844 read_total += read_count;
846 while (read_count != 0 && read_total < file_len);
850 DEBUG_MSG("fread(%s) success, size %ld", record.payload.buffer, file_len);
852 filePayload.length= file_len;
853 _nfc_emul_util_alloc_mem(filePayload.buffer, filePayload.length);
855 if (filePayload.buffer == NULL) {
856 DEBUG_MSG("_nfc_emul_util_alloc_mem failed");
857 _nfc_emul_util_free_mem(file_data);
860 memcpy(filePayload.buffer, file_data, filePayload.length);
862 _nfc_emul_util_free_mem(file_data);
865 if (record.id.buffer == NULL) {
866 char *file_name = NULL;
868 file_name = strrchr(record_payload, '/');
869 if (file_name == NULL) {
870 file_name = (char *) record_payload;
876 record.id.length = strlen(file_name);
877 _nfc_emul_util_alloc_mem(record.id.buffer, record.id.length);
879 if (record.id.buffer == NULL) {
880 DEBUG_MSG("_nfc_emul_util_alloc_mem failed");
881 _nfc_emul_util_free_mem(filePayload.buffer);
884 memcpy(record.id.buffer, file_name, record.id.length);
888 DEBUG_MSG("file open error");
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);
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);
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);
912 create_record_count++;
913 DEBUG_MSG("Create Record Sucess. Create Record Count[%d]", create_record_count);
915 /* To Do : memory issue */
918 if (record.typeName.buffer != NULL) {
919 _nfc_emul_util_free_mem(record.typeName.buffer);
921 if (record.id.buffer != NULL) {
922 _nfc_emul_util_free_mem(record.id.buffer);
924 if (record.payload.buffer != NULL) {
925 _nfc_emul_util_free_mem(record.payload.buffer);
927 if(filePayload.buffer != NULL) {
928 _nfc_emul_util_free_mem(filePayload.buffer);
931 DEBUG_MSG("Create Record Loop End");
937 return create_record_count;
940 static bool _net_nfc_change_file_owner_permission(FILE *file)
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;
952 /* change permission */
953 fchmod(fileno(file), 0777);
957 buffer_len = sysconf(_SC_GETPW_R_SIZE_MAX);
958 if (buffer_len == -1)
963 _net_nfc_util_alloc_mem(buffer, buffer_len);
967 getpwnam_r("inhouse", &pwd, buffer, buffer_len, &pw_inhouse);
968 _net_nfc_util_free_mem(buffer);
971 buffer_len = sysconf(_SC_GETGR_R_SIZE_MAX);
972 if (buffer_len == -1)
977 _net_nfc_util_alloc_mem(buffer, buffer_len);
981 getgrnam_r("inhouse", &grp, buffer, buffer_len, &gr_inhouse);
982 _net_nfc_util_free_mem(buffer);
984 if ((pw_inhouse != NULL) && (gr_inhouse != NULL))
986 if (fchown(fileno(file), pw_inhouse->pw_uid, gr_inhouse->gr_gid) < 0)
988 DEBUG_MSG("failed to change owner");
995 static void _net_nfc_store_ndef_message(data_s *data)
997 char file_name[1024] = { 0, };
1006 snprintf(file_name, sizeof(file_name), "%s/%s/%s", "/opt/usr/share/nfc-manager-daemon",
1007 "message", "emul-ndef-message.txt");
1011 if ((fp = fopen(file_name, "w")) != NULL)
1015 if ((length = fwrite(data->buffer, 1, data->length, fp)) > 0)
1017 DEBUG_MSG("[%d] bytes is written", length);
1019 _net_nfc_change_file_owner_permission(fp);
1025 DEBUG_MSG("write is failed = [%d]", data->length);
1032 static net_nfc_error_e _net_nfc_retrieve_ndef_message(data_s *data)
1034 char file_name[1024] = { 0, };
1036 net_nfc_error_e result = NET_NFC_OK;
1040 return NET_NFC_NULL_PARAMETER;
1044 snprintf(file_name, sizeof(file_name), "%s/%s/%s", "/opt/usr/share/nfc-manager-daemon",
1045 "message", "emul-ndef-message.txt");
1047 if ((fp = fopen(file_name, "r")) != NULL)
1051 /* rewind to start of file */
1052 fseek(fp, 0, SEEK_END);
1054 fseek(fp, 0, SEEK_SET);
1056 DEBUG_MSG("message length = [%ld]", size);
1060 if (net_nfc_util_init_data(data, size) == true)
1067 current = fread(data->buffer + offset, 1,
1068 data->length - offset, fp);
1073 } while (offset < data->length);
1075 if (offset == data->length) {
1076 result = NET_NFC_OK;
1078 result = NET_NFC_NO_NDEF_MESSAGE;
1083 result = NET_NFC_ALLOC_FAIL;
1088 result = NET_NFC_OPERATION_FAIL;
1095 result = NET_NFC_NO_DATA_FOUND;
1101 static bool _net_nfc_create_ndef_from_emulMsg(emulMsg_s *emul_msg)
1105 if (emul_msg->file_data == NULL) {
1110 net_nfc_error_e result = NET_NFC_OK;
1111 data_s rawdata = { NULL, 0 };
1113 int record_count = emul_msg->record_count;
1115 if(record_count == 0) {
1119 /* create ndef msg */
1120 ndef_message_h ndef_message = NULL;
1121 int ndef_length = 0;
1123 if((result = net_nfc_util_create_ndef_message ((ndef_message_s **) &ndef_message)) != NET_NFC_OK)
1125 DEBUG_MSG("failed to create ndef message [%d]", result);
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);
1131 /* convert ndef msg to raw data */
1132 ndef_length = net_nfc_util_get_ndef_message_length ((ndef_message_s *) ndef_message);
1135 DEBUG_MSG("ndef_message size is zero!");
1138 rawdata.length = ndef_length;
1139 _nfc_emul_util_alloc_mem(rawdata.buffer, ndef_length);
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);
1146 _net_nfc_store_ndef_message(&rawdata);
1153 static bool _net_nfc_create_emulMsg(emulMsg_s **emul_msg, uint8_t * data, long int size)
1160 *emul_msg = (emulMsg_s *)calloc(1, sizeof(emulMsg_s));
1161 if(*emul_msg == NULL)
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));
1170 DEBUG_MSG("emul_msg->message_id >>>>[%d]", (*emul_msg)->message_id);
1172 if (_net_nfc_is_data_emulMsgData((*emul_msg)->message_id)) {
1174 emulMsgData = strtok(NULL, NET_NFC_EMUL_MSG_DATA_SEPERATOR);
1175 DEBUG_MSG("emulMsgData >>>>[%s]", emulMsgData);
1177 switch ((*emul_msg)->message_id) {
1178 case EMUL_NFC_TAG_DISCOVERED :
1179 case EMUL_NFC_P2P_SEND :
1181 /* get message : Tag Type, Record Count, Records */
1182 int target_type = -1;
1183 char file_data[BUFFER_LENGTH_MAX]={ 0, };
1186 sscanf(emulMsgData, NET_NFC_EMUL_TAG_DISCOVERED_DATA_FORMAT, &target_type, &((*emul_msg)->record_count), file_data);
1188 (*emul_msg)->target_type = (emul_target_type) target_type;
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);
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 >>>>>>>>>>>>>>>>>>>>>>>>");
1201 if ( !_net_nfc_create_ndef_from_emulMsg((*emul_msg))) {
1202 DEBUG_ERR_MSG("read ndef_msg is failed >>>");
1205 DEBUG_ERR_MSG("_net_nfc_create_ndef_from_emulMsg end");
1210 /* exception case */
1211 DEBUG_ERR_MSG("_net_nfc_set_emulMsg error. Data is currupted");
1220 switch ((*emul_msg)->message_id) {
1221 case EMUL_NFC_P2P_DISCOVERED :{
1222 (*emul_msg)->target_type = EMUL_NFC_TARGET;
1226 case EMUL_NFC_TAG_DETACHED :{
1227 DEBUG_MSG("TAG DETACHED");
1228 if(!_net_nfc_emul_get_is_tag_attached())
1230 DEBUG_ERR_MSG("tag is not attached!!");
1236 case EMUL_NFC_P2P_DETACHED :{
1237 DEBUG_MSG("P2P DETACHED");
1238 if(!_net_nfc_emul_get_is_p2p_attached())
1240 DEBUG_ERR_MSG("tag is not attached!!");
1248 /* exception case */
1249 DEBUG_ERR_MSG("_net_nfc_set_emulMsg error. Data is currupted");
1261 static void _net_nfc_destroy_emulMsg(emulMsg_s *emul_msg)
1265 if(emul_msg != NULL && emul_msg->file_data != NULL)
1267 free(emul_msg->file_data);
1274 static int _net_nfc_emul_convert_target_type(emul_target_type targetType)
1280 switch (targetType) {
1281 case EMUL_TAG_TOPAZ_JEWEL :
1282 covert = NET_NFC_JEWEL_PICC;
1285 case EMUL_TAG_MIFARE_UL :
1286 covert = NET_NFC_MIFARE_ULTRA_PICC;
1289 case EMUL_TAG_FELICA :
1290 covert = NET_NFC_FELICA_PICC;
1293 case EMUL_TAG_MIFARE_DESFIRE :
1294 covert = NET_NFC_MIFARE_DESFIRE_PICC;
1297 case EMUL_NFC_TARGET :
1298 covert = NET_NFC_NFCIP1_TARGET;
1301 case EMUL_TARGET_TYPE_MAX :
1303 /* exception case */
1304 DEBUG_ERR_MSG("_net_nfc_emul_convert_target_type error. Target type is unknown");
1309 DEBUG_MSG("covert [%d]", covert);
1316 static void _net_nfc_target_discovered_cb(emulMsg_s *emul_msg)
1321 net_nfc_target_handle_s* handle = NULL;
1324 __net_nfc_make_valid_target_handle (&handle);
1325 if(handle == NULL) {
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 };
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)
1346 target_detected->length = length;
1347 target_detected->request_type = NET_NFC_MESSAGE_SERVICE_STANDALONE_TARGET_DETECTED;
1348 target_detected->handle = handle;
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);
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);
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);
1369 DEBUG_MSG("set tag connection");
1370 handle->connection_type = NET_NFC_TAG_CONNECTION;
1371 _net_nfc_emul_set_is_tag_attached(true);
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);
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);
1386 static void _net_nfc_tag_detached_cb(void)
1390 _net_nfc_emul_set_is_tag_attached(false);
1395 static void _net_nfc_target_detached_cb(void)
1399 _net_nfc_emul_set_is_p2p_attached(false);
1401 /* For P2P, we send msg to manager */
1402 net_nfc_request_llcp_msg_t *req_msg = NULL;
1404 _nfc_emul_util_alloc_mem(req_msg, sizeof(net_nfc_request_llcp_msg_t));
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;
1410 DEBUG_MSG("deactivated callback is called");
1411 g_emul_controller_llcp_cb(req_msg, NULL);
1415 static void _net_nfc_llcp_data_receive_cb(void* pContext)
1419 net_nfc_request_llcp_msg_t *req_msg = NULL;
1421 _nfc_emul_util_alloc_mem(req_msg, sizeof(net_nfc_request_llcp_msg_t));
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;
1428 DEBUG_MSG("receive callback is called >>");
1429 g_emul_controller_llcp_cb(req_msg, pContext);
1435 static void _net_nfc_llcp_data_receive_from_cb(void* pContext)
1439 net_nfc_request_llcp_msg_t *req_msg = NULL;
1441 _nfc_emul_util_alloc_mem(req_msg, sizeof(net_nfc_request_llcp_msg_t));
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;
1448 DEBUG_MSG("receive_from callback is called >>");
1449 g_emul_controller_llcp_cb(req_msg, pContext);
1455 static void _net_nfc_llcp_create_snep_server_msg(snep_command_field_e resp_field)
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 };
1465 version = SNEP_MAJOR_VER;
1466 version = (((version << 4) & 0xf0) | (SNEP_MINOR_VER & 0x0f));
1468 _net_nfc_retrieve_ndef_message(&rawdata);
1470 /* version response length payload*/
1471 Snep_Server_msg->data->length = sizeof(uint8_t) + sizeof(uint8_t) + sizeof(uint32_t) + rawdata.length;
1481 length_field = htonl(rawdata.length);
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);
1487 /* copy ndef information to response msg */
1488 if(rawdata.length > 0)
1489 memcpy(temp, rawdata.buffer, rawdata.length);
1494 static llcp_state_e _net_nfc_get_llcp_state(void* pContext)
1499 state = NET_NFC_STATE_EXCHANGER_SERVER;
1506 static bool _net_nfc_make_llcp_data(emulMsg_s *emul_msg)
1509 data_s rawdata = { NULL, 0 };
1511 _net_nfc_retrieve_ndef_message(&rawdata);
1513 if (emul_msg->record_count == 0 || rawdata.length == 0)
1515 DEBUG_ERR_MSG("data is zero >>>");
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);
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 >>>");
1526 if (llcp_server_data == NULL)
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);
1533 DEBUG_MSG("send first segment >>>");
1535 if (llcp_server_data == NULL) {
1539 llcp_server_data->length = SNEP_MAX_BUFFER;
1540 memcpy(llcp_server_data->buffer, Snep_Server_msg->data->buffer, SNEP_MAX_BUFFER);
1542 Snep_Server_msg->isSegment = true;
1543 Snep_Server_msg->offset = SNEP_MAX_BUFFER;
1551 static void _net_nfc_send_emulMsg_to_nfc_manager(emulMsg_s *emul_msg)
1555 switch (emul_msg->message_id) {
1556 case EMUL_NFC_TAG_DISCOVERED :{
1557 _net_nfc_target_discovered_cb(emul_msg);
1561 case EMUL_NFC_TAG_DETACHED :{
1562 _net_nfc_tag_detached_cb();
1566 case EMUL_NFC_P2P_DISCOVERED : {
1567 _net_nfc_initialize_llcp();
1568 _net_nfc_target_discovered_cb(emul_msg);
1572 case EMUL_NFC_P2P_SEND : {
1573 if(!_net_nfc_emul_get_is_p2p_attached())
1575 DEBUG_ERR_MSG("target is not attached!!");
1579 if(_net_nfc_make_llcp_data(emul_msg)) {
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");
1588 _net_nfc_llcp_data_receive_cb(socket_info->user_context); /* call callback */
1591 DEBUG_ERR_MSG("make_p2p_data is fail!!");
1597 case EMUL_NFC_P2P_DETACHED : {
1598 if(!_net_nfc_emul_get_is_p2p_attached())
1600 DEBUG_ERR_MSG("target is not attached!!");
1604 _net_nfc_target_detached_cb();
1605 _net_nfc_deinitialize_llcp();
1610 DEBUG_ERR_MSG("message_id is wrong!!");
1617 static void _net_nfc_process_emulMsg(uint8_t * data, long int size)
1621 emulMsg_s *emul_msg = NULL;
1623 /* create emul_msg */
1624 _net_nfc_create_emulMsg(&emul_msg, data, size);
1626 DEBUG_MSG("emul_msg->message_id >>>>[%d]", emul_msg->message_id);
1628 /* processing emul_msg */
1629 _net_nfc_send_emulMsg_to_nfc_manager(emul_msg);
1631 /* destroy emul_msg */
1632 _net_nfc_destroy_emulMsg(emul_msg);
1637 #ifdef __USE_EPOLL_FOR_FILE__
1638 static void emul_ReaderThread(void * pArg)
1640 DEBUG_MSG("emul_ReaderThread START >>>>");
1642 char file_name[1024] = { 0, };
1643 bool condition = true;
1645 int emulMsg_file_fd = -1;
1646 int emulMsg_poll_fd = -1;
1647 struct epoll_event *emulMsg_poll_events = NULL;
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);
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 !!!!");
1661 if((emulMsg_poll_fd = epoll_create1(EPOLL_CLOEXEC)) == -1)
1663 DEBUG_MSG("epoll_create1 is occured");
1666 if((emulMsg_poll_events = (struct epoll_event *)calloc(1, sizeof(struct epoll_event) * EPOLL_SIZE)) == NULL)
1668 DEBUG_MSG("calloc is occured");
1672 struct epoll_event ev;
1674 ev.events = EPOLLIN | EPOLLET | EPOLLHUP | EPOLLERR;
1675 ev.data.fd = emulMsg_file_fd;
1677 /* add file fd to epoll */
1678 epoll_ctl(emulMsg_poll_fd, EPOLL_CTL_ADD, emulMsg_file_fd, &ev);
1680 while (condition == true) {
1682 int num_of_files = 0;
1685 /* to do : I will add mutex in the future */
1688 DEBUG_MSG("epoll wait >>>>");
1690 if(emulMsg_poll_fd == -1 || emulMsg_file_fd == -1)
1692 DEBUG_MSG("client is deinitialized. ");
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");
1702 DEBUG_MSG("no data is changed ");
1707 for(index = 0; index < num_of_files; index++)
1709 if( (emulMsg_poll_events[index].events & (EPOLLHUP)) || (emulMsg_poll_events[index].events & (EPOLLERR)))
1711 DEBUG_MSG("connection is closed");
1715 else if(emulMsg_poll_events[index].events & EPOLLIN)
1717 if(emulMsg_poll_events[index].data.fd == emulMsg_file_fd)
1719 char readbuffer[READ_BUFFER_LENGTH_MAX];
1722 DEBUG_MSG("precess POLLIN ");
1724 memset(readbuffer, 0x00, sizeof(READ_BUFFER_LENGTH_MAX));
1726 readcnt = read(emulMsg_file_fd, readbuffer, READ_BUFFER_LENGTH_MAX);
1728 DEBUG_MSG("message readcnt= [%d] ", readcnt);
1729 DEBUG_MSG("message = [%s] ", readbuffer);
1732 _net_nfc_process_emulMsg(readbuffer + NET_NFC_EMUL_HEADER_LENGTH, readcnt - NET_NFC_EMUL_HEADER_LENGTH);
1736 DEBUG_MSG("not expected socket connection");
1742 if(num_of_files == index)
1744 DEBUG_MSG("unknown event");
1756 close(emulMsg_file_fd);
1758 DEBUG_MSG("emul_ReaderThread END >>>>");
1763 static void emul_ReaderThread(void * pArg)
1765 DEBUG_MSG("emul_ReaderThread START >>>>");
1768 char file_name[1024] = { 0, };
1770 char readBuffer[READ_BUFFER_LENGTH_MAX];
1776 bool condition = true;
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);
1783 DEBUG_MSG("Start Current Time [%ld]", (unsigned long) curTime);
1785 /* polling 500 ms */
1787 while (condition != 0) {
1792 if ((fp = fopen(file_name, "r")) == NULL) {
1793 DEBUG_MSG("file open error");
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) {
1804 DEBUG_MSG("FILE Modified Time [%ld]", (unsigned long) st.st_mtime);
1805 /* update current time */
1807 DEBUG_MSG("Get New Current Time [%ld]", (unsigned long) curTime);
1812 DEBUG_MSG("stat error");
1818 memset(readBuffer, 0x00, READ_BUFFER_LENGTH_MAX);
1820 if (fscanf(fp, "%[^\n]", readBuffer)) {
1821 DEBUG_MSG("get DATA >>>> readBuffer [%s]", readBuffer);
1824 /* process message */
1825 _net_nfc_process_emulMsg((uint8_t *) readBuffer, (long int) strlen(readBuffer));
1830 DEBUG_MSG("LOOP END >>>>");
1834 DEBUG_MSG("emul_ReaderThread END >>>>");
1838 static bool _net_nfc_emul_controller_start_thread (void)
1844 ret = pthread_create(&gEmulThread, NULL, (emul_Nfc_thread_handler_t)emul_ReaderThread, (void*) "emul_read_thread");
1854 static void _net_nfc_emul_controller_stop_thread (void)
1858 pthread_cancel(gEmulThread);
1862 pthread_cond_signal(&cond);
1864 pthread_join(gEmulThread, NULL);
1869 static bool _net_nfc_emul_controller_create_interfaceFile (void)
1871 char file_name[1024] = { 0, };
1874 bool retval = false;
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");
1886 DEBUG_MSG("folder is already created");
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);
1893 if (stat(file_name, &st) == 0) {
1894 DEBUG_MSG("file is already created");
1898 if ((fp = fopen(file_name, "w")) != NULL)
1900 struct passwd *pw_root = NULL;
1901 struct group *gr_root = NULL;
1903 fchmod(fileno(fp), 0755);
1905 pw_root = getpwnam("root");
1906 gr_root = getgrnam("root");
1908 if ((pw_root != NULL) && (gr_root != NULL))
1910 if (fchown(fileno(fp), pw_root->pw_uid, gr_root->gr_gid) < 0)
1912 DEBUG_ERR_MSG("failed to change owner");
1919 DEBUG_ERR_MSG("failed to get privilege");
1926 DEBUG_ERR_MSG("failed to create file");
1934 static bool net_nfc_emul_controller_init (net_nfc_error_e* result)
1938 if (result == NULL) {
1944 DEBUG_MSG("start stack init ");
1946 if (g_stack_init_successful == true)
1948 DEBUG_MSG("Already statck is initialized");
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");
1959 DEBUG_MSG("Stack init finished");
1961 g_stack_init_successful = true;
1962 *result = NET_NFC_OK;
1969 static bool net_nfc_emul_controller_deinit (void)
1974 if (g_stack_init_successful == false)
1976 DEBUG_MSG("Already statck is deinitialized");
1980 _net_nfc_emul_controller_stop_thread();
1982 g_stack_init_successful = false;
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)
1995 if (result == NULL) {
1999 *result = NET_NFC_OK;
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;
2013 static bool net_nfc_emul_controller_unregister_listener()
2017 g_emul_controller_target_cb = NULL;
2018 g_emul_controller_se_cb = NULL;
2019 g_emul_controller_llcp_cb = NULL;
2026 static bool net_nfc_emul_controller_get_firmware_version(data_s **data, net_nfc_error_e *result)
2028 if (data == NULL || result == NULL)
2033 *result = NET_NFC_OK;
2037 *data = (data_s *)calloc(1, sizeof(data_s));
2042 (*data)->length = 10;
2043 (*data)->buffer = (uint8_t *)calloc(1, (*data)->length);
2045 snprintf((char *)(*data)->buffer, (*data)->length, "%d.%d", 1, 0);
2052 static bool net_nfc_emul_controller_check_firmware_version(net_nfc_error_e* result)
2054 if (result == NULL) {
2058 *result = NET_NFC_OK;
2067 static bool net_nfc_emul_controller_update_firmware(net_nfc_error_e* result)
2069 if (result == NULL) {
2073 *result = NET_NFC_OK;
2082 static bool net_nfc_emul_controller_get_stack_information(net_nfc_stack_information_s* stack_info, net_nfc_error_e* result)
2084 if (result == NULL) {
2088 *result = NET_NFC_OK;
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)
2107 *result = NET_NFC_OK;
2109 if (mode == NET_NFC_DISCOVERY_MODE_START)
2111 mode = NET_NFC_DISCOVERY_MODE_CONFIG;
2113 else if (mode == NET_NFC_DISCOVERY_MODE_STOP)
2115 mode = NET_NFC_DISCOVERY_MODE_CONFIG;
2116 config = NET_NFC_ALL_DISABLE;
2121 if ((mode == NET_NFC_DISCOVERY_MODE_CONFIG))
2123 if (config == NET_NFC_ALL_DISABLE)
2125 /* This handle is not useful anymore */
2126 __net_nfc_make_invalid_target_handle ();
2128 /* reset socket_info */
2129 for (idx = 0; idx < LLCP_NB_SOCKET_MAX; idx++)
2131 _net_nfc_remove_socket_slot((net_nfc_llcp_socket_t) idx);
2134 DEBUG_MSG("Kill Thread");
2136 ret = net_nfc_emul_controller_deinit();
2138 else if(config == NET_NFC_ALL_ENABLE)
2140 net_nfc_error_e err;
2142 DEBUG_MSG("Create Thread");
2143 ret = net_nfc_emul_controller_init(&err);
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)
2154 if (result == NULL) {
2158 *result = NET_NFC_OK;
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)
2169 if (result == NULL) {
2173 *result = NET_NFC_OK;
2182 static bool net_nfc_emul_controller_check_target_presence(net_nfc_target_handle_s* handle, net_nfc_error_e* result)
2184 if (result == NULL) {
2188 *result = NET_NFC_OK;
2192 if (_net_nfc_emul_get_is_tag_attached()) {
2196 DEBUG_MSG("TAG Detached");
2201 static bool net_nfc_emul_controller_connect(net_nfc_target_handle_s* handle, net_nfc_error_e* result)
2203 if (result == NULL) {
2207 *result = NET_NFC_OK;
2209 if (!__net_nfc_is_valide_target_handle(handle)) {
2210 *result = NET_NFC_INVALID_HANDLE;
2221 static bool net_nfc_emul_controller_disconnect(net_nfc_target_handle_s* handle, net_nfc_error_e* result)
2223 if (result == NULL) {
2227 *result = NET_NFC_OK;
2231 if (!__net_nfc_is_valide_target_handle(handle)) {
2232 *result = NET_NFC_INVALID_HANDLE;
2236 // This handle is not useful anymore
2237 __net_nfc_make_invalid_target_handle ();
2239 /* reset socket_info */
2241 for (; idx < LLCP_NB_SOCKET_MAX; idx++)
2243 _net_nfc_remove_socket_slot((net_nfc_llcp_socket_t) idx);
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)
2253 data_s rawdata = { NULL, 0 };
2255 if (result == NULL) {
2259 *result = NET_NFC_OK;
2261 if (handle == NULL || ndef_card_state == NULL || max_data_size == NULL || real_data_size == NULL || result == NULL)
2263 *result = NET_NFC_NULL_PARAMETER;
2267 if (!__net_nfc_is_valide_target_handle(handle)) {
2268 *result = NET_NFC_INVALID_HANDLE;
2274 _net_nfc_retrieve_ndef_message(&rawdata);
2276 if (_net_nfc_emul_get_is_tag_attached())
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);
2285 DEBUG_MSG("target detached");
2293 static bool net_nfc_emul_controller_make_read_only_ndef(net_nfc_target_handle_s* handle, net_nfc_error_e* result)
2295 if (result == NULL) {
2299 *result = NET_NFC_OK;
2301 if (!__net_nfc_is_valide_target_handle(handle)) {
2302 *result = NET_NFC_INVALID_HANDLE;
2313 static bool net_nfc_emul_controller_read_ndef(net_nfc_target_handle_s* handle, data_s** data, net_nfc_error_e* result)
2315 int real_data_size = 0;
2316 data_s rawdata = { NULL, 0 };
2320 if (result == NULL) {
2324 *result = NET_NFC_OK;
2326 if(handle == NULL || data == NULL || result == NULL)
2328 DEBUG_ERR_MSG("NET_NFC_NULL_PARAMETER >>>");
2329 *result = NET_NFC_NULL_PARAMETER;
2333 if (!__net_nfc_is_valide_target_handle(handle)) {
2334 DEBUG_ERR_MSG("NET_NFC_INVALID_HANDLE >>>");
2335 *result = NET_NFC_INVALID_HANDLE;
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;
2345 *result = _net_nfc_retrieve_ndef_message(&rawdata);
2346 if(*result != NET_NFC_OK)
2349 real_data_size = rawdata.length;
2351 if(real_data_size == 0)
2353 DEBUG_ERR_MSG("read ndef_msg is failed >>> real_data_size is zero");
2354 *result = NET_NFC_NO_NDEF_MESSAGE;
2358 *data = (data_s*) calloc(1, sizeof(data_s));
2362 *result = NET_NFC_ALLOC_FAIL;
2366 (*data)->length = real_data_size;
2367 (*data)->buffer = (uint8_t *)calloc(1, real_data_size);
2369 if((*data)->buffer == NULL)
2372 *result = NET_NFC_ALLOC_FAIL;
2376 /* copy rawdata to data->buffer */
2377 memcpy((*data)->buffer, rawdata.buffer, real_data_size);
2384 static bool net_nfc_emul_controller_write_ndef(net_nfc_target_handle_s* handle, data_s* data, net_nfc_error_e* result)
2386 if (result == NULL) {
2390 *result = NET_NFC_OK;
2392 if (!__net_nfc_is_valide_target_handle(handle)) {
2393 *result = NET_NFC_INVALID_HANDLE;
2399 DEBUG_MSG("net_nfc_emul_controller_write_ndef success >>>");
2401 _net_nfc_store_ndef_message(data);
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)
2414 if (result == NULL) {
2418 if (info == NULL || info->trans_data.buffer == NULL ||
2419 info->trans_data.length == 0) {
2420 *result = NET_NFC_INVALID_PARAM;
2424 *result = NET_NFC_OK;
2427 if (!__net_nfc_is_valide_target_handle(handle)) {
2428 *result = NET_NFC_INVALID_HANDLE;
2434 if (info->dev_type == NET_NFC_MIFARE_DESFIRE_PICC) {
2435 DEBUG_MSG("NET_NFC_MIFARE_DESFIRE_PICC");
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) {
2443 _net_nfc_util_alloc_mem(temp, sizeof(data_s));
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;
2455 *result = NET_NFC_ALLOC_FAIL;
2456 _net_nfc_util_free_mem(temp);
2459 *result = NET_NFC_ALLOC_FAIL;
2462 *result = NET_NFC_NOT_SUPPORTED;
2466 *result = NET_NFC_NOT_SUPPORTED;
2474 static bool net_nfc_emul_controller_format_ndef(net_nfc_target_handle_s* handle, data_s* secure_key, net_nfc_error_e* result)
2476 if (result == NULL) {
2480 *result = NET_NFC_OK;
2482 if (!__net_nfc_is_valide_target_handle(handle)) {
2483 *result = NET_NFC_INVALID_HANDLE;
2494 static bool net_nfc_emul_controller_exception_handler(void)
2498 net_nfc_error_e error;
2500 if(net_nfc_emul_controller_init(&error) == false)
2502 DEBUG_ERR_MSG("exception handler is failed!!");
2511 static bool net_nfc_emul_controller_is_ready(net_nfc_error_e* result)
2513 if (result == NULL) {
2517 *result = NET_NFC_OK;
2523 return g_stack_init_successful;
2527 /*******************
2529 ********************/
2531 static bool net_nfc_emul_controller_llcp_config (net_nfc_llcp_config_info_s * config, net_nfc_error_e * result)
2533 if (result == NULL) {
2537 *result = NET_NFC_OK;
2546 static bool net_nfc_emul_controller_llcp_check_llcp(net_nfc_target_handle_s* handle, net_nfc_error_e* result)
2548 if (result == NULL) {
2552 *result = NET_NFC_OK;
2554 if (!__net_nfc_is_valide_target_handle(handle)) {
2555 *result = NET_NFC_INVALID_HANDLE;
2566 static bool net_nfc_emul_controller_llcp_activate_llcp(net_nfc_target_handle_s* handle, net_nfc_error_e* result)
2568 if (result == NULL) {
2572 *result = NET_NFC_OK;
2574 if (!__net_nfc_is_valide_target_handle(handle)) {
2575 *result = NET_NFC_INVALID_HANDLE;
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)
2588 if (result == NULL) {
2592 *result = NET_NFC_OK;
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!!");
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!!");
2611 if (llcp_state == NET_NFC_STATE_EXCHANGER_SERVER) {
2612 DEBUG_MSG("NET_NFC_STATE_EXCHANGER_SERVER");
2614 *socket = NET_NFC_EMUL_SNEP_SERVER_SOCKET_NUMBER;
2616 else if (llcp_state == NET_NFC_STATE_EXCHANGER_SERVER_NPP) {
2617 DEBUG_MSG("NET_NFC_STATE_EXCHANGER_SERVER_NPP");
2619 *socket = NET_NFC_EMUL_NPP_SERVER_SOCKET_NUMBER;
2621 else if (llcp_state == NET_NFC_STATE_EXCHANGER_CLIENT) {
2622 DEBUG_MSG("NET_NFC_STATE_EXCHANGER_CLIENT");
2624 *socket = NET_NFC_EMUL_CLIENT_SOCKET_NUMBER;
2626 else if (llcp_state == NET_NFC_STATE_CONN_HANDOVER_REQUEST) {
2627 DEBUG_MSG("NET_NFC_STATE_CONN_HANDOVER_REQUEST");
2629 *socket = NET_NFC_EMUL_HANDOVER_REQUEST_SOCKET_NUMBER;
2631 else if (llcp_state == NET_NFC_STATE_CONN_HANDOVER_SELECT) {
2632 DEBUG_MSG("NET_NFC_STATE_CONN_HANDOVER_SELECT");
2634 *socket = NET_NFC_EMUL_HANDOVER_SELECT_SOCKET_NUMBER;
2637 DEBUG_MSG("we doesn't support..");
2642 DEBUG_MSG("socket is created = [0x%x]", *socket);
2644 socket_info->socket_handle = *socket;
2645 socket_info->user_context = (void *) user_param;
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)
2654 if (result == NULL) {
2658 *result = NET_NFC_OK;
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)
2669 if (result == NULL) {
2673 *result = NET_NFC_OK;
2675 if (!__net_nfc_is_valide_target_handle(handle)) {
2676 *result = NET_NFC_INVALID_HANDLE;
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;
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");
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");
2699 if (llcp_state == NET_NFC_STATE_EXCHANGER_SERVER) {
2700 _nfc_emul_util_alloc_mem(detail, sizeof(*detail));
2704 detail->length = sizeof(*detail);
2705 detail->request_type = NET_NFC_MESSAGE_SERVICE_LLCP_LISTEN;
2707 socket_info->user_context = user_param;
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;
2714 DEBUG_MSG("accept callback is called");
2715 g_emul_controller_llcp_cb(detail, socket_info->user_context);
2719 DEBUG_MSG("llcp_state is [%d]", llcp_state);
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)
2730 if (result == NULL) {
2734 *result = NET_NFC_OK;
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)
2745 if (result == NULL) {
2749 *result = NET_NFC_OK;
2751 if (!__net_nfc_is_valide_target_handle(handle)) {
2752 *result = NET_NFC_INVALID_HANDLE;
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");
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");
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)
2778 if (result == NULL) {
2782 *result = NET_NFC_OK;
2784 if (!__net_nfc_is_valide_target_handle(handle)) {
2785 *result = NET_NFC_INVALID_HANDLE;
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");
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");
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)
2811 if (result == NULL) {
2815 *result = NET_NFC_OK;
2817 if (!__net_nfc_is_valide_target_handle(handle)) {
2818 *result = NET_NFC_INVALID_HANDLE;
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");
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");
2837 if(llcp_state == NET_NFC_STATE_EXCHANGER_SERVER) {
2838 net_nfc_request_llcp_msg_t *req_msg = NULL;
2840 _nfc_emul_util_alloc_mem(req_msg, sizeof(net_nfc_request_llcp_msg_t));
2842 socket_info->user_context = user_param;
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;
2849 DEBUG_MSG("send callback is called");
2850 g_emul_controller_llcp_cb(req_msg, socket_info->user_context);
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)
2861 if (result == NULL) {
2865 *result = NET_NFC_OK;
2867 if (!__net_nfc_is_valide_target_handle(handle)) {
2868 *result = NET_NFC_INVALID_HANDLE;
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");
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");
2887 if(llcp_state == NET_NFC_STATE_EXCHANGER_SERVER) {
2888 net_nfc_request_llcp_msg_t *req_msg = NULL;
2890 _nfc_emul_util_alloc_mem(req_msg, sizeof(net_nfc_request_llcp_msg_t));
2892 socket_info->user_context = user_param;
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;
2899 DEBUG_MSG("send_to callback is called");
2900 g_emul_controller_llcp_cb(req_msg, socket_info->user_context);
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)
2912 if (result == NULL || data == NULL) {
2916 *result = NET_NFC_OK;
2918 if (!__net_nfc_is_valide_target_handle(handle)) {
2919 *result = NET_NFC_INVALID_HANDLE;
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");
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");
2938 if (llcp_state == NET_NFC_STATE_EXCHANGER_SERVER) {
2940 DEBUG_MSG("NET_NFC_STATE_EXCHANGER_SERVER");
2941 socket_info->user_context = user_param;
2943 if(Snep_Server_msg->isSegment) {
2944 /* send snep msg continueosly ..*/
2945 DEBUG_MSG("send segments for snep msg");
2947 int remained_size = 0;
2949 remained_size = Snep_Server_msg->data->length - Snep_Server_msg->offset;
2950 DEBUG_MSG("remained_size[%d]", remained_size);
2952 /* copy rawdata to llcp_server_data->buffer */
2953 if (remained_size <= SNEP_MAX_BUFFER) {
2954 DEBUG_MSG("send last segment >>>");
2956 llcp_server_data->length = remained_size;
2957 memcpy(llcp_server_data->buffer, Snep_Server_msg->data->buffer+Snep_Server_msg->offset , remained_size);
2959 Snep_Server_msg->isSegment = false;
2962 DEBUG_MSG("send continue segment >>>");
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);
2967 Snep_Server_msg->offset += SNEP_MAX_BUFFER;
2970 _net_nfc_llcp_data_receive_cb(socket_info->user_context); /* call callback */
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 */
2976 DEBUG_MSG("data address is set");
2977 llcp_server_data = data;
2980 DEBUG_ERR_MSG("data address is NULL");
2986 DEBUG_MSG("we don't support..");
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)
2996 if (result == NULL) {
3000 *result = NET_NFC_OK;
3002 if (!__net_nfc_is_valide_target_handle(handle)) {
3003 *result = NET_NFC_INVALID_HANDLE;
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");
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");
3022 if (llcp_state == NET_NFC_STATE_EXCHANGER_SERVER) {
3024 DEBUG_MSG("NET_NFC_STATE_EXCHANGER_SERVER");
3025 socket_info->user_context = user_param;
3027 if(Snep_Server_msg->isSegment) {
3028 /* send snep msg continueosly ..*/
3029 DEBUG_MSG("send segments for snep msg");
3031 int remained_size = 0;
3033 remained_size = Snep_Server_msg->data->length - Snep_Server_msg->offset;
3034 DEBUG_MSG("remained_size[%d]", remained_size);
3036 /* copy rawdata to llcp_server_data->buffer */
3037 if (remained_size <= SNEP_MAX_BUFFER) {
3038 DEBUG_MSG("send last segment >>>");
3040 llcp_server_data->length = remained_size;
3041 memcpy(llcp_server_data->buffer, Snep_Server_msg->data->buffer+Snep_Server_msg->offset , remained_size);
3043 Snep_Server_msg->isSegment = false;
3046 DEBUG_MSG("send continue segment >>>");
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);
3051 Snep_Server_msg->offset += SNEP_MAX_BUFFER;
3054 _net_nfc_llcp_data_receive_from_cb(socket_info->user_context); /* call callback */
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 */
3060 DEBUG_MSG("data address is set");
3061 llcp_server_data = data;
3064 DEBUG_ERR_MSG("data address is NULL");
3070 DEBUG_MSG("we donen't support..");
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)
3080 if (result == NULL) {
3084 *result = NET_NFC_OK;
3086 if (!__net_nfc_is_valide_target_handle(handle)) {
3087 *result = NET_NFC_INVALID_HANDLE;
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");
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");
3106 /* send msg to framework */
3107 net_nfc_request_llcp_msg_t *req_msg = NULL;
3109 _nfc_emul_util_alloc_mem(req_msg, sizeof(net_nfc_request_llcp_msg_t));
3111 socket_info->user_context = user_param;
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;
3118 DEBUG_MSG("disconnect callback is called");
3119 g_emul_controller_llcp_cb(req_msg, user_param);
3122 /* reset socket_info */
3123 _net_nfc_remove_socket_slot((net_nfc_llcp_socket_t) socket);
3130 static bool net_nfc_emul_controller_llcp_socket_close(net_nfc_llcp_socket_t socket, net_nfc_error_e* result)
3132 if (result == NULL) {
3136 *result = NET_NFC_OK;
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)
3147 if (result == NULL) {
3151 *result = NET_NFC_OK;
3153 if (!__net_nfc_is_valide_target_handle(handle)) {
3154 *result = NET_NFC_INVALID_HANDLE;
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)
3167 if (result == NULL) {
3171 *result = NET_NFC_OK;
3173 if (!__net_nfc_is_valide_target_handle(handle)) {
3174 *result = NET_NFC_INVALID_HANDLE;
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)
3187 /* In llcp specification ver 1.1, default miu size is 128 */
3188 const uint16_t default_miu = 128;
3190 if (result == NULL) {
3194 *result = NET_NFC_OK;
3196 if (!__net_nfc_is_valide_target_handle(handle)) {
3197 *result = NET_NFC_INVALID_HANDLE;
3203 option->miu = default_miu;
3211 static bool net_nfc_emul_controller_support_nfc(net_nfc_error_e *result)
3214 struct stat st = { 0, };
3216 if (stat("/dev/nfc0", &st) == -1)
3219 *result = NET_NFC_NOT_SUPPORTED;
3224 *result = NET_NFC_OK;
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)
3232 DEBUG_ERR_MSG("se open start");
3233 *result = NET_NFC_OK;
3234 __net_nfc_make_valid_target_handle(handle);
3236 DEBUG_ERR_MSG("se_open end");
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)
3244 int length = sizeof(buffer);
3247 DEBUG_ERR_MSG("se get_atr start");
3248 *result = NET_NFC_OK;
3251 strncpy(buffer, "getatr", sizeof(buffer));
3253 temp = (data_s *)calloc(1, sizeof(*temp));
3255 temp->buffer = (uint8_t *)calloc(1, length);
3256 if (temp->buffer != NULL) {
3257 memcpy(temp->buffer, buffer, length);
3258 temp->length = length;
3260 *result = NET_NFC_OK;
3263 *result = NET_NFC_ALLOC_FAIL;
3267 *result = NET_NFC_ALLOC_FAIL;
3270 DEBUG_ERR_MSG("se get atr end");
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)
3281 DEBUG_ERR_MSG("se send apdu start");
3283 *result = NET_NFC_OK;
3287 strncpy(buffer, "response", sizeof(buffer));
3289 length = strlen(buffer);
3291 temp = (data_s *)calloc(1, sizeof(*temp));
3293 temp->buffer = (uint8_t *)calloc(1, length);
3294 if (temp->buffer != NULL) {
3295 memcpy(temp->buffer, buffer, length);
3296 temp->length = length;
3299 *result = NET_NFC_OK;
3302 *result = NET_NFC_ALLOC_FAIL;
3306 *result = NET_NFC_ALLOC_FAIL;
3310 DEBUG_ERR_MSG("se send apdu end");
3313 static bool net_nfc_emul_controller_secure_element_close(net_nfc_target_handle_s *handle,
3314 net_nfc_error_e *result)
3316 DEBUG_ERR_MSG("se close start");
3317 *result = NET_NFC_OK;
3319 DEBUG_ERR_MSG("se close end");
3322 ////////////// INTERFACE END //////////