-
Notifications
You must be signed in to change notification settings - Fork 388
/
CIPEncapsulationPacket.go
344 lines (294 loc) · 11 KB
/
CIPEncapsulationPacket.go
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package model
import (
"github.com/apache/plc4x/plc4go/internal/plc4go/spi/utils"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
)
// Code generated by code-generation. DO NOT EDIT.
// The data-structure of this message
type CIPEncapsulationPacket struct {
SessionHandle uint32
Status uint32
SenderContext []uint8
Options uint32
Child ICIPEncapsulationPacketChild
}
// The corresponding interface
type ICIPEncapsulationPacket interface {
// CommandType returns CommandType
CommandType() uint16
// GetSessionHandle returns SessionHandle
GetSessionHandle() uint32
// GetStatus returns Status
GetStatus() uint32
// GetSenderContext returns SenderContext
GetSenderContext() []uint8
// GetOptions returns Options
GetOptions() uint32
// GetLengthInBytes returns the length in bytes
GetLengthInBytes() uint16
// GetLengthInBits returns the length in bits
GetLengthInBits() uint16
// Serialize serializes this type
Serialize(writeBuffer utils.WriteBuffer) error
}
type ICIPEncapsulationPacketParent interface {
SerializeParent(writeBuffer utils.WriteBuffer, child ICIPEncapsulationPacket, serializeChildFunction func() error) error
GetTypeName() string
}
type ICIPEncapsulationPacketChild interface {
Serialize(writeBuffer utils.WriteBuffer) error
InitializeParent(parent *CIPEncapsulationPacket, sessionHandle uint32, status uint32, senderContext []uint8, options uint32)
GetTypeName() string
ICIPEncapsulationPacket
}
///////////////////////////////////////////////////////////
// Accessors for property fields.
///////////////////////////////////////////////////////////
func (m *CIPEncapsulationPacket) GetSessionHandle() uint32 {
return m.SessionHandle
}
func (m *CIPEncapsulationPacket) GetStatus() uint32 {
return m.Status
}
func (m *CIPEncapsulationPacket) GetSenderContext() []uint8 {
return m.SenderContext
}
func (m *CIPEncapsulationPacket) GetOptions() uint32 {
return m.Options
}
///////////////////////////////////////////////////////////
// Accessors for virtual fields.
///////////////////////////////////////////////////////////
// NewCIPEncapsulationPacket factory function for CIPEncapsulationPacket
func NewCIPEncapsulationPacket(sessionHandle uint32, status uint32, senderContext []uint8, options uint32) *CIPEncapsulationPacket {
return &CIPEncapsulationPacket{SessionHandle: sessionHandle, Status: status, SenderContext: senderContext, Options: options}
}
func CastCIPEncapsulationPacket(structType interface{}) *CIPEncapsulationPacket {
castFunc := func(typ interface{}) *CIPEncapsulationPacket {
if casted, ok := typ.(CIPEncapsulationPacket); ok {
return &casted
}
if casted, ok := typ.(*CIPEncapsulationPacket); ok {
return casted
}
return nil
}
return castFunc(structType)
}
func (m *CIPEncapsulationPacket) GetTypeName() string {
return "CIPEncapsulationPacket"
}
func (m *CIPEncapsulationPacket) GetLengthInBits() uint16 {
return m.GetLengthInBitsConditional(false)
}
func (m *CIPEncapsulationPacket) GetLengthInBitsConditional(lastItem bool) uint16 {
return m.Child.GetLengthInBits()
}
func (m *CIPEncapsulationPacket) GetParentLengthInBits() uint16 {
lengthInBits := uint16(0)
// Discriminator Field (commandType)
lengthInBits += 16
// Implicit Field (len)
lengthInBits += 16
// Simple field (sessionHandle)
lengthInBits += 32
// Simple field (status)
lengthInBits += 32
// Array field
if len(m.SenderContext) > 0 {
lengthInBits += 8 * uint16(len(m.SenderContext))
}
// Simple field (options)
lengthInBits += 32
// Reserved Field (reserved)
lengthInBits += 32
return lengthInBits
}
func (m *CIPEncapsulationPacket) GetLengthInBytes() uint16 {
return m.GetLengthInBits() / 8
}
func CIPEncapsulationPacketParse(readBuffer utils.ReadBuffer) (*CIPEncapsulationPacket, error) {
if pullErr := readBuffer.PullContext("CIPEncapsulationPacket"); pullErr != nil {
return nil, pullErr
}
// Discriminator Field (commandType) (Used as input to a switch field)
commandType, _commandTypeErr := readBuffer.ReadUint16("commandType", 16)
if _commandTypeErr != nil {
return nil, errors.Wrap(_commandTypeErr, "Error parsing 'commandType' field")
}
// Implicit Field (len) (Used for parsing, but it's value is not stored as it's implicitly given by the objects content)
len, _lenErr := readBuffer.ReadUint16("len", 16)
_ = len
if _lenErr != nil {
return nil, errors.Wrap(_lenErr, "Error parsing 'len' field")
}
// Simple Field (sessionHandle)
_sessionHandle, _sessionHandleErr := readBuffer.ReadUint32("sessionHandle", 32)
if _sessionHandleErr != nil {
return nil, errors.Wrap(_sessionHandleErr, "Error parsing 'sessionHandle' field")
}
sessionHandle := _sessionHandle
// Simple Field (status)
_status, _statusErr := readBuffer.ReadUint32("status", 32)
if _statusErr != nil {
return nil, errors.Wrap(_statusErr, "Error parsing 'status' field")
}
status := _status
// Array field (senderContext)
if pullErr := readBuffer.PullContext("senderContext", utils.WithRenderAsList(true)); pullErr != nil {
return nil, pullErr
}
// Count array
senderContext := make([]uint8, uint16(8))
{
for curItem := uint16(0); curItem < uint16(uint16(8)); curItem++ {
_item, _err := readBuffer.ReadUint8("", 8)
if _err != nil {
return nil, errors.Wrap(_err, "Error parsing 'senderContext' field")
}
senderContext[curItem] = _item
}
}
if closeErr := readBuffer.CloseContext("senderContext", utils.WithRenderAsList(true)); closeErr != nil {
return nil, closeErr
}
// Simple Field (options)
_options, _optionsErr := readBuffer.ReadUint32("options", 32)
if _optionsErr != nil {
return nil, errors.Wrap(_optionsErr, "Error parsing 'options' field")
}
options := _options
// Reserved Field (Compartmentalized so the "reserved" variable can't leak)
{
reserved, _err := readBuffer.ReadUint32("reserved", 32)
if _err != nil {
return nil, errors.Wrap(_err, "Error parsing 'reserved' field")
}
if reserved != uint32(0x00000000) {
log.Info().Fields(map[string]interface{}{
"expected value": uint32(0x00000000),
"got value": reserved,
}).Msg("Got unexpected response.")
}
}
// Switch Field (Depending on the discriminator values, passes the instantiation to a sub-type)
var _parent *CIPEncapsulationPacket
var typeSwitchError error
switch {
case commandType == 0x0101: // CIPEncapsulationConnectionRequest
_parent, typeSwitchError = CIPEncapsulationConnectionRequestParse(readBuffer)
case commandType == 0x0201: // CIPEncapsulationConnectionResponse
_parent, typeSwitchError = CIPEncapsulationConnectionResponseParse(readBuffer)
case commandType == 0x0107: // CIPEncapsulationReadRequest
_parent, typeSwitchError = CIPEncapsulationReadRequestParse(readBuffer)
case commandType == 0x0207: // CIPEncapsulationReadResponse
_parent, typeSwitchError = CIPEncapsulationReadResponseParse(readBuffer, len)
default:
// TODO: return actual type
typeSwitchError = errors.New("Unmapped type")
}
if typeSwitchError != nil {
return nil, errors.Wrap(typeSwitchError, "Error parsing sub-type for type-switch.")
}
if closeErr := readBuffer.CloseContext("CIPEncapsulationPacket"); closeErr != nil {
return nil, closeErr
}
// Finish initializing
_parent.Child.InitializeParent(_parent, sessionHandle, status, senderContext, options)
return _parent, nil
}
func (m *CIPEncapsulationPacket) Serialize(writeBuffer utils.WriteBuffer) error {
return m.Child.Serialize(writeBuffer)
}
func (m *CIPEncapsulationPacket) SerializeParent(writeBuffer utils.WriteBuffer, child ICIPEncapsulationPacket, serializeChildFunction func() error) error {
if pushErr := writeBuffer.PushContext("CIPEncapsulationPacket"); pushErr != nil {
return pushErr
}
// Discriminator Field (commandType) (Used as input to a switch field)
commandType := uint16(child.CommandType())
_commandTypeErr := writeBuffer.WriteUint16("commandType", 16, (commandType))
if _commandTypeErr != nil {
return errors.Wrap(_commandTypeErr, "Error serializing 'commandType' field")
}
// Implicit Field (len) (Used for parsing, but it's value is not stored as it's implicitly given by the objects content)
len := uint16(uint16(uint16(m.GetLengthInBytes())) - uint16(uint16(28)))
_lenErr := writeBuffer.WriteUint16("len", 16, (len))
if _lenErr != nil {
return errors.Wrap(_lenErr, "Error serializing 'len' field")
}
// Simple Field (sessionHandle)
sessionHandle := uint32(m.SessionHandle)
_sessionHandleErr := writeBuffer.WriteUint32("sessionHandle", 32, (sessionHandle))
if _sessionHandleErr != nil {
return errors.Wrap(_sessionHandleErr, "Error serializing 'sessionHandle' field")
}
// Simple Field (status)
status := uint32(m.Status)
_statusErr := writeBuffer.WriteUint32("status", 32, (status))
if _statusErr != nil {
return errors.Wrap(_statusErr, "Error serializing 'status' field")
}
// Array Field (senderContext)
if m.SenderContext != nil {
if pushErr := writeBuffer.PushContext("senderContext", utils.WithRenderAsList(true)); pushErr != nil {
return pushErr
}
for _, _element := range m.SenderContext {
_elementErr := writeBuffer.WriteUint8("", 8, _element)
if _elementErr != nil {
return errors.Wrap(_elementErr, "Error serializing 'senderContext' field")
}
}
if popErr := writeBuffer.PopContext("senderContext", utils.WithRenderAsList(true)); popErr != nil {
return popErr
}
}
// Simple Field (options)
options := uint32(m.Options)
_optionsErr := writeBuffer.WriteUint32("options", 32, (options))
if _optionsErr != nil {
return errors.Wrap(_optionsErr, "Error serializing 'options' field")
}
// Reserved Field (reserved)
{
_err := writeBuffer.WriteUint32("reserved", 32, uint32(0x00000000))
if _err != nil {
return errors.Wrap(_err, "Error serializing 'reserved' field")
}
}
// Switch field (Depending on the discriminator values, passes the serialization to a sub-type)
if _typeSwitchErr := serializeChildFunction(); _typeSwitchErr != nil {
return errors.Wrap(_typeSwitchErr, "Error serializing sub-type field")
}
if popErr := writeBuffer.PopContext("CIPEncapsulationPacket"); popErr != nil {
return popErr
}
return nil
}
func (m *CIPEncapsulationPacket) String() string {
if m == nil {
return "<nil>"
}
buffer := utils.NewBoxedWriteBufferWithOptions(true, true)
m.Serialize(buffer)
return buffer.GetBox().String()
}