Repository navigation
Expand file tree
/
Copy pathmodem.cpp
More file actions
1791 lines (1678 loc) · 51.1 KB
/
Copy pathmodem.cpp
File metadata and controls
1791 lines (1678 loc) · 51.1 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
#include <QtCore/QDir>
#include <QtCore/QString>
#include <QtCore/QTimer>
#include <QtCore/QList>
#include <QtCore/QSettings>
#include <QtWidgets/QApplication>
#include <QtNetwork/QHostAddress>
#include <QtNetwork/QHostInfo>
#include <QtNetwork/QNetworkSession>
#include <QtNetwork/QNetworkConfiguration>
#include <QtNetwork/QNetworkConfigurationManager>
#include "utils.hpp"
#include "tncpanel.hpp"
#include "tncsetup.hpp"
#include "modem.hpp"
class ARDOPModem::ARDOPModemPrivate
{
public:
bool blnCMDListener;
bool blnDataListener;
bool boolTcpPortOpen;
bool isGreetingMessageSent;
bool blnTerminatingConnection;
quint32 intHostIBCmdPtr;
quint32 intHostIBDataPtr;
int bytesToWrite;
int bytesWritten;
int bytesReceived;
int bytesToRcv;
int bytesAvailable;
int transferTimerId;
int TotalBytes;
int numBytesForCurrentDataType;
int m_buffers; // number of buffers set or returned
int m_bw; // bandwidth size set or returned
quint16 blockSize;
QByteArray buffer;
QByteArray bytHostIBCmdBuffer;
QByteArray bytHostIBDataBuffer;
QByteArray bytLastCMD_DataSent;
QString id;
QString myNickName;
QString username;
QString greetingMessage;
QString m_grid; // gridsquare set or returned
QString outdata;
};
ARDOPModem::ARDOPModem(QObject *parent) : QObject(parent)
{
#ifdef DEBUG_MODEM
qDebug() << "Constructor Starting... ";
#endif
MCB = new ModemControlBlock;
mp = new ARDOPModemPrivate;
mp->id = "";
// Get saved network configuration
#ifdef DEBUG_MODEM
qDebug() << "reading stored settings... ";
#endif
readSettings(); // get stored settings for modem use
m_tcpipaddress = MCB->TCPAddress;
m_tcpipport = MCB->TCPPort;
if ( m_nickname == "" ) m_nickname = "ARDOPMODEM";
mp->blnCMDListener = false;
mp->blnDataListener = false;
mp->boolTcpPortOpen = false;
mp->isGreetingMessageSent = false;
mp->numBytesForCurrentDataType = -1;
mp->transferTimerId = 0;
mp->intHostIBCmdPtr = 0;
mp->outdata = "C: ";
blnClosing = false;
strLastCommandToHost = "";
modem_mode = Initializing; // set modem mode to Idle on startup
connstate = WaitingForGreeting;
interface = TCPIP; // set to tcp for testing host interface
#ifdef DEBUG_MODEM
qDebug() << "Continuing to load ... ";
#endif
tmrNetworkSend = new QTimer(this);
tmrNetworkSend->setInterval(30000); // connection send timeout at 30,000 ms or 3 secs.
// open the TNC GUI Panel
#ifdef DEBUG_MODEM
qDebug() << "Showing TNC Panel";
#endif
TNC = new TNCPanel;
TNC->setParentObject(this);
TNC->setMCB(MCB);
// TNC->setRCB(RCB);
TNC->show();
clearTNCPanel();
EnableHostLink();
#ifdef DEBUG_MODEM
qDebug() << "Loading done";
#endif
}
ARDOPModem::~ARDOPModem()
{
// try to close and delete host connections here
// if ( objTCPIPCmdClient ) objTCPIPCmdClient->close();
// if ( objTCPIPServer ) objTCPIPServer->close();
// delete objTCPIPCmdClient;
// delete objTCPIPServer;
delete TNC;
delete mp;
}
// ----------------------------------------------------
// EVENT HANDLERS
// ----------------------------------------------------
void ARDOPModem::slotNetworkTimerTimeout()
{
// interval has ended -- connection has timed out
tmrNetworkSend->stop();
#ifdef DEBUG_MODEM
qDebug() << "Server send datagram timeout";
#endif
}
void ARDOPModem::slotSocketStateChg(QAbstractSocket::SocketState s)
{
// determine the connection state on change and act accordingly
switch ( s )
{
case QAbstractSocket::BoundState:
break;
case QAbstractSocket::ClosingState:
break;
case QAbstractSocket::ConnectedState:
m_hostConnected = true;
break;
case QAbstractSocket::ConnectingState:
break;
case QAbstractSocket::HostLookupState:
break;
case QAbstractSocket::ListeningState:
if ( objTCPIPCmdClient->bytesAvailable() > 0 ) processReadyRead();
break;
case QAbstractSocket::UnconnectedState:
m_hostConnected = false;
break;
default:
break;
}
}
void ARDOPModem::closeEvent(QCloseEvent *event)
{
Q_UNUSED(event)
#ifdef DEBUG_MODEM
qDebug() << "...Modem closing";
#endif
blnClosing = true;
}
// ----------------------------------------------------
// PROPERTY SETS AND GETS
// ----------------------------------------------------
void ARDOPModem::setInterfaceType(const QString typ)
{
m_interfacetype = typ; // set the type description for display and property return
if (typ == "TCPIP" ) interface = TCPIP; // set the enum pointer
if (typ == "SERIAL" ) interface = SERIAL; // to be used internally
if (typ == "BLUETOOTH" ) interface = BLUETOOTH; // instead of string compare
}
QString ARDOPModem::getTCPIPAddress()
{
return MCB->TCPAddress;
}
QString ARDOPModem::getSerialPortName()
{
return objSerial->objectName();
}
// ----------------------------------------------------
// PUBLIC METHODS AND FUNCTIONS
// ----------------------------------------------------
bool ARDOPModem::SendCommandToHost(QString strText)
{
#ifdef DEBUG_MODEM
qDebug() << "Sending command to network - " << strText;
#endif
switch ( interface )
{
case TCPIP:
{
QString msg = "C:" + strText + "\n";
objTCPIPCmdClient->write(msg.toLatin1());
if ( objTCPIPCmdClient->error() < 0 )
{
#ifdef DEBUG_MODEM
qDebug() << "Error writing to server:" << objTCPIPCmdClient->errorString();
#endif
return false;
}
else
{
return true;
}
}
case SERIAL:
if ( objSerial && objSerial->isOpen() )
{
try
{
// objSerial->write(bytToSend, bytToSend.length());
return true;
}
catch (exception ex)
{
qCritical() << tr("[HostInterface.SendCommandToHost] Serial Port, c:") << strText << tr(" error:") << QString().arg(objSerial->error()) << " " << objSerial->errorString();
}
}
else
{
qCritical() << tr("[HostInterface.SendCommandToHost] Serial Port ") << MCB->SerCOMPort << tr(" not open");
return false;
}
return false;
case BLUETOOTH:
// This will handle BlueTooth connections ...
// TODO: Add BlueTooth
return false; // Temporary
default:
qCritical() << tr("[HostInterface.SendCommandToHost] No TCPIP, serial, or BlueTooth parameters");
} // switch
return false;
} // SendCommandToHost()
// ----------------------------------------------------
// PRIVATE SLOTS
// ----------------------------------------------------
void ARDOPModem::slotNewConnection()
{
#ifdef DEBUG_MODEM
qDebug() << "Setting up TCP Client so modem can listen for datagrams... ";
#endif
objTCPIPCmdClient = objTCPIPServer->nextPendingConnection();
connect(objTCPIPCmdClient,SIGNAL(readyRead()),this,SLOT(slotNewMessage()));
connect(objTCPIPCmdClient, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(slotConnectionError(QAbstractSocket::SocketError)));
QString msg = "Connected ";
msg += MCB->TCPAddress;
msg += ":";
msg += QString("%1").arg(MCB->TCPPort);
TNC->ui->lblTCP->setText(msg);
QString sheet = "QLabel {background-color: #ccffcc; }"; // ColorSystemButton;
TNC->ui->lblTCP->setStyleSheet(sheet);
TNC->ui->ledTCP->setEnabled(true);
}
void ARDOPModem::slotNewMessage()
{
#ifdef DEBUG_MODEM
qDebug() << tr("Getting New Message");
#endif
QString message = objTCPIPCmdClient->readAll();
#ifdef DEBUG_MODEM
qDebug() << "Message received:" << message;
#endif
if ( message.left(2) != "C:" ) ProcessMessage(message);
if ( message.isEmpty() ) return;
ProcessMessage(message);
}
void ARDOPModem::removeConnection(QTcpSocket *connection)
{
if ( peers.contains(connection->peerAddress()) ) peers.remove(connection->peerAddress());
connection->deleteLater();
}
void ARDOPModem::sessionOpened()
{
}
void ARDOPModem::slotConnected()
{
mp->boolTcpPortOpen = true;
if ( MCB->DebugLog )
{
qDebug() << tr("Modem connected...")
<< "\n"
<< tr("...Modem ")
<< tr("ready For Use...");
}
}
void ARDOPModem::slotRemoveConnection(QString nick)
{
if ( nick.isEmpty() ) return;
QString msg = "Disconnected ";
TNC->ui->lblHost->setText(msg);
QString sheet = "QLabel {background-color: #cccccc; }"; // ColorSystemButton;
TNC->ui->lblHost->setStyleSheet(sheet);
TNC->ui->ledConn->setEnabled(false);
}
void ARDOPModem::slotConnectionError(QAbstractSocket::SocketError socketError)
{
QString msg = "";
QString msg2 = "";
switch ( socketError )
{
case QAbstractSocket::AddressInUseError:
msg2 = "Address in use";
break;
case QAbstractSocket::DatagramTooLargeError:
msg2 = "Datagram Too Large";
break;
case QAbstractSocket::HostNotFoundError:
msg2 = "Host Not Found";
break;
case QAbstractSocket::NetworkError:
msg2 = "General Network Error";
break;
case QAbstractSocket::OperationError:
msg2 = "Invalid Network Operation";
break;
case QAbstractSocket::ProxyAuthenticationRequiredError:
msg2 = "Proxy Authentication Required";
break;
case QAbstractSocket::ProxyConnectionClosedError:
msg2 = "Proxy Connection Closed";
break;
case QAbstractSocket::ProxyConnectionRefusedError:
msg2 = "Proxy Connection Refused";
break;
case QAbstractSocket::ProxyConnectionTimeoutError:
msg2 = "Proxy Connection Timeout";
break;
case QAbstractSocket::ProxyNotFoundError:
msg2 = "Proxy Not Found";
break;
case QAbstractSocket::ProxyProtocolError:
msg2 = "Proxy Protocol Violation";
break;
case QAbstractSocket::RemoteHostClosedError:
msg2 = "Remote Host Closed";
break;
case QAbstractSocket::SocketAccessError:
msg2 = "Socket Access Error";
break;
case QAbstractSocket::SocketAddressNotAvailableError:
msg2 = "Socket Address Not Available";
break;
case QAbstractSocket::SocketResourceError:
msg2 = "Socket Resource Error";
break;
case QAbstractSocket::SocketTimeoutError:
msg2 = "Socket Timeout";
break;
case QAbstractSocket::SslHandshakeFailedError:
msg2 = "Ssl Handshake Failure";
break;
case QAbstractSocket::SslInternalError:
msg2 = "Generalized Ssl Internal Error";
break;
case QAbstractSocket::SslInvalidUserDataError:
msg2 = "Invalid Ssl User Data";
break;
case QAbstractSocket::TemporaryError:
msg2 = "Temporary Error";
break;
case QAbstractSocket::UnfinishedSocketOperationError:
msg2 = "Unfinished Socket Operation";
break;
case QAbstractSocket::UnknownSocketError:
msg2 = "Unknown Socket Error";
break;
case QAbstractSocket::UnsupportedSocketOperationError:
msg2 = "Unsupported Socket Operation";
break;
default:
break;
}
msg = tr("Modem ") + objTCPIPServer->serverAddress().toString();
msg += ":";
msg += QString("%1").arg(objTCPIPServer->serverPort());
qCritical() << msg;
msg = tr("Connection Error . . .");
msg += QString("%1").arg(socketError);
qCritical() << msg;
if ( QTcpSocket *connection = qobject_cast<QTcpSocket *>(sender()) ) removeConnection(connection);
}
void ARDOPModem::slotUpdateClient(qint64 b)
{
Q_UNUSED(b)
// bytesReceived += (int)b;
// buffer = tcpconnection->readAll();
if ( MCB->DebugLog ) qDebug() << "Client Rcvd msg: " << mp->buffer;
// if ( bytesReceived == TotalBytes || tcpconnection->atEnd() )
// {
//processBytes();
//tcpconnection->close();
// }
}
// ----------------------------------------------------
// PRIVATE METHODS AND FUNCTIONS
// ----------------------------------------------------
void ARDOPModem::ProcessMessage(QString indata)
{
#ifdef DEBUG_MODEM
qDebug() << "...Modem received message....";
#endif
QString pdata = indata.trimmed(); // gets rid of the \n
QStringList cmd = pdata.split(" ",QString::KeepEmptyParts);
#ifdef DEBUG_MODEM
qDebug() << "Rcvd message size = " << QString("%1").arg(indata.size()-1);
#endif
// the "C:" says it is a server side command - so
// the command parser should ignore it
if ( cmd.at(0) == "C:") return;
// only contains "c:"
if ( cmd.count() == 1 && cmd.at(0) == "c:") return;
if ( !pdata.endsWith("\0" ) )
{
#ifdef DEBUG_MODEM
qDebug() << "Rcvd message ends w/NULL - " << indata;
#endif
}
else if ( !pdata.endsWith("\n" ) )
{
if ( MCB->DebugLog ) qDebug() << "Rcvd message ends w/CR - " << indata;
}
if ( MCB->DebugLog ) qDebug() << "Checking Rcvd message type";
qDebug() << "PDATA:" << pdata;
for ( int x = 0; x < cmd.count(); ++x)
{
#ifdef DEBUG_MODEM
qDebug() << "CMD: " << cmd.at(x);
#endif
}
if ( cmd.at(1) == "ABORT" )
{
#ifdef DEBUG_MODEM
qDebug() << "ABORT command executed....exiting";
#endif
TNC->close();
delete TNC;
exit(0);
}
else if ( cmd.at(1) == "CMD" )
{
#ifdef DEBUG_MODEM
qDebug() << "Command sent " << pdata;
#endif
}
else if ( cmd.at(1) == "VERSION" )
{
QString msgstr = "Echoing Version at Server: ";
QString msg = APP_VERSION;
qDebug() << msgstr << msg;
QByteArray data = QString("C: VERSION ").toUtf8();
data += APP_VERSION;
if ( !objTCPIPCmdClient->isOpen() ) objTCPIPCmdClient->open(QTcpSocket::WriteOnly);
objTCPIPCmdClient->write(data,data.length());
objTCPIPCmdClient->close();
return;
}
else if ( cmd.at(1) == "MODE" )
{
quint16 modevector = AllModes.indexOf(cmd.at(2),0);
switch (modevector)
{
case 0:
MCB->ProtocolMode = "8FSK.200.25";
qDebug() << "Changing mode to " << MCB->ProtocolMode;
break;
case 1:
MCB->ProtocolMode = "4FSK.200.50S";
break;
case 2:
MCB->ProtocolMode = "4FSK.200.50";
break;
case 3:
MCB->ProtocolMode = "4PSK.200.100S";
break;
case 4:
MCB->ProtocolMode = "4PSK.200.100";
break;
case 5:
MCB->ProtocolMode = "8PSK.200.100";
break;
case 6:
MCB->ProtocolMode = "16FSK_500.25S";
break;
case 7:
MCB->ProtocolMode = "16FSK.500.25";
break;
case 8:
MCB->ProtocolMode = "4FSK.500.100S";
break;
case 9:
MCB->ProtocolMode = "4FSK.500.100";
break;
case 10:
MCB->ProtocolMode = "4PSK.500.100";
break;
case 11:
MCB->ProtocolMode = "8PSK.500.100";
break;
case 12:
MCB->ProtocolMode = "4PSK.500.167";
break;
case 13:
MCB->ProtocolMode = "8PSK.500.167";
break;
case 14:
MCB->ProtocolMode = "4FSK.1000.100";
break;
case 15:
MCB->ProtocolMode = "4PSK.1000.100";
break;
case 16:
MCB->ProtocolMode = "8PSK.1000.100";
break;
case 17:
MCB->ProtocolMode = "4PSK.1000.167";
break;
case 18:
MCB->ProtocolMode = "8PSK.1000.167";
break;
case 19:
MCB->ProtocolMode = "4FSK.2000.600S";
break;
case 20:
MCB->ProtocolMode = "4FSK.2000.600";
break;
case 21:
MCB->ProtocolMode = "4FSK.2000.100";
break;
case 22:
MCB->ProtocolMode = "4PSK.2000.100";
break;
case 23:
MCB->ProtocolMode = "8PSK.2000.100";
break;
case 24:
MCB->ProtocolMode = "4PSK.2000.167";
break;
case 25:
MCB->ProtocolMode = "8PSK.2000.167";
break;
default:
MCB->ProtocolMode = "4PSK.500.100";
break;
}
QString msg = "Rx Frame ";
msg += MCB->ProtocolMode;
TNC->ui->lblRxFrameTxt->setText(msg);
QString sheet = "QLabel {background-color: #ccffcc; }"; // ColorSystemButton;
TNC->ui->lblRxFrameTxt->setStyleSheet(sheet);
}
else
{
QStringList l = indata.split(' ',QString::KeepEmptyParts);
QString strlist;
if ( MCB->DebugLog ) qDebug() << "Strings " << strlist;
}
mp->buffer.chop(1);
processCMD(QString(mp->buffer.data()));
mp->buffer.clear();
if ( MCB->DebugLog ) qDebug() << "Received command verified: " << mp->buffer;
}
bool ARDOPModem::TerminateHostLink()
{
mp->blnTerminatingConnection = true;
Status stcStatus;
switch ( interface )
{
case TCPIP:
try
{
QString msg = "Disconnecting...";
TNC->ui->lblTCP->setText(msg);
QString sheet = "QLabel {background-color: #ff9933; }"; // ColorKacki;
TNC->ui->lblTCP->setStyleSheet(sheet);
TNC->ui->ledTCP->setEnabled(false);
/*
if ( objTCPData )
{
objTCPIPDataClient->close();
delete objTCPIPDataClient;
delete objTCPData;
}
strTCPIPControlConnectionID = "";
strTCPIPDataConnectionID = "";
*/
// We leave listening socket, so
// can accept another connection
}
catch (exception Ex)
{
qCritical() << tr("[HostInterface.TerminateHostLink] error: ") << Ex.what();
stcStatus.BackColor = ColorLightSalmon; // set to red ....
emit signalCtrlChgd(stcStatus.ControlName,stcStatus.BackColor,stcStatus.Text,stcStatus.Value);
MCB->LinkedToHost = false;
return false;
}
MCB->LinkedToHost = false;
return true;
case SERIAL:
break;
case BLUETOOTH:
break;
default:
qCritical() << tr("[HostInterface.TerminateHostLink] Interface Type not set");
}
return false;
} // TerminateHostLink()
void ARDOPModem::processReadyRead()
{
if ( MCB->DebugLog ) qDebug() << "Processing ReadyRead Data";
// readProtocolHeader(); // determine header type
if ( connstate == WaitingForGreeting )
{
if ( MCB->DebugLog )
qDebug() << "Processing ReadyRead Reading Data - waiting for greeting";
// readProtocolHeader(); // determine header type
//buffer = tcpconnection->read(numBytesForCurrentDataType);
if ( MCB->DebugLog )
{
QString msgstr;
msgstr = "Processing ReadyRead Reading Data - data rcvd" + mp->buffer + " size: " + QString("%1").arg(mp->buffer.size());
qDebug() << msgstr;
}
if ( mp->buffer.size() == 0 )
{
#ifdef DEBUG_MODEM
qDebug() << "Modem...not enough buffer data returning";
qDebug() << "Modem...buffer data" << mp->buffer.size();
qDebug() << "Modem...number of bytes " << mp->buffer.size();
#endif
return;
}
#ifdef DEBUG_MODEM
qDebug() << "Modem reading greeting";
#endif
// if ( currentDataType != Greeting) return;
#ifdef DEBUG_MODEM
qDebug() << "Data Type Undefined";
#endif
// QString username = QString(buffer) + '@' + tcpconnection->peerAddress().toString() + ':' + QString::number(tcpconnection->peerPort());
mp->buffer.clear();
#ifdef DEBUG_MODEM
qDebug() << "New state ReadingGreeting";
#endif
if ( !mp->isGreetingMessageSent )
{
mp->greetingMessage = "greeting";
//sendGreetingMessage();
}
connstate = ReadyForUse;
return;
}
#ifdef DEBUG_MODEM
qDebug() << "process data";
qDebug() << "Greeting found - New state...ReadyForUse";
#endif
connstate = ReadyForUse;
}
void ARDOPModem::processData()
{
/*
if ( MCB->DebugLog ) qDebug() << "Reading data to buffer";
buffer = tcpconnection->read(numBytesForCurrentDataType);
if ( MCB->DebugLog ) qDebug() << "If bytes read != num bytes for data type";
if (buffer.size() != numBytesForCurrentDataType) return;
if ( MCB->DebugLog ) qDebug() << "Checking data type";
switch (currentDataType)
{
case Greeting:
if ( MCB->DebugLog )
qDebug() << "New greeting arived: " << QString::fromUtf8(buffer);
return;
break;
case PlainText:
if ( MCB->DebugLog )
{
qDebug() << "New message arived: " << QString::fromUtf8(buffer);
qDebug() << "Sending Command to Interpreter ";
}
commandInterpreter(buffer);
break;
case ModeExact:
if ( MCB->DebugLog )
{
qDebug() << "New message arived: " << QString::fromUtf8(buffer);
qDebug() << "Sending Command to Interpreter ";
}
commandInterpreter(buffer);
break;
case Ping:
tcpconnection->write("PONG 1 p");
break;
case Pong:
pongTime.restart();
break;
default:
currentDataType = Undefined;
numBytesForCurrentDataType = 0;
buffer.clear();
if ( MCB->DebugLog )
{
qDebug() << "Data Type Undefined";
qDebug() << "...Clearing buffer ... done processingData";
}
break;
}
*/
}
bool ARDOPModem::readCMDHeader()
{
#ifdef DEBUG_MODEM
qDebug() << "Reading Protocol Header";
#endif
mp->buffer.clear();
#ifdef DEBUG_MODEM
qDebug() << "ReadProtocolHeader reading data into buffer";
#endif
// readDataIntoBuffer(MaxBufferSize);
QList<QByteArray> incmd;
incmd << mp->buffer.split(' ');
QString msgstr;
msgstr = "parsing data from buffer - " + mp->buffer;
msgstr = " mode: " + incmd[0];
if ( MCB->DebugLog ) qDebug() << msgstr;
if ( incmd[0] == "CLOSE" )
{ // if command is to close do it now.
exit(0);
}
#ifdef DEBUG_MODEM
qDebug() << "Reading Protocol Header - returning data: " << mp->buffer;
#endif
return true;
}
bool ARDOPModem::hasEnoughData()
{
#ifdef DEBUG_MODEM
qDebug() << "checking for data - correct number of bytes: " << mp->numBytesForCurrentDataType;
#endif
if ( mp->buffer.size() <= 0 )
{
#ifdef DEBUG_MODEM
qDebug() << "bytes available < data type or:\n"
<< "num bytes not enough - starting timer \n"
<< "buffer bytes: "
<< mp->buffer.size() << "\n"
<< "tcpconnection bytes= "
// << QString("%1").arg(tcpconnection->bytesAvailable())
<< "bytes for data type: "
<< QString("%1").arg(mp->numBytesForCurrentDataType)
<< "\n checked data returning false";
#endif
return false;
}
#ifdef DEBUG_MODEM
qDebug() << "checked data good - returning true ";
#endif
return true;
}
int ARDOPModem::dataLengthForCurrentDataType()
{
/*
if ( MCB->DebugLog ) qDebug() << "checking for data length";
if ( buffer.size() <= 0 ) return 0;
if ( MCB->DebugLog ) qDebug() << "bytesAvailable > 0";
if ( readDataIntoBuffer(MAX_BUF_SIZE) <= 0 ) return 0;
if ( MCB->DebugLog ) qDebug() << "good data in buffer";
int number = buffer.size();
if ( MCB->DebugLog ) qDebug() << "will return if not ends w/token";
if ( !buffer.endsWith(SeparatorToken) ) return number;
if ( MCB->DebugLog ) qDebug() << "ends with separator token";
buffer.chop(1);
number = buffer.size();
if ( MCB->DebugLog ) qDebug() << "received data length " << number;
return number;
*/
return 0;
}
void ARDOPModem::sendMessage(const char *message)
{
#ifdef DEBUG_MODEM
qDebug() << "got " << QString(message).toUtf8();
#endif
QByteArray msg = QString(message).toUtf8();
QByteArray data = "MESSAGE " + ' ' + msg;
#ifdef DEBUG_MODEM
qDebug() << "Modem sending message " << data;
#endif
try
{
if ( !mp->boolTcpPortOpen )
{
#ifdef DEBUG_MODEM
qDebug() << "Modem connection not open for write";
#endif
// tcpconnection->connectToHost(QHostAddress("127.0.0.1"),8600);
mp->boolTcpPortOpen = true;
}
#ifdef DEBUG_MODEM
qDebug() << "Modem writing to host";
#endif
// tcpconnection->write(data); //== data.size();
// tcpconnection->disconnectFromHost();
}
catch (...)
{
// if ( MCB->DebugLog ) qCritical() << "Modem Socket Error connecting for write:" << tcpconnection->errorString();
}
try
{
// tcpconnection->connectToHost(id, 8600);
}
catch (...)
{
// if ( MCB->DebugLog ) qCritical() << "Modem Socket Error opening port for write:" << tcpconnection->errorString();
}
// boolTcpPortOpen = tcpconnection->isOpen();
mp->boolTcpPortOpen = false;
#ifdef DEBUG_MODEM
qDebug() << "Modem port to host open for write: " << mp->boolTcpPortOpen;
#endif
// tcpconnection->write(message,sizeof(message));
}
int ARDOPModem::readDataIntoBuffer(int x)
{
Q_UNUSED(x);
#ifdef DEBUG_MODEM
qDebug() << "Starting...";
#endif
// if ( buffer.size() > MaxBufferSize ) return -1;
#ifdef DEBUG_MODEM
qDebug() << "Data less than max";
#endif
int numBytesBeforeRead = mp->buffer.size();
// if ( numBytesBeforeRead == MaxBufferSize ) return 0;
#ifdef DEBUG_MODEM
qDebug() << "Max not reached";
#endif
/*
while ( tcpconnection->bytesAvailable() > 0 && buffer.size() < MAX_BUF_SIZE )
{
if ( MCB->DebugLog ) qDebug() << "Appending data";
buffer.append(tcpconnection->read(1));
//if (buffer.endsWith(SeparatorToken)) break;
}
#ifdef DEBUG_MODEM
QString msg;
msg = "Data appended returning Size: ";
msg += QString("%1").arg(buffer.size());
msg += " B4: ";
msg += QString("%1").arg(numBytesBeforeRead);
qDebug() << msg;
#endif
*/
int z = mp->buffer.size() - numBytesBeforeRead;
if (z == 0) z = mp->buffer.size();
#ifdef DEBUG_MODEM
qDebug() << "returning ...";
#endif
return z;
}
void ARDOPModem::disconnect()
{
// if ( tcpconnection->isOpen() ) tcpconnection->disconnect();
mp->boolTcpPortOpen = false;
#ifdef DEBUG_MODEM
qDebug() << "closing connection";
#endif
}
void ARDOPModem::disconnected()
{
#ifdef DEBUG_MODEM
qDebug() << "connection disconnected and closing";
#endif
// tcpconnection->close();
#ifdef DEBUG_MODEM
qDebug() << "connection closed";
#endif
}
void ARDOPModem::connectionError()
{
// errNo = tcpconnection->error();
// errMsg = tcpconnection->errorString();
#ifdef DEBUG_MODEM
qDebug() << "Unexpected Connection Error: "
<< QString().arg(errNo)
<< " - " << errMsg;
#endif
}
bool ARDOPModem::hasConnection(const QHostAddress &senderIp, int senderPort) const
{
if ( senderPort == -1 ) return peers.contains(senderIp);
if ( !peers.contains(senderIp) ) return false;
QList<QTcpSocket *> connections = peers.values(senderIp);
foreach (QTcpSocket *connection, connections)
{
if ( connection->peerPort() == senderPort ) return true;
}
QString msg = "Connected ";
msg += MCB->TCPAddress;
msg += ":";
msg += QString("%1").arg(MCB->TCPPort);
TNC->ui->lblTCP->setText(msg);
QString sheet = "QLabel {background-color: #ccffcc; }"; // ColorLightGreen;
TNC->ui->lblTCP->setStyleSheet(sheet);
TNC->ui->ledTCP->setEnabled(true);
return false;
}
// =========================================================
// PUBLIC METHODS
// =========================================================
// =========================================================
// PRIVATE FUNCTIONS
// =========================================================
bool ARDOPModem::EnableHostLink()
{
#ifdef DEBUG_MODEM
qDebug() << "Enabling Host Link";
#endif
mp->blnTerminatingConnection = false;
Status stcStatus;
// qint16 Trace = 0;
MCB->LinkedToHost = false;
switch ( interface )