-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathnxdnencoder.cpp
332 lines (291 loc) · 7.61 KB
/
nxdnencoder.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
/*
Copyright (C) 2019 Doug McLain
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 3 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, see <https://www.gnu.org/licenses/>.
*/
#include "nxdnencoder.h"
#include <cstring>
const int dvsi_interleave[49] = {
0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 41, 43, 45, 47,
1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34, 37, 40, 42, 44, 46, 48,
2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35, 38
};
/*
const int dvsi_deinterleave[49] = {
0, 18, 36, 1, 19, 37, 2, 20, 38, 3, 21, 39, 4, 22, 40, 5, 23, 41,
6, 24, 42, 7, 25, 43, 8, 26, 44, 9, 27, 45, 10, 28, 46, 11, 29, 47,
12, 30, 48, 13, 31, 14, 32, 15, 33, 16, 34, 17, 35
};
*/
const uint8_t NXDN_LICH_RFCT_RDCH = 2U;
const uint8_t NXDN_LICH_USC_SACCH_NS = 0U;
const uint8_t NXDN_LICH_USC_SACCH_SS = 2U;
const uint8_t NXDN_LICH_STEAL_FACCH = 0U;
const uint8_t NXDN_LICH_STEAL_NONE = 3U;
const uint8_t NXDN_LICH_DIRECTION_INBOUND = 0U;
const uint8_t NXDN_MESSAGE_TYPE_VCALL = 1U;
const uint8_t NXDN_MESSAGE_TYPE_TX_REL = 8U;
const unsigned char 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])
NXDNEncoder::NXDNEncoder()
{
m_nxdncnt = 0;
m_srcid = 12065;
m_dstid = 26000;
}
uint8_t * NXDNEncoder::get_frame(uint8_t *ambe)
{
memcpy(m_nxdnframe, "NXDND", 5);
m_nxdnframe[5U] = (m_srcid >> 8) & 0xFFU;
m_nxdnframe[6U] = (m_srcid >> 0) & 0xFFU;
m_nxdnframe[7U] = (m_dstid >> 8) & 0xFFU;
m_nxdnframe[8U] = (m_dstid >> 0) & 0xFFU;
m_nxdnframe[9U] = 0x01U;
if(!m_nxdncnt || m_eot){
encode_header();
}
else{
m_ambe = ambe;
encode_data();
}
if (m_nxdnframe[10U] == 0x81U || m_nxdnframe[10U] == 0x83U) {
m_nxdnframe[9U] |= m_nxdnframe[15U] == 0x01U ? 0x04U : 0x00U;
m_nxdnframe[9U] |= m_nxdnframe[15U] == 0x08U ? 0x08U : 0x00U;
}
else if ((m_nxdnframe[10U] & 0xF0U) == 0x90U) {
m_nxdnframe[9U] |= 0x02U;
if (m_nxdnframe[10U] == 0x90U || m_nxdnframe[10U] == 0x92U || m_nxdnframe[10U] == 0x9CU || m_nxdnframe[10U] == 0x9EU) {
m_nxdnframe[9U] |= m_nxdnframe[12U] == 0x09U ? 0x04U : 0x00U;
m_nxdnframe[9U] |= m_nxdnframe[12U] == 0x08U ? 0x08U : 0x00U;
}
}
if(m_eot){
m_nxdncnt = 0;
m_eot = false;
}
else{
++m_nxdncnt;
}
return m_nxdnframe;
}
void NXDNEncoder::encode_header()
{
const uint8_t idle[3U] = {0x10, 0x00, 0x00};
m_lich = 0;
memset(m_sacch, 0, 5U);
memset(m_layer3, 0, 22U);
set_lich_rfct(NXDN_LICH_RFCT_RDCH);
set_lich_fct(NXDN_LICH_USC_SACCH_NS);
set_lich_option(NXDN_LICH_STEAL_FACCH);
set_lich_dir(NXDN_LICH_DIRECTION_INBOUND);
m_nxdnframe[10U] = get_lich();
set_sacch_ran(0x01);
set_sacch_struct(0); //Single
set_sacch_data(idle);
get_sacch(&m_nxdnframe[11U]);
if(m_eot){
set_layer3_msgtype(NXDN_MESSAGE_TYPE_TX_REL);
}
else{
set_layer3_msgtype(NXDN_MESSAGE_TYPE_VCALL);
}
set_layer3_srcid(m_srcid);
set_layer3_dstid(m_dstid);
set_layer3_grp(true);
set_layer3_blks(0U);
memcpy(&m_nxdnframe[15U], m_layer3, 14U);
memcpy(&m_nxdnframe[29U], m_layer3, 14U);
}
void NXDNEncoder::encode_data()
{
uint8_t msg[3U];
m_lich = 0;
memset(m_sacch, 0, 5U);
memset(m_layer3, 0, 22U);
set_lich_rfct(NXDN_LICH_RFCT_RDCH);
set_lich_fct(NXDN_LICH_USC_SACCH_SS);
set_lich_option(NXDN_LICH_STEAL_NONE);
set_lich_dir(NXDN_LICH_DIRECTION_INBOUND);
m_nxdnframe[10U] = get_lich();
set_sacch_ran(0x01);
set_layer3_msgtype(NXDN_MESSAGE_TYPE_VCALL);
set_layer3_srcid(m_srcid);
set_layer3_dstid(m_dstid);
set_layer3_grp(true);
set_layer3_blks(0U);
switch(m_nxdncnt % 4){
case 0:
set_sacch_struct(3);
layer3_encode(msg, 18U, 0U);
set_sacch_data(msg);
break;
case 1:
set_sacch_struct(2);
layer3_encode(msg, 18U, 18U);
set_sacch_data(msg);
break;
case 2:
set_sacch_struct(1);
layer3_encode(msg, 18U, 36U);
set_sacch_data(msg);
break;
case 3:
set_sacch_struct(0);
layer3_encode(msg, 18U, 54U);
set_sacch_data(msg);
break;
}
get_sacch(&m_nxdnframe[11U]);
if(m_hwtx){
for(int i = 0; i < 4; ++i){
deinterleave_ambe(&m_ambe[7*i]);
}
}
memcpy(&m_nxdnframe[15], m_ambe, 7);
for(int i = 0; i < 7; ++i){
m_nxdnframe[21+i] |= (m_ambe[7+i] >> 1);
m_nxdnframe[22+i] = (m_ambe[7+i] & 1) << 7;
}
m_nxdnframe[28] |= (m_ambe[13] >> 2);
memcpy(&m_nxdnframe[29], &m_ambe[14], 7);
for(int i = 0; i < 7; ++i){
m_nxdnframe[35+i] |= (m_ambe[21+i] >> 1);
m_nxdnframe[36+i] = (m_ambe[21+i] & 1) << 7;
}
m_nxdnframe[41] |= (m_ambe[27] >> 2);
}
void NXDNEncoder::deinterleave_ambe(uint8_t *d)
{
uint8_t dvsi_data[49];
uint8_t ambe_data[7];
memset(ambe_data, 0, 7);
for(int i = 0; i < 6; ++i){
for(int j = 0; j < 8; j++){
dvsi_data[j+(8*i)] = (1 & (d[i] >> (7 - j)));
}
}
dvsi_data[48] = (1 & (d[6] >> 7));
for(int i = 0, j; i < 49; ++i){
j = dvsi_interleave[i];
ambe_data[i/8] += (dvsi_data[j])<<(7-(i%8));
//j = dvsi_deinterleave[i];
//ambe_data[j/8] += (dvsi_data[i])<<(7-(j%8));
}
memcpy(d, ambe_data, 7);
}
void NXDNEncoder::set_lich_rfct(uint8_t rfct)
{
m_lich &= 0x3FU;
m_lich |= (rfct << 6) & 0xC0U;
}
void NXDNEncoder::set_lich_fct(uint8_t fct)
{
m_lich &= 0xCFU;
m_lich |= (fct << 4) & 0x30U;
}
void NXDNEncoder::set_lich_option(uint8_t o)
{
m_lich &= 0xF3U;
m_lich |= (o << 2) & 0x0CU;
}
void NXDNEncoder::set_lich_dir(uint8_t d)
{
m_lich &= 0xFDU;
m_lich |= (d << 1) & 0x02U;
}
uint8_t NXDNEncoder::get_lich()
{
bool parity;
switch (m_lich & 0xF0U) {
case 0x80U:
case 0xB0U:
parity = true;
default:
parity = false;
}
if (parity)
m_lich |= 0x01U;
else
m_lich &= 0xFEU;
return m_lich;
}
void NXDNEncoder::set_sacch_ran(uint8_t ran)
{
m_sacch[0] &= 0xC0U;
m_sacch[0] |= ran;
}
void NXDNEncoder::set_sacch_struct(uint8_t s)
{
m_sacch[0] &= 0x3FU;
m_sacch[0] |= (s << 6) & 0xC0U;;
}
void NXDNEncoder::set_sacch_data(const uint8_t *d)
{
uint8_t offset = 8U;
for (uint8_t i = 0U; i < 18U; i++, offset++) {
bool b = READ_BIT1(d, i);
WRITE_BIT1(m_sacch, offset, b);
}
}
void NXDNEncoder::get_sacch(uint8_t *d)
{
memcpy(d, m_sacch, 4U);
encode_crc6(d, 26);
}
void NXDNEncoder::set_layer3_msgtype(uint8_t t)
{
m_layer3[0] &= 0xC0U;
m_layer3[0] |= t & 0x3FU;
}
void NXDNEncoder::set_layer3_srcid(uint16_t src)
{
m_layer3[3U] = (src >> 8) & 0xFF;
m_layer3[4U] = (src >> 0) & 0xFF ;
}
void NXDNEncoder::set_layer3_dstid(uint16_t dst)
{
m_layer3[5U] = (dst >> 8) & 0xFF;
m_layer3[6U] = (dst >> 0) & 0xFF ;
}
void NXDNEncoder::set_layer3_grp(bool grp)
{
m_layer3[2U] |= grp ? 0x20U : 0x20U;
}
void NXDNEncoder::set_layer3_blks(uint8_t b)
{
m_layer3[8U] &= 0xF0U;
m_layer3[8U] |= b & 0x0FU;
}
void NXDNEncoder::layer3_encode(uint8_t* d, uint8_t len, uint8_t offset)
{
for (uint32_t i = 0U; i < len; i++, offset++) {
bool b = READ_BIT1(m_layer3, offset);
WRITE_BIT1(d, i, b);
}
}
void NXDNEncoder::encode_crc6(uint8_t *d, uint8_t len)
{
uint8_t crc = 0x3FU;
for (unsigned int i = 0U; i < len; i++) {
bool bit1 = READ_BIT1(d, i) != 0x00U;
bool bit2 = (crc & 0x20U) == 0x20U;
crc <<= 1;
if (bit1 ^ bit2)
crc ^= 0x27U;
}
crc &= 0x3FU;
uint8_t n = len;
for (uint8_t i = 2U; i < 8U; i++, n++) {
bool b = READ_BIT1((&crc), i);
WRITE_BIT1(d, n, b);
}
}