Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BLE_API mbed-dev-bin nRF51822-bluetooth-mdw
Fork of microbit-dal-bluetooth-mdw by
MicroBitBLEManager.h
00001 /* 00002 The MIT License (MIT) 00003 00004 Copyright (c) 2016 British Broadcasting Corporation. 00005 This software is provided by Lancaster University by arrangement with the BBC. 00006 00007 Permission is hereby granted, free of charge, to any person obtaining a 00008 copy of this software and associated documentation files (the "Software"), 00009 to deal in the Software without restriction, including without limitation 00010 the rights to use, copy, modify, merge, publish, distribute, sublicense, 00011 and/or sell copies of the Software, and to permit persons to whom the 00012 Software is furnished to do so, subject to the following conditions: 00013 00014 The above copyright notice and this permission notice shall be included in 00015 all copies or substantial portions of the Software. 00016 00017 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00018 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00019 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 00020 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00021 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 00022 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 00023 DEALINGS IN THE SOFTWARE. 00024 */ 00025 00026 #ifndef MICROBIT_BLE_MANAGER_H 00027 #define MICROBIT_BLE_MANAGER_H 00028 00029 #include "mbed.h" 00030 #include "MicroBitConfig.h" 00031 00032 /* 00033 * The underlying Nordic libraries that support BLE do not compile cleanly with the stringent GCC settings we employ 00034 * If we're compiling under GCC, then we suppress any warnings generated from this code (but not the rest of the DAL) 00035 * The ARM cc compiler is more tolerant. We don't test __GNUC__ here to detect GCC as ARMCC also typically sets this 00036 * as a compatability option, but does not support the options used... 00037 */ 00038 #if !defined (__arm) 00039 #pragma GCC diagnostic push 00040 #pragma GCC diagnostic ignored "-Wunused-parameter" 00041 #endif 00042 #include "ble/BLE.h" 00043 00044 /* 00045 * Return to our predefined compiler settings. 00046 */ 00047 #if !defined (__arm) 00048 #pragma GCC diagnostic pop 00049 #endif 00050 00051 #include "ble/services/DeviceInformationService.h" 00052 #include "MicroBitDFUService.h" 00053 #include "MicroBitEventService.h" 00054 #include "MicroBitLEDService.h" 00055 #include "MicroBitAccelerometerService.h" 00056 #include "MicroBitMagnetometerService.h" 00057 #include "MicroBitButtonService.h" 00058 #include "MicroBitIOPinService.h" 00059 #include "MicroBitTemperatureService.h" 00060 #include "ExternalEvents.h" 00061 #include "MicroBitButton.h" 00062 #include "MicroBitStorage.h" 00063 00064 #include "MicroBitAnimationService.h" 00065 00066 #define MICROBIT_BLE_PAIR_REQUEST 0x01 00067 #define MICROBIT_BLE_PAIR_COMPLETE 0x02 00068 #define MICROBIT_BLE_PAIR_PASSCODE 0x04 00069 #define MICROBIT_BLE_PAIR_SUCCESSFUL 0x08 00070 00071 #define MICROBIT_BLE_PAIRING_TIMEOUT 90 00072 #define MICROBIT_BLE_POWER_LEVELS 8 00073 #define MICROBIT_BLE_MAXIMUM_BONDS 4 00074 #define MICROBIT_BLE_ENABLE_BONDING true 00075 00076 extern const int8_t MICROBIT_BLE_POWER_LEVEL[]; 00077 00078 struct BLESysAttribute 00079 { 00080 uint8_t sys_attr[8]; 00081 }; 00082 00083 struct BLESysAttributeStore 00084 { 00085 BLESysAttribute sys_attrs[MICROBIT_BLE_MAXIMUM_BONDS]; 00086 }; 00087 00088 /** 00089 * Class definition for the MicroBitBLEManager. 00090 * 00091 */ 00092 class MicroBitBLEManager : MicroBitComponent 00093 { 00094 public: 00095 00096 // The mbed abstraction of the BlueTooth Low Energy (BLE) hardware 00097 BLEDevice *ble; 00098 00099 //an instance of MicroBitStorage used to store sysAttrs from softdevice 00100 MicroBitStorage* storage; 00101 00102 /** 00103 * Constructor. 00104 * 00105 * Configure and manage the micro:bit's Bluetooth Low Energy (BLE) stack. 00106 * 00107 * @param _storage an instance of MicroBitStorage used to persist sys attribute information. (This is required for compatability with iOS). 00108 * 00109 * @note The BLE stack *cannot* be brought up in a static context (the software simply hangs or corrupts itself). 00110 * Hence, the init() member function should be used to initialise the BLE stack. 00111 */ 00112 MicroBitBLEManager(MicroBitStorage& _storage); 00113 00114 /** 00115 * Constructor. 00116 * 00117 * Configure and manage the micro:bit's Bluetooth Low Energy (BLE) stack. 00118 * 00119 * @note The BLE stack *cannot* be brought up in a static context (the software simply hangs or corrupts itself). 00120 * Hence, the init() member function should be used to initialise the BLE stack. 00121 */ 00122 MicroBitBLEManager(); 00123 00124 /** 00125 * Post constructor initialisation method as the BLE stack cannot be brought 00126 * up in a static context. 00127 * 00128 * @param deviceName The name used when advertising 00129 * @param serialNumber The serial number exposed by the device information service 00130 * @param messageBus An instance of an EventModel, used during pairing. 00131 * @param enableBonding If true, the security manager enabled bonding. 00132 * 00133 * @code 00134 * bleManager.init(uBit.getName(), uBit.getSerial(), uBit.messageBus, true); 00135 * @endcode 00136 */ 00137 void init(ManagedString deviceName, ManagedString serialNumber, EventModel& messageBus, bool enableBonding); 00138 00139 /** 00140 * Change the output power level of the transmitter to the given value. 00141 * 00142 * @param power a value in the range 0..7, where 0 is the lowest power and 7 is the highest. 00143 * 00144 * @return MICROBIT_OK on success, or MICROBIT_INVALID_PARAMETER if the value is out of range. 00145 * 00146 * @code 00147 * // maximum transmission power. 00148 * bleManager.setTransmitPower(7); 00149 * @endcode 00150 */ 00151 int setTransmitPower(int power); 00152 00153 /** 00154 * Enter pairing mode. This is mode is called to initiate pairing, and to enable FOTA programming 00155 * of the micro:bit in cases where BLE is disabled during normal operation. 00156 * 00157 * @param display An instance of MicroBitDisplay used when displaying pairing information. 00158 * @param authorizationButton The button to use to authorise a pairing request. 00159 * 00160 * @code 00161 * // initiate pairing mode 00162 * bleManager.pairingMode(uBit.display, uBit.buttonA); 00163 * @endcode 00164 */ 00165 void pairingMode(MicroBitDisplay &display, MicroBitButton &authorisationButton); 00166 00167 /** 00168 * When called, the micro:bit will begin advertising for a predefined period, 00169 * MICROBIT_BLE_ADVERTISING_TIMEOUT seconds to bonded devices. 00170 */ 00171 void advertise(); 00172 00173 /** 00174 * Determines the number of devices currently bonded with this micro:bit. 00175 * @return The number of active bonds. 00176 */ 00177 int getBondCount(); 00178 00179 /** 00180 * A request to pair has been received from a BLE device. 00181 * If we're in pairing mode, display the passkey to the user. 00182 * Also, purge the bonding table if it has reached capacity. 00183 * 00184 * @note for internal use only. 00185 */ 00186 void pairingRequested(ManagedString passKey); 00187 00188 /** 00189 * A pairing request has been sucessfully completed. 00190 * If we're in pairing mode, display a success or failure message. 00191 * 00192 * @note for internal use only. 00193 */ 00194 void pairingComplete(bool success); 00195 00196 /** 00197 * Periodic callback in thread context. 00198 * We use this here purely to safely issue a disconnect operation after a pairing operation is complete. 00199 */ 00200 void idleTick(); 00201 00202 private: 00203 00204 /** 00205 * Displays the device's ID code as a histogram on the provided MicroBitDisplay instance. 00206 * 00207 * @param display The display instance used for displaying the histogram. 00208 */ 00209 void showNameHistogram(MicroBitDisplay &display); 00210 00211 int pairingStatus; 00212 ManagedString passKey; 00213 ManagedString deviceName; 00214 00215 }; 00216 00217 #endif
Generated on Tue Jul 12 2022 12:45:45 by
1.7.2
