forked from philpem/dsd-dmr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dsd_frame_sync.c
322 lines (293 loc) · 10 KB
/
dsd_frame_sync.c
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
/*
* Copyright (C) 2010 DSD Author
* GPG Key ID: 0x3F1D7FD0 (74EF 430D F7F2 0A48 FCE6 F630 FAA2 635D 3F1D 7FD0)
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
#include "dsd.h"
void Shellsort_int(int *in, unsigned int n)
{
int i, j;
int v;
unsigned int inc = 1;
do {
inc = 3 * inc + 1;
} while (inc <= n);
do {
inc = inc / 3;
for (i = inc + 1; i <= n; i++) {
v = in[i-1];
j = i;
while (in[j-inc-1] > v) {
in[j-1] = in[j-inc-1];
j -= inc;
if (j <= inc)
break;
}
in[j-1] = v;
}
} while (inc > 1);
}
int
getFrameSync (dsd_opts * opts, dsd_state * state)
{
/* detects frame sync and returns frame type
* 0 = +P25p1
* 1 = -P25p1
* 2 = +X2-TDMA (non inverted signal data frame)
* 3 = -X2-TDMA (inverted signal data frame)
* 4 = +DMR (non inverted signal data frame)
* 5 = -DMR (inverted signal data frame)
* 6 = +NXDN (non inverted data frame)
* 7 = -NXDN (inverted data frame)
* 8 = +D-STAR
* 9 = -D-STAR
* 10 = +D-STAR_HD
* 11 = -D-STAR_HD
* 12 = +DMR (non inverted signal voice frame)
* 13 = -DMR (inverted signal voice frame)
* 14 = +X2-TDMA (non inverted signal voice frame)
* 15 = -X2-TDMA (inverted signal voice frame)
* 16 = +NXDN (non inverted voice frame)
* 17 = -NXDN (inverted voice frame)
*/
int i, t, dibit, sync, symbol, synctest_pos, lastt;
char synctest[25];
char *synctest_p;
char synctest_buf[10240];
int lmin, lmax, lidx;
int lbuf[24], lbuf2[24];
for (i = 18; i < 24; i++) {
lbuf[i] = 0;
lbuf2[i] = 0;
}
// detect frame sync
t = 0;
synctest[24] = 0;
synctest_pos = 0;
synctest_p = synctest_buf;
sync = 0;
lmin = 0;
lmax = 0;
lidx = 0;
lastt = 0;
while (sync == 0) {
t++;
symbol = getSymbol (opts, state, 0);
lbuf[lidx] = symbol;
state->sbuf[state->sidx] = symbol;
if (lidx == 23) {
lidx = 0;
} else {
lidx++;
}
if (state->sidx == (state->ssize - 1)) {
state->sidx = 0;
} else {
state->sidx++;
}
if (lastt == 23) {
lastt = 0;
state->rf_mod = 2;
} else {
lastt++;
}
//determine dibit state
if (symbol > 0) {
dibit = 49; // '1'
} else {
dibit = 51; // '3'
}
*synctest_p = dibit;
if (t >= 18) {
for (i = 0; i < 24; i++) {
lbuf2[i] = lbuf[i];
}
Shellsort_int (lbuf2, 24);
lmin = (lbuf2[2] + lbuf2[3] + lbuf2[4]) / 3;
lmax = (lbuf2[21] + lbuf2[20] + lbuf2[19]) / 3;
if (opts->datascope && (lidx == 0)) {
print_datascope(state, lbuf2, 24);
}
memcpy (synctest, (synctest_p - 23), 24);
if ((memcmp (synctest, P25P1_SYNC, 24) == 0) || (memcmp (synctest, INV_P25P1_SYNC, 24) == 0)) {
state->offset = synctest_pos;
strcpy (state->ftype, "P25p1");
if (synctest[0] == '1') {
state->lastsynctype = 0;
} else {
state->lastsynctype = 1;
}
goto update_sync_and_return;
}
if ((memcmp (synctest, DMR_MS_DATA_SYNC, 24) == 0) || (memcmp (synctest, DMR_BS_DATA_SYNC, 24) == 0)) {
state->offset = synctest_pos;
state->dmrMsMode = (synctest[22] == '1');
strcpy (state->ftype, "DMR ");
if (opts->inverted_dmr == 0) {
// data frame
if (state->lastsynctype != 4) {
state->firstframe = 1;
}
state->lastsynctype = 4;
} else {
// inverted voice frame
if (state->lastsynctype != 13) {
state->firstframe = 1;
}
state->lastsynctype = 13;
}
goto update_sync_and_return;
}
if ((memcmp (synctest, DMR_MS_VOICE_SYNC, 24) == 0) || (memcmp (synctest, DMR_BS_VOICE_SYNC, 24) == 0)) {
state->offset = synctest_pos;
state->dmrMsMode = (synctest[22] == '3');
strcpy (state->ftype, "DMR ");
if (opts->inverted_dmr == 0) {
// voice frame
if (state->lastsynctype != 12) {
state->firstframe = 1;
}
state->lastsynctype = 12;
} else {
// inverted data frame
if (state->lastsynctype != 5) {
state->firstframe = 1;
}
state->lastsynctype = 5;
}
goto update_sync_and_return;
}
if ((memcmp (synctest, X2TDMA_BS_DATA_SYNC, 24) == 0) || (memcmp (synctest, X2TDMA_MS_DATA_SYNC, 24) == 0)) {
state->offset = synctest_pos;
state->dmrMsMode = (synctest[22] == '1');
strcpy (state->ftype, "X2-TDMA ");
if (opts->inverted_x2tdma == 0) {
// data frame
state->lastsynctype = 2;
} else {
// inverted voice frame
if (state->lastsynctype != 15) {
state->firstframe = 1;
}
state->lastsynctype = 15;
}
goto update_sync_and_return;
}
if ((memcmp (synctest, X2TDMA_BS_VOICE_SYNC, 24) == 0) || (memcmp (synctest, X2TDMA_MS_VOICE_SYNC, 24) == 0)) {
state->offset = synctest_pos;
state->dmrMsMode = (synctest[22] == '3');
strcpy (state->ftype, "X2-TDMA ");
if (opts->inverted_x2tdma == 0) {
// voice frame
if (state->lastsynctype != 14) {
state->firstframe = 1;
}
state->lastsynctype = 14;
} else {
// inverted data frame
state->lastsynctype = 3;
}
goto update_sync_and_return;
}
if ((memcmp (synctest+6, NXDN_BS_VOICE_SYNC, 18) == 0) ||
(memcmp (synctest+6, NXDN_MS_VOICE_SYNC, 18) == 0) ||
(memcmp (synctest+6, NXDN_BS_DATA_SYNC, 18) == 0) ||
(memcmp (synctest+6, NXDN_MS_DATA_SYNC, 18) == 0)) {
if (synctest[21] == '1') {
state->lastsynctype = 6;
} else {
state->lastsynctype = 16;
}
if ((state->lastsynctype == 6) || (state->lastsynctype == 16)) {
state->offset = synctest_pos;
strcpy (state->ftype, "NXDN96 ");
goto update_sync_and_return;
}
}
if ((memcmp (synctest+6, INV_NXDN_BS_VOICE_SYNC, 18) == 0) ||
(memcmp (synctest+6, INV_NXDN_MS_VOICE_SYNC, 18) == 0) ||
(memcmp (synctest+6, INV_NXDN_BS_DATA_SYNC, 18) == 0) ||
(memcmp (synctest+6, INV_NXDN_MS_DATA_SYNC, 18) == 0)) {
if (synctest[21] == '3') {
state->lastsynctype = 7;
} else {
state->lastsynctype = 17;
}
if ((state->lastsynctype == 7) || (state->lastsynctype == 17)) {
state->offset = synctest_pos;
strcpy (state->ftype, "NXDN96 ");
goto update_sync_and_return;
}
}
if ((memcmp (synctest, DSTAR_SYNC, 24) == 0) || (memcmp (synctest, INV_DSTAR_SYNC, 24) == 0)) {
state->offset = synctest_pos;
strcpy (state->ftype, "D-STAR ");
if (synctest[0] == '3') {
state->lastsynctype = 8;
} else {
state->lastsynctype = 9;
}
goto update_sync_and_return;
}
if ((memcmp (synctest, DSTAR_HD_SYNC, 24) == 0) || (memcmp (synctest, INV_DSTAR_HD_SYNC, 24) == 0)) {
state->offset = synctest_pos;
strcpy (state->ftype, "D-STARHD");
if (synctest[0] == '1') {
state->lastsynctype = 10;
} else {
state->lastsynctype = 11;
}
goto update_sync_and_return;
}
}
if (exitflag == 1) {
cleanupAndExit (opts, state);
}
if (synctest_pos < 10200) {
synctest_pos++;
synctest_p++;
} else {
// buffer reset
synctest_pos = 0;
synctest_p = synctest_buf;
state->lastsynctype = -1;
}
if (state->lastsynctype != -1) {
//if (synctest_pos >= 1800) {
if (synctest_pos >= 9000) {
if ((state->lastsynctype != -1) && !(opts->datascope)) {
printf ("Sync: no sync\n");
}
state->lastsynctype = -1;
#if 1
state->max = 3000;
state->min = -3000;
state->umid = 1000;
state->lmid = -1000;
state->center = 0;
#endif
return (-1);
}
}
}
return (-1);
update_sync_and_return:
// recalibrate center/umid/lmid
state->max = ((state->max) + (lmax)) / 2;
state->min = ((state->min) + (lmin)) / 2;
state->center = ((state->max) + (state->min)) / 2;
state->umid = (((state->max) - state->center) * 5 / 8) + state->center;
state->lmid = (((state->min) - state->center) * 5 / 8) + state->center;
return state->lastsynctype;
}