forked from Schrolli91/multimon-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demod_flex.c
1312 lines (1107 loc) · 44.6 KB
/
demod_flex.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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* demod_flex.c
*
* Copyright 2004,2006,2010 Free Software Foundation, Inc.
* Copyright (C) 2015 Craig Shelley ([email protected])
*
* FLEX Radio Paging Decoder - Adapted from GNURadio for use with Multimon
*
* GNU Radio 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, or (at your option)
* any later version.
*
* GNU Radio 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 GNU Radio; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
/*
* Version 0.9.1v (10 Jan 2019)
* Modification (to this file) made by Rob0101
* Fixed marking messages with K,F,C - One case had a 'C' marked as a 'K'
* Version 0.9.0v (22 May 2018)
* Modification (to this file) made by Bruce Quinton ([email protected])
* - Addded Define at top of file to modify the way missed group messages are reported in the debug output (default is 1; report missed capcodes on the same line)
* REPORT_GROUP_CODES 1 // Report each cleared faulty group capcode : 0 = Each on a new line; 1 = All on the same line;
* Version 0.8.9 (20 Mar 2018)
* Modification (to this file) made by Bruce Quinton ([email protected])
* - Issue #101 created by bertinhollan (https://github.com/bertinholland): Bug flex: Wrong split up group message after a data corruption frame.
* - Added logic to the FIW decoding that checks for any 'Group Messages' and if the frame has past them remove the group message and log output
* - The following settings (at the top of this file, just under these comments) have changed from:
* PHASE_LOCKED_RATE 0.150
* PHASE_UNLOCKED_RATE 0.150
* these new settings appear to work better when attempting to locate the Sync lock in the message preamble.
* Version 0.8.8v (20 APR 2018)
* Modification (to this file) made by Bruce Quinton ([email protected])
* - Issue #101 created by bertinhollan (https://github.com/bertinholland): Bug flex: Wrong split up group message after a data corruption frame.
* Version 0.8.7v (11 APR 2018)
* Modification (to this file) made by Bruce Quinton ([email protected]) and Rob0101 (as seen on github: https://github.com/rob0101)
* - Issue *#95 created by rob0101: '-a FLEX dropping first character of some message on regular basis'
* - Implemented Rob0101's suggestion of K, F and C flags to indicate the message fragmentation:
* 'K' message is complete and O'K' to display to the world.
* 'F' message is a 'F'ragment and needs a 'C'ontinuation message to complete it. Message = Fragment + Continuation
* 'C' message is a 'C'ontinuation of another fragmented message
* Version 0.8.6v (18 Dec 2017)
* Modification (to this file) made by Bruce Quinton ([email protected]) on behalf of bertinhollan (https://github.com/bertinholland)
* - Issue #87 created by bertinhollan: Reported issue is that the flex period timeout was too short and therefore some group messages were not being processed correctly
* After some testing bertinhollan found that increasing the timeout period fixed the issue in his area. I have done further testing in my local
* area and found the change has not reduced my success rate. I think the timeout is a localisation setting and I have added "DEMOD_TIMEOUT"
* to the definitions in the top of this file (the default value is 100 bertinhollan's prefered value, changed up from 50)
* Version 0.8.5v (08 Sep 2017)
* Modification made by Bruce Quinton ([email protected])
* - Issue #78 - Found a problem in the length detection sequence, modified the if statement to ensure the message length is
* only checked for Aplha messages, the other types calculate thier length while decoding
* Version 0.8.4v (05 Sep 2017)
* Modification made by Bruce Quinton ([email protected])
* - Found a bug in the code that was not handling multiple group messages within the same frame,
* and the long address bit was being miss treated in the same cases. Both issue have been fixed but further testing will help.
* Version 0.8.3v (22 Jun 2017)
* Modification made by Bruce Quinton ([email protected])
* - I had previously tagged Group Messages as GPN message types,
* this was my own identification rather than a Flex standard type.
* Now that I have cleaned up all identified (so far) issues I have changed back to the correct Flex message type of ALN (Alpha).
* Version 0.8.2v (21 Jun 2017)
* Modification made by Bruce Quinton ([email protected])
* - Fixed group messaging capcode issue - modified the Capcode Array to be int64_t rather than int (I was incorrectly casting the long to an int)
* Version 0.8.1v (16 Jun 2017)
* Modification made by Bruce Quinton ([email protected])
* - Added Debugging to help track the group messaging issues
* - Improved Alpha output and removed several loops to improve CPU cycles
* Version 0.8v (08 Jun 2017)
* Modification made by Bruce Quinton ([email protected])
* - Added Group Messaging
* - Fixed Phase adjustments (phasing as part of Symbol identification)
* - Fixed Alpha numeric length adjustments to stop "Invalid Vector" errors
* - Fixed numeric message treatment
* - Fixed invalid identification of "unknown" messages
* - Added 3200 2 fsk identification to all more message types to be processed (this was a big deal for NZ)
* - Changed uint to int variables
*
*/
/* ---------------------------------------------------------------------- */
#include "multimon.h"
#include "filter.h"
#include "BCHCode.h"
#include <math.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
/* ---------------------------------------------------------------------- */
#define FREQ_SAMP 22050
#define FILTLEN 1
#define REPORT_GROUP_CODES 1 // Report each cleared faulty group capcode : 0 = Each on a new line; 1 = All on the same line;
#define FLEX_SYNC_MARKER 0xA6C6AAAAul // Synchronisation code marker for FLEX
#define SLICE_THRESHOLD 0.667 // For 4 level code, levels 0 and 3 have 3 times the amplitude of levels 1 and 2, so quantise at 2/3
#define DC_OFFSET_FILTER 0.010 // DC Offset removal IIR filter response (seconds)
#define PHASE_LOCKED_RATE 0.045 // Correction factor for locked state
#define PHASE_UNLOCKED_RATE 0.050 // Correction factor for unlocked state
#define LOCK_LEN 24 // Number of symbols to check for phase locking (max 32)
#define IDLE_THRESHOLD 0 // Number of idle codewords allowed in data section
#define CAPCODES_INDEX 0
#define DEMOD_TIMEOUT 100 // Maximum number of periods with no zero crossings before we decide that the system is not longer within a Timing lock.
enum Flex_PageTypeEnum {
FLEX_PAGETYPE_SECURE,
FLEX_PAGETYPE_SHORT_INSTRUCTION,
FLEX_PAGETYPE_TONE,
FLEX_PAGETYPE_STANDARD_NUMERIC,
FLEX_PAGETYPE_SPECIAL_NUMERIC,
FLEX_PAGETYPE_ALPHANUMERIC,
FLEX_PAGETYPE_BINARY,
FLEX_PAGETYPE_NUMBERED_NUMERIC
};
enum Flex_StateEnum {
FLEX_STATE_SYNC1,
FLEX_STATE_FIW,
FLEX_STATE_SYNC2,
FLEX_STATE_DATA
};
struct Flex_Demodulator {
unsigned int sample_freq;
double sample_last;
int locked;
int phase;
unsigned int sample_count;
unsigned int symbol_count;
double envelope_sum;
int envelope_count;
uint64_t lock_buf;
int symcount[4];
int timeout;
int nonconsec;
unsigned int baud; // Current baud rate
};
struct Flex_GroupHandler {
int64_t GroupCodes[17][1000];
int GroupCycle[17];
int GroupFrame[17];
};
struct Flex_Modulation {
double symbol_rate;
double envelope;
double zero;
};
struct Flex_State {
unsigned int sync2_count;
unsigned int data_count;
unsigned int fiwcount;
enum Flex_StateEnum Current;
enum Flex_StateEnum Previous;
};
struct Flex_Sync {
unsigned int sync; // Outer synchronization code
unsigned int baud; // Baudrate of SYNC2 and DATA
unsigned int levels; // FSK encoding of SYNC2 and DATA
unsigned int polarity; // 0=Positive (Normal) 1=Negative (Inverted)
uint64_t syncbuf;
};
struct Flex_FIW {
unsigned int rawdata;
unsigned int checksum;
unsigned int cycleno;
unsigned int frameno;
unsigned int fix3;
};
struct Flex_Phase {
unsigned int buf[88];
int idle_count;
};
struct Flex_Data {
int phase_toggle;
unsigned int data_bit_counter;
struct Flex_Phase PhaseA;
struct Flex_Phase PhaseB;
struct Flex_Phase PhaseC;
struct Flex_Phase PhaseD;
};
struct Flex_Decode {
enum Flex_PageTypeEnum type;
int long_address;
int64_t capcode;
struct BCHCode * BCHCode;
};
struct Flex {
struct Flex_Demodulator Demodulator;
struct Flex_Modulation Modulation;
struct Flex_State State;
struct Flex_Sync Sync;
struct Flex_FIW FIW;
struct Flex_Data Data;
struct Flex_Decode Decode;
struct Flex_GroupHandler GroupHandler;
};
int is_alphanumeric_page(struct Flex * flex) {
if (flex==NULL) return 0;
return (flex->Decode.type == FLEX_PAGETYPE_ALPHANUMERIC ||
flex->Decode.type == FLEX_PAGETYPE_SECURE);
}
int is_numeric_page(struct Flex * flex) {
if (flex==NULL) return 0;
return (flex->Decode.type == FLEX_PAGETYPE_STANDARD_NUMERIC ||
flex->Decode.type == FLEX_PAGETYPE_SPECIAL_NUMERIC ||
flex->Decode.type == FLEX_PAGETYPE_NUMBERED_NUMERIC);
}
int is_tone_page(struct Flex * flex) {
if (flex==NULL) return 0;
return (flex->Decode.type == FLEX_PAGETYPE_TONE);
}
unsigned int count_bits(struct Flex * flex, unsigned int data) {
if (flex==NULL) return 0;
#ifdef USE_BUILTIN_POPCOUNT
return __builtin_popcount(data);
#else
unsigned int n = (data >> 1) & 0x77777777;
data = data - n;
n = (n >> 1) & 0x77777777;
data = data - n;
n = (n >> 1) & 0x77777777;
data = data - n;
data = (data + (data >> 4)) & 0x0f0f0f0f;
data = data * 0x01010101;
return data >> 24;
#endif
}
static int bch3121_fix_errors(struct Flex * flex, uint32_t * data_to_fix, char PhaseNo) {
if (flex==NULL) return -1;
int i=0;
int recd[31];
/*Convert the data pattern into an array of coefficients*/
unsigned int data=*data_to_fix;
for (i=0; i<31; i++) {
recd[i] = (data>>30)&1;
data<<=1;
}
/*Decode and correct the coefficients*/
int decode_error=BCHCode_Decode(flex->Decode.BCHCode, recd);
/*Decode successful?*/
if (!decode_error) {
/*Convert the coefficient array back to a bit pattern*/
data=0;
for (i=0; i<31; i++) {
data<<=1;
data|=recd[i];
}
/*Count the number of fixed errors*/
int fixed=count_bits(flex, (*data_to_fix & 0x7FFFFFFF) ^ data);
if (fixed>0) {
verbprintf(3, "FLEX: Phase %c Fixed %i errors @ 0x%08x (0x%08x -> 0x%08x)\n", PhaseNo, fixed, (*data_to_fix&0x7FFFFFFF) ^ data, (*data_to_fix&0x7FFFFFFF), data );
}
/*Write the fixed data back to the caller*/
*data_to_fix=data;
} else {
verbprintf(3, "FLEX: Phase %c Data corruption - Unable to fix errors.\n", PhaseNo);
}
return decode_error;
}
static unsigned int flex_sync_check(struct Flex * flex, uint64_t buf) {
if (flex==NULL) return 0;
// 64-bit FLEX sync code:
// AAAA:BBBBBBBB:CCCC
//
// Where BBBBBBBB is always 0xA6C6AAAA
// and AAAA^CCCC is 0xFFFF
//
// Specific values of AAAA determine what bps and encoding the
// packet is beyond the frame information word
//
// First we match on the marker field with a hamming distance < 4
// Then we match on the outer code with a hamming distance < 4
unsigned int marker = (buf & 0x0000FFFFFFFF0000ULL) >> 16;
unsigned short codehigh = (buf & 0xFFFF000000000000ULL) >> 48;
unsigned short codelow = ~(buf & 0x000000000000FFFFULL);
int retval=0;
if (count_bits(flex, marker ^ FLEX_SYNC_MARKER) < 4 && count_bits(flex, codelow ^ codehigh) < 4 ) {
retval=codehigh;
} else {
retval=0;
}
return retval;
}
static unsigned int flex_sync(struct Flex * flex, unsigned char sym) {
if (flex==NULL) return 0;
int retval=0;
flex->Sync.syncbuf = (flex->Sync.syncbuf << 1) | ((sym < 2)?1:0);
retval=flex_sync_check(flex, flex->Sync.syncbuf);
if (retval!=0) {
flex->Sync.polarity=0;
} else {
/*If a positive sync pattern was not found, look for a negative (inverted) one*/
retval=flex_sync_check(flex, ~flex->Sync.syncbuf);
if (retval!=0) {
flex->Sync.polarity=1;
}
}
return retval;
}
static void decode_mode(struct Flex * flex, unsigned int sync_code) {
if (flex==NULL) return;
struct {
int sync;
unsigned int baud;
unsigned int levels;
} flex_modes[] = {
{ 0x870C, 1600, 2 },
{ 0xB068, 1600, 4 },
{ 0x7B18, 3200, 2 },
{ 0xDEA0, 3200, 4 },
{ 0x4C7C, 3200, 4 },
{0,0,0}
};
int x=0;
int i=0;
for (i=0; flex_modes[i].sync!=0; i++) {
if (count_bits(flex, flex_modes[i].sync ^ sync_code) < 4) {
flex->Sync.sync = sync_code;
flex->Sync.baud = flex_modes[i].baud;
flex->Sync.levels = flex_modes[i].levels;
x = 1;
break;
}
}
if(x==0){
verbprintf(3, "FLEX: Sync Code not found, defaulting to 1600bps 2FSK\n");
}
}
static void read_2fsk(struct Flex * flex, unsigned int sym, unsigned int * dat) {
if (flex==NULL) return;
*dat = (*dat >> 1) | ((sym > 1)?0x80000000:0);
}
static int decode_fiw(struct Flex * flex) {
if (flex==NULL) return -1;
unsigned int fiw = flex->FIW.rawdata;
int decode_error = bch3121_fix_errors(flex, &fiw, 'F');
if (decode_error) {
verbprintf(3, "FLEX: Unable to decode FIW, too much data corruption\n");
return 1;
}
// The only relevant bits in the FIW word for the purpose of this function
// are those masked by 0x001FFFFF.
flex->FIW.checksum = fiw & 0xF;
flex->FIW.cycleno = (fiw >> 4) & 0xF;
flex->FIW.frameno = (fiw >> 8) & 0x7F;
flex->FIW.fix3 = (fiw >> 15) & 0x3F;
unsigned int checksum = (fiw & 0xF);
checksum += ((fiw >> 4) & 0xF);
checksum += ((fiw >> 8) & 0xF);
checksum += ((fiw >> 12) & 0xF);
checksum += ((fiw >> 16) & 0xF);
checksum += ((fiw >> 20) & 0x01);
checksum &= 0xF;
if (checksum == 0xF) {
int timeseconds = flex->FIW.cycleno*4*60 + flex->FIW.frameno*4*60/128;
verbprintf(2, "FLEX: FrameInfoWord: cycleno=%02i frameno=%03i fix3=0x%02x time=%02i:%02i\n",
flex->FIW.cycleno,
flex->FIW.frameno,
flex->FIW.fix3,
timeseconds/60,
timeseconds%60);
// Lets check the FrameNo against the expected group message frames, if we have 'Missed a group message' tell the user and clear the Cap Codes
for(int g = 0; g < 17 ;g++)
{
// Do we have a group message pending for this groupbit?
if(flex->GroupHandler.GroupFrame[g] >= 0)
{
int Reset = 0;
verbprintf(4, "Flex: GroupBit %i, FrameNo: %i, Cycle No: %i target Cycle No: %i\n", g, flex->GroupHandler.GroupFrame[g], flex->GroupHandler.GroupCycle[g], (int)flex->FIW.cycleno);
// Now lets check if its expected in this frame..
if((int)flex->FIW.cycleno == flex->GroupHandler.GroupCycle[g])
{
if(flex->GroupHandler.GroupFrame[g] < (int)flex->FIW.frameno)
{
Reset = 1;
}
}
// Check if we should have sent a group message in the previous cycle
else if(flex->FIW.cycleno == 0)
{
if(flex->GroupHandler.GroupCycle[g] == 15)
{
Reset = 1;
}
}
// If we are waiting for the cycle to roll over then move onto the next for loop item
else if(flex->FIW.cycleno == 15 && flex->GroupHandler.GroupCycle[g] == 0)
{
continue;
}
// Otherwise if the target cycle is less than the current cycle, reset the data
else if(flex->GroupHandler.GroupCycle[g] < (int)flex->FIW.cycleno)
{
Reset = 1;
}
if(Reset == 1)
{
int endpoint = flex->GroupHandler.GroupCodes[g][CAPCODES_INDEX];
if(REPORT_GROUP_CODES > 0)
{
verbprintf(3,"FLEX: Group messages seem to have been missed; Groupbit: %i; Total Capcodes: %i; Clearing Data; Capcodes: ", g, endpoint);
}
for(int capIndex = 1; capIndex <= endpoint; capIndex++)
{
if(REPORT_GROUP_CODES == 0)
{
verbprintf(3,"FLEX: Group messages seem to have been missed; Groupbit: %i; Clearing data; Capcode: [%09lld]\n", g, flex->GroupHandler.GroupCodes[g][capIndex]);
}
else
{
if(capIndex > 1)
{
verbprintf(3,",");
}
verbprintf(3,"[%09lld]", flex->GroupHandler.GroupCodes[g][capIndex]);
}
}
if(REPORT_GROUP_CODES > 0)
{
verbprintf(3,"\n");
}
// reset the value
flex->GroupHandler.GroupCodes[g][CAPCODES_INDEX] = 0;
flex->GroupHandler.GroupFrame[g] = -1;
flex->GroupHandler.GroupCycle[g] = -1;
}
}
}
return 0;
} else {
verbprintf(3, "FLEX: Bad Checksum 0x%x\n", checksum);
return 1;
}
}
static void parse_alphanumeric(struct Flex * flex, unsigned int * phaseptr, char PhaseNo, int mw1, int mw2, int flex_groupmessage) {
if (flex==NULL) return;
verbprintf(3, "FLEX: Parse Alpha Numeric\n");
int i;
time_t now=time(NULL);
struct tm * gmt=gmtime(&now);
// char buf[1024], *message;
char message[1024];
int currentChar = 0;
char frag_flag = '?';
int frag = (phaseptr[mw1] >> 11) & 0x03;
int cont = (phaseptr[mw1] >> 0x0A) & 0x01;
if (cont == 0 && frag == 3) frag_flag = 'K'; // complete, ready to send
if (cont == 0 && frag != 3) frag_flag = 'C'; // incomplete until appended to 1 or more 'F's
if (cont == 1 ) frag_flag = 'F'; // incomplete until a 'C' fragment is appended
mw1++;
for (i = mw1; i <= mw2; i++) {
unsigned int dw = phaseptr[i];
unsigned char ch;
if (i > mw1 || frag != 0x03) {
ch = dw & 0x7F;
if (ch != 0x03) {
message[currentChar] = ch;
currentChar++;
}
}
ch = (dw >> 7) & 0x7F;
if (ch != 0x03) {
message[currentChar] = ch;
currentChar++;
}
ch = (dw >> 14) & 0x7F;
if (ch != 0x03) {
message[currentChar] = ch;
currentChar++;
}
}
message[currentChar] = '\0';
// message = '\0';
verbprintf(0, "FLEX: %04i-%02i-%02i %02i:%02i:%02i %i/%i/%c/%c %02i.%03i [%09lld] ALN ",
gmt->tm_year+1900, gmt->tm_mon+1, gmt->tm_mday, gmt->tm_hour, gmt->tm_min, gmt->tm_sec,
flex->Sync.baud, flex->Sync.levels, frag_flag, PhaseNo, flex->FIW.cycleno, flex->FIW.frameno, flex->Decode.capcode);
verbprintf(0, "%s\n", message);
if(flex_groupmessage == 1) {
int groupbit = flex->Decode.capcode-2029568;
if(groupbit < 0) return;
int endpoint = flex->GroupHandler.GroupCodes[groupbit][CAPCODES_INDEX];
for(int g = 1; g <= endpoint;g++)
{
verbprintf(1, "FLEX Group message output: Groupbit: %i Total Capcodes; %i; index %i; Capcode: [%09lld]\n", groupbit, endpoint, g, flex->GroupHandler.GroupCodes[groupbit][g]);
verbprintf(0, "FLEX: %04i-%02i-%02i %02i:%02i:%02i %i/%i/%c/%c %02i.%03i [%09lld] ALN ", gmt->tm_year+1900, gmt->tm_mon+1, gmt->tm_mday, gmt->tm_hour, gmt->tm_min, gmt->tm_sec,
flex->Sync.baud, flex->Sync.levels, frag_flag, PhaseNo, flex->FIW.cycleno, flex->FIW.frameno, flex->GroupHandler.GroupCodes[groupbit][g]);
verbprintf(0, "%s\n", message);
}
// reset the value
flex->GroupHandler.GroupCodes[groupbit][CAPCODES_INDEX] = 0;
flex->GroupHandler.GroupFrame[groupbit] = -1;
flex->GroupHandler.GroupCycle[groupbit] = -1;
}
}
static void parse_numeric(struct Flex * flex, unsigned int * phaseptr, char PhaseNo, int j) {
if (flex==NULL) return;
unsigned const char flex_bcd[17] = "0123456789 U -][";
int w1 = phaseptr[j] >> 7;
int w2 = w1 >> 7;
w1 = w1 & 0x7f;
w2 = (w2 & 0x07) + w1; // numeric message is 7 words max
time_t now=time(NULL);
struct tm * gmt=gmtime(&now);
verbprintf(0, "FLEX: %04i-%02i-%02i %02i:%02i:%02i %i/%i/%c %02i.%03i [%09lld] NUM ", gmt->tm_year+1900, gmt->tm_mon+1, gmt->tm_mday, gmt->tm_hour, gmt->tm_min, gmt->tm_sec,
flex->Sync.baud, flex->Sync.levels, PhaseNo, flex->FIW.cycleno, flex->FIW.frameno, flex->Decode.capcode);
// Get first dataword from message field or from second
// vector word if long address
int dw;
if(!flex->Decode.long_address) {
dw = phaseptr[w1];
w1++;
w2++;
} else {
dw = phaseptr[j+1];
}
unsigned char digit = 0;
int count = 4;
if(flex->Decode.type == FLEX_PAGETYPE_NUMBERED_NUMERIC) {
count += 10; // Skip 10 header bits for numbered numeric pages
} else {
count += 2; // Otherwise skip 2
}
int i;
for(i = w1; i <= w2; i++) {
int k;
for(k = 0; k < 21; k++) {
// Shift LSB from data word into digit
digit = (digit >> 1) & 0x0F;
if(dw & 0x01) {
digit ^= 0x08;
}
dw >>= 1;
if(--count == 0) {
verbprintf(0, "%c", flex_bcd[digit]);
count = 4;
}
}
dw = phaseptr[i];
}
verbprintf(0, "\n");
}
static void parse_tone_only(struct Flex * flex, char PhaseNo) {
if (flex==NULL) return;
time_t now=time(NULL);
struct tm * gmt=gmtime(&now);
verbprintf(0, "FLEX: %04i-%02i-%02i %02i:%02i:%02i %i/%i/%c %02i.%03i [%09lld] TON\n", gmt->tm_year+1900, gmt->tm_mon+1, gmt->tm_mday, gmt->tm_hour, gmt->tm_min, gmt->tm_sec,
flex->Sync.baud, flex->Sync.levels, PhaseNo, flex->FIW.cycleno, flex->FIW.frameno, flex->Decode.capcode);
}
static void parse_unknown(struct Flex * flex, unsigned int * phaseptr, char PhaseNo, int mw1, int mw2) {
if (flex==NULL) return;
time_t now=time(NULL);
struct tm * gmt=gmtime(&now);
verbprintf(0, "FLEX: %04i-%02i-%02i %02i:%02i:%02i %i/%i/%c %02i.%03i [%09lld] UNK", gmt->tm_year+1900, gmt->tm_mon+1, gmt->tm_mday, gmt->tm_hour, gmt->tm_min, gmt->tm_sec,
flex->Sync.baud, flex->Sync.levels, PhaseNo, flex->FIW.cycleno, flex->FIW.frameno, flex->Decode.capcode);
int i;
for (i = mw1; i <= mw2; i++) {
verbprintf(0, " %08x", phaseptr[i]);
}
verbprintf(0, "\n");
}
//static void parse_capcode(struct Flex * flex, uint32_t aw1, uint32_t aw2) {
static void parse_capcode(struct Flex * flex, uint32_t aw1) {
if (flex==NULL) return;
flex->Decode.long_address = (aw1 < 0x008001L) ||
(aw1 > 0x1E0000L) ||
(aw1 > 0x1E7FFEL);
///if (flex->Decode.long_address)
// flex->Decode.capcode = (int64_t)aw1+((int64_t)(aw2^0x001FFFFFul)<<15)+0x1F9000ull; // Don't ask
//else
flex->Decode.capcode = aw1-0x8000;
}
static void decode_phase(struct Flex * flex, char PhaseNo) {
if (flex==NULL) return;
uint32_t *phaseptr=NULL;
int i, j;
switch (PhaseNo) {
case 'A': phaseptr=flex->Data.PhaseA.buf; break;
case 'B': phaseptr=flex->Data.PhaseB.buf; break;
case 'C': phaseptr=flex->Data.PhaseC.buf; break;
case 'D': phaseptr=flex->Data.PhaseD.buf; break;
}
for (i=0; i<88; i++) {
int decode_error=bch3121_fix_errors(flex, &phaseptr[i], PhaseNo);
if (decode_error) {
verbprintf(3, "FLEX: Garbled message at block %i\n", i);
// If the previous frame was a short message then we need to Null out the Group Message pointer
// this issue and sugested resolution was presented by 'bertinholland'
return;
}
/*Extract just the message bits*/
phaseptr[i]&=0x001FFFFF;
}
// Block information word is the first data word in frame
uint32_t biw = phaseptr[0];
// Nothing to see here, please move along
if (biw == 0 || biw == 0x001FFFFF) {
verbprintf(3, "FLEX: Nothing to see here, please move along\n");
return;
}
// Vector start index is bits 15-10
// Address start address is bits 9-8, plus one for offset
int voffset = (biw >> 10) & 0x3f;
int aoffset = ((biw >> 8) & 0x03) + 1;
verbprintf(3, "FLEX: BlockInfoWord: (Phase %c) BIW:%08X AW:%02i-%02i (%i pages)\n", PhaseNo, biw, aoffset, voffset, voffset-aoffset);
int flex_groupmessage = 0;
// Iterate through pages and dispatch to appropriate handler
for (i = aoffset; i < voffset; i++) {
j = voffset+i-aoffset; // Start of vector field for address @ i
if (phaseptr[i] == 0x00000000 ||
phaseptr[i] == 0x001FFFFF) {
verbprintf(3, "FLEX: Idle codewords, invalid address\n");
continue; // Idle codewords, invalid address
}
parse_capcode(flex, phaseptr[i]);
// parse_capcode(flex, phaseptr[i], phaseptr[i+1]); // Older version maybe still needed so I'm not removing it (yet)
if (flex->Decode.long_address)
{
verbprintf(4, "FLEX: Found 'Long Address' bit, ignoring as I think this is handled incorrectly at the moment issue#79\n");
// i++;
}
if ((flex->Decode.capcode >= 2029568) && (flex->Decode.capcode <= 2029583)) {
flex_groupmessage = 1;
}
if (flex->Decode.capcode > 4297068542ll || flex->Decode.capcode < 0) { // Invalid address (by spec, maximum address)
verbprintf(3, "FLEX: Invalid address\n");
continue;
}
verbprintf(3, "FLEX: CAPCODE:%016lx\n", flex->Decode.capcode);
// Parse vector information word for address @ offset 'i'
uint32_t viw = phaseptr[j];
flex->Decode.type = ((viw >> 4) & 0x00000007);
int mw1 = (viw >> 7) & 0x00000007F;
int len = (viw >> 14) & 0x0000007F;
int w1 = (int)(viw >> 7);
int w2 = w1 >> 7;
w1 = w1 & 0x7f;
w2 = (w2 & 0x7f) + w1 - 1;
// int wL = w2 - w1;
if (flex->Decode.type == FLEX_PAGETYPE_SHORT_INSTRUCTION)
{
// if (flex_groupmessage == 1) continue;
unsigned int iAssignedFrame = (int)((viw >> 10) & 0x7f); // Frame with groupmessage
int groupbit = (int)((viw >> 17) & 0x7f); // Listen to this groupcode
////////#############################################################################
////////#############################################################################
flex->GroupHandler.GroupCodes[groupbit][CAPCODES_INDEX]++;
int CapcodePlacement = flex->GroupHandler.GroupCodes[groupbit][CAPCODES_INDEX];
verbprintf(1, "FLEX: Found Short Instruction, Group bit: %i capcodes in group so far %i, adding Capcode: [%09lld]\n", groupbit, CapcodePlacement, flex->Decode.capcode);
flex->GroupHandler.GroupCodes[groupbit][CapcodePlacement] = flex->Decode.capcode;
flex->GroupHandler.GroupFrame[groupbit] = iAssignedFrame;
// Ok, so the cycle and frame can be used to make sure we haven't missed the message frame.
// but the cycle is 0 - 15 and the frame is 0 - 127
if(iAssignedFrame > flex->FIW.frameno)
{
flex->GroupHandler.GroupCycle[groupbit] = (int)flex->FIW.cycleno;
verbprintf(4, "FLEX: Message frame is in this cycle: %i\n", flex->GroupHandler.GroupCycle[groupbit]);
}
else
{
if(flex->FIW.cycleno == 15)
{
flex->GroupHandler.GroupCycle[groupbit] = 0;
}
else
{
flex->GroupHandler.GroupCycle[groupbit] = (int)flex->FIW.cycleno++;
}
verbprintf(4, "FLEX: Message frame is in the next cycle: %i\n", flex->GroupHandler.GroupCycle[groupbit]);
}
// Nothing else to do with this word.. move on!!
continue;
}
int mw2 = mw1+(len - 1);
if (mw1 == 0 && mw2 == 0){
verbprintf(3, "FLEX: Invalid VIW\n");
continue; // Invalid VIW
}
if (is_tone_page(flex))
mw1 = mw2 = 0;
// Check if this is an alpha message
if (is_alphanumeric_page(flex)) {
if (mw1 > 87 || mw2 > 87){
verbprintf(3, "FLEX: Invalid Offsets\n");
continue; // Invalid offsets
}
parse_alphanumeric(flex, phaseptr, PhaseNo, mw1, mw2, flex_groupmessage);
}
else if (is_numeric_page(flex))
parse_numeric(flex, phaseptr, PhaseNo, j);
else if (is_tone_page(flex))
parse_tone_only(flex, PhaseNo);
else
parse_unknown(flex, phaseptr, PhaseNo, mw1, mw2);
}
}
static void clear_phase_data(struct Flex * flex) {
if (flex==NULL) return;
int i;
for (i=0; i<88; i++) {
flex->Data.PhaseA.buf[i]=0;
flex->Data.PhaseB.buf[i]=0;
flex->Data.PhaseC.buf[i]=0;
flex->Data.PhaseD.buf[i]=0;
}
flex->Data.PhaseA.idle_count=0;
flex->Data.PhaseB.idle_count=0;
flex->Data.PhaseC.idle_count=0;
flex->Data.PhaseD.idle_count=0;
flex->Data.phase_toggle=0;
flex->Data.data_bit_counter=0;
}
static void decode_data(struct Flex * flex) {
if (flex==NULL) return;
if (flex->Sync.baud == 1600) {
if (flex->Sync.levels==2) {
decode_phase(flex, 'A');
} else {
decode_phase(flex, 'A');
decode_phase(flex, 'B');
}
} else {
if (flex->Sync.levels==2) {
decode_phase(flex, 'A');
decode_phase(flex, 'C');
} else {
decode_phase(flex, 'A');
decode_phase(flex, 'B');
decode_phase(flex, 'C');
decode_phase(flex, 'D');
}
}
}
static int read_data(struct Flex * flex, unsigned char sym) {
if (flex==NULL) return -1;
// Here is where we output a 1 or 0 on each phase according
// to current FLEX mode and symbol value. Unassigned phases
// are zero from the enter_idle() initialization.
//
// FLEX can transmit the data portion of the frame at either
// 1600 bps or 3200 bps, and can use either two- or four-level
// FSK encoding.
//
// At 1600 bps, 2-level, a single "phase" is transmitted with bit
// value '0' using level '3' and bit value '1' using level '0'.
//
// At 1600 bps, 4-level, a second "phase" is transmitted, and the
// di-bits are encoded with a gray code:
//
// Symbol Phase 1 Phase 2
// ------ ------- -------
// 0 1 1
// 1 1 0
// 2 0 0
// 3 0 1
//
// At 1600 bps, 4-level, these are called PHASE A and PHASE B.
//
// At 3200 bps, the same 1 or 2 bit encoding occurs, except that
// additionally two streams are interleaved on alternating symbols.
// Thus, PHASE A (and PHASE B if 4-level) are decoded on one symbol,
// then PHASE C (and PHASE D if 4-level) are decoded on the next.
int bit_a=0; //Received data bit for Phase A
int bit_b=0; //Received data bit for Phase B
bit_a = (sym > 1);
if (flex->Sync.levels == 4) {
bit_b = (sym == 1) || (sym == 2);
}
if (flex->Sync.baud == 1600) {
flex->Data.phase_toggle=0;
}
//By making the index scan the data words in this way, the data is deinterlaced
//Bits 0, 1, and 2 map straight through to give a 0-7 sequence that repeats 32 times before moving to 8-15 repeating 32 times
unsigned int idx= ((flex->Data.data_bit_counter>>5)&0xFFF8) | (flex->Data.data_bit_counter&0x0007);
if (flex->Data.phase_toggle==0) {
flex->Data.PhaseA.buf[idx] = (flex->Data.PhaseA.buf[idx]>>1) | (bit_a?(0x80000000):0);
flex->Data.PhaseB.buf[idx] = (flex->Data.PhaseB.buf[idx]>>1) | (bit_b?(0x80000000):0);
flex->Data.phase_toggle=1;
if ((flex->Data.data_bit_counter & 0xFF) == 0xFF) {
if (flex->Data.PhaseA.buf[idx] == 0x00000000 || flex->Data.PhaseA.buf[idx] == 0xffffffff) flex->Data.PhaseA.idle_count++;
if (flex->Data.PhaseB.buf[idx] == 0x00000000 || flex->Data.PhaseB.buf[idx] == 0xffffffff) flex->Data.PhaseB.idle_count++;
}
} else {
flex->Data.PhaseC.buf[idx] = (flex->Data.PhaseC.buf[idx]>>1) | (bit_a?(0x80000000):0);
flex->Data.PhaseD.buf[idx] = (flex->Data.PhaseD.buf[idx]>>1) | (bit_b?(0x80000000):0);
flex->Data.phase_toggle=0;
if ((flex->Data.data_bit_counter & 0xFF) == 0xFF) {
if (flex->Data.PhaseC.buf[idx] == 0x00000000 || flex->Data.PhaseC.buf[idx] == 0xffffffff) flex->Data.PhaseC.idle_count++;
if (flex->Data.PhaseD.buf[idx] == 0x00000000 || flex->Data.PhaseD.buf[idx] == 0xffffffff) flex->Data.PhaseD.idle_count++;
}
}
if (flex->Sync.baud == 1600 || flex->Data.phase_toggle==0) {
flex->Data.data_bit_counter++;
}
/*Report if all active phases have gone idle*/
int idle=0;
if (flex->Sync.baud == 1600) {
if (flex->Sync.levels==2) {
idle=(flex->Data.PhaseA.idle_count>IDLE_THRESHOLD);
} else {
idle=((flex->Data.PhaseA.idle_count>IDLE_THRESHOLD) && (flex->Data.PhaseB.idle_count>IDLE_THRESHOLD));
}
} else {
if (flex->Sync.levels==2) {
idle=((flex->Data.PhaseA.idle_count>IDLE_THRESHOLD) && (flex->Data.PhaseC.idle_count>IDLE_THRESHOLD));
} else {
idle=((flex->Data.PhaseA.idle_count>IDLE_THRESHOLD) && (flex->Data.PhaseB.idle_count>IDLE_THRESHOLD) && (flex->Data.PhaseC.idle_count>IDLE_THRESHOLD) && (flex->Data.PhaseD.idle_count>IDLE_THRESHOLD));
}
}
return idle;
}
static void report_state(struct Flex * flex) {
if (flex->State.Current != flex->State.Previous) {
flex->State.Previous = flex->State.Current;
char * state="Unknown";
switch (flex->State.Current) {
case FLEX_STATE_SYNC1:
state="SYNC1";
break;
case FLEX_STATE_FIW:
state="FIW";
break;
case FLEX_STATE_SYNC2:
state="SYNC2";
break;
case FLEX_STATE_DATA:
state="DATA";
break;
default:
break;
}
verbprintf(1, "FLEX: State: %s\n", state);
}
}
//Called for each received symbol
static void flex_sym(struct Flex * flex, unsigned char sym) {
if (flex==NULL) return;
/*If the signal has a negative polarity, the symbols must be inverted*/