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 <net_nfc_oem_controller.h>
30 #include <net_nfc_typedef.h>
31 #include <net_nfc_util_internal.h>
32 #include <net_nfc_util_ndef_message.h>
33 #include <net_nfc_util_ndef_record.h>
34 #include "nfc_debug_private.h"
37 EMUL_NFC_TAG_DISCOVERED = 100,
38 EMUL_NFC_TAG_DETACHED,
39 EMUL_NFC_P2P_DISCOVERED,
40 EMUL_NFC_P2P_DETACHED,
46 EMUL_TAG_TOPAZ_JEWEL = 1,
49 EMUL_TAG_MIFARE_DESFIRE,
55 EMUL_NDEF_TNF_EMPTY = 0,
56 EMUL_NDEF_TNF_WELL_KNOWN,
57 EMUL_NDEF_TNF_MIME_MEDIA,
58 EMUL_NDEF_TNF_ABSOLUTE_URI,
59 EMUL_NDEF_TNF_EXTERNAL,
61 } emul_ndef_name_format;
64 NET_NFC_STATE_EXCHANGER_SERVER = 0x00,
65 NET_NFC_STATE_EXCHANGER_SERVER_NPP,
67 NET_NFC_STATE_EXCHANGER_CLIENT,
68 NET_NFC_STATE_CONN_HANDOVER_REQUEST,
69 NET_NFC_STATE_CONN_HANDOVER_SELECT,
74 SNEP_REQ_CONTINUE = 0x00,
77 SNEP_REQ_REJECT = 0x7F,
78 SNEP_RESP_CONT = 0x80,
79 SNEP_RESP_SUCCESS = 0x81,
80 SNEP_RESP_NOT_FOUND = 0xC0,
81 SNEP_RESP_EXCESS_DATA = 0xC1,
82 SNEP_RESP_BAD_REQ = 0xC2,
83 SNEP_RESP_NOT_IMPLEMENT = 0xE0,
84 SNEP_RESP_UNSUPPORTED_VER = 0xE1,
85 SNEP_RESP_REJECT = 0xFF,
86 } snep_command_field_e;
88 typedef struct _socket_info_s {
89 net_nfc_llcp_socket_t socket_handle;
94 typedef struct _net_nfc_oem_llcp_state_t {
97 unsigned int fragment_offset;
99 net_nfc_llcp_socket_t socket;
100 uint16_t max_capability;
101 net_nfc_target_handle_s *handle;
102 net_nfc_error_e prev_result;
103 net_nfc_llcp_socket_t incomming_socket;
104 ndef_message_s *requester;
105 ndef_message_s *selector;
110 llcp_app_protocol_e type_app_protocol;
111 net_nfc_conn_handover_carrier_type_e type;
113 } net_nfc_oem_llcp_state_t;
115 typedef struct _snep_msg_s {
120 /* Members below are used for snep msg of client */
125 typedef struct _emulMsg_data_s {
126 net_nfc_record_tnf_e tnf;
134 typedef struct _emulMsg_s {
135 emul_message_id message_id;
136 emul_target_type target_type;
141 typedef void* (*emul_Nfc_thread_handler_t)(void *pParam);
143 #define __USE_EPOLL_FOR_FILE__ 1
145 /* for emulator management */
146 #define NET_NFC_EMUL_DATA_PATH "/dev"
147 #define NET_NFC_EMUL_MESSAGE_FILE_NAME "nfc0"
148 #define NET_NFC_EMUL_MSG_ID_SEPERATOR "\n:"
149 #define NET_NFC_EMUL_MSG_DATA_SEPERATOR "\n\0"
150 #define NET_NFC_EMUL_MSG_RECORD_SEPERATOR "\n,"
151 #define NET_NFC_EMUL_TAG_DISCOVERED_DATA_FORMAT "%d,%d,%[^\n]"
152 #define NET_NFC_EMUL_HEADER_LENGTH 8
154 #ifdef __USE_EPOLL_FOR_FILE__
155 #define EPOLL_SIZE 128
158 /* for llcp functionality */
159 #define LLCP_NB_SOCKET_MAX 5
160 #define NET_NFC_EMUL_SNEP_SERVER_SOCKET_NUMBER 0
161 #define NET_NFC_EMUL_INCOMING_SOCKET_NUMBER 0
162 #define NET_NFC_EMUL_NPP_SERVER_SOCKET_NUMBER 1
163 #define NET_NFC_EMUL_CLIENT_SOCKET_NUMBER 2
164 #define NET_NFC_EMUL_HANDOVER_REQUEST_SOCKET_NUMBER 3
165 #define NET_NFC_EMUL_HANDOVER_SELECT_SOCKET_NUMBER 4
167 #define SNEP_MAX_BUFFER 128
168 #define SNEP_MAJOR_VER 1
169 #define SNEP_MINOR_VER 0
171 /* static variable */
172 #define BUFFER_LENGTH_MAX 4096
173 #define READ_BUFFER_LENGTH_MAX BUFFER_LENGTH_MAX
174 #define WRITE_BUFFER_LENGTH_MAX BUFFER_LENGTH_MAX
175 #define NET_NFC_MAX_LLCP_SOCKET_BUFFER BUFFER_LENGTH_MAX
177 /* listener callback */
178 static target_detection_listener_cb g_emul_controller_target_cb;
179 static se_transaction_listener_cb g_emul_controller_se_cb;
180 static llcp_event_listener_cb g_emul_controller_llcp_cb;
181 static hce_apdu_listener_cb g_emul_controller_hce_cb;
183 /* for emulator management */
184 pthread_t gEmulThread;
186 /* for stack management */
187 static net_nfc_target_handle_s *current_working_handle = NULL;
188 static bool g_stack_init_successful = 0;
189 static bool g_tag_attached = 0;
190 static bool g_p2p_attached = 0;
191 static int current_connection_id = 1;
192 /* for llcp functionality */
194 socket_info_s socket_info_array[LLCP_NB_SOCKET_MAX] = { {0, } };
196 snep_msg_s *Snep_Server_msg;
197 data_s *llcp_server_data = NULL;
199 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
203 /* free memory, after free given memory it set NULL. Before proceed free, this function also check NULL */
204 static void __nfc_emul_util_free_mem(void **mem, char *filename, unsigned int line);
205 /* allocation memory */
206 static void __nfc_emul_util_alloc_mem(void **mem, int size, char *filename, unsigned int line);
207 #define _nfc_emul_util_alloc_mem(mem, size) __nfc_emul_util_alloc_mem((void**)&mem, size, __FILE__, __LINE__)
208 #define _nfc_emul_util_free_mem(mem) __nfc_emul_util_free_mem((void**)&mem, __FILE__, __LINE__)
211 static void __nfc_emul_util_free_mem(void **mem, char *filename, unsigned int line)
213 if (mem == NULL || *mem == NULL) {
214 LOGE("FILE: %s, LINE:%d, Invalid parameter in mem free util (pinter is NULL)", filename, line);
221 static void __nfc_emul_util_alloc_mem(void **mem, int size, char *filename, unsigned int line)
223 if (mem == NULL || size <= 0) {
224 LOGE("FILE: %s, LINE:%d, Invalid parameter in mem alloc util", filename, line);
228 DEBUG_MSG("size to malloc() = [%d]", size);
231 LOGE("FILE: %s, LINE:%d, WARNING: Pointer is already allocated or it was not initialized with NULL", filename, line);
236 memset(*mem, 0x0, size);
238 LOGE("FILE: %s, LINE:%d, Allocation is failed", filename, line);
241 static bool __net_nfc_is_valide_target_handle(net_nfc_target_handle_s *handle)
243 bool result = (current_working_handle == handle);
245 DEBUG_MSG("[WARNING]: INVALID HANDLE IS DETECTED!");
250 static void __net_nfc_make_valid_target_handle(net_nfc_target_handle_s **handle)
252 if (current_working_handle != NULL)
253 DEBUG_MSG("[WARNING]: HANDLE WAS ALLOCATED ALREADY!");
255 _nfc_emul_util_alloc_mem(*handle, sizeof(net_nfc_target_handle_s));
257 current_working_handle = *handle;
260 static void __net_nfc_make_invalid_target_handle()
262 if (current_working_handle != NULL) {
263 _nfc_emul_util_free_mem(current_working_handle);
264 current_working_handle = NULL;
268 static socket_info_s* _net_nfc_get_available_socket_slot()
271 for (; idx < LLCP_NB_SOCKET_MAX; idx++) {
272 if (socket_info_array[idx].isValid == false) {
273 memset(&(socket_info_array[idx]), 0x00, sizeof(socket_info_s));
274 socket_info_array[idx].isValid = true;
275 return &(socket_info_array[idx]);
279 DEBUG_ERR_MSG("_net_nfc_get_available_socket_slot is failed");
283 static void _net_nfc_remove_socket_slot(net_nfc_llcp_socket_t socket)
287 for (; idx < LLCP_NB_SOCKET_MAX; idx++) {
288 if (socket_info_array[idx].isValid == true &&
289 socket_info_array[idx].socket_handle == socket) {
290 socket_info_array[idx].isValid = false;
291 socket_info_array[idx].socket_handle = 0;
292 socket_info_array[idx].user_context = NULL;
297 static socket_info_s* _net_nfc_find_server_socket(net_nfc_llcp_socket_t socket)
300 for (; idx < LLCP_NB_SOCKET_MAX; idx++) {
301 if (socket_info_array[idx].socket_handle == socket && socket_info_array[idx].isValid == true)
302 return &(socket_info_array[idx]);
305 DEBUG_ERR_MSG("_net_nfc_find_server_socket is failed");
309 static void _net_nfc_initialize_llcp(void)
313 if (Snep_Server_msg == NULL) {
314 Snep_Server_msg = calloc(1, sizeof(snep_msg_s));
315 if (Snep_Server_msg == NULL)
318 Snep_Server_msg->data = calloc(1, sizeof(data_s));
319 if (Snep_Server_msg->data == NULL)
322 Snep_Server_msg->data->buffer = calloc(1, sizeof(uint8_t) * BUFFER_LENGTH_MAX);
328 static void _net_nfc_deinitialize_llcp(void)
332 if (Snep_Server_msg != NULL) {
333 if (Snep_Server_msg->data != NULL) {
334 if (Snep_Server_msg->data->buffer != NULL) {
335 free(Snep_Server_msg->data->buffer);
336 Snep_Server_msg->data->buffer = NULL;
339 free(Snep_Server_msg->data);
340 Snep_Server_msg->data = NULL;
343 free(Snep_Server_msg);
344 Snep_Server_msg = NULL;
347 llcp_server_data = NULL;
352 static bool _net_nfc_emul_get_is_tag_attached(void)
354 return g_tag_attached;
357 static void _net_nfc_emul_set_is_tag_attached(bool is_detached)
359 g_tag_attached = is_detached;
360 DEBUG_MSG("set g_tag_attached [%d]", g_tag_attached);
363 static bool _net_nfc_emul_get_is_p2p_attached(void)
365 return g_p2p_attached;
368 static void _net_nfc_emul_set_is_p2p_attached(bool is_detached)
370 g_p2p_attached = is_detached;
371 DEBUG_MSG("set g_p2p_attached [%d]", g_p2p_attached);
374 static bool _net_nfc_is_data_emulMsgData(emul_message_id messageId)
381 case EMUL_NFC_TAG_DISCOVERED:
385 case EMUL_NFC_P2P_SEND:
389 case EMUL_NFC_TAG_DETACHED:
390 case EMUL_NFC_P2P_DISCOVERED:
391 case EMUL_NFC_P2P_DETACHED:
397 DEBUG_MSG("retval [%d]", retval);
404 static net_nfc_record_tnf_e _net_nfc_get_tnf_type(int name_format)
408 net_nfc_record_tnf_e tnf = NET_NFC_RECORD_EMPTY;
410 switch (name_format) {
411 case EMUL_NDEF_TNF_EMPTY:
412 tnf = NET_NFC_RECORD_EMPTY;
415 case EMUL_NDEF_TNF_WELL_KNOWN:
416 tnf = NET_NFC_RECORD_WELL_KNOWN_TYPE;
419 case EMUL_NDEF_TNF_MIME_MEDIA:
420 tnf = NET_NFC_RECORD_MIME_TYPE;
423 case EMUL_NDEF_TNF_ABSOLUTE_URI:
424 tnf = NET_NFC_RECORD_URI;
427 case EMUL_NDEF_TNF_EXTERNAL:
428 tnf = NET_NFC_RECORD_EXTERNAL_RTD;
431 case EMUL_NDEF_TNF_UNKNOWN:
432 tnf = NET_NFC_RECORD_UNKNOWN;
436 tnf = NET_NFC_RECORD_UNKNOWN;
437 DEBUG_MSG("data is currupted");
441 DEBUG_MSG("tnf [%d]", tnf);
449 static int _net_nfc_create_records_from_emulMsg(emulMsg_s *emul_msg, ndef_message_s **ndef_message, int record_count)
454 int create_record_count = 0;
455 char emulMsg[BUFFER_LENGTH_MAX] = {0};
457 memcpy(emulMsg, emul_msg->file_data, sizeof(emulMsg) - 1);
459 /* parsing data and create record to record structure */
460 for (index = 0; index < record_count; index++) {
464 char *record_payload;
466 emulMsg_data_s record = {0};
467 data_s filePayload = {NULL, 0};
471 name_format = strtok((char *)emulMsg, NET_NFC_EMUL_MSG_RECORD_SEPERATOR);
473 name_format = strtok(NULL, NET_NFC_EMUL_MSG_RECORD_SEPERATOR);
475 type_name = strtok(NULL, NET_NFC_EMUL_MSG_RECORD_SEPERATOR);
476 record_id = strtok(NULL, NET_NFC_EMUL_MSG_RECORD_SEPERATOR);
477 if (index == record_count - 1) {
478 /* the last payload : we have to read sentence fully */
479 record_payload = strtok(NULL, "\n");
481 record_payload = strtok(NULL, NET_NFC_EMUL_MSG_RECORD_SEPERATOR);
484 /* assign data to record structure */
485 record.tnf = _net_nfc_get_tnf_type(atoi(name_format));
487 DEBUG_MSG("Data : type_name ");
489 record.typeName.length = strlen(type_name);
490 _nfc_emul_util_alloc_mem(record.typeName.buffer, record.typeName.length + 1);
492 if (record.typeName.buffer == NULL) {
493 DEBUG_MSG("_nfc_emul_util_alloc_mem failed");
496 memcpy(record.typeName.buffer, type_name, sizeof(record.typeName.buffer) - 1);
498 DEBUG_MSG("Data : record_id ");
500 record.id.length = strlen(record_id);
501 _nfc_emul_util_alloc_mem(record.id.buffer, record.id.length + 1);
503 if (record.id.buffer == NULL) {
504 DEBUG_MSG("_nfc_emul_util_alloc_mem failed");
507 memcpy(record.id.buffer, record_id, sizeof(record.id.buffer) - 1);
509 DEBUG_MSG("Data : record_payload ");
511 record.payload.length = strlen(record_payload);
512 _nfc_emul_util_alloc_mem(record.payload.buffer, record.payload.length + 1);
514 if (record.payload.buffer == NULL) {
515 DEBUG_MSG("_nfc_emul_util_alloc_mem failed");
518 memcpy(record.payload.buffer, record_payload, sizeof(record.payload.buffer) - 1);
520 #ifndef __EMUL_DEBUG__
521 DEBUG_ERR_MSG("RECORD DATA START >>>>>>>>>>>>>>>>>>>>>>>>");
522 DEBUG_MSG("TNF >>>>[%d]", record.tnf);
523 LOGE("type_name >>>>[%s]", type_name);
524 LOGE("record_id >>>>[%s]", record_id);
525 DEBUG_MSG("record_payload >>>>[%s]", record_payload);
526 DEBUG_ERR_MSG("RECORD DATA END >>>>>>>>>>>>>>>>>>>>>>>>");
530 ndef_record_h new_record = NULL;
531 net_nfc_error_e result = NET_NFC_OK;
533 if (record.tnf == NET_NFC_RECORD_EMPTY) {
534 if ((result = net_nfc_util_create_record(NET_NFC_RECORD_EMPTY, &record.typeName, &record.id, &record.payload, (ndef_record_s **) &new_record)) != NET_NFC_OK) {
535 DEBUG_MSG("net_nfc_create_record failed[%d]", result);
538 } else if (record.tnf == NET_NFC_RECORD_UNKNOWN) {
539 if ((result = net_nfc_util_create_record(NET_NFC_RECORD_UNKNOWN, &record.typeName, &record.id, &record.payload, (ndef_record_s **) &new_record)) != NET_NFC_OK) {
540 DEBUG_MSG("net_nfc_create_record failed[%d]", result);
543 } else if (record.tnf == NET_NFC_RECORD_WELL_KNOWN_TYPE) {
544 if (!strncmp((char *)record.typeName.buffer, "U", 1)) {
545 DEBUG_MSG("URI Type ");
547 data_s payload_data = { NULL, 0 };
549 if (record.payload.buffer != NULL) {
550 payload_data.length = strlen((char *)record_payload) + 1;
552 _nfc_emul_util_alloc_mem(payload_data.buffer, payload_data.length);
553 if (payload_data.buffer == NULL) {
554 DEBUG_MSG("_nfc_emul_util_alloc_mem failed");
558 payload_data.buffer[0] = NET_NFC_SCHEMA_FULL_URI; /* first byte of payload is protocol scheme */
559 memcpy(payload_data.buffer + 1, record.payload.buffer, payload_data.length - 1);
562 if (net_nfc_util_create_record(record.tnf, &record.typeName, &record.id, &payload_data, (ndef_record_s**) &new_record) != NET_NFC_OK) {
563 DEBUG_ERR_MSG("net_nfc_util_create_record is failed");
567 if (payload_data.buffer != NULL)
568 _nfc_emul_util_free_mem(payload_data.buffer);
569 } else if (!strncmp((char *)record.typeName.buffer, "T", 1)) {
570 DEBUG_MSG("TEXT Type ");
572 data_s payload_data = { NULL, 0 };
576 if (record.payload.buffer != NULL) {
577 payload_data.length = strlen((char *)record_payload) + strlen("en-US") + 1;
579 _nfc_emul_util_alloc_mem(payload_data.buffer, payload_data.length);
580 if (payload_data.buffer == NULL) {
581 DEBUG_MSG("_nfc_emul_util_alloc_mem failed");
585 controll_byte = strlen("en-US") & 0x3F;
587 payload_data.buffer[0] = controll_byte;
590 memcpy(payload_data.buffer + offset, "en-US", strlen("en-US"));
592 offset = offset + strlen("en-US");
593 memcpy(payload_data.buffer + offset, record.payload.buffer, strlen(record_payload));
596 if (net_nfc_util_create_record(record.tnf, &record.typeName, &record.id, &payload_data, (ndef_record_s**) &new_record) != NET_NFC_OK) {
597 DEBUG_ERR_MSG("net_nfc_util_create_record is failed");
601 if (payload_data.buffer != NULL)
602 _nfc_emul_util_free_mem(payload_data.buffer);
604 DEBUG_ERR_MSG("NET_NFC_RECORD_WELL_KNOWN_TYPE >> typeName is wrong");
607 } else if ((record.tnf == NET_NFC_RECORD_MIME_TYPE)) {
610 /* open file : size limit? 10k? */
611 file = fopen(record_payload, "r");
613 long int file_len = 0, read_count = 0, read_total = 0;
614 uint8_t *file_data = NULL;
616 fseek(file, 0, SEEK_END);
617 file_len = ftell(file);
618 fseek(file, 0, SEEK_SET);
620 _nfc_emul_util_alloc_mem(file_data, file_len);
622 if (file_data == NULL) {
623 DEBUG_MSG("_nfc_emul_util_alloc_mem failed");
630 read_count = fread(file_data + read_total, 1, file_len - read_total, file);
631 read_total += read_count;
632 } while (read_count != 0 && read_total < file_len);
636 DEBUG_MSG("fread(%s) success, size %ld", record.payload.buffer, file_len);
638 filePayload.length = file_len;
639 _nfc_emul_util_alloc_mem(filePayload.buffer, filePayload.length);
641 if (filePayload.buffer == NULL) {
642 DEBUG_MSG("_nfc_emul_util_alloc_mem failed");
643 _nfc_emul_util_free_mem(file_data);
646 memcpy(filePayload.buffer, file_data, filePayload.length);
648 _nfc_emul_util_free_mem(file_data);
651 if (record.id.buffer == NULL) {
652 char *file_name = NULL;
654 file_name = strrchr(record_payload, '/');
655 if (file_name == NULL)
656 file_name = (char *) record_payload;
660 record.id.length = strlen(file_name);
661 _nfc_emul_util_alloc_mem(record.id.buffer, record.id.length + 1);
663 if (record.id.buffer == NULL) {
664 DEBUG_MSG("_nfc_emul_util_alloc_mem failed");
665 _nfc_emul_util_free_mem(filePayload.buffer);
668 memcpy(record.id.buffer, file_name, sizeof(record.id.buffer) - 1);
671 DEBUG_MSG("file open error");
676 if ((result = net_nfc_util_create_record(record.tnf, &record.typeName, &record.id, &filePayload, (ndef_record_s **) &new_record)) != NET_NFC_OK) {
677 DEBUG_MSG("net_nfc_create_record failed[%d]", result);
681 /* NET_NFC_RECORD_URI or NET_NFC_RECORD_EXTERNAL_RTD */
682 if ((result = net_nfc_util_create_record(record.tnf, &record.typeName, &record.id, &record.payload, (ndef_record_s **) &new_record)) != NET_NFC_OK) {
683 DEBUG_MSG("net_nfc_create_record failed[%d]", result);
688 /* append record to ndef msg */
689 if ((result = net_nfc_util_append_record((ndef_message_s*) *ndef_message, (ndef_record_s *) new_record)) != NET_NFC_OK) {
690 DEBUG_MSG("net_nfc_util_append_record failed[%d]", result);
694 create_record_count++;
695 DEBUG_MSG("Create Record Sucess. Create Record Count[%d]", create_record_count);
700 if (record.typeName.buffer != NULL)
701 _nfc_emul_util_free_mem(record.typeName.buffer);
703 if (record.id.buffer != NULL)
704 _nfc_emul_util_free_mem(record.id.buffer);
706 if (record.payload.buffer != NULL)
707 _nfc_emul_util_free_mem(record.payload.buffer);
709 if (filePayload.buffer != NULL)
710 _nfc_emul_util_free_mem(filePayload.buffer);
712 DEBUG_MSG("Create Record Loop End");
718 return create_record_count;
721 static bool _net_nfc_change_file_owner_permission(FILE *file)
724 size_t buffer_len = 0;
725 struct passwd pwd = {0};
726 struct passwd *pw_inhouse = NULL;
727 struct group grp = {0};
728 struct group *gr_inhouse = NULL;
733 /* change permission */
734 fchmod(fileno(file), 0644);
738 buffer_len = sysconf(_SC_GETPW_R_SIZE_MAX);
739 if (buffer_len == -1)
742 _net_nfc_util_alloc_mem(buffer, buffer_len);
746 getpwnam_r("inhouse", &pwd, buffer, buffer_len, &pw_inhouse);
747 _net_nfc_util_free_mem(buffer);
750 buffer_len = sysconf(_SC_GETGR_R_SIZE_MAX);
751 if (buffer_len == -1)
754 _net_nfc_util_alloc_mem(buffer, buffer_len);
758 getgrnam_r("inhouse", &grp, buffer, buffer_len, &gr_inhouse);
759 _net_nfc_util_free_mem(buffer);
761 if ((pw_inhouse != NULL) && (gr_inhouse != NULL)) {
762 if (fchown(fileno(file), pw_inhouse->pw_uid, gr_inhouse->gr_gid) < 0)
763 DEBUG_MSG("failed to change owner");
769 static void _net_nfc_store_ndef_message(data_s *data)
771 char file_name[1024] = {0};
778 snprintf(file_name, sizeof(file_name), "%s/%s/%s", "/opt/usr/share/nfc-manager-daemon",
779 "message", "emul-ndef-message.txt");
783 if ((fp = fopen(file_name, "w")) != NULL) {
786 if ((length = fwrite(data->buffer, 1, data->length, fp)) > 0) {
787 DEBUG_MSG("[%d] bytes is written", length);
789 _net_nfc_change_file_owner_permission(fp);
793 DEBUG_MSG("write is failed = [%d]", data->length);
800 static net_nfc_error_e _net_nfc_retrieve_ndef_message(data_s *data)
802 char file_name[1024] = {0};
804 net_nfc_error_e result = NET_NFC_OK;
807 return NET_NFC_NULL_PARAMETER;
810 snprintf(file_name, sizeof(file_name), "%s/%s/%s", "/opt/usr/share/nfc-manager-daemon",
811 "message", "emul-ndef-message.txt");
813 if ((fp = fopen(file_name, "r")) != NULL) {
816 /* rewind to start of file */
817 fseek(fp, 0, SEEK_END);
819 fseek(fp, 0, SEEK_SET);
821 DEBUG_MSG("message length = [%ld]", size);
824 if (net_nfc_util_init_data(data, size) == true) {
830 current = fread(data->buffer + offset, 1,
831 data->length - offset, fp);
836 } while (offset < data->length);
838 if (offset == data->length)
841 result = NET_NFC_NO_NDEF_MESSAGE;
843 result = NET_NFC_ALLOC_FAIL;
846 result = NET_NFC_OPERATION_FAIL;
851 result = NET_NFC_NO_DATA_FOUND;
857 static bool _net_nfc_create_ndef_from_emulMsg(emulMsg_s *emul_msg)
861 if (emul_msg->file_data == NULL)
865 net_nfc_error_e result = NET_NFC_OK;
866 data_s rawdata = {NULL, 0};
868 int record_count = emul_msg->record_count;
870 if (record_count == 0)
873 /* create ndef msg */
874 ndef_message_h ndef_message = NULL;
877 if ((result = net_nfc_util_create_ndef_message((ndef_message_s **)&ndef_message)) != NET_NFC_OK)
878 DEBUG_MSG("failed to create ndef message [%d]", result);
880 /* create records and append it to ndef_msg*/
881 emul_msg->record_count = _net_nfc_create_records_from_emulMsg(emul_msg, (ndef_message_s **)&ndef_message, record_count);
883 /* convert ndef msg to raw data */
884 ndef_length = net_nfc_util_get_ndef_message_length((ndef_message_s *)ndef_message);
887 DEBUG_MSG("ndef_message size is zero!");
889 rawdata.length = ndef_length;
890 _nfc_emul_util_alloc_mem(rawdata.buffer, ndef_length);
892 if ((result = net_nfc_util_convert_ndef_message_to_rawdata((ndef_message_s*)ndef_message, &rawdata)) != NET_NFC_OK)
893 DEBUG_MSG("net_nfc_util_convert_ndef_message_to_rawdata is failed![%d]", result);
895 _net_nfc_store_ndef_message(&rawdata);
897 _nfc_emul_util_free_mem(rawdata.buffer);
898 net_nfc_util_free_ndef_message(ndef_message);
905 static bool _net_nfc_create_emulMsg(emulMsg_s **emul_msg, uint8_t *data, long int size)
912 *emul_msg = calloc(1, sizeof(emulMsg_s));
913 if (*emul_msg == NULL)
916 /* emulData => ID : MSG ex) 100:1,1,1,U,samsung,http://www.naver.com */
917 emulMsgID = strtok((char *)data, NET_NFC_EMUL_MSG_ID_SEPERATOR);
918 if (emulMsgID != NULL)
919 (*emul_msg)->message_id = (emul_message_id)(atoi(emulMsgID));
921 DEBUG_MSG("emul_msg->message_id >>>>[%d]", (*emul_msg)->message_id);
923 if (_net_nfc_is_data_emulMsgData((*emul_msg)->message_id)) {
925 emulMsgData = strtok(NULL, NET_NFC_EMUL_MSG_DATA_SEPERATOR);
926 DEBUG_MSG("emulMsgData >>>>[%s]", emulMsgData);
928 switch ((*emul_msg)->message_id) {
929 case EMUL_NFC_TAG_DISCOVERED:
930 case EMUL_NFC_P2P_SEND:
932 /* get message : Tag Type, Record Count, Records */
933 int target_type = -1;
934 char file_data[BUFFER_LENGTH_MAX] = {0};
937 sscanf(emulMsgData, NET_NFC_EMUL_TAG_DISCOVERED_DATA_FORMAT, &target_type, &((*emul_msg)->record_count), file_data);
939 (*emul_msg)->target_type = (emul_target_type)target_type;
941 length = strlen(file_data) + 1;
942 _nfc_emul_util_alloc_mem((*emul_msg)->file_data, length);
943 memcpy((*emul_msg)->file_data, file_data, length);
945 DEBUG_ERR_MSG("EMUL MESSAGE DATA START >>>>>>>>>>>>>>>>>>>>>>>>");
946 DEBUG_MSG("message_id >>>>[%d]", (*emul_msg)->message_id);
947 DEBUG_MSG("target_type >>>>[%d]", (*emul_msg)->target_type);
948 DEBUG_MSG("record_count >>>>[%d]", (*emul_msg)->record_count);
949 DEBUG_MSG("file_data >>>>[%s]", (char *)(*emul_msg)->file_data);
950 DEBUG_ERR_MSG("EMUL MESSAGE DATA END >>>>>>>>>>>>>>>>>>>>>>>>");
952 if (!_net_nfc_create_ndef_from_emulMsg((*emul_msg)))
953 DEBUG_ERR_MSG("read ndef_msg is failed >>>");
955 DEBUG_ERR_MSG("_net_nfc_create_ndef_from_emulMsg end");
961 DEBUG_ERR_MSG("_net_nfc_set_emulMsg error. Data is currupted");
966 switch ((*emul_msg)->message_id) {
967 case EMUL_NFC_P2P_DISCOVERED:
968 (*emul_msg)->target_type = EMUL_NFC_TARGET;
971 case EMUL_NFC_TAG_DETACHED:
972 DEBUG_MSG("TAG DETACHED");
973 if (!_net_nfc_emul_get_is_tag_attached()) {
974 DEBUG_ERR_MSG("tag is not attached!!");
979 case EMUL_NFC_P2P_DETACHED:
980 DEBUG_MSG("P2P DETACHED");
981 if (!_net_nfc_emul_get_is_p2p_attached()) {
982 DEBUG_ERR_MSG("tag is not attached!!");
989 DEBUG_ERR_MSG("_net_nfc_set_emulMsg error. Data is currupted");
1000 static void _net_nfc_destroy_emulMsg(emulMsg_s *emul_msg)
1004 if (emul_msg != NULL) {
1005 if (emul_msg->file_data != NULL)
1006 free(emul_msg->file_data);
1014 static int _net_nfc_emul_convert_target_type(emul_target_type targetType)
1020 switch (targetType) {
1021 case EMUL_TAG_TOPAZ_JEWEL:
1022 covert = NET_NFC_JEWEL_PICC;
1025 case EMUL_TAG_MIFARE_UL:
1026 covert = NET_NFC_MIFARE_ULTRA_PICC;
1029 case EMUL_TAG_FELICA:
1030 covert = NET_NFC_FELICA_PICC;
1033 case EMUL_TAG_MIFARE_DESFIRE:
1034 covert = NET_NFC_MIFARE_DESFIRE_PICC;
1037 case EMUL_NFC_TARGET:
1038 covert = NET_NFC_NFCIP1_TARGET;
1041 case EMUL_TARGET_TYPE_MAX:
1043 /* exception case */
1044 DEBUG_ERR_MSG("_net_nfc_emul_convert_target_type error. Target type is unknown");
1048 DEBUG_MSG("covert [%d]", covert);
1055 static void _net_nfc_target_discovered_cb(emulMsg_s *emul_msg)
1060 net_nfc_target_handle_s *handle = NULL;
1063 __net_nfc_make_valid_target_handle(&handle);
1068 net_nfc_request_target_detected_t *target_detected = NULL;
1069 uint8_t device_info[] = {0x03, 0x55, 0x49, 0x44, 0x07, 0x04, 0x93, 0xB7, 0xD9, 0x5B, 0x02, 0x80, \
1070 0x08, 0x41, 0x50, 0x50, 0x5F, 0x44, 0x41, 0x54, 0x41, 0x00, 0x03, 0x53, 0x41, 0x4B, \
1071 0x01, 0x00, 0x04, 0x41, 0x54, 0x51, 0x41, 0x02, 0x44, 0x00, 0x0D, 0x4D, 0x41, 0x58, \
1072 0x5F, 0x44, 0x41, 0x54, 0x41, 0x5F, 0x52, 0x41, 0x54, 0x45, 0x01, 0x00, 0x08, 0x46, \
1073 0x57, 0x49, 0x5F, 0x53, 0x46, 0x47, 0x54, 0x01, 0x03, 0x49, 0x44, 0x6D, 0x07, 0x04, \
1074 0x93, 0xB7, 0xD9, 0x5B, 0x02, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
1077 length = sizeof(net_nfc_request_target_detected_t) + sizeof(device_info);
1078 _nfc_emul_util_alloc_mem(target_detected, length);
1079 if (target_detected == NULL)
1082 target_detected->length = length;
1083 target_detected->request_type = NET_NFC_MESSAGE_SERVICE_STANDALONE_TARGET_DETECTED;
1084 target_detected->handle = handle;
1086 target_detected->devType = _net_nfc_emul_convert_target_type(emul_msg->target_type);
1087 if (!target_detected->devType) {
1088 DEBUG_MSG("target_detected->devType is unknown");
1089 _nfc_emul_util_free_mem(target_detected);
1093 target_detected->handle->connection_id = current_connection_id++;
1095 if (target_detected->devType == NET_NFC_NFCIP1_TARGET) {
1096 DEBUG_MSG("set llcp connection type. remote device is target");
1097 handle->connection_type = NET_NFC_P2P_CONNECTION_TARGET;
1098 _net_nfc_emul_set_is_p2p_attached(true);
1099 } else if (target_detected->devType == NET_NFC_NFCIP1_INITIATOR) {
1100 DEBUG_MSG("set llcp connection type. remote device is initiator");
1101 handle->connection_type = NET_NFC_P2P_CONNECTION_INITIATOR;
1102 _net_nfc_emul_set_is_p2p_attached(true);
1104 DEBUG_MSG("set tag connection");
1105 handle->connection_type = NET_NFC_TAG_CONNECTION;
1106 _net_nfc_emul_set_is_tag_attached(true);
1109 target_detected->number_of_keys = 7;
1110 target_detected->target_info_values.length = sizeof(device_info);
1111 memcpy(&target_detected->target_info_values.buffer, device_info, target_detected->target_info_values.length);
1113 /* call target_cb */
1114 if (g_emul_controller_target_cb != NULL) {
1115 DEBUG_MSG("discovered callback is called");
1116 g_emul_controller_target_cb(target_detected, NULL);
1121 static void _net_nfc_tag_detached_cb(void)
1125 _net_nfc_emul_set_is_tag_attached(false);
1130 static void _net_nfc_target_detached_cb(void)
1134 _net_nfc_emul_set_is_p2p_attached(false);
1136 /* For P2P, we send msg to manager */
1137 net_nfc_request_llcp_msg_t *req_msg = NULL;
1139 _nfc_emul_util_alloc_mem(req_msg, sizeof(net_nfc_request_llcp_msg_t));
1141 if (req_msg != NULL) {
1142 req_msg->length = sizeof(net_nfc_request_llcp_msg_t);
1143 req_msg->request_type = NET_NFC_MESSAGE_SERVICE_LLCP_DEACTIVATED;
1145 DEBUG_MSG("deactivated callback is called");
1146 g_emul_controller_llcp_cb(req_msg, NULL);
1150 static void _net_nfc_llcp_data_receive_cb(void *pContext)
1154 net_nfc_request_llcp_msg_t *req_msg = NULL;
1156 _nfc_emul_util_alloc_mem(req_msg, sizeof(net_nfc_request_llcp_msg_t));
1158 if (req_msg != NULL) {
1159 req_msg->length = sizeof(net_nfc_request_llcp_msg_t);
1160 req_msg->request_type = NET_NFC_MESSAGE_SERVICE_LLCP_RECEIVE;
1161 req_msg->result = NET_NFC_OK;
1163 DEBUG_MSG("receive callback is called >>");
1164 g_emul_controller_llcp_cb(req_msg, pContext);
1170 static void _net_nfc_llcp_data_receive_from_cb(void *pContext)
1174 net_nfc_request_llcp_msg_t *req_msg = NULL;
1176 _nfc_emul_util_alloc_mem(req_msg, sizeof(net_nfc_request_llcp_msg_t));
1178 if (req_msg != NULL) {
1179 req_msg->length = sizeof(net_nfc_request_llcp_msg_t);
1180 req_msg->request_type = NET_NFC_MESSAGE_SERVICE_LLCP_RECEIVE_FROM;
1181 req_msg->result = NET_NFC_OK;
1183 DEBUG_MSG("receive_from callback is called >>");
1184 g_emul_controller_llcp_cb(req_msg, pContext);
1190 static void _net_nfc_llcp_create_snep_server_msg(snep_command_field_e resp_field)
1194 uint8_t *temp = Snep_Server_msg->data->buffer;
1195 uint8_t response = (uint8_t)resp_field;
1196 uint8_t version = 0;
1197 uint32_t length_field = 0;
1198 data_s rawdata = {NULL, 0};
1200 version = SNEP_MAJOR_VER;
1201 version = (((version << 4) & 0xf0) | (SNEP_MINOR_VER & 0x0f));
1203 _net_nfc_retrieve_ndef_message(&rawdata);
1205 /* version response length payload*/
1206 Snep_Server_msg->data->length = sizeof(uint8_t) + sizeof(uint8_t) + sizeof(uint32_t) + rawdata.length;
1216 length_field = htonl(rawdata.length);
1218 /* length will be se 0. so we don't need to copy value */
1219 memcpy(temp, &length_field, sizeof(uint32_t));
1220 temp += sizeof(uint32_t);
1222 /* copy ndef information to response msg */
1223 if (rawdata.length > 0)
1224 memcpy(temp, rawdata.buffer, rawdata.length);
1226 _nfc_emul_util_free_mem(rawdata.buffer);
1231 static llcp_state_e _net_nfc_get_llcp_state(void *pContext)
1236 state = NET_NFC_STATE_EXCHANGER_SERVER;
1243 static bool _net_nfc_make_llcp_data(emulMsg_s *emul_msg)
1246 data_s rawdata = {NULL, 0};
1248 _net_nfc_retrieve_ndef_message(&rawdata);
1249 _nfc_emul_util_free_mem(rawdata.buffer);
1251 if (emul_msg->record_count == 0 || rawdata.length == 0) {
1252 DEBUG_ERR_MSG("data is zero >>>");
1256 /* For SNEP, we should create snep msg, and then copy it to llcp_server_data */
1257 _net_nfc_llcp_create_snep_server_msg(SNEP_REQ_PUT);
1259 /* copy rawdata to llcp_server_data->buffer */
1260 if (Snep_Server_msg->data->length <= SNEP_MAX_BUFFER) {
1261 DEBUG_MSG("The snep msg size is small than SNEP_MAX_BUFFER >>>");
1263 if (llcp_server_data == NULL)
1266 llcp_server_data->length = Snep_Server_msg->data->length;
1267 memcpy(llcp_server_data->buffer, Snep_Server_msg->data->buffer, Snep_Server_msg->data->length);
1269 DEBUG_MSG("send first segment >>>");
1271 if (llcp_server_data == NULL)
1274 llcp_server_data->length = SNEP_MAX_BUFFER;
1275 memcpy(llcp_server_data->buffer, Snep_Server_msg->data->buffer, SNEP_MAX_BUFFER);
1277 Snep_Server_msg->isSegment = true;
1278 Snep_Server_msg->offset = SNEP_MAX_BUFFER;
1286 static void _net_nfc_send_emulMsg_to_nfc_manager(emulMsg_s *emul_msg)
1290 switch (emul_msg->message_id) {
1291 case EMUL_NFC_TAG_DISCOVERED:
1292 _net_nfc_target_discovered_cb(emul_msg);
1295 case EMUL_NFC_TAG_DETACHED:
1296 _net_nfc_tag_detached_cb();
1299 case EMUL_NFC_P2P_DISCOVERED:
1300 _net_nfc_initialize_llcp();
1301 _net_nfc_target_discovered_cb(emul_msg);
1304 case EMUL_NFC_P2P_SEND:
1305 if (!_net_nfc_emul_get_is_p2p_attached()) {
1306 DEBUG_ERR_MSG("target is not attached!!");
1310 if (_net_nfc_make_llcp_data(emul_msg)) {
1311 /* find snep server*/
1312 socket_info_s *socket_info = _net_nfc_find_server_socket(NET_NFC_EMUL_SNEP_SERVER_SOCKET_NUMBER);
1313 if (socket_info == NULL) {
1314 DEBUG_ERR_MSG("socket_info is NULL");
1318 _net_nfc_llcp_data_receive_cb(socket_info->user_context); /* call callback */
1320 DEBUG_ERR_MSG("make_p2p_data is fail!!");
1325 case EMUL_NFC_P2P_DETACHED:
1326 if (!_net_nfc_emul_get_is_p2p_attached()) {
1327 DEBUG_ERR_MSG("target is not attached!!");
1331 _net_nfc_target_detached_cb();
1332 _net_nfc_deinitialize_llcp();
1336 DEBUG_ERR_MSG("message_id is wrong!!");
1343 static void _net_nfc_process_emulMsg(uint8_t *data, long int size)
1347 emulMsg_s *emul_msg = NULL;
1349 /* create emul_msg */
1350 _net_nfc_create_emulMsg(&emul_msg, data, size);
1352 DEBUG_MSG("emul_msg->message_id >>>>[%d]", emul_msg->message_id);
1354 /* processing emul_msg */
1355 _net_nfc_send_emulMsg_to_nfc_manager(emul_msg);
1357 /* destroy emul_msg */
1358 _net_nfc_destroy_emulMsg(emul_msg);
1363 #ifdef __USE_EPOLL_FOR_FILE__
1364 static void emul_ReaderThread(void *pArg)
1366 DEBUG_MSG("emul_ReaderThread START >>>>");
1368 char file_name[1024] = {0};
1369 bool condition = true;
1371 int emulMsg_file_fd = -1;
1372 int emulMsg_poll_fd = -1;
1373 struct epoll_event *emulMsg_poll_events = NULL;
1375 /* make file name */
1376 snprintf(file_name, sizeof(file_name), "%s/%s", NET_NFC_EMUL_DATA_PATH, NET_NFC_EMUL_MESSAGE_FILE_NAME);
1377 LOGE("file path : %s", file_name);
1379 /* open file for poll */
1380 emulMsg_file_fd = open(file_name, O_RDONLY | O_NONBLOCK);
1381 if (emulMsg_file_fd < 0) {
1382 DEBUG_MSG("file open error !!!!");
1387 if ((emulMsg_poll_fd = epoll_create1(EPOLL_CLOEXEC)) == -1) {
1388 DEBUG_MSG("epoll_create1 is occured");
1389 close(emulMsg_file_fd);
1393 if ((emulMsg_poll_events = calloc(1, sizeof(struct epoll_event) * EPOLL_SIZE)) == NULL) {
1394 DEBUG_MSG("calloc is occured");
1395 close(emulMsg_file_fd);
1400 struct epoll_event ev;
1402 ev.events = EPOLLIN | EPOLLET | EPOLLHUP | EPOLLERR;
1403 ev.data.fd = emulMsg_file_fd;
1405 /* add file fd to epoll */
1406 epoll_ctl(emulMsg_poll_fd, EPOLL_CTL_ADD, emulMsg_file_fd, &ev);
1408 while (condition == true) {
1410 int num_of_files = 0;
1413 /* to do : I will add mutex in the future */
1416 DEBUG_MSG("epoll wait >>>>");
1418 while ((num_of_files = epoll_wait(emulMsg_poll_fd, emulMsg_poll_events, EPOLL_SIZE, -1)) == 0)
1419 DEBUG_MSG("no data is changed ");
1421 for (index = 0; index < num_of_files; index++) {
1422 if ((emulMsg_poll_events[index].events & (EPOLLHUP)) || (emulMsg_poll_events[index].events & (EPOLLERR))) {
1423 DEBUG_MSG("connection is closed");
1426 } else if (emulMsg_poll_events[index].events & EPOLLIN) {
1427 if (emulMsg_poll_events[index].data.fd == emulMsg_file_fd) {
1428 char readbuffer[READ_BUFFER_LENGTH_MAX + 1];
1431 DEBUG_MSG("precess POLLIN ");
1433 memset(readbuffer, 0x00, sizeof(READ_BUFFER_LENGTH_MAX) + 1);
1435 readcnt = read(emulMsg_file_fd, readbuffer, READ_BUFFER_LENGTH_MAX);
1437 DEBUG_MSG("message readcnt= [%d] ", readcnt);
1438 DEBUG_MSG("message = [%s] ", readbuffer);
1441 _net_nfc_process_emulMsg((uint8_t *)readbuffer + NET_NFC_EMUL_HEADER_LENGTH, (long int)readcnt - NET_NFC_EMUL_HEADER_LENGTH);
1443 DEBUG_MSG("not expected socket connection");
1447 if (num_of_files == index) {
1448 DEBUG_MSG("unknown event");
1460 close(emulMsg_file_fd);
1462 if (emulMsg_poll_events != NULL)
1463 free(emulMsg_poll_events);
1465 DEBUG_MSG("emul_ReaderThread END >>>>");
1470 static void emul_ReaderThread(void *pArg)
1472 DEBUG_MSG("emul_ReaderThread START >>>>");
1475 char file_name[1024] = {0};
1477 char readBuffer[READ_BUFFER_LENGTH_MAX];
1483 bool condition = true;
1485 /* make file name */
1486 snprintf(file_name, sizeof(file_name), "%s/%s", NET_NFC_EMUL_DATA_PATH, NET_NFC_EMUL_MESSAGE_FILE_NAME);
1487 LOGE("file path : %s", file_name);
1490 DEBUG_MSG("Start Current Time [%ld]", (unsigned long)curTime);
1492 /* polling 500 ms */
1494 while (condition != 0) {
1498 if ((fp = fopen(file_name, "r")) == NULL) {
1499 DEBUG_MSG("file open error");
1502 /* get the modified time of the file */
1503 if (stat(file_name, &st) == 0) {
1504 if ((unsigned long)st.st_mtime <= (unsigned long)curTime) {
1508 DEBUG_MSG("FILE Modified Time [%ld]", (unsigned long)st.st_mtime);
1509 /* update current time */
1511 DEBUG_MSG("Get New Current Time [%ld]", (unsigned long)curTime);
1515 DEBUG_MSG("stat error");
1521 memset(readBuffer, 0x00, READ_BUFFER_LENGTH_MAX);
1523 if (fscanf(fp, "%[^\n]", readBuffer))
1524 DEBUG_MSG("get DATA >>>> readBuffer [%s]", readBuffer);
1526 /* process message */
1527 _net_nfc_process_emulMsg((uint8_t *)readBuffer, (long int)strlen(readBuffer));
1532 DEBUG_MSG("LOOP END >>>>");
1536 DEBUG_MSG("emul_ReaderThread END >>>>");
1540 static bool _net_nfc_emul_controller_start_thread(void)
1546 ret = pthread_create(&gEmulThread, NULL, (emul_Nfc_thread_handler_t)emul_ReaderThread, (void*)"emul_read_thread");
1556 static void _net_nfc_emul_controller_stop_thread(void)
1560 pthread_cancel(gEmulThread);
1564 pthread_cond_signal(&cond);
1566 pthread_join(gEmulThread, NULL);
1571 static bool net_nfc_emul_controller_init(net_nfc_error_e *result)
1580 DEBUG_MSG("start stack init ");
1582 if (g_stack_init_successful == true) {
1583 DEBUG_MSG("Already statck is initialized");
1588 /* start reader thread : to get event from Inject */
1589 if (!_net_nfc_emul_controller_start_thread()) {
1590 DEBUG_ERR_MSG("Failed to create emul thread");
1594 DEBUG_MSG("Stack init finished");
1596 g_stack_init_successful = true;
1597 *result = NET_NFC_OK;
1604 static bool net_nfc_emul_controller_deinit(void)
1609 if (g_stack_init_successful == false) {
1610 DEBUG_MSG("Already statck is deinitialized");
1614 _net_nfc_emul_controller_stop_thread();
1616 g_stack_init_successful = false;
1622 static bool net_nfc_emul_controller_register_listener(
1623 target_detection_listener_cb target_detection_listener,
1624 se_transaction_listener_cb se_transaction_listener,
1625 llcp_event_listener_cb llcp_event_listener,
1626 hce_apdu_listener_cb hce_apdu_listener,
1627 net_nfc_error_e *result)
1632 *result = NET_NFC_OK;
1636 g_emul_controller_target_cb = target_detection_listener;
1637 g_emul_controller_se_cb = se_transaction_listener;
1638 g_emul_controller_llcp_cb = llcp_event_listener;
1639 g_emul_controller_hce_cb = hce_apdu_listener;
1646 static bool net_nfc_emul_controller_unregister_listener()
1650 g_emul_controller_target_cb = NULL;
1651 g_emul_controller_se_cb = NULL;
1652 g_emul_controller_llcp_cb = NULL;
1659 static bool net_nfc_emul_controller_get_firmware_version(data_s **data, net_nfc_error_e *result)
1661 if (data == NULL || result == NULL)
1664 *result = NET_NFC_OK;
1668 *data = calloc(1, sizeof(data_s));
1673 (*data)->length = 10;
1674 (*data)->buffer = calloc(1, (*data)->length);
1676 if ((*data)->buffer == NULL)
1679 snprintf((char *)(*data)->buffer, (*data)->length, "%d.%d", 1, 0);
1686 static bool net_nfc_emul_controller_check_firmware_version(net_nfc_error_e *result)
1691 *result = NET_NFC_OK;
1700 static bool net_nfc_emul_controller_update_firmware(net_nfc_error_e *result)
1705 *result = NET_NFC_OK;
1714 static bool net_nfc_emul_controller_get_stack_information(net_nfc_stack_information_s *stack_info, net_nfc_error_e *result)
1719 *result = NET_NFC_OK;
1728 static bool net_nfc_emul_controller_configure_discovery(net_nfc_discovery_mode_e mode, net_nfc_event_filter_e config, net_nfc_error_e *result)
1736 *result = NET_NFC_OK;
1738 if (mode == NET_NFC_DISCOVERY_MODE_START) {
1739 mode = NET_NFC_DISCOVERY_MODE_CONFIG;
1740 } else if (mode == NET_NFC_DISCOVERY_MODE_STOP) {
1741 mode = NET_NFC_DISCOVERY_MODE_CONFIG;
1742 config = NET_NFC_ALL_DISABLE;
1747 if ((mode == NET_NFC_DISCOVERY_MODE_CONFIG)) {
1748 if (config == NET_NFC_ALL_DISABLE) {
1749 /* This handle is not useful anymore */
1750 __net_nfc_make_invalid_target_handle();
1752 /* reset socket_info */
1753 for (idx = 0; idx < LLCP_NB_SOCKET_MAX; idx++)
1754 _net_nfc_remove_socket_slot((net_nfc_llcp_socket_t)idx);
1756 DEBUG_MSG("Kill Thread");
1758 ret = net_nfc_emul_controller_deinit();
1759 } else if (config == NET_NFC_ALL_ENABLE) {
1760 net_nfc_error_e err;
1762 DEBUG_MSG("Create Thread");
1763 ret = net_nfc_emul_controller_init(&err);
1772 static bool net_nfc_emul_controller_get_secure_element_list(net_nfc_secure_element_info_s *list, int *count, net_nfc_error_e *result)
1777 *result = NET_NFC_OK;
1786 static bool net_nfc_emul_controller_set_secure_element_mode(net_nfc_secure_element_type_e element_type, net_nfc_secure_element_mode_e mode, net_nfc_error_e *result)
1791 *result = NET_NFC_OK;
1800 static bool net_nfc_emul_controller_check_target_presence(net_nfc_target_handle_s *handle, net_nfc_error_e *result)
1805 *result = NET_NFC_OK;
1809 if (_net_nfc_emul_get_is_tag_attached()) {
1812 DEBUG_MSG("TAG Detached");
1817 static bool net_nfc_emul_controller_connect(net_nfc_target_handle_s *handle, net_nfc_error_e *result)
1822 *result = NET_NFC_OK;
1824 if (!__net_nfc_is_valide_target_handle(handle)) {
1825 *result = NET_NFC_INVALID_HANDLE;
1836 static bool net_nfc_emul_controller_disconnect(net_nfc_target_handle_s *handle, net_nfc_error_e *result)
1841 *result = NET_NFC_OK;
1845 if (!__net_nfc_is_valide_target_handle(handle)) {
1846 *result = NET_NFC_INVALID_HANDLE;
1850 // This handle is not useful anymore
1851 __net_nfc_make_invalid_target_handle();
1853 /* reset socket_info */
1855 for (; idx < LLCP_NB_SOCKET_MAX; idx++)
1856 _net_nfc_remove_socket_slot((net_nfc_llcp_socket_t)idx);
1863 static bool net_nfc_emul_controller_check_ndef(net_nfc_target_handle_s *handle, uint8_t *ndef_card_state, int *max_data_size, int *real_data_size, net_nfc_error_e *result)
1865 data_s rawdata = {NULL, 0};
1870 *result = NET_NFC_OK;
1872 if (handle == NULL || ndef_card_state == NULL || max_data_size == NULL || real_data_size == NULL || result == NULL) {
1873 *result = NET_NFC_NULL_PARAMETER;
1877 if (!__net_nfc_is_valide_target_handle(handle)) {
1878 *result = NET_NFC_INVALID_HANDLE;
1884 _net_nfc_retrieve_ndef_message(&rawdata);
1885 _nfc_emul_util_free_mem(rawdata.buffer);
1887 if (_net_nfc_emul_get_is_tag_attached()) {
1888 *ndef_card_state = NET_NFC_NDEF_CARD_READ_WRITE;
1889 *max_data_size = BUFFER_LENGTH_MAX;
1890 *real_data_size = rawdata.length;
1891 DEBUG_MSG("Card State : [%d] MAX data size :[%d] actual data size = [%d]", *ndef_card_state, *max_data_size, *real_data_size);
1893 DEBUG_MSG("target detached");
1901 static bool net_nfc_emul_controller_make_read_only_ndef(net_nfc_target_handle_s *handle, net_nfc_error_e *result)
1906 *result = NET_NFC_OK;
1908 if (!__net_nfc_is_valide_target_handle(handle)) {
1909 *result = NET_NFC_INVALID_HANDLE;
1920 static bool net_nfc_emul_controller_read_ndef(net_nfc_target_handle_s *handle, data_s **data, net_nfc_error_e *result)
1922 int real_data_size = 0;
1923 data_s rawdata = {NULL, 0};
1930 *result = NET_NFC_OK;
1932 if (handle == NULL || data == NULL || result == NULL) {
1933 DEBUG_ERR_MSG("NET_NFC_NULL_PARAMETER >>>");
1934 *result = NET_NFC_NULL_PARAMETER;
1938 if (!__net_nfc_is_valide_target_handle(handle)) {
1939 DEBUG_ERR_MSG("NET_NFC_INVALID_HANDLE >>>");
1940 *result = NET_NFC_INVALID_HANDLE;
1944 if (!_net_nfc_emul_get_is_tag_attached()) {
1945 DEBUG_ERR_MSG("NET_NFC_NOT_ALLOWED_OPERATION >>>");
1946 *result = NET_NFC_NOT_ALLOWED_OPERATION;
1950 *result = _net_nfc_retrieve_ndef_message(&rawdata);
1951 if (*result != NET_NFC_OK) {
1952 _nfc_emul_util_free_mem(rawdata.buffer);
1956 real_data_size = rawdata.length;
1958 if (real_data_size == 0) {
1959 DEBUG_ERR_MSG("read ndef_msg is failed >>> real_data_size is zero");
1960 *result = NET_NFC_NO_NDEF_MESSAGE;
1961 _nfc_emul_util_free_mem(rawdata.buffer);
1965 *data = calloc(1, sizeof(data_s));
1967 if (*data == NULL) {
1968 *result = NET_NFC_ALLOC_FAIL;
1969 _nfc_emul_util_free_mem(rawdata.buffer);
1973 (*data)->length = real_data_size;
1974 (*data)->buffer = calloc(1, real_data_size);
1976 if ((*data)->buffer == NULL) {
1978 *result = NET_NFC_ALLOC_FAIL;
1979 _nfc_emul_util_free_mem(rawdata.buffer);
1983 /* copy rawdata to data->buffer */
1984 memcpy((*data)->buffer, rawdata.buffer, real_data_size);
1985 _nfc_emul_util_free_mem(rawdata.buffer);
1992 static bool net_nfc_emul_controller_write_ndef(net_nfc_target_handle_s *handle, data_s *data, net_nfc_error_e *result)
1997 *result = NET_NFC_OK;
1999 if (!__net_nfc_is_valide_target_handle(handle)) {
2000 *result = NET_NFC_INVALID_HANDLE;
2006 DEBUG_MSG("net_nfc_emul_controller_write_ndef success >>>");
2008 _net_nfc_store_ndef_message(data);
2016 static bool net_nfc_emul_controller_transceive(net_nfc_target_handle_s *handle,
2017 net_nfc_transceive_info_s *info, data_s **data, net_nfc_error_e *result)
2024 if (info == NULL || info->trans_data.buffer == NULL ||
2025 info->trans_data.length == 0) {
2026 *result = NET_NFC_INVALID_PARAM;
2030 *result = NET_NFC_OK;
2033 if (!__net_nfc_is_valide_target_handle(handle)) {
2034 *result = NET_NFC_INVALID_HANDLE;
2040 /* This implementation is not needed on Emulator environment */
2047 static bool net_nfc_emul_controller_format_ndef(net_nfc_target_handle_s *handle, data_s *secure_key, net_nfc_error_e *result)
2052 *result = NET_NFC_OK;
2054 if (!__net_nfc_is_valide_target_handle(handle)) {
2055 *result = NET_NFC_INVALID_HANDLE;
2066 static bool net_nfc_emul_controller_exception_handler(void)
2070 net_nfc_error_e error;
2072 if (net_nfc_emul_controller_init(&error) == false) {
2073 DEBUG_ERR_MSG("exception handler is failed!!");
2082 static bool net_nfc_emul_controller_is_ready(net_nfc_error_e *result)
2087 *result = NET_NFC_OK;
2093 return g_stack_init_successful;
2097 /*******************
2099 ********************/
2101 static bool net_nfc_emul_controller_llcp_config(net_nfc_llcp_config_info_s *config, net_nfc_error_e *result)
2106 *result = NET_NFC_OK;
2115 static bool net_nfc_emul_controller_llcp_check_llcp(net_nfc_target_handle_s *handle, net_nfc_error_e *result)
2120 *result = NET_NFC_OK;
2122 if (!__net_nfc_is_valide_target_handle(handle)) {
2123 *result = NET_NFC_INVALID_HANDLE;
2134 static bool net_nfc_emul_controller_llcp_activate_llcp(net_nfc_target_handle_s *handle, net_nfc_error_e *result)
2139 *result = NET_NFC_OK;
2141 if (!__net_nfc_is_valide_target_handle(handle)) {
2142 *result = NET_NFC_INVALID_HANDLE;
2153 static bool net_nfc_emul_controller_llcp_create_socket(net_nfc_llcp_socket_t *socket, net_nfc_socket_type_e socketType, uint16_t miu, uint8_t rw, net_nfc_error_e *result, void *user_param)
2158 *result = NET_NFC_OK;
2162 /* get available socket */
2163 socket_info_s *socket_info = _net_nfc_get_available_socket_slot();
2164 if (socket_info == NULL) {
2165 DEBUG_ERR_MSG("The available socket is nothing!!");
2169 /* get llcp state */
2170 llcp_state_e llcp_state = NET_NFC_STATE_UNKNOWN;
2171 llcp_state = _net_nfc_get_llcp_state(user_param);
2172 if (llcp_state == NET_NFC_STATE_UNKNOWN) {
2173 DEBUG_ERR_MSG("llcp_state is NET_NFC_STATE_UNKNOWN!!");
2177 if (llcp_state == NET_NFC_STATE_EXCHANGER_SERVER) {
2178 DEBUG_MSG("NET_NFC_STATE_EXCHANGER_SERVER");
2180 *socket = NET_NFC_EMUL_SNEP_SERVER_SOCKET_NUMBER;
2181 } else if (llcp_state == NET_NFC_STATE_EXCHANGER_SERVER_NPP) {
2182 DEBUG_MSG("NET_NFC_STATE_EXCHANGER_SERVER_NPP");
2184 *socket = NET_NFC_EMUL_NPP_SERVER_SOCKET_NUMBER;
2185 } else if (llcp_state == NET_NFC_STATE_EXCHANGER_CLIENT) {
2186 DEBUG_MSG("NET_NFC_STATE_EXCHANGER_CLIENT");
2188 *socket = NET_NFC_EMUL_CLIENT_SOCKET_NUMBER;
2189 } else if (llcp_state == NET_NFC_STATE_CONN_HANDOVER_REQUEST) {
2190 DEBUG_MSG("NET_NFC_STATE_CONN_HANDOVER_REQUEST");
2192 *socket = NET_NFC_EMUL_HANDOVER_REQUEST_SOCKET_NUMBER;
2193 } else if (llcp_state == NET_NFC_STATE_CONN_HANDOVER_SELECT) {
2194 DEBUG_MSG("NET_NFC_STATE_CONN_HANDOVER_SELECT");
2196 *socket = NET_NFC_EMUL_HANDOVER_SELECT_SOCKET_NUMBER;
2198 DEBUG_MSG("we doesn't support..");
2203 DEBUG_MSG("socket is created = [0x%x]", *socket);
2205 socket_info->socket_handle = *socket;
2206 socket_info->user_context = (void *)user_param;
2213 static bool net_nfc_emul_controller_llcp_bind(net_nfc_llcp_socket_t socket, uint8_t service_access_point, net_nfc_error_e *result)
2218 *result = NET_NFC_OK;
2227 static bool net_nfc_emul_controller_llcp_listen(net_nfc_target_handle_s *handle, uint8_t *service_access_name, net_nfc_llcp_socket_t socket, net_nfc_error_e *result, void *user_param)
2232 *result = NET_NFC_OK;
2234 if (!__net_nfc_is_valide_target_handle(handle)) {
2235 *result = NET_NFC_INVALID_HANDLE;
2241 /* Emul don't create real socket. So, we don't need to wait accept from remote socket */
2242 /* In here, send accept event for only snep */
2243 net_nfc_request_listen_socket_t *detail = NULL;
2245 socket_info_s *socket_info = _net_nfc_find_server_socket(socket);
2246 if (socket_info == NULL) {
2247 DEBUG_ERR_MSG("socket_info is NULL");
2251 llcp_state_e llcp_state = NET_NFC_STATE_UNKNOWN;
2252 llcp_state = _net_nfc_get_llcp_state(socket_info->user_context);
2253 if (llcp_state == NET_NFC_STATE_UNKNOWN) {
2254 DEBUG_ERR_MSG("llcp_state is NET_NFC_STATE_UNKNOWN");
2258 if (llcp_state == NET_NFC_STATE_EXCHANGER_SERVER) {
2259 _nfc_emul_util_alloc_mem(detail, sizeof(*detail));
2261 if (detail != NULL) {
2262 detail->length = sizeof(*detail);
2263 detail->request_type = NET_NFC_MESSAGE_SERVICE_LLCP_LISTEN;
2265 socket_info->user_context = user_param;
2267 detail->handle = handle;
2268 detail->client_socket = NET_NFC_EMUL_INCOMING_SOCKET_NUMBER;
2269 detail->trans_param = socket_info->user_context;
2270 detail->result = NET_NFC_OK;
2272 DEBUG_MSG("accept callback is called");
2273 g_emul_controller_llcp_cb(detail, socket_info->user_context);
2276 DEBUG_MSG("llcp_state is [%d]", llcp_state);
2284 /* below accept function does not used. */
2285 static bool net_nfc_emul_controller_llcp_accept(net_nfc_llcp_socket_t socket, net_nfc_error_e *result, void *user_param)
2290 *result = NET_NFC_OK;
2299 static bool net_nfc_emul_controller_llcp_connect(net_nfc_target_handle_s *handle, net_nfc_llcp_socket_t socket, uint8_t service_access_point, net_nfc_error_e *result, void *user_param)
2304 *result = NET_NFC_OK;
2306 if (!__net_nfc_is_valide_target_handle(handle)) {
2307 *result = NET_NFC_INVALID_HANDLE;
2313 socket_info_s *socket_info = _net_nfc_find_server_socket(socket);
2314 if (socket_info == NULL) {
2315 DEBUG_ERR_MSG("socket_info is NULL");
2319 llcp_state_e llcp_state = NET_NFC_STATE_UNKNOWN;
2320 llcp_state = _net_nfc_get_llcp_state(socket_info->user_context);
2321 if (llcp_state == NET_NFC_STATE_UNKNOWN) {
2322 DEBUG_ERR_MSG("llcp_state is NET_NFC_STATE_UNKNOWN");
2331 static bool net_nfc_emul_controller_llcp_connect_by_url(net_nfc_target_handle_s *handle, net_nfc_llcp_socket_t socket, uint8_t *service_access_name, net_nfc_error_e *result, void *user_param)
2336 *result = NET_NFC_OK;
2338 if (!__net_nfc_is_valide_target_handle(handle)) {
2339 *result = NET_NFC_INVALID_HANDLE;
2345 socket_info_s *socket_info = _net_nfc_find_server_socket(socket);
2346 if (socket_info == NULL) {
2347 DEBUG_ERR_MSG("socket_info is NULL");
2351 llcp_state_e llcp_state = NET_NFC_STATE_UNKNOWN;
2352 llcp_state = _net_nfc_get_llcp_state(socket_info->user_context);
2353 if (llcp_state == NET_NFC_STATE_UNKNOWN) {
2354 DEBUG_ERR_MSG("llcp_state is NET_NFC_STATE_UNKNOWN");
2363 static bool net_nfc_emul_controller_llcp_send(net_nfc_target_handle_s *handle, net_nfc_llcp_socket_t socket, data_s *data, net_nfc_error_e *result, void *user_param)
2368 *result = NET_NFC_OK;
2370 if (!__net_nfc_is_valide_target_handle(handle)) {
2371 *result = NET_NFC_INVALID_HANDLE;
2377 socket_info_s *socket_info = _net_nfc_find_server_socket(socket);
2378 if (socket_info == NULL) {
2379 DEBUG_ERR_MSG("socket_info is NULL");
2383 llcp_state_e llcp_state = NET_NFC_STATE_UNKNOWN;
2384 llcp_state = _net_nfc_get_llcp_state(socket_info->user_context);
2385 if (llcp_state == NET_NFC_STATE_UNKNOWN) {
2386 DEBUG_ERR_MSG("llcp_state is NET_NFC_STATE_UNKNOWN");
2390 if (llcp_state == NET_NFC_STATE_EXCHANGER_SERVER) {
2391 net_nfc_request_llcp_msg_t *req_msg = NULL;
2393 _nfc_emul_util_alloc_mem(req_msg, sizeof(net_nfc_request_llcp_msg_t));
2395 socket_info->user_context = user_param;
2397 if (req_msg != NULL) {
2398 req_msg->length = sizeof(net_nfc_request_llcp_msg_t);
2399 req_msg->request_type = NET_NFC_MESSAGE_SERVICE_LLCP_SEND;
2400 req_msg->result = NET_NFC_OK;
2402 DEBUG_MSG("send callback is called");
2403 g_emul_controller_llcp_cb(req_msg, socket_info->user_context);
2412 static bool net_nfc_emul_controller_llcp_send_to(net_nfc_target_handle_s *handle, net_nfc_llcp_socket_t socket, data_s *data, uint8_t service_access_point, net_nfc_error_e *result, void *user_param)
2417 *result = NET_NFC_OK;
2419 if (!__net_nfc_is_valide_target_handle(handle)) {
2420 *result = NET_NFC_INVALID_HANDLE;
2426 socket_info_s *socket_info = _net_nfc_find_server_socket(socket);
2427 if (socket_info == NULL) {
2428 DEBUG_ERR_MSG("socket_info is NULL");
2432 llcp_state_e llcp_state = NET_NFC_STATE_UNKNOWN;
2433 llcp_state = _net_nfc_get_llcp_state(socket_info->user_context);
2434 if (llcp_state == NET_NFC_STATE_UNKNOWN) {
2435 DEBUG_ERR_MSG("llcp_state is NET_NFC_STATE_UNKNOWN");
2439 if (llcp_state == NET_NFC_STATE_EXCHANGER_SERVER) {
2440 net_nfc_request_llcp_msg_t *req_msg = NULL;
2442 _nfc_emul_util_alloc_mem(req_msg, sizeof(net_nfc_request_llcp_msg_t));
2444 socket_info->user_context = user_param;
2446 if (req_msg != NULL) {
2447 req_msg->length = sizeof(net_nfc_request_llcp_msg_t);
2448 req_msg->request_type = NET_NFC_MESSAGE_SERVICE_LLCP_SEND_TO;
2449 req_msg->result = NET_NFC_OK;
2451 DEBUG_MSG("send_to callback is called");
2452 g_emul_controller_llcp_cb(req_msg, socket_info->user_context);
2462 static bool net_nfc_emul_controller_llcp_recv(net_nfc_target_handle_s *handle, net_nfc_llcp_socket_t socket, data_s *data, net_nfc_error_e *result, void *user_param)
2464 if (result == NULL || data == NULL)
2467 *result = NET_NFC_OK;
2469 if (!__net_nfc_is_valide_target_handle(handle)) {
2470 *result = NET_NFC_INVALID_HANDLE;
2476 socket_info_s *socket_info = _net_nfc_find_server_socket(socket);
2477 if (socket_info == NULL) {
2478 DEBUG_ERR_MSG("socket_info is NULL");
2482 llcp_state_e llcp_state = NET_NFC_STATE_UNKNOWN;
2483 llcp_state = _net_nfc_get_llcp_state(socket_info->user_context);
2484 if (llcp_state == NET_NFC_STATE_UNKNOWN) {
2485 DEBUG_ERR_MSG("llcp_state is NET_NFC_STATE_UNKNOWN");
2489 if (llcp_state == NET_NFC_STATE_EXCHANGER_SERVER) {
2491 DEBUG_MSG("NET_NFC_STATE_EXCHANGER_SERVER");
2492 socket_info->user_context = user_param;
2494 if (Snep_Server_msg->isSegment) {
2495 /* send snep msg continueosly ..*/
2496 DEBUG_MSG("send segments for snep msg");
2498 int remained_size = 0;
2500 remained_size = Snep_Server_msg->data->length - Snep_Server_msg->offset;
2501 DEBUG_MSG("remained_size[%d]", remained_size);
2503 /* copy rawdata to llcp_server_data->buffer */
2504 if (remained_size <= SNEP_MAX_BUFFER) {
2505 DEBUG_MSG("send last segment >>>");
2507 llcp_server_data->length = remained_size;
2508 memcpy(llcp_server_data->buffer, Snep_Server_msg->data->buffer + Snep_Server_msg->offset, remained_size);
2510 Snep_Server_msg->isSegment = false;
2512 DEBUG_MSG("send continue segment >>>");
2514 llcp_server_data->length = SNEP_MAX_BUFFER;
2515 memcpy(llcp_server_data->buffer, Snep_Server_msg->data->buffer + Snep_Server_msg->offset, SNEP_MAX_BUFFER);
2517 Snep_Server_msg->offset += SNEP_MAX_BUFFER;
2520 _net_nfc_llcp_data_receive_cb(socket_info->user_context); /* call callback */
2522 /* In here, we dosen't call _net_nfc_llcp_data_receive_cb. just wait event from emulator */
2523 /*After copying data address, we will return it, immediately */
2524 DEBUG_MSG("data address is set");
2525 llcp_server_data = data;
2528 DEBUG_MSG("we don't support..");
2536 static bool net_nfc_emul_controller_llcp_recv_from(net_nfc_target_handle_s *handle, net_nfc_llcp_socket_t socket, data_s *data, net_nfc_error_e *result, void *user_param)
2541 *result = NET_NFC_OK;
2543 if (!__net_nfc_is_valide_target_handle(handle)) {
2544 *result = NET_NFC_INVALID_HANDLE;
2550 socket_info_s *socket_info = _net_nfc_find_server_socket(socket);
2551 if (socket_info == NULL) {
2552 DEBUG_ERR_MSG("socket_info is NULL");
2556 llcp_state_e llcp_state = NET_NFC_STATE_UNKNOWN;
2557 llcp_state = _net_nfc_get_llcp_state(socket_info->user_context);
2558 if (llcp_state == NET_NFC_STATE_UNKNOWN) {
2559 DEBUG_ERR_MSG("llcp_state is NET_NFC_STATE_UNKNOWN");
2563 if (llcp_state == NET_NFC_STATE_EXCHANGER_SERVER) {
2565 DEBUG_MSG("NET_NFC_STATE_EXCHANGER_SERVER");
2566 socket_info->user_context = user_param;
2568 if (Snep_Server_msg->isSegment) {
2569 /* send snep msg continueosly ..*/
2570 DEBUG_MSG("send segments for snep msg");
2572 int remained_size = 0;
2574 remained_size = Snep_Server_msg->data->length - Snep_Server_msg->offset;
2575 DEBUG_MSG("remained_size[%d]", remained_size);
2577 /* copy rawdata to llcp_server_data->buffer */
2578 if (remained_size <= SNEP_MAX_BUFFER) {
2579 DEBUG_MSG("send last segment >>>");
2581 llcp_server_data->length = remained_size;
2582 memcpy(llcp_server_data->buffer, Snep_Server_msg->data->buffer + Snep_Server_msg->offset, remained_size);
2584 Snep_Server_msg->isSegment = false;
2586 DEBUG_MSG("send continue segment >>>");
2588 llcp_server_data->length = SNEP_MAX_BUFFER;
2589 memcpy(llcp_server_data->buffer, Snep_Server_msg->data->buffer + Snep_Server_msg->offset, SNEP_MAX_BUFFER);
2591 Snep_Server_msg->offset += SNEP_MAX_BUFFER;
2594 _net_nfc_llcp_data_receive_from_cb(socket_info->user_context); /* call callback */
2596 /* In here, we dosen't call _net_nfc_llcp_data_receive_cb. just wait event from emulator */
2597 /*After copying data address, we will return it, immediately */
2599 DEBUG_MSG("data address is set");
2600 llcp_server_data = data;
2602 DEBUG_ERR_MSG("data address is NULL");
2607 DEBUG_MSG("we donen't support..");
2615 static bool net_nfc_emul_controller_llcp_disconnect(net_nfc_target_handle_s *handle, net_nfc_llcp_socket_t socket, net_nfc_error_e *result, void *user_param)
2620 *result = NET_NFC_OK;
2622 if (!__net_nfc_is_valide_target_handle(handle)) {
2623 *result = NET_NFC_INVALID_HANDLE;
2629 socket_info_s *socket_info = _net_nfc_find_server_socket(socket);
2630 if (socket_info == NULL) {
2631 DEBUG_ERR_MSG("socket_info is NULL");
2635 llcp_state_e llcp_state = NET_NFC_STATE_UNKNOWN;
2636 llcp_state = _net_nfc_get_llcp_state(socket_info->user_context);
2637 if (llcp_state == NET_NFC_STATE_UNKNOWN) {
2638 DEBUG_ERR_MSG("llcp_state is NET_NFC_STATE_UNKNOWN");
2642 /* send msg to framework */
2643 net_nfc_request_llcp_msg_t *req_msg = NULL;
2645 _nfc_emul_util_alloc_mem(req_msg, sizeof(net_nfc_request_llcp_msg_t));
2647 socket_info->user_context = user_param;
2649 if (req_msg != NULL) {
2650 req_msg->length = sizeof(net_nfc_request_llcp_msg_t);
2651 req_msg->request_type = NET_NFC_MESSAGE_SERVICE_LLCP_DISCONNECT;
2652 req_msg->result = NET_NFC_OK;
2654 DEBUG_MSG("disconnect callback is called");
2655 g_emul_controller_llcp_cb(req_msg, user_param);
2658 /* reset socket_info */
2659 _net_nfc_remove_socket_slot((net_nfc_llcp_socket_t)socket);
2666 static bool net_nfc_emul_controller_llcp_socket_close(net_nfc_llcp_socket_t socket,
2667 net_nfc_error_e *result)
2672 *result = NET_NFC_OK;
2681 static bool net_nfc_emul_controller_llcp_reject(net_nfc_target_handle_s *handle, net_nfc_llcp_socket_t socket, net_nfc_error_e *result)
2686 *result = NET_NFC_OK;
2688 if (!__net_nfc_is_valide_target_handle(handle)) {
2689 *result = NET_NFC_INVALID_HANDLE;
2700 static bool net_nfc_emul_controller_llcp_get_remote_config(net_nfc_target_handle_s *handle, net_nfc_llcp_config_info_s *config, net_nfc_error_e *result)
2702 /* In llcp specification ver 1.1, default miu size is 128 */
2703 const uint16_t default_miu = 128;
2708 *result = NET_NFC_OK;
2710 if (!__net_nfc_is_valide_target_handle(handle)) {
2711 *result = NET_NFC_INVALID_HANDLE;
2717 config->miu = default_miu;
2724 static bool net_nfc_emul_controller_llcp_get_remote_socket_info(net_nfc_target_handle_s *handle, net_nfc_llcp_socket_t socket, net_nfc_llcp_socket_option_s *option, net_nfc_error_e *result)
2726 /* In llcp specification ver 1.1, default miu size is 128 */
2727 const uint16_t default_miu = 128;
2732 *result = NET_NFC_OK;
2734 if (!__net_nfc_is_valide_target_handle(handle)) {
2735 *result = NET_NFC_INVALID_HANDLE;
2741 option->miu = default_miu;
2749 static bool net_nfc_emul_controller_support_nfc(net_nfc_error_e *result)
2752 struct stat st = {0};
2754 if (stat("/dev/nfc0", &st) == -1) {
2756 *result = NET_NFC_NOT_SUPPORTED;
2761 *result = NET_NFC_OK;
2767 static bool net_nfc_emul_controller_secure_element_open(net_nfc_secure_element_type_e element_type, net_nfc_target_handle_s **handle, net_nfc_error_e *result)
2769 DEBUG_ERR_MSG("se open start");
2770 *result = NET_NFC_OK;
2771 __net_nfc_make_valid_target_handle(handle);
2773 DEBUG_ERR_MSG("se_open end");
2776 static bool net_nfc_emul_controller_secure_element_get_atr(net_nfc_target_handle_s *handle,
2777 data_s **atr, net_nfc_error_e *result)
2781 int length = sizeof(buffer);
2784 DEBUG_ERR_MSG("se get_atr start");
2785 *result = NET_NFC_OK;
2788 strncpy(buffer, "getatr", sizeof(buffer));
2790 temp = calloc(1, sizeof(*temp));
2792 temp->buffer = calloc(1, length);
2793 if (temp->buffer != NULL) {
2794 memcpy(temp->buffer, buffer, length);
2795 temp->length = length;
2797 *result = NET_NFC_OK;
2800 *result = NET_NFC_ALLOC_FAIL;
2804 *result = NET_NFC_ALLOC_FAIL;
2807 DEBUG_ERR_MSG("se get atr end");
2810 static bool net_nfc_emul_controller_secure_element_send_apdu(net_nfc_target_handle_s *handle,
2811 data_s *command, data_s **response, net_nfc_error_e *result)
2818 DEBUG_ERR_MSG("se send apdu start");
2820 *result = NET_NFC_OK;
2824 strncpy(buffer, "response", sizeof(buffer));
2826 length = strlen(buffer);
2828 temp = calloc(1, sizeof(*temp));
2830 temp->buffer = calloc(1, length + 1);
2831 if (temp->buffer != NULL) {
2832 memcpy(temp->buffer, buffer, sizeof(temp->buffer) - 1);
2833 temp->length = length;
2836 *result = NET_NFC_OK;
2839 *result = NET_NFC_ALLOC_FAIL;
2843 *result = NET_NFC_ALLOC_FAIL;
2847 DEBUG_ERR_MSG("se send apdu end");
2850 static bool net_nfc_emul_controller_secure_element_close(net_nfc_target_handle_s *handle,
2851 net_nfc_error_e *result)
2853 DEBUG_ERR_MSG("se close start");
2854 *result = NET_NFC_OK;
2856 DEBUG_ERR_MSG("se close end");
2859 __attribute__((visibility("default"))) bool onload(net_nfc_oem_interface_s *emul_interfaces)
2863 emul_interfaces->init = net_nfc_emul_controller_init;
2864 emul_interfaces->deinit = net_nfc_emul_controller_deinit;
2865 emul_interfaces->register_listener = net_nfc_emul_controller_register_listener;
2866 emul_interfaces->unregister_listener = net_nfc_emul_controller_unregister_listener;
2867 emul_interfaces->get_firmware_version = net_nfc_emul_controller_get_firmware_version;
2868 emul_interfaces->check_firmware_version = net_nfc_emul_controller_check_firmware_version;
2869 emul_interfaces->update_firmeware = net_nfc_emul_controller_update_firmware;
2870 emul_interfaces->get_stack_information = net_nfc_emul_controller_get_stack_information;
2871 emul_interfaces->configure_discovery = net_nfc_emul_controller_configure_discovery;
2872 emul_interfaces->get_secure_element_list = net_nfc_emul_controller_get_secure_element_list;
2873 emul_interfaces->set_secure_element_mode = net_nfc_emul_controller_set_secure_element_mode;
2874 emul_interfaces->connect = net_nfc_emul_controller_connect;
2875 emul_interfaces->disconnect = net_nfc_emul_controller_disconnect;
2876 emul_interfaces->check_ndef = net_nfc_emul_controller_check_ndef;
2877 emul_interfaces->check_presence = net_nfc_emul_controller_check_target_presence;
2878 emul_interfaces->read_ndef = net_nfc_emul_controller_read_ndef;
2879 emul_interfaces->write_ndef = net_nfc_emul_controller_write_ndef;
2880 emul_interfaces->make_read_only_ndef = net_nfc_emul_controller_make_read_only_ndef;
2881 emul_interfaces->transceive = net_nfc_emul_controller_transceive;
2882 emul_interfaces->format_ndef = net_nfc_emul_controller_format_ndef;
2883 emul_interfaces->exception_handler = net_nfc_emul_controller_exception_handler;
2884 emul_interfaces->is_ready = net_nfc_emul_controller_is_ready;
2886 emul_interfaces->config_llcp = net_nfc_emul_controller_llcp_config;
2887 emul_interfaces->check_llcp_status = net_nfc_emul_controller_llcp_check_llcp;
2888 emul_interfaces->activate_llcp = net_nfc_emul_controller_llcp_activate_llcp;
2889 emul_interfaces->create_llcp_socket = net_nfc_emul_controller_llcp_create_socket;
2890 emul_interfaces->bind_llcp_socket = net_nfc_emul_controller_llcp_bind;
2891 emul_interfaces->listen_llcp_socket = net_nfc_emul_controller_llcp_listen;
2892 emul_interfaces->accept_llcp_socket = net_nfc_emul_controller_llcp_accept;
2893 emul_interfaces->connect_llcp_by_url = net_nfc_emul_controller_llcp_connect_by_url;
2894 emul_interfaces->connect_llcp = net_nfc_emul_controller_llcp_connect;
2895 emul_interfaces->disconnect_llcp = net_nfc_emul_controller_llcp_disconnect;
2896 emul_interfaces->close_llcp_socket = net_nfc_emul_controller_llcp_socket_close;
2897 emul_interfaces->recv_llcp = net_nfc_emul_controller_llcp_recv;
2898 emul_interfaces->send_llcp = net_nfc_emul_controller_llcp_send;
2899 emul_interfaces->recv_from_llcp = net_nfc_emul_controller_llcp_recv_from;
2900 emul_interfaces->send_to_llcp = net_nfc_emul_controller_llcp_send_to;
2901 emul_interfaces->reject_llcp = net_nfc_emul_controller_llcp_reject;
2902 emul_interfaces->get_remote_config = net_nfc_emul_controller_llcp_get_remote_config;
2903 emul_interfaces->get_remote_socket_info = net_nfc_emul_controller_llcp_get_remote_socket_info;
2905 emul_interfaces->support_nfc = net_nfc_emul_controller_support_nfc;
2907 emul_interfaces->secure_element_open = net_nfc_emul_controller_secure_element_open;
2908 emul_interfaces->secure_element_get_atr = net_nfc_emul_controller_secure_element_get_atr;
2909 emul_interfaces->secure_element_send_apdu = net_nfc_emul_controller_secure_element_send_apdu;
2910 emul_interfaces->secure_element_close = net_nfc_emul_controller_secure_element_close;
2912 emul_interfaces->hce_response_apdu = NULL;
2913 emul_interfaces->route_aid = NULL;
2914 emul_interfaces->unroute_aid = NULL;
2915 emul_interfaces->commit_routing = NULL;
2916 emul_interfaces->set_default_route = NULL;
2917 emul_interfaces->clear_aid_table = NULL;
2918 emul_interfaces->get_aid_tablesize = NULL;