From d22868169f31ec8d832686ddd9595074c527e3b4 Mon Sep 17 00:00:00 2001 From: Rohit Grover Date: Tue, 2 Sep 2014 13:34:54 +0100 Subject: [PATCH] 54:e2294c844c83 Add support for adding descriptors --- btle/custom/custom_helper.cpp | 50 +++++++++++++++++++++++++++++++++++ btle/custom/custom_helper.h | 7 +++++ nRF51GattServer.cpp | 33 ++++++++++++++++++----- nRF51GattServer.h | 4 +++ 4 files changed, 88 insertions(+), 6 deletions(-) diff --git a/btle/custom/custom_helper.cpp b/btle/custom/custom_helper.cpp index 6420640..05defaa 100644 --- a/btle/custom/custom_helper.cpp +++ b/btle/custom/custom_helper.cpp @@ -245,3 +245,53 @@ error_t custom_add_in_characteristic(uint16_t service_handle, return ERROR_NONE; } + + + +/**************************************************************************/ +/*! + @brief Adds a new descriptor to the custom service, assigning + value, a UUID add-on value, etc. + + @param[in] char_handle + @param[in] p_uuid The 16-bit value to add to the base UUID + for this descriptor (normally >1 + since 1 is typically used by the primary + service). + @param[in] max_length The maximum length of this descriptor + + @returns + @retval ERROR_NONE Everything executed normally +*/ +/**************************************************************************/ +error_t custom_add_in_descriptor(uint16_t char_handle, + ble_uuid_t *p_uuid, + uint8_t *p_data, + uint16_t min_length, + uint16_t max_length, + uint16_t *p_desc_handle) +{ + /* Descriptor metadata */ + ble_gatts_attr_md_t desc_md = {0}; + + desc_md.vloc = BLE_GATTS_VLOC_STACK; + desc_md.vlen = (min_length == max_length) ? 0 : 1; + + /* Make it readable and writable */ + BLE_GAP_CONN_SEC_MODE_SET_OPEN(&desc_md.read_perm); + BLE_GAP_CONN_SEC_MODE_SET_OPEN(&desc_md.write_perm); + + ble_gatts_attr_t attr_desc = {0}; + + attr_desc.p_uuid = p_uuid; + attr_desc.p_attr_md = &desc_md; + attr_desc.init_len = min_length; + attr_desc.max_len = max_length; + attr_desc.p_value = p_data; + + ASSERT_STATUS ( sd_ble_gatts_descriptor_add(char_handle, + &attr_desc, + p_desc_handle)); + + return ERROR_NONE; +} diff --git a/btle/custom/custom_helper.h b/btle/custom/custom_helper.h index 5fe541f..d4fa3cc 100644 --- a/btle/custom/custom_helper.h +++ b/btle/custom/custom_helper.h @@ -38,6 +38,13 @@ error_t custom_add_in_characteristic(uint16_t service_handle, uint16_t max_length, ble_gatts_char_handles_t *p_char_handle); +error_t custom_add_in_descriptor(uint16_t char_handle, + ble_uuid_t *p_uuid, + uint8_t *p_data, + uint16_t min_length, + uint16_t max_length, + uint16_t *p_desc_handle); + #ifdef __cplusplus } #endif diff --git a/nRF51GattServer.cpp b/nRF51GattServer.cpp index 09a9dcb..c1e5767 100644 --- a/nRF51GattServer.cpp +++ b/nRF51GattServer.cpp @@ -63,10 +63,10 @@ ble_error_t nRF51GattServer::addService(GattService &service) nordicUUID = custom_convert_to_nordic_uuid(p_char->getUUID()); ASSERT ( ERROR_NONE == - custom_add_in_characteristic(service.getHandle(), + custom_add_in_characteristic(BLE_GATT_HANDLE_INVALID, &nordicUUID, p_char->getProperties(), - NULL, + p_char->getValuePtr(), p_char->getInitialLength(), p_char->getMaxLength(), &nrfCharacteristicHandles[characteristicCount]), @@ -77,9 +77,28 @@ ble_error_t nRF51GattServer::addService(GattService &service) p_characteristics[characteristicCount++] = p_char; p_char->setHandle(charHandle); - if ((p_char->getValuePtr() != NULL) && (p_char->getInitialLength() > 0)) { - updateValue(charHandle, p_char->getValuePtr(), p_char->getInitialLength(), false /* localOnly */); + + /* Add optional descriptors if any */ + /* ToDo: Make sure we don't overflow the array */ + for (uint8_t j = 0; j < p_char->getDescriptorCount(); j++) { + GattAttribute *p_desc = p_char->getDescriptor(j); + + nordicUUID = custom_convert_to_nordic_uuid(p_desc->getUUID()); + + ASSERT ( ERROR_NONE == + custom_add_in_descriptor(BLE_GATT_HANDLE_INVALID, + &nordicUUID, + p_desc->getValuePtr(), + p_desc->getInitialLength(), + p_desc->getMaxLength(), + &nrfDescriptorHandles[descriptorCount]), + BLE_ERROR_PARAM_OUT_OF_RANGE ); + + uint16_t descHandle = descriptorCount; + p_descriptors[descriptorCount++] = p_desc; + p_desc->setHandle(descHandle); } + } serviceCount++; @@ -272,8 +291,10 @@ void nRF51GattServer::hwCallback(ble_evt_t *p_ble_evt) event = GattServerEvents::GATT_EVENT_DATA_WRITTEN; break; - case BLE_EVT_TX_COMPLETE: - handleEvent(GattServerEvents::GATT_EVENT_DATA_SENT); + case BLE_EVT_TX_COMPLETE: + for (uint8_t i = 0; ievt.common_evt.params.tx_complete.count; i++){ + handleEvent(GattServerEvents::GATT_EVENT_DATA_SENT); + } return; case BLE_GATTS_EVT_HVC: diff --git a/nRF51GattServer.h b/nRF51GattServer.h index c391713..acf5559 100644 --- a/nRF51GattServer.h +++ b/nRF51GattServer.h @@ -24,6 +24,7 @@ #include "public/GattServer.h" #define BLE_TOTAL_CHARACTERISTICS 10 +#define BLE_TOTAL_DESCRIPTORS 10 class nRF51GattServer : public GattServer { @@ -50,10 +51,13 @@ public: private: GattCharacteristic *p_characteristics[BLE_TOTAL_CHARACTERISTICS]; ble_gatts_char_handles_t nrfCharacteristicHandles[BLE_TOTAL_CHARACTERISTICS]; + GattAttribute *p_descriptors[BLE_TOTAL_DESCRIPTORS]; + uint16_t nrfDescriptorHandles[BLE_TOTAL_DESCRIPTORS]; nRF51GattServer() { serviceCount = 0; characteristicCount = 0; + descriptorCount = 0; }; nRF51GattServer(nRF51GattServer const &);