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.
16 #include <linux/limits.h>
18 #include "net_nfc_debug_internal.h"
19 #include "net_nfc_util_internal.h"
20 #include "net_nfc_util_ndef_message.h"
21 #include "net_nfc_ndef_message.h"
22 #include "net_nfc_data.h"
25 /* public functions */
26 API net_nfc_error_e net_nfc_create_ndef_message(ndef_message_s **ndef_message)
28 return net_nfc_util_create_ndef_message(ndef_message);
31 API net_nfc_error_e net_nfc_create_rawdata_from_ndef_message(
32 ndef_message_s *ndef_message, data_s **rawdata)
36 net_nfc_error_e result;
38 RETV_IF(NULL == ndef_message, NET_NFC_NULL_PARAMETER);
39 RETV_IF(NULL == rawdata, NET_NFC_NULL_PARAMETER);
43 result = net_nfc_get_ndef_message_byte_length(ndef_message, &count);
44 if (result != NET_NFC_OK)
47 result = net_nfc_create_data(&data ,NULL, count);
48 if (result != NET_NFC_OK)
51 result = net_nfc_util_convert_ndef_message_to_rawdata(ndef_message, data);
53 if (result == NET_NFC_OK)
56 net_nfc_free_data(data);
61 API net_nfc_error_e net_nfc_create_ndef_message_from_rawdata(
62 ndef_message_s **ndef_message, data_s *rawdata)
65 net_nfc_error_e result;
67 RETV_IF(NULL == ndef_message, NET_NFC_NULL_PARAMETER);
68 RETV_IF(NULL == rawdata, NET_NFC_NULL_PARAMETER);
72 result = net_nfc_create_ndef_message(&msg);
73 if (result != NET_NFC_OK)
76 result = net_nfc_util_convert_rawdata_to_ndef_message(rawdata, msg);
77 if (result == NET_NFC_OK)
80 net_nfc_free_ndef_message(msg);
85 API net_nfc_error_e net_nfc_get_ndef_message_byte_length(
86 ndef_message_s *ndef_message, uint32_t *length)
88 net_nfc_error_e result;
90 RETV_IF(NULL == ndef_message, NET_NFC_NULL_PARAMETER);
91 RETV_IF(NULL == length, NET_NFC_NULL_PARAMETER);
93 *length = net_nfc_util_get_ndef_message_length(ndef_message);
97 result = NET_NFC_INVALID_PARAM;
102 API net_nfc_error_e net_nfc_append_record_to_ndef_message(
103 ndef_message_s *ndef_message, ndef_record_s *record)
105 RETV_IF(NULL == ndef_message, NET_NFC_NULL_PARAMETER);
106 RETV_IF(NULL == record, NET_NFC_NULL_PARAMETER);
108 return net_nfc_util_append_record(ndef_message, record);
111 API net_nfc_error_e net_nfc_free_ndef_message(ndef_message_s *ndef_message)
113 RETV_IF(NULL == ndef_message, NET_NFC_NULL_PARAMETER);
115 return net_nfc_util_free_ndef_message(ndef_message);
118 API net_nfc_error_e net_nfc_get_ndef_message_record_count(
119 ndef_message_s *ndef_message, int *count)
121 RETV_IF(NULL == ndef_message, NET_NFC_NULL_PARAMETER);
122 RETV_IF(NULL == count, NET_NFC_NULL_PARAMETER);
124 *count = ndef_message->recordCount;
128 API void net_nfc_ndef_print_message (ndef_message_s *ndef_message)
130 net_nfc_util_print_ndef_message(ndef_message);
134 API net_nfc_error_e net_nfc_search_record_by_type(ndef_message_s *ndef_message,
135 net_nfc_record_tnf_e tnf, data_s *type, ndef_record_s **record)
137 return net_nfc_util_search_record_by_type(ndef_message, tnf, type, record);
140 API net_nfc_error_e net_nfc_append_record_by_index(ndef_message_s *ndef_message,
141 int index, ndef_record_s *record)
143 return net_nfc_util_append_record_by_index(ndef_message, index, record);
146 API net_nfc_error_e net_nfc_get_record_by_index (ndef_message_s *ndef_message,
147 int index, ndef_record_s **record)
149 return net_nfc_util_get_record_by_index(ndef_message, index, record);
152 API net_nfc_error_e net_nfc_remove_record_by_index(ndef_message_s *ndef_message,
155 return net_nfc_util_remove_record_by_index(ndef_message, index);
158 API net_nfc_error_e net_nfc_retrieve_current_ndef_message(
159 ndef_message_s **ndef_message)
162 char file_path[PATH_MAX] = { 0, };
163 net_nfc_error_e result = NET_NFC_UNKNOWN_ERROR;
165 RETV_IF(NULL == ndef_message, NET_NFC_NULL_PARAMETER);
167 snprintf(file_path, sizeof(file_path), "%s/%s/%s", NET_NFC_MANAGER_DATA_PATH,
168 NET_NFC_MANAGER_DATA_PATH_MESSAGE, NET_NFC_MANAGER_NDEF_FILE_NAME);
170 if ((fp = fopen(file_path, "r")) != NULL)
174 /* rewind to start of file */
175 fseek(fp, 0, SEEK_END);
177 fseek(fp, 0, SEEK_SET);
179 NFC_DBG("message length = [%ld]", size);
183 data_s data = { NULL, 0 };
185 if (net_nfc_util_alloc_data(&data, size) == true)
191 current = fread(data.buffer + offset, 1,
192 data.length - offset, fp);
197 } while (offset < data.length);
199 if (offset == data.length)
200 result = net_nfc_create_ndef_message_from_rawdata(ndef_message, &data);
202 NFC_ERR("failed to read ndef message");
204 net_nfc_util_free_data(&data);
208 result = NET_NFC_ALLOC_FAIL;
213 result = NET_NFC_NO_NDEF_MESSAGE;
220 result = NET_NFC_NO_NDEF_MESSAGE;