-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
qiec104.cpp
187 lines (157 loc) · 5.32 KB
/
qiec104.cpp
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
/*
* This software implements an IEC 60870-5-104 protocol tester.
* Copyright © 2010-2024 Ricardo L. Olsen
*
* Disclaimer
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "qiec104.h"
#include <QtWidgets/QApplication>
QIec104::QIec104(QObject *parent) : QObject(parent) {
mEnding = false;
mAllowConnect = true;
SendCommands = 0;
ForcePrimary = 0;
mLog.activateLog();
mLog.doLogTime();
tcps = new QTcpSocket(nullptr);
tmKeepAlive = new QTimer();
connect(tmKeepAlive, SIGNAL(timeout()), this, SLOT(slot_keep_alive()));
connect(tcps, SIGNAL(readyRead()), this, SLOT(slot_tcpreadytoread()));
connect(tcps, SIGNAL(connected()), this, SLOT(slot_tcpconnect()));
connect(tcps, SIGNAL(disconnected()), this, SLOT(slot_tcpdisconnect()));
connect(tcps, SIGNAL(errorOccurred(QAbstractSocket::SocketError)), this,
SLOT(slot_tcperror(QAbstractSocket::SocketError)),
Qt::DirectConnection);
//tcps->moveToThread(&tcpThread);
//tcpThread.start(QThread::TimeCriticalPriority);
}
QIec104::~QIec104() {
delete tmKeepAlive;
delete tcps;
}
void QIec104::waitBytes(int bytes, int msTout) {
while (tcps->bytesAvailable() < bytes && msTout > 0) {
tcps->waitForReadyRead(8);
msTout -= 8;
}
}
void QIec104::dataIndication(iec_obj *obj, unsigned numpoints) {
emit signal_dataIndication(obj, numpoints);
}
void QIec104::connectTCP() {
static int cnt = 0;
char buf[100];
tcps->close();
if (!mEnding && mAllowConnect) {
// alternate main and backup UTR IP address, if configured
if ((++cnt) % 2 || strcmp(getSecondaryIP_backup(), "") == 0) {
tcps->connectToHost(getSecondaryIP(), quint16(getPortTCP()),
QIODevice::ReadWrite);
sprintf(buf, "Try to connect IP: %s", getSecondaryIP());
mLog.pushMsg(const_cast<char *>(buf));
} else {
tcps->connectToHost(getSecondaryIP_backup(), quint16(getPortTCP()),
QIODevice::ReadWrite);
sprintf(buf, "Try to connect IP: %s", getSecondaryIP_backup());
mLog.pushMsg(const_cast<char *>(buf));
}
}
}
void QIec104::disconnectTCP() { tcps->close(); }
void QIec104::slot_tcperror(QAbstractSocket::SocketError socketError) {
if (socketError != QAbstractSocket::SocketTimeoutError) {
char buf[100];
sprintf(buf, "SocketError: %d", socketError);
mLog.pushMsg(const_cast<char *>(buf));
}
}
int QIec104::readTCP(char *buf, int szmax) {
int ret = int(tcps->read(buf, szmax));
if (!mEnding && ret > 0)
return ret;
else
return 0;
}
// send tcp data, user provided
void QIec104::sendTCP(char *data, int sz) {
if (tcps->state() == QAbstractSocket::ConnectedState)
if (!mEnding) {
tcps->write(data, sz);
if (mLog.isLogging())
LogFrame(data, sz, true);
}
}
void QIec104::slot_tcpconnect() {
tcps->setSocketOption(QAbstractSocket::LowDelayOption, 1);
onConnectTCP();
emit signal_tcp_connect();
}
void QIec104::slot_tcpdisconnect() {
onDisconnectTCP();
emit signal_tcp_disconnect();
}
void QIec104::slot_keep_alive() {
static unsigned int cnts = 1;
if (!mEnding) {
cnts++;
if (!(cnts % 5))
if (tcps->state() != QAbstractSocket::ConnectedState && mAllowConnect) {
mLog.pushMsg("!!!!!TRY TO CONNECT!");
connectTCP();
}
onTimerSecond();
}
}
void QIec104::interrogationActConfIndication() {
emit signal_interrogationActConfIndication();
}
void QIec104::interrogationActTermIndication() {
emit signal_interrogationActTermIndication();
}
void QIec104::commandActRespIndication(iec_obj *obj) {
emit signal_commandActRespIndication(obj);
}
void QIec104::terminate() {
mEnding = true;
tcps->close();
//tcpThread.quit();
//tcpThread.wait(1000);
//if (tcpThread.isRunning())
// tcpThread.terminate();
//if (tcpThread.isRunning())
// tcpThread.wait(2000);
}
void QIec104::slot_tcpreadytoread() {
if (tcps->bytesAvailable() < 6)
tcps->waitForReadyRead(8);
packetReadyTCP();
}
void QIec104::disable_connect() {
mAllowConnect = false;
if (tcps->state() == QAbstractSocket::ConnectedState)
disconnectTCP();
}
void QIec104::enable_connect() { mAllowConnect = true; }
int QIec104::bytesAvailableTCP() { return int(tcps->bytesAvailable()); }