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")))
31 net_nfc_error_e net_nfc_create_record(ndef_record_h *record,
32 net_nfc_record_tnf_e tnf, data_h typeName, data_h id, data_h payload)
34 return net_nfc_util_create_record(tnf, (data_s *)typeName,
35 (data_s *)id, (data_s *)payload, (ndef_record_s **)record);
39 net_nfc_error_e net_nfc_create_text_type_record(ndef_record_h *record,
40 const char *text, const char *language_code_str,
41 net_nfc_encode_type_e encode)
43 return net_nfc_util_create_text_type_record(text, language_code_str,
44 encode, (ndef_record_s **)record);
48 net_nfc_error_e net_nfc_create_uri_type_record(ndef_record_h *record,
49 const char *uri, net_nfc_schema_type_e protocol_schema)
51 return net_nfc_util_create_uri_type_record(uri, protocol_schema,
52 (ndef_record_s **)record);
56 net_nfc_error_e net_nfc_free_record(ndef_record_h record)
58 return net_nfc_util_free_record((ndef_record_s *)record);
62 net_nfc_error_e net_nfc_get_record_payload(ndef_record_h record,
65 ndef_record_s *struct_record = (ndef_record_s *)record;
67 if (record == NULL || payload == NULL)
68 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)
81 return NET_NFC_NULL_PARAMETER;
83 *type = (data_h)&(struct_record->type_s);
89 net_nfc_error_e net_nfc_get_record_id(ndef_record_h record, data_h *id)
91 ndef_record_s *struct_record = (ndef_record_s *)record;
93 if (record == NULL || id == NULL)
94 return NET_NFC_NULL_PARAMETER;
96 *id = (data_h)&(struct_record->id_s);
102 net_nfc_error_e net_nfc_get_record_tnf(ndef_record_h record,
103 net_nfc_record_tnf_e *TNF)
105 ndef_record_s *struct_record = (ndef_record_s *)record;
107 if (record == NULL || TNF == NULL)
108 return NET_NFC_NULL_PARAMETER;
110 *TNF = (net_nfc_record_tnf_e)struct_record->TNF;
116 net_nfc_error_e net_nfc_set_record_id(ndef_record_h record, data_h id)
118 data_s *tmp_id = (data_s *)id;
120 if (record == NULL || tmp_id == NULL)
121 return NET_NFC_NULL_PARAMETER;
123 return net_nfc_util_set_record_id((ndef_record_s *)record,
124 tmp_id->buffer, tmp_id->length);
128 net_nfc_error_e net_nfc_get_record_flags(ndef_record_h record, uint8_t *flag)
130 ndef_record_s *struct_record = (ndef_record_s *)record;
132 if (record == NULL || flag == NULL)
133 return NET_NFC_NULL_PARAMETER;
135 *flag = struct_record->MB;
137 *flag += struct_record->ME;
139 *flag += struct_record->CF;
141 *flag += struct_record->SR;
143 *flag += struct_record->IL;
145 *flag += struct_record->TNF;
150 NET_NFC_EXPORT_API uint8_t net_nfc_get_record_mb(uint8_t flag)
152 return ((flag >> 7) & 0x01);
155 NET_NFC_EXPORT_API uint8_t net_nfc_get_record_me(uint8_t flag)
157 return ((flag >> 6) & 0x01);
160 NET_NFC_EXPORT_API uint8_t net_nfc_get_record_cf(uint8_t flag)
162 return ((flag >> 5) & 0x01);
165 NET_NFC_EXPORT_API uint8_t net_nfc_get_record_sr(uint8_t flag)
167 return ((flag >> 4) & 0x01);
170 NET_NFC_EXPORT_API uint8_t net_nfc_get_record_il(uint8_t flag)
172 return ((flag >> 3) & 0x01);
175 static bool _is_text_record(ndef_record_h record)
180 if ((net_nfc_get_record_type(record, &type) == NET_NFC_OK) &&
181 (strncmp((char *)net_nfc_get_data_buffer(type),
183 net_nfc_get_data_length(type)) == 0))
190 net_nfc_error_e net_nfc_create_text_string_from_text_record(
191 ndef_record_h record, char **buffer)
193 net_nfc_error_e result;
196 if (record == NULL || buffer == NULL)
197 return NET_NFC_NULL_PARAMETER;
201 if (_is_text_record(record) == false) {
202 DEBUG_ERR_MSG("record type is not matched");
204 return NET_NFC_NDEF_RECORD_IS_NOT_EXPECTED_TYPE;
207 result = net_nfc_get_record_payload(record, &payload);
208 if (result == NET_NFC_OK) {
209 uint8_t *buffer_temp = net_nfc_get_data_buffer(payload);
210 uint32_t buffer_length = net_nfc_get_data_length(payload);
212 int controllbyte = buffer_temp[0];
213 int lang_code_length = controllbyte & 0x3F;
214 int index = lang_code_length + 1;
215 int text_length = buffer_length - (lang_code_length + 1);
219 _net_nfc_util_alloc_mem(temp, text_length + 1);
221 memcpy(temp, &(buffer_temp[index]), text_length);
223 DEBUG_CLIENT_MSG("text = [%s]", temp);
227 result = NET_NFC_ALLOC_FAIL;
235 net_nfc_error_e net_nfc_get_languange_code_string_from_text_record(
236 ndef_record_h record, char **lang_code_str)
238 net_nfc_error_e result;
241 if (record == NULL || lang_code_str == NULL)
242 return NET_NFC_NULL_PARAMETER;
244 *lang_code_str = NULL;
246 if (_is_text_record(record) == false) {
247 DEBUG_ERR_MSG("record type is not matched");
249 return NET_NFC_NDEF_RECORD_IS_NOT_EXPECTED_TYPE;
252 result = net_nfc_get_record_payload(record, &payload);
253 if (result == NET_NFC_OK) {
254 uint8_t *buffer_temp = net_nfc_get_data_buffer(payload);
257 int controllbyte = buffer_temp[0];
258 int lang_code_length = controllbyte & 0x3F;
261 _net_nfc_util_alloc_mem(buffer, lang_code_length + 1);
262 if (buffer != NULL) {
263 memcpy(buffer, &(buffer_temp[index]), lang_code_length);
265 DEBUG_CLIENT_MSG("language code = [%s]", buffer);
267 *lang_code_str = buffer;
269 result = NET_NFC_ALLOC_FAIL;
277 net_nfc_error_e net_nfc_get_encoding_type_from_text_record(ndef_record_h record,
278 net_nfc_encode_type_e *encoding)
280 net_nfc_error_e result;
283 if (record == NULL || encoding == NULL)
284 return NET_NFC_NULL_PARAMETER;
286 *encoding = NET_NFC_ENCODE_UTF_8;
288 if (_is_text_record(record) == false) {
289 DEBUG_ERR_MSG("record type is not matched");
291 return NET_NFC_NDEF_RECORD_IS_NOT_EXPECTED_TYPE;
294 result = net_nfc_get_record_payload(record, &payload);
295 if (result == NET_NFC_OK) {
296 uint8_t *buffer_temp = net_nfc_get_data_buffer(payload);
298 int controllbyte = buffer_temp[0];
300 if ((controllbyte & 0x80) == 0x80)
301 *encoding = NET_NFC_ENCODE_UTF_16;
308 net_nfc_error_e net_nfc_create_uri_string_from_uri_record(ndef_record_h record,
311 return net_nfc_util_create_uri_string_from_uri_record(
312 (ndef_record_s *)record, uri);