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)
26 return NET_NFC_NULL_PARAMETER;
28 if (record->type_s.buffer != NULL)
29 _net_nfc_util_free_mem(record->type_s.buffer);
30 if (record->id_s.buffer != NULL)
31 _net_nfc_util_free_mem(record->id_s.buffer);
32 if (record->payload_s.buffer != NULL)
33 _net_nfc_util_free_mem(record->payload_s.buffer);
35 _net_nfc_util_free_mem(record);
40 net_nfc_error_e net_nfc_util_create_record(net_nfc_record_tnf_e recordType, data_s *typeName, data_s *id, data_s *payload, ndef_record_s **record)
42 ndef_record_s *record_temp = NULL;
44 if (typeName == NULL || payload == NULL || record == NULL)
46 return NET_NFC_NULL_PARAMETER;
49 if (recordType < NET_NFC_RECORD_EMPTY || recordType > NET_NFC_RECORD_UNCHAGNED)
51 return NET_NFC_OUT_OF_BOUND;
55 if (recordType == NET_NFC_RECORD_EMPTY)
57 if ((typeName->buffer != NULL) || (payload->buffer != NULL) || (id->buffer != NULL) || (typeName->length != 0) || (payload->length != 0) || (id->length != 0))
58 return NET_NFC_NULL_PARAMETER;
61 _net_nfc_util_alloc_mem(record_temp, sizeof(ndef_record_s));
62 if (record_temp == NULL)
64 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 (record_temp->type_s.buffer == NULL)
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, record_temp->payload_s.length);
94 if (record_temp->payload_s.buffer == NULL)
96 _net_nfc_util_free_mem(record_temp->type_s.buffer);
97 _net_nfc_util_free_mem(record_temp);
99 return NET_NFC_ALLOC_FAIL;
102 memcpy(record_temp->payload_s.buffer, payload->buffer, record_temp->payload_s.length);
106 record_temp->payload_s.buffer = NULL;
107 record_temp->payload_s.length = 0;
110 if (payload->length < 256)
119 // set id and id length and IL field
120 if (id != NULL && id->buffer != NULL && id->length > 0)
122 record_temp->id_s.length = id->length;
123 _net_nfc_util_alloc_mem(record_temp->id_s.buffer, record_temp->id_s.length);
124 if (record_temp->id_s.buffer == NULL)
126 _net_nfc_util_free_mem(record_temp->payload_s.buffer);
127 _net_nfc_util_free_mem(record_temp->type_s.buffer);
128 _net_nfc_util_free_mem(record_temp);
130 return NET_NFC_ALLOC_FAIL;
133 memcpy(record_temp->id_s.buffer, id->buffer, record_temp->id_s.length);
139 record_temp->id_s.buffer = NULL;
140 record_temp->id_s.length = 0;
143 // this is default value
147 record_temp->next = NULL;
149 *record = record_temp;
154 net_nfc_error_e net_nfc_util_create_uri_type_record(const char *uri, net_nfc_schema_type_e protocol_schema, ndef_record_s **record)
156 net_nfc_error_e error;
158 data_s payload_data = { NULL, 0 };
162 return NET_NFC_NULL_PARAMETER;
165 payload_data.length = strlen((char *)uri) + 1;
166 if (payload_data.length == 1)
168 return NET_NFC_INVALID_PARAM;
171 _net_nfc_util_alloc_mem(payload_data.buffer, payload_data.length);
172 if (payload_data.buffer == NULL)
174 return NET_NFC_ALLOC_FAIL;
177 payload_data.buffer[0] = protocol_schema; /* first byte of payload is protocol scheme */
178 memcpy(payload_data.buffer + 1, uri, payload_data.length - 1);
180 type_data.length = 1;
181 type_data.buffer = (uint8_t *)URI_RECORD_TYPE;
183 error = net_nfc_util_create_record(NET_NFC_RECORD_WELL_KNOWN_TYPE, &type_data, NULL, &payload_data, record);
185 _net_nfc_util_free_mem(payload_data.buffer);
190 net_nfc_error_e net_nfc_util_create_text_type_record(const char *text, const char *lang_code_str, net_nfc_encode_type_e encode, ndef_record_s **record)
197 if (text == NULL || lang_code_str == NULL)
199 return NET_NFC_NULL_PARAMETER;
202 if ((encode < NET_NFC_ENCODE_UTF_8 || encode > NET_NFC_ENCODE_UTF_16))
204 return NET_NFC_OUT_OF_BOUND;
207 payload_data.length = strlen((char *)text) + strlen(lang_code_str) + 1;
209 _net_nfc_util_alloc_mem(payload_data.buffer, payload_data.length);
210 if (payload_data.buffer == NULL)
212 return NET_NFC_ALLOC_FAIL;
215 controll_byte = strlen(lang_code_str) & 0x3F;
216 if (encode == NET_NFC_ENCODE_UTF_16)
218 controll_byte = controll_byte | 0x80;
221 payload_data.buffer[0] = controll_byte;
224 memcpy(payload_data.buffer + offset, lang_code_str, strlen(lang_code_str));
226 offset = offset + strlen(lang_code_str);
227 memcpy(payload_data.buffer + offset, (char *)text, strlen((char *)text));
229 type_data.length = 1;
230 type_data.buffer = (uint8_t *)TEXT_RECORD_TYPE;
232 net_nfc_util_create_record(NET_NFC_RECORD_WELL_KNOWN_TYPE, &type_data, NULL, &payload_data, record);
234 _net_nfc_util_free_mem(payload_data.buffer);
239 net_nfc_error_e net_nfc_util_set_record_id(ndef_record_s *record, uint8_t *data, int length)
241 if (record == NULL || data == NULL)
243 return NET_NFC_NULL_PARAMETER;
248 return NET_NFC_OUT_OF_BOUND;
251 if (record->id_s.buffer != NULL && record->id_s.length > 0)
253 _net_nfc_util_free_mem(record->id_s.buffer);
256 _net_nfc_util_alloc_mem(record->id_s.buffer, length);
257 if (record->id_s.buffer == NULL)
259 return NET_NFC_ALLOC_FAIL;
261 memcpy(record->id_s.buffer, data, length);
262 record->id_s.length = length;
268 uint32_t net_nfc_util_get_record_length(ndef_record_s *Record)
270 uint32_t RecordLength = 1;
275 /* Type length is present only for following TNF
276 NET_NFC_TNF_NFCWELLKNOWN
277 NET_NFC_TNF_MEDIATYPE
278 SLP_FRINET_NFC_NDEFRECORD_TNF_ABSURI
279 SLP_FRINET_NFC_NDEFRECORD_TNF_NFCEXT
282 /* ++ is for the Type Length Byte */
284 if (Record->TNF != NET_NFC_NDEF_TNF_EMPTY &&
285 Record->TNF != NET_NFC_NDEF_TNF_UNKNOWN &&
286 Record->TNF != NET_NFC_NDEF_TNF_UNCHANGED)
288 RecordLength += Record->type_s.length;
291 /* to check if payloadlength is 8bit or 32bit*/
294 /* ++ is for the Payload Length Byte */
295 RecordLength++;/* for short record*/
299 /* + NET_NFC_NDEF_NORMAL_RECORD_BYTE is for the Payload Length Byte */
303 /* for non empty record */
304 if (Record->TNF != NET_NFC_NDEF_TNF_EMPTY)
306 RecordLength += Record->payload_s.length;
309 /* ID and IDlength are present only if IL flag is set*/
312 RecordLength += Record->id_s.length;
313 /* ++ is for the ID Length Byte */
320 net_nfc_error_e net_nfc_util_create_uri_string_from_uri_record(ndef_record_s *record, char **uri)
322 net_nfc_error_e result = NET_NFC_OK;
324 if (record == NULL || uri == NULL)
326 return NET_NFC_INVALID_PARAM;
331 if (record->TNF == NET_NFC_RECORD_WELL_KNOWN_TYPE &&
332 (record->type_s.length == 1 && record->type_s.buffer[0] == 'U'))
334 data_s *payload = &record->payload_s;
336 if (payload->length > 0)
339 const char *scheme = NULL;
341 /* buffer length include a schema byte.
342 * so it does not need to allocate one more byte for string. */
343 if ((scheme = net_nfc_util_get_schema_string(payload->buffer[0])) != NULL)
345 length = strlen(scheme);
348 *uri = (char *)calloc(1, length + payload->length);
352 memcpy(*uri, scheme, length);
353 memcpy(*uri + length, payload->buffer + 1, payload->length - 1);
357 result = NET_NFC_ALLOC_FAIL;
362 DEBUG_ERR_MSG("invalid payload in record");
365 else if (record->TNF == NET_NFC_RECORD_URI)
367 data_s *type = &record->type_s;
369 if (type->length > 0)
371 *uri = (char *)calloc(1, type->length + 1);
375 memcpy(*uri, type->buffer, type->length);
379 result = NET_NFC_ALLOC_FAIL;
385 DEBUG_ERR_MSG("no uri record");
386 result = NET_NFC_NDEF_RECORD_IS_NOT_EXPECTED_TYPE;