AdsLib
Loading...
Searching...
No Matches
AmsHeader.h
1// SPDX-License-Identifier: MIT
6#pragma once
7
8#define UNUSED(x) (void)(x)
9
10#include "AdsDef.h"
11#include "wrap_endian.h"
12
13#include <array>
14#include <cstddef>
15#include <cstring>
16
17#pragma pack (push, 1)
19 AmsTcpHeader(const uint8_t* frame)
20 {
21 memcpy(this, frame, sizeof(*this));
22 }
23
24 AmsTcpHeader(const uint32_t numBytes = 0)
25 : reserved(0),
26 leLength(bhf::ads::htole(numBytes))
27 {
28 UNUSED(reserved);
29 }
30
31 uint32_t length() const
32 {
33 return bhf::ads::letoh(leLength);
34 }
35private:
36 uint16_t reserved;
37 uint32_t leLength;
38};
39
41 static const uint32_t SDO_UPLOAD = 0xF302;
42
43 AoERequestHeader(uint16_t sdoIndex, uint8_t sdoSubIndex, uint32_t dataLength)
44 : AoERequestHeader(SDO_UPLOAD, ((uint32_t)sdoIndex) << 16 | sdoSubIndex, dataLength)
45 {}
46
47 AoERequestHeader(uint32_t indexGroup, uint32_t indexOffset, uint32_t dataLength)
48 : leGroup(bhf::ads::htole(indexGroup)),
49 leOffset(bhf::ads::htole(indexOffset)),
50 leLength(bhf::ads::htole(dataLength))
51 {}
52
53private:
54 const uint32_t leGroup;
55 const uint32_t leOffset;
56 const uint32_t leLength;
57};
58
60 AoEReadWriteReqHeader(uint32_t indexGroup, uint32_t indexOffset, uint32_t readLength, uint32_t writeLength)
61 : AoERequestHeader(indexGroup, indexOffset, readLength),
62 leWriteLength(bhf::ads::htole(writeLength))
63 {}
64private:
65 const uint32_t leWriteLength;
66};
67
69 AdsWriteCtrlRequest(uint16_t ads, uint16_t dev, uint32_t dataLength)
70 : leAdsState(bhf::ads::htole(ads)),
71 leDevState(bhf::ads::htole(dev)),
72 leLength(bhf::ads::htole(dataLength))
73 {}
74
75private:
76 const uint16_t leAdsState;
77 const uint16_t leDevState;
78 const uint32_t leLength;
79};
80
82 AdsAddDeviceNotificationRequest(uint32_t __group,
83 uint32_t __offset,
84 uint32_t __length,
85 uint32_t __mode,
86 uint32_t __maxDelay,
87 uint32_t __cycleTime)
88 : leGroup(bhf::ads::htole(__group)),
89 leOffset(bhf::ads::htole(__offset)),
90 leLength(bhf::ads::htole(__length)),
91 leMode(bhf::ads::htole(__mode)),
92 leMaxDelay(bhf::ads::htole(__maxDelay)),
93 leCycleTime(bhf::ads::htole(__cycleTime)),
94 reserved()
95 {
96 UNUSED(reserved);
97 }
98
99private:
100 const uint32_t leGroup;
101 const uint32_t leOffset;
102 const uint32_t leLength;
103 const uint32_t leMode;
104 const uint32_t leMaxDelay;
105 const uint32_t leCycleTime;
106 const std::array<uint8_t, 16> reserved;
107};
108
109struct AoEHeader {
110 static const uint16_t AMS_REQUEST = 0x0004;
111 static const uint16_t AMS_RESPONSE = 0x0005;
112 static const uint16_t AMS_UDP = 0x0040;
113 static const uint16_t INVALID = 0x0000;
114 static const uint16_t READ_DEVICE_INFO = 0x0001;
115 static const uint16_t READ = 0x0002;
116 static const uint16_t WRITE = 0x0003;
117 static const uint16_t READ_STATE = 0x0004;
118 static const uint16_t WRITE_CONTROL = 0x0005;
119 static const uint16_t ADD_DEVICE_NOTIFICATION = 0x0006;
120 static const uint16_t DEL_DEVICE_NOTIFICATION = 0x0007;
121 static const uint16_t DEVICE_NOTIFICATION = 0x0008;
122 static const uint16_t READ_WRITE = 0x0009;
123
124 AoEHeader()
125 : leTargetPort(0),
126 leSourcePort(0),
127 leCmdId(0),
128 leStateFlags(0),
129 leLength(0),
130 leErrorCode(0),
131 leInvokeId(0)
132 {}
133
134 AoEHeader(const AmsNetId& __targetAddr,
135 uint16_t __targetPort,
136 const AmsNetId& __sourceAddr,
137 uint16_t __sourcePort,
138 uint16_t __cmdId,
139 uint32_t __length,
140 uint32_t __invokeId)
141 : targetNetId(__targetAddr),
142 leTargetPort(bhf::ads::htole(__targetPort)),
143 sourceNetId(__sourceAddr),
144 leSourcePort(bhf::ads::htole(__sourcePort)),
145 leCmdId(bhf::ads::htole(__cmdId)),
146 leStateFlags(bhf::ads::htole(AMS_REQUEST)),
147 leLength(bhf::ads::htole(__length)),
148 leErrorCode(0),
149 leInvokeId(bhf::ads::htole(__invokeId))
150 {}
151
152 AoEHeader(const uint8_t* frame)
153 {
154 memcpy(this, frame, sizeof(*this));
155 }
156
157 uint16_t cmdId() const
158 {
159 return bhf::ads::letoh(leCmdId);
160 }
161
162 uint32_t errorCode() const
163 {
164 return bhf::ads::letoh(leErrorCode);
165 }
166
167 uint32_t invokeId() const
168 {
169 return bhf::ads::letoh(leInvokeId);
170 }
171
172 uint32_t length() const
173 {
174 return bhf::ads::letoh(leLength);
175 }
176
177 AmsAddr sourceAms() const
178 {
179 return AmsAddr { sourceAddr(), sourcePort() };
180 }
181
182 AmsNetId sourceAddr() const
183 {
184 return sourceNetId;
185 }
186
187 uint16_t sourcePort() const
188 {
189 return bhf::ads::letoh(leSourcePort);
190 }
191
192 uint16_t stateFlags() const
193 {
194 return bhf::ads::letoh(leStateFlags);
195 }
196
197 AmsNetId targetAddr() const
198 {
199 return targetNetId;
200 }
201
202 uint16_t targetPort() const
203 {
204 return bhf::ads::letoh(leTargetPort);
205 }
206
207private:
208 AmsNetId targetNetId;
209 uint16_t leTargetPort;
210 AmsNetId sourceNetId;
211 uint16_t leSourcePort;
212 uint16_t leCmdId;
213 uint16_t leStateFlags;
214 uint32_t leLength;
215 uint32_t leErrorCode;
216 uint32_t leInvokeId;
217};
218
221 : leResult(0)
222 {}
223
224 AoEResponseHeader(const uint8_t* frame)
225 {
226 memcpy(this, frame, sizeof(*this));
227 }
228
229 uint32_t result() const
230 {
231 return bhf::ads::letoh(leResult);
232 }
233private:
234 uint32_t leResult;
235};
236
239 : leReadLength(0)
240 {}
241
242 AoEReadResponseHeader(const uint8_t* frame)
243 {
244 memcpy(this, frame, sizeof(*this));
245 }
246
247 uint32_t readLength() const
248 {
249 return bhf::ads::letoh(leReadLength);
250 }
251private:
252 uint32_t leReadLength;
253};
254#pragma pack (pop)
Definition: AmsHeader.h:81
Definition: AmsHeader.h:68
The complete address of an ADS device can be stored in this structure.
Definition: AdsDef.h:237
The NetId of and ADS device can be represented in this structure.
Definition: AdsDef.h:224
Definition: AmsHeader.h:18
Definition: AmsHeader.h:109
Definition: AmsHeader.h:237
Definition: AmsHeader.h:59
Definition: AmsHeader.h:40
Definition: AmsHeader.h:219