-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfreemodbusTcp.cpp
286 lines (226 loc) · 8.84 KB
/
freemodbusTcp.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
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
/**
* \file
* \brief Definitions of TCP-related functions for FreeMODBUS
*
* \author Copyright (C) 2019-2022 Kamil Szczygiel https://distortec.com https://freddiechopin.info
*
* \par License
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not
* distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include "freemodbusTcpPoll.hpp"
#if MB_TCP_ENABLED == 1
#include "FreemodbusInstance.hpp"
#include "ListenSocket.hpp"
#include "mbport.h"
#include "lwip/sockets.h"
#include "distortos/Mutex.hpp"
#include "estd/ScopeGuard.hpp"
#include <mutex>
#include <cassert>
namespace
{
/*---------------------------------------------------------------------------------------------------------------------+
| local objects
+---------------------------------------------------------------------------------------------------------------------*/
/// default port for Modbus TCP
constexpr uint16_t defaultPort {502};
/// index of high byte of frame length in MBAP header
constexpr size_t frameLengthHigh {4};
/// index of low byte of frame length in MBAP header
constexpr size_t frameLengthLow {frameLengthHigh + 1};
/*---------------------------------------------------------------------------------------------------------------------+
| local functions
+---------------------------------------------------------------------------------------------------------------------*/
/**
* \brief Releases client socket from FreemodbusInstance.
*
* \param [in] freemodbusInstance is a reference to FreemodbusInstance from which client socket will be released
*/
void releaseClientSocket(FreemodbusInstance& freemodbusInstance)
{
{
assert(freemodbusInstance.listenSocket != nullptr);
assert(freemodbusInstance.listenSocketsRangeMutex != nullptr);
std::lock_guard<distortos::Mutex> lockGuard {*freemodbusInstance.listenSocketsRangeMutex};
const auto ret = freemodbusInstance.listenSocket->clientDisconnected() != 0;
if (ret != 0)
return;
}
while (lwip_recv(freemodbusInstance.clientSocket, freemodbusInstance.frameBuffer,
sizeof(freemodbusInstance.frameBuffer), MSG_DONTWAIT) > 0);
lwip_close(freemodbusInstance.clientSocket);
freemodbusInstance.clientSocket = -1;
}
/**
* \brief Disconnects client if Modbus TCP keepalive deadline has expired.
*
* \param [in] freemodbusInstance is a reference to FreemodbusInstance from which client socket will be released
*/
void checkKeepalive(FreemodbusInstance& freemodbusInstance)
{
// if client is disconnected do nothing
if (freemodbusInstance.clientSocket == -1)
return;
// if keepalive is disabled do nothing
if (freemodbusInstance.tcpKeepaliveDuration == distortos::TickClock::duration{})
return;
if (freemodbusInstance.tcpKeepaliveDeadline < distortos::TickClock::now())
releaseClientSocket(freemodbusInstance);
}
} // namespace
/*---------------------------------------------------------------------------------------------------------------------+
| global functions
+---------------------------------------------------------------------------------------------------------------------*/
void freemodbusTcpPoll(FreemodbusInstance& instance, const distortos::TickClock::time_point deadline)
{
assert(instance.listenSocket != nullptr);
distortos::TickClock::duration left;
while ((left = deadline - distortos::TickClock::now()) >= distortos::TickClock::duration{})
{
auto keepaliveScopeGuard = estd::makeScopeGuard(
[&instance]()
{
checkKeepalive(instance);
});
fd_set fdSet;
FD_ZERO(&fdSet);
if (instance.clientSocket != -1)
FD_SET(instance.clientSocket, &fdSet);
else if (instance.listenSocket->getSocket() != -1)
FD_SET(instance.listenSocket->getSocket(), &fdSet);
{
const auto leftSeconds = std::chrono::duration_cast<std::chrono::seconds>(left);
const auto leftMicroseconds = std::chrono::duration_cast<std::chrono::microseconds>(left - leftSeconds);
timeval timeout {};
timeout.tv_sec = leftSeconds.count();
timeout.tv_usec = leftMicroseconds.count();
if (lwip_select(std::max(instance.clientSocket, instance.listenSocket->getSocket()) + 1, &fdSet, nullptr,
nullptr, &timeout) <= 0)
return;
}
if (instance.clientSocket != -1 && FD_ISSET(instance.clientSocket, &fdSet) != 0)
{
const uint16_t length = instance.bytesInBuffer < FreemodbusInstance::mbapHeaderSize - 1 ? 0 :
((instance.frameBuffer[frameLengthHigh] << 8) | instance.frameBuffer[frameLengthLow]);
const size_t totalSize = FreemodbusInstance::mbapHeaderSize - 1 + length;
if (totalSize > sizeof(instance.frameBuffer))
{
instance.bytesInBuffer = 0;
releaseClientSocket(instance);
continue;
}
const auto ret = lwip_recv(instance.clientSocket, &instance.frameBuffer[instance.bytesInBuffer],
totalSize - instance.bytesInBuffer, {});
if (ret <= 0)
{
instance.bytesInBuffer = 0;
releaseClientSocket(instance);
continue;
}
else
{
instance.bytesInBuffer += ret;
if (totalSize >= FreemodbusInstance::mbapHeaderSize && instance.bytesInBuffer == totalSize)
{
instance.tcpKeepaliveDeadline = distortos::TickClock::now() + instance.tcpKeepaliveDuration;
xMBPortEventPost(&instance.rawInstance, EV_FRAME_RECEIVED);
keepaliveScopeGuard.release();
return;
}
}
}
if (instance.listenSocket->getSocket() != -1 && FD_ISSET(instance.listenSocket->getSocket(), &fdSet) != 0)
{
const auto clientSocket = lwip_accept(instance.listenSocket->getSocket(), nullptr, nullptr);
if (clientSocket == -1)
return;
auto closeScopeGuard = estd::makeScopeGuard(
[clientSocket]()
{
lwip_close(clientSocket);
});
instance.tcpKeepaliveDeadline = distortos::TickClock::now() + instance.tcpKeepaliveDuration;
{
assert(instance.listenSocketsRangeMutex != nullptr);
std::lock_guard<distortos::Mutex> lockGuard {*instance.listenSocketsRangeMutex};
const auto ret = instance.listenSocket->clientConnected();
if (ret != 0)
return;
}
closeScopeGuard.release();
instance.clientSocket = clientSocket;
keepaliveScopeGuard.release();
}
}
}
extern "C" void vMBTCPPortClose(xMBInstance* const instance)
{
assert(instance != nullptr);
vMBTCPPortDisable(instance);
auto& freemodbusInstance = *reinterpret_cast<FreemodbusInstance*>(instance);
{
assert(freemodbusInstance.listenSocket != nullptr);
assert(freemodbusInstance.listenSocketsRangeMutex != nullptr);
std::lock_guard<distortos::Mutex> lockGuard {*freemodbusInstance.listenSocketsRangeMutex};
freemodbusInstance.listenSocket->unbind();
}
freemodbusInstance.listenSocket = nullptr;
}
extern "C" void vMBTCPPortDisable(xMBInstance* const instance)
{
assert(instance != nullptr);
auto& freemodbusInstance = *reinterpret_cast<FreemodbusInstance*>(instance);
if (freemodbusInstance.clientSocket != -1)
releaseClientSocket(freemodbusInstance);
}
extern "C" bool xMBTCPPortGetRequest(xMBInstance* const instance, uint8_t** const frame, uint16_t* const length)
{
assert(instance != nullptr);
auto& freemodbusInstance = *reinterpret_cast<FreemodbusInstance*>(instance);
*frame = freemodbusInstance.frameBuffer;
*length = freemodbusInstance.bytesInBuffer;
freemodbusInstance.bytesInBuffer = {};
return true;
}
extern "C" bool xMBTCPPortInit(xMBInstance* const instance, const uint16_t port)
{
assert(instance != nullptr);
auto& freemodbusInstance = *reinterpret_cast<FreemodbusInstance*>(instance);
assert(freemodbusInstance.listenSocket == nullptr);
assert(freemodbusInstance.listenSocketsRangeMutex != nullptr);
const auto realPort = port != 0 ? port : defaultPort;
std::lock_guard<distortos::Mutex> lockGuard {*freemodbusInstance.listenSocketsRangeMutex};
auto chosenListenSocket = std::find_if(freemodbusInstance.listenSocketsRange.begin(),
freemodbusInstance.listenSocketsRange.end(),
[realPort](const ListenSocket& listenSocket) -> bool
{
return listenSocket.getPort() == realPort;
});
if (chosenListenSocket == freemodbusInstance.listenSocketsRange.end())
chosenListenSocket = std::find_if(freemodbusInstance.listenSocketsRange.begin(),
freemodbusInstance.listenSocketsRange.end(),
[](const ListenSocket& listenSocket) -> bool
{
return listenSocket.getPort() == 0;
});
if (chosenListenSocket == freemodbusInstance.listenSocketsRange.end())
return false;
if (chosenListenSocket->bind(realPort) != 0)
return false;
freemodbusInstance.listenSocket = chosenListenSocket;
return true;
}
extern "C" bool xMBTCPPortSendResponse(xMBInstance* const instance, const uint8_t* const frame, const uint16_t length)
{
assert(instance != nullptr);
auto& freemodbusInstance = *reinterpret_cast<FreemodbusInstance*>(instance);
const auto ret = lwip_send(freemodbusInstance.clientSocket, frame, length, {});
if (ret != length)
{
releaseClientSocket(freemodbusInstance);
return false;
}
return true;
}
#endif // MB_TCP_ENABLED == 1