Solution for Bluetooth SIG hands-on training course
Dependencies: BLE_API mbed-dev-bin nRF51822-bluetooth-mdw
Fork of microbit-dal-bluetooth-mdw_starter by
Revision 77:9909cbcd0ece, committed 2016-12-27
- Comitter:
- bluetooth_mdw
- Date:
- Tue Dec 27 11:55:17 2016 +0000
- Parent:
- 76:50e98a8862ab
- Child:
- 78:ad415e663f07
- Commit message:
- Animation service skeleton
Changed in this revision
--- a/inc/bluetooth/MicroBitAnimationService.h Tue Dec 27 10:59:38 2016 +0000 +++ b/inc/bluetooth/MicroBitAnimationService.h Tue Dec 27 11:55:17 2016 +0000 @@ -30,14 +30,7 @@ #include "Animator.h" #include "ble/BLE.h" -// UUIDs for our service and characteristics -extern const uint8_t MicroBitAnimationServiceUUID[]; -// animation type: indicates the type of animation that should be executed by the microbit : R|W -extern const uint8_t MicroBitAnimationServiceAnimationTypeCharacteristicUUID[]; -// animation status: indicates whether or not an animation is currently in progress : R|N -extern const uint8_t MicroBitAnimationServiceAnimationStatusCharacteristicUUID[]; -// animation control: allows various types of control to be exercised (start|stop|faster|slower) : W -extern const uint8_t MicroBitAnimationServiceAnimationControlCharacteristicUUID[]; +// TODO: UUIDs for our service and characteristics /** * Class definition for a MicroBit BLE Animation Service. @@ -63,15 +56,14 @@ // Bluetooth stack we're running on. BLEDevice &ble; - // memory for our Animation characteristics. - uint8_t animation_type_buffer[1]; - uint8_t animation_status_buffer[1]; - uint8_t animation_control_buffer[1]; + + // TODO: memory for our Animation characteristics. + - // handles on this service's characterisitics. - GattAttribute::Handle_t animationTypeCharacteristicHandle; - GattAttribute::Handle_t animationStatusCharacteristicHandle; - GattAttribute::Handle_t animationControlCharacteristicHandle; + + // TODO: handles on this service's characterisitics. + + void animationStatusUpdate(MicroBitEvent e);
--- a/source/bluetooth/MicroBitAnimationService.cpp Tue Dec 27 10:59:38 2016 +0000 +++ b/source/bluetooth/MicroBitAnimationService.cpp Tue Dec 27 11:55:17 2016 +0000 @@ -41,57 +41,35 @@ MicroBitAnimationService::MicroBitAnimationService(BLEDevice &_ble) : ble(_ble) { - // UUID, valuePTR, length, max length, properties - GattCharacteristic animationTypeCharacteristic( - MicroBitAnimationServiceAnimationTypeCharacteristicUUID, - (uint8_t *)animation_type_buffer, - 1, - 1, - GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE); - - GattCharacteristic animationStatusCharacteristic( - MicroBitAnimationServiceAnimationStatusCharacteristicUUID, - (uint8_t *)animation_status_buffer, - 1, - 1, - GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); + // TODO: Three GattCharacteristic objects. UUID, valuePTR, length, max length, properties. + - GattCharacteristic animationControlCharacteristic( - MicroBitAnimationServiceAnimationControlCharacteristicUUID, - (uint8_t *)animation_control_buffer, - 1, - 1, - GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE); + // TODO: initialise characteristic value buffers - animation_type_buffer[0] = 0x00; - animation_status_buffer[0] = 0x00; - animation_control_buffer[0] = 0x00; + // Set default security requirements - optional // animationTypeCharacteristic.requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL); // animationStatusCharacteristic.requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL); // animationControlCharacteristic.requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL); + // TODO: create GattService containing the three characteristics // create an array of our characteristics so we can pass them to the service when we create it - GattCharacteristic *characteristics[] = {&animationTypeCharacteristic, &animationStatusCharacteristic, &animationControlCharacteristic}; // create the animation service and specify its characteristics - GattService service(MicroBitAnimationServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *)); + + - // add the service to the Bluetooth attribute table - ble.addService(service); + // TODO: add the service to the Bluetooth attribute table + - // take a note of the handles of our new characteristics now that they're in the attribute table - animationTypeCharacteristicHandle = animationTypeCharacteristic.getValueHandle(); - animationStatusCharacteristicHandle = animationStatusCharacteristic.getValueHandle(); - animationControlCharacteristicHandle = animationControlCharacteristic.getValueHandle(); + // TODO: take a note of the handles of our new characteristics now that they're in the attribute table + + + // TODO: set the attribute table's characteristic values to the initialised buffer values - ble.gattServer().write(animationTypeCharacteristicHandle, (const uint8_t *)&animation_type_buffer, sizeof(animation_type_buffer)); - ble.gattServer().write(animationStatusCharacteristicHandle, (const uint8_t *)&animation_status_buffer, sizeof(animation_status_buffer)); - ble.gattServer().write(animationControlCharacteristicHandle, (const uint8_t *)&animation_control_buffer, sizeof(animation_control_buffer)); - // register a callback function for when a characteristic is written to - ble.onDataWritten(this, &MicroBitAnimationService::onDataWritten); + // TODO: register a callback function for when a characteristic is written to // subscribe to animation status events so they can be notified to the connected client if (EventModel::defaultEventBus) @@ -105,28 +83,19 @@ void MicroBitAnimationService::onDataWritten(const GattWriteCallbackParams *params) { - if (params->handle == animationTypeCharacteristicHandle && params->len >= 1) { - uint8_t *value = (uint8_t *)params->data; - uint16_t event_value = static_cast<uint16_t>(value[0]); - MicroBitEvent evt(ANIMATION_TYPE_EVENT, event_value); - return; - } + // TODO: handle write to animation type characteristic - if (params->handle == animationControlCharacteristicHandle && params->len >= 1) { - uint8_t *value = (uint8_t *)params->data; - uint16_t event_value = static_cast<uint16_t>(value[0]); - MicroBitEvent evt(ANIMATION_CONTROL_EVENT, event_value); - return; - } + + // TODO: handle write to animation control characteristic + + } void MicroBitAnimationService::animationStatusUpdate(MicroBitEvent e) { - if (ble.getGapState().connected) - { - animation_status_buffer[0] = e.value; - ble.gattServer().notify(animationStatusCharacteristicHandle,(uint8_t *)animation_status_buffer, 1); - } + // TODO: notify connected client of change to the animation status characteristic + + } const uint8_t MicroBitAnimationServiceUUID[] = {