-
Notifications
You must be signed in to change notification settings - Fork 1
/
net6.go
215 lines (182 loc) · 4.8 KB
/
net6.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
package hexabus
import (
"net"
"regexp"
"time"
)
// Defaults used by the network communication.
const (
// hexabus default port
PORT = "61616"
// package transmit timeout
NET_TIMEOUT = 3
)
// structure to hold all EID's of a hexabus device and its capabilities
type EID struct {
Eid uint32 // Eid
Dtype byte // data type
Desc string // description
Writable bool // writeable
}
func QueryEids(address string, eid_qty uint16) ([]EID, error) {
eid_mask := []uint16{}
eid_descriptors := []uint16{}
eid_map := []EID{}
// find all EID's in eid_qty that are 0 or can be multiplyed by 32
for i := uint16(0); i < eid_qty; i = i + 32 {
eid_descriptors = append(eid_descriptors, uint16(i))
}
// build eid_mask to check what EID's are available
for _, descriptor := range eid_descriptors {
pq := QueryPacket{FLAG_NONE, uint32(descriptor)}
result, err := pq.Send(address)
if err != nil {
return nil, err
}
// change byte order to read LSB first
result = result[len(result)-6 : len(result)-2]
result = []byte{result[3], result[2], result[1], result[0]}
for _, bit := range result {
eid_mask = append(eid_mask, uint16((bit)&1))
eid_mask = append(eid_mask, uint16((bit>>1)&1))
eid_mask = append(eid_mask, uint16((bit>>2)&1))
eid_mask = append(eid_mask, uint16((bit>>3)&1))
eid_mask = append(eid_mask, uint16((bit>>4)&1))
eid_mask = append(eid_mask, uint16((bit>>5)&1))
eid_mask = append(eid_mask, uint16((bit>>6)&1))
eid_mask = append(eid_mask, uint16((bit>>7)&1))
}
}
// query all availabel EID's and build a map of struc EID
for eid, avlb := range eid_mask {
if avlb == 1 {
result, err := EpQueryPacket{0, uint32(eid)}.Send(address)
if err != nil {
return nil, err
}
pei := EpInfoPacket{}
err = pei.Decode(result)
if err != nil {
return nil, err
}
var data interface{}
if pei.Dtype != DTYPE_UINT8 {
data = uint8(1)
} else {
data = uint32(1)
}
// check if endpoint is writable
err = WritePacket{FLAG_NONE, uint32(eid), DTYPE_UNDEFINED, data}.Send(address)
if err == Error(0x02) {
eid_map = append(eid_map, EID{uint32(eid), pei.Dtype, pei.Data.(string), false})
} else if err == Error(0x04) {
eid_map = append(eid_map, EID{uint32(eid), pei.Dtype, pei.Data.(string), true})
} else {
return nil, err
}
}
}
return eid_map, nil
}
func (p QueryPacket) Send(address string) ([]byte, error) {
packet := p.Encode()
// check if port is set otherwhise append default hexabus port
var validPort = regexp.MustCompile(`:[0-9]{1,5}$`)
if !validPort.MatchString(address) {
address += ":" + PORT
}
readbuf := make([]byte, 152)
conn, err := net.DialTimeout("udp6", address, time.Duration(NET_TIMEOUT)*time.Second)
if err != nil {
return nil, err
}
err = conn.SetReadDeadline(time.Now().Add(time.Duration(NET_TIMEOUT * time.Second)))
if err != nil {
return nil, err
}
_, err = conn.Write(packet)
if err != nil {
return nil, err
}
n, err := conn.Read(readbuf)
if err != nil {
return nil, err
}
return readbuf[:n], nil
}
func (p WritePacket) Send(address string) error {
packet, err := p.Encode()
if err != nil {
return err
}
// check if port is set otherwhise append default hexabus port
var validPort = regexp.MustCompile(`:[0-9]{1,5}$`)
if !validPort.MatchString(address) {
address += ":" + PORT
}
readbuf := make([]byte, 152)
conn, err := net.DialTimeout("udp6", address, time.Duration(NET_TIMEOUT)*time.Second)
if err != nil {
return err
}
err = conn.SetReadDeadline(time.Now().Add(time.Duration(NET_TIMEOUT * time.Second)))
if err != nil {
return err
}
_, err = conn.Write(packet)
if err != nil {
return err
}
n, err := conn.Read(readbuf)
if err != nil {
if opErr, ok := err.(net.Error); ok && !opErr.Timeout() {
return err
}
}
if n > 0 {
err = checkCRC(readbuf[:n])
if err != nil {
return err
}
err = checkHeader(readbuf[:n])
if err != nil {
return err
}
ptype, err := PacketType(readbuf[:n])
if err != nil {
return err
}
if ptype == PTYPE_ERROR {
ep := ErrorPacket{}
ep.Decode(readbuf[:n])
return Error(ep.Error)
}
}
return nil
}
func (p EpQueryPacket) Send(address string) ([]byte, error) {
packet := p.Encode()
// check if port is set otherwhise append default hexabus port
var validPort = regexp.MustCompile(`:[0-9]{1,5}$`)
if !validPort.MatchString(address) {
address += ":" + PORT
}
readbuf := make([]byte, 152)
conn, err := net.DialTimeout("udp6", address, time.Duration(NET_TIMEOUT)*time.Second)
if err != nil {
return nil, err
}
err = conn.SetReadDeadline(time.Now().Add(time.Duration(NET_TIMEOUT * time.Second)))
if err != nil {
return nil, err
}
_, err = conn.Write(packet)
if err != nil {
return nil, err
}
n, err := conn.Read(readbuf)
if err != nil {
return nil, err
}
return readbuf[:n], nil
}