-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTCPComms.cpp
More file actions
executable file
·169 lines (146 loc) · 4.9 KB
/
TCPComms.cpp
File metadata and controls
executable file
·169 lines (146 loc) · 4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include "TCPComms.h"
#include "LttoComms.h"
#include <QNetworkConfiguration>
#include <QNetworkConfigurationManager>
#include <QNetworkSession>
//TCPComms tcpComms;
TCPComms::TCPComms(QObject *parent) :
QObject(parent)
{
qDebug() << "TCPComms::TCPComms() - Constructing.......";
tcpSocket = new QTcpSocket(this);
emit TcpCommsConnectionStatus(false);
setIsTCPinitialised(false);
isConnected = false;
}
bool TCPComms::ConnectTCP()
{
bool result = false;
connectToCombobulatorWiFi();
if(checkIPaddress() != true)
{
//qDebug() << "TCPComms::ConnectTCP() - Not connected to Combobulator Wi-Fi";
}
else
{
//qDebug() << "TCPComms::ConnectTCP() - Connecting";
tcpSocket->connectToHost(HOST_IP_ADDRESS,TCP_IP_PORT);
result = true;
}
return result;
}
bool TCPComms::DisconnectTCP()
{
tcpSocket->disconnectFromHost();
qDebug() << "TCPComms::DisconnectTCP() - Disconnected !!!";
emit TcpCommsConnectionStatus(false);
return false;
}
bool TCPComms::connectToCombobulatorWiFi()
{
return false;
qDebug() << "TCPComms::connectToCombobulatorWiFi()";
// bool result = false;
// QNetworkInterface networkInterface;
// QNetworkConfiguration networkConfig;
// QNetworkConfigurationManager networkManager;
// auto networkConnection = networkManager.allConfigurations();
// for (auto &networkFound : networkConnection)
// {
// if (networkFound.bearerType() == QNetworkConfiguration::BearerWLAN)
// {
// qDebug() << "TCPComms::connectToCombobulatorWiFi() - Network found: " << networkFound.name();
// if (networkFound.name() == "Combobulator")
// networkConfig = networkFound;
// result = true;
// }
// }
// auto session = new QNetworkSession(networkConfig, this);
// session->open();
// qDebug() << "TCPComms::connectToCombobulatorWiFi() - " << result;
// return result;
}
bool TCPComms::checkIPaddress()
{
bool result = false;
QList<QHostAddress> list = QNetworkInterface::allAddresses();
for(int nIter=0; nIter<list.count(); nIter++)
{
if(!list[nIter].isLoopback())
if (list[nIter].protocol() == QAbstractSocket::IPv4Protocol )
{
QString thisIP = list[nIter].toString();
//qDebug() << "TCPComms::checkIPaddress() - " << thisIP;
if(thisIP.startsWith("192.168.42"))
{
//qDebug() << "TCPComms::checkIPaddress() - We have a Combobulator ";
result = true;
}
}
}
return result;
// foreach (const QNetworkInterface &netInterface, QNetworkInterface::allInterfaces()) {
// QNetworkInterface::InterfaceFlags flags = netInterface.flags();
// if( (bool)(flags & QNetworkInterface::IsRunning) && !(bool)(flags & QNetworkInterface::IsLoopBack)){
// foreach (const QNetworkAddressEntry &address, netInterface.addressEntries()) {
// if(address.ip().protocol() == QAbstractSocket::IPv4Protocol)
// qDebug() << address.ip().toString();
// }
// }
// }
}
void TCPComms::connected()
{
qDebug() << "\n\tTCPComms::connected !!!! :-)\n";
emit TcpCommsConnectionStatus(true);
qDebug() << "TCPComms::connected() - \tCONNECTION ESTABLISHED";
isConnected = true;
}
void TCPComms::disconnected()
{
qDebug() << "\n\t\tTCPComms::disconnected :-(\n";
//lttoComms.setTcpCommsConnected(false);
emit TcpCommsConnectionStatus(false);
isConnected = false;
}
void TCPComms::receivePacket()
{
QByteArray rX;
rX = tcpSocket->readAll();
emit newTCPdata(rX);
}
void TCPComms::sendPacket(QByteArray data)
{
if (tcpSocket->state() == QAbstractSocket::ConnectedState)
{
//qDebug() << "TCPComms::sendPacket(QByteArray data)" << data;
tcpSocket->write(data);
}
else
{
//TODO1: FIX THIS ASAP if(lttoComms.getSerialUSBcommsConnected() == false)
//qDebug() << "TCPComms::sendPacket() - Triggered ConnectTCP()";
ConnectTCP();
}
}
void TCPComms::initialiseTCPsignalsAndSlots()
{
qDebug() << "TCPcomms::initialiseTCPsignalsAndSlots() - Triggered";
//Needs to be done here not in constructor because lttoComms a static and may not exist when this static is created.
setIsTCPinitialised(true);
connect(tcpSocket, SIGNAL(connected()), this, SLOT(connected()) );
connect(tcpSocket, SIGNAL(disconnected()), this, SLOT(disconnected()) );
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(receivePacket()), Qt::DirectConnection );
//connect(tcpSocket, SIGNAL(disconnected()), lttoComms, SLOT(TCPdisconnected()) );
//connect(tcpSocket, SIGNAL(connected()), lttoComms, SLOT(TCPconnected()) );
//connect(lttoComms, SIGNAL(sendSerialData(QByteArray)), this, SLOT(sendPacket(QByteArray)) );
//connect(this, SIGNAL(newTCPdata(QByteArray)), lttoComms, SLOT(receivePacket(QByteArray)) );
}
bool TCPComms::getIsTCPinitialised() const
{
return isTCPinitialised;
}
void TCPComms::setIsTCPinitialised(bool value)
{
isTCPinitialised = value;
}