Nordic stack and drivers for the mbed BLE API
Dependents: BLE_Health_Thermometer2
Fork of nRF51822 by
Revision 6:bbb4357dc135, committed 2014-05-28
- Comitter:
- Rohit Grover
- Date:
- Wed May 28 12:55:18 2014 +0100
- Parent:
- 5:b11766b636aa
- Child:
- 7:dd6465921aaa
- Commit message:
- running uncrustify against a few sources
Changed in this revision
--- a/btle/custom/custom_helper.cpp Fri May 23 11:14:45 2014 +0100 +++ b/btle/custom/custom_helper.cpp Wed May 28 12:55:18 2014 +0100 @@ -20,50 +20,49 @@ /*! @brief Adds the base UUID to the custom service. All UUIDs used by this service are based on this 128-bit UUID. - + @note This UUID needs to be added to the SoftDevice stack before adding the service's primary service via 'sd_ble_gatts_service_add' @param[in] p_uuid_base A pointer to the 128-bit UUID array (8*16) - + @returns The UUID type. A return value of 0 should be considered an error. - + @retval 0x00 BLE_UUID_TYPE_UNKNOWN @retval 0x01 BLE_UUID_TYPE_BLE @retval 0x02 BLE_UUID_TYPE_VENDOR_BEGIN - + @section EXAMPLE @code // Take note that bytes 2/3 are blank since these are used to identify // the primary service and individual characteristics #define CFG_CUSTOM_UUID_BASE "\x6E\x40\x00\x00\xB5\xA3\xF3\x93\xE0\xA9\xE5\x0E\x24\xDC\xCA\x9E" - + uint8_t uuid_type = custom_add_uuid_base(CFG_CUSTOM_UUID_BASE); ASSERT(uuid_type > 0, ERROR_NOT_FOUND); - + // We can now safely add the primary service and any characteristics // for our custom service ... - + @endcode */ /**************************************************************************/ -uint8_t custom_add_uuid_base(uint8_t const * const p_uuid_base) +uint8_t custom_add_uuid_base(uint8_t const *const p_uuid_base) { - ble_uuid128_t base_uuid; - uint8_t uuid_type = 0; + ble_uuid128_t base_uuid; + uint8_t uuid_type = 0; - /* Reverse the bytes since ble_uuid128_t is LSB */ - for(uint8_t i=0; i<16; i++) - { - base_uuid.uuid128[i] = p_uuid_base[15-i]; - } + /* Reverse the bytes since ble_uuid128_t is LSB */ + for (uint8_t i = 0; i<16; i++) { + base_uuid.uuid128[i] = p_uuid_base[15 - i]; + } - ASSERT_INT( ERROR_NONE, sd_ble_uuid_vs_add( &base_uuid, &uuid_type ), 0); + ASSERT_INT( ERROR_NONE, sd_ble_uuid_vs_add( &base_uuid, &uuid_type ), 0); - return uuid_type; + return uuid_type; } /**************************************************************************/ @@ -71,19 +70,19 @@ */ /**************************************************************************/ -error_t custom_decode_uuid_base(uint8_t const * const p_uuid_base, ble_uuid_t * p_uuid) +error_t custom_decode_uuid_base(uint8_t const *const p_uuid_base, + ble_uuid_t *p_uuid) { - uint8_t uuid_base_le[16]; + uint8_t uuid_base_le[16]; - /* Reverse the bytes since ble_uuid128_t is LSB */ - for(uint8_t i=0; i<16; i++) - { - uuid_base_le[i] = p_uuid_base[15-i]; - } + /* Reverse the bytes since ble_uuid128_t is LSB */ + for (uint8_t i = 0; i<16; i++) { + uuid_base_le[i] = p_uuid_base[15 - i]; + } - ASSERT_STATUS( sd_ble_uuid_decode(16, uuid_base_le, p_uuid) ); + ASSERT_STATUS( sd_ble_uuid_decode(16, uuid_base_le, p_uuid)); - return ERROR_NONE; + return ERROR_NONE; } /**************************************************************************/ @@ -100,64 +99,66 @@ defined by ble_gatt_char_props_t @param[in] max_length The maximum length of this characeristic @param[in] p_char_handle - + @returns @retval ERROR_NONE Everything executed normally */ /**************************************************************************/ -error_t custom_add_in_characteristic(uint16_t service_handle, ble_uuid_t* p_uuid, uint8_t properties, - uint8_t *p_data, uint16_t min_length, uint16_t max_length, - ble_gatts_char_handles_t* p_char_handle) +error_t custom_add_in_characteristic(uint16_t service_handle, + ble_uuid_t *p_uuid, + uint8_t properties, + uint8_t *p_data, + uint16_t min_length, + uint16_t max_length, + ble_gatts_char_handles_t *p_char_handle) { - /* Characteristic metadata */ - ble_gatts_attr_md_t cccd_md; - ble_gatt_char_props_t char_props; - - memcpy(&char_props, &properties, 1); + /* Characteristic metadata */ + ble_gatts_attr_md_t cccd_md; + ble_gatt_char_props_t char_props; - if ( char_props.notify || char_props.indicate ) - { - /* Notification requires cccd */ - memclr_( &cccd_md, sizeof(ble_gatts_attr_md_t) ); - cccd_md.vloc = BLE_GATTS_VLOC_STACK; - BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm); - BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.write_perm); - } + memcpy(&char_props, &properties, 1); - ble_gatts_char_md_t char_md = { 0 }; + if (char_props.notify || char_props.indicate) { + /* Notification requires cccd */ + memclr_( &cccd_md, sizeof(ble_gatts_attr_md_t)); + cccd_md.vloc = BLE_GATTS_VLOC_STACK; + BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm); + BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.write_perm); + } - char_md.char_props = char_props; - char_md.p_cccd_md = (char_props.notify || char_props.indicate ) ? &cccd_md : NULL; + ble_gatts_char_md_t char_md = {0}; - /* Attribute declaration */ - ble_gatts_attr_md_t attr_md = { 0 }; + char_md.char_props = char_props; + char_md.p_cccd_md = + (char_props.notify || char_props.indicate) ? &cccd_md : NULL; - attr_md.vloc = BLE_GATTS_VLOC_STACK; - attr_md.vlen = (min_length == max_length) ? 0 : 1; + /* Attribute declaration */ + ble_gatts_attr_md_t attr_md = {0}; - if ( char_props.read || char_props.notify || char_props.indicate ) - { - BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm); - } + attr_md.vloc = BLE_GATTS_VLOC_STACK; + attr_md.vlen = (min_length == max_length) ? 0 : 1; - if ( char_props.write ) - { - BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm); - } + if (char_props.read || char_props.notify || char_props.indicate) { + BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm); + } + + if (char_props.write) { + BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm); + } - ble_gatts_attr_t attr_char_value = { 0 }; + ble_gatts_attr_t attr_char_value = {0}; - attr_char_value.p_uuid = p_uuid; - attr_char_value.p_attr_md = &attr_md; - attr_char_value.init_len = min_length; - attr_char_value.max_len = max_length; - attr_char_value.p_value = p_data; + attr_char_value.p_uuid = p_uuid; + attr_char_value.p_attr_md = &attr_md; + attr_char_value.init_len = min_length; + attr_char_value.max_len = max_length; + attr_char_value.p_value = p_data; - ASSERT_STATUS ( sd_ble_gatts_characteristic_add(service_handle, - &char_md, - &attr_char_value, - p_char_handle) ); + ASSERT_STATUS ( sd_ble_gatts_characteristic_add(service_handle, + &char_md, + &attr_char_value, + p_char_handle)); - return ERROR_NONE; + return ERROR_NONE; }
--- a/btle/custom/custom_helper.h Fri May 23 11:14:45 2014 +0100 +++ b/btle/custom/custom_helper.h Wed May 28 12:55:18 2014 +0100 @@ -18,21 +18,26 @@ #define _CUSTOM_HELPER_H_ #ifdef __cplusplus - extern "C" { +extern "C" { #endif #include "common/common.h" #include "ble.h" -uint8_t custom_add_uuid_base(uint8_t const * const p_uuid_base); -error_t custom_decode_uuid(uint8_t const * const p_uuid_base, ble_uuid_t * p_uuid); +uint8_t custom_add_uuid_base(uint8_t const *const p_uuid_base); +error_t custom_decode_uuid(uint8_t const *const p_uuid_base, + ble_uuid_t *p_uuid); -error_t custom_add_in_characteristic(uint16_t service_handle, ble_uuid_t* p_uuid, uint8_t properties, - uint8_t *p_data, uint16_t min_length, uint16_t max_length, - ble_gatts_char_handles_t* p_char_handle); +error_t custom_add_in_characteristic(uint16_t service_handle, + ble_uuid_t *p_uuid, + uint8_t properties, + uint8_t *p_data, + uint16_t min_length, + uint16_t max_length, + ble_gatts_char_handles_t *p_char_handle); #ifdef __cplusplus } #endif -#endif +#endif // ifndef _CUSTOM_HELPER_H_
--- a/nRF51GattServer.cpp Fri May 23 11:14:45 2014 +0100 +++ b/nRF51GattServer.cpp Wed May 28 12:55:18 2014 +0100 @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - + #include "nRF51GattServer.h" #include "mbed.h" @@ -25,12 +25,12 @@ /**************************************************************************/ /*! @brief Adds a new service to the GATT table on the peripheral - + @returns ble_error_t - + @retval BLE_ERROR_NONE Everything executed properly - + @section EXAMPLE @code @@ -43,41 +43,49 @@ /* ToDo: Make sure we don't overflow the array, etc. */ /* ToDo: Make sure this service UUID doesn't already exist (?) */ /* ToDo: Basic validation */ - + /* Add the service to the nRF51 */ ble_uuid_t uuid; - if (service.primaryServiceID.type == UUID::UUID_TYPE_SHORT) - { - /* 16-bit BLE UUID */ - uuid.type = BLE_UUID_TYPE_BLE; - } - else - { - /* 128-bit Custom UUID */ - uuid.type = custom_add_uuid_base( service.primaryServiceID.base ); + if (service.primaryServiceID.type == UUID::UUID_TYPE_SHORT) { + /* 16-bit BLE UUID */ + uuid.type = BLE_UUID_TYPE_BLE; + } else { + /* 128-bit Custom UUID */ + uuid.type = custom_add_uuid_base( service.primaryServiceID.base ); } uuid.uuid = service.primaryServiceID.value; - ASSERT( ERROR_NONE == sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &uuid, &service.handle), BLE_ERROR_PARAM_OUT_OF_RANGE ); + ASSERT( ERROR_NONE == + sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, + &uuid, + &service.handle), + BLE_ERROR_PARAM_OUT_OF_RANGE ); /* Add characteristics to the service */ - for (uint8_t i = 0; i < service.characteristicCount; i++) - { - GattCharacteristic * p_char = service.characteristics[i]; + for (uint8_t i = 0; i < service.characteristicCount; i++) { + GattCharacteristic *p_char = service.characteristics[i]; - uuid.uuid = p_char->uuid; - ASSERT ( ERROR_NONE == custom_add_in_characteristic(service.handle, &uuid, p_char->properties, - NULL, p_char->lenMin, p_char->lenMax, &nrfCharacteristicHandles[characteristicCount]), BLE_ERROR_PARAM_OUT_OF_RANGE ); - - /* Update the characteristic handle */ - p_char->handle = characteristicCount; - p_characteristics[characteristicCount++] = p_char; + uuid.uuid = p_char->uuid; + ASSERT ( ERROR_NONE == + custom_add_in_characteristic(service.handle, + &uuid, + p_char->properties, + NULL, + p_char->lenMin, + p_char->lenMax, + &nrfCharacteristicHandles[ + characteristicCount]), + BLE_ERROR_PARAM_OUT_OF_RANGE ); + + /* Update the characteristic handle */ + p_char->handle = characteristicCount; + p_characteristics[characteristicCount++] = p_char; } serviceCount++; - + return BLE_ERROR_NONE; } @@ -93,12 +101,12 @@ (raw byte array in LSB format) @param[in] len The number of bytes read into the buffer - + @returns ble_error_t - + @retval BLE_ERROR_NONE Everything executed properly - + @section EXAMPLE @code @@ -106,10 +114,15 @@ @endcode */ /**************************************************************************/ -ble_error_t nRF51GattServer::readValue(uint16_t charHandle, uint8_t buffer[], uint16_t len) +ble_error_t nRF51GattServer::readValue(uint16_t charHandle, + uint8_t buffer[], + uint16_t len) { - ASSERT( ERROR_NONE == sd_ble_gatts_value_get(nrfCharacteristicHandles[charHandle].value_handle, 0, &len, buffer), BLE_ERROR_PARAM_OUT_OF_RANGE); - + ASSERT( ERROR_NONE == + sd_ble_gatts_value_get(nrfCharacteristicHandles[charHandle]. + value_handle, 0, + &len, buffer), BLE_ERROR_PARAM_OUT_OF_RANGE); + return BLE_ERROR_NONE; } @@ -125,12 +138,12 @@ (raw byte array in LSB format) @param[in] len The number of bytes in buffer - + @returns ble_error_t - + @retval BLE_ERROR_NONE Everything executed properly - + @section EXAMPLE @code @@ -138,43 +151,69 @@ @endcode */ /**************************************************************************/ -ble_error_t nRF51GattServer::updateValue(uint16_t charHandle, uint8_t buffer[], uint16_t len, bool localOnly) +ble_error_t nRF51GattServer::updateValue(uint16_t charHandle, + uint8_t buffer[], + uint16_t len, + bool localOnly) { - uint16_t gapConnectionHandle = nRF51Gap::getInstance().getConnectionHandle(); - - if (localOnly) - { - /* Only update locally regardless of notify/indicate */ - ASSERT_INT( ERROR_NONE, sd_ble_gatts_value_set(nrfCharacteristicHandles[charHandle].value_handle, 0, &len, buffer), BLE_ERROR_PARAM_OUT_OF_RANGE ); - } - - if ((p_characteristics[charHandle]->properties & (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)) && - (gapConnectionHandle != BLE_CONN_HANDLE_INVALID) ) - { - /* HVX update for the characteristic value */ - ble_gatts_hvx_params_t hvx_params; + uint16_t gapConnectionHandle = + nRF51Gap::getInstance().getConnectionHandle(); + + if (localOnly) { + /* Only update locally regardless of notify/indicate */ + ASSERT_INT( ERROR_NONE, + sd_ble_gatts_value_set(nrfCharacteristicHandles[charHandle]. + value_handle, + 0, + &len, + buffer), + BLE_ERROR_PARAM_OUT_OF_RANGE ); + } + + if ((p_characteristics[charHandle]->properties & + (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE | + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)) && + (gapConnectionHandle != BLE_CONN_HANDLE_INVALID)) { + /* HVX update for the characteristic value */ + ble_gatts_hvx_params_t hvx_params; - hvx_params.handle = nrfCharacteristicHandles[charHandle].value_handle; - hvx_params.type = (p_characteristics[charHandle]->properties & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) ? BLE_GATT_HVX_NOTIFICATION : BLE_GATT_HVX_INDICATION; - hvx_params.offset = 0; - hvx_params.p_data = buffer; - hvx_params.p_len = &len; + hvx_params.handle = nrfCharacteristicHandles[charHandle].value_handle; + hvx_params.type = + (p_characteristics[charHandle]->properties & + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) ? + BLE_GATT_HVX_NOTIFICATION : BLE_GATT_HVX_INDICATION; + hvx_params.offset = 0; + hvx_params.p_data = buffer; + hvx_params.p_len = &len; - error_t error = (error_t) sd_ble_gatts_hvx(gapConnectionHandle, &hvx_params); + error_t error = (error_t) sd_ble_gatts_hvx(gapConnectionHandle, + &hvx_params); - /* ERROR_INVALID_STATE, ERROR_BUSY, ERROR_GATTS_SYS_ATTR_MISSING and ERROR_NO_TX_BUFFERS the ATT table has been updated. */ - if ( (error != ERROR_NONE ) && (error != ERROR_INVALID_STATE) && - (error != ERROR_BLE_NO_TX_BUFFERS ) && (error != ERROR_BUSY ) && - (error != ERROR_BLEGATTS_SYS_ATTR_MISSING ) ) - { - ASSERT_INT( ERROR_NONE, sd_ble_gatts_value_set(nrfCharacteristicHandles[charHandle].value_handle, 0, &len, buffer), BLE_ERROR_PARAM_OUT_OF_RANGE ); + /* ERROR_INVALID_STATE, ERROR_BUSY, ERROR_GATTS_SYS_ATTR_MISSING and + *ERROR_NO_TX_BUFFERS the ATT table has been updated. */ + if ((error != ERROR_NONE) && (error != ERROR_INVALID_STATE) && + (error != ERROR_BLE_NO_TX_BUFFERS) && (error != ERROR_BUSY) && + (error != ERROR_BLEGATTS_SYS_ATTR_MISSING)) { + ASSERT_INT( ERROR_NONE, + sd_ble_gatts_value_set(nrfCharacteristicHandles[ + charHandle]. + value_handle, + 0, + &len, + buffer), + BLE_ERROR_PARAM_OUT_OF_RANGE ); + } + } else { + ASSERT_INT( ERROR_NONE, + sd_ble_gatts_value_set(nrfCharacteristicHandles[charHandle]. + value_handle, + 0, + &len, + buffer), + BLE_ERROR_PARAM_OUT_OF_RANGE ); } - } else - { - ASSERT_INT( ERROR_NONE, sd_ble_gatts_value_set(nrfCharacteristicHandles[charHandle].value_handle, 0, &len, buffer), BLE_ERROR_PARAM_OUT_OF_RANGE ); - } - - return BLE_ERROR_NONE; + + return BLE_ERROR_NONE; } /**************************************************************************/ @@ -182,60 +221,65 @@ @brief Callback handler for events getting pushed up from the SD */ /**************************************************************************/ -void nRF51GattServer::hwCallback(ble_evt_t * p_ble_evt) +void nRF51GattServer::hwCallback(ble_evt_t *p_ble_evt) { - uint16_t handle_value; - GattServerEvents::gattEvent_t event; + uint16_t handle_value; + GattServerEvents::gattEvent_t event; - switch (p_ble_evt->header.evt_id) - { + switch (p_ble_evt->header.evt_id) { case BLE_GATTS_EVT_WRITE: - /* There are 2 use case here: Values being updated & CCCD (indicate/notify) enabled */ - - /* 1.) Handle CCCD changes */ - handle_value = p_ble_evt->evt.gatts_evt.params.write.handle; - for(uint8_t i=0; i<characteristicCount; i++) - { - if ( (p_characteristics[i]->properties & (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)) && - (nrfCharacteristicHandles[i].cccd_handle == handle_value) ) - { - uint16_t cccd_value = (p_ble_evt->evt.gatts_evt.params.write.data[1] << 8) | p_ble_evt->evt.gatts_evt.params.write.data[0]; /* Little Endian but M0 may be mis-aligned */ + /* There are 2 use case here: Values being updated & CCCD + *(indicate/notify) enabled */ + + /* 1.) Handle CCCD changes */ + handle_value = p_ble_evt->evt.gatts_evt.params.write.handle; + for (uint8_t i = 0; i<characteristicCount; i++) { + if ((p_characteristics[i]->properties & + (GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE | + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)) && + (nrfCharacteristicHandles[i].cccd_handle == handle_value)) { + uint16_t cccd_value = + (p_ble_evt->evt.gatts_evt.params.write.data[1] << + 8) | p_ble_evt->evt.gatts_evt.params.write.data[0]; /* + * Little Endian but M0 may + * be mis-aligned */ - if ( ((p_characteristics[i]->properties & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE) && (cccd_value & BLE_GATT_HVX_INDICATION )) || - ((p_characteristics[i]->properties & GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY ) && (cccd_value & BLE_GATT_HVX_NOTIFICATION))) - { - event = GattServerEvents::GATT_EVENT_UPDATES_ENABLED; - } else - { - event = GattServerEvents::GATT_EVENT_UPDATES_DISABLED; - } + if (((p_characteristics[i]->properties & + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE) && + (cccd_value & BLE_GATT_HVX_INDICATION)) || + ((p_characteristics[i]->properties & + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) && + (cccd_value & BLE_GATT_HVX_NOTIFICATION))) { + event = GattServerEvents::GATT_EVENT_UPDATES_ENABLED; + } else { + event = GattServerEvents::GATT_EVENT_UPDATES_DISABLED; + } - handleEvent(event, i); - return; + handleEvent(event, i); + return; + } } - } - /* 2.) Changes to the characteristic value will be handled with other events below */ - event = GattServerEvents::GATT_EVENT_DATA_WRITTEN; - break; + /* 2.) Changes to the characteristic value will be handled with other + *events below */ + event = GattServerEvents::GATT_EVENT_DATA_WRITTEN; + break; case BLE_GATTS_EVT_HVC: - /* Indication confirmation received */ - event = GattServerEvents::GATT_EVENT_CONFIRMATION_RECEIVED; - handle_value = p_ble_evt->evt.gatts_evt.params.hvc.handle; - break; + /* Indication confirmation received */ + event = GattServerEvents::GATT_EVENT_CONFIRMATION_RECEIVED; + handle_value = p_ble_evt->evt.gatts_evt.params.hvc.handle; + break; - default: - return; - } + default: + return; + } - /* Find index (charHandle) in the pool */ - for(uint8_t i=0; i<characteristicCount; i++) - { - if (nrfCharacteristicHandles[i].value_handle == handle_value) - { - handleEvent(event, i); - break; + /* Find index (charHandle) in the pool */ + for (uint8_t i = 0; i<characteristicCount; i++) { + if (nrfCharacteristicHandles[i].value_handle == handle_value) { + handleEvent(event, i); + break; + } } - } }