-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathble_controller.cpp
More file actions
413 lines (356 loc) · 11 KB
/
ble_controller.cpp
File metadata and controls
413 lines (356 loc) · 11 KB
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*!
* Copyright (c) 2021 Bosch Sensortec GmbH. All rights reserved.
*
* BSD-3-Clause
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @file ble_controller.cpp
* @date 03 Jan 2024
* @version 2.1.5
*
* @brief ble_controller
*
*
*/
/* own header include */
#include "ble_controller.h"
bleController::ble_cmd bleController::cmd_list[] = {
{"setlabel", &bleController::parse_cmd_set_label, bleController::SET_LABEL},
{"setlabelinfo", &bleController::parse_cmd_set_label_info, bleController::SET_LABEL_INFO},
{"getlabelinfo", &bleController::parse_cmd_get_label_info, bleController::GET_LABEL_INFO},
{"setrtctime", &bleController::parse_cmd_set_rtc_time, bleController::SET_RTC_TIME},
{"getrtctime", &bleController::parse_cmd_get_rtc_time, bleController::GET_RTC_TIME},
{"start", &bleController::parse_cmd_start_streaming, bleController::START_STREAMING},
{"stop", &bleController::parse_cmd_stop_streaming, bleController::STOP_STREAMING},
{"readconfig", &bleController::parse_cmd_read_config, bleController::READ_CONFIG},
{"setappmode", &bleController::parse_cmd_set_appmode, bleController::SET_APPMODE},
{"getappmode", &bleController::parse_cmd_get_appmode, bleController::GET_APPMODE},
{"setgroundtruth", &bleController::parse_cmd_set_groundtruth, bleController::SET_GROUNDTRUTH},
{"getfwversion", &bleController::parse_cmd_get_fw_version, bleController::GET_FW_VERSION},
};
QueueHandle_t bleController::msg_queue = nullptr;
BLECharacteristic *bleController::ble_char_tx = nullptr, *bleController::ble_char_rx = nullptr;
BLEServer *bleController::pServer = nullptr;
/*!
* @brief bleController class Constructor
*/
bleController::bleController(bleCallBack callBack) : _callBack(callBack)
{}
/*!
* @brief function to initialize the ble controller
*/
demo_ret_code bleController::begin()
{
demo_ret_code ret_code = EDK_OK;
msg_queue = xQueueCreate(BLE_MSG_QUEUE_LEN, sizeof(ble_msg));
/* Initialize BLE with Device name */
BLEDevice::init("BME688 Development Kit");
/* Create Server */
pServer = BLEDevice::createServer();
pServer->setCallbacks(new serverCallbacks());
/* Create UART Service */
BLEService *pService = pServer->createService(SERVICE_UUID);
/* add characteristics for transmitting and receiving */
ble_char_tx = pService->createCharacteristic(
CHARACTERISTIC_UUID_TX,
BLECharacteristic::PROPERTY_NOTIFY
);
ble_char_tx->addDescriptor(new BLE2902());
ble_char_rx = pService->createCharacteristic(
CHARACTERISTIC_UUID_RX,
BLECharacteristic::PROPERTY_WRITE
);
/* set callback functions */
ble_char_rx->setCallbacks(this);
/* start advertising */
pService->start();
pServer->getAdvertising()->start();
return ret_code;
}
/*!
* @brief : This function fetches the RTC time which is requested through ble command
*/
bleController::cmd_status bleController::parse_cmd_get_rtc_time(std::stringstream& ss, ble_msg& msg)
{
return CMD_VALID;
}
/*!
* @brief : This function parses the RTC time received from the ble device and updates
* the RTC time to the ble structure
*/
bleController::cmd_status bleController::parse_cmd_set_rtc_time(std::stringstream& ss, ble_msg& msg)
{
uint32_t rtc;
if (ss >> rtc)
{
msg.rtc_time = rtc;
return CMD_VALID;
}
return CMD_INVALID;
}
/*!
* @brief : This function updates the received label to the ble structure
*/
bleController::cmd_status bleController::parse_cmd_set_label(std::stringstream& ss, ble_msg& msg)
{
uint32_t label;
if (ss >> label)
{
msg.label = label;
return CMD_VALID;
}
return CMD_INVALID;
}
/*!
* @brief : This function fetches the current label information
*/
bleController::cmd_status bleController::parse_cmd_get_label_info(std::stringstream& ss, ble_msg& msg)
{
return CMD_VALID;
}
/*!
* @brief : This function updates the received label information to the ble structure
*/
bleController::cmd_status bleController::parse_cmd_set_label_info(std::stringstream& ss, ble_msg& msg)
{
uint32_t label;
std::string lbl_name, lbl_desc;
if (ss >> label)
{
msg.label_info.label = label;
/* read label name until comma */
if (std::getline(ss, lbl_name, ','))
{
lbl_name.erase(lbl_name.begin());
if (lbl_name.length() > LABEL_NAME_SIZE)
{
return MAX_LABEL_NAME_REACHED;
}
memset(msg.label_info.label_name, 0, (LABEL_NAME_SIZE + 1));
strncpy(msg.label_info.label_name, lbl_name.c_str(), (LABEL_NAME_SIZE - 1));
/* read label description until dot */
if(std::getline(ss, lbl_desc, '.'))
{
if (lbl_desc.length() > LABEL_DESC_SIZE)
{
return MAX_LABEL_DESCRIPTION_REACHED;
}
memset(msg.label_info.label_desc, 0, (LABEL_DESC_SIZE + 1));
strncpy(msg.label_info.label_desc, lbl_desc.c_str(), (LABEL_DESC_SIZE - 1));
return CMD_VALID;
}
}
}
return CMD_INVALID;
}
/*!
* @brief : This function launches sensor data or sensor data and BSEC output streaming through ble
* based on the app mode
*/
bleController::cmd_status bleController::parse_cmd_start_streaming(std::stringstream& ss, ble_msg& msg)
{
int32_t sensor_num, sample_rate, output_id;
if (ss >> sensor_num)
{
msg.bsec.selected_sensor = static_cast<uint8_t>(sensor_num);
if (ss >> sample_rate)
{
msg.bsec.sample_rate = static_cast<uint8_t>(sample_rate);
msg.bsec.len = 0;
while ((msg.bsec.len < BSEC_NUMBER_OUTPUTS) && (ss >> output_id))
{
msg.bsec.output_id[msg.bsec.len++] = static_cast<uint8_t>(output_id);
}
if (ss >> output_id)
{
return BSEC_OUTPUT_EXCESS_ERROR;
}
return CMD_VALID;
}
}
return CMD_INVALID;
}
/*!
* @brief : This function stops ble streaming
*/
bleController::cmd_status bleController::parse_cmd_stop_streaming(std::stringstream& ss, ble_msg& msg)
{
return CMD_VALID;
}
/*!
* @brief : This function launches the config file data through ble
*/
bleController::cmd_status bleController::parse_cmd_read_config(std::stringstream& ss, ble_msg& msg)
{
int32_t file_type;
if (ss >> file_type)
{
msg.file_type = static_cast<config_file>(file_type);
return CMD_VALID;
}
return CMD_INVALID;
}
/*!
* @brief : This function updates the current Appmode
*/
bleController::cmd_status bleController::parse_cmd_set_appmode(std::stringstream& ss, ble_msg& msg)
{
int32_t mode;
if (ss >> mode)
{
msg.mode = static_cast<uint8_t>(mode);
return CMD_VALID;
}
return CMD_INVALID;
}
/*!
* @brief : This function retrieves the current Appmode through ble
*/
bleController::cmd_status bleController::parse_cmd_get_appmode(std::stringstream& ss, ble_msg& msg)
{
return CMD_VALID;
}
/*!
* @brief : This function updates the Groundtruth
*/
bleController::cmd_status bleController::parse_cmd_set_groundtruth(std::stringstream& ss, ble_msg& msg)
{
int32_t ground_truth;
if (ss >> ground_truth)
{
msg.ground_truth = static_cast<uint32_t>(ground_truth);
return CMD_VALID;
}
return CMD_INVALID;
}
/*!
* @brief : This function retrieves the current firmware version through ble
*/
bleController::cmd_status bleController::parse_cmd_get_fw_version(std::stringstream& ss, ble_msg& msg)
{
return CMD_VALID;
}
/*!
* @brief function gets called when data is received from a bluetooth device.
* It will read in the sent bluetooth command
*/
void bleController::onWrite(BLECharacteristic *pCharacteristic)
{
// ORIGINALE std::string rx_value = pCharacteristic->getValue(), cmd_name;
// Converti in std::string se necessario
std::string rx_value = std::string(pCharacteristic->getValue().c_str());
std::string cmd_name;
std::stringstream ss(rx_value);
cmd_status status = CMD_INVALID;
ble_msg msg;
if (ss >> cmd_name)
{
StaticJsonDocument<BLE_JSON_DOC_SIZE> jsonDoc;
for (auto& cmd : cmd_list)
{
if (cmd_name == cmd.name)
{
status = cmd.parse(ss, msg);
if (status == CMD_VALID)
{
msg.name = cmd.name;
msg.id = cmd.id;
if (xQueueSendFromISR(msg_queue, (const void*)&msg, 0) == pdPASS)
{
return;
}
else
{
status = CONTROLLER_QUEUE_FULL;
}
}
break;
}
}
jsonDoc[cmd_name.c_str()] = status;
send_notification(jsonDoc);
}
}
/*!
* @brief : This function checks the ble connection status, restarts advertising if disconnected
*/
void bleController::check_ble_connection_sts()
{
/* disconnecting */
if (!device_connected && old_device_connected)
{
pServer->startAdvertising(); /* restart advertising, when ble is disconnected */
old_device_connected = device_connected;
}
/* connecting */
if (device_connected && !old_device_connected)
{
old_device_connected = device_connected;
}
}
/**
* @brief function dequeues the last received ble message. The ble callBack is
* called if a new message is available.
*/
bool bleController::dequeue_ble_msg(void)
{
ble_msg msg;
if (xQueueReceive(msg_queue, &msg, 0) == pdPASS)
{
StaticJsonDocument<BLE_JSON_DOC_SIZE> jsonDoc;
if (_callBack != nullptr)
{
_callBack(msg, jsonDoc);
send_notification(jsonDoc);
}
return true;
}
return false;
}
/*!
* @brief function to send a json formatted notification
*/
void bleController::send_notification(JsonDocument& jsonDoc)
{
String notif, msg;
serializeJson(jsonDoc, notif);
size_t not_len = notif.length(), begin_msg = 0, end_msg = BLE_CONTROLLER_NOTIF_SIZE;
while (begin_msg < not_len)
{
if (end_msg > not_len)
{
end_msg = not_len;
}
msg = notif.substring(begin_msg, end_msg);
begin_msg += BLE_CONTROLLER_NOTIF_SIZE;
end_msg += BLE_CONTROLLER_NOTIF_SIZE;
ble_char_tx->setValue((const char*)msg.c_str());
ble_char_tx->notify();
}
}