SG RFID nRF51822 fork
Fork of nRF51822 by
Revision 54:e2294c844c83, committed 2014-08-28
- Comitter:
- carlescufi
- Date:
- Thu Aug 28 15:50:59 2014 +0200
- Parent:
- 53:1e5c300cec7f
- Child:
- 55:9689ec201907
- Commit message:
- Add support for adding descriptors
Changed in this revision
--- a/btle/custom/custom_helper.cpp Fri Jul 25 10:33:52 2014 +0100 +++ b/btle/custom/custom_helper.cpp Thu Aug 28 15:50:59 2014 +0200 @@ -245,3 +245,53 @@ 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; +}
--- a/btle/custom/custom_helper.h Fri Jul 25 10:33:52 2014 +0100 +++ b/btle/custom/custom_helper.h Thu Aug 28 15:50:59 2014 +0200 @@ -38,6 +38,13 @@ 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
--- a/nRF51GattServer.cpp Fri Jul 25 10:33:52 2014 +0100 +++ b/nRF51GattServer.cpp Thu Aug 28 15:50:59 2014 +0200 @@ -63,10 +63,10 @@ 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 @@ 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 @@ 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; i<p_ble_evt->evt.common_evt.params.tx_complete.count; i++){ + handleEvent(GattServerEvents::GATT_EVENT_DATA_SENT); + } return; case BLE_GATTS_EVT_HVC:
--- a/nRF51GattServer.h Fri Jul 25 10:33:52 2014 +0100 +++ b/nRF51GattServer.h Thu Aug 28 15:50:59 2014 +0200 @@ -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 @@ 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 &);