My fork

Dependencies:   BLE_API mbed-dev-bin nRF51822-bluetooth-mdw

Fork of microbit-dal-bluetooth-mdw by Martin Woolley

Committer:
bluetooth_mdw
Date:
Thu Jan 05 10:29:03 2017 +0000
Revision:
79:ab48b2043312
Parent:
74:9771cd712730
Added externs to header file

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"
Jonathan Austin 1:8aa5cdb4ab67 63
bluetooth_mdw 74:9771cd712730 64 #include "MicroBitAnimationService.h"
bluetooth_mdw 74:9771cd712730 65
Jonathan Austin 1:8aa5cdb4ab67 66 #define MICROBIT_BLE_PAIR_REQUEST 0x01
Jonathan Austin 1:8aa5cdb4ab67 67 #define MICROBIT_BLE_PAIR_COMPLETE 0x02
Jonathan Austin 1:8aa5cdb4ab67 68 #define MICROBIT_BLE_PAIR_PASSCODE 0x04
Jonathan Austin 1:8aa5cdb4ab67 69 #define MICROBIT_BLE_PAIR_SUCCESSFUL 0x08
Jonathan Austin 1:8aa5cdb4ab67 70
Jonathan Austin 1:8aa5cdb4ab67 71 #define MICROBIT_BLE_PAIRING_TIMEOUT 90
Jonathan Austin 1:8aa5cdb4ab67 72 #define MICROBIT_BLE_POWER_LEVELS 8
Jonathan Austin 1:8aa5cdb4ab67 73 #define MICROBIT_BLE_MAXIMUM_BONDS 4
Jonathan Austin 1:8aa5cdb4ab67 74 #define MICROBIT_BLE_ENABLE_BONDING true
Jonathan Austin 1:8aa5cdb4ab67 75
Jonathan Austin 1:8aa5cdb4ab67 76 extern const int8_t MICROBIT_BLE_POWER_LEVEL[];
Jonathan Austin 1:8aa5cdb4ab67 77
Jonathan Austin 1:8aa5cdb4ab67 78 struct BLESysAttribute
Jonathan Austin 1:8aa5cdb4ab67 79 {
LancasterUniversity 3:d86a4ddc1867 80 uint8_t sys_attr[8];
Jonathan Austin 1:8aa5cdb4ab67 81 };
Jonathan Austin 1:8aa5cdb4ab67 82
Jonathan Austin 1:8aa5cdb4ab67 83 struct BLESysAttributeStore
Jonathan Austin 1:8aa5cdb4ab67 84 {
Jonathan Austin 1:8aa5cdb4ab67 85 BLESysAttribute sys_attrs[MICROBIT_BLE_MAXIMUM_BONDS];
Jonathan Austin 1:8aa5cdb4ab67 86 };
Jonathan Austin 1:8aa5cdb4ab67 87
Jonathan Austin 1:8aa5cdb4ab67 88 /**
Jonathan Austin 1:8aa5cdb4ab67 89 * Class definition for the MicroBitBLEManager.
Jonathan Austin 1:8aa5cdb4ab67 90 *
Jonathan Austin 1:8aa5cdb4ab67 91 */
Jonathan Austin 1:8aa5cdb4ab67 92 class MicroBitBLEManager : MicroBitComponent
Jonathan Austin 1:8aa5cdb4ab67 93 {
Jonathan Austin 1:8aa5cdb4ab67 94 public:
Jonathan Austin 1:8aa5cdb4ab67 95
Jonathan Austin 1:8aa5cdb4ab67 96 // The mbed abstraction of the BlueTooth Low Energy (BLE) hardware
Jonathan Austin 1:8aa5cdb4ab67 97 BLEDevice *ble;
Jonathan Austin 1:8aa5cdb4ab67 98
Jonathan Austin 1:8aa5cdb4ab67 99 //an instance of MicroBitStorage used to store sysAttrs from softdevice
Jonathan Austin 1:8aa5cdb4ab67 100 MicroBitStorage* storage;
Jonathan Austin 1:8aa5cdb4ab67 101
Jonathan Austin 1:8aa5cdb4ab67 102 /**
Jonathan Austin 1:8aa5cdb4ab67 103 * Constructor.
Jonathan Austin 1:8aa5cdb4ab67 104 *
Jonathan Austin 1:8aa5cdb4ab67 105 * Configure and manage the micro:bit's Bluetooth Low Energy (BLE) stack.
Jonathan Austin 1:8aa5cdb4ab67 106 *
Jonathan Austin 1:8aa5cdb4ab67 107 * @param _storage an instance of MicroBitStorage used to persist sys attribute information. (This is required for compatability with iOS).
Jonathan Austin 1:8aa5cdb4ab67 108 *
Jonathan Austin 1:8aa5cdb4ab67 109 * @note The BLE stack *cannot* be brought up in a static context (the software simply hangs or corrupts itself).
Jonathan Austin 1:8aa5cdb4ab67 110 * Hence, the init() member function should be used to initialise the BLE stack.
Jonathan Austin 1:8aa5cdb4ab67 111 */
Jonathan Austin 1:8aa5cdb4ab67 112 MicroBitBLEManager(MicroBitStorage& _storage);
Jonathan Austin 1:8aa5cdb4ab67 113
Jonathan Austin 1:8aa5cdb4ab67 114 /**
Jonathan Austin 1:8aa5cdb4ab67 115 * Constructor.
Jonathan Austin 1:8aa5cdb4ab67 116 *
Jonathan Austin 1:8aa5cdb4ab67 117 * Configure and manage the micro:bit's Bluetooth Low Energy (BLE) stack.
Jonathan Austin 1:8aa5cdb4ab67 118 *
Jonathan Austin 1:8aa5cdb4ab67 119 * @note The BLE stack *cannot* be brought up in a static context (the software simply hangs or corrupts itself).
Jonathan Austin 1:8aa5cdb4ab67 120 * Hence, the init() member function should be used to initialise the BLE stack.
Jonathan Austin 1:8aa5cdb4ab67 121 */
Jonathan Austin 1:8aa5cdb4ab67 122 MicroBitBLEManager();
Jonathan Austin 1:8aa5cdb4ab67 123
Jonathan Austin 1:8aa5cdb4ab67 124 /**
Jonathan Austin 1:8aa5cdb4ab67 125 * Post constructor initialisation method as the BLE stack cannot be brought
Jonathan Austin 1:8aa5cdb4ab67 126 * up in a static context.
Jonathan Austin 1:8aa5cdb4ab67 127 *
Jonathan Austin 1:8aa5cdb4ab67 128 * @param deviceName The name used when advertising
Jonathan Austin 1:8aa5cdb4ab67 129 * @param serialNumber The serial number exposed by the device information service
Jonathan Austin 1:8aa5cdb4ab67 130 * @param messageBus An instance of an EventModel, used during pairing.
Jonathan Austin 1:8aa5cdb4ab67 131 * @param enableBonding If true, the security manager enabled bonding.
Jonathan Austin 1:8aa5cdb4ab67 132 *
Jonathan Austin 1:8aa5cdb4ab67 133 * @code
Jonathan Austin 1:8aa5cdb4ab67 134 * bleManager.init(uBit.getName(), uBit.getSerial(), uBit.messageBus, true);
Jonathan Austin 1:8aa5cdb4ab67 135 * @endcode
Jonathan Austin 1:8aa5cdb4ab67 136 */
Jonathan Austin 1:8aa5cdb4ab67 137 void init(ManagedString deviceName, ManagedString serialNumber, EventModel& messageBus, bool enableBonding);
Jonathan Austin 1:8aa5cdb4ab67 138
Jonathan Austin 1:8aa5cdb4ab67 139 /**
Jonathan Austin 1:8aa5cdb4ab67 140 * Change the output power level of the transmitter to the given value.
Jonathan Austin 1:8aa5cdb4ab67 141 *
Jonathan Austin 1:8aa5cdb4ab67 142 * @param power a value in the range 0..7, where 0 is the lowest power and 7 is the highest.
Jonathan Austin 1:8aa5cdb4ab67 143 *
Jonathan Austin 1:8aa5cdb4ab67 144 * @return MICROBIT_OK on success, or MICROBIT_INVALID_PARAMETER if the value is out of range.
Jonathan Austin 1:8aa5cdb4ab67 145 *
Jonathan Austin 1:8aa5cdb4ab67 146 * @code
Jonathan Austin 1:8aa5cdb4ab67 147 * // maximum transmission power.
Jonathan Austin 1:8aa5cdb4ab67 148 * bleManager.setTransmitPower(7);
Jonathan Austin 1:8aa5cdb4ab67 149 * @endcode
Jonathan Austin 1:8aa5cdb4ab67 150 */
Jonathan Austin 1:8aa5cdb4ab67 151 int setTransmitPower(int power);
Jonathan Austin 1:8aa5cdb4ab67 152
Jonathan Austin 1:8aa5cdb4ab67 153 /**
Jonathan Austin 1:8aa5cdb4ab67 154 * Enter pairing mode. This is mode is called to initiate pairing, and to enable FOTA programming
Jonathan Austin 1:8aa5cdb4ab67 155 * of the micro:bit in cases where BLE is disabled during normal operation.
Jonathan Austin 1:8aa5cdb4ab67 156 *
Jonathan Austin 1:8aa5cdb4ab67 157 * @param display An instance of MicroBitDisplay used when displaying pairing information.
Jonathan Austin 1:8aa5cdb4ab67 158 * @param authorizationButton The button to use to authorise a pairing request.
Jonathan Austin 1:8aa5cdb4ab67 159 *
Jonathan Austin 1:8aa5cdb4ab67 160 * @code
Jonathan Austin 1:8aa5cdb4ab67 161 * // initiate pairing mode
Jonathan Austin 1:8aa5cdb4ab67 162 * bleManager.pairingMode(uBit.display, uBit.buttonA);
Jonathan Austin 1:8aa5cdb4ab67 163 * @endcode
Jonathan Austin 1:8aa5cdb4ab67 164 */
Jonathan Austin 1:8aa5cdb4ab67 165 void pairingMode(MicroBitDisplay &display, MicroBitButton &authorisationButton);
Jonathan Austin 1:8aa5cdb4ab67 166
Jonathan Austin 1:8aa5cdb4ab67 167 /**
Jonathan Austin 1:8aa5cdb4ab67 168 * When called, the micro:bit will begin advertising for a predefined period,
Jonathan Austin 1:8aa5cdb4ab67 169 * MICROBIT_BLE_ADVERTISING_TIMEOUT seconds to bonded devices.
Jonathan Austin 1:8aa5cdb4ab67 170 */
Jonathan Austin 1:8aa5cdb4ab67 171 void advertise();
Jonathan Austin 1:8aa5cdb4ab67 172
Jonathan Austin 1:8aa5cdb4ab67 173 /**
Jonathan Austin 1:8aa5cdb4ab67 174 * Determines the number of devices currently bonded with this micro:bit.
Jonathan Austin 1:8aa5cdb4ab67 175 * @return The number of active bonds.
Jonathan Austin 1:8aa5cdb4ab67 176 */
Jonathan Austin 1:8aa5cdb4ab67 177 int getBondCount();
Jonathan Austin 1:8aa5cdb4ab67 178
Jonathan Austin 1:8aa5cdb4ab67 179 /**
Jonathan Austin 1:8aa5cdb4ab67 180 * A request to pair has been received from a BLE device.
Jonathan Austin 1:8aa5cdb4ab67 181 * If we're in pairing mode, display the passkey to the user.
Jonathan Austin 1:8aa5cdb4ab67 182 * Also, purge the bonding table if it has reached capacity.
Jonathan Austin 1:8aa5cdb4ab67 183 *
Jonathan Austin 1:8aa5cdb4ab67 184 * @note for internal use only.
Jonathan Austin 1:8aa5cdb4ab67 185 */
Jonathan Austin 1:8aa5cdb4ab67 186 void pairingRequested(ManagedString passKey);
Jonathan Austin 1:8aa5cdb4ab67 187
Jonathan Austin 1:8aa5cdb4ab67 188 /**
Jonathan Austin 1:8aa5cdb4ab67 189 * A pairing request has been sucessfully completed.
Jonathan Austin 1:8aa5cdb4ab67 190 * If we're in pairing mode, display a success or failure message.
Jonathan Austin 1:8aa5cdb4ab67 191 *
Jonathan Austin 1:8aa5cdb4ab67 192 * @note for internal use only.
Jonathan Austin 1:8aa5cdb4ab67 193 */
Jonathan Austin 1:8aa5cdb4ab67 194 void pairingComplete(bool success);
Jonathan Austin 1:8aa5cdb4ab67 195
Jonathan Austin 1:8aa5cdb4ab67 196 /**
Jonathan Austin 1:8aa5cdb4ab67 197 * Periodic callback in thread context.
Jonathan Austin 1:8aa5cdb4ab67 198 * We use this here purely to safely issue a disconnect operation after a pairing operation is complete.
Jonathan Austin 1:8aa5cdb4ab67 199 */
Jonathan Austin 1:8aa5cdb4ab67 200 void idleTick();
Jonathan Austin 1:8aa5cdb4ab67 201
Jonathan Austin 1:8aa5cdb4ab67 202 private:
Jonathan Austin 1:8aa5cdb4ab67 203
Jonathan Austin 1:8aa5cdb4ab67 204 /**
Jonathan Austin 1:8aa5cdb4ab67 205 * Displays the device's ID code as a histogram on the provided MicroBitDisplay instance.
Jonathan Austin 1:8aa5cdb4ab67 206 *
Jonathan Austin 1:8aa5cdb4ab67 207 * @param display The display instance used for displaying the histogram.
Jonathan Austin 1:8aa5cdb4ab67 208 */
Jonathan Austin 1:8aa5cdb4ab67 209 void showNameHistogram(MicroBitDisplay &display);
Jonathan Austin 1:8aa5cdb4ab67 210
Jonathan Austin 1:8aa5cdb4ab67 211 int pairingStatus;
Jonathan Austin 1:8aa5cdb4ab67 212 ManagedString passKey;
Jonathan Austin 1:8aa5cdb4ab67 213 ManagedString deviceName;
Jonathan Austin 1:8aa5cdb4ab67 214
Jonathan Austin 1:8aa5cdb4ab67 215 };
Jonathan Austin 1:8aa5cdb4ab67 216
LancasterUniversity 3:d86a4ddc1867 217 #endif