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.
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"
26 #ifndef NET_NFC_EXPORT_API
27 #define NET_NFC_EXPORT_API __attribute__((visibility("default")))
31 net_nfc_error_e net_nfc_get_tag_type(net_nfc_target_info_h target_info,
32 net_nfc_target_type_e *type)
34 net_nfc_target_info_s *target_info_private =
35 (net_nfc_target_info_s *)target_info;
39 return NET_NFC_NULL_PARAMETER;
42 *type = NET_NFC_UNKNOWN_TARGET;
44 if (target_info == NULL)
46 return NET_NFC_INVALID_HANDLE;
49 *type = target_info_private->devType;
55 net_nfc_error_e net_nfc_get_tag_handle(net_nfc_target_info_h target_info,
56 net_nfc_target_handle_h *handle)
58 net_nfc_target_info_s *target_info_private =
59 (net_nfc_target_info_s *)target_info;
63 return NET_NFC_NULL_PARAMETER;
68 if (target_info == NULL)
70 return NET_NFC_INVALID_HANDLE;
73 *handle = (net_nfc_target_handle_h)target_info_private->handle;
79 net_nfc_error_e net_nfc_get_tag_ndef_support(net_nfc_target_info_h target_info,
82 net_nfc_target_info_s *target_info_private =
83 (net_nfc_target_info_s *)target_info;
85 if (target_info == NULL || is_support == NULL)
87 return NET_NFC_NULL_PARAMETER;
90 *is_support = (bool)target_info_private->is_ndef_supported;
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)
99 net_nfc_target_info_s *target_info_private =
100 (net_nfc_target_info_s *)target_info;
102 if (target_info == NULL || state == NULL)
104 return NET_NFC_NULL_PARAMETER;
107 *state = (net_nfc_ndef_card_state_e)target_info_private->ndefCardState;
113 net_nfc_error_e net_nfc_get_tag_max_data_size(net_nfc_target_info_h target_info,
116 net_nfc_target_info_s *target_info_private =
117 (net_nfc_target_info_s *)target_info;
119 if (target_info == NULL || max_size == NULL)
121 return NET_NFC_NULL_PARAMETER;
124 *max_size = target_info_private->maxDataSize;
130 net_nfc_error_e net_nfc_get_tag_actual_data_size(
131 net_nfc_target_info_h target_info, uint32_t * actual_data)
133 net_nfc_target_info_s *target_info_private =
134 (net_nfc_target_info_s *)target_info;
136 if (target_info == NULL || actual_data == NULL)
138 return NET_NFC_NULL_PARAMETER;
141 *actual_data = target_info_private->actualDataSize;
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)
150 net_nfc_target_info_s *handle = (net_nfc_target_info_s *)target_info;
153 if (keys == NULL || number_of_keys == NULL || target_info == NULL)
155 return NET_NFC_NULL_PARAMETER;
158 if (handle->tag_info_list == NULL)
160 return NET_NFC_NO_DATA_FOUND;
163 if (handle->number_of_keys <= 0)
165 return NET_NFC_NO_DATA_FOUND;
168 DEBUG_CLIENT_MSG("number of keys = [%d]", handle->number_of_keys);
170 if (handle->keylist != NULL)
172 *number_of_keys = handle->number_of_keys;
173 *keys = handle->keylist;
178 i = handle->number_of_keys * sizeof(char *);
180 _net_nfc_util_alloc_mem(*keys, i);
182 return NET_NFC_ALLOC_FAIL;
184 net_nfc_tag_info_s *tag_info = handle->tag_info_list;
186 for (; i < handle->number_of_keys; i++, tag_info++)
188 (*keys)[i] = tag_info->key;
191 *number_of_keys = handle->number_of_keys;
193 /* store local context */
194 handle->keylist = *keys;
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)
203 net_nfc_target_info_s *handle = (net_nfc_target_info_s *)target_info;
204 net_nfc_tag_info_s *tag_info;
206 if (target_info == NULL || key == NULL || value == NULL)
208 return NET_NFC_NULL_PARAMETER;
211 if (handle->tag_info_list == NULL)
213 return NET_NFC_NO_DATA_FOUND;
218 tag_info = handle->tag_info_list;
220 for (; i < handle->number_of_keys; i++, tag_info++)
222 if (strcmp(key, tag_info->key) == 0)
224 if (tag_info->value == NULL)
226 return NET_NFC_NO_DATA_FOUND;
230 *value = tag_info->value;
236 if (i == handle->number_of_keys)
237 return NET_NFC_NO_DATA_FOUND;
243 net_nfc_error_e net_nfc_get_tag_ndef_message(net_nfc_target_info_h target_info,
246 net_nfc_target_info_s *target_info_private =
247 (net_nfc_target_info_s *)target_info;
248 net_nfc_error_e result;
250 if (target_info == NULL || msg == NULL)
252 return NET_NFC_NULL_PARAMETER;
255 result = net_nfc_create_ndef_message_from_rawdata(msg,
256 (data_h)&target_info_private->raw_data);
262 net_nfc_error_e net_nfc_duplicate_target_info(net_nfc_target_info_h origin,
263 net_nfc_target_info_h *result)
265 net_nfc_target_info_s *handle = (net_nfc_target_info_s *)origin;
266 net_nfc_target_info_s *temp = NULL;
268 if (handle == NULL || result == NULL)
270 return NET_NFC_NULL_PARAMETER;
273 _net_nfc_util_alloc_mem(temp, sizeof(net_nfc_target_info_s));
276 return NET_NFC_ALLOC_FAIL;
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;
287 if (temp->number_of_keys > 0)
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)
294 _net_nfc_util_free_mem(temp);
295 return NET_NFC_ALLOC_FAIL;
298 for (i = 0; i < handle->number_of_keys; i++)
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);
303 if (handle->tag_info_list[i].value != NULL)
305 data_s *data = (data_s *)handle->tag_info_list[i].value;
307 net_nfc_create_data(&temp->tag_info_list[i].value, data->buffer, data->length);
312 if (handle->raw_data.length > 0)
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);
318 *result = (net_nfc_target_info_h)temp;
323 static net_nfc_error_e _release_tag_info(net_nfc_target_info_s *info)
325 net_nfc_tag_info_s *list = NULL;
328 return NET_NFC_NULL_PARAMETER;
330 list = info->tag_info_list;
335 for (i = 0; i < info->number_of_keys; i++, list++)
337 if (list->key != NULL)
338 _net_nfc_util_free_mem(list->key);
340 if (list->value != NULL)
341 net_nfc_free_data(list->value);
344 _net_nfc_util_free_mem(info->tag_info_list);
347 if (info->keylist != NULL)
349 _net_nfc_util_free_mem(info->keylist);
352 net_nfc_util_clear_data(&info->raw_data);
358 net_nfc_error_e net_nfc_release_tag_info(net_nfc_target_info_h target_info)
360 net_nfc_target_info_s *info = (net_nfc_target_info_s *)target_info;
361 net_nfc_error_e result;
364 return NET_NFC_NULL_PARAMETER;
366 result = _release_tag_info(info);
368 _net_nfc_util_free_mem(info);