nrf5/sdk: Adding support for adding 16-bit uuid's in advertisment packet. The services in paramter list can mix 16-bit and 128-bit.

This commit is contained in:
Glenn Ruben Bakke 2017-02-17 21:47:38 +01:00
parent 9befd776fa
commit c81b606b01
1 changed files with 101 additions and 38 deletions

View File

@ -375,53 +375,116 @@ bool ble_drv_advertise_data(ubluepy_advertise_data_t * p_adv_params) {
if (p_adv_params->num_of_services > 0) { if (p_adv_params->num_of_services > 0) {
uint8_t size_byte_pos = byte_pos; bool type_16bit_present = false;
bool type_128bit_present = false;
// skip length byte for now, apply total length post calculation
byte_pos += BLE_ADV_LENGTH_FIELD_SIZE;
adv_data[byte_pos] = BLE_GAP_AD_TYPE_128BIT_SERVICE_UUID_COMPLETE;
byte_pos += BLE_ADV_AD_TYPE_FIELD_SIZE;
uint8_t uuid_total_size = 0;
uint8_t encoded_size = 0;
for (uint8_t i = 0; i < p_adv_params->num_of_services; i++) { for (uint8_t i = 0; i < p_adv_params->num_of_services; i++) {
ubluepy_service_obj_t * p_service = (ubluepy_service_obj_t *)p_adv_params->p_services[i]; ubluepy_service_obj_t * p_service = (ubluepy_service_obj_t *)p_adv_params->p_services[i];
if (p_service->p_uuid->type == UBLUEPY_UUID_16_BIT) {
ble_uuid_t uuid; type_16bit_present = true;
uuid.type = p_service->p_uuid->uuid_vs_idx;
uuid.uuid = (uint16_t)(*(uint16_t *)&p_service->p_uuid->value[0]);
// calculate total size of uuids
if (sd_ble_uuid_encode(&uuid, &encoded_size, NULL) != 0) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
"Can not encode UUID, to check length."));
} }
// do encoding into the adv buffer if (p_service->p_uuid->type == UBLUEPY_UUID_128_BIT) {
if (sd_ble_uuid_encode(&uuid, &encoded_size, &adv_data[byte_pos]) != 0) { type_128bit_present = true;
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
"Can encode UUID into the advertisment packet."));
} }
BLE_DRIVER_LOG("encoded uuid for service %u: ", 0);
for (uint8_t j = 0; j < encoded_size; j++) {
BLE_DRIVER_LOG(HEX2_FMT " ", adv_data[byte_pos + j]);
}
BLE_DRIVER_LOG("\n");
uuid_total_size += encoded_size; // size of entry
byte_pos += encoded_size; // relative to adv data packet
BLE_DRIVER_LOG("ADV: uuid size: %u, type: %u, uuid: %u, vs_idx: %u\n",
encoded_size, p_service->p_uuid->type,
(uint16_t)(*(uint16_t *)&p_service->p_uuid->value[0]),
p_service->p_uuid->uuid_vs_idx);
} }
adv_data[size_byte_pos] = (BLE_ADV_AD_TYPE_FIELD_SIZE + uuid_total_size); if (type_16bit_present) {
} uint8_t size_byte_pos = byte_pos;
// skip length byte for now, apply total length post calculation
byte_pos += BLE_ADV_LENGTH_FIELD_SIZE;
adv_data[byte_pos] = BLE_GAP_AD_TYPE_16BIT_SERVICE_UUID_COMPLETE;
byte_pos += BLE_ADV_AD_TYPE_FIELD_SIZE;
uint8_t uuid_total_size = 0;
uint8_t encoded_size = 0;
for (uint8_t i = 0; i < p_adv_params->num_of_services; i++) {
ubluepy_service_obj_t * p_service = (ubluepy_service_obj_t *)p_adv_params->p_services[i];
ble_uuid_t uuid;
uuid.type = p_service->p_uuid->type;
uuid.uuid = (uint16_t)(*(uint16_t *)&p_service->p_uuid->value[0]);
// calculate total size of uuids
if (sd_ble_uuid_encode(&uuid, &encoded_size, NULL) != 0) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
"Can not encode UUID, to check length."));
}
// do encoding into the adv buffer
if (sd_ble_uuid_encode(&uuid, &encoded_size, &adv_data[byte_pos]) != 0) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
"Can encode UUID into the advertisment packet."));
}
BLE_DRIVER_LOG("encoded uuid for service %u: ", 0);
for (uint8_t j = 0; j < encoded_size; j++) {
BLE_DRIVER_LOG(HEX2_FMT " ", adv_data[byte_pos + j]);
}
BLE_DRIVER_LOG("\n");
uuid_total_size += encoded_size; // size of entry
byte_pos += encoded_size; // relative to adv data packet
BLE_DRIVER_LOG("ADV: uuid size: %u, type: %u, uuid: %u, vs_idx: %u\n",
encoded_size, p_service->p_uuid->type,
(uint16_t)(*(uint16_t *)&p_service->p_uuid->value[0]),
p_service->p_uuid->uuid_vs_idx);
}
adv_data[size_byte_pos] = (BLE_ADV_AD_TYPE_FIELD_SIZE + uuid_total_size);
}
if (type_128bit_present) {
uint8_t size_byte_pos = byte_pos;
// skip length byte for now, apply total length post calculation
byte_pos += BLE_ADV_LENGTH_FIELD_SIZE;
adv_data[byte_pos] = BLE_GAP_AD_TYPE_128BIT_SERVICE_UUID_COMPLETE;
byte_pos += BLE_ADV_AD_TYPE_FIELD_SIZE;
uint8_t uuid_total_size = 0;
uint8_t encoded_size = 0;
for (uint8_t i = 0; i < p_adv_params->num_of_services; i++) {
ubluepy_service_obj_t * p_service = (ubluepy_service_obj_t *)p_adv_params->p_services[i];
ble_uuid_t uuid;
uuid.type = p_service->p_uuid->uuid_vs_idx;
uuid.uuid = (uint16_t)(*(uint16_t *)&p_service->p_uuid->value[0]);
// calculate total size of uuids
if (sd_ble_uuid_encode(&uuid, &encoded_size, NULL) != 0) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
"Can not encode UUID, to check length."));
}
// do encoding into the adv buffer
if (sd_ble_uuid_encode(&uuid, &encoded_size, &adv_data[byte_pos]) != 0) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
"Can encode UUID into the advertisment packet."));
}
BLE_DRIVER_LOG("encoded uuid for service %u: ", 0);
for (uint8_t j = 0; j < encoded_size; j++) {
BLE_DRIVER_LOG(HEX2_FMT " ", adv_data[byte_pos + j]);
}
BLE_DRIVER_LOG("\n");
uuid_total_size += encoded_size; // size of entry
byte_pos += encoded_size; // relative to adv data packet
BLE_DRIVER_LOG("ADV: uuid size: %u, type: %u, uuid: %u, vs_idx: %u\n",
encoded_size, p_service->p_uuid->type,
(uint16_t)(*(uint16_t *)&p_service->p_uuid->value[0]),
p_service->p_uuid->uuid_vs_idx);
}
adv_data[size_byte_pos] = (BLE_ADV_AD_TYPE_FIELD_SIZE + uuid_total_size);
}
}
// scan response data not set // scan response data not set
if (sd_ble_gap_adv_data_set(adv_data, byte_pos, NULL, 0) != 0) { if (sd_ble_gap_adv_data_set(adv_data, byte_pos, NULL, 0) != 0) {