-
Notifications
You must be signed in to change notification settings - Fork 45
/
rtl_wmbus.c
1372 lines (1123 loc) · 47.4 KB
/
rtl_wmbus.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
/*-
* Copyright (c) 2024 <[email protected]>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <getopt.h>
#include <stdint.h>
#include <limits.h>
#include <stddef.h>
#include <stdlib.h>
#include <complex.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <fixedptc/fixedptc.h>
#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
#define WINDOWS_BUILD 1
#else
#define WINDOWS_BUILD 0
#endif
#include "build/version.h"
#include "fir.h"
#include "iir.h"
#include "ppf.h"
#include "moving_average_filter.h"
#include "atan2.h"
#include "rtl_wmbus_util.h"
#include "t1_c1_packet_decoder.h"
#include "s1_packet_decoder.h"
#if WINDOWS_BUILD == 1
#define CHECK_FLOW 0
#include <io.h>
#warning "Compiling for Win discludes network support."
static inline void START_ALARM(void) {}
static inline void STOP_ALARM(void) {}
#else
#define CHECK_FLOW 1
#include <signal.h>
#include <unistd.h>
#include "net_support.h"
static inline void START_ALARM(void) { alarm(5); }
static inline void STOP_ALARM(void) { alarm(0); }
static void sig_alarm_handler(int signo)
{
fprintf(stderr, "rtl_wmbus: exiting since incoming data stopped flowing!\n");
exit(EXIT_FAILURE);
}
#endif
#ifndef TIME2_ALGORITHM_ENABLED
#define TIME2_ALGORITHM_ENABLED 1
#endif
#ifndef RUN_LENGTH_ALGORITHM_ENABLED
#define RUN_LENGTH_ALGORITHM_ENABLED 1
#endif
#ifndef T1_C1_DC_OFFSET_ALPHA
#define T1_C1_DC_OFFSET_ALPHA 0.999f
#endif
#ifndef S1_DC_OFFSET_ALPHA
#define S1_DC_OFFSET_ALPHA 0.999f
#endif
static const uint32_t ACCESS_CODE_T1_C1 = 0x543d;
static const uint32_t ACCESS_CODE_T1_C1_BITMASK = 0xFFFFu;
static const unsigned ACCESS_CODE_T1_C1_ERRORS = 0u; // 0 if no errors allowed
static const uint32_t ACCESS_CODE_S1 = 0x547696;
static const uint32_t ACCESS_CODE_S1_BITMASK = 0xFFFFFFu;
static const unsigned ACCESS_CODE_S1_ERRORS = 0u; // 0 if no errors allowed
/* deglitch_filter_t1_c1 has been calculated by a Python script as follows.
The filter is counting "1" among 7 bits and saying "1" if count("1") >= 3 else "0".
Notice here count("1") >= 3. (More intuitive in that case would be count("1") >= 3.5.)
That forces the filter to put more "1" than "0" on the output, because RTL-SDR streams
more "0" than "1" - i don't know why RTL-SDR do this.
x = 'static const uint8_t deglitch_filter_t1_c1[128] = {'
mod8 = 8
for i in range(2**7):
s = '{0:07b};'.format(i)
val = '1' if bin(i).count("1") >= 3 else '0'
print(s[0] + ";" + s[1] + ";" + s[2] + ";" + s[3] + ";" + s[4] + ";" + s[5] + ";" + s[6] + ";;%d;;%s" % (bin(i).count("1"), val))
if i % 8 == 0: x += '\n\t'
x += val + ','
x += '};\n'
print(x)
*/
static const uint8_t deglitch_filter_t1_c1[128] =
{
0,0,0,0,0,0,0,1,
0,0,0,1,0,1,1,1,
0,0,0,1,0,1,1,1,
0,1,1,1,1,1,1,1,
0,0,0,1,0,1,1,1,
0,1,1,1,1,1,1,1,
0,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
0,0,0,1,0,1,1,1,
0,1,1,1,1,1,1,1,
0,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
0,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1
};
/* 1) Force the filter to put more ones than zeros on the output.
2) Zeros surrounded by ones are ones and vice versa.
*/
static const uint8_t deglitch_filter_s1[16] = {
// 0000 0001 0010 0011 0100 0101 0110 0111
0, 1, 0, 1, 0, 1, 1, 1,
// 1000 1001 1010 1011 1100 1101 1110 1111
0, 1, 1, 1, 1, 1, 1, 1
};
//static FILE *demod_out = NULL;
static FILE *demod_out2_t1_c1 = NULL;
static FILE *demod_out2_s1 = NULL;
//static FILE *clock_out = NULL;
//static FILE *bits_out = NULL;
//static FILE *rawbits_out = NULL;
static inline float moving_average_t1_c1(float sample, size_t i_or_q)
{
#define COEFFS 8
static int i_hist[COEFFS];
static int q_hist[COEFFS];
static MAVGI_FILTER filter[2] = // i/q
{
{.length = COEFFS, .hist = i_hist}, // 0
{.length = COEFFS, .hist = q_hist} // 1
};
#undef COEFFS
return mavgi(sample, &filter[i_or_q]);
}
static inline float moving_average_s1(float sample, size_t i_or_q)
{
#define COEFFS 16
static int i_hist[COEFFS];
static int q_hist[COEFFS];
static MAVGI_FILTER filter[2] = // i/q
{
{.length = COEFFS, .hist = i_hist}, // 0
{.length = COEFFS, .hist = q_hist} // 1
};
#undef COEFFS
return mavgi(sample, &filter[i_or_q]);
}
static inline float lp_fir_butter_1600kHz_160kHz_200kHz_t1_c1(float sample, size_t i_or_q)
{
#define COEFFS 23
static float b[COEFFS] = {0.000140535927, 1.102280392e-05, 0.0001309279731, 0.001356012537, 0.00551787474, 0.01499414005, 0.03160167988, 0.05525973093, 0.08315031015, 0.1099887688, 0.1295143636, 0.1366692652, 0.1295143636, 0.1099887688, 0.08315031015, 0.05525973093, 0.03160167988, 0.01499414005, 0.00551787474, 0.001356012537, 0.0001309279731, 1.102280392e-05, 0.000140535927, };
//static float b[COEFFS] = {0.001645672124, 0.0004733757463, -0.002542116469, -0.008572441674, -0.01545406295, -0.01651661113, -0.002914917097, 0.03113207374, 0.08317149659, 0.1410058012, 0.1866042197, 0.2039350204, 0.1866042197, 0.1410058012, 0.08317149659, 0.03113207374, -0.002914917097, -0.01651661113, -0.01545406295, -0.008572441674, -0.002542116469, 0.0004733757463, 0.001645672124, };
static float i_hist[COEFFS] = {};
static float q_hist[COEFFS] = {};
static FIRF_FILTER filter[2] = // i/q
{
{.length = COEFFS, .b = b, .hist = i_hist}, // 0
{.length = COEFFS, .b = b, .hist = q_hist} // 1
};
#undef COEFFS
return firf(sample, &filter[i_or_q]);
}
static inline float lp_fir_butter_1600kHz_160kHz_200kHz_s1(float sample, size_t i_or_q)
{
#define COEFFS 23
static float b[COEFFS] = {0.000140535927, 1.102280392e-05, 0.0001309279731, 0.001356012537, 0.00551787474, 0.01499414005, 0.03160167988, 0.05525973093, 0.08315031015, 0.1099887688, 0.1295143636, 0.1366692652, 0.1295143636, 0.1099887688, 0.08315031015, 0.05525973093, 0.03160167988, 0.01499414005, 0.00551787474, 0.001356012537, 0.0001309279731, 1.102280392e-05, 0.000140535927, };
//static float b[COEFFS] = {0.001645672124, 0.0004733757463, -0.002542116469, -0.008572441674, -0.01545406295, -0.01651661113, -0.002914917097, 0.03113207374, 0.08317149659, 0.1410058012, 0.1866042197, 0.2039350204, 0.1866042197, 0.1410058012, 0.08317149659, 0.03113207374, -0.002914917097, -0.01651661113, -0.01545406295, -0.008572441674, -0.002542116469, 0.0004733757463, 0.001645672124, };
static float i_hist[COEFFS] = {};
static float q_hist[COEFFS] = {};
static FIRF_FILTER filter[2] = // i/q
{
{.length = COEFFS, .b = b, .hist = i_hist}, // 0
{.length = COEFFS, .b = b, .hist = q_hist} // 1
};
#undef COEFFS
return firf(sample, &filter[i_or_q]);
}
static inline float lp_firfp_butter_1600kHz_160kHz_200kHz(float sample, size_t i_or_q)
{
#define COEFFS 23
static const fixedpt b[COEFFS] = {fixedpt_rconst(0.000140535927), fixedpt_rconst(1.102280392e-05), fixedpt_rconst(0.0001309279731), fixedpt_rconst(0.001356012537), fixedpt_rconst(0.00551787474),
fixedpt_rconst(0.01499414005), fixedpt_rconst(0.03160167988), fixedpt_rconst(0.05525973093), fixedpt_rconst(0.08315031015), fixedpt_rconst(0.1099887688),
fixedpt_rconst(0.1295143636), fixedpt_rconst(0.1366692652), fixedpt_rconst(0.1295143636), fixedpt_rconst(0.1099887688), fixedpt_rconst(0.08315031015),
fixedpt_rconst(0.05525973093), fixedpt_rconst(0.03160167988), fixedpt_rconst(0.01499414005), fixedpt_rconst(0.00551787474), fixedpt_rconst(0.001356012537),
fixedpt_rconst(0.0001309279731), fixedpt_rconst(1.102280392e-05), fixedpt_rconst(0.000140535927),
};
static fixedpt i_hist[COEFFS] = {};
static fixedpt q_hist[COEFFS] = {};
static FIRFP_FILTER filter[2] = // i/q
{
{.length = COEFFS, .b = b, .hist = i_hist}, // 0
{.length = COEFFS, .b = b, .hist = q_hist} // 1
};
#undef COEFFS
return fixedpt_tofloat(firfp(fixedpt_fromint(sample), &filter[i_or_q]));
}
static inline float lp_ppf_butter_1600kHz_160kHz_200kHz(float sample, size_t i_or_q)
{
#define PHASES 2
#define COEFFS 12
static float b[PHASES][COEFFS] =
{
{0.000140535927, 0.0001309279731, 0.00551787474, 0.03160167988, 0.08315031015, 0.1295143636, 0.1295143636, 0.08315031015, 0.03160167988, 0.00551787474, 0.0001309279731, 0.000140535927, },
{1.102280392e-05, 0.001356012537, 0.01499414005, 0.05525973093, 0.1099887688, 0.1366692652, 0.1099887688, 0.05525973093, 0.01499414005, 0.001356012537, 1.102280392e-05, 0, },
};
static float i_hist[PHASES][COEFFS] = {};
static float q_hist[PHASES][COEFFS] = {};
static FIRF_FILTER fir[2][PHASES] =
{
{
// i/q phase
{.length = COEFFS, .b = b[1], .hist = i_hist[0]}, // 0 0
{.length = COEFFS, .b = b[0], .hist = i_hist[1]} // 0 1
},
{
// i/q phase
{.length = COEFFS, .b = b[1], .hist = q_hist[0]}, // 1 0
{.length = COEFFS, .b = b[0], .hist = q_hist[1]} // 1 1
},
};
static PPF_FILTER filter[2] =
{
{.sum = 0, .phase = 0, .max_phase = PHASES, .fir = fir[0]}, // 0 =: i
{.sum = 0, .phase = 0, .max_phase = PHASES, .fir = fir[1]}, // 1 =: q
};
#undef COEFFS
#undef PHASES
return ppf(sample, &filter[i_or_q]);
}
static inline float lp_ppffp_butter_1600kHz_160kHz_200kHz(float sample, size_t i_or_q)
{
#define PHASES 2
#define COEFFS 12
static const fixedpt b[PHASES][COEFFS] =
{
{fixedpt_rconst(0.000140535927), fixedpt_rconst(0.0001309279731), fixedpt_rconst(0.00551787474), fixedpt_rconst(0.03160167988), fixedpt_rconst(0.08315031015), fixedpt_rconst(0.1295143636), fixedpt_rconst(0.1295143636), fixedpt_rconst(0.08315031015), fixedpt_rconst(0.03160167988), fixedpt_rconst(0.00551787474), fixedpt_rconst(0.0001309279731), fixedpt_rconst(0.000140535927), },
{fixedpt_rconst(1.102280392e-05), fixedpt_rconst(0.001356012537), fixedpt_rconst(0.01499414005), fixedpt_rconst(0.05525973093), fixedpt_rconst(0.1099887688), fixedpt_rconst(0.1366692652), fixedpt_rconst(0.1099887688), fixedpt_rconst(0.05525973093), fixedpt_rconst(0.01499414005), fixedpt_rconst(0.001356012537), fixedpt_rconst(1.102280392e-05), fixedpt_rconst(0), },
};
static fixedpt i_hist[PHASES][COEFFS] = {};
static fixedpt q_hist[PHASES][COEFFS] = {};
static FIRFP_FILTER fir[2][PHASES] =
{
{
// i/q phase
{.length = COEFFS, .b = b[1], .hist = i_hist[0]}, // 0 0
{.length = COEFFS, .b = b[0], .hist = i_hist[1]} // 0 1
},
{
// i/q phase
{.length = COEFFS, .b = b[1], .hist = q_hist[0]}, // 1 0
{.length = COEFFS, .b = b[0], .hist = q_hist[1]} // 1 1
},
};
static PPFFP_FILTER filter[2] =
{
{.sum = fixedpt_rconst(0), .phase = 0, .max_phase = PHASES, .fir = fir[0]}, // 0 =: i
{.sum = fixedpt_rconst(0), .phase = 0, .max_phase = PHASES, .fir = fir[1]}, // 1 =: q
};
#undef COEFFS
#undef PHASES
return fixedpt_tofloat(ppffp(fixedpt_fromint(sample), &filter[i_or_q]));
}
static inline float bp_iir_cheb1_800kHz_90kHz_98kHz_102kHz_110kHz(float sample)
{
#define GAIN 1.874981046e-06
#define SECTIONS 3
static const float b[3*SECTIONS] = {1, 1.999994649, 0.9999946492, 1, -1.99999482, 0.9999948196, 1, 1.703868036e-07, -1.000010531, };
static const float a[3*SECTIONS] = {1, -1.387139203, 0.9921518712, 1, -1.403492665, 0.9845934971, 1, -1.430055639, 0.9923856172, };
static float hist[3*SECTIONS] = {};
static IIRF_FILTER filter = {.sections = SECTIONS, .b = b, .a = a, .gain = GAIN, .hist = hist};
#undef SECTIONS
#undef GAIN
return iirf(sample, &filter);
}
static inline float bp_iir_cheb1_800kHz_22kHz_30kHz_34kHz_42kHz(float sample)
{
#define GAIN 1.874981046e-06
#define SECTIONS 3
static const float b[3*SECTIONS] = {1, 1.999994187, 0.9999941867, 1, -1.999994026,0.9999940262, 1, -1.605750097e-07, -1.000011787, };
static const float a[3*SECTIONS] = {1, -1.92151475, 0.9918135499, 1, -1.922481015,0.984593497, 1, -1.937432099, 0.9927241336, };
static float hist[3*SECTIONS] = {};
static IIRF_FILTER filter = {.sections = SECTIONS, .b = b, .a = a, .gain = GAIN, .hist = hist};
#undef SECTIONS
#undef GAIN
return iirf(sample, &filter);
}
static inline float lp_fir_butter_800kHz_100kHz_160kHz(float sample)
{
#define COEFFS 11
static float b[COEFFS] = {-0.00456638213, -0.002571450348, 0.02689425925, 0.1141330398, 0.2264456422, 0.2793297826, 0.2264456422, 0.1141330398, 0.02689425925, -0.002571450348, -0.00456638213, };
static float hist[COEFFS];
static FIRF_FILTER filter = {.length = COEFFS, .b = b, .hist = hist};
#undef COEFFS
return firf(sample, &filter);
}
static inline float lp_fir_butter_800kHz_32kHz_36kHz(float sample)
{
#define COEFFS 46
static float b[COEFFS] = {-0.000649081282, -0.0009491938209, -0.001361601657, -0.001910785234, -0.002570133495, -0.003251218426, -0.003801634695, -0.004012672882, -0.003636803575, -0.002413585945, -0.0001013597693, 0.003488892085, 0.008461671287, 0.01481127545, 0.02240598045, 0.03098477999, 0.0401679839, 0.04948137286, 0.05839197924, 0.06635211627, 0.07284719662, 0.07744230649, 0.07982251613, 0.07982251613, 0.07744230649, 0.07284719662, 0.06635211627, 0.05839197924, 0.04948137286, 0.0401679839, 0.03098477999, 0.02240598045, 0.01481127545, 0.008461671287, 0.003488892085, -0.0001013597693, -0.002413585945, -0.003636803575, -0.004012672882, -0.003801634695, -0.003251218426, -0.002570133495, -0.001910785234, -0.001361601657, -0.0009491938209, -0.000649081282, };
static float hist[COEFFS];
static FIRF_FILTER filter = {.length = COEFFS, .b = b, .hist = hist};
#undef COEFFS
return firf(sample, &filter);
}
/* https://liquidsdr.org/blog/lms-equalizer/ */
static inline void equalizer_complex_t1_c1(float *i, float *q)
{
static const float mu = 0.05f;
static size_t buf_index = 0;
#define COEFFS 21
static float complex w[COEFFS] = {
0.f, 0.f, 0.f, 0.f, 0.f,
0.f, 0.f, 0.f, 0.f, 0.f,
1.f,
0.f, 0.f, 0.f, 0.f, 0.f,
0.f, 0.f, 0.f, 0.f, 0.f,
};
static float complex b[COEFFS];
b[buf_index] = *i + *q * _Complex_I;
buf_index = (buf_index + 1) % COEFFS;
float complex r = 0.f;
for (int k = 0; k < COEFFS; k++)
{
r += b[(buf_index+k)%COEFFS] * conjf(w[k]);
}
const float complex e = (*q >= 0.f) ? (127.5f * _Complex_I) : (-127.5f * _Complex_I);
for (int k = 0; k < COEFFS; k++)
{
w[k] = w[k] - mu * conjf(e)*b[(buf_index+k)%COEFFS];
}
#undef COEFFS
//fprintf(stdout, "%8.3f, %8.3f, %8.3f, %8.3f\n", *i, crealf(r), *q, cimagf(r));
*i = crealf(r);
*q = cimagf(r);
}
static inline float equalizer_t1_c1(const float sample, const float d)
{
static const float mu = 0.05f;
#define COEFFS 9
static float b[COEFFS] = { [COEFFS/2 + 1] = 1.f, };
static float hist[COEFFS];
static FIRF_FILTER filter = {.length = COEFFS, .b = b, .hist = hist};
#undef COEFFS
const float r = firf(sample, &filter);
const float e = d - r;
const float mu_e = mu * e;
firf_lms(mu_e, &filter);
return r;
}
static inline float equalizer_s1(const float sample, const float d)
{
static const float mu = 0.05f;
#define COEFFS 19
static float b[COEFFS] = { [COEFFS/2 + 1] = 1.f, };
static float hist[COEFFS];
static FIRF_FILTER filter = {.length = COEFFS, .b = b, .hist = hist};
#undef COEFFS
const float r = firf(sample, &filter);
const float e = d - r;
const float mu_e = mu * e;
firf_lms(mu_e, &filter);
return r;
}
static float rssi_filter_t1_c1(float sample)
{
static float old_sample;
#define ALPHA 0.6789f
old_sample = ALPHA*sample + (1.0f - ALPHA)*old_sample;
#undef ALPHA
return old_sample;
}
static float rssi_filter_s1(float sample)
{
static float old_sample;
#define ALPHA 0.6789f
old_sample = ALPHA*sample + (1.0f - ALPHA)*old_sample;
#undef ALPHA
return old_sample;
}
static float s1_remove_dc_offset_demod(float x)
{
static float x_old, y_old;
y_old = (1.f + S1_DC_OFFSET_ALPHA)/2.f * (x - x_old) + S1_DC_OFFSET_ALPHA * y_old;
x_old = x;
return y_old;
}
static float t1_c1_remove_dc_offset_demod(float x)
{
static float x_old, y_old;
y_old = (1.f + T1_C1_DC_OFFSET_ALPHA)/2.f * (x - x_old) + T1_C1_DC_OFFSET_ALPHA * y_old;
x_old = x;
return y_old;
}
static inline float polar_discriminator_t1_c1(float i, float q)
{
static float complex s_last;
const float complex s = i + q * _Complex_I;
const float complex y = s * conjf(s_last);
#if 1
const float delta_phi = atan2_libm(y);
#elif 0
const float delta_phi = atan2_approximation(y);
#else
const float delta_phi = atan2_approximation2(y);
#endif
s_last = s;
return delta_phi;
}
static inline float polar_discriminator_t1_c1_inaccurate(float i, float q)
{
// We are going to use only complex part of the phase difference
// so avoid unnecesary computation of real part. The math behind:
// cargf = atan (delta_phi_imag / delta_phi_real) / pi;
// In the formula only the sign is of interest - we compute delta_phi_imag only.
static float i_last, q_last;
const float delta_phi_imag = i_last*q - i*q_last;
i_last = i;
q_last = q;
return delta_phi_imag;
}
static inline float polar_discriminator_s1(float i, float q)
{
static float complex s_last;
const float complex s = i + q * _Complex_I;
const float complex y = s * conjf(s_last);
#if 1
const float delta_phi = atan2_libm(y);
#elif 0
const float delta_phi = atan2_approximation(y);
#else
const float delta_phi = atan2_approximation2(y);
#endif
s_last = s;
return delta_phi;
}
static inline float polar_discriminator_s1_inaccurate(float i, float q)
{
// We are going to use only complex part of the phase difference
// so avoid unnecesary computation of real part. The math behind:
// cargf = atan (delta_phi_imag / delta_phi_real) / pi;
// In the formula only the sign is of interest - we compute delta_phi_imag only.
static float i_last, q_last;
const float delta_phi_imag = i_last*q - i*q_last;
i_last = i;
q_last = q;
return delta_phi_imag;
}
/** @brief Sparse Ones runs in time proportional to the number
* of 1 bits.
*
* From: http://gurmeet.net/puzzles/fast-bit-counting-routines
*/
static inline unsigned count_set_bits_sparse_one(uint32_t n)
{
unsigned count = 0;
while (n)
{
count++;
n &= (n - 1) ; // set rightmost 1 bit in n to 0
}
return count;
}
static inline unsigned count_set_bits(uint32_t n)
{
#if defined(__i386__) || defined(__arm__)
return __builtin_popcount(n);
#else
return count_set_bits_sparse_one(n);
#endif
}
struct runlength_algorithm_s1
{
int run_length;
unsigned state;
uint32_t raw_bitstream;
uint32_t bitstream;
int samples_per_bit[2];
struct s1_packet_decoder_work decoder;
};
static void runlength_algorithm_reset_s1(struct runlength_algorithm_s1 *algo)
{
algo->run_length = 0;
algo->state = 0u;
algo->raw_bitstream = 0;
algo->bitstream = 0;
algo->samples_per_bit[0] = 24; // Data rate is 32768 bps which gives us approx. 24 samples
algo->samples_per_bit[1] = 24; // at a sample rate of 800kHz (800kHz / 32768bps = 24.41 ~= 24 samples).
reset_s1_packet_decoder(&algo->decoder);
}
static void runlength_algorithm_s1(unsigned raw_bit, unsigned rssi, struct runlength_algorithm_s1 *algo)
{
algo->raw_bitstream = (algo->raw_bitstream << 1) | raw_bit;
const unsigned state = deglitch_filter_s1[algo->raw_bitstream & 0xFu];
// Edge detector.
if (algo->state == state)
{
algo->run_length++;
}
else
{
// Get the current bit length expressed in samples as an
// average of two preceeding symbols.
const int samples_per_bit = (algo->samples_per_bit[0] + algo->samples_per_bit[1]) / 2;
// Reset the state machine if the current bit length (in samples)
// is less than 0.5 or more than 1.5 of the ideal symbol length.
if (samples_per_bit <= 24/2 || samples_per_bit >= (24+24/2))
{
runlength_algorithm_reset_s1(algo);
algo->state = state;
algo->run_length = 1;
return;
}
// Reset the state machine if the sequence of ones (or zeros)
// is less than 0.5 symbol length that we assume.
const int half_bit_length = samples_per_bit/2;
const int run_length = algo->run_length;
if (run_length <= half_bit_length)
{
runlength_algorithm_reset_s1(algo);
algo->state = state;
algo->run_length = 1;
return;
}
int num_of_bits_rx;
for (num_of_bits_rx = 0; algo->run_length > half_bit_length; num_of_bits_rx++)
{
algo->run_length -= samples_per_bit;
unsigned bit = algo->state;
algo->bitstream = (algo->bitstream << 1) | bit;
if (count_set_bits((algo->bitstream & ACCESS_CODE_S1_BITMASK) ^ ACCESS_CODE_S1) <= ACCESS_CODE_S1_ERRORS)
{
bit |= (1u<<PACKET_PREAMBLE_DETECTED_SHIFT); // packet detected; mark the bit similar to "Access Code"-Block in GNU Radio
}
s1_packet_decoder(bit, rssi, &algo->decoder, "rla;");
}
//fprintf(stdout, "%u, %d, bits: %d, 0: %u, 1: %u\n", algo->state, run_length, num_of_bits_rx, algo->samples_per_bit[0], algo->samples_per_bit[1]);
algo->samples_per_bit[algo->state] = run_length / num_of_bits_rx;
algo->state = state;
algo->run_length = 1;
}
}
struct runlength_algorithm_t1_c1
{
int run_length;
int bit_length;
int cum_run_length_error;
unsigned state;
uint32_t raw_bitstream;
uint32_t bitstream;
struct t1_c1_packet_decoder_work decoder;
};
static void runlength_algorithm_reset_t1_c1(struct runlength_algorithm_t1_c1 *algo)
{
algo->run_length = 0;
algo->bit_length = 8 * 256;
algo->cum_run_length_error = 0;
algo->state = 0u;
algo->raw_bitstream = 0;
algo->bitstream = 0;
reset_t1_c1_packet_decoder(&algo->decoder);
}
static void runlength_algorithm_t1_c1(unsigned raw_bit, unsigned rssi, struct runlength_algorithm_t1_c1 *algo)
{
algo->raw_bitstream = (algo->raw_bitstream << 1) | raw_bit;
const unsigned state = deglitch_filter_t1_c1[algo->raw_bitstream & 0x3Fu];
// Edge detector.
if (algo->state == state)
{
algo->run_length++;
}
else
{
if (algo->run_length < 5)
{
runlength_algorithm_reset_t1_c1(algo);
algo->state = state;
algo->run_length = 1;
return;
}
//const int unscaled_run_length = algo->run_length;
algo->run_length *= 256; // resolution scaling up for fixed point calculation
const int half_bit_length = algo->bit_length / 2;
if (algo->run_length <= half_bit_length)
{
runlength_algorithm_reset_t1_c1(algo);
algo->state = state;
algo->run_length = 1;
return;
}
int num_of_bits_rx;
for (num_of_bits_rx = 0; algo->run_length > half_bit_length; num_of_bits_rx++)
{
algo->run_length -= algo->bit_length;
unsigned bit = algo->state;
algo->bitstream = (algo->bitstream << 1) | bit;
if (count_set_bits((algo->bitstream & ACCESS_CODE_T1_C1_BITMASK) ^ ACCESS_CODE_T1_C1) <= ACCESS_CODE_T1_C1_ERRORS)
{
bit |= (1u<<PACKET_PREAMBLE_DETECTED_SHIFT); // packet detected; mark the bit similar to "Access Code"-Block in GNU Radio
}
t1_c1_packet_decoder(bit, rssi, &algo->decoder, "rla;");
}
#if 0
const int bit_error_length = algo->run_length / num_of_bits_rx;
if (in_rx_t1_c1_packet_decoder(&algo->decoder))
{
fprintf(stdout, "rl = %d, num_of_bits_rx = %d, bit_length = %d, old_bit_error_length = %d, new_bit_error_length = %d\n",
unscaled_run_length, num_of_bits_rx, algo->bit_length, algo->bit_error_length, bit_error_length);
}
#endif
// Some kind of PI controller is implemented below: u[n] = u[n-1] + Kp * e[n] + Ki * sum(e[0..n]).
// Kp and Ki were found by experiment; e[n] := algo->run_length; u[[n] is the new bit length; u[n-1] is the last known bit length
algo->cum_run_length_error += algo->run_length; // sum(e[0..n])
#define PI_KP 32
#define PI_KI 16
//algo->bit_length += (algo->run_length / PI_KP + algo->cum_run_length_error / PI_KI) / num_of_bits_rx;
algo->bit_length += (algo->run_length + algo->cum_run_length_error / PI_KI) / (PI_KP * num_of_bits_rx);
#undef PI_KI
#undef PI_KP
algo->state = state;
algo->run_length = 1;
}
}
struct time2_algorithm_t1_c1
{
uint32_t bitstream;
struct t1_c1_packet_decoder_work t1_c1_decoder;
};
static void time2_algorithm_t1_c1_reset(struct time2_algorithm_t1_c1 *algo)
{
algo->bitstream = 0;
reset_t1_c1_packet_decoder(&algo->t1_c1_decoder);
}
static void time2_algorithm_t1_c1(unsigned bit, unsigned rssi, struct time2_algorithm_t1_c1 *algo)
{
algo->bitstream = (algo->bitstream << 1) | bit;
if (count_set_bits((algo->bitstream & ACCESS_CODE_T1_C1_BITMASK) ^ ACCESS_CODE_T1_C1) <= ACCESS_CODE_T1_C1_ERRORS)
{
bit |= (1u<<PACKET_PREAMBLE_DETECTED_SHIFT); // packet detected; mark the bit similar to "Access Code"-Block in GNU Radio
}
t1_c1_packet_decoder(bit, rssi, &algo->t1_c1_decoder, "t2a;");
}
struct time2_algorithm_s1
{
uint32_t bitstream;
struct s1_packet_decoder_work s1_decoder;
};
static void time2_algorithm_s1_reset(struct time2_algorithm_s1 *algo)
{
algo->bitstream = 0;
reset_s1_packet_decoder(&algo->s1_decoder);
}
static void time2_algorithm_s1(unsigned bit, unsigned rssi, struct time2_algorithm_s1 *algo)
{
algo->bitstream = (algo->bitstream << 1) | bit;
if (count_set_bits((algo->bitstream & ACCESS_CODE_S1_BITMASK) ^ ACCESS_CODE_S1) <= ACCESS_CODE_S1_ERRORS)
{
bit |= (1u<<PACKET_PREAMBLE_DETECTED_SHIFT); // packet detected; mark the bit similar to "Access Code"-Block in GNU Radio
}
s1_packet_decoder(bit, rssi, &algo->s1_decoder, "t2a;");
}
static int opts_run_length_algorithm_enabled = 1;
static int opts_time2_algorithm_enabled = TIME2_ALGORITHM_ENABLED;
static unsigned opts_decimation_rate = 2u;
static int opts_s1_t1_c1_simultaneously = 0;
static int opts_accurate_atan = 1;
static int opts_remove_dc_offset = 0;
int opts_show_used_algorithm = 0;
static int opts_t1_c1_processing_enabled = 1;
static int opts_s1_processing_enabled = 1;
static int opts_check_flow = 0;
static const unsigned opts_CLOCK_LOCK_THRESHOLD_T1_C1 = 2; // Is not implemented as option yet.
static const unsigned opts_CLOCK_LOCK_THRESHOLD_S1 = 2; // Is not implemented as option yet.
static void print_usage(const char *program_name)
{
fprintf(stdout, "rtl_wmbus: " VERSION "\n\n");
fprintf(stdout, "Usage %s:\n", program_name);
fprintf(stdout, "\t-o remove DC offset\n");
fprintf(stdout, "\t-a accelerate (use an inaccurate atan version)\n");
fprintf(stdout, "\t-r 0 to disable run length algorithm\n");
fprintf(stdout, "\t-t 0 to disable time2 algorithm\n");
fprintf(stdout, "\t-d 2 set decimation rate to 2 (defaults to 2 if omitted)\n");
fprintf(stdout, "\t-v show used algorithm in the output\n");
fprintf(stdout, "\t-V show version\n");
fprintf(stdout, "\t-s receive S1 and T1/C1 datagrams simultaneously. rtl_sdr _MUST_ be set to 868.625MHz (-f 868.625M)\n");
fprintf(stdout, "\t-p [T,S] to disable processing T1/C1 or S1 mode\n");
fprintf(stdout, "\t-f exit if incoming data stalled for 5 seconds\n");
fprintf(stdout, "\t-h print this help\n");
}
static void print_version(void)
{
fprintf(stdout, "rtl_wmbus: " VERSION "\n");
fprintf(stdout, COMMIT "\n");
}
static void process_options(int argc, char *argv[])
{
int option;
while ((option = getopt(argc, argv, "ofad:p:r:vVst:")) != -1)
{
switch (option)
{
case 'o':
opts_remove_dc_offset = 1;
break;
case 'f':
opts_check_flow = 1;
#if CHECK_FLOW == 0
fprintf(stderr, "rtl_wmbus: Warning! You supplied the option -f but this build of rtl_wmbus cannot check flow of incoming data!\n");
#endif
break;
case 'a':
opts_accurate_atan = 0;
break;
case 'p':
if (strcmp(optarg, "T") == 0 || strcmp(optarg, "t") == 0)
{
opts_t1_c1_processing_enabled = 0;
}
else if (strcmp(optarg, "S") == 0 || strcmp(optarg, "s") == 0)
{
opts_s1_processing_enabled = 0;
}
else
{
print_usage(argv[0]);
exit(EXIT_FAILURE);
}
break;
case 'r':
if (strcmp(optarg, "0") == 0)
{
opts_run_length_algorithm_enabled = 0;
}
else
{
print_usage(argv[0]);
exit(EXIT_FAILURE);
}
break;
case 't':
if (strcmp(optarg, "0") == 0)
{
opts_time2_algorithm_enabled = 0;
}
else
{
print_usage(argv[0]);
exit(EXIT_FAILURE);
}
break;
case 'd':
opts_decimation_rate = strtoul(optarg, NULL, 10);
break;
case 's':
opts_s1_t1_c1_simultaneously = 1;
break;
case 'v':
opts_show_used_algorithm = 1;
break;
case 'V':
print_version();
exit(EXIT_SUCCESS);
break;
default:
print_usage(argv[0]);
exit(EXIT_FAILURE);
}
}
}
static float *LUT_FREQUENCY_TRANSLATION_PLUS_COSINE = NULL;
static float *LUT_FREQUENCY_TRANSLATION_PLUS_SINE = NULL;
#define FREQ_STEP_KHZ (25)
/* fs_kHz is the sample rate in kHz. */
static void setup_lookup_tables_for_frequency_translation(int fs_kHz)
{
const int ft_kHz = FREQ_STEP_KHZ;
const size_t n_max = fs_kHz/ft_kHz;
free(LUT_FREQUENCY_TRANSLATION_PLUS_COSINE);
LUT_FREQUENCY_TRANSLATION_PLUS_COSINE = malloc(n_max *sizeof(LUT_FREQUENCY_TRANSLATION_PLUS_COSINE[0]));
if (!LUT_FREQUENCY_TRANSLATION_PLUS_COSINE) exit(EXIT_FAILURE);
free(LUT_FREQUENCY_TRANSLATION_PLUS_SINE);
LUT_FREQUENCY_TRANSLATION_PLUS_SINE = malloc(n_max *sizeof(LUT_FREQUENCY_TRANSLATION_PLUS_SINE[0]));
if (!LUT_FREQUENCY_TRANSLATION_PLUS_SINE) exit(EXIT_FAILURE);
for (size_t n = 0; n < n_max; n++)
{
const double phi = (2. * M_PI * (ft_kHz * n)) / fs_kHz;
LUT_FREQUENCY_TRANSLATION_PLUS_COSINE[n] = cosf(phi);
LUT_FREQUENCY_TRANSLATION_PLUS_SINE[n] = -sinf(phi); // Minus sinf!
}
}
/* Positive frequencies shift: ft = +325kHz the signal will shift from 868.625M right to 868.95M.
Negative frequencies shift: ft = -325kHz the signal will shift from 868.625M right to 868.3M. */
static void shift_freq_plus_minus325(float *iplus, float *qplus, float *iminus, float *qminus, int fs_kHz)
{
const int ft = 325;
static size_t n = 0;