-
Notifications
You must be signed in to change notification settings - Fork 4
/
sacp.go
362 lines (306 loc) · 7.9 KB
/
sacp.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
/*
Author: https://github.com/kanocz
*/
package main
import (
"bytes"
"crypto/md5"
"encoding/binary"
"encoding/hex"
"errors"
"io"
"log"
"net"
"time"
)
const (
SACP_data_len = 60 * 1024 // just as defined in original python code
)
var (
errInvalidSACP = errors.New("data doesn't look like SACP packet")
errInvalidSACPVer = errors.New("SACP version missmatch")
errInvalidChksum = errors.New("SACP checksum doesn't match data")
errInvalidSize = errors.New("SACP package is too short")
)
type SACP_pack struct {
// 0xAA byte
// 0x55 byte
// DataLength uint16
// 0x01 (SACP version)
ReceiverID byte
// head_chksum byte
SenderID byte
Attribute byte
Sequence uint16
CommandSet byte
CommandID byte
Data []byte
// data_checksum uint16
}
func (sacp SACP_pack) Encode() []byte {
result := make([]byte, 15+len(sacp.Data))
result[0] = 0xAA
result[1] = 0x55
binary.LittleEndian.PutUint16(result[2:4], uint16(len(sacp.Data)+6+2))
result[4] = 0x01
result[5] = sacp.ReceiverID
result[6] = sacp.headChksum(result[:6])
result[7] = sacp.SenderID
result[8] = sacp.Attribute
binary.LittleEndian.PutUint16(result[9:11], sacp.Sequence)
result[11] = sacp.CommandSet
result[12] = sacp.CommandID
if len(sacp.Data) > 0 { // this also include check on nil
copy(result[13:], sacp.Data)
}
binary.LittleEndian.PutUint16(result[len(result)-2:], sacp.U16Chksum(result[7:], uint16(len(sacp.Data))+6))
return result[:]
}
func (sacp *SACP_pack) Decode(data []byte) error {
if len(data) < 13 {
return errInvalidSize
}
if data[0] != 0xAA && data[1] != 0x55 {
return errInvalidSACP
}
dataLen := binary.LittleEndian.Uint16(data[2:4])
if dataLen != uint16(len(data)-7) {
return errInvalidSize
}
if data[4] != 0x01 {
return errInvalidSACPVer
}
if sacp.headChksum(data[:6]) != data[6] {
return errInvalidChksum
}
if binary.LittleEndian.Uint16(data[len(data)-2:]) != sacp.U16Chksum(data[7:], dataLen-2) {
return errInvalidChksum
}
sacp.ReceiverID = data[5]
sacp.SenderID = data[7]
sacp.Attribute = data[8]
sacp.Sequence = binary.LittleEndian.Uint16(data[9:11])
sacp.CommandSet = data[11]
sacp.CommandID = data[12]
sacp.Data = data[13 : len(data)-2]
return nil
}
func (sacp *SACP_pack) headChksum(data []byte) byte {
crc := byte(0)
poly := byte(7)
for i := 0; i < len(data); i++ {
for j := 0; j < 8; j++ {
bit := ((data[i] & 0xff) >> (7 - j) & 0x01) == 1
c07 := (crc >> 7 & 0x01) == 1
crc = crc << 1
if (!c07 && bit) || (c07 && !bit) {
crc ^= poly
}
}
}
crc = crc & 0xff
return crc
}
func (sacp *SACP_pack) U16Chksum(package_data []byte, length uint16) uint16 {
check_num := uint32(0)
if length > 0 {
for i := 0; i < int(length-1); i += 2 {
check_num += uint32((uint32(package_data[i])&0xff)<<8 | uint32(package_data[i+1])&0xff)
check_num &= 0xffffffff
}
if length%2 != 0 {
check_num += uint32(package_data[length-1])
}
}
for check_num > 0xFFFF {
check_num = ((check_num >> 16) & 0xFFFF) + (check_num & 0xFFFF)
}
check_num = ^check_num
return uint16(check_num & 0xFFFF)
}
func writeSACPstring(w io.Writer, s string) {
binary.Write(w, binary.LittleEndian, uint16(len(s)))
w.Write([]byte(s))
}
func writeSACPbytes(w io.Writer, s []byte) {
binary.Write(w, binary.LittleEndian, uint16(len(s)))
w.Write(s)
}
func writeLE[T any](w io.Writer, u T) {
binary.Write(w, binary.LittleEndian, u)
}
func SACP_connect(ip string, timeout time.Duration) (net.Conn, error) {
conn, err := net.Dial("tcp4", ip+":8888")
if err != nil {
// log.Printf("Error connecting to %s: %v", ip, err)
return nil, err
}
conn.SetWriteDeadline(time.Now().Add(timeout))
_, err = conn.Write(SACP_pack{
ReceiverID: 2,
SenderID: 0,
Attribute: 0,
Sequence: 1,
CommandSet: 0x01,
CommandID: 0x05,
Data: []byte{
11, 0, 's', 'm', '2', 'u', 'p', 'l', 'o', 'a', 'd', 'e', 'r',
0, 0,
0, 0,
},
}.Encode())
if err != nil {
// log.Println("Error writing \"hello\": ", err)
conn.Close()
return nil, err
}
for {
p, err := SACP_read(conn, timeout)
if err != nil || p == nil {
// log.Println("Error reading \"hello\" responce: ", err)
conn.Close()
return nil, err
}
if Debug {
log.Printf("-- SACP_connect got:\n%v", p)
}
if p.CommandSet == 1 && p.CommandID == 5 {
break
}
}
if Debug {
log.Println("-- Connected to printer")
}
return conn, nil
}
func SACP_read(conn net.Conn, timeout time.Duration) (*SACP_pack, error) {
var buf [SACP_data_len + 15]byte
deadline := time.Now().Add(timeout)
conn.SetReadDeadline(deadline)
n, err := conn.Read(buf[:4])
if err != nil {
return nil, err
}
if n != 4 {
return nil, errInvalidSize
}
dataLen := binary.LittleEndian.Uint16(buf[2:4])
n, err = conn.Read(buf[4 : dataLen+7])
if err != nil {
return nil, err
}
if n != int(dataLen+3) {
return nil, errInvalidSize
}
var sacp SACP_pack
err = sacp.Decode(buf[:dataLen+7])
return &sacp, err
}
func SACP_start_upload(conn net.Conn, filename string, gcode []byte, timeout time.Duration) error {
// prepare data for upload begin packet
package_count := uint16((len(gcode) / SACP_data_len) + 1)
md5hash := md5.Sum(gcode)
data := bytes.Buffer{}
writeSACPstring(&data, filename)
writeLE(&data, uint32(len(gcode)))
writeLE(&data, package_count)
writeSACPstring(&data, hex.EncodeToString(md5hash[:]))
if Debug {
log.Println("-- Starting upload ...")
}
conn.SetWriteDeadline(time.Now().Add(timeout))
_, err := conn.Write(SACP_pack{
ReceiverID: 2,
SenderID: 0,
Attribute: 0,
Sequence: 1,
CommandSet: 0xb0,
CommandID: 0x00,
Data: data.Bytes(),
}.Encode())
if err != nil {
return err
}
for {
// always receive packet, then send responce
conn.SetReadDeadline(time.Now().Add(timeout))
p, err := SACP_read(conn, time.Second*10)
if err != nil {
return err
}
if p == nil {
return errInvalidSize
}
if Debug {
log.Printf("-- Got reply from printer: %v", p)
}
switch {
case p.CommandSet == 0xb0 && p.CommandID == 0:
// just ignore, don't know that this message means :)
case p.CommandSet == 0xb0 && p.CommandID == 1:
// sending next chunk
if len(p.Data) < 4 {
return errInvalidSize
}
md5_len := binary.LittleEndian.Uint16(p.Data[:2])
if len(p.Data) < 2+int(md5_len)+2 {
return errInvalidSize
}
pkgRequested := binary.LittleEndian.Uint16(p.Data[2+md5_len : 2+md5_len+2])
var pkgData []byte
if pkgRequested == package_count-1 { // last package
pkgData = gcode[SACP_data_len*int(pkgRequested):]
} else { // regular package
pkgData = gcode[SACP_data_len*int(pkgRequested) : SACP_data_len*int(pkgRequested+1)]
}
data := bytes.Buffer{}
data.WriteByte(0)
writeSACPstring(&data, hex.EncodeToString(md5hash[:]))
writeLE(&data, pkgRequested)
writeSACPbytes(&data, pkgData)
// log.Printf(" sending package %d of %d", pkgRequested+1, package_count)
perc := float64(pkgRequested+1) / float64(package_count) * 100.0
log.Printf(" - SACP sending %.1f%%", perc)
conn.SetWriteDeadline(time.Now().Add(timeout))
_, err := conn.Write(SACP_pack{
ReceiverID: 2,
SenderID: 0,
Attribute: 1,
Sequence: p.Sequence,
CommandSet: 0xb0,
CommandID: 0x01,
Data: data.Bytes(),
}.Encode())
if err != nil {
return err
}
case p.CommandSet == 0xb0 && p.CommandID == 2:
// send finished!!!
if len(p.Data) == 1 && p.Data[0] == 0 {
if Debug {
log.Print("-- Upload finished")
}
if err := SACP_disconnect(conn, timeout); err != nil {
return err
}
return nil // everything is ok!
}
log.Print("Unable to process b0/02 with invalid data", p.Data)
default:
continue
}
}
}
func SACP_disconnect(conn net.Conn, timeout time.Duration) (err error) {
conn.SetWriteDeadline(time.Now().Add(timeout))
_, err = conn.Write(SACP_pack{
ReceiverID: 2,
SenderID: 0,
Attribute: 0,
Sequence: 1,
CommandSet: 0x01,
CommandID: 0x06,
Data: []byte{},
}.Encode())
return err
}