Skip to content

Commit 3cc5325

Browse files
committed
Initial work on arduinoOTA with downloadAPI
1 parent f9d5088 commit 3cc5325

File tree

1 file changed

+48
-14
lines changed

1 file changed

+48
-14
lines changed

main.go

+48-14
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ import (
66
"fmt"
77
"io"
88
"io/ioutil"
9+
"net"
910
"net/http"
1011
"net/http/httptrace"
1112
"os"
13+
"path/filepath"
1214
"regexp"
1315
"strconv"
1416
"strings"
@@ -20,20 +22,21 @@ const AppVersion = "1.2.0"
2022
var compileInfo string
2123

2224
var (
23-
version = flag.Bool("version", false, "Prints program version")
24-
networkAddress = flag.String("address", "localhost", "The address of the board")
25-
networkPort = flag.String("port", "80", "The board needs to be listening on this port")
26-
username = flag.String("username", "", "Username for authentication")
27-
password = flag.String("password", "", "Password for authentication")
28-
sketchPath = flag.String("sketch", "", "Sketch path")
29-
uploadEndpoint = flag.String("upload", "", "Upload endpoint")
30-
resetEndpoint = flag.String("reset", "", "Upload endpoint")
31-
syncEndpoint = flag.String("sync", "", "Upload endpoint")
32-
binMode = flag.Bool("b", false, "Upload binary mode")
33-
verbose = flag.Bool("v", true, "Verbose flag")
34-
quiet = flag.Bool("q", false, "Quiet flag")
35-
useSsl = flag.String("ssl", "", "SSL flag")
36-
syncRet = flag.String("sync_exp", "", "sync expected return code in format code:string")
25+
version = flag.Bool("version", false, "Prints program version")
26+
networkAddress = flag.String("address", "localhost", "The address of the board")
27+
networkPort = flag.String("port", "80", "The board needs to be listening on this port")
28+
username = flag.String("username", "", "Username for authentication")
29+
password = flag.String("password", "", "Password for authentication")
30+
sketchPath = flag.String("sketch", "", "Sketch path")
31+
uploadEndpoint = flag.String("upload", "", "Upload endpoint")
32+
resetEndpoint = flag.String("reset", "", "Upload endpoint")
33+
syncEndpoint = flag.String("sync", "", "Upload endpoint")
34+
binMode = flag.Bool("b", false, "Upload binary mode")
35+
verbose = flag.Bool("v", true, "Verbose flag")
36+
quiet = flag.Bool("q", false, "Quiet flag")
37+
useSsl = flag.String("ssl", "", "SSL flag")
38+
syncRet = flag.String("sync_exp", "", "sync expected return code in format code:string")
39+
hasDownloadFile = flag.Bool("d", false, "set to true to take advantage of downloadFile API")
3740
)
3841

3942
type Item struct {
@@ -135,6 +138,15 @@ func main() {
135138
sketchData = bytes.NewBufferString(str)
136139
}
137140

141+
if *hasDownloadFile {
142+
http.ListenAndServe(*networkPort, http.FileServer(http.Dir(filepath.Dir(*sketchPath))))
143+
// find my ip if not specified
144+
ip := getMyIP(net.ParseIP(*networkAddress))
145+
url := "http://" + ip.String() + ":" + *networkPort + "/" + filepath.Base(*sketchPath)
146+
sketchData = bytes.NewBufferString(url)
147+
fmt.Println("Serving sketch on " + url)
148+
}
149+
138150
req, err := http.NewRequest("POST", httpheader+*networkAddress+":"+*networkPort+*uploadEndpoint, sketchData)
139151
if err != nil {
140152
if *verbose {
@@ -228,3 +240,25 @@ func StreamToBytes(stream io.Reader) *bytes.Buffer {
228240
func StreamToString(stream io.Reader) string {
229241
return StreamToBytes(stream).String()
230242
}
243+
244+
func getMyIP(otherip net.IP) net.IP {
245+
ifaces, _ := net.Interfaces()
246+
// handle err
247+
var ips []net.IP
248+
for _, i := range ifaces {
249+
addrs, _ := i.Addrs()
250+
// handle err
251+
for _, addr := range addrs {
252+
switch v := addr.(type) {
253+
case *net.IPNet:
254+
if v.Contains(otherip) {
255+
return v.IP
256+
}
257+
case *net.IPAddr:
258+
ips = append(ips, v.IP)
259+
}
260+
// process IP address
261+
}
262+
}
263+
return nil
264+
}

0 commit comments

Comments
 (0)