revise logging and coding style
[platform/core/connectivity/nfc-manager-neard.git] / common / net_nfc_util_ndef_message.c
1 /*
2  * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  *     http://floralicense.org/license/
9  *
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.
15  */
16
17
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"
23
24 static net_nfc_error_e __net_nfc_repair_record_flags(ndef_message_s *ndef_message);
25
26 net_nfc_error_e net_nfc_util_convert_rawdata_to_ndef_message(
27                 data_s *rawdata, ndef_message_s *ndef)
28 {
29         uint8_t *last = NULL;
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;
35
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);
39
40         current = rawdata->buffer;
41         last = current + rawdata->length;
42
43         for(ndef->recordCount = 0; current < last; ndef->recordCount++)
44         {
45                 ndef_header = *current++;
46
47                 if(ndef->recordCount == 0)
48                 {
49                         /* first record has MB field */
50                         if((ndef_header & NET_NFC_NDEF_RECORD_MASK_MB) == 0)
51                                 return NET_NFC_INVALID_FORMAT;
52
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;
56                 }
57
58                 _net_nfc_util_alloc_mem(newRec, sizeof(ndef_record_s));
59                 if (NULL == newRec)
60                 {
61                         result = NET_NFC_ALLOC_FAIL;
62                         goto error;
63                 }
64
65                 /* ndef header set */
66                 if (ndef_header & NET_NFC_NDEF_RECORD_MASK_MB)
67                         newRec->MB = 1;
68
69                 if (ndef_header & NET_NFC_NDEF_RECORD_MASK_ME)
70                         newRec->ME = 1;
71
72                 if (ndef_header & NET_NFC_NDEF_RECORD_MASK_CF)
73                         newRec->CF = 1;
74
75                 if (ndef_header & NET_NFC_NDEF_RECORD_MASK_SR)
76                         newRec->SR = 1;
77
78                 if (ndef_header & NET_NFC_NDEF_RECORD_MASK_IL)
79                         newRec->IL = 1;
80
81                 newRec->TNF = ndef_header & NET_NFC_NDEF_RECORD_MASK_TNF;
82
83                 newRec->type_s.length = *current++;
84
85                 /* SR = 1 -> payload is 1 byte, SR = 0 -> payload is 4 bytes */
86                 if(ndef_header & NET_NFC_NDEF_RECORD_MASK_SR)
87                 {
88                         newRec->payload_s.length = *current++;
89                 }
90                 else
91                 {
92                         newRec->payload_s.length = (uint32_t)((*current) << 24);
93                         current++;
94
95                         newRec->payload_s.length += (uint32_t)((*current) << 16);
96                         current++;
97
98                         newRec->payload_s.length += (uint32_t)((*current) << 8);
99                         current++;
100
101                         newRec->payload_s.length += (uint32_t)((*current));
102                         current++;
103                 }
104
105                 /* ID length check */
106                 if(ndef_header & NET_NFC_NDEF_RECORD_MASK_IL)
107                         newRec->id_s.length = *current++;
108                 else
109                         newRec->id_s.length = 0;
110
111                 /* to do : chunked record */
112
113                 /* empty record check */
114                 if((ndef_header & NET_NFC_NDEF_RECORD_MASK_TNF) == NET_NFC_NDEF_TNF_EMPTY)
115                 {
116                         if(newRec->type_s.length != 0 || newRec->id_s.length != 0
117                                 || newRec->payload_s.length != 0)
118                         {
119                                 result = NET_NFC_INVALID_FORMAT;
120                                 goto error;
121                         }
122                 }
123
124                 if((ndef_header & NET_NFC_NDEF_RECORD_MASK_TNF) == NET_NFC_NDEF_TNF_UNKNOWN)
125                 {
126                         if(newRec->type_s.length != 0)
127                         {
128                                 result = NET_NFC_INVALID_FORMAT;
129                                 goto error;
130                         }
131                 }
132
133                 /* put Type buffer */
134                 if(newRec->type_s.length > 0)
135                 {
136                         _net_nfc_util_alloc_mem(newRec->type_s.buffer, newRec->type_s.length);
137                         if (NULL == newRec->type_s.buffer)
138                         {
139                                 result = NET_NFC_ALLOC_FAIL;
140                                 goto error;
141                         }
142
143                         memcpy(newRec->type_s.buffer, current, newRec->type_s.length);
144                         current += newRec->type_s.length;
145                 }
146                 else
147                 {
148                         newRec->type_s.buffer = NULL;
149                 }
150
151                 /* put ID buffer */
152                 if(newRec->id_s.length > 0)
153                 {
154                         _net_nfc_util_alloc_mem(newRec->id_s.buffer, newRec->id_s.length);
155                         if (NULL == newRec->id_s.buffer)
156                         {
157                                 result = NET_NFC_ALLOC_FAIL;
158                                 goto error;
159                         }
160
161                         memcpy(newRec->id_s.buffer, current, newRec->id_s.length);
162                         current += newRec->id_s.length;
163                 }
164                 else
165                 {
166                         newRec->id_s.buffer = NULL;
167                 }
168
169                 /* put Payload buffer */
170                 if(newRec->payload_s.length > 0)
171                 {
172                         _net_nfc_util_alloc_mem(newRec->payload_s.buffer, newRec->payload_s.length);
173                         if (newRec->payload_s.buffer == NULL)
174                         {
175                                 result = NET_NFC_ALLOC_FAIL;
176                                 goto error;
177                         }
178
179                         memcpy(newRec->payload_s.buffer, current, newRec->payload_s.length);
180                         current += newRec->payload_s.length;
181                 }
182                 else
183                 {
184                         newRec->payload_s.buffer = NULL;
185                 }
186
187                 if (ndef->recordCount == 0)
188                         ndef->records = newRec;
189                 else
190                         prevRec->next = newRec;
191
192                 prevRec = newRec;
193                 newRec = NULL;
194
195                 if(ndef_header & NET_NFC_NDEF_RECORD_MASK_ME)
196                 {
197                         break;
198                 }
199         }
200
201         ndef->recordCount++;
202
203         if((current != last) || ((ndef_header & NET_NFC_NDEF_RECORD_MASK_ME) == 0))
204         {
205                 result = NET_NFC_INVALID_FORMAT;
206                 goto error;
207         }
208
209         return NET_NFC_OK;
210
211 error:
212
213         NFC_ERR("parser error");
214
215         if (newRec)
216         {
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);
221         }
222
223         prevRec = ndef->records;
224
225         while(prevRec)
226         {
227                 ndef_record_s *tmpRec = NULL;
228
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);
232
233                 tmpRec = prevRec->next;
234                 _net_nfc_util_free_mem(prevRec);
235                 prevRec = tmpRec;
236         }
237
238         ndef->records = NULL;
239
240         return result;
241 }
242
243 net_nfc_error_e net_nfc_util_convert_ndef_message_to_rawdata(ndef_message_s *ndef, data_s *rawdata)
244 {
245         ndef_record_s *record = NULL;
246         uint8_t *current = NULL;
247         uint8_t ndef_header;
248
249         if (rawdata == NULL || ndef == NULL)
250                 return NET_NFC_NULL_PARAMETER;
251
252         record = ndef->records;
253         current = rawdata->buffer;
254
255         while(record)
256         {
257                 ndef_header = 0x00;
258
259                 if(record->MB)
260                         ndef_header |= NET_NFC_NDEF_RECORD_MASK_MB;
261                 if(record->ME)
262                         ndef_header |= NET_NFC_NDEF_RECORD_MASK_ME;
263                 if(record->CF)
264                         ndef_header |= NET_NFC_NDEF_RECORD_MASK_CF;
265                 if(record->SR)
266                         ndef_header |= NET_NFC_NDEF_RECORD_MASK_SR;
267                 if(record->IL)
268                         ndef_header |= NET_NFC_NDEF_RECORD_MASK_IL;
269
270                 ndef_header |= record->TNF;
271
272                 *current++ = ndef_header;
273
274                 /* check empty record */
275                 if(record->TNF == NET_NFC_NDEF_TNF_EMPTY)
276                 {
277                         /* set type length to zero */
278                         *current++ = 0x00;
279
280                         /* set payload length to zero */
281                         *current++ = 0x00;
282
283                         /* set ID length to zero */
284                         if(record->IL)
285                         {
286                                 *current++ = 0x00;
287                         }
288
289                         record = record->next;
290
291                         continue;
292                 }
293
294                 /* set type length */
295                 if(record->TNF == NET_NFC_NDEF_TNF_UNKNOWN || record->TNF == NET_NFC_NDEF_TNF_UNCHANGED)
296                 {
297                         *current++ = 0x00;
298                 }
299                 else
300                 {
301                         *current++ = record->type_s.length;
302                 }
303
304                 /* set payload length */
305                 if(record->SR)
306                 {
307                         *current++ = (uint8_t)(record->payload_s.length & 0x000000FF);
308                 }
309                 else
310                 {
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) ;
315                 }
316
317                 /* set ID length */
318                 if(record->IL)
319                 {
320                         *current++ = record->id_s.length;
321                 }
322
323                 /* set type buffer */
324                 if((record->TNF != NET_NFC_NDEF_TNF_UNKNOWN) && (record->TNF != NET_NFC_NDEF_TNF_UNCHANGED))
325                 {
326                         memcpy(current, record->type_s.buffer, record->type_s.length);
327                         current += record->type_s.length;
328                 }
329
330                 /* set ID buffer */
331                 memcpy(current, record->id_s.buffer, record->id_s.length);
332                 current += record->id_s.length;
333
334                 /* set payload buffer */
335                 memcpy(current, record->payload_s.buffer, record->payload_s.length);
336                 current += record->payload_s.length;
337
338                 record = record->next;
339         }
340
341         return NET_NFC_OK;
342 }
343
344 net_nfc_error_e net_nfc_util_append_record(ndef_message_s *msg, ndef_record_s *record)
345 {
346         if (msg == NULL || record == NULL)
347                 return NET_NFC_NULL_PARAMETER;
348
349         if (msg->recordCount == 0)
350         {
351                 // set short message and append
352                 record->MB = 1;
353                 record->ME = 1;
354                 record->next = NULL;
355
356                 msg->records = record;
357
358                 msg->recordCount++;
359
360                 NFC_DBG("record is added to NDEF message :: count [%d]", msg->recordCount);
361         }
362         else
363         {
364                 ndef_record_s *current = NULL;
365                 ndef_record_s *prev = NULL;
366
367                 // set flag :: this record is FIRST
368                 current = msg->records;
369
370                 if (current != NULL)
371                 {
372                         // first node
373                         current->MB = 1;
374                         current->ME = 0;
375
376                         prev = current;
377
378                         // second node
379                         current = current->next;
380
381                         while (current != NULL)
382                         {
383                                 current->MB = 0;
384                                 current->ME = 0;
385                                 prev = current;
386                                 current = current->next;
387                         }
388
389                         // set flag :: this record is END
390                         record->MB = 0;
391                         record->ME = 1;
392
393                         prev->next = record;
394                         msg->recordCount++;
395                 }
396
397         }
398
399         return NET_NFC_OK;
400 }
401
402 uint32_t net_nfc_util_get_ndef_message_length(ndef_message_s *message)
403 {
404         ndef_record_s *current;
405         int total = 0;
406
407         if (message == NULL)
408                 return 0;
409
410         current = message->records;
411
412         while (current != NULL)
413         {
414                 total += net_nfc_util_get_record_length(current);
415                 current = current->next;
416         }
417
418         return total;
419 }
420
421 void net_nfc_util_print_ndef_message(ndef_message_s *msg)
422 {
423         int idx = 0, idx2 = 0;
424         ndef_record_s *current = NULL;
425         char buffer[1024];
426
427         if (msg == NULL)
428         {
429                 return;
430         }
431
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++)
437         {
438                 if (current == NULL)
439                 {
440                         NFC_ERR("Message Record is NULL!! unexpected error");
441                         NFC_DBG("============================================================\n");
442                         return;
443                 }
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)
450                 {
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);
454                 }
455                 if (current->id_s.buffer != NULL)
456                 {
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);
460                 }
461                 if (current->payload_s.buffer != NULL)
462                 {
463                         NFC_DBG("Payload: ");
464                         for (idx2 = 0; idx2 < current->payload_s.length; idx2++)
465                         {
466                                 if (idx2 % 16 == 0)
467                                         NFC_DBG("\n\t");
468                                 NFC_DBG("%02X ", current->payload_s.buffer[idx2]);
469                         }
470                         NFC_DBG("\n");
471                 }
472                 current = current->next;
473         }
474         //                123456789012345678901234567890123456789012345678901234567890
475         NFC_DBG("============================================================\n");
476
477 }
478
479 net_nfc_error_e net_nfc_util_free_ndef_message(ndef_message_s *msg)
480 {
481         int idx = 0;
482         ndef_record_s *prev, *current;
483
484         if (msg == NULL)
485                 return NET_NFC_NULL_PARAMETER;
486
487         current = msg->records;
488
489         for (idx = 0; idx < msg->recordCount; idx++)
490         {
491                 if (current == NULL)
492                         break;
493
494                 prev = current;
495                 current = current->next;
496
497                 net_nfc_util_free_record(prev);
498         }
499
500         _net_nfc_util_free_mem(msg);
501
502         return NET_NFC_OK;
503 }
504
505 net_nfc_error_e net_nfc_util_create_ndef_message(ndef_message_s **ndef_message)
506 {
507         if (ndef_message == NULL) {
508                 return NET_NFC_NULL_PARAMETER;
509         }
510
511         _net_nfc_util_alloc_mem(*ndef_message, sizeof(ndef_message_s));
512         if (*ndef_message == NULL) {
513                 return NET_NFC_ALLOC_FAIL;
514         }
515
516         return NET_NFC_OK;
517 }
518
519 net_nfc_error_e net_nfc_util_remove_record_by_index(ndef_message_s *ndef_message, int index)
520 {
521         int current_idx = 0;
522         ndef_record_s *prev;
523         ndef_record_s *next;
524         ndef_record_s *current;
525
526         if (ndef_message == NULL)
527         {
528                 return NET_NFC_NULL_PARAMETER;
529         }
530
531         if (index < 0 || index >= ndef_message->recordCount)
532         {
533                 return NET_NFC_OUT_OF_BOUND;
534         }
535
536         if (index == 0)
537         {
538                 current = ndef_message->records;
539                 next = ndef_message->records->next;
540                 ndef_message->records = next;
541         }
542         else
543         {
544                 prev = ndef_message->records;
545                 for (; current_idx < index - 1; current_idx++)
546                 {
547                         prev = prev->next;
548                         if (prev == NULL)
549                         {
550                                 return NET_NFC_INVALID_FORMAT;
551                         }
552                 }
553                 current = prev->next;
554                 if (current == NULL)
555                 {
556                         return NET_NFC_INVALID_FORMAT;
557                 }
558                 next = current->next;
559                 prev->next = next;
560         }
561
562         net_nfc_util_free_record(current);
563         (ndef_message->recordCount)--;
564
565         return __net_nfc_repair_record_flags(ndef_message);
566 }
567
568 net_nfc_error_e net_nfc_util_get_record_by_index(ndef_message_s *ndef_message, int index, ndef_record_s **record)
569 {
570         ndef_record_s *current;
571         int idx = 0;
572
573         if (ndef_message == NULL || record == NULL)
574         {
575                 return NET_NFC_NULL_PARAMETER;
576         }
577
578         if (index < 0 || index >= ndef_message->recordCount)
579         {
580                 return NET_NFC_OUT_OF_BOUND;
581         }
582
583         current = ndef_message->records;
584
585         for (; current != NULL && idx < index; idx++)
586         {
587                 current = current->next;
588         }
589
590         *record = current;
591
592         return NET_NFC_OK;
593 }
594
595 net_nfc_error_e net_nfc_util_append_record_by_index(ndef_message_s *ndef_message, int index, ndef_record_s *record)
596 {
597         int idx = 0;
598         ndef_record_s *prev;
599         ndef_record_s *next;
600
601         if (ndef_message == NULL || record == NULL)
602         {
603                 return NET_NFC_NULL_PARAMETER;
604         }
605
606         if (index < 0 || index > ndef_message->recordCount)
607         {
608                 return NET_NFC_OUT_OF_BOUND;
609         }
610
611         prev = ndef_message->records;
612
613         if (index == 0)
614         {
615                 ndef_message->records = record;
616                 record->next = prev;
617         }
618         else
619         {
620                 for (; idx < index - 1; idx++)
621                 {
622                         prev = prev->next;
623                         if (prev == NULL)
624                         {
625                                 return NET_NFC_INVALID_FORMAT;
626                         }
627                 }
628                 next = prev->next;
629                 prev->next = record;
630                 record->next = next;
631         }
632         (ndef_message->recordCount)++;
633
634         return __net_nfc_repair_record_flags(ndef_message);
635 }
636
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)
638 {
639         int idx = 0;
640         ndef_record_s *tmp_record;
641         uint32_t type_length;
642         uint8_t *buf;
643
644         if (ndef_message == NULL || type == NULL || record == NULL)
645         {
646                 return NET_NFC_NULL_PARAMETER;
647         }
648
649         type_length = type->length;
650         buf = type->buffer;
651
652         /* remove prefix of nfc specific urn */
653         if (type_length > 12)
654         {
655                 if (memcmp(buf, "urn:nfc:ext:", 12) == 0 ||
656                                 memcmp(buf, "urn:nfc:wkt:", 12) == 0)
657                 {
658                         buf += 12;
659                         type_length -= 12;
660                 }
661         }
662
663         tmp_record = ndef_message->records;
664
665         for (; idx < ndef_message->recordCount; idx++)
666         {
667                 if (tmp_record == NULL)
668                 {
669                         *record = NULL;
670
671                         return NET_NFC_INVALID_FORMAT;
672                 }
673
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)
677                 {
678                         *record = tmp_record;
679
680                         return NET_NFC_OK;
681                 }
682
683                 tmp_record = tmp_record->next;
684         }
685
686         return NET_NFC_NO_DATA_FOUND;
687 }
688
689 net_nfc_error_e net_nfc_util_search_record_by_id(ndef_message_s *ndef_message, data_s *id, ndef_record_s **record)
690 {
691         int idx = 0;
692         ndef_record_s *record_in_msg;
693         uint32_t id_length;
694         uint8_t *buf;
695
696         if (ndef_message == NULL || id == NULL || record == NULL)
697         {
698                 return NET_NFC_NULL_PARAMETER;
699         }
700
701         id_length = id->length;
702         buf = id->buffer;
703
704         record_in_msg = ndef_message->records;
705
706         for (; idx < ndef_message->recordCount; idx++)
707         {
708                 if (record_in_msg == NULL)
709                 {
710                         *record = NULL;
711
712                         return NET_NFC_INVALID_FORMAT;
713                 }
714                 if (id_length == record_in_msg->id_s.length &&
715                                 memcmp(buf, record_in_msg->id_s.buffer, id_length) == 0)
716                 {
717                         *record = record_in_msg;
718
719                         return NET_NFC_OK;
720                 }
721
722                 record_in_msg = record_in_msg->next;
723         }
724
725         return NET_NFC_NO_DATA_FOUND;
726 }
727
728 static net_nfc_error_e __net_nfc_repair_record_flags(ndef_message_s *ndef_message)
729 {
730         int idx = 0;
731         ndef_record_s *record;
732
733         if (ndef_message == NULL)
734         {
735                 return NET_NFC_NULL_PARAMETER;
736         }
737
738         record = ndef_message->records;
739
740         if (ndef_message->recordCount == 1)
741         {
742                 if (record == NULL)
743                 {
744                         return NET_NFC_INVALID_FORMAT;
745                 }
746
747                 record->MB = 1;
748                 record->ME = 1;
749
750                 return NET_NFC_OK;
751         }
752
753         for (idx = 0; idx < ndef_message->recordCount; idx++)
754         {
755                 if (record == NULL)
756                 {
757                         return NET_NFC_INVALID_FORMAT;
758                 }
759
760                 if (idx == 0)
761                 {
762                         record->MB = 1;
763                         record->ME = 0;
764                 }
765                 else if (idx == ndef_message->recordCount - 1)
766                 {
767                         record->MB = 0;
768                         record->ME = 1;
769                 }
770                 else
771                 {
772                         record->MB = 0;
773                         record->ME = 0;
774                 }
775                 record = record->next;
776         }
777
778         return NET_NFC_OK;
779 }
780