-
Notifications
You must be signed in to change notification settings - Fork 314
/
Copy pathrrbb.c
488 lines (389 loc) · 11.4 KB
/
rrbb.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
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
//
// This file is part of Dire Wolf, an amateur radio packet TNC.
//
// Copyright (C) 2011, 2012, 2013, 2014, 2015 John Langner, WB2OSZ
//
// 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, see <http://www.gnu.org/licenses/>.
//
/********************************************************************************
*
* File: rrbb.c
*
* Purpose: Raw Received Bit Buffer.
* An array of bits used to hold data out of
* the demodulator before feeding it into the HLDC decoding.
*
* Version 1.2: Save initial state of 9600 baud descrambler so we can
* attempt bit fix up on G3RUH/K9NG scrambled data.
*
* Version 1.3: Store as bytes rather than packing 8 bits per byte.
*
*******************************************************************************/
#define RRBB_C
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "direwolf.h"
#include "textcolor.h"
#include "ax25_pad.h"
#include "rrbb.h"
#define MAGIC1 0x12344321
#define MAGIC2 0x56788765
static int new_count = 0;
static int delete_count = 0;
/***********************************************************************************
*
* Name: rrbb_new
*
* Purpose: Allocate space for an array of samples.
*
* Inputs: chan - Radio channel from whence it came.
*
* subchan - Which demodulator of the channel.
*
* slice - multiple thresholds per demodulator.
*
* is_scrambled - Is data scrambled? (true, false)
*
* descram_state - State of data descrambler.
*
* prev_descram - Previous descrambled bit.
*
* Returns: Handle to be used by other functions.
*
* Description:
*
***********************************************************************************/
rrbb_t rrbb_new (int chan, int subchan, int slice, int is_scrambled, int descram_state, int prev_descram)
{
rrbb_t result;
assert (chan >= 0 && chan < MAX_CHANS);
assert (subchan >= 0 && subchan < MAX_SUBCHANS);
assert (slice >= 0 && slice < MAX_SLICERS);
result = malloc(sizeof(struct rrbb_s));
result->magic1 = MAGIC1;
result->chan = chan;
result->subchan = subchan;
result->slice = slice;
result->magic2 = MAGIC2;
new_count++;
if (new_count > delete_count + 100) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("MEMORY LEAK, rrbb_new, new_count=%d, delete_count=%d\n", new_count, delete_count);
}
rrbb_clear (result, is_scrambled, descram_state, prev_descram);
return (result);
}
/***********************************************************************************
*
* Name: rrbb_clear
*
* Purpose: Clear by setting length to zero, etc.
*
* Inputs: b -Handle for sample array.
*
* is_scrambled - Is data scrambled? (true, false)
*
* descram_state - State of data descrambler.
*
* prev_descram - Previous descrambled bit.
*
***********************************************************************************/
void rrbb_clear (rrbb_t b, int is_scrambled, int descram_state, int prev_descram)
{
assert (b != NULL);
assert (b->magic1 == MAGIC1);
assert (b->magic2 == MAGIC2);
assert (is_scrambled == 0 || is_scrambled == 1);
assert (prev_descram == 0 || prev_descram == 1);
b->nextp = NULL;
b->alevel.rec = 9999; // TODO: was there some reason for this instead of 0 or -1?
b->alevel.mark = 9999;
b->alevel.space = 9999;
b->len = 0;
b->is_scrambled = is_scrambled;
b->descram_state = descram_state;
b->prev_descram = prev_descram;
}
/***********************************************************************************
*
* Name: rrbb_append_bit
*
* Purpose: Append another bit to the end.
*
* Inputs: Handle for sample array.
* Value for the sample.
*
***********************************************************************************/
/* Definition in header file so it can be inlined. */
/***********************************************************************************
*
* Name: rrbb_chop8
*
* Purpose: Remove 8 from the length.
*
* Inputs: Handle for bit array.
*
* Description: Back up after appending the flag sequence.
*
***********************************************************************************/
void rrbb_chop8 (rrbb_t b)
{
assert (b != NULL);
assert (b->magic1 == MAGIC1);
assert (b->magic2 == MAGIC2);
if (b->len >= 8) {
b->len -= 8;
}
}
/***********************************************************************************
*
* Name: rrbb_get_len
*
* Purpose: Get number of bits in the array.
*
* Inputs: Handle for bit array.
*
***********************************************************************************/
int rrbb_get_len (rrbb_t b)
{
assert (b != NULL);
assert (b->magic1 == MAGIC1);
assert (b->magic2 == MAGIC2);
return (b->len);
}
/***********************************************************************************
*
* Name: rrbb_get_bit
*
* Purpose: Get value of bit in specified position.
*
* Inputs: Handle for sample array.
* Index into array.
*
***********************************************************************************/
/* Definition in header file so it can be inlined. */
/***********************************************************************************
*
* Name: rrbb_flip_bit
*
* Purpose: Complement the value of bit in specified position.
*
* Inputs: Handle for bit array.
* Index into array.
*
***********************************************************************************/
//void rrbb_flip_bit (rrbb_t b, unsigned int ind)
//{
// unsigned int di, mi;
//
// assert (b != NULL);
// assert (b->magic1 == MAGIC1);
// assert (b->magic2 == MAGIC2);
//
// assert (ind < b->len);
//
// di = ind / SOI;
// mi = ind % SOI;
//
// b->data[di] ^= masks[mi];
//}
/***********************************************************************************
*
* Name: rrbb_delete
*
* Purpose: Free the storage associated with the bit array.
*
* Inputs: Handle for bit array.
*
***********************************************************************************/
void rrbb_delete (rrbb_t b)
{
assert (b != NULL);
assert (b->magic1 == MAGIC1);
assert (b->magic2 == MAGIC2);
b->magic1 = 0;
b->magic2 = 0;
free (b);
delete_count++;
}
/***********************************************************************************
*
* Name: rrbb_set_netxp
*
* Purpose: Set the nextp field, used to maintain a queue.
*
* Inputs: b Handle for bit array.
* np New value for nextp.
*
***********************************************************************************/
void rrbb_set_nextp (rrbb_t b, rrbb_t np)
{
assert (b != NULL);
assert (b->magic1 == MAGIC1);
assert (b->magic2 == MAGIC2);
b->nextp = np;
}
/***********************************************************************************
*
* Name: rrbb_get_netxp
*
* Purpose: Get value of nextp field.
*
* Inputs: b Handle for bit array.
*
***********************************************************************************/
rrbb_t rrbb_get_nextp (rrbb_t b)
{
assert (b != NULL);
assert (b->magic1 == MAGIC1);
assert (b->magic2 == MAGIC2);
return (b->nextp);
}
/***********************************************************************************
*
* Name: rrbb_get_chan
*
* Purpose: Get channel from which bit buffer was received.
*
* Inputs: b Handle for bit array.
*
***********************************************************************************/
int rrbb_get_chan (rrbb_t b)
{
assert (b != NULL);
assert (b->magic1 == MAGIC1);
assert (b->magic2 == MAGIC2);
assert (b->chan >= 0 && b->chan < MAX_CHANS);
return (b->chan);
}
/***********************************************************************************
*
* Name: rrbb_get_subchan
*
* Purpose: Get subchannel from which bit buffer was received.
*
* Inputs: b Handle for bit array.
*
***********************************************************************************/
int rrbb_get_subchan (rrbb_t b)
{
assert (b != NULL);
assert (b->magic1 == MAGIC1);
assert (b->magic2 == MAGIC2);
assert (b->subchan >= 0 && b->subchan < MAX_SUBCHANS);
return (b->subchan);
}
/***********************************************************************************
*
* Name: rrbb_get_slice
*
* Purpose: Get slice number from which bit buffer was received.
*
* Inputs: b Handle for bit array.
*
***********************************************************************************/
int rrbb_get_slice (rrbb_t b)
{
assert (b != NULL);
assert (b->magic1 == MAGIC1);
assert (b->magic2 == MAGIC2);
assert (b->slice >= 0 && b->slice < MAX_SLICERS);
return (b->slice);
}
/***********************************************************************************
*
* Name: rrbb_set_audio_level
*
* Purpose: Set audio level at time the frame was received.
*
* Inputs: b Handle for bit array.
* alevel Audio level.
*
***********************************************************************************/
void rrbb_set_audio_level (rrbb_t b, alevel_t alevel)
{
assert (b != NULL);
assert (b->magic1 == MAGIC1);
assert (b->magic2 == MAGIC2);
b->alevel = alevel;
}
/***********************************************************************************
*
* Name: rrbb_get_audio_level
*
* Purpose: Get audio level at time the frame was received.
*
* Inputs: b Handle for bit array.
*
***********************************************************************************/
alevel_t rrbb_get_audio_level (rrbb_t b)
{
assert (b != NULL);
assert (b->magic1 == MAGIC1);
assert (b->magic2 == MAGIC2);
return (b->alevel);
}
/***********************************************************************************
*
* Name: rrbb_get_is_scrambled
*
* Purpose: Find out if using scrambled data.
*
* Inputs: b Handle for bit array.
*
* Returns: True (for 9600 baud) or false (for slower AFSK).
*
***********************************************************************************/
int rrbb_get_is_scrambled (rrbb_t b)
{
assert (b != NULL);
assert (b->magic1 == MAGIC1);
assert (b->magic2 == MAGIC2);
return (b->is_scrambled);
}
/***********************************************************************************
*
* Name: rrbb_get_descram_state
*
* Purpose: Get data descrambler state before first data bit of frame.
*
* Inputs: b Handle for bit array.
*
***********************************************************************************/
int rrbb_get_descram_state (rrbb_t b)
{
assert (b != NULL);
assert (b->magic1 == MAGIC1);
assert (b->magic2 == MAGIC2);
return (b->descram_state);
}
/***********************************************************************************
*
* Name: rrbb_get_prev_descram
*
* Purpose: Get previous descrambled bit before first data bit of frame.
*
* Inputs: b Handle for bit array.
*
***********************************************************************************/
int rrbb_get_prev_descram (rrbb_t b)
{
assert (b != NULL);
assert (b->magic1 == MAGIC1);
assert (b->magic2 == MAGIC2);
return (b->prev_descram);
}
/* end rrbb.c */