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 net_nfc_carrier_property_s *arg1 = (net_nfc_carrier_property_s *)key1;
51 net_nfc_carrier_property_s *arg2 = (net_nfc_carrier_property_s *)key2;
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 = (net_nfc_carrier_property_s *)data;
86 search_index *nth = (search_index *)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 = (net_nfc_carrier_property_s *)data;
110 NFC_DBG("FREE: group is found");
111 net_nfc_util_free_carrier_group((net_nfc_carrier_property_s *)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(net_nfc_carrier_config_s *config, uint16_t attribute, uint16_t size, uint8_t * data)
202 net_nfc_carrier_property_s *elem = NULL;
204 NFC_DBG("ADD property: [ATTRIB:0x%X, SIZE:%d]", attribute, size);
206 if (config == NULL || data == NULL)
208 return NET_NFC_NULL_PARAMETER;
211 if (__find_property_by_attrubute(config->data, attribute) != NULL)
213 return NET_NFC_ALREADY_REGISTERED;
216 _net_nfc_util_alloc_mem(elem, sizeof (net_nfc_carrier_property_s));
219 return NET_NFC_ALLOC_FAIL;
221 elem->attribute = attribute;
223 elem->is_group = false;
225 _net_nfc_util_alloc_mem(elem->data, size);
226 if (elem->data == NULL)
228 _net_nfc_util_free_mem(elem);
229 return NET_NFC_ALLOC_FAIL;
231 memcpy(elem->data, data, size);
233 config->data = g_list_append(config->data, elem);
234 config->length += size + 2 * __net_nfc_get_size_of_attribute(attribute);
236 NFC_DBG("ADD completed total length %d", config->length);
241 net_nfc_error_e net_nfc_util_remove_carrier_config_property(net_nfc_carrier_config_s *config, uint16_t attribute)
243 net_nfc_carrier_property_s *elem = NULL;
247 return NET_NFC_NULL_PARAMETER;
250 elem = __find_property_by_attrubute(config->data, attribute);
253 return NET_NFC_NO_DATA_FOUND;
256 config->data = g_list_remove(config->data, elem);
257 config->length -= (elem->length + 2 * __net_nfc_get_size_of_attribute(attribute));
261 net_nfc_util_free_carrier_group((net_nfc_carrier_property_s *)elem);
265 _net_nfc_util_free_mem(elem->data);
266 _net_nfc_util_free_mem(elem);
272 net_nfc_error_e net_nfc_util_get_carrier_config_property(
273 net_nfc_carrier_config_s *config, uint16_t attribute, uint16_t *size, uint8_t **data)
275 net_nfc_carrier_property_s *elem = NULL;
277 if (config == NULL || size == NULL || data == NULL)
278 return NET_NFC_NULL_PARAMETER;
280 elem = __find_property_by_attrubute(config->data, attribute);
285 return NET_NFC_NO_DATA_FOUND;
288 *size = elem->length;
297 net_nfc_error_e net_nfc_util_append_carrier_config_group(
298 net_nfc_carrier_config_s *config, net_nfc_carrier_property_s *group)
300 if (config == NULL || group == NULL)
301 return NET_NFC_NULL_PARAMETER;
303 if (g_list_find(config->data, group) != NULL)
304 return NET_NFC_ALREADY_REGISTERED;
306 config->data = g_list_append(config->data, group);
307 config->length += group->length+4;
312 net_nfc_error_e net_nfc_util_remove_carrier_config_group(net_nfc_carrier_config_s *config, net_nfc_carrier_property_s *group)
314 if (config == NULL || group == NULL)
316 return NET_NFC_NULL_PARAMETER;
319 if (g_list_find(config->data, group) != NULL)
321 return NET_NFC_NO_DATA_FOUND;
324 config->length -= group->length;
325 config->data = g_list_remove(config->data, group);
327 net_nfc_util_free_carrier_group((net_nfc_carrier_property_s *)group);
332 net_nfc_error_e net_nfc_util_get_carrier_config_group(net_nfc_carrier_config_s *config, int index, net_nfc_carrier_property_s **group)
336 if (config == NULL || group == NULL)
338 return NET_NFC_NULL_PARAMETER;
343 return NET_NFC_OUT_OF_BOUND;
347 result.target = index;
350 g_list_foreach(config->data, __find_nth_group, &result);
352 if (result.found == NULL)
354 return NET_NFC_NO_DATA_FOUND;
356 *group = (net_nfc_carrier_property_s *)result.found;
361 net_nfc_error_e net_nfc_util_free_carrier_config(net_nfc_carrier_config_s *config)
365 return NET_NFC_NULL_PARAMETER;
368 g_list_foreach(config->data, __free_all_data, NULL);
369 g_list_free(config->data);
371 _net_nfc_util_free_mem(config);
376 net_nfc_error_e net_nfc_util_create_carrier_config_group(net_nfc_carrier_property_s **group, uint16_t attribute)
380 return NET_NFC_NULL_PARAMETER;
383 _net_nfc_util_alloc_mem(*group, sizeof(net_nfc_carrier_property_s));
386 return NET_NFC_ALLOC_FAIL;
389 (*group)->attribute = attribute;
390 (*group)->is_group = true;
395 net_nfc_error_e net_nfc_util_add_carrier_config_group_property(net_nfc_carrier_property_s *group, uint16_t attribute, uint16_t size, uint8_t *data)
397 net_nfc_carrier_property_s *elem = NULL;
399 NFC_DBG("ADD group property: [ATTRIB:0x%X, SIZE:%d]", attribute, size);
401 if (group == NULL || data == NULL)
403 return NET_NFC_NULL_PARAMETER;
406 if (__find_property_by_attrubute((GList *)group->data, attribute) != NULL)
408 return NET_NFC_ALREADY_REGISTERED;
411 _net_nfc_util_alloc_mem(elem, sizeof (net_nfc_carrier_property_s));
414 return NET_NFC_ALLOC_FAIL;
416 elem->attribute = attribute;
418 elem->is_group = false;
420 _net_nfc_util_alloc_mem(elem->data, size);
421 if (elem->data == NULL)
423 _net_nfc_util_free_mem(elem);
424 return NET_NFC_ALLOC_FAIL;
426 memcpy(elem->data, data, size);
427 group->length += size + 2 * __net_nfc_get_size_of_attribute(attribute);
428 group->data = g_list_append((GList *)(group->data), elem);
430 NFC_DBG("ADD group completed total length %d", group->length);
435 net_nfc_error_e net_nfc_util_get_carrier_config_group_property(net_nfc_carrier_property_s *group, uint16_t attribute, uint16_t *size, uint8_t ** data)
437 net_nfc_carrier_property_s *elem = NULL;
439 if (group == NULL || size == NULL || data == NULL)
441 return NET_NFC_NULL_PARAMETER;
444 elem = __find_property_by_attrubute((GList*)(group->data), attribute);
449 return NET_NFC_NO_DATA_FOUND;
452 *size = elem->length;
458 net_nfc_error_e net_nfc_util_remove_carrier_config_group_property(net_nfc_carrier_property_s *group, uint16_t attribute)
460 net_nfc_carrier_property_s *elem = NULL;
464 return NET_NFC_NULL_PARAMETER;
467 elem = __find_property_by_attrubute((GList*)(group->data), attribute);
470 return NET_NFC_NO_DATA_FOUND;
472 group->length -= elem->length;
473 group->data = g_list_remove((GList*)(group->data), elem);
475 _net_nfc_util_free_mem(elem->data);
476 _net_nfc_util_free_mem(elem);
482 net_nfc_error_e net_nfc_util_free_carrier_group(net_nfc_carrier_property_s *group)
486 return NET_NFC_NULL_PARAMETER;
488 g_list_foreach((GList*)(group->data), __free_all_data, NULL);
489 g_list_free((GList*)(group->data));
491 _net_nfc_util_free_mem(group);
496 static void __make_serial_wifi(gpointer data, gpointer user_data)
498 net_nfc_carrier_property_s *info = (net_nfc_carrier_property_s *)data;
499 data_s *payload = user_data;
503 if (info == NULL || user_data == NULL)
506 current = payload->buffer + payload->length;
507 inc = __net_nfc_get_size_of_attribute(info->attribute);
511 NFC_DBG("[WIFI]Found Group make recursive");
512 *(uint16_t *)current = info->attribute;
513 net_nfc_convert_byte_order(current,2);
514 *(uint16_t *)(current + inc) = info->length;
515 net_nfc_convert_byte_order((current + inc),2);
516 payload->length += (inc + inc);
517 g_list_foreach((GList *)info->data, __make_serial_wifi, payload);
521 NFC_DBG("[WIFI]Element is found attrib:0x%X length:%d current:%d", info->attribute, info->length, payload->length);
522 *(uint16_t *)current = info->attribute;
523 net_nfc_convert_byte_order(current,2);
524 *(uint16_t *)(current + inc) = info->length;
525 net_nfc_convert_byte_order((current + inc),2);
526 memcpy(current + inc + inc, info->data, info->length);
527 payload->length += (inc + inc + info->length);
531 static void __make_serial_bt(gpointer data, gpointer user_data)
533 net_nfc_carrier_property_s *info = (net_nfc_carrier_property_s *)data;
534 data_s *payload = user_data;
538 if (info == NULL || user_data == NULL)
541 current = payload->buffer + payload->length; /* payload->length is zero */
545 NFC_DBG("[BT]Found Group. call recursive");
546 g_list_foreach((GList *)info->data, __make_serial_bt, payload);
550 if (info->attribute != NET_NFC_BT_ATTRIBUTE_ADDRESS)
552 NFC_DBG("[BT]Element is found attrib:0x%X length:%d current:%d", info->attribute, info->length, payload->length);
553 inc = __net_nfc_get_size_of_attribute(info->attribute);
554 *current = info->length + 1;
555 *(current + inc) = info->attribute;
556 memcpy(current + inc + inc, info->data, info->length);
557 payload->length += (inc + inc + info->length);
561 NFC_DBG("[BT]BT address is found length:%d", info->length);
562 memcpy(current, info->data, info->length);
563 payload->length += (info->length);
568 net_nfc_error_e net_nfc_util_create_ndef_record_with_carrier_config(ndef_record_s **record, net_nfc_carrier_config_s *config)
570 data_s payload = { NULL, 0 };
571 data_s record_type = { NULL, 0 };
573 if (record == NULL || config == NULL)
575 return NET_NFC_NULL_PARAMETER;
578 _net_nfc_util_alloc_mem(payload.buffer, config->length);
579 if (payload.buffer == NULL)
581 return NET_NFC_ALLOC_FAIL;
583 payload.length = 0; /* this should be zero because this will be used as current position of data written */
585 if (config->type == NET_NFC_CONN_HANDOVER_CARRIER_WIFI_BSS)
587 record_type.buffer = (uint8_t *)CONN_HANDOVER_WIFI_BSS_CARRIER_MIME_NAME;
588 record_type.length = strlen(CONN_HANDOVER_WIFI_BSS_CARRIER_MIME_NAME);
589 g_list_foreach(config->data, __make_serial_wifi, &payload);
591 else if (config->type == NET_NFC_CONN_HANDOVER_CARRIER_WIFI_IBSS)
593 record_type.buffer = (uint8_t *)CONN_HANDOVER_WIFI_IBSS_CARRIER_MIME_NAME;
594 record_type.length = strlen(CONN_HANDOVER_WIFI_IBSS_CARRIER_MIME_NAME);
595 g_list_foreach(config->data, __make_serial_wifi, &payload);
597 else if (config->type == NET_NFC_CONN_HANDOVER_CARRIER_BT)
599 record_type.buffer = (uint8_t *)CONN_HANDOVER_BT_CARRIER_MIME_NAME;
600 record_type.length = strlen(CONN_HANDOVER_BT_CARRIER_MIME_NAME);
601 payload.buffer += 2; /* OOB total length */
602 g_list_foreach(config->data, __make_serial_bt, &payload);
603 payload.buffer -= 2; /* return to original */
605 payload.buffer[0] = payload.length & 0xFF;
606 payload.buffer[1] = (payload.length >> 8) & 0xFF;
610 return NET_NFC_NOT_SUPPORTED;
613 NFC_DBG("payload length = %d", payload.length);
615 return net_nfc_util_create_record(NET_NFC_RECORD_MIME_TYPE, &record_type, NULL, &payload, record);
618 static net_nfc_error_e __net_nfc_get_list_from_serial_for_wifi(GList **list, uint8_t *data, uint32_t length)
620 uint8_t *current = data;
621 uint8_t *last = current + length;
623 while (current < last)
625 net_nfc_carrier_property_s *elem = NULL;
626 _net_nfc_util_alloc_mem(elem, sizeof(net_nfc_carrier_property_s));
629 return NET_NFC_ALLOC_FAIL;
631 elem->attribute = current[0]<<8|current[1];
632 elem->length = current[2]<<8|current[3];
634 if (elem->attribute == NET_NFC_WIFI_ATTRIBUTE_CREDENTIAL)
636 __net_nfc_get_list_from_serial_for_wifi(list, (current + 4), elem->length);
637 elem->is_group = true;
641 _net_nfc_util_alloc_mem(elem->data, elem->length);
642 if (elem->data == NULL)
644 _net_nfc_util_free_mem(elem);
645 return NET_NFC_ALLOC_FAIL;
647 memcpy(elem->data, (current + 4), elem->length);
648 elem->is_group = false;
650 *list = g_list_append(*list, elem);
651 current += (4 + elem->length);
657 net_nfc_error_e __net_nfc_get_list_from_serial_for_bt(GList **list, uint8_t *data, uint32_t length)
659 net_nfc_carrier_property_s *elem = NULL;
660 uint8_t *current = data;
661 uint8_t *last = NULL;
663 current += 2; /* remove oob data length two bytes length*/
666 _net_nfc_util_alloc_mem(elem, sizeof(net_nfc_carrier_property_s));
669 return NET_NFC_ALLOC_FAIL;
671 elem->attribute = (uint16_t)NET_NFC_BT_ATTRIBUTE_ADDRESS;
672 elem->length = 6; /* BT address length is always 6 */
674 _net_nfc_util_alloc_mem(elem->data, elem->length);
675 if (elem->data == NULL)
677 _net_nfc_util_free_mem(elem);
678 return NET_NFC_ALLOC_FAIL;
680 memcpy(elem->data, current, elem->length);
681 elem->is_group = false;
683 current += 6; /* BT address length is always 6 */
684 length -= 6; /* substracted by 6 (Address length)*/
685 *list = g_list_append(*list, elem);
687 last = current + length;
689 while (current < last)
691 _net_nfc_util_alloc_mem(elem, sizeof(net_nfc_carrier_property_s));
694 return NET_NFC_ALLOC_FAIL;
696 elem->length = *((uint8_t *)current) - 1;
697 elem->attribute = *((uint8_t *)(++current));
699 _net_nfc_util_alloc_mem(elem->data, elem->length);
700 if (elem->data == NULL)
702 _net_nfc_util_free_mem(elem);
703 return NET_NFC_ALLOC_FAIL;
705 memcpy(elem->data, (++current), elem->length);
706 elem->is_group = false;
708 current += elem->length;
709 *list = g_list_append(*list, elem);
715 net_nfc_error_e net_nfc_util_create_carrier_config_from_config_record(net_nfc_carrier_config_s **config, ndef_record_s *record)
717 net_nfc_error_e result = NET_NFC_OK;
718 net_nfc_conn_handover_carrier_type_e type;
720 if (record == NULL || config == NULL)
722 return NET_NFC_NULL_PARAMETER;
725 if (strncmp((char *)record->type_s.buffer, CONN_HANDOVER_WIFI_BSS_CARRIER_MIME_NAME, record->type_s.length) == 0)
727 type = NET_NFC_CONN_HANDOVER_CARRIER_WIFI_BSS;
729 else if (strncmp((char *)record->type_s.buffer, CONN_HANDOVER_WIFI_IBSS_CARRIER_MIME_NAME, record->type_s.length) == 0)
731 type = NET_NFC_CONN_HANDOVER_CARRIER_WIFI_IBSS;
733 else if (strncmp((char *)record->type_s.buffer, CONN_HANDOVER_BT_CARRIER_MIME_NAME, record->type_s.length) == 0)
735 type = NET_NFC_CONN_HANDOVER_CARRIER_BT;
739 NFC_DBG("Record type is not config type");
740 return NET_NFC_INVALID_FORMAT;
743 result = net_nfc_util_create_carrier_config(config, type);
746 return NET_NFC_ALLOC_FAIL;
749 switch ((*config)->type)
751 case NET_NFC_CONN_HANDOVER_CARRIER_WIFI_BSS :
752 case NET_NFC_CONN_HANDOVER_CARRIER_WIFI_IBSS :
753 result = __net_nfc_get_list_from_serial_for_wifi((GList **)&((*config)->data), record->payload_s.buffer, record->payload_s.length);
755 case NET_NFC_CONN_HANDOVER_CARRIER_BT :
756 result = __net_nfc_get_list_from_serial_for_bt((GList **)&((*config)->data), record->payload_s.buffer, record->payload_s.length);
758 case NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN :
759 result = NET_NFC_NOT_SUPPORTED;
763 if (result != NET_NFC_OK)
765 net_nfc_util_free_carrier_config((net_nfc_carrier_config_s *)*config);
771 net_nfc_error_e net_nfc_util_create_handover_request_message(ndef_message_s **message)
773 ndef_message_s *inner_message = NULL;
774 net_nfc_error_e error;
775 ndef_record_s *record = NULL;
776 data_s type = { NULL, 0 };
777 data_s payload = { NULL, 0 };
782 return NET_NFC_NULL_PARAMETER;
785 error = net_nfc_util_create_ndef_message(message);
786 if (error != NET_NFC_OK)
791 error = net_nfc_util_create_ndef_message(&inner_message);
792 if (error != NET_NFC_OK)
794 net_nfc_util_free_ndef_message(*message);
800 __net_nfc_util_create_connection_handover_collsion_resolution_record(&record);
801 net_nfc_util_append_record(inner_message, record);
803 size = net_nfc_util_get_ndef_message_length(inner_message) + 1;
804 _net_nfc_util_alloc_mem(payload.buffer, size);
805 if (payload.buffer == NULL)
807 net_nfc_util_free_ndef_message(inner_message);
808 net_nfc_util_free_ndef_message(*message);
811 return NET_NFC_ALLOC_FAIL;
813 payload.length = size;
815 uint8_t version = CH_VERSION;
817 (payload.buffer)[0] = version;
821 error = net_nfc_util_convert_ndef_message_to_rawdata(inner_message, &payload);
822 if (error != NET_NFC_OK)
824 _net_nfc_util_free_mem(payload.buffer);
825 net_nfc_util_free_ndef_message(inner_message);
826 net_nfc_util_free_ndef_message(*message);
832 net_nfc_util_free_ndef_message(inner_message);
836 type.buffer = (uint8_t *)CH_REQ_RECORD_TYPE;
837 type.length = strlen(CH_REQ_RECORD_TYPE);
839 net_nfc_util_create_record(NET_NFC_RECORD_WELL_KNOWN_TYPE, &type, NULL, &payload, &record);
840 net_nfc_util_append_record(*message, record);
842 _net_nfc_util_free_mem(payload.buffer);
847 net_nfc_error_e net_nfc_util_create_handover_select_message(ndef_message_s **message)
849 net_nfc_error_e error = NET_NFC_OK;
850 ndef_record_s *record = NULL;
851 data_s type = { NULL, 0 };
852 data_s payload = { NULL, 0 };
856 return NET_NFC_NULL_PARAMETER;
859 error = net_nfc_util_create_ndef_message(message);
860 if (error != NET_NFC_OK)
865 _net_nfc_util_alloc_mem(payload.buffer, 1);
866 if (payload.buffer == NULL)
868 net_nfc_util_free_ndef_message(*message);
869 return NET_NFC_ALLOC_FAIL;
871 payload.length = (uint32_t)1;
873 (payload.buffer)[0] = CH_VERSION;
875 type.buffer = (uint8_t*)CH_SEL_RECORD_TYPE;
876 type.length = strlen(CH_SEL_RECORD_TYPE);
878 net_nfc_util_create_record(NET_NFC_RECORD_WELL_KNOWN_TYPE, &type, NULL, &payload, &record);
879 net_nfc_util_append_record(*message, record);
881 _net_nfc_util_free_mem(payload.buffer);
886 net_nfc_error_e net_nfc_util_create_handover_carrier_record(ndef_record_s ** record)
888 data_s payload = {NULL,0};
890 data_s type = { NULL, 0 };
891 uint8_t *buffer_ptr = NULL;
893 _net_nfc_util_alloc_mem(payload.buffer,26);//hardcoded as of now
894 if (payload.buffer == NULL)
895 return NET_NFC_ALLOC_FAIL;
897 buffer_ptr = payload.buffer;
900 buffer_ptr[0]= NET_NFC_RECORD_MIME_TYPE;
902 //copy the MIME type length
903 buffer_ptr[1] = strlen(CONN_HANDOVER_WIFI_BSS_CARRIER_MIME_NAME);
906 memcpy(&buffer_ptr[2],CONN_HANDOVER_WIFI_BSS_CARRIER_MIME_NAME,(payload.buffer)[1]);
907 payload.buffer[25] = '\0';
910 type.buffer = (uint8_t *)CH_CAR_RECORD_TYPE;
911 type.length = strlen(CH_CAR_RECORD_TYPE);
913 net_nfc_util_create_record(NET_NFC_RECORD_WELL_KNOWN_TYPE,
917 (ndef_record_s **)record);
919 _net_nfc_util_free_mem(payload.buffer);
923 net_nfc_error_e net_nfc_util_create_handover_error_record(ndef_record_s **record, uint8_t reason, uint32_t data)
942 _net_nfc_util_alloc_mem(payload.buffer, size);
943 if (payload.buffer == NULL)
945 return NET_NFC_ALLOC_FAIL;
947 payload.length = size;
949 type.buffer = (uint8_t *)ERROR_RECORD_TYPE;
950 type.length = strlen(ERROR_RECORD_TYPE);
952 net_nfc_util_create_record(NET_NFC_RECORD_WELL_KNOWN_TYPE, &type, NULL, &payload, (ndef_record_s **)record);
954 _net_nfc_util_free_mem(payload.buffer);
959 /* inner_msg should be freed after using */
961 static net_nfc_error_e __net_nfc_get_inner_message(ndef_message_s *message, ndef_message_s *inner_msg)
963 net_nfc_error_e error;
964 ndef_record_s *inner_record = NULL;
966 if (message == NULL || inner_msg == NULL)
968 return NET_NFC_NULL_PARAMETER;
971 inner_record = message->records;
972 if (inner_record == NULL)
974 // This message is not connection handover message
975 return NET_NFC_INVALID_FORMAT;
978 if (strncmp((char*)(inner_record->type_s.buffer), CH_REQ_RECORD_TYPE, (size_t)(inner_record->type_s.length)) != 0
979 && strncmp((char*)(inner_record->type_s.buffer), CH_SEL_RECORD_TYPE, (size_t)(inner_record->type_s.length)) != 0)
981 // This message is not connection handover message
982 return NET_NFC_INVALID_FORMAT;
985 if (inner_record->payload_s.length > 1)
987 /* There is Alternative Carrier Record or Collision Res. Rec. */
988 (inner_record->payload_s.buffer)++; /* version */
989 (inner_record->payload_s.length)--;
990 error = net_nfc_util_convert_rawdata_to_ndef_message(&(inner_record->payload_s), inner_msg);
991 (inner_record->payload_s.buffer)--;
992 (inner_record->payload_s.length)++;
996 error = NET_NFC_NO_DATA_FOUND;
1002 static net_nfc_error_e __net_nfc_replace_inner_message(ndef_message_s *message, ndef_message_s *inner_msg)
1004 net_nfc_error_e error;
1005 ndef_record_s *inner_record = NULL;
1007 if (message == NULL || inner_msg == NULL)
1009 return NET_NFC_NULL_PARAMETER;
1012 inner_record = message->records;
1013 if (inner_record == NULL)
1015 // This message is not connection handover message
1016 NFC_ERR("inner_record == NULL");
1017 return NET_NFC_INVALID_FORMAT;
1020 if (strncmp((char *)(inner_record->type_s.buffer), CH_REQ_RECORD_TYPE, (size_t)(inner_record->type_s.length)) != 0
1021 && strncmp((char *)(inner_record->type_s.buffer), CH_SEL_RECORD_TYPE, (size_t)(inner_record->type_s.length)) != 0)
1023 // This message is not connection handover message
1024 NFC_ERR("unknown type [%s]", inner_record->type_s.buffer);
1025 return NET_NFC_INVALID_FORMAT;
1028 if (inner_record->payload_s.length >= 1)
1030 /* There is Alternative Carrier Record or Collision Res. Rec. */
1031 data_s tdata = { NULL, 0 };
1034 inner_length = net_nfc_util_get_ndef_message_length(inner_msg);
1036 _net_nfc_util_alloc_mem(tdata.buffer, inner_length + 1);
1037 if (tdata.buffer == NULL)
1039 return NET_NFC_ALLOC_FAIL;
1043 tdata.length = inner_length;
1044 error = net_nfc_util_convert_ndef_message_to_rawdata(inner_msg, &tdata);
1045 if (error == NET_NFC_OK)
1049 (tdata.buffer)[0] = (inner_record->payload_s.buffer)[0];
1050 _net_nfc_util_free_mem(inner_record->payload_s.buffer);
1051 inner_record->payload_s.buffer = tdata.buffer;
1052 inner_record->payload_s.length = tdata.length;
1056 NFC_ERR("net_nfc_util_convert_ndef_message_to_rawdata failed [%d]", error);
1057 _net_nfc_util_free_mem(tdata.buffer);
1062 NFC_ERR("inner_record->payload_s.length(%d) < 1 ", inner_record->payload_s.length);
1063 error = NET_NFC_INVALID_FORMAT;
1069 net_nfc_error_e net_nfc_util_append_carrier_config_record(ndef_message_s *message, ndef_record_s *record, net_nfc_conn_handover_carrier_state_e power_status)
1071 ndef_message_s *inner_msg = NULL;
1072 ndef_record_s *carrier_rec = NULL;
1073 data_s payload = { NULL, 0 };
1074 data_s type = { NULL, 0 };
1075 int config_ref_count = 0;
1076 net_nfc_error_e error;
1077 uint8_t buffer[256] = { 0, };
1084 if (message == NULL || record == NULL)
1086 return NET_NFC_NULL_PARAMETER;
1089 if ((error = net_nfc_util_create_ndef_message(&inner_msg)) != NET_NFC_OK)
1091 NFC_ERR("net_nfc_util_create_ndef_message failed [%d]", error);
1096 if ((error = __net_nfc_get_inner_message(message, inner_msg)) == NET_NFC_OK)
1099 ndef_record_s *last_rec = inner_msg->records;
1101 for (; idx < inner_msg->recordCount; idx++)
1103 last_rec = last_rec->next;
1106 if (strncmp((char *)last_rec->type_s.buffer, COLLISION_DETECT_RECORD_TYPE, (size_t)last_rec->type_s.length) == 0)
1108 config_ref_count = 0;
1110 else if (strncmp((char *)last_rec->type_s.buffer, ALTERNATIVE_RECORD_TYPE, (size_t)last_rec->type_s.length) == 0)
1112 memcpy(buffer, last_rec->payload_s.buffer, last_rec->payload_s.length);
1113 config_ref_count = last_rec->payload_s.buffer[1];
1118 /* selector record type can include empty inner message. so that case, we will continue */
1119 if (message->records != NULL &&
1120 memcmp((char *)message->records->type_s.buffer, CH_SEL_RECORD_TYPE, (size_t)message->records->type_s.length) != 0)
1122 NFC_ERR("ERROR [%d]", error);
1124 net_nfc_util_free_ndef_message(inner_msg);
1130 type.buffer = (uint8_t *)ALTERNATIVE_RECORD_TYPE;
1131 type.length = strlen(ALTERNATIVE_RECORD_TYPE);
1135 // id = '0' + config_ref_count;
1139 buffer[offset++] = (uint8_t)(power_status & 0x3); /* first byte, power status */
1141 /* carrier data ref. len */
1142 buffer[offset++] = config_ref_count;
1144 /* carrier data ref. */
1145 offset += (config_ref_count - 1);
1146 buffer[offset++] = id;
1149 /* aux data ref. len */
1150 buffer[offset++] = 0;
1152 /* carrier data ref. */
1153 for (i = 0; i < 0; i++)
1155 buffer[offset++] = 0;
1158 payload.buffer = buffer;
1159 payload.length = offset;
1161 error = net_nfc_util_create_record(NET_NFC_RECORD_WELL_KNOWN_TYPE, &type, NULL, &payload, &carrier_rec);
1162 if (error != NET_NFC_OK)
1164 NFC_ERR("net_nfc_util_create_record failed [%d]", error);
1166 net_nfc_util_free_ndef_message(inner_msg);
1171 error = net_nfc_util_append_record(inner_msg, carrier_rec);
1172 if (error != NET_NFC_OK)
1174 NFC_ERR("net_nfc_util_create_record failed [%d]", error);
1176 net_nfc_util_free_record(carrier_rec);
1177 net_nfc_util_free_ndef_message(inner_msg);
1182 error = __net_nfc_replace_inner_message(message, inner_msg);
1183 net_nfc_util_free_ndef_message(inner_msg);
1184 if (error != NET_NFC_OK)
1186 NFC_ERR("__net_nfc_replace_inner_message failed [%d]", error);
1191 /* set record id to record that will be appended to ndef message */
1192 error = net_nfc_util_set_record_id((ndef_record_s *)record, &id, sizeof(id));
1193 if (error != NET_NFC_OK)
1195 NFC_ERR("net_nfc_util_set_record_id failed [%d]", error);
1200 error = net_nfc_util_append_record(message, (ndef_record_s *)record);
1201 if (error != NET_NFC_OK)
1203 NFC_ERR("net_nfc_util_append_record failed [%d]", error);
1211 net_nfc_error_e net_nfc_util_remove_carrier_config_record(ndef_message_s *message, ndef_record_s *record)
1213 ndef_message_s *inner_msg = NULL;
1216 net_nfc_error_e error;
1217 ndef_record_s *current = NULL;
1218 ndef_record_s *record_priv = (ndef_record_s *)record;
1220 if (message == NULL || record == NULL)
1222 return NET_NFC_NULL_PARAMETER;
1225 current = message->records;
1227 for (idx = 0; idx < message->recordCount; idx++)
1229 if (current == record)
1233 current = current->next;
1236 if (current == NULL || idx == message->recordCount)
1238 NFC_DBG("The reference is not found in config records");
1240 return NET_NFC_NO_DATA_FOUND;
1244 if ((error = net_nfc_util_create_ndef_message(&inner_msg)) != NET_NFC_OK)
1246 NFC_DBG("net_nfc_util_create_ndef_message failed [%d]", error);
1251 if ((error = __net_nfc_get_inner_message(message, inner_msg)) == NET_NFC_OK)
1253 current = inner_msg->records;
1255 for (idx = 0; idx < inner_msg->recordCount; idx++, current = current->next)
1257 if (strncmp((char *)current->type_s.buffer, ALTERNATIVE_RECORD_TYPE, (size_t)current->type_s.length) == 0)
1259 if ((uint32_t)(current->payload_s.buffer[1]) == record_priv->id_s.length &&
1260 strncmp((char *)(current->payload_s.buffer + 2), (char*)(record_priv->id_s.buffer), (size_t)current->payload_s.buffer[1]) == 0)
1262 // comparing the instance
1268 if (current == NULL || idx == message->recordCount)
1270 NFC_DBG("The reference is not found in inner message");
1272 error = NET_NFC_NO_DATA_FOUND;
1276 net_nfc_util_remove_record_by_index(inner_msg, idx);
1277 __net_nfc_replace_inner_message(message, inner_msg);
1279 // remove the record only if the inner record is found.
1280 net_nfc_util_remove_record_by_index(message, saved_idx);
1284 net_nfc_util_free_ndef_message(inner_msg);
1289 net_nfc_error_e net_nfc_util_get_carrier_config_record(ndef_message_s *message, int index, ndef_record_s** record)
1291 ndef_message_s *inner_msg = NULL;
1293 net_nfc_error_e error;
1294 ndef_record_s *current = NULL;
1296 int current_idx = 0;
1298 if (message == NULL || record == NULL)
1300 return NET_NFC_NULL_PARAMETER;
1303 if ((error = net_nfc_util_create_ndef_message(&inner_msg)) != NET_NFC_OK)
1305 NFC_DBG("net_nfc_util_create_ndef_message failed [%d]", error);
1310 if ((error = __net_nfc_get_inner_message(message, inner_msg)) == NET_NFC_OK)
1312 current = inner_msg->records;
1313 for (idx = 0; idx < inner_msg->recordCount; idx++)
1315 if (strncmp((char*)current->type_s.buffer, ALTERNATIVE_RECORD_TYPE, (size_t)current->type_s.length) == 0)
1317 if (current_idx == index)
1321 current = current->next;
1324 if (current == NULL || idx == message->recordCount)
1326 NFC_DBG("The reference is not found in inner message");
1328 error = NET_NFC_NO_DATA_FOUND;
1332 id.buffer = (current->payload_s.buffer) + 2;
1333 id.length = current->payload_s.buffer[1];
1335 error = net_nfc_util_search_record_by_id(message, &id, record);
1339 net_nfc_util_free_ndef_message(inner_msg);
1344 net_nfc_error_e net_nfc_util_get_handover_random_number(ndef_message_s *message, unsigned short *random_number)
1346 net_nfc_error_e error;
1347 ndef_message_s *inner_msg = NULL;
1348 ndef_record_s *cr_record = NULL;
1350 if (message == NULL)
1352 return NET_NFC_NULL_PARAMETER;
1355 if ((error = net_nfc_util_create_ndef_message(&inner_msg)) != NET_NFC_OK)
1357 NFC_DBG("net_nfc_util_create_ndef_message failed [%d]", error);
1362 if ((error = __net_nfc_get_inner_message(message, inner_msg)) == NET_NFC_OK)
1364 cr_record = inner_msg->records;
1365 if (strncmp((char*)cr_record->type_s.buffer, COLLISION_DETECT_RECORD_TYPE, (size_t)cr_record->type_s.length) != 0
1366 || cr_record->payload_s.length < 2)
1368 NFC_DBG("There is no Collision resolution record");
1370 error = NET_NFC_INVALID_FORMAT;
1374 *random_number = ((unsigned short)cr_record->payload_s.buffer[0] << 8) | (unsigned short)(cr_record->payload_s.buffer[1]);
1378 net_nfc_util_free_ndef_message(inner_msg);
1383 net_nfc_error_e net_nfc_util_get_alternative_carrier_record_count(ndef_message_s *message, unsigned int *count)
1385 net_nfc_error_e error;
1386 ndef_message_s *inner_msg = NULL;
1387 ndef_record_s *current = NULL;
1390 if (message == NULL || count == 0)
1392 return NET_NFC_NULL_PARAMETER;
1395 if ((error = net_nfc_util_create_ndef_message(&inner_msg)) != NET_NFC_OK)
1397 NFC_DBG("net_nfc_util_create_ndef_message failed [%d]", error);
1404 if ((error = __net_nfc_get_inner_message(message, inner_msg)) == NET_NFC_OK)
1406 current = inner_msg->records;
1407 for (idx = 0; idx < inner_msg->recordCount; idx++)
1409 if (strncmp((char *)current->type_s.buffer, ALTERNATIVE_RECORD_TYPE, (size_t)current->type_s.length) == 0)
1413 current = current->next;
1417 net_nfc_util_free_ndef_message(inner_msg);
1422 net_nfc_error_e net_nfc_util_get_alternative_carrier_power_status(ndef_message_s *message, int index, net_nfc_conn_handover_carrier_state_e *power_state)
1424 net_nfc_error_e error;
1425 ndef_message_s *inner_msg = NULL;
1426 ndef_record_s *current = NULL;
1430 if (message == NULL)
1432 return NET_NFC_NULL_PARAMETER;
1437 return NET_NFC_OUT_OF_BOUND;
1440 if ((error = net_nfc_util_create_ndef_message(&inner_msg)) != NET_NFC_OK)
1442 NFC_DBG("net_nfc_util_create_ndef_message failed [%d]", error);
1447 if ((error = __net_nfc_get_inner_message(message, inner_msg)) == NET_NFC_OK)
1449 error = NET_NFC_OUT_OF_BOUND;
1450 current = inner_msg->records;
1451 for (idx = 0; idx < inner_msg->recordCount; idx++)
1453 if (strncmp((char *)current->type_s.buffer, ALTERNATIVE_RECORD_TYPE, (size_t)current->type_s.length) == 0)
1455 if (idx_count == index)
1457 *power_state = current->payload_s.buffer[0] & 0x3;
1463 current = current->next;
1467 net_nfc_util_free_ndef_message(inner_msg);
1472 net_nfc_error_e net_nfc_util_set_alternative_carrier_power_status(ndef_message_s *message, int index, net_nfc_conn_handover_carrier_state_e power_status)
1474 net_nfc_error_e error;
1475 ndef_message_s *inner_msg = NULL;
1477 if (message == NULL)
1479 return NET_NFC_NULL_PARAMETER;
1483 return NET_NFC_OUT_OF_BOUND;
1486 if ((error = net_nfc_util_create_ndef_message(&inner_msg)) != NET_NFC_OK)
1488 NFC_DBG("net_nfc_util_create_ndef_message failed [%d]", error);
1493 if ((error = __net_nfc_get_inner_message(message, inner_msg)) == NET_NFC_OK)
1497 ndef_record_s *current = inner_msg->records;
1499 error = NET_NFC_OUT_OF_BOUND;
1500 for (idx = 0; idx < inner_msg->recordCount; idx++, current = current->next)
1502 if (strncmp((char *)current->type_s.buffer, ALTERNATIVE_RECORD_TYPE, (size_t)current->type_s.length) == 0)
1504 if (idx_count == index)
1506 current->payload_s.buffer[0] = (power_status & 0x3) | (current->payload_s.buffer[0] & 0xFC);
1508 __net_nfc_replace_inner_message(message, inner_msg);
1517 net_nfc_util_free_ndef_message(inner_msg);
1522 net_nfc_error_e net_nfc_util_get_alternative_carrier_type_from_record(ndef_record_s *record, net_nfc_conn_handover_carrier_type_e *type)
1524 if(strncmp((char*)record->type_s.buffer, CH_CAR_RECORD_TYPE, (size_t)record->type_s.length) == 0)
1526 NFC_DBG("CH_CAR_RECORD_TYPE");
1528 char ctf = record->payload_s.buffer[0] & 0x07;
1529 char ctype_length = record->payload_s.buffer[1];
1532 case NET_NFC_RECORD_MIME_TYPE: /* Media Type as defined in [RFC 2046] */
1533 if (strncmp((char *)&record->payload_s.buffer[2],
1534 CONN_HANDOVER_BT_CARRIER_MIME_NAME,
1535 (size_t)ctype_length) == 0)
1537 *type = NET_NFC_CONN_HANDOVER_CARRIER_BT;
1539 else if (strncmp((char *)&record->payload_s.buffer[2],
1540 CONN_HANDOVER_WIFI_BSS_CARRIER_MIME_NAME,
1541 (size_t)ctype_length) == 0)
1543 *type = NET_NFC_CONN_HANDOVER_CARRIER_WIFI_BSS;
1545 else if (strncmp((char *)&record->payload_s.buffer[2],
1546 CONN_HANDOVER_WIFI_IBSS_CARRIER_MIME_NAME,
1547 (size_t)ctype_length) == 0)
1549 *type = NET_NFC_CONN_HANDOVER_CARRIER_WIFI_IBSS;
1553 *type = NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN;
1556 case NET_NFC_RECORD_WELL_KNOWN_TYPE: /* NFC Forum Well-known Type*/
1557 case NET_NFC_RECORD_URI: /* Absolute URIs as defined in [RFC 3986] */
1558 case NET_NFC_RECORD_EXTERNAL_RTD: /* NFC Forum external type */
1560 *type = NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN;
1567 NFC_DBG("Other record type");
1568 if (strncmp((char *)record->type_s.buffer, CONN_HANDOVER_BT_CARRIER_MIME_NAME,
1569 (size_t)record->type_s.length) == 0)
1571 *type = NET_NFC_CONN_HANDOVER_CARRIER_BT;
1573 else if (strncmp((char *)record->type_s.buffer,
1574 CONN_HANDOVER_WIFI_BSS_CARRIER_MIME_NAME,
1575 (size_t)record->type_s.length) == 0)
1577 *type = NET_NFC_CONN_HANDOVER_CARRIER_WIFI_BSS;
1579 else if (strncmp((char *)record->type_s.buffer,
1580 CONN_HANDOVER_WIFI_IBSS_CARRIER_MIME_NAME,
1581 (size_t)record->type_s.length) == 0)
1583 *type = NET_NFC_CONN_HANDOVER_CARRIER_WIFI_IBSS;
1587 *type = NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN;
1593 net_nfc_error_e net_nfc_util_get_alternative_carrier_type(ndef_message_s *message,
1594 int index, net_nfc_conn_handover_carrier_type_e *type)
1596 ndef_record_s *record = NULL;
1597 net_nfc_error_e ret;
1599 ret = net_nfc_util_get_carrier_config_record(message, index, &record);
1600 if (ret != NET_NFC_OK)
1603 return net_nfc_util_get_alternative_carrier_type_from_record(record, type);
1606 net_nfc_error_e net_nfc_util_get_selector_power_status(ndef_message_s *message,
1607 net_nfc_conn_handover_carrier_state_e *power_state)
1609 net_nfc_error_e error;
1610 ndef_message_s *inner_msg = NULL;
1611 ndef_record_s *current = NULL;
1613 net_nfc_conn_handover_carrier_state_e selector_state;
1615 selector_state = NET_NFC_CONN_HANDOVER_CARRIER_INACTIVATE;
1617 if (message == NULL)
1619 return NET_NFC_NULL_PARAMETER;
1622 if ((error = net_nfc_util_create_ndef_message(&inner_msg)) == NET_NFC_OK)
1624 if ((error = __net_nfc_get_inner_message(message, inner_msg)) == NET_NFC_OK)
1626 if (inner_msg->recordCount > 1)
1628 current = inner_msg->records;
1629 for (idx = 0; idx < inner_msg->recordCount; idx++)
1631 if (strncmp((char *)current->type_s.buffer, ALTERNATIVE_RECORD_TYPE,
1632 (size_t)current->type_s.length) == 0)
1634 if (((current->payload_s.buffer[0] & 0x3) == NET_NFC_CONN_HANDOVER_CARRIER_ACTIVATE) || ((current->payload_s.buffer[0] & 0x3) == NET_NFC_CONN_HANDOVER_CARRIER_ACTIVATING))
1636 selector_state = NET_NFC_CONN_HANDOVER_CARRIER_ACTIVATE;
1640 current = current->next;
1645 /* always activate when ac is only one */
1646 selector_state = NET_NFC_CONN_HANDOVER_CARRIER_ACTIVATE;
1649 *power_state = selector_state;
1653 NFC_ERR("_net_nfc_util_create_ndef_message failed [%d]", error);
1656 net_nfc_util_free_ndef_message(inner_msg);
1660 NFC_ERR("_net_nfc_util_create_ndef_message failed [%d]", error);
1661 error = NET_NFC_ALLOC_FAIL;