-
Notifications
You must be signed in to change notification settings - Fork 12
/
pack.go
485 lines (431 loc) · 12.7 KB
/
pack.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
package gologix
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"reflect"
"github.com/npat-efault/crc16"
)
type cipPack struct {
}
type Packable interface {
Pack(w io.Writer) (int, error)
}
type Unpackable interface {
Unpack(r io.Reader) (n int, err error)
}
func (p cipPack) Align(t reflect.Type) int {
// If a type is a struct we need to check the alignment of every field.
// If any of the fields have an alignment of 8 (LINT, LREAL, etc...)
// then the struct also has an alignment of 8.
if t.Kind() != reflect.Struct {
return t.Align()
}
if t.Kind() == reflect.Array {
return p.Align(t.Elem())
}
a := 1
for i := 0; i < t.NumField(); i++ {
subfield_align := p.Align(t.Field(i).Type)
if subfield_align > a {
a = subfield_align
}
}
return a
}
func (p cipPack) Order() binary.ByteOrder {
return binary.LittleEndian
}
type Serializable interface {
Bytes() []byte
Len() int
}
// given a list of structures, serialize them with the Bytes() method if available,
// otherwise serialize with binary.write()
func Serialize(strs ...any) (*bytes.Buffer, error) {
b := new(bytes.Buffer)
for _, str := range strs {
switch serializable_str := str.(type) {
case string:
strlen := uint32(len(serializable_str))
err := binary.Write(b, binary.LittleEndian, strlen)
if err != nil {
return nil, fmt.Errorf("problem writing string header: %w", err)
}
if strlen%2 == 1 {
strlen++
}
b2 := make([]byte, 84)
copy(b2, serializable_str)
err = binary.Write(b, binary.LittleEndian, b2)
if err != nil {
return nil, fmt.Errorf("problem writing string payload: %w", err)
}
case Serializable:
// if the struct is serializable, we should use its Bytes() function to get its
// representation instead of binary.Write
_, err := b.Write(serializable_str.Bytes())
if err != nil {
return nil, err
}
case any:
err := binary.Write(b, binary.LittleEndian, str)
if err != nil {
return nil, fmt.Errorf("problem writing str to buffer. %w", err)
}
}
}
return b, nil
}
// serialize data into w appropriately for CIP messaging
// obeys alignment and padding rules
func Pack(w io.Writer, data any) (int, error) {
p := cipPack{}
switch d := data.(type) {
case Packable:
return d.Pack(w)
case Serializable:
n, err := w.Write(d.Bytes())
if err != nil {
return 0, nil
}
return n, nil
}
// keep track of how many bytes we've written. This is so we can correct field alignment with padding bytes if needed
pos := 0
// bitpos and bitpack are for packing bits into bytes. bitpos is the position in the byte and bitpack is the packed bits that
// haven't been written to w yet.
bitpos := 0
bitpack := byte(0)
// start reflecting and loop through the fields of the struct
refType := reflect.TypeOf(data)
refVal := reflect.ValueOf(data)
for i := 0; i < refType.NumField(); i++ {
field := refType.Field(i)
a := p.Align(field.Type)
t := field.Tag.Get("pack")
s := int(field.Type.Size())
k := refVal.Field(i).Kind()
// if there isn't a nopack tag on the field, we need to check for bools that need combined into bytes
if t != "nopack" {
// there are two conditions where we pack bits. Multiple bools in series or a bool array.
switch k {
case reflect.Array:
// we have an array without "nopack". Check if it is a bool array
arr := refVal.Field(i)
at := arr.Type().Elem()
if at.Kind() == reflect.Bool {
l := arr.Len()
for ai := 0; ai < l; ai++ {
bval := arr.Index(ai).Bool()
ival := byte(0)
if bval {
ival = 1
}
bitpack = bitpack | (ival << bitpos)
bitpos++
// when we have a full byte, flush it.
if bitpos >= 8 {
_, err := w.Write([]byte{bitpack})
if err != nil {
//TODO: make this function return error?
return pos, fmt.Errorf("problem writing bitpack to buffer. %v", err)
}
bitpos = 0
bitpack = 0
pos += 1
}
}
continue
}
case reflect.Bool:
// try to pack bools
bval := refVal.Field(i).Bool()
ival := byte(0)
if bval {
ival = 1
}
bitpack = bitpack | (ival << bitpos)
bitpos++
// when we have a full byte, flush it.
if bitpos >= 8 {
_, err := w.Write([]byte{bitpack})
if err != nil {
return pos, fmt.Errorf("problem writing bitpacked byte. %v", err)
}
bitpos = 0
bitpack = 0
pos += 1
}
continue
}
}
// we don't have a packable bool. First thing we need to do is check whether there are some packed bools that still need flushed out.
if bitpos > 0 {
// we have at least one bit that needs flushed.
_, err := w.Write([]byte{bitpack})
if err != nil {
return pos, fmt.Errorf("problem writing bitpacked byte. %v", err)
}
bitpos = 0
bitpack = 0
pos += 1
}
// make sure we are writing the new data for this field to the properly aligned byte
rem := a - (pos % a)
if rem < a && rem > 0 {
// need paddding bits
pad := make([]byte, rem)
_, err := w.Write(pad)
if err != nil {
return pos, fmt.Errorf("problem writing pad to buffer. %v", err)
}
pos += rem
}
// finally, if the field is some sub-structure, recurse. Otherwise we will write the data out
if k != reflect.Struct {
err := binary.Write(w, p.Order(), refVal.Field(i).Interface())
if err != nil {
return pos, fmt.Errorf("problem reading sub-structure. %v", err)
}
} else {
var err error
s, err = Pack(w, refVal.Field(i).Interface())
if err != nil {
return pos, fmt.Errorf("problem packing interface: %w", err)
}
}
pos += s
}
// Last thing we need to do is check whether there are some packed bools that still need flushed out.
if bitpos > 0 {
// we have at least one bit that needs flushed.
_, err := w.Write([]byte{bitpack})
if err != nil {
return pos, fmt.Errorf("problem flushing bitpack, %v", err)
}
pos += 1
}
return pos, nil
}
// deserialize data from r appropriately for CIP messaging
// obeys alignment and padding rules
func Unpack(r io.Reader, data any) (n int, err error) {
p := cipPack{}
switch d := data.(type) {
case Unpackable:
return d.Unpack(r)
}
// bitpos and bitpack are for packing bits into bytes. bitpos is the position in the byte and bitpack is the packed bits that
// haven't been written to w yet.
bitpos := 0
bitpack := byte(0)
// start reflecting and loop through the fields of the struct
refVal := reflect.ValueOf(data)
if refVal.Kind() == reflect.Ptr {
refVal = reflect.ValueOf(data).Elem()
}
refType := refVal.Type()
for i := 0; i < refType.NumField(); i++ {
field := refType.Field(i)
a := p.Align(field.Type)
t := field.Tag.Get("pack")
s := int(field.Type.Size())
k := refVal.Field(i).Kind()
// if there isn't a nopack tag on the field, we need to check for bools that need combined into bytes
if t != "nopack" {
// there are two conditions where we pack bits. Multiple bools in series or a bool array.
switch k {
case reflect.Array:
// we have an array without "nopack". Check if it is a bool array
arr := refVal.Field(i)
at := arr.Type().Elem()
if at.Kind() == reflect.Bool {
l := arr.Len()
for ai := 0; ai < l; ai++ {
if bitpos == 0 {
br := []byte{0}
_, err = r.Read(br)
if err != nil {
return n, fmt.Errorf("problem reading bool. %w", err)
}
bitpack = br[0]
n += 1
}
val := bitpack & (1 << bitpos)
bval := val != 0
arr.Index(ai).SetBool(bval)
bitpos++
// when we have a full byte, flush it.
if bitpos >= 8 {
bitpos = 0
}
}
continue
}
case reflect.Bool:
// try to pack bools
if bitpos == 0 {
br := []byte{0}
_, err = r.Read(br)
if err != nil {
return n, fmt.Errorf("problem reading packed bool. %w", err)
}
bitpack = br[0]
n += 1
}
val := bitpack & (1 << bitpos)
bval := val != 0
refVal.Field(i).SetBool(bval)
bitpos++
// when we have a full byte, flush it.
if bitpos >= 8 {
bitpos = 0
}
continue
}
}
// we don't have a packable bool. First thing we need to do is check whether there are some packed bools that still need flushed out.
if bitpos > 0 {
// we have at least one bit that needs flushed.
bitpos = 0
}
// make sure we are writing the new data for this field to the properly aligned byte
rem := a - (n % a)
if rem < a && rem > 0 {
// need paddding bits
pad := make([]byte, rem)
_, err = r.Read(pad)
if err != nil {
return
}
n += rem
}
// finally, if the field is some sub-structure, recurse. Otherwise we will write the data out
if k != reflect.Struct {
//binary.Read(r, p.Order(), refVal.Field(i).Interface())
err = binary.Read(r, p.Order(), refVal.Field(i).Addr().Interface())
if err != nil {
return
}
} else {
val := refVal.Field(i).Addr().Interface()
s, err = Unpack(r, val)
if err != nil {
return
}
}
n += s
}
// Last thing we need to do is check whether there are some packed bools that still need flushed out.
return
}
func ReadPacked[T any](client *Client, tag string) (T, error) {
var data T
buf := new(bytes.Buffer)
size, err := Pack(buf, data)
if err != nil {
return data, err
}
b := make([]byte, size)
err = client.Read(tag, &b)
if err != nil {
return data, fmt.Errorf("couldn't read %s as bytes. %w", tag, err)
}
_, err = Unpack(bytes.NewBuffer(b), &data)
if err != nil {
return data, fmt.Errorf("problem unpacking from buffer. %w", err)
}
return data, nil
}
// perform type encoding per TypeEncode_CIPRW.pdf from the rockwell site. Also returns the abbreviated type ID
func TypeEncode(data any) (string, uint16, error) {
// TODO: does this whole thing break if we have a struct with bools, what with their ZZZZZZ prefixed values and all?
// I suspect it does. The UDT type definitions won gold at the bad idea olympics.
encoded := ""
// bitpos and bitpack are for packing bits into bytes. bitpos is the position in the byte and bitpack is the packed bits that
// haven't been written to w yet.
bitpos := 0
// start reflecting and loop through the fields of the struct
refType := reflect.TypeOf(data)
refVal := reflect.ValueOf(data)
// start with the structure name
encoded = refType.Name()
for i := 0; i < refType.NumField(); i++ {
field := refType.Field(i)
k := refVal.Field(i).Kind()
// there are two conditions where we pack bits. Multiple bools in series or a bool array.
switch k {
case reflect.Array:
// we have an array without "nopack". Check if it is a bool array
arr := refVal.Field(i)
var at string
if arr.Type().Elem().Kind() != reflect.Struct {
at, _ = goTypeToLogixTypeName(field.Type.Elem())
} else {
at, _, _ = TypeEncode(arr.Index(0).Interface())
}
encoded += fmt.Sprintf(",%s[%d]", at, arr.Len())
continue
case reflect.Bool:
// try to pack bools
if bitpos == 0 {
encoded += ",SINT"
}
bitpos++
// when we have a full byte, flush it.
if bitpos >= 8 {
bitpos = 0
}
continue
}
// we don't have a packable bool. First thing we need to do is check whether there are some packed bools that still need flushed out.
bitpos = 0
// finally, if the field is some sub-structure, recurse. Otherwise we will write the data out
if k != reflect.Struct {
n, _ := goTypeToLogixTypeName(refVal.Field(i).Type())
encoded += fmt.Sprintf(",%s", n)
} else {
str_text, _, err := TypeEncode(refVal.Field(i).Interface())
if err != nil {
return "", 0, fmt.Errorf("problem encoding sub-structure: %w", err)
}
encoded += fmt.Sprintf(",%s", str_text)
}
}
crc := crc16.Checksum(crc_conf, []byte(encoded))
return encoded, crc, nil
}
func goTypeToLogixTypeName(t reflect.Type) (string, error) {
switch t.Name() {
case "int8":
return "SINT", nil
case "int16":
return "INT", nil
case "int32":
return "DINT", nil
case "int64":
return "LINT", nil
case "uint8":
return "SINT", nil
case "uint16":
return "UINT", nil
case "uint32":
return "UDINT", nil
case "uint64":
return "ULINT", nil
case "float32":
return "REAL", nil
case "float64":
return "LREAL", nil
}
return "", nil
}
// this is the confirguration for the CRC16 checksum used in the abbreviated type ID calculation for
// UDT types..
var crc_conf = &crc16.Conf{
Poly: 0x8005,
BitRev: true,
BigEnd: false,
}