Skip to content

Commit 30e6746

Browse files
committed
- fix name of the upload file on windows
- fix some bugs
1 parent 4c4cd96 commit 30e6746

File tree

4 files changed

+38
-27
lines changed

4 files changed

+38
-27
lines changed

connector_sacp.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,17 @@ func (sc *SACPConnector) Ping(p *Printer) bool {
3232
}
3333

3434
func (sc *SACPConnector) Connect() (err error) {
35-
if conn, err := SACP_connect(sc.printer.IP, SACPTimeout*time.Second); err == nil {
35+
conn, err := SACP_connect(sc.printer.IP, SACPTimeout*time.Second)
36+
if conn != nil {
3637
sc.conn = conn
3738
}
38-
return
39+
return err
3940
}
4041

4142
func (sc *SACPConnector) Disconnect() error {
43+
if sc.conn != nil {
44+
sc.conn.Close()
45+
}
4246
return nil
4347
}
4448

main.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"log"
66
"os"
77
"os/signal"
8-
"path"
98
"path/filepath"
109
"syscall"
1110
"time"
@@ -144,17 +143,17 @@ func main() {
144143
envFilename := os.Getenv("SLIC3R_PP_OUTPUT_NAME")
145144

146145
// Upload files to host
147-
for _, filepath := range _Payloads {
148-
content, err := os.ReadFile(filepath)
146+
for _, fpath := range _Payloads {
147+
content, err := os.ReadFile(fpath)
149148
if err != nil {
150149
log.Panicln(err)
151150
}
152-
st, _ := os.Stat(filepath)
151+
st, _ := os.Stat(fpath)
153152
var fname string
154153
if envFilename == "" {
155-
fname = normalizedFilename(path.Base(filepath))
154+
fname = normalizedFilename(filepath.Base(fpath))
156155
} else {
157-
fname = normalizedFilename(path.Base(envFilename))
156+
fname = normalizedFilename(filepath.Base(envFilename))
158157
}
159158
log.Printf("Uploading file '%s' [%s]...", fname, humanReadableSize(st.Size()))
160159
if err := Connector.Upload(printer, fname, content); err != nil {

octoprint.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@ func startOctoPrintServer(listenAddr string, printer *Printer) error {
2727
mux.HandleFunc("/api/files/local", func(w http.ResponseWriter, r *http.Request) {
2828
// Check if request is a POST request
2929
if r.Method != http.MethodPost {
30+
log.Print("Method not allowed: ", r.Method)
3031
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
3132
return
3233
}
3334

3435
// Retrieve the uploaded file
3536
file, fd, err := r.FormFile("file")
3637
if err != nil {
38+
log.Print("Error retrieving file: ", err.Error())
3739
http.Error(w, err.Error(), http.StatusBadRequest)
3840
return
3941
}
@@ -43,6 +45,7 @@ func startOctoPrintServer(listenAddr string, printer *Printer) error {
4345
// Send the stream to the printer
4446
content, _ := io.ReadAll(file)
4547
if err := Connector.Upload(printer, fd.Filename, content); err != nil {
48+
log.Print("Error uploading file: ", err.Error())
4649
http.Error(w, err.Error(), http.StatusInternalServerError)
4750
return
4851
}

sacp.go

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func (sacp SACP_pack) Encode() []byte {
6262
copy(result[13:], sacp.Data)
6363
}
6464

65-
binary.LittleEndian.PutUint16(result[len(result)-2:], sacp.U16Chksum(result[7:], len(sacp.Data)+6))
65+
binary.LittleEndian.PutUint16(result[len(result)-2:], sacp.U16Chksum(result[7:], uint16(len(sacp.Data))+6))
6666

6767
return result[:]
6868
}
@@ -71,11 +71,11 @@ func (sacp *SACP_pack) Decode(data []byte) error {
7171
if len(data) < 13 {
7272
return errInvalidSize
7373
}
74-
if data[0] != 0xAA || data[1] != 0x55 {
74+
if data[0] != 0xAA && data[1] != 0x55 {
7575
return errInvalidSACP
7676
}
7777
dataLen := binary.LittleEndian.Uint16(data[2:4])
78-
if int(dataLen) != (len(data) - 7) {
78+
if dataLen != uint16(len(data)-7) {
7979
return errInvalidSize
8080
}
8181
if data[4] != 0x01 {
@@ -84,7 +84,7 @@ func (sacp *SACP_pack) Decode(data []byte) error {
8484
if sacp.headChksum(data[:6]) != data[6] {
8585
return errInvalidChksum
8686
}
87-
if binary.LittleEndian.Uint16(data[len(data)-2:]) != sacp.U16Chksum(data[7:], int(dataLen)-2) {
87+
if binary.LittleEndian.Uint16(data[len(data)-2:]) != sacp.U16Chksum(data[7:], dataLen-2) {
8888
return errInvalidChksum
8989
}
9090

@@ -100,31 +100,31 @@ func (sacp *SACP_pack) Decode(data []byte) error {
100100
}
101101

102102
func (sacp *SACP_pack) headChksum(data []byte) byte {
103-
crc := 0
104-
poly := 7
103+
crc := byte(0)
104+
poly := byte(7)
105105
for i := 0; i < len(data); i++ {
106106
for j := 0; j < 8; j++ {
107-
bit := data[i]&255>>(7-j)&1 == 1
108-
c07 := crc>>7&1 == 1
107+
bit := ((data[i] & 0xff) >> (7 - j) & 0x01) == 1
108+
c07 := (crc >> 7 & 0x01) == 1
109109
crc = crc << 1
110110
if (!c07 && bit) || (c07 && !bit) {
111111
crc ^= poly
112112
}
113113
}
114114
}
115-
crc = crc & 255
116-
return byte(crc)
115+
crc = crc & 0xff
116+
return crc
117117
}
118118

119-
func (sacp *SACP_pack) U16Chksum(package_data []byte, length int) uint16 {
120-
check_num := uint64(0)
119+
func (sacp *SACP_pack) U16Chksum(package_data []byte, length uint16) uint16 {
120+
check_num := uint32(0)
121121
if length > 0 {
122-
for i := 0; i < length-1; i += 2 {
123-
check_num += uint64(package_data[i])<<8 | uint64(package_data[i+1])
124-
check_num &= 0xffffffff // TODO: maybe just use uint32?
122+
for i := 0; i < int(length-1); i += 2 {
123+
check_num += uint32((uint32(package_data[i])&0xff)<<8 | uint32(package_data[i+1])&0xff)
124+
check_num &= 0xffffffff
125125
}
126126
if length%2 != 0 {
127-
check_num += uint64(package_data[length-1])
127+
check_num += uint32(package_data[length-1])
128128
}
129129
}
130130
for check_num > 0xFFFF {
@@ -196,22 +196,27 @@ func SACP_connect(ip string, timeout time.Duration) (net.Conn, error) {
196196
}
197197

198198
func SACP_read(conn net.Conn, timeout time.Duration) (*SACP_pack, error) {
199-
200199
var buf [SACP_data_len + 15]byte
201200

202201
deadline := time.Now().Add(timeout)
203202
conn.SetReadDeadline(deadline)
204203

205204
n, err := conn.Read(buf[:4])
206-
if err != nil || n != 4 {
205+
if err != nil {
207206
return nil, err
208207
}
208+
if n != 4 {
209+
return nil, errInvalidSize
210+
}
209211

210212
dataLen := binary.LittleEndian.Uint16(buf[2:4])
211213
n, err = conn.Read(buf[4 : dataLen+7])
212-
if err != nil || n != int(dataLen+3) {
214+
if err != nil {
213215
return nil, err
214216
}
217+
if n != int(dataLen+3) {
218+
return nil, errInvalidSize
219+
}
215220

216221
var sacp SACP_pack
217222
err = sacp.Decode(buf[:dataLen+7])

0 commit comments

Comments
 (0)