This is for Bluetooth World 2018

Dependencies:   BLE_API mbed-dev-bin nRF51822

Fork of microbit-dal by Lancaster University

Committer:
krenbluetoothsig
Date:
Mon Aug 06 08:39:40 2018 +0000
Revision:
76:23105cbbad61
Parent:
3:d86a4ddc1867
1. NEW: Add #include "MicroBitAnimationService.h" to MicroBitBLEManager.h;  *;  * In MicroBitConfig.h:;  *   #define MICROBIT_BLE_OPEN                       1;  *   #define MICROBIT_SD_GATT_TABLE_SIZE             0x500

Who changed what in which revision?

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