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.
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_h *ndef_message)
28 return net_nfc_util_create_ndef_message((ndef_message_s **)ndef_message);
31 API net_nfc_error_e net_nfc_create_rawdata_from_ndef_message(
32 ndef_message_h ndef_message, data_h *rawdata)
35 net_nfc_error_e result;
38 if (ndef_message == NULL || rawdata == NULL)
39 return NET_NFC_NULL_PARAMETER;
43 result = net_nfc_get_ndef_message_byte_length(ndef_message, &count);
44 if (result != NET_NFC_OK) {
48 result = net_nfc_create_data(&data ,NULL, count);
49 if (result != NET_NFC_OK) {
53 result = net_nfc_util_convert_ndef_message_to_rawdata(
54 (ndef_message_s *)ndef_message, (data_s *)data);
55 if (result == NET_NFC_OK)
58 net_nfc_free_data(data);
63 API net_nfc_error_e net_nfc_create_ndef_message_from_rawdata(
64 ndef_message_h *ndef_message, data_h rawdata)
66 net_nfc_error_e result;
69 if (ndef_message == NULL || rawdata == NULL)
70 return NET_NFC_NULL_PARAMETER;
74 result = net_nfc_create_ndef_message(&msg);
75 if (result != NET_NFC_OK)
78 result = net_nfc_util_convert_rawdata_to_ndef_message(
79 (data_s *)rawdata, (ndef_message_s *)msg);
80 if (result == NET_NFC_OK)
83 net_nfc_free_ndef_message(msg);
88 API net_nfc_error_e net_nfc_get_ndef_message_byte_length(
89 ndef_message_h ndef_message, uint32_t *length)
91 net_nfc_error_e result;
93 if (ndef_message == NULL || length == NULL)
94 return NET_NFC_NULL_PARAMETER;
96 *length = net_nfc_util_get_ndef_message_length((ndef_message_s *)ndef_message);
100 result = NET_NFC_INVALID_PARAM;
105 API net_nfc_error_e net_nfc_append_record_to_ndef_message(
106 ndef_message_h ndef_message, ndef_record_h record)
108 if (ndef_message == NULL || record == NULL)
109 return NET_NFC_NULL_PARAMETER;
111 return net_nfc_util_append_record((ndef_message_s*)ndef_message, (ndef_record_s *)record);
114 API net_nfc_error_e net_nfc_free_ndef_message(ndef_message_h ndef_message)
116 if (ndef_message == NULL)
117 return NET_NFC_NULL_PARAMETER;
119 return net_nfc_util_free_ndef_message((ndef_message_s *)ndef_message);
122 API net_nfc_error_e net_nfc_get_ndef_message_record_count(
123 ndef_message_h ndef_message, int *count)
125 ndef_message_s *msg = (ndef_message_s *)ndef_message;
127 if (ndef_message == NULL || count == NULL)
128 return NET_NFC_NULL_PARAMETER;
130 *count = msg->recordCount;
135 API void net_nfc_ndef_print_message (ndef_message_h ndef_message )
137 net_nfc_util_print_ndef_message((ndef_message_s *)(ndef_message));
141 API net_nfc_error_e net_nfc_search_record_by_type (ndef_message_h ndef_message,
142 net_nfc_record_tnf_e tnf, data_h type, ndef_record_h* record)
144 return net_nfc_util_search_record_by_type ((ndef_message_s*)ndef_message, tnf, (data_s *)type, (ndef_record_s**)record);
147 API net_nfc_error_e net_nfc_append_record_by_index (ndef_message_h ndef_message,
148 int index, ndef_record_h record)
150 return net_nfc_util_append_record_by_index ((ndef_message_s *) ndef_message, index, (ndef_record_s *) record);
153 API net_nfc_error_e net_nfc_get_record_by_index (ndef_message_h ndef_message,
154 int index, ndef_record_h* record)
156 return net_nfc_util_get_record_by_index ((ndef_message_s*) ndef_message, index, (ndef_record_s**) record);
159 API net_nfc_error_e net_nfc_remove_record_by_index (ndef_message_h ndef_message,
162 return net_nfc_util_remove_record_by_index ((ndef_message_s*)ndef_message, index);
165 API net_nfc_error_e net_nfc_retrieve_current_ndef_message(
166 ndef_message_h* ndef_message)
168 net_nfc_error_e result = NET_NFC_UNKNOWN_ERROR;
169 char file_path[PATH_MAX] = { 0, };
172 if (ndef_message == NULL)
173 return NET_NFC_NULL_PARAMETER;
175 snprintf(file_path, sizeof(file_path), "%s/%s/%s", NET_NFC_MANAGER_DATA_PATH,
176 NET_NFC_MANAGER_DATA_PATH_MESSAGE, NET_NFC_MANAGER_NDEF_FILE_NAME);
178 if ((fp = fopen(file_path, "r")) != NULL)
182 /* rewind to start of file */
183 fseek(fp, 0, SEEK_END);
185 fseek(fp, 0, SEEK_SET);
187 DEBUG_CLIENT_MSG("message length = [%ld]", size);
191 data_s data = { NULL, 0 };
193 if (net_nfc_util_alloc_data(&data, size) == true)
199 current = fread(data.buffer + offset, 1,
200 data.length - offset, fp);
205 } while (offset < data.length);
207 if (offset == data.length) {
208 result = net_nfc_create_ndef_message_from_rawdata(ndef_message, &data);
210 DEBUG_ERR_MSG("failed to read ndef message");
212 net_nfc_util_free_data(&data);
216 result = NET_NFC_ALLOC_FAIL;
221 result = NET_NFC_NO_NDEF_MESSAGE;
228 result = NET_NFC_NO_NDEF_MESSAGE;