Fix doxygen
[platform/core/api/nfc.git] / src / net_nfc_client_target_info.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 <stdbool.h>
18
19 #include "net_nfc_typedef_internal.h"
20 #include "net_nfc_debug_internal.h"
21 #include "net_nfc_data.h"
22 #include "net_nfc_target_info.h"
23 #include "net_nfc_util_internal.h"
24 #include "net_nfc_ndef_message.h"
25
26 #ifndef NET_NFC_EXPORT_API
27 #define NET_NFC_EXPORT_API __attribute__((visibility("default")))
28 #endif
29
30 /* LCOV_EXCL_START */
31
32 NET_NFC_EXPORT_API
33 net_nfc_error_e net_nfc_get_tag_type(net_nfc_target_info_h target_info,
34         net_nfc_target_type_e *type)
35 {
36         net_nfc_target_info_s *target_info_private =
37                 (net_nfc_target_info_s *)target_info;
38
39         if (type == NULL)
40                 return NET_NFC_NULL_PARAMETER;
41
42         *type = NET_NFC_UNKNOWN_TARGET;
43
44         if (target_info == NULL)
45                 return NET_NFC_INVALID_HANDLE;
46
47         *type = target_info_private->devType;
48
49         return NET_NFC_OK;
50 }
51
52 NET_NFC_EXPORT_API
53 net_nfc_error_e net_nfc_get_tag_handle(net_nfc_target_info_h target_info,
54         net_nfc_target_handle_h *handle)
55 {
56         net_nfc_target_info_s *target_info_private =
57                 (net_nfc_target_info_s *)target_info;
58
59         if (handle == NULL)
60                 return NET_NFC_NULL_PARAMETER;
61
62         *handle = NULL;
63
64         if (target_info == NULL)
65                 return NET_NFC_INVALID_HANDLE;
66
67         *handle = (net_nfc_target_handle_h)target_info_private->handle;
68
69         return NET_NFC_OK;
70 }
71
72 NET_NFC_EXPORT_API
73 net_nfc_error_e net_nfc_get_tag_ndef_support(net_nfc_target_info_h target_info,
74         bool *is_support)
75 {
76         net_nfc_target_info_s *target_info_private =
77                 (net_nfc_target_info_s *)target_info;
78
79         if (target_info == NULL || is_support == NULL)
80                 return NET_NFC_NULL_PARAMETER;
81
82         *is_support = (bool)target_info_private->is_ndef_supported;
83
84         return NET_NFC_OK;
85 }
86
87 NET_NFC_EXPORT_API
88 net_nfc_error_e net_nfc_get_tag_ndef_state(net_nfc_target_info_h target_info,
89         net_nfc_ndef_card_state_e *state)
90 {
91         net_nfc_target_info_s *target_info_private =
92                 (net_nfc_target_info_s *)target_info;
93
94         if (target_info == NULL || state == NULL)
95                 return NET_NFC_NULL_PARAMETER;
96
97         *state = (net_nfc_ndef_card_state_e)target_info_private->ndefCardState;
98
99         return NET_NFC_OK;
100 }
101
102 NET_NFC_EXPORT_API
103 net_nfc_error_e net_nfc_get_tag_max_data_size(net_nfc_target_info_h target_info,
104         uint32_t *max_size)
105 {
106         net_nfc_target_info_s *target_info_private =
107                 (net_nfc_target_info_s *)target_info;
108
109         if (target_info == NULL || max_size == NULL)
110                 return NET_NFC_NULL_PARAMETER;
111
112         *max_size = target_info_private->maxDataSize;
113
114         return NET_NFC_OK;
115 }
116
117 NET_NFC_EXPORT_API
118 net_nfc_error_e net_nfc_get_tag_actual_data_size(
119         net_nfc_target_info_h target_info, uint32_t * actual_data)
120 {
121         net_nfc_target_info_s *target_info_private =
122                 (net_nfc_target_info_s *)target_info;
123
124         if (target_info == NULL || actual_data == NULL)
125                 return NET_NFC_NULL_PARAMETER;
126
127         *actual_data = target_info_private->actualDataSize;
128
129         return NET_NFC_OK;
130 }
131
132 NET_NFC_EXPORT_API
133 net_nfc_error_e net_nfc_get_tag_info_keys(net_nfc_target_info_h target_info,
134         char ***keys, int * number_of_keys)
135 {
136         net_nfc_target_info_s *handle = (net_nfc_target_info_s *)target_info;
137         int i = 0;
138
139         if (keys == NULL || number_of_keys == NULL || target_info == NULL)
140                 return NET_NFC_NULL_PARAMETER;
141
142         if (handle->tag_info_list == NULL)
143                 return NET_NFC_NO_DATA_FOUND;
144
145         if (handle->number_of_keys <= 0)
146                 return NET_NFC_NO_DATA_FOUND;
147
148         DEBUG_CLIENT_MSG("number of keys = [%d]", handle->number_of_keys);
149
150         if (handle->keylist != NULL) {
151                 *number_of_keys = handle->number_of_keys;
152                 *keys = handle->keylist;
153
154                 return NET_NFC_OK;
155         }
156
157         _net_nfc_util_alloc_mem(*keys, handle->number_of_keys * sizeof(char *));
158
159         if (*keys == NULL)
160                 return NET_NFC_ALLOC_FAIL;
161
162         net_nfc_tag_info_s *tag_info = handle->tag_info_list;
163
164         for (; i < handle->number_of_keys; i++, tag_info++)
165                 (*keys)[i] = tag_info->key;
166
167         *number_of_keys = handle->number_of_keys;
168
169         /* store local context */
170         handle->keylist = *keys;
171
172         return NET_NFC_OK;
173 }
174
175 NET_NFC_EXPORT_API
176 net_nfc_error_e net_nfc_get_tag_info_value(net_nfc_target_info_h target_info,
177         const char *key, data_h *value)
178 {
179         net_nfc_target_info_s *handle = (net_nfc_target_info_s *)target_info;
180         net_nfc_tag_info_s *tag_info;
181
182         if (target_info == NULL || key == NULL || value == NULL)
183                 return NET_NFC_NULL_PARAMETER;
184
185         if (handle->tag_info_list == NULL)
186                 return NET_NFC_NO_DATA_FOUND;
187
188         int i = 0;
189
190         tag_info = handle->tag_info_list;
191
192         for (; i < handle->number_of_keys; i++, tag_info++) {
193                 if (strcmp(key, tag_info->key) == 0) {
194                         if (tag_info->value == NULL) {
195                                 return NET_NFC_NO_DATA_FOUND;
196                         } else {
197                                 *value = tag_info->value;
198                                 break;
199                         }
200                 }
201         }
202
203         if (i == handle->number_of_keys)
204                 return NET_NFC_NO_DATA_FOUND;
205
206         return NET_NFC_OK;
207 }
208
209 NET_NFC_EXPORT_API
210 net_nfc_error_e net_nfc_get_tag_ndef_message(net_nfc_target_info_h target_info,
211         ndef_message_h *msg)
212 {
213         net_nfc_target_info_s *target_info_private =
214                 (net_nfc_target_info_s *)target_info;
215         net_nfc_error_e result;
216
217         if (target_info == NULL || msg == NULL)
218                 return NET_NFC_NULL_PARAMETER;
219
220         result = net_nfc_create_ndef_message_from_rawdata(msg,
221                 (data_h)&target_info_private->raw_data);
222
223         return result;
224 }
225
226 NET_NFC_EXPORT_API
227 net_nfc_error_e net_nfc_duplicate_target_info(net_nfc_target_info_h origin,
228         net_nfc_target_info_h *result)
229 {
230         net_nfc_target_info_s *handle = (net_nfc_target_info_s *)origin;
231         net_nfc_target_info_s *temp = NULL;
232
233         if (handle == NULL || result == NULL)
234                 return NET_NFC_NULL_PARAMETER;
235
236         _net_nfc_util_alloc_mem(temp, sizeof(net_nfc_target_info_s));
237         if (temp == NULL)
238                 return NET_NFC_ALLOC_FAIL;
239
240         temp->ndefCardState = handle->ndefCardState;
241         temp->actualDataSize = handle->actualDataSize;
242         temp->maxDataSize = handle->maxDataSize;
243         temp->devType = handle->devType;
244         temp->handle = handle->handle;
245         temp->is_ndef_supported = handle->is_ndef_supported;
246         temp->number_of_keys = handle->number_of_keys;
247
248         if (temp->number_of_keys > 0) {
249                 int i;
250
251                 _net_nfc_util_alloc_mem(temp->tag_info_list, temp->number_of_keys * sizeof(net_nfc_tag_info_s));
252                 if (temp->tag_info_list == NULL) {
253                         _net_nfc_util_free_mem(temp);
254                         return NET_NFC_ALLOC_FAIL;
255                 }
256
257                 for (i = 0; i < handle->number_of_keys; i++) {
258                         if (handle->tag_info_list[i].key != NULL)
259                                 _net_nfc_util_strdup(temp->tag_info_list[i].key, handle->tag_info_list[i].key);
260
261                         if (handle->tag_info_list[i].value != NULL) {
262                                 data_s *data = (data_s *)handle->tag_info_list[i].value;
263
264                                 net_nfc_create_data(&temp->tag_info_list[i].value, data->buffer, data->length);
265                         }
266                 }
267         }
268
269         if (handle->raw_data.length > 0) {
270                 net_nfc_util_init_data(&temp->raw_data, handle->raw_data.length);
271                 memcpy(temp->raw_data.buffer, handle->raw_data.buffer, temp->raw_data.length);
272         }
273
274         *result = (net_nfc_target_info_h)temp;
275
276         return NET_NFC_OK;
277 }
278
279 static net_nfc_error_e _release_tag_info(net_nfc_target_info_s *info)
280 {
281         net_nfc_tag_info_s *list = NULL;
282
283         if (info == NULL)
284                 return NET_NFC_NULL_PARAMETER;
285
286         list = info->tag_info_list;
287         if (list != NULL) {
288                 int i;
289
290                 for (i = 0; i < info->number_of_keys; i++, list++) {
291                         if (list->key != NULL)
292                                 _net_nfc_util_free_mem(list->key);
293
294                         if (list->value != NULL)
295                                 net_nfc_free_data(list->value);
296                 }
297
298                 _net_nfc_util_free_mem(info->tag_info_list);
299         }
300
301         if (info->keylist != NULL)
302                 _net_nfc_util_free_mem(info->keylist);
303
304         net_nfc_util_clear_data(&info->raw_data);
305
306         return NET_NFC_OK;
307 }
308
309 NET_NFC_EXPORT_API
310 net_nfc_error_e net_nfc_release_tag_info(net_nfc_target_info_h target_info)
311 {
312         net_nfc_target_info_s *info = (net_nfc_target_info_s *)target_info;
313         net_nfc_error_e result;
314
315         if (info == NULL)
316                 return NET_NFC_NULL_PARAMETER;
317
318         result = _release_tag_info(info);
319
320         _net_nfc_util_free_mem(info);
321
322         return result;
323 }
324
325 /* LCOV_EXCL_STOP */
326