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 80:74afcabdc9a1, committed 2017-02-06
- Comitter:
- bluetooth_mdw
- Date:
- Mon Feb 06 09:41:10 2017 +0000
- Parent:
- 79:ab48b2043312
- Child:
- 82:91e085d6ad72
- Commit message:
- Button B displays name
Changed in this revision
--- a/inc/bluetooth/MicroBitAnimationService.h Thu Jan 05 10:29:03 2017 +0000 +++ b/inc/bluetooth/MicroBitAnimationService.h Mon Feb 06 09:41:10 2017 +0000 @@ -1,28 +1,3 @@ -/* -The MIT License (MIT) - -Copyright (c) 2016 British Broadcasting Corporation. -This software is provided by Lancaster University by arrangement with the BBC. - -Permission is hereby granted, free of charge, to any person obtaining a -copy of this software and associated documentation files (the "Software"), -to deal in the Software without restriction, including without limitation -the rights to use, copy, modify, merge, publish, distribute, sublicense, -and/or sell copies of the Software, and to permit persons to whom the -Software is furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO Animation SHALL -THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -DEALINGS IN THE SOFTWARE. -*/ - #ifndef MICROBIT_ANIMATION_SERVICE_H #define MICROBIT_ANIMATION_SERVICE_H @@ -63,17 +38,18 @@ // Bluetooth stack we're running on. BLEDevice &ble; - - // TODO: memory for our Animation characteristics. - + // memory for our Animation characteristics. + uint8_t animation_type_buffer[1]; + uint8_t animation_status_buffer[1]; + uint8_t animation_control_buffer[1]; - - // TODO: handles on this service's characterisitics. - - + // handles on this service's characterisitics. + GattAttribute::Handle_t animationTypeCharacteristicHandle; + GattAttribute::Handle_t animationStatusCharacteristicHandle; + GattAttribute::Handle_t animationControlCharacteristicHandle; void animationStatusUpdate(MicroBitEvent e); }; -#endif +#endif \ No newline at end of file
--- a/source/bluetooth/MicroBitAnimationService.cpp Thu Jan 05 10:29:03 2017 +0000 +++ b/source/bluetooth/MicroBitAnimationService.cpp Mon Feb 06 09:41:10 2017 +0000 @@ -41,35 +41,57 @@ MicroBitAnimationService::MicroBitAnimationService(BLEDevice &_ble) : ble(_ble) { - // TODO: Three GattCharacteristic objects. UUID, valuePTR, length, max length, properties. - + // 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: initialise characteristic value buffers + GattCharacteristic animationControlCharacteristic( + MicroBitAnimationServiceAnimationControlCharacteristicUUID, + (uint8_t *)animation_control_buffer, + 1, + 1, + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE); - + 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 *)); - // TODO: add the service to the Bluetooth attribute table - + // add the service to the Bluetooth attribute table + ble.addService(service); - // TODO: take a note of the handles of our new characteristics now that they're in the 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(); + + 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)); - // TODO: set the attribute table's characteristic values to the initialised buffer values - - - // TODO: register a callback function for when a characteristic is written to + // register a callback function for when a characteristic is written to + ble.onDataWritten(this, &MicroBitAnimationService::onDataWritten); // subscribe to animation status events so they can be notified to the connected client if (EventModel::defaultEventBus) @@ -83,19 +105,28 @@ void MicroBitAnimationService::onDataWritten(const GattWriteCallbackParams *params) { - // TODO: handle write to animation type characteristic + 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 control 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; + } } void MicroBitAnimationService::animationStatusUpdate(MicroBitEvent e) { - // TODO: notify connected client of change to the animation status characteristic - - + if (ble.getGapState().connected) + { + animation_status_buffer[0] = e.value; + ble.gattServer().notify(animationStatusCharacteristicHandle,(uint8_t *)animation_status_buffer, 1); + } } const uint8_t MicroBitAnimationServiceUUID[] = { @@ -112,4 +143,4 @@ const uint8_t MicroBitAnimationServiceAnimationControlCharacteristicUUID[] = { 0xe9,0x5d,0xb8,0x4c,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8 -}; +}; \ No newline at end of file