Correcting the record type for "Hc" record
[platform/core/connectivity/nfc-manager-neard.git] / common / net_nfc_util_handover.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 <glib.h>
19
20 #include "net_nfc_typedef_internal.h"
21 #include "net_nfc_debug_internal.h"
22 #include "net_nfc_util_defines.h"
23 #include "net_nfc_util_internal.h"
24 #include "net_nfc_util_ndef_record.h"
25 #include "net_nfc_util_ndef_message.h"
26 #include "net_nfc_util_handover.h"
27
28 typedef struct _search_index
29 {
30         int target;
31         int current;
32         void *found;
33 } search_index;
34
35 void net_nfc_convert_byte_order(unsigned char *array, int size)
36 {
37         int i;
38         unsigned char tmp_char;
39
40         for (i=0;i<size/2;i++) {
41                 tmp_char = array[i];
42                 array[i] = array[size-1-i];
43                 array[size-1-i] = tmp_char;
44         }
45 }
46
47
48 static int __property_equal_to(gconstpointer key1, gconstpointer key2)
49 {
50         net_nfc_carrier_property_s *arg1 = (net_nfc_carrier_property_s *)key1;
51         net_nfc_carrier_property_s *arg2 = (net_nfc_carrier_property_s *)key2;
52
53         if (arg1->attribute < arg2->attribute)
54         {
55                 return -1;
56         }
57         else if (arg1->attribute > arg2->attribute)
58         {
59                 return 1;
60         }
61         else
62         {
63                 return 0;
64         }
65 }
66
67 static net_nfc_carrier_property_s *__find_property_by_attrubute(GList *list, uint16_t attribute)
68 {
69         GList *found = NULL;
70         net_nfc_carrier_property_s temp;
71
72         temp.attribute = attribute;
73         found = g_list_find_custom(list, &temp, __property_equal_to);
74
75         if (found == NULL)
76         {
77                 return NULL;
78         }
79
80         return (net_nfc_carrier_property_s *)found->data;
81 }
82
83 static void __find_nth_group(gpointer data, gpointer user_data)
84 {
85         net_nfc_carrier_property_s *info = (net_nfc_carrier_property_s *)data;
86         search_index *nth = (search_index *)user_data;
87
88         if (info == NULL || user_data == NULL)
89                 return;
90
91         if (info->is_group)
92         {
93                 if (nth->current == nth->target)
94                 {
95                         nth->found = data;
96                 }
97                 nth->current++;
98         }
99 }
100
101 static void __free_all_data(gpointer data, gpointer user_data)
102 {
103         net_nfc_carrier_property_s *info = (net_nfc_carrier_property_s *)data;
104
105         if (info == NULL)
106                 return;
107
108         if (info->is_group)
109         {
110                 NFC_DBG("FREE: group is found");
111                 net_nfc_util_free_carrier_group((net_nfc_carrier_property_s *)info);
112         }
113         else
114         {
115                 NFC_DBG("FREE: element is found ATTRIB:0x%X length:%d", info->attribute, info->length);
116                 _net_nfc_util_free_mem(info->data);
117                 _net_nfc_util_free_mem(info);
118         }
119 }
120
121 static net_nfc_error_e __net_nfc_util_create_connection_handover_collsion_resolution_record(ndef_record_s **record)
122 {
123         uint32_t state = 0;
124         data_s typeName = { 0 };
125         data_s payload = { 0 };
126         uint8_t rand_buffer[2] = { 0, 0 };
127         uint16_t random_num;
128
129         if (record == NULL)
130                 return NET_NFC_NULL_PARAMETER;
131
132         state = (uint32_t)time(NULL);
133         random_num = (unsigned short)rand_r(&state);
134
135         typeName.buffer = (uint8_t *)COLLISION_DETECT_RECORD_TYPE;
136         typeName.length = strlen(COLLISION_DETECT_RECORD_TYPE);
137
138         rand_buffer[0] = (random_num & 0xff00) >> 8;    // MSB
139         rand_buffer[1] = (random_num & 0x00ff); // LSB
140
141         payload.buffer = rand_buffer;
142         payload.length = 2;
143
144         NFC_DBG("rand number = [0x%x] [0x%x] => [0x%x]", payload.buffer[0], payload.buffer[1], random_num);
145
146         return net_nfc_util_create_record(NET_NFC_RECORD_WELL_KNOWN_TYPE, &typeName, NULL, &payload, record);
147 }
148
149 static int __net_nfc_get_size_of_attribute(int attribute)
150 {
151         switch (attribute)
152         {
153         case NET_NFC_BT_ATTRIBUTE_UUID16_PART :
154         case NET_NFC_BT_ATTRIBUTE_UUID16 :
155         case NET_NFC_BT_ATTRIBUTE_UUID32_PART :
156         case NET_NFC_BT_ATTRIBUTE_UUID32 :
157         case NET_NFC_BT_ATTRIBUTE_UUID128_PART :
158         case NET_NFC_BT_ATTRIBUTE_UUID128 :
159         case NET_NFC_BT_ATTRIBUTE_NAME_PART :
160         case NET_NFC_BT_ATTRIBUTE_NAME :
161         case NET_NFC_BT_ATTRIBUTE_TXPOWER :
162         case NET_NFC_BT_ATTRIBUTE_OOB_COD :
163         case NET_NFC_BT_ATTRIBUTE_OOB_HASH_C :
164         case NET_NFC_BT_ATTRIBUTE_OOB_HASH_R :
165         case NET_NFC_BT_ATTRIBUTE_ID :
166         case NET_NFC_BT_ATTRIBUTE_MANUFACTURER :
167         case NET_NFC_BT_ATTRIBUTE_ADDRESS :
168                 //              case NET_NFC_WIFI_ATTRIBUTE_VERSION2:
169                 return 1;
170
171         default :
172                 return 2;
173         }
174 }
175
176 net_nfc_error_e net_nfc_util_create_carrier_config(net_nfc_carrier_config_s **config, net_nfc_conn_handover_carrier_type_e type)
177 {
178         if (config == NULL)
179         {
180                 return NET_NFC_NULL_PARAMETER;
181         }
182
183         if (type < 0 || type >= NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN)
184         {
185                 return NET_NFC_OUT_OF_BOUND;
186         }
187
188         _net_nfc_util_alloc_mem(*config, sizeof(net_nfc_carrier_config_s));
189         if (*config == NULL)
190         {
191                 return NET_NFC_ALLOC_FAIL;
192         }
193
194         (*config)->type = type;
195         (*config)->length = 0;
196
197         return NET_NFC_OK;
198 }
199
200 net_nfc_error_e net_nfc_util_add_carrier_config_property(net_nfc_carrier_config_s *config, uint16_t attribute, uint16_t size, uint8_t * data)
201 {
202         net_nfc_carrier_property_s *elem = NULL;
203
204         NFC_DBG("ADD property: [ATTRIB:0x%X, SIZE:%d]", attribute, size);
205
206         if (config == NULL || data == NULL)
207         {
208                 return NET_NFC_NULL_PARAMETER;
209         }
210
211         if (__find_property_by_attrubute(config->data, attribute) != NULL)
212         {
213                 return NET_NFC_ALREADY_REGISTERED;
214         }
215
216         _net_nfc_util_alloc_mem(elem, sizeof (net_nfc_carrier_property_s));
217         if (elem == NULL)
218         {
219                 return NET_NFC_ALLOC_FAIL;
220         }
221         elem->attribute = attribute;
222         elem->length = size;
223         elem->is_group = false;
224
225         _net_nfc_util_alloc_mem(elem->data, size);
226         if (elem->data == NULL)
227         {
228                 _net_nfc_util_free_mem(elem);
229                 return NET_NFC_ALLOC_FAIL;
230         }
231         memcpy(elem->data, data, size);
232
233         config->data = g_list_append(config->data, elem);
234         config->length += size + 2 * __net_nfc_get_size_of_attribute(attribute);
235
236         NFC_DBG("ADD completed total length %d", config->length);
237
238         return NET_NFC_OK;
239 }
240
241 net_nfc_error_e net_nfc_util_remove_carrier_config_property(net_nfc_carrier_config_s *config, uint16_t attribute)
242 {
243         net_nfc_carrier_property_s *elem = NULL;
244
245         if (config == NULL)
246         {
247                 return NET_NFC_NULL_PARAMETER;
248         }
249
250         elem = __find_property_by_attrubute(config->data, attribute);
251         if (elem == NULL)
252         {
253                 return NET_NFC_NO_DATA_FOUND;
254         }
255
256         config->data = g_list_remove(config->data, elem);
257         config->length -= (elem->length + 2 * __net_nfc_get_size_of_attribute(attribute));
258
259         if (elem->is_group)
260         {
261                 net_nfc_util_free_carrier_group((net_nfc_carrier_property_s *)elem);
262         }
263         else
264         {
265                 _net_nfc_util_free_mem(elem->data);
266                 _net_nfc_util_free_mem(elem);
267         }
268
269         return NET_NFC_OK;
270 }
271
272 net_nfc_error_e net_nfc_util_get_carrier_config_property(
273                 net_nfc_carrier_config_s *config, uint16_t attribute, uint16_t *size, uint8_t **data)
274 {
275         net_nfc_carrier_property_s *elem = NULL;
276
277         if (config == NULL || size == NULL || data == NULL)
278                 return NET_NFC_NULL_PARAMETER;
279
280         elem = __find_property_by_attrubute(config->data, attribute);
281         if (elem == NULL)
282         {
283                 *size = 0;
284                 *data = NULL;
285                 return NET_NFC_NO_DATA_FOUND;
286         }
287
288         *size = elem->length;
289         if (elem->is_group)
290                 *data = NULL;
291         else
292                 *data = elem->data;
293
294         return NET_NFC_OK;
295 }
296
297 net_nfc_error_e net_nfc_util_append_carrier_config_group(
298                 net_nfc_carrier_config_s *config, net_nfc_carrier_property_s *group)
299 {
300         if (config == NULL || group == NULL)
301                 return NET_NFC_NULL_PARAMETER;
302
303         if (g_list_find(config->data, group) != NULL)
304                 return NET_NFC_ALREADY_REGISTERED;
305
306         config->data = g_list_append(config->data, group);
307         config->length += group->length+4;
308
309         return NET_NFC_OK;
310 }
311
312 net_nfc_error_e net_nfc_util_remove_carrier_config_group(net_nfc_carrier_config_s *config, net_nfc_carrier_property_s *group)
313 {
314         if (config == NULL || group == NULL)
315         {
316                 return NET_NFC_NULL_PARAMETER;
317         }
318
319         if (g_list_find(config->data, group) != NULL)
320         {
321                 return NET_NFC_NO_DATA_FOUND;
322         }
323
324         config->length -= group->length;
325         config->data = g_list_remove(config->data, group);
326
327         net_nfc_util_free_carrier_group((net_nfc_carrier_property_s *)group);
328
329         return NET_NFC_OK;
330 }
331
332 net_nfc_error_e net_nfc_util_get_carrier_config_group(net_nfc_carrier_config_s *config, int index, net_nfc_carrier_property_s **group)
333 {
334         search_index result;
335
336         if (config == NULL || group == NULL)
337         {
338                 return NET_NFC_NULL_PARAMETER;
339         }
340
341         if (index < 0)
342         {
343                 return NET_NFC_OUT_OF_BOUND;
344         }
345
346         result.current = 0;
347         result.target = index;
348         result.found = NULL;
349
350         g_list_foreach(config->data, __find_nth_group, &result);
351
352         if (result.found == NULL)
353         {
354                 return NET_NFC_NO_DATA_FOUND;
355         }
356         *group = (net_nfc_carrier_property_s *)result.found;
357
358         return NET_NFC_OK;
359 }
360
361 net_nfc_error_e net_nfc_util_free_carrier_config(net_nfc_carrier_config_s *config)
362 {
363         if (config == NULL)
364         {
365                 return NET_NFC_NULL_PARAMETER;
366         }
367
368         g_list_foreach(config->data, __free_all_data, NULL);
369         g_list_free(config->data);
370
371         _net_nfc_util_free_mem(config);
372
373         return NET_NFC_OK;
374 }
375
376 net_nfc_error_e net_nfc_util_create_carrier_config_group(net_nfc_carrier_property_s **group, uint16_t attribute)
377 {
378         if (group == NULL)
379         {
380                 return NET_NFC_NULL_PARAMETER;
381         }
382
383         _net_nfc_util_alloc_mem(*group, sizeof(net_nfc_carrier_property_s));
384         if (*group == NULL)
385         {
386                 return NET_NFC_ALLOC_FAIL;
387         }
388
389         (*group)->attribute = attribute;
390         (*group)->is_group = true;
391
392         return NET_NFC_OK;
393 }
394
395 net_nfc_error_e net_nfc_util_add_carrier_config_group_property(net_nfc_carrier_property_s *group, uint16_t attribute, uint16_t size, uint8_t *data)
396 {
397         net_nfc_carrier_property_s *elem = NULL;
398
399         NFC_DBG("ADD group property: [ATTRIB:0x%X, SIZE:%d]", attribute, size);
400
401         if (group == NULL || data == NULL)
402         {
403                 return NET_NFC_NULL_PARAMETER;
404         }
405
406         if (__find_property_by_attrubute((GList *)group->data, attribute) != NULL)
407         {
408                 return NET_NFC_ALREADY_REGISTERED;
409         }
410
411         _net_nfc_util_alloc_mem(elem, sizeof (net_nfc_carrier_property_s));
412         if (elem == NULL)
413         {
414                 return NET_NFC_ALLOC_FAIL;
415         }
416         elem->attribute = attribute;
417         elem->length = size;
418         elem->is_group = false;
419
420         _net_nfc_util_alloc_mem(elem->data, size);
421         if (elem->data == NULL)
422         {
423                 _net_nfc_util_free_mem(elem);
424                 return NET_NFC_ALLOC_FAIL;
425         }
426         memcpy(elem->data, data, size);
427         group->length += size + 2 * __net_nfc_get_size_of_attribute(attribute);
428         group->data = g_list_append((GList *)(group->data), elem);
429
430         NFC_DBG("ADD group completed total length %d", group->length);
431
432         return NET_NFC_OK;
433 }
434
435 net_nfc_error_e net_nfc_util_get_carrier_config_group_property(net_nfc_carrier_property_s *group, uint16_t attribute, uint16_t *size, uint8_t ** data)
436 {
437         net_nfc_carrier_property_s *elem = NULL;
438
439         if (group == NULL || size == NULL || data == NULL)
440         {
441                 return NET_NFC_NULL_PARAMETER;
442         }
443
444         elem = __find_property_by_attrubute((GList*)(group->data), attribute);
445         if (elem == NULL)
446         {
447                 *size = 0;
448                 *data = NULL;
449                 return NET_NFC_NO_DATA_FOUND;
450         }
451
452         *size = elem->length;
453         *data = elem->data;
454
455         return NET_NFC_OK;
456 }
457
458 net_nfc_error_e net_nfc_util_remove_carrier_config_group_property(net_nfc_carrier_property_s *group, uint16_t attribute)
459 {
460         net_nfc_carrier_property_s *elem = NULL;
461
462         if (group == NULL)
463         {
464                 return NET_NFC_NULL_PARAMETER;
465         }
466
467         elem = __find_property_by_attrubute((GList*)(group->data), attribute);
468         if (elem == NULL)
469         {
470                 return NET_NFC_NO_DATA_FOUND;
471         }
472         group->length -= elem->length;
473         group->data = g_list_remove((GList*)(group->data), elem);
474
475         _net_nfc_util_free_mem(elem->data);
476         _net_nfc_util_free_mem(elem);
477
478         return NET_NFC_OK;
479
480 }
481
482 net_nfc_error_e net_nfc_util_free_carrier_group(net_nfc_carrier_property_s *group)
483 {
484         if (group == NULL)
485         {
486                 return NET_NFC_NULL_PARAMETER;
487         }
488         g_list_foreach((GList*)(group->data), __free_all_data, NULL);
489         g_list_free((GList*)(group->data));
490
491         _net_nfc_util_free_mem(group);
492
493         return NET_NFC_OK;
494 }
495
496 static void __make_serial_wifi(gpointer data, gpointer user_data)
497 {
498         net_nfc_carrier_property_s *info = (net_nfc_carrier_property_s *)data;
499         data_s *payload = user_data;
500         uint8_t *current;
501         int inc = 0;
502
503         if (info == NULL || user_data == NULL)
504                 return;
505
506         current = payload->buffer + payload->length;
507         inc = __net_nfc_get_size_of_attribute(info->attribute);
508
509         if (info->is_group)
510         {
511                 NFC_DBG("[WIFI]Found Group make recursive");
512                 *(uint16_t *)current = info->attribute;
513                 net_nfc_convert_byte_order(current,2);
514                 *(uint16_t *)(current + inc) = info->length;
515                 net_nfc_convert_byte_order((current + inc),2);
516                 payload->length += (inc + inc);
517                 g_list_foreach((GList *)info->data, __make_serial_wifi, payload);
518         }
519         else
520         {
521                 NFC_DBG("[WIFI]Element is found attrib:0x%X length:%d current:%d", info->attribute, info->length, payload->length);
522                 *(uint16_t *)current = info->attribute;
523                 net_nfc_convert_byte_order(current,2);
524                 *(uint16_t *)(current + inc) = info->length;
525                 net_nfc_convert_byte_order((current + inc),2);
526                 memcpy(current + inc + inc, info->data, info->length);
527                 payload->length += (inc + inc + info->length);
528         }
529 }
530
531 static void __make_serial_bt(gpointer data, gpointer user_data)
532 {
533         net_nfc_carrier_property_s *info = (net_nfc_carrier_property_s *)data;
534         data_s *payload = user_data;
535         uint8_t *current;
536         int inc = 0;
537
538         if (info == NULL || user_data == NULL)
539                 return;
540
541         current = payload->buffer + payload->length; /* payload->length is zero */
542
543         if (info->is_group)
544         {
545                 NFC_DBG("[BT]Found Group. call recursive");
546                 g_list_foreach((GList *)info->data, __make_serial_bt, payload);
547         }
548         else
549         {
550                 if (info->attribute != NET_NFC_BT_ATTRIBUTE_ADDRESS)
551                 {
552                         NFC_DBG("[BT]Element is found attrib:0x%X length:%d current:%d", info->attribute, info->length, payload->length);
553                         inc = __net_nfc_get_size_of_attribute(info->attribute);
554                         *current = info->length + 1;
555                         *(current + inc) = info->attribute;
556                         memcpy(current + inc + inc, info->data, info->length);
557                         payload->length += (inc + inc + info->length);
558                 }
559                 else
560                 {
561                         NFC_DBG("[BT]BT address is found length:%d", info->length);
562                         memcpy(current, info->data, info->length);
563                         payload->length += (info->length);
564                 }
565         }
566 }
567
568 net_nfc_error_e net_nfc_util_create_ndef_record_with_carrier_config(ndef_record_s **record, net_nfc_carrier_config_s *config)
569 {
570         data_s payload = { NULL, 0 };
571         data_s record_type = { NULL, 0 };
572
573         if (record == NULL || config == NULL)
574         {
575                 return NET_NFC_NULL_PARAMETER;
576         }
577
578         _net_nfc_util_alloc_mem(payload.buffer, config->length);
579         if (payload.buffer == NULL)
580         {
581                 return NET_NFC_ALLOC_FAIL;
582         }
583         payload.length = 0; /* this should be zero because this will be used as current position of data written */
584
585         if (config->type == NET_NFC_CONN_HANDOVER_CARRIER_WIFI_BSS)
586         {
587                 record_type.buffer = (uint8_t *)CONN_HANDOVER_WIFI_BSS_CARRIER_MIME_NAME;
588                 record_type.length = strlen(CONN_HANDOVER_WIFI_BSS_CARRIER_MIME_NAME);
589                 g_list_foreach(config->data, __make_serial_wifi, &payload);
590         }
591         else if (config->type == NET_NFC_CONN_HANDOVER_CARRIER_WIFI_IBSS)
592         {
593                 record_type.buffer = (uint8_t *)CONN_HANDOVER_WIFI_IBSS_CARRIER_MIME_NAME;
594                 record_type.length = strlen(CONN_HANDOVER_WIFI_IBSS_CARRIER_MIME_NAME);
595                 g_list_foreach(config->data, __make_serial_wifi, &payload);
596         }
597         else if (config->type == NET_NFC_CONN_HANDOVER_CARRIER_BT)
598         {
599                 record_type.buffer = (uint8_t *)CONN_HANDOVER_BT_CARRIER_MIME_NAME;
600                 record_type.length = strlen(CONN_HANDOVER_BT_CARRIER_MIME_NAME);
601                 payload.buffer += 2; /* OOB total length */
602                 g_list_foreach(config->data, __make_serial_bt, &payload);
603                 payload.buffer -= 2; /* return to original */
604                 payload.length += 2;
605                 payload.buffer[0] = payload.length & 0xFF;
606                 payload.buffer[1] = (payload.length >> 8) & 0xFF;
607         }
608         else
609         {
610                 return NET_NFC_NOT_SUPPORTED;
611         }
612
613         NFC_DBG("payload length = %d", payload.length);
614
615         return net_nfc_util_create_record(NET_NFC_RECORD_MIME_TYPE, &record_type, NULL, &payload, record);
616 }
617
618 static net_nfc_error_e __net_nfc_get_list_from_serial_for_wifi(GList **list, uint8_t *data, uint32_t length)
619 {
620         uint8_t *current = data;
621         uint8_t *last = current + length;
622
623         while (current < last)
624         {
625                 net_nfc_carrier_property_s *elem = NULL;
626                 _net_nfc_util_alloc_mem(elem, sizeof(net_nfc_carrier_property_s));
627                 if (elem == NULL)
628                 {
629                         return NET_NFC_ALLOC_FAIL;
630                 }
631                 elem->attribute = current[0]<<8|current[1];
632                 elem->length = current[2]<<8|current[3];
633
634                 if (elem->attribute == NET_NFC_WIFI_ATTRIBUTE_CREDENTIAL)
635                 {
636                         __net_nfc_get_list_from_serial_for_wifi(list, (current + 4), elem->length);
637                         elem->is_group = true;
638                 }
639                 else
640                 {
641                         _net_nfc_util_alloc_mem(elem->data, elem->length);
642                         if (elem->data == NULL)
643                         {
644                                 _net_nfc_util_free_mem(elem);
645                                 return NET_NFC_ALLOC_FAIL;
646                         }
647                         memcpy(elem->data, (current + 4), elem->length);
648                         elem->is_group = false;
649                 }
650                 *list = g_list_append(*list, elem);
651                 current += (4 + elem->length);
652         }
653
654         return NET_NFC_OK;
655 }
656
657 net_nfc_error_e __net_nfc_get_list_from_serial_for_bt(GList **list, uint8_t *data, uint32_t length)
658 {
659         net_nfc_carrier_property_s *elem = NULL;
660         uint8_t *current = data;
661         uint8_t *last = NULL;
662
663         current += 2; /* remove oob data length  two bytes length*/
664         length -= 2;
665
666         _net_nfc_util_alloc_mem(elem, sizeof(net_nfc_carrier_property_s));
667         if (elem == NULL)
668         {
669                 return NET_NFC_ALLOC_FAIL;
670         }
671         elem->attribute = (uint16_t)NET_NFC_BT_ATTRIBUTE_ADDRESS;
672         elem->length = 6; /* BT address length is always 6 */
673
674         _net_nfc_util_alloc_mem(elem->data, elem->length);
675         if (elem->data == NULL)
676         {
677                 _net_nfc_util_free_mem(elem);
678                 return NET_NFC_ALLOC_FAIL;
679         }
680         memcpy(elem->data, current, elem->length);
681         elem->is_group = false;
682
683         current += 6; /* BT address length is always 6 */
684         length -= 6; /* substracted by 6 (Address length)*/
685         *list = g_list_append(*list, elem);
686
687         last = current + length;
688
689         while (current < last)
690         {
691                 _net_nfc_util_alloc_mem(elem, sizeof(net_nfc_carrier_property_s));
692                 if (elem == NULL)
693                 {
694                         return NET_NFC_ALLOC_FAIL;
695                 }
696                 elem->length = *((uint8_t *)current) - 1;
697                 elem->attribute = *((uint8_t *)(++current));
698
699                 _net_nfc_util_alloc_mem(elem->data, elem->length);
700                 if (elem->data == NULL)
701                 {
702                         _net_nfc_util_free_mem(elem);
703                         return NET_NFC_ALLOC_FAIL;
704                 }
705                 memcpy(elem->data, (++current), elem->length);
706                 elem->is_group = false;
707
708                 current += elem->length;
709                 *list = g_list_append(*list, elem);
710         }
711
712         return NET_NFC_OK;
713 }
714
715 net_nfc_error_e net_nfc_util_create_carrier_config_from_config_record(net_nfc_carrier_config_s **config, ndef_record_s *record)
716 {
717         net_nfc_error_e result = NET_NFC_OK;
718         net_nfc_conn_handover_carrier_type_e type;
719
720         if (record == NULL || config == NULL)
721         {
722                 return NET_NFC_NULL_PARAMETER;
723         }
724
725         if (strncmp((char *)record->type_s.buffer, CONN_HANDOVER_WIFI_BSS_CARRIER_MIME_NAME, record->type_s.length) == 0)
726         {
727                 type = NET_NFC_CONN_HANDOVER_CARRIER_WIFI_BSS;
728         }
729         else if (strncmp((char *)record->type_s.buffer, CONN_HANDOVER_WIFI_IBSS_CARRIER_MIME_NAME, record->type_s.length) == 0)
730         {
731                 type = NET_NFC_CONN_HANDOVER_CARRIER_WIFI_IBSS;
732         }
733         else if (strncmp((char *)record->type_s.buffer, CONN_HANDOVER_BT_CARRIER_MIME_NAME, record->type_s.length) == 0)
734         {
735                 type = NET_NFC_CONN_HANDOVER_CARRIER_BT;
736         }
737         else
738         {
739                 NFC_DBG("Record type is not config type");
740                 return NET_NFC_INVALID_FORMAT;
741         }
742
743         result = net_nfc_util_create_carrier_config(config, type);
744         if (*config == NULL)
745         {
746                 return NET_NFC_ALLOC_FAIL;
747         }
748
749         switch ((*config)->type)
750         {
751         case NET_NFC_CONN_HANDOVER_CARRIER_WIFI_BSS :
752         case NET_NFC_CONN_HANDOVER_CARRIER_WIFI_IBSS :
753                 result = __net_nfc_get_list_from_serial_for_wifi((GList **)&((*config)->data), record->payload_s.buffer, record->payload_s.length);
754                 break;
755         case NET_NFC_CONN_HANDOVER_CARRIER_BT :
756                 result = __net_nfc_get_list_from_serial_for_bt((GList **)&((*config)->data), record->payload_s.buffer, record->payload_s.length);
757                 break;
758         case NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN :
759                 result = NET_NFC_NOT_SUPPORTED;
760                 break;
761         }
762
763         if (result != NET_NFC_OK)
764         {
765                 net_nfc_util_free_carrier_config((net_nfc_carrier_config_s *)*config);
766         }
767
768         return result;
769 }
770
771 net_nfc_error_e net_nfc_util_create_handover_request_message(ndef_message_s **message)
772 {
773         ndef_message_s *inner_message = NULL;
774         net_nfc_error_e error;
775         ndef_record_s *record = NULL;
776         data_s type = { NULL, 0 };
777         data_s payload = { NULL, 0 };
778         int size = 0;
779
780         if (message == NULL)
781         {
782                 return NET_NFC_NULL_PARAMETER;
783         }
784
785         error = net_nfc_util_create_ndef_message(message);
786         if (error != NET_NFC_OK)
787         {
788                 return error;
789         }
790
791         error = net_nfc_util_create_ndef_message(&inner_message);
792         if (error != NET_NFC_OK)
793         {
794                 net_nfc_util_free_ndef_message(*message);
795                 *message = NULL;
796
797                 return error;
798         }
799
800         __net_nfc_util_create_connection_handover_collsion_resolution_record(&record);
801         net_nfc_util_append_record(inner_message, record);
802
803         size = net_nfc_util_get_ndef_message_length(inner_message) + 1;
804         _net_nfc_util_alloc_mem(payload.buffer, size);
805         if (payload.buffer == NULL)
806         {
807                 net_nfc_util_free_ndef_message(inner_message);
808                 net_nfc_util_free_ndef_message(*message);
809                 *message = NULL;
810
811                 return NET_NFC_ALLOC_FAIL;
812         }
813         payload.length = size;
814
815         uint8_t version = CH_VERSION;
816
817         (payload.buffer)[0] = version;
818         (payload.buffer)++;
819         (payload.length)--;
820
821         error = net_nfc_util_convert_ndef_message_to_rawdata(inner_message, &payload);
822         if (error != NET_NFC_OK)
823         {
824                 _net_nfc_util_free_mem(payload.buffer);
825                 net_nfc_util_free_ndef_message(inner_message);
826                 net_nfc_util_free_ndef_message(*message);
827                 *message = NULL;
828
829                 return error;
830         }
831
832         net_nfc_util_free_ndef_message(inner_message);
833         (payload.buffer)--;
834         (payload.length)++;
835
836         type.buffer = (uint8_t *)CH_REQ_RECORD_TYPE;
837         type.length = strlen(CH_REQ_RECORD_TYPE);
838
839         net_nfc_util_create_record(NET_NFC_RECORD_WELL_KNOWN_TYPE, &type, NULL, &payload, &record);
840         net_nfc_util_append_record(*message, record);
841
842         _net_nfc_util_free_mem(payload.buffer);
843
844         return NET_NFC_OK;
845 }
846
847 net_nfc_error_e net_nfc_util_create_handover_select_message(ndef_message_s **message)
848 {
849         net_nfc_error_e error = NET_NFC_OK;
850         ndef_record_s *record = NULL;
851         data_s type = { NULL, 0 };
852         data_s payload = { NULL, 0 };
853
854         if (message == NULL)
855         {
856                 return NET_NFC_NULL_PARAMETER;
857         }
858
859         error = net_nfc_util_create_ndef_message(message);
860         if (error != NET_NFC_OK)
861         {
862                 return error;
863         }
864
865         _net_nfc_util_alloc_mem(payload.buffer, 1);
866         if (payload.buffer == NULL)
867         {
868                 net_nfc_util_free_ndef_message(*message);
869                 return NET_NFC_ALLOC_FAIL;
870         }
871         payload.length = (uint32_t)1;
872
873         (payload.buffer)[0] = CH_VERSION;
874
875         type.buffer = (uint8_t*)CH_SEL_RECORD_TYPE;
876         type.length = strlen(CH_SEL_RECORD_TYPE);
877
878         net_nfc_util_create_record(NET_NFC_RECORD_WELL_KNOWN_TYPE, &type, NULL, &payload, &record);
879         net_nfc_util_append_record(*message, record);
880
881         _net_nfc_util_free_mem(payload.buffer);
882
883         return NET_NFC_OK;
884 }
885
886 net_nfc_error_e net_nfc_util_create_handover_carrier_record(ndef_record_s ** record)
887 {
888         data_s payload = {NULL,0};
889
890         data_s type = { NULL, 0 };
891         uint8_t *buffer_ptr = NULL;
892
893         _net_nfc_util_alloc_mem(payload.buffer,26);//hardcoded as of now
894         if (payload.buffer == NULL)
895                 return NET_NFC_ALLOC_FAIL;
896
897         buffer_ptr = payload.buffer;
898
899         //copy ctf
900         buffer_ptr[0]= NET_NFC_RECORD_MIME_TYPE;
901
902         //copy the MIME type length
903         buffer_ptr[1] = strlen(CONN_HANDOVER_WIFI_BSS_CARRIER_MIME_NAME);
904
905         //copy the MIME
906         memcpy(&buffer_ptr[2],CONN_HANDOVER_WIFI_BSS_CARRIER_MIME_NAME,(payload.buffer)[1]);
907         payload.buffer[25] = '\0';
908
909         payload.length =26;
910         type.buffer = (uint8_t *)CH_CAR_RECORD_TYPE;
911         type.length = strlen(CH_CAR_RECORD_TYPE);
912
913         net_nfc_util_create_record(NET_NFC_RECORD_WELL_KNOWN_TYPE,
914                         &type,
915                         NULL,
916                         &payload,
917                         (ndef_record_s **)record);
918
919         _net_nfc_util_free_mem(payload.buffer);
920
921         return NET_NFC_OK;
922 }
923 net_nfc_error_e net_nfc_util_create_handover_error_record(ndef_record_s **record, uint8_t reason, uint32_t data)
924 {
925         data_s type;
926         data_s payload;
927         int size = 1;
928
929         switch (reason)
930         {
931         case 0x01 :
932                 size += 1;
933                 break;
934         case 0x02 :
935                 size += 4;
936                 break;
937         case 0x03 :
938                 size += 1;
939                 break;
940         }
941
942         _net_nfc_util_alloc_mem(payload.buffer, size);
943         if (payload.buffer == NULL)
944         {
945                 return NET_NFC_ALLOC_FAIL;
946         }
947         payload.length = size;
948
949         type.buffer = (uint8_t *)ERROR_RECORD_TYPE;
950         type.length = strlen(ERROR_RECORD_TYPE);
951
952         net_nfc_util_create_record(NET_NFC_RECORD_WELL_KNOWN_TYPE, &type, NULL, &payload, (ndef_record_s **)record);
953
954         _net_nfc_util_free_mem(payload.buffer);
955
956         return NET_NFC_OK;
957 }
958
959 /*      inner_msg should be freed after using   */
960
961 static net_nfc_error_e __net_nfc_get_inner_message(ndef_message_s *message, ndef_message_s *inner_msg)
962 {
963         net_nfc_error_e error;
964         ndef_record_s *inner_record = NULL;
965
966         if (message == NULL || inner_msg == NULL)
967         {
968                 return NET_NFC_NULL_PARAMETER;
969         }
970
971         inner_record = message->records;
972         if (inner_record == NULL)
973         {
974                 // This message is not connection handover message
975                 return NET_NFC_INVALID_FORMAT;
976         }
977
978         if (strncmp((char*)(inner_record->type_s.buffer), CH_REQ_RECORD_TYPE, (size_t)(inner_record->type_s.length)) != 0
979                         && strncmp((char*)(inner_record->type_s.buffer), CH_SEL_RECORD_TYPE, (size_t)(inner_record->type_s.length)) != 0)
980         {
981                 // This message is not connection handover message
982                 return NET_NFC_INVALID_FORMAT;
983         }
984
985         if (inner_record->payload_s.length > 1)
986         {
987                 /* There is Alternative Carrier Record or Collision Res. Rec. */
988                 (inner_record->payload_s.buffer)++; /* version */
989                 (inner_record->payload_s.length)--;
990                 error = net_nfc_util_convert_rawdata_to_ndef_message(&(inner_record->payload_s), inner_msg);
991                 (inner_record->payload_s.buffer)--;
992                 (inner_record->payload_s.length)++;
993         }
994         else
995         {
996                 error = NET_NFC_NO_DATA_FOUND;
997         }
998
999         return error;
1000 }
1001
1002 static net_nfc_error_e __net_nfc_replace_inner_message(ndef_message_s *message, ndef_message_s *inner_msg)
1003 {
1004         net_nfc_error_e error;
1005         ndef_record_s *inner_record = NULL;
1006
1007         if (message == NULL || inner_msg == NULL)
1008         {
1009                 return NET_NFC_NULL_PARAMETER;
1010         }
1011
1012         inner_record = message->records;
1013         if (inner_record == NULL)
1014         {
1015                 // This message is not connection handover message
1016                 NFC_ERR("inner_record == NULL");
1017                 return NET_NFC_INVALID_FORMAT;
1018         }
1019
1020         if (strncmp((char *)(inner_record->type_s.buffer), CH_REQ_RECORD_TYPE, (size_t)(inner_record->type_s.length)) != 0
1021                         && strncmp((char *)(inner_record->type_s.buffer), CH_SEL_RECORD_TYPE, (size_t)(inner_record->type_s.length)) != 0)
1022         {
1023                 // This message is not connection handover message
1024                 NFC_ERR("unknown type [%s]", inner_record->type_s.buffer);
1025                 return NET_NFC_INVALID_FORMAT;
1026         }
1027
1028         if (inner_record->payload_s.length >= 1)
1029         {
1030                 /* There is Alternative Carrier Record or Collision Res. Rec. */
1031                 data_s tdata = { NULL, 0 };
1032                 int inner_length;
1033
1034                 inner_length = net_nfc_util_get_ndef_message_length(inner_msg);
1035
1036                 _net_nfc_util_alloc_mem(tdata.buffer, inner_length + 1);
1037                 if (tdata.buffer == NULL)
1038                 {
1039                         return NET_NFC_ALLOC_FAIL;
1040                 }
1041
1042                 (tdata.buffer)++;
1043                 tdata.length = inner_length;
1044                 error = net_nfc_util_convert_ndef_message_to_rawdata(inner_msg, &tdata);
1045                 if (error == NET_NFC_OK)
1046                 {
1047                         (tdata.buffer)--;
1048                         (tdata.length)++;
1049                         (tdata.buffer)[0] = (inner_record->payload_s.buffer)[0];
1050                         _net_nfc_util_free_mem(inner_record->payload_s.buffer);
1051                         inner_record->payload_s.buffer = tdata.buffer;
1052                         inner_record->payload_s.length = tdata.length;
1053                 }
1054                 else
1055                 {
1056                         NFC_ERR("net_nfc_util_convert_ndef_message_to_rawdata failed [%d]", error);
1057                         _net_nfc_util_free_mem(tdata.buffer);
1058                 }
1059         }
1060         else
1061         {
1062                 NFC_ERR("inner_record->payload_s.length(%d) < 1 ", inner_record->payload_s.length);
1063                 error = NET_NFC_INVALID_FORMAT;
1064         }
1065
1066         return error;
1067 }
1068
1069 net_nfc_error_e net_nfc_util_append_carrier_config_record(ndef_message_s *message, ndef_record_s *record, net_nfc_conn_handover_carrier_state_e power_status)
1070 {
1071         ndef_message_s *inner_msg = NULL;
1072         ndef_record_s *carrier_rec = NULL;
1073         data_s payload = { NULL, 0 };
1074         data_s type = { NULL, 0 };
1075         int config_ref_count = 0;
1076         net_nfc_error_e error;
1077         uint8_t buffer[256] = { 0, };
1078 #if 0
1079         int i;
1080 #endif
1081         int offset;
1082         uint8_t id;
1083
1084         if (message == NULL || record == NULL)
1085         {
1086                 return NET_NFC_NULL_PARAMETER;
1087         }
1088
1089         if ((error = net_nfc_util_create_ndef_message(&inner_msg)) != NET_NFC_OK)
1090         {
1091                 NFC_ERR("net_nfc_util_create_ndef_message failed [%d]", error);
1092
1093                 return error;
1094         }
1095
1096         if ((error = __net_nfc_get_inner_message(message, inner_msg)) == NET_NFC_OK)
1097         {
1098                 int idx = 1;
1099                 ndef_record_s *last_rec = inner_msg->records;
1100
1101                 for (; idx < inner_msg->recordCount; idx++)
1102                 {
1103                         last_rec = last_rec->next;
1104                 }
1105
1106                 if (strncmp((char *)last_rec->type_s.buffer, COLLISION_DETECT_RECORD_TYPE, (size_t)last_rec->type_s.length) == 0)
1107                 {
1108                         config_ref_count = 0;
1109                 }
1110                 else if (strncmp((char *)last_rec->type_s.buffer, ALTERNATIVE_RECORD_TYPE, (size_t)last_rec->type_s.length) == 0)
1111                 {
1112                         memcpy(buffer, last_rec->payload_s.buffer, last_rec->payload_s.length);
1113                         config_ref_count = last_rec->payload_s.buffer[1];
1114                 }
1115         }
1116         else
1117         {
1118                 /* selector record type can include empty inner message. so that case, we will continue */
1119                 if (message->records != NULL &&
1120                                 memcmp((char *)message->records->type_s.buffer, CH_SEL_RECORD_TYPE, (size_t)message->records->type_s.length) != 0)
1121                 {
1122                         NFC_ERR("ERROR [%d]", error);
1123
1124                         net_nfc_util_free_ndef_message(inner_msg);
1125
1126                         return error;
1127                 }
1128         }
1129
1130         type.buffer = (uint8_t *)ALTERNATIVE_RECORD_TYPE;
1131         type.length = strlen(ALTERNATIVE_RECORD_TYPE);
1132
1133         config_ref_count++;
1134         offset = 0;
1135         //      id = '0' + config_ref_count;
1136         id = 'b';
1137
1138         /* carrier flag */
1139         buffer[offset++] = (uint8_t)(power_status & 0x3);       /* first byte, power status */
1140
1141         /* carrier data ref. len */
1142         buffer[offset++] = config_ref_count;
1143
1144         /* carrier data ref. */
1145         offset += (config_ref_count - 1);
1146         buffer[offset++] = id;
1147
1148         /* FIXME */
1149         /* aux data ref. len */
1150         buffer[offset++] = 0;
1151 #if 0 /* FIXME */
1152         /* carrier data ref. */
1153         for (i = 0; i < 0; i++)
1154         {
1155                 buffer[offset++] = 0;
1156         }
1157 #endif
1158         payload.buffer = buffer;
1159         payload.length = offset;
1160
1161         error = net_nfc_util_create_record(NET_NFC_RECORD_WELL_KNOWN_TYPE, &type, NULL, &payload, &carrier_rec);
1162         if (error != NET_NFC_OK)
1163         {
1164                 NFC_ERR("net_nfc_util_create_record failed [%d]", error);
1165
1166                 net_nfc_util_free_ndef_message(inner_msg);
1167
1168                 return error;
1169         }
1170
1171         error = net_nfc_util_append_record(inner_msg, carrier_rec);
1172         if (error != NET_NFC_OK)
1173         {
1174                 NFC_ERR("net_nfc_util_create_record failed [%d]", error);
1175
1176                 net_nfc_util_free_record(carrier_rec);
1177                 net_nfc_util_free_ndef_message(inner_msg);
1178
1179                 return error;
1180         }
1181
1182         error = __net_nfc_replace_inner_message(message, inner_msg);
1183         net_nfc_util_free_ndef_message(inner_msg);
1184         if (error != NET_NFC_OK)
1185         {
1186                 NFC_ERR("__net_nfc_replace_inner_message failed [%d]", error);
1187
1188                 return error;
1189         }
1190
1191         /* set record id to record that will be appended to ndef message */
1192         error = net_nfc_util_set_record_id((ndef_record_s *)record, &id, sizeof(id));
1193         if (error != NET_NFC_OK)
1194         {
1195                 NFC_ERR("net_nfc_util_set_record_id failed [%d]", error);
1196
1197                 return error;
1198         }
1199
1200         error = net_nfc_util_append_record(message, (ndef_record_s *)record);
1201         if (error != NET_NFC_OK)
1202         {
1203                 NFC_ERR("net_nfc_util_append_record failed [%d]", error);
1204
1205                 return error;
1206         }
1207
1208         return NET_NFC_OK;
1209 }
1210
1211 net_nfc_error_e net_nfc_util_remove_carrier_config_record(ndef_message_s *message, ndef_record_s *record)
1212 {
1213         ndef_message_s *inner_msg = NULL;
1214         int idx = 0;
1215         int saved_idx = 0;
1216         net_nfc_error_e error;
1217         ndef_record_s *current = NULL;
1218         ndef_record_s *record_priv = (ndef_record_s *)record;
1219
1220         if (message == NULL || record == NULL)
1221         {
1222                 return NET_NFC_NULL_PARAMETER;
1223         }
1224
1225         current = message->records;
1226
1227         for (idx = 0; idx < message->recordCount; idx++)
1228         {
1229                 if (current == record)
1230                 {
1231                         break;
1232                 }
1233                 current = current->next;
1234         }
1235
1236         if (current == NULL || idx == message->recordCount)
1237         {
1238                 NFC_DBG("The reference is not found in config records");
1239
1240                 return NET_NFC_NO_DATA_FOUND;
1241         }
1242         saved_idx = idx;
1243
1244         if ((error = net_nfc_util_create_ndef_message(&inner_msg)) != NET_NFC_OK)
1245         {
1246                 NFC_DBG("net_nfc_util_create_ndef_message failed [%d]", error);
1247
1248                 return error;
1249         }
1250
1251         if ((error = __net_nfc_get_inner_message(message, inner_msg)) == NET_NFC_OK)
1252         {
1253                 current = inner_msg->records;
1254
1255                 for (idx = 0; idx < inner_msg->recordCount; idx++, current = current->next)
1256                 {
1257                         if (strncmp((char *)current->type_s.buffer, ALTERNATIVE_RECORD_TYPE, (size_t)current->type_s.length) == 0)
1258                         {
1259                                 if ((uint32_t)(current->payload_s.buffer[1]) == record_priv->id_s.length &&
1260                                                 strncmp((char *)(current->payload_s.buffer + 2), (char*)(record_priv->id_s.buffer), (size_t)current->payload_s.buffer[1]) == 0)
1261                                 {
1262                                         // comparing the instance
1263                                         break;
1264                                 }
1265                         }
1266                 }
1267
1268                 if (current == NULL || idx == message->recordCount)
1269                 {
1270                         NFC_DBG("The reference is not found in inner message");
1271
1272                         error = NET_NFC_NO_DATA_FOUND;
1273                 }
1274                 else
1275                 {
1276                         net_nfc_util_remove_record_by_index(inner_msg, idx);
1277                         __net_nfc_replace_inner_message(message, inner_msg);
1278
1279                         // remove the record only if the inner record is found.
1280                         net_nfc_util_remove_record_by_index(message, saved_idx);
1281                 }
1282         }
1283
1284         net_nfc_util_free_ndef_message(inner_msg);
1285
1286         return error;
1287 }
1288
1289 net_nfc_error_e net_nfc_util_get_carrier_config_record(ndef_message_s *message, int index, ndef_record_s** record)
1290 {
1291         ndef_message_s *inner_msg = NULL;
1292         data_s id;
1293         net_nfc_error_e error;
1294         ndef_record_s *current = NULL;
1295         int idx = 0;
1296         int current_idx = 0;
1297
1298         if (message == NULL || record == NULL)
1299         {
1300                 return NET_NFC_NULL_PARAMETER;
1301         }
1302
1303         if ((error = net_nfc_util_create_ndef_message(&inner_msg)) != NET_NFC_OK)
1304         {
1305                 NFC_DBG("net_nfc_util_create_ndef_message failed [%d]", error);
1306
1307                 return error;
1308         }
1309
1310         if ((error = __net_nfc_get_inner_message(message, inner_msg)) == NET_NFC_OK)
1311         {
1312                 current = inner_msg->records;
1313                 for (idx = 0; idx < inner_msg->recordCount; idx++)
1314                 {
1315                         if (strncmp((char*)current->type_s.buffer, ALTERNATIVE_RECORD_TYPE, (size_t)current->type_s.length) == 0)
1316                         {
1317                                 if (current_idx == index)
1318                                         break;
1319                                 current_idx++;
1320                         }
1321                         current = current->next;
1322                 }
1323
1324                 if (current == NULL || idx == message->recordCount)
1325                 {
1326                         NFC_DBG("The reference is not found in inner message");
1327
1328                         error = NET_NFC_NO_DATA_FOUND;
1329                 }
1330                 else
1331                 {
1332                         id.buffer = (current->payload_s.buffer) + 2;
1333                         id.length = current->payload_s.buffer[1];
1334
1335                         error = net_nfc_util_search_record_by_id(message, &id, record);
1336                 }
1337         }
1338
1339         net_nfc_util_free_ndef_message(inner_msg);
1340
1341         return error;
1342 }
1343
1344 net_nfc_error_e net_nfc_util_get_handover_random_number(ndef_message_s *message, unsigned short *random_number)
1345 {
1346         net_nfc_error_e error;
1347         ndef_message_s *inner_msg = NULL;
1348         ndef_record_s *cr_record = NULL;
1349
1350         if (message == NULL)
1351         {
1352                 return NET_NFC_NULL_PARAMETER;
1353         }
1354
1355         if ((error = net_nfc_util_create_ndef_message(&inner_msg)) != NET_NFC_OK)
1356         {
1357                 NFC_DBG("net_nfc_util_create_ndef_message failed [%d]", error);
1358
1359                 return error;
1360         }
1361
1362         if ((error = __net_nfc_get_inner_message(message, inner_msg)) == NET_NFC_OK)
1363         {
1364                 cr_record = inner_msg->records;
1365                 if (strncmp((char*)cr_record->type_s.buffer, COLLISION_DETECT_RECORD_TYPE, (size_t)cr_record->type_s.length) != 0
1366                                 || cr_record->payload_s.length < 2)
1367                 {
1368                         NFC_DBG("There is no Collision resolution record");
1369
1370                         error = NET_NFC_INVALID_FORMAT;
1371                 }
1372                 else
1373                 {
1374                         *random_number = ((unsigned short)cr_record->payload_s.buffer[0] << 8) | (unsigned short)(cr_record->payload_s.buffer[1]);
1375                 }
1376         }
1377
1378         net_nfc_util_free_ndef_message(inner_msg);
1379
1380         return error;
1381 }
1382
1383 net_nfc_error_e net_nfc_util_get_alternative_carrier_record_count(ndef_message_s *message, unsigned int *count)
1384 {
1385         net_nfc_error_e error;
1386         ndef_message_s *inner_msg = NULL;
1387         ndef_record_s *current = NULL;
1388         int idx;
1389
1390         if (message == NULL || count == 0)
1391         {
1392                 return NET_NFC_NULL_PARAMETER;
1393         }
1394
1395         if ((error = net_nfc_util_create_ndef_message(&inner_msg)) != NET_NFC_OK)
1396         {
1397                 NFC_DBG("net_nfc_util_create_ndef_message failed [%d]", error);
1398
1399                 return error;
1400         }
1401
1402         *count = 0;
1403
1404         if ((error = __net_nfc_get_inner_message(message, inner_msg)) == NET_NFC_OK)
1405         {
1406                 current = inner_msg->records;
1407                 for (idx = 0; idx < inner_msg->recordCount; idx++)
1408                 {
1409                         if (strncmp((char *)current->type_s.buffer, ALTERNATIVE_RECORD_TYPE, (size_t)current->type_s.length) == 0)
1410                         {
1411                                 (*count)++;
1412                         }
1413                         current = current->next;
1414                 }
1415         }
1416
1417         net_nfc_util_free_ndef_message(inner_msg);
1418
1419         return error;
1420 }
1421
1422 net_nfc_error_e net_nfc_util_get_alternative_carrier_power_status(ndef_message_s *message, int index, net_nfc_conn_handover_carrier_state_e *power_state)
1423 {
1424         net_nfc_error_e error;
1425         ndef_message_s *inner_msg = NULL;
1426         ndef_record_s *current = NULL;
1427         int idx;
1428         int idx_count = 0;
1429
1430         if (message == NULL)
1431         {
1432                 return NET_NFC_NULL_PARAMETER;
1433         }
1434
1435         if (index < 0)
1436         {
1437                 return NET_NFC_OUT_OF_BOUND;
1438         }
1439
1440         if ((error = net_nfc_util_create_ndef_message(&inner_msg)) != NET_NFC_OK)
1441         {
1442                 NFC_DBG("net_nfc_util_create_ndef_message failed [%d]", error);
1443
1444                 return error;
1445         }
1446
1447         if ((error = __net_nfc_get_inner_message(message, inner_msg)) == NET_NFC_OK)
1448         {
1449                 error = NET_NFC_OUT_OF_BOUND;
1450                 current = inner_msg->records;
1451                 for (idx = 0; idx < inner_msg->recordCount; idx++)
1452                 {
1453                         if (strncmp((char *)current->type_s.buffer, ALTERNATIVE_RECORD_TYPE, (size_t)current->type_s.length) == 0)
1454                         {
1455                                 if (idx_count == index)
1456                                 {
1457                                         *power_state = current->payload_s.buffer[0] & 0x3;
1458                                         error = NET_NFC_OK;
1459                                         break;
1460                                 }
1461                                 idx_count++;
1462                         }
1463                         current = current->next;
1464                 }
1465         }
1466
1467         net_nfc_util_free_ndef_message(inner_msg);
1468
1469         return error;
1470 }
1471
1472 net_nfc_error_e net_nfc_util_set_alternative_carrier_power_status(ndef_message_s *message, int index, net_nfc_conn_handover_carrier_state_e power_status)
1473 {
1474         net_nfc_error_e error;
1475         ndef_message_s *inner_msg = NULL;
1476
1477         if (message == NULL)
1478         {
1479                 return NET_NFC_NULL_PARAMETER;
1480         }
1481         if (index < 0)
1482         {
1483                 return NET_NFC_OUT_OF_BOUND;
1484         }
1485
1486         if ((error = net_nfc_util_create_ndef_message(&inner_msg)) != NET_NFC_OK)
1487         {
1488                 NFC_DBG("net_nfc_util_create_ndef_message failed [%d]", error);
1489
1490                 return error;
1491         }
1492
1493         if ((error = __net_nfc_get_inner_message(message, inner_msg)) == NET_NFC_OK)
1494         {
1495                 int idx;
1496                 int idx_count = 0;
1497                 ndef_record_s *current = inner_msg->records;
1498
1499                 error = NET_NFC_OUT_OF_BOUND;
1500                 for (idx = 0; idx < inner_msg->recordCount; idx++, current = current->next)
1501                 {
1502                         if (strncmp((char *)current->type_s.buffer, ALTERNATIVE_RECORD_TYPE, (size_t)current->type_s.length) == 0)
1503                         {
1504                                 if (idx_count == index)
1505                                 {
1506                                         current->payload_s.buffer[0] = (power_status & 0x3) | (current->payload_s.buffer[0] & 0xFC);
1507
1508                                         __net_nfc_replace_inner_message(message, inner_msg);
1509                                         error = NET_NFC_OK;
1510                                         break;
1511                                 }
1512                                 idx_count++;
1513                         }
1514                 }
1515         }
1516
1517         net_nfc_util_free_ndef_message(inner_msg);
1518
1519         return error;
1520 }
1521
1522 net_nfc_error_e net_nfc_util_get_alternative_carrier_type_from_record(ndef_record_s *record, net_nfc_conn_handover_carrier_type_e *type)
1523 {
1524         if(strncmp((char*)record->type_s.buffer, CH_CAR_RECORD_TYPE, (size_t)record->type_s.length) == 0)
1525         {
1526                 NFC_DBG("CH_CAR_RECORD_TYPE");
1527
1528                 char ctf = record->payload_s.buffer[0] & 0x07;
1529                 char ctype_length = record->payload_s.buffer[1];
1530                 switch(ctf)
1531                 {
1532                 case NET_NFC_RECORD_MIME_TYPE:          /* Media Type as defined in [RFC 2046] */
1533                         if (strncmp((char *)&record->payload_s.buffer[2],
1534                                                 CONN_HANDOVER_BT_CARRIER_MIME_NAME,
1535                                                 (size_t)ctype_length) == 0)
1536                         {
1537                                 *type = NET_NFC_CONN_HANDOVER_CARRIER_BT;
1538                         }
1539                         else if (strncmp((char *)&record->payload_s.buffer[2],
1540                                                 CONN_HANDOVER_WIFI_BSS_CARRIER_MIME_NAME,
1541                                                 (size_t)ctype_length) == 0)
1542                         {
1543                                 *type = NET_NFC_CONN_HANDOVER_CARRIER_WIFI_BSS;
1544                         }
1545                         else if (strncmp((char *)&record->payload_s.buffer[2],
1546                                                 CONN_HANDOVER_WIFI_IBSS_CARRIER_MIME_NAME,
1547                                                 (size_t)ctype_length) == 0)
1548                         {
1549                                 *type = NET_NFC_CONN_HANDOVER_CARRIER_WIFI_IBSS;
1550                         }
1551                         else
1552                         {
1553                                 *type = NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN;
1554                         }
1555                 break;
1556                 case NET_NFC_RECORD_WELL_KNOWN_TYPE:    /* NFC Forum Well-known Type*/
1557                 case NET_NFC_RECORD_URI:                                /* Absolute URIs as defined in [RFC 3986] */
1558                 case NET_NFC_RECORD_EXTERNAL_RTD:               /* NFC Forum external type */
1559                 default:
1560                         *type = NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN;
1561                         break;
1562                 }
1563
1564         }
1565         else
1566         {
1567                 NFC_DBG("Other record type");
1568                 if (strncmp((char *)record->type_s.buffer, CONN_HANDOVER_BT_CARRIER_MIME_NAME,
1569                         (size_t)record->type_s.length) == 0)
1570                 {
1571                         *type = NET_NFC_CONN_HANDOVER_CARRIER_BT;
1572                 }
1573                 else if (strncmp((char *)record->type_s.buffer,
1574                         CONN_HANDOVER_WIFI_BSS_CARRIER_MIME_NAME,
1575                         (size_t)record->type_s.length) == 0)
1576                 {
1577                         *type = NET_NFC_CONN_HANDOVER_CARRIER_WIFI_BSS;
1578                 }
1579                 else if (strncmp((char *)record->type_s.buffer,
1580                         CONN_HANDOVER_WIFI_IBSS_CARRIER_MIME_NAME,
1581                                         (size_t)record->type_s.length) == 0)
1582                 {
1583                         *type = NET_NFC_CONN_HANDOVER_CARRIER_WIFI_IBSS;
1584                 }
1585                 else
1586                 {
1587                         *type = NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN;
1588                 }
1589         }
1590         return NET_NFC_OK;
1591 }
1592
1593 net_nfc_error_e net_nfc_util_get_alternative_carrier_type(ndef_message_s *message,
1594         int index, net_nfc_conn_handover_carrier_type_e *type)
1595 {
1596         ndef_record_s *record = NULL;
1597         net_nfc_error_e ret;
1598
1599         ret = net_nfc_util_get_carrier_config_record(message, index, &record);
1600         if (ret != NET_NFC_OK)
1601                 return ret;
1602
1603         return net_nfc_util_get_alternative_carrier_type_from_record(record, type);
1604 }
1605
1606 net_nfc_error_e net_nfc_util_get_selector_power_status(ndef_message_s *message,
1607         net_nfc_conn_handover_carrier_state_e *power_state)
1608 {
1609         net_nfc_error_e error;
1610         ndef_message_s *inner_msg = NULL;
1611         ndef_record_s *current = NULL;
1612         int idx;
1613         net_nfc_conn_handover_carrier_state_e selector_state;
1614
1615         selector_state = NET_NFC_CONN_HANDOVER_CARRIER_INACTIVATE;
1616
1617         if (message == NULL)
1618         {
1619                 return NET_NFC_NULL_PARAMETER;
1620         }
1621
1622         if ((error = net_nfc_util_create_ndef_message(&inner_msg)) == NET_NFC_OK)
1623         {
1624                 if ((error = __net_nfc_get_inner_message(message, inner_msg)) == NET_NFC_OK)
1625                 {
1626                         if (inner_msg->recordCount > 1)
1627                         {
1628                                 current = inner_msg->records;
1629                                 for (idx = 0; idx < inner_msg->recordCount; idx++)
1630                                 {
1631                                         if (strncmp((char *)current->type_s.buffer, ALTERNATIVE_RECORD_TYPE,
1632                                                 (size_t)current->type_s.length) == 0)
1633                                         {
1634                                                 if (((current->payload_s.buffer[0] & 0x3) == NET_NFC_CONN_HANDOVER_CARRIER_ACTIVATE) || ((current->payload_s.buffer[0] & 0x3) == NET_NFC_CONN_HANDOVER_CARRIER_ACTIVATING))
1635                                                 {
1636                                                         selector_state = NET_NFC_CONN_HANDOVER_CARRIER_ACTIVATE;
1637                                                         break;
1638                                                 }
1639                                         }
1640                                         current = current->next;
1641                                 }
1642                         }
1643                         else
1644                         {
1645                                 /* always activate when ac is only one */
1646                                 selector_state = NET_NFC_CONN_HANDOVER_CARRIER_ACTIVATE;
1647                         }
1648
1649                         *power_state = selector_state;
1650                 }
1651                 else
1652                 {
1653                         NFC_ERR("_net_nfc_util_create_ndef_message failed [%d]", error);
1654                 }
1655
1656                 net_nfc_util_free_ndef_message(inner_msg);
1657         }
1658         else
1659         {
1660                 NFC_ERR("_net_nfc_util_create_ndef_message failed [%d]", error);
1661                 error = NET_NFC_ALLOC_FAIL;
1662         }
1663
1664         return error;
1665 }
1666