
this is using the mbed os version 5-13-1
source/BleManager.cpp@119:8d939a902333, 2019-05-27 (annotated)
- Committer:
- ocomeni
- Date:
- Mon May 27 12:34:58 2019 +0000
- Branch:
- PassingRegression
- Revision:
- 119:8d939a902333
- Parent:
- 118:8df0e9c2ee3f
- Child:
- 120:779b74689747
- implemented cloud connection keep-alive mechanism; - implemented BLE manager state machine for processing messages from radio RX and AT commands.; this version passes end-2-end testing
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 | 119:8d939a902333 | 35 | |
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 | 118:8df0e9c2ee3f | 64 | BLEProtocol::AddressBytes_t &peer_address, |
ocomeni | 118:8df0e9c2ee3f | 65 | MemoryPool<at_ble_msg_t, PQDSZ_BLE> *aT2BleDatamPool, |
ocomeni | 118:8df0e9c2ee3f | 66 | Queue<at_ble_msg_t, PQDSZ_BLE> *aT2BleDataQueue, |
ocomeni | 118:8df0e9c2ee3f | 67 | MemoryPool<ble_at_msg_t, PQDSZ_BLE> *ble2ATDatamPool, |
ocomeni | 118:8df0e9c2ee3f | 68 | Queue<ble_at_msg_t, PQDSZ_BLE> *ble2ATDataQueue, |
ocomeni | 118:8df0e9c2ee3f | 69 | ble_config_t *ble_config) : |
ocomeni | 75:08eff6258e1b | 70 | _ble(ble), |
ocomeni | 75:08eff6258e1b | 71 | _event_queue(event_queue), |
ocomeni | 75:08eff6258e1b | 72 | _peer_address(peer_address), |
ocomeni | 118:8df0e9c2ee3f | 73 | _aT2BleDatamPool (aT2BleDatamPool), |
ocomeni | 118:8df0e9c2ee3f | 74 | _aT2BleDataQueue (aT2BleDataQueue), |
ocomeni | 118:8df0e9c2ee3f | 75 | _ble2ATDatamPool (ble2ATDatamPool), |
ocomeni | 118:8df0e9c2ee3f | 76 | _ble2ATDataQueue (ble2ATDataQueue), |
ocomeni | 78:07bb86e3ce14 | 77 | ble_config(ble_config), |
ocomeni | 75:08eff6258e1b | 78 | _handle(0), |
ocomeni | 113:888e262ff0a9 | 79 | _is_connecting(false), |
ocomeni | 113:888e262ff0a9 | 80 | _led1(LED1, 0) |
ocomeni | 113:888e262ff0a9 | 81 | { |
ocomeni | 113:888e262ff0a9 | 82 | isConnected = false; |
ocomeni | 113:888e262ff0a9 | 83 | } |
ocomeni | 75:08eff6258e1b | 84 | |
ocomeni | 75:08eff6258e1b | 85 | SMDevice::~SMDevice() |
ocomeni | 75:08eff6258e1b | 86 | { |
ocomeni | 75:08eff6258e1b | 87 | if (_ble.hasInitialized()) { |
ocomeni | 75:08eff6258e1b | 88 | _ble.shutdown(); |
ocomeni | 75:08eff6258e1b | 89 | } |
ocomeni | 75:08eff6258e1b | 90 | } |
ocomeni | 75:08eff6258e1b | 91 | |
ocomeni | 75:08eff6258e1b | 92 | /** Start BLE interface initialisation */ |
ocomeni | 75:08eff6258e1b | 93 | void SMDevice::run() |
ocomeni | 75:08eff6258e1b | 94 | { |
ocomeni | 103:7b566b522427 | 95 | dbg_printf(LOG, "\r\n [BTLE MAN] Thread Id = %X\r\n", (uint32_t)ThisThread::get_id()); |
ocomeni | 87:99b37d26ff2a | 96 | |
ocomeni | 75:08eff6258e1b | 97 | ble_error_t error; |
ocomeni | 75:08eff6258e1b | 98 | |
ocomeni | 108:3c8fb2c6e7bf | 99 | /* to show we're running we'll blink every 10secs */ |
ocomeni | 119:8d939a902333 | 100 | //_event_queue.call_every(10000, this, &SMDevice::blink); |
ocomeni | 75:08eff6258e1b | 101 | |
ocomeni | 77:0b505d1e15f4 | 102 | /* to show we're advertising we'll print status every minute */ |
ocomeni | 119:8d939a902333 | 103 | //_event_queue.call_every(60000, this, &SMDevice::reportGapState); |
ocomeni | 119:8d939a902333 | 104 | |
ocomeni | 119:8d939a902333 | 105 | /* process queues every BLE_PROCESS_QUEUES_INTERVAL_MS */ |
ocomeni | 119:8d939a902333 | 106 | _event_queue.call_every(BLE_PROCESS_QUEUES_INTERVAL_MS, this, &SMDevice::processQueues); |
ocomeni | 77:0b505d1e15f4 | 107 | |
ocomeni | 77:0b505d1e15f4 | 108 | |
ocomeni | 77:0b505d1e15f4 | 109 | |
ocomeni | 75:08eff6258e1b | 110 | if (_ble.hasInitialized()) { |
ocomeni | 103:7b566b522427 | 111 | dbg_printf(LOG, "Ble instance already initialised.\r\n"); |
ocomeni | 75:08eff6258e1b | 112 | return; |
ocomeni | 75:08eff6258e1b | 113 | } |
ocomeni | 75:08eff6258e1b | 114 | |
ocomeni | 75:08eff6258e1b | 115 | /* this will inform us off all events so we can schedule their handling |
ocomeni | 75:08eff6258e1b | 116 | * using our event queue */ |
ocomeni | 75:08eff6258e1b | 117 | _ble.onEventsToProcess( |
ocomeni | 75:08eff6258e1b | 118 | makeFunctionPointer(this, &SMDevice::schedule_ble_events) |
ocomeni | 75:08eff6258e1b | 119 | ); |
ocomeni | 75:08eff6258e1b | 120 | |
ocomeni | 75:08eff6258e1b | 121 | /* handle timeouts, for example when connection attempts fail */ |
ocomeni | 75:08eff6258e1b | 122 | _ble.gap().onTimeout( |
ocomeni | 75:08eff6258e1b | 123 | makeFunctionPointer(this, &SMDevice::on_timeout) |
ocomeni | 75:08eff6258e1b | 124 | ); |
ocomeni | 75:08eff6258e1b | 125 | |
ocomeni | 75:08eff6258e1b | 126 | error = _ble.init(this, &SMDevice::on_init_complete); |
ocomeni | 75:08eff6258e1b | 127 | |
ocomeni | 75:08eff6258e1b | 128 | if (error) { |
ocomeni | 103:7b566b522427 | 129 | dbg_printf(LOG, "Error returned by BLE::init.\r\n"); |
ocomeni | 75:08eff6258e1b | 130 | return; |
ocomeni | 75:08eff6258e1b | 131 | } |
ocomeni | 75:08eff6258e1b | 132 | |
ocomeni | 75:08eff6258e1b | 133 | /* this will not return until shutdown */ |
ocomeni | 77:0b505d1e15f4 | 134 | //_event_queue.dispatch_forever(); |
ocomeni | 75:08eff6258e1b | 135 | } |
ocomeni | 75:08eff6258e1b | 136 | |
ocomeni | 77:0b505d1e15f4 | 137 | |
ocomeni | 77:0b505d1e15f4 | 138 | void SMDevice::shutDown() |
ocomeni | 77:0b505d1e15f4 | 139 | { |
ocomeni | 77:0b505d1e15f4 | 140 | if (_ble.hasInitialized()) { |
ocomeni | 77:0b505d1e15f4 | 141 | _ble.shutdown(); |
ocomeni | 103:7b566b522427 | 142 | dbg_printf(LOG, "Shutting down BLE Instance...\r\n"); |
ocomeni | 77:0b505d1e15f4 | 143 | _event_queue.break_dispatch(); |
ocomeni | 77:0b505d1e15f4 | 144 | } |
ocomeni | 77:0b505d1e15f4 | 145 | } |
ocomeni | 77:0b505d1e15f4 | 146 | |
ocomeni | 77:0b505d1e15f4 | 147 | |
ocomeni | 75:08eff6258e1b | 148 | /* event handler functions */ |
ocomeni | 75:08eff6258e1b | 149 | |
ocomeni | 75:08eff6258e1b | 150 | /** Respond to a pairing request. This will be called by the stack |
ocomeni | 75:08eff6258e1b | 151 | * when a pairing request arrives and expects the application to |
ocomeni | 75:08eff6258e1b | 152 | * call acceptPairingRequest or cancelPairingRequest */ |
ocomeni | 75:08eff6258e1b | 153 | void SMDevice::pairingRequest( |
ocomeni | 75:08eff6258e1b | 154 | ble::connection_handle_t connectionHandle |
ocomeni | 75:08eff6258e1b | 155 | ) { |
ocomeni | 103:7b566b522427 | 156 | dbg_printf(LOG, "Pairing requested - authorising\r\n"); |
ocomeni | 75:08eff6258e1b | 157 | _ble.securityManager().acceptPairingRequest(connectionHandle); |
ocomeni | 75:08eff6258e1b | 158 | } |
ocomeni | 75:08eff6258e1b | 159 | |
ocomeni | 75:08eff6258e1b | 160 | /** Inform the application of a successful pairing. Terminate the demonstration. */ |
ocomeni | 75:08eff6258e1b | 161 | void SMDevice::pairingResult( |
ocomeni | 75:08eff6258e1b | 162 | ble::connection_handle_t connectionHandle, |
ocomeni | 75:08eff6258e1b | 163 | SecurityManager::SecurityCompletionStatus_t result |
ocomeni | 75:08eff6258e1b | 164 | ) { |
ocomeni | 75:08eff6258e1b | 165 | if (result == SecurityManager::SEC_STATUS_SUCCESS) { |
ocomeni | 103:7b566b522427 | 166 | dbg_printf(LOG, "Pairing successful\r\n"); |
ocomeni | 75:08eff6258e1b | 167 | } else { |
ocomeni | 103:7b566b522427 | 168 | dbg_printf(LOG, "Pairing failed\r\n"); |
ocomeni | 75:08eff6258e1b | 169 | } |
ocomeni | 75:08eff6258e1b | 170 | } |
ocomeni | 75:08eff6258e1b | 171 | |
ocomeni | 75:08eff6258e1b | 172 | /** Inform the application of change in encryption status. This will be |
ocomeni | 75:08eff6258e1b | 173 | * communicated through the serial port */ |
ocomeni | 75:08eff6258e1b | 174 | void SMDevice::linkEncryptionResult( |
ocomeni | 75:08eff6258e1b | 175 | ble::connection_handle_t connectionHandle, |
ocomeni | 75:08eff6258e1b | 176 | ble::link_encryption_t result |
ocomeni | 75:08eff6258e1b | 177 | ) { |
ocomeni | 75:08eff6258e1b | 178 | if (result == ble::link_encryption_t::ENCRYPTED) { |
ocomeni | 103:7b566b522427 | 179 | dbg_printf(LOG, "Link ENCRYPTED\r\n"); |
ocomeni | 75:08eff6258e1b | 180 | } else if (result == ble::link_encryption_t::ENCRYPTED_WITH_MITM) { |
ocomeni | 103:7b566b522427 | 181 | dbg_printf(LOG, "Link ENCRYPTED_WITH_MITM\r\n"); |
ocomeni | 75:08eff6258e1b | 182 | } else if (result == ble::link_encryption_t::NOT_ENCRYPTED) { |
ocomeni | 103:7b566b522427 | 183 | dbg_printf(LOG, "Link NOT_ENCRYPTED\r\n"); |
ocomeni | 75:08eff6258e1b | 184 | } |
ocomeni | 75:08eff6258e1b | 185 | |
ocomeni | 76:6afda865fbf8 | 186 | #ifdef DEMO_BLE_SECURITY |
ocomeni | 75:08eff6258e1b | 187 | /* disconnect in 2 s */ |
ocomeni | 75:08eff6258e1b | 188 | _event_queue.call_in( |
ocomeni | 75:08eff6258e1b | 189 | 2000, &_ble.gap(), |
ocomeni | 75:08eff6258e1b | 190 | &Gap::disconnect, _handle, Gap::REMOTE_USER_TERMINATED_CONNECTION |
ocomeni | 75:08eff6258e1b | 191 | ); |
ocomeni | 76:6afda865fbf8 | 192 | #endif |
ocomeni | 75:08eff6258e1b | 193 | } |
ocomeni | 75:08eff6258e1b | 194 | |
ocomeni | 75:08eff6258e1b | 195 | /** Override to start chosen activity when initialisation completes */ |
ocomeni | 75:08eff6258e1b | 196 | //void SMDevice::start() = 0; |
ocomeni | 75:08eff6258e1b | 197 | |
ocomeni | 75:08eff6258e1b | 198 | /** This is called when BLE interface is initialised and starts the demonstration */ |
ocomeni | 75:08eff6258e1b | 199 | void SMDevice::on_init_complete(BLE::InitializationCompleteCallbackContext *event) |
ocomeni | 75:08eff6258e1b | 200 | { |
ocomeni | 75:08eff6258e1b | 201 | ble_error_t error; |
ocomeni | 75:08eff6258e1b | 202 | |
ocomeni | 75:08eff6258e1b | 203 | if (event->error) { |
ocomeni | 103:7b566b522427 | 204 | dbg_printf(LOG, "Error during the initialisation\r\n"); |
ocomeni | 75:08eff6258e1b | 205 | return; |
ocomeni | 75:08eff6258e1b | 206 | } |
ocomeni | 75:08eff6258e1b | 207 | |
ocomeni | 75:08eff6258e1b | 208 | /* This path will be used to store bonding information but will fallback |
ocomeni | 75:08eff6258e1b | 209 | * to storing in memory if file access fails (for example due to lack of a filesystem) */ |
ocomeni | 75:08eff6258e1b | 210 | const char* db_path = "/fs/bt_sec_db"; |
ocomeni | 75:08eff6258e1b | 211 | /* If the security manager is required this needs to be called before any |
ocomeni | 75:08eff6258e1b | 212 | * calls to the Security manager happen. */ |
ocomeni | 75:08eff6258e1b | 213 | error = _ble.securityManager().init( |
ocomeni | 75:08eff6258e1b | 214 | true, |
ocomeni | 75:08eff6258e1b | 215 | false, |
ocomeni | 79:a2187bbfa407 | 216 | SecurityManager::IO_CAPS_DISPLAY_ONLY, // SecurityManager::IO_CAPS_NONE |
ocomeni | 116:2296cf274661 | 217 | ble_config->pairingKey, |
ocomeni | 75:08eff6258e1b | 218 | false, |
ocomeni | 75:08eff6258e1b | 219 | db_path |
ocomeni | 75:08eff6258e1b | 220 | ); |
ocomeni | 75:08eff6258e1b | 221 | |
ocomeni | 75:08eff6258e1b | 222 | if (error) { |
ocomeni | 103:7b566b522427 | 223 | dbg_printf(LOG, "Error during init %d\r\n", error); |
ocomeni | 75:08eff6258e1b | 224 | return; |
ocomeni | 75:08eff6258e1b | 225 | } |
ocomeni | 75:08eff6258e1b | 226 | |
ocomeni | 75:08eff6258e1b | 227 | error = _ble.securityManager().preserveBondingStateOnReset(true); |
ocomeni | 75:08eff6258e1b | 228 | |
ocomeni | 75:08eff6258e1b | 229 | if (error) { |
ocomeni | 103:7b566b522427 | 230 | dbg_printf(LOG, "Error during preserveBondingStateOnReset %d\r\n", error); |
ocomeni | 75:08eff6258e1b | 231 | } |
ocomeni | 75:08eff6258e1b | 232 | |
ocomeni | 75:08eff6258e1b | 233 | #if MBED_CONF_APP_FILESYSTEM_SUPPORT |
ocomeni | 75:08eff6258e1b | 234 | /* Enable privacy so we can find the keys */ |
ocomeni | 75:08eff6258e1b | 235 | error = _ble.gap().enablePrivacy(true); |
ocomeni | 75:08eff6258e1b | 236 | |
ocomeni | 75:08eff6258e1b | 237 | if (error) { |
ocomeni | 103:7b566b522427 | 238 | dbg_printf(LOG, "Error enabling privacy\r\n"); |
ocomeni | 75:08eff6258e1b | 239 | } |
ocomeni | 75:08eff6258e1b | 240 | |
ocomeni | 75:08eff6258e1b | 241 | Gap::PeripheralPrivacyConfiguration_t configuration_p = { |
ocomeni | 75:08eff6258e1b | 242 | /* use_non_resolvable_random_address */ false, |
ocomeni | 75:08eff6258e1b | 243 | Gap::PeripheralPrivacyConfiguration_t::REJECT_NON_RESOLVED_ADDRESS |
ocomeni | 75:08eff6258e1b | 244 | }; |
ocomeni | 75:08eff6258e1b | 245 | _ble.gap().setPeripheralPrivacyConfiguration(&configuration_p); |
ocomeni | 75:08eff6258e1b | 246 | |
ocomeni | 75:08eff6258e1b | 247 | Gap::CentralPrivacyConfiguration_t configuration_c = { |
ocomeni | 75:08eff6258e1b | 248 | /* use_non_resolvable_random_address */ false, |
ocomeni | 75:08eff6258e1b | 249 | Gap::CentralPrivacyConfiguration_t::RESOLVE_AND_FORWARD |
ocomeni | 75:08eff6258e1b | 250 | }; |
ocomeni | 75:08eff6258e1b | 251 | _ble.gap().setCentralPrivacyConfiguration(&configuration_c); |
ocomeni | 75:08eff6258e1b | 252 | |
ocomeni | 75:08eff6258e1b | 253 | /* this demo switches between being master and slave */ |
ocomeni | 75:08eff6258e1b | 254 | _ble.securityManager().setHintFutureRoleReversal(true); |
ocomeni | 75:08eff6258e1b | 255 | #endif |
ocomeni | 75:08eff6258e1b | 256 | |
ocomeni | 75:08eff6258e1b | 257 | /* Tell the security manager to use methods in this class to inform us |
ocomeni | 75:08eff6258e1b | 258 | * of any events. Class needs to implement SecurityManagerEventHandler. */ |
ocomeni | 75:08eff6258e1b | 259 | _ble.securityManager().setSecurityManagerEventHandler(this); |
ocomeni | 75:08eff6258e1b | 260 | |
ocomeni | 75:08eff6258e1b | 261 | /* print device address */ |
ocomeni | 75:08eff6258e1b | 262 | Gap::AddressType_t addr_type; |
ocomeni | 75:08eff6258e1b | 263 | Gap::Address_t addr; |
ocomeni | 75:08eff6258e1b | 264 | _ble.gap().getAddress(&addr_type, addr); |
ocomeni | 103:7b566b522427 | 265 | dbg_printf(LOG, "Device address: %02x:%02x:%02x:%02x:%02x:%02x\r\n", |
ocomeni | 75:08eff6258e1b | 266 | addr[5], addr[4], addr[3], addr[2], addr[1], addr[0]); |
ocomeni | 75:08eff6258e1b | 267 | |
ocomeni | 75:08eff6258e1b | 268 | /* when scanning we want to connect to a peer device so we need to |
ocomeni | 75:08eff6258e1b | 269 | * attach callbacks that are used by Gap to notify us of events */ |
ocomeni | 75:08eff6258e1b | 270 | _ble.gap().onConnection(this, &SMDevice::on_connect); |
ocomeni | 75:08eff6258e1b | 271 | _ble.gap().onDisconnection(this, &SMDevice::on_disconnect); |
ocomeni | 76:6afda865fbf8 | 272 | _ble.gattServer().onDataWritten(this, &SMDevice::onDataWrittenCallback); |
ocomeni | 79:a2187bbfa407 | 273 | //_ble.securityManager().onPasskeyDisplay(this, &SMDevice::passkeyDisplayCallback); |
ocomeni | 79:a2187bbfa407 | 274 | //_ble.securityManager().onSecuritySetupCompleted(this, &SMDevice::securitySetupCompletedCallback); |
ocomeni | 75:08eff6258e1b | 275 | |
ocomeni | 75:08eff6258e1b | 276 | /* start test in 500 ms */ |
ocomeni | 75:08eff6258e1b | 277 | _event_queue.call_in(500, this, &SMDevice::start); |
ocomeni | 75:08eff6258e1b | 278 | } |
ocomeni | 75:08eff6258e1b | 279 | |
ocomeni | 75:08eff6258e1b | 280 | /** This is called by Gap to notify the application we connected */ |
ocomeni | 75:08eff6258e1b | 281 | //void SMDevice::on_connect(const Gap::ConnectionCallbackParams_t *connection_event); |
ocomeni | 75:08eff6258e1b | 282 | |
ocomeni | 75:08eff6258e1b | 283 | /** This is called by Gap to notify the application we disconnected, |
ocomeni | 75:08eff6258e1b | 284 | * in our case it ends the demonstration. */ |
ocomeni | 75:08eff6258e1b | 285 | void SMDevice::on_disconnect(const Gap::DisconnectionCallbackParams_t *event) |
ocomeni | 75:08eff6258e1b | 286 | { |
ocomeni | 103:7b566b522427 | 287 | dbg_printf(LOG, "Disconnected\r\n"); |
ocomeni | 77:0b505d1e15f4 | 288 | #ifndef DEMO_BLE_SECURITY |
ocomeni | 103:7b566b522427 | 289 | dbg_printf(LOG, "Restarting advertising...\r\n"); |
ocomeni | 113:888e262ff0a9 | 290 | _ble.gap().startAdvertising(ble::LEGACY_ADVERTISING_HANDLE); |
ocomeni | 77:0b505d1e15f4 | 291 | #else |
ocomeni | 75:08eff6258e1b | 292 | _event_queue.break_dispatch(); |
ocomeni | 77:0b505d1e15f4 | 293 | #endif |
ocomeni | 113:888e262ff0a9 | 294 | isConnected = false; |
ocomeni | 75:08eff6258e1b | 295 | } |
ocomeni | 75:08eff6258e1b | 296 | |
ocomeni | 75:08eff6258e1b | 297 | /** End demonstration unexpectedly. Called if timeout is reached during advertising, |
ocomeni | 75:08eff6258e1b | 298 | * scanning or connection initiation */ |
ocomeni | 75:08eff6258e1b | 299 | void SMDevice::on_timeout(const Gap::TimeoutSource_t source) |
ocomeni | 75:08eff6258e1b | 300 | { |
ocomeni | 103:7b566b522427 | 301 | dbg_printf(LOG, "Unexpected timeout - aborting\r\n"); |
ocomeni | 75:08eff6258e1b | 302 | _event_queue.break_dispatch(); |
ocomeni | 75:08eff6258e1b | 303 | } |
ocomeni | 75:08eff6258e1b | 304 | |
ocomeni | 75:08eff6258e1b | 305 | /** Schedule processing of events from the BLE in the event queue. */ |
ocomeni | 75:08eff6258e1b | 306 | void SMDevice::schedule_ble_events(BLE::OnEventsToProcessCallbackContext *context) |
ocomeni | 75:08eff6258e1b | 307 | { |
ocomeni | 75:08eff6258e1b | 308 | _event_queue.call(mbed::callback(&context->ble, &BLE::processEvents)); |
ocomeni | 75:08eff6258e1b | 309 | }; |
ocomeni | 75:08eff6258e1b | 310 | |
ocomeni | 76:6afda865fbf8 | 311 | /** Echo received data back */ |
ocomeni | 76:6afda865fbf8 | 312 | void SMDevice::EchoBleUartReceived() |
ocomeni | 76:6afda865fbf8 | 313 | { |
ocomeni | 76:6afda865fbf8 | 314 | uart->writeString(buffer); |
ocomeni | 76:6afda865fbf8 | 315 | uart->writeString("\n"); //flushes uart output buffer and sends data |
ocomeni | 76:6afda865fbf8 | 316 | } |
ocomeni | 76:6afda865fbf8 | 317 | |
ocomeni | 76:6afda865fbf8 | 318 | |
ocomeni | 79:a2187bbfa407 | 319 | /** Send data aynchronously using BLE */ |
ocomeni | 79:a2187bbfa407 | 320 | void SMDevice::sendBLEUartData(char * str) |
ocomeni | 79:a2187bbfa407 | 321 | { |
ocomeni | 113:888e262ff0a9 | 322 | //Gap::GapState_t gapState = _ble.gap().getState(); |
ocomeni | 113:888e262ff0a9 | 323 | //Gap::GapState_t gapState = _ble.getGapState(); |
ocomeni | 113:888e262ff0a9 | 324 | //gapState.connected |
ocomeni | 113:888e262ff0a9 | 325 | if(isConnected){ |
ocomeni | 79:a2187bbfa407 | 326 | uart->writeString(str); |
ocomeni | 79:a2187bbfa407 | 327 | uart->writeString("\n"); //flushes uart output buffer and sends data |
ocomeni | 79:a2187bbfa407 | 328 | } |
ocomeni | 113:888e262ff0a9 | 329 | else |
ocomeni | 113:888e262ff0a9 | 330 | { |
ocomeni | 113:888e262ff0a9 | 331 | dbg_printf(LOG, "BLE not connected\r\n"); |
ocomeni | 113:888e262ff0a9 | 332 | } |
ocomeni | 79:a2187bbfa407 | 333 | } |
ocomeni | 79:a2187bbfa407 | 334 | |
ocomeni | 79:a2187bbfa407 | 335 | |
ocomeni | 76:6afda865fbf8 | 336 | /** |
ocomeni | 79:a2187bbfa407 | 337 | * This callback allows the UARTService to receive updates. |
ocomeni | 76:6afda865fbf8 | 338 | * |
ocomeni | 76:6afda865fbf8 | 339 | * @param[in] params |
ocomeni | 76:6afda865fbf8 | 340 | * Information about the characterisitc being updated. |
ocomeni | 76:6afda865fbf8 | 341 | */ |
ocomeni | 76:6afda865fbf8 | 342 | void SMDevice::onDataWrittenCallback(const GattWriteCallbackParams *params) { |
ocomeni | 76:6afda865fbf8 | 343 | if ((uart != NULL) && (params->handle == uart->getTXCharacteristicHandle())) { |
ocomeni | 76:6afda865fbf8 | 344 | uint16_t bytesRead = params->len; |
ocomeni | 76:6afda865fbf8 | 345 | |
ocomeni | 103:7b566b522427 | 346 | dbg_printf(LOG, "received %u bytes\n\r ", bytesRead); |
ocomeni | 76:6afda865fbf8 | 347 | |
ocomeni | 76:6afda865fbf8 | 348 | if(bytesRead >= 255){ |
ocomeni | 103:7b566b522427 | 349 | dbg_printf(LOG, "Overflow command %u n\r ", bytesRead); |
ocomeni | 76:6afda865fbf8 | 350 | bytesRead = 255; |
ocomeni | 76:6afda865fbf8 | 351 | } |
ocomeni | 76:6afda865fbf8 | 352 | |
ocomeni | 76:6afda865fbf8 | 353 | unsigned index = 0; |
ocomeni | 76:6afda865fbf8 | 354 | for (; index < bytesRead; index++) { |
ocomeni | 76:6afda865fbf8 | 355 | buffer[index] = params->data[index]; |
ocomeni | 76:6afda865fbf8 | 356 | } |
ocomeni | 76:6afda865fbf8 | 357 | |
ocomeni | 76:6afda865fbf8 | 358 | buffer[index++] = 0; |
ocomeni | 76:6afda865fbf8 | 359 | |
ocomeni | 103:7b566b522427 | 360 | dbg_printf(LOG, "Data : %s ",buffer); |
ocomeni | 103:7b566b522427 | 361 | dbg_printf(LOG, "\r\n"); |
ocomeni | 76:6afda865fbf8 | 362 | /* start echo in 50 ms */ |
ocomeni | 76:6afda865fbf8 | 363 | _event_queue.call_in(50, this, &SMDevice::EchoBleUartReceived); |
ocomeni | 76:6afda865fbf8 | 364 | //_event_queue.call(EchoBleUartReceived); |
ocomeni | 76:6afda865fbf8 | 365 | |
ocomeni | 76:6afda865fbf8 | 366 | } |
ocomeni | 76:6afda865fbf8 | 367 | } |
ocomeni | 79:a2187bbfa407 | 368 | |
ocomeni | 79:a2187bbfa407 | 369 | |
ocomeni | 79:a2187bbfa407 | 370 | |
ocomeni | 79:a2187bbfa407 | 371 | |
ocomeni | 75:08eff6258e1b | 372 | /** Blink LED to show we're running */ |
ocomeni | 75:08eff6258e1b | 373 | void SMDevice::blink(void) |
ocomeni | 75:08eff6258e1b | 374 | { |
ocomeni | 75:08eff6258e1b | 375 | _led1 = !_led1; |
ocomeni | 75:08eff6258e1b | 376 | } |
ocomeni | 75:08eff6258e1b | 377 | |
ocomeni | 75:08eff6258e1b | 378 | |
ocomeni | 77:0b505d1e15f4 | 379 | void SMDevice::reportGapState() |
ocomeni | 77:0b505d1e15f4 | 380 | { |
ocomeni | 113:888e262ff0a9 | 381 | //Gap::GapState_t gapState = _ble.gap().getState(); |
ocomeni | 77:0b505d1e15f4 | 382 | char connStr[20] = " Not Connected "; |
ocomeni | 77:0b505d1e15f4 | 383 | char advStr[20] = " Not Advertising "; |
ocomeni | 113:888e262ff0a9 | 384 | //char devName[20] = ""; |
ocomeni | 113:888e262ff0a9 | 385 | //if(gapState.advertising){ |
ocomeni | 113:888e262ff0a9 | 386 | if(_ble.gap().isAdvertisingActive(ble::LEGACY_ADVERTISING_HANDLE)){ |
ocomeni | 77:0b505d1e15f4 | 387 | strncpy(advStr, " Advertising ", 20); |
ocomeni | 77:0b505d1e15f4 | 388 | } |
ocomeni | 113:888e262ff0a9 | 389 | if(isConnected){ |
ocomeni | 77:0b505d1e15f4 | 390 | strncpy(connStr, " Connected ", 20); |
ocomeni | 77:0b505d1e15f4 | 391 | } |
ocomeni | 103:7b566b522427 | 392 | dbg_printf(LOG, "\n Advertising Status = %s\n Connection Status = %s\n", advStr, connStr); |
ocomeni | 77:0b505d1e15f4 | 393 | |
ocomeni | 77:0b505d1e15f4 | 394 | } |
ocomeni | 77:0b505d1e15f4 | 395 | |
ocomeni | 77:0b505d1e15f4 | 396 | |
ocomeni | 75:08eff6258e1b | 397 | /** A peripheral device will advertise, accept the connection and request |
ocomeni | 75:08eff6258e1b | 398 | * a change in link security. */ |
ocomeni | 118:8df0e9c2ee3f | 399 | SMDevicePeripheral::SMDevicePeripheral(BLE &ble, events::EventQueue &event_queue, BLEProtocol::AddressBytes_t &peer_address, |
ocomeni | 118:8df0e9c2ee3f | 400 | MemoryPool<at_ble_msg_t, PQDSZ_BLE> *aT2BleDatamPool, |
ocomeni | 118:8df0e9c2ee3f | 401 | Queue<at_ble_msg_t, PQDSZ_BLE> *aT2BleDataQueue, |
ocomeni | 118:8df0e9c2ee3f | 402 | MemoryPool<ble_at_msg_t, PQDSZ_BLE> *ble2ATDatamPool, |
ocomeni | 118:8df0e9c2ee3f | 403 | Queue<ble_at_msg_t, PQDSZ_BLE> *ble2ATDataQueue, |
ocomeni | 118:8df0e9c2ee3f | 404 | ble_config_t *ble_config) |
ocomeni | 118:8df0e9c2ee3f | 405 | : SMDevice(ble, event_queue, peer_address, |
ocomeni | 118:8df0e9c2ee3f | 406 | aT2BleDatamPool, aT2BleDataQueue, |
ocomeni | 118:8df0e9c2ee3f | 407 | ble2ATDatamPool, ble2ATDataQueue, |
ocomeni | 118:8df0e9c2ee3f | 408 | ble_config) { } |
ocomeni | 75:08eff6258e1b | 409 | |
ocomeni | 75:08eff6258e1b | 410 | void SMDevicePeripheral::start() |
ocomeni | 75:08eff6258e1b | 411 | { |
ocomeni | 75:08eff6258e1b | 412 | /* Set up and start advertising */ |
ocomeni | 75:08eff6258e1b | 413 | |
ocomeni | 75:08eff6258e1b | 414 | ble_error_t error; |
ocomeni | 75:08eff6258e1b | 415 | GapAdvertisingData advertising_data; |
ocomeni | 75:08eff6258e1b | 416 | |
ocomeni | 75:08eff6258e1b | 417 | /* add advertising flags */ |
ocomeni | 75:08eff6258e1b | 418 | advertising_data.addFlags(GapAdvertisingData::LE_GENERAL_DISCOVERABLE |
ocomeni | 75:08eff6258e1b | 419 | | GapAdvertisingData::BREDR_NOT_SUPPORTED); |
ocomeni | 75:08eff6258e1b | 420 | |
ocomeni | 75:08eff6258e1b | 421 | /* add device name */ |
ocomeni | 75:08eff6258e1b | 422 | advertising_data.addData( |
ocomeni | 75:08eff6258e1b | 423 | GapAdvertisingData::COMPLETE_LOCAL_NAME, |
ocomeni | 116:2296cf274661 | 424 | (const uint8_t *)ble_config->deviceName, |
ocomeni | 116:2296cf274661 | 425 | strlen(ble_config->deviceName) |
ocomeni | 76:6afda865fbf8 | 426 | ); |
ocomeni | 76:6afda865fbf8 | 427 | /* Setup primary service */ |
ocomeni | 76:6afda865fbf8 | 428 | uart = new UARTService(_ble); |
ocomeni | 76:6afda865fbf8 | 429 | |
ocomeni | 75:08eff6258e1b | 430 | |
ocomeni | 76:6afda865fbf8 | 431 | /* add device name */ |
ocomeni | 76:6afda865fbf8 | 432 | error = advertising_data.addData( |
ocomeni | 76:6afda865fbf8 | 433 | GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS , |
ocomeni | 76:6afda865fbf8 | 434 | (const uint8_t *)UARTServiceUUID_reversed, |
ocomeni | 76:6afda865fbf8 | 435 | sizeof(sizeof(UARTServiceUUID_reversed)) |
ocomeni | 76:6afda865fbf8 | 436 | ); |
ocomeni | 76:6afda865fbf8 | 437 | /* setup advertising */ |
ocomeni | 76:6afda865fbf8 | 438 | //error = _ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
ocomeni | 76:6afda865fbf8 | 439 | //ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
ocomeni | 76:6afda865fbf8 | 440 | //error = _ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
ocomeni | 76:6afda865fbf8 | 441 | /* set up the services that can be discovered */ |
ocomeni | 76:6afda865fbf8 | 442 | //error = _ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,(const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); |
ocomeni | 76:6afda865fbf8 | 443 | |
ocomeni | 76:6afda865fbf8 | 444 | |
ocomeni | 76:6afda865fbf8 | 445 | //error = _ble.gap().setAdvertisingPayload(advertising_data); |
ocomeni | 75:08eff6258e1b | 446 | |
ocomeni | 75:08eff6258e1b | 447 | if (error) { |
ocomeni | 103:7b566b522427 | 448 | dbg_printf(LOG, "Error during Gap::setAdvertisingPayload\r\n"); |
ocomeni | 75:08eff6258e1b | 449 | return; |
ocomeni | 75:08eff6258e1b | 450 | } |
ocomeni | 75:08eff6258e1b | 451 | |
ocomeni | 75:08eff6258e1b | 452 | /* advertise to everyone */ |
ocomeni | 75:08eff6258e1b | 453 | _ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
ocomeni | 75:08eff6258e1b | 454 | /* how many milliseconds between advertisements, lower interval |
ocomeni | 75:08eff6258e1b | 455 | * increases the chances of being seen at the cost of more power */ |
ocomeni | 76:6afda865fbf8 | 456 | //_ble.gap().setAdvertisingInterval(20); |
ocomeni | 76:6afda865fbf8 | 457 | //_ble.gap().setAdvertisingTimeout(0); |
ocomeni | 116:2296cf274661 | 458 | _ble.gap().setAdvertisingInterval(ble_config->advInterval); /* setting in ble_config */ |
ocomeni | 116:2296cf274661 | 459 | _ble.gap().setAdvertisingTimeout(ble_config->advTimeout); /* setting in ble_config */ |
ocomeni | 75:08eff6258e1b | 460 | |
ocomeni | 113:888e262ff0a9 | 461 | error = _ble.gap().startAdvertising(ble::LEGACY_ADVERTISING_HANDLE); |
ocomeni | 75:08eff6258e1b | 462 | |
ocomeni | 75:08eff6258e1b | 463 | if (error) { |
ocomeni | 103:7b566b522427 | 464 | dbg_printf(LOG, "Error during Gap::startAdvertising.\r\n"); |
ocomeni | 75:08eff6258e1b | 465 | return; |
ocomeni | 75:08eff6258e1b | 466 | } |
ocomeni | 75:08eff6258e1b | 467 | |
ocomeni | 103:7b566b522427 | 468 | dbg_printf(LOG, "Please connect to device\r\n"); |
ocomeni | 75:08eff6258e1b | 469 | |
ocomeni | 75:08eff6258e1b | 470 | /** This tells the stack to generate a pairingRequest event |
ocomeni | 75:08eff6258e1b | 471 | * which will require this application to respond before pairing |
ocomeni | 75:08eff6258e1b | 472 | * can proceed. Setting it to false will automatically accept |
ocomeni | 75:08eff6258e1b | 473 | * pairing. */ |
ocomeni | 75:08eff6258e1b | 474 | _ble.securityManager().setPairingRequestAuthorisation(true); |
ocomeni | 75:08eff6258e1b | 475 | } |
ocomeni | 75:08eff6258e1b | 476 | |
ocomeni | 75:08eff6258e1b | 477 | /** This is called by Gap to notify the application we connected, |
ocomeni | 75:08eff6258e1b | 478 | * in our case it immediately requests a change in link security */ |
ocomeni | 75:08eff6258e1b | 479 | void SMDevicePeripheral::on_connect(const Gap::ConnectionCallbackParams_t *connection_event) |
ocomeni | 75:08eff6258e1b | 480 | { |
ocomeni | 75:08eff6258e1b | 481 | ble_error_t error; |
ocomeni | 75:08eff6258e1b | 482 | |
ocomeni | 75:08eff6258e1b | 483 | /* remember the device that connects to us now so we can connect to it |
ocomeni | 75:08eff6258e1b | 484 | * during the next demonstration */ |
ocomeni | 75:08eff6258e1b | 485 | memcpy(_peer_address, connection_event->peerAddr, sizeof(_peer_address)); |
ocomeni | 75:08eff6258e1b | 486 | |
ocomeni | 103:7b566b522427 | 487 | dbg_printf(LOG, "Connected to: %02x:%02x:%02x:%02x:%02x:%02x\r\n", |
ocomeni | 75:08eff6258e1b | 488 | _peer_address[5], _peer_address[4], _peer_address[3], |
ocomeni | 75:08eff6258e1b | 489 | _peer_address[2], _peer_address[1], _peer_address[0]); |
ocomeni | 75:08eff6258e1b | 490 | |
ocomeni | 75:08eff6258e1b | 491 | /* store the handle for future Security Manager requests */ |
ocomeni | 75:08eff6258e1b | 492 | _handle = connection_event->handle; |
ocomeni | 75:08eff6258e1b | 493 | |
ocomeni | 75:08eff6258e1b | 494 | /* Request a change in link security. This will be done |
ocomeni | 75:08eff6258e1b | 495 | * indirectly by asking the master of the connection to |
ocomeni | 75:08eff6258e1b | 496 | * change it. Depending on circumstances different actions |
ocomeni | 75:08eff6258e1b | 497 | * may be taken by the master which will trigger events |
ocomeni | 75:08eff6258e1b | 498 | * which the applications should deal with. */ |
ocomeni | 75:08eff6258e1b | 499 | error = _ble.securityManager().setLinkSecurity( |
ocomeni | 75:08eff6258e1b | 500 | _handle, |
ocomeni | 75:08eff6258e1b | 501 | SecurityManager::SECURITY_MODE_ENCRYPTION_NO_MITM |
ocomeni | 75:08eff6258e1b | 502 | ); |
ocomeni | 75:08eff6258e1b | 503 | |
ocomeni | 75:08eff6258e1b | 504 | if (error) { |
ocomeni | 103:7b566b522427 | 505 | dbg_printf(LOG, "Error during SM::setLinkSecurity %d\r\n", error); |
ocomeni | 75:08eff6258e1b | 506 | return; |
ocomeni | 75:08eff6258e1b | 507 | } |
ocomeni | 103:7b566b522427 | 508 | dbg_printf(LOG, "SM::setLinkSecurity setup\r\n"); |
ocomeni | 113:888e262ff0a9 | 509 | isConnected = true; |
ocomeni | 75:08eff6258e1b | 510 | } |
ocomeni | 75:08eff6258e1b | 511 | |
ocomeni | 77:0b505d1e15f4 | 512 | void SMDevicePeripheral::stopAdvertising() |
ocomeni | 77:0b505d1e15f4 | 513 | { |
ocomeni | 77:0b505d1e15f4 | 514 | if (_ble.hasInitialized()) { |
ocomeni | 77:0b505d1e15f4 | 515 | ble_error_t error; |
ocomeni | 113:888e262ff0a9 | 516 | error = _ble.gap().stopAdvertising(ble::LEGACY_ADVERTISING_HANDLE);; |
ocomeni | 77:0b505d1e15f4 | 517 | if(error){ |
ocomeni | 103:7b566b522427 | 518 | dbg_printf(LOG, " Error stopping advertising...\r\n"); |
ocomeni | 77:0b505d1e15f4 | 519 | return; |
ocomeni | 77:0b505d1e15f4 | 520 | } |
ocomeni | 103:7b566b522427 | 521 | dbg_printf(LOG, "Stopping advertising...\r\n"); |
ocomeni | 77:0b505d1e15f4 | 522 | //_event_queue.break_dispatch(); |
ocomeni | 77:0b505d1e15f4 | 523 | } |
ocomeni | 77:0b505d1e15f4 | 524 | } |
ocomeni | 77:0b505d1e15f4 | 525 | void SMDevicePeripheral::startAdvertising() |
ocomeni | 77:0b505d1e15f4 | 526 | { |
ocomeni | 77:0b505d1e15f4 | 527 | if (_ble.hasInitialized()) { |
ocomeni | 77:0b505d1e15f4 | 528 | ble_error_t error; |
ocomeni | 113:888e262ff0a9 | 529 | error = _ble.gap().startAdvertising(ble::LEGACY_ADVERTISING_HANDLE); |
ocomeni | 77:0b505d1e15f4 | 530 | if(error){ |
ocomeni | 103:7b566b522427 | 531 | dbg_printf(LOG, " Error Restarting advertising...\r\n"); |
ocomeni | 77:0b505d1e15f4 | 532 | return; |
ocomeni | 77:0b505d1e15f4 | 533 | } |
ocomeni | 103:7b566b522427 | 534 | dbg_printf(LOG, "Restarting advertising...\r\n"); |
ocomeni | 77:0b505d1e15f4 | 535 | //_event_queue.break_dispatch(); |
ocomeni | 77:0b505d1e15f4 | 536 | } |
ocomeni | 77:0b505d1e15f4 | 537 | } |
ocomeni | 77:0b505d1e15f4 | 538 | |
ocomeni | 77:0b505d1e15f4 | 539 | |
ocomeni | 119:8d939a902333 | 540 | void SMDevicePeripheral::processQueues() |
ocomeni | 119:8d939a902333 | 541 | { |
ocomeni | 119:8d939a902333 | 542 | dequeueATdataResponse(); |
ocomeni | 119:8d939a902333 | 543 | switch(bleCmd) |
ocomeni | 119:8d939a902333 | 544 | { |
ocomeni | 119:8d939a902333 | 545 | case BLE_CMD_NONE: |
ocomeni | 119:8d939a902333 | 546 | break; |
ocomeni | 119:8d939a902333 | 547 | case BLE_CMD_CONFIG: |
ocomeni | 119:8d939a902333 | 548 | _aT2BleDatamPool->free(data_msg); |
ocomeni | 119:8d939a902333 | 549 | bleCmd = BLE_CMD_NONE; |
ocomeni | 119:8d939a902333 | 550 | break; |
ocomeni | 119:8d939a902333 | 551 | case BLE_CMD_CONNECT: |
ocomeni | 119:8d939a902333 | 552 | _aT2BleDatamPool->free(data_msg); |
ocomeni | 119:8d939a902333 | 553 | bleCmd = BLE_CMD_NONE; |
ocomeni | 119:8d939a902333 | 554 | break; |
ocomeni | 119:8d939a902333 | 555 | case BLE_CMD_DISCONNECT: |
ocomeni | 119:8d939a902333 | 556 | _aT2BleDatamPool->free(data_msg); |
ocomeni | 119:8d939a902333 | 557 | bleCmd = BLE_CMD_NONE; |
ocomeni | 119:8d939a902333 | 558 | break; |
ocomeni | 119:8d939a902333 | 559 | case BLE_CMD_SEND_RX_DATA_2AT: |
ocomeni | 119:8d939a902333 | 560 | _aT2BleDatamPool->free(data_msg); |
ocomeni | 119:8d939a902333 | 561 | bleCmd = BLE_CMD_NONE; |
ocomeni | 119:8d939a902333 | 562 | break; |
ocomeni | 119:8d939a902333 | 563 | case BLE_CMD_SEND_AT_DATA_2BLE: |
ocomeni | 119:8d939a902333 | 564 | _aT2BleDatamPool->free(data_msg); |
ocomeni | 119:8d939a902333 | 565 | bleCmd = BLE_CMD_NONE; |
ocomeni | 119:8d939a902333 | 566 | break; |
ocomeni | 119:8d939a902333 | 567 | default: |
ocomeni | 119:8d939a902333 | 568 | _aT2BleDatamPool->free(data_msg); |
ocomeni | 119:8d939a902333 | 569 | bleCmd = BLE_CMD_NONE; |
ocomeni | 119:8d939a902333 | 570 | break; |
ocomeni | 119:8d939a902333 | 571 | } |
ocomeni | 119:8d939a902333 | 572 | |
ocomeni | 119:8d939a902333 | 573 | } |
ocomeni | 119:8d939a902333 | 574 | |
ocomeni | 119:8d939a902333 | 575 | |
ocomeni | 119:8d939a902333 | 576 | bool SMDevicePeripheral::queueBleDataResponse(ble_at_msg_t at_resp) |
ocomeni | 119:8d939a902333 | 577 | { |
ocomeni | 119:8d939a902333 | 578 | ble_at_msg_t *atData = _ble2ATDatamPool->alloc(); |
ocomeni | 119:8d939a902333 | 579 | if(atData == NULL) return false; // queue full; |
ocomeni | 119:8d939a902333 | 580 | atData->at_resp = at_resp.at_resp; |
ocomeni | 119:8d939a902333 | 581 | atData->dataLen = at_resp.dataLen; |
ocomeni | 119:8d939a902333 | 582 | memcpy(atData->buffer, at_resp.buffer, at_resp.dataLen); |
ocomeni | 119:8d939a902333 | 583 | _ble2ATDataQueue->put(atData); |
ocomeni | 119:8d939a902333 | 584 | dbg_printf(LOG, "[BLE-MAN] queued data size = %d : at_resp = %d\n", at_resp.dataLen, at_resp.at_resp); |
ocomeni | 119:8d939a902333 | 585 | return true; |
ocomeni | 119:8d939a902333 | 586 | } |
ocomeni | 119:8d939a902333 | 587 | |
ocomeni | 119:8d939a902333 | 588 | |
ocomeni | 119:8d939a902333 | 589 | bool SMDevicePeripheral::dequeueATdataResponse(){ |
ocomeni | 119:8d939a902333 | 590 | if(bleCmd != BLE_CMD_NONE) return false; // busy |
ocomeni | 119:8d939a902333 | 591 | osEvent evt = _aT2BleDataQueue->get(0); |
ocomeni | 119:8d939a902333 | 592 | if(evt.status == osEventMessage){ |
ocomeni | 119:8d939a902333 | 593 | data_msg = (at_ble_msg_t*)evt.value.p; |
ocomeni | 119:8d939a902333 | 594 | setNextCommand(data_msg->ble_cmd); |
ocomeni | 119:8d939a902333 | 595 | //_wiFi2ATDatamPool->free(data_msg); |
ocomeni | 119:8d939a902333 | 596 | } |
ocomeni | 119:8d939a902333 | 597 | return true; |
ocomeni | 119:8d939a902333 | 598 | } |
ocomeni | 119:8d939a902333 | 599 | |
ocomeni | 119:8d939a902333 | 600 | void SMDevicePeripheral::sendATresponseBytes(at_cmd_resp_t at_cmd, const uint8_t * buf, int len) |
ocomeni | 119:8d939a902333 | 601 | { |
ocomeni | 119:8d939a902333 | 602 | at_data_resp = new ble_at_msg_t; |
ocomeni | 119:8d939a902333 | 603 | // package and send on BLE data queue |
ocomeni | 119:8d939a902333 | 604 | // set string length |
ocomeni | 119:8d939a902333 | 605 | at_data_resp->dataLen = len; |
ocomeni | 119:8d939a902333 | 606 | // copy data |
ocomeni | 119:8d939a902333 | 607 | memcpy(at_data_resp->buffer, buf, len); |
ocomeni | 119:8d939a902333 | 608 | // copy response type |
ocomeni | 119:8d939a902333 | 609 | at_data_resp->at_resp = at_cmd; |
ocomeni | 119:8d939a902333 | 610 | bool queueResult = true; |
ocomeni | 119:8d939a902333 | 611 | int wait_count = 0; |
ocomeni | 119:8d939a902333 | 612 | queueResult = queueBleDataResponse(*at_data_resp); |
ocomeni | 119:8d939a902333 | 613 | delete at_data_resp; |
ocomeni | 119:8d939a902333 | 614 | at_data_resp = NULL; |
ocomeni | 119:8d939a902333 | 615 | dbg_printf(LOG, "[BLE-MAN] sendATresponseBytes completed successfully\r\n"); |
ocomeni | 119:8d939a902333 | 616 | } |
ocomeni | 119:8d939a902333 | 617 | |
ocomeni | 119:8d939a902333 | 618 | bool SMDevicePeripheral::setNextCommand(ble_cmd_t cmd) |
ocomeni | 119:8d939a902333 | 619 | { |
ocomeni | 119:8d939a902333 | 620 | dbg_printf(LOG, "\n [BLE-MAN] About to set next BLE manager command to %d\n", cmd); |
ocomeni | 119:8d939a902333 | 621 | if(bleCmd == BLE_CMD_NONE){ |
ocomeni | 119:8d939a902333 | 622 | bleCmd = cmd; |
ocomeni | 119:8d939a902333 | 623 | return true; // success |
ocomeni | 119:8d939a902333 | 624 | } |
ocomeni | 119:8d939a902333 | 625 | dbg_printf(LOG, "\n [BLE-MAN] Busy : current state = %d \n", bleCmd); |
ocomeni | 119:8d939a902333 | 626 | return false; // BleManager busy |
ocomeni | 119:8d939a902333 | 627 | } |
ocomeni | 77:0b505d1e15f4 | 628 | |
ocomeni | 75:08eff6258e1b | 629 | /** A central device will scan, connect to a peer and request pairing. */ |
ocomeni | 75:08eff6258e1b | 630 | |
ocomeni | 118:8df0e9c2ee3f | 631 | SMDeviceCentral::SMDeviceCentral(BLE &ble, events::EventQueue &event_queue, BLEProtocol::AddressBytes_t &peer_address, |
ocomeni | 118:8df0e9c2ee3f | 632 | MemoryPool<at_ble_msg_t, PQDSZ_BLE> *aT2BleDatamPool, |
ocomeni | 118:8df0e9c2ee3f | 633 | Queue<at_ble_msg_t, PQDSZ_BLE> *aT2BleDataQueue, |
ocomeni | 118:8df0e9c2ee3f | 634 | MemoryPool<ble_at_msg_t, PQDSZ_BLE> *ble2ATDatamPool, |
ocomeni | 118:8df0e9c2ee3f | 635 | Queue<ble_at_msg_t, PQDSZ_BLE> *ble2ATDataQueue, |
ocomeni | 118:8df0e9c2ee3f | 636 | ble_config_t *ble_config) |
ocomeni | 118:8df0e9c2ee3f | 637 | : SMDevice(ble, event_queue, peer_address, |
ocomeni | 118:8df0e9c2ee3f | 638 | aT2BleDatamPool, aT2BleDataQueue, |
ocomeni | 118:8df0e9c2ee3f | 639 | ble2ATDatamPool, ble2ATDataQueue, |
ocomeni | 118:8df0e9c2ee3f | 640 | ble_config) { }; |
ocomeni | 75:08eff6258e1b | 641 | |
ocomeni | 75:08eff6258e1b | 642 | void SMDeviceCentral::start() |
ocomeni | 75:08eff6258e1b | 643 | { |
ocomeni | 75:08eff6258e1b | 644 | /* start scanning and attach a callback that will handle advertisements |
ocomeni | 75:08eff6258e1b | 645 | * and scan requests responses */ |
ocomeni | 75:08eff6258e1b | 646 | ble_error_t error = _ble.gap().startScan(this, &SMDeviceCentral::on_scan); |
ocomeni | 75:08eff6258e1b | 647 | |
ocomeni | 103:7b566b522427 | 648 | dbg_printf(LOG, "Please advertise\r\n"); |
ocomeni | 75:08eff6258e1b | 649 | |
ocomeni | 103:7b566b522427 | 650 | dbg_printf(LOG, "Scanning for: %02x:%02x:%02x:%02x:%02x:%02x\r\n", |
ocomeni | 75:08eff6258e1b | 651 | _peer_address[5], _peer_address[4], _peer_address[3], |
ocomeni | 75:08eff6258e1b | 652 | _peer_address[2], _peer_address[1], _peer_address[0]); |
ocomeni | 75:08eff6258e1b | 653 | |
ocomeni | 75:08eff6258e1b | 654 | if (error) { |
ocomeni | 103:7b566b522427 | 655 | dbg_printf(LOG, "Error during Gap::startScan %d\r\n", error); |
ocomeni | 75:08eff6258e1b | 656 | return; |
ocomeni | 75:08eff6258e1b | 657 | } |
ocomeni | 75:08eff6258e1b | 658 | } |
ocomeni | 75:08eff6258e1b | 659 | |
ocomeni | 75:08eff6258e1b | 660 | /** Look at scan payload to find a peer device and connect to it */ |
ocomeni | 75:08eff6258e1b | 661 | void SMDeviceCentral::on_scan(const Gap::AdvertisementCallbackParams_t *params) |
ocomeni | 75:08eff6258e1b | 662 | { |
ocomeni | 75:08eff6258e1b | 663 | /* don't bother with analysing scan result if we're already connecting */ |
ocomeni | 75:08eff6258e1b | 664 | if (_is_connecting) { |
ocomeni | 75:08eff6258e1b | 665 | return; |
ocomeni | 75:08eff6258e1b | 666 | } |
ocomeni | 75:08eff6258e1b | 667 | |
ocomeni | 75:08eff6258e1b | 668 | /* connect to the same device that connected to us */ |
ocomeni | 75:08eff6258e1b | 669 | if (memcmp(params->peerAddr, _peer_address, sizeof(_peer_address)) == 0) { |
ocomeni | 75:08eff6258e1b | 670 | |
ocomeni | 75:08eff6258e1b | 671 | ble_error_t error = _ble.gap().connect( |
ocomeni | 75:08eff6258e1b | 672 | params->peerAddr, params->peerAddrType, |
ocomeni | 75:08eff6258e1b | 673 | NULL, NULL |
ocomeni | 75:08eff6258e1b | 674 | ); |
ocomeni | 75:08eff6258e1b | 675 | |
ocomeni | 75:08eff6258e1b | 676 | if (error) { |
ocomeni | 103:7b566b522427 | 677 | dbg_printf(LOG, "Error during Gap::connect %d\r\n", error); |
ocomeni | 75:08eff6258e1b | 678 | return; |
ocomeni | 75:08eff6258e1b | 679 | } |
ocomeni | 75:08eff6258e1b | 680 | |
ocomeni | 103:7b566b522427 | 681 | dbg_printf(LOG, "Connecting... "); |
ocomeni | 75:08eff6258e1b | 682 | |
ocomeni | 75:08eff6258e1b | 683 | /* we may have already scan events waiting |
ocomeni | 75:08eff6258e1b | 684 | * to be processed so we need to remember |
ocomeni | 75:08eff6258e1b | 685 | * that we are already connecting and ignore them */ |
ocomeni | 75:08eff6258e1b | 686 | _is_connecting = true; |
ocomeni | 75:08eff6258e1b | 687 | |
ocomeni | 75:08eff6258e1b | 688 | return; |
ocomeni | 75:08eff6258e1b | 689 | } |
ocomeni | 75:08eff6258e1b | 690 | } |
ocomeni | 75:08eff6258e1b | 691 | |
ocomeni | 75:08eff6258e1b | 692 | /** This is called by Gap to notify the application we connected, |
ocomeni | 75:08eff6258e1b | 693 | * in our case it immediately request pairing */ |
ocomeni | 75:08eff6258e1b | 694 | void SMDeviceCentral::on_connect(const Gap::ConnectionCallbackParams_t *connection_event) |
ocomeni | 75:08eff6258e1b | 695 | { |
ocomeni | 75:08eff6258e1b | 696 | ble_error_t error; |
ocomeni | 75:08eff6258e1b | 697 | |
ocomeni | 75:08eff6258e1b | 698 | /* store the handle for future Security Manager requests */ |
ocomeni | 75:08eff6258e1b | 699 | _handle = connection_event->handle; |
ocomeni | 75:08eff6258e1b | 700 | |
ocomeni | 75:08eff6258e1b | 701 | /* in this example the local device is the master so we request pairing */ |
ocomeni | 75:08eff6258e1b | 702 | error = _ble.securityManager().requestPairing(_handle); |
ocomeni | 75:08eff6258e1b | 703 | |
ocomeni | 103:7b566b522427 | 704 | dbg_printf(LOG, "Connected\r\n"); |
ocomeni | 75:08eff6258e1b | 705 | |
ocomeni | 75:08eff6258e1b | 706 | if (error) { |
ocomeni | 103:7b566b522427 | 707 | dbg_printf(LOG, "Error during SM::requestPairing %d\r\n", error); |
ocomeni | 75:08eff6258e1b | 708 | return; |
ocomeni | 75:08eff6258e1b | 709 | } |
ocomeni | 75:08eff6258e1b | 710 | |
ocomeni | 75:08eff6258e1b | 711 | /* upon pairing success the application will disconnect */ |
ocomeni | 75:08eff6258e1b | 712 | } |
ocomeni | 75:08eff6258e1b | 713 | |
ocomeni | 75:08eff6258e1b | 714 | |
ocomeni | 75:08eff6258e1b | 715 | |
ocomeni | 75:08eff6258e1b | 716 | #if MBED_CONF_APP_FILESYSTEM_SUPPORT |
ocomeni | 75:08eff6258e1b | 717 | bool create_filesystem() |
ocomeni | 75:08eff6258e1b | 718 | { |
ocomeni | 75:08eff6258e1b | 719 | static LittleFileSystem fs("fs"); |
ocomeni | 75:08eff6258e1b | 720 | |
ocomeni | 75:08eff6258e1b | 721 | /* replace this with any physical block device your board supports (like an SD card) */ |
ocomeni | 75:08eff6258e1b | 722 | static HeapBlockDevice bd(4096, 256); |
ocomeni | 75:08eff6258e1b | 723 | |
ocomeni | 75:08eff6258e1b | 724 | int err = bd.init(); |
ocomeni | 75:08eff6258e1b | 725 | |
ocomeni | 75:08eff6258e1b | 726 | if (err) { |
ocomeni | 75:08eff6258e1b | 727 | return false; |
ocomeni | 75:08eff6258e1b | 728 | } |
ocomeni | 75:08eff6258e1b | 729 | |
ocomeni | 75:08eff6258e1b | 730 | err = bd.erase(0, bd.size()); |
ocomeni | 75:08eff6258e1b | 731 | |
ocomeni | 75:08eff6258e1b | 732 | if (err) { |
ocomeni | 75:08eff6258e1b | 733 | return false; |
ocomeni | 75:08eff6258e1b | 734 | } |
ocomeni | 75:08eff6258e1b | 735 | |
ocomeni | 75:08eff6258e1b | 736 | err = fs.mount(&bd); |
ocomeni | 75:08eff6258e1b | 737 | |
ocomeni | 75:08eff6258e1b | 738 | if (err) { |
ocomeni | 75:08eff6258e1b | 739 | /* Reformat if we can't mount the filesystem */ |
ocomeni | 103:7b566b522427 | 740 | dbg_printf(LOG, "No filesystem found, formatting...\r\n"); |
ocomeni | 75:08eff6258e1b | 741 | |
ocomeni | 75:08eff6258e1b | 742 | err = fs.reformat(&bd); |
ocomeni | 75:08eff6258e1b | 743 | |
ocomeni | 75:08eff6258e1b | 744 | if (err) { |
ocomeni | 75:08eff6258e1b | 745 | return false; |
ocomeni | 75:08eff6258e1b | 746 | } |
ocomeni | 75:08eff6258e1b | 747 | } |
ocomeni | 75:08eff6258e1b | 748 | |
ocomeni | 75:08eff6258e1b | 749 | return true; |
ocomeni | 75:08eff6258e1b | 750 | } |
ocomeni | 75:08eff6258e1b | 751 | #endif //MBED_CONF_APP_FILESYSTEM_SUPPORT |
ocomeni | 75:08eff6258e1b | 752 | #ifdef BLE_SECURITY_MAIN |
ocomeni | 75:08eff6258e1b | 753 | int main() |
ocomeni | 75:08eff6258e1b | 754 | { |
ocomeni | 75:08eff6258e1b | 755 | BLE& ble = BLE::Instance(); |
ocomeni | 75:08eff6258e1b | 756 | events::EventQueue queue; |
ocomeni | 75:08eff6258e1b | 757 | |
ocomeni | 75:08eff6258e1b | 758 | #if MBED_CONF_APP_FILESYSTEM_SUPPORT |
ocomeni | 75:08eff6258e1b | 759 | /* if filesystem creation fails or there is no filesystem the security manager |
ocomeni | 75:08eff6258e1b | 760 | * will fallback to storing the security database in memory */ |
ocomeni | 75:08eff6258e1b | 761 | if (!create_filesystem()) { |
ocomeni | 103:7b566b522427 | 762 | dbg_printf(LOG, "Filesystem creation failed, will use memory storage\r\n"); |
ocomeni | 75:08eff6258e1b | 763 | } |
ocomeni | 75:08eff6258e1b | 764 | #endif |
ocomeni | 75:08eff6258e1b | 765 | |
ocomeni | 75:08eff6258e1b | 766 | while(1) { |
ocomeni | 75:08eff6258e1b | 767 | { |
ocomeni | 103:7b566b522427 | 768 | dbg_printf(LOG, "\r\n PERIPHERAL \r\n\r\n"); |
ocomeni | 75:08eff6258e1b | 769 | SMDevicePeripheral peripheral(ble, queue, peer_address); |
ocomeni | 75:08eff6258e1b | 770 | peripheral.run(); |
ocomeni | 75:08eff6258e1b | 771 | } |
ocomeni | 75:08eff6258e1b | 772 | |
ocomeni | 75:08eff6258e1b | 773 | { |
ocomeni | 103:7b566b522427 | 774 | dbg_printf(LOG, "\r\n CENTRAL \r\n\r\n"); |
ocomeni | 75:08eff6258e1b | 775 | SMDeviceCentral central(ble, queue, peer_address); |
ocomeni | 75:08eff6258e1b | 776 | central.run(); |
ocomeni | 75:08eff6258e1b | 777 | } |
ocomeni | 75:08eff6258e1b | 778 | } |
ocomeni | 75:08eff6258e1b | 779 | |
ocomeni | 75:08eff6258e1b | 780 | return 0; |
ocomeni | 75:08eff6258e1b | 781 | } |
ocomeni | 75:08eff6258e1b | 782 | #endif |