-
Notifications
You must be signed in to change notification settings - Fork 0
/
Waveout.c.bak
1178 lines (879 loc) · 25.1 KB
/
Waveout.c.bak
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
//
// Passes audio samples to the sound interface
// Windows uses WaveOut
// Nucleo uses DMA
// Linux will use ALSA
// This is the Windows Version
#define _CRT_SECURE_NO_DEPRECATE
#define _USE_32BIT_TIME_T
#include <windows.h>
#include <mmsystem.h>
#ifdef USE_DIREWOLF
#include "direwolf/fsk_demod_state.h"
#include "direwolf/demod_afsk.h"
#endif
#pragma comment(lib, "winmm.lib")
void printtick(char * msg);
void PollReceivedSamples();
HANDLE OpenCOMPort(VOID * pPort, int speed, BOOL SetDTR, BOOL SetRTS, BOOL Quiet, int Stopbits);
VOID COMSetDTR(HANDLE fd);
VOID COMClearDTR(HANDLE fd);
VOID COMSetRTS(HANDLE fd);
VOID COMClearRTS(HANDLE fd);
VOID processargs(int argc, char * argv[]);
#include <math.h>
#include "ARDOPC.h"
void GetSoundDevices();
#ifdef LOGTOHOST
// Log output sent to host instead of File
#define LOGBUFFERSIZE 2048
char LogToHostBuffer[LOGBUFFERSIZE];
int LogToHostBufferLen;
#endif
// Windows works with signed samples +- 32767
// STM32 DAC uses unsigned 0 - 4095
// Currently use 1200 samples for TX but 480 for RX to reduce latency
short buffer[2][SendSize]; // Two Transfer/DMA buffers of 0.1 Sec
short inbuffer[5][ReceiveSize]; // Input Transfer/ buffers of 0.1 Sec
BOOL Loopback = FALSE;
//BOOL Loopback = TRUE;
char CaptureDevice[80] = "0"; //"2";
char PlaybackDevice[80] = "0"; //"1";
char * CaptureDevices = NULL;
char * PlaybackDevices = NULL;
int CaptureCount = 0;
int PlaybackCount = 0;
int CaptureIndex = -1; // Card number
int PlayBackIndex = -1;
char CaptureNames[16][MAXPNAMELEN + 2]= {""};
char PlaybackNames[16][MAXPNAMELEN + 2]= {""};
WAVEFORMATEX wfx = { WAVE_FORMAT_PCM, 1, 12000, 12000, 2, 16, 0 };
HWAVEOUT hWaveOut = 0;
HWAVEIN hWaveIn = 0;
WAVEHDR header[2] =
{
{(char *)buffer[0], 0, 0, 0, 0, 0, 0, 0},
{(char *)buffer[1], 0, 0, 0, 0, 0, 0, 0}
};
WAVEHDR inheader[5] =
{
{(char *)inbuffer[0], 0, 0, 0, 0, 0, 0, 0},
{(char *)inbuffer[1], 0, 0, 0, 0, 0, 0, 0},
{(char *)inbuffer[2], 0, 0, 0, 0, 0, 0, 0},
{(char *)inbuffer[3], 0, 0, 0, 0, 0, 0, 0},
{(char *)inbuffer[4], 0, 0, 0, 0, 0, 0, 0}
};
WAVEOUTCAPS pwoc;
WAVEINCAPS pwic;
unsigned int RTC = 0;
void InitSound(BOOL Quiet);
void HostPoll();
void TCPHostPoll();
void SerialHostPoll();
BOOL WriteCOMBlock(HANDLE fd, char * Block, int BytesToWrite);
int Ticks;
LARGE_INTEGER Frequency;
LARGE_INTEGER StartTicks;
LARGE_INTEGER NewTicks;
int LastNow;
extern void Generate50BaudTwoToneLeaderTemplate();
extern BOOL blnDISCRepeating;
#define TARGET_RESOLUTION 1 // 1-millisecond target resolution
VOID __cdecl Debugprintf(const char * format, ...)
{
char Mess[10000];
va_list(arglist);
va_start(arglist, format);
vsprintf(Mess, format, arglist);
strcat(Mess, "\r\n");
WriteDebugLog(6, Mess);
return;
}
BOOL CtrlHandler(DWORD fdwCtrlType)
{
switch( fdwCtrlType )
{
// Handle the CTRL-C signal.
case CTRL_C_EVENT:
printf( "Ctrl-C event\n\n" );
blnClosing = TRUE;
Beep( 750, 300 );
Sleep(1000);
return( TRUE );
// CTRL-CLOSE: confirm that the user wants to exit.
case CTRL_CLOSE_EVENT:
blnClosing = TRUE;
printf( "Ctrl-Close event\n\n" );
Sleep(20000);
Beep( 750, 300 );
return( TRUE );
// Pass other signals to the next handler.
case CTRL_BREAK_EVENT:
Beep( 900, 200 );
printf( "Ctrl-Break event\n\n" );
blnClosing = TRUE;
Beep( 750, 300 );
return FALSE;
case CTRL_LOGOFF_EVENT:
Beep( 1000, 200 );
printf( "Ctrl-Logoff event\n\n" );
return FALSE;
case CTRL_SHUTDOWN_EVENT:
Beep( 750, 500 );
printf( "Ctrl-Shutdown event\n\n" );
blnClosing = TRUE;
Beep( 750, 300 );
return FALSE;
default:
return FALSE;
}
}
void main(int argc, char * argv[])
{
TIMECAPS tc;
unsigned int wTimerRes;
DWORD t, lastt = 0;
int i = 0;
SetConsoleCtrlHandler((PHANDLER_ROUTINE)CtrlHandler, TRUE);
if (timeGetDevCaps(&tc, sizeof(TIMECAPS)) != TIMERR_NOERROR)
{
// Error; application can't continue.
}
wTimerRes = min(max(tc.wPeriodMin, TARGET_RESOLUTION), tc.wPeriodMax);
timeBeginPeriod(wTimerRes);
t = timeGetTime();
WriteDebugLog(LOGALERT, "ARDOPC Version %s", ProductVersion);
processargs(argc, argv);
if (HostPort[0])
{
char *pkt = strlop(HostPort, '/');
if (_memicmp(HostPort, "COM", 3) == 0)
{
SerialMode = 1;
port = atoi(HostPort + 3);
}
else
port = atoi(HostPort);
if (pkt)
pktport = atoi(pkt);
}
_strupr(CaptureDevice);
_strupr(PlaybackDevice);
if (PTTPort[0])
{
char * Baud = strlop(PTTPort, ':');
if (Baud)
PTTBAUD = atoi(Baud);
hPTTDevice = OpenCOMPort(PTTPort, PTTBAUD, FALSE, FALSE, FALSE, 0);
}
if (CATPort[0])
{
char * Baud = strlop(CATPort, ':');
if (strcmp(CATPort, PTTPort) == 0)
{
hCATDevice = hPTTDevice;
}
else
{
if (Baud)
CATBAUD = atoi(Baud);
hCATDevice = OpenCOMPort(CATPort, CATBAUD, FALSE, FALSE, FALSE, 0);
}
}
if (hCATDevice)
{
WriteDebugLog(LOGALERT, "CAT Control on port %s", CATPort);
COMSetRTS(hPTTDevice);
COMSetDTR(hPTTDevice);
if (PTTOffCmdLen)
{
WriteDebugLog(LOGALERT, "PTT using CAT Port", CATPort);
RadioControl = TRUE;
}
}
else
{
// Warn of -u and -k defined but no CAT Port
if (PTTOffCmdLen)
WriteDebugLog(LOGALERT, "Warning PTT Off string defined but no CAT port", CATPort);
}
if (hPTTDevice)
{
WriteDebugLog(LOGALERT, "Using RTS on port %s for PTT", PTTPort);
COMClearRTS(hPTTDevice);
COMClearDTR(hPTTDevice);
RadioControl = TRUE;
}
QueryPerformanceFrequency(&Frequency);
Frequency.QuadPart /= 1000; // Microsecs
QueryPerformanceCounter(&StartTicks);
GetSoundDevices();
if(!SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS))
printf("Failed to set High Priority (%d)\n"), GetLastError();
ardopmain();
}
unsigned int getTicks()
{
return timeGetTime();
// QueryPerformanceCounter(&NewTicks);
// return (int)(NewTicks.QuadPart - StartTicks.QuadPart) / Frequency.QuadPart;
}
void printtick(char * msg)
{
QueryPerformanceCounter(&NewTicks);
WriteDebugLog(LOGCRIT, "%s %i\r", msg, Now - LastNow);
LastNow = Now;
}
void txSleep(int mS)
{
// called while waiting for next TX buffer. Run background processes
PollReceivedSamples(); // discard any received samples
if (SerialMode)
SerialHostPoll();
else
TCPHostPoll();
Sleep(mS);
if (PKTLEDTimer && Now > PKTLEDTimer)
{
PKTLEDTimer = 0;
SetLED(PKTLED, 0); // turn off packet rxed led
}
}
int PriorSize = 0;
int Index = 0; // DMA TX Buffer being used 0 or 1
int inIndex = 0; // DMA Buffer being used
FILE * wavfp1;
BOOL DMARunning = FALSE; // Used to start DMA on first write
short * SendtoCard(unsigned short * buf, int n)
{
header[Index].dwBufferLength = n * 2;
waveOutPrepareHeader(hWaveOut, &header[Index], sizeof(WAVEHDR));
waveOutWrite(hWaveOut, &header[Index], sizeof(WAVEHDR));
// wait till previous buffer is complete
while (!(header[!Index].dwFlags & WHDR_DONE))
{
txSleep(10); // Run buckground while waiting
}
waveOutUnprepareHeader(hWaveOut, &header[!Index], sizeof(WAVEHDR));
Index = !Index;
return &buffer[Index][0];
}
// // This generates a nice musical pattern for sound interface testing
// for (t = 0; t < sizeof(buffer); ++t)
// buffer[t] =((((t * (t >> 8 | t >> 9) & 46 & t >> 8)) ^ (t & t >> 13 | t >> 6)) & 0xFF);
void GetSoundDevices()
{
int i;
WriteDebugLog(LOGALERT, "Capture Devices");
CaptureCount = waveInGetNumDevs();
CaptureDevices = malloc((MAXPNAMELEN + 2) * CaptureCount);
CaptureDevices[0] = 0;
for (i = 0; i < CaptureCount; i++)
{
waveInOpen(&hWaveIn, i, &wfx, 0, 0, CALLBACK_NULL); //WAVE_MAPPER
waveInGetDevCaps((UINT_PTR)hWaveIn, &pwic, sizeof(WAVEINCAPS));
if (CaptureDevices)
strcat(CaptureDevices, ",");
strcat(CaptureDevices, pwic.szPname);
WriteDebugLog(LOGALERT, "%d %s", i, pwic.szPname);
memcpy(&CaptureNames[i][0], pwic.szPname, MAXPNAMELEN);
_strupr(&CaptureNames[i][0]);
}
WriteDebugLog(LOGALERT, "Playback Devices");
PlaybackCount = waveOutGetNumDevs();
PlaybackDevices = malloc((MAXPNAMELEN + 2) * PlaybackCount);
PlaybackDevices[0] = 0;
for (i = 0; i < PlaybackCount; i++)
{
waveOutOpen(&hWaveOut, i, &wfx, 0, 0, CALLBACK_NULL); //WAVE_MAPPER
waveOutGetDevCaps((UINT_PTR)hWaveOut, &pwoc, sizeof(WAVEOUTCAPS));
if (PlaybackDevices[0])
strcat(PlaybackDevices, ",");
strcat(PlaybackDevices, pwoc.szPname);
WriteDebugLog(LOGALERT, "%i %s", i, pwoc.szPname);
memcpy(&PlaybackNames[i][0], pwoc.szPname, MAXPNAMELEN);
_strupr(&PlaybackNames[i][0]);
waveOutClose(hWaveOut);
}
}
void InitSound(BOOL Report)
{
int i, ret;
header[0].dwFlags = WHDR_DONE;
header[1].dwFlags = WHDR_DONE;
if (strlen(PlaybackDevice) <= 2)
PlayBackIndex = atoi(PlaybackDevice);
else
{
// Name instead of number. Look for a substring match
for (i = 0; i < PlaybackCount; i++)
{
if (strstr(&PlaybackNames[i][0], PlaybackDevice))
{
PlayBackIndex = i;
break;
}
}
}
ret = waveOutOpen(&hWaveOut, PlayBackIndex, &wfx, 0, 0, CALLBACK_NULL); //WAVE_MAPPER
if (ret)
WriteDebugLog(LOGALERT, "Failed to open WaveOut Device %s Error %d", PlaybackDevice, ret);
else
{
ret = waveOutGetDevCaps((UINT_PTR)hWaveOut, &pwoc, sizeof(WAVEOUTCAPS));
if (Report)
WriteDebugLog(LOGALERT, "Opened WaveOut Device %s", pwoc.szPname);
}
if (strlen(CaptureDevice) <= 2)
CaptureIndex = atoi(CaptureDevice);
else
{
// Name instead of number. Look for a substring match
for (i = 0; i < CaptureCount; i++)
{
if (strstr(&CaptureNames[i][0], CaptureDevice))
{
CaptureIndex = i;
break;
}
}
}
ret = waveInOpen(&hWaveIn, CaptureIndex, &wfx, 0, 0, CALLBACK_NULL); //WAVE_MAPPER
if (ret)
WriteDebugLog(LOGALERT, "Failed to open WaveIn Device %s Error %d", CaptureDevice, ret);
else
{
ret = waveInGetDevCaps((UINT_PTR)hWaveIn, &pwic, sizeof(WAVEINCAPS));
if (Report)
WriteDebugLog(LOGALERT, "Opened WaveIn Device %s", pwic.szPname);
}
// wavfp1 = fopen("s:\\textxxx.wav", "wb");
for (i = 0; i < NumberofinBuffers; i++)
{
inheader[i].dwBufferLength = ReceiveSize * 2;
ret = waveInPrepareHeader(hWaveIn, &inheader[i], sizeof(WAVEHDR));
ret = waveInAddBuffer(hWaveIn, &inheader[i], sizeof(WAVEHDR));
}
ret = waveInStart(hWaveIn);
}
int min = 0, max = 0, lastlevelGUI = 0, lastlevelreport = 0;
UCHAR CurrentLevel = 0; // Peak from current samples
void PollReceivedSamples()
{
// Process any captured samples
// Ideally call at least every 100 mS, more than 200 will loose data
// For level display we want a fairly rapir level average but only want to report
// to log every 10 secs or so
if (inheader[inIndex].dwFlags & WHDR_DONE)
{
short * ptr = &inbuffer[inIndex][0];
int i;
for (i = 0; i < ReceiveSize; i++)
{
if (*(ptr) < min)
min = *ptr;
else if (*(ptr) > max)
max = *ptr;
ptr++;
}
CurrentLevel = ((max - min) * 75) /32768; // Scale to 150 max
if ((Now - lastlevelGUI) > 2000) // 2 Secs
{
if (WaterfallActive == 0 && SpectrumActive == 0) // Don't need to send as included in Waterfall Line
SendtoGUI('L', &CurrentLevel, 1); // Signal Level
lastlevelGUI = Now;
if ((Now - lastlevelreport) > 10000) // 10 Secs
{
char HostCmd[64];
lastlevelreport = Now;
sprintf(HostCmd, "INPUTPEAKS %d %d", min, max);
WriteDebugLog(LOGDEBUG, "Input peaks = %d, %d", min, max);
SendCommandToHostQuiet(HostCmd);
}
min = max = 0;
}
// WriteDebugLog(LOGDEBUG, "Process %d %d", inIndex, inheader[inIndex].dwBytesRecorded/2);
if (Capturing && Loopback == FALSE)
ProcessNewSamples(&inbuffer[inIndex][0], inheader[inIndex].dwBytesRecorded/2);
waveInUnprepareHeader(hWaveIn, &inheader[inIndex], sizeof(WAVEHDR));
inheader[inIndex].dwFlags = 0;
waveInPrepareHeader(hWaveIn, &inheader[inIndex], sizeof(WAVEHDR));
waveInAddBuffer(hWaveIn, &inheader[inIndex], sizeof(WAVEHDR));
inIndex++;
if (inIndex == NumberofinBuffers)
inIndex = 0;
}
}
void StopCapture()
{
Capturing = FALSE;
// waveInStop(hWaveIn);
// WriteDebugLog(LOGDEBUG, "Stop Capture");
}
void StartCapture()
{
Capturing = TRUE;
DiscardOldSamples();
ClearAllMixedSamples();
State = SearchingForLeader;
// WriteDebugLog(LOGDEBUG, "Start Capture");
}
void CloseSound()
{
waveInClose(hWaveIn);
waveOutClose(hWaveOut);
}
#include <stdarg.h>
FILE *logfile = NULL;
VOID CloseDebugLog()
{
if(logfile)
fclose(logfile);
logfile = NULL;
}
VOID WriteDebugLog(int LogLevel, const char * format, ...)
{
char Mess[10000];
va_list(arglist);
char timebuf[128];
UCHAR Value[100];
SYSTEMTIME st;
va_start(arglist, format);
#ifdef LOGTOHOST
vsnprintf(&Mess[1], sizeof(Mess), format, arglist);
strcat(Mess, "\r\n");
Mess[0] = LogLevel + '0';
SendLogToHost(Mess, strlen(Mess));
#else
vsnprintf(Mess, sizeof(Mess), format, arglist);
strcat(Mess, "\r\n");
if (LogLevel <= ConsoleLogLevel)
printf(Mess);
if (!DebugLog)
return;
if (LogLevel > FileLogLevel)
return;
GetSystemTime(&st);
if (logfile == NULL)
{
if (HostPort[0])
sprintf(Value, "%s%s_%04d%02d%02d.log",
"ARDOPDebug", HostPort, st.wYear, st.wMonth, st.wDay);
else
sprintf(Value, "%s%d_%04d%02d%02d.log",
"ARDOPDebug", port, st.wYear, st.wMonth, st.wDay);
if ((logfile = fopen(Value, "ab")) == NULL)
return;
}
sprintf(timebuf, "%02d:%02d:%02d.%03d ",
st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
fputs(timebuf, logfile);
fputs(Mess, logfile);
#endif
return;
}
VOID WriteExceptionLog(const char * format, ...)
{
char Mess[10000];
va_list(arglist);
char timebuf[32];
UCHAR Value[100];
FILE *logfile = NULL;
SYSTEMTIME st;
va_start(arglist, format);
vsnprintf(Mess, sizeof(Mess), format, arglist);
strcat(Mess, "\r\n");
printf(Mess);
GetSystemTime(&st);
if (HostPort[0])
sprintf(Value, "%s%s_%04d%02d%02d.log",
"ARDOPException", HostPort, st.wYear, st.wMonth, st.wDay);
else
sprintf(Value, "%s%d_%04d%02d%02d.log",
"ARDOPException", port, st.wYear, st.wMonth, st.wDay);
if ((logfile = fopen(Value, "ab")) == NULL)
return;
sprintf(timebuf, "%02d:%02d:%02d.%03d ",
st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
fputs(timebuf, logfile);
fputs(Mess, logfile);
fclose(logfile);
return;
}
FILE *statslogfile = NULL;
VOID CloseStatsLog()
{
fclose(statslogfile);
statslogfile = NULL;
}
VOID Statsprintf(const char * format, ...)
{
char Mess[10000];
va_list(arglist);
UCHAR Value[100];
char timebuf[32];
SYSTEMTIME st;
va_start(arglist, format);
vsnprintf(Mess, sizeof(Mess), format, arglist);
strcat(Mess, "\r\n");
if (statslogfile == NULL)
{
GetSystemTime(&st);
if (HostPort[0])
sprintf(Value, "%s%s_%04d%02d%02d.log",
"ARDOPSession", HostPort, st.wYear, st.wMonth, st.wDay);
else
sprintf(Value, "%s%d_%04d%02d%02d.log",
"ARDOPSession", port, st.wYear, st.wMonth, st.wDay);
if ((statslogfile = fopen(Value, "ab")) == NULL)
return;
else
{
sprintf(timebuf, "%02d:%02d:%02d.%03d\r\n",
st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
fputs(timebuf, statslogfile);
}
}
fputs(Mess, statslogfile);
printf(Mess);
return;
}
VOID WriteSamples(short * buffer, int len)
{
fwrite(buffer, 1, len * 2, wavfp1);
}
unsigned short * SoundInit()
{
Index = 0;
return &buffer[0][0];
}
// Called at end of transmission
extern int Number; // Number of samples waiting to be sent
void SoundFlush()
{
// Append Trailer then wait for TX to complete
AddTrailer(); // add the trailer.
if (Loopback)
ProcessNewSamples(buffer[Index], Number);
SendtoCard(buffer[Index], Number);
// Wait for all sound output to complete
while (!(header[0].dwFlags & WHDR_DONE))
txSleep(10);
while (!(header[1].dwFlags & WHDR_DONE))
txSleep(10);
// I think we should turn round the link here. I dont see the point in
// waiting for MainPoll
SoundIsPlaying = FALSE;
//'Debug.WriteLine("[tmrPoll.Tick] Play stop. Length = " & Format(Now.Subtract(dttTestStart).TotalMilliseconds, "#") & " ms")
// WriteDebugLog(LOGDEBUG, "Play complete blnEnbARQRpt = %d", blnEnbARQRpt);
if (blnEnbARQRpt > 0 || blnDISCRepeating) // Start Repeat Timer if frame should be repeated
dttNextPlay = Now + intFrameRepeatInterval;
// WriteDebugLog(LOGDEBUG, "Now %d Now - dttNextPlay 1 = %d", Now, Now - dttNextPlay);
KeyPTT(FALSE); // Unkey the Transmitter
// Clear the capture buffers. I think this is only needed when testing
// with audio loopback.
// memset(&buffer[0], 0, 2400);
// memset(&buffer[1], 0, 2400);
StartCapture();
//' clear the transmit label
// stcStatus.BackColor = SystemColors.Control
// stcStatus.ControlName = "lblXmtFrame" ' clear the transmit label
// queTNCStatus.Enqueue(stcStatus)
// stcStatus.ControlName = "lblRcvFrame" ' clear the Receive label
// queTNCStatus.Enqueue(stcStatus)
return;
}
void StartCodec(char * strFault)
{
strFault[0] = 0;
InitSound(FALSE);
}
void StopCodec(char * strFault)
{
CloseSound();
strFault[0] = 0;
}
VOID RadioPTT(BOOL PTTState)
{
if (PTTMode & PTTRTS)
if (PTTState)
COMSetRTS(hPTTDevice);
else
COMClearRTS(hPTTDevice);
if (PTTMode & PTTDTR)
if (PTTState)
COMSetDTR(hPTTDevice);
else
COMClearDTR(hPTTDevice);
if (PTTMode & PTTCI_V)
if (PTTState)
WriteCOMBlock(hCATDevice, PTTOnCmd, PTTOnCmdLen);
else
WriteCOMBlock(hCATDevice, PTTOffCmd, PTTOffCmdLen);
}
// Function to send PTT TRUE or PTT FALSE comannad to Host or if local Radio control Keys radio PTT
const char BoolString[2][6] = {"FALSE", "TRUE"};
BOOL KeyPTT(BOOL blnPTT)
{
// Returns TRUE if successful False otherwise
if (blnLastPTT && !blnPTT)
dttStartRTMeasure = Now; // start a measurement on release of PTT.
if (!RadioControl)
if (blnPTT)
SendCommandToHostQuiet("PTT TRUE");
else
SendCommandToHostQuiet("PTT FALSE");
else
RadioPTT(blnPTT);
WriteDebugLog(LOGDEBUG, "[Main.KeyPTT] PTT-%s", BoolString[blnPTT]);
blnLastPTT = blnPTT;
SetLED(0, blnPTT);
return TRUE;
}
void PlatformSleep()
{
// Sleep to avoid using all cpu
Sleep(10);
if (PKTLEDTimer && Now > PKTLEDTimer)
{
PKTLEDTimer = 0;
SetLED(PKTLED, 0); // turn off packet rxed led
}
}
void displayState(const char * State)
{
char Msg[80];
strcpy(Msg, State);
SendtoGUI('S', Msg, strlen(Msg) + 1); // Protocol State
// Dummy for i2c display
}
void DrawTXMode(const char * Mode)
{
char Msg[80];
strcpy(Msg, Mode);
SendtoGUI('T', Msg, strlen(Msg) + 1); // TX Frame
}
void DrawTXFrame(const char * Frame)
{
char Msg[80];
strcpy(Msg, Frame);
SendtoGUI('T', Msg, strlen(Msg) + 1); // TX Frame
}
void DrawRXFrame(int State, const char * Frame)
{
unsigned char Msg[64];
Msg[0] = State; // Pending/Good/Bad
strcpy(&Msg[1], Frame);
SendtoGUI('R', Msg, strlen(Frame) + 2); // RX Frame
}
char Leds[8]= {0};
unsigned int PKTLEDTimer = 0;
void SetLED(int LED, int State)
{
// If GUI active send state
Leds[LED] = State;
SendtoGUI('D', Leds, 8);
}
void displayCall(int dirn, char * call)
{
char Msg[32];
sprintf(Msg, "%c%s", dirn, call);
SendtoGUI('I', Msg, strlen(Msg));
}
HANDLE OpenCOMPort(VOID * pPort, int speed, BOOL SetDTR, BOOL SetRTS, BOOL Quiet, int Stopbits)
{
char szPort[80];
BOOL fRetVal ;
COMMTIMEOUTS CommTimeOuts ;
int Err;
char buf[100];
HANDLE fd;
DCB dcb;
// if Port Name starts COM, convert to \\.\COM or ports above 10 wont work
if ((unsigned int)pPort < 256) // just a com port number
sprintf( szPort, "\\\\.\\COM%d", pPort);
else if (_memicmp(pPort, "COM", 3) == 0)
{
char * pp = (char *)pPort;
int p = atoi(&pp[3]);
sprintf( szPort, "\\\\.\\COM%d", p);
}
else
strcpy(szPort, pPort);
// open COMM device
fd = CreateFile( szPort, GENERIC_READ | GENERIC_WRITE,
0, // exclusive access
NULL, // no security attrs
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL );
if (fd == (HANDLE) -1)
{
if (Quiet == 0)
{
if (pPort < (VOID *)256)
sprintf(buf," COM%d could not be opened \r\n ", (unsigned int)pPort);
else
sprintf(buf," %s could not be opened \r\n ", pPort);
// WritetoConsoleLocal(buf);
OutputDebugString(buf);
}
return (FALSE);
}
Err = GetFileType(fd);
// setup device buffers
SetupComm(fd, 4096, 4096 ) ;
// purge any information in the buffer
PurgeComm(fd, PURGE_TXABORT | PURGE_RXABORT |
PURGE_TXCLEAR | PURGE_RXCLEAR ) ;
// set up for overlapped I/O
CommTimeOuts.ReadIntervalTimeout = 0xFFFFFFFF ;
CommTimeOuts.ReadTotalTimeoutMultiplier = 0 ;
CommTimeOuts.ReadTotalTimeoutConstant = 0 ;
CommTimeOuts.WriteTotalTimeoutMultiplier = 0 ;
// CommTimeOuts.WriteTotalTimeoutConstant = 0 ;
CommTimeOuts.WriteTotalTimeoutConstant = 500 ;
SetCommTimeouts(fd, &CommTimeOuts ) ;
dcb.DCBlength = sizeof( DCB ) ;
GetCommState(fd, &dcb ) ;
dcb.BaudRate = speed;
dcb.ByteSize = 8;
dcb.Parity = 0;
dcb.StopBits = TWOSTOPBITS;
dcb.StopBits = Stopbits;
// setup hardware flow control
dcb.fOutxDsrFlow = 0;
dcb.fDtrControl = DTR_CONTROL_DISABLE ;