-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBLEAgent.h
326 lines (278 loc) · 10.3 KB
/
BLEAgent.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
Copyright (c) 2024 Arduino SA
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#pragma once
#include <list>
#include "Arduino.h"
#include <Arduino_DebugUtils.h>
#include <algorithm>
#include <ArduinoBLE.h>
#include "utility/HCI.h"
#include "BLEStringCharacteristic.h"
#include "BLECharacteristic.h"
#include "configuratorAgents/agents/ConfiguratorAgent.h"
#include "configuratorAgents/agents/BoardConfigurationProtocol/BoardConfigurationProtocol.h"
#include "utility/LEDFeedback.h"
#include "configuratorAgents/agents/BoardConfigurationProtocol/cbor/CBORInstances.h"
#define BASE_LOCAL_NAME "Arduino"
#define ARDUINO_COMPANY_ID 0x09A3
#if defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT)
#define VID USB_VID
#define PID USB_PID
#elif defined(ARDUINO_NANO_RP2040_CONNECT) || defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_NICLA_VISION) || defined(ARDUINO_GIGA)
#include "pins_arduino.h"
#define VID BOARD_VENDORID
#define PID BOARD_PRODUCTID
#elif defined(ARDUINO_OPTA)
#include "pins_arduino.h"
#define VID _BOARD_VENDORID
#define PID _BOARD_PRODUCTID
#elif defined(ARDUINO_PORTENTA_C33)
#include "pins_arduino.h"
#define VID USB_VID
#define PID USB_PID
#elif defined(ARDUINO_UNOR4_WIFI)
#define VID 0x2341
#define PID 0x1002
#else
#error "Board not supported for BLE configuration"
#endif
/**
* @class BLEAgentClass
* @brief This class is responsible for managing BLE communication with a peer device/client for board configuration purposes.
* It extends the ConfiguratorAgent class and implements the BoardConfigurationProtocol interface to handle
* communication, message exchange, and connection management over a Bluetooth Low Energy interface.
*/
class BLEAgentClass : public ConfiguratorAgent, private BoardConfigurationProtocol {
public:
BLEAgentClass();
AgentConfiguratorStates begin();
AgentConfiguratorStates end();
AgentConfiguratorStates update();
void disconnectPeer();
bool receivedMsgAvailable();
bool getReceivedMsg(ProvisioningInputMessage &msg);
bool sendMsg(ProvisioningOutputMessage &msg);
bool isPeerConnected();
inline AgentTypes getAgentType() {
return AgentTypes::BLE;
};
private:
enum class BLEEvent { NONE,
DISCONNECTED,
SUBSCRIBED };
static inline BLEEvent _bleEvent = BLEEvent::NONE;
AgentConfiguratorStates _state = AgentConfiguratorStates::END;
BLEService _confService; // BLE Configuration Service
BLECharacteristic _inputStreamCharacteristic;
BLECharacteristic _outputStreamCharacteristic;
String _localName;
uint8_t _manufacturerData[8];
size_t _readByte = 0;
/*BLEAgent private methods*/
AgentConfiguratorStates handlePeerConnected();
bool setLocalName();
bool setManufacturerData();
/*ArduinoBLE events callback functions*/
static void blePeripheralDisconnectHandler(BLEDevice central);
static void bleOutputStreamSubscribed(BLEDevice central, BLECharacteristic characteristic);
/*BoardConfigurationProtocol pure virtual methods implementation*/
bool received();
size_t available();
uint8_t read();
int write(const uint8_t *data, size_t len);
void handleDisconnectRequest();
void clearInputBuffer();
};
inline BLEAgentClass::BLEAgentClass()
: _confService{ "5e5be887-c816-4d4f-b431-9eb34b02f4d9" },
_inputStreamCharacteristic{ "0000ffe1-0000-1000-8000-00805f9b34fc", BLEWrite, 256 },
_outputStreamCharacteristic{ "0000ffe1-0000-1000-8000-00805f9b34fa", BLEIndicate, 64 } {
}
inline ConfiguratorAgent::AgentConfiguratorStates BLEAgentClass::begin() {
if (_state != AgentConfiguratorStates::END) {
return _state;
}
if (!BLE.begin()) {
DEBUG_ERROR("BLEAgentClass::%s Starting Bluetooth® Low Energy module failed!", __FUNCTION__);
return AgentConfiguratorStates::ERROR;
}
if (!setLocalName()) {
DEBUG_WARNING("BLEAgentClass::%s fail to set local name", __FUNCTION__);
}
// set manufacturer data
if (!setManufacturerData()) {
DEBUG_WARNING("BLEAgentClass::%s fail to set manufacturer data", __FUNCTION__);
}
BLE.setAdvertisedService(_confService);
BLE.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler);
_outputStreamCharacteristic.setEventHandler(BLESubscribed, bleOutputStreamSubscribed);
// add the characteristic to the service
_confService.addCharacteristic(_outputStreamCharacteristic);
_confService.addCharacteristic(_inputStreamCharacteristic);
// add service
BLE.addService(_confService);
// start advertising
BLE.advertise();
LEDFeedbackClass::getInstance().setMode(LEDFeedbackClass::LEDFeedbackMode::BLE_AVAILABLE);
_state = AgentConfiguratorStates::INIT;
return _state;
}
inline ConfiguratorAgent::AgentConfiguratorStates BLEAgentClass::end() {
if (_state != AgentConfiguratorStates::END) {
if (_state != AgentConfiguratorStates::INIT) {
disconnectPeer();
}
BLE.stopAdvertise();
BLE.end();
clear();
LEDFeedbackClass::getInstance().setMode(LEDFeedbackClass::LEDFeedbackMode::NONE);
_state = AgentConfiguratorStates::END;
}
return _state;
}
inline ConfiguratorAgent::AgentConfiguratorStates BLEAgentClass::update() {
if (_state == AgentConfiguratorStates::END) {
return _state;
}
BLE.poll();
switch (_bleEvent) {
case BLEEvent::SUBSCRIBED:
if (_state != AgentConfiguratorStates::PEER_CONNECTED) {
_state = AgentConfiguratorStates::PEER_CONNECTED;
LEDFeedbackClass::getInstance().setMode(LEDFeedbackClass::LEDFeedbackMode::PEER_CONNECTED);
}
break;
case BLEEvent::DISCONNECTED:
_inputStreamCharacteristic.writeValue("");
clear();
LEDFeedbackClass::getInstance().setMode(LEDFeedbackClass::LEDFeedbackMode::BLE_AVAILABLE);
_state = AgentConfiguratorStates::INIT;
break;
default:
break;
}
_bleEvent = BLEEvent::NONE;
switch (_state) {
case AgentConfiguratorStates::INIT: break;
case AgentConfiguratorStates::PEER_CONNECTED: _state = handlePeerConnected(); break;
case AgentConfiguratorStates::RECEIVED_DATA: break;
case AgentConfiguratorStates::ERROR: break;
case AgentConfiguratorStates::END: break;
}
checkOutputPacketValidity();
return _state;
}
inline bool BLEAgentClass::getReceivedMsg(ProvisioningInputMessage &msg) {
bool res = BoardConfigurationProtocol::getMsg(msg);
if (receivedMsgAvailable() == false) {
_state = AgentConfiguratorStates::PEER_CONNECTED;
}
return res;
}
inline bool BLEAgentClass::receivedMsgAvailable() {
return BoardConfigurationProtocol::msgAvailable();
}
inline bool BLEAgentClass::sendMsg(ProvisioningOutputMessage &msg) {
return BoardConfigurationProtocol::sendMsg(msg);
}
inline bool BLEAgentClass::isPeerConnected() {
return _outputStreamCharacteristic.subscribed() && (_state == AgentConfiguratorStates::PEER_CONNECTED || _state == AgentConfiguratorStates::RECEIVED_DATA);
}
inline void BLEAgentClass::blePeripheralDisconnectHandler(BLEDevice central) {
// central disconnected event handler
_bleEvent = BLEEvent::DISCONNECTED;
DEBUG_INFO("BLEAgentClass Disconnected event, central: %s", central.address().c_str());
}
inline void BLEAgentClass::bleOutputStreamSubscribed(BLEDevice central, BLECharacteristic characteristic) {
_bleEvent = BLEEvent::SUBSCRIBED;
DEBUG_INFO("BLEAgentClass Connected event, central: %s", central.address().c_str());
}
inline bool BLEAgentClass::received() {
bool res = _inputStreamCharacteristic.written();
if (res) {
_readByte = 0;
}
return res;
}
inline size_t BLEAgentClass::available() {
return _inputStreamCharacteristic.valueLength();
}
inline uint8_t BLEAgentClass::read() {
const uint8_t *charValue = _inputStreamCharacteristic.value();
if (_readByte < _inputStreamCharacteristic.valueLength()) {
return charValue[_readByte++];
}
return 0;
}
inline int BLEAgentClass::write(const uint8_t *data, size_t len) {
return _outputStreamCharacteristic.writeValue(data, len);
}
inline void BLEAgentClass::handleDisconnectRequest() {
}
inline ConfiguratorAgent::AgentConfiguratorStates BLEAgentClass::handlePeerConnected() {
AgentConfiguratorStates nextState = _state;
TransmissionResult res = sendAndReceive();
switch (res) {
case TransmissionResult::INVALID_DATA:
// clear the input buffer
_inputStreamCharacteristic.writeValue("");
break;
case TransmissionResult::PEER_NOT_AVAILABLE:
disconnectPeer();
nextState = AgentConfiguratorStates::INIT;
break;
case TransmissionResult::DATA_RECEIVED:
nextState = AgentConfiguratorStates::RECEIVED_DATA;
break;
default:
break;
}
return nextState;
}
//The local name is sent after a BLE scan Request
inline bool BLEAgentClass::setLocalName() {
_Static_assert(sizeof(BASE_LOCAL_NAME) < 19, "Error BLE device Local Name too long. Reduce BASE_LOCAL_NAME length"); //Check at compile time if the local name length is valid
char vid[5];
char pid[5];
sprintf(vid, "%04x", VID);
sprintf(pid, "%04x", PID);
_localName = BASE_LOCAL_NAME;
_localName.concat("-");
_localName.concat(vid);
_localName.concat("-");
_localName.concat(pid);
BLE.setDeviceName(_localName.c_str());
return BLE.setLocalName(_localName.c_str());
}
//The manufacturer data is sent with the service uuid as advertised data
inline bool BLEAgentClass::setManufacturerData() {
uint8_t addr[6] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
HCI.readBdAddr(addr);
uint16_t companyID = ARDUINO_COMPANY_ID;
_manufacturerData[0] = (uint8_t)companyID & 0xFF;
_manufacturerData[1] = (uint8_t)(companyID >> 8);
for (int i = 2, j = 0; j < 6; j++, i++) {
_manufacturerData[i] = addr[5 - j];
}
return BLE.setManufacturerData(_manufacturerData, sizeof(_manufacturerData));
}
inline void BLEAgentClass::disconnectPeer() {
_inputStreamCharacteristic.writeValue("");
uint32_t start = millis();
BLE.disconnect();
do {
BLE.poll();
} while (millis() - start < 200);
clear();
LEDFeedbackClass::getInstance().setMode(LEDFeedbackClass::LEDFeedbackMode::BLE_AVAILABLE);
_state = AgentConfiguratorStates::INIT;
return;
}
inline void BLEAgentClass::clearInputBuffer() {
// clear the input buffer
_inputStreamCharacteristic.writeValue("");
}