Fix doxygen
[platform/core/api/nfc.git] / src / net_nfc_client_ndef_message.c
1 /*
2   * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
3   *
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
7
8   *     http://floralicense.org/license/
9   *
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.
15   */
16
17 #include <linux/limits.h>
18
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"
24
25 #ifndef NET_NFC_EXPORT_API
26 #define NET_NFC_EXPORT_API __attribute__((visibility("default")))
27 #endif
28
29 /* LCOV_EXCL_START */
30
31
32 #ifndef NET_NFC_MANAGER_DATA_PATH
33 #define NET_NFC_MANAGER_DATA_PATH               "/opt/usr/share/nfc-manager-daemon"
34 #endif
35
36 #ifndef NET_NFC_MANAGER_DATA_PATH_MESSAGE
37 #define NET_NFC_MANAGER_DATA_PATH_MESSAGE       "message"
38 #endif
39
40 #ifndef NET_NFC_MANAGER_NDEF_FILE_NAME
41 #define NET_NFC_MANAGER_NDEF_FILE_NAME          "ndef-message.txt"
42 #endif
43
44
45 /* public functions */
46 NET_NFC_EXPORT_API
47 net_nfc_error_e net_nfc_create_ndef_message(ndef_message_h *ndef_message)
48 {
49         return net_nfc_util_create_ndef_message(
50                 (ndef_message_s **)ndef_message);
51 }
52
53 NET_NFC_EXPORT_API
54 net_nfc_error_e net_nfc_create_rawdata_from_ndef_message(
55         ndef_message_h ndef_message, data_h *rawdata)
56 {
57         uint32_t count;
58         net_nfc_error_e result;
59         data_h data;
60
61         if (ndef_message == NULL || rawdata == NULL)
62                 return NET_NFC_NULL_PARAMETER;
63
64         *rawdata = NULL;
65
66         result = net_nfc_get_ndef_message_byte_length(ndef_message, &count);
67         if (result != NET_NFC_OK)
68                 return result;
69
70         result = net_nfc_create_data(&data, NULL, count);
71         if (result != NET_NFC_OK)
72                 return result;
73
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)
77                 *rawdata = data;
78         else
79                 net_nfc_free_data(data);
80
81         return result;
82 }
83
84 NET_NFC_EXPORT_API
85 net_nfc_error_e net_nfc_create_ndef_message_from_rawdata(
86         ndef_message_h *ndef_message, data_h rawdata)
87 {
88         net_nfc_error_e result = NET_NFC_OK;
89         ndef_message_h msg;
90
91         if (ndef_message == NULL || rawdata == NULL)
92                 return NET_NFC_NULL_PARAMETER;
93
94         *ndef_message = NULL;
95
96         result = net_nfc_create_ndef_message(&msg);
97         if (result != NET_NFC_OK)
98                 return result;
99
100         result = net_nfc_util_convert_rawdata_to_ndef_message(
101                 (data_s *)rawdata, (ndef_message_s *)msg);
102         if (result == NET_NFC_OK)
103                 *ndef_message = msg;
104         else
105                 net_nfc_free_ndef_message(msg);
106
107         return result;
108 }
109
110 NET_NFC_EXPORT_API
111 net_nfc_error_e net_nfc_get_ndef_message_byte_length(
112         ndef_message_h ndef_message, uint32_t *length)
113 {
114         net_nfc_error_e result;
115
116         if (ndef_message == NULL || length == NULL)
117                 return NET_NFC_NULL_PARAMETER;
118
119         *length = net_nfc_util_get_ndef_message_length(
120                 (ndef_message_s *)ndef_message);
121         if (*length > 0)
122                 result = NET_NFC_OK;
123         else
124                 result = NET_NFC_INVALID_PARAM;
125
126         return result;
127 }
128
129 NET_NFC_EXPORT_API
130 net_nfc_error_e net_nfc_append_record_to_ndef_message(
131         ndef_message_h ndef_message, ndef_record_h record)
132 {
133         if (ndef_message == NULL || record == NULL)
134                 return NET_NFC_NULL_PARAMETER;
135
136         return net_nfc_util_append_record((ndef_message_s *)ndef_message,
137                 (ndef_record_s *)record);
138 }
139
140 NET_NFC_EXPORT_API
141 net_nfc_error_e net_nfc_free_ndef_message(ndef_message_h ndef_message)
142 {
143         if (ndef_message == NULL)
144                 return NET_NFC_NULL_PARAMETER;
145
146         return net_nfc_util_free_ndef_message((ndef_message_s *)ndef_message);
147 }
148
149 NET_NFC_EXPORT_API
150 net_nfc_error_e net_nfc_get_ndef_message_record_count(
151         ndef_message_h ndef_message, int *count)
152 {
153         ndef_message_s *msg = (ndef_message_s *)ndef_message;
154
155         if (ndef_message == NULL || count == NULL)
156                 return NET_NFC_NULL_PARAMETER;
157
158         *count = msg->recordCount;
159
160         return NET_NFC_OK;
161 }
162
163 NET_NFC_EXPORT_API void net_nfc_ndef_print_message(ndef_message_h ndef_message)
164 {
165         net_nfc_util_print_ndef_message((ndef_message_s *)ndef_message);
166 }
167
168
169 NET_NFC_EXPORT_API
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)
172 {
173         return net_nfc_util_search_record_by_type(
174                 (ndef_message_s *)ndef_message, tnf,
175                 (data_s *)type, (ndef_record_s **)record);
176 }
177
178 NET_NFC_EXPORT_API
179 net_nfc_error_e net_nfc_append_record_by_index(ndef_message_h ndef_message,
180         int index, ndef_record_h record)
181 {
182         return net_nfc_util_append_record_by_index(
183                 (ndef_message_s *)ndef_message, index,
184                 (ndef_record_s *) record);
185 }
186
187 NET_NFC_EXPORT_API
188 net_nfc_error_e net_nfc_get_record_by_index(ndef_message_h ndef_message,
189         int index, ndef_record_h *record)
190 {
191         return net_nfc_util_get_record_by_index(
192                 (ndef_message_s *)ndef_message, index,
193                 (ndef_record_s **)record);
194 }
195
196 NET_NFC_EXPORT_API
197 net_nfc_error_e net_nfc_remove_record_by_index(ndef_message_h ndef_message,
198         int index)
199 {
200         return net_nfc_util_remove_record_by_index(
201                 (ndef_message_s*)ndef_message, index);
202 }
203
204 NET_NFC_EXPORT_API
205 net_nfc_error_e net_nfc_retrieve_current_ndef_message(
206         ndef_message_h *ndef_message)
207 {
208         net_nfc_error_e result = NET_NFC_UNKNOWN_ERROR;
209         char file_path[PATH_MAX] = { 0, };
210         FILE *fp = NULL;
211
212         if (ndef_message == NULL)
213                 return NET_NFC_NULL_PARAMETER;
214
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);
219
220         if ((fp = fopen(file_path, "r")) != NULL) {
221                 long int size = 0;
222
223                 /* rewind to start of file */
224                 fseek(fp, 0, SEEK_END);
225                 size = ftell(fp);
226                 fseek(fp, 0, SEEK_SET);
227
228                 DEBUG_CLIENT_MSG("message length = [%ld]", size);
229
230                 if (size > 0) {
231                         data_s data = { NULL, 0 };
232
233                         if (net_nfc_util_init_data(&data, size) == true) {
234                                 int current;
235                                 size_t offset = 0;
236
237                                 /* read fully */
238                                 do {
239                                         current = fread(data.buffer + offset, 1,
240                                                 data.length - offset, fp);
241                                         if (current > 0)
242                                                 offset += current;
243                                         else
244                                                 break;
245                                 } while (offset < data.length);
246
247                                 if (offset == data.length)
248                                         result = net_nfc_create_ndef_message_from_rawdata(ndef_message, &data);
249                                 else
250                                         DEBUG_ERR_MSG("failed to read ndef message");
251
252                                 net_nfc_util_clear_data(&data);
253                         } else {
254                                 result = NET_NFC_ALLOC_FAIL;
255                         }
256                 } else {
257                         result = NET_NFC_NO_NDEF_MESSAGE;
258                 }
259
260                 fclose(fp);
261         } else {
262                 result = NET_NFC_NO_NDEF_MESSAGE;
263         }
264
265         return result;
266 }
267
268 /* LCOV_EXCL_STOP */
269