2 * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
4 * Licensed under the Flora License, Version 1.1 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://floralicense.org/license/
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
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"
28 typedef struct _search_index
35 void net_nfc_convert_byte_order(unsigned char *array, int size)
38 unsigned char tmp_char;
40 for (i=0;i<size/2;i++) {
42 array[i] = array[size-1-i];
43 array[size-1-i] = tmp_char;
48 static int __property_equal_to(gconstpointer key1, gconstpointer key2)
50 const net_nfc_carrier_property_s *arg1 = key1;
51 const net_nfc_carrier_property_s *arg2 = key2;
53 if (arg1->attribute < arg2->attribute)
57 else if (arg1->attribute > arg2->attribute)
67 static net_nfc_carrier_property_s *__find_property_by_attrubute(GList *list, uint16_t attribute)
70 net_nfc_carrier_property_s temp;
72 temp.attribute = attribute;
73 found = g_list_find_custom(list, &temp, __property_equal_to);
80 return (net_nfc_carrier_property_s *)found->data;
83 static void __find_nth_group(gpointer data, gpointer user_data)
85 net_nfc_carrier_property_s *info = data;
86 search_index *nth = user_data;
88 if (info == NULL || user_data == NULL)
93 if (nth->current == nth->target)
101 static void __free_all_data(gpointer data, gpointer user_data)
103 net_nfc_carrier_property_s *info = data;
110 NFC_DBG("FREE: group is found");
111 net_nfc_util_free_carrier_group(info);
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);
121 static net_nfc_error_e __net_nfc_util_create_connection_handover_collsion_resolution_record(ndef_record_s **record)
124 data_s typeName = { 0 };
125 data_s payload = { 0 };
126 uint8_t rand_buffer[2] = { 0, 0 };
130 return NET_NFC_NULL_PARAMETER;
132 state = (uint32_t)time(NULL);
133 random_num = (unsigned short)rand_r(&state);
135 typeName.buffer = (uint8_t *)COLLISION_DETECT_RECORD_TYPE;
136 typeName.length = strlen(COLLISION_DETECT_RECORD_TYPE);
138 rand_buffer[0] = (random_num & 0xff00) >> 8; // MSB
139 rand_buffer[1] = (random_num & 0x00ff); // LSB
141 payload.buffer = rand_buffer;
144 NFC_DBG("rand number = [0x%x] [0x%x] => [0x%x]", payload.buffer[0], payload.buffer[1], random_num);
146 return net_nfc_util_create_record(NET_NFC_RECORD_WELL_KNOWN_TYPE, &typeName, NULL, &payload, record);
149 static int __net_nfc_get_size_of_attribute(int attribute)
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:
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)
180 return NET_NFC_NULL_PARAMETER;
183 if (type < 0 || type >= NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN)
185 return NET_NFC_OUT_OF_BOUND;
188 _net_nfc_util_alloc_mem(*config, sizeof(net_nfc_carrier_config_s));
191 return NET_NFC_ALLOC_FAIL;
194 (*config)->type = type;
195 (*config)->length = 0;
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)
203 net_nfc_carrier_property_s *elem = NULL;
205 NFC_DBG("ADD property: [ATTRIB:0x%X, SIZE:%d]", attribute, size);
207 if (config == NULL || data == NULL)
209 return NET_NFC_NULL_PARAMETER;
212 if (__find_property_by_attrubute(config->data, attribute) != NULL)
214 return NET_NFC_ALREADY_REGISTERED;
217 _net_nfc_util_alloc_mem(elem, sizeof (net_nfc_carrier_property_s));
220 return NET_NFC_ALLOC_FAIL;
222 elem->attribute = attribute;
224 elem->is_group = false;
226 _net_nfc_util_alloc_mem(elem->data, size);
227 if (elem->data == NULL)
229 _net_nfc_util_free_mem(elem);
230 return NET_NFC_ALLOC_FAIL;
232 memcpy(elem->data, data, size);
234 config->data = g_list_append(config->data, elem);
235 config->length += size + 2 * __net_nfc_get_size_of_attribute(attribute);
237 NFC_DBG("ADD completed total length %d", config->length);
242 net_nfc_error_e net_nfc_util_remove_carrier_config_property(
243 net_nfc_carrier_config_s *config, uint16_t attribute)
245 net_nfc_carrier_property_s *elem = NULL;
249 return NET_NFC_NULL_PARAMETER;
252 elem = __find_property_by_attrubute(config->data, attribute);
255 return NET_NFC_NO_DATA_FOUND;
258 config->data = g_list_remove(config->data, elem);
259 config->length -= (elem->length + 2 * __net_nfc_get_size_of_attribute(attribute));
263 net_nfc_util_free_carrier_group(elem);
267 _net_nfc_util_free_mem(elem->data);
268 _net_nfc_util_free_mem(elem);
274 net_nfc_error_e net_nfc_util_get_carrier_config_property(
275 net_nfc_carrier_config_s *config,
280 net_nfc_carrier_property_s *elem = NULL;
282 if (config == NULL || size == NULL || data == NULL)
283 return NET_NFC_NULL_PARAMETER;
285 elem = __find_property_by_attrubute(config->data, attribute);
290 return NET_NFC_NO_DATA_FOUND;
293 *size = elem->length;
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)
305 if (config == NULL || group == NULL)
306 return NET_NFC_NULL_PARAMETER;
308 if (g_list_find(config->data, group) != NULL)
309 return NET_NFC_ALREADY_REGISTERED;
311 config->data = g_list_append(config->data, group);
312 config->length += group->length+4;
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)
320 if (config == NULL || group == NULL)
322 return NET_NFC_NULL_PARAMETER;
325 if (g_list_find(config->data, group) != NULL)
327 return NET_NFC_NO_DATA_FOUND;
330 config->length -= group->length;
331 config->data = g_list_remove(config->data, group);
333 net_nfc_util_free_carrier_group(group);
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)
343 if (config == NULL || group == NULL)
345 return NET_NFC_NULL_PARAMETER;
350 return NET_NFC_OUT_OF_BOUND;
354 result.target = index;
357 g_list_foreach(config->data, __find_nth_group, &result);
359 if (result.found == NULL)
361 return NET_NFC_NO_DATA_FOUND;
363 *group = (net_nfc_carrier_property_s *)result.found;
368 net_nfc_error_e net_nfc_util_free_carrier_config(net_nfc_carrier_config_s *config)
372 return NET_NFC_NULL_PARAMETER;
375 g_list_foreach(config->data, __free_all_data, NULL);
376 g_list_free(config->data);
378 _net_nfc_util_free_mem(config);
383 net_nfc_error_e net_nfc_util_create_carrier_config_group(
384 net_nfc_carrier_property_s **group, uint16_t attribute)
388 return NET_NFC_NULL_PARAMETER;
391 _net_nfc_util_alloc_mem(*group, sizeof(net_nfc_carrier_property_s));
394 return NET_NFC_ALLOC_FAIL;
397 (*group)->attribute = attribute;
398 (*group)->is_group = true;
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)
406 net_nfc_carrier_property_s *elem = NULL;
408 NFC_DBG("ADD group property: [ATTRIB:0x%X, SIZE:%d]", attribute, size);
410 if (group == NULL || data == NULL)
412 return NET_NFC_NULL_PARAMETER;
415 if (__find_property_by_attrubute((GList *)group->data, attribute) != NULL)
417 return NET_NFC_ALREADY_REGISTERED;
420 _net_nfc_util_alloc_mem(elem, sizeof (net_nfc_carrier_property_s));
423 return NET_NFC_ALLOC_FAIL;
425 elem->attribute = attribute;
427 elem->is_group = false;
429 _net_nfc_util_alloc_mem(elem->data, size);
430 if (elem->data == NULL)
432 _net_nfc_util_free_mem(elem);
433 return NET_NFC_ALLOC_FAIL;
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);
439 NFC_DBG("ADD group completed total length %d", group->length);
444 net_nfc_error_e net_nfc_util_get_carrier_config_group_property(
445 net_nfc_carrier_property_s *group,
450 net_nfc_carrier_property_s *elem = NULL;
452 if (group == NULL || size == NULL || data == NULL)
454 return NET_NFC_NULL_PARAMETER;
457 elem = __find_property_by_attrubute((GList*)(group->data), attribute);
462 return NET_NFC_NO_DATA_FOUND;
465 *size = elem->length;
471 net_nfc_error_e net_nfc_util_remove_carrier_config_group_property(
472 net_nfc_carrier_property_s *group, uint16_t attribute)
474 net_nfc_carrier_property_s *elem = NULL;
478 return NET_NFC_NULL_PARAMETER;
481 elem = __find_property_by_attrubute((GList*)(group->data), attribute);
484 return NET_NFC_NO_DATA_FOUND;
486 group->length -= elem->length;
487 group->data = g_list_remove((GList*)(group->data), elem);
489 _net_nfc_util_free_mem(elem->data);
490 _net_nfc_util_free_mem(elem);
496 net_nfc_error_e net_nfc_util_free_carrier_group(net_nfc_carrier_property_s *group)
500 return NET_NFC_NULL_PARAMETER;
502 g_list_foreach((GList*)(group->data), __free_all_data, NULL);
503 g_list_free((GList*)(group->data));
505 _net_nfc_util_free_mem(group);
510 static void __make_serial_wifi(gpointer data, gpointer user_data)
512 net_nfc_carrier_property_s *info = data;
513 data_s *payload = user_data;
517 if (info == NULL || user_data == NULL)
520 current = payload->buffer + payload->length;
521 inc = __net_nfc_get_size_of_attribute(info->attribute);
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);
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);
545 static void __make_serial_bt(gpointer data, gpointer user_data)
547 net_nfc_carrier_property_s *info = data;
548 data_s *payload = user_data;
552 if (info == NULL || user_data == NULL)
555 current = payload->buffer + payload->length; /* payload->length is zero */
559 NFC_DBG("[BT]Found Group. call recursive");
560 g_list_foreach((GList *)info->data, __make_serial_bt, payload);
564 if (info->attribute != NET_NFC_BT_ATTRIBUTE_ADDRESS)
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);
575 NFC_DBG("[BT]BT address is found length:%d", info->length);
576 memcpy(current, info->data, info->length);
577 payload->length += (info->length);
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)
585 data_s payload = { NULL, 0 };
586 data_s record_type = { NULL, 0 };
588 if (record == NULL || config == NULL)
590 return NET_NFC_NULL_PARAMETER;
593 _net_nfc_util_alloc_mem(payload.buffer, config->length);
594 if (payload.buffer == NULL)
596 return NET_NFC_ALLOC_FAIL;
598 payload.length = 0; /* this should be zero because this will be used as current position of data written */
600 if (config->type == NET_NFC_CONN_HANDOVER_CARRIER_WIFI_BSS)
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);
606 else if (config->type == NET_NFC_CONN_HANDOVER_CARRIER_WIFI_IBSS)
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);
612 else if (config->type == NET_NFC_CONN_HANDOVER_CARRIER_BT)
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 */
620 payload.buffer[0] = payload.length & 0xFF;
621 payload.buffer[1] = (payload.length >> 8) & 0xFF;
625 return NET_NFC_NOT_SUPPORTED;
628 NFC_DBG("payload length = %d", payload.length);
630 return net_nfc_util_create_record(NET_NFC_RECORD_MIME_TYPE, &record_type, NULL, &payload, record);
633 static net_nfc_error_e __net_nfc_get_list_from_serial_for_wifi(GList **list, uint8_t *data, uint32_t length)
635 uint8_t *current = data;
636 uint8_t *last = current + length;
638 while (current < last)
640 net_nfc_carrier_property_s *elem = NULL;
641 _net_nfc_util_alloc_mem(elem, sizeof(net_nfc_carrier_property_s));
644 return NET_NFC_ALLOC_FAIL;
646 elem->attribute = current[0]<<8|current[1];
647 elem->length = current[2]<<8|current[3];
649 if (elem->attribute == NET_NFC_WIFI_ATTRIBUTE_CREDENTIAL)
651 __net_nfc_get_list_from_serial_for_wifi(list, (current + 4), elem->length);
652 elem->is_group = true;
656 _net_nfc_util_alloc_mem(elem->data, elem->length);
657 if (elem->data == NULL)
659 _net_nfc_util_free_mem(elem);
660 return NET_NFC_ALLOC_FAIL;
662 memcpy(elem->data, (current + 4), elem->length);
663 elem->is_group = false;
665 *list = g_list_append(*list, elem);
666 current += (4 + elem->length);
672 net_nfc_error_e __net_nfc_get_list_from_serial_for_bt(GList **list, uint8_t *data, uint32_t length)
674 net_nfc_carrier_property_s *elem = NULL;
675 uint8_t *current = data;
676 uint8_t *last = NULL;
678 current += 2; /* remove oob data length two bytes length*/
681 _net_nfc_util_alloc_mem(elem, sizeof(net_nfc_carrier_property_s));
684 return NET_NFC_ALLOC_FAIL;
686 elem->attribute = (uint16_t)NET_NFC_BT_ATTRIBUTE_ADDRESS;
687 elem->length = 6; /* BT address length is always 6 */
689 _net_nfc_util_alloc_mem(elem->data, elem->length);
690 if (elem->data == NULL)
692 _net_nfc_util_free_mem(elem);
693 return NET_NFC_ALLOC_FAIL;
695 memcpy(elem->data, current, elem->length);
696 elem->is_group = false;
698 current += 6; /* BT address length is always 6 */
699 length -= 6; /* substracted by 6 (Address length)*/
700 *list = g_list_append(*list, elem);
702 last = current + length;
704 while (current < last)
706 _net_nfc_util_alloc_mem(elem, sizeof(net_nfc_carrier_property_s));
709 return NET_NFC_ALLOC_FAIL;
711 elem->length = *((uint8_t *)current) - 1;
712 elem->attribute = *((uint8_t *)(++current));
714 _net_nfc_util_alloc_mem(elem->data, elem->length);
715 if (elem->data == NULL)
717 _net_nfc_util_free_mem(elem);
718 return NET_NFC_ALLOC_FAIL;
720 memcpy(elem->data, (++current), elem->length);
721 elem->is_group = false;
723 current += elem->length;
724 *list = g_list_append(*list, elem);
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)
733 net_nfc_error_e result = NET_NFC_OK;
734 net_nfc_conn_handover_carrier_type_e type;
736 if (record == NULL || config == NULL)
738 return NET_NFC_NULL_PARAMETER;
741 if (strncmp((char *)record->type_s.buffer, CONN_HANDOVER_WIFI_BSS_CARRIER_MIME_NAME, record->type_s.length) == 0)
743 type = NET_NFC_CONN_HANDOVER_CARRIER_WIFI_BSS;
745 else if (strncmp((char *)record->type_s.buffer, CONN_HANDOVER_WIFI_IBSS_CARRIER_MIME_NAME, record->type_s.length) == 0)
747 type = NET_NFC_CONN_HANDOVER_CARRIER_WIFI_IBSS;
749 else if (strncmp((char *)record->type_s.buffer, CONN_HANDOVER_BT_CARRIER_MIME_NAME, record->type_s.length) == 0)
751 type = NET_NFC_CONN_HANDOVER_CARRIER_BT;
755 NFC_DBG("Record type is not config type");
756 return NET_NFC_INVALID_FORMAT;
759 result = net_nfc_util_create_carrier_config(config, type);
762 return NET_NFC_ALLOC_FAIL;
765 switch ((*config)->type)
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);
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);
774 case NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN :
775 result = NET_NFC_NOT_SUPPORTED;
779 if (result != NET_NFC_OK)
781 net_nfc_util_free_carrier_config((net_nfc_carrier_config_s *)*config);
787 net_nfc_error_e net_nfc_util_create_handover_request_message(ndef_message_s **message)
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 };
798 return NET_NFC_NULL_PARAMETER;
801 error = net_nfc_util_create_ndef_message(message);
802 if (error != NET_NFC_OK)
807 error = net_nfc_util_create_ndef_message(&inner_message);
808 if (error != NET_NFC_OK)
810 net_nfc_util_free_ndef_message(*message);
816 __net_nfc_util_create_connection_handover_collsion_resolution_record(&record);
817 net_nfc_util_append_record(inner_message, record);
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)
823 net_nfc_util_free_ndef_message(inner_message);
824 net_nfc_util_free_ndef_message(*message);
827 return NET_NFC_ALLOC_FAIL;
829 payload.length = size;
831 uint8_t version = CH_VERSION;
833 (payload.buffer)[0] = version;
837 error = net_nfc_util_convert_ndef_message_to_rawdata(inner_message, &payload);
838 if (error != NET_NFC_OK)
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);
848 net_nfc_util_free_ndef_message(inner_message);
852 type.buffer = (uint8_t *)CH_REQ_RECORD_TYPE;
853 type.length = strlen(CH_REQ_RECORD_TYPE);
855 net_nfc_util_create_record(NET_NFC_RECORD_WELL_KNOWN_TYPE, &type, NULL, &payload, &record);
856 net_nfc_util_append_record(*message, record);
858 _net_nfc_util_free_mem(payload.buffer);
863 net_nfc_error_e net_nfc_util_create_handover_select_message(ndef_message_s **message)
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 };
872 return NET_NFC_NULL_PARAMETER;
875 error = net_nfc_util_create_ndef_message(message);
876 if (error != NET_NFC_OK)
881 _net_nfc_util_alloc_mem(payload.buffer, 1);
882 if (payload.buffer == NULL)
884 net_nfc_util_free_ndef_message(*message);
885 return NET_NFC_ALLOC_FAIL;
887 payload.length = (uint32_t)1;
889 (payload.buffer)[0] = CH_VERSION;
891 type.buffer = (uint8_t*)CH_SEL_RECORD_TYPE;
892 type.length = strlen(CH_SEL_RECORD_TYPE);
894 net_nfc_util_create_record(NET_NFC_RECORD_WELL_KNOWN_TYPE, &type, NULL, &payload, &record);
895 net_nfc_util_append_record(*message, record);
897 _net_nfc_util_free_mem(payload.buffer);
902 net_nfc_error_e net_nfc_util_create_handover_carrier_record(ndef_record_s ** record)
904 data_s payload = {NULL,0};
906 data_s type = { NULL, 0 };
907 uint8_t *buffer_ptr = NULL;
909 _net_nfc_util_alloc_mem(payload.buffer,26);//hardcoded as of now
910 if (payload.buffer == NULL)
911 return NET_NFC_ALLOC_FAIL;
913 buffer_ptr = payload.buffer;
916 buffer_ptr[0]= NET_NFC_RECORD_MIME_TYPE;
918 //copy the MIME type length
919 buffer_ptr[1] = strlen(CONN_HANDOVER_WIFI_BSS_CARRIER_MIME_NAME);
922 memcpy(&buffer_ptr[2],CONN_HANDOVER_WIFI_BSS_CARRIER_MIME_NAME,(payload.buffer)[1]);
923 payload.buffer[25] = '\0';
926 type.buffer = (uint8_t *)CH_CAR_RECORD_TYPE;
927 type.length = strlen(CH_CAR_RECORD_TYPE);
929 net_nfc_util_create_record(NET_NFC_RECORD_WELL_KNOWN_TYPE,
933 (ndef_record_s **)record);
935 _net_nfc_util_free_mem(payload.buffer);
939 net_nfc_error_e net_nfc_util_create_handover_error_record(ndef_record_s **record, uint8_t reason, uint32_t data)
958 _net_nfc_util_alloc_mem(payload.buffer, size);
959 if (payload.buffer == NULL)
961 return NET_NFC_ALLOC_FAIL;
963 payload.length = size;
965 type.buffer = (uint8_t *)ERROR_RECORD_TYPE;
966 type.length = strlen(ERROR_RECORD_TYPE);
968 net_nfc_util_create_record(NET_NFC_RECORD_WELL_KNOWN_TYPE, &type, NULL, &payload, (ndef_record_s **)record);
970 _net_nfc_util_free_mem(payload.buffer);
975 /* inner_msg should be freed after using */
977 static net_nfc_error_e __net_nfc_get_inner_message(ndef_message_s *message, ndef_message_s *inner_msg)
979 net_nfc_error_e error;
980 ndef_record_s *inner_record = NULL;
982 if (message == NULL || inner_msg == NULL)
984 return NET_NFC_NULL_PARAMETER;
987 inner_record = message->records;
988 if (inner_record == NULL)
990 // This message is not connection handover message
991 return NET_NFC_INVALID_FORMAT;
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)
997 // This message is not connection handover message
998 return NET_NFC_INVALID_FORMAT;
1001 if (inner_record->payload_s.length > 1)
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)++;
1012 error = NET_NFC_NO_DATA_FOUND;
1018 static net_nfc_error_e __net_nfc_replace_inner_message(ndef_message_s *message, ndef_message_s *inner_msg)
1020 net_nfc_error_e error;
1021 ndef_record_s *inner_record = NULL;
1023 if (message == NULL || inner_msg == NULL)
1025 return NET_NFC_NULL_PARAMETER;
1028 inner_record = message->records;
1029 if (inner_record == NULL)
1031 // This message is not connection handover message
1032 NFC_ERR("inner_record == NULL");
1033 return NET_NFC_INVALID_FORMAT;
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)
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;
1044 if (inner_record->payload_s.length >= 1)
1046 /* There is Alternative Carrier Record or Collision Res. Rec. */
1047 data_s tdata = { NULL, 0 };
1050 inner_length = net_nfc_util_get_ndef_message_length(inner_msg);
1052 _net_nfc_util_alloc_mem(tdata.buffer, inner_length + 1);
1053 if (tdata.buffer == NULL)
1055 return NET_NFC_ALLOC_FAIL;
1059 tdata.length = inner_length;
1060 error = net_nfc_util_convert_ndef_message_to_rawdata(inner_msg, &tdata);
1061 if (error == NET_NFC_OK)
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;
1072 NFC_ERR("net_nfc_util_convert_ndef_message_to_rawdata failed [%d]", error);
1073 _net_nfc_util_free_mem(tdata.buffer);
1078 NFC_ERR("inner_record->payload_s.length(%d) < 1 ", inner_record->payload_s.length);
1079 error = NET_NFC_INVALID_FORMAT;
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)
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, };
1100 if (message == NULL || record == NULL)
1102 return NET_NFC_NULL_PARAMETER;
1105 if ((error = net_nfc_util_create_ndef_message(&inner_msg)) != NET_NFC_OK)
1107 NFC_ERR("net_nfc_util_create_ndef_message failed [%d]", error);
1112 if ((error = __net_nfc_get_inner_message(message, inner_msg)) == NET_NFC_OK)
1115 ndef_record_s *last_rec = inner_msg->records;
1117 for (; idx < inner_msg->recordCount; idx++)
1119 last_rec = last_rec->next;
1122 if (strncmp((char *)last_rec->type_s.buffer, COLLISION_DETECT_RECORD_TYPE, (size_t)last_rec->type_s.length) == 0)
1124 config_ref_count = 0;
1126 else if (strncmp((char *)last_rec->type_s.buffer, ALTERNATIVE_RECORD_TYPE, (size_t)last_rec->type_s.length) == 0)
1128 memcpy(buffer, last_rec->payload_s.buffer, last_rec->payload_s.length);
1129 config_ref_count = last_rec->payload_s.buffer[1];
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)
1138 NFC_ERR("ERROR [%d]", error);
1140 net_nfc_util_free_ndef_message(inner_msg);
1146 type.buffer = (uint8_t *)ALTERNATIVE_RECORD_TYPE;
1147 type.length = strlen(ALTERNATIVE_RECORD_TYPE);
1151 // id = '0' + config_ref_count;
1155 buffer[offset++] = (uint8_t)(power_status & 0x3); /* first byte, power status */
1157 /* carrier data ref. len */
1158 buffer[offset++] = config_ref_count;
1160 /* carrier data ref. */
1161 offset += (config_ref_count - 1);
1162 buffer[offset++] = id;
1165 /* aux data ref. len */
1166 buffer[offset++] = 0;
1168 /* carrier data ref. */
1169 for (i = 0; i < 0; i++)
1171 buffer[offset++] = 0;
1174 payload.buffer = buffer;
1175 payload.length = offset;
1177 error = net_nfc_util_create_record(NET_NFC_RECORD_WELL_KNOWN_TYPE, &type, NULL, &payload, &carrier_rec);
1178 if (error != NET_NFC_OK)
1180 NFC_ERR("net_nfc_util_create_record failed [%d]", error);
1182 net_nfc_util_free_ndef_message(inner_msg);
1187 error = net_nfc_util_append_record(inner_msg, carrier_rec);
1188 if (error != NET_NFC_OK)
1190 NFC_ERR("net_nfc_util_create_record failed [%d]", error);
1192 net_nfc_util_free_record(carrier_rec);
1193 net_nfc_util_free_ndef_message(inner_msg);
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)
1202 NFC_ERR("__net_nfc_replace_inner_message failed [%d]", error);
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)
1211 NFC_ERR("net_nfc_util_set_record_id failed [%d]", error);
1216 error = net_nfc_util_append_record(message, (ndef_record_s *)record);
1217 if (error != NET_NFC_OK)
1219 NFC_ERR("net_nfc_util_append_record failed [%d]", error);
1227 net_nfc_error_e net_nfc_util_remove_carrier_config_record(ndef_message_s *message, ndef_record_s *record)
1229 ndef_message_s *inner_msg = NULL;
1232 net_nfc_error_e error;
1233 ndef_record_s *current = NULL;
1234 ndef_record_s *record_priv = (ndef_record_s *)record;
1236 if (message == NULL || record == NULL)
1238 return NET_NFC_NULL_PARAMETER;
1241 current = message->records;
1243 for (idx = 0; idx < message->recordCount; idx++)
1245 if (current == record)
1249 current = current->next;
1252 if (current == NULL || idx == message->recordCount)
1254 NFC_DBG("The reference is not found in config records");
1256 return NET_NFC_NO_DATA_FOUND;
1260 if ((error = net_nfc_util_create_ndef_message(&inner_msg)) != NET_NFC_OK)
1262 NFC_DBG("net_nfc_util_create_ndef_message failed [%d]", error);
1267 if ((error = __net_nfc_get_inner_message(message, inner_msg)) == NET_NFC_OK)
1269 current = inner_msg->records;
1271 for (idx = 0; idx < inner_msg->recordCount; idx++, current = current->next)
1273 if (strncmp((char *)current->type_s.buffer, ALTERNATIVE_RECORD_TYPE, (size_t)current->type_s.length) == 0)
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)
1278 // comparing the instance
1284 if (current == NULL || idx == message->recordCount)
1286 NFC_DBG("The reference is not found in inner message");
1288 error = NET_NFC_NO_DATA_FOUND;
1292 net_nfc_util_remove_record_by_index(inner_msg, idx);
1293 __net_nfc_replace_inner_message(message, inner_msg);
1295 // remove the record only if the inner record is found.
1296 net_nfc_util_remove_record_by_index(message, saved_idx);
1300 net_nfc_util_free_ndef_message(inner_msg);
1305 net_nfc_error_e net_nfc_util_get_carrier_config_record(ndef_message_s *message, int index, ndef_record_s** record)
1307 ndef_message_s *inner_msg = NULL;
1309 net_nfc_error_e error;
1310 ndef_record_s *current = NULL;
1312 int current_idx = 0;
1314 if (message == NULL || record == NULL)
1316 return NET_NFC_NULL_PARAMETER;
1319 if ((error = net_nfc_util_create_ndef_message(&inner_msg)) != NET_NFC_OK)
1321 NFC_DBG("net_nfc_util_create_ndef_message failed [%d]", error);
1326 if ((error = __net_nfc_get_inner_message(message, inner_msg)) == NET_NFC_OK)
1328 current = inner_msg->records;
1329 for (idx = 0; idx < inner_msg->recordCount; idx++)
1331 if (strncmp((char*)current->type_s.buffer, ALTERNATIVE_RECORD_TYPE, (size_t)current->type_s.length) == 0)
1333 if (current_idx == index)
1337 current = current->next;
1340 if (current == NULL || idx == message->recordCount)
1342 NFC_DBG("The reference is not found in inner message");
1344 error = NET_NFC_NO_DATA_FOUND;
1348 id.buffer = (current->payload_s.buffer) + 2;
1349 id.length = current->payload_s.buffer[1];
1351 error = net_nfc_util_search_record_by_id(message, &id, record);
1355 net_nfc_util_free_ndef_message(inner_msg);
1360 net_nfc_error_e net_nfc_util_get_handover_random_number(ndef_message_s *message, unsigned short *random_number)
1362 net_nfc_error_e error;
1363 ndef_message_s *inner_msg = NULL;
1364 ndef_record_s *cr_record = NULL;
1366 if (message == NULL)
1368 return NET_NFC_NULL_PARAMETER;
1371 if ((error = net_nfc_util_create_ndef_message(&inner_msg)) != NET_NFC_OK)
1373 NFC_DBG("net_nfc_util_create_ndef_message failed [%d]", error);
1378 if ((error = __net_nfc_get_inner_message(message, inner_msg)) == NET_NFC_OK)
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)
1384 NFC_DBG("There is no Collision resolution record");
1386 error = NET_NFC_INVALID_FORMAT;
1390 *random_number = ((unsigned short)cr_record->payload_s.buffer[0] << 8) | (unsigned short)(cr_record->payload_s.buffer[1]);
1394 net_nfc_util_free_ndef_message(inner_msg);
1399 net_nfc_error_e net_nfc_util_get_alternative_carrier_record_count(ndef_message_s *message, unsigned int *count)
1401 net_nfc_error_e error;
1402 ndef_message_s *inner_msg = NULL;
1403 ndef_record_s *current = NULL;
1406 if (message == NULL || count == 0)
1408 return NET_NFC_NULL_PARAMETER;
1411 if ((error = net_nfc_util_create_ndef_message(&inner_msg)) != NET_NFC_OK)
1413 NFC_DBG("net_nfc_util_create_ndef_message failed [%d]", error);
1420 if ((error = __net_nfc_get_inner_message(message, inner_msg)) == NET_NFC_OK)
1422 current = inner_msg->records;
1423 for (idx = 0; idx < inner_msg->recordCount; idx++)
1425 if (strncmp((char *)current->type_s.buffer, ALTERNATIVE_RECORD_TYPE, (size_t)current->type_s.length) == 0)
1429 current = current->next;
1433 net_nfc_util_free_ndef_message(inner_msg);
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)
1440 net_nfc_error_e error;
1441 ndef_message_s *inner_msg = NULL;
1442 ndef_record_s *current = NULL;
1446 if (message == NULL)
1448 return NET_NFC_NULL_PARAMETER;
1453 return NET_NFC_OUT_OF_BOUND;
1456 if ((error = net_nfc_util_create_ndef_message(&inner_msg)) != NET_NFC_OK)
1458 NFC_DBG("net_nfc_util_create_ndef_message failed [%d]", error);
1463 if ((error = __net_nfc_get_inner_message(message, inner_msg)) == NET_NFC_OK)
1465 error = NET_NFC_OUT_OF_BOUND;
1466 current = inner_msg->records;
1467 for (idx = 0; idx < inner_msg->recordCount; idx++)
1469 if (strncmp((char *)current->type_s.buffer, ALTERNATIVE_RECORD_TYPE, (size_t)current->type_s.length) == 0)
1471 if (idx_count == index)
1473 *power_state = current->payload_s.buffer[0] & 0x3;
1479 current = current->next;
1483 net_nfc_util_free_ndef_message(inner_msg);
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)
1490 net_nfc_error_e error;
1491 ndef_message_s *inner_msg = NULL;
1493 if (message == NULL)
1495 return NET_NFC_NULL_PARAMETER;
1499 return NET_NFC_OUT_OF_BOUND;
1502 if ((error = net_nfc_util_create_ndef_message(&inner_msg)) != NET_NFC_OK)
1504 NFC_DBG("net_nfc_util_create_ndef_message failed [%d]", error);
1509 if ((error = __net_nfc_get_inner_message(message, inner_msg)) == NET_NFC_OK)
1513 ndef_record_s *current = inner_msg->records;
1515 error = NET_NFC_OUT_OF_BOUND;
1516 for (idx = 0; idx < inner_msg->recordCount; idx++, current = current->next)
1518 if (strncmp((char *)current->type_s.buffer, ALTERNATIVE_RECORD_TYPE, (size_t)current->type_s.length) == 0)
1520 if (idx_count == index)
1522 current->payload_s.buffer[0] = (power_status & 0x3) | (current->payload_s.buffer[0] & 0xFC);
1524 __net_nfc_replace_inner_message(message, inner_msg);
1533 net_nfc_util_free_ndef_message(inner_msg);
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)
1540 if(strncmp((char*)record->type_s.buffer, CH_CAR_RECORD_TYPE, (size_t)record->type_s.length) == 0)
1542 NFC_DBG("CH_CAR_RECORD_TYPE");
1544 char ctf = record->payload_s.buffer[0] & 0x07;
1545 char ctype_length = record->payload_s.buffer[1];
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)
1553 *type = NET_NFC_CONN_HANDOVER_CARRIER_BT;
1555 else if (strncmp((char *)&record->payload_s.buffer[2],
1556 CONN_HANDOVER_WIFI_BSS_CARRIER_MIME_NAME,
1557 (size_t)ctype_length) == 0)
1559 *type = NET_NFC_CONN_HANDOVER_CARRIER_WIFI_BSS;
1561 else if (strncmp((char *)&record->payload_s.buffer[2],
1562 CONN_HANDOVER_WIFI_IBSS_CARRIER_MIME_NAME,
1563 (size_t)ctype_length) == 0)
1565 *type = NET_NFC_CONN_HANDOVER_CARRIER_WIFI_IBSS;
1569 *type = NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN;
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 */
1576 *type = NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN;
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)
1587 *type = NET_NFC_CONN_HANDOVER_CARRIER_BT;
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)
1593 *type = NET_NFC_CONN_HANDOVER_CARRIER_WIFI_BSS;
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)
1599 *type = NET_NFC_CONN_HANDOVER_CARRIER_WIFI_IBSS;
1603 *type = NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN;
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)
1612 ndef_record_s *record = NULL;
1613 net_nfc_error_e ret;
1615 ret = net_nfc_util_get_carrier_config_record(message, index, &record);
1616 if (ret != NET_NFC_OK)
1619 return net_nfc_util_get_alternative_carrier_type_from_record(record, type);
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)
1625 net_nfc_error_e error;
1626 ndef_message_s *inner_msg = NULL;
1627 ndef_record_s *current = NULL;
1629 net_nfc_conn_handover_carrier_state_e selector_state;
1631 selector_state = NET_NFC_CONN_HANDOVER_CARRIER_INACTIVATE;
1633 if (message == NULL)
1635 return NET_NFC_NULL_PARAMETER;
1638 if ((error = net_nfc_util_create_ndef_message(&inner_msg)) == NET_NFC_OK)
1640 if ((error = __net_nfc_get_inner_message(message, inner_msg)) == NET_NFC_OK)
1642 if (inner_msg->recordCount > 1)
1644 current = inner_msg->records;
1645 for (idx = 0; idx < inner_msg->recordCount; idx++)
1647 if (strncmp((char *)current->type_s.buffer, ALTERNATIVE_RECORD_TYPE,
1648 (size_t)current->type_s.length) == 0)
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))
1652 selector_state = NET_NFC_CONN_HANDOVER_CARRIER_ACTIVATE;
1656 current = current->next;
1661 /* always activate when ac is only one */
1662 selector_state = NET_NFC_CONN_HANDOVER_CARRIER_ACTIVATE;
1665 *power_state = selector_state;
1669 NFC_ERR("_net_nfc_util_create_ndef_message failed [%d]", error);
1672 net_nfc_util_free_ndef_message(inner_msg);
1676 NFC_ERR("_net_nfc_util_create_ndef_message failed [%d]", error);
1677 error = NET_NFC_ALLOC_FAIL;