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")))
32 #ifndef NET_NFC_MANAGER_DATA_PATH
33 #define NET_NFC_MANAGER_DATA_PATH "/opt/usr/share/nfc-manager-daemon"
36 #ifndef NET_NFC_MANAGER_DATA_PATH_MESSAGE
37 #define NET_NFC_MANAGER_DATA_PATH_MESSAGE "message"
40 #ifndef NET_NFC_MANAGER_NDEF_FILE_NAME
41 #define NET_NFC_MANAGER_NDEF_FILE_NAME "ndef-message.txt"
45 /* public functions */
47 net_nfc_error_e net_nfc_create_ndef_message(ndef_message_h *ndef_message)
49 return net_nfc_util_create_ndef_message(
50 (ndef_message_s **)ndef_message);
54 net_nfc_error_e net_nfc_create_rawdata_from_ndef_message(
55 ndef_message_h ndef_message, data_h *rawdata)
58 net_nfc_error_e result;
61 if (ndef_message == NULL || rawdata == NULL)
62 return NET_NFC_NULL_PARAMETER;
66 result = net_nfc_get_ndef_message_byte_length(ndef_message, &count);
67 if (result != NET_NFC_OK)
70 result = net_nfc_create_data(&data, NULL, count);
71 if (result != NET_NFC_OK)
74 result = net_nfc_util_convert_ndef_message_to_rawdata(
75 (ndef_message_s *)ndef_message, (data_s *)data);
76 if (result == NET_NFC_OK)
79 net_nfc_free_data(data);
85 net_nfc_error_e net_nfc_create_ndef_message_from_rawdata(
86 ndef_message_h *ndef_message, data_h rawdata)
88 net_nfc_error_e result;
91 if (ndef_message == NULL || rawdata == NULL)
92 return NET_NFC_NULL_PARAMETER;
96 result = net_nfc_create_ndef_message(&msg);
97 if (result != NET_NFC_OK)
100 result = net_nfc_util_convert_rawdata_to_ndef_message(
101 (data_s *)rawdata, (ndef_message_s *)msg);
102 if (result == NET_NFC_OK)
105 net_nfc_free_ndef_message(msg);
111 net_nfc_error_e net_nfc_get_ndef_message_byte_length(
112 ndef_message_h ndef_message, uint32_t *length)
114 net_nfc_error_e result;
116 if (ndef_message == NULL || length == NULL)
117 return NET_NFC_NULL_PARAMETER;
119 *length = net_nfc_util_get_ndef_message_length(
120 (ndef_message_s *)ndef_message);
124 result = NET_NFC_INVALID_PARAM;
130 net_nfc_error_e net_nfc_append_record_to_ndef_message(
131 ndef_message_h ndef_message, ndef_record_h record)
133 if (ndef_message == NULL || record == NULL)
134 return NET_NFC_NULL_PARAMETER;
136 return net_nfc_util_append_record((ndef_message_s *)ndef_message,
137 (ndef_record_s *)record);
141 net_nfc_error_e net_nfc_free_ndef_message(ndef_message_h ndef_message)
143 if (ndef_message == NULL)
144 return NET_NFC_NULL_PARAMETER;
146 return net_nfc_util_free_ndef_message((ndef_message_s *)ndef_message);
150 net_nfc_error_e net_nfc_get_ndef_message_record_count(
151 ndef_message_h ndef_message, int *count)
153 ndef_message_s *msg = (ndef_message_s *)ndef_message;
155 if (ndef_message == NULL || count == NULL)
156 return NET_NFC_NULL_PARAMETER;
158 *count = msg->recordCount;
163 NET_NFC_EXPORT_API void net_nfc_ndef_print_message(ndef_message_h ndef_message)
165 net_nfc_util_print_ndef_message((ndef_message_s *)ndef_message);
170 net_nfc_error_e net_nfc_search_record_by_type(ndef_message_h ndef_message,
171 net_nfc_record_tnf_e tnf, data_h type, ndef_record_h *record)
173 return net_nfc_util_search_record_by_type(
174 (ndef_message_s *)ndef_message, tnf,
175 (data_s *)type, (ndef_record_s **)record);
179 net_nfc_error_e net_nfc_append_record_by_index(ndef_message_h ndef_message,
180 int index, ndef_record_h record)
182 return net_nfc_util_append_record_by_index(
183 (ndef_message_s *)ndef_message, index,
184 (ndef_record_s *) record);
188 net_nfc_error_e net_nfc_get_record_by_index(ndef_message_h ndef_message,
189 int index, ndef_record_h *record)
191 return net_nfc_util_get_record_by_index(
192 (ndef_message_s *)ndef_message, index,
193 (ndef_record_s **)record);
197 net_nfc_error_e net_nfc_remove_record_by_index(ndef_message_h ndef_message,
200 return net_nfc_util_remove_record_by_index(
201 (ndef_message_s*)ndef_message, index);
205 net_nfc_error_e net_nfc_retrieve_current_ndef_message(
206 ndef_message_h *ndef_message)
208 net_nfc_error_e result = NET_NFC_UNKNOWN_ERROR;
209 char file_path[PATH_MAX] = { 0, };
212 if (ndef_message == NULL)
213 return NET_NFC_NULL_PARAMETER;
215 snprintf(file_path, sizeof(file_path), "%s/%s/%s",
216 NET_NFC_MANAGER_DATA_PATH,
217 NET_NFC_MANAGER_DATA_PATH_MESSAGE,
218 NET_NFC_MANAGER_NDEF_FILE_NAME);
220 if ((fp = fopen(file_path, "r")) != NULL) {
223 /* rewind to start of file */
224 fseek(fp, 0, SEEK_END);
226 fseek(fp, 0, SEEK_SET);
228 DEBUG_CLIENT_MSG("message length = [%ld]", size);
231 data_s data = { NULL, 0 };
233 if (net_nfc_util_init_data(&data, size) == true) {
239 current = fread(data.buffer + offset, 1,
240 data.length - offset, fp);
245 } while (offset < data.length);
247 if (offset == data.length)
248 result = net_nfc_create_ndef_message_from_rawdata(ndef_message, &data);
250 DEBUG_ERR_MSG("failed to read ndef message");
252 net_nfc_util_clear_data(&data);
254 result = NET_NFC_ALLOC_FAIL;
257 result = NET_NFC_NO_NDEF_MESSAGE;
262 result = NET_NFC_NO_NDEF_MESSAGE;