fork microbit-dal
Dependencies: BLE_API mbed-dev-bin nRF51822
Fork of microbit-dal by
Diff: inc/bluetooth/MicroBitBLEManager.h
- Revision:
- 74:a8f5674a0079
- Parent:
- 3:d86a4ddc1867
--- a/inc/bluetooth/MicroBitBLEManager.h Wed Jul 13 14:32:54 2016 +0000 +++ b/inc/bluetooth/MicroBitBLEManager.h Wed Feb 08 07:49:17 2017 +0000 @@ -35,7 +35,7 @@ * The ARM cc compiler is more tolerant. We don't test __GNUC__ here to detect GCC as ARMCC also typically sets this * as a compatability option, but does not support the options used... */ -#if !defined (__arm) +#if !defined(__arm) #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunused-parameter" #endif @@ -44,7 +44,7 @@ /* * Return to our predefined compiler settings. */ -#if !defined (__arm) +#if !defined(__arm) #pragma GCC diagnostic pop #endif @@ -61,21 +61,24 @@ #include "MicroBitButton.h" #include "MicroBitStorage.h" -#define MICROBIT_BLE_PAIR_REQUEST 0x01 -#define MICROBIT_BLE_PAIR_COMPLETE 0x02 -#define MICROBIT_BLE_PAIR_PASSCODE 0x04 -#define MICROBIT_BLE_PAIR_SUCCESSFUL 0x08 +#define MICROBIT_BLE_PAIR_REQUEST 0x01 +#define MICROBIT_BLE_PAIR_COMPLETE 0x02 +#define MICROBIT_BLE_PAIR_PASSCODE 0x04 +#define MICROBIT_BLE_PAIR_SUCCESSFUL 0x08 -#define MICROBIT_BLE_PAIRING_TIMEOUT 90 -#define MICROBIT_BLE_POWER_LEVELS 8 -#define MICROBIT_BLE_MAXIMUM_BONDS 4 -#define MICROBIT_BLE_ENABLE_BONDING true +#define MICROBIT_BLE_PAIRING_TIMEOUT 90 +#define MICROBIT_BLE_POWER_LEVELS 8 +#define MICROBIT_BLE_MAXIMUM_BONDS 4 +#define MICROBIT_BLE_ENABLE_BONDING true + +#define MICROBIT_BLE_EDDYSTONE_ADV_INTERVAL 400 +#define MICROBIT_BLE_EDDYSTONE_DEFAULT_POWER 0xF0 extern const int8_t MICROBIT_BLE_POWER_LEVEL[]; struct BLESysAttribute { - uint8_t sys_attr[8]; + uint8_t sys_attr[8]; }; struct BLESysAttributeStore @@ -89,13 +92,14 @@ */ class MicroBitBLEManager : MicroBitComponent { - public: + public: + static MicroBitBLEManager *manager; - // The mbed abstraction of the BlueTooth Low Energy (BLE) hardware - BLEDevice *ble; + // The mbed abstraction of the BlueTooth Low Energy (BLE) hardware + BLEDevice *ble; //an instance of MicroBitStorage used to store sysAttrs from softdevice - MicroBitStorage* storage; + MicroBitStorage *storage; /** * Constructor. @@ -107,7 +111,7 @@ * @note The BLE stack *cannot* be brought up in a static context (the software simply hangs or corrupts itself). * Hence, the init() member function should be used to initialise the BLE stack. */ - MicroBitBLEManager(MicroBitStorage& _storage); + MicroBitBLEManager(MicroBitStorage &_storage); /** * Constructor. @@ -120,6 +124,15 @@ MicroBitBLEManager(); /** + * getInstance + * + * Allows other objects to easily obtain a pointer to the single instance of this object. By rights the constructor should be made + * private to properly implement the singleton pattern. + * + */ + static MicroBitBLEManager *getInstance(); + + /** * Post constructor initialisation method as the BLE stack cannot be brought * up in a static context. * @@ -132,7 +145,7 @@ * bleManager.init(uBit.getName(), uBit.getSerial(), uBit.messageBus, true); * @endcode */ - void init(ManagedString deviceName, ManagedString serialNumber, EventModel& messageBus, bool enableBonding); + void init(ManagedString deviceName, ManagedString serialNumber, EventModel &messageBus, bool enableBonding); /** * Change the output power level of the transmitter to the given value. @@ -174,42 +187,102 @@ */ int getBondCount(); - /** + /** * A request to pair has been received from a BLE device. * If we're in pairing mode, display the passkey to the user. * Also, purge the bonding table if it has reached capacity. * * @note for internal use only. */ - void pairingRequested(ManagedString passKey); + void pairingRequested(ManagedString passKey); - /** + /** * A pairing request has been sucessfully completed. * If we're in pairing mode, display a success or failure message. * * @note for internal use only. */ - void pairingComplete(bool success); + void pairingComplete(bool success); - /** + /** * Periodic callback in thread context. * We use this here purely to safely issue a disconnect operation after a pairing operation is complete. */ - void idleTick(); + void idleTick(); + + /** + * Stops any currently running BLE advertisements + */ + void stopAdvertising(); +#if CONFIG_ENABLED(MICROBIT_BLE_EDDYSTONE_URL) - private: + /** + * Set the content of Eddystone URL frames + * + * @param url The url to broadcast + * + * @param calibratedPower the transmission range of the beacon (Defaults to: 0xF0 ~10m). + * + * @param connectable true to keep bluetooth connectable for other services, false otherwise. (Defaults to true) + * + * @param interval the rate at which the micro:bit will advertise url frames. (Defaults to MICROBIT_BLE_EDDYSTONE_ADV_INTERVAL) + * + * @note The calibratedPower value ranges from -100 to +20 to a resolution of 1. The calibrated power should be binary encoded. + * More information can be found at https://github.com/google/eddystone/tree/master/eddystone-uid#tx-power + */ + int advertiseEddystoneUrl(const char *url, int8_t calibratedPower = MICROBIT_BLE_EDDYSTONE_DEFAULT_POWER, bool connectable = true, uint16_t interval = MICROBIT_BLE_EDDYSTONE_ADV_INTERVAL); - /** - * Displays the device's ID code as a histogram on the provided MicroBitDisplay instance. - * - * @param display The display instance used for displaying the histogram. - */ - void showNameHistogram(MicroBitDisplay &display); + /** + * Set the content of Eddystone URL frames, but accepts a ManagedString as a url. + * + * @param url The url to broadcast + * + * @param calibratedPower the transmission range of the beacon (Defaults to: 0xF0 ~10m). + * + * @param connectable true to keep bluetooth connectable for other services, false otherwise. (Defaults to true) + * + * @param interval the rate at which the micro:bit will advertise url frames. (Defaults to MICROBIT_BLE_EDDYSTONE_ADV_INTERVAL) + * + * @note The calibratedPower value ranges from -100 to +20 to a resolution of 1. The calibrated power should be binary encoded. + * More information can be found at https://github.com/google/eddystone/tree/master/eddystone-uid#tx-power + */ + int advertiseEddystoneUrl(ManagedString url, int8_t calibratedPower = MICROBIT_BLE_EDDYSTONE_DEFAULT_POWER, bool connectable = true, uint16_t interval = MICROBIT_BLE_EDDYSTONE_ADV_INTERVAL); +#endif - int pairingStatus; - ManagedString passKey; - ManagedString deviceName; +#if CONFIG_ENABLED(MICROBIT_BLE_EDDYSTONE_UID) + /** + * Set the content of Eddystone UID frames + * + * @param uid_namespace: the uid namespace. Must 10 bytes long. + * + * @param uid_instance: the uid instance value. Must 6 bytes long. + * + * @param calibratedPower the transmission range of the beacon (Defaults to: 0xF0 ~10m). + * + * @param connectable true to keep bluetooth connectable for other services, false otherwise. (Defaults to true) + * + * @param interval the rate at which the micro:bit will advertise url frames. (Defaults to MICROBIT_BLE_EDDYSTONE_ADV_INTERVAL) + * + * @note The calibratedPower value ranges from -100 to +20 to a resolution of 1. The calibrated power should be binary encoded. + * More information can be found at https://github.com/google/eddystone/tree/master/eddystone-uid#tx-power + */ + 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); +#endif + private: + /** + * Displays the device's ID code as a histogram on the provided MicroBitDisplay instance. + * + * @param display The display instance used for displaying the histogram. + */ + void showNameHistogram(MicroBitDisplay &display); + + #define MICROBIT_BLE_DISCONNECT_AFTER_PAIRING_DELAY 500 + unsigned long pairing_completed_at_time; + + int pairingStatus; + ManagedString passKey; + ManagedString deviceName; }; -#endif \ No newline at end of file +#endif