
this is using the mbed os version 5-13-1
source/BleManager.h
- Committer:
- ocomeni
- Date:
- 2019-03-16
- Revision:
- 78:07bb86e3ce14
- Parent:
- 77:0b505d1e15f4
- Child:
- 79:a2187bbfa407
File content as of revision 78:07bb86e3ce14:
#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, 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(); 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(); /** * 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); private: DigitalOut _led1; protected: BLE &_ble; ble_config_t ble_config; events::EventQueue &_event_queue; BLEProtocol::AddressBytes_t &_peer_address; ble::connection_handle_t _handle; bool _is_connecting; }; /** 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, 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(); }; /** 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, 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__