Skip to content

Commit 019f95a

Browse files
committed
Merge branch 'devel' into seriallist_native_rebased
2 parents 98e0600 + a338c00 commit 019f95a

File tree

6 files changed

+95
-39
lines changed

6 files changed

+95
-39
lines changed

conn.go

+22-10
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import (
1111
"encoding/hex"
1212
"encoding/pem"
1313
"errors"
14+
"io/ioutil"
1415
"net/http"
16+
"path"
1517

1618
log "github.com/Sirupsen/logrus"
1719
"github.com/arduino/arduino-create-agent/utilities"
@@ -46,16 +48,22 @@ func (s *WsServer) ServeHTTP(c *gin.Context) {
4648
s.Server.ServeHTTP(c.Writer, c.Request)
4749
}
4850

51+
type AdditionalFile struct {
52+
Hex []byte `json:"hex"`
53+
Filename string `json:"filename"`
54+
}
55+
4956
// Upload contains the data to upload a sketch onto a board
5057
type Upload struct {
51-
Port string `json:"port"`
52-
Board string `json:"board"`
53-
Rewrite string `json:"rewrite"`
54-
Commandline string `json:"commandline"`
55-
Signature string `json:"signature"`
56-
Extra boardExtraInfo `json:"extra"`
57-
Hex []byte `json:"hex"`
58-
Filename string `json:"filename"`
58+
Port string `json:"port"`
59+
Board string `json:"board"`
60+
Rewrite string `json:"rewrite"`
61+
Commandline string `json:"commandline"`
62+
Signature string `json:"signature"`
63+
Extra boardExtraInfo `json:"extra"`
64+
Hex []byte `json:"hex"`
65+
Filename string `json:"filename"`
66+
ExtraFiles []AdditionalFile `json:"extrafiles"`
5967
}
6068

6169
func uploadHandler(c *gin.Context) {
@@ -96,17 +104,21 @@ func uploadHandler(c *gin.Context) {
96104

97105
buffer := bytes.NewBuffer(data.Hex)
98106

99-
path, err := utilities.SaveFileonTempDir(data.Filename, buffer)
107+
filepath, err := utilities.SaveFileonTempDir(data.Filename, buffer)
100108
if err != nil {
101109
c.String(http.StatusBadRequest, err.Error())
102110
return
103111
}
104112

113+
for _, extraFile := range data.ExtraFiles {
114+
ioutil.WriteFile(path.Join(path.Dir(filepath), extraFile.Filename), extraFile.Hex, 0644)
115+
}
116+
105117
if data.Rewrite != "" {
106118
data.Board = data.Rewrite
107119
}
108120

109-
go spProgramRW(data.Port, data.Board, path, data.Commandline, data.Extra)
121+
go spProgramRW(data.Port, data.Board, filepath, data.Commandline, data.Extra)
110122

111123
c.String(http.StatusAccepted, "")
112124
}

programmer.go

+28-15
Original file line numberDiff line numberDiff line change
@@ -227,15 +227,15 @@ func spProgramLocal(portname string, boardname string, filePath string, commandl
227227

228228
log.Printf("Received commandline (unresolved):" + commandline)
229229

230-
commandline = strings.Replace(commandline, "{build.path}", filepath.ToSlash(filepath.Dir(filePath)), 1)
231-
commandline = strings.Replace(commandline, "{build.project_name}", strings.TrimSuffix(filepath.Base(filePath), filepath.Ext(filepath.Base(filePath))), 1)
232-
commandline = strings.Replace(commandline, "{serial.port}", portname, 1)
233-
commandline = strings.Replace(commandline, "{serial.port.file}", filepath.Base(portname), 1)
230+
commandline = strings.Replace(commandline, "{build.path}", filepath.ToSlash(filepath.Dir(filePath)), -1)
231+
commandline = strings.Replace(commandline, "{build.project_name}", strings.TrimSuffix(filepath.Base(filePath), filepath.Ext(filepath.Base(filePath))), -1)
232+
commandline = strings.Replace(commandline, "{serial.port}", portname, -1)
233+
commandline = strings.Replace(commandline, "{serial.port.file}", filepath.Base(portname), -1)
234234

235235
if extraInfo.Verbose == true {
236-
commandline = strings.Replace(commandline, "{upload.verbose}", extraInfo.ParamsVerbose, 1)
236+
commandline = strings.Replace(commandline, "{upload.verbose}", extraInfo.ParamsVerbose, -1)
237237
} else {
238-
commandline = strings.Replace(commandline, "{upload.verbose}", extraInfo.ParamsQuiet, 1)
238+
commandline = strings.Replace(commandline, "{upload.verbose}", extraInfo.ParamsQuiet, -1)
239239
}
240240

241241
// search for runtime variables and replace with values from globalToolsMap
@@ -328,7 +328,7 @@ func spHandlerProgram(flasher string, cmdString []string) error {
328328
return err
329329
}
330330

331-
multi := io.MultiReader(stderr, stdout)
331+
//multi := io.MultiReader(stderr, stdout)
332332

333333
// Stdout buffer
334334
//var cmdOutput []byte
@@ -341,16 +341,29 @@ func spHandlerProgram(flasher string, cmdString []string) error {
341341

342342
err = oscmd.Start()
343343

344-
in := bufio.NewScanner(multi)
344+
stdout_copy := bufio.NewScanner(stdout)
345+
stderr_copy := bufio.NewScanner(stderr)
345346

346-
in.Split(bufio.ScanLines)
347+
stdout_copy.Split(bufio.ScanLines)
348+
stderr_copy.Split(bufio.ScanLines)
347349

348-
for in.Scan() {
349-
log.Info(in.Text())
350-
mapD := map[string]string{"ProgrammerStatus": "Busy", "Msg": in.Text()}
351-
mapB, _ := json.Marshal(mapD)
352-
h.broadcastSys <- mapB
353-
}
350+
go func() {
351+
for stdout_copy.Scan() {
352+
log.Info(stdout_copy.Text())
353+
mapD := map[string]string{"ProgrammerStatus": "Busy", "Msg": stdout_copy.Text()}
354+
mapB, _ := json.Marshal(mapD)
355+
h.broadcastSys <- mapB
356+
}
357+
}()
358+
359+
go func() {
360+
for stderr_copy.Scan() {
361+
log.Info(stderr_copy.Text())
362+
mapD := map[string]string{"ProgrammerStatus": "Busy", "Msg": stderr_copy.Text()}
363+
mapB, _ := json.Marshal(mapD)
364+
h.broadcastSys <- mapB
365+
}
366+
}()
354367

355368
err = oscmd.Wait()
356369

tools/download.go

+24-14
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ type index struct {
5353
var systems = map[string]string{
5454
"linuxamd64": "x86_64-linux-gnu",
5555
"linux386": "i686-linux-gnu",
56-
"darwinamd64": "i386-apple-darwin11",
56+
"darwinamd64": "apple-darwin",
5757
"windows386": "i686-mingw32",
5858
}
5959

@@ -71,15 +71,13 @@ func checkGPGSig(fileName string, sigFileName string) error {
7171
if err != nil {
7272
return err
7373
}
74-
7574
defer sigFile.Close()
7675

7776
// Get a Reader for the signature file
7877
file, err := os.Open(fileName)
7978
if err != nil {
8079
return err
8180
}
82-
8381
defer file.Close()
8482

8583
publicKeyBin, err := hex.DecodeString(publicKeyHex)
@@ -241,7 +239,10 @@ func (t *Tools) Download(name, version, behaviour string) error {
241239
return err
242240
}
243241

244-
t.installDrivers(location)
242+
err = t.installDrivers(location)
243+
if err != nil {
244+
return err
245+
}
245246

246247
// Ensure that the files are executable
247248
t.Logger.Println("Ensure that the files are executable")
@@ -413,10 +414,9 @@ func extractTarGz(body []byte, location string) (string, error) {
413414
}
414415

415416
file, err := os.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, info.Mode())
416-
if err != nil {
417-
//return location, err
417+
if err == nil {
418+
defer file.Close()
418419
}
419-
defer file.Close()
420420
_, err = io.Copy(file, tarReader)
421421
if err != nil {
422422
//return location, err
@@ -425,15 +425,26 @@ func extractTarGz(body []byte, location string) (string, error) {
425425
return location, nil
426426
}
427427

428-
func (t *Tools) installDrivers(location string) {
428+
func (t *Tools) installDrivers(location string) error {
429429
if runtime.GOOS == "windows" {
430430
if _, err := os.Stat(filepath.Join(location, "post_install.bat")); err == nil {
431431
t.Logger.Println("Installing drivers")
432-
oscmd := exec.Command(filepath.Join(location, "post_install.bat"))
433-
TellCommandNotToSpawnShell(oscmd)
434-
oscmd.Run()
432+
ok := MessageBox("Installing drivers", "We are about to install some drivers needed to use Arduino/Genuino boards\nDo you want to continue?")
433+
t.Logger.Println(ok)
434+
if ok == 6 {
435+
os.Chdir(location)
436+
oscmd := exec.Command("post_install.bat")
437+
TellCommandNotToSpawnShell(oscmd)
438+
t.Logger.Println(oscmd)
439+
err = oscmd.Run()
440+
t.Logger.Println(err)
441+
return err
442+
} else {
443+
return errors.New("Could not install drivers")
444+
}
435445
}
436446
}
447+
return nil
437448
}
438449

439450
func extractBz2(body []byte, location string) (string, error) {
@@ -481,10 +492,9 @@ func extractBz2(body []byte, location string) (string, error) {
481492
}
482493

483494
file, err := os.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, info.Mode())
484-
if err != nil {
485-
//return location, err
495+
if err == nil {
496+
defer file.Close()
486497
}
487-
defer file.Close()
488498
_, err = io.Copy(file, tarReader)
489499
if err != nil {
490500
//return location, err

tools/hidefile_darwin.go

+4
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,7 @@ func hideFile(path string) {
1010

1111
func TellCommandNotToSpawnShell(_ *exec.Cmd) {
1212
}
13+
14+
func MessageBox(title, text string) int {
15+
return 0
16+
}

tools/hidefile_linux.go

+4
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,7 @@ func hideFile(path string) {
1010

1111
func TellCommandNotToSpawnShell(_ *exec.Cmd) {
1212
}
13+
14+
func MessageBox(title, text string) int {
15+
return 0
16+
}

tools/hidefile_windows.go

+13
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package tools
33
import (
44
"os/exec"
55
"syscall"
6+
"unsafe"
67
)
78

89
func hideFile(path string) {
@@ -15,3 +16,15 @@ func hideFile(path string) {
1516
func TellCommandNotToSpawnShell(oscmd *exec.Cmd) {
1617
oscmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
1718
}
19+
20+
func MessageBox(title, text string) int {
21+
var mod = syscall.NewLazyDLL("user32.dll")
22+
var proc = mod.NewProc("MessageBoxW")
23+
var MB_YESNO = 0x00000004
24+
25+
ret, _, _ := proc.Call(0,
26+
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(text))),
27+
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(title))),
28+
uintptr(MB_YESNO))
29+
return int(ret)
30+
}

0 commit comments

Comments
 (0)