From c7c80af5541f6489c3118e6e4c1ce60aa32ad5d5 Mon Sep 17 00:00:00 2001 From: Szymon Janc Date: Wed, 31 Oct 2012 12:22:19 +0100 Subject: [PATCH] bluetooth: Use struct bt_data in near_ndef_prepare_handover_record This also fix creating EIR with length field in host order instead of little endian. --- src/bluetooth.c | 65 +++++++++++++++++++++++++++------------------------------ src/ndef.c | 14 ++++++------- src/near.h | 3 +-- 3 files changed, 38 insertions(+), 44 deletions(-) diff --git a/src/bluetooth.c b/src/bluetooth.c index 62d8aac..8ab7993 100644 --- a/src/bluetooth.c +++ b/src/bluetooth.c @@ -791,11 +791,9 @@ done: * mime_props is a bitmask we use to add or not specific fields in the * oob frame (e.g.: OOB keys) * */ -uint8_t *__near_bluetooth_local_get_properties(int *bt_data_len, - uint16_t mime_props) +struct bt_data *__near_bluetooth_local_get_properties(uint16_t mime_props) { - uint8_t *bt_oob_block = NULL; - uint16_t bt_oob_block_size = 0; + struct bt_data *data = NULL; uint8_t offset; char hash[OOB_SP_SIZE]; @@ -807,25 +805,26 @@ uint8_t *__near_bluetooth_local_get_properties(int *bt_data_len, goto fail; } - bt_oob_block_size = sizeof(uint16_t) /* stored oob size */ + data = g_try_malloc0(sizeof(*data)); + if (data == NULL) + goto fail; + + data->size = sizeof(uint16_t) /* stored oob size */ + BT_ADDRESS_SIZE; /* device address */ - bt_oob_block = g_try_malloc0(EIR_SIZE_MAX); - if (bt_oob_block == NULL) - goto fail; offset = sizeof(uint16_t); /* Skip size...will be filled later */ /* Now prepare data frame */ - memcpy(bt_oob_block + offset, bt_def_oob_data.bd_addr, BT_ADDRESS_SIZE); + memcpy(data->data + offset, bt_def_oob_data.bd_addr, BT_ADDRESS_SIZE); offset += BT_ADDRESS_SIZE; /* CoD */ - bt_oob_block_size += COD_SIZE + EIR_HEADER_LEN; + data->size += COD_SIZE + EIR_HEADER_LEN; - bt_oob_block[offset++] = COD_SIZE + EIR_SIZE_LEN; - bt_oob_block[offset++] = EIR_CLASS_OF_DEVICE; + data->data[offset++] = COD_SIZE + EIR_SIZE_LEN; + data->data[offset++] = EIR_CLASS_OF_DEVICE; - memcpy(bt_oob_block + offset, + memcpy(data->data + offset, (uint8_t *)&bt_def_oob_data.class_of_device, COD_SIZE); offset += COD_SIZE; @@ -837,20 +836,20 @@ uint8_t *__near_bluetooth_local_get_properties(int *bt_data_len, bt_sync_oob_readlocaldata(bt_conn, bt_def_oob_data.def_adapter, hash, random) == OOB_SP_SIZE) { - bt_oob_block_size += 2 * (OOB_SP_SIZE + EIR_HEADER_LEN); + data->size += 2 * (OOB_SP_SIZE + EIR_HEADER_LEN); /* OOB datas */ if (hash != NULL) { - bt_oob_block[offset++] = OOB_SP_SIZE + EIR_SIZE_LEN; - bt_oob_block[offset++] = EIR_SP_HASH; - memcpy(bt_oob_block + offset, hash, OOB_SP_SIZE); + data->data[offset++] = OOB_SP_SIZE + EIR_SIZE_LEN; + data->data[offset++] = EIR_SP_HASH; + memcpy(data->data + offset, hash, OOB_SP_SIZE); offset += OOB_SP_SIZE; } if (random != NULL) { - bt_oob_block[offset++] = OOB_SP_SIZE + EIR_SIZE_LEN; - bt_oob_block[offset++] = EIR_SP_RANDOMIZER; - memcpy(bt_oob_block + offset, random, OOB_SP_SIZE); + data->data[offset++] = OOB_SP_SIZE + EIR_SIZE_LEN; + data->data[offset++] = EIR_SP_RANDOMIZER; + memcpy(data->data + offset, random, OOB_SP_SIZE); offset += OOB_SP_SIZE; } } @@ -859,34 +858,32 @@ uint8_t *__near_bluetooth_local_get_properties(int *bt_data_len, if (bt_def_oob_data.bt_name != NULL) { int name_len; - bt_oob_block_size += EIR_HEADER_LEN; + data->size += EIR_HEADER_LEN; - if (bt_oob_block_size + bt_def_oob_data.bt_name_len + if (data->size + bt_def_oob_data.bt_name_len > EIR_SIZE_MAX) { - name_len = EIR_SIZE_MAX - bt_oob_block_size; - bt_oob_block[offset++] = name_len + EIR_SIZE_LEN; + name_len = EIR_SIZE_MAX - data->size; + data->data[offset++] = name_len + EIR_SIZE_LEN; /* EIR data type */ - bt_oob_block[offset++] = EIR_NAME_SHORT; + data->data[offset++] = EIR_NAME_COMPLETE; } else { name_len = bt_def_oob_data.bt_name_len; - bt_oob_block[offset++] = name_len + EIR_SIZE_LEN; + data->data[offset++] = name_len + EIR_SIZE_LEN; /* EIR data type */ - bt_oob_block[offset++] = EIR_NAME_COMPLETE; + data->data[offset++] = EIR_NAME_SHORT; } - bt_oob_block_size += name_len; - memcpy(bt_oob_block + offset, bt_def_oob_data.bt_name, - name_len); + data->size += name_len; + memcpy(data->data + offset, bt_def_oob_data.bt_name, name_len); offset += name_len; } - *(uint16_t *)bt_oob_block = bt_oob_block_size ; - *bt_data_len = bt_oob_block_size; + data->data[0] = data->size ; - return bt_oob_block; + return data; fail: - g_free(bt_oob_block); + g_free(data); return NULL; } diff --git a/src/ndef.c b/src/ndef.c index 3290551..b0cf614 100644 --- a/src/ndef.c +++ b/src/ndef.c @@ -1767,8 +1767,7 @@ struct near_ndef_message *near_ndef_prepare_handover_record(char* type_name, uint8_t carriers) { - uint8_t *oob_data = NULL; - int oob_size; + struct bt_data *data = NULL; struct near_ndef_message *hs_msg = NULL; struct near_ndef_message *ac_msg = NULL; struct near_ndef_message *cr_msg = NULL; @@ -1819,14 +1818,13 @@ struct near_ndef_message *near_ndef_prepare_handover_record(char* type_name, props = near_get_carrier_properties(record, NEAR_CARRIER_BLUETOOTH); - oob_data = __near_bluetooth_local_get_properties(&oob_size, - props); - if (oob_data == NULL) { + data = __near_bluetooth_local_get_properties(props); + if (data == NULL) { near_error("Getting Bluetooth OOB data failed"); goto fail; } - bt_msg = near_ndef_prepare_bt_message(oob_data, oob_size, + bt_msg = near_ndef_prepare_bt_message(data->data, data->size, cdr, 1); if (bt_msg == NULL) goto fail; @@ -1923,7 +1921,7 @@ struct near_ndef_message *near_ndef_prepare_handover_record(char* type_name, g_free(bt_msg); } - g_free(oob_data); + g_free(data); DBG("Hs NDEF done"); @@ -1952,7 +1950,7 @@ fail: g_free(bt_msg); } - g_free(oob_data); + g_free(data); return NULL; } diff --git a/src/near.h b/src/near.h index af36859..9981c03 100644 --- a/src/near.h +++ b/src/near.h @@ -189,8 +189,7 @@ void __near_bluetooth_cleanup(void); int __near_bluetooth_parse_oob_record(struct bt_data *data, uint16_t *properties, near_bool_t pair); int __near_bluetooth_pair(void *data); -uint8_t *__near_bluetooth_local_get_properties(int *bt_data_len, - uint16_t mime_props); +struct bt_data *__near_bluetooth_local_get_properties(uint16_t mime_props); void __near_agent_ndef_parse_records(GList *records); int __near_agent_ndef_register(const char *sender, const char *path, -- 2.7.4