-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathble_process.h
267 lines (222 loc) · 7.44 KB
/
ble_process.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/* mbed Microcontroller Library
* Copyright (c) 2006-2019 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.
*/
#ifndef BLE_PROCESS_H_
#define BLE_PROCESS_H_
#include <stdint.h>
#include "pretty_printer.h"
#include <events/mbed_events.h>
#include "platform/Callback.h"
#include "platform/NonCopyable.h"
#include "ble/BLE.h"
#include "Gap.h"
#include "gap/AdvertisingDataParser.h"
#include "ble/common/FunctionPointerWithContext.h"
static const uint16_t MAX_ADVERTISING_PAYLOAD_SIZE = 50;
/**
* Handle initialization and shutdown of the BLE Instance.
* It will also run the event queue and call your post init callback when everything is up and running.
*/
class BLEProcess : private mbed::NonCopyable<BLEProcess>, public ble::Gap::EventHandler
{
public:
/**
* Construct a BLEProcess from an event queue and a ble interface.
* Call start() to initiate ble processing.
*/
BLEProcess(events::EventQueue &event_queue, BLE &ble_interface) :
_event_queue(event_queue),
_ble(ble_interface),
_gap(ble_interface.gap()),
_adv_data_builder(_adv_buffer)
{
}
~BLEProcess()
{
stop();
}
/**
* Initialize the ble interface, configure it and start advertising.
*/
void start()
{
printf("Ble process started.\r\n");
if (_ble.hasInitialized()) {
printf("Error: the ble instance has already been initialized.\r\n");
return;
}
/* handle gap events */
_gap.setEventHandler(this);
/* This will inform us off all events so we can schedule their handling
* using our event queue */
_ble.onEventsToProcess(
makeFunctionPointer(this, &BLEProcess::schedule_ble_events)
);
ble_error_t error = _ble.init(
this, &BLEProcess::on_init_complete
);
if (error) {
print_error(error, "Error returned by BLE::init.\r\n");
return;
}
// Process the event queue.
_event_queue.dispatch_forever();
return;
}
/**
* Close existing connections and stop the process.
*/
void stop()
{
if (_ble.hasInitialized()) {
_ble.shutdown();
printf("Ble process stopped.");
}
}
/**
* Subscription to the ble interface initialization event.
*
* @param[in] cb The callback object that will be called when the ble interface is initialized.
*/
void on_init(mbed::Callback<void(BLE&, events::EventQueue&)> cb)
{
_post_init_cb = cb;
}
/**
* Set callback for a succesful connection.
*
* @param[in] cb The callback object that will be called when we connect to a peer
*/
void on_connect(mbed::Callback<void(BLE&, events::EventQueue&, const ble::ConnectionCompleteEvent &event)> cb)
{
_post_connect_cb = cb;
}
/** Name we advertise as. */
virtual const char* get_device_name()
{
static const char name[] = "BleProcess";
return name;
}
protected:
/**
* Sets up adverting payload and start advertising.
* This function is invoked when the ble interface is initialized.
*/
void on_init_complete(BLE::InitializationCompleteCallbackContext *event)
{
if (event->error) {
print_error(event->error, "Error during the initialisation\r\n");
return;
}
printf("Ble instance initialized\r\n");
/* All calls are serialised on the user thread through the event queue */
start_activity();
if (_post_init_cb) {
_post_init_cb(_ble, _event_queue);
}
}
/**
* Start the gatt client process when a connection event is received.
* This is called by Gap to notify the application we connected
*/
void onConnectionComplete(const ble::ConnectionCompleteEvent &event) override
{
if (event.getStatus() == BLE_ERROR_NONE) {
printf("Connected to: ");
print_address(event.getPeerAddress());
if (_post_connect_cb) {
_post_connect_cb(_ble, _event_queue, event);
}
} else {
printf("Failed to connect\r\n");
start_activity();
}
}
/**
* Stop the gatt client process when the device is disconnected then restart advertising.
* This is called by Gap to notify the application we disconnected
*/
void onDisconnectionComplete(const ble::DisconnectionCompleteEvent &event) override
{
printf("Disconnected.\r\n");
start_activity();
}
/** Restarts main activity */
void onAdvertisingEnd(const ble::AdvertisingEndEvent &event)
{
start_activity();
}
/**
* Start advertising or scanning. Triggered by init or disconnection.
*/
virtual void start_activity()
{
_event_queue.call([this]() { start_advertising(); });
}
/**
* Start the advertising process; it ends when a device connects.
*/
void start_advertising()
{
ble_error_t error;
if (_gap.isAdvertisingActive(_adv_handle)) {
/* we're already advertising */
return;
}
ble::AdvertisingParameters adv_params(
ble::advertising_type_t::CONNECTABLE_UNDIRECTED,
ble::adv_interval_t(ble::millisecond_t(40))
);
error = _gap.setAdvertisingParameters(_adv_handle, adv_params);
if (error) {
printf("_ble.gap().setAdvertisingParameters() failed\r\n");
return;
}
_adv_data_builder.clear();
_adv_data_builder.setFlags();
_adv_data_builder.setName(get_device_name());
/* Set payload for the set */
error = _gap.setAdvertisingPayload(
_adv_handle, _adv_data_builder.getAdvertisingData()
);
if (error) {
print_error(error, "Gap::setAdvertisingPayload() failed\r\n");
return;
}
error = _gap.startAdvertising(_adv_handle, ble::adv_duration_t(ble::millisecond_t(4000)));
if (error) {
print_error(error, "Gap::startAdvertising() failed\r\n");
return;
}
printf("Advertising as \"%s\"\r\n", get_device_name());
}
/**
* Schedule processing of events from the BLE middleware in the event queue.
*/
void schedule_ble_events(BLE::OnEventsToProcessCallbackContext *event)
{
_event_queue.call(mbed::callback(&event->ble, &BLE::processEvents));
}
protected:
events::EventQueue &_event_queue;
BLE &_ble;
ble::Gap &_gap;
uint8_t _adv_buffer[MAX_ADVERTISING_PAYLOAD_SIZE];
ble::AdvertisingDataBuilder _adv_data_builder;
ble::advertising_handle_t _adv_handle = ble::LEGACY_ADVERTISING_HANDLE;
mbed::Callback<void(BLE&, events::EventQueue&)> _post_init_cb;
mbed::Callback<void(BLE&, events::EventQueue&, const ble::ConnectionCompleteEvent &event)> _post_connect_cb;
};
#endif /* BLE_PROCESS_H_ */