7
7
"io"
8
8
"io/ioutil"
9
9
"net/http"
10
+ "net/http/httptrace"
10
11
"os"
11
12
"regexp"
12
13
"strconv"
22
23
version = flag .Bool ("version" , false , "Prints program version" )
23
24
networkAddress = flag .String ("address" , "localhost" , "The address of the board" )
24
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" )
25
28
sketchPath = flag .String ("sketch" , "" , "Sketch path" )
26
29
uploadEndpoint = flag .String ("upload" , "" , "Upload endpoint" )
27
30
resetEndpoint = flag .String ("reset" , "" , "Upload endpoint" )
@@ -46,6 +49,10 @@ func main() {
46
49
os .Exit (0 )
47
50
}
48
51
52
+ var httpClient = & http.Client {
53
+ Timeout : time .Second * 10 ,
54
+ }
55
+
49
56
httpheader := "http://"
50
57
51
58
if * useSsl != "" {
@@ -68,7 +75,7 @@ func main() {
68
75
fmt .Println ("Resetting the board" )
69
76
}
70
77
71
- resp , err := http .Post (httpheader + * networkAddress + ":" + * networkPort + * syncEndpoint , "" , nil )
78
+ resp , err := httpClient .Post (httpheader + * networkAddress + ":" + * networkPort + * syncEndpoint , "" , nil )
72
79
if err != nil || resp .StatusCode != syncRetCode {
73
80
if * verbose {
74
81
fmt .Println ("Failed to reset the board, upload failed" )
@@ -86,7 +93,7 @@ func main() {
86
93
timeout := 0
87
94
88
95
for timeout < 10 {
89
- resp , err := http .Get (httpheader + * networkAddress + ":" + * networkPort + * syncEndpoint )
96
+ resp , err := httpClient .Get (httpheader + * networkAddress + ":" + * networkPort + * syncEndpoint )
90
97
if err != nil {
91
98
if * verbose {
92
99
fmt .Println ("Failed to reset the board, upload failed" )
@@ -108,10 +115,6 @@ func main() {
108
115
}
109
116
110
117
if * uploadEndpoint != "" {
111
- if * verbose {
112
- fmt .Println ("Uploading the sketch" )
113
- }
114
-
115
118
f , err := os .Open (* sketchPath )
116
119
if err != nil {
117
120
if * verbose {
@@ -139,9 +142,44 @@ func main() {
139
142
}
140
143
os .Exit (1 )
141
144
}
142
- req .Header .Set ("Content-Type" , "application/x-www-form-urlencoded" )
143
145
144
- resp , err := http .DefaultClient .Do (req )
146
+ if * binMode {
147
+ req .Header .Set ("Content-Type" , "application/octet-stream" )
148
+ } else {
149
+ req .Header .Set ("Content-Type" , "application/x-www-form-urlencoded" )
150
+ }
151
+
152
+ if len (* username ) > 0 && len (* password ) != 0 {
153
+ req .SetBasicAuth (* username , * password )
154
+ }
155
+
156
+ if * verbose {
157
+ trace := & httptrace.ClientTrace {
158
+ ConnectStart : func (network , addr string ) {
159
+ fmt .Print ("Connecting to board ... " )
160
+ },
161
+ ConnectDone : func (network , addr string , err error ) {
162
+ if err != nil {
163
+ fmt .Println ("failed!" )
164
+ } else {
165
+ fmt .Println (" done" )
166
+ }
167
+ },
168
+ WroteHeaders : func () {
169
+ fmt .Print ("Uploading sketch ... " )
170
+ },
171
+ WroteRequest : func (wri httptrace.WroteRequestInfo ) {
172
+ fmt .Println (" done" )
173
+ fmt .Print ("Flashing sketch ... " )
174
+ },
175
+ GotFirstResponseByte : func () {
176
+ fmt .Println (" done" )
177
+ },
178
+ }
179
+ req = req .WithContext (httptrace .WithClientTrace (req .Context (), trace ))
180
+ }
181
+
182
+ resp , err := httpClient .Do (req )
145
183
if err != nil {
146
184
if * verbose {
147
185
fmt .Println ("Error flashing the sketch" )
@@ -170,7 +208,7 @@ func main() {
170
208
fmt .Println ("Resetting the board" )
171
209
}
172
210
173
- resp , err := http .Post (httpheader + * networkAddress + ":" + * networkPort + * resetEndpoint , "" , nil )
211
+ resp , err := httpClient .Post (httpheader + * networkAddress + ":" + * networkPort + * resetEndpoint , "" , nil )
174
212
if err != nil {
175
213
if * verbose {
176
214
fmt .Println ("Failed to reset the board, please reset maually" )
0 commit comments