-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRS92D.c
More file actions
1182 lines (1027 loc) · 37.3 KB
/
RS92D.c
File metadata and controls
1182 lines (1027 loc) · 37.3 KB
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
/*
* RS92D
* sync header: correlation/matched filter
* files: RS92D.c nav_gps_vel.c bch_ecc_mod.c bch_ecc_mod.h demod_mod.c demod_mod.h
* compile:
* (a)
* gcc -c demod_mod.c
* gcc -DINCLUDESTATIC RS92D.c demod_mod.o -lm -o RS92D
* (b)
* gcc -c demod_mod.c
* gcc -c bch_ecc_mod.c
* gcc RS92D.c demod_mod.o bch_ecc_mod.o -lm -o RS92D
* CYGWIN
* gcc -DCYGWIN RS92D.c demod_mod.o bch_ecc_mod.o -lm -O3 -o RS92D
*
* author: zilog80 code modified to RS92-D by diehardsk
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <time.h>
#ifdef CYGWIN
#include <fcntl.h> // cygwin: _setmode()
#include <io.h>
#endif
// optional JSON "version"
// (a) set global
// gcc -DVERSION_JSN [-I<inc_dir>] ...
#ifdef VERSION_JSN
#include "version_jsn.h"
#endif
// or
// (b) set local compiler option, e.g.
// gcc -DVER_JSN_STR=\"0.0.2\" ...
#include "demod_mod.h"
//#define INCLUDESTATIC 1
#ifdef INCLUDESTATIC
#include "bch_ecc_mod.c"
#else
#include "bch_ecc_mod.h"
#endif
typedef struct {
i8_t sub; // subframe printing
i8_t raw; // raw frames
i8_t ecn; // Reed-Solomon ECC Error Count
i8_t ptu; // PTU: measurements
i8_t inv; // inversion
i8_t aut;
i8_t aux; // aux/ozone
i8_t jsn; // JSON output
i8_t dbg; // debug
i8_t udp; // UDP output
i8_t arx; // auto_rx
i8_t dat; // date-time
} option_t;
static option_t option;
static RS_t RS;
typedef struct {
char *host;
char *port;
int sfd;
time_t ip_time;
} hostcfg_t;
static hostcfg_t hostcfg;
typedef struct {
int typ;
int msglen;
int msgpos;
int parpos;
int hdrlen;
int frmlen;
} rscfg_t;
static rscfg_t cfg_rs92 = { 92, 240-6-24, 6, 240-24, 6, 240};
static char *key[10], *val[10];
static char extracnt;
/* --- RS92 digital: 8N1 manchester --- */
#define BITS (1+8+1) // 10
#define FRAMESTART 6
#define FRAME_LEN 240
static char rs92_rawheader[] = //"10100110011001101001" //2A 8N1 Manchester encoded
//"10100110011001101001" //2A
"10100110011001101001" //2A
"10100110011001101001" //2A
"1010011001100110100110101010100110101001"; //2A 10
static ui8_t RS92_header_bytes[6] = { 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x10};
static ui8_t RS92D_padding[130] = {
0x34, 0x12, 0x78, 0x56, 0x12, 0x90, 0x00, 0x04, 0x01, 0x04, 0x02, 0x04, 0x03, 0x04, 0x04, 0x04,
0x05, 0x04, 0x06, 0x04, 0x07, 0x04, 0x08, 0x04, 0x09, 0x04, 0x0a, 0x04, 0x0b, 0x04, 0x0c, 0x04,
0x0d, 0x04, 0x0e, 0x04, 0x0f, 0x04, 0x10, 0x04, 0x11, 0x04, 0x12, 0x04, 0x13, 0x04, 0x14, 0x04,
0x15, 0x04, 0x16, 0x04, 0x17, 0x04, 0x18, 0x04, 0x19, 0x04, 0x1a, 0x04, 0x1b, 0x04, 0x1c, 0x04,
0x1d, 0x04, 0x1e, 0x04, 0x1f, 0x04, 0x20, 0x04, 0x21, 0x04, 0x22, 0x04, 0x23, 0x04, 0x24, 0x04,
0x25, 0x04, 0x26, 0x04, 0x27, 0x04, 0x28, 0x04, 0x29, 0x04, 0x2a, 0x04, 0x2b, 0x04, 0x2c, 0x04,
0x2d, 0x04, 0x2e, 0x04, 0x2f, 0x04, 0x30, 0x04, 0x31, 0x04, 0x32, 0x04, 0x33, 0x04, 0x34, 0x04,
0x35, 0x04, 0x36, 0x04, 0x37, 0x04, 0x38, 0x04, 0x39, 0x04, 0x3a, 0x04, 0x3b, 0x04, 0x3c, 0x04,
0x00, 0x00 };
typedef struct {
int frnr;
char id[11];
ui16_t conf_kt; // kill timer (sec)
int freq; // freq/kHz (RS92)
int jsn_freq; // freq/kHz (SDR)
ui32_t crc;
ui8_t frame[FRAME_LEN];
ui8_t cal_state[2];
ui8_t calfrms;
ui8_t calibytes[32*16];
ui8_t calfrchk[32];
float cal_f32[256];
float T;
float _RH;
float _P;
i32_t alt;
unsigned short aux[4];
} gpx_t;
static gpx_t *gpx, *gpx1_pointer, *gpx2_pointer;
/* ------------------------------------------------------------------------------------ */
#define BIT_RATE 4800
/* ------------------------------------------------------------------------------------ */
// manchester1 1->10,0->01: 1.bit
// manchester2 0->10,1->01: 2.bit
// RS92-SGP: 8N1 manchester2
static int bits2byte(char bits[]) {
int i, byteval=0, d=1;
//if (bits[0] != 0) return 0x100; // erasure?
//if (bits[9] != 1) return 0x100; // erasure?
for (i = 1; i <= 8; i++) { // little endian
/* for (i = 8; i > 1; i--) { // big endian */
if (bits[i] == 1) byteval += d;
else if (bits[i] == 0) byteval += 0;
d <<= 1;
}
return byteval;
}
/* ------------------------------------------------------------------------------------ */
#define crc_STAT (1<<0)
#define pos_FrameNb 0x08 // 2 byte
#define pos_SondeID 0x0C // 8 byte // oder: 0x0A, 10 byte?
#define pos_BitField0C 0x14 // 1 byte
#define pos_BitField0D 0x15 // 1 byte
#define pos_CalData 0x17 // 1 byte, counter 0x00..0x1f
#define pos_Calfreq 0x1A // 2 byte, calfr 0x00
#define crc_PTU (1<<1)
#define pos_PTU 0x2C // 24 byte
#define crc_AUX (1<<2)
#define pos_AUX 0x48 // 10 byte
#define pos_AuxData 0x4A // 8 byte
#define pos_PADD 0x56 // 130 bytes
#define BLOCK_CFG 0x6510 // frame[pos_FrameNb-2], frame[pos_FrameNb-1]
#define BLOCK_PTU 0x690C
//#define BLOCK_GPS 0x673D
#define BLOCK_AUX 0x6805
#define BLOCK_PADD 0xFF41
#define LEN_CFG (2*(BLOCK_CFG & 0xFF))
#define LEN_PTU (2*(BLOCK_PTU & 0xFF))
#define LEN_AUX (2*(BLOCK_AUX & 0xFF))
#define BYTE_TO_BINARY_PATTERN "%c%c%c%c %c%c%c%c"
#define BYTE_TO_BINARY(byte) \
(byte & 0x80 ? '1' : '0'), \
(byte & 0x40 ? '1' : '0'), \
(byte & 0x20 ? '1' : '0'), \
(byte & 0x10 ? '1' : '0'), \
(byte & 0x08 ? '1' : '0'), \
(byte & 0x04 ? '1' : '0'), \
(byte & 0x02 ? '1' : '0'), \
(byte & 0x01 ? '1' : '0')
static int send_UDP(char *message) {
struct addrinfo hints;
struct addrinfo *result, *rp;
int s;
size_t len;
double elap_minutes;
time_t time_now = time(NULL);
elap_minutes = difftime(time_now, hostcfg.ip_time)/60;
if (hostcfg.sfd == -1 || elap_minutes > 60) { //max once per hour
fprintf(stderr, "\nresolving IP.. ");
if (hostcfg.sfd != -1)
close(hostcfg.sfd);
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
hints.ai_flags = 0;
hints.ai_protocol = 0; /* Any protocol */
s = getaddrinfo(hostcfg.host, hostcfg.port, &hints, &result);
if (s != 0) {
fprintf(stderr, "getaddrinfo: %s", gai_strerror(s));
freeaddrinfo(result);
return -1;
}
for (rp = result; rp != NULL; rp = rp->ai_next) {
hostcfg.sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (hostcfg.sfd == -1)
continue;
if (connect(hostcfg.sfd, rp->ai_addr, rp->ai_addrlen) != -1)
break; /* Success */
close(hostcfg.sfd);
hostcfg.sfd = -1;
}
if (rp == NULL) { /* No address succeeded */
fprintf(stderr, "could not connect to host");
freeaddrinfo(result);
return -2;
}
freeaddrinfo(result);
hostcfg.ip_time = time(NULL);
fprintf(stderr, "success");
}
len = strlen(message);
if (write(hostcfg.sfd, message, len) != len) {
fprintf(stderr, "\nUnable to send UDP message");
return -3;
}
}
static int crc16(int start, int len) {
int crc16poly = 0x1021;
int rem = 0xFFFF, i, j;
int byte;
if (start+len >= FRAME_LEN) return -1;
for (i = 0; i < len; i++) {
byte = gpx->frame[start+i];
rem = rem ^ (byte << 8);
for (j = 0; j < 8; j++) {
if (rem & 0x8000) {
rem = (rem << 1) ^ crc16poly;
}
else {
rem = (rem << 1);
}
rem &= 0xFFFF;
}
}
return rem;
}
static int crc_fail(int start, int len) {
int crc_frame, crc;
crc_frame = gpx->frame[start + len] | (gpx->frame[start + len + 1] << 8);
crc = crc16(start, len);
if (crc_frame != crc)
return -1;
return 0;
}
static int get_FrameNb() {
int i;
unsigned byte;
ui8_t frnr_bytes[2];
int frnr;
for (i = 0; i < 2; i++) {
byte = gpx->frame[pos_FrameNb + i];
frnr_bytes[i] = byte;
}
frnr = frnr_bytes[0] + (frnr_bytes[1] << 8);
gpx->frnr = frnr;
return 0;
}
static int get_SondeID() {
int i;
unsigned byte;
char sondeid_bytes[10];
ui8_t calfr;
if (crc_fail(pos_FrameNb, LEN_CFG)) {
gpx->crc |= crc_STAT;
return -2;
}
for (i = 0; i < 8; i++) {
byte = gpx->frame[pos_SondeID + i];
if ((byte < 0x20) || (byte > 0x7E)) return -1;
sondeid_bytes[i] = byte;
}
sondeid_bytes[8] = '\0';
if ( strncmp(gpx->id, sondeid_bytes, 8) != 0 ) { //id doesn't match
//let's swap pointer between gpx1 <-> gpx2 datasets
gpx_t *original_pointer = gpx;
if (gpx == gpx1_pointer)
gpx = gpx2_pointer;
else gpx = gpx1_pointer;
//copy new data frame
memcpy(gpx->frame, original_pointer->frame, FRAME_LEN);
if ( strncmp(gpx->id, sondeid_bytes, 8) != 0 ) { //ID doesn't match the other dataset
memset(gpx->calibytes, 0, 32*16); //reset data (only not recently used dataset)
memset(gpx->calfrchk, 0, 32);
memset(gpx->cal_f32, 0, 256*4);
gpx->calfrms = 0;
gpx->T = -275.15f;
gpx->_RH = -1.0f;
gpx->_P = -1.0f;
gpx->alt = -10000;
gpx->freq = 0;
gpx->conf_kt = 0;
// new ID:
memcpy(gpx->id, sondeid_bytes, 8); //store new ID
}
}
memcpy(gpx->cal_state, gpx->frame+(pos_FrameNb + 12), 2);
calfr = gpx->frame[pos_CalData]; // 0..31
if (calfr < 32) {
if (gpx->calfrchk[calfr] == 0) // if we don't have this cal. fragment yet
{
for (i = 0; i < 16; i++) {
gpx->calibytes[calfr*16 + i] = gpx->frame[pos_CalData+1+i];
}
gpx->calfrchk[calfr] = 1;
}
}
if (gpx->calfrms < 32) {
gpx->calfrms = 0;
for (i = 0; i < 32; i++) gpx->calfrms += (gpx->calfrchk[i]>0);
}
if (gpx->calfrms == 32)
{
ui8_t xcal[66*5];
gpx->calfrms += 1;
for (int j = 0; j < 66*5; j++) {
xcal[j] = gpx->calibytes[0x40+j];
}
for (int j = 0; j < 66; j++) {
ui8_t idx = xcal[5*j];
ui8_t *dat = xcal+(5*j+1);
ui32_t le_dat32 = dat[0] | (dat[1]<<8) | (dat[2]<<16) | (dat[3]<<24);
float *pf32 = (float*)&le_dat32;
gpx->cal_f32[idx] = *pf32;
if (option.dbg)
{
if (idx/10 == 3 || idx/10 == 4 || idx/10 == 5)
{
printf(" %3d :", idx);
for (int i = 1; i < 5; i++) {
printf(" %02x", xcal[5*j+i]);
}
printf(" : %f", *pf32);
printf("\n");
}
}
}
}
return 0;
}
// ----------------------------------------------------------------------------------------------------
// PTU
// cf. Haeberli (2001),
// https://brmlab.cz/project/weathersonde/telemetry_decoding
//
static float poly5(float x, float *a) {
float p = 0.0;
p = ((((a[5]*x+a[4])*x+a[3])*x+a[2])*x+a[1])*x+a[0];
return p;
}
static float nu(float t, float t0, float y0) {
// t=1/f2-1/f , t0=1/f2-1/f1 , 1/freq=meas24
float y = t / t0;
return 1.0f / (y0 - y);
}
static int get_Meas() {
ui32_t temp, pres, hum1, hum2, ref1, ref2, ref3, ref4;
ui8_t *meas24 = gpx->frame+pos_PTU;
float T, U1, U2, _P, _rh, x;
temp = meas24[ 0] | (meas24[ 1]<<8) | (meas24[ 2]<<16); // ch1
hum1 = meas24[ 3] | (meas24[ 4]<<8) | (meas24[ 5]<<16); // ch2
hum2 = meas24[ 6] | (meas24[ 7]<<8) | (meas24[ 8]<<16); // ch3
ref1 = meas24[ 9] | (meas24[10]<<8) | (meas24[11]<<16); // ch4
ref2 = meas24[12] | (meas24[13]<<8) | (meas24[14]<<16); // ch5
pres = meas24[15] | (meas24[16]<<8) | (meas24[17]<<16); // ch6
ref3 = meas24[18] | (meas24[19]<<8) | (meas24[20]<<16); // ch7
ref4 = meas24[21] | (meas24[22]<<8) | (meas24[23]<<16); // ch8
if (gpx->calfrms > 0x20)
{
// Temperature
x = nu( (float)(ref1 - temp), (float)(ref1 - ref4), gpx->cal_f32[37] );
T = poly5(x, gpx->cal_f32+30);
if (T > -120.0f && T < 80.0f) gpx->T = T;
else gpx->T = -273.15f;
// rel. Humidity (ref3 or ref4 ?)
x = nu( (float)(ref1 - hum1), (float)(ref1 - ref3), gpx->cal_f32[47] );
U1 = poly5(x, gpx->cal_f32+40); // c[44]=c[45]=0
x = nu( (float)(ref1 - hum2), (float)(ref1 - ref3), gpx->cal_f32[57] );
U2 = poly5(x, gpx->cal_f32+50); // c[54]=c[55]=0
_rh = U1 > U2 ? U1 : U2; // max(U1,U2), vgl. cal_state[1].bit3
gpx->_RH = _rh;
if (gpx->_RH < 0.0f) gpx->_RH = 0.0f;
if (gpx->_RH > 100.0f) gpx->_RH = 100.0f;
// correction for higher RH-sensor temperature (at low temperatures)?
// (cf. amt-7-4463-2014)
// if (T<-60C || P<100hPa): cal_state[1].bit2=0
// (Hyland and Wexler)
// if (T>-60C && P>100hPa): rh = _rh*vaporSatP(Trh)/vaporSatP(T) ...
// estimate Trh ?
// (uncorrected) Pressure
x = nu( (float)(ref1 - pres), (float)(ref1 - ref4), gpx->cal_f32[17] );
_P = poly5(x, gpx->cal_f32+10);
if (_P < 0.0f && _P > 2000.0f) _P = -1.0f;
gpx->_P = _P;
// correction for x and coefficients?
}
return 0;
}
// Altitude calculation using barometric formula for standard atmosphere
static int calc_PAlt() {
float Pb, Tb, Lb, hb, RgM = 8.31446/(9.80665*0.0289644);
float pressure = gpx->_P; //hPa
if (pressure <= 0.0f) return -1;
if (pressure < 8.6802) {
Pb = 8.6802;
Tb = 228.65;
Lb = 0.0028;
hb = 32000.0;
}
else if (pressure < 54.7489) {
Pb = 54.7489;
Tb = 216.65;
Lb = 0.001;
hb = 20000.0;
}
else if (pressure < 226.321) {
Pb = 226.321;
Tb = 216.65;
Lb = 0.0;
hb = 11000.0;
}
else {
Pb = 1013.25;
Tb = 288.15;
Lb = -0.0065;
hb = 0.0;
}
if (Lb == 0.0) gpx->alt = round(hb - RgM*Tb*log(pressure/Pb));
else gpx->alt = round(hb + Tb/Lb*(pow(pressure/Pb, -RgM*Lb)-1.0));
return 0;
}
// ----------------------------------------------------------------------------------------------------
static int get_PTU() {
if (crc_fail(pos_PTU, LEN_PTU)) {
gpx->crc |= crc_PTU;
return -2;
}
if (gpx->calfrms > 0x20) {
int ret = get_Meas();
calc_PAlt();
return ret;
}
else return 0;
}
static int get_Aux() {
int i;
unsigned short byte;
for (i = 0; i < 4; i++) {
byte = gpx->frame[pos_AuxData+2*i] + (gpx->frame[pos_AuxData+2*i+1]<<8);
gpx->aux[i] = byte;
}
if (crc_fail(pos_AUX, LEN_AUX)) {
gpx->crc |= crc_AUX;
return -2;
}
else return 0;
}
static int get_Cal() {
int i;
unsigned byte;
ui8_t calfr = 0;
ui8_t bytes[2];
int freq = 0;
ui16_t killtime = 0;
byte = gpx->frame[pos_CalData];
calfr = byte;
if (option.sub == 1) {
fprintf(stdout, "\n");
fprintf(stdout, "[%5d] ", gpx->frnr);
fprintf(stdout, " 0x%02x:", calfr);
for (i = 0; i < 16; i++) {
byte = gpx->frame[pos_CalData+1+i];
fprintf(stdout, " %02x", byte);
}
if ((gpx->crc & crc_STAT)==0) fprintf(stdout, " [OK]"); else fprintf(stdout, " [NO]");
}
if (calfr == 0x00) {
for (i = 0; i < 2; i++) {
bytes[i] = gpx->frame[pos_Calfreq + i];
}
byte = bytes[0] + (bytes[1] << 8);
freq = 1600000 + 10*byte; // kHz
gpx->freq = freq;
if(option.jsn < 2) fprintf(stdout, ": fq %d", freq);
for (i = 0; i < 2; i++) {
bytes[i] = gpx->frame[pos_Calfreq + 2 + i];
}
killtime = bytes[0] + (bytes[1] << 8); // signed?
if (killtime < 0xFFFF && option.jsn < 2) {
fprintf(stdout, "; KT:%ds", killtime);
}
gpx->conf_kt = killtime;
}
return 0;
}
/* ------------------------------------------------------------------------------------ */
#define rs_N 255
#define rs_R 24
#define rs_K (rs_N-rs_R)
static int rs92_ecc(int msglen) {
int i;
int errors;
ui8_t cw[rs_N];
ui8_t err_pos[rs_R], err_val[rs_R];
memset(cw, 0, rs_N);
if (msglen > FRAME_LEN) msglen = FRAME_LEN;
for (i = msglen; i < FRAME_LEN; i++) gpx->frame[i] = 0;//xFF;
for (i = 0; i < rs_R; i++) cw[i] = gpx->frame[cfg_rs92.parpos+i];
for (i = 0; i < cfg_rs92.msglen; i++) cw[rs_R+i] = gpx->frame[cfg_rs92.msgpos+i];
errors = rs_decode(&RS, cw, err_pos, err_val);
for (i = 0; i < rs_R; i++) gpx->frame[cfg_rs92.parpos+i] = cw[i];
for (i = 0; i < cfg_rs92.msglen; i++) gpx->frame[cfg_rs92.msgpos+i] = cw[rs_R+i];
return errors;
}
/* ------------------------------------------------------------------------------------ */
static int print_data(int ec) {
int j = 0;
int err1, err2, err4;
err1 = get_SondeID();
err1 |= get_FrameNb();
err2 = get_PTU();
err4 = get_Aux();
if (!err1)
{
if (option.jsn < 2) {
fprintf(stdout, "[%5d] ", gpx->frnr);
fprintf(stdout, "(%s) ", gpx->id);
if (option.dbg) {
ui8_t BFC = gpx->frame[pos_BitField0C];
ui8_t BFD = gpx->frame[pos_BitField0D];
fprintf(stdout, " BF="BYTE_TO_BINARY_PATTERN", "BYTE_TO_BINARY_PATTERN,
BYTE_TO_BINARY(BFC), BYTE_TO_BINARY(BFD));
}
if (!err2 && option.ptu) {
if (gpx->T > -273.0f) fprintf(stdout, " T=%.1fC ", gpx->T);
if (gpx->_RH > -0.5f) fprintf(stdout, " _RH=%.0f%% ", gpx->_RH);
if (gpx->_P > 0.0f) fprintf(stdout, " _P=%.1fhPa ", gpx->_P);
if (gpx->alt >-10000) fprintf(stdout, " alt=%dm ", gpx->alt);
}
if (option.aux && !err4) {
if (gpx->aux[0] != 0 || gpx->aux[1] != 0 || gpx->aux[2] != 0 || gpx->aux[3] != 0) {
fprintf(stdout, " # %04x %04x %04x %04x", gpx->aux[0], gpx->aux[1], gpx->aux[2], gpx->aux[3]);
}
}
fprintf(stdout, " ");
if (option.dbg) {
fprintf(stdout, "# [");
for (j=0; j<3; j++) fprintf(stdout, "%d", (gpx->crc>>j)&1);
fprintf(stdout, "]");
}
if (option.ecn) {
if (ec > 0) fprintf(stdout, " (%d)", ec);
if (ec < 0) fprintf(stdout, " (-)");
}
}
get_Cal();
if (option.jsn || option.udp) {
// Print out telemetry data as JSON
if ((gpx->crc & crc_STAT)==0) //(!err1)
{
char str[200], sentence[1000];
char *ver_jsn = NULL;
if (option.jsn==1) fprintf(stdout, "\n");
sprintf(sentence, "{\"type\":\"RS92\", \"subtype\":\"RS92-D\"");
sprintf(str, ", \"frame\":%d, \"id\":\"%s\"", gpx->frnr, gpx->id);
strcat(sentence, str);
if (option.ptu && !err2) {
if (gpx->T > -273.0f) {
sprintf(str, ", \"temp\":%.1f", gpx->T );
strcat(sentence, str);
}
if (gpx->_RH > -0.5f) {
sprintf(str, ", \"humidity\":%.1f", gpx->_RH );
strcat(sentence, str);
}
if (gpx->_P > 0.0f) {
sprintf(str, ", \"pressure\":%.2f", gpx->_P );
strcat(sentence, str);
}
if (gpx->alt >-10000 || option.arx) {
sprintf(str, ", \"alt\":%d", gpx->alt);
strcat(sentence, str);
}
}
else if (option.arx && err2) {
sprintf(str, ", \"alt\":%d", -10000);
strcat(sentence, str);
}
if ((gpx->crc & crc_AUX)==0 && (gpx->aux[0] != 0 || gpx->aux[1] != 0 || gpx->aux[2] != 0 || gpx->aux[3] != 0)) {
sprintf(str, ", \"aux\":\"%04x%04x%04x%04x\"", gpx->aux[0], gpx->aux[1], gpx->aux[2], gpx->aux[3]);
strcat(sentence, str);
}
if (gpx->jsn_freq > 0) { // rs92-frequency: gpx->freq
int fq_kHz = gpx->jsn_freq;
sprintf(str, ", \"freq\":%d", fq_kHz );
strcat(sentence, str);
}
// Include frequency derived from subframe information if available.
if (gpx->freq > 0) {
sprintf(str, ", \"tx_frequency\":%d", gpx->freq );
strcat(sentence, str);
}
if (option.arx || option.dat) {
time_t now;
now = time(NULL);
char dt_now[sizeof "2023-01-01T07:35:00Z"];
strftime(dt_now, sizeof dt_now, "%FT%TZ", gmtime(&now));
sprintf(str, ", \"datetime\":\"%s\"", dt_now );
strcat(sentence, str);
// if (option.arx)
// strcat(sentence, ", \"lat\":0.0, \"lon\":0.0");
}
#ifdef VER_JSN_STR
ver_jsn = VER_JSN_STR;
#endif
if (ver_jsn && *ver_jsn != '\0') {
sprintf(str, ", \"version\":\"%s\"", ver_jsn);
strcat(sentence, str);
}
for (int i = 0; i < extracnt; i++) {
if ((val[i][0]>='0' && val[i][0]<='9') || val[i][0]=='-' || val[i][0]=='+') //number?
sprintf(str, ", \"%s\":%s", key[i], val[i]);
else
sprintf(str, ", \"%s\":\"%s\"", key[i], val[i]);
strcat(sentence, str);
}
strcat(sentence, "}");
if (option.jsn == 1 || option.jsn == 2) {
fprintf(stdout, "%s", sentence); //JSON string to sdout
}
if (option.jsn==1) fprintf(stdout, "\n");
if (option.udp) //UDP packet
send_UDP(sentence);
}
}
if (option.jsn!=3) fprintf(stdout, "\n");
}
return err1;
}
static void print_frame(int len) {
int i, ec = 0;
ui8_t byte;
gpx->crc = 0;
//we can repair fix data in unused frame space before running ecc
for (i = 0; i < sizeof(RS92D_padding); i++) {
gpx->frame[i + pos_PADD] = RS92D_padding[i];
}
ec = rs92_ecc(len);
for (i = len; i < FRAME_LEN; i++) {
gpx->frame[i] = 0;
}
if (option.raw) {
for (i = 0; i < len; i++) {
byte = gpx->frame[i];
fprintf(stdout, "%02x", byte);
}
if (option.ecn) {
fprintf(stdout, " ");
if (ec >= 0) fprintf(stdout, " [OK]"); else fprintf(stdout, " [NO]");
if (ec > 0) fprintf(stdout, " (%d)", ec);
if (ec < 0) fprintf(stdout, " (-)");
}
fprintf(stdout, "\n");
}
else print_data(ec);
}
/* -------------------------------------------------------------------------- */
int main(int argc, char *argv[]) {
FILE *fp;
char *fpname = NULL;
int option_min = 0;
int option_iq = 0;
int option_iqdc = 0;
int option_lp = 0;
int option_dc = 0;
int option_noLUT = 0;
int option_softin = 0;
int option_pcmraw = 0;
int sel_wavch = 0; // audio channel: left
int spike = 0;
int fileloaded = 0;
int rawhex = 0;
int cfreq = -1;
char bitbuf[BITS];
int bitpos = 0,
b8pos = 0,
byte_count = FRAMESTART;
int bit, byte;
int bitQ;
int k;
int header_found = 0;
float thres = 0.5;
float _mv = 0.0;
float set_lpIQbw = -1.0f;
int symlen = 2;
int bitofs = 2; // +0 .. +3
int shift = 0;
pcm_t pcm = {0};
dsp_t dsp = {0}; //memset(&dsp, 0, sizeof(dsp));
hdb_t hdb = {0};
gpx_t gpx1 = {0};
gpx_t gpx2 = {0};
gpx1_pointer = &gpx1;
gpx2_pointer = &gpx2;
gpx = gpx1_pointer; //pointer to currently used dataset
#ifdef CYGWIN
_setmode(fileno(stdin), O_BINARY);
#endif
setbuf(stdout, NULL);
fpname = argv[0];
++argv;
while ((*argv) && (!fileloaded)) {
if ( (strcmp(*argv, "-h") == 0) || (strcmp(*argv, "--help") == 0) ) {
fprintf(stderr, "%s [options] <file>\n", fpname);
fprintf(stderr, " file: audio.wav or raw_data\n");
fprintf(stderr, " options:\n");
fprintf(stderr, " -r, --raw (raw hex output)\n");
fprintf(stderr, " -i, --invert\n");
fprintf(stderr, " --sub (print subframe data)\n");
fprintf(stderr, " --aux (print aux data if any)\n");
fprintf(stderr, " --eccn (Reed-Solomon err count)\n");
fprintf(stderr, " --ths <x> (peak threshold; default=%.1f)\n", thres);
fprintf(stderr, " --json (adds JSON output)\n");
fprintf(stderr, " --jsnonly (exclusively JSON out)\n");
fprintf(stderr, " -u <host> <port> (UDP JSON output)\n");
return 0;
}
else if ( (strcmp(*argv, "-v") == 0) ) { }
else if ( (strcmp(*argv, "--sub") == 0) ) { option.sub = 1; }
else if ( (strcmp(*argv, "--aux") == 0) ) { option.aux = 1; }
else if (strcmp(*argv, "--jsnonly") == 0) { option.jsn = 2; }
else if (strcmp(*argv, "--eccn") == 0) { option.ecn = 1; }
else if (strcmp(*argv, "--ptu" ) == 0) { option.ptu = 1; }
else if ( (strcmp(*argv, "-r") == 0) || (strcmp(*argv, "--raw") == 0) ) {
option.raw = 1;
}
else if ( (strcmp(*argv, "-i") == 0) || (strcmp(*argv, "--invert") == 0) ) {
option.inv = 1;
}
else if (strcmp(*argv, "--json") == 0) {
option.jsn = 1;
}
else if (strcmp(*argv, "--jsn_cfq") == 0) {
int frq = -1; // center frequency / Hz
++argv;
if (*argv) frq = atoi(*argv); else return -1;
if (frq < 300000000) frq = -1;
cfreq = frq;
}
else if (strcmp(*argv, "--spike") == 0) { spike = 1; }
else if (strcmp(*argv, "--ch2") == 0) { sel_wavch = 1; } // right channel (default: 0=left)
else if (strcmp(*argv, "--softin") == 0) { option_softin = 1; } // float32 soft input
else if (strcmp(*argv, "--silent") == 0) { option.jsn = 3; }
else if (strcmp(*argv, "--ths") == 0) {
++argv;
if (*argv) {
thres = atof(*argv);
}
else return -1;
}
else if ( (strcmp(*argv, "-d") == 0) ) {
++argv;
if (*argv) {
shift = atoi(*argv);
if (shift > 4) shift = 4;
if (shift < -4) shift = -4;
}
else return -1;
}
else if (strcmp(*argv, "--iq0") == 0) { option_iq = 1; } // differential/FM-demod
else if (strcmp(*argv, "--iq2") == 0) { option_iq = 2; }
else if (strcmp(*argv, "--iq3") == 0) { option_iq = 3; } // iq2==iq3
else if (strcmp(*argv, "--iqdc") == 0) { option_iqdc = 1; } // iq-dc removal (iq0,2,3)
else if (strcmp(*argv, "--IQ") == 0) { // fq baseband -> IF (rotate from and decimate)
double fq = 0.0; // --IQ <fq> , -0.5 < fq < 0.5
++argv;
if (*argv) fq = atof(*argv);
else return -1;
if (fq < -0.5) fq = -0.5;
if (fq > 0.5) fq = 0.5;
dsp.xlt_fq = -fq; // S(t) -> S(t)*exp(-f*2pi*I*t)
option_iq = 5;
}
else if (strcmp(*argv, "--lpIQ") == 0) { option_lp |= LP_IQ; } // IQ/IF lowpass
else if (strcmp(*argv, "--lpbw") == 0) { // IQ lowpass BW / kHz
double bw = 0.0;
++argv;
if (*argv) bw = atof(*argv);
else return -1;
if (bw > 4.6 && bw < 48.0) set_lpIQbw = bw*1e3;
option_lp |= LP_IQ;
}
else if (strcmp(*argv, "--lpFM") == 0) { option_lp |= LP_FM; } // FM lowpass
else if (strcmp(*argv, "--dc") == 0) { option_dc = 1; }
else if (strcmp(*argv, "--noLUT") == 0) { option_noLUT = 1; }
else if (strcmp(*argv, "--min") == 0) {
option_min = 1;
}
else if (strcmp(*argv, "--dbg" ) == 0) { option.dbg = 1; }
else if (strcmp(*argv, "--rawhex") == 0) { rawhex = 2; } // raw hex input
else if (strcmp(*argv, "-") == 0) {
int sample_rate = 0, bits_sample = 0, channels = 0;
++argv;
if (*argv) sample_rate = atoi(*argv); else return -1;
++argv;
if (*argv) bits_sample = atoi(*argv); else return -1;
channels = 2;
if (sample_rate < 1 || (bits_sample != 8 && bits_sample != 16 && bits_sample != 32)) {
fprintf(stderr, "- <sr> <bs>\n");
return -1;
}
pcm.sr = sample_rate;
pcm.bps = bits_sample;
pcm.nch = channels;
option_pcmraw = 1;
}
else if (strcmp(*argv, "-u") == 0) {
option.udp = 1;
hostcfg.sfd = -1;
++argv;
if (*argv) hostcfg.host = *argv; else return -1;
++argv;
if (*argv) hostcfg.port = *argv; else return -1;
}
else if (strcmp(*argv, "--autorx") == 0) { option.arx = 1;}
else if (strcmp(*argv, "--datetime") == 0) { option.dat = 1;}
else if (strcmp(*argv, "--add") == 0) {
++argv;
if (*argv) key[extracnt] = *argv; else return -1;
++argv;
if (*argv) val[extracnt] = *argv; else return -1;
++extracnt;
}
else {
fp = fopen(*argv, "rb");
if (fp == NULL) {
fprintf(stderr, "error: open %s\n", *argv);
return -1;
}
fileloaded = 1;
}
++argv;
}
if (!fileloaded) fp = stdin;
option.ptu = 1;
option.aut = 0;
if (option_iq == 5 && option_dc) option_lp |= LP_FM;
// LUT faster for decM, however frequency correction after decimation
// LUT recommonded if decM > 2
//
if (option_noLUT && option_iq == 5) dsp.opt_nolut = 1; else dsp.opt_nolut = 0;
rs_init_RS255(&RS);
// init gpx
memcpy(gpx1.frame, RS92_header_bytes, sizeof(RS92_header_bytes)); // 6 header bytes
memcpy(gpx2.frame, RS92_header_bytes, sizeof(RS92_header_bytes));
if (cfreq > 0) {
gpx1.jsn_freq = (cfreq+500)/1000;
gpx2.jsn_freq = gpx1.jsn_freq;
}
#ifdef EXT_FSK
if (!option_softin) {
option_softin = 1;
fprintf(stderr, "reading float32 soft symbols\n");
}
#endif
if (!rawhex) {
if (!option_softin) {
if (option_iq == 0 && option_pcmraw) {
fclose(fp);