Skip to content

Commit e9a0a91

Browse files
author
Alice Pintus
committed
Merge pull request #91 from arduino/safari
Support safari by changing the upload handler
2 parents bd2e0e1 + 34fafb7 commit e9a0a91

File tree

3 files changed

+62
-67
lines changed

3 files changed

+62
-67
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,5 @@ By making a contribution to this project, I certify that:
9595

9696
## Creating a release
9797
Just create a new release on github, and our drone server will build and upload
98-
the compiled binaries for every architecture in a zip file in the release itself.
98+
ithe compiled binaries for every architecture in a zip file in the release itself.
99+

conn.go

+33-40
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package main
44

55
import (
6+
"bytes"
67
"crypto"
78
"crypto/rsa"
89
"crypto/sha256"
@@ -11,7 +12,6 @@ import (
1112
"encoding/pem"
1213
"errors"
1314
"net/http"
14-
"strconv"
1515

1616
log "github.com/Sirupsen/logrus"
1717
"github.com/gin-gonic/gin"
@@ -45,75 +45,68 @@ func (s *WsServer) ServeHTTP(c *gin.Context) {
4545
s.Server.ServeHTTP(c.Writer, c.Request)
4646
}
4747

48+
// Upload contains the data to upload a sketch onto a board
49+
type Upload struct {
50+
Port string `json:"port"`
51+
Board string `json:"board"`
52+
Rewrite string `json:"rewrite"`
53+
Commandline string `json:"commandline"`
54+
Signature string `json:"signature"`
55+
Extra boardExtraInfo `json:"extra"`
56+
Hex []byte `json:"hex"`
57+
Filename string `json:"filename"`
58+
}
59+
4860
func uploadHandler(c *gin.Context) {
49-
log.Print("Received a upload")
50-
port := c.PostForm("port")
51-
if port == "" {
61+
data := new(Upload)
62+
c.BindJSON(data)
63+
64+
log.Printf("%+v", data)
65+
66+
if data.Port == "" {
5267
c.String(http.StatusBadRequest, "port is required")
5368
return
5469
}
55-
board := c.PostForm("board")
56-
if board == "" {
70+
71+
if data.Board == "" {
5772
c.String(http.StatusBadRequest, "board is required")
5873
log.Error("board is required")
5974
return
6075
}
61-
board_rewrite := c.PostForm("board_rewrite")
62-
63-
var extraInfo boardExtraInfo
64-
65-
extraInfo.authdata.UserName = c.PostForm("auth_user")
66-
extraInfo.authdata.Password = c.PostForm("auth_pass")
67-
commandline := c.PostForm("commandline")
68-
if commandline == "undefined" {
69-
commandline = ""
70-
}
7176

72-
signature := c.PostForm("signature")
73-
if signature == "" {
77+
if data.Signature == "" {
7478
c.String(http.StatusBadRequest, "signature is required")
75-
log.Error("signature is required")
7679
return
7780
}
7881

79-
if extraInfo.networkPort {
80-
err := verifyCommandLine(commandline, signature)
82+
if data.Extra.Network {
83+
err := verifyCommandLine(data.Commandline, data.Signature)
8184

8285
if err != nil {
8386
c.String(http.StatusBadRequest, "signature is invalid")
84-
log.Error("signature is invalid")
85-
log.Error(err)
8687
return
8788
}
8889
}
8990

90-
extraInfo.use_1200bps_touch, _ = strconv.ParseBool(c.PostForm("use_1200bps_touch"))
91-
extraInfo.wait_for_upload_port, _ = strconv.ParseBool(c.PostForm("wait_for_upload_port"))
92-
extraInfo.networkPort, _ = strconv.ParseBool(c.PostForm("network"))
93-
94-
if extraInfo.networkPort == false && commandline == "" {
91+
if data.Extra.Network == false && data.Commandline == "" {
9592
c.String(http.StatusBadRequest, "commandline is required for local board")
96-
log.Error("commandline is required for local board")
9793
return
9894
}
9995

100-
sketch, header, err := c.Request.FormFile("sketch_hex")
96+
buffer := bytes.NewBuffer(data.Hex)
97+
98+
path, err := saveFileonTempDir(data.Filename, buffer)
10199
if err != nil {
102100
c.String(http.StatusBadRequest, err.Error())
103101
}
104102

105-
if header != nil {
106-
path, err := saveFileonTempDir(header.Filename, sketch)
107-
if err != nil {
108-
c.String(http.StatusBadRequest, err.Error())
109-
}
103+
if data.Rewrite != "" {
104+
data.Board = data.Rewrite
105+
}
110106

111-
if board_rewrite != "" {
112-
board = board_rewrite
113-
}
107+
go spProgramRW(data.Port, data.Board, path, data.Commandline, data.Extra)
114108

115-
go spProgramRW(port, board, path, commandline, extraInfo)
116-
}
109+
c.String(http.StatusAccepted, "")
117110
}
118111

119112
func verifyCommandLine(input string, signature string) error {

programmer.go

+27-26
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@ import (
55
"bytes"
66
"encoding/json"
77
"fmt"
8-
log "github.com/Sirupsen/logrus"
9-
"github.com/facchinm/go-serial"
10-
"github.com/mattn/go-shellwords"
11-
"github.com/sfreiberg/simplessh"
12-
"github.com/xrash/smetrics"
138
"io"
149
"mime/multipart"
1510
"net/http"
@@ -21,6 +16,12 @@ import (
2116
"strconv"
2217
"strings"
2318
"time"
19+
20+
log "github.com/Sirupsen/logrus"
21+
"github.com/facchinm/go-serial"
22+
"github.com/mattn/go-shellwords"
23+
"github.com/sfreiberg/simplessh"
24+
"github.com/xrash/smetrics"
2425
)
2526

2627
var compiling = false
@@ -31,15 +32,15 @@ func colonToUnderscore(input string) string {
3132
}
3233

3334
type basicAuthData struct {
34-
UserName string
35-
Password string
35+
Username string `json:"username"`
36+
Password string `json:"password"`
3637
}
3738

3839
type boardExtraInfo struct {
39-
use_1200bps_touch bool
40-
wait_for_upload_port bool
41-
networkPort bool
42-
authdata basicAuthData
40+
Use1200bpsTouch bool `json:"use_100bps_touch"`
41+
WaitForUploadPort bool `json:"wait_for_upload_port"`
42+
Network bool `json:"network"`
43+
Auth basicAuthData `json:"auth"`
4344
}
4445

4546
// Scp uploads sourceFile to remote machine like native scp console app.
@@ -90,15 +91,15 @@ func spProgramSSHNetwork(portname string, boardname string, filePath string, com
9091
log.Println("Starting network upload")
9192
log.Println("Board Name: " + boardname)
9293

93-
if authdata.UserName == "" {
94-
authdata.UserName = "root"
94+
if authdata.Username == "" {
95+
authdata.Username = "root"
9596
}
9697

9798
if authdata.Password == "" {
9899
authdata.Password = "arduino"
99100
}
100101

101-
ssh_client, err := simplessh.ConnectWithPassword(portname+":22", authdata.UserName, authdata.Password)
102+
ssh_client, err := simplessh.ConnectWithPassword(portname+":22", authdata.Username, authdata.Password)
102103
if err != nil {
103104
log.Println("Error connecting via ssh")
104105
return err
@@ -133,8 +134,8 @@ func spProgramNetwork(portname string, boardname string, filePath string, authda
133134
log.Println("Starting network upload")
134135
log.Println("Board Name: " + boardname)
135136

136-
if authdata.UserName == "" {
137-
authdata.UserName = "root"
137+
if authdata.Username == "" {
138+
authdata.Username = "root"
138139
}
139140

140141
if authdata.Password == "" {
@@ -182,8 +183,8 @@ func spProgramNetwork(portname string, boardname string, filePath string, authda
182183
}
183184
// Don't forget to set the content type, this will contain the boundary.
184185
req.Header.Set("Content-Type", w.FormDataContentType())
185-
if authdata.UserName != "" {
186-
req.SetBasicAuth(authdata.UserName, authdata.Password)
186+
if authdata.Username != "" {
187+
req.SetBasicAuth(authdata.Username, authdata.Password)
187188
}
188189

189190
//h.broadcastSys <- []byte("Start flashing with command " + cmdString)
@@ -211,8 +212,8 @@ func spProgramNetwork(portname string, boardname string, filePath string, authda
211212
func spProgramLocal(portname string, boardname string, filePath string, commandline string, extraInfo boardExtraInfo) error {
212213

213214
var err error
214-
if extraInfo.use_1200bps_touch {
215-
portname, err = touch_port_1200bps(portname, extraInfo.wait_for_upload_port)
215+
if extraInfo.Use1200bpsTouch {
216+
portname, err = touch_port_1200bps(portname, extraInfo.WaitForUploadPort)
216217
}
217218

218219
if err != nil {
@@ -264,11 +265,11 @@ func spProgramRW(portname string, boardname string, filePath string, commandline
264265

265266
var err error
266267

267-
if extraInfo.networkPort {
268-
err = spProgramNetwork(portname, boardname, filePath, extraInfo.authdata)
268+
if extraInfo.Network {
269+
err = spProgramNetwork(portname, boardname, filePath, extraInfo.Auth)
269270
if err != nil {
270271
// no http method available, try ssh upload
271-
err = spProgramSSHNetwork(portname, boardname, filePath, commandline, extraInfo.authdata)
272+
err = spProgramSSHNetwork(portname, boardname, filePath, commandline, extraInfo.Auth)
272273
}
273274
} else {
274275
err = spProgramLocal(portname, boardname, filePath, commandline, extraInfo)
@@ -423,7 +424,7 @@ func findNewPortName(slice1 []string, slice2 []string) string {
423424
return ""
424425
}
425426

426-
func touch_port_1200bps(portname string, wait_for_upload_port bool) (string, error) {
427+
func touch_port_1200bps(portname string, WaitForUploadPort bool) (string, error) {
427428
initialPortName := portname
428429
log.Println("Restarting in bootloader mode")
429430

@@ -455,7 +456,7 @@ func touch_port_1200bps(portname string, wait_for_upload_port bool) (string, err
455456
}()
456457

457458
// wait for port to disappear
458-
if wait_for_upload_port {
459+
if WaitForUploadPort {
459460
for {
460461
ports, _ = serial.GetPortsList()
461462
log.Println(ports)
@@ -471,7 +472,7 @@ func touch_port_1200bps(portname string, wait_for_upload_port bool) (string, err
471472
}
472473

473474
// wait for port to reappear
474-
if wait_for_upload_port {
475+
if WaitForUploadPort {
475476
after_reset_ports, _ := serial.GetPortsList()
476477
log.Println(after_reset_ports)
477478
for {

0 commit comments

Comments
 (0)