
this is using the mbed os version 5-13-1
source/BleManager.cpp@113:888e262ff0a9, 2019-05-18 (annotated)
- Committer:
- ocomeni
- Date:
- Sat May 18 10:50:49 2019 +0000
- Branch:
- PassingRegression
- Revision:
- 113:888e262ff0a9
- Parent:
- 108:3c8fb2c6e7bf
- Child:
- 116:2296cf274661
BLE now working at startup.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ocomeni | 74:f26e846adfe9 | 1 | /* mbed Microcontroller Library |
ocomeni | 74:f26e846adfe9 | 2 | * Copyright (c) 2006-2013 ARM Limited |
ocomeni | 74:f26e846adfe9 | 3 | * |
ocomeni | 74:f26e846adfe9 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
ocomeni | 74:f26e846adfe9 | 5 | * you may not use this file except in compliance with the License. |
ocomeni | 74:f26e846adfe9 | 6 | * You may obtain a copy of the License at |
ocomeni | 74:f26e846adfe9 | 7 | * |
ocomeni | 74:f26e846adfe9 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
ocomeni | 74:f26e846adfe9 | 9 | * |
ocomeni | 74:f26e846adfe9 | 10 | * Unless required by applicable law or agreed to in writing, software |
ocomeni | 74:f26e846adfe9 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
ocomeni | 74:f26e846adfe9 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
ocomeni | 74:f26e846adfe9 | 13 | * See the License for the specific language governing permissions and |
ocomeni | 74:f26e846adfe9 | 14 | * limitations under the License. |
ocomeni | 74:f26e846adfe9 | 15 | */ |
ocomeni | 75:08eff6258e1b | 16 | |
ocomeni | 74:f26e846adfe9 | 17 | #include <events/mbed_events.h> |
ocomeni | 74:f26e846adfe9 | 18 | #include <mbed.h> |
ocomeni | 103:7b566b522427 | 19 | #include "debug.h" |
ocomeni | 76:6afda865fbf8 | 20 | #include "common_config.h" |
ocomeni | 74:f26e846adfe9 | 21 | #include "ble/BLE.h" |
ocomeni | 76:6afda865fbf8 | 22 | #include "ble/services/UARTService.h" |
ocomeni | 74:f26e846adfe9 | 23 | #include "SecurityManager.h" |
ocomeni | 75:08eff6258e1b | 24 | #include "BleManager.h" |
ocomeni | 75:08eff6258e1b | 25 | #if MBED_CONF_APP_FILESYSTEM_SUPPORT |
ocomeni | 75:08eff6258e1b | 26 | #include "LittleFileSystem.h" |
ocomeni | 75:08eff6258e1b | 27 | #include "HeapBlockDevice.h" |
ocomeni | 75:08eff6258e1b | 28 | #endif //MBED_CONF_APP_FILESYSTEM_SUPPORT |
ocomeni | 103:7b566b522427 | 29 | #define FILE_CODE "btle" |
ocomeni | 75:08eff6258e1b | 30 | |
ocomeni | 75:08eff6258e1b | 31 | |
ocomeni | 113:888e262ff0a9 | 32 | //static const uint8_t DEVICE_NAME[] = "SM_device"; |
ocomeni | 76:6afda865fbf8 | 33 | //static const uint16_t uuid16_list[] = {LEDService::LED_SERVICE_UUID}; |
ocomeni | 76:6afda865fbf8 | 34 | extern UARTService *uart; |
ocomeni | 76:6afda865fbf8 | 35 | extern char buffer[BUFFER_LEN]; |
ocomeni | 75:08eff6258e1b | 36 | /** This example demonstrates all the basic setup required |
ocomeni | 75:08eff6258e1b | 37 | * for pairing and setting up link security both as a central and peripheral |
ocomeni | 75:08eff6258e1b | 38 | * |
ocomeni | 75:08eff6258e1b | 39 | * The example is implemented as two classes, one for the peripheral and one |
ocomeni | 75:08eff6258e1b | 40 | * for central inheriting from a common base. They are run in sequence and |
ocomeni | 75:08eff6258e1b | 41 | * require a peer device to connect to. During the peripheral device demonstration |
ocomeni | 75:08eff6258e1b | 42 | * a peer device is required to connect. In the central device demonstration |
ocomeni | 75:08eff6258e1b | 43 | * this peer device will be scanned for and connected to - therefore it should |
ocomeni | 75:08eff6258e1b | 44 | * be advertising with the same address as when it connected. |
ocomeni | 75:08eff6258e1b | 45 | * |
ocomeni | 75:08eff6258e1b | 46 | * During the test output is written on the serial connection to monitor its |
ocomeni | 75:08eff6258e1b | 47 | * progress. |
ocomeni | 75:08eff6258e1b | 48 | */ |
ocomeni | 75:08eff6258e1b | 49 | |
ocomeni | 75:08eff6258e1b | 50 | //static const uint8_t DEVICE_NAME[] = "SM_device"; |
ocomeni | 75:08eff6258e1b | 51 | |
ocomeni | 75:08eff6258e1b | 52 | /* for demonstration purposes we will store the peer device address |
ocomeni | 75:08eff6258e1b | 53 | * of the device that connects to us in the first demonstration |
ocomeni | 75:08eff6258e1b | 54 | * so we can use its address to reconnect to it later */ |
ocomeni | 75:08eff6258e1b | 55 | //static BLEProtocol::AddressBytes_t peer_address; |
ocomeni | 75:08eff6258e1b | 56 | |
ocomeni | 75:08eff6258e1b | 57 | /** Base class for both peripheral and central. The same class that provides |
ocomeni | 75:08eff6258e1b | 58 | * the logic for the application also implements the SecurityManagerEventHandler |
ocomeni | 75:08eff6258e1b | 59 | * which is the interface used by the Security Manager to communicate events |
ocomeni | 75:08eff6258e1b | 60 | * back to the applications. You can provide overrides for a selection of events |
ocomeni | 75:08eff6258e1b | 61 | * your application is interested in. |
ocomeni | 75:08eff6258e1b | 62 | */ |
ocomeni | 78:07bb86e3ce14 | 63 | SMDevice::SMDevice(BLE &ble, events::EventQueue &event_queue, |
ocomeni | 78:07bb86e3ce14 | 64 | BLEProtocol::AddressBytes_t &peer_address, ble_config_t ble_config) : |
ocomeni | 75:08eff6258e1b | 65 | _ble(ble), |
ocomeni | 75:08eff6258e1b | 66 | _event_queue(event_queue), |
ocomeni | 75:08eff6258e1b | 67 | _peer_address(peer_address), |
ocomeni | 78:07bb86e3ce14 | 68 | ble_config(ble_config), |
ocomeni | 75:08eff6258e1b | 69 | _handle(0), |
ocomeni | 113:888e262ff0a9 | 70 | _is_connecting(false), |
ocomeni | 113:888e262ff0a9 | 71 | _led1(LED1, 0) |
ocomeni | 113:888e262ff0a9 | 72 | { |
ocomeni | 113:888e262ff0a9 | 73 | isConnected = false; |
ocomeni | 113:888e262ff0a9 | 74 | } |
ocomeni | 75:08eff6258e1b | 75 | |
ocomeni | 75:08eff6258e1b | 76 | SMDevice::~SMDevice() |
ocomeni | 75:08eff6258e1b | 77 | { |
ocomeni | 75:08eff6258e1b | 78 | if (_ble.hasInitialized()) { |
ocomeni | 75:08eff6258e1b | 79 | _ble.shutdown(); |
ocomeni | 75:08eff6258e1b | 80 | } |
ocomeni | 75:08eff6258e1b | 81 | } |
ocomeni | 75:08eff6258e1b | 82 | |
ocomeni | 75:08eff6258e1b | 83 | /** Start BLE interface initialisation */ |
ocomeni | 75:08eff6258e1b | 84 | void SMDevice::run() |
ocomeni | 75:08eff6258e1b | 85 | { |
ocomeni | 103:7b566b522427 | 86 | dbg_printf(LOG, "\r\n [BTLE MAN] Thread Id = %X\r\n", (uint32_t)ThisThread::get_id()); |
ocomeni | 87:99b37d26ff2a | 87 | |
ocomeni | 75:08eff6258e1b | 88 | ble_error_t error; |
ocomeni | 75:08eff6258e1b | 89 | |
ocomeni | 108:3c8fb2c6e7bf | 90 | /* to show we're running we'll blink every 10secs */ |
ocomeni | 108:3c8fb2c6e7bf | 91 | _event_queue.call_every(10000, this, &SMDevice::blink); |
ocomeni | 75:08eff6258e1b | 92 | |
ocomeni | 77:0b505d1e15f4 | 93 | /* to show we're advertising we'll print status every minute */ |
ocomeni | 77:0b505d1e15f4 | 94 | _event_queue.call_every(60000, this, &SMDevice::reportGapState); |
ocomeni | 77:0b505d1e15f4 | 95 | |
ocomeni | 77:0b505d1e15f4 | 96 | |
ocomeni | 77:0b505d1e15f4 | 97 | |
ocomeni | 75:08eff6258e1b | 98 | if (_ble.hasInitialized()) { |
ocomeni | 103:7b566b522427 | 99 | dbg_printf(LOG, "Ble instance already initialised.\r\n"); |
ocomeni | 75:08eff6258e1b | 100 | return; |
ocomeni | 75:08eff6258e1b | 101 | } |
ocomeni | 75:08eff6258e1b | 102 | |
ocomeni | 75:08eff6258e1b | 103 | /* this will inform us off all events so we can schedule their handling |
ocomeni | 75:08eff6258e1b | 104 | * using our event queue */ |
ocomeni | 75:08eff6258e1b | 105 | _ble.onEventsToProcess( |
ocomeni | 75:08eff6258e1b | 106 | makeFunctionPointer(this, &SMDevice::schedule_ble_events) |
ocomeni | 75:08eff6258e1b | 107 | ); |
ocomeni | 75:08eff6258e1b | 108 | |
ocomeni | 75:08eff6258e1b | 109 | /* handle timeouts, for example when connection attempts fail */ |
ocomeni | 75:08eff6258e1b | 110 | _ble.gap().onTimeout( |
ocomeni | 75:08eff6258e1b | 111 | makeFunctionPointer(this, &SMDevice::on_timeout) |
ocomeni | 75:08eff6258e1b | 112 | ); |
ocomeni | 75:08eff6258e1b | 113 | |
ocomeni | 75:08eff6258e1b | 114 | error = _ble.init(this, &SMDevice::on_init_complete); |
ocomeni | 75:08eff6258e1b | 115 | |
ocomeni | 75:08eff6258e1b | 116 | if (error) { |
ocomeni | 103:7b566b522427 | 117 | dbg_printf(LOG, "Error returned by BLE::init.\r\n"); |
ocomeni | 75:08eff6258e1b | 118 | return; |
ocomeni | 75:08eff6258e1b | 119 | } |
ocomeni | 75:08eff6258e1b | 120 | |
ocomeni | 75:08eff6258e1b | 121 | /* this will not return until shutdown */ |
ocomeni | 77:0b505d1e15f4 | 122 | //_event_queue.dispatch_forever(); |
ocomeni | 75:08eff6258e1b | 123 | } |
ocomeni | 75:08eff6258e1b | 124 | |
ocomeni | 77:0b505d1e15f4 | 125 | |
ocomeni | 77:0b505d1e15f4 | 126 | void SMDevice::shutDown() |
ocomeni | 77:0b505d1e15f4 | 127 | { |
ocomeni | 77:0b505d1e15f4 | 128 | if (_ble.hasInitialized()) { |
ocomeni | 77:0b505d1e15f4 | 129 | _ble.shutdown(); |
ocomeni | 103:7b566b522427 | 130 | dbg_printf(LOG, "Shutting down BLE Instance...\r\n"); |
ocomeni | 77:0b505d1e15f4 | 131 | _event_queue.break_dispatch(); |
ocomeni | 77:0b505d1e15f4 | 132 | } |
ocomeni | 77:0b505d1e15f4 | 133 | } |
ocomeni | 77:0b505d1e15f4 | 134 | |
ocomeni | 77:0b505d1e15f4 | 135 | |
ocomeni | 75:08eff6258e1b | 136 | /* event handler functions */ |
ocomeni | 75:08eff6258e1b | 137 | |
ocomeni | 75:08eff6258e1b | 138 | /** Respond to a pairing request. This will be called by the stack |
ocomeni | 75:08eff6258e1b | 139 | * when a pairing request arrives and expects the application to |
ocomeni | 75:08eff6258e1b | 140 | * call acceptPairingRequest or cancelPairingRequest */ |
ocomeni | 75:08eff6258e1b | 141 | void SMDevice::pairingRequest( |
ocomeni | 75:08eff6258e1b | 142 | ble::connection_handle_t connectionHandle |
ocomeni | 75:08eff6258e1b | 143 | ) { |
ocomeni | 103:7b566b522427 | 144 | dbg_printf(LOG, "Pairing requested - authorising\r\n"); |
ocomeni | 75:08eff6258e1b | 145 | _ble.securityManager().acceptPairingRequest(connectionHandle); |
ocomeni | 75:08eff6258e1b | 146 | } |
ocomeni | 75:08eff6258e1b | 147 | |
ocomeni | 75:08eff6258e1b | 148 | /** Inform the application of a successful pairing. Terminate the demonstration. */ |
ocomeni | 75:08eff6258e1b | 149 | void SMDevice::pairingResult( |
ocomeni | 75:08eff6258e1b | 150 | ble::connection_handle_t connectionHandle, |
ocomeni | 75:08eff6258e1b | 151 | SecurityManager::SecurityCompletionStatus_t result |
ocomeni | 75:08eff6258e1b | 152 | ) { |
ocomeni | 75:08eff6258e1b | 153 | if (result == SecurityManager::SEC_STATUS_SUCCESS) { |
ocomeni | 103:7b566b522427 | 154 | dbg_printf(LOG, "Pairing successful\r\n"); |
ocomeni | 75:08eff6258e1b | 155 | } else { |
ocomeni | 103:7b566b522427 | 156 | dbg_printf(LOG, "Pairing failed\r\n"); |
ocomeni | 75:08eff6258e1b | 157 | } |
ocomeni | 75:08eff6258e1b | 158 | } |
ocomeni | 75:08eff6258e1b | 159 | |
ocomeni | 75:08eff6258e1b | 160 | /** Inform the application of change in encryption status. This will be |
ocomeni | 75:08eff6258e1b | 161 | * communicated through the serial port */ |
ocomeni | 75:08eff6258e1b | 162 | void SMDevice::linkEncryptionResult( |
ocomeni | 75:08eff6258e1b | 163 | ble::connection_handle_t connectionHandle, |
ocomeni | 75:08eff6258e1b | 164 | ble::link_encryption_t result |
ocomeni | 75:08eff6258e1b | 165 | ) { |
ocomeni | 75:08eff6258e1b | 166 | if (result == ble::link_encryption_t::ENCRYPTED) { |
ocomeni | 103:7b566b522427 | 167 | dbg_printf(LOG, "Link ENCRYPTED\r\n"); |
ocomeni | 75:08eff6258e1b | 168 | } else if (result == ble::link_encryption_t::ENCRYPTED_WITH_MITM) { |
ocomeni | 103:7b566b522427 | 169 | dbg_printf(LOG, "Link ENCRYPTED_WITH_MITM\r\n"); |
ocomeni | 75:08eff6258e1b | 170 | } else if (result == ble::link_encryption_t::NOT_ENCRYPTED) { |
ocomeni | 103:7b566b522427 | 171 | dbg_printf(LOG, "Link NOT_ENCRYPTED\r\n"); |
ocomeni | 75:08eff6258e1b | 172 | } |
ocomeni | 75:08eff6258e1b | 173 | |
ocomeni | 76:6afda865fbf8 | 174 | #ifdef DEMO_BLE_SECURITY |
ocomeni | 75:08eff6258e1b | 175 | /* disconnect in 2 s */ |
ocomeni | 75:08eff6258e1b | 176 | _event_queue.call_in( |
ocomeni | 75:08eff6258e1b | 177 | 2000, &_ble.gap(), |
ocomeni | 75:08eff6258e1b | 178 | &Gap::disconnect, _handle, Gap::REMOTE_USER_TERMINATED_CONNECTION |
ocomeni | 75:08eff6258e1b | 179 | ); |
ocomeni | 76:6afda865fbf8 | 180 | #endif |
ocomeni | 75:08eff6258e1b | 181 | } |
ocomeni | 75:08eff6258e1b | 182 | |
ocomeni | 75:08eff6258e1b | 183 | /** Override to start chosen activity when initialisation completes */ |
ocomeni | 75:08eff6258e1b | 184 | //void SMDevice::start() = 0; |
ocomeni | 75:08eff6258e1b | 185 | |
ocomeni | 75:08eff6258e1b | 186 | /** This is called when BLE interface is initialised and starts the demonstration */ |
ocomeni | 75:08eff6258e1b | 187 | void SMDevice::on_init_complete(BLE::InitializationCompleteCallbackContext *event) |
ocomeni | 75:08eff6258e1b | 188 | { |
ocomeni | 75:08eff6258e1b | 189 | ble_error_t error; |
ocomeni | 75:08eff6258e1b | 190 | |
ocomeni | 75:08eff6258e1b | 191 | if (event->error) { |
ocomeni | 103:7b566b522427 | 192 | dbg_printf(LOG, "Error during the initialisation\r\n"); |
ocomeni | 75:08eff6258e1b | 193 | return; |
ocomeni | 75:08eff6258e1b | 194 | } |
ocomeni | 75:08eff6258e1b | 195 | |
ocomeni | 75:08eff6258e1b | 196 | /* This path will be used to store bonding information but will fallback |
ocomeni | 75:08eff6258e1b | 197 | * to storing in memory if file access fails (for example due to lack of a filesystem) */ |
ocomeni | 75:08eff6258e1b | 198 | const char* db_path = "/fs/bt_sec_db"; |
ocomeni | 75:08eff6258e1b | 199 | /* If the security manager is required this needs to be called before any |
ocomeni | 75:08eff6258e1b | 200 | * calls to the Security manager happen. */ |
ocomeni | 75:08eff6258e1b | 201 | error = _ble.securityManager().init( |
ocomeni | 75:08eff6258e1b | 202 | true, |
ocomeni | 75:08eff6258e1b | 203 | false, |
ocomeni | 79:a2187bbfa407 | 204 | SecurityManager::IO_CAPS_DISPLAY_ONLY, // SecurityManager::IO_CAPS_NONE |
ocomeni | 79:a2187bbfa407 | 205 | ble_config.pairingKey, |
ocomeni | 75:08eff6258e1b | 206 | false, |
ocomeni | 75:08eff6258e1b | 207 | db_path |
ocomeni | 75:08eff6258e1b | 208 | ); |
ocomeni | 75:08eff6258e1b | 209 | |
ocomeni | 75:08eff6258e1b | 210 | if (error) { |
ocomeni | 103:7b566b522427 | 211 | dbg_printf(LOG, "Error during init %d\r\n", error); |
ocomeni | 75:08eff6258e1b | 212 | return; |
ocomeni | 75:08eff6258e1b | 213 | } |
ocomeni | 75:08eff6258e1b | 214 | |
ocomeni | 75:08eff6258e1b | 215 | error = _ble.securityManager().preserveBondingStateOnReset(true); |
ocomeni | 75:08eff6258e1b | 216 | |
ocomeni | 75:08eff6258e1b | 217 | if (error) { |
ocomeni | 103:7b566b522427 | 218 | dbg_printf(LOG, "Error during preserveBondingStateOnReset %d\r\n", error); |
ocomeni | 75:08eff6258e1b | 219 | } |
ocomeni | 75:08eff6258e1b | 220 | |
ocomeni | 75:08eff6258e1b | 221 | #if MBED_CONF_APP_FILESYSTEM_SUPPORT |
ocomeni | 75:08eff6258e1b | 222 | /* Enable privacy so we can find the keys */ |
ocomeni | 75:08eff6258e1b | 223 | error = _ble.gap().enablePrivacy(true); |
ocomeni | 75:08eff6258e1b | 224 | |
ocomeni | 75:08eff6258e1b | 225 | if (error) { |
ocomeni | 103:7b566b522427 | 226 | dbg_printf(LOG, "Error enabling privacy\r\n"); |
ocomeni | 75:08eff6258e1b | 227 | } |
ocomeni | 75:08eff6258e1b | 228 | |
ocomeni | 75:08eff6258e1b | 229 | Gap::PeripheralPrivacyConfiguration_t configuration_p = { |
ocomeni | 75:08eff6258e1b | 230 | /* use_non_resolvable_random_address */ false, |
ocomeni | 75:08eff6258e1b | 231 | Gap::PeripheralPrivacyConfiguration_t::REJECT_NON_RESOLVED_ADDRESS |
ocomeni | 75:08eff6258e1b | 232 | }; |
ocomeni | 75:08eff6258e1b | 233 | _ble.gap().setPeripheralPrivacyConfiguration(&configuration_p); |
ocomeni | 75:08eff6258e1b | 234 | |
ocomeni | 75:08eff6258e1b | 235 | Gap::CentralPrivacyConfiguration_t configuration_c = { |
ocomeni | 75:08eff6258e1b | 236 | /* use_non_resolvable_random_address */ false, |
ocomeni | 75:08eff6258e1b | 237 | Gap::CentralPrivacyConfiguration_t::RESOLVE_AND_FORWARD |
ocomeni | 75:08eff6258e1b | 238 | }; |
ocomeni | 75:08eff6258e1b | 239 | _ble.gap().setCentralPrivacyConfiguration(&configuration_c); |
ocomeni | 75:08eff6258e1b | 240 | |
ocomeni | 75:08eff6258e1b | 241 | /* this demo switches between being master and slave */ |
ocomeni | 75:08eff6258e1b | 242 | _ble.securityManager().setHintFutureRoleReversal(true); |
ocomeni | 75:08eff6258e1b | 243 | #endif |
ocomeni | 75:08eff6258e1b | 244 | |
ocomeni | 75:08eff6258e1b | 245 | /* Tell the security manager to use methods in this class to inform us |
ocomeni | 75:08eff6258e1b | 246 | * of any events. Class needs to implement SecurityManagerEventHandler. */ |
ocomeni | 75:08eff6258e1b | 247 | _ble.securityManager().setSecurityManagerEventHandler(this); |
ocomeni | 75:08eff6258e1b | 248 | |
ocomeni | 75:08eff6258e1b | 249 | /* print device address */ |
ocomeni | 75:08eff6258e1b | 250 | Gap::AddressType_t addr_type; |
ocomeni | 75:08eff6258e1b | 251 | Gap::Address_t addr; |
ocomeni | 75:08eff6258e1b | 252 | _ble.gap().getAddress(&addr_type, addr); |
ocomeni | 103:7b566b522427 | 253 | dbg_printf(LOG, "Device address: %02x:%02x:%02x:%02x:%02x:%02x\r\n", |
ocomeni | 75:08eff6258e1b | 254 | addr[5], addr[4], addr[3], addr[2], addr[1], addr[0]); |
ocomeni | 75:08eff6258e1b | 255 | |
ocomeni | 75:08eff6258e1b | 256 | /* when scanning we want to connect to a peer device so we need to |
ocomeni | 75:08eff6258e1b | 257 | * attach callbacks that are used by Gap to notify us of events */ |
ocomeni | 75:08eff6258e1b | 258 | _ble.gap().onConnection(this, &SMDevice::on_connect); |
ocomeni | 75:08eff6258e1b | 259 | _ble.gap().onDisconnection(this, &SMDevice::on_disconnect); |
ocomeni | 76:6afda865fbf8 | 260 | _ble.gattServer().onDataWritten(this, &SMDevice::onDataWrittenCallback); |
ocomeni | 79:a2187bbfa407 | 261 | //_ble.securityManager().onPasskeyDisplay(this, &SMDevice::passkeyDisplayCallback); |
ocomeni | 79:a2187bbfa407 | 262 | //_ble.securityManager().onSecuritySetupCompleted(this, &SMDevice::securitySetupCompletedCallback); |
ocomeni | 75:08eff6258e1b | 263 | |
ocomeni | 75:08eff6258e1b | 264 | /* start test in 500 ms */ |
ocomeni | 75:08eff6258e1b | 265 | _event_queue.call_in(500, this, &SMDevice::start); |
ocomeni | 75:08eff6258e1b | 266 | } |
ocomeni | 75:08eff6258e1b | 267 | |
ocomeni | 75:08eff6258e1b | 268 | /** This is called by Gap to notify the application we connected */ |
ocomeni | 75:08eff6258e1b | 269 | //void SMDevice::on_connect(const Gap::ConnectionCallbackParams_t *connection_event); |
ocomeni | 75:08eff6258e1b | 270 | |
ocomeni | 75:08eff6258e1b | 271 | /** This is called by Gap to notify the application we disconnected, |
ocomeni | 75:08eff6258e1b | 272 | * in our case it ends the demonstration. */ |
ocomeni | 75:08eff6258e1b | 273 | void SMDevice::on_disconnect(const Gap::DisconnectionCallbackParams_t *event) |
ocomeni | 75:08eff6258e1b | 274 | { |
ocomeni | 103:7b566b522427 | 275 | dbg_printf(LOG, "Disconnected\r\n"); |
ocomeni | 77:0b505d1e15f4 | 276 | #ifndef DEMO_BLE_SECURITY |
ocomeni | 103:7b566b522427 | 277 | dbg_printf(LOG, "Restarting advertising...\r\n"); |
ocomeni | 113:888e262ff0a9 | 278 | _ble.gap().startAdvertising(ble::LEGACY_ADVERTISING_HANDLE); |
ocomeni | 77:0b505d1e15f4 | 279 | #else |
ocomeni | 75:08eff6258e1b | 280 | _event_queue.break_dispatch(); |
ocomeni | 77:0b505d1e15f4 | 281 | #endif |
ocomeni | 113:888e262ff0a9 | 282 | isConnected = false; |
ocomeni | 75:08eff6258e1b | 283 | } |
ocomeni | 75:08eff6258e1b | 284 | |
ocomeni | 75:08eff6258e1b | 285 | /** End demonstration unexpectedly. Called if timeout is reached during advertising, |
ocomeni | 75:08eff6258e1b | 286 | * scanning or connection initiation */ |
ocomeni | 75:08eff6258e1b | 287 | void SMDevice::on_timeout(const Gap::TimeoutSource_t source) |
ocomeni | 75:08eff6258e1b | 288 | { |
ocomeni | 103:7b566b522427 | 289 | dbg_printf(LOG, "Unexpected timeout - aborting\r\n"); |
ocomeni | 75:08eff6258e1b | 290 | _event_queue.break_dispatch(); |
ocomeni | 75:08eff6258e1b | 291 | } |
ocomeni | 75:08eff6258e1b | 292 | |
ocomeni | 75:08eff6258e1b | 293 | /** Schedule processing of events from the BLE in the event queue. */ |
ocomeni | 75:08eff6258e1b | 294 | void SMDevice::schedule_ble_events(BLE::OnEventsToProcessCallbackContext *context) |
ocomeni | 75:08eff6258e1b | 295 | { |
ocomeni | 75:08eff6258e1b | 296 | _event_queue.call(mbed::callback(&context->ble, &BLE::processEvents)); |
ocomeni | 75:08eff6258e1b | 297 | }; |
ocomeni | 75:08eff6258e1b | 298 | |
ocomeni | 76:6afda865fbf8 | 299 | /** Echo received data back */ |
ocomeni | 76:6afda865fbf8 | 300 | void SMDevice::EchoBleUartReceived() |
ocomeni | 76:6afda865fbf8 | 301 | { |
ocomeni | 76:6afda865fbf8 | 302 | uart->writeString(buffer); |
ocomeni | 76:6afda865fbf8 | 303 | uart->writeString("\n"); //flushes uart output buffer and sends data |
ocomeni | 76:6afda865fbf8 | 304 | } |
ocomeni | 76:6afda865fbf8 | 305 | |
ocomeni | 76:6afda865fbf8 | 306 | |
ocomeni | 79:a2187bbfa407 | 307 | /** Send data aynchronously using BLE */ |
ocomeni | 79:a2187bbfa407 | 308 | void SMDevice::sendBLEUartData(char * str) |
ocomeni | 79:a2187bbfa407 | 309 | { |
ocomeni | 113:888e262ff0a9 | 310 | //Gap::GapState_t gapState = _ble.gap().getState(); |
ocomeni | 113:888e262ff0a9 | 311 | //Gap::GapState_t gapState = _ble.getGapState(); |
ocomeni | 113:888e262ff0a9 | 312 | //gapState.connected |
ocomeni | 113:888e262ff0a9 | 313 | if(isConnected){ |
ocomeni | 79:a2187bbfa407 | 314 | uart->writeString(str); |
ocomeni | 79:a2187bbfa407 | 315 | uart->writeString("\n"); //flushes uart output buffer and sends data |
ocomeni | 79:a2187bbfa407 | 316 | } |
ocomeni | 113:888e262ff0a9 | 317 | else |
ocomeni | 113:888e262ff0a9 | 318 | { |
ocomeni | 113:888e262ff0a9 | 319 | dbg_printf(LOG, "BLE not connected\r\n"); |
ocomeni | 113:888e262ff0a9 | 320 | } |
ocomeni | 79:a2187bbfa407 | 321 | } |
ocomeni | 79:a2187bbfa407 | 322 | |
ocomeni | 79:a2187bbfa407 | 323 | |
ocomeni | 76:6afda865fbf8 | 324 | /** |
ocomeni | 79:a2187bbfa407 | 325 | * This callback allows the UARTService to receive updates. |
ocomeni | 76:6afda865fbf8 | 326 | * |
ocomeni | 76:6afda865fbf8 | 327 | * @param[in] params |
ocomeni | 76:6afda865fbf8 | 328 | * Information about the characterisitc being updated. |
ocomeni | 76:6afda865fbf8 | 329 | */ |
ocomeni | 76:6afda865fbf8 | 330 | void SMDevice::onDataWrittenCallback(const GattWriteCallbackParams *params) { |
ocomeni | 76:6afda865fbf8 | 331 | if ((uart != NULL) && (params->handle == uart->getTXCharacteristicHandle())) { |
ocomeni | 76:6afda865fbf8 | 332 | uint16_t bytesRead = params->len; |
ocomeni | 76:6afda865fbf8 | 333 | |
ocomeni | 103:7b566b522427 | 334 | dbg_printf(LOG, "received %u bytes\n\r ", bytesRead); |
ocomeni | 76:6afda865fbf8 | 335 | |
ocomeni | 76:6afda865fbf8 | 336 | if(bytesRead >= 255){ |
ocomeni | 103:7b566b522427 | 337 | dbg_printf(LOG, "Overflow command %u n\r ", bytesRead); |
ocomeni | 76:6afda865fbf8 | 338 | bytesRead = 255; |
ocomeni | 76:6afda865fbf8 | 339 | } |
ocomeni | 76:6afda865fbf8 | 340 | |
ocomeni | 76:6afda865fbf8 | 341 | unsigned index = 0; |
ocomeni | 76:6afda865fbf8 | 342 | for (; index < bytesRead; index++) { |
ocomeni | 76:6afda865fbf8 | 343 | buffer[index] = params->data[index]; |
ocomeni | 76:6afda865fbf8 | 344 | } |
ocomeni | 76:6afda865fbf8 | 345 | |
ocomeni | 76:6afda865fbf8 | 346 | buffer[index++] = 0; |
ocomeni | 76:6afda865fbf8 | 347 | |
ocomeni | 103:7b566b522427 | 348 | dbg_printf(LOG, "Data : %s ",buffer); |
ocomeni | 103:7b566b522427 | 349 | dbg_printf(LOG, "\r\n"); |
ocomeni | 76:6afda865fbf8 | 350 | /* start echo in 50 ms */ |
ocomeni | 76:6afda865fbf8 | 351 | _event_queue.call_in(50, this, &SMDevice::EchoBleUartReceived); |
ocomeni | 76:6afda865fbf8 | 352 | //_event_queue.call(EchoBleUartReceived); |
ocomeni | 76:6afda865fbf8 | 353 | |
ocomeni | 76:6afda865fbf8 | 354 | } |
ocomeni | 76:6afda865fbf8 | 355 | } |
ocomeni | 79:a2187bbfa407 | 356 | |
ocomeni | 79:a2187bbfa407 | 357 | |
ocomeni | 79:a2187bbfa407 | 358 | |
ocomeni | 79:a2187bbfa407 | 359 | |
ocomeni | 75:08eff6258e1b | 360 | /** Blink LED to show we're running */ |
ocomeni | 75:08eff6258e1b | 361 | void SMDevice::blink(void) |
ocomeni | 75:08eff6258e1b | 362 | { |
ocomeni | 75:08eff6258e1b | 363 | _led1 = !_led1; |
ocomeni | 75:08eff6258e1b | 364 | } |
ocomeni | 75:08eff6258e1b | 365 | |
ocomeni | 75:08eff6258e1b | 366 | |
ocomeni | 77:0b505d1e15f4 | 367 | void SMDevice::reportGapState() |
ocomeni | 77:0b505d1e15f4 | 368 | { |
ocomeni | 113:888e262ff0a9 | 369 | //Gap::GapState_t gapState = _ble.gap().getState(); |
ocomeni | 77:0b505d1e15f4 | 370 | char connStr[20] = " Not Connected "; |
ocomeni | 77:0b505d1e15f4 | 371 | char advStr[20] = " Not Advertising "; |
ocomeni | 113:888e262ff0a9 | 372 | //char devName[20] = ""; |
ocomeni | 113:888e262ff0a9 | 373 | //if(gapState.advertising){ |
ocomeni | 113:888e262ff0a9 | 374 | if(_ble.gap().isAdvertisingActive(ble::LEGACY_ADVERTISING_HANDLE)){ |
ocomeni | 77:0b505d1e15f4 | 375 | strncpy(advStr, " Advertising ", 20); |
ocomeni | 77:0b505d1e15f4 | 376 | } |
ocomeni | 113:888e262ff0a9 | 377 | if(isConnected){ |
ocomeni | 77:0b505d1e15f4 | 378 | strncpy(connStr, " Connected ", 20); |
ocomeni | 77:0b505d1e15f4 | 379 | } |
ocomeni | 103:7b566b522427 | 380 | dbg_printf(LOG, "\n Advertising Status = %s\n Connection Status = %s\n", advStr, connStr); |
ocomeni | 77:0b505d1e15f4 | 381 | |
ocomeni | 77:0b505d1e15f4 | 382 | } |
ocomeni | 77:0b505d1e15f4 | 383 | |
ocomeni | 77:0b505d1e15f4 | 384 | |
ocomeni | 75:08eff6258e1b | 385 | /** A peripheral device will advertise, accept the connection and request |
ocomeni | 75:08eff6258e1b | 386 | * a change in link security. */ |
ocomeni | 78:07bb86e3ce14 | 387 | SMDevicePeripheral::SMDevicePeripheral(BLE &ble, events::EventQueue &event_queue, BLEProtocol::AddressBytes_t &peer_address, ble_config_t ble_config) |
ocomeni | 78:07bb86e3ce14 | 388 | : SMDevice(ble, event_queue, peer_address, ble_config) { } |
ocomeni | 75:08eff6258e1b | 389 | |
ocomeni | 75:08eff6258e1b | 390 | void SMDevicePeripheral::start() |
ocomeni | 75:08eff6258e1b | 391 | { |
ocomeni | 75:08eff6258e1b | 392 | /* Set up and start advertising */ |
ocomeni | 75:08eff6258e1b | 393 | |
ocomeni | 75:08eff6258e1b | 394 | ble_error_t error; |
ocomeni | 75:08eff6258e1b | 395 | GapAdvertisingData advertising_data; |
ocomeni | 75:08eff6258e1b | 396 | |
ocomeni | 75:08eff6258e1b | 397 | /* add advertising flags */ |
ocomeni | 75:08eff6258e1b | 398 | advertising_data.addFlags(GapAdvertisingData::LE_GENERAL_DISCOVERABLE |
ocomeni | 75:08eff6258e1b | 399 | | GapAdvertisingData::BREDR_NOT_SUPPORTED); |
ocomeni | 75:08eff6258e1b | 400 | |
ocomeni | 75:08eff6258e1b | 401 | /* add device name */ |
ocomeni | 75:08eff6258e1b | 402 | advertising_data.addData( |
ocomeni | 75:08eff6258e1b | 403 | GapAdvertisingData::COMPLETE_LOCAL_NAME, |
ocomeni | 79:a2187bbfa407 | 404 | (const uint8_t *)ble_config.deviceName, |
ocomeni | 113:888e262ff0a9 | 405 | strlen(ble_config.deviceName) |
ocomeni | 76:6afda865fbf8 | 406 | ); |
ocomeni | 76:6afda865fbf8 | 407 | /* Setup primary service */ |
ocomeni | 76:6afda865fbf8 | 408 | uart = new UARTService(_ble); |
ocomeni | 76:6afda865fbf8 | 409 | |
ocomeni | 75:08eff6258e1b | 410 | |
ocomeni | 76:6afda865fbf8 | 411 | /* add device name */ |
ocomeni | 76:6afda865fbf8 | 412 | error = advertising_data.addData( |
ocomeni | 76:6afda865fbf8 | 413 | GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS , |
ocomeni | 76:6afda865fbf8 | 414 | (const uint8_t *)UARTServiceUUID_reversed, |
ocomeni | 76:6afda865fbf8 | 415 | sizeof(sizeof(UARTServiceUUID_reversed)) |
ocomeni | 76:6afda865fbf8 | 416 | ); |
ocomeni | 76:6afda865fbf8 | 417 | /* setup advertising */ |
ocomeni | 76:6afda865fbf8 | 418 | //error = _ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
ocomeni | 76:6afda865fbf8 | 419 | //ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
ocomeni | 76:6afda865fbf8 | 420 | //error = _ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
ocomeni | 76:6afda865fbf8 | 421 | /* set up the services that can be discovered */ |
ocomeni | 76:6afda865fbf8 | 422 | //error = _ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,(const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); |
ocomeni | 76:6afda865fbf8 | 423 | |
ocomeni | 76:6afda865fbf8 | 424 | |
ocomeni | 76:6afda865fbf8 | 425 | //error = _ble.gap().setAdvertisingPayload(advertising_data); |
ocomeni | 75:08eff6258e1b | 426 | |
ocomeni | 75:08eff6258e1b | 427 | if (error) { |
ocomeni | 103:7b566b522427 | 428 | dbg_printf(LOG, "Error during Gap::setAdvertisingPayload\r\n"); |
ocomeni | 75:08eff6258e1b | 429 | return; |
ocomeni | 75:08eff6258e1b | 430 | } |
ocomeni | 75:08eff6258e1b | 431 | |
ocomeni | 75:08eff6258e1b | 432 | /* advertise to everyone */ |
ocomeni | 75:08eff6258e1b | 433 | _ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
ocomeni | 75:08eff6258e1b | 434 | /* how many milliseconds between advertisements, lower interval |
ocomeni | 75:08eff6258e1b | 435 | * increases the chances of being seen at the cost of more power */ |
ocomeni | 76:6afda865fbf8 | 436 | //_ble.gap().setAdvertisingInterval(20); |
ocomeni | 76:6afda865fbf8 | 437 | //_ble.gap().setAdvertisingTimeout(0); |
ocomeni | 78:07bb86e3ce14 | 438 | _ble.gap().setAdvertisingInterval(ble_config.advInterval); /* setting in ble_config */ |
ocomeni | 78:07bb86e3ce14 | 439 | _ble.gap().setAdvertisingTimeout(ble_config.advTimeout); /* setting in ble_config */ |
ocomeni | 75:08eff6258e1b | 440 | |
ocomeni | 113:888e262ff0a9 | 441 | error = _ble.gap().startAdvertising(ble::LEGACY_ADVERTISING_HANDLE); |
ocomeni | 75:08eff6258e1b | 442 | |
ocomeni | 75:08eff6258e1b | 443 | if (error) { |
ocomeni | 103:7b566b522427 | 444 | dbg_printf(LOG, "Error during Gap::startAdvertising.\r\n"); |
ocomeni | 75:08eff6258e1b | 445 | return; |
ocomeni | 75:08eff6258e1b | 446 | } |
ocomeni | 75:08eff6258e1b | 447 | |
ocomeni | 103:7b566b522427 | 448 | dbg_printf(LOG, "Please connect to device\r\n"); |
ocomeni | 75:08eff6258e1b | 449 | |
ocomeni | 75:08eff6258e1b | 450 | /** This tells the stack to generate a pairingRequest event |
ocomeni | 75:08eff6258e1b | 451 | * which will require this application to respond before pairing |
ocomeni | 75:08eff6258e1b | 452 | * can proceed. Setting it to false will automatically accept |
ocomeni | 75:08eff6258e1b | 453 | * pairing. */ |
ocomeni | 75:08eff6258e1b | 454 | _ble.securityManager().setPairingRequestAuthorisation(true); |
ocomeni | 75:08eff6258e1b | 455 | } |
ocomeni | 75:08eff6258e1b | 456 | |
ocomeni | 75:08eff6258e1b | 457 | /** This is called by Gap to notify the application we connected, |
ocomeni | 75:08eff6258e1b | 458 | * in our case it immediately requests a change in link security */ |
ocomeni | 75:08eff6258e1b | 459 | void SMDevicePeripheral::on_connect(const Gap::ConnectionCallbackParams_t *connection_event) |
ocomeni | 75:08eff6258e1b | 460 | { |
ocomeni | 75:08eff6258e1b | 461 | ble_error_t error; |
ocomeni | 75:08eff6258e1b | 462 | |
ocomeni | 75:08eff6258e1b | 463 | /* remember the device that connects to us now so we can connect to it |
ocomeni | 75:08eff6258e1b | 464 | * during the next demonstration */ |
ocomeni | 75:08eff6258e1b | 465 | memcpy(_peer_address, connection_event->peerAddr, sizeof(_peer_address)); |
ocomeni | 75:08eff6258e1b | 466 | |
ocomeni | 103:7b566b522427 | 467 | dbg_printf(LOG, "Connected to: %02x:%02x:%02x:%02x:%02x:%02x\r\n", |
ocomeni | 75:08eff6258e1b | 468 | _peer_address[5], _peer_address[4], _peer_address[3], |
ocomeni | 75:08eff6258e1b | 469 | _peer_address[2], _peer_address[1], _peer_address[0]); |
ocomeni | 75:08eff6258e1b | 470 | |
ocomeni | 75:08eff6258e1b | 471 | /* store the handle for future Security Manager requests */ |
ocomeni | 75:08eff6258e1b | 472 | _handle = connection_event->handle; |
ocomeni | 75:08eff6258e1b | 473 | |
ocomeni | 75:08eff6258e1b | 474 | /* Request a change in link security. This will be done |
ocomeni | 75:08eff6258e1b | 475 | * indirectly by asking the master of the connection to |
ocomeni | 75:08eff6258e1b | 476 | * change it. Depending on circumstances different actions |
ocomeni | 75:08eff6258e1b | 477 | * may be taken by the master which will trigger events |
ocomeni | 75:08eff6258e1b | 478 | * which the applications should deal with. */ |
ocomeni | 75:08eff6258e1b | 479 | error = _ble.securityManager().setLinkSecurity( |
ocomeni | 75:08eff6258e1b | 480 | _handle, |
ocomeni | 75:08eff6258e1b | 481 | SecurityManager::SECURITY_MODE_ENCRYPTION_NO_MITM |
ocomeni | 75:08eff6258e1b | 482 | ); |
ocomeni | 75:08eff6258e1b | 483 | |
ocomeni | 75:08eff6258e1b | 484 | if (error) { |
ocomeni | 103:7b566b522427 | 485 | dbg_printf(LOG, "Error during SM::setLinkSecurity %d\r\n", error); |
ocomeni | 75:08eff6258e1b | 486 | return; |
ocomeni | 75:08eff6258e1b | 487 | } |
ocomeni | 103:7b566b522427 | 488 | dbg_printf(LOG, "SM::setLinkSecurity setup\r\n"); |
ocomeni | 113:888e262ff0a9 | 489 | isConnected = true; |
ocomeni | 75:08eff6258e1b | 490 | } |
ocomeni | 75:08eff6258e1b | 491 | |
ocomeni | 77:0b505d1e15f4 | 492 | void SMDevicePeripheral::stopAdvertising() |
ocomeni | 77:0b505d1e15f4 | 493 | { |
ocomeni | 77:0b505d1e15f4 | 494 | if (_ble.hasInitialized()) { |
ocomeni | 77:0b505d1e15f4 | 495 | ble_error_t error; |
ocomeni | 113:888e262ff0a9 | 496 | error = _ble.gap().stopAdvertising(ble::LEGACY_ADVERTISING_HANDLE);; |
ocomeni | 77:0b505d1e15f4 | 497 | if(error){ |
ocomeni | 103:7b566b522427 | 498 | dbg_printf(LOG, " Error stopping advertising...\r\n"); |
ocomeni | 77:0b505d1e15f4 | 499 | return; |
ocomeni | 77:0b505d1e15f4 | 500 | } |
ocomeni | 103:7b566b522427 | 501 | dbg_printf(LOG, "Stopping advertising...\r\n"); |
ocomeni | 77:0b505d1e15f4 | 502 | //_event_queue.break_dispatch(); |
ocomeni | 77:0b505d1e15f4 | 503 | } |
ocomeni | 77:0b505d1e15f4 | 504 | } |
ocomeni | 77:0b505d1e15f4 | 505 | void SMDevicePeripheral::startAdvertising() |
ocomeni | 77:0b505d1e15f4 | 506 | { |
ocomeni | 77:0b505d1e15f4 | 507 | if (_ble.hasInitialized()) { |
ocomeni | 77:0b505d1e15f4 | 508 | ble_error_t error; |
ocomeni | 113:888e262ff0a9 | 509 | error = _ble.gap().startAdvertising(ble::LEGACY_ADVERTISING_HANDLE); |
ocomeni | 77:0b505d1e15f4 | 510 | if(error){ |
ocomeni | 103:7b566b522427 | 511 | dbg_printf(LOG, " Error Restarting advertising...\r\n"); |
ocomeni | 77:0b505d1e15f4 | 512 | return; |
ocomeni | 77:0b505d1e15f4 | 513 | } |
ocomeni | 103:7b566b522427 | 514 | dbg_printf(LOG, "Restarting advertising...\r\n"); |
ocomeni | 77:0b505d1e15f4 | 515 | //_event_queue.break_dispatch(); |
ocomeni | 77:0b505d1e15f4 | 516 | } |
ocomeni | 77:0b505d1e15f4 | 517 | } |
ocomeni | 77:0b505d1e15f4 | 518 | |
ocomeni | 77:0b505d1e15f4 | 519 | |
ocomeni | 77:0b505d1e15f4 | 520 | |
ocomeni | 75:08eff6258e1b | 521 | /** A central device will scan, connect to a peer and request pairing. */ |
ocomeni | 75:08eff6258e1b | 522 | |
ocomeni | 78:07bb86e3ce14 | 523 | SMDeviceCentral::SMDeviceCentral(BLE &ble, events::EventQueue &event_queue, BLEProtocol::AddressBytes_t &peer_address, ble_config_t ble_config) |
ocomeni | 78:07bb86e3ce14 | 524 | : SMDevice(ble, event_queue, peer_address, ble_config) { }; |
ocomeni | 75:08eff6258e1b | 525 | |
ocomeni | 75:08eff6258e1b | 526 | void SMDeviceCentral::start() |
ocomeni | 75:08eff6258e1b | 527 | { |
ocomeni | 75:08eff6258e1b | 528 | /* start scanning and attach a callback that will handle advertisements |
ocomeni | 75:08eff6258e1b | 529 | * and scan requests responses */ |
ocomeni | 75:08eff6258e1b | 530 | ble_error_t error = _ble.gap().startScan(this, &SMDeviceCentral::on_scan); |
ocomeni | 75:08eff6258e1b | 531 | |
ocomeni | 103:7b566b522427 | 532 | dbg_printf(LOG, "Please advertise\r\n"); |
ocomeni | 75:08eff6258e1b | 533 | |
ocomeni | 103:7b566b522427 | 534 | dbg_printf(LOG, "Scanning for: %02x:%02x:%02x:%02x:%02x:%02x\r\n", |
ocomeni | 75:08eff6258e1b | 535 | _peer_address[5], _peer_address[4], _peer_address[3], |
ocomeni | 75:08eff6258e1b | 536 | _peer_address[2], _peer_address[1], _peer_address[0]); |
ocomeni | 75:08eff6258e1b | 537 | |
ocomeni | 75:08eff6258e1b | 538 | if (error) { |
ocomeni | 103:7b566b522427 | 539 | dbg_printf(LOG, "Error during Gap::startScan %d\r\n", error); |
ocomeni | 75:08eff6258e1b | 540 | return; |
ocomeni | 75:08eff6258e1b | 541 | } |
ocomeni | 75:08eff6258e1b | 542 | } |
ocomeni | 75:08eff6258e1b | 543 | |
ocomeni | 75:08eff6258e1b | 544 | /** Look at scan payload to find a peer device and connect to it */ |
ocomeni | 75:08eff6258e1b | 545 | void SMDeviceCentral::on_scan(const Gap::AdvertisementCallbackParams_t *params) |
ocomeni | 75:08eff6258e1b | 546 | { |
ocomeni | 75:08eff6258e1b | 547 | /* don't bother with analysing scan result if we're already connecting */ |
ocomeni | 75:08eff6258e1b | 548 | if (_is_connecting) { |
ocomeni | 75:08eff6258e1b | 549 | return; |
ocomeni | 75:08eff6258e1b | 550 | } |
ocomeni | 75:08eff6258e1b | 551 | |
ocomeni | 75:08eff6258e1b | 552 | /* connect to the same device that connected to us */ |
ocomeni | 75:08eff6258e1b | 553 | if (memcmp(params->peerAddr, _peer_address, sizeof(_peer_address)) == 0) { |
ocomeni | 75:08eff6258e1b | 554 | |
ocomeni | 75:08eff6258e1b | 555 | ble_error_t error = _ble.gap().connect( |
ocomeni | 75:08eff6258e1b | 556 | params->peerAddr, params->peerAddrType, |
ocomeni | 75:08eff6258e1b | 557 | NULL, NULL |
ocomeni | 75:08eff6258e1b | 558 | ); |
ocomeni | 75:08eff6258e1b | 559 | |
ocomeni | 75:08eff6258e1b | 560 | if (error) { |
ocomeni | 103:7b566b522427 | 561 | dbg_printf(LOG, "Error during Gap::connect %d\r\n", error); |
ocomeni | 75:08eff6258e1b | 562 | return; |
ocomeni | 75:08eff6258e1b | 563 | } |
ocomeni | 75:08eff6258e1b | 564 | |
ocomeni | 103:7b566b522427 | 565 | dbg_printf(LOG, "Connecting... "); |
ocomeni | 75:08eff6258e1b | 566 | |
ocomeni | 75:08eff6258e1b | 567 | /* we may have already scan events waiting |
ocomeni | 75:08eff6258e1b | 568 | * to be processed so we need to remember |
ocomeni | 75:08eff6258e1b | 569 | * that we are already connecting and ignore them */ |
ocomeni | 75:08eff6258e1b | 570 | _is_connecting = true; |
ocomeni | 75:08eff6258e1b | 571 | |
ocomeni | 75:08eff6258e1b | 572 | return; |
ocomeni | 75:08eff6258e1b | 573 | } |
ocomeni | 75:08eff6258e1b | 574 | } |
ocomeni | 75:08eff6258e1b | 575 | |
ocomeni | 75:08eff6258e1b | 576 | /** This is called by Gap to notify the application we connected, |
ocomeni | 75:08eff6258e1b | 577 | * in our case it immediately request pairing */ |
ocomeni | 75:08eff6258e1b | 578 | void SMDeviceCentral::on_connect(const Gap::ConnectionCallbackParams_t *connection_event) |
ocomeni | 75:08eff6258e1b | 579 | { |
ocomeni | 75:08eff6258e1b | 580 | ble_error_t error; |
ocomeni | 75:08eff6258e1b | 581 | |
ocomeni | 75:08eff6258e1b | 582 | /* store the handle for future Security Manager requests */ |
ocomeni | 75:08eff6258e1b | 583 | _handle = connection_event->handle; |
ocomeni | 75:08eff6258e1b | 584 | |
ocomeni | 75:08eff6258e1b | 585 | /* in this example the local device is the master so we request pairing */ |
ocomeni | 75:08eff6258e1b | 586 | error = _ble.securityManager().requestPairing(_handle); |
ocomeni | 75:08eff6258e1b | 587 | |
ocomeni | 103:7b566b522427 | 588 | dbg_printf(LOG, "Connected\r\n"); |
ocomeni | 75:08eff6258e1b | 589 | |
ocomeni | 75:08eff6258e1b | 590 | if (error) { |
ocomeni | 103:7b566b522427 | 591 | dbg_printf(LOG, "Error during SM::requestPairing %d\r\n", error); |
ocomeni | 75:08eff6258e1b | 592 | return; |
ocomeni | 75:08eff6258e1b | 593 | } |
ocomeni | 75:08eff6258e1b | 594 | |
ocomeni | 75:08eff6258e1b | 595 | /* upon pairing success the application will disconnect */ |
ocomeni | 75:08eff6258e1b | 596 | } |
ocomeni | 75:08eff6258e1b | 597 | |
ocomeni | 75:08eff6258e1b | 598 | |
ocomeni | 75:08eff6258e1b | 599 | |
ocomeni | 75:08eff6258e1b | 600 | #if MBED_CONF_APP_FILESYSTEM_SUPPORT |
ocomeni | 75:08eff6258e1b | 601 | bool create_filesystem() |
ocomeni | 75:08eff6258e1b | 602 | { |
ocomeni | 75:08eff6258e1b | 603 | static LittleFileSystem fs("fs"); |
ocomeni | 75:08eff6258e1b | 604 | |
ocomeni | 75:08eff6258e1b | 605 | /* replace this with any physical block device your board supports (like an SD card) */ |
ocomeni | 75:08eff6258e1b | 606 | static HeapBlockDevice bd(4096, 256); |
ocomeni | 75:08eff6258e1b | 607 | |
ocomeni | 75:08eff6258e1b | 608 | int err = bd.init(); |
ocomeni | 75:08eff6258e1b | 609 | |
ocomeni | 75:08eff6258e1b | 610 | if (err) { |
ocomeni | 75:08eff6258e1b | 611 | return false; |
ocomeni | 75:08eff6258e1b | 612 | } |
ocomeni | 75:08eff6258e1b | 613 | |
ocomeni | 75:08eff6258e1b | 614 | err = bd.erase(0, bd.size()); |
ocomeni | 75:08eff6258e1b | 615 | |
ocomeni | 75:08eff6258e1b | 616 | if (err) { |
ocomeni | 75:08eff6258e1b | 617 | return false; |
ocomeni | 75:08eff6258e1b | 618 | } |
ocomeni | 75:08eff6258e1b | 619 | |
ocomeni | 75:08eff6258e1b | 620 | err = fs.mount(&bd); |
ocomeni | 75:08eff6258e1b | 621 | |
ocomeni | 75:08eff6258e1b | 622 | if (err) { |
ocomeni | 75:08eff6258e1b | 623 | /* Reformat if we can't mount the filesystem */ |
ocomeni | 103:7b566b522427 | 624 | dbg_printf(LOG, "No filesystem found, formatting...\r\n"); |
ocomeni | 75:08eff6258e1b | 625 | |
ocomeni | 75:08eff6258e1b | 626 | err = fs.reformat(&bd); |
ocomeni | 75:08eff6258e1b | 627 | |
ocomeni | 75:08eff6258e1b | 628 | if (err) { |
ocomeni | 75:08eff6258e1b | 629 | return false; |
ocomeni | 75:08eff6258e1b | 630 | } |
ocomeni | 75:08eff6258e1b | 631 | } |
ocomeni | 75:08eff6258e1b | 632 | |
ocomeni | 75:08eff6258e1b | 633 | return true; |
ocomeni | 75:08eff6258e1b | 634 | } |
ocomeni | 75:08eff6258e1b | 635 | #endif //MBED_CONF_APP_FILESYSTEM_SUPPORT |
ocomeni | 75:08eff6258e1b | 636 | #ifdef BLE_SECURITY_MAIN |
ocomeni | 75:08eff6258e1b | 637 | int main() |
ocomeni | 75:08eff6258e1b | 638 | { |
ocomeni | 75:08eff6258e1b | 639 | BLE& ble = BLE::Instance(); |
ocomeni | 75:08eff6258e1b | 640 | events::EventQueue queue; |
ocomeni | 75:08eff6258e1b | 641 | |
ocomeni | 75:08eff6258e1b | 642 | #if MBED_CONF_APP_FILESYSTEM_SUPPORT |
ocomeni | 75:08eff6258e1b | 643 | /* if filesystem creation fails or there is no filesystem the security manager |
ocomeni | 75:08eff6258e1b | 644 | * will fallback to storing the security database in memory */ |
ocomeni | 75:08eff6258e1b | 645 | if (!create_filesystem()) { |
ocomeni | 103:7b566b522427 | 646 | dbg_printf(LOG, "Filesystem creation failed, will use memory storage\r\n"); |
ocomeni | 75:08eff6258e1b | 647 | } |
ocomeni | 75:08eff6258e1b | 648 | #endif |
ocomeni | 75:08eff6258e1b | 649 | |
ocomeni | 75:08eff6258e1b | 650 | while(1) { |
ocomeni | 75:08eff6258e1b | 651 | { |
ocomeni | 103:7b566b522427 | 652 | dbg_printf(LOG, "\r\n PERIPHERAL \r\n\r\n"); |
ocomeni | 75:08eff6258e1b | 653 | SMDevicePeripheral peripheral(ble, queue, peer_address); |
ocomeni | 75:08eff6258e1b | 654 | peripheral.run(); |
ocomeni | 75:08eff6258e1b | 655 | } |
ocomeni | 75:08eff6258e1b | 656 | |
ocomeni | 75:08eff6258e1b | 657 | { |
ocomeni | 103:7b566b522427 | 658 | dbg_printf(LOG, "\r\n CENTRAL \r\n\r\n"); |
ocomeni | 75:08eff6258e1b | 659 | SMDeviceCentral central(ble, queue, peer_address); |
ocomeni | 75:08eff6258e1b | 660 | central.run(); |
ocomeni | 75:08eff6258e1b | 661 | } |
ocomeni | 75:08eff6258e1b | 662 | } |
ocomeni | 75:08eff6258e1b | 663 | |
ocomeni | 75:08eff6258e1b | 664 | return 0; |
ocomeni | 75:08eff6258e1b | 665 | } |
ocomeni | 75:08eff6258e1b | 666 | #endif |