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