Official Sheffield ARMBand micro:bit program
microbit/microbit-dal/inc/bluetooth/MicroBitBLEManager.h@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 | #ifndef MICROBIT_BLE_MANAGER_H |
MrBedfordVan | 0:b9164b348919 | 27 | #define MICROBIT_BLE_MANAGER_H |
MrBedfordVan | 0:b9164b348919 | 28 | |
MrBedfordVan | 0:b9164b348919 | 29 | #include "mbed.h" |
MrBedfordVan | 0:b9164b348919 | 30 | #include "MicroBitConfig.h" |
MrBedfordVan | 0:b9164b348919 | 31 | |
MrBedfordVan | 0:b9164b348919 | 32 | /* |
MrBedfordVan | 0:b9164b348919 | 33 | * The underlying Nordic libraries that support BLE do not compile cleanly with the stringent GCC settings we employ |
MrBedfordVan | 0:b9164b348919 | 34 | * If we're compiling under GCC, then we suppress any warnings generated from this code (but not the rest of the DAL) |
MrBedfordVan | 0:b9164b348919 | 35 | * The ARM cc compiler is more tolerant. We don't test __GNUC__ here to detect GCC as ARMCC also typically sets this |
MrBedfordVan | 0:b9164b348919 | 36 | * as a compatability option, but does not support the options used... |
MrBedfordVan | 0:b9164b348919 | 37 | */ |
MrBedfordVan | 0:b9164b348919 | 38 | #if !defined (__arm) |
MrBedfordVan | 0:b9164b348919 | 39 | #pragma GCC diagnostic push |
MrBedfordVan | 0:b9164b348919 | 40 | #pragma GCC diagnostic ignored "-Wunused-parameter" |
MrBedfordVan | 0:b9164b348919 | 41 | #endif |
MrBedfordVan | 0:b9164b348919 | 42 | #include "ble/BLE.h" |
MrBedfordVan | 0:b9164b348919 | 43 | |
MrBedfordVan | 0:b9164b348919 | 44 | /* |
MrBedfordVan | 0:b9164b348919 | 45 | * Return to our predefined compiler settings. |
MrBedfordVan | 0:b9164b348919 | 46 | */ |
MrBedfordVan | 0:b9164b348919 | 47 | #if !defined (__arm) |
MrBedfordVan | 0:b9164b348919 | 48 | #pragma GCC diagnostic pop |
MrBedfordVan | 0:b9164b348919 | 49 | #endif |
MrBedfordVan | 0:b9164b348919 | 50 | |
MrBedfordVan | 0:b9164b348919 | 51 | #include "ble/services/DeviceInformationService.h" |
MrBedfordVan | 0:b9164b348919 | 52 | #include "MicroBitDFUService.h" |
MrBedfordVan | 0:b9164b348919 | 53 | #include "MicroBitEventService.h" |
MrBedfordVan | 0:b9164b348919 | 54 | #include "MicroBitLEDService.h" |
MrBedfordVan | 0:b9164b348919 | 55 | #include "MicroBitAccelerometerService.h" |
MrBedfordVan | 0:b9164b348919 | 56 | #include "MicroBitMagnetometerService.h" |
MrBedfordVan | 0:b9164b348919 | 57 | #include "MicroBitButtonService.h" |
MrBedfordVan | 0:b9164b348919 | 58 | #include "MicroBitIOPinService.h" |
MrBedfordVan | 0:b9164b348919 | 59 | #include "MicroBitTemperatureService.h" |
MrBedfordVan | 0:b9164b348919 | 60 | #include "ExternalEvents.h" |
MrBedfordVan | 0:b9164b348919 | 61 | #include "MicroBitButton.h" |
MrBedfordVan | 0:b9164b348919 | 62 | #include "MicroBitStorage.h" |
MrBedfordVan | 0:b9164b348919 | 63 | |
MrBedfordVan | 0:b9164b348919 | 64 | #define MICROBIT_BLE_PAIR_REQUEST 0x01 |
MrBedfordVan | 0:b9164b348919 | 65 | #define MICROBIT_BLE_PAIR_COMPLETE 0x02 |
MrBedfordVan | 0:b9164b348919 | 66 | #define MICROBIT_BLE_PAIR_PASSCODE 0x04 |
MrBedfordVan | 0:b9164b348919 | 67 | #define MICROBIT_BLE_PAIR_SUCCESSFUL 0x08 |
MrBedfordVan | 0:b9164b348919 | 68 | |
MrBedfordVan | 0:b9164b348919 | 69 | #define MICROBIT_BLE_PAIRING_TIMEOUT 90 |
MrBedfordVan | 0:b9164b348919 | 70 | #define MICROBIT_BLE_POWER_LEVELS 8 |
MrBedfordVan | 0:b9164b348919 | 71 | #define MICROBIT_BLE_MAXIMUM_BONDS 4 |
MrBedfordVan | 0:b9164b348919 | 72 | #define MICROBIT_BLE_ENABLE_BONDING true |
MrBedfordVan | 0:b9164b348919 | 73 | |
MrBedfordVan | 0:b9164b348919 | 74 | extern const int8_t MICROBIT_BLE_POWER_LEVEL[]; |
MrBedfordVan | 0:b9164b348919 | 75 | |
MrBedfordVan | 0:b9164b348919 | 76 | struct BLESysAttribute |
MrBedfordVan | 0:b9164b348919 | 77 | { |
MrBedfordVan | 0:b9164b348919 | 78 | uint8_t sys_attr[8]; |
MrBedfordVan | 0:b9164b348919 | 79 | }; |
MrBedfordVan | 0:b9164b348919 | 80 | |
MrBedfordVan | 0:b9164b348919 | 81 | struct BLESysAttributeStore |
MrBedfordVan | 0:b9164b348919 | 82 | { |
MrBedfordVan | 0:b9164b348919 | 83 | BLESysAttribute sys_attrs[MICROBIT_BLE_MAXIMUM_BONDS]; |
MrBedfordVan | 0:b9164b348919 | 84 | }; |
MrBedfordVan | 0:b9164b348919 | 85 | |
MrBedfordVan | 0:b9164b348919 | 86 | /** |
MrBedfordVan | 0:b9164b348919 | 87 | * Class definition for the MicroBitBLEManager. |
MrBedfordVan | 0:b9164b348919 | 88 | * |
MrBedfordVan | 0:b9164b348919 | 89 | */ |
MrBedfordVan | 0:b9164b348919 | 90 | class MicroBitBLEManager : MicroBitComponent |
MrBedfordVan | 0:b9164b348919 | 91 | { |
MrBedfordVan | 0:b9164b348919 | 92 | public: |
MrBedfordVan | 0:b9164b348919 | 93 | |
MrBedfordVan | 0:b9164b348919 | 94 | // The mbed abstraction of the BlueTooth Low Energy (BLE) hardware |
MrBedfordVan | 0:b9164b348919 | 95 | BLEDevice *ble; |
MrBedfordVan | 0:b9164b348919 | 96 | |
MrBedfordVan | 0:b9164b348919 | 97 | //an instance of MicroBitStorage used to store sysAttrs from softdevice |
MrBedfordVan | 0:b9164b348919 | 98 | MicroBitStorage* storage; |
MrBedfordVan | 0:b9164b348919 | 99 | |
MrBedfordVan | 0:b9164b348919 | 100 | /** |
MrBedfordVan | 0:b9164b348919 | 101 | * Constructor. |
MrBedfordVan | 0:b9164b348919 | 102 | * |
MrBedfordVan | 0:b9164b348919 | 103 | * Configure and manage the micro:bit's Bluetooth Low Energy (BLE) stack. |
MrBedfordVan | 0:b9164b348919 | 104 | * |
MrBedfordVan | 0:b9164b348919 | 105 | * @param _storage an instance of MicroBitStorage used to persist sys attribute information. (This is required for compatability with iOS). |
MrBedfordVan | 0:b9164b348919 | 106 | * |
MrBedfordVan | 0:b9164b348919 | 107 | * @note The BLE stack *cannot* be brought up in a static context (the software simply hangs or corrupts itself). |
MrBedfordVan | 0:b9164b348919 | 108 | * Hence, the init() member function should be used to initialise the BLE stack. |
MrBedfordVan | 0:b9164b348919 | 109 | */ |
MrBedfordVan | 0:b9164b348919 | 110 | MicroBitBLEManager(MicroBitStorage& _storage); |
MrBedfordVan | 0:b9164b348919 | 111 | |
MrBedfordVan | 0:b9164b348919 | 112 | /** |
MrBedfordVan | 0:b9164b348919 | 113 | * Constructor. |
MrBedfordVan | 0:b9164b348919 | 114 | * |
MrBedfordVan | 0:b9164b348919 | 115 | * Configure and manage the micro:bit's Bluetooth Low Energy (BLE) stack. |
MrBedfordVan | 0:b9164b348919 | 116 | * |
MrBedfordVan | 0:b9164b348919 | 117 | * @note The BLE stack *cannot* be brought up in a static context (the software simply hangs or corrupts itself). |
MrBedfordVan | 0:b9164b348919 | 118 | * Hence, the init() member function should be used to initialise the BLE stack. |
MrBedfordVan | 0:b9164b348919 | 119 | */ |
MrBedfordVan | 0:b9164b348919 | 120 | MicroBitBLEManager(); |
MrBedfordVan | 0:b9164b348919 | 121 | |
MrBedfordVan | 0:b9164b348919 | 122 | /** |
MrBedfordVan | 0:b9164b348919 | 123 | * Post constructor initialisation method as the BLE stack cannot be brought |
MrBedfordVan | 0:b9164b348919 | 124 | * up in a static context. |
MrBedfordVan | 0:b9164b348919 | 125 | * |
MrBedfordVan | 0:b9164b348919 | 126 | * @param deviceName The name used when advertising |
MrBedfordVan | 0:b9164b348919 | 127 | * @param serialNumber The serial number exposed by the device information service |
MrBedfordVan | 0:b9164b348919 | 128 | * @param messageBus An instance of an EventModel, used during pairing. |
MrBedfordVan | 0:b9164b348919 | 129 | * @param enableBonding If true, the security manager enabled bonding. |
MrBedfordVan | 0:b9164b348919 | 130 | * |
MrBedfordVan | 0:b9164b348919 | 131 | * @code |
MrBedfordVan | 0:b9164b348919 | 132 | * bleManager.init(uBit.getName(), uBit.getSerial(), uBit.messageBus, true); |
MrBedfordVan | 0:b9164b348919 | 133 | * @endcode |
MrBedfordVan | 0:b9164b348919 | 134 | */ |
MrBedfordVan | 0:b9164b348919 | 135 | void init(ManagedString deviceName, ManagedString serialNumber, EventModel& messageBus, bool enableBonding); |
MrBedfordVan | 0:b9164b348919 | 136 | |
MrBedfordVan | 0:b9164b348919 | 137 | /** |
MrBedfordVan | 0:b9164b348919 | 138 | * Change the output power level of the transmitter to the given value. |
MrBedfordVan | 0:b9164b348919 | 139 | * |
MrBedfordVan | 0:b9164b348919 | 140 | * @param power a value in the range 0..7, where 0 is the lowest power and 7 is the highest. |
MrBedfordVan | 0:b9164b348919 | 141 | * |
MrBedfordVan | 0:b9164b348919 | 142 | * @return MICROBIT_OK on success, or MICROBIT_INVALID_PARAMETER if the value is out of range. |
MrBedfordVan | 0:b9164b348919 | 143 | * |
MrBedfordVan | 0:b9164b348919 | 144 | * @code |
MrBedfordVan | 0:b9164b348919 | 145 | * // maximum transmission power. |
MrBedfordVan | 0:b9164b348919 | 146 | * bleManager.setTransmitPower(7); |
MrBedfordVan | 0:b9164b348919 | 147 | * @endcode |
MrBedfordVan | 0:b9164b348919 | 148 | */ |
MrBedfordVan | 0:b9164b348919 | 149 | int setTransmitPower(int power); |
MrBedfordVan | 0:b9164b348919 | 150 | |
MrBedfordVan | 0:b9164b348919 | 151 | /** |
MrBedfordVan | 0:b9164b348919 | 152 | * Enter pairing mode. This is mode is called to initiate pairing, and to enable FOTA programming |
MrBedfordVan | 0:b9164b348919 | 153 | * of the micro:bit in cases where BLE is disabled during normal operation. |
MrBedfordVan | 0:b9164b348919 | 154 | * |
MrBedfordVan | 0:b9164b348919 | 155 | * @param display An instance of MicroBitDisplay used when displaying pairing information. |
MrBedfordVan | 0:b9164b348919 | 156 | * @param authorizationButton The button to use to authorise a pairing request. |
MrBedfordVan | 0:b9164b348919 | 157 | * |
MrBedfordVan | 0:b9164b348919 | 158 | * @code |
MrBedfordVan | 0:b9164b348919 | 159 | * // initiate pairing mode |
MrBedfordVan | 0:b9164b348919 | 160 | * bleManager.pairingMode(uBit.display, uBit.buttonA); |
MrBedfordVan | 0:b9164b348919 | 161 | * @endcode |
MrBedfordVan | 0:b9164b348919 | 162 | */ |
MrBedfordVan | 0:b9164b348919 | 163 | void pairingMode(MicroBitDisplay &display, MicroBitButton &authorisationButton); |
MrBedfordVan | 0:b9164b348919 | 164 | |
MrBedfordVan | 0:b9164b348919 | 165 | /** |
MrBedfordVan | 0:b9164b348919 | 166 | * When called, the micro:bit will begin advertising for a predefined period, |
MrBedfordVan | 0:b9164b348919 | 167 | * MICROBIT_BLE_ADVERTISING_TIMEOUT seconds to bonded devices. |
MrBedfordVan | 0:b9164b348919 | 168 | */ |
MrBedfordVan | 0:b9164b348919 | 169 | void advertise(); |
MrBedfordVan | 0:b9164b348919 | 170 | |
MrBedfordVan | 0:b9164b348919 | 171 | /** |
MrBedfordVan | 0:b9164b348919 | 172 | * Determines the number of devices currently bonded with this micro:bit. |
MrBedfordVan | 0:b9164b348919 | 173 | * @return The number of active bonds. |
MrBedfordVan | 0:b9164b348919 | 174 | */ |
MrBedfordVan | 0:b9164b348919 | 175 | int getBondCount(); |
MrBedfordVan | 0:b9164b348919 | 176 | |
MrBedfordVan | 0:b9164b348919 | 177 | /** |
MrBedfordVan | 0:b9164b348919 | 178 | * A request to pair has been received from a BLE device. |
MrBedfordVan | 0:b9164b348919 | 179 | * If we're in pairing mode, display the passkey to the user. |
MrBedfordVan | 0:b9164b348919 | 180 | * Also, purge the bonding table if it has reached capacity. |
MrBedfordVan | 0:b9164b348919 | 181 | * |
MrBedfordVan | 0:b9164b348919 | 182 | * @note for internal use only. |
MrBedfordVan | 0:b9164b348919 | 183 | */ |
MrBedfordVan | 0:b9164b348919 | 184 | void pairingRequested(ManagedString passKey); |
MrBedfordVan | 0:b9164b348919 | 185 | |
MrBedfordVan | 0:b9164b348919 | 186 | /** |
MrBedfordVan | 0:b9164b348919 | 187 | * A pairing request has been sucessfully completed. |
MrBedfordVan | 0:b9164b348919 | 188 | * If we're in pairing mode, display a success or failure message. |
MrBedfordVan | 0:b9164b348919 | 189 | * |
MrBedfordVan | 0:b9164b348919 | 190 | * @note for internal use only. |
MrBedfordVan | 0:b9164b348919 | 191 | */ |
MrBedfordVan | 0:b9164b348919 | 192 | void pairingComplete(bool success); |
MrBedfordVan | 0:b9164b348919 | 193 | |
MrBedfordVan | 0:b9164b348919 | 194 | /** |
MrBedfordVan | 0:b9164b348919 | 195 | * Periodic callback in thread context. |
MrBedfordVan | 0:b9164b348919 | 196 | * We use this here purely to safely issue a disconnect operation after a pairing operation is complete. |
MrBedfordVan | 0:b9164b348919 | 197 | */ |
MrBedfordVan | 0:b9164b348919 | 198 | void idleTick(); |
MrBedfordVan | 0:b9164b348919 | 199 | |
MrBedfordVan | 0:b9164b348919 | 200 | private: |
MrBedfordVan | 0:b9164b348919 | 201 | |
MrBedfordVan | 0:b9164b348919 | 202 | /** |
MrBedfordVan | 0:b9164b348919 | 203 | * Displays the device's ID code as a histogram on the provided MicroBitDisplay instance. |
MrBedfordVan | 0:b9164b348919 | 204 | * |
MrBedfordVan | 0:b9164b348919 | 205 | * @param display The display instance used for displaying the histogram. |
MrBedfordVan | 0:b9164b348919 | 206 | */ |
MrBedfordVan | 0:b9164b348919 | 207 | void showNameHistogram(MicroBitDisplay &display); |
MrBedfordVan | 0:b9164b348919 | 208 | |
MrBedfordVan | 0:b9164b348919 | 209 | int pairingStatus; |
MrBedfordVan | 0:b9164b348919 | 210 | ManagedString passKey; |
MrBedfordVan | 0:b9164b348919 | 211 | ManagedString deviceName; |
MrBedfordVan | 0:b9164b348919 | 212 | |
MrBedfordVan | 0:b9164b348919 | 213 | }; |
MrBedfordVan | 0:b9164b348919 | 214 | |
MrBedfordVan | 0:b9164b348919 | 215 | #endif |