forked from EdgeTX/edgetx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulti_protolist.cpp
395 lines (331 loc) · 10.8 KB
/
multi_protolist.cpp
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
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "multi_protolist.h"
#include "multi_rfprotos.h"
#include "strhelpers.h"
#include "audio.h"
#include "translations.h"
#include "pulses/modules_helpers.h"
#include "pulses/pulses.h"
#include "gui_common.h"
#include "opentx.h" // reusableBuffer
#include <algorithm>
#if !defined(SIMU)
#include <FreeRTOS/include/FreeRTOS.h>
#include <FreeRTOS/include/timers.h>
struct ProtoScanTimer {
TimerHandle_t timer = nullptr;
StaticTimer_t timerBuffer;
};
static ProtoScanTimer _protoScanTimers[NUM_MODULES];
void MultiRfProtocols::timerCb(TimerHandle_t xTimer)
{
uint8_t moduleIdx = (uint8_t)(uintptr_t)pvTimerGetTimerID(xTimer);
auto instance = MultiRfProtocols::instance(moduleIdx);
if (instance->scanState == ScanBegin || instance->scanState == Scanning) {
instance->fillBuiltinProtos();
}
}
#endif
MultiRfProtocols* MultiRfProtocols::_instance[MAX_MODULES] = {};
// MPM telemetry packet format type = MultiProtoDef (0x11)
//
// data[0] = protocol number, 0xFF is an invalid list entry
// (Option value too large) and nothing sent after
// data[1..n] = protocol name null terminated
// data[n+1] = flags
// flags>>4 Option text number to be displayed
// (check multi status for description)
// flags&0x01 failsafe supported
// flags&0x02 Channel Map Disabled supported
// data[n+2] = number of sub protocols
// data[n+3] = sub protocols text length, only sent if nbr_sub != 0
// data[n+4..] = sub protocol names, only sent if nbr_sub != 0
constexpr uint8_t MULTI_INVALID_PROTO = 0xFF;
constexpr uint32_t MULTI_PROTOLIST_TIMEOUT = 100; // 100ms
constexpr uint32_t MULTI_PROTOLIST_START_TIMEOUT = 3000; // 3s
MultiRfProtocols::MultiRfProtocols(unsigned int moduleIdx) : moduleIdx(moduleIdx)
{
#if defined(SIMU)
// prevent scanning from starting on simu
fillBuiltinProtos();
#endif
}
bool MultiRfProtocols::RfProto::parse(const uint8_t* data, uint8_t len)
{
const char* s = (const char*)data;
uint8_t subProtoNr = 0;
uint8_t subProtoLen = 0;
// proto label string
while (*s && len--) s++;
if (*s || !len) return false;
label = (const char*)data;
s++;
len--;
// flags, subProtoNr, subProto label max length
if (len < 2) return false;
flags = (uint8_t) * (s++);
//TRACE("flags: 0x%02X", flags);
subProtoNr = (uint8_t) * (s++);
len -= 2;
if (!len) return true;
subProtoLen = (uint8_t) * (s++);
len--;
if (len < subProtoNr * subProtoLen) return false;
fillSubProtoList(s, subProtoNr, subProtoLen);
return true;
}
void MultiRfProtocols::RfProto::fillSubProtoList(const char* str, int n, int len)
{
char tmp[len + 1];
subProtos.reserve(n);
for (int i = 0; i < n; i++, str += len) {
strncpy(tmp, str, len);
tmp[len] = '\0';
subProtos.emplace_back((const char*)tmp);
//TRACE("%s: '%s'", label.c_str(), subProtos.back().c_str());
}
}
void MultiRfProtocols::RfProto::fillSubProtoList(const char *const *str, int n)
{
subProtos.reserve(n);
for (int i = 0; i < n; i++) {
subProtos.emplace_back(str[i]);
}
}
MultiRfProtocols* MultiRfProtocols::instance(unsigned int moduleIdx)
{
if (moduleIdx >= NUM_MODULES) return nullptr;
if (!_instance[moduleIdx]) {
_instance[moduleIdx] = new MultiRfProtocols(moduleIdx);
}
return _instance[moduleIdx];
}
void MultiRfProtocols::removeInstance(unsigned int moduleIdx)
{
delete _instance[moduleIdx];
_instance[moduleIdx] = nullptr;
}
int MultiRfProtocols::getIndex(unsigned int proto) const
{
auto it = proto2idx.find(proto);
return it != proto2idx.end() ? it->second : -1;
}
const MultiRfProtocols::RfProto* MultiRfProtocols::getProto(unsigned int proto) const
{
int idx = getIndex(proto);
if (idx >= 0 && (unsigned)idx < protoList.size())
return &protoList[idx];
return nullptr;
}
std::string MultiRfProtocols::getProtoLabel(unsigned int proto) const
{
if (scanState != ScanEnd) {
MultiModuleStatus& status = getMultiModuleStatus(moduleIdx);
if (status.protocolName[0] && status.isValid()) {
return std::string(status.protocolName);
} else if (proto <= MODULE_SUBTYPE_MULTI_LAST) {
char tmp[8];
getStringAtIndex(tmp, STR_MULTI_PROTOCOLS, proto);
return std::string(tmp);
}
} else {
int idx = getIndex(proto);
if (idx >= 0 && (unsigned)idx < protoList.size()) {
return protoList[idx].label;
}
}
return std::string();
}
std::string MultiRfProtocols::getLastProtoLabel() const
{
if (!protoList.empty())
return protoList.back().label;
return std::string();
}
float MultiRfProtocols::getProgress() const
{
constexpr float WAIT_TIME_RATIO = 0.7; // wait time accounts for 70%
constexpr float PROTOS_RATIO = 1.0 - WAIT_TIME_RATIO;
if (scanState == ScanStop) return 0.0;
if (scanState == ScanBegin) {
float t = (float)(RTOS_GET_MS() - scanStart) / (float)MULTI_PROTOLIST_START_TIMEOUT;
return t * WAIT_TIME_RATIO;
}
return WAIT_TIME_RATIO + ((float)getNProtos() / (float)totalProtos) * PROTOS_RATIO;
}
void MultiRfProtocols::fillList(std::function<void(const RfProto&)> addProto) const
{
for (const auto& p : protoList) {
addProto(p);
}
}
bool MultiRfProtocols::triggerScan()
{
if (scanState == ScanStop
&& moduleState[moduleIdx].mode == MODULE_MODE_NORMAL) {
proto2idx.clear();
protoList.clear();
scanState = ScanBegin;
currentProto = MULTI_INVALID_PROTO;
moduleState[moduleIdx].mode = MODULE_MODE_GET_HARDWARE_INFO;
scanStart = lastScan = RTOS_GET_MS();
#if !defined(SIMU)
auto scanTimer = &_protoScanTimers[moduleIdx];
if (!scanTimer->timer) {
scanTimer->timer = xTimerCreateStatic(
"MPM", MULTI_PROTOLIST_START_TIMEOUT / RTOS_MS_PER_TICK, pdTRUE,
(void*)moduleIdx, MultiRfProtocols::timerCb, &scanTimer->timerBuffer);
} else {
if (xTimerChangePeriod(scanTimer->timer,
MULTI_PROTOLIST_START_TIMEOUT / RTOS_MS_PER_TICK,
0) != pdPASS) {
/* The timer period could not be reset. */
}
}
if (scanTimer->timer) {
if( xTimerStart( scanTimer->timer, 0 ) != pdPASS ) {
/* The timer could not be set into the Active state. */
}
}
#endif
return true;
}
return false;
}
bool MultiRfProtocols::scanReply(const uint8_t* packet, uint8_t len)
{
switch (scanState) {
case ScanBegin:
case Scanning:
if (packet && len) {
uint8_t replyProtoId = *packet;
len--;
packet++;
// new status received
if (replyProtoId != MULTI_INVALID_PROTO) {
if (currentProto == MULTI_INVALID_PROTO) {
//TRACE("# of protos: %d", totalProtos);
totalProtos = replyProtoId;
scanState = Scanning;
protoList.reserve(totalProtos);
} else {
//TRACE("Proto = %d; label = '%s'", replyProtoId,
// (const char*)packet);
int proto = replyProtoId - 1;
if (isMultiProtocolSelectable(proto)) {
RfProto rfProto(proto);
if (rfProto.parse(packet, len)) {
proto2idx[proto] = protoList.size();
protoList.emplace_back(rfProto);
} else {
TRACE("Error parsing proto [%d]", proto);
}
} else {
// do not count excluded protocols
totalProtos--;
}
}
currentProto++;
lastScan = RTOS_GET_MS();
#if !defined(SIMU)
auto scanTimer = &_protoScanTimers[moduleIdx];
if (scanTimer->timer) {
if (xTimerChangePeriod(scanTimer->timer,
MULTI_PROTOLIST_TIMEOUT / RTOS_MS_PER_TICK,
0) != pdPASS) {
/* The timer period could not be reset. */
}
}
#endif
return true;
} else {
scanState = ScanEnd;
setModuleMode(moduleIdx, MODULE_MODE_NORMAL);
#if !defined(SIMU)
auto scanTimer = &_protoScanTimers[moduleIdx];
if (scanTimer->timer) {
if (xTimerStop(scanTimer->timer, 0) != pdPASS) {
/* The timer period could not be stopped. */
}
}
#endif
break;
}
} else {
uint32_t timeout = MULTI_PROTOLIST_TIMEOUT;
if (scanState == ScanBegin) {
// Timeout = 3s solely because of the very slow start time of the
// MPM...
timeout = MULTI_PROTOLIST_START_TIMEOUT;
}
if (RTOS_GET_MS() - lastScan >= timeout) {
TRACE("proto scan timeout!");
scanState = ScanInvalid;
}
}
break;
case ScanInvalid: {
fillBuiltinProtos();
} break;
default:
break;
}
return false;
}
void MultiRfProtocols::fillBuiltinProtos()
{
const mm_protocol_definition* pdef =
getMultiProtocolDefinition(MODULE_SUBTYPE_MULTI_FIRST);
// build the list of static protos
protoList.clear();
protoList.reserve(MODULE_SUBTYPE_MULTI_LAST - MODULE_SUBTYPE_MULTI_FIRST + 1);
for (; pdef->protocol != MODULE_SUBTYPE_MULTI_SENTINEL; pdef++) {
RfProto rfProto(pdef->protocol);
if (pdef->protocol == MM_RF_CUSTOM_SELECTED) break; // skip custom proto
char tmp[8];
rfProto.label = getStringAtIndex(tmp, STR_MULTI_PROTOCOLS, pdef->protocol);
rfProto.flags =
(pdef->failsafe ? 0x01 : 0) | (pdef->disable_ch_mapping ? 0x02 : 0);
if (pdef->optionsstr != nullptr) {
for (uint8_t i = 0; i < getMaxMultiOptions(); i++) {
if (pdef->optionsstr == mm_options_strings::options[i]) {
rfProto.flags |= i << 4;
break;
}
}
}
if (pdef->subTypeString) {
rfProto.fillSubProtoList(pdef->subTypeString, pdef->maxSubtype + 1);
}
protoList.emplace_back(rfProto);
}
// sort it by label
std::sort(
protoList.begin(), protoList.end(),
[](const RfProto& a, const RfProto& b) { return a.label < b.label; });
// index the sorted list
proto2idx.clear();
for (unsigned int i = 0; i < protoList.size(); i++)
proto2idx[protoList[i].proto] = i;
scanState = ScanEnd;
setModuleMode(moduleIdx, MODULE_MODE_NORMAL);
}