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