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_debug_internal.h"
18 #include "net_nfc_util_defines.h"
19 #include "net_nfc_util_internal.h"
20 #include "net_nfc_util_ndef_message.h"
21 #include "net_nfc_util_ndef_record.h"
23 net_nfc_error_e net_nfc_util_free_record(ndef_record_s *record)
25 RETV_IF(NULL == record, NET_NFC_NULL_PARAMETER);
27 if (record->type_s.buffer != NULL)
28 _net_nfc_util_free_mem(record->type_s.buffer);
29 if (record->id_s.buffer != NULL)
30 _net_nfc_util_free_mem(record->id_s.buffer);
31 if (record->payload_s.buffer != NULL)
32 _net_nfc_util_free_mem(record->payload_s.buffer);
34 _net_nfc_util_free_mem(record);
39 net_nfc_error_e net_nfc_util_create_record(net_nfc_record_tnf_e recordType,
40 const data_s *typeName, const data_s *id, const data_s *payload,
41 ndef_record_s **record)
43 ndef_record_s *record_temp = NULL;
45 RETV_IF(NULL == typeName, NET_NFC_NULL_PARAMETER);
46 RETV_IF(NULL == payload, NET_NFC_NULL_PARAMETER);
47 RETV_IF(NULL == record, NET_NFC_NULL_PARAMETER);
49 RETV_IF(recordType < NET_NFC_RECORD_EMPTY, NET_NFC_OUT_OF_BOUND);
50 RETV_IF(recordType > NET_NFC_RECORD_UNCHAGNED, NET_NFC_OUT_OF_BOUND);
53 if (NET_NFC_RECORD_EMPTY == recordType)
55 if ((typeName->buffer != NULL) || (payload->buffer != NULL)
56 || (id->buffer != NULL) || (typeName->length != 0) || (payload->length != 0)
59 return NET_NFC_NULL_PARAMETER;
63 _net_nfc_util_alloc_mem(record_temp, sizeof(ndef_record_s));
64 if (NULL == record_temp)
65 return NET_NFC_ALLOC_FAIL;
67 // set type name and length and TNF field
68 record_temp->TNF = recordType;
69 record_temp->type_s.length = typeName->length;
71 if(record_temp->type_s.length > 0)
73 _net_nfc_util_alloc_mem(record_temp->type_s.buffer, record_temp->type_s.length);
74 if (NULL == record_temp->type_s.buffer)
76 _net_nfc_util_free_mem(record_temp);
78 return NET_NFC_ALLOC_FAIL;
81 memcpy(record_temp->type_s.buffer, typeName->buffer, record_temp->type_s.length);
85 record_temp->type_s.buffer = NULL;
86 record_temp->type_s.length = 0;
90 record_temp->payload_s.length = payload->length;
91 if(payload->length >0)
93 _net_nfc_util_alloc_mem(record_temp->payload_s.buffer,
94 record_temp->payload_s.length);
96 if (NULL == record_temp->payload_s.buffer)
98 _net_nfc_util_free_mem(record_temp->type_s.buffer);
99 _net_nfc_util_free_mem(record_temp);
101 return NET_NFC_ALLOC_FAIL;
104 memcpy(record_temp->payload_s.buffer, payload->buffer,
105 record_temp->payload_s.length);
109 record_temp->payload_s.buffer = NULL;
110 record_temp->payload_s.length = 0;
113 if (payload->length < 256)
118 // set id and id length and IL field
119 if (id != NULL && id->buffer != NULL && id->length > 0)
121 record_temp->id_s.length = id->length;
122 _net_nfc_util_alloc_mem(record_temp->id_s.buffer, record_temp->id_s.length);
123 if (NULL == record_temp->id_s.buffer)
125 _net_nfc_util_free_mem(record_temp->payload_s.buffer);
126 _net_nfc_util_free_mem(record_temp->type_s.buffer);
127 _net_nfc_util_free_mem(record_temp);
129 return NET_NFC_ALLOC_FAIL;
132 memcpy(record_temp->id_s.buffer, id->buffer, record_temp->id_s.length);
138 record_temp->id_s.buffer = NULL;
139 record_temp->id_s.length = 0;
142 // this is default value
146 record_temp->next = NULL;
148 *record = record_temp;
153 net_nfc_error_e net_nfc_util_create_uri_type_record(const char *uri,
154 net_nfc_schema_type_e protocol_schema, ndef_record_s **record)
157 net_nfc_error_e error;
158 data_s payload_data = { NULL, 0 };
160 RETV_IF(NULL == uri, NET_NFC_NULL_PARAMETER);
162 payload_data.length = strlen((char *)uri) + 1;
164 RETV_IF(1 == payload_data.length, NET_NFC_INVALID_PARAM);
166 _net_nfc_util_alloc_mem(payload_data.buffer, payload_data.length);
167 if (NULL == payload_data.buffer)
168 return NET_NFC_ALLOC_FAIL;
170 payload_data.buffer[0] = protocol_schema; /* first byte of payload is protocol scheme */
171 memcpy(payload_data.buffer + 1, uri, payload_data.length - 1);
173 type_data.length = 1;
174 type_data.buffer = (uint8_t *)URI_RECORD_TYPE;
176 error = net_nfc_util_create_record(NET_NFC_RECORD_WELL_KNOWN_TYPE,
177 &type_data, NULL, &payload_data, record);
179 _net_nfc_util_free_mem(payload_data.buffer);
184 net_nfc_error_e net_nfc_util_create_text_type_record(const char *text,
185 const char *lang_code_str, net_nfc_encode_type_e encode, ndef_record_s **record)
192 RETV_IF(NULL == text, NET_NFC_NULL_PARAMETER);
193 RETV_IF(NULL == lang_code_str, NET_NFC_NULL_PARAMETER);
195 RETVM_IF(encode < NET_NFC_ENCODE_UTF_8 || NET_NFC_ENCODE_UTF_16 < encode ,
196 NET_NFC_OUT_OF_BOUND, "encode(%d) is invalid", encode);
198 payload_data.length = strlen((char *)text) + strlen(lang_code_str) + 1;
200 _net_nfc_util_alloc_mem(payload_data.buffer, payload_data.length);
201 if (NULL == payload_data.buffer)
203 return NET_NFC_ALLOC_FAIL;
206 controll_byte = strlen(lang_code_str) & 0x3F;
207 if (NET_NFC_ENCODE_UTF_16 == encode)
209 controll_byte = controll_byte | 0x80;
212 payload_data.buffer[0] = controll_byte;
215 memcpy(payload_data.buffer + offset, lang_code_str, strlen(lang_code_str));
217 offset = offset + strlen(lang_code_str);
218 memcpy(payload_data.buffer + offset, (char *)text, strlen((char *)text));
220 type_data.length = 1;
221 type_data.buffer = (uint8_t *)TEXT_RECORD_TYPE;
223 net_nfc_util_create_record(NET_NFC_RECORD_WELL_KNOWN_TYPE, &type_data, NULL,
224 &payload_data, record);
226 _net_nfc_util_free_mem(payload_data.buffer);
231 net_nfc_error_e net_nfc_util_set_record_id(ndef_record_s *record,
232 uint8_t *data, int length)
234 RETV_IF(NULL == data, NET_NFC_NULL_PARAMETER);
235 RETV_IF(NULL == record, NET_NFC_NULL_PARAMETER);
236 RETV_IF(length < 1, NET_NFC_OUT_OF_BOUND);
238 if (record->id_s.buffer != NULL && record->id_s.length > 0)
239 _net_nfc_util_free_mem(record->id_s.buffer);
241 _net_nfc_util_alloc_mem(record->id_s.buffer, length);
243 if (record->id_s.buffer == NULL)
244 return NET_NFC_ALLOC_FAIL;
246 memcpy(record->id_s.buffer, data, length);
247 record->id_s.length = length;
253 uint32_t net_nfc_util_get_record_length(ndef_record_s *Record)
255 uint32_t RecordLength = 1;
257 RETV_IF(NULL == Record, 0);
259 /* Type length is present only for following TNF
260 NET_NFC_TNF_NFCWELLKNOWN
261 NET_NFC_TNF_MEDIATYPE
262 SLP_FRINET_NFC_NDEFRECORD_TNF_ABSURI
263 SLP_FRINET_NFC_NDEFRECORD_TNF_NFCEXT
266 /* ++ is for the Type Length Byte */
268 if (Record->TNF != NET_NFC_NDEF_TNF_EMPTY &&
269 Record->TNF != NET_NFC_NDEF_TNF_UNKNOWN &&
270 Record->TNF != NET_NFC_NDEF_TNF_UNCHANGED)
272 RecordLength += Record->type_s.length;
275 /* to check if payloadlength is 8bit or 32bit*/
278 /* ++ is for the Payload Length Byte */
279 RecordLength++;/* for short record*/
283 /* + NET_NFC_NDEF_NORMAL_RECORD_BYTE is for the Payload Length Byte */
287 /* for non empty record */
288 if (Record->TNF != NET_NFC_NDEF_TNF_EMPTY)
289 RecordLength += Record->payload_s.length;
291 /* ID and IDlength are present only if IL flag is set*/
294 RecordLength += Record->id_s.length;
295 /* ++ is for the ID Length Byte */
302 net_nfc_error_e net_nfc_util_create_uri_string_from_uri_record(
303 ndef_record_s *record, char **uri)
305 net_nfc_error_e result = NET_NFC_OK;
307 RETV_IF(NULL == uri, NET_NFC_INVALID_PARAM);
308 RETV_IF(NULL == record, NET_NFC_INVALID_PARAM);
312 if (NET_NFC_RECORD_WELL_KNOWN_TYPE == record->TNF &&
313 (record->type_s.length == 1 && record->type_s.buffer[0] == 'U'))
315 data_s *payload = &record->payload_s;
317 if (payload->length > 0)
320 const char *scheme = NULL;
322 /* buffer length include a schema byte.
323 * so it does not need to allocate one more byte for string. */
324 if ((scheme = net_nfc_util_get_schema_string(payload->buffer[0])) != NULL)
325 length = strlen(scheme);
327 *uri = (char *)calloc(1, length + payload->length);
331 memcpy(*uri, scheme, length);
332 memcpy(*uri + length, payload->buffer + 1, payload->length - 1);
336 result = NET_NFC_ALLOC_FAIL;
341 NFC_ERR("invalid payload in record");
344 else if (NET_NFC_RECORD_URI == record->TNF)
346 data_s *type = &record->type_s;
348 if (type->length > 0)
350 *uri = (char *)calloc(1, type->length + 1);
353 memcpy(*uri, type->buffer, type->length);
355 result = NET_NFC_ALLOC_FAIL;
360 NFC_ERR("no uri record");
361 result = NET_NFC_NDEF_RECORD_IS_NOT_EXPECTED_TYPE;