-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSMMain.c
More file actions
1578 lines (1165 loc) · 32.9 KB
/
Copy pathSMMain.c
File metadata and controls
1578 lines (1165 loc) · 32.9 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
/*
Copyright (C) 2019-2020 Andrei Kopanchuk UZ7HO
This file is part of QtSoundModem
QtSoundModem 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.
QtSoundModem 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 QtSoundModem. If not, see http://www.gnu.org/licenses
*/
// UZ7HO Soundmodem Port by John Wiseman G8BPQ
#include "UZ7HOStuff.h"
#include "fftw3.h"
#include <time.h>
#include "ecc.h" // RS Constants
#include "hidapi.h"
#include <fcntl.h>
#include <errno.h>
#include <stdint.h>
void platformInit();
void RsCreate();
void detector_init();
void KISS_init();
void ax25_init();
void init_raduga();
void il2p_init(int il2p_debug);
void SoundFlush();
void BufferFull(short * Samples, int nSamples);
void PollReceivedSamples();
void chk_dcd1(int snd_ch, int buf_size);
void make_core_BPF(UCHAR snd_ch, short freq, short width);
void modulator(UCHAR snd_ch, int buf_size);
void sendAckModeAcks(int snd_ch);
void PktARDOPEncode(UCHAR * Data, int Len, int Chan);
void RUHEncode(unsigned char * Data, int Len, int chan);
void sendRSID(int Chan, int dropTX);
void SetupGPIOPTT();
int stricmp(const unsigned char * pStr1, const unsigned char *pStr2);
int gpioInitialise(void);
int OpenCOMPort(char * pPort, int speed, BOOL SetDTR, BOOL SetRTS, BOOL Quiet, int Stopbits);
void HAMLIBSetPTT(int PTTState);
void FLRigSetPTT(int PTTState);
void StartWatchdog();
void StopWatchdog();
void gpioWrite(unsigned gpio, unsigned level);
BOOL WriteCOMBlock(int fd, char * Block, int BytesToWrite);
void COMSetDTR(int fd);
void COMSetRTS(int fd);
void analiz_frame(int snd_ch, string * frame, char * code, boolean fecflag);
size_t write(int fd, void * buf, size_t count);
int close(int fd);
void SendMgmtPTT(int snd_ch, int PTTState);
void PollQSound();
BOOL KISSServ;
int KISSPort;
int NeedWaterfallHeaders = 0;
BOOL AGWServ;
int AGWPort;
int Number = 0; // Number waiting to be sent
int SoundIsPlaying = 0;
int UDPSoundIsPlaying = 0;
int Capturing = 0;
int txmin = 0;
int txmax = 0;
extern unsigned short buffer[2][1200];
extern int SoundMode;
extern int needRSID[4];
extern short * DMABuffer;
unsigned short * SendtoCard(unsigned short * buf, int n);
short * SoundInit();
void DoTX(int Chan);
void UDPPollReceivedSamples();
extern void checkforCWID();
extern int SampleNo;
extern int pnt_change[5]; // Freq Changed Flag
// fftw library interface
fftwf_complex *in, *out;
fftwf_plan p;
int FFTSize = 4096;
char * Wisdom;
void initfft()
{
fftwf_import_wisdom_from_string(Wisdom);
fftwf_set_timelimit(30);
#ifndef WIN32
printf("It may take up to 30 seconds for the program to start for the first time\n");
#endif
in = (fftwf_complex*)fftwf_malloc(sizeof(fftwf_complex) * 10000);
out = (fftwf_complex*)fftwf_malloc(sizeof(fftwf_complex) * 10000);
p = fftwf_plan_dft_1d(FFTSize, in, out, FFTW_FORWARD, FFTW_PATIENT);
Wisdom = fftwf_export_wisdom_to_string();
}
void dofft(short * inp, float * outr, float * outi)
{
int i;
fftwf_complex * fft = in;
for (i = 0; i < FFTSize; i++)
{
fft[0][0] = inp[0] * 1.0f;
fft[0][1] = 0;
fft++;
inp++;
}
fftwf_execute(p);
fft = out;
for (i = 0; i < FFTSize; i++)
{
outr[0] = fft[0][0];
outi[0] = fft[0][1];
fft++;
outi++;
outr++;
}
}
void freefft()
{
fftwf_destroy_plan(p);
fftwf_free(in);
fftwf_free(out);
}
int nonGUIMode = 0;
void soundMain()
{
// non platform specific initialisation
platformInit();
// initialise fft library
RsCreate(); // RS code for MPSK
detector_init();
KISS_init();
ax25_init();
init_raduga(); // Set up waterfall colour table
initfft();
il2p_init(1);
if (nonGUIMode)
{
Firstwaterfall = 0;
Secondwaterfall = 0;
}
OpenPTTPort();
}
void SampleSink(int LR, short Sample)
{
// This version is passed samples one at a time, as we don't have
// enough RAM in embedded systems to hold a full audio frame
// LR - 1 == Right Chan
#ifdef TEENSY
int work = Sample;
DMABuffer[Number++] = (work + 32768) >> 4; // 12 bit left justify
#else
if (SCO) // Single Channel Output - same to both L and R
{
DMABuffer[2 * Number] = Sample;
DMABuffer[1 + 2 * Number] = Sample;
}
else
{
if (LR) // Right
{
DMABuffer[1 + 2 * Number] = Sample;
DMABuffer[2 * Number] = 0;
}
else
{
DMABuffer[2 * Number] = Sample;
DMABuffer[1 + 2 * Number] = 0;
}
}
if (using48000)
{
// Need to upsample to 48K. Try just duplicating sample
uint16_t * ptr = &DMABuffer[2 * Number];
*(&ptr[1]) = *(ptr);
*(&ptr[2]) = *(ptr);
*(&ptr[3]) = *(ptr);
Number += 3;
}
Number++;
#endif
if (Number >= SendSize)
{
// send this buffer to sound interface
DMABuffer = SendtoCard(DMABuffer, SendSize);
Number = 0;
}
// Last120[Last120Put++] = Sample;
// if (Last120Put == (intN + 1))
// Last120Put = 0;
SampleNo++;
}
void Flush()
{
SoundFlush();
}
int ipow(int base, int exp)
{
int result = 1;
while (exp)
{
if (exp & 1)
result *= base;
exp >>= 1;
base *= base;
}
return result;
}
int NumberOfBitsNeeded(int PowerOfTwo)
{
int i;
for (i = 0; i <= 16; i++)
{
if ((PowerOfTwo & ipow(2, i)) != 0)
return i;
}
return 0;
}
int ReverseBits(int Index, int NumBits)
{
int i, Rev = 0;
for (i = 0; i < NumBits; i++)
{
Rev = (Rev * 2) | (Index & 1);
Index = Index / 2;
}
return Rev;
}
void FourierTransform(int NumSamples, short * RealIn, float * RealOut, float * ImagOut, int InverseTransform)
{
float AngleNumerator;
unsigned char NumBits;
int i, j, K, n, BlockSize, BlockEnd;
float DeltaAngle, DeltaAr;
float Alpha, Beta;
float TR, TI, AR, AI;
if (InverseTransform)
AngleNumerator = -2.0f * M_PI;
else
AngleNumerator = 2.0f * M_PI;
NumBits = NumberOfBitsNeeded(NumSamples);
for (i = 0; i < NumSamples; i++)
{
j = ReverseBits(i, NumBits);
RealOut[j] = RealIn[i];
ImagOut[j] = 0.0f; // Not using i in ImageIn[i];
}
BlockEnd = 1;
BlockSize = 2;
while (BlockSize <= NumSamples)
{
DeltaAngle = AngleNumerator / BlockSize;
Alpha = sinf(0.5f * DeltaAngle);
Alpha = 2.0f * Alpha * Alpha;
Beta = sinf(DeltaAngle);
i = 0;
while (i < NumSamples)
{
AR = 1.0f;
AI = 0.0f;
j = i;
for (n = 0; n < BlockEnd; n++)
{
K = j + BlockEnd;
TR = AR * RealOut[K] - AI * ImagOut[K];
TI = AI * RealOut[K] + AR * ImagOut[K];
RealOut[K] = RealOut[j] - TR;
ImagOut[K] = ImagOut[j] - TI;
RealOut[j] = RealOut[j] + TR;
ImagOut[j] = ImagOut[j] + TI;
DeltaAr = Alpha * AR + Beta * AI;
AI = AI - (Alpha * AI - Beta * AR);
AR = AR - DeltaAr;
j = j + 1;
}
i = i + BlockSize;
}
BlockEnd = BlockSize;
BlockSize = BlockSize * 2;
}
if (InverseTransform)
{
// Normalize the resulting time samples...
for (i = 0; i < NumSamples; i++)
{
RealOut[i] = RealOut[i] / NumSamples;
ImagOut[i] = ImagOut[i] / NumSamples;
}
}
}
int LastBusyCheck = 0;
extern UCHAR CurrentLevel;
#ifdef PLOTSPECTRUM
float dblMagSpectrum[206];
float dblMaxScale = 0.0f;
extern UCHAR Pixels[4096];
extern UCHAR * pixelPointer;
#endif
extern int blnBusyStatus;
int BusyDet = 5;
#define PLOTWATERFALL
int WaterfallActive = 1;
int SpectrumActive;
float BinSize;
extern int intLastStart;
extern int intLastStop;
void SMSortSignals2(float * dblMag, int intStartBin, int intStopBin, int intNumBins, float * dblAVGSignalPerBin, float * dblAVGBaselinePerBin);
BOOL SMBusyDetect3(int Chan, float * dblMag, int intStart, int intStop) // this only called while searching for leader ...once leader detected, no longer called.
{
// First sort signals and look at highes signals:baseline ratio..
float dblAVGSignalPerBinNarrow, dblAVGSignalPerBinWide, dblAVGBaselineNarrow, dblAVGBaselineWide;
float dblSlowAlpha = 0.2f;
static float dblAvgStoNNarrow[4] = { 0 }, dblAvgStoNWide[4] = {0};
int intNarrow = 8; // 8 x 11.72 Hz about 94 z
int intWide = ((intStop - intStart) * 2) / 3; //* 0.66);
int blnBusy = FALSE;
int BusyDet4th = BusyDet * BusyDet * BusyDet * BusyDet;
// First sort signals and look at highest signals:baseline ratio..
// First narrow band (~94Hz)
SMSortSignals2(dblMag, intStart, intStop, intNarrow, &dblAVGSignalPerBinNarrow, &dblAVGBaselineNarrow);
// Shouldn't dblAvgStoNNarrow, dblAvgStoNWide be static ??????
if (intLastStart == intStart && intLastStop == intStop)
dblAvgStoNNarrow[Chan] = (1 - dblSlowAlpha) * dblAvgStoNNarrow[Chan] + dblSlowAlpha * dblAVGSignalPerBinNarrow / dblAVGBaselineNarrow;
else
{
// This initializes the Narrow average after a bandwidth change
dblAvgStoNNarrow[Chan] = dblAVGSignalPerBinNarrow / dblAVGBaselineNarrow;
}
// Wide band (66% of current bandwidth)
SMSortSignals2(dblMag, intStart, intStop, intWide, &dblAVGSignalPerBinWide, &dblAVGBaselineWide);
if (intLastStart == intStart && intLastStop == intStop)
dblAvgStoNWide[Chan] = (1 - dblSlowAlpha) * dblAvgStoNWide[Chan] + dblSlowAlpha * dblAVGSignalPerBinWide / dblAVGBaselineWide;
else
{
// This initializes the Wide average after a bandwidth change
dblAvgStoNWide[Chan] = dblAVGSignalPerBinWide / dblAVGBaselineWide;
intLastStart = intStart;
intLastStop = intStop;
}
// Preliminary calibration...future a function of bandwidth and BusyDet.
blnBusy = (dblAvgStoNNarrow[Chan] > (3 + 0.008 * BusyDet4th)) || (dblAvgStoNWide[Chan] > (5 + 0.02 * BusyDet4th));
// if (BusyDet == 0)
// blnBusy = FALSE; // 0 Disables check ?? Is this the best place to do this?
// WriteDebugLog(LOGDEBUG, "Busy %d Wide %f Narrow %f", blnBusy, dblAvgStoNWide, dblAvgStoNNarrow);
return blnBusy;
}
extern int compare(const void *p1, const void *p2);
void SMSortSignals2(float * dblMag, int intStartBin, int intStopBin, int intNumBins, float * dblAVGSignalPerBin, float * dblAVGBaselinePerBin)
{
// puts the top intNumber of bins between intStartBin and intStopBin into dblAVGSignalPerBin, the rest into dblAvgBaselinePerBin
// for decent accuracy intNumBins should be < 75% of intStopBin-intStartBin)
// This version uses a native sort function which is much faster and reduces CPU loading significantly on wide bandwidths.
float dblSort[8192];
float dblSum1 = 0, dblSum2 = 0;
int numtoSort = (intStopBin - intStartBin) + 1, i;
memcpy(dblSort, &dblMag[intStartBin], numtoSort * sizeof(float));
qsort((void *)dblSort, numtoSort, sizeof(float), compare);
for (i = numtoSort - 1; i >= 0; i--)
{
if (i >= (numtoSort - intNumBins))
dblSum1 += dblSort[i];
else
dblSum2 += dblSort[i];
}
*dblAVGSignalPerBin = dblSum1 / intNumBins;
*dblAVGBaselinePerBin = dblSum2 / (intStopBin - intStartBin - intNumBins - 1);
}
extern void updateDCD(int Chan, BOOL State);
void SMUpdateBusyDetector(int LR, float * Real, float *Imag)
{
// Energy based detector for each channel.
// Fed from FFT generated for waterfall display
// FFT size is 4096
float dblMag[4096];
static BOOL blnLastBusyStatus[4];
float dblMagAvg = 0;
int i, chan;
if (Now - LastBusyCheck < 100) // ??
return;
LastBusyCheck = Now;
// We need to run busy test on the frequncies used by each modem.
for (chan = 0; chan < 4; chan++)
{
int Low, High, Start, End;
if (soundChannel[chan] != (LR + 1)) // on this side of soundcard
continue;
Low = tx_freq[chan] - txbpf[chan] / 2;
High = tx_freq[chan] + txbpf[chan] / 2;
if (Low < 100)
continue;
if (High > 3300)
continue;
// Low = tx_freq[chan] - 0.5*rx_shift[chan];
// High = tx_freq[chan] + 0.5*rx_shift[chan];
// BinSize is width of each fft bin in Hz
Start = (Low / BinSize); // First and last bins to process
End = (High / BinSize);
for (i = Start; i < End; i++)
{
dblMag[i] = powf(Real[i], 2) + powf(Imag[i], 2); // first pass
dblMagAvg += dblMag[i];
}
blnBusyStatus = SMBusyDetect3(chan, dblMag, Start, End);
if (blnBusyStatus && !blnLastBusyStatus[chan])
{
// Debugprintf("Ch %d Busy True", chan);
updateDCD(chan, TRUE);
}
else if (blnLastBusyStatus[chan] && !blnBusyStatus)
{
// Debugprintf("Ch %d Busy False", chan);
updateDCD(chan, FALSE);
}
blnLastBusyStatus[chan] = blnBusyStatus;
}
}
extern short rawSamples[2400]; // Get Frame Type need 2400 and we may add 1200
int rawSamplesLength = 0;
extern int maxrawSamplesLength;
void ProcessNewSamples(short * Samples, int nSamples)
{
if (SoundIsPlaying == FALSE && UDPSoundIsPlaying == FALSE)
BufferFull(Samples, nSamples);
};
void doCalib(int Chan, int Act)
{
if (Chan == 0 && calib_mode[1])
return;
if (Chan == 1 && calib_mode[0])
return;
if (Act == 0)
{
calib_mode[Chan] = 0;
tx_status[Chan] = TX_SILENCE; // Stop TX
SoundIsPlaying = 0;
Flush();
RadioPTT(Chan, 0);
Debugprintf("Stop Calib");
}
else
{
if (calib_mode[Chan] == 0)
SampleNo = 0;
calib_mode[Chan] = Act;
}
}
int Freq_Change(int Chan, int Freq)
{
int low, high;
low = round(rx_shift[Chan] / 2 + (RCVR[Chan] * rcvr_offset[Chan]));
high = round(RX_Samplerate / 2 - (rx_shift[Chan] / 2 + RCVR[Chan] * rcvr_offset[Chan]));
if (Freq < 300)
return rx_freq[Chan]; // Dont allow change
if (Freq < low)
return rx_freq[Chan]; // Dont allow change
if (Freq > high)
return rx_freq[Chan]; // Dont allow change
rx_freq[Chan] = Freq;
tx_freq[Chan] = Freq;
pnt_change[Chan] = TRUE;
NeedWaterfallHeaders = TRUE;
return Freq;
}
void MainLoop()
{
// Called by background thread every 10 ms (maybe)
// Actually we may have two cards
// Original only allowed one channel per card.
// I think we should be able to run more, ie two or more
// modems on same soundcard channel
// So All the soundcard stuff will need to be generalised
if (UDPServ)
UDPPollReceivedSamples();
if (SoundMode == 5)
PollQSound();
if (SoundMode == 3)
UDPPollReceivedSamples();
else
PollReceivedSamples();
for (int i = 0; i < 4; i++)
{
if (modem_mode[i] == MODE_ARDOP)
{
chk_dcd1(i, 512);
}
}
DoTX(0);
DoTX(1);
DoTX(2);
DoTX(3);
}
int ARDOPSendToCard(int Chan, int Len)
{
// Send Next Block of samples to the soundcard
short * in = &ARDOPTXBuffer[Chan][ARDOPTXPtr[Chan]]; // Enough to hold whole frame of samples
short * out = DMABuffer;
int LR = modemtoSoundLR[Chan];
int i;
for (i = 0; i < Len; i++)
{
if (SCO) // Single Channel Output - same to both L and R
{
*out++ = *in;
*out++ = *in++;
}
else
{
if (LR) // Right
{
*out++ = 0;
*out++ = *in++;
}
else
{
*out++ = *in++;
*out++ = 0;
}
}
}
DMABuffer = SendtoCard(DMABuffer, Len);
ARDOPTXPtr[Chan] += Len;
// See if end of buffer
if (ARDOPTXPtr[Chan] > ARDOPTXLen[Chan])
return 1;
return 0;
}
void DoTX(int Chan)
{
// This kicks off a send sequence or calibrate
// printtick("dotx");
if (calib_mode[Chan])
{
// Maybe new calib or continuation
if (pnt_change[Chan])
{
make_core_BPF(Chan, rx_freq[Chan], bpf[Chan]);
make_core_TXBPF(Chan, tx_freq[Chan], txbpf[Chan]);
pnt_change[Chan] = FALSE;
}
// Note this may block in SendtoCard
modulator(Chan, tx_bufsize);
return;
}
// I think we have to detect NO_DATA here and drop PTT and return to SILENCE
if (tx_status[Chan] == TX_NO_DATA)
{
Flush();
Debugprintf("TX Complete %d", SampleNo);
RadioPTT(Chan, 0);
Continuation[Chan] = 0;
tx_status[Chan] = TX_SILENCE;
// We should now send any ackmode acks as the channel is now free for dest to reply
sendAckModeAcks(Chan);
}
if (tx_status[Chan] != TX_SILENCE)
{
// Continue the send
if (modem_mode[Chan] == MODE_ARDOP || modem_mode[Chan] == MODE_RUH)
{
// if (SeeIfCardBusy())
// return 0;
if (ARDOPSendToCard(Chan, SendSize) == 1)
{
// End of TX
Number = 0;
Flush();
// See if more to send. If so, don't drop PTT
if (all_frame_buf[Chan].Count)
{
SoundIsPlaying = TRUE;
Number = 0;
Continuation[Chan] = 1;
Debugprintf("TX Continuing");
string * myTemp = Strings(&all_frame_buf[Chan], 0); // get message
string * tx_data;
if ((myTemp->Data[0] & 0x0f) == 12) // ACKMODE
{
// Save copy then copy data up 3 bytes
Add(&KISS_acked[Chan], duplicateString(myTemp));
mydelete(myTemp, 0, 3);
myTemp->Length -= sizeof(void *);
}
else
{
// Just remove control
mydelete(myTemp, 0, 1);
}
tx_data = duplicateString(myTemp); // so can free original below
Delete(&all_frame_buf[Chan], 0); // This will invalidate temp
AGW_AX25_frame_analiz(Chan, FALSE, tx_data);
put_frame(Chan, tx_data, "", TRUE, FALSE);
if (modem_mode[Chan] == MODE_ARDOP)
PktARDOPEncode(tx_data->Data, tx_data->Length - 2, Chan);
else
RUHEncode(tx_data->Data, tx_data->Length - 2, Chan);
freeString(tx_data);
// Samples are now in DMABuffer = Send first block
DMABuffer = SoundInit();
ARDOPSendToCard(Chan, SendSize);
tx_status[Chan] = TX_FRAME;
return;
}
Debugprintf("TX Complete %d", SampleNo);
RadioPTT(Chan, 0);
Continuation[Chan] = 0;
tx_status[Chan] = TX_SILENCE;
// We should now send any ackmode acks as the channel is now free for dest to reply
}
return;
}
modulator(Chan, tx_bufsize);
return;
}
if (SoundIsPlaying || UDPSoundIsPlaying)
return;
// Not doing anything so see if we have anything new to send
// See if frequency has changed
if (pnt_change[Chan])
{
make_core_BPF(Chan, rx_freq[Chan], bpf[Chan]);
make_core_TXBPF(Chan, tx_freq[Chan], txbpf[Chan]);
pnt_change[Chan] = FALSE;
}
// See if we need an RSID
if (needRSID[Chan])
{
needRSID[Chan] = 0;
// Note this may block in SampleSink
Debugprintf("Sending RSID");
sendRSID(Chan, all_frame_buf[Chan].Count == 0);
return;
}
if (all_frame_buf[Chan].Count == 0)
return;
// Start a new send. modulator should handle TXD etc
checkforCWID(); // See if need to start CWID timer in afteractivity mode
Debugprintf("TX Start");
SampleNo = 0;
SoundIsPlaying = TRUE;
RadioPTT(Chan, 1);
Number = 0;
if (modem_mode[Chan] == MODE_ARDOP)
{
// I think ARDOP will have to generate a whole frame of samples
// then send them out a bit at a time to avoid stopping here for
// possibly 10's of seconds
// Can do this here as unlike normal ardop we don't need to run on Teensy
// to 12000 sample rate we need either 24K or 48K per second, depending on
// where we do the stereo mux.
// Slowest rate is 50 baud, so a 255 byte packet would take about a minute
// allowing for RS overhead. Not really realistic put perhaps should be possible.
// RAM isn't an issue so maybe allocate 2 MB.
// ?? Should we allow two ARDOP modems - could make sense if we can run sound
// card channels independently
string * myTemp = Strings(&all_frame_buf[Chan], 0); // get message
string * tx_data;
if ((myTemp->Data[0] & 0x0f) == 12) // ACKMODE
{
// Save copy then copy data up 3 bytes
Add(&KISS_acked[Chan], duplicateString(myTemp));
mydelete(myTemp, 0, 3);
myTemp->Length -= sizeof(void *);
}
else
{
// Just remove control
mydelete(myTemp, 0, 1);
}
tx_data = duplicateString(myTemp); // so can free original below
Delete(&all_frame_buf[Chan], 0); // This will invalidate temp
AGW_AX25_frame_analiz(Chan, FALSE, tx_data);
put_frame(Chan, tx_data, "", TRUE, FALSE);
PktARDOPEncode(tx_data->Data, tx_data->Length - 2, Chan);
freeString(tx_data);
// Samples are now in DMABuffer = Send first block
ARDOPSendToCard(Chan, SendSize);
tx_status[Chan] = TX_FRAME;
}
else if (modem_mode[Chan] == MODE_RUH)
{
// Same as for ARDOP. Generate a whole frame of samples
// then send them out a bit at a time to avoid stopping here
// We allow two RUH modems
string * myTemp = Strings(&all_frame_buf[Chan], 0); // get message
string * tx_data;
if ((myTemp->Data[0] & 0x0f) == 12) // ACKMODE
{
// Save copy then copy data up 3 bytes
Add(&KISS_acked[Chan], duplicateString(myTemp));
mydelete(myTemp, 0, 3);
myTemp->Length -= sizeof(void *);
}
else
{
// Just remove control
mydelete(myTemp, 0, 1);
}
tx_data = duplicateString(myTemp); // so can free original below
Delete(&all_frame_buf[Chan], 0); // This will invalidate temp
AGW_AX25_frame_analiz(Chan, FALSE, tx_data);
put_frame(Chan, tx_data, "", TRUE, FALSE);
RUHEncode(tx_data->Data, tx_data->Length - 2, Chan);
freeString(tx_data);
// Samples are now in DMABuffer = Send first block
ARDOPSendToCard(Chan, SendSize);
tx_status[Chan] = TX_FRAME;
}
else
modulator(Chan, tx_bufsize);
return;
}
void RX2TX(int snd_ch)
{
if (snd_status[snd_ch] == SND_IDLE)
{
DoTX(snd_ch);
}
}
// PTT Stuff
int hPTTDevice = 0;
char PTTPort[80] = ""; // Port for Hardware PTT - may be same as control port.
int PTTBAUD = 19200;
int PTTMode = PTTRTS; // PTT Control Flags.
char PTTOnString[128] = "";
char PTTOffString[128] = "";
UCHAR PTTOnCmd[64];
UCHAR PTTOnCmdLen = 0;