-
Notifications
You must be signed in to change notification settings - Fork 220
Expand file tree
/
Copy pathAdsLib.cpp
More file actions
321 lines (291 loc) · 8.28 KB
/
AdsLib.cpp
File metadata and controls
321 lines (291 loc) · 8.28 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
// SPDX-License-Identifier: MIT
/**
Copyright (c) 2015 - 2022 Beckhoff Automation GmbH & Co. KG
*/
#include "AdsLib.h"
#include "AmsRouter.h"
static AmsRouter &GetRouter()
{
static AmsRouter router;
return router;
}
#define ASSERT_PORT(port) \
do { \
if ((port) <= 0 || (port) > UINT16_MAX) { \
return ADSERR_CLIENT_PORTNOTOPEN; \
} \
} while (false)
#define ASSERT_PORT_AND_AMSADDR(port, pAddr) \
do { \
ASSERT_PORT(port); \
if (!(pAddr)) { \
return ADSERR_CLIENT_NOAMSADDR; \
} \
} while (false)
namespace bhf
{
namespace ads
{
long AddLocalRoute(const AmsNetId ams, const char *ip)
{
try {
return GetRouter().AddRoute(ams, ip);
} catch (const std::bad_alloc &) {
return GLOBALERR_NO_MEMORY;
} catch (const std::runtime_error &) {
return GLOBALERR_TARGET_PORT;
}
}
void DelLocalRoute(const AmsNetId ams)
{
GetRouter().DelRoute(ams);
}
void SetLocalAddress(const AmsNetId ams)
{
GetRouter().SetLocalAddress(ams);
}
}
}
long AdsPortCloseEx(long port)
{
ASSERT_PORT(port);
return GetRouter().ClosePort((uint16_t)port);
}
long AdsPortOpenEx()
{
return GetRouter().OpenPort();
}
long AdsGetLocalAddressEx(long port, AmsAddr *pAddr)
{
ASSERT_PORT_AND_AMSADDR(port, pAddr);
return GetRouter().GetLocalAddress((uint16_t)port, pAddr);
}
long AdsSyncReadReqEx2(long port, const AmsAddr *pAddr, uint32_t indexGroup,
uint32_t indexOffset, uint32_t bufferLength,
void *buffer, uint32_t *bytesRead)
{
ASSERT_PORT_AND_AMSADDR(port, pAddr);
if (!buffer) {
return ADSERR_CLIENT_INVALIDPARM;
}
try {
AmsRequest request{ *pAddr,
(uint16_t)port,
AoEHeader::READ,
bufferLength,
buffer,
bytesRead,
sizeof(AoERequestHeader) };
request.frame.prepend(AoERequestHeader{ indexGroup, indexOffset,
bufferLength });
return GetRouter().AdsRequest(request);
} catch (const std::bad_alloc &) {
return GLOBALERR_NO_MEMORY;
}
}
long AdsSyncReadDeviceInfoReqEx(long port, const AmsAddr *pAddr, char *devName,
AdsVersion *version)
{
ASSERT_PORT_AND_AMSADDR(port, pAddr);
if (!devName || !version) {
return ADSERR_CLIENT_INVALIDPARM;
}
try {
static const size_t NAME_LENGTH = 16;
uint8_t buffer[sizeof(*version) + NAME_LENGTH];
AmsRequest request{ *pAddr, (uint16_t)port,
AoEHeader::READ_DEVICE_INFO, sizeof(buffer),
buffer };
const auto status = GetRouter().AdsRequest(request);
if (!status) {
version->version = buffer[0];
version->revision = buffer[1];
version->build = bhf::ads::letoh<uint16_t>(
buffer + offsetof(AdsVersion, build));
memcpy(devName, buffer + sizeof(*version), NAME_LENGTH);
}
return status;
} catch (const std::bad_alloc &) {
return GLOBALERR_NO_MEMORY;
}
}
long AdsSyncReadStateReqEx(long port, const AmsAddr *pAddr, uint16_t *adsState,
uint16_t *devState)
{
ASSERT_PORT_AND_AMSADDR(port, pAddr);
if (!adsState || !devState) {
return ADSERR_CLIENT_INVALIDPARM;
}
try {
uint16_t buffer[2];
AmsRequest request{ *pAddr, (uint16_t)port,
AoEHeader::READ_STATE, sizeof(buffer),
buffer };
const auto status = GetRouter().AdsRequest(request);
if (!status) {
*adsState = bhf::ads::letoh(buffer[0]);
*devState = bhf::ads::letoh(buffer[1]);
}
return status;
} catch (const std::bad_alloc &) {
return GLOBALERR_NO_MEMORY;
}
}
long AdsSyncReadWriteReqEx2(long port, const AmsAddr *pAddr,
uint32_t indexGroup, uint32_t indexOffset,
uint32_t readLength, void *readData,
uint32_t writeLength, const void *writeData,
uint32_t *bytesRead)
{
ASSERT_PORT_AND_AMSADDR(port, pAddr);
if ((readLength && !readData) || (writeLength && !writeData)) {
return ADSERR_CLIENT_INVALIDPARM;
}
try {
AmsRequest request{ *pAddr,
(uint16_t)port,
AoEHeader::READ_WRITE,
readLength,
readData,
bytesRead,
sizeof(AoEReadWriteReqHeader) +
writeLength };
request.frame.prepend(writeData, writeLength);
request.frame.prepend(AoEReadWriteReqHeader{
indexGroup, indexOffset, readLength, writeLength });
return GetRouter().AdsRequest(request);
} catch (const std::bad_alloc &) {
return GLOBALERR_NO_MEMORY;
}
}
long AdsSyncWriteReqEx(long port, const AmsAddr *pAddr, uint32_t indexGroup,
uint32_t indexOffset, uint32_t bufferLength,
const void *buffer)
{
ASSERT_PORT_AND_AMSADDR(port, pAddr);
if (bufferLength && !buffer) {
return ADSERR_CLIENT_INVALIDPARM;
}
try {
AmsRequest request{
*pAddr,
(uint16_t)port,
AoEHeader::WRITE,
0,
nullptr,
nullptr,
sizeof(AoERequestHeader) + bufferLength,
};
request.frame.prepend(buffer, bufferLength);
request.frame.prepend<AoERequestHeader>(
{ indexGroup, indexOffset, bufferLength });
return GetRouter().AdsRequest(request);
} catch (const std::bad_alloc &) {
return GLOBALERR_NO_MEMORY;
}
}
long AdsSyncWriteControlReqEx(long port, const AmsAddr *pAddr,
uint16_t adsState, uint16_t devState,
uint32_t bufferLength, const void *buffer)
{
ASSERT_PORT_AND_AMSADDR(port, pAddr);
try {
AmsRequest request{ *pAddr,
(uint16_t)port,
AoEHeader::WRITE_CONTROL,
0,
nullptr,
nullptr,
sizeof(AdsWriteCtrlRequest) +
bufferLength };
request.frame.prepend(buffer, bufferLength);
request.frame.prepend<AdsWriteCtrlRequest>(
{ adsState, devState, bufferLength });
return GetRouter().AdsRequest(request);
} catch (const std::bad_alloc &) {
return GLOBALERR_NO_MEMORY;
}
}
long AdsSyncAddDeviceNotificationReqEx(long port, const AmsAddr *pAddr,
uint32_t indexGroup,
uint32_t indexOffset,
const AdsNotificationAttrib *pAttrib,
PAdsNotificationFuncEx pFunc,
uint32_t hUser, uint32_t *pNotification)
{
ASSERT_PORT_AND_AMSADDR(port, pAddr);
if (!pAttrib || !pFunc || !pNotification) {
return ADSERR_CLIENT_INVALIDPARM;
}
try {
uint8_t buffer[sizeof(*pNotification)];
AmsRequest request{ *pAddr,
(uint16_t)port,
AoEHeader::ADD_DEVICE_NOTIFICATION,
sizeof(buffer),
buffer,
nullptr,
sizeof(AdsAddDeviceNotificationRequest) };
request.frame.prepend(AdsAddDeviceNotificationRequest{
indexGroup, indexOffset, pAttrib->cbLength,
pAttrib->nTransMode, pAttrib->nMaxDelay,
pAttrib->nCycleTime });
auto notify = std::make_shared<Notification>(pFunc, hUser,
pAttrib->cbLength,
*pAddr,
(uint16_t)port);
return GetRouter().AddNotification(request, pNotification,
notify);
} catch (const std::bad_alloc &) {
return GLOBALERR_NO_MEMORY;
}
}
long AdsSyncDelDeviceNotificationReqEx(long port, const AmsAddr *pAddr,
uint32_t hNotification)
{
ASSERT_PORT_AND_AMSADDR(port, pAddr);
return GetRouter().DelNotification((uint16_t)port, pAddr,
hNotification);
}
long AdsAddSyntheticDeviceNotificationReqEx(long port, const AmsAddr *pAddr,
uint32_t nType,
PAdsSyntheticNotificationFuncEx pFunc,
uint32_t hUser, uint32_t *pNotification)
{
ASSERT_PORT_AND_AMSADDR(port, pAddr);
if (!pFunc || !pNotification) {
return ADSERR_CLIENT_INVALIDPARM;
}
if (nType != NOTIFY_NOTIFICATION_RCV &&
nType != NOTIFY_CONNECTION_LOST) {
return ADSERR_CLIENT_INVALIDPARM;
}
try {
auto notify = std::make_shared<SyntheticNotification>(
pFunc, hUser, *pAddr, (uint16_t)port, nType);
return GetRouter().AddSyntheticNotification(
(uint16_t)port, *pAddr, pNotification, notify);
} catch (const std::bad_alloc &) {
return GLOBALERR_NO_MEMORY;
}
}
long AdsDelSyntheticDeviceNotificationReqEx(long port, const AmsAddr *pAddr,
uint32_t hNotification)
{
ASSERT_PORT_AND_AMSADDR(port, pAddr);
return GetRouter().DelSyntheticNotification((uint16_t)port, pAddr,
hNotification);
}
long AdsSyncGetTimeoutEx(long port, uint32_t *timeout)
{
ASSERT_PORT(port);
if (!timeout) {
return ADSERR_CLIENT_INVALIDPARM;
}
return GetRouter().GetTimeout((uint16_t)port, *timeout);
}
long AdsSyncSetTimeoutEx(long port, uint32_t timeout)
{
ASSERT_PORT(port);
return GetRouter().SetTimeout((uint16_t)port, timeout);
}