-
Notifications
You must be signed in to change notification settings - Fork 0
/
ARQ.c
2632 lines (2047 loc) · 86.7 KB
/
ARQ.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
// ARDOP TNC ARQ Code
//
#ifdef WIN32
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#define _CRT_SECURE_NO_DEPRECATE
#define _USE_32BIT_TIME_T
#include <windows.h>
#include <mmsystem.h>
#pragma comment(lib, "winmm.lib")
#endif
#include "ARDOPC.h"
#ifdef TEENSY
#define PKTLED LED3 // flash when packet received
extern unsigned int PKTLEDTimer;
#endif
extern UCHAR bytData[];
extern int intLastRcvdFrameQuality;
extern int intRmtLeaderMeasure;
extern BOOL blnAbort;
extern int intRepeatCount;
extern unsigned int dttLastFECIDSent;
extern unsigned int tmrSendTimeout;
extern BOOL blnFramePending;
extern int dttLastBusyTrip;
extern int dttPriorLastBusyTrip;
extern int dttLastBusyClear;
int intLastFrameIDToHost = 0;
int intLastFailedFrameID = 0;
int intLastARQDataFrameToHost = -1;
int intARQDefaultDlyMs = 100; // Not sure if this really need with optimized leader length. 100 ms doesn't add much overhead.
int intAvgQuality; // the filtered average reported quality (0 to 100) practical range 50 to 96
int intShiftUpDn = 0;
int intFrameTypePtr = 0; // Pointer to the current data mode in bytFrameTypesForBW()
int intRmtLeaderMeas = 0;
int intTrackingQuality = -1;
UCHAR bytLastARQDataFrameSent = 0; // initialize to an improper data frame
UCHAR bytLastARQDataFrameAcked = 0; // initialize to an improper data frame
void ClearTuningStats();
void ClearQualityStats();
void updateDisplay();
void DrawTXMode(const char * TXMode);
int bytQDataInProcessLen = 0; // Lenght of frame to send/last sent
BOOL blnLastFrameSentData = FALSE;
extern char CarrierOk[8];
extern int LastDataFrameType;
extern BOOL blnARQDisconnect;
extern const short FrameSize[256];
// ARQ State Variables
char AuxCalls[10][10] = {0};
int AuxCallsLength = 0;
int intBW; // Requested connect speed
int intSessionBW; // Negotiated speed
const char ARQBandwidths[9][12] = {"200FORCED", "500FORCED", "1000FORCED", "2000FORCED", "200MAX", "500MAX", "1000MAX", "2000MAX", "UNDEFINED"};
enum _ARQSubStates ARQState;
const char ARQSubStates[10][11] = {"None", "ISSConReq", "ISSConAck", "ISSData", "ISSId", "IRSConAck", "IRSData", "IRSBreak", "IRSfromISS", "DISCArqEnd"};
char strRemoteCallsign[10];
char strLocalCallsign[10];
char strFinalIDCallsign[10];
UCHAR bytLastARQSessionID;
BOOL blnEnbARQRpt;
BOOL blnListen = TRUE;
BOOL Monitor = TRUE;
BOOL AutoBreak = TRUE;
BOOL blnBREAKCmd = FALSE;
BOOL BusyBlock = FALSE;
UCHAR bytPendingSessionID;
UCHAR bytSessionID = 0xff;
BOOL blnARQConnected;
UCHAR bytCurrentFrameType = 0; // The current frame type used for sending
UCHAR * bytFrameTypesForBW; // Holds the byte array for Data modes for a session bandwidth. First are most robust, last are fastest
int bytFrameTypesForBWLength = 0;
UCHAR * bytShiftUpThresholds;
int bytShiftUpThresholdsLength;
BOOL blnPending;
int dttTimeoutTrip;
int intLastARQDataFrameToHost;
int intAvgQuality;
int intReceivedLeaderLen;
unsigned int tmrFinalID = 0;
unsigned int tmrIRSPendingTimeout = 0;
unsigned int tmrPollOBQueue;
UCHAR bytLastReceivedDataFrameType;
BOOL blnDISCRepeating;
int intRmtLeaderMeas;
int intOBBytesToConfirm = 0; // remaining bytes to confirm
int intBytesConfirmed = 0; // Outbound bytes confirmed by ACK and squenced
int intReportedLeaderLen = 0; // Zero out the Reported leader length the length reported to the remote station
BOOL blnLastPSNPassed = FALSE; // the last PSN passed True for Odd, FALSE for even.
BOOL blnInitiatedConnection = FALSE; // flag to indicate if this station initiated the connection
short dblAvgPECreepPerCarrier = 0; // computed phase error creep
int dttLastIDSent; // date/time of last ID
int intTotalSymbols = 0; // To compute the sample rate error
extern int bytDataToSendLength;
int intFrameRepeatInterval;
extern int intLeaderRcvdMs;
int intTrackingQuality;
int intNAKctr = 0;
int intACKctr = 0;
UCHAR bytLastACKedDataFrameType;
int Encode4FSKControl(UCHAR bytFrameType, UCHAR bytSessionID, UCHAR * bytreturn);
int EncodeConACKwTiming(UCHAR bytFrameType, int intRcvdLeaderLenMs, UCHAR bytSessionID, UCHAR * bytreturn);
int IRSNegotiateBW(int intConReqFrameType);
int GetNextFrameData(int * intUpDn, UCHAR * bytFrameTypeToSend, UCHAR * strMod, BOOL blnInitialize);
BOOL CheckForDisconnect();
BOOL Send10MinID();
void ProcessPingFrame(char * bytData);
void LogStats();
int ComputeInterFrameInterval(int intRequestedIntervalMS);
BOOL CheckForDisconnect();
// Tuning Stats
int intLeaderDetects;
int intLeaderSyncs;
int intAccumLeaderTracking;
float dblFSKTuningSNAvg;
int intGoodFSKFrameTypes;
int intFailedFSKFrameTypes;
int intAccumFSKTracking;
int intFSKSymbolCnt;
int intGoodFSKFrameDataDecodes;
int intFailedFSKFrameDataDecodes;
int intAvgFSKQuality;
int intFrameSyncs;
int intGoodPSKSummationDecodes;
int intGoodFSKSummationDecodes;
int intGoodQAMSummationDecodes;
float dblLeaderSNAvg;
int intAccumPSKLeaderTracking;
float dblAvgPSKRefErr;
int intPSKTrackAttempts;
int intAccumPSKTracking;
int intQAMTrackAttempts;
int intAccumQAMTracking;
int intPSKSymbolCnt;
int intQAMSymbolCnt;
int intGoodPSKFrameDataDecodes;
int intFailedPSKFrameDataDecodes;
int intGoodQAMFrameDataDecodes;
int intFailedQAMFrameDataDecodes;
int intAvgPSKQuality;
float dblAvgDecodeDistance;
int intDecodeDistanceCount;
int intShiftUPs;
int intShiftDNs;
unsigned int dttStartSession;
int intLinkTurnovers;
int intEnvelopeCors;
float dblAvgCorMaxToMaxProduct;
int intConReqSN;
int intConReqQuality;
// Subroutine to compute a 8 bit CRC value and append it to the Data...
UCHAR GenCRC8(char * Data)
{
//For CRC-8-CCITT = x^8 + x^7 +x^3 + x^2 + 1 intPoly = 1021 Init FFFF
int intPoly = 0xC6; // This implements the CRC polynomial x^8 + x^7 +x^3 + x^2 + 1
int intRegister = 0xFF;
int i;
unsigned int j;
BOOL blnBit;
for (j = 0; j < strlen(Data); j++)
{
int Val = Data[j];
for (i = 7; i >= 0; i--) // for each bit processing MS bit first
{
blnBit = (Val & 0x80) != 0;
Val = Val << 1;
if ((intRegister & 0x80) == 0x80) // the MSB of the register is set
{
// Shift left, place data bit as LSB, then divide
// Register := shiftRegister left shift 1
// Register := shiftRegister xor polynomial
if (blnBit)
intRegister = 0xFF & (1 + 2 * intRegister);
else
intRegister = 0xFF & (2 * intRegister);
intRegister = intRegister ^ intPoly;
}
else
{
// the MSB is not set
// Register is not divisible by polynomial yet.
// Just shift left and bring current data bit onto LSB of shiftRegister
if (blnBit)
intRegister = 0xFF & (1 + 2 * intRegister);
else
intRegister = 0xFF & (2 * intRegister);
}
}
}
return intRegister & 0xFF; // LS 8 bits of Register
}
int ComputeInterFrameInterval(int intRequestedIntervalMS)
{
return max(1000, intRequestedIntervalMS + intRmtLeaderMeas);
}
// Subroutine to Set the protocol state
void SetARDOPProtocolState(int value)
{
char HostCmd[24];
if (ProtocolState == value)
return;
ProtocolState = value;
displayState(ARDOPStates[ProtocolState]);
newStatus = TRUE; // report to PTC
//Dim stcStatus As Status
//stcStatus.ControlName = "lblState"
//stcStatus.Text = ARDOPState.ToString
switch(ProtocolState)
{
case DISC:
blnARQDisconnect = FALSE; // always clear the ARQ Disconnect Flag from host.
//stcStatus.BackColor = System.Drawing.Color.White
blnARQConnected = FALSE;
blnPending = FALSE;
ClearDataToSend();
SetLED(ISSLED, FALSE);
SetLED(IRSLED, FALSE);
displayCall(0x20, "");
break;
case FECRcv:
//stcStatus.BackColor = System.Drawing.Color.PowderBlue
break;
case FECSend:
InitializeConnection();
intLastFrameIDToHost = -1;
intLastFailedFrameID = -1;
//ReDim bytFailedData(-1)
//stcStatus.BackColor = System.Drawing.Color.Orange
break;
// Case ProtocolState.IRS
// stcStatus.BackColor = System.Drawing.Color.LightGreen
case ISS:
case IDLE:
blnFramePending = FALSE; // Added 0.6.4 to insure any prior repeating frame is cancelled before new data.
blnEnbARQRpt = FALSE;
SetLED(ISSLED, TRUE);
SetLED(IRSLED, FALSE);
// stcStatus.BackColor = System.Drawing.Color.LightSalmon
break;
case IRS:
case IRStoISS:
SetLED(IRSLED, TRUE);
SetLED(ISSLED, FALSE);
bytLastACKedDataFrameType = 0; // Clear on entry to IRS or IRS to ISS states. 3/15/2018
break;
// Case ProtocolState.IDLE
// stcStatus.BackColor = System.Drawing.Color.NavajoWhite
// Case ProtocolState.OFFLINE
// stcStatus.BackColor = System.Drawing.Color.Silver
}
//queTNCStatus.Enqueue(stcStatus)
sprintf(HostCmd, "NEWSTATE %s ", ARDOPStates[ProtocolState]);
QueueCommandToHost(HostCmd);
}
// Function to Get the next ARQ frame returns TRUE if frame repeating is enable
BOOL GetNextARQFrame()
{
//Dim bytToMod(-1) As Byte
char HostCmd[80];
if (blnAbort) // handles ABORT (aka Dirty Disconnect)
{
//if (DebugLog) ;(("[ARDOPprotocol.GetNextARQFrame] ABORT...going to ProtocolState DISC, return FALSE")
ClearDataToSend();
SetARDOPProtocolState(DISC);
InitializeConnection();
blnAbort = FALSE;
blnEnbARQRpt = FALSE;
blnDISCRepeating = FALSE;
intRepeatCount = 0;
return FALSE;
}
if (blnDISCRepeating) // handle the repeating DISC reply
{
intRepeatCount += 1;
blnEnbARQRpt = FALSE;
if (intRepeatCount > 5) // do 5 tries then force disconnect
{
QueueCommandToHost("DISCONNECTED");
sprintf(HostCmd, "STATUS END NOT RECEIVED CLOSING ARQ SESSION WITH %s", strRemoteCallsign);
QueueCommandToHost(HostCmd);
blnDISCRepeating = FALSE;
blnEnbARQRpt = FALSE;
ClearDataToSend();
SetARDOPProtocolState(DISC);
intRepeatCount = 0;
InitializeConnection();
return FALSE; //indicates end repeat
}
WriteDebugLog(LOGDEBUG, "Repeating DISC %d", intRepeatCount);
EncLen = Encode4FSKControl(DISCFRAME, bytSessionID, bytEncodedBytes);
return TRUE; // continue with DISC repeats
}
if (ProtocolState == ISS || ProtocolState == IDLE)
if (CheckForDisconnect())
return FALSE;
if (ProtocolState == ISS && ARQState == ISSConReq) // Handles Repeating ConReq frames
{
intRepeatCount++;
if (intRepeatCount > ARQConReqRepeats)
{
ClearDataToSend();
SetARDOPProtocolState(DISC);
intRepeatCount = 0;
blnPending = FALSE;
displayCall(0x20, "");
if (strRemoteCallsign[0])
{
sprintf(HostCmd, "STATUS CONNECT TO %s FAILED!", strRemoteCallsign);
QueueCommandToHost(HostCmd);
InitializeConnection();
return FALSE; // 'indicates end repeat
}
else
{
QueueCommandToHost("STATUS END ARQ CALL");
InitializeConnection();
return FALSE; //indicates end repeat
}
//Clear the mnuBusy status on the main form
/// Dim stcStatus As Status = Nothing
// stcStatus.ControlName = "mnuBusy"
// queTNCStatus.Enqueue(stcStatus)
}
return TRUE; // ' continue with repeats
}
if (ProtocolState == ISS && ARQState == IRSConAck)
{
// Handles ISS repeat of ConAck
intRepeatCount += 1;
if (intRepeatCount <= ARQConReqRepeats)
return TRUE;
else
{
SetARDOPProtocolState(DISC);
ARQState = DISCArqEnd;
sprintf(HostCmd, "STATUS CONNECT TO %s FAILED!", strRemoteCallsign);
QueueCommandToHost(HostCmd);
intRepeatCount = 0;
InitializeConnection();
return FALSE;
}
}
// Handles a timeout from an ARQ connected State
if (ProtocolState == ISS || ProtocolState == IDLE || ProtocolState == IRS || ProtocolState == IRStoISS)
{
if ((Now - dttTimeoutTrip) / 1000 > ARQTimeout) // (Handles protocol rule 1.7)
{
if (!blnTimeoutTriggered)
{
WriteDebugLog(LOGDEBUG, "[ARDOPprotocol.GetNexARQFrame] Timeout setting SendTimeout timer to start.");
blnEnbARQRpt = FALSE;
blnTimeoutTriggered = TRUE; // prevents a retrigger
tmrSendTimeout = Now + 1000;
return FALSE;
}
}
}
if (ProtocolState == DISC && intPINGRepeats > 0)
{
intRepeatCount++;
if (intRepeatCount <= intPINGRepeats && blnPINGrepeating)
{
dttLastPINGSent = Now;
return TRUE; // continue PING
}
intPINGRepeats = 0;
blnPINGrepeating = False;
return FALSE;
}
// Handles the DISC state (no repeats)
if (ProtocolState == DISC) // never repeat in DISC state
{
blnARQDisconnect = FALSE;
intRepeatCount = 0;
return FALSE;
}
// ' Handles all other possibly repeated Frames
return blnEnbARQRpt; // not all frame types repeat...blnEnbARQRpt is set/cleared in ProcessRcvdARQFrame
}
// function to generate 8 bit session ID
UCHAR GenerateSessionID(char * strCallingCallSign, char *strTargetCallsign)
{
char bytToCRC[20];
int Len = sprintf(bytToCRC, "%s%s", strCallingCallSign, strTargetCallsign);
UCHAR ID = GenCRC8(bytToCRC);
if (ID == 255)
// rare case where the computed session ID woudl be FF
// Remap a SessionID of FF to 0...FF reserved for FEC mode
return 0;
return ID;
}
// Function to compute the optimum leader based on the Leader sent and the reported Received leader
void CalculateOptimumLeader(int intReportedReceivedLeaderMS,int intLeaderSentMS)
{
intCalcLeader = max(200, 120 + intLeaderSentMS - intReportedReceivedLeaderMS); // This appears to work well on HF sim tests May 31, 2015
// WriteDebugLog(LOGDEBUG, ("[ARDOPprotocol.CalcualteOptimumLeader] Leader Sent=" & intLeaderSentMS.ToString & " ReportedReceived=" & intReportedReceivedLeaderMS.ToString & " Calculated=" & stcConnection.intCalcLeader.ToString)
}
// Function to determine if call is to Callsign or one of the AuxCalls
BOOL IsCallToMe(char * strCallsign, UCHAR * bytReplySessionID)
{
// returns true and sets bytReplySessionID if is to me.
int i;
if (strcmp(strCallsign, Callsign) == 0)
{
*bytReplySessionID = GenerateSessionID(bytData, strCallsign);
return TRUE;
}
for (i = 0; i < AuxCallsLength; i++)
{
if (strcmp(strCallsign, AuxCalls[i]) == 0)
{
*bytReplySessionID = GenerateSessionID(bytData, strCallsign);
return TRUE;
}
}
return FALSE;
}
BOOL IsPingToMe(char * strCallsign)
{
int i;
if (strcmp(strCallsign, Callsign) == 0)
return TRUE;
for (i = 0; i < AuxCallsLength; i++)
{
if (strcmp(strCallsign, AuxCalls[i]) == 0)
return TRUE;
}
return FALSE;
}
/*
ModeToSpeed() = {
40 768
42
44 1296
46 429
48
4A 881
4C
4E 288
50 1536
52 2592
54
56 4305
58 429
5A 329
5C
5E
60 3072
62 5184
64
66 8610
68 1762
6A
6C
6E
70 6144
72 10286
74
76 17228
78 3624
7A 5863
7C 4338
7E
*/
// Function to get base (even) data modes by bandwidth for ARQ sessions
// Streamlined 0.3.1.6
// 200 8FSK.200.25, 4FSK.200.50, 4PSK.200.100, 8PSK.200.100, 16QAM.200.100
// (288, 429, 768, 1296, 1512 byte/min)
// New version for more robust modes: Rev 1.0.2 11/21/2017
// (310, 436, 756, 1296, 1512 byte/min)
// Modes are
/*
"4PSK.200.100.E", // 0x40
"4PSK.200.100S.E", 0x42
"8PSK.200.100.E", 0x44
"16QAM.200.100.E", // 46
"4FSK.200.50S.E", // 48
"4FSK.500.100.E", 4A
"4FSK.500.100S.E", 4C
"4PSK.500.100.E", // 50
"8PSK.500.100.E",
"8PSK.500.100.O", //0x52
"16QAM.500.100.E", //54
"4PSK.1000.100.E", //60
"8PSK.1000.100.E", 62,
"16QAM.1000.100.E", 64
"4PSK.2000.100.E", //70
"8PSK.2000.100.E", 72
"16QAM.2000.100.E", 74
"16QAM.2000.100.O", // 75
"4FSK.2000.600.E", // Experimental //7A
"4FSK.2000.600S.E", // Experimental// 7C
*/
//4FSK.200.50S, 4PSK.200.100S, 4PSK.200.100, 8PSK.200.100, 16QAM.200.100
static UCHAR DataModes200[] = {0x48, 0x42, 0x40, 0x44, 0x46};
static UCHAR DataModes200FSK[] = {0x48};
//4FSK.200.50S, 4PSK.200.100S, 4PSK.200.100, 4PSK.500.100, 8PSK.500.100, 16QAM.500.100)
// (310, 436, 756, 1509, 2566, 3024 bytes/min)
//Dim byt500 As Byte() = {&H48, &H42, &H40, &H50, &H52, &H54}
static UCHAR DataModes500[] = {0x48, 0x42, 0x40, 0x50, 0x52, 0x54};
static UCHAR DataModes500FSK[] = {0x48};
//4FSK500.100S, 4FSK500.100, 4PSK500.100, 4PSK1000.100, 8PSK.1000.100
//(701, 865, 1509, 3018, 5133 bytes/min)
static UCHAR DataModes1000[] = {0x4C, 0x4A, 0x50, 0x60, 0x62, 0x64};
static UCHAR DataModes1000FSK[] = {0x4C, 0x4A};
// 2000 Non-FM
//4FSK500.100S, 4FSK500.100, 4PSK500.100, 4PSK1000.100, 4PSK2000.100, 8PSK.2000.100, 16QAM.2000.100
//(701, 865, 1509, 3018, 6144, 10386 bytes/min)
//Dim byt2000 As Byte() = {&H4C, &H4A, &H50, &H60, &H70, &H72, &H74} ' Note addtion of 16QAM 8 carrier mode 16QAM2000.100.E/O
static UCHAR DataModes2000[] = {0x4C, 0x4A, 0x50, 0x60, 0x70, 0x72, 0x74};
static UCHAR DataModes2000FSK[] = {0x4C, 0x4A};
//2000 FM
//' These include the 600 baud modes for FM only.
//' The following is temporary, Plan to replace 8PSK 8 carrier modes with high baud 4PSK and 8PSK.
// 4FSK.500.100S, 4FSK.500.100, 4FSK.2000.600S, 4FSK.2000.600)
// (701, 865, 4338, 5853 bytes/min)
//Dim byt2000 As Byte() = {&H4C, &H4A, &H7C, &H7A}
static UCHAR DataModes2000FM[] = {0x4C, 0x4A, 0x7C, 0x7A};
static UCHAR DataModes2000FMFSK[] = {0x4C, 0x4A, 0x7C, 0x7A};
static UCHAR NoDataModes[1] = {0};
UCHAR * GetDataModes(int intBW)
{
// Revised version 0.3.5
// idea is to use this list in the gear shift algorithm to select modulation mode based on bandwidth and robustness.
// Sequence modes in approximate order of robustness ...most robust first, shorter frames of same modulation first
if (intBW == 200)
{
if (FSKOnly)
{
bytFrameTypesForBWLength = sizeof(DataModes200FSK);
return DataModes200FSK;
}
bytFrameTypesForBWLength = sizeof(DataModes200);
return DataModes200;
}
if (intBW == 500)
{
if (FSKOnly)
{
bytFrameTypesForBWLength = sizeof(DataModes500FSK);
return DataModes500FSK;
}
bytFrameTypesForBWLength = sizeof(DataModes500);
return DataModes500;
}
if (intBW == 1000)
{
if (FSKOnly)
{
bytFrameTypesForBWLength = sizeof(DataModes1000FSK);
return DataModes1000FSK;
}
bytFrameTypesForBWLength = sizeof(DataModes1000);
return DataModes1000;
}
if (intBW == 2000)
{
if (TuningRange > 0 && !Use600Modes)
{
if (FSKOnly)
{
bytFrameTypesForBWLength = sizeof(DataModes2000FSK);
return DataModes2000FSK;
}
bytFrameTypesForBWLength = sizeof(DataModes2000);
return DataModes2000;
}
else
{
if (FSKOnly)
{
bytFrameTypesForBWLength = sizeof(DataModes2000FMFSK);
return DataModes2000FMFSK;
}
bytFrameTypesForBWLength = sizeof(DataModes2000FM);
return DataModes2000FM;
}
}
bytFrameTypesForBWLength = 0;
return NoDataModes;
}
// Function to get Shift up thresholds by bandwidth for ARQ sessions
static UCHAR byt200[] = {82, 84, 84, 85, 0};
static UCHAR byt500[] = {80, 84, 84, 75, 79, 0};
static UCHAR byt1000[] = {80, 80, 80, 80, 75, 0};
static UCHAR byt2000[] = {80, 80, 80, 76, 85, 75, 0}; // Threshold for 8PSK 167 baud changed from 73 to 80 on rev 0.7.2.3
static UCHAR byt2000FM[] = {60, 85, 85, 0};
UCHAR * GetShiftUpThresholds(int intBW)
{
//' Initial values determined by finding the following process: (all using Pink Gaussian Noise channel 0 to 3 KHz)
//' 1) Find Min S:N that will give reliable (at least 4/5 tries) decoding at the fastest mode for the bandwidth.
//' 2) At that SAME S:N use the next fastest (more robust mode for the bandwidth)
//' 3) Over several frames average the Quality of the decoded frame in 2) above That quality value is the one that
//' is then used as the shift up threshold for that mode. (note the top mode will never use a value to shift up).
//' This might be adjusted some but should along with a requirement for two successive ACKs make a good algorithm
if (intBW == 200)
return byt200;
if (intBW == 500)
return byt500;
if (intBW == 1000)
return byt1000;
// default to 2000
if (TuningRange > 0 && !Use600Modes)
return byt2000;
else
return byt2000FM;
}
unsigned short ModeHasWorked[16] = {0}; // used to attempt to make gear shift more stable.
unsigned short ModeHasBeenTried[16] = {0};
unsigned short ModeNAKS[16] = {0};
// Subroutine to shift up to the next higher throughput or down to the next more robust data modes based on average reported quality
void Gearshift_9()
{
// More complex mechanism to gear shift based on intAvgQuality, current state and bytes remaining.
// This can be refined later with different or dynamic Trip points etc.
// Revised Oct 8, 2016 Rev 0.7.2.2 to use intACKctr as well as intNAKctr and bytShiftUpThresholds using FrameInfo.GetShiftUpThresholds
char strOldMode[18] = "";
char strNewMode[18] = "";
int DownNAKS = 2; // Normal (changed from 3 Nov 17)
int intBytesRemaining = bytDataToSendLength;
if (ModeHasWorked[intFrameTypePtr] == 0) // This mode has never worked
DownNAKS = 1; // Revert immediately
if (intACKctr)
ModeHasWorked[intFrameTypePtr]++;
else if (intNAKctr)
ModeNAKS[intFrameTypePtr]++;
if (intFrameTypePtr > 0 && intNAKctr >= DownNAKS)
{
strcpy(strOldMode, Name(bytFrameTypesForBW[intFrameTypePtr]));
strOldMode[strlen(strOldMode) - 2] = 0;
strcpy(strNewMode, Name(bytFrameTypesForBW[intFrameTypePtr - 1]));
strNewMode[strlen(strNewMode) - 2] = 0;
WriteDebugLog(LOGINFO, "[ARDOPprotocol.Gearshift_9] intNAKCtr= %d Shift down from Frame type %s New Mode: %s", intNAKctr, strOldMode, strNewMode);
intShiftUpDn = -1;
intAvgQuality = 0; // Clear intAvgQuality causing the first received Quality to become the new average
intNAKctr = 0;
intACKctr = 0;
intShiftDNs++;
}
else if (intAvgQuality > bytShiftUpThresholds[intFrameTypePtr] && intFrameTypePtr < (bytFrameTypesForBWLength - 1) && intACKctr >= 2)
{
// if above Hi Trip setup so next call of GetNextFrameData will select a faster mode if one is available
// But don't shift if we can send remaining data in current mode
if (intBytesRemaining <= FrameSize[bytFrameTypesForBW[intFrameTypePtr]])
{
intShiftUpDn = 0;
return;
}
// if the new mode has been tried before, and immediately failed, don't try again
// till we get at least 5 sucessive acks
if (ModeHasBeenTried[intFrameTypePtr + 1] && ModeHasWorked[intFrameTypePtr + 1] == 0 && intACKctr < 5)
{
intShiftUpDn = 0;
return;
}
intShiftUpDn = 1;
ModeHasBeenTried[intFrameTypePtr + intShiftUpDn] = 1;
strcpy(strNewMode, Name(bytFrameTypesForBW[intFrameTypePtr + intShiftUpDn]));
strNewMode[strlen(strNewMode) - 2] = 0;
WriteDebugLog(LOGINFO, "[ARDOPprotocol.Gearshift_9] ShiftUpDn = %d, AvgQuality=%d New Mode: %s",
intShiftUpDn, intAvgQuality, strNewMode);
intAvgQuality = 0; // Clear intAvgQuality causing the first received Quality to become the new average
intNAKctr = 0;
intACKctr = 0;
intShiftUPs++;
}
}
/*
void Gearshift_5x()
{
//' More complex mechanism to gear shift based on intAvgQuality, current state and bytes remaining.
//' This can be refined later with different or dynamic Trip points etc.
int intTripHi = 79; // Modified in revision 0.4.0 (was 82)
int intTripLow = 69; // Modified in revision 0.4.0 (was 72)
int intBytesRemaining = bytDataToSendLength;
if (intNAKctr >= 5 && intFrameTypePtr > 0) // NAK threshold changed from 10 to 6 on rev 0.3.5.2
{
WriteDebugLog(LOGDEBUG, "[ARDOPprotocol.Gearshift_5] intNAKCtr=%d ShiftUpDn = -1", intNAKctr);
intShiftUpDn = -1; //Shift down if 5 NAKs without ACK.
intAvgQuality = (intTripHi + intTripLow) / 2; // init back to mid way
intNAKctr = 0;
}
else if (intAvgQuality > intTripHi && intFrameTypePtr < bytFrameTypesForBWLength) // ' if above Hi Trip setup so next call of GetNextFrameData will select a faster mode if one is available
{
intShiftUpDn = 0;
if (TuningRange == 0)
{
switch (intFrameTypePtr)
{
case 0:
if (intBytesRemaining > 64)
intShiftUpDn = 2;
else if (intBytesRemaining > 32)
intShiftUpDn = 1;
break;
case 1:
if (intBytesRemaining > 200)
intShiftUpDn = 2;
else if (intBytesRemaining > 64)
intShiftUpDn = 1;
break;
case 2:
if (intBytesRemaining > 400)
intShiftUpDn = 2;
else if (intBytesRemaining > 200)
intShiftUpDn = 1;
break;
case 3:
if (intBytesRemaining > 600) intShiftUpDn = 1;
break;
case 4:
if (intBytesRemaining > 512) intShiftUpDn = 1;
break;
}
}
else if (intSessionBW == 200)
intShiftUpDn = 1;
else if (intFrameTypePtr == 0 && intBytesRemaining > 32)
intShiftUpDn = 2;
else
intShiftUpDn = 1;
WriteDebugLog(LOGDEBUG, "[ARDOPprotocol.Gearshift_5] ShiftUpDn = %d, AvgQuality=%d Resetting to %d New Mode: %s",
intShiftUpDn, intAvgQuality, (intTripHi + intTripLow) / 2, Name(bytFrameTypesForBW[intFrameTypePtr + intShiftUpDn]));
intAvgQuality = 0; // init back to mid way
intNAKctr = 0;
}
else if (intAvgQuality < intTripLow && intFrameTypePtr > 0) // if below Low Trip setup so next call of GetNextFrameData will select a more robust mode if one is available
{
intShiftUpDn = 0;
if (TuningRange == 0)
{
switch (intFrameTypePtr)
{
case 1:
if (intBytesRemaining < 33) intShiftUpDn = -1;
break;
case 2:
case 4:
case 5:
intShiftUpDn = -1;
break;
case 3:
intShiftUpDn = -2;
break;
}
}
else if (intSessionBW == 200)
intShiftUpDn = -1;
else
{
if (intFrameTypePtr == 2 && intBytesRemaining < 17)
intShiftUpDn = -2;
else
intShiftUpDn = -1;
}
WriteDebugLog(LOGDEBUG, "[ARDOPprotocol.Gearshift_5] ShiftUpDn = %d, AvgQuality=%d Resetting to %d New Mode: %s",
intShiftUpDn, intAvgQuality, (intTripHi + intTripLow) / 2, Name(bytFrameTypesForBW[intFrameTypePtr + intShiftUpDn]));
intAvgQuality; // init back to mid way
intNAKctr = 0;
}
// if (intShiftUpDn < 0)
// intShiftDNs++;
// else if (intShiftUpDn > 0)
// intShiftUPs++;
}
*/
// Subroutine to provide exponential averaging for reported received quality from ACK/NAK to data frame.
void ComputeQualityAvg(int intReportedQuality)
{
float dblAlpha = 0.5f; // adjust this for exponential averaging speed. smaller alpha = slower response & smoother averages but less rapid shifting.
if (intAvgQuality == 0)
{
intAvgQuality = intReportedQuality;
WriteDebugLog(LOGDEBUG, "[ARDOPprotocol.ComputeQualityAvg] Initialize AvgQuality= %d", intAvgQuality);
}
else
{
intAvgQuality = intAvgQuality * (1 - dblAlpha) + (dblAlpha * intReportedQuality) + 0.5f; // exponential averager
WriteDebugLog(LOGDEBUG, "[ARDOPprotocol.ComputeQualityAvg] Reported Quality= %d New Avg Quality= %d", intReportedQuality, intAvgQuality);
}
}
// a function to get then number of carriers from the frame type
int GetNumCarriers(UCHAR bytFrameType)
{
int intNumCar, dummy;
char strType[18];