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.
17 #include "net_nfc_ndef_record.h"
18 #include "net_nfc_ndef_message.h"
19 #include "net_nfc_data.h"
20 #include "net_nfc_debug_internal.h"
21 #include "net_nfc_util_internal.h"
22 #include "net_nfc_util_ndef_record.h"
24 #ifndef NET_NFC_EXPORT_API
25 #define NET_NFC_EXPORT_API __attribute__((visibility("default")))
29 net_nfc_error_e net_nfc_create_record(ndef_record_h *record,
30 net_nfc_record_tnf_e tnf, data_h typeName, data_h id, data_h payload)
32 return net_nfc_util_create_record(tnf, (data_s *)typeName,
33 (data_s *)id, (data_s *)payload, (ndef_record_s **)record);
37 net_nfc_error_e net_nfc_create_text_type_record(ndef_record_h *record,
38 const char *text, const char *language_code_str,
39 net_nfc_encode_type_e encode)
41 return net_nfc_util_create_text_type_record(text, language_code_str,
42 encode, (ndef_record_s **)record);
46 net_nfc_error_e net_nfc_create_uri_type_record(ndef_record_h *record,
47 const char *uri, net_nfc_schema_type_e protocol_schema)
49 return net_nfc_util_create_uri_type_record(uri, protocol_schema,
50 (ndef_record_s **)record);
54 net_nfc_error_e net_nfc_free_record(ndef_record_h record)
56 return net_nfc_util_free_record((ndef_record_s *)record);
60 net_nfc_error_e net_nfc_get_record_payload(ndef_record_h record,
63 ndef_record_s *struct_record = (ndef_record_s *)record;
65 if (record == NULL || payload == NULL)
67 return NET_NFC_NULL_PARAMETER;
70 *payload = (data_h)&(struct_record->payload_s);
76 net_nfc_error_e net_nfc_get_record_type(ndef_record_h record, data_h *type)
78 ndef_record_s *struct_record = (ndef_record_s *)record;
80 if (record == NULL || type == NULL)
82 return NET_NFC_NULL_PARAMETER;
85 *type = (data_h)&(struct_record->type_s);
91 net_nfc_error_e net_nfc_get_record_id(ndef_record_h record, data_h *id)
93 ndef_record_s *struct_record = (ndef_record_s *)record;
95 if (record == NULL || id == NULL)
97 return NET_NFC_NULL_PARAMETER;
100 *id = (data_h)&(struct_record->id_s);
106 net_nfc_error_e net_nfc_get_record_tnf(ndef_record_h record,
107 net_nfc_record_tnf_e *TNF)
109 ndef_record_s *struct_record = (ndef_record_s *)record;
111 if (record == NULL || TNF == NULL)
113 return NET_NFC_NULL_PARAMETER;
116 *TNF = (net_nfc_record_tnf_e)struct_record->TNF;
122 net_nfc_error_e net_nfc_set_record_id(ndef_record_h record, data_h id)
124 data_s *tmp_id = (data_s *)id;
126 if (record == NULL || tmp_id == NULL)
128 return NET_NFC_NULL_PARAMETER;
131 return net_nfc_util_set_record_id((ndef_record_s *)record,
132 tmp_id->buffer, tmp_id->length);
136 net_nfc_error_e net_nfc_get_record_flags(ndef_record_h record, uint8_t *flag)
138 ndef_record_s *struct_record = (ndef_record_s *)record;
140 if (record == NULL || flag == NULL)
142 return NET_NFC_NULL_PARAMETER;
145 *flag = struct_record->MB;
147 *flag += struct_record->ME;
149 *flag += struct_record->CF;
151 *flag += struct_record->SR;
153 *flag += struct_record->IL;
155 *flag += struct_record->TNF;
160 NET_NFC_EXPORT_API uint8_t net_nfc_get_record_mb(uint8_t flag)
162 return ((flag >> 7) & 0x01);
165 NET_NFC_EXPORT_API uint8_t net_nfc_get_record_me(uint8_t flag)
167 return ((flag >> 6) & 0x01);
170 NET_NFC_EXPORT_API uint8_t net_nfc_get_record_cf(uint8_t flag)
172 return ((flag >> 5) & 0x01);
175 NET_NFC_EXPORT_API uint8_t net_nfc_get_record_sr(uint8_t flag)
177 return ((flag >> 4) & 0x01);
180 NET_NFC_EXPORT_API uint8_t net_nfc_get_record_il(uint8_t flag)
182 return ((flag >> 3) & 0x01);
185 static bool _is_text_record(ndef_record_h record)
190 if ((net_nfc_get_record_type(record, &type) == NET_NFC_OK) &&
191 (strncmp((char *)net_nfc_get_data_buffer(type),
193 net_nfc_get_data_length(type)) == 0))
200 net_nfc_error_e net_nfc_create_text_string_from_text_record(
201 ndef_record_h record, char **buffer)
203 net_nfc_error_e result;
206 if (record == NULL || buffer == NULL)
208 return NET_NFC_NULL_PARAMETER;
213 if (_is_text_record(record) == false)
215 DEBUG_ERR_MSG("record type is not matched");
217 return NET_NFC_NDEF_RECORD_IS_NOT_EXPECTED_TYPE;
220 result = net_nfc_get_record_payload(record, &payload);
221 if (result == NET_NFC_OK)
223 uint8_t *buffer_temp = net_nfc_get_data_buffer(payload);
224 uint32_t buffer_length = net_nfc_get_data_length(payload);
226 int controllbyte = buffer_temp[0];
227 int lang_code_length = controllbyte & 0x3F;
228 int index = lang_code_length + 1;
229 int text_length = buffer_length - (lang_code_length + 1);
233 _net_nfc_util_alloc_mem(temp, text_length + 1);
236 memcpy(temp, &(buffer_temp[index]), text_length);
238 DEBUG_CLIENT_MSG("text = [%s]", temp);
244 result = NET_NFC_ALLOC_FAIL;
252 net_nfc_error_e net_nfc_get_languange_code_string_from_text_record(
253 ndef_record_h record, char **lang_code_str)
255 net_nfc_error_e result;
258 if (record == NULL || lang_code_str == NULL)
260 return NET_NFC_NULL_PARAMETER;
263 *lang_code_str = NULL;
265 if (_is_text_record(record) == false)
267 DEBUG_ERR_MSG("record type is not matched");
269 return NET_NFC_NDEF_RECORD_IS_NOT_EXPECTED_TYPE;
272 result = net_nfc_get_record_payload(record, &payload);
273 if (result == NET_NFC_OK)
275 uint8_t *buffer_temp = net_nfc_get_data_buffer(payload);
278 int controllbyte = buffer_temp[0];
279 int lang_code_length = controllbyte & 0x3F;
282 _net_nfc_util_alloc_mem(buffer, lang_code_length + 1);
285 memcpy(buffer, &(buffer_temp[index]), lang_code_length);
287 DEBUG_CLIENT_MSG("language code = [%s]", buffer);
289 *lang_code_str = buffer;
293 result = NET_NFC_ALLOC_FAIL;
301 net_nfc_error_e net_nfc_get_encoding_type_from_text_record(ndef_record_h record,
302 net_nfc_encode_type_e *encoding)
304 net_nfc_error_e result;
307 if (record == NULL || encoding == NULL)
309 return NET_NFC_NULL_PARAMETER;
312 *encoding = NET_NFC_ENCODE_UTF_8;
314 if (_is_text_record(record) == false)
316 DEBUG_ERR_MSG("record type is not matched");
318 return NET_NFC_NDEF_RECORD_IS_NOT_EXPECTED_TYPE;
321 result = net_nfc_get_record_payload(record, &payload);
322 if (result == NET_NFC_OK)
324 uint8_t *buffer_temp = net_nfc_get_data_buffer(payload);
326 int controllbyte = buffer_temp[0];
328 if ((controllbyte & 0x80) == 0x80)
330 *encoding = NET_NFC_ENCODE_UTF_16;
338 net_nfc_error_e net_nfc_create_uri_string_from_uri_record(ndef_record_h record,
341 return net_nfc_util_create_uri_string_from_uri_record(
342 (ndef_record_s *)record, uri);