2 * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
4 * Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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.
17 #include <linux/limits.h>
19 #include "net_nfc_debug_internal.h"
20 #include "net_nfc_util_internal.h"
21 #include "net_nfc_util_ndef_message.h"
22 #include "net_nfc_ndef_message.h"
23 #include "net_nfc_data.h"
25 #ifndef NET_NFC_EXPORT_API
26 #define NET_NFC_EXPORT_API __attribute__((visibility("default")))
30 #ifndef NET_NFC_MANAGER_DATA_PATH
31 #define NET_NFC_MANAGER_DATA_PATH "/opt/usr/share/nfc-manager-daemon"
34 #ifndef NET_NFC_MANAGER_DATA_PATH_MESSAGE
35 #define NET_NFC_MANAGER_DATA_PATH_MESSAGE "message"
38 #ifndef NET_NFC_MANAGER_NDEF_FILE_NAME
39 #define NET_NFC_MANAGER_NDEF_FILE_NAME "ndef-message.txt"
43 /* public functions */
45 net_nfc_error_e net_nfc_create_ndef_message(ndef_message_h *ndef_message)
47 return net_nfc_util_create_ndef_message(
48 (ndef_message_s **)ndef_message);
52 net_nfc_error_e net_nfc_create_rawdata_from_ndef_message(
53 ndef_message_h ndef_message, data_h *rawdata)
56 net_nfc_error_e result;
59 if (ndef_message == NULL || rawdata == NULL) {
60 return NET_NFC_NULL_PARAMETER;
65 result = net_nfc_get_ndef_message_byte_length(ndef_message, &count);
66 if (result != NET_NFC_OK) {
70 result = net_nfc_create_data(&data ,NULL, count);
71 if (result != NET_NFC_OK) {
75 result = net_nfc_util_convert_ndef_message_to_rawdata(
76 (ndef_message_s *)ndef_message, (data_s *)data);
77 if (result == NET_NFC_OK) {
80 net_nfc_free_data(data);
87 net_nfc_error_e net_nfc_create_ndef_message_from_rawdata(
88 ndef_message_h *ndef_message, data_h rawdata)
90 net_nfc_error_e result;
93 if (ndef_message == NULL || rawdata == NULL) {
94 return NET_NFC_NULL_PARAMETER;
99 result = net_nfc_create_ndef_message(&msg);
100 if (result != NET_NFC_OK) {
104 result = net_nfc_util_convert_rawdata_to_ndef_message(
105 (data_s *)rawdata, (ndef_message_s *)msg);
106 if (result == NET_NFC_OK) {
109 net_nfc_free_ndef_message(msg);
116 net_nfc_error_e net_nfc_get_ndef_message_byte_length(
117 ndef_message_h ndef_message, uint32_t *length)
119 net_nfc_error_e result;
121 if (ndef_message == NULL || length == NULL){
122 return NET_NFC_NULL_PARAMETER;
125 *length = net_nfc_util_get_ndef_message_length(
126 (ndef_message_s *)ndef_message);
130 result = NET_NFC_INVALID_PARAM;
137 net_nfc_error_e net_nfc_append_record_to_ndef_message(
138 ndef_message_h ndef_message, ndef_record_h record)
140 if (ndef_message == NULL || record == NULL) {
141 return NET_NFC_NULL_PARAMETER;
144 return net_nfc_util_append_record((ndef_message_s *)ndef_message,
145 (ndef_record_s *)record);
149 net_nfc_error_e net_nfc_free_ndef_message(ndef_message_h ndef_message)
151 if (ndef_message == NULL) {
152 return NET_NFC_NULL_PARAMETER;
155 return net_nfc_util_free_ndef_message((ndef_message_s *)ndef_message);
159 net_nfc_error_e net_nfc_get_ndef_message_record_count(
160 ndef_message_h ndef_message, int *count)
162 ndef_message_s *msg = (ndef_message_s *)ndef_message;
164 if (ndef_message == NULL || count == NULL) {
165 return NET_NFC_NULL_PARAMETER;
168 *count = msg->recordCount;
173 NET_NFC_EXPORT_API void net_nfc_ndef_print_message(ndef_message_h ndef_message)
175 net_nfc_util_print_ndef_message((ndef_message_s *)ndef_message);
180 net_nfc_error_e net_nfc_search_record_by_type(ndef_message_h ndef_message,
181 net_nfc_record_tnf_e tnf, data_h type, ndef_record_h *record)
183 return net_nfc_util_search_record_by_type(
184 (ndef_message_s *)ndef_message, tnf,
185 (data_s *)type, (ndef_record_s **)record);
189 net_nfc_error_e net_nfc_append_record_by_index(ndef_message_h ndef_message,
190 int index, ndef_record_h record)
192 return net_nfc_util_append_record_by_index(
193 (ndef_message_s *)ndef_message, index,
194 (ndef_record_s *) record);
198 net_nfc_error_e net_nfc_get_record_by_index(ndef_message_h ndef_message,
199 int index, ndef_record_h *record)
201 return net_nfc_util_get_record_by_index(
202 (ndef_message_s *)ndef_message, index,
203 (ndef_record_s **)record);
207 net_nfc_error_e net_nfc_remove_record_by_index(ndef_message_h ndef_message,
210 return net_nfc_util_remove_record_by_index(
211 (ndef_message_s*)ndef_message, index);
215 net_nfc_error_e net_nfc_retrieve_current_ndef_message(
216 ndef_message_h *ndef_message)
218 net_nfc_error_e result = NET_NFC_UNKNOWN_ERROR;
219 char file_path[PATH_MAX] = { 0, };
222 if (ndef_message == NULL)
224 return NET_NFC_NULL_PARAMETER;
227 snprintf(file_path, sizeof(file_path), "%s/%s/%s",
228 NET_NFC_MANAGER_DATA_PATH,
229 NET_NFC_MANAGER_DATA_PATH_MESSAGE,
230 NET_NFC_MANAGER_NDEF_FILE_NAME);
232 if ((fp = fopen(file_path, "r")) != NULL)
236 /* rewind to start of file */
237 fseek(fp, 0, SEEK_END);
239 fseek(fp, 0, SEEK_SET);
241 DEBUG_CLIENT_MSG("message length = [%ld]", size);
245 data_s data = { NULL, 0 };
247 if (net_nfc_util_init_data(&data, size) == true)
254 current = fread(data.buffer + offset, 1,
255 data.length - offset, fp);
260 } while (offset < data.length);
262 if (offset == data.length) {
263 result = net_nfc_create_ndef_message_from_rawdata(ndef_message, &data);
265 DEBUG_ERR_MSG("failed to read ndef message");
268 net_nfc_util_clear_data(&data);
272 result = NET_NFC_ALLOC_FAIL;
277 result = NET_NFC_NO_NDEF_MESSAGE;
284 result = NET_NFC_NO_NDEF_MESSAGE;