Damien Abos / microbit-dal

Dependencies:   BLE_API mbed-dev-bin nRF51822

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MicroBitBLEManager.h Source File

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 "MicroBitValueService.h"
00056 #include "MicroBitAccelerometerService.h"
00057 #include "MicroBitMagnetometerService.h"
00058 #include "MicroBitButtonService.h"
00059 #include "MicroBitIOPinService.h"
00060 #include "MicroBitTemperatureService.h"
00061 #include "ExternalEvents.h"
00062 #include "MicroBitButton.h"
00063 #include "MicroBitStorage.h"
00064 
00065 #define MICROBIT_BLE_PAIR_REQUEST               0x01
00066 #define MICROBIT_BLE_PAIR_COMPLETE              0x02
00067 #define MICROBIT_BLE_PAIR_PASSCODE              0x04
00068 #define MICROBIT_BLE_PAIR_SUCCESSFUL            0x08
00069 
00070 #define MICROBIT_BLE_PAIRING_TIMEOUT            90
00071 #define MICROBIT_BLE_POWER_LEVELS               8
00072 #define MICROBIT_BLE_MAXIMUM_BONDS              4
00073 #define MICROBIT_BLE_ENABLE_BONDING             true
00074 
00075 extern const int8_t MICROBIT_BLE_POWER_LEVEL[];
00076 
00077 struct BLESysAttribute
00078 {
00079     uint8_t         sys_attr[8];
00080 };
00081 
00082 struct BLESysAttributeStore
00083 {
00084     BLESysAttribute sys_attrs[MICROBIT_BLE_MAXIMUM_BONDS];
00085 };
00086 
00087 /**
00088   * Class definition for the MicroBitBLEManager.
00089   *
00090   */
00091 class MicroBitBLEManager : MicroBitComponent
00092 {
00093     public:
00094 
00095     // The mbed abstraction of the BlueTooth Low Energy (BLE) hardware
00096     BLEDevice                       *ble;
00097 
00098     //an instance of MicroBitStorage used to store sysAttrs from softdevice
00099     MicroBitStorage* storage;
00100 
00101     /**
00102      * Constructor.
00103      *
00104      * Configure and manage the micro:bit's Bluetooth Low Energy (BLE) stack.
00105      *
00106      * @param _storage an instance of MicroBitStorage used to persist sys attribute information. (This is required for compatability with iOS).
00107      *
00108      * @note The BLE stack *cannot*  be brought up in a static context (the software simply hangs or corrupts itself).
00109      * Hence, the init() member function should be used to initialise the BLE stack.
00110      */
00111     MicroBitBLEManager(MicroBitStorage& _storage);
00112 
00113     /**
00114      * Constructor.
00115      *
00116      * Configure and manage the micro:bit's Bluetooth Low Energy (BLE) stack.
00117      *
00118      * @note The BLE stack *cannot*  be brought up in a static context (the software simply hangs or corrupts itself).
00119      * Hence, the init() member function should be used to initialise the BLE stack.
00120      */
00121     MicroBitBLEManager();
00122 
00123     /**
00124       * Post constructor initialisation method as the BLE stack cannot be brought
00125       * up in a static context.
00126       *
00127       * @param deviceName The name used when advertising
00128       * @param serialNumber The serial number exposed by the device information service
00129       * @param messageBus An instance of an EventModel, used during pairing.
00130       * @param enableBonding If true, the security manager enabled bonding.
00131       *
00132       * @code
00133       * bleManager.init(uBit.getName(), uBit.getSerial(), uBit.messageBus, true);
00134       * @endcode
00135       */
00136     void init(ManagedString deviceName, ManagedString serialNumber, EventModel& messageBus, bool enableBonding);
00137 
00138     /**
00139      * Change the output power level of the transmitter to the given value.
00140      *
00141      * @param power a value in the range 0..7, where 0 is the lowest power and 7 is the highest.
00142      *
00143      * @return MICROBIT_OK on success, or MICROBIT_INVALID_PARAMETER if the value is out of range.
00144      *
00145      * @code
00146      * // maximum transmission power.
00147      * bleManager.setTransmitPower(7);
00148      * @endcode
00149      */
00150     int setTransmitPower(int power);
00151 
00152     /**
00153      * Enter pairing mode. This is mode is called to initiate pairing, and to enable FOTA programming
00154      * of the micro:bit in cases where BLE is disabled during normal operation.
00155      *
00156      * @param display An instance of MicroBitDisplay used when displaying pairing information.
00157      * @param authorizationButton The button to use to authorise a pairing request.
00158      *
00159      * @code
00160      * // initiate pairing mode
00161      * bleManager.pairingMode(uBit.display, uBit.buttonA);
00162      * @endcode
00163      */
00164     void pairingMode(MicroBitDisplay &display, MicroBitButton &authorisationButton);
00165 
00166     /**
00167      * When called, the micro:bit will begin advertising for a predefined period,
00168      * MICROBIT_BLE_ADVERTISING_TIMEOUT seconds to bonded devices.
00169      */
00170     void advertise();
00171 
00172     /**
00173      * Determines the number of devices currently bonded with this micro:bit.
00174      * @return The number of active bonds.
00175      */
00176     int getBondCount();
00177 
00178     /**
00179      * A request to pair has been received from a BLE device.
00180      * If we're in pairing mode, display the passkey to the user.
00181      * Also, purge the bonding table if it has reached capacity.
00182      *
00183      * @note for internal use only.
00184      */
00185     void pairingRequested(ManagedString passKey);
00186 
00187     /**
00188      * A pairing request has been sucessfully completed.
00189      * If we're in pairing mode, display a success or failure message.
00190      *
00191      * @note for internal use only.
00192      */
00193     void pairingComplete(bool success);
00194 
00195     /**
00196      * Periodic callback in thread context.
00197      * We use this here purely to safely issue a disconnect operation after a pairing operation is complete.
00198      */
00199     void idleTick();
00200 
00201     private:
00202 
00203     /**
00204      * Displays the device's ID code as a histogram on the provided MicroBitDisplay instance.
00205      *
00206      * @param display The display instance used for displaying the histogram.
00207      */
00208     void showNameHistogram(MicroBitDisplay &display);
00209 
00210     int             pairingStatus;
00211     ManagedString   passKey;
00212     ManagedString   deviceName;
00213 
00214 };
00215 
00216 #endif