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.
26 #include <sys/epoll.h>
29 #include <arpa/inet.h>
30 #include <hal-nfc-interface.h>
31 #include "nfc_debug_private.h"
34 #define EXPORT __attribute__ ((visibility("default")))
37 #define NET_NFC_NDEF_RECORD_MASK_MB 0x80 /**< This marks the begin of a NDEF Message. */
38 #define NET_NFC_NDEF_RECORD_MASK_ME 0x40 /**< Set if the record is at the Message End. */
39 #define NET_NFC_NDEF_RECORD_MASK_CF 0x20 /**< Chunk Flag: The record is a record chunk only. */
40 #define NET_NFC_NDEF_RECORD_MASK_SR 0x10 /**< Short Record: Payload Length is encoded in ONE byte only. */
41 #define NET_NFC_NDEF_RECORD_MASK_IL 0x08 /**< The ID Length Field is present. */
42 #define NET_NFC_NDEF_RECORD_MASK_TNF 0x07 /**< Type Name Format. */
45 EMUL_NFC_TAG_DISCOVERED = 100,
46 EMUL_NFC_TAG_DETACHED,
47 EMUL_NFC_P2P_DISCOVERED,
48 EMUL_NFC_P2P_DETACHED,
54 EMUL_TAG_TOPAZ_JEWEL = 1,
57 EMUL_TAG_MIFARE_DESFIRE,
63 EMUL_NDEF_TNF_EMPTY = 0,
64 EMUL_NDEF_TNF_WELL_KNOWN,
65 EMUL_NDEF_TNF_MIME_MEDIA,
66 EMUL_NDEF_TNF_ABSOLUTE_URI,
67 EMUL_NDEF_TNF_EXTERNAL,
68 EMUL_NDEF_TNF_UNKNOWN,
69 EMUL_NDEF_TNF_UNCHANGED
70 } emul_ndef_name_format;
73 NET_NFC_STATE_EXCHANGER_SERVER = 0x00,
74 NET_NFC_STATE_EXCHANGER_SERVER_NPP,
76 NET_NFC_STATE_EXCHANGER_CLIENT,
77 NET_NFC_STATE_CONN_HANDOVER_REQUEST,
78 NET_NFC_STATE_CONN_HANDOVER_SELECT,
83 SNEP_REQ_CONTINUE = 0x00,
86 SNEP_REQ_REJECT = 0x7F,
87 SNEP_RESP_CONT = 0x80,
88 SNEP_RESP_SUCCESS = 0x81,
89 SNEP_RESP_NOT_FOUND = 0xC0,
90 SNEP_RESP_EXCESS_DATA = 0xC1,
91 SNEP_RESP_BAD_REQ = 0xC2,
92 SNEP_RESP_NOT_IMPLEMENT = 0xE0,
93 SNEP_RESP_UNSUPPORTED_VER = 0xE1,
94 SNEP_RESP_REJECT = 0xFF,
95 } snep_command_field_e;
97 typedef struct _socket_info_s {
98 net_nfc_llcp_socket_t socket_handle;
103 typedef struct _net_nfc_oem_llcp_state_t {
106 unsigned int fragment_offset;
108 net_nfc_llcp_socket_t socket;
109 uint16_t max_capability;
110 net_nfc_target_handle_s *handle;
111 net_nfc_error_e prev_result;
112 net_nfc_llcp_socket_t incomming_socket;
113 ndef_message_s *requester;
114 ndef_message_s *selector;
119 llcp_app_protocol_e type_app_protocol;
120 net_nfc_conn_handover_carrier_type_e type;
122 } net_nfc_oem_llcp_state_t;
124 typedef struct _snep_msg_s {
129 /* Members below are used for snep msg of client */
134 typedef struct _emulMsg_data_s {
135 net_nfc_record_tnf_e tnf;
143 typedef struct _emulMsg_s {
144 emul_message_id message_id;
145 emul_target_type target_type;
150 typedef void* (*emul_Nfc_thread_handler_t)(void *pParam);
152 #define __USE_EPOLL_FOR_FILE__ 1
154 /* for emulator management */
155 #define NET_NFC_EMUL_DATA_PATH "/dev"
156 #define NET_NFC_EMUL_MESSAGE_FILE_NAME "nfc0"
157 #define NET_NFC_EMUL_MSG_ID_SEPERATOR "\n:"
158 #define NET_NFC_EMUL_MSG_DATA_SEPERATOR "\n\0"
159 #define NET_NFC_EMUL_MSG_RECORD_SEPERATOR "\n,"
160 #define NET_NFC_EMUL_TAG_DISCOVERED_DATA_FORMAT "%d,%d,%[^\n]"
161 #define NET_NFC_EMUL_HEADER_LENGTH 8
163 #ifdef __USE_EPOLL_FOR_FILE__
164 #define EPOLL_SIZE 128
167 /* for llcp functionality */
168 #define LLCP_NB_SOCKET_MAX 5
169 #define NET_NFC_EMUL_SNEP_SERVER_SOCKET_NUMBER 0
170 #define NET_NFC_EMUL_INCOMING_SOCKET_NUMBER 0
171 #define NET_NFC_EMUL_NPP_SERVER_SOCKET_NUMBER 1
172 #define NET_NFC_EMUL_CLIENT_SOCKET_NUMBER 2
173 #define NET_NFC_EMUL_HANDOVER_REQUEST_SOCKET_NUMBER 3
174 #define NET_NFC_EMUL_HANDOVER_SELECT_SOCKET_NUMBER 4
176 #define SNEP_MAX_BUFFER 128
177 #define SNEP_MAJOR_VER 1
178 #define SNEP_MINOR_VER 0
180 /* static variable */
181 #define BUFFER_LENGTH_MAX 4096
182 #define READ_BUFFER_LENGTH_MAX BUFFER_LENGTH_MAX
183 #define WRITE_BUFFER_LENGTH_MAX BUFFER_LENGTH_MAX
184 #define NET_NFC_MAX_LLCP_SOCKET_BUFFER BUFFER_LENGTH_MAX
186 /* listener callback */
187 static target_detection_listener_cb g_emul_controller_target_cb;
188 static se_transaction_listener_cb g_emul_controller_se_cb;
189 static llcp_event_listener_cb g_emul_controller_llcp_cb;
190 static hce_apdu_listener_cb g_emul_controller_hce_cb;
192 /* for emulator management */
193 static pthread_t gEmulThread;
195 /* for stack management */
196 static net_nfc_target_handle_s *current_working_handle = NULL;
197 static bool g_stack_init_successful = 0;
198 static bool g_tag_attached = 0;
199 static bool g_p2p_attached = 0;
200 static int current_connection_id = 1;
201 /* for llcp functionality */
203 static socket_info_s socket_info_array[LLCP_NB_SOCKET_MAX] = {{0}};
205 static snep_msg_s *Snep_Server_msg;
206 static data_s *llcp_server_data = NULL;
208 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
212 /* free memory, after free given memory it set NULL. Before proceed free, this function also check NULL */
213 static void __nfc_emul_util_free_mem(void **mem, char *filename, unsigned int line);
214 /* allocation memory */
215 static void __nfc_emul_util_alloc_mem(void **mem, int size, char *filename, unsigned int line);
216 #define _nfc_emul_util_alloc_mem(mem, size) __nfc_emul_util_alloc_mem((void**)&mem, size, __FILE__, __LINE__)
217 #define _nfc_emul_util_free_mem(mem) __nfc_emul_util_free_mem((void**)&mem, __FILE__, __LINE__)
220 static void __nfc_emul_util_free_mem(void **mem, char *filename, unsigned int line)
222 if (mem == NULL || *mem == NULL) {
223 LOGE("FILE: %s, LINE:%d, Invalid parameter in mem free util (pinter is NULL)", filename, line);
230 static void __nfc_emul_util_alloc_mem(void **mem, int size, char *filename, unsigned int line)
232 if (mem == NULL || size <= 0) {
233 LOGE("FILE: %s, LINE:%d, Invalid parameter in mem alloc util", filename, line);
237 DEBUG_MSG("size to malloc() = [%d]", size);
240 LOGE("FILE: %s, LINE:%d, WARNING: Pointer is already allocated or it was not initialized with NULL", filename, line);
242 *mem = calloc(1, size);
245 LOGE("FILE: %s, LINE:%d, Allocation is failed", filename, line);
248 static bool _net_nfc_util_init_data(data_s * data, uint32_t length)
250 if (data == NULL || length == 0)
253 _nfc_emul_util_alloc_mem(data->buffer, length);
254 if (data->buffer == NULL)
257 data->length = length;
262 static void _net_nfc_util_clear_data(data_s * data)
267 if (data->buffer != NULL) {
268 _nfc_emul_util_free_mem(data->buffer);
275 static net_nfc_error_e _net_nfc_util_free_record(ndef_record_s * record)
278 return NET_NFC_NULL_PARAMETER;
280 _net_nfc_util_clear_data(&record->type_s);
281 _net_nfc_util_clear_data(&record->id_s);
282 _net_nfc_util_clear_data(&record->payload_s);
284 _nfc_emul_util_free_mem(record);
289 static net_nfc_error_e _net_nfc_util_free_ndef_message(ndef_message_s * msg)
292 ndef_record_s *prev, *current;
295 return NET_NFC_NULL_PARAMETER;
297 current = msg->records;
299 for (idx = 0; idx < msg->recordCount; idx++) {
304 current = current->next;
306 _net_nfc_util_free_record(prev);
309 _nfc_emul_util_free_mem(msg);
314 static net_nfc_error_e _net_nfc_util_create_record(net_nfc_record_tnf_e recordType, data_s * typeName, data_s * id, data_s * payload, ndef_record_s ** record)
316 ndef_record_s *record_temp = NULL;
319 return NET_NFC_NULL_PARAMETER;
321 if (recordType < NET_NFC_RECORD_EMPTY || recordType > NET_NFC_RECORD_UNCHAGNED)
322 return NET_NFC_OUT_OF_BOUND;
325 if (recordType == NET_NFC_RECORD_EMPTY) {
326 if ((typeName != NULL && typeName->length > 0) || (payload != NULL && payload->length > 0) || (id != NULL && id->length > 0))
327 return NET_NFC_INVALID_PARAM;
329 if (typeName == NULL || typeName->buffer == NULL || typeName->length == 0)
330 return NET_NFC_INVALID_PARAM;
333 _nfc_emul_util_alloc_mem(record_temp, sizeof(ndef_record_s));
334 if (record_temp == NULL)
335 return NET_NFC_ALLOC_FAIL;
336 // set type name and length and TNF field
337 record_temp->TNF = recordType;
339 if (typeName != NULL && typeName->length > 0) {
340 if (_net_nfc_util_init_data(&record_temp->type_s, typeName->length) == false) {
341 _nfc_emul_util_free_mem(record_temp);
343 return NET_NFC_ALLOC_FAIL;
346 memcpy(record_temp->type_s.buffer, typeName->buffer, record_temp->type_s.length);
348 record_temp->type_s.buffer = NULL;
349 record_temp->type_s.length = 0;
355 if (payload != NULL && payload->length > 0) {
356 if (_net_nfc_util_init_data(&record_temp->payload_s, payload->length) == false) {
357 _net_nfc_util_clear_data(&record_temp->type_s);
358 _nfc_emul_util_free_mem(record_temp);
360 return NET_NFC_ALLOC_FAIL;
363 memcpy(record_temp->payload_s.buffer, payload->buffer, record_temp->payload_s.length);
365 if (payload->length > 255)
368 record_temp->payload_s.buffer = NULL;
369 record_temp->payload_s.length = 0;
372 // set id and id length and IL field
373 if (id != NULL && id->buffer != NULL && id->length > 0) {
374 if (_net_nfc_util_init_data(&record_temp->id_s, id->length) == false) {
375 _net_nfc_util_clear_data(&record_temp->payload_s);
376 _net_nfc_util_clear_data(&record_temp->type_s);
377 _nfc_emul_util_free_mem(record_temp);
379 return NET_NFC_ALLOC_FAIL;
382 memcpy(record_temp->id_s.buffer, id->buffer, record_temp->id_s.length);
386 record_temp->id_s.buffer = NULL;
387 record_temp->id_s.length = 0;
390 // this is default value
394 record_temp->next = NULL;
396 *record = record_temp;
401 static net_nfc_error_e _net_nfc_util_append_record(ndef_message_s * msg, ndef_record_s * record)
403 if (msg == NULL || record == NULL)
404 return NET_NFC_NULL_PARAMETER;
406 if (msg->recordCount == 0) {
407 // set short message and append
412 msg->records = record;
416 DEBUG_MSG("record is added to NDEF message :: count [%d]", msg->recordCount);
418 ndef_record_s *current = NULL;
419 ndef_record_s *prev = NULL;
421 // set flag :: this record is FIRST
422 current = msg->records;
424 if (current != NULL) {
432 current = current->next;
434 while (current != NULL) {
438 current = current->next;
441 // set flag :: this record is END
453 static uint32_t _net_nfc_util_get_record_length(ndef_record_s * Record)
455 uint32_t RecordLength = 1;
460 /* Type length is present only for following TNF
461 NET_NFC_TNF_NFCWELLKNOWN
462 NET_NFC_TNF_MEDIATYPE
463 SLP_FRINET_NFC_NDEFRECORD_TNF_ABSURI
464 SLP_FRINET_NFC_NDEFRECORD_TNF_NFCEXT
467 /* ++ is for the Type Length Byte */
469 if (Record->TNF != EMUL_NDEF_TNF_EMPTY && Record->TNF != EMUL_NDEF_TNF_UNKNOWN && Record->TNF != EMUL_NDEF_TNF_UNCHANGED)
470 RecordLength += Record->type_s.length;
472 /* to check if payloadlength is 8bit or 32bit */
473 if (Record->SR != 0) {
474 /* ++ is for the Payload Length Byte */
475 RecordLength++; /* for short record */
477 /* + NET_NFC_NDEF_NORMAL_RECORD_BYTE is for the Payload Length Byte */
481 /* for non empty record */
482 if (Record->TNF != EMUL_NDEF_TNF_EMPTY)
483 RecordLength += Record->payload_s.length;
485 /* ID and IDlength are present only if IL flag is set */
486 if (Record->IL != 0) {
487 RecordLength += Record->id_s.length;
488 /* ++ is for the ID Length Byte */
495 static uint32_t _net_nfc_util_get_ndef_message_length(ndef_message_s * message)
497 ndef_record_s *current;
503 current = message->records;
505 while (current != NULL) {
506 total += _net_nfc_util_get_record_length(current);
507 current = current->next;
513 static net_nfc_error_e _net_nfc_util_create_ndef_message(ndef_message_s ** ndef_message)
515 if (ndef_message == NULL)
516 return NET_NFC_NULL_PARAMETER;
518 _nfc_emul_util_alloc_mem(*ndef_message, sizeof(ndef_message_s));
519 if (*ndef_message == NULL)
520 return NET_NFC_ALLOC_FAIL;
525 static net_nfc_error_e _net_nfc_util_convert_ndef_message_to_rawdata(ndef_message_s * ndef, data_s * rawdata)
527 ndef_record_s *record = NULL;
528 uint8_t *current = NULL;
531 if (rawdata == NULL || ndef == NULL)
532 return NET_NFC_NULL_PARAMETER;
534 record = ndef->records;
535 current = rawdata->buffer;
541 ndef_header |= NET_NFC_NDEF_RECORD_MASK_MB;
543 ndef_header |= NET_NFC_NDEF_RECORD_MASK_ME;
545 ndef_header |= NET_NFC_NDEF_RECORD_MASK_CF;
547 ndef_header |= NET_NFC_NDEF_RECORD_MASK_SR;
549 ndef_header |= NET_NFC_NDEF_RECORD_MASK_IL;
551 ndef_header |= record->TNF;
553 *current++ = ndef_header;
555 /* check empty record */
556 if (record->TNF == EMUL_NDEF_TNF_EMPTY) {
557 /* set type length to zero */
560 /* set payload length to zero */
563 /* set ID length to zero */
567 record = record->next;
573 /* set type length */
574 if (record->TNF == EMUL_NDEF_TNF_UNKNOWN || record->TNF == EMUL_NDEF_TNF_UNCHANGED)
577 *current++ = record->type_s.length;
579 /* set payload length */
581 *current++ = (uint8_t) (record->payload_s.length & 0x000000FF);
583 *current++ = (uint8_t) ((record->payload_s.length & 0xFF000000) >> 24);
584 *current++ = (uint8_t) ((record->payload_s.length & 0x00FF0000) >> 16);
585 *current++ = (uint8_t) ((record->payload_s.length & 0x0000FF00) >> 8);
586 *current++ = (uint8_t) (record->payload_s.length & 0x000000FF);
591 *current++ = record->id_s.length;
593 /* set type buffer */
594 if ((record->TNF != EMUL_NDEF_TNF_UNKNOWN) && (record->TNF != EMUL_NDEF_TNF_UNCHANGED)) {
595 memcpy(current, record->type_s.buffer, record->type_s.length);
596 current += record->type_s.length;
600 memcpy(current, record->id_s.buffer, record->id_s.length);
601 current += record->id_s.length;
603 /* set payload buffer */
604 memcpy(current, record->payload_s.buffer, record->payload_s.length);
605 current += record->payload_s.length;
607 record = record->next;
613 static bool __net_nfc_is_valide_target_handle(net_nfc_target_handle_s *handle)
615 bool result = (current_working_handle == handle);
617 DEBUG_MSG("[WARNING]: INVALID HANDLE IS DETECTED!");
622 static void __net_nfc_make_valid_target_handle(net_nfc_target_handle_s **handle)
624 if (current_working_handle != NULL)
625 DEBUG_MSG("[WARNING]: HANDLE WAS ALLOCATED ALREADY!");
627 _nfc_emul_util_alloc_mem(*handle, sizeof(net_nfc_target_handle_s));
629 current_working_handle = *handle;
632 static void __net_nfc_make_invalid_target_handle()
634 if (current_working_handle != NULL) {
635 _nfc_emul_util_free_mem(current_working_handle);
636 current_working_handle = NULL;
640 static socket_info_s* _net_nfc_get_available_socket_slot()
643 for (; idx < LLCP_NB_SOCKET_MAX; idx++) {
644 if (socket_info_array[idx].isValid == false) {
645 memset(&(socket_info_array[idx]), 0x00, sizeof(socket_info_s));
646 socket_info_array[idx].isValid = true;
647 return &(socket_info_array[idx]);
651 DEBUG_ERR_MSG("_net_nfc_get_available_socket_slot is failed");
655 static void _net_nfc_remove_socket_slot(net_nfc_llcp_socket_t socket)
659 for (; idx < LLCP_NB_SOCKET_MAX; idx++) {
660 if (socket_info_array[idx].isValid == true &&
661 socket_info_array[idx].socket_handle == socket) {
662 socket_info_array[idx].isValid = false;
663 socket_info_array[idx].socket_handle = 0;
664 socket_info_array[idx].user_context = NULL;
669 static socket_info_s* _net_nfc_find_server_socket(net_nfc_llcp_socket_t socket)
672 for (; idx < LLCP_NB_SOCKET_MAX; idx++) {
673 if (socket_info_array[idx].socket_handle == socket && socket_info_array[idx].isValid == true)
674 return &(socket_info_array[idx]);
677 DEBUG_ERR_MSG("_net_nfc_find_server_socket is failed");
681 static void _net_nfc_initialize_llcp(void)
685 if (Snep_Server_msg == NULL) {
686 Snep_Server_msg = calloc(1, sizeof(snep_msg_s));
687 if (Snep_Server_msg == NULL)
690 Snep_Server_msg->data = calloc(1, sizeof(data_s));
691 if (Snep_Server_msg->data == NULL)
694 Snep_Server_msg->data->buffer = calloc(1, sizeof(uint8_t) * BUFFER_LENGTH_MAX);
700 static void _net_nfc_deinitialize_llcp(void)
704 if (Snep_Server_msg != NULL) {
705 if (Snep_Server_msg->data != NULL) {
706 if (Snep_Server_msg->data->buffer != NULL) {
707 free(Snep_Server_msg->data->buffer);
708 Snep_Server_msg->data->buffer = NULL;
711 free(Snep_Server_msg->data);
712 Snep_Server_msg->data = NULL;
715 free(Snep_Server_msg);
716 Snep_Server_msg = NULL;
719 llcp_server_data = NULL;
724 static bool _net_nfc_emul_get_is_tag_attached(void)
726 return g_tag_attached;
729 static void _net_nfc_emul_set_is_tag_attached(bool is_detached)
731 g_tag_attached = is_detached;
732 DEBUG_MSG("set g_tag_attached [%d]", g_tag_attached);
735 static bool _net_nfc_emul_get_is_p2p_attached(void)
737 return g_p2p_attached;
740 static void _net_nfc_emul_set_is_p2p_attached(bool is_detached)
742 g_p2p_attached = is_detached;
743 DEBUG_MSG("set g_p2p_attached [%d]", g_p2p_attached);
746 static bool _net_nfc_is_data_emulMsgData(emul_message_id messageId)
753 case EMUL_NFC_TAG_DISCOVERED:
757 case EMUL_NFC_P2P_SEND:
761 case EMUL_NFC_TAG_DETACHED:
762 case EMUL_NFC_P2P_DISCOVERED:
763 case EMUL_NFC_P2P_DETACHED:
769 DEBUG_MSG("retval [%d]", retval);
776 static net_nfc_record_tnf_e _net_nfc_get_tnf_type(int name_format)
780 net_nfc_record_tnf_e tnf = NET_NFC_RECORD_EMPTY;
782 switch (name_format) {
783 case EMUL_NDEF_TNF_EMPTY:
784 tnf = NET_NFC_RECORD_EMPTY;
787 case EMUL_NDEF_TNF_WELL_KNOWN:
788 tnf = NET_NFC_RECORD_WELL_KNOWN_TYPE;
791 case EMUL_NDEF_TNF_MIME_MEDIA:
792 tnf = NET_NFC_RECORD_MIME_TYPE;
795 case EMUL_NDEF_TNF_ABSOLUTE_URI:
796 tnf = NET_NFC_RECORD_URI;
799 case EMUL_NDEF_TNF_EXTERNAL:
800 tnf = NET_NFC_RECORD_EXTERNAL_RTD;
803 case EMUL_NDEF_TNF_UNKNOWN:
804 tnf = NET_NFC_RECORD_UNKNOWN;
808 tnf = NET_NFC_RECORD_UNKNOWN;
809 DEBUG_MSG("data is currupted");
813 DEBUG_MSG("tnf [%d]", tnf);
821 static inline int _create_records_well_known(emulMsg_data_s *record, char *record_payload, ndef_record_h *new_record)
823 if (!strncmp((char *)record->typeName.buffer, "U", 1)) {
824 DEBUG_MSG("URI Type ");
826 data_s payload_data = {NULL, 0};
828 if (record->payload.buffer != NULL) {
829 payload_data.length = strlen(record_payload) + 1;
831 _nfc_emul_util_alloc_mem(payload_data.buffer, payload_data.length);
832 if (payload_data.buffer == NULL) {
833 DEBUG_MSG("_nfc_emul_util_alloc_mem failed");
837 payload_data.buffer[0] = NET_NFC_SCHEMA_FULL_URI; /* first byte of payload is protocol scheme */
838 memcpy(payload_data.buffer + 1, record->payload.buffer, payload_data.length - 1);
841 if (_net_nfc_util_create_record(record->tnf, &record->typeName, &record->id, &payload_data, (ndef_record_s**)new_record) != NET_NFC_OK) {
842 DEBUG_ERR_MSG("net_nfc_util_create_record is failed");
846 if (payload_data.buffer != NULL)
847 _nfc_emul_util_free_mem(payload_data.buffer);
848 } else if (!strncmp((char *)record->typeName.buffer, "T", 1)) {
849 DEBUG_MSG("TEXT Type ");
851 data_s payload_data = {NULL, 0};
855 if (record->payload.buffer != NULL) {
856 payload_data.length = strlen((char *)record_payload) + strlen("en-US") + 1;
858 _nfc_emul_util_alloc_mem(payload_data.buffer, payload_data.length);
859 if (payload_data.buffer == NULL) {
860 DEBUG_MSG("_nfc_emul_util_alloc_mem failed");
864 controll_byte = strlen("en-US") & 0x3F;
866 payload_data.buffer[0] = controll_byte;
869 memcpy(payload_data.buffer + offset, "en-US", strlen("en-US"));
871 offset = offset + strlen("en-US");
872 memcpy(payload_data.buffer + offset, record->payload.buffer, strlen(record_payload));
875 if (_net_nfc_util_create_record(record->tnf, &record->typeName, &record->id, &payload_data, (ndef_record_s**)new_record) != NET_NFC_OK) {
876 DEBUG_ERR_MSG("net_nfc_util_create_record is failed");
880 if (payload_data.buffer != NULL)
881 _nfc_emul_util_free_mem(payload_data.buffer);
883 DEBUG_ERR_MSG("NET_NFC_RECORD_WELL_KNOWN_TYPE >> typeName is wrong");
889 static inline int _create_records_mime(emulMsg_data_s *record, char *record_payload, ndef_record_h *new_record)
892 data_s filePayload = {NULL, 0};
894 /* open file : size limit? 10k? */
895 file = fopen(record_payload, "r");
897 long int file_len = 0, read_count = 0, read_total = 0;
898 uint8_t *file_data = NULL;
900 fseek(file, 0, SEEK_END);
901 file_len = ftell(file);
902 fseek(file, 0, SEEK_SET);
904 _nfc_emul_util_alloc_mem(file_data, file_len);
906 if (file_data == NULL) {
907 DEBUG_MSG("_nfc_emul_util_alloc_mem failed");
914 read_count = fread(file_data + read_total, 1, file_len - read_total, file);
915 read_total += read_count;
916 } while (read_count != 0 && read_total < file_len);
920 DEBUG_MSG("fread(%s) success, size %ld", record->payload.buffer, file_len);
922 filePayload.length = file_len;
923 _nfc_emul_util_alloc_mem(filePayload.buffer, filePayload.length);
925 if (filePayload.buffer == NULL) {
926 DEBUG_MSG("_nfc_emul_util_alloc_mem failed");
927 _nfc_emul_util_free_mem(file_data);
930 memcpy(filePayload.buffer, file_data, filePayload.length);
932 _nfc_emul_util_free_mem(file_data);
935 if (record->id.buffer == NULL) {
936 char *file_name = NULL;
938 file_name = strrchr(record_payload, '/');
939 if (file_name == NULL)
940 file_name = (char *)record_payload;
944 record->id.length = strlen(file_name);
945 _nfc_emul_util_alloc_mem(record->id.buffer, record->id.length + 1);
947 if (record->id.buffer == NULL) {
948 DEBUG_MSG("_nfc_emul_util_alloc_mem failed");
949 _nfc_emul_util_free_mem(filePayload.buffer);
952 memcpy(record->id.buffer, file_name, sizeof(record->id.buffer) - 1);
955 DEBUG_MSG("file open error");
960 int ret = _net_nfc_util_create_record(record->tnf, &record->typeName, &record->id,
961 &filePayload, (ndef_record_s **)new_record);
962 _nfc_emul_util_free_mem(filePayload.buffer);
963 if (ret != NET_NFC_OK) {
964 DEBUG_MSG("net_nfc_create_record failed[%d]", ret);
971 static void _create_records_free(emulMsg_data_s *record)
973 if (record->typeName.buffer)
974 _nfc_emul_util_free_mem(record->typeName.buffer);
976 if (record->id.buffer)
977 _nfc_emul_util_free_mem(record->id.buffer);
979 if (record->payload.buffer)
980 _nfc_emul_util_free_mem(record->payload.buffer);
983 static int _net_nfc_create_records_from_emulMsg(emulMsg_s *emul_msg, ndef_message_s **ndef_message, int record_count)
988 int create_record_count = 0;
989 char emulMsg[BUFFER_LENGTH_MAX] = {0};
991 memcpy(emulMsg, emul_msg->file_data, sizeof(emulMsg) - 1);
993 /* parsing data and create record to record structure */
994 for (index = 0; index < record_count; index++) {
998 char *record_payload;
1000 emulMsg_data_s record = {0};
1004 name_format = strtok((char *)emulMsg, NET_NFC_EMUL_MSG_RECORD_SEPERATOR);
1006 name_format = strtok(NULL, NET_NFC_EMUL_MSG_RECORD_SEPERATOR);
1008 type_name = strtok(NULL, NET_NFC_EMUL_MSG_RECORD_SEPERATOR);
1009 record_id = strtok(NULL, NET_NFC_EMUL_MSG_RECORD_SEPERATOR);
1010 if (index == record_count - 1) {
1011 /* the last payload : we have to read sentence fully */
1012 record_payload = strtok(NULL, "\n");
1014 record_payload = strtok(NULL, NET_NFC_EMUL_MSG_RECORD_SEPERATOR);
1017 /* assign data to record structure */
1018 record.tnf = _net_nfc_get_tnf_type(atoi(name_format));
1020 DEBUG_MSG("Data : type_name ");
1022 record.typeName.length = strlen(type_name);
1023 _nfc_emul_util_alloc_mem(record.typeName.buffer, record.typeName.length + 1);
1025 if (record.typeName.buffer == NULL) {
1026 DEBUG_MSG("_nfc_emul_util_alloc_mem failed");
1027 return create_record_count;
1029 memcpy(record.typeName.buffer, type_name, sizeof(record.typeName.buffer) - 1);
1031 DEBUG_MSG("Data : record_id ");
1033 record.id.length = strlen(record_id);
1034 _nfc_emul_util_alloc_mem(record.id.buffer, record.id.length + 1);
1036 if (record.id.buffer == NULL) {
1037 DEBUG_MSG("_nfc_emul_util_alloc_mem failed");
1038 _nfc_emul_util_free_mem(record.typeName.buffer);
1039 return create_record_count;
1041 memcpy(record.id.buffer, record_id, sizeof(record.id.buffer) - 1);
1043 DEBUG_MSG("Data : record_payload ");
1045 record.payload.length = strlen(record_payload);
1046 _nfc_emul_util_alloc_mem(record.payload.buffer, record.payload.length + 1);
1048 if (record.payload.buffer == NULL) {
1049 DEBUG_MSG("_nfc_emul_util_alloc_mem failed");
1050 _nfc_emul_util_free_mem(record.typeName.buffer);
1051 _nfc_emul_util_free_mem(record.id.buffer);
1052 return create_record_count;
1054 memcpy(record.payload.buffer, record_payload, sizeof(record.payload.buffer) - 1);
1056 #ifndef __EMUL_DEBUG__
1057 DEBUG_ERR_MSG("RECORD DATA START >>>>>>>>>>>>>>>>>>>>>>>>");
1058 DEBUG_MSG("TNF >>>>[%d]", record.tnf);
1059 LOGE("type_name >>>>[%s]", type_name);
1060 LOGE("record_id >>>>[%s]", record_id);
1061 DEBUG_MSG("record_payload >>>>[%s]", record_payload);
1062 DEBUG_ERR_MSG("RECORD DATA END >>>>>>>>>>>>>>>>>>>>>>>>");
1066 ndef_record_h new_record = NULL;
1067 net_nfc_error_e result = NET_NFC_OK;
1069 switch (record.tnf) {
1070 case NET_NFC_RECORD_EMPTY:
1071 result = _net_nfc_util_create_record(NET_NFC_RECORD_EMPTY, &record.typeName,
1072 &record.id, &record.payload, (ndef_record_s **)&new_record);
1073 if (NET_NFC_OK != result) {
1074 DEBUG_MSG("net_nfc_create_record failed[%d]", result);
1075 _create_records_free(&record);
1076 return create_record_count;
1079 case NET_NFC_RECORD_UNKNOWN:
1080 result = _net_nfc_util_create_record(NET_NFC_RECORD_UNKNOWN, &record.typeName,
1081 &record.id, &record.payload, (ndef_record_s **)&new_record);
1082 if (NET_NFC_OK != result) {
1083 DEBUG_MSG("net_nfc_create_record failed[%d]", result);
1084 _create_records_free(&record);
1085 return create_record_count;
1088 case NET_NFC_RECORD_WELL_KNOWN_TYPE:
1089 if (_create_records_well_known(&record, record_payload, &new_record) < 0) {
1090 _create_records_free(&record);
1091 return create_record_count;
1094 case NET_NFC_RECORD_MIME_TYPE:
1095 if (_create_records_mime(&record, record_payload, &new_record) < 0) {
1096 _create_records_free(&record);
1097 return create_record_count;
1101 /* NET_NFC_RECORD_URI or NET_NFC_RECORD_EXTERNAL_RTD */
1102 result = _net_nfc_util_create_record(record.tnf, &record.typeName,
1103 &record.id, &record.payload, (ndef_record_s **)&new_record);
1104 if (NET_NFC_OK != result) {
1105 DEBUG_MSG("net_nfc_create_record failed[%d]", result);
1106 _create_records_free(&record);
1107 return create_record_count;
1112 /* append record to ndef msg */
1113 if ((result = _net_nfc_util_append_record((ndef_message_s*)*ndef_message, (ndef_record_s *)new_record)) != NET_NFC_OK) {
1114 DEBUG_MSG("net_nfc_util_append_record failed[%d]", result);
1115 _create_records_free(&record);
1116 return create_record_count;
1119 create_record_count++;
1120 DEBUG_MSG("Create Record Sucess. Create Record Count[%d]", create_record_count);
1122 _create_records_free(&record);
1123 DEBUG_MSG("Create Record Loop End");
1128 return create_record_count;
1131 static bool _net_nfc_change_file_owner_permission(FILE *file)
1133 char *buffer = NULL;
1134 size_t buffer_len = 0;
1135 struct passwd pwd = {0};
1136 struct passwd *pw_inhouse = NULL;
1137 struct group grp = {0};
1138 struct group *gr_inhouse = NULL;
1143 /* change permission */
1144 fchmod(fileno(file), 0644);
1148 buffer_len = sysconf(_SC_GETPW_R_SIZE_MAX);
1149 if (buffer_len == -1)
1152 _nfc_emul_util_alloc_mem(buffer, buffer_len);
1156 getpwnam_r("inhouse", &pwd, buffer, buffer_len, &pw_inhouse);
1157 _nfc_emul_util_free_mem(buffer);
1160 buffer_len = sysconf(_SC_GETGR_R_SIZE_MAX);
1161 if (buffer_len == -1)
1164 _nfc_emul_util_alloc_mem(buffer, buffer_len);
1168 getgrnam_r("inhouse", &grp, buffer, buffer_len, &gr_inhouse);
1169 _nfc_emul_util_free_mem(buffer);
1171 if ((pw_inhouse != NULL) && (gr_inhouse != NULL)) {
1172 if (fchown(fileno(file), pw_inhouse->pw_uid, gr_inhouse->gr_gid) < 0)
1173 DEBUG_MSG("failed to change owner");
1179 static void _net_nfc_store_ndef_message(data_s *data)
1181 char file_name[1024] = {0};
1188 snprintf(file_name, sizeof(file_name), "%s/%s/%s", "/opt/usr/share/nfc-manager-daemon",
1189 "message", "emul-ndef-message.txt");
1193 if ((fp = fopen(file_name, "w")) != NULL) {
1196 if ((length = fwrite(data->buffer, 1, data->length, fp)) > 0) {
1197 DEBUG_MSG("[%d] bytes is written", length);
1199 _net_nfc_change_file_owner_permission(fp);
1203 DEBUG_MSG("write is failed = [%d]", data->length);
1210 static net_nfc_error_e _net_nfc_retrieve_ndef_message(data_s *data)
1212 char file_name[1024] = {0};
1214 net_nfc_error_e result = NET_NFC_OK;
1217 return NET_NFC_NULL_PARAMETER;
1220 snprintf(file_name, sizeof(file_name), "%s/%s/%s", "/opt/usr/share/nfc-manager-daemon",
1221 "message", "emul-ndef-message.txt");
1223 if ((fp = fopen(file_name, "r")) != NULL) {
1226 /* rewind to start of file */
1227 fseek(fp, 0, SEEK_END);
1229 fseek(fp, 0, SEEK_SET);
1231 DEBUG_MSG("message length = [%ld]", size);
1234 if (_net_nfc_util_init_data(data, size) == true) {
1240 current = fread(data->buffer + offset, 1,
1241 data->length - offset, fp);
1246 } while (offset < data->length);
1248 if (offset == data->length)
1249 result = NET_NFC_OK;
1251 result = NET_NFC_NO_NDEF_MESSAGE;
1253 result = NET_NFC_ALLOC_FAIL;
1256 result = NET_NFC_OPERATION_FAIL;
1261 result = NET_NFC_NO_DATA_FOUND;
1267 static bool _net_nfc_create_ndef_from_emulMsg(emulMsg_s *emul_msg)
1271 if (emul_msg->file_data == NULL)
1275 net_nfc_error_e result = NET_NFC_OK;
1276 data_s rawdata = {NULL, 0};
1278 int record_count = emul_msg->record_count;
1280 if (record_count == 0)
1283 /* create ndef msg */
1284 ndef_message_h ndef_message = NULL;
1285 int ndef_length = 0;
1287 if ((result = _net_nfc_util_create_ndef_message((ndef_message_s **)&ndef_message)) != NET_NFC_OK)
1288 DEBUG_MSG("failed to create ndef message [%d]", result);
1290 /* create records and append it to ndef_msg*/
1291 emul_msg->record_count = _net_nfc_create_records_from_emulMsg(emul_msg, (ndef_message_s **)&ndef_message, record_count);
1293 /* convert ndef msg to raw data */
1294 ndef_length = _net_nfc_util_get_ndef_message_length((ndef_message_s *)ndef_message);
1297 DEBUG_MSG("ndef_message size is zero!");
1299 rawdata.length = ndef_length;
1300 _nfc_emul_util_alloc_mem(rawdata.buffer, ndef_length);
1302 if ((result = _net_nfc_util_convert_ndef_message_to_rawdata((ndef_message_s*)ndef_message, &rawdata)) != NET_NFC_OK)
1303 DEBUG_MSG("net_nfc_util_convert_ndef_message_to_rawdata is failed![%d]", result);
1305 _net_nfc_store_ndef_message(&rawdata);
1307 _nfc_emul_util_free_mem(rawdata.buffer);
1308 _net_nfc_util_free_ndef_message(ndef_message);
1315 static bool _net_nfc_create_emulMsg(emulMsg_s **emul_msg, uint8_t *data, long int size)
1322 *emul_msg = calloc(1, sizeof(emulMsg_s));
1323 if (*emul_msg == NULL)
1326 /* emulData => ID : MSG ex) 100:1,1,1,U,samsung,http://www.naver.com */
1327 emulMsgID = strtok((char *)data, NET_NFC_EMUL_MSG_ID_SEPERATOR);
1328 if (emulMsgID != NULL)
1329 (*emul_msg)->message_id = (emul_message_id)(atoi(emulMsgID));
1331 DEBUG_MSG("emul_msg->message_id >>>>[%d]", (*emul_msg)->message_id);
1333 if (_net_nfc_is_data_emulMsgData((*emul_msg)->message_id)) {
1335 emulMsgData = strtok(NULL, NET_NFC_EMUL_MSG_DATA_SEPERATOR);
1336 DEBUG_MSG("emulMsgData >>>>[%s]", emulMsgData);
1338 switch ((*emul_msg)->message_id) {
1339 case EMUL_NFC_TAG_DISCOVERED:
1340 case EMUL_NFC_P2P_SEND:
1342 /* get message : Tag Type, Record Count, Records */
1343 int target_type = -1;
1344 char file_data[BUFFER_LENGTH_MAX] = {0};
1347 sscanf(emulMsgData, NET_NFC_EMUL_TAG_DISCOVERED_DATA_FORMAT, &target_type, &((*emul_msg)->record_count), file_data);
1349 (*emul_msg)->target_type = (emul_target_type)target_type;
1351 length = strlen(file_data) + 1;
1352 _nfc_emul_util_alloc_mem((*emul_msg)->file_data, length);
1353 memcpy((*emul_msg)->file_data, file_data, length);
1355 DEBUG_ERR_MSG("EMUL MESSAGE DATA START >>>>>>>>>>>>>>>>>>>>>>>>");
1356 DEBUG_MSG("message_id >>>>[%d]", (*emul_msg)->message_id);
1357 DEBUG_MSG("target_type >>>>[%d]", (*emul_msg)->target_type);
1358 DEBUG_MSG("record_count >>>>[%d]", (*emul_msg)->record_count);
1359 DEBUG_MSG("file_data >>>>[%s]", (char *)(*emul_msg)->file_data);
1360 DEBUG_ERR_MSG("EMUL MESSAGE DATA END >>>>>>>>>>>>>>>>>>>>>>>>");
1362 if (!_net_nfc_create_ndef_from_emulMsg((*emul_msg)))
1363 DEBUG_ERR_MSG("read ndef_msg is failed >>>");
1365 DEBUG_ERR_MSG("_net_nfc_create_ndef_from_emulMsg end");
1370 /* exception case */
1371 DEBUG_ERR_MSG("_net_nfc_set_emulMsg error. Data is currupted");
1376 switch ((*emul_msg)->message_id) {
1377 case EMUL_NFC_P2P_DISCOVERED:
1378 (*emul_msg)->target_type = EMUL_NFC_TARGET;
1381 case EMUL_NFC_TAG_DETACHED:
1382 DEBUG_MSG("TAG DETACHED");
1383 if (!_net_nfc_emul_get_is_tag_attached()) {
1384 DEBUG_ERR_MSG("tag is not attached!!");
1389 case EMUL_NFC_P2P_DETACHED:
1390 DEBUG_MSG("P2P DETACHED");
1391 if (!_net_nfc_emul_get_is_p2p_attached()) {
1392 DEBUG_ERR_MSG("tag is not attached!!");
1398 /* exception case */
1399 DEBUG_ERR_MSG("_net_nfc_set_emulMsg error. Data is currupted");
1410 static void _net_nfc_destroy_emulMsg(emulMsg_s *emul_msg)
1414 if (emul_msg != NULL) {
1415 if (emul_msg->file_data != NULL)
1416 free(emul_msg->file_data);
1424 static int _net_nfc_emul_convert_target_type(emul_target_type targetType)
1430 switch (targetType) {
1431 case EMUL_TAG_TOPAZ_JEWEL:
1432 covert = NET_NFC_JEWEL_PICC;
1435 case EMUL_TAG_MIFARE_UL:
1436 covert = NET_NFC_MIFARE_ULTRA_PICC;
1439 case EMUL_TAG_FELICA:
1440 covert = NET_NFC_FELICA_PICC;
1443 case EMUL_TAG_MIFARE_DESFIRE:
1444 covert = NET_NFC_MIFARE_DESFIRE_PICC;
1447 case EMUL_NFC_TARGET:
1448 covert = NET_NFC_NFCIP1_TARGET;
1451 case EMUL_TARGET_TYPE_MAX:
1453 /* exception case */
1454 DEBUG_ERR_MSG("_net_nfc_emul_convert_target_type error. Target type is unknown");
1458 DEBUG_MSG("covert [%d]", covert);
1465 static void _net_nfc_target_discovered_cb(emulMsg_s *emul_msg)
1470 net_nfc_target_handle_s *handle = NULL;
1473 __net_nfc_make_valid_target_handle(&handle);
1478 net_nfc_request_target_detected_t *target_detected = NULL;
1479 uint8_t device_info[] = {0x03, 0x55, 0x49, 0x44, 0x07, 0x04, 0x93, 0xB7, 0xD9, 0x5B, 0x02, 0x80, \
1480 0x08, 0x41, 0x50, 0x50, 0x5F, 0x44, 0x41, 0x54, 0x41, 0x00, 0x03, 0x53, 0x41, 0x4B, \
1481 0x01, 0x00, 0x04, 0x41, 0x54, 0x51, 0x41, 0x02, 0x44, 0x00, 0x0D, 0x4D, 0x41, 0x58, \
1482 0x5F, 0x44, 0x41, 0x54, 0x41, 0x5F, 0x52, 0x41, 0x54, 0x45, 0x01, 0x00, 0x08, 0x46, \
1483 0x57, 0x49, 0x5F, 0x53, 0x46, 0x47, 0x54, 0x01, 0x03, 0x49, 0x44, 0x6D, 0x07, 0x04, \
1484 0x93, 0xB7, 0xD9, 0x5B, 0x02, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
1487 length = sizeof(net_nfc_request_target_detected_t) + sizeof(device_info);
1488 _nfc_emul_util_alloc_mem(target_detected, length);
1489 if (target_detected == NULL)
1492 target_detected->length = length;
1493 target_detected->request_type = NET_NFC_MESSAGE_SERVICE_STANDALONE_TARGET_DETECTED;
1494 target_detected->handle = handle;
1496 target_detected->devType = _net_nfc_emul_convert_target_type(emul_msg->target_type);
1497 if (!target_detected->devType) {
1498 DEBUG_MSG("target_detected->devType is unknown");
1499 _nfc_emul_util_free_mem(target_detected);
1503 target_detected->handle->connection_id = current_connection_id++;
1505 if (target_detected->devType == NET_NFC_NFCIP1_TARGET) {
1506 DEBUG_MSG("set llcp connection type. remote device is target");
1507 handle->connection_type = NET_NFC_P2P_CONNECTION_TARGET;
1508 _net_nfc_emul_set_is_p2p_attached(true);
1509 } else if (target_detected->devType == NET_NFC_NFCIP1_INITIATOR) {
1510 DEBUG_MSG("set llcp connection type. remote device is initiator");
1511 handle->connection_type = NET_NFC_P2P_CONNECTION_INITIATOR;
1512 _net_nfc_emul_set_is_p2p_attached(true);
1514 DEBUG_MSG("set tag connection");
1515 handle->connection_type = NET_NFC_TAG_CONNECTION;
1516 _net_nfc_emul_set_is_tag_attached(true);
1519 target_detected->number_of_keys = 7;
1520 target_detected->target_info_values.length = sizeof(device_info);
1521 memcpy(&target_detected->target_info_values.buffer, device_info, target_detected->target_info_values.length);
1523 /* call target_cb */
1524 if (g_emul_controller_target_cb != NULL) {
1525 DEBUG_MSG("discovered callback is called");
1526 g_emul_controller_target_cb(target_detected, NULL);
1531 static void _net_nfc_tag_detached_cb(void)
1535 _net_nfc_emul_set_is_tag_attached(false);
1540 static void _net_nfc_target_detached_cb(void)
1544 _net_nfc_emul_set_is_p2p_attached(false);
1546 /* For P2P, we send msg to manager */
1547 net_nfc_request_llcp_msg_t *req_msg = NULL;
1549 _nfc_emul_util_alloc_mem(req_msg, sizeof(net_nfc_request_llcp_msg_t));
1551 if (req_msg != NULL) {
1552 req_msg->length = sizeof(net_nfc_request_llcp_msg_t);
1553 req_msg->request_type = NET_NFC_MESSAGE_SERVICE_LLCP_DEACTIVATED;
1555 DEBUG_MSG("deactivated callback is called");
1556 g_emul_controller_llcp_cb(req_msg, NULL);
1560 static void _net_nfc_llcp_data_receive_cb(void *pContext)
1564 net_nfc_request_llcp_msg_t *req_msg = NULL;
1566 _nfc_emul_util_alloc_mem(req_msg, sizeof(net_nfc_request_llcp_msg_t));
1568 if (req_msg != NULL) {
1569 req_msg->length = sizeof(net_nfc_request_llcp_msg_t);
1570 req_msg->request_type = NET_NFC_MESSAGE_SERVICE_LLCP_RECEIVE;
1571 req_msg->result = NET_NFC_OK;
1573 DEBUG_MSG("receive callback is called >>");
1574 g_emul_controller_llcp_cb(req_msg, pContext);
1580 static void _net_nfc_llcp_data_receive_from_cb(void *pContext)
1584 net_nfc_request_llcp_msg_t *req_msg = NULL;
1586 _nfc_emul_util_alloc_mem(req_msg, sizeof(net_nfc_request_llcp_msg_t));
1588 if (req_msg != NULL) {
1589 req_msg->length = sizeof(net_nfc_request_llcp_msg_t);
1590 req_msg->request_type = NET_NFC_MESSAGE_SERVICE_LLCP_RECEIVE_FROM;
1591 req_msg->result = NET_NFC_OK;
1593 DEBUG_MSG("receive_from callback is called >>");
1594 g_emul_controller_llcp_cb(req_msg, pContext);
1600 static void _net_nfc_llcp_create_snep_server_msg(snep_command_field_e resp_field)
1604 uint8_t *temp = Snep_Server_msg->data->buffer;
1605 uint8_t response = (uint8_t)resp_field;
1606 uint8_t version = 0;
1607 uint32_t length_field = 0;
1608 data_s rawdata = {NULL, 0};
1610 version = SNEP_MAJOR_VER;
1611 version = (((version << 4) & 0xf0) | (SNEP_MINOR_VER & 0x0f));
1613 _net_nfc_retrieve_ndef_message(&rawdata);
1615 /* version response length payload*/
1616 Snep_Server_msg->data->length = sizeof(uint8_t) + sizeof(uint8_t) + sizeof(uint32_t) + rawdata.length;
1626 length_field = htonl(rawdata.length);
1628 /* length will be se 0. so we don't need to copy value */
1629 memcpy(temp, &length_field, sizeof(uint32_t));
1630 temp += sizeof(uint32_t);
1632 /* copy ndef information to response msg */
1633 if (rawdata.length > 0)
1634 memcpy(temp, rawdata.buffer, rawdata.length);
1636 _nfc_emul_util_free_mem(rawdata.buffer);
1641 static llcp_state_e _net_nfc_get_llcp_state(void *pContext)
1646 state = NET_NFC_STATE_EXCHANGER_SERVER;
1653 static bool _net_nfc_make_llcp_data(emulMsg_s *emul_msg)
1656 data_s rawdata = {NULL, 0};
1658 _net_nfc_retrieve_ndef_message(&rawdata);
1659 _nfc_emul_util_free_mem(rawdata.buffer);
1661 if (emul_msg->record_count == 0 || rawdata.length == 0) {
1662 DEBUG_ERR_MSG("data is zero >>>");
1666 /* For SNEP, we should create snep msg, and then copy it to llcp_server_data */
1667 _net_nfc_llcp_create_snep_server_msg(SNEP_REQ_PUT);
1669 /* copy rawdata to llcp_server_data->buffer */
1670 if (Snep_Server_msg->data->length <= SNEP_MAX_BUFFER) {
1671 DEBUG_MSG("The snep msg size is small than SNEP_MAX_BUFFER >>>");
1673 if (llcp_server_data == NULL)
1676 llcp_server_data->length = Snep_Server_msg->data->length;
1677 memcpy(llcp_server_data->buffer, Snep_Server_msg->data->buffer, Snep_Server_msg->data->length);
1679 DEBUG_MSG("send first segment >>>");
1681 if (llcp_server_data == NULL)
1684 llcp_server_data->length = SNEP_MAX_BUFFER;
1685 memcpy(llcp_server_data->buffer, Snep_Server_msg->data->buffer, SNEP_MAX_BUFFER);
1687 Snep_Server_msg->isSegment = true;
1688 Snep_Server_msg->offset = SNEP_MAX_BUFFER;
1696 static void _net_nfc_send_emulMsg_to_nfc_manager(emulMsg_s *emul_msg)
1700 switch (emul_msg->message_id) {
1701 case EMUL_NFC_TAG_DISCOVERED:
1702 _net_nfc_target_discovered_cb(emul_msg);
1705 case EMUL_NFC_TAG_DETACHED:
1706 _net_nfc_tag_detached_cb();
1709 case EMUL_NFC_P2P_DISCOVERED:
1710 _net_nfc_initialize_llcp();
1711 _net_nfc_target_discovered_cb(emul_msg);
1714 case EMUL_NFC_P2P_SEND:
1715 if (!_net_nfc_emul_get_is_p2p_attached()) {
1716 DEBUG_ERR_MSG("target is not attached!!");
1720 if (_net_nfc_make_llcp_data(emul_msg)) {
1721 /* find snep server*/
1722 socket_info_s *socket_info = _net_nfc_find_server_socket(NET_NFC_EMUL_SNEP_SERVER_SOCKET_NUMBER);
1723 if (socket_info == NULL) {
1724 DEBUG_ERR_MSG("socket_info is NULL");
1728 _net_nfc_llcp_data_receive_cb(socket_info->user_context); /* call callback */
1730 DEBUG_ERR_MSG("make_p2p_data is fail!!");
1735 case EMUL_NFC_P2P_DETACHED:
1736 if (!_net_nfc_emul_get_is_p2p_attached()) {
1737 DEBUG_ERR_MSG("target is not attached!!");
1741 _net_nfc_target_detached_cb();
1742 _net_nfc_deinitialize_llcp();
1746 DEBUG_ERR_MSG("message_id is wrong!!");
1753 static void _net_nfc_process_emulMsg(uint8_t *data, long int size)
1757 emulMsg_s *emul_msg = NULL;
1759 /* create emul_msg */
1760 _net_nfc_create_emulMsg(&emul_msg, data, size);
1762 DEBUG_MSG("emul_msg->message_id >>>>[%d]", emul_msg->message_id);
1764 /* processing emul_msg */
1765 _net_nfc_send_emulMsg_to_nfc_manager(emul_msg);
1767 /* destroy emul_msg */
1768 _net_nfc_destroy_emulMsg(emul_msg);
1773 #ifdef __USE_EPOLL_FOR_FILE__
1774 static void emul_ReaderThread(void *pArg)
1776 DEBUG_MSG("emul_ReaderThread START >>>>");
1778 char file_name[1024] = {0};
1779 bool condition = true;
1781 int emulMsg_file_fd = -1;
1782 int emulMsg_poll_fd = -1;
1783 struct epoll_event *emulMsg_poll_events = NULL;
1785 /* make file name */
1786 snprintf(file_name, sizeof(file_name), "%s/%s", NET_NFC_EMUL_DATA_PATH, NET_NFC_EMUL_MESSAGE_FILE_NAME);
1787 LOGE("file path : %s", file_name);
1789 /* open file for poll */
1790 emulMsg_file_fd = open(file_name, O_RDONLY | O_NONBLOCK);
1791 if (emulMsg_file_fd < 0) {
1792 DEBUG_MSG("file open error !!!!");
1797 if ((emulMsg_poll_fd = epoll_create1(EPOLL_CLOEXEC)) == -1) {
1798 DEBUG_MSG("epoll_create1 is occured");
1799 close(emulMsg_file_fd);
1803 if ((emulMsg_poll_events = calloc(1, sizeof(struct epoll_event) * EPOLL_SIZE)) == NULL) {
1804 DEBUG_MSG("calloc is occured");
1805 close(emulMsg_file_fd);
1810 struct epoll_event ev;
1812 ev.events = EPOLLIN | EPOLLET | EPOLLHUP | EPOLLERR;
1813 ev.data.fd = emulMsg_file_fd;
1815 /* add file fd to epoll */
1816 epoll_ctl(emulMsg_poll_fd, EPOLL_CTL_ADD, emulMsg_file_fd, &ev);
1818 while (condition == true) {
1820 int num_of_files = 0;
1823 /* to do : I will add mutex in the future */
1826 DEBUG_MSG("epoll wait >>>>");
1828 while ((num_of_files = epoll_wait(emulMsg_poll_fd, emulMsg_poll_events, EPOLL_SIZE, -1)) == 0)
1829 DEBUG_MSG("no data is changed ");
1831 for (index = 0; index < num_of_files; index++) {
1832 if ((emulMsg_poll_events[index].events & (EPOLLHUP)) || (emulMsg_poll_events[index].events & (EPOLLERR))) {
1833 DEBUG_MSG("connection is closed");
1836 } else if (emulMsg_poll_events[index].events & EPOLLIN) {
1837 if (emulMsg_poll_events[index].data.fd == emulMsg_file_fd) {
1838 char readbuffer[READ_BUFFER_LENGTH_MAX + 1];
1841 DEBUG_MSG("precess POLLIN ");
1843 memset(readbuffer, 0x00, sizeof(READ_BUFFER_LENGTH_MAX) + 1);
1845 readcnt = read(emulMsg_file_fd, readbuffer, READ_BUFFER_LENGTH_MAX);
1847 DEBUG_MSG("message readcnt= [%d] ", readcnt);
1848 DEBUG_MSG("message = [%s] ", readbuffer);
1851 _net_nfc_process_emulMsg((uint8_t *)readbuffer + NET_NFC_EMUL_HEADER_LENGTH, (long int)readcnt - NET_NFC_EMUL_HEADER_LENGTH);
1853 DEBUG_MSG("not expected socket connection");
1857 if (num_of_files == index) {
1858 DEBUG_MSG("unknown event");
1870 close(emulMsg_file_fd);
1872 if (emulMsg_poll_events != NULL)
1873 free(emulMsg_poll_events);
1875 DEBUG_MSG("emul_ReaderThread END >>>>");
1880 static void emul_ReaderThread(void *pArg)
1882 DEBUG_MSG("emul_ReaderThread START >>>>");
1885 char file_name[1024] = {0};
1887 char readBuffer[READ_BUFFER_LENGTH_MAX];
1893 bool condition = true;
1895 /* make file name */
1896 snprintf(file_name, sizeof(file_name), "%s/%s", NET_NFC_EMUL_DATA_PATH, NET_NFC_EMUL_MESSAGE_FILE_NAME);
1897 LOGE("file path : %s", file_name);
1900 DEBUG_MSG("Start Current Time [%ld]", (unsigned long)curTime);
1902 /* polling 500 ms */
1904 while (condition != 0) {
1908 if ((fp = fopen(file_name, "r")) == NULL) {
1909 DEBUG_MSG("file open error");
1912 /* get the modified time of the file */
1913 if (stat(file_name, &st) == 0) {
1914 if ((unsigned long)st.st_mtime <= (unsigned long)curTime) {
1918 DEBUG_MSG("FILE Modified Time [%ld]", (unsigned long)st.st_mtime);
1919 /* update current time */
1921 DEBUG_MSG("Get New Current Time [%ld]", (unsigned long)curTime);
1925 DEBUG_MSG("stat error");
1931 memset(readBuffer, 0x00, READ_BUFFER_LENGTH_MAX);
1933 if (fscanf(fp, "%[^\n]", readBuffer))
1934 DEBUG_MSG("get DATA >>>> readBuffer [%s]", readBuffer);
1936 /* process message */
1937 _net_nfc_process_emulMsg((uint8_t *)readBuffer, (long int)strlen(readBuffer));
1942 DEBUG_MSG("LOOP END >>>>");
1946 DEBUG_MSG("emul_ReaderThread END >>>>");
1950 static bool _net_nfc_emul_controller_start_thread(void)
1956 ret = pthread_create(&gEmulThread, NULL, (emul_Nfc_thread_handler_t)emul_ReaderThread, (void*)"emul_read_thread");
1966 static void _net_nfc_emul_controller_stop_thread(void)
1970 pthread_cancel(gEmulThread);
1974 pthread_cond_signal(&cond);
1976 pthread_join(gEmulThread, NULL);
1981 static bool net_nfc_emul_controller_init(net_nfc_error_e *result)
1990 DEBUG_MSG("start stack init ");
1992 if (g_stack_init_successful == true) {
1993 DEBUG_MSG("Already statck is initialized");
1998 /* start reader thread : to get event from Inject */
1999 if (!_net_nfc_emul_controller_start_thread()) {
2000 DEBUG_ERR_MSG("Failed to create emul thread");
2004 DEBUG_MSG("Stack init finished");
2006 g_stack_init_successful = true;
2007 *result = NET_NFC_OK;
2014 static bool net_nfc_emul_controller_deinit(void)
2019 if (g_stack_init_successful == false) {
2020 DEBUG_MSG("Already statck is deinitialized");
2024 _net_nfc_emul_controller_stop_thread();
2026 g_stack_init_successful = false;
2032 static bool net_nfc_emul_controller_register_listener(
2033 target_detection_listener_cb target_detection_listener,
2034 se_transaction_listener_cb se_transaction_listener,
2035 llcp_event_listener_cb llcp_event_listener,
2036 hce_apdu_listener_cb hce_apdu_listener,
2037 net_nfc_error_e *result)
2042 *result = NET_NFC_OK;
2046 g_emul_controller_target_cb = target_detection_listener;
2047 g_emul_controller_se_cb = se_transaction_listener;
2048 g_emul_controller_llcp_cb = llcp_event_listener;
2049 g_emul_controller_hce_cb = hce_apdu_listener;
2056 static bool net_nfc_emul_controller_unregister_listener()
2060 g_emul_controller_target_cb = NULL;
2061 g_emul_controller_se_cb = NULL;
2062 g_emul_controller_llcp_cb = NULL;
2069 static bool net_nfc_emul_controller_get_firmware_version(data_s **data, net_nfc_error_e *result)
2071 if (data == NULL || result == NULL)
2074 *result = NET_NFC_OK;
2078 *data = calloc(1, sizeof(data_s));
2083 (*data)->length = 10;
2084 (*data)->buffer = calloc(1, (*data)->length);
2086 if ((*data)->buffer == NULL)
2089 snprintf((char *)(*data)->buffer, (*data)->length, "%d.%d", 1, 0);
2096 static bool net_nfc_emul_controller_check_firmware_version(net_nfc_error_e *result)
2101 *result = NET_NFC_OK;
2110 static bool net_nfc_emul_controller_update_firmware(net_nfc_error_e *result)
2115 *result = NET_NFC_OK;
2124 static bool net_nfc_emul_controller_get_stack_information(net_nfc_stack_information_s *stack_info, net_nfc_error_e *result)
2129 *result = NET_NFC_OK;
2138 static bool net_nfc_emul_controller_configure_discovery(net_nfc_discovery_mode_e mode, net_nfc_event_filter_e config, net_nfc_error_e *result)
2146 *result = NET_NFC_OK;
2148 if (mode == NET_NFC_DISCOVERY_MODE_START) {
2149 mode = NET_NFC_DISCOVERY_MODE_CONFIG;
2150 } else if (mode == NET_NFC_DISCOVERY_MODE_STOP) {
2151 mode = NET_NFC_DISCOVERY_MODE_CONFIG;
2152 config = NET_NFC_ALL_DISABLE;
2157 if ((mode == NET_NFC_DISCOVERY_MODE_CONFIG)) {
2158 if (config == NET_NFC_ALL_DISABLE) {
2159 /* This handle is not useful anymore */
2160 __net_nfc_make_invalid_target_handle();
2162 /* reset socket_info */
2163 for (idx = 0; idx < LLCP_NB_SOCKET_MAX; idx++)
2164 _net_nfc_remove_socket_slot((net_nfc_llcp_socket_t)idx);
2166 DEBUG_MSG("Kill Thread");
2168 ret = net_nfc_emul_controller_deinit();
2169 } else if (config == NET_NFC_ALL_ENABLE) {
2170 net_nfc_error_e err;
2172 DEBUG_MSG("Create Thread");
2173 ret = net_nfc_emul_controller_init(&err);
2182 static bool net_nfc_emul_controller_get_secure_element_list(net_nfc_secure_element_info_s *list, int *count, net_nfc_error_e *result)
2187 *result = NET_NFC_OK;
2196 static bool net_nfc_emul_controller_set_secure_element_mode(net_nfc_secure_element_type_e element_type, net_nfc_secure_element_mode_e mode, net_nfc_error_e *result)
2201 *result = NET_NFC_OK;
2210 static bool net_nfc_emul_controller_check_target_presence(net_nfc_target_handle_s *handle, net_nfc_error_e *result)
2215 *result = NET_NFC_OK;
2219 if (_net_nfc_emul_get_is_tag_attached()) {
2222 DEBUG_MSG("TAG Detached");
2227 static bool net_nfc_emul_controller_connect(net_nfc_target_handle_s *handle, net_nfc_error_e *result)
2232 *result = NET_NFC_OK;
2234 if (!__net_nfc_is_valide_target_handle(handle)) {
2235 *result = NET_NFC_INVALID_HANDLE;
2246 static bool net_nfc_emul_controller_disconnect(net_nfc_target_handle_s *handle, net_nfc_error_e *result)
2251 *result = NET_NFC_OK;
2255 if (!__net_nfc_is_valide_target_handle(handle)) {
2256 *result = NET_NFC_INVALID_HANDLE;
2260 // This handle is not useful anymore
2261 __net_nfc_make_invalid_target_handle();
2263 /* reset socket_info */
2265 for (; idx < LLCP_NB_SOCKET_MAX; idx++)
2266 _net_nfc_remove_socket_slot((net_nfc_llcp_socket_t)idx);
2273 static bool net_nfc_emul_controller_check_ndef(net_nfc_target_handle_s *handle, uint8_t *ndef_card_state, int *max_data_size, int *real_data_size, net_nfc_error_e *result)
2275 data_s rawdata = {NULL, 0};
2280 *result = NET_NFC_OK;
2282 if (handle == NULL || ndef_card_state == NULL || max_data_size == NULL || real_data_size == NULL || result == NULL) {
2283 *result = NET_NFC_NULL_PARAMETER;
2287 if (!__net_nfc_is_valide_target_handle(handle)) {
2288 *result = NET_NFC_INVALID_HANDLE;
2294 _net_nfc_retrieve_ndef_message(&rawdata);
2295 _nfc_emul_util_free_mem(rawdata.buffer);
2297 if (_net_nfc_emul_get_is_tag_attached()) {
2298 *ndef_card_state = NET_NFC_NDEF_CARD_READ_WRITE;
2299 *max_data_size = BUFFER_LENGTH_MAX;
2300 *real_data_size = rawdata.length;
2301 DEBUG_MSG("Card State : [%d] MAX data size :[%d] actual data size = [%d]", *ndef_card_state, *max_data_size, *real_data_size);
2303 DEBUG_MSG("target detached");
2311 static bool net_nfc_emul_controller_make_read_only_ndef(net_nfc_target_handle_s *handle, net_nfc_error_e *result)
2316 *result = NET_NFC_OK;
2318 if (!__net_nfc_is_valide_target_handle(handle)) {
2319 *result = NET_NFC_INVALID_HANDLE;
2330 static bool net_nfc_emul_controller_read_ndef(net_nfc_target_handle_s *handle, data_s **data, net_nfc_error_e *result)
2332 int real_data_size = 0;
2333 data_s rawdata = {NULL, 0};
2340 *result = NET_NFC_OK;
2342 if (handle == NULL || data == NULL || result == NULL) {
2343 DEBUG_ERR_MSG("NET_NFC_NULL_PARAMETER >>>");
2344 *result = NET_NFC_NULL_PARAMETER;
2348 if (!__net_nfc_is_valide_target_handle(handle)) {
2349 DEBUG_ERR_MSG("NET_NFC_INVALID_HANDLE >>>");
2350 *result = NET_NFC_INVALID_HANDLE;
2354 if (!_net_nfc_emul_get_is_tag_attached()) {
2355 DEBUG_ERR_MSG("NET_NFC_NOT_ALLOWED_OPERATION >>>");
2356 *result = NET_NFC_NOT_ALLOWED_OPERATION;
2360 *result = _net_nfc_retrieve_ndef_message(&rawdata);
2361 if (*result != NET_NFC_OK) {
2362 _nfc_emul_util_free_mem(rawdata.buffer);
2366 real_data_size = rawdata.length;
2368 if (real_data_size == 0) {
2369 DEBUG_ERR_MSG("read ndef_msg is failed >>> real_data_size is zero");
2370 *result = NET_NFC_NO_NDEF_MESSAGE;
2371 _nfc_emul_util_free_mem(rawdata.buffer);
2375 *data = calloc(1, sizeof(data_s));
2377 if (*data == NULL) {
2378 *result = NET_NFC_ALLOC_FAIL;
2379 _nfc_emul_util_free_mem(rawdata.buffer);
2383 (*data)->length = real_data_size;
2384 (*data)->buffer = calloc(1, real_data_size);
2386 if ((*data)->buffer == NULL) {
2388 *result = NET_NFC_ALLOC_FAIL;
2389 _nfc_emul_util_free_mem(rawdata.buffer);
2393 /* copy rawdata to data->buffer */
2394 memcpy((*data)->buffer, rawdata.buffer, real_data_size);
2395 _nfc_emul_util_free_mem(rawdata.buffer);
2402 static bool net_nfc_emul_controller_write_ndef(net_nfc_target_handle_s *handle, data_s *data, net_nfc_error_e *result)
2407 *result = NET_NFC_OK;
2409 if (!__net_nfc_is_valide_target_handle(handle)) {
2410 *result = NET_NFC_INVALID_HANDLE;
2416 DEBUG_MSG("net_nfc_emul_controller_write_ndef success >>>");
2418 _net_nfc_store_ndef_message(data);
2426 static bool net_nfc_emul_controller_transceive(net_nfc_target_handle_s *handle,
2427 net_nfc_transceive_info_s *info, data_s **data, net_nfc_error_e *result)
2434 if (info == NULL || info->trans_data.buffer == NULL ||
2435 info->trans_data.length == 0) {
2436 *result = NET_NFC_INVALID_PARAM;
2440 *result = NET_NFC_OK;
2443 if (!__net_nfc_is_valide_target_handle(handle)) {
2444 *result = NET_NFC_INVALID_HANDLE;
2450 /* This implementation is not needed on Emulator environment */
2457 static bool net_nfc_emul_controller_format_ndef(net_nfc_target_handle_s *handle, data_s *secure_key, net_nfc_error_e *result)
2462 *result = NET_NFC_OK;
2464 if (!__net_nfc_is_valide_target_handle(handle)) {
2465 *result = NET_NFC_INVALID_HANDLE;
2476 static bool net_nfc_emul_controller_exception_handler(void)
2480 net_nfc_error_e error;
2482 if (net_nfc_emul_controller_init(&error) == false) {
2483 DEBUG_ERR_MSG("exception handler is failed!!");
2492 static bool net_nfc_emul_controller_is_ready(net_nfc_error_e *result)
2497 *result = NET_NFC_OK;
2503 return g_stack_init_successful;
2507 /*******************
2509 ********************/
2511 static bool net_nfc_emul_controller_llcp_config(net_nfc_llcp_config_info_s *config, net_nfc_error_e *result)
2516 *result = NET_NFC_OK;
2525 static bool net_nfc_emul_controller_llcp_check_llcp(net_nfc_target_handle_s *handle, net_nfc_error_e *result)
2530 *result = NET_NFC_OK;
2532 if (!__net_nfc_is_valide_target_handle(handle)) {
2533 *result = NET_NFC_INVALID_HANDLE;
2544 static bool net_nfc_emul_controller_llcp_activate_llcp(net_nfc_target_handle_s *handle, net_nfc_error_e *result)
2549 *result = NET_NFC_OK;
2551 if (!__net_nfc_is_valide_target_handle(handle)) {
2552 *result = NET_NFC_INVALID_HANDLE;
2563 static bool net_nfc_emul_controller_llcp_create_socket(net_nfc_llcp_socket_t *socket, net_nfc_socket_type_e socketType, uint16_t miu, uint8_t rw, net_nfc_error_e *result, void *user_param)
2568 *result = NET_NFC_OK;
2572 /* get available socket */
2573 socket_info_s *socket_info = _net_nfc_get_available_socket_slot();
2574 if (socket_info == NULL) {
2575 DEBUG_ERR_MSG("The available socket is nothing!!");
2579 /* get llcp state */
2580 llcp_state_e llcp_state = NET_NFC_STATE_UNKNOWN;
2581 llcp_state = _net_nfc_get_llcp_state(user_param);
2582 if (llcp_state == NET_NFC_STATE_UNKNOWN) {
2583 DEBUG_ERR_MSG("llcp_state is NET_NFC_STATE_UNKNOWN!!");
2587 if (llcp_state == NET_NFC_STATE_EXCHANGER_SERVER) {
2588 DEBUG_MSG("NET_NFC_STATE_EXCHANGER_SERVER");
2590 *socket = NET_NFC_EMUL_SNEP_SERVER_SOCKET_NUMBER;
2591 } else if (llcp_state == NET_NFC_STATE_EXCHANGER_SERVER_NPP) {
2592 DEBUG_MSG("NET_NFC_STATE_EXCHANGER_SERVER_NPP");
2594 *socket = NET_NFC_EMUL_NPP_SERVER_SOCKET_NUMBER;
2595 } else if (llcp_state == NET_NFC_STATE_EXCHANGER_CLIENT) {
2596 DEBUG_MSG("NET_NFC_STATE_EXCHANGER_CLIENT");
2598 *socket = NET_NFC_EMUL_CLIENT_SOCKET_NUMBER;
2599 } else if (llcp_state == NET_NFC_STATE_CONN_HANDOVER_REQUEST) {
2600 DEBUG_MSG("NET_NFC_STATE_CONN_HANDOVER_REQUEST");
2602 *socket = NET_NFC_EMUL_HANDOVER_REQUEST_SOCKET_NUMBER;
2603 } else if (llcp_state == NET_NFC_STATE_CONN_HANDOVER_SELECT) {
2604 DEBUG_MSG("NET_NFC_STATE_CONN_HANDOVER_SELECT");
2606 *socket = NET_NFC_EMUL_HANDOVER_SELECT_SOCKET_NUMBER;
2608 DEBUG_MSG("we doesn't support..");
2613 DEBUG_MSG("socket is created = [0x%x]", *socket);
2615 socket_info->socket_handle = *socket;
2616 socket_info->user_context = (void *)user_param;
2623 static bool net_nfc_emul_controller_llcp_bind(net_nfc_llcp_socket_t socket, uint8_t service_access_point, net_nfc_error_e *result)
2628 *result = NET_NFC_OK;
2637 static bool net_nfc_emul_controller_llcp_listen(net_nfc_target_handle_s *handle, uint8_t *service_access_name, net_nfc_llcp_socket_t socket, net_nfc_error_e *result, void *user_param)
2642 *result = NET_NFC_OK;
2644 if (!__net_nfc_is_valide_target_handle(handle)) {
2645 *result = NET_NFC_INVALID_HANDLE;
2651 /* Emul don't create real socket. So, we don't need to wait accept from remote socket */
2652 /* In here, send accept event for only snep */
2653 net_nfc_request_listen_socket_t *detail = NULL;
2655 socket_info_s *socket_info = _net_nfc_find_server_socket(socket);
2656 if (socket_info == NULL) {
2657 DEBUG_ERR_MSG("socket_info is NULL");
2661 llcp_state_e llcp_state = NET_NFC_STATE_UNKNOWN;
2662 llcp_state = _net_nfc_get_llcp_state(socket_info->user_context);
2663 if (llcp_state == NET_NFC_STATE_UNKNOWN) {
2664 DEBUG_ERR_MSG("llcp_state is NET_NFC_STATE_UNKNOWN");
2668 if (llcp_state == NET_NFC_STATE_EXCHANGER_SERVER) {
2669 _nfc_emul_util_alloc_mem(detail, sizeof(*detail));
2671 if (detail != NULL) {
2672 detail->length = sizeof(*detail);
2673 detail->request_type = NET_NFC_MESSAGE_SERVICE_LLCP_LISTEN;
2675 socket_info->user_context = user_param;
2677 detail->handle = handle;
2678 detail->client_socket = NET_NFC_EMUL_INCOMING_SOCKET_NUMBER;
2679 detail->trans_param = socket_info->user_context;
2680 detail->result = NET_NFC_OK;
2682 DEBUG_MSG("accept callback is called");
2683 g_emul_controller_llcp_cb(detail, socket_info->user_context);
2686 DEBUG_MSG("llcp_state is [%d]", llcp_state);
2694 /* below accept function does not used. */
2695 static bool net_nfc_emul_controller_llcp_accept(net_nfc_llcp_socket_t socket, net_nfc_error_e *result, void *user_param)
2700 *result = NET_NFC_OK;
2709 static bool net_nfc_emul_controller_llcp_connect(net_nfc_target_handle_s *handle, net_nfc_llcp_socket_t socket, uint8_t service_access_point, net_nfc_error_e *result, void *user_param)
2714 *result = NET_NFC_OK;
2716 if (!__net_nfc_is_valide_target_handle(handle)) {
2717 *result = NET_NFC_INVALID_HANDLE;
2723 socket_info_s *socket_info = _net_nfc_find_server_socket(socket);
2724 if (socket_info == NULL) {
2725 DEBUG_ERR_MSG("socket_info is NULL");
2729 llcp_state_e llcp_state = NET_NFC_STATE_UNKNOWN;
2730 llcp_state = _net_nfc_get_llcp_state(socket_info->user_context);
2731 if (llcp_state == NET_NFC_STATE_UNKNOWN) {
2732 DEBUG_ERR_MSG("llcp_state is NET_NFC_STATE_UNKNOWN");
2741 static bool net_nfc_emul_controller_llcp_connect_by_url(net_nfc_target_handle_s *handle, net_nfc_llcp_socket_t socket, uint8_t *service_access_name, net_nfc_error_e *result, void *user_param)
2746 *result = NET_NFC_OK;
2748 if (!__net_nfc_is_valide_target_handle(handle)) {
2749 *result = NET_NFC_INVALID_HANDLE;
2755 socket_info_s *socket_info = _net_nfc_find_server_socket(socket);
2756 if (socket_info == NULL) {
2757 DEBUG_ERR_MSG("socket_info is NULL");
2761 llcp_state_e llcp_state = NET_NFC_STATE_UNKNOWN;
2762 llcp_state = _net_nfc_get_llcp_state(socket_info->user_context);
2763 if (llcp_state == NET_NFC_STATE_UNKNOWN) {
2764 DEBUG_ERR_MSG("llcp_state is NET_NFC_STATE_UNKNOWN");
2773 static bool net_nfc_emul_controller_llcp_send(net_nfc_target_handle_s *handle, net_nfc_llcp_socket_t socket, data_s *data, net_nfc_error_e *result, void *user_param)
2778 *result = NET_NFC_OK;
2780 if (!__net_nfc_is_valide_target_handle(handle)) {
2781 *result = NET_NFC_INVALID_HANDLE;
2787 socket_info_s *socket_info = _net_nfc_find_server_socket(socket);
2788 if (socket_info == NULL) {
2789 DEBUG_ERR_MSG("socket_info is NULL");
2793 llcp_state_e llcp_state = NET_NFC_STATE_UNKNOWN;
2794 llcp_state = _net_nfc_get_llcp_state(socket_info->user_context);
2795 if (llcp_state == NET_NFC_STATE_UNKNOWN) {
2796 DEBUG_ERR_MSG("llcp_state is NET_NFC_STATE_UNKNOWN");
2800 if (llcp_state == NET_NFC_STATE_EXCHANGER_SERVER) {
2801 net_nfc_request_llcp_msg_t *req_msg = NULL;
2803 _nfc_emul_util_alloc_mem(req_msg, sizeof(net_nfc_request_llcp_msg_t));
2805 socket_info->user_context = user_param;
2807 if (req_msg != NULL) {
2808 req_msg->length = sizeof(net_nfc_request_llcp_msg_t);
2809 req_msg->request_type = NET_NFC_MESSAGE_SERVICE_LLCP_SEND;
2810 req_msg->result = NET_NFC_OK;
2812 DEBUG_MSG("send callback is called");
2813 g_emul_controller_llcp_cb(req_msg, socket_info->user_context);
2822 static bool net_nfc_emul_controller_llcp_send_to(net_nfc_target_handle_s *handle, net_nfc_llcp_socket_t socket, data_s *data, uint8_t service_access_point, net_nfc_error_e *result, void *user_param)
2827 *result = NET_NFC_OK;
2829 if (!__net_nfc_is_valide_target_handle(handle)) {
2830 *result = NET_NFC_INVALID_HANDLE;
2836 socket_info_s *socket_info = _net_nfc_find_server_socket(socket);
2837 if (socket_info == NULL) {
2838 DEBUG_ERR_MSG("socket_info is NULL");
2842 llcp_state_e llcp_state = NET_NFC_STATE_UNKNOWN;
2843 llcp_state = _net_nfc_get_llcp_state(socket_info->user_context);
2844 if (llcp_state == NET_NFC_STATE_UNKNOWN) {
2845 DEBUG_ERR_MSG("llcp_state is NET_NFC_STATE_UNKNOWN");
2849 if (llcp_state == NET_NFC_STATE_EXCHANGER_SERVER) {
2850 net_nfc_request_llcp_msg_t *req_msg = NULL;
2852 _nfc_emul_util_alloc_mem(req_msg, sizeof(net_nfc_request_llcp_msg_t));
2854 socket_info->user_context = user_param;
2856 if (req_msg != NULL) {
2857 req_msg->length = sizeof(net_nfc_request_llcp_msg_t);
2858 req_msg->request_type = NET_NFC_MESSAGE_SERVICE_LLCP_SEND_TO;
2859 req_msg->result = NET_NFC_OK;
2861 DEBUG_MSG("send_to callback is called");
2862 g_emul_controller_llcp_cb(req_msg, socket_info->user_context);
2872 static bool net_nfc_emul_controller_llcp_recv(net_nfc_target_handle_s *handle, net_nfc_llcp_socket_t socket, data_s *data, net_nfc_error_e *result, void *user_param)
2874 if (result == NULL || data == NULL)
2877 *result = NET_NFC_OK;
2879 if (!__net_nfc_is_valide_target_handle(handle)) {
2880 *result = NET_NFC_INVALID_HANDLE;
2886 socket_info_s *socket_info = _net_nfc_find_server_socket(socket);
2887 if (socket_info == NULL) {
2888 DEBUG_ERR_MSG("socket_info is NULL");
2892 llcp_state_e llcp_state = NET_NFC_STATE_UNKNOWN;
2893 llcp_state = _net_nfc_get_llcp_state(socket_info->user_context);
2894 if (llcp_state == NET_NFC_STATE_UNKNOWN) {
2895 DEBUG_ERR_MSG("llcp_state is NET_NFC_STATE_UNKNOWN");
2899 if (llcp_state == NET_NFC_STATE_EXCHANGER_SERVER) {
2901 DEBUG_MSG("NET_NFC_STATE_EXCHANGER_SERVER");
2902 socket_info->user_context = user_param;
2904 if (Snep_Server_msg->isSegment) {
2905 /* send snep msg continueosly ..*/
2906 DEBUG_MSG("send segments for snep msg");
2908 int remained_size = 0;
2910 remained_size = Snep_Server_msg->data->length - Snep_Server_msg->offset;
2911 DEBUG_MSG("remained_size[%d]", remained_size);
2913 /* copy rawdata to llcp_server_data->buffer */
2914 if (remained_size <= SNEP_MAX_BUFFER) {
2915 DEBUG_MSG("send last segment >>>");
2917 llcp_server_data->length = remained_size;
2918 memcpy(llcp_server_data->buffer, Snep_Server_msg->data->buffer + Snep_Server_msg->offset, remained_size);
2920 Snep_Server_msg->isSegment = false;
2922 DEBUG_MSG("send continue segment >>>");
2924 llcp_server_data->length = SNEP_MAX_BUFFER;
2925 memcpy(llcp_server_data->buffer, Snep_Server_msg->data->buffer + Snep_Server_msg->offset, SNEP_MAX_BUFFER);
2927 Snep_Server_msg->offset += SNEP_MAX_BUFFER;
2930 _net_nfc_llcp_data_receive_cb(socket_info->user_context); /* call callback */
2932 /* In here, we dosen't call _net_nfc_llcp_data_receive_cb. just wait event from emulator */
2933 /*After copying data address, we will return it, immediately */
2934 DEBUG_MSG("data address is set");
2935 llcp_server_data = data;
2938 DEBUG_MSG("we don't support..");
2946 static bool net_nfc_emul_controller_llcp_recv_from(net_nfc_target_handle_s *handle, net_nfc_llcp_socket_t socket, data_s *data, net_nfc_error_e *result, void *user_param)
2951 *result = NET_NFC_OK;
2953 if (!__net_nfc_is_valide_target_handle(handle)) {
2954 *result = NET_NFC_INVALID_HANDLE;
2960 socket_info_s *socket_info = _net_nfc_find_server_socket(socket);
2961 if (socket_info == NULL) {
2962 DEBUG_ERR_MSG("socket_info is NULL");
2966 llcp_state_e llcp_state = NET_NFC_STATE_UNKNOWN;
2967 llcp_state = _net_nfc_get_llcp_state(socket_info->user_context);
2968 if (llcp_state == NET_NFC_STATE_UNKNOWN) {
2969 DEBUG_ERR_MSG("llcp_state is NET_NFC_STATE_UNKNOWN");
2973 if (llcp_state == NET_NFC_STATE_EXCHANGER_SERVER) {
2975 DEBUG_MSG("NET_NFC_STATE_EXCHANGER_SERVER");
2976 socket_info->user_context = user_param;
2978 if (Snep_Server_msg->isSegment) {
2979 /* send snep msg continueosly ..*/
2980 DEBUG_MSG("send segments for snep msg");
2982 int remained_size = 0;
2984 remained_size = Snep_Server_msg->data->length - Snep_Server_msg->offset;
2985 DEBUG_MSG("remained_size[%d]", remained_size);
2987 /* copy rawdata to llcp_server_data->buffer */
2988 if (remained_size <= SNEP_MAX_BUFFER) {
2989 DEBUG_MSG("send last segment >>>");
2991 llcp_server_data->length = remained_size;
2992 memcpy(llcp_server_data->buffer, Snep_Server_msg->data->buffer + Snep_Server_msg->offset, remained_size);
2994 Snep_Server_msg->isSegment = false;
2996 DEBUG_MSG("send continue segment >>>");
2998 llcp_server_data->length = SNEP_MAX_BUFFER;
2999 memcpy(llcp_server_data->buffer, Snep_Server_msg->data->buffer + Snep_Server_msg->offset, SNEP_MAX_BUFFER);
3001 Snep_Server_msg->offset += SNEP_MAX_BUFFER;
3004 _net_nfc_llcp_data_receive_from_cb(socket_info->user_context); /* call callback */
3006 /* In here, we dosen't call _net_nfc_llcp_data_receive_cb. just wait event from emulator */
3007 /*After copying data address, we will return it, immediately */
3009 DEBUG_MSG("data address is set");
3010 llcp_server_data = data;
3012 DEBUG_ERR_MSG("data address is NULL");
3017 DEBUG_MSG("we donen't support..");
3025 static bool net_nfc_emul_controller_llcp_disconnect(net_nfc_target_handle_s *handle, net_nfc_llcp_socket_t socket, net_nfc_error_e *result, void *user_param)
3030 *result = NET_NFC_OK;
3032 if (!__net_nfc_is_valide_target_handle(handle)) {
3033 *result = NET_NFC_INVALID_HANDLE;
3039 socket_info_s *socket_info = _net_nfc_find_server_socket(socket);
3040 if (socket_info == NULL) {
3041 DEBUG_ERR_MSG("socket_info is NULL");
3045 llcp_state_e llcp_state = NET_NFC_STATE_UNKNOWN;
3046 llcp_state = _net_nfc_get_llcp_state(socket_info->user_context);
3047 if (llcp_state == NET_NFC_STATE_UNKNOWN) {
3048 DEBUG_ERR_MSG("llcp_state is NET_NFC_STATE_UNKNOWN");
3052 /* send msg to framework */
3053 net_nfc_request_llcp_msg_t *req_msg = NULL;
3055 _nfc_emul_util_alloc_mem(req_msg, sizeof(net_nfc_request_llcp_msg_t));
3057 socket_info->user_context = user_param;
3059 if (req_msg != NULL) {
3060 req_msg->length = sizeof(net_nfc_request_llcp_msg_t);
3061 req_msg->request_type = NET_NFC_MESSAGE_SERVICE_LLCP_DISCONNECT;
3062 req_msg->result = NET_NFC_OK;
3064 DEBUG_MSG("disconnect callback is called");
3065 g_emul_controller_llcp_cb(req_msg, user_param);
3068 /* reset socket_info */
3069 _net_nfc_remove_socket_slot((net_nfc_llcp_socket_t)socket);
3076 static bool net_nfc_emul_controller_llcp_socket_close(net_nfc_llcp_socket_t socket,
3077 net_nfc_error_e *result)
3082 *result = NET_NFC_OK;
3091 static bool net_nfc_emul_controller_llcp_reject(net_nfc_target_handle_s *handle, net_nfc_llcp_socket_t socket, net_nfc_error_e *result)
3096 *result = NET_NFC_OK;
3098 if (!__net_nfc_is_valide_target_handle(handle)) {
3099 *result = NET_NFC_INVALID_HANDLE;
3110 static bool net_nfc_emul_controller_llcp_get_remote_config(net_nfc_target_handle_s *handle, net_nfc_llcp_config_info_s *config, net_nfc_error_e *result)
3112 /* In llcp specification ver 1.1, default miu size is 128 */
3113 const uint16_t default_miu = 128;
3118 *result = NET_NFC_OK;
3120 if (!__net_nfc_is_valide_target_handle(handle)) {
3121 *result = NET_NFC_INVALID_HANDLE;
3127 config->miu = default_miu;
3134 static bool net_nfc_emul_controller_llcp_get_remote_socket_info(net_nfc_target_handle_s *handle, net_nfc_llcp_socket_t socket, net_nfc_llcp_socket_option_s *option, net_nfc_error_e *result)
3136 /* In llcp specification ver 1.1, default miu size is 128 */
3137 const uint16_t default_miu = 128;
3142 *result = NET_NFC_OK;
3144 if (!__net_nfc_is_valide_target_handle(handle)) {
3145 *result = NET_NFC_INVALID_HANDLE;
3151 option->miu = default_miu;
3159 static bool net_nfc_emul_controller_support_nfc(net_nfc_error_e *result)
3162 struct stat st = {0};
3164 if (stat("/dev/nfc0", &st) == -1) {
3166 *result = NET_NFC_NOT_SUPPORTED;
3171 *result = NET_NFC_OK;
3177 static bool net_nfc_emul_controller_secure_element_open(net_nfc_secure_element_type_e element_type, net_nfc_target_handle_s **handle, net_nfc_error_e *result)
3179 DEBUG_ERR_MSG("se open start");
3180 *result = NET_NFC_OK;
3181 __net_nfc_make_valid_target_handle(handle);
3183 DEBUG_ERR_MSG("se_open end");
3186 static bool net_nfc_emul_controller_secure_element_get_atr(net_nfc_target_handle_s *handle,
3187 data_s **atr, net_nfc_error_e *result)
3191 int length = sizeof(buffer);
3194 DEBUG_ERR_MSG("se get_atr start");
3195 *result = NET_NFC_OK;
3198 strncpy(buffer, "getatr", sizeof(buffer));
3200 temp = calloc(1, sizeof(*temp));
3202 temp->buffer = calloc(1, length);
3203 if (temp->buffer != NULL) {
3204 memcpy(temp->buffer, buffer, length);
3205 temp->length = length;
3207 *result = NET_NFC_OK;
3210 *result = NET_NFC_ALLOC_FAIL;
3214 *result = NET_NFC_ALLOC_FAIL;
3217 DEBUG_ERR_MSG("se get atr end");
3220 static bool net_nfc_emul_controller_secure_element_send_apdu(net_nfc_target_handle_s *handle,
3221 data_s *command, data_s **response, net_nfc_error_e *result)
3228 DEBUG_ERR_MSG("se send apdu start");
3230 *result = NET_NFC_OK;
3234 strncpy(buffer, "response", sizeof(buffer));
3236 length = strlen(buffer);
3238 temp = calloc(1, sizeof(*temp));
3240 temp->buffer = calloc(1, length + 1);
3241 if (temp->buffer != NULL) {
3242 memcpy(temp->buffer, buffer, sizeof(temp->buffer) - 1);
3243 temp->length = length;
3246 *result = NET_NFC_OK;
3249 *result = NET_NFC_ALLOC_FAIL;
3253 *result = NET_NFC_ALLOC_FAIL;
3257 DEBUG_ERR_MSG("se send apdu end");
3260 static bool net_nfc_emul_controller_secure_element_close(net_nfc_target_handle_s *handle,
3261 net_nfc_error_e *result)
3263 DEBUG_ERR_MSG("se close start");
3264 *result = NET_NFC_OK;
3266 DEBUG_ERR_MSG("se close end");
3269 static int nfc_backend_init(void **data)
3271 hal_backend_nfc_funcs *nfc_funcs;
3273 nfc_funcs = calloc(1, sizeof(hal_backend_nfc_funcs));
3277 nfc_funcs->start = net_nfc_emul_controller_init;
3278 nfc_funcs->stop = net_nfc_emul_controller_deinit;
3279 nfc_funcs->register_listener = net_nfc_emul_controller_register_listener;
3280 nfc_funcs->unregister_listener = net_nfc_emul_controller_unregister_listener;
3281 nfc_funcs->get_firmware_version = net_nfc_emul_controller_get_firmware_version;
3282 nfc_funcs->check_firmware_version = net_nfc_emul_controller_check_firmware_version;
3283 nfc_funcs->update_firmware = net_nfc_emul_controller_update_firmware;
3284 nfc_funcs->get_stack_information = net_nfc_emul_controller_get_stack_information;
3285 nfc_funcs->configure_discovery = net_nfc_emul_controller_configure_discovery;
3286 nfc_funcs->get_secure_element_list = net_nfc_emul_controller_get_secure_element_list;
3287 nfc_funcs->set_secure_element_mode = net_nfc_emul_controller_set_secure_element_mode;
3288 nfc_funcs->connect = net_nfc_emul_controller_connect;
3289 nfc_funcs->disconnect = net_nfc_emul_controller_disconnect;
3290 nfc_funcs->check_ndef = net_nfc_emul_controller_check_ndef;
3291 nfc_funcs->check_target_presence = net_nfc_emul_controller_check_target_presence;
3292 nfc_funcs->read_ndef = net_nfc_emul_controller_read_ndef;
3293 nfc_funcs->write_ndef = net_nfc_emul_controller_write_ndef;
3294 nfc_funcs->make_read_only_ndef = net_nfc_emul_controller_make_read_only_ndef;
3295 nfc_funcs->transceive = net_nfc_emul_controller_transceive;
3296 nfc_funcs->format_ndef = net_nfc_emul_controller_format_ndef;
3297 nfc_funcs->exception_handler = net_nfc_emul_controller_exception_handler;
3298 nfc_funcs->is_ready = net_nfc_emul_controller_is_ready;
3300 nfc_funcs->llcp_config = net_nfc_emul_controller_llcp_config;
3301 nfc_funcs->llcp_check_llcp = net_nfc_emul_controller_llcp_check_llcp;
3302 nfc_funcs->llcp_activate_llcp = net_nfc_emul_controller_llcp_activate_llcp;
3303 nfc_funcs->llcp_create_socket = net_nfc_emul_controller_llcp_create_socket;
3304 nfc_funcs->llcp_bind = net_nfc_emul_controller_llcp_bind;
3305 nfc_funcs->llcp_listen = net_nfc_emul_controller_llcp_listen;
3306 nfc_funcs->llcp_accept = net_nfc_emul_controller_llcp_accept;
3307 nfc_funcs->llcp_connect_by_url = net_nfc_emul_controller_llcp_connect_by_url;
3308 nfc_funcs->llcp_connect = net_nfc_emul_controller_llcp_connect;
3309 nfc_funcs->llcp_disconnect = net_nfc_emul_controller_llcp_disconnect;
3310 nfc_funcs->llcp_socket_close = net_nfc_emul_controller_llcp_socket_close;
3311 nfc_funcs->llcp_recv = net_nfc_emul_controller_llcp_recv;
3312 nfc_funcs->llcp_send = net_nfc_emul_controller_llcp_send;
3313 nfc_funcs->llcp_recv_from = net_nfc_emul_controller_llcp_recv_from;
3314 nfc_funcs->llcp_send_to = net_nfc_emul_controller_llcp_send_to;
3315 nfc_funcs->llcp_reject = net_nfc_emul_controller_llcp_reject;
3316 nfc_funcs->llcp_get_remote_config = net_nfc_emul_controller_llcp_get_remote_config;
3317 nfc_funcs->llcp_get_remote_socket_info = net_nfc_emul_controller_llcp_get_remote_socket_info;
3319 nfc_funcs->support_nfc = net_nfc_emul_controller_support_nfc;
3321 nfc_funcs->secure_element_open = net_nfc_emul_controller_secure_element_open;
3322 nfc_funcs->secure_element_get_atr = net_nfc_emul_controller_secure_element_get_atr;
3323 nfc_funcs->secure_element_send_apdu = net_nfc_emul_controller_secure_element_send_apdu;
3324 nfc_funcs->secure_element_close = net_nfc_emul_controller_secure_element_close;
3326 nfc_funcs->hce_response_apdu = NULL;
3327 nfc_funcs->secure_element_route_aid = NULL;
3328 nfc_funcs->secure_element_unroute_aid = NULL;
3329 nfc_funcs->secure_element_commit_routing = NULL;
3330 nfc_funcs->secure_element_set_default_route = NULL;
3331 nfc_funcs->secure_element_clear_aid_table = NULL;
3332 nfc_funcs->secure_element_get_aid_table_size = NULL;
3336 *data = (void *)nfc_funcs;
3341 static int nfc_backend_exit(void *data)
3350 EXPORT hal_backend hal_backend_nfc_data = {
3351 .name = "nfc-backend",
3352 .vendor = "Emulator",
3353 .abi_version = HAL_ABI_VERSION_TIZEN_6_5,
3354 .init = nfc_backend_init,
3355 .exit = nfc_backend_exit,