-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLttoComms.cpp
More file actions
883 lines (771 loc) · 28.8 KB
/
LttoComms.cpp
File metadata and controls
883 lines (771 loc) · 28.8 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
#include "LttoComms.h"
#include <QEventLoop>
#include <QDebug>
#include "Game.h"
//#define LOCAL_DEBUG_RX
//#define LOCAL_DEBUG_TX
//#define LCD_DISABLED
LttoComms::LttoComms(QObject *parent) : QObject(parent)
{
qDebug() << "LttoComms::LttoComms() - Constructing.......";
dontAnnounceGame = false;
useLongDataPacketsOverTCP = false;
setTcpCommsConnected(false);
currentTaggerBeingHosted = 0;
//TODO: These are only needed due to 2nd instance
//setUseLazerSwarm(false);
//setUseLongDataPacketsOverTCP(true);
tcpComms = new TCPComms(this);
serialUSBcomms = new SerialUSBcomms(this);
lazerSwarm = new LazerSwarm;
connect(this, SIGNAL(sendSerialData(QByteArray)), serialUSBcomms, SLOT(sendPacket(QByteArray)) );
connect(serialUSBcomms, SIGNAL(newSerialUSBdata(QByteArray)), this, SLOT(receivePacket(QByteArray)) );
connect(tcpComms, SIGNAL(TcpCommsConnectionStatus(bool)), this, SLOT(setTcpCommsConnected(bool)) );
connect(this, SIGNAL(sendSerialData(QByteArray)), tcpComms, SLOT(sendPacket(QByteArray)) );
connect(tcpComms, SIGNAL(newTCPdata(QByteArray)), this, SLOT(receivePacket(QByteArray)) );
}
LttoComms *LttoComms::getInstance()
{
static LttoComms *instance = new LttoComms();
return instance;
}
////////////////////////////////////////////////////////////////////
void LttoComms::initialise()
{
qDebug() << "LttoComms::initialise() - Triggered";
if(serialUSBcomms->getIsUSBinitialised() == false)
{
serialUSBcomms->initialiseUSBsignalsAndSlots();
}
if(tcpComms->getIsTCPinitialised() == false)
{
tcpComms->initialiseTCPsignalsAndSlots();
}
if(serialUSBcomms->getSerialCommsConnected() == false)
{
serialUSBcomms->setUpSerialPort();
}
}
bool LttoComms::getConnectionStatus()
{
bool status = false;
if (getTcpCommsConnected() == true || serialUSBcomms->getSerialCommsConnected() == true) status = true;
if (tcpComms->checkIPaddress() != true && serialUSBcomms->getSerialCommsConnected() != true) status = false;
return status;
}
bool LttoComms::checkIPaddress() //checks to see if the current network is 192.168.42.???
{
bool status = tcpComms->checkIPaddress();
if(getSerialUSBcommsConnected() == true) status = true;
return status;
}
bool LttoComms::sendPacket(char type, int data, bool dataFormat)
{
bool result = true;
bool fullTCPpacketToSend = false;
static QByteArray packet;
//Calculating BCD and the CheckSum.
//This is here because it always does my head in !!!!!
//Certain Data is BCD Hex It means it is 2 x 4bit numbers in a single Byte
// Therefore 10 is actually 0001 0000 (which is 0x16)
// and 42 is actually 0100 0010 (which is 0x64)
// and 79 is actually 0111 1001 (which is 0x121)
// and 100 becomes 1111 1111 (which is 0xFF)
//This means that '10' is actually 0x16.
//However other data (taggerID, GameID, Flags, etc) are just plain Hex/Dec all the time.
//****** Now comes the nasty part!!!!! ******
//The LazerSwarm then needs all data sent as actual Hex, whereas the LTTO library expects it as Dec '10'.
//The CheckSum is the Sum of the final Hex values or Decimal values AFTER conversion (where applicable) to BCD.
switch (type)
{
case PACKET:
calculatedCheckSumTx = data;
fullTCPpacketToSend = false;
packetString = createIRstring(data);
packetString.prepend('P');
if(getUseLongDataPacketsOverTCP()) packetString.prepend("ltto:");
packet.append(packetString.toUtf8());
//qDebug() << "lttoComms::sendPacket() - Packet = " << data << "\t:" << packetString;
break;
case DATA:
if (dataFormat == BCD)
{
calculatedCheckSumTx += ConvertDecToBCD(data);
data = ConvertDecToBCD(data);
packetString = createIRstring(data);
packetString.prepend('D');
packet.append(packetString.toUtf8());
//qDebug() << "lttoComms::sendPacket() - BCD Data = " << data << "\t:" << packetString << "\tBCD=" << dataFormat;
}
else
{
calculatedCheckSumTx += data;
packetString = createIRstring(data);
packetString.prepend('D');
packet.append(packetString.toUtf8());
//qDebug() << "lttoComms::sendPacket() - Dec Data = " << data << "\t:" << packetString << "\tBCD=" << dataFormat;
}
break;
case CHECKSUM:
calculatedCheckSumTx = calculatedCheckSumTx % 256; // CheckSum is the remainder of dividing by 256.
calculatedCheckSumTx = calculatedCheckSumTx | 256; // Set the required 9th MSB bit to 1 to indicate it is a checksum
packetString = createIRstring(calculatedCheckSumTx);
packetString.prepend('C');
packet.append(packetString.toUtf8());
fullTCPpacketToSend = true;
//qDebug() << "lttoComms::sendPacket() - CheckSum = \t" << packetString;
break;
case TAG:
packetString = createIRstring(data);
packetString.prepend('T');
if(getUseLongDataPacketsOverTCP()) packetString.prepend("ltto:");
packet.append(packetString.toUtf8());
fullTCPpacketToSend = true;
//qDebug() << "lttoComms::sendPacket() - Tag = \t" << packetString << endl << endl;
break;
case BEACON:
packetString = createIRstring(data);
packetString.prepend('Z');
if(getUseLongDataPacketsOverTCP()) packetString.prepend("ltto:");
packet.append(packetString.toUtf8());
fullTCPpacketToSend = true;
break;
default:
qDebug() << "lttoComms::sendPacket() - No Packet type specified. You ninkom poop.";
result = false;
}
if (useLongDataPacketsOverTCP)
{
packet.append(":");
if (fullTCPpacketToSend == true)
{
packet.append(" \r\n");
emit sendSerialData(packet);
#ifdef LOCAL_DEBUG_TX
if (packet.contains("P02")) qDebug() << "lttoComms::sendPacket() - P02";
else if (packet.contains("P129")) qDebug() << "lttoComms::sendPacket() - P129";
else
//qDebug() << "lttoComms::sendPacket() - Full Packet = " << packet;
#endif
fullTCPpacketToSend = false;
packet.clear();
}
}
else
{
if (getUseLazerSwarm())
{
QByteArray packetToTranslate;
packetToTranslate.append(packet);
packet.clear();
packet.append(lazerSwarm->translateCommand(packetToTranslate).toUtf8());
packet.append(" \r\n");
}
else
packet.append(":");
//qDebug() << "lttoComms::sendPacket() LazerSwarm Mode- " << packet;
emit sendSerialData(packet); //Connects to TCPComms::sendPacket slot && SerialUSBcomms::sendPacket slot
tcpComms->sendPacket(packet);
packet.clear();
nonBlockingDelay(INTERPACKET_DELAY_MSEC);
}
return result;
}
void LttoComms::sendLCDtext(QString textToSend, int lineNumber, bool drawScreen)
{
#ifdef LCD_DISABLED
return;
#endif
//if(!tcpCommsConnected) return; // USB means it is a Lazerswarm, which does not accept TXT.
QByteArray textBA;
textToSend.prepend("TXT" + QString::number(lineNumber)+ ":");
textBA.append(textToSend.toUtf8() + ":" + QString::number(drawScreen).toUtf8() + "\r\n");
tcpComms->sendPacket(textBA);
//qDebug() << "LttoComms::sendLCDtext() - " << textBA;
//lttoComms.nonBlockingDelay(TEXT_SENT_DELAY);
}
void LttoComms::sendLCDtext(int xCursor, int yCursor, QString text, int fontSize, int colour, bool clearDisp, bool drawDisplay)
{
#ifdef LCD_DISABLED
return;
#endif
//if(!tcpCommsConnected) return; // DANGER : This blocks autoreconnect !!!
// USB means it is a Lazerswarm, which does not accept TXT.
QByteArray textBA;
QString textToSend = "";
textToSend.prepend("DSP," + QString::number(xCursor) + "," + QString::number(yCursor) + "," + text + "," + QString::number(fontSize) + "," + QString::number(colour) + "," + QString::number(clearDisp) + "," + QString::number(drawDisplay));
textBA.append(textToSend.toUtf8() + "\r\n");
tcpComms->sendPacket(textBA);
//lttoComms.nonBlockingDelay(TEXT_SENT_DELAY);
}
void LttoComms::sendLEDcolour(int Red, int Green, int Blue)
{
//if(!tcpCommsConnected) return; // USB means it is a Lazerswarm, which does not accept TXT.
QByteArray textBA;
QString textToSend = "";
textToSend.prepend("LED," + QString::number(Red) + "," + QString::number(Green) + "," + QString::number(Blue));
textBA.append(textToSend.toUtf8() + "\r\n");
//qDebug() << "LttoComms::sendLEDcolour() -" << textBA;
tcpComms->sendPacket(textBA);
}
void LttoComms::sendLEDcolour(QString colour)
{
//if(!tcpCommsConnected) return; // USB means it is a Lazerswarm, which does not accept TXT.
QByteArray textBA;
QString textToSend = "";
textToSend.prepend("LED," + colour);
textBA.append(textToSend.toUtf8() + "\r\n");
//qDebug() << "LttoComms::sendLEDcolour() -" << textBA;
tcpComms->sendPacket(textBA);
}
void LttoComms::sendOTAtext(QString ssidText, QString pswdText)
{
//if(!tcpCommsConnected) return; // USB means it is a Lazerswarm, which does not perform OTA upgrades.
QByteArray textBA;
QString textToSend = "";
textToSend.prepend("OTA," + ssidText + "," + pswdText);
textBA.append(textToSend.toUtf8() + "\r\n");
//qDebug() << "LttoComms::sendOTAtext() -" << textBA;
tcpComms->sendPacket(textBA);
}
void LttoComms::sendPing(QString pingText)
{
QByteArray textBA;
QString textToSend = "";
textToSend.prepend("PING," + pingText);
textBA.append(textToSend.toUtf8() + "\r\n");
qDebug() << "LttoComms::sendPing() -" << textBA;
tcpComms->sendPacket(textBA);
}
void LttoComms::sendHeartBeat()
{
QByteArray textBA;
QString textToSend = "HEARTBEAT";
textBA.append(textToSend.toUtf8() + "\r\n");
//qDebug() << "LttoComms::sendHeartBeat()-";
tcpComms->sendPacket(textBA);
}
void LttoComms::sendEspRestart()
{
qDebug() << "LttoComms::sendEspRestart()";
tcpComms->sendPacket("ESP_RESTART\r\n");
}
void LttoComms::sendDisconnectClient()
{
qDebug() << "LttoComms::sendDisconnectClient()";
tcpComms->sendPacket("DISCONNECT\r\n");
}
void LttoComms::sendGetFirmwareVersion()
{
qDebug() << "LttoComms::sendGetFirmwareVersion()";
tcpComms->sendPacket("FIRMWARE_REPORT\r\n");
}
QString LttoComms::createIRstring(int data)
{
QString createdPacket;
if (getUseLazerSwarm()) createdPacket = QString::number(data,16).toUpper();
else createdPacket = QString::number(data,10);
if (createdPacket.length() == 1) createdPacket.prepend('0');
return createdPacket;
}
////////////////////////////////////////////////////////////////////
void LttoComms::receivePacket(QByteArray RxData)
{
//qDebug() << "LttoComms::receivePacket() - " << RxData;
bool isPacketComplete = false;
irDataIn.append(RxData);
if (irDataIn.startsWith("\r\n"))
{
if(irDataIn.length() == 2)
{
irDataIn = "";
return;
}
//qDebug() << "\t\t------------------------------------------------------------\n\t\t\tLttoComms::receivePacket() - irData starts with \\r\\n !!!!!!!!!!!!!!!!!!";
irDataIn = irDataIn.remove(0,2);
//qDebug() << "\t\tModified irDataIn =" << irDataIn;
}
if (irDataIn.endsWith("\r\n") || irDataIn.endsWith("@"))
{
//qDebug() << "\tLttoComms::receivePacket() ="<< irDataIn;
//Remove the \r\n
irDataIn = irDataIn.trimmed();
//qDebug() << "\tLttoComms::receivePacket() - Trimmed:"<< irDataIn;
if (irDataIn.startsWith("ERROR"))
{
qDebug() <<"LttoComms::receivePacket() - ERROR:" << irDataIn;
irDataIn.clear();
return;
}
else if (irDataIn.startsWith("STOP"))
{
setDontAnnounceGame(true);
qDebug() << "\tLttoComms::receivePacket() - STOP";
irDataIn.clear();
return;
}
else if (irDataIn.startsWith("START"))
{
setDontAnnounceGame(false);
setDontAnnounceFailedSignal(false);
qDebug() << "LttoComms::receivePacket() - START";
irDataIn.clear();
return;
}
else if (irDataIn.startsWith("PONG"))
{
emit PongReceived(irDataIn.remove(0,5));
qDebug() << "LttoComms::receivePacket - PONG: " << irDataIn;
irDataIn.clear();
return;
}
else if (irDataIn.startsWith("OTA"))
{
emit OtaReceived(irDataIn.remove(0,4));
qDebug() << "LttoComms::receivePacket - OTA Ack: " << irDataIn;
irDataIn.clear();
return;
}
else if (irDataIn.startsWith("H-B-Ack"))
{
emit HbAckReceived(irDataIn);
qDebug() << "LttoComms::receivePacket - H-B-Ack:" << irDataIn;
irDataIn.clear();
return;
}
else if (irDataIn.startsWith("BATT"))
{
emit BattVoltsReceived(irDataIn.remove(0,5));
qDebug() << "LttoComms::receivePacket - Battery Volts: " << irDataIn;
irDataIn.clear();
return;
}
else if (irDataIn.startsWith("FIRMWARE"))
{
emit FirmwareVersionReceived(irDataIn.remove(0,9));
qDebug() << "LttoComms::receivePacket - Firmware: " << irDataIn;
irDataIn.clear();
return;
}
//Check if this is a CombobulatorHost or LazerSwarm packet.
else if (irDataIn.endsWith("@"))
// CombobulatorHost packet
{
setDontAnnounceGame(true);
setDontAnnounceFailedSignal(true);
// remove the @
irDataIn.remove((irDataIn.size()-1), 1);
//BreakUp messages into "Pxx", "Dxx", "Dxx", "Cxxx,"
int delimiter = 0;
QByteArray packetToAdd;
while(irDataIn.length() > 0)
{
delimiter = irDataIn.indexOf(",");
packetToAdd = irDataIn.left(delimiter);
irDataIn.remove(0, delimiter + 1);
//qDebug() << "** - " << irDataIn;
rxPacketList.append(packetToAdd);
}
#ifdef LOCAL_DEBUG_RX
qDebug() << "LttoComms::receivePacket() -> LTTO library mode - " << " - " << rxPacketList;
#endif
isPacketComplete = true;
}
else if (irDataIn.startsWith("RCV"))
// Lazerswarm packet
{
rxPacketList.append(lazerSwarm->decodeCommand(irDataIn));
#ifdef LOCAL_DEBUG_RX
qDebug() << "LttoComms::receivePacket() LazerSwarm mode - " << lazerSwarm->decodeCommand(irDataIn);
#endif
if (lazerSwarm->decodeCommand(irDataIn).startsWith("C"))
{
#ifdef LOCAL_DEBUG_RX
qDebug() << "LttoComms::receivePacket() LazerSwarm mode - ___________";
#endif
isPacketComplete = true;
}
irDataIn = "";
}
//TODO: Somewhere here I should process Beacons so I can be hostile, and Tags to know which team to be hostile towards.
// Or do I do that inside the WiFiKit32 ??????
else
{
//The message is invalid, so start Tx again.
qDebug() << "\t\tLttoComms::receivePacket() - bad packet receveived, bailing out" << irDataIn;
setDontAnnounceGame(false);
irDataIn="";
}
//qDebug() << "LttoComms::receivePacket() - Done";
}
if (isPacketComplete)
{
irDataIn.clear();
if (!rxPacketList.empty())
{
//qDebug() << "isPacketComplete:" << rxPacketList.last();
//if (!rxPacketList.last().startsWith("D") || !rxPacketList.first().startsWith("P"))
// if (rxPacketList.last().startsWith("C") || rxPacketList.first().startsWith("B") || rxPacketList.first().startsWith("T") )
// {
#ifdef LOCAL_DEBUG_RX
qDebug() << "\tLttoComms::receivePacket() - FULL PACKET!" << rxPacketList;
#endif
processPacket(rxPacketList);
// }
}
}
}
bool LttoComms::getHostingCommsActive() const
{
return hostingCommsActive;
}
void LttoComms::setHostingCommsActive(bool value)
{
hostingCommsActive = value;
}
int LttoComms::getCurrentTaggerBeingHosted() const
{
return currentTaggerBeingHosted;
}
void LttoComms::setCurrentTaggerBeingHosted(int value)
{
currentTaggerBeingHosted = value;
}
bool LttoComms::getUseLongDataPacketsOverTCP() const
{
return useLongDataPacketsOverTCP;
}
void LttoComms::setUseLongDataPacketsOverTCP(bool value)
{
useLongDataPacketsOverTCP = value;
}
void LttoComms::closePorts()
{
qDebug() << "LttoComms::closePorts()";
tcpComms->DisconnectTCP();
serialUSBcomms->closeSerialPort();
setTcpCommsConnected(false);
//setSerialUSBcommsConnected(false);
}
bool LttoComms::getSerialUSBcommsConnected() const
{
return serialUSBcomms->getSerialCommsConnected();
//return serialUSBcommsConnected;
}
//void LttoComms::setSerialUSBcommsConnected(bool value)
//{
// serialUSBcommsConnected = value;
//}
bool LttoComms::getDontAnnounceFailedSignal() const
{
return dontAnnounceFailedSignal;
}
void LttoComms::setDontAnnounceFailedSignal(bool value)
{
dontAnnounceFailedSignal = value;
}
/////////////////////////////////////////////////////////////////////////
bool LttoComms::getTcpCommsConnected() const
{
return tcpCommsConnected;
}
void LttoComms::setTcpCommsConnected(bool value)
{
//qDebug() << "LttoComms::setTcpCommsConnected() triggered";
tcpCommsConnected = value;
setUseLongDataPacketsOverTCP(value); // Activated/Deactivated here !!!!
setUseLazerSwarm(!value);
if(value) sendGetFirmwareVersion();
}
/////////////////////////////////////////////////////////////////////////
int LttoComms::getMissedAnnounceCount() const
{
return missedAnnounceCount;
}
void LttoComms::setMissedAnnounceCount(int value)
{
missedAnnounceCount = value;
}
void LttoComms::androidRxPacket(QByteArray data)
{
receivePacket(data);
}
bool LttoComms::getDontAnnounceGame() const
{
return dontAnnounceGame;
}
void LttoComms::setDontAnnounceGame(bool value)
{
dontAnnounceGame = value;
if (value == true) missedAnnounceCount = 0;
}
bool LttoComms::getUseLazerSwarm() const
{
return useLazerSwarm;
}
void LttoComms::setUseLazerSwarm(bool value)
{
qDebug() <<"LttoComms::setUseLazerSwarm = " << value;
useLazerSwarm = value;
}
bool LttoComms::isCheckSumCorrect(int _command, int _game, int _tagger, int _flags, int _checksum) // Normal Hosting
{
bool result = false;
calculatedCheckSumRx = _command;
calculatedCheckSumRx += _game;
calculatedCheckSumRx += _tagger;
calculatedCheckSumRx += _flags;
calculatedCheckSumRx = calculatedCheckSumRx%256;
if (calculatedCheckSumRx == _checksum%256) result = true;
//qDebug() << "LttoComms::isCheckSumCorrect() -" << calculatedCheckSumRx << ":" << result;
return result;
}
bool LttoComms::isCheckSumCorrect(int _command, int _game, int _tagger, int _taggerInfo, int _smartDeviceInfo, int _checksum) // Ltar Host
{
bool result = false;
calculatedCheckSumRx = _command;
calculatedCheckSumRx += _game;
calculatedCheckSumRx += _tagger;
calculatedCheckSumRx += _taggerInfo;
calculatedCheckSumRx += _smartDeviceInfo;
calculatedCheckSumRx = calculatedCheckSumRx%256;
if (calculatedCheckSumRx == _checksum%256) result = true;
//qDebug() << "LttoComms::isCheckSumCorrect() - LTAR Host=" << calculatedCheckSumRx << ":" << result;
return result;
}
bool LttoComms::isCheckSumCorrect(int _command, int _game, int _teamAndPlayer, int _tagsTaken, int _survivedMinutes, int _survivedSeconds, int _zoneTimeMinutes, int _zoneTimeSeconds, int _flags, int _checksum) // Debrief mode
{
bool result = false;
calculatedCheckSumRx = _command;
calculatedCheckSumRx += _game;
calculatedCheckSumRx += _teamAndPlayer;
calculatedCheckSumRx += _tagsTaken;
calculatedCheckSumRx += _survivedMinutes;
calculatedCheckSumRx += _survivedSeconds;
calculatedCheckSumRx += _zoneTimeMinutes;
calculatedCheckSumRx += _zoneTimeSeconds;
calculatedCheckSumRx += _flags;
calculatedCheckSumRx = calculatedCheckSumRx%256;
if (calculatedCheckSumRx == _checksum%256) result = true;
return result;
}
bool LttoComms::isCheckSumCorrect(int _command, int _game, int _teamAndPlayer, int _playersInReportByte, int _tagsP1, int _tagsP2, int _tagsP3, int _tagsP4, int _tagsP5, int _tagsP6, int _tagsP7, int _tagsP8, int _checksum)
{
bool result = false;
calculatedCheckSumRx = _command;
calculatedCheckSumRx += _game;
calculatedCheckSumRx += _teamAndPlayer;
calculatedCheckSumRx += _playersInReportByte;
calculatedCheckSumRx += _tagsP1;
calculatedCheckSumRx += _tagsP2;
calculatedCheckSumRx += _tagsP3;
calculatedCheckSumRx += _tagsP4;
calculatedCheckSumRx += _tagsP5;
calculatedCheckSumRx += _tagsP6;
calculatedCheckSumRx += _tagsP7;
calculatedCheckSumRx += _tagsP8;
//qDebug() << "LttoComms::isCheckSumCorrect() -" << calculatedCheckSumRx << ":" << calculatedCheckSumRx%256;
calculatedCheckSumRx = calculatedCheckSumRx%256;
if (calculatedCheckSumRx == _checksum%256) result = true;
return result;
}
void LttoComms::processPacket(QList<QByteArray> data)
{
//#ifdef LOCAL_DEBUG_RX
qDebug() << "\tLttoComms::processPacket()" << data;
//#endif
int command = extract(data);
int game = 0;
int tagger = 0;
int taggerInfo = 0;
int smartDeviceInfo = 0;
int flags = 0;
int teamAndPlayer = 0;
int tagsTaken = 0;
int playersInReportByte = 0;
int survivedMinutes = 0;
int survivedSeconds = 0;
int zoneTimeMinutes = 0;
int zoneTimeSeconds = 0;
int checksum = 0;
int tagsP1 = 0;
int tagsP2 = 0;
int tagsP3 = 0;
int tagsP4 = 0;
int tagsP5 = 0;
int tagsP6 = 0;
int tagsP7 = 0;
int tagsP8 = 0;
switch (command)
{
case REQUEST_JOIN_GAME: //P16
game = extract(data);
tagger = extract(data);
flags = extract(data);
checksum = extract(data);
if(currentTaggerBeingHosted != tagger && currentTaggerBeingHosted != 0)
{
qDebug() << "\tLttoComms::processPacket - **** Tagger ID mis-match **** Dumping packet";
static int failCount = 0;
if (failCount++ > 5) currentTaggerBeingHosted = 0; //reset in case a tagger died mid-hosting.
break; // to stop contention when two taggers respond.
}
if(isCheckSumCorrect(command, game, tagger, flags, checksum)) emit RequestJoinGame(game, tagger, flags, false);
//else setDontAnnounceGame(false);
break;
case REQUEST_JOIN_LTAR_GAME: //P130
game = extract(data);
tagger = extract(data);
taggerInfo = extract(data);
smartDeviceInfo = extract(data);
checksum = extract(data);
if(isCheckSumCorrect(command, game, tagger, taggerInfo, smartDeviceInfo, checksum)) emit RequestJoinGame(game, tagger, 0, true);
else qDebug() << "\tLttoComms::processPacket - REQUEST_JOIN_LTAR_GAME **** CheckSum Error"; //setDontAnnounceGame(false);
break;
case ACK_PLAYER_ASSIGN: //P17
game = extract(data);
tagger = extract(data);
checksum = extract(data);
if(isCheckSumCorrect(command, game, tagger, flags, checksum)) emit AckPlayerAssignment(game, tagger, false);
else
{
setDontAnnounceFailedSignal(true);
qDebug() << "\tLttoComms::processPacket - ACK_ASSIGN_PLAYER **** CheckSum Error";
}
break;
case ACK_LTAR_PLAYER_ASSIGN: //P132
game = extract(data);
tagger = extract(data);
teamAndPlayer = extract(data);
checksum = extract(data);
if(isCheckSumCorrect(command, game, tagger, teamAndPlayer, checksum)) emit AckPlayerAssignment(game, tagger, true);
else
{
setDontAnnounceFailedSignal(true);
qDebug() << "\tLttoComms::processPacket - - ACK_ASSIGN_PLAYER **** P132 CheckSum Error";
}
break;
case TAG_SUMMARY: //P64
game = extract(data);
teamAndPlayer = extract(data);
tagsTaken = extract(data);
survivedMinutes = extract(data);
survivedSeconds = extract(data);
zoneTimeMinutes = extract(data);
zoneTimeSeconds = extract(data);
flags = extract(data);
checksum = extract(data);
if(isCheckSumCorrect(command, game, teamAndPlayer, tagsTaken, survivedMinutes, survivedSeconds, zoneTimeMinutes, zoneTimeSeconds, flags, checksum))
emit TagSummaryReceived(game, teamAndPlayer, tagsTaken, survivedMinutes, survivedSeconds, zoneTimeMinutes, zoneTimeSeconds, flags);
else setDontAnnounceGame(false);
break;
case TEAM_1_TAG_REPORT: //P65
case TEAM_2_TAG_REPORT: //P66
case TEAM_3_TAG_REPORT: //P67
game = extract(data);
teamAndPlayer = extract(data);
playersInReportByte = extract(data);
if((playersInReportByte >> 0) & 1) tagsP1 = extract(data);
if((playersInReportByte >> 1) & 1) tagsP2 = extract(data);
if((playersInReportByte >> 2) & 1) tagsP3 = extract(data);
if((playersInReportByte >> 3) & 1) tagsP4 = extract(data);
if((playersInReportByte >> 4) & 1) tagsP5 = extract(data);
if((playersInReportByte >> 5) & 1) tagsP6 = extract(data);
if((playersInReportByte >> 6) & 1) tagsP7 = extract(data);
if((playersInReportByte >> 7) & 1) tagsP8 = extract(data);
checksum = extract(data);
if(isCheckSumCorrect(command, game, teamAndPlayer, playersInReportByte, tagsP1, tagsP2, tagsP3, tagsP4, tagsP5, tagsP6, tagsP7, tagsP8, checksum))
{
tagsP1 = ConvertBCDtoDec(tagsP1);
tagsP2 = ConvertBCDtoDec(tagsP2);
tagsP3 = ConvertBCDtoDec(tagsP3);
tagsP4 = ConvertBCDtoDec(tagsP4);
tagsP5 = ConvertBCDtoDec(tagsP5);
tagsP6 = ConvertBCDtoDec(tagsP6);
tagsP7 = ConvertBCDtoDec(tagsP7);
tagsP8 = ConvertBCDtoDec(tagsP8);
switch(command)
{
case TEAM_1_TAG_REPORT:
emit Team1TagReportReceived(game, teamAndPlayer, tagsP1, tagsP2, tagsP3, tagsP4, tagsP5, tagsP6, tagsP7, tagsP8);
break;
case TEAM_2_TAG_REPORT:
emit Team2TagReportReceived(game, teamAndPlayer, tagsP1, tagsP2, tagsP3, tagsP4, tagsP5, tagsP6, tagsP7, tagsP8);
break;
case TEAM_3_TAG_REPORT:
emit Team3TagReportReceived(game, teamAndPlayer, tagsP1, tagsP2, tagsP3, tagsP4, tagsP5, tagsP6, tagsP7, tagsP8);
break;
}
}
else setDontAnnounceGame(false);
break;
case BEACON:
qDebug() << "LttoComms::processPacket() - BEACON !";
//setDontAnnounceGame(false);
break;
case LTAR_BEACON:
qDebug() << "LttoComms::processPacket() - LTAR BEACON !";
break;
case TAG:
qDebug() << "LttoComms::processPacket() - TAG !";
//setDontAnnounceGame(false);
break;
case 50: //TODO remove this kludge (due to ESP sending echoes of P50 Host messages).
qDebug() << "LttoComms::processPacket() - Dumping P50 !";
break;
case 2: //Somehow we are seeing our own hosting messages
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
case 12:
case 49:
case 129:
qDebug() << "LttoComms::processPacket() - Host packet received - P" << command << data;
setDontAnnounceGame(false);
break;
default:
//We end up here if the ESP32 misses the Packet Header
qDebug() << "LttoComms::processPacket() - ESP32 missed the packet header again:" << command; // << data;
setDontAnnounceGame(false);
break;
}
rxPacketList.clear();
data.clear();
}
int LttoComms::extract(QList<QByteArray> &data)
{
QString command;
if (data.empty() == false)
{
command = data.takeFirst();
command = command.mid(1 , (command.length()-1) );
}
bool ok = false;
return command.toInt(&ok, 10);
}
int LttoComms::ConvertDecToBCD(int dec)
{
if(gameInfo.getIsLTARGame()) return dec; //LTAR hosting protocol does not use BCD
if (dec == 100) return 0xFF;
//return static_cast<int>(((dec/10) << 4) | (dec %10) );
return (int) (((dec/10) << 4) | (dec %10) );
}
int LttoComms::ConvertBCDtoDec(int bcd)
{
if(gameInfo.getIsLTARGame()) return bcd; //LTAR hosting protocol does not use BCD
if (bcd == 0xFF) return bcd;
//return static_cast<int>(((bcd >> 4) & 0xF) *10) + (bcd & 0xF);
return (int) (((bcd >> 4) & 0xF) *10) + (bcd & 0xF);
}
void LttoComms::nonBlockingDelay(int mSec)
{
QEventLoop loop;
QTimer::singleShot(mSec, &loop, SLOT(quit()));
loop.exec();
}