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

Dependencies:   mbed-http

Committer:
ocomeni
Date:
Thu Mar 14 21:34:06 2019 +0000
Revision:
75:08eff6258e1b
Parent:
74:f26e846adfe9
Child:
76:6afda865fbf8
Ble Security example now working!

Who changed what in which revision?

UserRevisionLine numberNew 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 75:08eff6258e1b 25
ocomeni 75:08eff6258e1b 26 /** This example demonstrates all the basic setup required
ocomeni 75:08eff6258e1b 27 * for pairing and setting up link security both as a central and peripheral
ocomeni 75:08eff6258e1b 28 *
ocomeni 75:08eff6258e1b 29 * The example is implemented as two classes, one for the peripheral and one
ocomeni 75:08eff6258e1b 30 * for central inheriting from a common base. They are run in sequence and
ocomeni 75:08eff6258e1b 31 * require a peer device to connect to. During the peripheral device demonstration
ocomeni 75:08eff6258e1b 32 * a peer device is required to connect. In the central device demonstration
ocomeni 75:08eff6258e1b 33 * this peer device will be scanned for and connected to - therefore it should
ocomeni 75:08eff6258e1b 34 * be advertising with the same address as when it connected.
ocomeni 75:08eff6258e1b 35 *
ocomeni 75:08eff6258e1b 36 * During the test output is written on the serial connection to monitor its
ocomeni 75:08eff6258e1b 37 * progress.
ocomeni 75:08eff6258e1b 38 */
ocomeni 75:08eff6258e1b 39
ocomeni 75:08eff6258e1b 40
ocomeni 75:08eff6258e1b 41 /* for demonstration purposes we will store the peer device address
ocomeni 75:08eff6258e1b 42 * of the device that connects to us in the first demonstration
ocomeni 75:08eff6258e1b 43 * so we can use its address to reconnect to it later */
ocomeni 75:08eff6258e1b 44 static BLEProtocol::AddressBytes_t peer_address;
ocomeni 75:08eff6258e1b 45
ocomeni 75:08eff6258e1b 46 /** Base class for both peripheral and central. The same class that provides
ocomeni 75:08eff6258e1b 47 * the logic for the application also implements the SecurityManagerEventHandler
ocomeni 75:08eff6258e1b 48 * which is the interface used by the Security Manager to communicate events
ocomeni 75:08eff6258e1b 49 * back to the applications. You can provide overrides for a selection of events
ocomeni 75:08eff6258e1b 50 * your application is interested in.
ocomeni 75:08eff6258e1b 51 */
ocomeni 75:08eff6258e1b 52 class SMDevice : private mbed::NonCopyable<SMDevice>,
ocomeni 75:08eff6258e1b 53 public SecurityManager::EventHandler
ocomeni 75:08eff6258e1b 54 {
ocomeni 75:08eff6258e1b 55 public:
ocomeni 75:08eff6258e1b 56 SMDevice(BLE &ble, events::EventQueue &event_queue, BLEProtocol::AddressBytes_t &peer_address);
ocomeni 75:08eff6258e1b 57
ocomeni 75:08eff6258e1b 58 virtual ~SMDevice();
ocomeni 75:08eff6258e1b 59
ocomeni 75:08eff6258e1b 60 /** Start BLE interface initialisation */
ocomeni 75:08eff6258e1b 61 void run();
ocomeni 75:08eff6258e1b 62
ocomeni 75:08eff6258e1b 63 /* event handler functions */
ocomeni 75:08eff6258e1b 64
ocomeni 75:08eff6258e1b 65 /** Respond to a pairing request. This will be called by the stack
ocomeni 75:08eff6258e1b 66 * when a pairing request arrives and expects the application to
ocomeni 75:08eff6258e1b 67 * call acceptPairingRequest or cancelPairingRequest */
ocomeni 75:08eff6258e1b 68 virtual void pairingRequest(
ocomeni 75:08eff6258e1b 69 ble::connection_handle_t connectionHandle
ocomeni 75:08eff6258e1b 70 );
ocomeni 75:08eff6258e1b 71
ocomeni 75:08eff6258e1b 72 /** Inform the application of a successful pairing. Terminate the demonstration. */
ocomeni 75:08eff6258e1b 73 virtual void pairingResult(
ocomeni 75:08eff6258e1b 74 ble::connection_handle_t connectionHandle,
ocomeni 75:08eff6258e1b 75 SecurityManager::SecurityCompletionStatus_t result
ocomeni 75:08eff6258e1b 76 );
ocomeni 75:08eff6258e1b 77
ocomeni 75:08eff6258e1b 78 /** Inform the application of change in encryption status. This will be
ocomeni 75:08eff6258e1b 79 * communicated through the serial port */
ocomeni 75:08eff6258e1b 80 virtual void linkEncryptionResult(
ocomeni 75:08eff6258e1b 81 ble::connection_handle_t connectionHandle,
ocomeni 75:08eff6258e1b 82 ble::link_encryption_t result
ocomeni 75:08eff6258e1b 83 );
ocomeni 75:08eff6258e1b 84
ocomeni 75:08eff6258e1b 85 private:
ocomeni 75:08eff6258e1b 86 /** Override to start chosen activity when initialisation completes */
ocomeni 75:08eff6258e1b 87 virtual void start() = 0;
ocomeni 75:08eff6258e1b 88
ocomeni 75:08eff6258e1b 89 /** This is called when BLE interface is initialised and starts the demonstration */
ocomeni 75:08eff6258e1b 90 void on_init_complete(BLE::InitializationCompleteCallbackContext *event);
ocomeni 75:08eff6258e1b 91
ocomeni 75:08eff6258e1b 92 /** This is called by Gap to notify the application we connected */
ocomeni 75:08eff6258e1b 93 virtual void on_connect(const Gap::ConnectionCallbackParams_t *connection_event) = 0;
ocomeni 75:08eff6258e1b 94
ocomeni 75:08eff6258e1b 95 /** This is called by Gap to notify the application we disconnected,
ocomeni 75:08eff6258e1b 96 * in our case it ends the demonstration. */
ocomeni 75:08eff6258e1b 97 void on_disconnect(const Gap::DisconnectionCallbackParams_t *event);
ocomeni 75:08eff6258e1b 98
ocomeni 75:08eff6258e1b 99 /** End demonstration unexpectedly. Called if timeout is reached during advertising,
ocomeni 75:08eff6258e1b 100 * scanning or connection initiation */
ocomeni 75:08eff6258e1b 101 void on_timeout(const Gap::TimeoutSource_t source);
ocomeni 75:08eff6258e1b 102
ocomeni 75:08eff6258e1b 103 /** Schedule processing of events from the BLE in the event queue. */
ocomeni 75:08eff6258e1b 104 void schedule_ble_events(BLE::OnEventsToProcessCallbackContext *context);
ocomeni 75:08eff6258e1b 105
ocomeni 75:08eff6258e1b 106 /** Blink LED to show we're running */
ocomeni 75:08eff6258e1b 107 void blink(void);
ocomeni 75:08eff6258e1b 108
ocomeni 75:08eff6258e1b 109 private:
ocomeni 75:08eff6258e1b 110 DigitalOut _led1;
ocomeni 75:08eff6258e1b 111
ocomeni 75:08eff6258e1b 112 protected:
ocomeni 75:08eff6258e1b 113 BLE &_ble;
ocomeni 75:08eff6258e1b 114 events::EventQueue &_event_queue;
ocomeni 75:08eff6258e1b 115 BLEProtocol::AddressBytes_t &_peer_address;
ocomeni 75:08eff6258e1b 116 ble::connection_handle_t _handle;
ocomeni 75:08eff6258e1b 117 bool _is_connecting;
ocomeni 75:08eff6258e1b 118 };
ocomeni 75:08eff6258e1b 119
ocomeni 75:08eff6258e1b 120 /** A peripheral device will advertise, accept the connection and request
ocomeni 75:08eff6258e1b 121 * a change in link security. */
ocomeni 75:08eff6258e1b 122 class SMDevicePeripheral : public SMDevice {
ocomeni 75:08eff6258e1b 123 public:
ocomeni 75:08eff6258e1b 124 SMDevicePeripheral(BLE &ble, events::EventQueue &event_queue, BLEProtocol::AddressBytes_t &peer_address);
ocomeni 75:08eff6258e1b 125
ocomeni 75:08eff6258e1b 126 virtual void start();
ocomeni 75:08eff6258e1b 127
ocomeni 75:08eff6258e1b 128 /** This is called by Gap to notify the application we connected,
ocomeni 75:08eff6258e1b 129 * in our case it immediately requests a change in link security */
ocomeni 75:08eff6258e1b 130 virtual void on_connect(const Gap::ConnectionCallbackParams_t *connection_event);
ocomeni 75:08eff6258e1b 131 };
ocomeni 75:08eff6258e1b 132
ocomeni 75:08eff6258e1b 133 /** A central device will scan, connect to a peer and request pairing. */
ocomeni 75:08eff6258e1b 134 class SMDeviceCentral : public SMDevice {
ocomeni 75:08eff6258e1b 135 public:
ocomeni 75:08eff6258e1b 136 SMDeviceCentral(BLE &ble, events::EventQueue &event_queue, BLEProtocol::AddressBytes_t &peer_address);
ocomeni 75:08eff6258e1b 137
ocomeni 75:08eff6258e1b 138 virtual void start();
ocomeni 75:08eff6258e1b 139
ocomeni 75:08eff6258e1b 140 /** Look at scan payload to find a peer device and connect to it */
ocomeni 75:08eff6258e1b 141 void on_scan(const Gap::AdvertisementCallbackParams_t *params);
ocomeni 75:08eff6258e1b 142
ocomeni 75:08eff6258e1b 143 /** This is called by Gap to notify the application we connected,
ocomeni 75:08eff6258e1b 144 * in our case it immediately request pairing */
ocomeni 75:08eff6258e1b 145 virtual void on_connect(const Gap::ConnectionCallbackParams_t *connection_event);
ocomeni 75:08eff6258e1b 146 };
ocomeni 75:08eff6258e1b 147
ocomeni 75:08eff6258e1b 148
ocomeni 75:08eff6258e1b 149 #if MBED_CONF_APP_FILESYSTEM_SUPPORT
ocomeni 75:08eff6258e1b 150 bool create_filesystem();
ocomeni 75:08eff6258e1b 151 #endif //MBED_CONF_APP_FILESYSTEM_SUPPORT
ocomeni 75:08eff6258e1b 152
ocomeni 74:f26e846adfe9 153 #endif // __BLE_MANAGER_H__