-
Notifications
You must be signed in to change notification settings - Fork 141
/
DMRSlotRX.cpp
502 lines (440 loc) · 13.4 KB
/
DMRSlotRX.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/*
* Copyright (C) 2009-2017 by Jonathan Naylor G4KLX
* Copyright (C) 2017,2018 by Andy Uribe CA6JAU
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "Config.h"
#if defined(DUPLEX)
#include "Globals.h"
#include "DMRSlotRX.h"
#include "DMRSlotType.h"
#include "Utils.h"
const uint8_t MAX_SYNC_BYTES_ERRS = 3U;
const uint8_t MAX_SYNC_LOST_FRAMES = 13U;
const uint16_t NOENDPTR = 9999U;
const uint8_t CONTROL_NONE = 0x00U;
const uint8_t CONTROL_VOICE = 0x20U;
const uint8_t CONTROL_DATA = 0x40U;
const uint8_t BIT_MASK_TABLE[] = {0x80U, 0x40U, 0x20U, 0x10U, 0x08U, 0x04U, 0x02U, 0x01U};
#define WRITE_BIT1(p,i,b) p[(i)>>3] = (b) ? (p[(i)>>3] | BIT_MASK_TABLE[(i)&7]) : (p[(i)>>3] & ~BIT_MASK_TABLE[(i)&7])
#define READ_BIT1(p,i) ((p[(i)>>3] & BIT_MASK_TABLE[(i)&7]) >> (7 - ((i)&7)))
CDMRSlotRX::CDMRSlotRX() :
m_slot(false),
m_patternBuffer(0x00U),
m_buffer(),
m_dataPtr(0U),
m_syncPtr1(0U),
m_startPtr1(0U),
m_endPtr1(NOENDPTR),
m_control1(CONTROL_NONE),
m_syncCount1(0U),
m_state1(DMRRXS_NONE),
m_n1(0U),
m_type1(0U),
m_syncPtr2(0U),
m_startPtr2(0U),
m_endPtr2(NOENDPTR),
m_control2(CONTROL_NONE),
m_syncCount2(0U),
m_state2(DMRRXS_NONE),
m_n2(0U),
m_type2(0U),
m_delayPtr(0U),
m_colorCode(0U),
m_delay(0U)
{
}
void CDMRSlotRX::start(bool slot)
{
m_slot = slot;
m_delayPtr = 0U;
}
void CDMRSlotRX::reset()
{
m_dataPtr = 0U;
m_delayPtr = 0U;
m_patternBuffer = 0U;
reset1();
reset2();
}
void CDMRSlotRX::reset1()
{
m_syncPtr1 = 0U;
m_control1 = CONTROL_NONE;
m_syncCount1 = 0U;
m_state1 = DMRRXS_NONE;
m_startPtr1 = 0U;
m_endPtr1 = NOENDPTR;
m_type1 = 0U;
m_n1 = 0U;
}
void CDMRSlotRX::reset2()
{
m_syncPtr2 = 0U;
m_control2 = CONTROL_NONE;
m_syncCount2 = 0U;
m_state2 = DMRRXS_NONE;
m_startPtr2 = 0U;
m_endPtr2 = NOENDPTR;
m_type2 = 0U;
m_n2 = 0U;
}
bool CDMRSlotRX::databit(bool bit)
{
uint16_t min, max;
m_delayPtr++;
if (m_delayPtr < m_delay)
return (m_state1 != DMRRXS_NONE) || (m_state2 != DMRRXS_NONE);
WRITE_BIT1(m_buffer, m_dataPtr, bit);
m_patternBuffer <<= 1;
if (bit)
m_patternBuffer |= 0x01U;
if (m_state1 == DMRRXS_NONE || m_state2 == DMRRXS_NONE) {
correlateSync();
} else {
if(m_slot) {
min = m_syncPtr2 + DMR_BUFFER_LENGTH_BITS - 2;
max = m_syncPtr2 + 2;
} else {
min = m_syncPtr1 + DMR_BUFFER_LENGTH_BITS - 2;
max = m_syncPtr1 + 2;
}
if (min >= DMR_BUFFER_LENGTH_BITS)
min -= DMR_BUFFER_LENGTH_BITS;
if (max >= DMR_BUFFER_LENGTH_BITS)
max -= DMR_BUFFER_LENGTH_BITS;
if (min < max) {
if (m_dataPtr >= min && m_dataPtr <= max)
correlateSync();
} else {
if (m_dataPtr >= min || m_dataPtr <= max)
correlateSync();
}
}
if(m_slot)
procSlot2();
else
procSlot1();
m_dataPtr++;
if (m_dataPtr >= DMR_BUFFER_LENGTH_BITS)
m_dataPtr = 0U;
return (m_state1 != DMRRXS_NONE) || (m_state2 != DMRRXS_NONE);;
}
void CDMRSlotRX::procSlot1()
{
if (m_dataPtr == m_endPtr1) {
frame1[0U] = m_control1;
bitsToBytes(m_startPtr1, DMR_FRAME_LENGTH_BYTES, frame1 + 1U);
if (m_control1 == CONTROL_DATA) {
// Data sync
uint8_t colorCode;
uint8_t dataType;
CDMRSlotType slotType;
slotType.decode(frame1 + 1U, colorCode, dataType);
if (colorCode == m_colorCode) {
m_syncCount1 = 0U;
m_n1 = 0U;
frame1[0U] |= dataType;
switch (dataType) {
case DT_DATA_HEADER:
DEBUG2("DMRSlot1RX: data header found pos", m_syncPtr1);
writeRSSIData1();
m_state1 = DMRRXS_DATA;
m_type1 = 0x00U;
break;
case DT_RATE_12_DATA:
case DT_RATE_34_DATA:
case DT_RATE_1_DATA:
if (m_state1 == DMRRXS_DATA) {
DEBUG2("DMRSlot1RX: data payload found pos", m_syncPtr1);
writeRSSIData1();
m_type1 = dataType;
}
break;
case DT_VOICE_LC_HEADER:
DEBUG2("DMRSlot1RX: voice header found pos", m_syncPtr1);
writeRSSIData1();
m_state1 = DMRRXS_VOICE;
break;
case DT_VOICE_PI_HEADER:
if (m_state1 == DMRRXS_VOICE) {
DEBUG2("DMRSlot1RX: voice pi header found pos", m_syncPtr1);
writeRSSIData1();
}
m_state1 = DMRRXS_VOICE;
break;
case DT_TERMINATOR_WITH_LC:
if (m_state1 == DMRRXS_VOICE) {
DEBUG2("DMRSlot1RX: voice terminator found pos", m_syncPtr1);
writeRSSIData1();
m_state1 = DMRRXS_NONE;
m_endPtr1 = NOENDPTR;
}
break;
default: // DT_CSBK
DEBUG2("DMRSlot1RX: csbk found pos", m_syncPtr1);
writeRSSIData1();
m_state1 = DMRRXS_NONE;
m_endPtr1 = NOENDPTR;
break;
}
}
} else if (m_control1 == CONTROL_VOICE) {
// Voice sync
DEBUG2("DMRSlot1RX: voice sync found pos", m_syncPtr1);
writeRSSIData1();
m_state1 = DMRRXS_VOICE;
m_syncCount1 = 0U;
m_n1 = 0U;
} else {
if (m_state1 != DMRRXS_NONE) {
m_syncCount1++;
if (m_syncCount1 >= MAX_SYNC_LOST_FRAMES) {
serial.writeDMRLost(0U);
reset1();
}
}
if (m_state1 == DMRRXS_VOICE) {
if (m_n1 >= 5U) {
frame1[0U] = CONTROL_VOICE;
m_n1 = 0U;
} else {
frame1[0U] = ++m_n1;
}
serial.writeDMRData(0U, frame1, DMR_FRAME_LENGTH_BYTES + 1U);
} else if (m_state1 == DMRRXS_DATA) {
if (m_type1 != 0x00U) {
frame1[0U] = CONTROL_DATA | m_type1;
writeRSSIData1();
}
}
}
// End of this slot, reset some items for the next slot.
m_control1 = CONTROL_NONE;
}
}
void CDMRSlotRX::procSlot2()
{
if (m_dataPtr == m_endPtr2) {
frame2[0U] = m_control2;
bitsToBytes(m_startPtr2, DMR_FRAME_LENGTH_BYTES, frame2 + 1U);
if (m_control2 == CONTROL_DATA) {
// Data sync
uint8_t colorCode;
uint8_t dataType;
CDMRSlotType slotType;
slotType.decode(frame2 + 1U, colorCode, dataType);
if (colorCode == m_colorCode) {
m_syncCount2 = 0U;
m_n2 = 0U;
frame2[0U] |= dataType;
switch (dataType) {
case DT_DATA_HEADER:
DEBUG2("DMRSlot2RX: data header found pos", m_syncPtr2);
writeRSSIData2();
m_state2 = DMRRXS_DATA;
m_type2 = 0x00U;
break;
case DT_RATE_12_DATA:
case DT_RATE_34_DATA:
case DT_RATE_1_DATA:
if (m_state2 == DMRRXS_DATA) {
DEBUG2("DMRSlot2RX: data payload found pos", m_syncPtr2);
writeRSSIData2();
m_type2 = dataType;
}
break;
case DT_VOICE_LC_HEADER:
DEBUG2("DMRSlot2RX: voice header found pos", m_syncPtr2);
writeRSSIData2();
m_state2 = DMRRXS_VOICE;
break;
case DT_VOICE_PI_HEADER:
if (m_state2 == DMRRXS_VOICE) {
DEBUG2("DMRSlot2RX: voice pi header found pos", m_syncPtr2);
writeRSSIData2();
}
m_state2 = DMRRXS_VOICE;
break;
case DT_TERMINATOR_WITH_LC:
if (m_state2 == DMRRXS_VOICE) {
DEBUG2("DMRSlot2RX: voice terminator found pos", m_syncPtr2);
writeRSSIData2();
m_state2 = DMRRXS_NONE;
m_endPtr2 = NOENDPTR;
}
break;
default: // DT_CSBK
DEBUG2("DMRSlot2RX: csbk found pos", m_syncPtr2);
writeRSSIData2();
m_state2 = DMRRXS_NONE;
m_endPtr2 = NOENDPTR;
break;
}
}
} else if (m_control2 == CONTROL_VOICE) {
// Voice sync
DEBUG2("DMRSlot2RX: voice sync found pos", m_syncPtr2);
writeRSSIData2();
m_state2 = DMRRXS_VOICE;
m_syncCount2 = 0U;
m_n2 = 0U;
} else {
if (m_state2 != DMRRXS_NONE) {
m_syncCount2++;
if (m_syncCount2 >= MAX_SYNC_LOST_FRAMES) {
serial.writeDMRLost(1U);
reset2();
}
}
if (m_state2 == DMRRXS_VOICE) {
if (m_n2 >= 5U) {
frame2[0U] = CONTROL_VOICE;
m_n2 = 0U;
} else {
frame2[0U] = ++m_n2;
}
serial.writeDMRData(1U, frame2, DMR_FRAME_LENGTH_BYTES + 1U);
} else if (m_state2 == DMRRXS_DATA) {
if (m_type2 != 0x00U) {
frame2[0U] = CONTROL_DATA | m_type2;
writeRSSIData2();
}
}
}
// End of this slot, reset some items for the next slot.
m_control2 = CONTROL_NONE;
}
}
void CDMRSlotRX::correlateSync()
{
uint16_t syncPtr;
uint16_t startPtr;
uint16_t endPtr;
uint8_t control;
if (countBits64((m_patternBuffer & DMR_SYNC_BITS_MASK) ^ DMR_MS_DATA_SYNC_BITS) <= MAX_SYNC_BYTES_ERRS) {
control = CONTROL_DATA;
syncPtr = m_dataPtr;
startPtr = m_dataPtr + DMR_BUFFER_LENGTH_BITS - DMR_SLOT_TYPE_LENGTH_BITS / 2U - DMR_INFO_LENGTH_BITS / 2U - DMR_SYNC_LENGTH_BITS + 1;
if (startPtr >= DMR_BUFFER_LENGTH_BITS)
startPtr -= DMR_BUFFER_LENGTH_BITS;
endPtr = m_dataPtr + DMR_SLOT_TYPE_LENGTH_BITS / 2U + DMR_INFO_LENGTH_BITS / 2U;
if (endPtr >= DMR_BUFFER_LENGTH_BITS)
endPtr -= DMR_BUFFER_LENGTH_BITS;
if(m_slot) {
m_syncPtr2 = syncPtr;
m_startPtr2 = startPtr;
m_endPtr2 = endPtr;
m_control2 = control;
} else {
m_syncPtr1 = syncPtr;
m_startPtr1 = startPtr;
m_endPtr1 = endPtr;
m_control1 = control;
}
DEBUG5("SYNC corr MS Data found slot/pos/start/end:", m_slot ? 2U : 1U, m_dataPtr, startPtr, endPtr);
} else if (countBits64((m_patternBuffer & DMR_SYNC_BITS_MASK) ^ DMR_MS_VOICE_SYNC_BITS) <= MAX_SYNC_BYTES_ERRS) {
control = CONTROL_VOICE;
syncPtr = m_dataPtr;
startPtr = m_dataPtr + DMR_BUFFER_LENGTH_BITS - DMR_SLOT_TYPE_LENGTH_BITS / 2U - DMR_INFO_LENGTH_BITS / 2U - DMR_SYNC_LENGTH_BITS + 1;
if (startPtr >= DMR_BUFFER_LENGTH_BITS)
startPtr -= DMR_BUFFER_LENGTH_BITS;
endPtr = m_dataPtr + DMR_SLOT_TYPE_LENGTH_BITS / 2U + DMR_INFO_LENGTH_BITS / 2U;
if (endPtr >= DMR_BUFFER_LENGTH_BITS)
endPtr -= DMR_BUFFER_LENGTH_BITS;
if(m_slot) {
m_syncPtr2 = syncPtr;
m_startPtr2 = startPtr;
m_endPtr2 = endPtr;
m_control2 = control;
} else {
m_syncPtr1 = syncPtr;
m_startPtr1 = startPtr;
m_endPtr1 = endPtr;
m_control1 = control;
}
DEBUG5("SYNC corr MS Voice found slot/pos/start/end: ", m_slot ? 2U : 1U, m_dataPtr, startPtr, endPtr);
}
}
void CDMRSlotRX::bitsToBytes(uint16_t start, uint8_t count, uint8_t* buffer)
{
for (uint8_t i = 0U; i < count; i++) {
buffer[i] = 0U;
buffer[i] |= READ_BIT1(m_buffer, start) << 7;
start++;
if (start >= DMR_BUFFER_LENGTH_BITS)
start -= DMR_BUFFER_LENGTH_BITS;
buffer[i] |= READ_BIT1(m_buffer, start) << 6;
start++;
if (start >= DMR_BUFFER_LENGTH_BITS)
start -= DMR_BUFFER_LENGTH_BITS;
buffer[i] |= READ_BIT1(m_buffer, start) << 5;
start++;
if (start >= DMR_BUFFER_LENGTH_BITS)
start -= DMR_BUFFER_LENGTH_BITS;
buffer[i] |= READ_BIT1(m_buffer, start) << 4;
start++;
if (start >= DMR_BUFFER_LENGTH_BITS)
start -= DMR_BUFFER_LENGTH_BITS;
buffer[i] |= READ_BIT1(m_buffer, start) << 3;
start++;
if (start >= DMR_BUFFER_LENGTH_BITS)
start -= DMR_BUFFER_LENGTH_BITS;
buffer[i] |= READ_BIT1(m_buffer, start) << 2;
start++;
if (start >= DMR_BUFFER_LENGTH_BITS)
start -= DMR_BUFFER_LENGTH_BITS;
buffer[i] |= READ_BIT1(m_buffer, start) << 1;
start++;
if (start >= DMR_BUFFER_LENGTH_BITS)
start -= DMR_BUFFER_LENGTH_BITS;
buffer[i] |= READ_BIT1(m_buffer, start) << 0;
start++;
if (start >= DMR_BUFFER_LENGTH_BITS)
start -= DMR_BUFFER_LENGTH_BITS;
}
}
void CDMRSlotRX::setColorCode(uint8_t colorCode)
{
m_colorCode = colorCode;
}
void CDMRSlotRX::setDelay(uint8_t delay)
{
m_delay = delay / 5;
}
void CDMRSlotRX::writeRSSIData1()
{
#if defined(SEND_RSSI_DATA)
uint16_t rssi = io.readRSSI();
frame1[34U] = (rssi >> 8) & 0xFFU;
frame1[35U] = (rssi >> 0) & 0xFFU;
serial.writeDMRData(0U, frame1, DMR_FRAME_LENGTH_BYTES + 3U);
#else
serial.writeDMRData(0U, frame1, DMR_FRAME_LENGTH_BYTES + 1U);
#endif
}
void CDMRSlotRX::writeRSSIData2()
{
#if defined(SEND_RSSI_DATA)
uint16_t rssi = io.readRSSI();
frame2[34U] = (rssi >> 8) & 0xFFU;
frame2[35U] = (rssi >> 0) & 0xFFU;
serial.writeDMRData(1U, frame2, DMR_FRAME_LENGTH_BYTES + 3U);
#else
serial.writeDMRData(1U, frame2, DMR_FRAME_LENGTH_BYTES + 1U);
#endif
}
#endif