
this is using the mbed os version 5-13-1
source/BleManager.h@129:590bdc2dcf5b, 2019-07-19 (annotated)
- Committer:
- ocomeni
- Date:
- Fri Jul 19 16:49:26 2019 +0000
- Branch:
- PassingRegression
- Revision:
- 129:590bdc2dcf5b
- Parent:
- 121:ac4f59839e4f
Implementation of Access token acquisition; 1. make request with credentials - DONE; 2. get response - DONE; 3. extract Id and refresh tokens from response - DONE; 4. integrate with code - DONE; Testing ongoing
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ocomeni | 75:08eff6258e1b | 1 | #ifndef __BLE_MANAGER_H__ |
ocomeni | 74:f26e846adfe9 | 2 | #define __BLE_MANAGER_H__ |
ocomeni | 75:08eff6258e1b | 3 | |
ocomeni | 75:08eff6258e1b | 4 | |
ocomeni | 75:08eff6258e1b | 5 | /* mbed Microcontroller Library |
ocomeni | 75:08eff6258e1b | 6 | * Copyright (c) 2006-2013 ARM Limited |
ocomeni | 75:08eff6258e1b | 7 | * |
ocomeni | 75:08eff6258e1b | 8 | * Licensed under the Apache License, Version 2.0 (the "License"); |
ocomeni | 75:08eff6258e1b | 9 | * you may not use this file except in compliance with the License. |
ocomeni | 75:08eff6258e1b | 10 | * You may obtain a copy of the License at |
ocomeni | 75:08eff6258e1b | 11 | * |
ocomeni | 75:08eff6258e1b | 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
ocomeni | 75:08eff6258e1b | 13 | * |
ocomeni | 75:08eff6258e1b | 14 | * Unless required by applicable law or agreed to in writing, software |
ocomeni | 75:08eff6258e1b | 15 | * distributed under the License is distributed on an "AS IS" BASIS, |
ocomeni | 75:08eff6258e1b | 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
ocomeni | 75:08eff6258e1b | 17 | * See the License for the specific language governing permissions and |
ocomeni | 75:08eff6258e1b | 18 | * limitations under the License. |
ocomeni | 75:08eff6258e1b | 19 | */ |
ocomeni | 75:08eff6258e1b | 20 | |
ocomeni | 75:08eff6258e1b | 21 | #include <events/mbed_events.h> |
ocomeni | 75:08eff6258e1b | 22 | #include <mbed.h> |
ocomeni | 75:08eff6258e1b | 23 | #include "ble/BLE.h" |
ocomeni | 75:08eff6258e1b | 24 | #include "SecurityManager.h" |
ocomeni | 78:07bb86e3ce14 | 25 | #include "common_types.h" |
ocomeni | 75:08eff6258e1b | 26 | |
ocomeni | 75:08eff6258e1b | 27 | /** This example demonstrates all the basic setup required |
ocomeni | 75:08eff6258e1b | 28 | * for pairing and setting up link security both as a central and peripheral |
ocomeni | 75:08eff6258e1b | 29 | * |
ocomeni | 75:08eff6258e1b | 30 | * The example is implemented as two classes, one for the peripheral and one |
ocomeni | 75:08eff6258e1b | 31 | * for central inheriting from a common base. They are run in sequence and |
ocomeni | 75:08eff6258e1b | 32 | * require a peer device to connect to. During the peripheral device demonstration |
ocomeni | 75:08eff6258e1b | 33 | * a peer device is required to connect. In the central device demonstration |
ocomeni | 75:08eff6258e1b | 34 | * this peer device will be scanned for and connected to - therefore it should |
ocomeni | 75:08eff6258e1b | 35 | * be advertising with the same address as when it connected. |
ocomeni | 75:08eff6258e1b | 36 | * |
ocomeni | 75:08eff6258e1b | 37 | * During the test output is written on the serial connection to monitor its |
ocomeni | 75:08eff6258e1b | 38 | * progress. |
ocomeni | 75:08eff6258e1b | 39 | */ |
ocomeni | 75:08eff6258e1b | 40 | |
ocomeni | 75:08eff6258e1b | 41 | |
ocomeni | 75:08eff6258e1b | 42 | /* for demonstration purposes we will store the peer device address |
ocomeni | 75:08eff6258e1b | 43 | * of the device that connects to us in the first demonstration |
ocomeni | 75:08eff6258e1b | 44 | * so we can use its address to reconnect to it later */ |
ocomeni | 75:08eff6258e1b | 45 | static BLEProtocol::AddressBytes_t peer_address; |
ocomeni | 75:08eff6258e1b | 46 | |
ocomeni | 75:08eff6258e1b | 47 | /** Base class for both peripheral and central. The same class that provides |
ocomeni | 75:08eff6258e1b | 48 | * the logic for the application also implements the SecurityManagerEventHandler |
ocomeni | 75:08eff6258e1b | 49 | * which is the interface used by the Security Manager to communicate events |
ocomeni | 75:08eff6258e1b | 50 | * back to the applications. You can provide overrides for a selection of events |
ocomeni | 75:08eff6258e1b | 51 | * your application is interested in. |
ocomeni | 75:08eff6258e1b | 52 | */ |
ocomeni | 75:08eff6258e1b | 53 | class SMDevice : private mbed::NonCopyable<SMDevice>, |
ocomeni | 75:08eff6258e1b | 54 | public SecurityManager::EventHandler |
ocomeni | 75:08eff6258e1b | 55 | { |
ocomeni | 75:08eff6258e1b | 56 | public: |
ocomeni | 78:07bb86e3ce14 | 57 | SMDevice(BLE &ble, events::EventQueue &event_queue, |
ocomeni | 118:8df0e9c2ee3f | 58 | BLEProtocol::AddressBytes_t &peer_address, |
ocomeni | 118:8df0e9c2ee3f | 59 | MemoryPool<at_ble_msg_t, PQDSZ_BLE> *aT2BleDatamPool, |
ocomeni | 118:8df0e9c2ee3f | 60 | Queue<at_ble_msg_t, PQDSZ_BLE> *aT2BleDataQueue, |
ocomeni | 118:8df0e9c2ee3f | 61 | MemoryPool<ble_at_msg_t, PQDSZ_BLE> *ble2ATDatamPool, |
ocomeni | 118:8df0e9c2ee3f | 62 | Queue<ble_at_msg_t, PQDSZ_BLE> *ble2ATDataQueue, |
ocomeni | 118:8df0e9c2ee3f | 63 | ble_config_t *ble_config); |
ocomeni | 75:08eff6258e1b | 64 | |
ocomeni | 75:08eff6258e1b | 65 | virtual ~SMDevice(); |
ocomeni | 75:08eff6258e1b | 66 | |
ocomeni | 75:08eff6258e1b | 67 | /** Start BLE interface initialisation */ |
ocomeni | 75:08eff6258e1b | 68 | void run(); |
ocomeni | 75:08eff6258e1b | 69 | |
ocomeni | 75:08eff6258e1b | 70 | /* event handler functions */ |
ocomeni | 75:08eff6258e1b | 71 | |
ocomeni | 75:08eff6258e1b | 72 | /** Respond to a pairing request. This will be called by the stack |
ocomeni | 75:08eff6258e1b | 73 | * when a pairing request arrives and expects the application to |
ocomeni | 75:08eff6258e1b | 74 | * call acceptPairingRequest or cancelPairingRequest */ |
ocomeni | 75:08eff6258e1b | 75 | virtual void pairingRequest( |
ocomeni | 75:08eff6258e1b | 76 | ble::connection_handle_t connectionHandle |
ocomeni | 75:08eff6258e1b | 77 | ); |
ocomeni | 75:08eff6258e1b | 78 | |
ocomeni | 75:08eff6258e1b | 79 | /** Inform the application of a successful pairing. Terminate the demonstration. */ |
ocomeni | 75:08eff6258e1b | 80 | virtual void pairingResult( |
ocomeni | 75:08eff6258e1b | 81 | ble::connection_handle_t connectionHandle, |
ocomeni | 75:08eff6258e1b | 82 | SecurityManager::SecurityCompletionStatus_t result |
ocomeni | 75:08eff6258e1b | 83 | ); |
ocomeni | 75:08eff6258e1b | 84 | |
ocomeni | 75:08eff6258e1b | 85 | /** Inform the application of change in encryption status. This will be |
ocomeni | 75:08eff6258e1b | 86 | * communicated through the serial port */ |
ocomeni | 75:08eff6258e1b | 87 | virtual void linkEncryptionResult( |
ocomeni | 75:08eff6258e1b | 88 | ble::connection_handle_t connectionHandle, |
ocomeni | 75:08eff6258e1b | 89 | ble::link_encryption_t result |
ocomeni | 75:08eff6258e1b | 90 | ); |
ocomeni | 77:0b505d1e15f4 | 91 | |
ocomeni | 77:0b505d1e15f4 | 92 | void shutDown(); |
ocomeni | 120:779b74689747 | 93 | void sendBLEUartData(const uint8_t * buf, int len); |
ocomeni | 75:08eff6258e1b | 94 | |
ocomeni | 113:888e262ff0a9 | 95 | protected: |
ocomeni | 113:888e262ff0a9 | 96 | // connection status |
ocomeni | 113:888e262ff0a9 | 97 | bool isConnected; |
ocomeni | 119:8d939a902333 | 98 | char buffer[MAX_BLE_PACKET_SIZE]; |
ocomeni | 75:08eff6258e1b | 99 | private: |
ocomeni | 113:888e262ff0a9 | 100 | /** Override to start chosen activity when initialisation completes */ |
ocomeni | 75:08eff6258e1b | 101 | virtual void start() = 0; |
ocomeni | 75:08eff6258e1b | 102 | |
ocomeni | 75:08eff6258e1b | 103 | /** This is called when BLE interface is initialised and starts the demonstration */ |
ocomeni | 75:08eff6258e1b | 104 | void on_init_complete(BLE::InitializationCompleteCallbackContext *event); |
ocomeni | 75:08eff6258e1b | 105 | |
ocomeni | 75:08eff6258e1b | 106 | /** This is called by Gap to notify the application we connected */ |
ocomeni | 75:08eff6258e1b | 107 | virtual void on_connect(const Gap::ConnectionCallbackParams_t *connection_event) = 0; |
ocomeni | 75:08eff6258e1b | 108 | |
ocomeni | 75:08eff6258e1b | 109 | /** This is called by Gap to notify the application we disconnected, |
ocomeni | 75:08eff6258e1b | 110 | * in our case it ends the demonstration. */ |
ocomeni | 75:08eff6258e1b | 111 | void on_disconnect(const Gap::DisconnectionCallbackParams_t *event); |
ocomeni | 75:08eff6258e1b | 112 | |
ocomeni | 75:08eff6258e1b | 113 | /** End demonstration unexpectedly. Called if timeout is reached during advertising, |
ocomeni | 75:08eff6258e1b | 114 | * scanning or connection initiation */ |
ocomeni | 75:08eff6258e1b | 115 | void on_timeout(const Gap::TimeoutSource_t source); |
ocomeni | 75:08eff6258e1b | 116 | |
ocomeni | 75:08eff6258e1b | 117 | /** Schedule processing of events from the BLE in the event queue. */ |
ocomeni | 75:08eff6258e1b | 118 | void schedule_ble_events(BLE::OnEventsToProcessCallbackContext *context); |
ocomeni | 75:08eff6258e1b | 119 | |
ocomeni | 75:08eff6258e1b | 120 | /** Blink LED to show we're running */ |
ocomeni | 75:08eff6258e1b | 121 | void blink(void); |
ocomeni | 76:6afda865fbf8 | 122 | /** Echo received data back */ |
ocomeni | 76:6afda865fbf8 | 123 | void EchoBleUartReceived(); |
ocomeni | 79:a2187bbfa407 | 124 | |
ocomeni | 77:0b505d1e15f4 | 125 | |
ocomeni | 77:0b505d1e15f4 | 126 | void reportGapState(); |
ocomeni | 119:8d939a902333 | 127 | |
ocomeni | 119:8d939a902333 | 128 | // application virtual methods called by peripheral |
ocomeni | 119:8d939a902333 | 129 | virtual bool queueBleDataResponse(ble_at_msg_t at_resp) = 0; |
ocomeni | 119:8d939a902333 | 130 | virtual bool dequeueATdataResponse() = 0; |
ocomeni | 120:779b74689747 | 131 | virtual void sendATresponseBytes(at_cmd_resp_t at_cmd) = 0; |
ocomeni | 119:8d939a902333 | 132 | virtual bool setNextCommand(ble_cmd_t cmd) = 0; |
ocomeni | 119:8d939a902333 | 133 | virtual void processQueues() = 0; |
ocomeni | 76:6afda865fbf8 | 134 | |
ocomeni | 76:6afda865fbf8 | 135 | /** |
ocomeni | 76:6afda865fbf8 | 136 | * This callback allows the LEDService to receive updates to the ledState Characteristic. |
ocomeni | 76:6afda865fbf8 | 137 | * |
ocomeni | 76:6afda865fbf8 | 138 | * @param[in] params |
ocomeni | 76:6afda865fbf8 | 139 | * Information about the characterisitc being updated. |
ocomeni | 76:6afda865fbf8 | 140 | */ |
ocomeni | 76:6afda865fbf8 | 141 | void onDataWrittenCallback(const GattWriteCallbackParams *params); |
ocomeni | 79:a2187bbfa407 | 142 | //void passkeyDisplayCallback(Gap::Handle_t handle, const SecurityManager::Passkey_t passkey); |
ocomeni | 79:a2187bbfa407 | 143 | //void securitySetupCompletedCallback(Gap::Handle_t handle, SecurityManager::SecurityCompletionStatus_t status); |
ocomeni | 75:08eff6258e1b | 144 | |
ocomeni | 119:8d939a902333 | 145 | protected: |
ocomeni | 119:8d939a902333 | 146 | BLE &_ble; |
ocomeni | 119:8d939a902333 | 147 | events::EventQueue &_event_queue; |
ocomeni | 119:8d939a902333 | 148 | BLEProtocol::AddressBytes_t &_peer_address; |
ocomeni | 119:8d939a902333 | 149 | ble_cmd_t bleCmd; |
ocomeni | 119:8d939a902333 | 150 | at_ble_msg_t *data_msg; |
ocomeni | 119:8d939a902333 | 151 | ble_at_msg_t *at_data_resp; |
ocomeni | 119:8d939a902333 | 152 | protected: |
ocomeni | 118:8df0e9c2ee3f | 153 | /* Queue and memory pool for AT to BLE data */ |
ocomeni | 118:8df0e9c2ee3f | 154 | MemoryPool<at_ble_msg_t, PQDSZ_BLE> *_aT2BleDatamPool; |
ocomeni | 118:8df0e9c2ee3f | 155 | Queue<at_ble_msg_t, PQDSZ_BLE> *_aT2BleDataQueue; |
ocomeni | 118:8df0e9c2ee3f | 156 | |
ocomeni | 118:8df0e9c2ee3f | 157 | |
ocomeni | 118:8df0e9c2ee3f | 158 | /* Queue and memory pool for BLE to AT data */ |
ocomeni | 118:8df0e9c2ee3f | 159 | MemoryPool<ble_at_msg_t, PQDSZ_BLE> *_ble2ATDatamPool; |
ocomeni | 118:8df0e9c2ee3f | 160 | Queue<ble_at_msg_t, PQDSZ_BLE> *_ble2ATDataQueue; |
ocomeni | 116:2296cf274661 | 161 | ble_config_t *ble_config; |
ocomeni | 75:08eff6258e1b | 162 | ble::connection_handle_t _handle; |
ocomeni | 75:08eff6258e1b | 163 | bool _is_connecting; |
ocomeni | 119:8d939a902333 | 164 | private: |
ocomeni | 119:8d939a902333 | 165 | DigitalOut _led1; |
ocomeni | 75:08eff6258e1b | 166 | }; |
ocomeni | 75:08eff6258e1b | 167 | |
ocomeni | 75:08eff6258e1b | 168 | /** A peripheral device will advertise, accept the connection and request |
ocomeni | 75:08eff6258e1b | 169 | * a change in link security. */ |
ocomeni | 75:08eff6258e1b | 170 | class SMDevicePeripheral : public SMDevice { |
ocomeni | 75:08eff6258e1b | 171 | public: |
ocomeni | 78:07bb86e3ce14 | 172 | SMDevicePeripheral(BLE &ble, events::EventQueue &event_queue, |
ocomeni | 118:8df0e9c2ee3f | 173 | BLEProtocol::AddressBytes_t &peer_address, |
ocomeni | 118:8df0e9c2ee3f | 174 | MemoryPool<at_ble_msg_t, PQDSZ_BLE> *aT2BleDatamPool, |
ocomeni | 118:8df0e9c2ee3f | 175 | Queue<at_ble_msg_t, PQDSZ_BLE> *aT2BleDataQueue, |
ocomeni | 118:8df0e9c2ee3f | 176 | MemoryPool<ble_at_msg_t, PQDSZ_BLE> *ble2ATDatamPool, |
ocomeni | 118:8df0e9c2ee3f | 177 | Queue<ble_at_msg_t, PQDSZ_BLE> *ble2ATDataQueue, |
ocomeni | 118:8df0e9c2ee3f | 178 | ble_config_t *ble_config); |
ocomeni | 75:08eff6258e1b | 179 | |
ocomeni | 75:08eff6258e1b | 180 | virtual void start(); |
ocomeni | 75:08eff6258e1b | 181 | |
ocomeni | 75:08eff6258e1b | 182 | /** This is called by Gap to notify the application we connected, |
ocomeni | 75:08eff6258e1b | 183 | * in our case it immediately requests a change in link security */ |
ocomeni | 75:08eff6258e1b | 184 | virtual void on_connect(const Gap::ConnectionCallbackParams_t *connection_event); |
ocomeni | 77:0b505d1e15f4 | 185 | void stopAdvertising(); |
ocomeni | 77:0b505d1e15f4 | 186 | void startAdvertising(); |
ocomeni | 119:8d939a902333 | 187 | virtual bool queueBleDataResponse(ble_at_msg_t at_resp); |
ocomeni | 119:8d939a902333 | 188 | virtual bool dequeueATdataResponse(); |
ocomeni | 120:779b74689747 | 189 | virtual void sendATresponseBytes(at_cmd_resp_t at_cmd); |
ocomeni | 119:8d939a902333 | 190 | virtual bool setNextCommand(ble_cmd_t cmd); |
ocomeni | 119:8d939a902333 | 191 | virtual void processQueues(); |
ocomeni | 121:ac4f59839e4f | 192 | private: |
ocomeni | 121:ac4f59839e4f | 193 | void sendConnectionResponses(); |
ocomeni | 121:ac4f59839e4f | 194 | void sendDisconnectionResponses(); |
ocomeni | 75:08eff6258e1b | 195 | }; |
ocomeni | 75:08eff6258e1b | 196 | |
ocomeni | 75:08eff6258e1b | 197 | /** A central device will scan, connect to a peer and request pairing. */ |
ocomeni | 75:08eff6258e1b | 198 | class SMDeviceCentral : public SMDevice { |
ocomeni | 75:08eff6258e1b | 199 | public: |
ocomeni | 78:07bb86e3ce14 | 200 | SMDeviceCentral(BLE &ble, events::EventQueue &event_queue, |
ocomeni | 118:8df0e9c2ee3f | 201 | BLEProtocol::AddressBytes_t &peer_address, |
ocomeni | 118:8df0e9c2ee3f | 202 | MemoryPool<at_ble_msg_t, PQDSZ_BLE> *aT2BleDatamPool, |
ocomeni | 118:8df0e9c2ee3f | 203 | Queue<at_ble_msg_t, PQDSZ_BLE> *aT2BleDataQueue, |
ocomeni | 118:8df0e9c2ee3f | 204 | MemoryPool<ble_at_msg_t, PQDSZ_BLE> *ble2ATDatamPool, |
ocomeni | 118:8df0e9c2ee3f | 205 | Queue<ble_at_msg_t, PQDSZ_BLE> *ble2ATDataQueue, |
ocomeni | 118:8df0e9c2ee3f | 206 | ble_config_t *ble_config); |
ocomeni | 75:08eff6258e1b | 207 | |
ocomeni | 75:08eff6258e1b | 208 | virtual void start(); |
ocomeni | 75:08eff6258e1b | 209 | |
ocomeni | 75:08eff6258e1b | 210 | /** Look at scan payload to find a peer device and connect to it */ |
ocomeni | 75:08eff6258e1b | 211 | void on_scan(const Gap::AdvertisementCallbackParams_t *params); |
ocomeni | 75:08eff6258e1b | 212 | |
ocomeni | 75:08eff6258e1b | 213 | /** This is called by Gap to notify the application we connected, |
ocomeni | 75:08eff6258e1b | 214 | * in our case it immediately request pairing */ |
ocomeni | 75:08eff6258e1b | 215 | virtual void on_connect(const Gap::ConnectionCallbackParams_t *connection_event); |
ocomeni | 75:08eff6258e1b | 216 | }; |
ocomeni | 75:08eff6258e1b | 217 | |
ocomeni | 75:08eff6258e1b | 218 | |
ocomeni | 75:08eff6258e1b | 219 | #if MBED_CONF_APP_FILESYSTEM_SUPPORT |
ocomeni | 75:08eff6258e1b | 220 | bool create_filesystem(); |
ocomeni | 75:08eff6258e1b | 221 | #endif //MBED_CONF_APP_FILESYSTEM_SUPPORT |
ocomeni | 75:08eff6258e1b | 222 | |
ocomeni | 74:f26e846adfe9 | 223 | #endif // __BLE_MANAGER_H__ |