fork microbit-dal

Dependencies:   BLE_API mbed-dev-bin nRF51822

Dependents:   microbit microbit

Fork of microbit-dal by Wendy Warne

Committer:
kenogami
Date:
Thu Aug 03 17:36:13 2017 +0000
Revision:
79:e4e6ab1c9835
Parent:
78:e4c9170fd9b9
commit change to library.

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
wwbluetooth 75:739b6a1c1b50 26
wwbluetooth 75:739b6a1c1b50 27
Jonathan Austin 1:8aa5cdb4ab67 28 #include "MicroBitConfig.h"
Jonathan Austin 1:8aa5cdb4ab67 29 #include "MicroBitBLEManager.h"
bluetooth_mdw 74:a8f5674a0079 30 #include "MicroBitEddystone.h"
Jonathan Austin 1:8aa5cdb4ab67 31 #include "MicroBitStorage.h"
Jonathan Austin 1:8aa5cdb4ab67 32 #include "MicroBitFiber.h"
bluetooth_mdw 74:a8f5674a0079 33 #include "MicroBitSystemTimer.h"
Jonathan Austin 1:8aa5cdb4ab67 34
Jonathan Austin 1:8aa5cdb4ab67 35 /* The underlying Nordic libraries that support BLE do not compile cleanly with the stringent GCC settings we employ.
Jonathan Austin 1:8aa5cdb4ab67 36 * 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 37 * 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 38 * as a compatability option, but does not support the options used...
Jonathan Austin 1:8aa5cdb4ab67 39 */
Jonathan Austin 1:8aa5cdb4ab67 40 #if !defined(__arm)
Jonathan Austin 1:8aa5cdb4ab67 41 #pragma GCC diagnostic ignored "-Wunused-function"
Jonathan Austin 1:8aa5cdb4ab67 42 #pragma GCC diagnostic push
Jonathan Austin 1:8aa5cdb4ab67 43 #pragma GCC diagnostic ignored "-Wunused-parameter"
Jonathan Austin 1:8aa5cdb4ab67 44 #endif
Jonathan Austin 1:8aa5cdb4ab67 45
Jonathan Austin 1:8aa5cdb4ab67 46 #include "ble.h"
Jonathan Austin 1:8aa5cdb4ab67 47
bluetooth_mdw 74:a8f5674a0079 48 extern "C" {
Jonathan Austin 1:8aa5cdb4ab67 49 #include "device_manager.h"
Jonathan Austin 1:8aa5cdb4ab67 50 uint32_t btle_set_gatt_table_size(uint32_t size);
Jonathan Austin 1:8aa5cdb4ab67 51 }
Jonathan Austin 1:8aa5cdb4ab67 52
Jonathan Austin 1:8aa5cdb4ab67 53 /*
Jonathan Austin 1:8aa5cdb4ab67 54 * Return to our predefined compiler settings.
Jonathan Austin 1:8aa5cdb4ab67 55 */
Jonathan Austin 1:8aa5cdb4ab67 56 #if !defined(__arm)
Jonathan Austin 1:8aa5cdb4ab67 57 #pragma GCC diagnostic pop
Jonathan Austin 1:8aa5cdb4ab67 58 #endif
Jonathan Austin 1:8aa5cdb4ab67 59
bluetooth_mdw 74:a8f5674a0079 60 #define MICROBIT_PAIRING_FADE_SPEED 4
Jonathan Austin 1:8aa5cdb4ab67 61
bluetooth_mdw 74:a8f5674a0079 62 // Some Black Magic to compare the definition of our security mode in MicroBitConfig with a given parameter.
bluetooth_mdw 74:a8f5674a0079 63 // Required as the MicroBitConfig option is actually an mbed enum, that is not normally comparable at compile time.
bluetooth_mdw 74:a8f5674a0079 64 //
bluetooth_mdw 74:a8f5674a0079 65
bluetooth_mdw 74:a8f5674a0079 66 #define __CAT(a, ...) a##__VA_ARGS__
wwbluetooth 75:739b6a1c1b50 67
bluetooth_mdw 74:a8f5674a0079 68
bluetooth_mdw 74:a8f5674a0079 69 const char *MICROBIT_BLE_MANUFACTURER = NULL;
bluetooth_mdw 74:a8f5674a0079 70 const char *MICROBIT_BLE_MODEL = "BBC micro:bit";
bluetooth_mdw 74:a8f5674a0079 71 const char *MICROBIT_BLE_HARDWARE_VERSION = NULL;
bluetooth_mdw 74:a8f5674a0079 72 const char *MICROBIT_BLE_FIRMWARE_VERSION = MICROBIT_DAL_VERSION;
bluetooth_mdw 74:a8f5674a0079 73 const char *MICROBIT_BLE_SOFTWARE_VERSION = NULL;
Jonathan Austin 1:8aa5cdb4ab67 74 const int8_t MICROBIT_BLE_POWER_LEVEL[] = {-30, -20, -16, -12, -8, -4, 0, 4};
Jonathan Austin 1:8aa5cdb4ab67 75
Jonathan Austin 1:8aa5cdb4ab67 76 /*
Jonathan Austin 1:8aa5cdb4ab67 77 * Many of the mbed interfaces we need to use only support callbacks to plain C functions, rather than C++ methods.
Jonathan Austin 1:8aa5cdb4ab67 78 * So, we maintain a pointer to the MicroBitBLEManager that's in use. Ths way, we can still access resources on the micro:bit
Jonathan Austin 1:8aa5cdb4ab67 79 * whilst keeping the code modular.
Jonathan Austin 1:8aa5cdb4ab67 80 */
bluetooth_mdw 74:a8f5674a0079 81 MicroBitBLEManager *MicroBitBLEManager::manager = NULL; // Singleton reference to the BLE manager. many mbed BLE API callbacks still do not support member funcions yet. :-(
bluetooth_mdw 74:a8f5674a0079 82
Jonathan Austin 1:8aa5cdb4ab67 83
Jonathan Austin 1:8aa5cdb4ab67 84
Jonathan Austin 1:8aa5cdb4ab67 85 /**
Jonathan Austin 1:8aa5cdb4ab67 86 * Constructor.
Jonathan Austin 1:8aa5cdb4ab67 87 * Configure and manage the micro:bit's Bluetooth Low Energy (BLE) stack.
Jonathan Austin 1:8aa5cdb4ab67 88 * @param _storage an instance of MicroBitStorage used to persist sys attribute information. (This is required for compatability with iOS).
Jonathan Austin 1:8aa5cdb4ab67 89 * @note The BLE stack *cannot* be brought up in a static context (the software simply hangs or corrupts itself).
Jonathan Austin 1:8aa5cdb4ab67 90 * Hence, the init() member function should be used to initialise the BLE stack.
Jonathan Austin 1:8aa5cdb4ab67 91 */
bluetooth_mdw 74:a8f5674a0079 92 MicroBitBLEManager::MicroBitBLEManager(MicroBitStorage &_storage) : storage(&_storage)
Jonathan Austin 1:8aa5cdb4ab67 93 {
Jonathan Austin 1:8aa5cdb4ab67 94 manager = this;
bluetooth_mdw 74:a8f5674a0079 95 this->ble = NULL;
bluetooth_mdw 74:a8f5674a0079 96 this->pairingStatus = 0;
Jonathan Austin 1:8aa5cdb4ab67 97 }
Jonathan Austin 1:8aa5cdb4ab67 98
Jonathan Austin 1:8aa5cdb4ab67 99 /**
Jonathan Austin 1:8aa5cdb4ab67 100 * Constructor.
Jonathan Austin 1:8aa5cdb4ab67 101 * Configure and manage the micro:bit's Bluetooth Low Energy (BLE) stack.
Jonathan Austin 1:8aa5cdb4ab67 102 * @note The BLE stack *cannot* be brought up in a static context (the software simply hangs or corrupts itself).
Jonathan Austin 1:8aa5cdb4ab67 103 * Hence, the init() member function should be used to initialise the BLE stack.
Jonathan Austin 1:8aa5cdb4ab67 104 */
bluetooth_mdw 74:a8f5674a0079 105 MicroBitBLEManager::MicroBitBLEManager() : storage(NULL)
Jonathan Austin 1:8aa5cdb4ab67 106 {
Jonathan Austin 1:8aa5cdb4ab67 107 manager = this;
bluetooth_mdw 74:a8f5674a0079 108 this->ble = NULL;
bluetooth_mdw 74:a8f5674a0079 109 this->pairingStatus = 0;
bluetooth_mdw 74:a8f5674a0079 110 }
bluetooth_mdw 74:a8f5674a0079 111
bluetooth_mdw 74:a8f5674a0079 112 /**
bluetooth_mdw 74:a8f5674a0079 113 * When called, the micro:bit will begin advertising for a predefined period,
bluetooth_mdw 74:a8f5674a0079 114 * MICROBIT_BLE_ADVERTISING_TIMEOUT seconds to bonded devices.
bluetooth_mdw 74:a8f5674a0079 115 */
bluetooth_mdw 74:a8f5674a0079 116 MicroBitBLEManager *MicroBitBLEManager::getInstance()
bluetooth_mdw 74:a8f5674a0079 117 {
bluetooth_mdw 74:a8f5674a0079 118 if (manager == 0)
bluetooth_mdw 74:a8f5674a0079 119 {
bluetooth_mdw 74:a8f5674a0079 120 manager = new MicroBitBLEManager;
bluetooth_mdw 74:a8f5674a0079 121 }
bluetooth_mdw 74:a8f5674a0079 122 return manager;
Jonathan Austin 1:8aa5cdb4ab67 123 }
Jonathan Austin 1:8aa5cdb4ab67 124
Jonathan Austin 1:8aa5cdb4ab67 125 /**
Jonathan Austin 1:8aa5cdb4ab67 126 * When called, the micro:bit will begin advertising for a predefined period,
Jonathan Austin 1:8aa5cdb4ab67 127 * MICROBIT_BLE_ADVERTISING_TIMEOUT seconds to bonded devices.
Jonathan Austin 1:8aa5cdb4ab67 128 */
Jonathan Austin 1:8aa5cdb4ab67 129 void MicroBitBLEManager::advertise()
Jonathan Austin 1:8aa5cdb4ab67 130 {
bluetooth_mdw 74:a8f5674a0079 131 if (ble)
Jonathan Austin 1:8aa5cdb4ab67 132 ble->gap().startAdvertising();
Jonathan Austin 1:8aa5cdb4ab67 133 }
Jonathan Austin 1:8aa5cdb4ab67 134
Jonathan Austin 1:8aa5cdb4ab67 135 /**
Jonathan Austin 1:8aa5cdb4ab67 136 * Post constructor initialisation method as the BLE stack cannot be brought
Jonathan Austin 1:8aa5cdb4ab67 137 * up in a static context.
Jonathan Austin 1:8aa5cdb4ab67 138 * @param deviceName The name used when advertising
Jonathan Austin 1:8aa5cdb4ab67 139 * @param serialNumber The serial number exposed by the device information service
Jonathan Austin 1:8aa5cdb4ab67 140 * @param messageBus An instance of an EventModel, used during pairing.
Jonathan Austin 1:8aa5cdb4ab67 141 * @param enableBonding If true, the security manager enabled bonding.
Jonathan Austin 1:8aa5cdb4ab67 142 * @code
Jonathan Austin 1:8aa5cdb4ab67 143 * bleManager.init(uBit.getName(), uBit.getSerial(), uBit.messageBus, true);
Jonathan Austin 1:8aa5cdb4ab67 144 * @endcode
Jonathan Austin 1:8aa5cdb4ab67 145 */
bluetooth_mdw 74:a8f5674a0079 146 void MicroBitBLEManager::init(ManagedString deviceName, ManagedString serialNumber, EventModel &messageBus, bool enableBonding)
Jonathan Austin 1:8aa5cdb4ab67 147 {
bluetooth_mdw 74:a8f5674a0079 148 ManagedString BLEName("BBC micro:bit");
bluetooth_mdw 74:a8f5674a0079 149 this->deviceName = deviceName;
Jonathan Austin 1:8aa5cdb4ab67 150
wwbluetooth 75:739b6a1c1b50 151 //wlw #if !(CONFIG_ENABLED(MICROBIT_BLE_WHITELIST))
bluetooth_mdw 74:a8f5674a0079 152 ManagedString namePrefix(" [");
bluetooth_mdw 74:a8f5674a0079 153 ManagedString namePostfix("]");
bluetooth_mdw 74:a8f5674a0079 154 BLEName = BLEName + namePrefix + deviceName + namePostfix;
wwbluetooth 75:739b6a1c1b50 155 //wlw #endif
Jonathan Austin 1:8aa5cdb4ab67 156
bluetooth_mdw 74:a8f5674a0079 157 // Start the BLE stack.
Jonathan Austin 1:8aa5cdb4ab67 158 #if CONFIG_ENABLED(MICROBIT_HEAP_REUSE_SD)
Jonathan Austin 1:8aa5cdb4ab67 159 btle_set_gatt_table_size(MICROBIT_SD_GATT_TABLE_SIZE);
Jonathan Austin 1:8aa5cdb4ab67 160 #endif
Jonathan Austin 1:8aa5cdb4ab67 161
Jonathan Austin 1:8aa5cdb4ab67 162 ble = new BLEDevice();
Jonathan Austin 1:8aa5cdb4ab67 163 ble->init();
Jonathan Austin 1:8aa5cdb4ab67 164
Jonathan Austin 1:8aa5cdb4ab67 165
Jonathan Austin 1:8aa5cdb4ab67 166 // Configure the stack to hold onto the CPU during critical timing events.
Jonathan Austin 1:8aa5cdb4ab67 167 // mbed-classic performs __disable_irq() calls in its timers that can cause
Jonathan Austin 1:8aa5cdb4ab67 168 // MIC failures on secure BLE channels...
Jonathan Austin 1:8aa5cdb4ab67 169 ble_common_opt_radio_cpu_mutex_t opt;
Jonathan Austin 1:8aa5cdb4ab67 170 opt.enable = 1;
Jonathan Austin 1:8aa5cdb4ab67 171 sd_ble_opt_set(BLE_COMMON_OPT_RADIO_CPU_MUTEX, (const ble_opt_t *)&opt);
Jonathan Austin 1:8aa5cdb4ab67 172
Jonathan Austin 1:8aa5cdb4ab67 173
Jonathan Austin 1:8aa5cdb4ab67 174
Jonathan Austin 1:8aa5cdb4ab67 175 // Configure the radio at our default power level
kenogami 78:e4c9170fd9b9 176 // setTransmitPower(MICROBIT_BLE_DEFAULT_TX_POWER);
kenogami 78:e4c9170fd9b9 177 // use higher power setting for iOS nrf Connect
kenogami 78:e4c9170fd9b9 178 setTransmitPower(6);
Jonathan Austin 1:8aa5cdb4ab67 179
bluetooth_mdw 74:a8f5674a0079 180 // Bring up core BLE services.
LancasterUniversity 29:62f8b007debf 181 #if CONFIG_ENABLED(MICROBIT_BLE_DFU_SERVICE)
Jonathan Austin 1:8aa5cdb4ab67 182 new MicroBitDFUService(*ble);
LancasterUniversity 29:62f8b007debf 183 #endif
LancasterUniversity 29:62f8b007debf 184
LancasterUniversity 29:62f8b007debf 185 #if CONFIG_ENABLED(MICROBIT_BLE_DEVICE_INFORMATION_SERVICE)
bluetooth_mdw 74:a8f5674a0079 186 DeviceInformationService ble_device_information_service(*ble, MICROBIT_BLE_MANUFACTURER, MICROBIT_BLE_MODEL, serialNumber.toCharArray(), MICROBIT_BLE_HARDWARE_VERSION, MICROBIT_BLE_FIRMWARE_VERSION, MICROBIT_BLE_SOFTWARE_VERSION);
LancasterUniversity 66:2fc7d7c2fffc 187 #else
LancasterUniversity 66:2fc7d7c2fffc 188 (void)serialNumber;
LancasterUniversity 29:62f8b007debf 189 #endif
LancasterUniversity 29:62f8b007debf 190
LancasterUniversity 29:62f8b007debf 191 #if CONFIG_ENABLED(MICROBIT_BLE_EVENT_SERVICE)
Jonathan Austin 1:8aa5cdb4ab67 192 new MicroBitEventService(*ble, messageBus);
LancasterUniversity 29:62f8b007debf 193 #else
LancasterUniversity 29:62f8b007debf 194 (void)messageBus;
LancasterUniversity 29:62f8b007debf 195 #endif
Jonathan Austin 1:8aa5cdb4ab67 196
Jonathan Austin 1:8aa5cdb4ab67 197
bluetooth_mdw 74:a8f5674a0079 198 // Setup advertising.
Jonathan Austin 1:8aa5cdb4ab67 199 #if CONFIG_ENABLED(MICROBIT_BLE_WHITELIST)
Jonathan Austin 1:8aa5cdb4ab67 200 ble->accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
Jonathan Austin 1:8aa5cdb4ab67 201 #else
Jonathan Austin 1:8aa5cdb4ab67 202 ble->accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
Jonathan Austin 1:8aa5cdb4ab67 203 #endif
Jonathan Austin 1:8aa5cdb4ab67 204
Jonathan Austin 1:8aa5cdb4ab67 205 ble->accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)BLEName.toCharArray(), BLEName.length());
Jonathan Austin 1:8aa5cdb4ab67 206 ble->setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
Jonathan Austin 1:8aa5cdb4ab67 207 ble->setAdvertisingInterval(200);
Jonathan Austin 1:8aa5cdb4ab67 208
Jonathan Austin 1:8aa5cdb4ab67 209 #if (MICROBIT_BLE_ADVERTISING_TIMEOUT > 0)
Jonathan Austin 1:8aa5cdb4ab67 210 ble->gap().setAdvertisingTimeout(MICROBIT_BLE_ADVERTISING_TIMEOUT);
Jonathan Austin 1:8aa5cdb4ab67 211 #endif
kenogami 79:e4e6ab1c9835 212 ble->gap().startAdvertising();
Jonathan Austin 1:8aa5cdb4ab67 213 }
Jonathan Austin 1:8aa5cdb4ab67 214
wwbluetooth 75:739b6a1c1b50 215
Jonathan Austin 1:8aa5cdb4ab67 216 /**
Jonathan Austin 1:8aa5cdb4ab67 217 * Change the output power level of the transmitter to the given value.
Jonathan Austin 1:8aa5cdb4ab67 218 * @param power a value in the range 0..7, where 0 is the lowest power and 7 is the highest.
Jonathan Austin 1:8aa5cdb4ab67 219 * @return MICROBIT_OK on success, or MICROBIT_INVALID_PARAMETER if the value is out of range.
Jonathan Austin 1:8aa5cdb4ab67 220 * @code
Jonathan Austin 1:8aa5cdb4ab67 221 * // maximum transmission power.
Jonathan Austin 1:8aa5cdb4ab67 222 * bleManager.setTransmitPower(7);
Jonathan Austin 1:8aa5cdb4ab67 223 * @endcode
Jonathan Austin 1:8aa5cdb4ab67 224 */
Jonathan Austin 1:8aa5cdb4ab67 225 int MicroBitBLEManager::setTransmitPower(int power)
Jonathan Austin 1:8aa5cdb4ab67 226 {
Jonathan Austin 1:8aa5cdb4ab67 227 if (power < 0 || power >= MICROBIT_BLE_POWER_LEVELS)
Jonathan Austin 1:8aa5cdb4ab67 228 return MICROBIT_INVALID_PARAMETER;
Jonathan Austin 1:8aa5cdb4ab67 229
Jonathan Austin 1:8aa5cdb4ab67 230 if (ble->gap().setTxPower(MICROBIT_BLE_POWER_LEVEL[power]) != NRF_SUCCESS)
Jonathan Austin 1:8aa5cdb4ab67 231 return MICROBIT_NOT_SUPPORTED;
Jonathan Austin 1:8aa5cdb4ab67 232
Jonathan Austin 1:8aa5cdb4ab67 233 return MICROBIT_OK;
Jonathan Austin 1:8aa5cdb4ab67 234 }
Jonathan Austin 1:8aa5cdb4ab67 235
Jonathan Austin 1:8aa5cdb4ab67 236
Jonathan Austin 1:8aa5cdb4ab67 237
Jonathan Austin 1:8aa5cdb4ab67 238
Jonathan Austin 1:8aa5cdb4ab67 239 /**
Jonathan Austin 1:8aa5cdb4ab67 240 * Periodic callback in thread context.
Jonathan Austin 1:8aa5cdb4ab67 241 * We use this here purely to safely issue a disconnect operation after a pairing operation is complete.
Jonathan Austin 1:8aa5cdb4ab67 242 */
Jonathan Austin 1:8aa5cdb4ab67 243 void MicroBitBLEManager::idleTick()
Jonathan Austin 1:8aa5cdb4ab67 244 {
bluetooth_mdw 74:a8f5674a0079 245
wwbluetooth 75:739b6a1c1b50 246 }
bluetooth_mdw 74:a8f5674a0079 247
bluetooth_mdw 74:a8f5674a0079 248
bluetooth_mdw 74:a8f5674a0079 249 /**
bluetooth_mdw 74:a8f5674a0079 250 * Stops any currently running BLE advertisements
bluetooth_mdw 74:a8f5674a0079 251 */
bluetooth_mdw 74:a8f5674a0079 252 void MicroBitBLEManager::stopAdvertising()
bluetooth_mdw 74:a8f5674a0079 253 {
bluetooth_mdw 74:a8f5674a0079 254 ble->gap().stopAdvertising();
bluetooth_mdw 74:a8f5674a0079 255 }
bluetooth_mdw 74:a8f5674a0079 256
bluetooth_mdw 74:a8f5674a0079 257 #if CONFIG_ENABLED(MICROBIT_BLE_EDDYSTONE_URL)
bluetooth_mdw 74:a8f5674a0079 258 /**
bluetooth_mdw 74:a8f5674a0079 259 * Set the content of Eddystone URL frames
bluetooth_mdw 74:a8f5674a0079 260 * @param url The url to broadcast
bluetooth_mdw 74:a8f5674a0079 261 * @param calibratedPower the transmission range of the beacon (Defaults to: 0xF0 ~10m).
bluetooth_mdw 74:a8f5674a0079 262 * @param connectable true to keep bluetooth connectable for other services, false otherwise. (Defaults to true)
bluetooth_mdw 74:a8f5674a0079 263 * @param interval the rate at which the micro:bit will advertise url frames. (Defaults to MICROBIT_BLE_EDDYSTONE_ADV_INTERVAL)
bluetooth_mdw 74:a8f5674a0079 264 * @note The calibratedPower value ranges from -100 to +20 to a resolution of 1. The calibrated power should be binary encoded.
bluetooth_mdw 74:a8f5674a0079 265 * More information can be found at https://github.com/google/eddystone/tree/master/eddystone-uid#tx-power
bluetooth_mdw 74:a8f5674a0079 266 */
bluetooth_mdw 74:a8f5674a0079 267 int MicroBitBLEManager::advertiseEddystoneUrl(const char* url, int8_t calibratedPower, bool connectable, uint16_t interval)
bluetooth_mdw 74:a8f5674a0079 268 {
bluetooth_mdw 74:a8f5674a0079 269 ble->gap().stopAdvertising();
bluetooth_mdw 74:a8f5674a0079 270 ble->clearAdvertisingPayload();
bluetooth_mdw 74:a8f5674a0079 271
bluetooth_mdw 74:a8f5674a0079 272 ble->setAdvertisingType(connectable ? GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED : GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED);
bluetooth_mdw 74:a8f5674a0079 273 ble->setAdvertisingInterval(interval);
bluetooth_mdw 74:a8f5674a0079 274
bluetooth_mdw 74:a8f5674a0079 275 ble->accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
bluetooth_mdw 74:a8f5674a0079 276
bluetooth_mdw 74:a8f5674a0079 277 int ret = MicroBitEddystone::getInstance()->setURL(ble, url, calibratedPower);
bluetooth_mdw 74:a8f5674a0079 278
bluetooth_mdw 74:a8f5674a0079 279 #if (MICROBIT_BLE_ADVERTISING_TIMEOUT > 0)
bluetooth_mdw 74:a8f5674a0079 280 ble->gap().setAdvertisingTimeout(MICROBIT_BLE_ADVERTISING_TIMEOUT);
bluetooth_mdw 74:a8f5674a0079 281 #endif
bluetooth_mdw 74:a8f5674a0079 282 ble->gap().startAdvertising();
bluetooth_mdw 74:a8f5674a0079 283
bluetooth_mdw 74:a8f5674a0079 284 return ret;
bluetooth_mdw 74:a8f5674a0079 285 }
Jonathan Austin 1:8aa5cdb4ab67 286
bluetooth_mdw 74:a8f5674a0079 287 /**
bluetooth_mdw 74:a8f5674a0079 288 * Set the content of Eddystone URL frames, but accepts a ManagedString as a url.
bluetooth_mdw 74:a8f5674a0079 289 * @param url The url to broadcast
bluetooth_mdw 74:a8f5674a0079 290 * @param calibratedPower the transmission range of the beacon (Defaults to: 0xF0 ~10m).
bluetooth_mdw 74:a8f5674a0079 291 * @param connectable true to keep bluetooth connectable for other services, false otherwise. (Defaults to true)
bluetooth_mdw 74:a8f5674a0079 292 * @param interval the rate at which the micro:bit will advertise url frames. (Defaults to MICROBIT_BLE_EDDYSTONE_ADV_INTERVAL)
bluetooth_mdw 74:a8f5674a0079 293 * @note The calibratedPower value ranges from -100 to +20 to a resolution of 1. The calibrated power should be binary encoded.
bluetooth_mdw 74:a8f5674a0079 294 * More information can be found at https://github.com/google/eddystone/tree/master/eddystone-uid#tx-power
bluetooth_mdw 74:a8f5674a0079 295 */
bluetooth_mdw 74:a8f5674a0079 296 int MicroBitBLEManager::advertiseEddystoneUrl(ManagedString url, int8_t calibratedPower, bool connectable, uint16_t interval)
bluetooth_mdw 74:a8f5674a0079 297 {
bluetooth_mdw 74:a8f5674a0079 298 return advertiseEddystoneUrl((char *)url.toCharArray(), calibratedPower, connectable, interval);
Jonathan Austin 1:8aa5cdb4ab67 299 }
bluetooth_mdw 74:a8f5674a0079 300 #endif
bluetooth_mdw 74:a8f5674a0079 301
bluetooth_mdw 74:a8f5674a0079 302
wwbluetooth 75:739b6a1c1b50 303 void MicroBitBLEManager::pairingMode(MicroBitDisplay &display, MicroBitButton &authorisationButton)
wwbluetooth 75:739b6a1c1b50 304 {
Jonathan Austin 1:8aa5cdb4ab67 305
wwbluetooth 75:739b6a1c1b50 306
wwbluetooth 75:739b6a1c1b50 307 }
Jonathan Austin 1:8aa5cdb4ab67 308
Jonathan Austin 1:8aa5cdb4ab67 309