-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathads.go
318 lines (270 loc) · 8.21 KB
/
ads.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
package goADS
import (
"fmt"
"encoding/binary"
"errors"
"bytes"
"strconv"
// "encoding/hex"
)
type ADSNotification struct {
handle uint32
}
// ReadDeviceInfo - ADS command id: 1/*{{{*/
type ADSReadDeviceInfo struct {
MajorVersion uint8
MinorVersion uint8
BuildVersion uint16
DeviceName string
}
func (conn *Connection) ReadDeviceInfo() (response ADSReadDeviceInfo, err error) {/*{{{*/
var resp []byte
// Try to send the request
resp,err = conn.sendRequest(1,[]byte{})
if err!=nil {
return
}
// Check the response length
if len(resp)!=24 {
return response, errors.New(fmt.Sprintf("Wrong length of response! Got %d bytes and it should be 24",len(resp)) )
}
// Check the result error code
result := binary.LittleEndian.Uint32(resp[0:4])
if result>0 {
err = errors.New("Got ADS error number "+strconv.FormatUint(uint64(result),10)+" in ReadDeviceInfo")
return
}
// Parse the response
response.MajorVersion = resp[4]
response.MinorVersion = resp[5]
response.BuildVersion = binary.LittleEndian.Uint16(resp[6:8])
response.DeviceName = string(resp[8:24])
logger.Debugf("response.majorVersion=%d",response.MajorVersion);
logger.Debugf("response.minorVersion=%d",response.MinorVersion);
logger.Debugf("response.buildVersion=%d",response.BuildVersion);
logger.Debugf("response.deviceName=%s",response.DeviceName);
return
}/*}}}*/
/*}}}*/
// Read - ADS command id: 2/*{{{*/
type ADSRead struct {
Data []byte
}
func (conn *Connection) Read(group uint32,offset uint32,length uint32) (response ADSRead, err error) {/*{{{*/
request := new(bytes.Buffer)
var content = []interface{}{
group,
offset,
length,
}
for _, v := range content {
err := binary.Write(request, binary.LittleEndian, v)
if err != nil {
logger.Errorf("binary.Write failed: %s", err)
}
}
// Try to send the request
resp,err := conn.sendRequest(2,request.Bytes())
if err!=nil {
return
}
// Check the result error code
result := binary.LittleEndian.Uint32(resp[0:4])
datalength := binary.LittleEndian.Uint32(resp[4:8])
if result>0 {
err = errors.New("Got ADS error number "+strconv.FormatUint(uint64(result),10)+" in Read")
return
}
if len(resp)-8!=int(datalength) {
return response, errors.New(fmt.Sprintf("Wrong length of response! Got %d bytes and it should be %d",len(resp),datalength) )
}
response.Data = resp[8:datalength+8]
//logger.Debugf("The read data at %d:%d: \r\n%s",group,offset,hex.Dump(response.Data))
return
}/*}}}*/
/*}}}*/
// Write - ADS command id: 3/*{{{*/
func (conn *Connection) Write(group uint32,offset uint32,data []byte) {
if conn==nil {
logger.Error("Failed to Write, connection is nil pointer");
return
}
request := new(bytes.Buffer)
length := uint32(len(data))
var content = []interface{}{
group,
offset,
length,
}
for _, v := range content {
err := binary.Write(request, binary.LittleEndian, v)
if err != nil {
logger.Errorf("binary.Write failed: %s", err)
}
}
_,err := request.Write(data)
if err != nil {
logger.Errorf("bytes.Write failed: %s", err)
}
// Try to send the request
resp,err := conn.sendRequest(3,request.Bytes())
if err!=nil {
return
}
// Check the result error code
result := binary.LittleEndian.Uint32(resp[0:4])
if result>0 {
err = errors.New("Got ADS error number "+strconv.FormatUint(uint64(result),10)+" in Write")
return
}
return
}
/*}}}*/
// ReadState - ADS command id: 4/*{{{*/
type ADSReadState struct {
ADSState uint16
ADSDeviceState uint16
}
func (conn *Connection) ReadState() (response ADSReadState, err error) {/*{{{*/
var resp []byte
// Try to send the request
resp,err = conn.sendRequest(4,[]byte{})
if err!=nil {
return
}
// Check the response length
if len(resp)!=24 {
return response, errors.New(fmt.Sprintf("Wrong length of response! Got %d bytes and it should be 24",len(resp)) )
}
// Check the result error code
result := binary.LittleEndian.Uint32(resp[0:4])
if result>0 {
err = errors.New("Got ADS error number "+strconv.FormatUint(uint64(result),10)+" in ReadState")
return
}
// Parse the response
response.ADSState = binary.LittleEndian.Uint16(resp[4:6])
response.ADSDeviceState = binary.LittleEndian.Uint16(resp[6:8])
logger.Debugf("response.ADSState=%d",response.ADSState);
logger.Debugf("response.ADSDeviceState=%d",response.ADSDeviceState);
return
}/*}}}*/
/*}}}*/
// WriteControl TODO - ADS command id: 5/*{{{*/
func (conn *Connection) WriteControl() {
}
/*}}}*/
// AddDeviceNotification - ADS command id: 6/*{{{*/
const (
ADS_NoTransmission = 0
ADS_ClientCycle = 1
ADS_ClientOnChange = 2
ADS_ServerCycle = 3
ADS_ServerOnChange = 4
ADS_Client1Reqest = 5
)
type ADSAddDeviceNotification struct {
Handle uint32
}
func (conn *Connection) AddDeviceNotification(group uint32,offset uint32,length uint32,transmissionMode uint32, maxDelay uint32, cycleTime uint32,callback func([]byte)) (response ADSAddDeviceNotification, err error) {/*{{{*/
request := new(bytes.Buffer)
reserved := make([]byte,16)
var content = []interface{}{
group,
offset,
length,
transmissionMode,
maxDelay, // 1 = 1ms (alt 100ns?)
cycleTime, // 1 = 1ms
reserved,
}
for _, v := range content {
err := binary.Write(request, binary.LittleEndian, v)
if err != nil {
logger.Errorf("binary.Write failed: %s", err)
}
}
// Try to send the request
handle,err := conn.createNotificationWorker(request.Bytes(),callback)
if err!=nil {
logger.Debug("Added notification handler, FAILED: ",err)
return
}
response.Handle = handle
logger.Debug("Added notification handler: ",handle)
return
}/*}}}*/
/*}}}*/
// DeleteDeviceNotification - ADS command id: 7/*{{{*/
func (conn *Connection) DeleteDeviceNotification(handle uint32) {/*{{{*/
request := new(bytes.Buffer)
var content = []interface{}{
handle,
}
for _, v := range content {
err := binary.Write(request, binary.LittleEndian, v)
if err != nil {
logger.Errorf("binary.Write failed: %s", err)
}
}
// Try to send the request
resp,err := conn.sendRequest(7,request.Bytes())
if err!=nil {
return
}
// Check the result error code
result := binary.LittleEndian.Uint32(resp[0:4])
if result>0 {
err = errors.New("Got ADS error number "+strconv.FormatUint(uint64(result),10)+" in DeleteDeviceNotification")
return
}
return
}/*}}}*/
/*}}}*/
// DeviceNotification - ADS command id: 8/*{{{*/
func (conn *Connection) DeviceNotification(in []byte) {
type ADSNotificationStream struct {
Length uint32
Stamps uint32
}
type ADSStampHeader struct {
Timestamp uint64
Samples uint32
}
type ADSNotificationSample struct {
Handle uint32
Size uint32
}
var stream ADSNotificationStream
var header ADSStampHeader
var sample ADSNotificationSample
var content []byte
data := bytes.NewBuffer(in)
// Read stream header
binary.Read(data, binary.LittleEndian, &stream)
for i:=0;i<int(stream.Stamps);i++ {
// Read stamp header
binary.Read(data, binary.LittleEndian, &header)
for j:=0;j<int(header.Samples);j++ {
binary.Read(data, binary.LittleEndian, &sample)
content = make([]byte,sample.Size)
data.Read(content);
_, test := conn.activeNotifications[sample.Handle]
if test {
// Try to send the response to the waiting request function
select {
case conn.activeNotifications[sample.Handle] <- content:
logger.Debugf("Successfully delived notification for handle %d",sample.Handle);
default:
logger.Errorf("Failed to deliver notification for handle %d, deleting device notification",sample.Handle);
conn.DeleteDeviceNotification(sample.Handle)
}
}
}
}
}
/*}}}*/
// ReadWrite TODO - ADS command id: 9/*{{{*/
func (conn *Connection) ReadWrite() {
}
/*}}}*/