Gestione Bluetooth per il progetto Tarallo

Committer:
fdalforno
Date:
Thu Jan 16 14:48:30 2020 +0000
Revision:
82:443323e7d4c9
Gestione client Bluetooth per scheda F401RE

Who changed what in which revision?

UserRevisionLine numberNew contents of line
fdalforno 82:443323e7d4c9 1 /* mbed Microcontroller Library
fdalforno 82:443323e7d4c9 2 * Copyright (c) 2006-2015 ARM Limited
fdalforno 82:443323e7d4c9 3 *
fdalforno 82:443323e7d4c9 4 * Licensed under the Apache License, Version 2.0 (the "License");
fdalforno 82:443323e7d4c9 5 * you may not use this file except in compliance with the License.
fdalforno 82:443323e7d4c9 6 * You may obtain a copy of the License at
fdalforno 82:443323e7d4c9 7 *
fdalforno 82:443323e7d4c9 8 * http://www.apache.org/licenses/LICENSE-2.0
fdalforno 82:443323e7d4c9 9 *
fdalforno 82:443323e7d4c9 10 * Unless required by applicable law or agreed to in writing, software
fdalforno 82:443323e7d4c9 11 * distributed under the License is distributed on an "AS IS" BASIS,
fdalforno 82:443323e7d4c9 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
fdalforno 82:443323e7d4c9 13 * See the License for the specific language governing permissions and
fdalforno 82:443323e7d4c9 14 * limitations under the License.
fdalforno 82:443323e7d4c9 15 */
fdalforno 82:443323e7d4c9 16
fdalforno 82:443323e7d4c9 17 #ifndef GATT_EXAMPLE_BLE_PROCESS_H_
fdalforno 82:443323e7d4c9 18 #define GATT_EXAMPLE_BLE_PROCESS_H_
fdalforno 82:443323e7d4c9 19
fdalforno 82:443323e7d4c9 20 #include <stdint.h>
fdalforno 82:443323e7d4c9 21 #include <stdio.h>
fdalforno 82:443323e7d4c9 22
fdalforno 82:443323e7d4c9 23 #include "events/EventQueue.h"
fdalforno 82:443323e7d4c9 24 #include "platform/Callback.h"
fdalforno 82:443323e7d4c9 25 #include "platform/NonCopyable.h"
fdalforno 82:443323e7d4c9 26
fdalforno 82:443323e7d4c9 27 #include "ble/BLE.h"
fdalforno 82:443323e7d4c9 28 #include "ble/Gap.h"
fdalforno 82:443323e7d4c9 29 #include "ble/GapAdvertisingParams.h"
fdalforno 82:443323e7d4c9 30 #include "ble/GapAdvertisingData.h"
fdalforno 82:443323e7d4c9 31 #include "ble/FunctionPointerWithContext.h"
fdalforno 82:443323e7d4c9 32
fdalforno 82:443323e7d4c9 33 /**
fdalforno 82:443323e7d4c9 34 * Handle initialization and shutdown of the BLE Instance.
fdalforno 82:443323e7d4c9 35 *
fdalforno 82:443323e7d4c9 36 * Setup advertising payload and manage advertising state.
fdalforno 82:443323e7d4c9 37 * Delegate to GattClientProcess once the connection is established.
fdalforno 82:443323e7d4c9 38 */
fdalforno 82:443323e7d4c9 39 class BLEProcess : private mbed::NonCopyable<BLEProcess> {
fdalforno 82:443323e7d4c9 40 public:
fdalforno 82:443323e7d4c9 41 /**
fdalforno 82:443323e7d4c9 42 * Construct a BLEProcess from an event queue and a ble interface.
fdalforno 82:443323e7d4c9 43 *
fdalforno 82:443323e7d4c9 44 * Call start() to initiate ble processing.
fdalforno 82:443323e7d4c9 45 */
fdalforno 82:443323e7d4c9 46 BLEProcess(events::EventQueue &event_queue, BLE &ble_interface) :
fdalforno 82:443323e7d4c9 47 _event_queue(event_queue),
fdalforno 82:443323e7d4c9 48 _ble_interface(ble_interface),
fdalforno 82:443323e7d4c9 49 _post_init_cb() {
fdalforno 82:443323e7d4c9 50 }
fdalforno 82:443323e7d4c9 51
fdalforno 82:443323e7d4c9 52 ~BLEProcess()
fdalforno 82:443323e7d4c9 53 {
fdalforno 82:443323e7d4c9 54 stop();
fdalforno 82:443323e7d4c9 55 }
fdalforno 82:443323e7d4c9 56
fdalforno 82:443323e7d4c9 57 /**
fdalforno 82:443323e7d4c9 58 * Initialize the ble interface, configure it and start advertising.
fdalforno 82:443323e7d4c9 59 */
fdalforno 82:443323e7d4c9 60 bool start()
fdalforno 82:443323e7d4c9 61 {
fdalforno 82:443323e7d4c9 62 printf("Ble process started.\r\n");
fdalforno 82:443323e7d4c9 63
fdalforno 82:443323e7d4c9 64 if (_ble_interface.hasInitialized()) {
fdalforno 82:443323e7d4c9 65 printf("Error: the ble instance has already been initialized.\r\n");
fdalforno 82:443323e7d4c9 66 return false;
fdalforno 82:443323e7d4c9 67 }
fdalforno 82:443323e7d4c9 68
fdalforno 82:443323e7d4c9 69 _ble_interface.onEventsToProcess(
fdalforno 82:443323e7d4c9 70 makeFunctionPointer(this, &BLEProcess::schedule_ble_events)
fdalforno 82:443323e7d4c9 71 );
fdalforno 82:443323e7d4c9 72
fdalforno 82:443323e7d4c9 73 ble_error_t error = _ble_interface.init(
fdalforno 82:443323e7d4c9 74 this, &BLEProcess::when_init_complete
fdalforno 82:443323e7d4c9 75 );
fdalforno 82:443323e7d4c9 76
fdalforno 82:443323e7d4c9 77 if (error) {
fdalforno 82:443323e7d4c9 78 printf("Error: %u returned by BLE::init.\r\n", error);
fdalforno 82:443323e7d4c9 79 return false;
fdalforno 82:443323e7d4c9 80 }
fdalforno 82:443323e7d4c9 81
fdalforno 82:443323e7d4c9 82 return true;
fdalforno 82:443323e7d4c9 83 }
fdalforno 82:443323e7d4c9 84
fdalforno 82:443323e7d4c9 85 /**
fdalforno 82:443323e7d4c9 86 * Close existing connections and stop the process.
fdalforno 82:443323e7d4c9 87 */
fdalforno 82:443323e7d4c9 88 void stop()
fdalforno 82:443323e7d4c9 89 {
fdalforno 82:443323e7d4c9 90 if (_ble_interface.hasInitialized()) {
fdalforno 82:443323e7d4c9 91 _ble_interface.shutdown();
fdalforno 82:443323e7d4c9 92 printf("Ble process stopped.");
fdalforno 82:443323e7d4c9 93 }
fdalforno 82:443323e7d4c9 94 }
fdalforno 82:443323e7d4c9 95
fdalforno 82:443323e7d4c9 96 /**
fdalforno 82:443323e7d4c9 97 * Subscription to the ble interface initialization event.
fdalforno 82:443323e7d4c9 98 *
fdalforno 82:443323e7d4c9 99 * @param[in] cb The callback object that will be called when the ble
fdalforno 82:443323e7d4c9 100 * interface is initialized.
fdalforno 82:443323e7d4c9 101 */
fdalforno 82:443323e7d4c9 102 void on_init(mbed::Callback<void(BLE&, events::EventQueue&)> cb)
fdalforno 82:443323e7d4c9 103 {
fdalforno 82:443323e7d4c9 104 _post_init_cb = cb;
fdalforno 82:443323e7d4c9 105 }
fdalforno 82:443323e7d4c9 106
fdalforno 82:443323e7d4c9 107 private:
fdalforno 82:443323e7d4c9 108 /**
fdalforno 82:443323e7d4c9 109 * Sets up adverting payload and start advertising.
fdalforno 82:443323e7d4c9 110 *
fdalforno 82:443323e7d4c9 111 * This function is invoked when the ble interface is initialized.
fdalforno 82:443323e7d4c9 112 */
fdalforno 82:443323e7d4c9 113 void when_init_complete(BLE::InitializationCompleteCallbackContext *event)
fdalforno 82:443323e7d4c9 114 {
fdalforno 82:443323e7d4c9 115 if (event->error) {
fdalforno 82:443323e7d4c9 116 printf("Error %u during the initialization\r\n", event->error);
fdalforno 82:443323e7d4c9 117 return;
fdalforno 82:443323e7d4c9 118 }
fdalforno 82:443323e7d4c9 119 printf("Ble instance initialized\r\n");
fdalforno 82:443323e7d4c9 120
fdalforno 82:443323e7d4c9 121 Gap &gap = _ble_interface.gap();
fdalforno 82:443323e7d4c9 122 ble_error_t error = gap.setAdvertisingPayload(make_advertising_data());
fdalforno 82:443323e7d4c9 123 if (error) {
fdalforno 82:443323e7d4c9 124 printf("Error %u during gap.setAdvertisingPayload\r\n", error);
fdalforno 82:443323e7d4c9 125 return;
fdalforno 82:443323e7d4c9 126 }
fdalforno 82:443323e7d4c9 127
fdalforno 82:443323e7d4c9 128 gap.setAdvertisingParams(make_advertising_params());
fdalforno 82:443323e7d4c9 129
fdalforno 82:443323e7d4c9 130 gap.onConnection(this, &BLEProcess::when_connection);
fdalforno 82:443323e7d4c9 131 gap.onDisconnection(this, &BLEProcess::when_disconnection);
fdalforno 82:443323e7d4c9 132
fdalforno 82:443323e7d4c9 133 start_advertising();
fdalforno 82:443323e7d4c9 134
fdalforno 82:443323e7d4c9 135 if (_post_init_cb) {
fdalforno 82:443323e7d4c9 136 _post_init_cb(_ble_interface, _event_queue);
fdalforno 82:443323e7d4c9 137 }
fdalforno 82:443323e7d4c9 138 }
fdalforno 82:443323e7d4c9 139
fdalforno 82:443323e7d4c9 140 /**
fdalforno 82:443323e7d4c9 141 * Start the gatt client process when a connection event is received.
fdalforno 82:443323e7d4c9 142 */
fdalforno 82:443323e7d4c9 143 void when_connection(const Gap::ConnectionCallbackParams_t *connection_event)
fdalforno 82:443323e7d4c9 144 {
fdalforno 82:443323e7d4c9 145 printf("Connected.\r\n");
fdalforno 82:443323e7d4c9 146 }
fdalforno 82:443323e7d4c9 147
fdalforno 82:443323e7d4c9 148 /**
fdalforno 82:443323e7d4c9 149 * Stop the gatt client process when the device is disconnected then restart
fdalforno 82:443323e7d4c9 150 * advertising.
fdalforno 82:443323e7d4c9 151 */
fdalforno 82:443323e7d4c9 152 void when_disconnection(const Gap::DisconnectionCallbackParams_t *event)
fdalforno 82:443323e7d4c9 153 {
fdalforno 82:443323e7d4c9 154 printf("Disconnected.\r\n");
fdalforno 82:443323e7d4c9 155 start_advertising();
fdalforno 82:443323e7d4c9 156 }
fdalforno 82:443323e7d4c9 157
fdalforno 82:443323e7d4c9 158 /**
fdalforno 82:443323e7d4c9 159 * Setup the advertising parameters.
fdalforno 82:443323e7d4c9 160 */
fdalforno 82:443323e7d4c9 161 void setup_advertising()
fdalforno 82:443323e7d4c9 162 {
fdalforno 82:443323e7d4c9 163 Gap &gap = _ble_interface.gap();
fdalforno 82:443323e7d4c9 164 gap.setAdvertisingPayload(make_advertising_data());
fdalforno 82:443323e7d4c9 165 gap.setAdvertisingParams(make_advertising_params());
fdalforno 82:443323e7d4c9 166 }
fdalforno 82:443323e7d4c9 167
fdalforno 82:443323e7d4c9 168 /**
fdalforno 82:443323e7d4c9 169 * Start the advertising process; it ends when a device connects.
fdalforno 82:443323e7d4c9 170 */
fdalforno 82:443323e7d4c9 171 void start_advertising()
fdalforno 82:443323e7d4c9 172 {
fdalforno 82:443323e7d4c9 173 ble_error_t error = _ble_interface.gap().startAdvertising();
fdalforno 82:443323e7d4c9 174 if (error) {
fdalforno 82:443323e7d4c9 175 printf("Error %u during gap.startAdvertising.\r\n", error);
fdalforno 82:443323e7d4c9 176 } else {
fdalforno 82:443323e7d4c9 177 printf("Advertising started.\r\n");
fdalforno 82:443323e7d4c9 178 }
fdalforno 82:443323e7d4c9 179 }
fdalforno 82:443323e7d4c9 180
fdalforno 82:443323e7d4c9 181 /**
fdalforno 82:443323e7d4c9 182 * Schedule processing of events from the BLE middleware in the event queue.
fdalforno 82:443323e7d4c9 183 */
fdalforno 82:443323e7d4c9 184 void schedule_ble_events(BLE::OnEventsToProcessCallbackContext *event)
fdalforno 82:443323e7d4c9 185 {
fdalforno 82:443323e7d4c9 186 _event_queue.call(mbed::callback(&event->ble, &BLE::processEvents));
fdalforno 82:443323e7d4c9 187 }
fdalforno 82:443323e7d4c9 188
fdalforno 82:443323e7d4c9 189 /**
fdalforno 82:443323e7d4c9 190 * Build data advertised by the BLE interface.
fdalforno 82:443323e7d4c9 191 */
fdalforno 82:443323e7d4c9 192 static GapAdvertisingData make_advertising_data(void)
fdalforno 82:443323e7d4c9 193 {
fdalforno 82:443323e7d4c9 194 static const uint8_t device_name[] = "GattClient";
fdalforno 82:443323e7d4c9 195 GapAdvertisingData advertising_data;
fdalforno 82:443323e7d4c9 196
fdalforno 82:443323e7d4c9 197 // add advertising flags
fdalforno 82:443323e7d4c9 198 advertising_data.addFlags(
fdalforno 82:443323e7d4c9 199 GapAdvertisingData::LE_GENERAL_DISCOVERABLE |
fdalforno 82:443323e7d4c9 200 GapAdvertisingData::BREDR_NOT_SUPPORTED
fdalforno 82:443323e7d4c9 201 );
fdalforno 82:443323e7d4c9 202
fdalforno 82:443323e7d4c9 203 // add device name
fdalforno 82:443323e7d4c9 204 advertising_data.addData(
fdalforno 82:443323e7d4c9 205 GapAdvertisingData::COMPLETE_LOCAL_NAME,
fdalforno 82:443323e7d4c9 206 device_name,
fdalforno 82:443323e7d4c9 207 sizeof(device_name)
fdalforno 82:443323e7d4c9 208 );
fdalforno 82:443323e7d4c9 209
fdalforno 82:443323e7d4c9 210 return advertising_data;
fdalforno 82:443323e7d4c9 211 }
fdalforno 82:443323e7d4c9 212
fdalforno 82:443323e7d4c9 213 /**
fdalforno 82:443323e7d4c9 214 * Build advertising parameters used by the BLE interface.
fdalforno 82:443323e7d4c9 215 */
fdalforno 82:443323e7d4c9 216 static GapAdvertisingParams make_advertising_params(void)
fdalforno 82:443323e7d4c9 217 {
fdalforno 82:443323e7d4c9 218 return GapAdvertisingParams(
fdalforno 82:443323e7d4c9 219 /* type */ GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED,
fdalforno 82:443323e7d4c9 220 /* interval */ GapAdvertisingParams::MSEC_TO_ADVERTISEMENT_DURATION_UNITS(500),
fdalforno 82:443323e7d4c9 221 /* timeout */ 0
fdalforno 82:443323e7d4c9 222 );
fdalforno 82:443323e7d4c9 223 }
fdalforno 82:443323e7d4c9 224
fdalforno 82:443323e7d4c9 225 events::EventQueue &_event_queue;
fdalforno 82:443323e7d4c9 226 BLE &_ble_interface;
fdalforno 82:443323e7d4c9 227 mbed::Callback<void(BLE&, events::EventQueue&)> _post_init_cb;
fdalforno 82:443323e7d4c9 228 };
fdalforno 82:443323e7d4c9 229
fdalforno 82:443323e7d4c9 230
fdalforno 82:443323e7d4c9 231 #endif /* GATT_EXAMPLE_BLE_PROCESS_H_ */