-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathlistidentity.go
115 lines (98 loc) · 3.29 KB
/
listidentity.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
package gologix
import (
"bytes"
"encoding/binary"
"fmt"
)
func (client *Client) ListIdentity() (*listIdentityResponeBody, error) {
client.Logger.Debug("listing identity")
_, data, err := client.send_recv_data(cipCommandListIdentity)
if err != nil {
return nil, err
}
items, err := readItems(data)
if err != nil {
return nil, fmt.Errorf("couldn't parse items. %w", err)
}
if len(items) != 1 {
return nil, fmt.Errorf("expected 1 item, got %d", len(items))
}
// The response only contains one item.
response := listIdentityResponeBody{}
err = response.ParseFromBytes(items[0].Data)
if err != nil {
return nil, fmt.Errorf("problem reading list identity response. %w", err)
}
return &response, nil
}
type listIdentityResponeBody struct {
EncapProtocolVersion uint16
SocketAddress listIdentitySocketAddress
Vendor VendorId
DeviceType DeviceType
ProductCode uint16
Revision uint16
Status uint16
SerialNumber uint32
ProductName string
State uint8
}
type listIdentitySocketAddress struct {
Family uint16
Port uint16
Address uint32
Zero0 uint32
Zero1 uint32
}
func (s *listIdentityResponeBody) ParseFromBytes(data []byte) error {
buf := bytes.NewBuffer(data)
err := binary.Read(buf, binary.LittleEndian, &s.EncapProtocolVersion)
if err != nil {
return fmt.Errorf("problem reading list identity response. field EncapProtocolVersion %w", err)
}
// For some reason this field in particular is big endian.
err = binary.Read(buf, binary.BigEndian, &s.SocketAddress)
if err != nil {
return fmt.Errorf("problem reading list identity response. field SocketAddress %w", err)
}
err = binary.Read(buf, binary.LittleEndian, &s.Vendor)
if err != nil {
return fmt.Errorf("problem reading list identity response. field Vendor %w", err)
}
err = binary.Read(buf, binary.LittleEndian, &s.DeviceType)
if err != nil {
return fmt.Errorf("problem reading list identity response. field DeviceType %w", err)
}
err = binary.Read(buf, binary.LittleEndian, &s.ProductCode)
if err != nil {
return fmt.Errorf("problem reading list identity response. field ProductCode %w", err)
}
err = binary.Read(buf, binary.LittleEndian, &s.Revision)
if err != nil {
return fmt.Errorf("problem reading list identity response. field Revision %w", err)
}
err = binary.Read(buf, binary.LittleEndian, &s.Status)
if err != nil {
return fmt.Errorf("problem reading list identity response. field Status %w", err)
}
err = binary.Read(buf, binary.LittleEndian, &s.SerialNumber)
if err != nil {
return fmt.Errorf("problem reading list identity response. field SerialNumber %w", err)
}
productNameLength := uint8(0)
err = binary.Read(buf, binary.LittleEndian, &productNameLength)
if err != nil {
return fmt.Errorf("problem reading list identity response. field ProductNameLength %w", err)
}
productName := make([]byte, productNameLength)
err = binary.Read(buf, binary.LittleEndian, &productName)
if err != nil {
return fmt.Errorf("problem reading list identity response. field ProductName %w", err)
}
s.ProductName = string(productName)
err = binary.Read(buf, binary.LittleEndian, &s.State)
if err != nil {
return fmt.Errorf("problem reading list identity response. field State %w", err)
}
return nil
}