-
Notifications
You must be signed in to change notification settings - Fork 2
/
UT0311-L0x.go
387 lines (311 loc) · 9.14 KB
/
UT0311-L0x.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
package UTO311_L0x
import (
"encoding/binary"
"fmt"
"net"
"net/netip"
"reflect"
"regexp"
"strconv"
"strings"
"github.com/uhppoted/uhppote-core/types"
)
type Marshaler interface {
MarshalUT0311L0x() ([]byte, error)
}
type Unmarshaler interface {
UnmarshalUT0311L0x([]byte) (interface{}, error)
}
var (
tBool = reflect.TypeOf(bool(false))
tByte = reflect.TypeOf(byte(0))
tUint16 = reflect.TypeOf(uint16(0))
tUint32 = reflect.TypeOf(uint32(0))
tIPv4 = reflect.TypeOf(net.IPv4(0, 0, 0, 0))
tMAC = reflect.TypeOf(net.HardwareAddr{})
tSOM = reflect.TypeOf(types.SOM(0))
tMsgType = reflect.TypeOf(types.MsgType(0))
tAddrPort = reflect.TypeOf(netip.AddrPort{})
)
var re = regexp.MustCompile(`offset:\s*([0-9]+)`)
var vre = regexp.MustCompile(`value:\s*((?:0[xX])?[0-9a-fA-F]+)`)
func Marshal(m interface{}) ([]byte, error) {
v := reflect.ValueOf(m)
bytes := make([]byte, 64)
bytes[0] = 0x17
if v.Type().Kind() == reflect.Ptr {
if err := marshal(v.Elem(), bytes); err != nil {
return nil, err
}
} else {
if err := marshal(reflect.Indirect(v), bytes); err != nil {
return nil, err
}
}
return bytes, nil
}
func marshal(s reflect.Value, bytes []byte) error {
if s.Kind() == reflect.Struct {
N := s.NumField()
for i := 0; i < N; i++ {
f := s.Field(i)
t := s.Type().Field(i)
if t.Anonymous {
if err := marshal(f, bytes); err != nil {
return err
}
} else {
tag := t.Tag.Get("uhppote")
switch t.Type {
case tSOM:
if value := vre.FindStringSubmatch(tag); value == nil {
bytes[0] = byte(f.Uint())
} else {
if v, err := strconv.ParseUint(value[1], 0, 8); err != nil {
return err
} else {
bytes[0] = byte(v)
}
}
case tMsgType:
if value := vre.FindStringSubmatch(tag); value == nil {
bytes[1] = byte(f.Uint())
} else {
if v, err := strconv.ParseUint(value[1], 0, 8); err != nil {
return err
} else {
bytes[1] = byte(v)
}
}
default:
if matched := re.FindStringSubmatch(tag); matched != nil {
offset, _ := strconv.Atoi(matched[1])
// Marshall with MarshalUT0311L0x{} interface
if m, ok := f.Interface().(Marshaler); ok {
// If f is a pointer type and the value is nil skips this field, leaving the buffer 'as is'
if f.Kind() != reflect.Ptr || !f.IsNil() {
if b, err := m.MarshalUT0311L0x(); err == nil {
copy(bytes[offset:offset+len(b)], b)
}
}
continue
}
// Marshal built-in types
switch t.Type {
case tByte:
value := vre.FindStringSubmatch(tag)
if value != nil {
v, err := strconv.ParseUint(value[1], 16, 8)
if err != nil {
return err
}
bytes[offset] = byte(v)
} else {
bytes[offset] = byte(f.Uint())
}
case tUint16:
binary.LittleEndian.PutUint16(bytes[offset:offset+4], uint16(f.Uint()))
case tUint32:
binary.LittleEndian.PutUint32(bytes[offset:offset+4], uint32(f.Uint()))
case tBool:
if f.Bool() {
bytes[offset] = 0x01
} else {
bytes[offset] = 0x00
}
case tIPv4:
copy(bytes[offset:offset+4], f.MethodByName("To4").Call([]reflect.Value{})[0].Bytes())
case tAddrPort:
v := f.MethodByName("MarshalBinary").Call([]reflect.Value{})
if !v[1].IsNil() {
return v[1].Interface().(error)
} else if slice := v[0].Bytes(); len(slice) != 6 {
return fmt.Errorf("invalid IPv4 address:port %v", slice)
} else {
copy(bytes[offset:offset+6], slice)
}
case tMAC:
copy(bytes[offset:offset+6], f.Bytes())
default:
panic(fmt.Errorf("cannot marshal field with type '%v'", t.Type))
}
}
}
}
}
}
return nil
}
func Unmarshal(bytes []byte, m interface{}) error {
v := reflect.ValueOf(m)
if v.Kind() == reflect.Ptr && v.Elem().Kind() == reflect.Struct {
return unmarshal(bytes, v.Elem())
}
return fmt.Errorf("cannot unmarshal value with kind '%s'", v.Type())
}
func UnmarshalAs(bytes []byte, m interface{}) (interface{}, error) {
v := reflect.ValueOf(m)
if v.Kind() != reflect.Ptr && v.Kind() == reflect.Struct {
s := reflect.New(v.Type()).Elem()
if err := unmarshal(bytes, s); err != nil {
return nil, err
}
return s.Interface(), nil
}
if v.Kind() == reflect.Ptr && v.Elem().Kind() == reflect.Struct {
s := reflect.New(v.Elem().Type()).Elem()
if err := unmarshal(bytes, s); err != nil {
return nil, err
}
return s.Interface(), nil
}
return nil, fmt.Errorf("cannot unmarshal value with kind '%s'", v.Type())
}
func UnmarshalArray(bytes [][]byte, array interface{}) error {
v := reflect.ValueOf(array)
if v.Kind() == reflect.Ptr && v.Elem().Kind() == reflect.Slice {
t := v.Elem().Type().Elem()
vv := reflect.MakeSlice(reflect.SliceOf(t), 0, 0)
for _, b := range bytes {
s := reflect.New(t).Elem()
if err := unmarshal(b, s); err != nil {
return err
}
vv = reflect.Append(vv, s)
}
v.Elem().Set(vv)
return nil
}
return fmt.Errorf("cannot unmarshal array to value with kind '%s'", v.Type())
}
func UnmarshalArrayElement(bytes []byte, array interface{}) (interface{}, error) {
v := reflect.ValueOf(array)
if v.Kind() == reflect.Ptr && v.Elem().Kind() == reflect.Slice {
t := v.Elem().Type().Elem()
s := reflect.New(t).Elem()
if err := unmarshal(bytes, s); err != nil {
return nil, err
}
return s.Interface(), nil
}
return nil, fmt.Errorf("cannot unmarshal array to value with kind '%s'", v.Type())
}
func unmarshal(bytes []byte, s reflect.Value) error {
// Validate message format
if len(bytes) != 64 {
return fmt.Errorf("invalid message length - expected 64 bytes, received %v", len(bytes))
}
// PATCH: v6.62 firmware sends 'listen' events with SOM 0x19
if (bytes[0] != 0x17) && (bytes[0] != 0x19 || bytes[1] != 0x20) {
return fmt.Errorf("invalid start of message - expected 0x17, received 0x%02x", bytes[0])
}
// Unmarshal fields tagged with `uhppote:"..."`
if s.Kind() == reflect.Struct {
N := s.NumField()
for i := 0; i < N; i++ {
f := s.Field(i)
t := s.Type().Field(i)
tag := t.Tag.Get("uhppote")
if !f.CanSet() {
continue
}
switch {
case t.Anonymous: // embedded structs
unmarshal(bytes, f)
case t.Type == tMsgType: // validate MsgType field
b := byte(f.Uint())
if value := vre.FindStringSubmatch(tag); value != nil {
if v, err := strconv.ParseUint(value[1], 0, 8); err != nil {
return err
} else {
b = byte(v)
}
}
if bytes[1] != b {
return fmt.Errorf("invalid MsgType in message - expected %02X, received %02X", b, bytes[1])
}
f.SetUint(uint64(bytes[1]))
default:
// Unmarshall fields tagged with `uhppote:"offset:<offset>"`
matched := re.FindStringSubmatch(tag)
if matched == nil {
continue
}
offset, _ := strconv.Atoi(matched[1])
// Unmarshall value fields with UnmarshalUT0311L0x{} interface
if u, ok := f.Addr().Interface().(Unmarshaler); ok {
if p, err := u.UnmarshalUT0311L0x(bytes[offset:]); err != nil {
return err
} else {
f.Set(reflect.Indirect(reflect.ValueOf(p)))
}
continue
}
// Unmarshall pointer fields with UnmarshalUT0311L0x{} interface
if u, ok := f.Interface().(Unmarshaler); ok {
if p, err := u.UnmarshalUT0311L0x(bytes[offset:]); err == nil && p != nil {
f.Set(reflect.ValueOf(p))
}
continue
}
// Unmarshal built-in types
switch t.Type {
case tBool:
if bytes[offset] == 0x01 {
f.SetBool(true)
} else if bytes[offset] == 0x00 {
f.SetBool(false)
} else {
return fmt.Errorf("invalid boolean value in message: %02x", bytes[offset])
}
case tByte:
value := vre.FindStringSubmatch(tag)
if value != nil {
v, err := strconv.ParseUint(value[1], 16, 8)
if err != nil {
return err
}
if bytes[offset] != byte(v) {
return fmt.Errorf("invalid value in message - expected %02x, received 0x%02x", v, bytes[offset])
}
}
f.SetUint(uint64(bytes[offset]))
case tUint16:
f.SetUint(uint64(binary.LittleEndian.Uint16(bytes[offset : offset+2])))
case tUint32:
f.SetUint(uint64(binary.LittleEndian.Uint32(bytes[offset : offset+4])))
case tIPv4:
f.SetBytes(net.IPv4(bytes[offset], bytes[offset+1], bytes[offset+2], bytes[offset+3]))
case tAddrPort:
var v netip.AddrPort
if err := v.UnmarshalBinary(bytes[offset : offset+6]); err != nil {
return err
} else {
f.Set(reflect.ValueOf(v))
}
case tMAC:
f.SetBytes(bytes[offset : offset+6])
default:
panic(fmt.Errorf("cannot unmarshal field with type '%v'", t.Type))
}
}
}
}
return nil
}
func Dump(m []byte, prefix string) string {
var b strings.Builder
for ix := 0; ix < len(m); ix += 16 {
chunk := m[ix:]
fmt.Fprintf(&b, "%s%08x ", prefix, ix)
for i := 0; i < 8 && i < len(chunk); i++ {
fmt.Fprintf(&b, " %02x", chunk[i])
}
fmt.Fprintf(&b, " ")
for i := 8; i < 16 && i < len(chunk); i++ {
fmt.Fprintf(&b, " %02x", chunk[i])
}
fmt.Fprintln(&b)
}
return b.String()
}