Eddystone test using modified DAL

Dependencies:   BLE_API mbed-dev-bin nRF51822

Dependents:   microbit-eddystone

Fork of microbit-dal by Lancaster University

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 "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 #define MICROBIT_BLE_PAIR_REQUEST 0x01
00065 #define MICROBIT_BLE_PAIR_COMPLETE 0x02
00066 #define MICROBIT_BLE_PAIR_PASSCODE 0x04
00067 #define MICROBIT_BLE_PAIR_SUCCESSFUL 0x08
00068 
00069 #define MICROBIT_BLE_PAIRING_TIMEOUT 90
00070 #define MICROBIT_BLE_POWER_LEVELS 8
00071 #define MICROBIT_BLE_MAXIMUM_BONDS 4
00072 #define MICROBIT_BLE_ENABLE_BONDING true
00073 
00074 #define MICROBIT_BLE_EDDYSTONE_ADV_INTERVAL     400
00075 #define MICROBIT_BLE_EDDYSTONE_DEFAULT_POWER    0xF0
00076 
00077 extern const int8_t MICROBIT_BLE_POWER_LEVEL[];
00078 
00079 struct BLESysAttribute
00080 {
00081     uint8_t sys_attr[8];
00082 };
00083 
00084 struct BLESysAttributeStore
00085 {
00086     BLESysAttribute sys_attrs[MICROBIT_BLE_MAXIMUM_BONDS];
00087 };
00088 
00089 /**
00090   * Class definition for the MicroBitBLEManager.
00091   *
00092   */
00093 class MicroBitBLEManager : MicroBitComponent
00094 {
00095   public:
00096     static MicroBitBLEManager *manager;
00097 
00098     // The mbed abstraction of the BlueTooth Low Energy (BLE) hardware
00099     BLEDevice *ble;
00100 
00101     //an instance of MicroBitStorage used to store sysAttrs from softdevice
00102     MicroBitStorage *storage;
00103 
00104     /**
00105      * Constructor.
00106      *
00107      * Configure and manage the micro:bit's Bluetooth Low Energy (BLE) stack.
00108      *
00109      * @param _storage an instance of MicroBitStorage used to persist sys attribute information. (This is required for compatability with iOS).
00110      *
00111      * @note The BLE stack *cannot*  be brought up in a static context (the software simply hangs or corrupts itself).
00112      * Hence, the init() member function should be used to initialise the BLE stack.
00113      */
00114     MicroBitBLEManager(MicroBitStorage &_storage);
00115 
00116     /**
00117      * Constructor.
00118      *
00119      * Configure and manage the micro:bit's Bluetooth Low Energy (BLE) stack.
00120      *
00121      * @note The BLE stack *cannot*  be brought up in a static context (the software simply hangs or corrupts itself).
00122      * Hence, the init() member function should be used to initialise the BLE stack.
00123      */
00124     MicroBitBLEManager();
00125 
00126     /**
00127      * getInstance
00128      *
00129      * Allows other objects to easily obtain a pointer to the single instance of this object. By rights the constructor should be made
00130      * private to properly implement the singleton pattern.
00131      *
00132      */
00133     static MicroBitBLEManager *getInstance();
00134 
00135     /**
00136       * Post constructor initialisation method as the BLE stack cannot be brought
00137       * up in a static context.
00138       *
00139       * @param deviceName The name used when advertising
00140       * @param serialNumber The serial number exposed by the device information service
00141       * @param messageBus An instance of an EventModel, used during pairing.
00142       * @param enableBonding If true, the security manager enabled bonding.
00143       *
00144       * @code
00145       * bleManager.init(uBit.getName(), uBit.getSerial(), uBit.messageBus, true);
00146       * @endcode
00147       */
00148     void init(ManagedString deviceName, ManagedString serialNumber, EventModel &messageBus, bool enableBonding);
00149 
00150     /**
00151      * Change the output power level of the transmitter to the given value.
00152      *
00153      * @param power a value in the range 0..7, where 0 is the lowest power and 7 is the highest.
00154      *
00155      * @return MICROBIT_OK on success, or MICROBIT_INVALID_PARAMETER if the value is out of range.
00156      *
00157      * @code
00158      * // maximum transmission power.
00159      * bleManager.setTransmitPower(7);
00160      * @endcode
00161      */
00162     int setTransmitPower(int power);
00163 
00164     /**
00165      * Enter pairing mode. This is mode is called to initiate pairing, and to enable FOTA programming
00166      * of the micro:bit in cases where BLE is disabled during normal operation.
00167      *
00168      * @param display An instance of MicroBitDisplay used when displaying pairing information.
00169      * @param authorizationButton The button to use to authorise a pairing request.
00170      *
00171      * @code
00172      * // initiate pairing mode
00173      * bleManager.pairingMode(uBit.display, uBit.buttonA);
00174      * @endcode
00175      */
00176     void pairingMode(MicroBitDisplay &display, MicroBitButton &authorisationButton);
00177 
00178     /**
00179      * When called, the micro:bit will begin advertising for a predefined period,
00180      * MICROBIT_BLE_ADVERTISING_TIMEOUT seconds to bonded devices.
00181      */
00182     void advertise();
00183 
00184     /**
00185      * Determines the number of devices currently bonded with this micro:bit.
00186      * @return The number of active bonds.
00187      */
00188     int getBondCount();
00189 
00190     /**
00191      * A request to pair has been received from a BLE device.
00192      * If we're in pairing mode, display the passkey to the user.
00193      * Also, purge the bonding table if it has reached capacity.
00194      *
00195      * @note for internal use only.
00196      */
00197     void pairingRequested(ManagedString passKey);
00198 
00199     /**
00200      * A pairing request has been sucessfully completed.
00201      * If we're in pairing mode, display a success or failure message.
00202      *
00203      * @note for internal use only.
00204      */
00205     void pairingComplete(bool success);
00206 
00207     /**
00208      * Periodic callback in thread context.
00209      * We use this here purely to safely issue a disconnect operation after a pairing operation is complete.
00210      */
00211     void idleTick();
00212 
00213     /**
00214     * Stops any currently running BLE advertisements
00215     */
00216     void stopAdvertising();
00217 #if CONFIG_ENABLED(MICROBIT_BLE_EDDYSTONE_URL)
00218 
00219     /**
00220       * Set the content of Eddystone URL frames
00221       *
00222       * @param url The url to broadcast
00223       *
00224       * @param calibratedPower the transmission range of the beacon (Defaults to: 0xF0 ~10m).
00225       *
00226       * @param connectable true to keep bluetooth connectable for other services, false otherwise. (Defaults to true)
00227       *
00228       * @param interval the rate at which the micro:bit will advertise url frames. (Defaults to MICROBIT_BLE_EDDYSTONE_ADV_INTERVAL)
00229       *
00230       * @note The calibratedPower value ranges from -100 to +20 to a resolution of 1. The calibrated power should be binary encoded.
00231       * More information can be found at https://github.com/google/eddystone/tree/master/eddystone-uid#tx-power
00232       */
00233     int advertiseEddystoneUrl(const char *url, int8_t calibratedPower = MICROBIT_BLE_EDDYSTONE_DEFAULT_POWER, bool connectable = true, uint16_t interval = MICROBIT_BLE_EDDYSTONE_ADV_INTERVAL);
00234 
00235     /**
00236       * Set the content of Eddystone URL frames, but accepts a ManagedString as a url.
00237       *
00238       * @param url The url to broadcast
00239       *
00240       * @param calibratedPower the transmission range of the beacon (Defaults to: 0xF0 ~10m).
00241       *
00242       * @param connectable true to keep bluetooth connectable for other services, false otherwise. (Defaults to true)
00243       *
00244       * @param interval the rate at which the micro:bit will advertise url frames. (Defaults to MICROBIT_BLE_EDDYSTONE_ADV_INTERVAL)
00245       *
00246       * @note The calibratedPower value ranges from -100 to +20 to a resolution of 1. The calibrated power should be binary encoded.
00247       * More information can be found at https://github.com/google/eddystone/tree/master/eddystone-uid#tx-power
00248       */
00249     int advertiseEddystoneUrl(ManagedString url, int8_t calibratedPower = MICROBIT_BLE_EDDYSTONE_DEFAULT_POWER, bool connectable = true, uint16_t interval = MICROBIT_BLE_EDDYSTONE_ADV_INTERVAL);
00250 #endif
00251 
00252 #if CONFIG_ENABLED(MICROBIT_BLE_EDDYSTONE_UID)
00253     /**
00254       * Set the content of Eddystone UID frames
00255       *
00256       * @param uid_namespace: the uid namespace. Must 10 bytes long.
00257       *
00258       * @param uid_instance:  the uid instance value. Must 6 bytes long.
00259       *
00260       * @param calibratedPower the transmission range of the beacon (Defaults to: 0xF0 ~10m).
00261       *
00262       * @param connectable true to keep bluetooth connectable for other services, false otherwise. (Defaults to true)
00263       *
00264       * @param interval the rate at which the micro:bit will advertise url frames. (Defaults to MICROBIT_BLE_EDDYSTONE_ADV_INTERVAL)
00265       *
00266       * @note The calibratedPower value ranges from -100 to +20 to a resolution of 1. The calibrated power should be binary encoded.
00267       * More information can be found at https://github.com/google/eddystone/tree/master/eddystone-uid#tx-power
00268       */
00269     int advertiseEddystoneUid(const char* uid_namespace, const char* uid_instance, int8_t calibratedPower = MICROBIT_BLE_EDDYSTONE_DEFAULT_POWER, bool connectable = true, uint16_t interval = MICROBIT_BLE_EDDYSTONE_ADV_INTERVAL);
00270 #endif
00271 
00272   private:
00273     /**
00274     * Displays the device's ID code as a histogram on the provided MicroBitDisplay instance.
00275     *
00276     * @param display The display instance used for displaying the histogram.
00277     */
00278     void showNameHistogram(MicroBitDisplay &display);
00279 
00280     #define MICROBIT_BLE_DISCONNECT_AFTER_PAIRING_DELAY  500
00281     unsigned long pairing_completed_at_time;   
00282 
00283     int pairingStatus;
00284     ManagedString passKey;
00285     ManagedString deviceName;
00286 };
00287 
00288 #endif