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.
18 #include "net_nfc_debug_internal.h"
19 #include "net_nfc_util_defines.h"
20 #include "net_nfc_util_internal.h"
21 #include "net_nfc_util_ndef_message.h"
22 #include "net_nfc_util_ndef_record.h"
24 static net_nfc_error_e __net_nfc_repair_record_flags(ndef_message_s *ndef_message);
26 net_nfc_error_e net_nfc_util_convert_rawdata_to_ndef_message(
27 data_s *rawdata, ndef_message_s *ndef)
30 uint8_t *current = NULL;
31 uint8_t ndef_header = 0;
32 ndef_record_s *newRec = NULL;
33 ndef_record_s *prevRec = NULL;
34 net_nfc_error_e result = NET_NFC_OK;
36 RETV_IF(NULL == ndef, NET_NFC_NULL_PARAMETER);
37 RETV_IF(NULL == rawdata, NET_NFC_NULL_PARAMETER);
38 RETV_IF(rawdata->length < 3, NET_NFC_INVALID_FORMAT);
40 current = rawdata->buffer;
41 last = current + rawdata->length;
43 for(ndef->recordCount = 0; current < last; ndef->recordCount++)
45 ndef_header = *current++;
47 if(ndef->recordCount == 0)
49 /* first record has MB field */
50 if((ndef_header & NET_NFC_NDEF_RECORD_MASK_MB) == 0)
51 return NET_NFC_INVALID_FORMAT;
53 /* first record should not be a chunked record */
54 if((ndef_header & NET_NFC_NDEF_RECORD_MASK_TNF) == NET_NFC_NDEF_TNF_UNCHANGED)
55 return NET_NFC_INVALID_FORMAT;
58 _net_nfc_util_alloc_mem(newRec, sizeof(ndef_record_s));
61 result = NET_NFC_ALLOC_FAIL;
66 if (ndef_header & NET_NFC_NDEF_RECORD_MASK_MB)
69 if (ndef_header & NET_NFC_NDEF_RECORD_MASK_ME)
72 if (ndef_header & NET_NFC_NDEF_RECORD_MASK_CF)
75 if (ndef_header & NET_NFC_NDEF_RECORD_MASK_SR)
78 if (ndef_header & NET_NFC_NDEF_RECORD_MASK_IL)
81 newRec->TNF = ndef_header & NET_NFC_NDEF_RECORD_MASK_TNF;
83 newRec->type_s.length = *current++;
85 /* SR = 1 -> payload is 1 byte, SR = 0 -> payload is 4 bytes */
86 if(ndef_header & NET_NFC_NDEF_RECORD_MASK_SR)
88 newRec->payload_s.length = *current++;
92 newRec->payload_s.length = (uint32_t)((*current) << 24);
95 newRec->payload_s.length += (uint32_t)((*current) << 16);
98 newRec->payload_s.length += (uint32_t)((*current) << 8);
101 newRec->payload_s.length += (uint32_t)((*current));
105 /* ID length check */
106 if(ndef_header & NET_NFC_NDEF_RECORD_MASK_IL)
107 newRec->id_s.length = *current++;
109 newRec->id_s.length = 0;
111 /* to do : chunked record */
113 /* empty record check */
114 if((ndef_header & NET_NFC_NDEF_RECORD_MASK_TNF) == NET_NFC_NDEF_TNF_EMPTY)
116 if(newRec->type_s.length != 0 || newRec->id_s.length != 0
117 || newRec->payload_s.length != 0)
119 result = NET_NFC_INVALID_FORMAT;
124 if((ndef_header & NET_NFC_NDEF_RECORD_MASK_TNF) == NET_NFC_NDEF_TNF_UNKNOWN)
126 if(newRec->type_s.length != 0)
128 result = NET_NFC_INVALID_FORMAT;
133 /* put Type buffer */
134 if(newRec->type_s.length > 0)
136 _net_nfc_util_alloc_mem(newRec->type_s.buffer, newRec->type_s.length);
137 if (NULL == newRec->type_s.buffer)
139 result = NET_NFC_ALLOC_FAIL;
143 memcpy(newRec->type_s.buffer, current, newRec->type_s.length);
144 current += newRec->type_s.length;
148 newRec->type_s.buffer = NULL;
152 if(newRec->id_s.length > 0)
154 _net_nfc_util_alloc_mem(newRec->id_s.buffer, newRec->id_s.length);
155 if (NULL == newRec->id_s.buffer)
157 result = NET_NFC_ALLOC_FAIL;
161 memcpy(newRec->id_s.buffer, current, newRec->id_s.length);
162 current += newRec->id_s.length;
166 newRec->id_s.buffer = NULL;
169 /* put Payload buffer */
170 if(newRec->payload_s.length > 0)
172 _net_nfc_util_alloc_mem(newRec->payload_s.buffer, newRec->payload_s.length);
173 if (newRec->payload_s.buffer == NULL)
175 result = NET_NFC_ALLOC_FAIL;
179 memcpy(newRec->payload_s.buffer, current, newRec->payload_s.length);
180 current += newRec->payload_s.length;
184 newRec->payload_s.buffer = NULL;
187 if (ndef->recordCount == 0)
188 ndef->records = newRec;
190 prevRec->next = newRec;
195 if(ndef_header & NET_NFC_NDEF_RECORD_MASK_ME)
203 if((current != last) || ((ndef_header & NET_NFC_NDEF_RECORD_MASK_ME) == 0))
205 result = NET_NFC_INVALID_FORMAT;
213 NFC_ERR("parser error");
217 _net_nfc_util_free_mem(newRec->type_s.buffer);
218 _net_nfc_util_free_mem(newRec->id_s.buffer);
219 _net_nfc_util_free_mem(newRec->payload_s.buffer);
220 _net_nfc_util_free_mem(newRec);
223 prevRec = ndef->records;
227 ndef_record_s *tmpRec = NULL;
229 _net_nfc_util_free_mem(prevRec->type_s.buffer);
230 _net_nfc_util_free_mem(prevRec->id_s.buffer);
231 _net_nfc_util_free_mem(prevRec->payload_s.buffer);
233 tmpRec = prevRec->next;
234 _net_nfc_util_free_mem(prevRec);
238 ndef->records = NULL;
243 net_nfc_error_e net_nfc_util_convert_ndef_message_to_rawdata(ndef_message_s *ndef, data_s *rawdata)
245 ndef_record_s *record = NULL;
246 uint8_t *current = NULL;
249 if (rawdata == NULL || ndef == NULL)
250 return NET_NFC_NULL_PARAMETER;
252 record = ndef->records;
253 current = rawdata->buffer;
260 ndef_header |= NET_NFC_NDEF_RECORD_MASK_MB;
262 ndef_header |= NET_NFC_NDEF_RECORD_MASK_ME;
264 ndef_header |= NET_NFC_NDEF_RECORD_MASK_CF;
266 ndef_header |= NET_NFC_NDEF_RECORD_MASK_SR;
268 ndef_header |= NET_NFC_NDEF_RECORD_MASK_IL;
270 ndef_header |= record->TNF;
272 *current++ = ndef_header;
274 /* check empty record */
275 if(record->TNF == NET_NFC_NDEF_TNF_EMPTY)
277 /* set type length to zero */
280 /* set payload length to zero */
283 /* set ID length to zero */
289 record = record->next;
294 /* set type length */
295 if(record->TNF == NET_NFC_NDEF_TNF_UNKNOWN || record->TNF == NET_NFC_NDEF_TNF_UNCHANGED)
301 *current++ = record->type_s.length;
304 /* set payload length */
307 *current++ = (uint8_t)(record->payload_s.length & 0x000000FF);
311 *current++ = (uint8_t)((record->payload_s.length & 0xFF000000) >> 24);
312 *current++ = (uint8_t)((record->payload_s.length & 0x00FF0000) >> 16);
313 *current++ = (uint8_t)((record->payload_s.length & 0x0000FF00) >> 8);
314 *current++ = (uint8_t)(record->payload_s.length & 0x000000FF) ;
320 *current++ = record->id_s.length;
323 /* set type buffer */
324 if((record->TNF != NET_NFC_NDEF_TNF_UNKNOWN) && (record->TNF != NET_NFC_NDEF_TNF_UNCHANGED))
326 memcpy(current, record->type_s.buffer, record->type_s.length);
327 current += record->type_s.length;
331 memcpy(current, record->id_s.buffer, record->id_s.length);
332 current += record->id_s.length;
334 /* set payload buffer */
335 memcpy(current, record->payload_s.buffer, record->payload_s.length);
336 current += record->payload_s.length;
338 record = record->next;
344 net_nfc_error_e net_nfc_util_append_record(ndef_message_s *msg, ndef_record_s *record)
346 if (msg == NULL || record == NULL)
347 return NET_NFC_NULL_PARAMETER;
349 if (msg->recordCount == 0)
351 // set short message and append
356 msg->records = record;
360 NFC_DBG("record is added to NDEF message :: count [%d]", msg->recordCount);
364 ndef_record_s *current = NULL;
365 ndef_record_s *prev = NULL;
367 // set flag :: this record is FIRST
368 current = msg->records;
379 current = current->next;
381 while (current != NULL)
386 current = current->next;
389 // set flag :: this record is END
402 uint32_t net_nfc_util_get_ndef_message_length(ndef_message_s *message)
404 ndef_record_s *current;
410 current = message->records;
412 while (current != NULL)
414 total += net_nfc_util_get_record_length(current);
415 current = current->next;
421 void net_nfc_util_print_ndef_message(ndef_message_s *msg)
423 int idx = 0, idx2 = 0;
424 ndef_record_s *current = NULL;
432 // 123456789012345678901234567890123456789012345678901234567890
433 NFC_DBG("========== NDEF Message ====================================\n");
434 NFC_DBG("Total NDEF Records count: %d\n", msg->recordCount);
435 current = msg->records;
436 for (idx = 0; idx < msg->recordCount; idx++)
440 NFC_ERR("Message Record is NULL!! unexpected error");
441 NFC_DBG("============================================================\n");
444 NFC_DBG("---------- Record -----------------------------------------\n");
445 NFC_DBG("MB:%d ME:%d CF:%d SR:%d IL:%d TNF:0x%02X\n",
446 current->MB, current->ME, current->CF, current->SR, current->IL, current->TNF);
447 NFC_DBG("TypeLength:%d PayloadLength:%d IDLength:%d\n",
448 current->type_s.length, current->payload_s.length, current->id_s.length);
449 if (current->type_s.buffer != NULL)
451 memcpy(buffer, current->type_s.buffer, current->type_s.length);
452 buffer[current->type_s.length] = '\0';
453 NFC_DBG("Type: %s\n", buffer);
455 if (current->id_s.buffer != NULL)
457 memcpy(buffer, current->id_s.buffer, current->id_s.length);
458 buffer[current->id_s.length] = '\0';
459 SECURE_LOGD("ID: %s\n", buffer);
461 if (current->payload_s.buffer != NULL)
463 NFC_DBG("Payload: ");
464 for (idx2 = 0; idx2 < current->payload_s.length; idx2++)
468 NFC_DBG("%02X ", current->payload_s.buffer[idx2]);
472 current = current->next;
474 // 123456789012345678901234567890123456789012345678901234567890
475 NFC_DBG("============================================================\n");
479 net_nfc_error_e net_nfc_util_free_ndef_message(ndef_message_s *msg)
482 ndef_record_s *prev, *current;
485 return NET_NFC_NULL_PARAMETER;
487 current = msg->records;
489 for (idx = 0; idx < msg->recordCount; idx++)
495 current = current->next;
497 net_nfc_util_free_record(prev);
500 _net_nfc_util_free_mem(msg);
505 net_nfc_error_e net_nfc_util_create_ndef_message(ndef_message_s **ndef_message)
507 if (ndef_message == NULL) {
508 return NET_NFC_NULL_PARAMETER;
511 _net_nfc_util_alloc_mem(*ndef_message, sizeof(ndef_message_s));
512 if (*ndef_message == NULL) {
513 return NET_NFC_ALLOC_FAIL;
519 net_nfc_error_e net_nfc_util_remove_record_by_index(ndef_message_s *ndef_message, int index)
524 ndef_record_s *current;
526 if (ndef_message == NULL)
528 return NET_NFC_NULL_PARAMETER;
531 if (index < 0 || index >= ndef_message->recordCount)
533 return NET_NFC_OUT_OF_BOUND;
538 current = ndef_message->records;
539 next = ndef_message->records->next;
540 ndef_message->records = next;
544 prev = ndef_message->records;
545 for (; current_idx < index - 1; current_idx++)
550 return NET_NFC_INVALID_FORMAT;
553 current = prev->next;
556 return NET_NFC_INVALID_FORMAT;
558 next = current->next;
562 net_nfc_util_free_record(current);
563 (ndef_message->recordCount)--;
565 return __net_nfc_repair_record_flags(ndef_message);
568 net_nfc_error_e net_nfc_util_get_record_by_index(ndef_message_s *ndef_message, int index, ndef_record_s **record)
570 ndef_record_s *current;
573 if (ndef_message == NULL || record == NULL)
575 return NET_NFC_NULL_PARAMETER;
578 if (index < 0 || index >= ndef_message->recordCount)
580 return NET_NFC_OUT_OF_BOUND;
583 current = ndef_message->records;
585 for (; current != NULL && idx < index; idx++)
587 current = current->next;
595 net_nfc_error_e net_nfc_util_append_record_by_index(ndef_message_s *ndef_message, int index, ndef_record_s *record)
601 if (ndef_message == NULL || record == NULL)
603 return NET_NFC_NULL_PARAMETER;
606 if (index < 0 || index > ndef_message->recordCount)
608 return NET_NFC_OUT_OF_BOUND;
611 prev = ndef_message->records;
615 ndef_message->records = record;
620 for (; idx < index - 1; idx++)
625 return NET_NFC_INVALID_FORMAT;
632 (ndef_message->recordCount)++;
634 return __net_nfc_repair_record_flags(ndef_message);
637 net_nfc_error_e net_nfc_util_search_record_by_type(ndef_message_s *ndef_message, net_nfc_record_tnf_e tnf, data_s *type, ndef_record_s **record)
640 ndef_record_s *tmp_record;
641 uint32_t type_length;
644 if (ndef_message == NULL || type == NULL || record == NULL)
646 return NET_NFC_NULL_PARAMETER;
649 type_length = type->length;
652 /* remove prefix of nfc specific urn */
653 if (type_length > 12)
655 if (memcmp(buf, "urn:nfc:ext:", 12) == 0 ||
656 memcmp(buf, "urn:nfc:wkt:", 12) == 0)
663 tmp_record = ndef_message->records;
665 for (; idx < ndef_message->recordCount; idx++)
667 if (tmp_record == NULL)
671 return NET_NFC_INVALID_FORMAT;
674 if (tmp_record->TNF == tnf &&
675 type_length == tmp_record->type_s.length &&
676 memcmp(buf, tmp_record->type_s.buffer, type_length) == 0)
678 *record = tmp_record;
683 tmp_record = tmp_record->next;
686 return NET_NFC_NO_DATA_FOUND;
689 net_nfc_error_e net_nfc_util_search_record_by_id(ndef_message_s *ndef_message, data_s *id, ndef_record_s **record)
692 ndef_record_s *record_in_msg;
696 if (ndef_message == NULL || id == NULL || record == NULL)
698 return NET_NFC_NULL_PARAMETER;
701 id_length = id->length;
704 record_in_msg = ndef_message->records;
706 for (; idx < ndef_message->recordCount; idx++)
708 if (record_in_msg == NULL)
712 return NET_NFC_INVALID_FORMAT;
714 if (id_length == record_in_msg->id_s.length &&
715 memcmp(buf, record_in_msg->id_s.buffer, id_length) == 0)
717 *record = record_in_msg;
722 record_in_msg = record_in_msg->next;
725 return NET_NFC_NO_DATA_FOUND;
728 static net_nfc_error_e __net_nfc_repair_record_flags(ndef_message_s *ndef_message)
731 ndef_record_s *record;
733 if (ndef_message == NULL)
735 return NET_NFC_NULL_PARAMETER;
738 record = ndef_message->records;
740 if (ndef_message->recordCount == 1)
744 return NET_NFC_INVALID_FORMAT;
753 for (idx = 0; idx < ndef_message->recordCount; idx++)
757 return NET_NFC_INVALID_FORMAT;
765 else if (idx == ndef_message->recordCount - 1)
775 record = record->next;