
this is using the mbed os version 5-13-1
source/BleManager.h
- Committer:
- ocomeni
- Date:
- 2019-07-19
- Branch:
- PassingRegression
- Revision:
- 129:590bdc2dcf5b
- Parent:
- 121:ac4f59839e4f
File content as of revision 129:590bdc2dcf5b:
#ifndef __BLE_MANAGER_H__ #define __BLE_MANAGER_H__ /* mbed Microcontroller Library * Copyright (c) 2006-2013 ARM Limited * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include <events/mbed_events.h> #include <mbed.h> #include "ble/BLE.h" #include "SecurityManager.h" #include "common_types.h" /** This example demonstrates all the basic setup required * for pairing and setting up link security both as a central and peripheral * * The example is implemented as two classes, one for the peripheral and one * for central inheriting from a common base. They are run in sequence and * require a peer device to connect to. During the peripheral device demonstration * a peer device is required to connect. In the central device demonstration * this peer device will be scanned for and connected to - therefore it should * be advertising with the same address as when it connected. * * During the test output is written on the serial connection to monitor its * progress. */ /* for demonstration purposes we will store the peer device address * of the device that connects to us in the first demonstration * so we can use its address to reconnect to it later */ static BLEProtocol::AddressBytes_t peer_address; /** Base class for both peripheral and central. The same class that provides * the logic for the application also implements the SecurityManagerEventHandler * which is the interface used by the Security Manager to communicate events * back to the applications. You can provide overrides for a selection of events * your application is interested in. */ class SMDevice : private mbed::NonCopyable<SMDevice>, public SecurityManager::EventHandler { public: SMDevice(BLE &ble, events::EventQueue &event_queue, BLEProtocol::AddressBytes_t &peer_address, MemoryPool<at_ble_msg_t, PQDSZ_BLE> *aT2BleDatamPool, Queue<at_ble_msg_t, PQDSZ_BLE> *aT2BleDataQueue, MemoryPool<ble_at_msg_t, PQDSZ_BLE> *ble2ATDatamPool, Queue<ble_at_msg_t, PQDSZ_BLE> *ble2ATDataQueue, ble_config_t *ble_config); virtual ~SMDevice(); /** Start BLE interface initialisation */ void run(); /* event handler functions */ /** Respond to a pairing request. This will be called by the stack * when a pairing request arrives and expects the application to * call acceptPairingRequest or cancelPairingRequest */ virtual void pairingRequest( ble::connection_handle_t connectionHandle ); /** Inform the application of a successful pairing. Terminate the demonstration. */ virtual void pairingResult( ble::connection_handle_t connectionHandle, SecurityManager::SecurityCompletionStatus_t result ); /** Inform the application of change in encryption status. This will be * communicated through the serial port */ virtual void linkEncryptionResult( ble::connection_handle_t connectionHandle, ble::link_encryption_t result ); void shutDown(); void sendBLEUartData(const uint8_t * buf, int len); protected: // connection status bool isConnected; char buffer[MAX_BLE_PACKET_SIZE]; private: /** Override to start chosen activity when initialisation completes */ virtual void start() = 0; /** This is called when BLE interface is initialised and starts the demonstration */ void on_init_complete(BLE::InitializationCompleteCallbackContext *event); /** This is called by Gap to notify the application we connected */ virtual void on_connect(const Gap::ConnectionCallbackParams_t *connection_event) = 0; /** This is called by Gap to notify the application we disconnected, * in our case it ends the demonstration. */ void on_disconnect(const Gap::DisconnectionCallbackParams_t *event); /** End demonstration unexpectedly. Called if timeout is reached during advertising, * scanning or connection initiation */ void on_timeout(const Gap::TimeoutSource_t source); /** Schedule processing of events from the BLE in the event queue. */ void schedule_ble_events(BLE::OnEventsToProcessCallbackContext *context); /** Blink LED to show we're running */ void blink(void); /** Echo received data back */ void EchoBleUartReceived(); void reportGapState(); // application virtual methods called by peripheral virtual bool queueBleDataResponse(ble_at_msg_t at_resp) = 0; virtual bool dequeueATdataResponse() = 0; virtual void sendATresponseBytes(at_cmd_resp_t at_cmd) = 0; virtual bool setNextCommand(ble_cmd_t cmd) = 0; virtual void processQueues() = 0; /** * This callback allows the LEDService to receive updates to the ledState Characteristic. * * @param[in] params * Information about the characterisitc being updated. */ void onDataWrittenCallback(const GattWriteCallbackParams *params); //void passkeyDisplayCallback(Gap::Handle_t handle, const SecurityManager::Passkey_t passkey); //void securitySetupCompletedCallback(Gap::Handle_t handle, SecurityManager::SecurityCompletionStatus_t status); protected: BLE &_ble; events::EventQueue &_event_queue; BLEProtocol::AddressBytes_t &_peer_address; ble_cmd_t bleCmd; at_ble_msg_t *data_msg; ble_at_msg_t *at_data_resp; protected: /* Queue and memory pool for AT to BLE data */ MemoryPool<at_ble_msg_t, PQDSZ_BLE> *_aT2BleDatamPool; Queue<at_ble_msg_t, PQDSZ_BLE> *_aT2BleDataQueue; /* Queue and memory pool for BLE to AT data */ MemoryPool<ble_at_msg_t, PQDSZ_BLE> *_ble2ATDatamPool; Queue<ble_at_msg_t, PQDSZ_BLE> *_ble2ATDataQueue; ble_config_t *ble_config; ble::connection_handle_t _handle; bool _is_connecting; private: DigitalOut _led1; }; /** A peripheral device will advertise, accept the connection and request * a change in link security. */ class SMDevicePeripheral : public SMDevice { public: SMDevicePeripheral(BLE &ble, events::EventQueue &event_queue, BLEProtocol::AddressBytes_t &peer_address, MemoryPool<at_ble_msg_t, PQDSZ_BLE> *aT2BleDatamPool, Queue<at_ble_msg_t, PQDSZ_BLE> *aT2BleDataQueue, MemoryPool<ble_at_msg_t, PQDSZ_BLE> *ble2ATDatamPool, Queue<ble_at_msg_t, PQDSZ_BLE> *ble2ATDataQueue, ble_config_t *ble_config); virtual void start(); /** This is called by Gap to notify the application we connected, * in our case it immediately requests a change in link security */ virtual void on_connect(const Gap::ConnectionCallbackParams_t *connection_event); void stopAdvertising(); void startAdvertising(); virtual bool queueBleDataResponse(ble_at_msg_t at_resp); virtual bool dequeueATdataResponse(); virtual void sendATresponseBytes(at_cmd_resp_t at_cmd); virtual bool setNextCommand(ble_cmd_t cmd); virtual void processQueues(); private: void sendConnectionResponses(); void sendDisconnectionResponses(); }; /** A central device will scan, connect to a peer and request pairing. */ class SMDeviceCentral : public SMDevice { public: SMDeviceCentral(BLE &ble, events::EventQueue &event_queue, BLEProtocol::AddressBytes_t &peer_address, MemoryPool<at_ble_msg_t, PQDSZ_BLE> *aT2BleDatamPool, Queue<at_ble_msg_t, PQDSZ_BLE> *aT2BleDataQueue, MemoryPool<ble_at_msg_t, PQDSZ_BLE> *ble2ATDatamPool, Queue<ble_at_msg_t, PQDSZ_BLE> *ble2ATDataQueue, ble_config_t *ble_config); virtual void start(); /** Look at scan payload to find a peer device and connect to it */ void on_scan(const Gap::AdvertisementCallbackParams_t *params); /** This is called by Gap to notify the application we connected, * in our case it immediately request pairing */ virtual void on_connect(const Gap::ConnectionCallbackParams_t *connection_event); }; #if MBED_CONF_APP_FILESYSTEM_SUPPORT bool create_filesystem(); #endif //MBED_CONF_APP_FILESYSTEM_SUPPORT #endif // __BLE_MANAGER_H__