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