-
Notifications
You must be signed in to change notification settings - Fork 0
/
dmrwav.c
1492 lines (1315 loc) · 53.7 KB
/
dmrwav.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
/*
DMRwav - make wav files of individual DMR transmissions
Copyright 2024 James Tittsler ZL2IA / AI8A
Based on code from:
DMRBot - ChatGPT Voice Bot for DMR https://github.com/narspt/DMRBot
Copyright (C) 2023 Nuno Silva
Which was in turn based on code from:
reflector_connectors https://github.com/nostar/reflector_connectors
MMDVM_CM https://github.com/juribeparada/MMDVM_CM
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <netdb.h>
#include <ctype.h>
#include <time.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#define AMBE_ENCODE_GAIN -15
#define AMBE_DECODE_GAIN 10
#define BUFSIZE 2048
#define TIMEOUT 60
//#define DEBUG
#define SWAP(n) (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24))
#define K(I) roundConstants[I]
static const uint32_t roundConstants[64] = {
0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL,
0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL,
0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL,
0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, 0xc19bf174UL,
0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL,
0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL,
0x983e5152UL, 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL,
0xc6e00bf3UL, 0xd5a79147UL, 0x06ca6351UL, 0x14292967UL,
0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL, 0x53380d13UL,
0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,
0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL,
0xd192e819UL, 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL,
0x19a4c116UL, 0x1e376c08UL, 0x2748774cUL, 0x34b0bcb5UL,
0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL, 0x682e6ff3UL,
0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL,
};
const unsigned char POLY[] = {64U, 56U, 14U, 1U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U};
const unsigned char EXP_TABLE[] = {
0x01U, 0x02U, 0x04U, 0x08U, 0x10U, 0x20U, 0x40U, 0x80U, 0x1DU, 0x3AU, 0x74U, 0xE8U, 0xCDU, 0x87U, 0x13U, 0x26U,
0x4CU, 0x98U, 0x2DU, 0x5AU, 0xB4U, 0x75U, 0xEAU, 0xC9U, 0x8FU, 0x03U, 0x06U, 0x0CU, 0x18U, 0x30U, 0x60U, 0xC0U,
0x9DU, 0x27U, 0x4EU, 0x9CU, 0x25U, 0x4AU, 0x94U, 0x35U, 0x6AU, 0xD4U, 0xB5U, 0x77U, 0xEEU, 0xC1U, 0x9FU, 0x23U,
0x46U, 0x8CU, 0x05U, 0x0AU, 0x14U, 0x28U, 0x50U, 0xA0U, 0x5DU, 0xBAU, 0x69U, 0xD2U, 0xB9U, 0x6FU, 0xDEU, 0xA1U,
0x5FU, 0xBEU, 0x61U, 0xC2U, 0x99U, 0x2FU, 0x5EU, 0xBCU, 0x65U, 0xCAU, 0x89U, 0x0FU, 0x1EU,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U, 0x84U, 0x3CU, 0x39U, 0x53U, 0x47U, 0x6DU, 0x41U, 0xA2U,
0x1FU, 0x2DU, 0x43U, 0xD8U, 0xB7U, 0x7BU, 0xA4U, 0x76U, 0xC4U, 0x17U, 0x49U, 0xECU, 0x7FU, 0x0CU, 0x6FU, 0xF6U,
0x6CU, 0xA1U, 0x3BU, 0x52U, 0x29U, 0x9DU, 0x55U, 0xAAU, 0xFBU, 0x60U, 0x86U, 0xB1U, 0xBBU, 0xCCU, 0x3EU, 0x5AU,
0xCBU, 0x59U, 0x5FU, 0xB0U, 0x9CU, 0xA9U, 0xA0U, 0x51U, 0x0BU, 0xF5U, 0x16U, 0xEBU, 0x7AU, 0x75U, 0x2CU, 0xD7U,
0x4FU, 0xAEU, 0xD5U, 0xE9U, 0xE6U, 0xE7U, 0xADU, 0xE8U, 0x74U, 0xD6U, 0xF4U, 0xEAU, 0xA8U, 0x50U, 0x58U, 0xAFU
};
const unsigned int ENCODING_TABLE_2087[] =
{0x0000U, 0xB08EU, 0xE093U, 0x501DU, 0x70A9U, 0xC027U, 0x903AU, 0x20B4U, 0x60DCU, 0xD052U, 0x804FU, 0x30C1U,
0x1075U, 0xA0FBU, 0xF0E6U, 0x4068U, 0x7036U, 0xC0B8U, 0x90A5U, 0x202BU, 0x009FU, 0xB011U, 0xE00CU, 0x5082U,
0x10EAU, 0xA064U, 0xF079U, 0x40F7U, 0x6043U, 0xD0CDU, 0x80D0U, 0x305EU, 0xD06CU, 0x60E2U, 0x30FFU, 0x8071U,
0xA0C5U, 0x104BU, 0x4056U, 0xF0D8U, 0xB0B0U, 0x003EU, 0x5023U, 0xE0ADU, 0xC019U, 0x7097U, 0x208AU, 0x9004U,
0xA05AU, 0x10D4U, 0x40C9U, 0xF047U, 0xD0F3U, 0x607DU, 0x3060U, 0x80EEU, 0xC086U, 0x7008U, 0x2015U, 0x909BU,
0xB02FU, 0x00A1U, 0x50BCU, 0xE032U, 0x90D9U, 0x2057U, 0x704AU, 0xC0C4U, 0xE070U, 0x50FEU, 0x00E3U, 0xB06DU,
0xF005U, 0x408BU, 0x1096U, 0xA018U, 0x80ACU, 0x3022U, 0x603FU, 0xD0B1U, 0xE0EFU, 0x5061U, 0x007CU, 0xB0F2U,
0x9046U, 0x20C8U, 0x70D5U, 0xC05BU, 0x8033U, 0x30BDU, 0x60A0U, 0xD02EU, 0xF09AU, 0x4014U, 0x1009U, 0xA087U,
0x40B5U, 0xF03BU, 0xA026U, 0x10A8U, 0x301CU, 0x8092U, 0xD08FU, 0x6001U, 0x2069U, 0x90E7U, 0xC0FAU, 0x7074U,
0x50C0U, 0xE04EU, 0xB053U, 0x00DDU, 0x3083U, 0x800DU, 0xD010U, 0x609EU, 0x402AU, 0xF0A4U, 0xA0B9U, 0x1037U,
0x505FU, 0xE0D1U, 0xB0CCU, 0x0042U, 0x20F6U, 0x9078U, 0xC065U, 0x70EBU, 0xA03DU, 0x10B3U, 0x40AEU, 0xF020U,
0xD094U, 0x601AU, 0x3007U, 0x8089U, 0xC0E1U, 0x706FU, 0x2072U, 0x90FCU, 0xB048U, 0x00C6U, 0x50DBU, 0xE055U,
0xD00BU, 0x6085U, 0x3098U, 0x8016U, 0xA0A2U, 0x102CU, 0x4031U, 0xF0BFU, 0xB0D7U, 0x0059U, 0x5044U, 0xE0CAU,
0xC07EU, 0x70F0U, 0x20EDU, 0x9063U, 0x7051U, 0xC0DFU, 0x90C2U, 0x204CU, 0x00F8U, 0xB076U, 0xE06BU, 0x50E5U,
0x108DU, 0xA003U, 0xF01EU, 0x4090U, 0x6024U, 0xD0AAU, 0x80B7U, 0x3039U, 0x0067U, 0xB0E9U, 0xE0F4U, 0x507AU,
0x70CEU, 0xC040U, 0x905DU, 0x20D3U, 0x60BBU, 0xD035U, 0x8028U, 0x30A6U, 0x1012U, 0xA09CU, 0xF081U, 0x400FU,
0x30E4U, 0x806AU, 0xD077U, 0x60F9U, 0x404DU, 0xF0C3U, 0xA0DEU, 0x1050U, 0x5038U, 0xE0B6U, 0xB0ABU, 0x0025U,
0x2091U, 0x901FU, 0xC002U, 0x708CU, 0x40D2U, 0xF05CU, 0xA041U, 0x10CFU, 0x307BU, 0x80F5U, 0xD0E8U, 0x6066U,
0x200EU, 0x9080U, 0xC09DU, 0x7013U, 0x50A7U, 0xE029U, 0xB034U, 0x00BAU, 0xE088U, 0x5006U, 0x001BU, 0xB095U,
0x9021U, 0x20AFU, 0x70B2U, 0xC03CU, 0x8054U, 0x30DAU, 0x60C7U, 0xD049U, 0xF0FDU, 0x4073U, 0x106EU, 0xA0E0U,
0x90BEU, 0x2030U, 0x702DU, 0xC0A3U, 0xE017U, 0x5099U, 0x0084U, 0xB00AU, 0xF062U, 0x40ECU, 0x10F1U, 0xA07FU,
0x80CBU, 0x3045U, 0x6058U, 0xD0D6U
};
const uint32_t ENCODING_TABLE_1676[] =
{0x0000U, 0x0273U, 0x04E5U, 0x0696U, 0x09C9U, 0x0BBAU, 0x0D2CU, 0x0F5FU, 0x11E2U, 0x1391U, 0x1507U, 0x1774U,
0x182BU, 0x1A58U, 0x1CCEU, 0x1EBDU, 0x21B7U, 0x23C4U, 0x2552U, 0x2721U, 0x287EU, 0x2A0DU, 0x2C9BU, 0x2EE8U,
0x3055U, 0x3226U, 0x34B0U, 0x36C3U, 0x399CU, 0x3BEFU, 0x3D79U, 0x3F0AU, 0x411EU, 0x436DU, 0x45FBU, 0x4788U,
0x48D7U, 0x4AA4U, 0x4C32U, 0x4E41U, 0x50FCU, 0x528FU, 0x5419U, 0x566AU, 0x5935U, 0x5B46U, 0x5DD0U, 0x5FA3U,
0x60A9U, 0x62DAU, 0x644CU, 0x663FU, 0x6960U, 0x6B13U, 0x6D85U, 0x6FF6U, 0x714BU, 0x7338U, 0x75AEU, 0x77DDU,
0x7882U, 0x7AF1U, 0x7C67U, 0x7E14U, 0x804FU, 0x823CU, 0x84AAU, 0x86D9U, 0x8986U, 0x8BF5U, 0x8D63U, 0x8F10U,
0x91ADU, 0x93DEU, 0x9548U, 0x973BU, 0x9864U, 0x9A17U, 0x9C81U, 0x9EF2U, 0xA1F8U, 0xA38BU, 0xA51DU, 0xA76EU,
0xA831U, 0xAA42U, 0xACD4U, 0xAEA7U, 0xB01AU, 0xB269U, 0xB4FFU, 0xB68CU, 0xB9D3U, 0xBBA0U, 0xBD36U, 0xBF45U,
0xC151U, 0xC322U, 0xC5B4U, 0xC7C7U, 0xC898U, 0xCAEBU, 0xCC7DU, 0xCE0EU, 0xD0B3U, 0xD2C0U, 0xD456U, 0xD625U,
0xD97AU, 0xDB09U, 0xDD9FU, 0xDFECU, 0xE0E6U, 0xE295U, 0xE403U, 0xE670U, 0xE92FU, 0xEB5CU, 0xEDCAU, 0xEFB9U,
0xF104U, 0xF377U, 0xF5E1U, 0xF792U, 0xF8CDU, 0xFABEU, 0xFC28U, 0xFE5BU
};
#define F2(A,B,C) ( ( A & B ) | ( C & ( A | B ) ) )
#define F1(E,F,G) ( G ^ ( E & ( F ^ G ) ) )
struct sockaddr_in host1;
struct sockaddr_in host2;
int udp1;
int udp2;
fd_set udpset;
uint8_t buf[BUFSIZE];
char callsign[10U];
int dmrid;
uint32_t sha256_state[8U];
uint32_t sha256_total[2];
uint32_t sha256_buffer[32U];
uint32_t sha256_buflen;
bool bptc_rawData[196];
bool bptc_deInterData[196];
bool emb_raw[128U];
bool emb_data[72U];
int tx_srcid;
int tx_tgid;
int host1_tg;
char *host1_pw;
char *prefix;
static const unsigned char fillbuf[64] = { 0x80, 0 };
#define DISCONNECTED 0
#define CONNECTING 1
#define DMR_AUTH 2
#define DMR_CONF 3
#define DMR_OPTS 4
#define CONNECTED_RW 5
#define CONNECTED_RO 6
// frame type
#define DMRMMDVM_FRAMETYPE_VOICE 0
#define DMRMMDVM_FRAMETYPE_VOICESYNC 1
#define DMRMMDVM_FRAMETYPE_DATASYNC 2
// slot type
#define MMDVM_SLOTTYPE_HEADER 1
#define MMDVM_SLOTTYPE_TERMINATOR 2
void byteToBitsBE(unsigned char byte, bool* bits)
{
bits[0U] = (byte & 0x80U) == 0x80U;
bits[1U] = (byte & 0x40U) == 0x40U;
bits[2U] = (byte & 0x20U) == 0x20U;
bits[3U] = (byte & 0x10U) == 0x10U;
bits[4U] = (byte & 0x08U) == 0x08U;
bits[5U] = (byte & 0x04U) == 0x04U;
bits[6U] = (byte & 0x02U) == 0x02U;
bits[7U] = (byte & 0x01U) == 0x01U;
}
void bitsToByteBE(bool* bits, unsigned char* byte)
{
*byte = bits[0U] ? 0x80U : 0x00U;
*byte |= bits[1U] ? 0x40U : 0x00U;
*byte |= bits[2U] ? 0x20U : 0x00U;
*byte |= bits[3U] ? 0x10U : 0x00U;
*byte |= bits[4U] ? 0x08U : 0x00U;
*byte |= bits[5U] ? 0x04U : 0x00U;
*byte |= bits[6U] ? 0x02U : 0x00U;
*byte |= bits[7U] ? 0x01U : 0x00U;
}
int max(int x, int y)
{
if (x > y)
return x;
else
return y;
}
static inline void set_uint32(unsigned char* cp, uint32_t v)
{
memcpy(cp, &v, sizeof v);
}
void process_signal(int sig)
{
static uint32_t c1 = 0;
static uint32_t c2 = 0;
static int cnt = 0;
uint8_t b[20];
uint8_t out[55];
if(sig == SIGINT){
fprintf(stderr, "\n\nShutting down link\n");
b[0] = 'R';
b[1] = 'P';
b[2] = 'T';
b[3] = 'C';
b[4] = 'L';
b[5] = (dmrid >> 24) & 0xff;
b[6] = (dmrid >> 16) & 0xff;
b[7] = (dmrid >> 8) & 0xff;
b[8] = (dmrid >> 0) & 0xff;
sendto(udp1, b, 9, 0, (const struct sockaddr *)&host1, sizeof(host1));
#ifdef DEBUG
fprintf(stderr, "SEND DMR: ");
for(int i = 0; i < 9; ++i)
fprintf(stderr, "%02x ", b[i]);
fprintf(stderr, "\n");
#endif
close(udp1);
close(udp2);
exit(EXIT_SUCCESS);
}
if(sig == SIGALRM){
++cnt;
char tag[] = { 'R','P','T','P','I','N','G' };
memcpy(b, tag, 7);
b[7] = (dmrid >> 24) & 0xff;
b[8] = (dmrid >> 16) & 0xff;
b[9] = (dmrid >> 8) & 0xff;
b[10] = (dmrid >> 0) & 0xff;
sendto(udp1, b, 11, 0, (const struct sockaddr *)&host1, sizeof(host1));
#ifdef DEBUG
fprintf(stderr, "SEND DMR: ");
for(int i = 0; i < 11; ++i)
fprintf(stderr, "%02x ", b[i]);
fprintf(stderr, "\n");
#endif
alarm(5);
}
}
void hamming_encode15113_2(bool* d)
{
d[11] = d[0] ^ d[1] ^ d[2] ^ d[3] ^ d[5] ^ d[7] ^ d[8];
d[12] = d[1] ^ d[2] ^ d[3] ^ d[4] ^ d[6] ^ d[8] ^ d[9];
d[13] = d[2] ^ d[3] ^ d[4] ^ d[5] ^ d[7] ^ d[9] ^ d[10];
d[14] = d[0] ^ d[1] ^ d[2] ^ d[4] ^ d[6] ^ d[7] ^ d[10];
}
void hamming_encode1393(bool* d)
{
d[9] = d[0] ^ d[1] ^ d[3] ^ d[5] ^ d[6];
d[10] = d[0] ^ d[1] ^ d[2] ^ d[4] ^ d[6] ^ d[7];
d[11] = d[0] ^ d[1] ^ d[2] ^ d[3] ^ d[5] ^ d[7] ^ d[8];
d[12] = d[0] ^ d[2] ^ d[4] ^ d[5] ^ d[8];
}
void bptc_encode(const unsigned char* in, unsigned char* out)
{
//Extract
bool bData[96U];
byteToBitsBE(in[0U], bData + 0U);
byteToBitsBE(in[1U], bData + 8U);
byteToBitsBE(in[2U], bData + 16U);
byteToBitsBE(in[3U], bData + 24U);
byteToBitsBE(in[4U], bData + 32U);
byteToBitsBE(in[5U], bData + 40U);
byteToBitsBE(in[6U], bData + 48U);
byteToBitsBE(in[7U], bData + 56U);
byteToBitsBE(in[8U], bData + 64U);
byteToBitsBE(in[9U], bData + 72U);
byteToBitsBE(in[10U], bData + 80U);
byteToBitsBE(in[11U], bData + 88U);
for (unsigned int i = 0U; i < 196U; i++)
bptc_deInterData[i] = false;
unsigned int pos = 0U;
for (unsigned int a = 4U; a <= 11U; a++, pos++)
bptc_deInterData[a] = bData[pos];
for (unsigned int a = 16U; a <= 26U; a++, pos++)
bptc_deInterData[a] = bData[pos];
for (unsigned int a = 31U; a <= 41U; a++, pos++)
bptc_deInterData[a] = bData[pos];
for (unsigned int a = 46U; a <= 56U; a++, pos++)
bptc_deInterData[a] = bData[pos];
for (unsigned int a = 61U; a <= 71U; a++, pos++)
bptc_deInterData[a] = bData[pos];
for (unsigned int a = 76U; a <= 86U; a++, pos++)
bptc_deInterData[a] = bData[pos];
for (unsigned int a = 91U; a <= 101U; a++, pos++)
bptc_deInterData[a] = bData[pos];
for (unsigned int a = 106U; a <= 116U; a++, pos++)
bptc_deInterData[a] = bData[pos];
for (unsigned int a = 121U; a <= 131U; a++, pos++)
bptc_deInterData[a] = bData[pos];
//Error check
for (unsigned int r = 0U; r < 9U; r++) {
unsigned int pos = (r * 15U) + 1U;
hamming_encode15113_2(bptc_deInterData + pos);
}
bool col[13U];
for (unsigned int c = 0U; c < 15U; c++) {
unsigned int pos = c + 1U;
for (unsigned int a = 0U; a < 13U; a++) {
col[a] = bptc_deInterData[pos];
pos = pos + 15U;
}
hamming_encode1393(col);
pos = c + 1U;
for (unsigned int a = 0U; a < 13U; a++) {
bptc_deInterData[pos] = col[a];
pos = pos + 15U;
}
}
//Interleave
for (unsigned int i = 0U; i < 196U; i++)
bptc_rawData[i] = false;
for (unsigned int a = 0U; a < 196U; a++) {
unsigned int interleaveSequence = (a * 181U) % 196U;
bptc_rawData[interleaveSequence] = bptc_deInterData[a];
}
//Extract
bitsToByteBE(bptc_rawData + 0U, &out[0U]);
bitsToByteBE(bptc_rawData + 8U, &out[1U]);
bitsToByteBE(bptc_rawData + 16U, &out[2U]);
bitsToByteBE(bptc_rawData + 24U, &out[3U]);
bitsToByteBE(bptc_rawData + 32U, &out[4U]);
bitsToByteBE(bptc_rawData + 40U, &out[5U]);
bitsToByteBE(bptc_rawData + 48U, &out[6U]);
bitsToByteBE(bptc_rawData + 56U, &out[7U]);
bitsToByteBE(bptc_rawData + 64U, &out[8U]);
bitsToByteBE(bptc_rawData + 72U, &out[9U]);
bitsToByteBE(bptc_rawData + 80U, &out[10U]);
bitsToByteBE(bptc_rawData + 88U, &out[11U]);
unsigned char byte;
bitsToByteBE(bptc_rawData + 96U, &byte);
out[12U] = (out[12U] & 0x3FU) | ((byte >> 0) & 0xC0U);
out[20U] = (out[20U] & 0xFCU) | ((byte >> 4) & 0x03U);
bitsToByteBE(bptc_rawData + 100U, &out[21U]);
bitsToByteBE(bptc_rawData + 108U, &out[22U]);
bitsToByteBE(bptc_rawData + 116U, &out[23U]);
bitsToByteBE(bptc_rawData + 124U, &out[24U]);
bitsToByteBE(bptc_rawData + 132U, &out[25U]);
bitsToByteBE(bptc_rawData + 140U, &out[26U]);
bitsToByteBE(bptc_rawData + 148U, &out[27U]);
bitsToByteBE(bptc_rawData + 156U, &out[28U]);
bitsToByteBE(bptc_rawData + 164U, &out[29U]);
bitsToByteBE(bptc_rawData + 172U, &out[30U]);
bitsToByteBE(bptc_rawData + 180U, &out[31U]);
bitsToByteBE(bptc_rawData + 188U, &out[32U]);
}
unsigned char rs129_gmult(unsigned char a, unsigned char b)
{
if (a == 0U || b == 0U)
return 0U;
unsigned int i = LOG_TABLE[a];
unsigned int j = LOG_TABLE[b];
return EXP_TABLE[i + j];
}
void generate_header()
{
uint8_t sync_ms_data[] = { 0x0D,0x5D,0x7F,0x77,0xFD,0x75,0x70 };
uint8_t payload[33];
memset(payload, 0, sizeof(payload));
uint8_t lc[12];
{
memset(lc, 0, sizeof(lc));
//DESTID/TGID
lc[3] = buf[8];
lc[4] = buf[9];
lc[5] = buf[10];
//SRCID
lc[6] = buf[5];
lc[7] = buf[6];
lc[8] = buf[7];
uint8_t parity[4];
//RS129 Encode begin
for (unsigned int i = 0U; i < 3U + 1U; i++)
parity[i] = 0x00U;
for (unsigned int i = 0U; i < 9; i++) {
unsigned char dbyte = lc[i] ^ parity[3U - 1U];
for (int j = 3U - 1; j > 0; j--)
parity[j] = parity[j - 1] ^ rs129_gmult(POLY[j], dbyte);
parity[0] = rs129_gmult(POLY[0], dbyte);
}
//RS129 Encode end
lc[9] = parity[2] ^ 0x96;
lc[10] = parity[1] ^ 0x96;
lc[11] = parity[0] ^ 0x96;
}
memcpy(payload+13, sync_ms_data, sizeof(sync_ms_data));
{
uint8_t slottype[3];
memset(slottype, 0, sizeof(slottype));
slottype[0] = (1 << 4) & 0xF0;
slottype[0] |= (1 << 0) & 0x0FU;
//Golay2087 encoding
slottype[1U] = ENCODING_TABLE_2087[slottype[0]] & 0xFFU;
slottype[2U] = ENCODING_TABLE_2087[slottype[0]] >> 8;
payload[12U] = (payload[12U] & 0xC0U) | ((slottype[0U] >> 2) & 0x3FU);
payload[13U] = (payload[13U] & 0x0FU) | ((slottype[0U] << 6) & 0xC0U) | ((slottype[1U] >> 2) & 0x30U);
payload[19U] = (payload[19U] & 0xF0U) | ((slottype[1U] >> 2) & 0x0FU);
payload[20U] = (payload[20U] & 0x03U) | ((slottype[1U] << 6) & 0xC0U) | ((slottype[2U] >> 2) & 0x3CU);
}
bptc_encode(lc, payload);
memcpy(buf + 20, payload, 33);
}
void sha256_process_block(const unsigned char* buffer, unsigned int len)
{
const uint32_t* words = (uint32_t*)buffer;
unsigned int nwords = len / sizeof(uint32_t);
const uint32_t* endp = words + nwords;
uint32_t x[16];
uint32_t a = sha256_state[0];
uint32_t b = sha256_state[1];
uint32_t c = sha256_state[2];
uint32_t d = sha256_state[3];
uint32_t e = sha256_state[4];
uint32_t f = sha256_state[5];
uint32_t g = sha256_state[6];
uint32_t h = sha256_state[7];
sha256_total[0] += len;
if (sha256_total[0] < len)
++sha256_total[1];
#define rol(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
#define S0(x) (rol(x,25)^rol(x,14)^(x>>3))
#define S1(x) (rol(x,15)^rol(x,13)^(x>>10))
#define SS0(x) (rol(x,30)^rol(x,19)^rol(x,10))
#define SS1(x) (rol(x,26)^rol(x,21)^rol(x,7))
#define M(I) (tm = S1(x[(I-2)&0x0f]) + x[(I-7)&0x0f] + S0(x[(I-15)&0x0f]) + x[I&0x0f], x[I&0x0f] = tm)
#define R(A,B,C,D,E,F,G,H,K,M) do { t0 = SS0(A) + F2(A,B,C); \
t1 = H + SS1(E) + F1(E,F,G) + K + M; \
D += t1; H = t0 + t1; \
} while(0)
while (words < endp) {
uint32_t tm;
uint32_t t0, t1;
for (unsigned int t = 0U; t < 16U; t++) {
x[t] = SWAP(*words);
words++;
}
R( a, b, c, d, e, f, g, h, K( 0), x[ 0] );
R( h, a, b, c, d, e, f, g, K( 1), x[ 1] );
R( g, h, a, b, c, d, e, f, K( 2), x[ 2] );
R( f, g, h, a, b, c, d, e, K( 3), x[ 3] );
R( e, f, g, h, a, b, c, d, K( 4), x[ 4] );
R( d, e, f, g, h, a, b, c, K( 5), x[ 5] );
R( c, d, e, f, g, h, a, b, K( 6), x[ 6] );
R( b, c, d, e, f, g, h, a, K( 7), x[ 7] );
R( a, b, c, d, e, f, g, h, K( 8), x[ 8] );
R( h, a, b, c, d, e, f, g, K( 9), x[ 9] );
R( g, h, a, b, c, d, e, f, K(10), x[10] );
R( f, g, h, a, b, c, d, e, K(11), x[11] );
R( e, f, g, h, a, b, c, d, K(12), x[12] );
R( d, e, f, g, h, a, b, c, K(13), x[13] );
R( c, d, e, f, g, h, a, b, K(14), x[14] );
R( b, c, d, e, f, g, h, a, K(15), x[15] );
R( a, b, c, d, e, f, g, h, K(16), M(16) );
R( h, a, b, c, d, e, f, g, K(17), M(17) );
R( g, h, a, b, c, d, e, f, K(18), M(18) );
R( f, g, h, a, b, c, d, e, K(19), M(19) );
R( e, f, g, h, a, b, c, d, K(20), M(20) );
R( d, e, f, g, h, a, b, c, K(21), M(21) );
R( c, d, e, f, g, h, a, b, K(22), M(22) );
R( b, c, d, e, f, g, h, a, K(23), M(23) );
R( a, b, c, d, e, f, g, h, K(24), M(24) );
R( h, a, b, c, d, e, f, g, K(25), M(25) );
R( g, h, a, b, c, d, e, f, K(26), M(26) );
R( f, g, h, a, b, c, d, e, K(27), M(27) );
R( e, f, g, h, a, b, c, d, K(28), M(28) );
R( d, e, f, g, h, a, b, c, K(29), M(29) );
R( c, d, e, f, g, h, a, b, K(30), M(30) );
R( b, c, d, e, f, g, h, a, K(31), M(31) );
R( a, b, c, d, e, f, g, h, K(32), M(32) );
R( h, a, b, c, d, e, f, g, K(33), M(33) );
R( g, h, a, b, c, d, e, f, K(34), M(34) );
R( f, g, h, a, b, c, d, e, K(35), M(35) );
R( e, f, g, h, a, b, c, d, K(36), M(36) );
R( d, e, f, g, h, a, b, c, K(37), M(37) );
R( c, d, e, f, g, h, a, b, K(38), M(38) );
R( b, c, d, e, f, g, h, a, K(39), M(39) );
R( a, b, c, d, e, f, g, h, K(40), M(40) );
R( h, a, b, c, d, e, f, g, K(41), M(41) );
R( g, h, a, b, c, d, e, f, K(42), M(42) );
R( f, g, h, a, b, c, d, e, K(43), M(43) );
R( e, f, g, h, a, b, c, d, K(44), M(44) );
R( d, e, f, g, h, a, b, c, K(45), M(45) );
R( c, d, e, f, g, h, a, b, K(46), M(46) );
R( b, c, d, e, f, g, h, a, K(47), M(47) );
R( a, b, c, d, e, f, g, h, K(48), M(48) );
R( h, a, b, c, d, e, f, g, K(49), M(49) );
R( g, h, a, b, c, d, e, f, K(50), M(50) );
R( f, g, h, a, b, c, d, e, K(51), M(51) );
R( e, f, g, h, a, b, c, d, K(52), M(52) );
R( d, e, f, g, h, a, b, c, K(53), M(53) );
R( c, d, e, f, g, h, a, b, K(54), M(54) );
R( b, c, d, e, f, g, h, a, K(55), M(55) );
R( a, b, c, d, e, f, g, h, K(56), M(56) );
R( h, a, b, c, d, e, f, g, K(57), M(57) );
R( g, h, a, b, c, d, e, f, K(58), M(58) );
R( f, g, h, a, b, c, d, e, K(59), M(59) );
R( e, f, g, h, a, b, c, d, K(60), M(60) );
R( d, e, f, g, h, a, b, c, K(61), M(61) );
R( c, d, e, f, g, h, a, b, K(62), M(62) );
R( b, c, d, e, f, g, h, a, K(63), M(63) );
a = sha256_state[0] += a;
b = sha256_state[1] += b;
c = sha256_state[2] += c;
d = sha256_state[3] += d;
e = sha256_state[4] += e;
f = sha256_state[5] += f;
g = sha256_state[6] += g;
h = sha256_state[7] += h;
}
}
void sha256_generate(char *in, int len, char *out)
{
unsigned int bytes, size;
sha256_state[0] = 0x6a09e667UL;
sha256_state[1] = 0xbb67ae85UL;
sha256_state[2] = 0x3c6ef372UL;
sha256_state[3] = 0xa54ff53aUL;
sha256_state[4] = 0x510e527fUL;
sha256_state[5] = 0x9b05688cUL;
sha256_state[6] = 0x1f83d9abUL;
sha256_state[7] = 0x5be0cd19UL;
sha256_total[0] = sha256_total[1] = 0;
sha256_buflen = 0;
if (sha256_buflen != 0U) {
unsigned int left_over = sha256_buflen;
unsigned int add = 128U - left_over > len ? len : 128U - left_over;
memcpy(&((char*)sha256_buffer)[left_over], in, add);
sha256_buflen += add;
if (sha256_buflen > 64U) {
sha256_process_block((unsigned char*)sha256_buffer, sha256_buflen & ~63U);
sha256_buflen &= 63U;
memcpy(sha256_buffer, &((char*)sha256_buffer)[(left_over + add) & ~63U], sha256_buflen);
}
in += add;
len -= add;
}
if (len >= 64U) {
sha256_process_block(in, len & ~63U);
in += (len & ~63U);
len &= 63U;
}
if (len > 0U) {
unsigned int left_over = sha256_buflen;
memcpy(&((char*)sha256_buffer)[left_over], in, len);
left_over += len;
if (left_over >= 64U) {
sha256_process_block((unsigned char*)sha256_buffer, 64U);
left_over -= 64U;
memcpy(sha256_buffer, &sha256_buffer[16], left_over);
}
sha256_buflen = left_over;
}
bytes = sha256_buflen;
size = (bytes < 56) ? 64 / 4 : 64 * 2 / 4;
sha256_total[0] += bytes;
if (sha256_total[0] < bytes)
++sha256_total[1];
set_uint32((unsigned char*)&sha256_buffer[size - 2], SWAP((sha256_total[1] << 3) | (sha256_total[0] >> 29)));
set_uint32((unsigned char*)&sha256_buffer[size - 1], SWAP(sha256_total[0] << 3));
memcpy(&((char*)sha256_buffer)[bytes], fillbuf, (size - 2) * 4 - bytes);
sha256_process_block((unsigned char*)sha256_buffer, size * 4);
for (unsigned int i = 0U; i < 8U; i++)
set_uint32(out + i * sizeof(sha256_state[0]), SWAP(sha256_state[i]));
}
void lc_get_data(uint8_t *bytes)
{
bool pf, r;
uint8_t fid, options;
pf = (bytes[0U] & 0x80U) == 0x80U;
r = (bytes[0U] & 0x40U) == 0x40U;
fid = bytes[1U];
options = bytes[2U];
bytes[0U] = 0;
if (pf)
bytes[0U] |= 0x80U;
if (r)
bytes[0U] |= 0x40U;
bytes[1U] = fid;
bytes[2U] = options;
bytes[3U] = tx_tgid >> 16;
bytes[4U] = tx_tgid >> 8;
bytes[5U] = tx_tgid >> 0;
bytes[6U] = tx_srcid >> 16;
bytes[7U] = tx_srcid >> 8;
bytes[8U] = tx_srcid >> 0;
}
void lc_get_data_bits(bool* bits)
{
uint8_t bytes[9U];
memset(bytes, 0, 9);
lc_get_data(bytes);
byteToBitsBE(bytes[0U], bits + 0U);
byteToBitsBE(bytes[1U], bits + 8U);
byteToBitsBE(bytes[2U], bits + 16U);
byteToBitsBE(bytes[3U], bits + 24U);
byteToBitsBE(bytes[4U], bits + 32U);
byteToBitsBE(bytes[5U], bits + 40U);
byteToBitsBE(bytes[6U], bits + 48U);
byteToBitsBE(bytes[7U], bits + 56U);
byteToBitsBE(bytes[8U], bits + 64U);
}
void encode16114(bool* d)
{
d[11] = d[0] ^ d[1] ^ d[2] ^ d[3] ^ d[5] ^ d[7] ^ d[8];
d[12] = d[1] ^ d[2] ^ d[3] ^ d[4] ^ d[6] ^ d[8] ^ d[9];
d[13] = d[2] ^ d[3] ^ d[4] ^ d[5] ^ d[7] ^ d[9] ^ d[10];
d[14] = d[0] ^ d[1] ^ d[2] ^ d[4] ^ d[6] ^ d[7] ^ d[10];
d[15] = d[0] ^ d[2] ^ d[5] ^ d[6] ^ d[8] ^ d[9] ^ d[10];
}
void encode_qr1676(uint8_t* data)
{
uint32_t value = (data[0U] >> 1) & 0x7FU;
uint32_t cksum = ENCODING_TABLE_1676[value];
data[0U] = cksum >> 8;
data[1U] = cksum & 0xFFU;
}
void encode_embedded_data()
{
uint32_t crc;
unsigned short total = 0U;
lc_get_data_bits(emb_data);
for (unsigned int i = 0U; i < 72U; i += 8U) {
unsigned char c;
bitsToByteBE(emb_data + i, &c);
total += c;
}
total %= 31U;
crc = total;
bool data[128U];
memset(data, 0x00U, 128U * sizeof(bool));
data[106U] = (crc & 0x01U) == 0x01U;
data[90U] = (crc & 0x02U) == 0x02U;
data[74U] = (crc & 0x04U) == 0x04U;
data[58U] = (crc & 0x08U) == 0x08U;
data[42U] = (crc & 0x10U) == 0x10U;
uint32_t b = 0U;
for (uint32_t a = 0U; a < 11U; a++, b++)
data[a] = emb_data[b];
for (uint32_t a = 16U; a < 27U; a++, b++)
data[a] = emb_data[b];
for (uint32_t a = 32U; a < 42U; a++, b++)
data[a] = emb_data[b];
for (uint32_t a = 48U; a < 58U; a++, b++)
data[a] = emb_data[b];
for (uint32_t a = 64U; a < 74U; a++, b++)
data[a] = emb_data[b];
for (uint32_t a = 80U; a < 90U; a++, b++)
data[a] = emb_data[b];
for (uint32_t a = 96U; a < 106U; a++, b++)
data[a] = emb_data[b];
for (uint32_t a = 0U; a < 112U; a += 16U)
encode16114(data + a);
for (uint32_t a = 0U; a < 16U; a++)
data[a + 112U] = data[a + 0U] ^ data[a + 16U] ^ data[a + 32U] ^ data[a + 48U] ^ data[a + 64U] ^ data[a + 80U] ^ data[a + 96U];
b = 0U;
for (uint32_t a = 0U; a < 128U; a++) {
emb_raw[a] = data[b];
b += 16U;
if (b > 127U)
b -= 127U;
}
}
uint8_t get_embedded_data(uint8_t* data, uint8_t n)
{
if (n >= 1U && n < 5U) {
n--;
bool bits[40U];
memset(bits, 0x00U, 40U * sizeof(bool));
memcpy(bits + 4U, emb_raw + n * 32U, 32U * sizeof(bool));
uint8_t bytes[5U];
bitsToByteBE(bits + 0U, &bytes[0U]);
bitsToByteBE(bits + 8U, &bytes[1U]);
bitsToByteBE(bits + 16U, &bytes[2U]);
bitsToByteBE(bits + 24U, &bytes[3U]);
bitsToByteBE(bits + 32U, &bytes[4U]);
data[14U] = (data[14U] & 0xF0U) | (bytes[0U] & 0x0FU);
data[15U] = bytes[1U];
data[16U] = bytes[2U];
data[17U] = bytes[3U];
data[18U] = (data[18U] & 0x0FU) | (bytes[4U] & 0xF0U);
switch (n) {
case 0U:
return 1U;
case 3U:
return 2U;
default:
return 3U;
}
} else {
data[14U] &= 0xF0U;
data[15U] = 0x00U;
data[16U] = 0x00U;
data[17U] = 0x00U;
data[18U] &= 0x0FU;
return 0U;
}
}
void get_emb_data(uint8_t* data, uint8_t lcss)
{
uint8_t DMREMB[2U];
uint8_t m_colorcode = 1;
DMREMB[0U] = (m_colorcode << 4) & 0xF0U;
//DMREMB[0U] |= m_PI ? 0x08U : 0x00U;
DMREMB[0U] |= (lcss << 1) & 0x06U;
DMREMB[1U] = 0x00U;
encode_qr1676(DMREMB);
data[13U] = (data[13U] & 0xF0U) | ((DMREMB[0U] >> 4U) & 0x0FU);
data[14U] = (data[14U] & 0x0FU) | ((DMREMB[0U] << 4U) & 0xF0U);
data[18U] = (data[18U] & 0xF0U) | ((DMREMB[1U] >> 4U) & 0x0FU);
data[19U] = (data[19U] & 0x0FU) | ((DMREMB[1U] << 4U) & 0xF0U);
}
int process_connect(int connect_status, char *buf)
{
char in[100];
char out[400];
int len = 0;
char latitude[20U], longitude[20U];
memset(in, 0, 100);
memset(out, 0, 400);
switch(connect_status){
case CONNECTING:
connect_status = DMR_AUTH;
memcpy(in, &buf[6], 4);
memcpy(out, "RPTK", 4);
out[4] = (dmrid >> 24) & 0xff;
out[5] = (dmrid >> 16) & 0xff;
out[6] = (dmrid >> 8) & 0xff;
out[7] = (dmrid >> 0) & 0xff;
memcpy(&in[4], host1_pw, strlen(host1_pw));
sha256_generate(in, strlen(host1_pw) + sizeof(uint32_t), &out[8]);
len = 40;
fprintf(stderr, "Sending auth to DMR...\n");
break;
case DMR_AUTH:
connect_status = DMR_CONF;
memcpy(out, "RPTC", 4);
out[4] = (dmrid >> 24) & 0xff;
out[5] = (dmrid >> 16) & 0xff;
out[6] = (dmrid >> 8) & 0xff;
out[7] = (dmrid >> 0) & 0xff;
sprintf(latitude, "%08f", 0.0f);
sprintf(longitude, "%09f", 0.0f);
sprintf(&out[8], "%-8.8s%09u%09u%02u%02u%8.8s%9.9s%03d%-20.20s%-19.19s%c%-124.124s%-40.40s%-40.40s", callsign,
145000000, 145000000, 1, 1, latitude, longitude, 0, "Nowhere","Portugal", '4', "www.google.com", "20210101", "MMDVM"); // 302 bytes
len = 302;
fprintf(stderr, "Sending conf to DMR...\n");
break;
case DMR_CONF:
connect_status = CONNECTED_RW;
memcpy(buf, "DMRD", 4);
buf[4] = 0x00;
buf[5] = (((dmrid>99999999)?dmrid/100:dmrid) >> 16) & 0xff;
buf[6] = (((dmrid>99999999)?dmrid/100:dmrid) >> 8) & 0xff;
buf[7] = (((dmrid>99999999)?dmrid/100:dmrid) >> 0) & 0xff;
buf[8] = (host1_tg >> 16) & 0xff;
buf[9] = (host1_tg >> 8) & 0xff;
buf[10] = (host1_tg >> 0) & 0xff;
buf[11] = (dmrid >> 24) & 0xff;
buf[12] = (dmrid >> 16) & 0xff;
buf[13] = (dmrid >> 8) & 0xff;
buf[14] = (dmrid >> 0) & 0xff;
buf[15] = 0xa1;
buf[16] = 0xb6;
buf[17] = 0x01;
buf[18] = 0x00;
buf[19] = 0x00;
generate_header();
memcpy(out, buf, 55);
len = 55;
fprintf(stderr, "Connected to DMR\n");
break;
}
sendto(udp1, out, len, 0, (const struct sockaddr *)&host1, sizeof(host1));
#ifdef DEBUG
fprintf(stderr, "SEND DMR: ");
for(int i = 0; i < len; ++i)
fprintf(stderr, "%02x ", (uint8_t)out[i]);
fprintf(stderr, "\n");
#endif
return connect_status;
}
typedef struct wav_header_t {
// RIFF Header
char riff_header[4]; // Contains "RIFF"
int wav_size; // Size of the wav portion of the file, which follows the first 8 bytes. File size - 8
char wave_header[4]; // Contains "WAVE"
// Format Header
char fmt_header[4]; // Contains "fmt " (includes trailing space)
int fmt_chunk_size; // Should be 16 for PCM
short audio_format; // Should be 1 for PCM. 3 for IEEE Float
short num_channels; // Number of channels
int sample_rate; // Sample rate (hz)
int byte_rate; // Number of bytes per second. sample_rate * num_channels * Bytes Per Sample
short sample_alignment; // Number of bytes per sample. num_channels * Bytes Per Sample
short bit_depth; // Number of bits per sample
// Data
char data_header[4]; // Contains "data"
int data_bytes; // Number of bytes in data. Number of samples * num_channels * sample byte size
} wav_header;
int64_t time_ms()
{
struct timespec now;
timespec_get(&now, TIME_UTC);
return ((int64_t) now.tv_sec) * 1000 + ((int64_t) now.tv_nsec) / 1000000;
}
int main(int argc, char **argv)
{
struct sockaddr_in rx;
struct hostent *hp;
struct timeval tv;
char * host1_url;
char * host2_url;
int host1_port;
int host2_port;
int rxlen;
int r;
int udprx,maxudp;
socklen_t l = sizeof(host1);
time_t pong_time1;
int64_t rx_streamid = -1;
uint32_t tx_streamid = 0;
time_t rx_endt = 0;
FILE *rx_ambefile = NULL;
FILE *rx_wavefile = NULL;
FILE *tx_wavefile = NULL;
wav_header rx_wavheader;
int rx_ambefcnt = 0;
int tx_ambefcnt = 0;
uint8_t tx_ambefr[3][9];
int64_t trgus = 0;
int rx_srcid = 0;
bool txpending = false;
char rx_filename[80U];
//change stdout/stderr to line buffering
setvbuf(stdout, NULL, _IOLBF, 0);
setvbuf(stderr, NULL, _IOLBF, 0);
//change working directory to the executable directory
char exepath[4096] = {0};