this is using the mbed os version 5-13-1
Diff: source/BleManager.h
- Revision:
- 75:08eff6258e1b
- Parent:
- 74:f26e846adfe9
- Child:
- 76:6afda865fbf8
--- a/source/BleManager.h Sun Mar 10 09:46:06 2019 +0000
+++ b/source/BleManager.h Thu Mar 14 21:34:06 2019 +0000
@@ -1,3 +1,153 @@
-#ifdndef __BLE_MANAGER_H__
+#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"
+
+/** 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);
+
+ 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
+ );
+
+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);
+
+private:
+ DigitalOut _led1;
+
+protected:
+ BLE &_ble;
+ 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);
+
+ 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);
+};
+
+/** 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);
+
+ 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__
\ No newline at end of file