Official Sheffield ARMBand micro:bit program
microbit/microbit-dal/source/bluetooth/MicroBitEventService.cpp@0:b9164b348919, 2016-10-17 (annotated)
- Committer:
- MrBedfordVan
- Date:
- Mon Oct 17 12:41:20 2016 +0000
- Revision:
- 0:b9164b348919
Official Sheffield ARMBand Micro:bit program
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MrBedfordVan | 0:b9164b348919 | 1 | /* |
MrBedfordVan | 0:b9164b348919 | 2 | The MIT License (MIT) |
MrBedfordVan | 0:b9164b348919 | 3 | |
MrBedfordVan | 0:b9164b348919 | 4 | Copyright (c) 2016 British Broadcasting Corporation. |
MrBedfordVan | 0:b9164b348919 | 5 | This software is provided by Lancaster University by arrangement with the BBC. |
MrBedfordVan | 0:b9164b348919 | 6 | |
MrBedfordVan | 0:b9164b348919 | 7 | Permission is hereby granted, free of charge, to any person obtaining a |
MrBedfordVan | 0:b9164b348919 | 8 | copy of this software and associated documentation files (the "Software"), |
MrBedfordVan | 0:b9164b348919 | 9 | to deal in the Software without restriction, including without limitation |
MrBedfordVan | 0:b9164b348919 | 10 | the rights to use, copy, modify, merge, publish, distribute, sublicense, |
MrBedfordVan | 0:b9164b348919 | 11 | and/or sell copies of the Software, and to permit persons to whom the |
MrBedfordVan | 0:b9164b348919 | 12 | Software is furnished to do so, subject to the following conditions: |
MrBedfordVan | 0:b9164b348919 | 13 | |
MrBedfordVan | 0:b9164b348919 | 14 | The above copyright notice and this permission notice shall be included in |
MrBedfordVan | 0:b9164b348919 | 15 | all copies or substantial portions of the Software. |
MrBedfordVan | 0:b9164b348919 | 16 | |
MrBedfordVan | 0:b9164b348919 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
MrBedfordVan | 0:b9164b348919 | 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
MrBedfordVan | 0:b9164b348919 | 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
MrBedfordVan | 0:b9164b348919 | 20 | THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
MrBedfordVan | 0:b9164b348919 | 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
MrBedfordVan | 0:b9164b348919 | 22 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
MrBedfordVan | 0:b9164b348919 | 23 | DEALINGS IN THE SOFTWARE. |
MrBedfordVan | 0:b9164b348919 | 24 | */ |
MrBedfordVan | 0:b9164b348919 | 25 | |
MrBedfordVan | 0:b9164b348919 | 26 | /** |
MrBedfordVan | 0:b9164b348919 | 27 | * Class definition for a MicroBit BLE Event Service. |
MrBedfordVan | 0:b9164b348919 | 28 | * Provides a BLE gateway onto an Event Model. |
MrBedfordVan | 0:b9164b348919 | 29 | */ |
MrBedfordVan | 0:b9164b348919 | 30 | |
MrBedfordVan | 0:b9164b348919 | 31 | #include "MicroBitConfig.h" |
MrBedfordVan | 0:b9164b348919 | 32 | #include "MicroBitEventService.h" |
MrBedfordVan | 0:b9164b348919 | 33 | #include "ble/UUID.h" |
MrBedfordVan | 0:b9164b348919 | 34 | #include "ExternalEvents.h" |
MrBedfordVan | 0:b9164b348919 | 35 | #include "MicroBitFiber.h" |
MrBedfordVan | 0:b9164b348919 | 36 | |
MrBedfordVan | 0:b9164b348919 | 37 | /** |
MrBedfordVan | 0:b9164b348919 | 38 | * Constructor. |
MrBedfordVan | 0:b9164b348919 | 39 | * Create a representation of the EventService |
MrBedfordVan | 0:b9164b348919 | 40 | * @param _ble The instance of a BLE device that we're running on. |
MrBedfordVan | 0:b9164b348919 | 41 | * @param _messageBus An instance of an EventModel which events will be mirrored from. |
MrBedfordVan | 0:b9164b348919 | 42 | */ |
MrBedfordVan | 0:b9164b348919 | 43 | MicroBitEventService::MicroBitEventService(BLEDevice &_ble, EventModel &_messageBus) : |
MrBedfordVan | 0:b9164b348919 | 44 | ble(_ble),messageBus(_messageBus) |
MrBedfordVan | 0:b9164b348919 | 45 | { |
MrBedfordVan | 0:b9164b348919 | 46 | GattCharacteristic microBitEventCharacteristic(MicroBitEventServiceMicroBitEventCharacteristicUUID, (uint8_t *)µBitEventBuffer, 0, sizeof(EventServiceEvent), |
MrBedfordVan | 0:b9164b348919 | 47 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); |
MrBedfordVan | 0:b9164b348919 | 48 | |
MrBedfordVan | 0:b9164b348919 | 49 | GattCharacteristic clientEventCharacteristic(MicroBitEventServiceClientEventCharacteristicUUID, (uint8_t *)&clientEventBuffer, 0, sizeof(EventServiceEvent), |
MrBedfordVan | 0:b9164b348919 | 50 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE); |
MrBedfordVan | 0:b9164b348919 | 51 | |
MrBedfordVan | 0:b9164b348919 | 52 | GattCharacteristic clientRequirementsCharacteristic(MicroBitEventServiceClientRequirementsCharacteristicUUID, (uint8_t *)&clientRequirementsBuffer, 0, sizeof(EventServiceEvent), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE); |
MrBedfordVan | 0:b9164b348919 | 53 | |
MrBedfordVan | 0:b9164b348919 | 54 | microBitRequirementsCharacteristic = new GattCharacteristic(MicroBitEventServiceMicroBitRequirementsCharacteristicUUID, (uint8_t *)µBitRequirementsBuffer, 0, sizeof(EventServiceEvent), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); |
MrBedfordVan | 0:b9164b348919 | 55 | |
MrBedfordVan | 0:b9164b348919 | 56 | microBitRequirementsCharacteristic->setReadAuthorizationCallback(this, &MicroBitEventService::onRequirementsRead); |
MrBedfordVan | 0:b9164b348919 | 57 | |
MrBedfordVan | 0:b9164b348919 | 58 | clientEventBuffer.type = 0x00; |
MrBedfordVan | 0:b9164b348919 | 59 | clientEventBuffer.reason = 0x00; |
MrBedfordVan | 0:b9164b348919 | 60 | |
MrBedfordVan | 0:b9164b348919 | 61 | microBitEventBuffer = microBitRequirementsBuffer = clientRequirementsBuffer = clientEventBuffer; |
MrBedfordVan | 0:b9164b348919 | 62 | |
MrBedfordVan | 0:b9164b348919 | 63 | messageBusListenerOffset = 0; |
MrBedfordVan | 0:b9164b348919 | 64 | |
MrBedfordVan | 0:b9164b348919 | 65 | // Set default security requirements |
MrBedfordVan | 0:b9164b348919 | 66 | microBitEventCharacteristic.requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL); |
MrBedfordVan | 0:b9164b348919 | 67 | clientEventCharacteristic.requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL); |
MrBedfordVan | 0:b9164b348919 | 68 | clientRequirementsCharacteristic.requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL); |
MrBedfordVan | 0:b9164b348919 | 69 | microBitRequirementsCharacteristic->requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL); |
MrBedfordVan | 0:b9164b348919 | 70 | |
MrBedfordVan | 0:b9164b348919 | 71 | GattCharacteristic *characteristics[] = {µBitEventCharacteristic, &clientEventCharacteristic, &clientRequirementsCharacteristic, microBitRequirementsCharacteristic}; |
MrBedfordVan | 0:b9164b348919 | 72 | GattService service(MicroBitEventServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *)); |
MrBedfordVan | 0:b9164b348919 | 73 | |
MrBedfordVan | 0:b9164b348919 | 74 | ble.addService(service); |
MrBedfordVan | 0:b9164b348919 | 75 | |
MrBedfordVan | 0:b9164b348919 | 76 | microBitEventCharacteristicHandle = microBitEventCharacteristic.getValueHandle(); |
MrBedfordVan | 0:b9164b348919 | 77 | clientEventCharacteristicHandle = clientEventCharacteristic.getValueHandle(); |
MrBedfordVan | 0:b9164b348919 | 78 | clientRequirementsCharacteristicHandle = clientRequirementsCharacteristic.getValueHandle(); |
MrBedfordVan | 0:b9164b348919 | 79 | |
MrBedfordVan | 0:b9164b348919 | 80 | ble.onDataWritten(this, &MicroBitEventService::onDataWritten); |
MrBedfordVan | 0:b9164b348919 | 81 | |
MrBedfordVan | 0:b9164b348919 | 82 | fiber_add_idle_component(this); |
MrBedfordVan | 0:b9164b348919 | 83 | } |
MrBedfordVan | 0:b9164b348919 | 84 | |
MrBedfordVan | 0:b9164b348919 | 85 | |
MrBedfordVan | 0:b9164b348919 | 86 | /** |
MrBedfordVan | 0:b9164b348919 | 87 | * Callback. Invoked when any of our attributes are written via BLE. |
MrBedfordVan | 0:b9164b348919 | 88 | */ |
MrBedfordVan | 0:b9164b348919 | 89 | void MicroBitEventService::onDataWritten(const GattWriteCallbackParams *params) |
MrBedfordVan | 0:b9164b348919 | 90 | { |
MrBedfordVan | 0:b9164b348919 | 91 | int len = params->len; |
MrBedfordVan | 0:b9164b348919 | 92 | EventServiceEvent *e = (EventServiceEvent *)params->data; |
MrBedfordVan | 0:b9164b348919 | 93 | |
MrBedfordVan | 0:b9164b348919 | 94 | if (params->handle == clientEventCharacteristicHandle) { |
MrBedfordVan | 0:b9164b348919 | 95 | |
MrBedfordVan | 0:b9164b348919 | 96 | // Read and fire all events... |
MrBedfordVan | 0:b9164b348919 | 97 | while (len >= 4) |
MrBedfordVan | 0:b9164b348919 | 98 | { |
MrBedfordVan | 0:b9164b348919 | 99 | MicroBitEvent evt(e->type, e->reason); |
MrBedfordVan | 0:b9164b348919 | 100 | len-=4; |
MrBedfordVan | 0:b9164b348919 | 101 | e++; |
MrBedfordVan | 0:b9164b348919 | 102 | } |
MrBedfordVan | 0:b9164b348919 | 103 | return; |
MrBedfordVan | 0:b9164b348919 | 104 | } |
MrBedfordVan | 0:b9164b348919 | 105 | |
MrBedfordVan | 0:b9164b348919 | 106 | if (params->handle == clientRequirementsCharacteristicHandle) { |
MrBedfordVan | 0:b9164b348919 | 107 | // Read and register for all the events given... |
MrBedfordVan | 0:b9164b348919 | 108 | while (len >= 4) |
MrBedfordVan | 0:b9164b348919 | 109 | { |
MrBedfordVan | 0:b9164b348919 | 110 | messageBus.listen(e->type, e->reason, this, &MicroBitEventService::onMicroBitEvent, MESSAGE_BUS_LISTENER_IMMEDIATE); |
MrBedfordVan | 0:b9164b348919 | 111 | |
MrBedfordVan | 0:b9164b348919 | 112 | len-=4; |
MrBedfordVan | 0:b9164b348919 | 113 | e++; |
MrBedfordVan | 0:b9164b348919 | 114 | } |
MrBedfordVan | 0:b9164b348919 | 115 | return; |
MrBedfordVan | 0:b9164b348919 | 116 | } |
MrBedfordVan | 0:b9164b348919 | 117 | } |
MrBedfordVan | 0:b9164b348919 | 118 | |
MrBedfordVan | 0:b9164b348919 | 119 | /** |
MrBedfordVan | 0:b9164b348919 | 120 | * Callback. Invoked when any events are sent on the microBit message bus. |
MrBedfordVan | 0:b9164b348919 | 121 | */ |
MrBedfordVan | 0:b9164b348919 | 122 | void MicroBitEventService::onMicroBitEvent(MicroBitEvent evt) |
MrBedfordVan | 0:b9164b348919 | 123 | { |
MrBedfordVan | 0:b9164b348919 | 124 | EventServiceEvent *e = µBitEventBuffer; |
MrBedfordVan | 0:b9164b348919 | 125 | |
MrBedfordVan | 0:b9164b348919 | 126 | if (ble.getGapState().connected) { |
MrBedfordVan | 0:b9164b348919 | 127 | e->type = evt.source; |
MrBedfordVan | 0:b9164b348919 | 128 | e->reason = evt.value; |
MrBedfordVan | 0:b9164b348919 | 129 | |
MrBedfordVan | 0:b9164b348919 | 130 | ble.gattServer().notify(microBitEventCharacteristicHandle, (const uint8_t *)e, sizeof(EventServiceEvent)); |
MrBedfordVan | 0:b9164b348919 | 131 | } |
MrBedfordVan | 0:b9164b348919 | 132 | } |
MrBedfordVan | 0:b9164b348919 | 133 | |
MrBedfordVan | 0:b9164b348919 | 134 | /** |
MrBedfordVan | 0:b9164b348919 | 135 | * Periodic callback from MicroBit scheduler. |
MrBedfordVan | 0:b9164b348919 | 136 | * If we're no longer connected, remove any registered Message Bus listeners. |
MrBedfordVan | 0:b9164b348919 | 137 | */ |
MrBedfordVan | 0:b9164b348919 | 138 | void MicroBitEventService::idleTick() |
MrBedfordVan | 0:b9164b348919 | 139 | { |
MrBedfordVan | 0:b9164b348919 | 140 | if (!ble.getGapState().connected && messageBusListenerOffset >0) { |
MrBedfordVan | 0:b9164b348919 | 141 | messageBusListenerOffset = 0; |
MrBedfordVan | 0:b9164b348919 | 142 | messageBus.ignore(MICROBIT_ID_ANY, MICROBIT_EVT_ANY, this, &MicroBitEventService::onMicroBitEvent); |
MrBedfordVan | 0:b9164b348919 | 143 | } |
MrBedfordVan | 0:b9164b348919 | 144 | } |
MrBedfordVan | 0:b9164b348919 | 145 | |
MrBedfordVan | 0:b9164b348919 | 146 | /** |
MrBedfordVan | 0:b9164b348919 | 147 | * Read callback on microBitRequirements characteristic. |
MrBedfordVan | 0:b9164b348919 | 148 | * |
MrBedfordVan | 0:b9164b348919 | 149 | * Used to iterate through the events that the code on this micro:bit is interested in. |
MrBedfordVan | 0:b9164b348919 | 150 | */ |
MrBedfordVan | 0:b9164b348919 | 151 | void MicroBitEventService::onRequirementsRead(GattReadAuthCallbackParams *params) |
MrBedfordVan | 0:b9164b348919 | 152 | { |
MrBedfordVan | 0:b9164b348919 | 153 | if (params->handle == microBitRequirementsCharacteristic->getValueHandle()) |
MrBedfordVan | 0:b9164b348919 | 154 | { |
MrBedfordVan | 0:b9164b348919 | 155 | // Walk through the lsit of message bus listeners. |
MrBedfordVan | 0:b9164b348919 | 156 | // We send one at a time, and our client can keep reading from this characterisitic until we return an emtpy value. |
MrBedfordVan | 0:b9164b348919 | 157 | MicroBitListener *l = messageBus.elementAt(messageBusListenerOffset++); |
MrBedfordVan | 0:b9164b348919 | 158 | |
MrBedfordVan | 0:b9164b348919 | 159 | if (l != NULL) |
MrBedfordVan | 0:b9164b348919 | 160 | { |
MrBedfordVan | 0:b9164b348919 | 161 | microBitRequirementsBuffer.type = l->id; |
MrBedfordVan | 0:b9164b348919 | 162 | microBitRequirementsBuffer.reason = l->value; |
MrBedfordVan | 0:b9164b348919 | 163 | ble.gattServer().write(microBitRequirementsCharacteristic->getValueHandle(), (uint8_t *)µBitRequirementsBuffer, sizeof(EventServiceEvent)); |
MrBedfordVan | 0:b9164b348919 | 164 | } else { |
MrBedfordVan | 0:b9164b348919 | 165 | ble.gattServer().write(microBitRequirementsCharacteristic->getValueHandle(), (uint8_t *)µBitRequirementsBuffer, 0); |
MrBedfordVan | 0:b9164b348919 | 166 | } |
MrBedfordVan | 0:b9164b348919 | 167 | } |
MrBedfordVan | 0:b9164b348919 | 168 | } |
MrBedfordVan | 0:b9164b348919 | 169 | |
MrBedfordVan | 0:b9164b348919 | 170 | const uint8_t MicroBitEventServiceUUID[] = { |
MrBedfordVan | 0:b9164b348919 | 171 | 0xe9,0x5d,0x93,0xaf,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8 |
MrBedfordVan | 0:b9164b348919 | 172 | }; |
MrBedfordVan | 0:b9164b348919 | 173 | |
MrBedfordVan | 0:b9164b348919 | 174 | const uint8_t MicroBitEventServiceMicroBitEventCharacteristicUUID[] = { |
MrBedfordVan | 0:b9164b348919 | 175 | 0xe9,0x5d,0x97,0x75,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8 |
MrBedfordVan | 0:b9164b348919 | 176 | }; |
MrBedfordVan | 0:b9164b348919 | 177 | |
MrBedfordVan | 0:b9164b348919 | 178 | const uint8_t MicroBitEventServiceClientEventCharacteristicUUID[] = { |
MrBedfordVan | 0:b9164b348919 | 179 | 0xe9,0x5d,0x54,0x04,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8 |
MrBedfordVan | 0:b9164b348919 | 180 | }; |
MrBedfordVan | 0:b9164b348919 | 181 | |
MrBedfordVan | 0:b9164b348919 | 182 | const uint8_t MicroBitEventServiceMicroBitRequirementsCharacteristicUUID[] = { |
MrBedfordVan | 0:b9164b348919 | 183 | 0xe9,0x5d,0xb8,0x4c,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8 |
MrBedfordVan | 0:b9164b348919 | 184 | }; |
MrBedfordVan | 0:b9164b348919 | 185 | |
MrBedfordVan | 0:b9164b348919 | 186 | const uint8_t MicroBitEventServiceClientRequirementsCharacteristicUUID[] = { |
MrBedfordVan | 0:b9164b348919 | 187 | 0xe9,0x5d,0x23,0xc4,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8 |
MrBedfordVan | 0:b9164b348919 | 188 | }; |