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")))
33 net_nfc_error_e net_nfc_get_tag_type(net_nfc_target_info_h target_info,
34 net_nfc_target_type_e *type)
36 net_nfc_target_info_s *target_info_private =
37 (net_nfc_target_info_s *)target_info;
40 return NET_NFC_NULL_PARAMETER;
42 *type = NET_NFC_UNKNOWN_TARGET;
44 if (target_info == NULL)
45 return NET_NFC_INVALID_HANDLE;
47 *type = target_info_private->devType;
53 net_nfc_error_e net_nfc_get_tag_handle(net_nfc_target_info_h target_info,
54 net_nfc_target_handle_h *handle)
56 net_nfc_target_info_s *target_info_private =
57 (net_nfc_target_info_s *)target_info;
60 return NET_NFC_NULL_PARAMETER;
64 if (target_info == NULL)
65 return NET_NFC_INVALID_HANDLE;
67 *handle = (net_nfc_target_handle_h)target_info_private->handle;
73 net_nfc_error_e net_nfc_get_tag_ndef_support(net_nfc_target_info_h target_info,
76 net_nfc_target_info_s *target_info_private =
77 (net_nfc_target_info_s *)target_info;
79 if (target_info == NULL || is_support == NULL)
80 return NET_NFC_NULL_PARAMETER;
82 *is_support = (bool)target_info_private->is_ndef_supported;
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)
91 net_nfc_target_info_s *target_info_private =
92 (net_nfc_target_info_s *)target_info;
94 if (target_info == NULL || state == NULL)
95 return NET_NFC_NULL_PARAMETER;
97 *state = (net_nfc_ndef_card_state_e)target_info_private->ndefCardState;
103 net_nfc_error_e net_nfc_get_tag_max_data_size(net_nfc_target_info_h target_info,
106 net_nfc_target_info_s *target_info_private =
107 (net_nfc_target_info_s *)target_info;
109 if (target_info == NULL || max_size == NULL)
110 return NET_NFC_NULL_PARAMETER;
112 *max_size = target_info_private->maxDataSize;
118 net_nfc_error_e net_nfc_get_tag_actual_data_size(
119 net_nfc_target_info_h target_info, uint32_t * actual_data)
121 net_nfc_target_info_s *target_info_private =
122 (net_nfc_target_info_s *)target_info;
124 if (target_info == NULL || actual_data == NULL)
125 return NET_NFC_NULL_PARAMETER;
127 *actual_data = target_info_private->actualDataSize;
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)
136 net_nfc_target_info_s *handle = (net_nfc_target_info_s *)target_info;
139 if (keys == NULL || number_of_keys == NULL || target_info == NULL)
140 return NET_NFC_NULL_PARAMETER;
142 if (handle->tag_info_list == NULL)
143 return NET_NFC_NO_DATA_FOUND;
145 if (handle->number_of_keys <= 0)
146 return NET_NFC_NO_DATA_FOUND;
148 DEBUG_CLIENT_MSG("number of keys = [%d]", handle->number_of_keys);
150 if (handle->keylist != NULL) {
151 *number_of_keys = handle->number_of_keys;
152 *keys = handle->keylist;
157 _net_nfc_util_alloc_mem(*keys, handle->number_of_keys * sizeof(char *));
160 return NET_NFC_ALLOC_FAIL;
162 net_nfc_tag_info_s *tag_info = handle->tag_info_list;
164 for (; i < handle->number_of_keys; i++, tag_info++)
165 (*keys)[i] = tag_info->key;
167 *number_of_keys = handle->number_of_keys;
169 /* store local context */
170 handle->keylist = *keys;
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)
179 net_nfc_target_info_s *handle = (net_nfc_target_info_s *)target_info;
180 net_nfc_tag_info_s *tag_info;
182 if (target_info == NULL || key == NULL || value == NULL)
183 return NET_NFC_NULL_PARAMETER;
185 if (handle->tag_info_list == NULL)
186 return NET_NFC_NO_DATA_FOUND;
190 tag_info = handle->tag_info_list;
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;
197 *value = tag_info->value;
203 if (i == handle->number_of_keys)
204 return NET_NFC_NO_DATA_FOUND;
210 net_nfc_error_e net_nfc_get_tag_ndef_message(net_nfc_target_info_h target_info,
213 net_nfc_target_info_s *target_info_private =
214 (net_nfc_target_info_s *)target_info;
215 net_nfc_error_e result;
217 if (target_info == NULL || msg == NULL)
218 return NET_NFC_NULL_PARAMETER;
220 result = net_nfc_create_ndef_message_from_rawdata(msg,
221 (data_h)&target_info_private->raw_data);
227 net_nfc_error_e net_nfc_duplicate_target_info(net_nfc_target_info_h origin,
228 net_nfc_target_info_h *result)
230 net_nfc_target_info_s *handle = (net_nfc_target_info_s *)origin;
231 net_nfc_target_info_s *temp = NULL;
233 if (handle == NULL || result == NULL)
234 return NET_NFC_NULL_PARAMETER;
236 _net_nfc_util_alloc_mem(temp, sizeof(net_nfc_target_info_s));
238 return NET_NFC_ALLOC_FAIL;
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;
248 if (temp->number_of_keys > 0) {
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;
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);
261 if (handle->tag_info_list[i].value != NULL) {
262 data_s *data = (data_s *)handle->tag_info_list[i].value;
264 net_nfc_create_data(&temp->tag_info_list[i].value, data->buffer, data->length);
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);
274 *result = (net_nfc_target_info_h)temp;
279 static net_nfc_error_e _release_tag_info(net_nfc_target_info_s *info)
281 net_nfc_tag_info_s *list = NULL;
284 return NET_NFC_NULL_PARAMETER;
286 list = info->tag_info_list;
290 for (i = 0; i < info->number_of_keys; i++, list++) {
291 if (list->key != NULL)
292 _net_nfc_util_free_mem(list->key);
294 if (list->value != NULL)
295 net_nfc_free_data(list->value);
298 _net_nfc_util_free_mem(info->tag_info_list);
301 if (info->keylist != NULL)
302 _net_nfc_util_free_mem(info->keylist);
304 net_nfc_util_clear_data(&info->raw_data);
310 net_nfc_error_e net_nfc_release_tag_info(net_nfc_target_info_h target_info)
312 net_nfc_target_info_s *info = (net_nfc_target_info_s *)target_info;
313 net_nfc_error_e result;
316 return NET_NFC_NULL_PARAMETER;
318 result = _release_tag_info(info);
320 _net_nfc_util_free_mem(info);