forked from g4klx/MMDVM_HS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DMRDMORX.cpp
294 lines (253 loc) · 8.55 KB
/
DMRDMORX.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
/*
* Copyright (C) 2009-2016 by Jonathan Naylor G4KLX
* Copyright (C) 2016,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"
#include "Globals.h"
#include "DMRDMORX.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)))
CDMRDMORX::CDMRDMORX() :
m_patternBuffer(0x00U),
m_buffer(),
m_dataPtr(0U),
m_syncPtr(0U),
m_startPtr(0U),
m_endPtr(NOENDPTR),
m_control(CONTROL_NONE),
m_syncCount(0U),
m_colorCode(0U),
m_state(DMORXS_NONE),
m_n(0U),
m_type(0U)
{
}
void CDMRDMORX::reset()
{
m_syncPtr = 0U;
m_control = CONTROL_NONE;
m_syncCount = 0U;
m_state = DMORXS_NONE;
m_startPtr = 0U;
m_endPtr = NOENDPTR;
}
void CDMRDMORX::databit(bool bit)
{
WRITE_BIT1(m_buffer, m_dataPtr, bit);
m_patternBuffer <<= 1;
if (bit)
m_patternBuffer |= 0x01U;
if (m_state == DMORXS_NONE) {
correlateSync();
} else {
uint16_t min = m_syncPtr + DMO_BUFFER_LENGTH_BITS - 2;
uint16_t max = m_syncPtr + 2;
if (min >= DMO_BUFFER_LENGTH_BITS)
min -= DMO_BUFFER_LENGTH_BITS;
if (max >= DMO_BUFFER_LENGTH_BITS)
max -= DMO_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_dataPtr == m_endPtr) {
frame[0U] = m_control;
bitsToBytes(m_startPtr, DMR_FRAME_LENGTH_BYTES, frame + 1U);
if (m_control == CONTROL_DATA) {
// Data sync
uint8_t colorCode;
uint8_t dataType;
CDMRSlotType slotType;
slotType.decode(frame + 1U, colorCode, dataType);
if (colorCode == m_colorCode) {
m_syncCount = 0U;
m_n = 0U;
frame[0U] |= dataType;
switch (dataType) {
case DT_DATA_HEADER:
DEBUG2("DMRDMORX: data header found pos", m_syncPtr);
writeRSSIData(frame);
m_state = DMORXS_DATA;
m_type = 0x00U;
break;
case DT_RATE_12_DATA:
case DT_RATE_34_DATA:
case DT_RATE_1_DATA:
if (m_state == DMORXS_DATA) {
DEBUG2("DMRDMORX: data payload found pos", m_syncPtr);
writeRSSIData(frame);
m_type = dataType;
}
break;
case DT_VOICE_LC_HEADER:
DEBUG2("DMRDMORX: voice header found pos", m_syncPtr);
writeRSSIData(frame);
m_state = DMORXS_VOICE;
break;
case DT_VOICE_PI_HEADER:
if (m_state == DMORXS_VOICE) {
DEBUG2("DMRDMORX: voice pi header found pos", m_syncPtr);
writeRSSIData(frame);
}
m_state = DMORXS_VOICE;
break;
case DT_TERMINATOR_WITH_LC:
if (m_state == DMORXS_VOICE) {
DEBUG2("DMRDMORX: voice terminator found pos", m_syncPtr);
writeRSSIData(frame);
reset();
}
break;
default: // DT_CSBK
DEBUG2("DMRDMORX: csbk found pos", m_syncPtr);
writeRSSIData(frame);
reset();
break;
}
}
} else if (m_control == CONTROL_VOICE) {
// Voice sync
DEBUG2("DMRDMORX: voice sync found pos", m_syncPtr);
writeRSSIData(frame);
m_state = DMORXS_VOICE;
m_syncCount = 0U;
m_n = 0U;
} else {
if (m_state != DMORXS_NONE) {
m_syncCount++;
if (m_syncCount >= MAX_SYNC_LOST_FRAMES) {
serial.writeDMRLost(true);
reset();
}
}
if (m_state == DMORXS_VOICE) {
if (m_n >= 5U) {
frame[0U] = CONTROL_VOICE;
m_n = 0U;
} else {
frame[0U] = ++m_n;
}
serial.writeDMRData(true, frame, DMR_FRAME_LENGTH_BYTES + 1U);
} else if (m_state == DMORXS_DATA) {
if (m_type != 0x00U) {
frame[0U] = CONTROL_DATA | m_type;
writeRSSIData(frame);
}
}
}
// End of this slot, reset some items for the next slot.
m_control = CONTROL_NONE;
}
m_dataPtr++;
if (m_dataPtr >= DMO_BUFFER_LENGTH_BITS)
m_dataPtr = 0U;
io.setDecode(m_state != DMORXS_NONE);
}
void CDMRDMORX::correlateSync()
{
if ( (countBits64((m_patternBuffer & DMR_SYNC_BITS_MASK) ^ DMR_MS_DATA_SYNC_BITS) <= MAX_SYNC_BYTES_ERRS) || \
(countBits64((m_patternBuffer & DMR_SYNC_BITS_MASK) ^ DMR_S2_DATA_SYNC_BITS) <= MAX_SYNC_BYTES_ERRS) ) {
m_control = CONTROL_DATA;
m_syncPtr = m_dataPtr;
m_startPtr = m_dataPtr + DMO_BUFFER_LENGTH_BITS - DMR_SLOT_TYPE_LENGTH_BITS / 2U - DMR_INFO_LENGTH_BITS / 2U - DMR_SYNC_LENGTH_BITS + 1;
if (m_startPtr >= DMO_BUFFER_LENGTH_BITS)
m_startPtr -= DMO_BUFFER_LENGTH_BITS;
m_endPtr = m_dataPtr + DMR_SLOT_TYPE_LENGTH_BITS / 2U + DMR_INFO_LENGTH_BITS / 2U;
if (m_endPtr >= DMO_BUFFER_LENGTH_BITS)
m_endPtr -= DMO_BUFFER_LENGTH_BITS;
m_modeTimerCnt = 0;
//DEBUG4("SYNC MS Data found pos/start/end:", m_dataPtr, m_startPtr, m_endPtr);
} else if ( (countBits64((m_patternBuffer & DMR_SYNC_BITS_MASK) ^ DMR_MS_VOICE_SYNC_BITS) <= MAX_SYNC_BYTES_ERRS) || \
(countBits64((m_patternBuffer & DMR_SYNC_BITS_MASK) ^ DMR_S2_VOICE_SYNC_BITS) <= MAX_SYNC_BYTES_ERRS) ) {
m_control = CONTROL_VOICE;
m_syncPtr = m_dataPtr;
m_startPtr = m_dataPtr + DMO_BUFFER_LENGTH_BITS - DMR_SLOT_TYPE_LENGTH_BITS / 2U - DMR_INFO_LENGTH_BITS / 2U - DMR_SYNC_LENGTH_BITS + 1;
if (m_startPtr >= DMO_BUFFER_LENGTH_BITS)
m_startPtr -= DMO_BUFFER_LENGTH_BITS;
m_endPtr = m_dataPtr + DMR_SLOT_TYPE_LENGTH_BITS / 2U + DMR_INFO_LENGTH_BITS / 2U;
if (m_endPtr >= DMO_BUFFER_LENGTH_BITS)
m_endPtr -= DMO_BUFFER_LENGTH_BITS;
m_modeTimerCnt = 0;
//DEBUG4("SYNC MS Voice found pos/start/end: ", m_dataPtr, m_startPtr, m_endPtr);
}
}
void CDMRDMORX::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 >= DMO_BUFFER_LENGTH_BITS)
start -= DMO_BUFFER_LENGTH_BITS;
buffer[i] |= READ_BIT1(m_buffer, start) << 6;
start++;
if (start >= DMO_BUFFER_LENGTH_BITS)
start -= DMO_BUFFER_LENGTH_BITS;
buffer[i] |= READ_BIT1(m_buffer, start) << 5;
start++;
if (start >= DMO_BUFFER_LENGTH_BITS)
start -= DMO_BUFFER_LENGTH_BITS;
buffer[i] |= READ_BIT1(m_buffer, start) << 4;
start++;
if (start >= DMO_BUFFER_LENGTH_BITS)
start -= DMO_BUFFER_LENGTH_BITS;
buffer[i] |= READ_BIT1(m_buffer, start) << 3;
start++;
if (start >= DMO_BUFFER_LENGTH_BITS)
start -= DMO_BUFFER_LENGTH_BITS;
buffer[i] |= READ_BIT1(m_buffer, start) << 2;
start++;
if (start >= DMO_BUFFER_LENGTH_BITS)
start -= DMO_BUFFER_LENGTH_BITS;
buffer[i] |= READ_BIT1(m_buffer, start) << 1;
start++;
if (start >= DMO_BUFFER_LENGTH_BITS)
start -= DMO_BUFFER_LENGTH_BITS;
buffer[i] |= READ_BIT1(m_buffer, start) << 0;
start++;
if (start >= DMO_BUFFER_LENGTH_BITS)
start -= DMO_BUFFER_LENGTH_BITS;
}
}
void CDMRDMORX::setColorCode(uint8_t colorCode)
{
m_colorCode = colorCode;
}
void CDMRDMORX::writeRSSIData(uint8_t* frame)
{
#if defined(SEND_RSSI_DATA)
uint16_t rssi = io.readRSSI();
frame[34U] = (rssi >> 8) & 0xFFU;
frame[35U] = (rssi >> 0) & 0xFFU;
serial.writeDMRData(true, frame, DMR_FRAME_LENGTH_BYTES + 3U);
#else
serial.writeDMRData(true, frame, DMR_FRAME_LENGTH_BYTES + 1U);
#endif
}