this is using the mbed os version 5-13-1

Dependencies:   mbed-http

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BleManager.h Source File

BleManager.h

00001 #ifndef __BLE_MANAGER_H__
00002 #define __BLE_MANAGER_H__
00003 
00004 
00005 /* mbed Microcontroller Library
00006  * Copyright (c) 2006-2013 ARM Limited
00007  *
00008  * Licensed under the Apache License, Version 2.0 (the "License");
00009  * you may not use this file except in compliance with the License.
00010  * You may obtain a copy of the License at
00011  *
00012  *     http://www.apache.org/licenses/LICENSE-2.0
00013  *
00014  * Unless required by applicable law or agreed to in writing, software
00015  * distributed under the License is distributed on an "AS IS" BASIS,
00016  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00017  * See the License for the specific language governing permissions and
00018  * limitations under the License.
00019  */
00020 
00021 #include <events/mbed_events.h>
00022 #include <mbed.h>
00023 #include "ble/BLE.h"
00024 #include "SecurityManager.h"
00025 #include "common_types.h"
00026 
00027 /** This example demonstrates all the basic setup required
00028  *  for pairing and setting up link security both as a central and peripheral
00029  *
00030  *  The example is implemented as two classes, one for the peripheral and one
00031  *  for central inheriting from a common base. They are run in sequence and
00032  *  require a peer device to connect to. During the peripheral device demonstration
00033  *  a peer device is required to connect. In the central device demonstration
00034  *  this peer device will be scanned for and connected to - therefore it should
00035  *  be advertising with the same address as when it connected.
00036  *
00037  *  During the test output is written on the serial connection to monitor its
00038  *  progress.
00039  */
00040 
00041 
00042 /* for demonstration purposes we will store the peer device address
00043  * of the device that connects to us in the first demonstration
00044  * so we can use its address to reconnect to it later */
00045 static BLEProtocol::AddressBytes_t peer_address;
00046 
00047 /** Base class for both peripheral and central. The same class that provides
00048  *  the logic for the application also implements the SecurityManagerEventHandler
00049  *  which is the interface used by the Security Manager to communicate events
00050  *  back to the applications. You can provide overrides for a selection of events
00051  *  your application is interested in.
00052  */
00053 class SMDevice : private mbed::NonCopyable<SMDevice>,
00054                  public SecurityManager::EventHandler
00055 {
00056 public:
00057     SMDevice(BLE &ble, events::EventQueue &event_queue, 
00058              BLEProtocol::AddressBytes_t &peer_address, 
00059              MemoryPool<at_ble_msg_t, PQDSZ_BLE> *aT2BleDatamPool, 
00060              Queue<at_ble_msg_t, PQDSZ_BLE> *aT2BleDataQueue, 
00061              MemoryPool<ble_at_msg_t, PQDSZ_BLE> *ble2ATDatamPool, 
00062              Queue<ble_at_msg_t, PQDSZ_BLE> *ble2ATDataQueue, 
00063              ble_config_t *ble_config);
00064 
00065     virtual ~SMDevice();
00066 
00067     /** Start BLE interface initialisation */
00068     void run();
00069 
00070     /* event handler functions */
00071 
00072     /** Respond to a pairing request. This will be called by the stack
00073      * when a pairing request arrives and expects the application to
00074      * call acceptPairingRequest or cancelPairingRequest */
00075     virtual void pairingRequest(
00076         ble::connection_handle_t connectionHandle
00077     );
00078 
00079     /** Inform the application of a successful pairing. Terminate the demonstration. */
00080     virtual void pairingResult(
00081         ble::connection_handle_t connectionHandle,
00082         SecurityManager::SecurityCompletionStatus_t result
00083     );
00084 
00085     /** Inform the application of change in encryption status. This will be
00086      * communicated through the serial port */
00087     virtual void linkEncryptionResult(
00088         ble::connection_handle_t connectionHandle,
00089         ble::link_encryption_t result
00090     );
00091     
00092     void shutDown();
00093     void sendBLEUartData(const uint8_t * buf, int len);
00094 
00095 protected:
00096     // connection status
00097     bool isConnected;
00098     char buffer[MAX_BLE_PACKET_SIZE];
00099 private:
00100    /** Override to start chosen activity when initialisation completes */
00101     virtual void start() = 0;
00102 
00103     /** This is called when BLE interface is initialised and starts the demonstration */
00104     void on_init_complete(BLE::InitializationCompleteCallbackContext *event);
00105 
00106     /** This is called by Gap to notify the application we connected */
00107     virtual void on_connect(const Gap::ConnectionCallbackParams_t *connection_event) = 0;
00108 
00109     /** This is called by Gap to notify the application we disconnected,
00110      *  in our case it ends the demonstration. */
00111     void on_disconnect(const Gap::DisconnectionCallbackParams_t *event);
00112 
00113     /** End demonstration unexpectedly. Called if timeout is reached during advertising,
00114      * scanning or connection initiation */
00115     void on_timeout(const Gap::TimeoutSource_t source);
00116 
00117     /** Schedule processing of events from the BLE in the event queue. */
00118     void schedule_ble_events(BLE::OnEventsToProcessCallbackContext *context);
00119 
00120     /** Blink LED to show we're running */
00121     void blink(void);
00122     /** Echo received data back                                       */
00123     void EchoBleUartReceived();
00124     
00125 
00126     void reportGapState();
00127 
00128     // application virtual methods called by peripheral
00129     virtual bool queueBleDataResponse(ble_at_msg_t at_resp) = 0;
00130     virtual bool dequeueATdataResponse() = 0;
00131     virtual void sendATresponseBytes(at_cmd_resp_t at_cmd) = 0;
00132     virtual bool setNextCommand(ble_cmd_t cmd) = 0;
00133     virtual void processQueues() = 0;
00134     
00135     /**
00136      * This callback allows the LEDService to receive updates to the ledState Characteristic.
00137      *
00138      * @param[in] params
00139      *     Information about the characterisitc being updated.
00140      */
00141     void onDataWrittenCallback(const GattWriteCallbackParams *params);
00142     //void passkeyDisplayCallback(Gap::Handle_t handle, const SecurityManager::Passkey_t passkey);
00143     //void securitySetupCompletedCallback(Gap::Handle_t handle, SecurityManager::SecurityCompletionStatus_t status);
00144 
00145 protected:
00146     BLE &_ble;
00147     events::EventQueue &_event_queue;
00148     BLEProtocol::AddressBytes_t &_peer_address;
00149     ble_cmd_t bleCmd;
00150     at_ble_msg_t *data_msg;
00151     ble_at_msg_t *at_data_resp;
00152 protected:
00153     /*  Queue and memory pool for AT to BLE data */
00154     MemoryPool<at_ble_msg_t, PQDSZ_BLE> *_aT2BleDatamPool;
00155     Queue<at_ble_msg_t, PQDSZ_BLE> *_aT2BleDataQueue;
00156     
00157     
00158     /*  Queue and memory pool for BLE to AT data */
00159     MemoryPool<ble_at_msg_t, PQDSZ_BLE> *_ble2ATDatamPool;
00160     Queue<ble_at_msg_t, PQDSZ_BLE> *_ble2ATDataQueue;
00161     ble_config_t *ble_config;
00162     ble::connection_handle_t _handle;
00163     bool _is_connecting;
00164 private:
00165     DigitalOut _led1;
00166 };
00167 
00168 /** A peripheral device will advertise, accept the connection and request
00169  * a change in link security. */
00170 class SMDevicePeripheral : public SMDevice {
00171 public:
00172     SMDevicePeripheral(BLE &ble, events::EventQueue &event_queue, 
00173                        BLEProtocol::AddressBytes_t &peer_address, 
00174                        MemoryPool<at_ble_msg_t, PQDSZ_BLE> *aT2BleDatamPool, 
00175                        Queue<at_ble_msg_t, PQDSZ_BLE> *aT2BleDataQueue, 
00176                        MemoryPool<ble_at_msg_t, PQDSZ_BLE> *ble2ATDatamPool, 
00177                        Queue<ble_at_msg_t, PQDSZ_BLE> *ble2ATDataQueue, 
00178                        ble_config_t *ble_config);
00179 
00180     virtual void start();
00181 
00182     /** This is called by Gap to notify the application we connected,
00183      *  in our case it immediately requests a change in link security */
00184     virtual void on_connect(const Gap::ConnectionCallbackParams_t *connection_event);
00185     void stopAdvertising();
00186     void startAdvertising();
00187     virtual bool queueBleDataResponse(ble_at_msg_t at_resp);
00188     virtual bool dequeueATdataResponse();
00189     virtual void sendATresponseBytes(at_cmd_resp_t at_cmd);
00190     virtual bool setNextCommand(ble_cmd_t cmd);
00191     virtual void processQueues();
00192 private:
00193     void sendConnectionResponses();
00194     void sendDisconnectionResponses();    
00195 };
00196 
00197 /** A central device will scan, connect to a peer and request pairing. */
00198 class SMDeviceCentral : public SMDevice {
00199 public:
00200     SMDeviceCentral(BLE &ble, events::EventQueue &event_queue, 
00201                     BLEProtocol::AddressBytes_t &peer_address, 
00202                     MemoryPool<at_ble_msg_t, PQDSZ_BLE> *aT2BleDatamPool, 
00203                     Queue<at_ble_msg_t, PQDSZ_BLE> *aT2BleDataQueue, 
00204                     MemoryPool<ble_at_msg_t, PQDSZ_BLE> *ble2ATDatamPool, 
00205                     Queue<ble_at_msg_t, PQDSZ_BLE> *ble2ATDataQueue, 
00206                     ble_config_t *ble_config);
00207 
00208     virtual void start();
00209 
00210     /** Look at scan payload to find a peer device and connect to it */
00211     void on_scan(const Gap::AdvertisementCallbackParams_t *params);
00212 
00213     /** This is called by Gap to notify the application we connected,
00214      *  in our case it immediately request pairing */
00215     virtual void on_connect(const Gap::ConnectionCallbackParams_t *connection_event);
00216 };
00217 
00218 
00219 #if MBED_CONF_APP_FILESYSTEM_SUPPORT
00220 bool create_filesystem();
00221 #endif //MBED_CONF_APP_FILESYSTEM_SUPPORT
00222 
00223 #endif // __BLE_MANAGER_H__