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