@@ -3,8 +3,9 @@ package main
33import (
44 "bufio"
55 "fmt"
6- "github.com/codeskyblue /go-sh "
6+ "github.com/mattn /go-shellwords "
77 "io"
8+ "io/ioutil"
89 "os"
910 "os/exec"
1011 "path/filepath"
@@ -21,16 +22,18 @@ func PrintlnVerbose(a ...interface{}) {
2122 }
2223}
2324
24- func main () {
25- fmt .Println ("Starting download script..." )
25+ func main_load (args []string ) {
2626
2727 // ARG 1: Path to binaries
2828 // ARG 2: BIN File to download
2929 // ARG 3: TTY port to use.
3030 // ARG 4: quiet/verbose
3131 // path may contain \ need to change all to /
3232
33- args := os .Args [1 :]
33+ if len (args ) < 4 {
34+ fmt .Println ("Not enough arguments" )
35+ os .Exit (1 )
36+ }
3437
3538 bin_path := args [0 ]
3639 dfu := bin_path + "/dfu-util"
@@ -42,6 +45,14 @@ func main() {
4245 com_port := args [2 ]
4346 verbosity := args [3 ]
4447
48+ ble_compliance_string := ""
49+ ble_compliance_offset := ""
50+ if len (args ) >= 5 {
51+ // Called by post 1.0.6 platform.txt
52+ ble_compliance_string = args [4 ]
53+ ble_compliance_offset = args [5 ]
54+ }
55+
4556 if verbosity == "quiet" {
4657 verbose = false
4758 } else {
@@ -62,54 +73,81 @@ func main() {
6273 }
6374 }
6475
65- for counter < 10 && board_found == false {
66- PrintlnVerbose ("Waiting for device..." )
67- out , err := sh .Command (dfu , dfu_flags , "-l" ).Output ()
76+ dfu_search_command := []string {dfu , dfu_flags , "-l" }
77+
78+ for counter < 100 && board_found == false {
79+ if counter % 10 == 0 {
80+ PrintlnVerbose ("Waiting for device..." )
81+ }
82+ err , found , _ := launchCommandAndWaitForOutput (dfu_search_command , "sensor_core" , false )
6883 if err != nil {
6984 fmt .Println (err )
7085 os .Exit (1 )
7186 }
72- if counter == 4 {
87+ if counter == 40 {
7388 fmt .Println ("Flashing is taking longer than expected" )
7489 fmt .Println ("Try pressing MASTER_RESET button" )
7590 }
76- if strings . Contains ( string ( out ), "sensor_core" ) {
91+ if found == true {
7792 board_found = true
7893 PrintlnVerbose ("Device found!" )
7994 break
8095 }
81- time .Sleep (1000 * time .Millisecond )
96+ time .Sleep (100 * time .Millisecond )
8297 counter ++
8398 }
8499
85100 if board_found == false {
86- fmt .Println ("ERROR: Device is not responding." )
101+ fmt .Println ("ERROR: Timed out waiting for Arduino 101 on " + com_port )
87102 os .Exit (1 )
88103 }
89104
90- dfu_download := [] string { dfu , dfu_flags , "-D" , bin_file_name , "-v" , "--alt" , "7" , "-R" }
105+ if ble_compliance_string != "" {
91106
92- oscmd := exec .Command (dfu_download [0 ], dfu_download [1 :]... )
107+ // obtain a temporary filename
108+ tmpfile , _ := ioutil .TempFile (os .TempDir (), "dfu" )
109+ tmpfile .Close ()
110+ os .Remove (tmpfile .Name ())
93111
94- tellCommandNotToSpawnShell (oscmd )
112+ // reset DFU interface counter
113+ dfu_reset_command := []string {dfu , dfu_flags , "-U" , tmpfile .Name (), "--alt" , "8" , "-K" , "1" }
95114
96- stdout , _ := oscmd .StdoutPipe ()
115+ err , _ , _ := launchCommandAndWaitForOutput (dfu_reset_command , "" , false )
116+ if err != nil {
117+ fmt .Println (err )
118+ os .Exit (1 )
119+ }
97120
98- stderr , _ := oscmd . StderrPipe ( )
121+ os . Remove ( tmpfile . Name () )
99122
100- multi := io .MultiReader (stderr , stdout )
123+ // download a piece of BLE firmware
124+ dfu_ble_dump_command := []string {dfu , dfu_flags , "-U" , tmpfile .Name (), "--alt" , "8" , "-K" , ble_compliance_offset }
101125
102- err := oscmd .Start ()
126+ err , _ , _ = launchCommandAndWaitForOutput (dfu_ble_dump_command , "" , false )
127+ if err != nil {
128+ fmt .Println (err )
129+ os .Exit (1 )
130+ }
103131
104- in := bufio .NewScanner (multi )
132+ // check for BLE library compliance
133+ PrintlnVerbose ("Verifying BLE version:" , ble_compliance_string )
134+ found := searchBLEversionInDFU (tmpfile .Name (), ble_compliance_string )
105135
106- in .Split (bufio .ScanLines )
136+ // remove the temporary file
137+ os .Remove (tmpfile .Name ())
138+
139+ if ! found {
140+ fmt .Println ("!! BLE firmware version is not in sync with CurieBLE library !!" )
141+ fmt .Println ("* Set Programmer to \" Arduino/Genuino 101 Firmware Updater\" " )
142+ fmt .Println ("* Update it using \" Burn Bootloader\" menu" )
143+ os .Exit (1 )
144+ }
145+ PrintlnVerbose ("BLE version: verified" )
107146
108- for in .Scan () {
109- PrintlnVerbose (in .Text ())
110147 }
111148
112- err = oscmd .Wait ()
149+ dfu_download := []string {dfu , dfu_flags , "-D" , bin_file_name , "-v" , "--alt" , "7" , "-R" }
150+ err , _ , _ := launchCommandAndWaitForOutput (dfu_download , "" , true )
113151
114152 if err == nil {
115153 fmt .Println ("SUCCESS: Sketch will execute in about 5 seconds." )
@@ -119,3 +157,106 @@ func main() {
119157 os .Exit (1 )
120158 }
121159}
160+
161+ func main_debug (args []string ) {
162+
163+ if len (args ) < 1 {
164+ fmt .Println ("Not enough arguments" )
165+ os .Exit (1 )
166+ }
167+
168+ verbose = true
169+
170+ type Command struct {
171+ command string
172+ background bool
173+ }
174+
175+ var commands []Command
176+
177+ fullcmdline := strings .Join (args [:], "" )
178+ temp_commands := strings .Split (fullcmdline , ";" )
179+ for _ , command := range temp_commands {
180+ background_commands := strings .Split (command , "&" )
181+ for i , command := range background_commands {
182+ var cmd Command
183+ cmd .background = (i < len (background_commands )- 1 )
184+ cmd .command = command
185+ commands = append (commands , cmd )
186+ }
187+ }
188+
189+ var err error
190+
191+ for _ , command := range commands {
192+ fmt .Println ("command: " + command .command )
193+ cmd , _ := shellwords .Parse (command .command )
194+ fmt .Println (cmd )
195+ if command .background == false {
196+ err , _ , _ = launchCommandAndWaitForOutput (cmd , "" , true )
197+ } else {
198+ err , _ = launchCommandBackground (cmd , "" , true )
199+ }
200+ if err != nil {
201+ fmt .Println ("ERROR: Command \" " + command .command + " \" failed" )
202+ os .Exit (1 )
203+ }
204+ }
205+ os .Exit (0 )
206+ }
207+
208+ func main () {
209+ name := os .Args [0 ]
210+ args := os .Args [1 :]
211+
212+ if strings .Contains (name , "load" ) {
213+ fmt .Println ("Starting download script..." )
214+ main_load (args )
215+ }
216+
217+ if strings .Contains (name , "debug" ) {
218+ fmt .Println ("Starting debug script..." )
219+ main_debug (args )
220+ }
221+
222+ fmt .Println ("Wrong executable name" )
223+ os .Exit (1 )
224+ }
225+
226+ func searchBLEversionInDFU (file string , string_to_search string ) bool {
227+ read , _ := ioutil .ReadFile (file )
228+ return strings .Contains (string (read ), string_to_search )
229+ }
230+
231+ func launchCommandAndWaitForOutput (command []string , stringToSearch string , print_output bool ) (error , bool , string ) {
232+ oscmd := exec .Command (command [0 ], command [1 :]... )
233+ tellCommandNotToSpawnShell (oscmd )
234+ stdout , _ := oscmd .StdoutPipe ()
235+ stderr , _ := oscmd .StderrPipe ()
236+ multi := io .MultiReader (stderr , stdout )
237+ err := oscmd .Start ()
238+ in := bufio .NewScanner (multi )
239+ in .Split (bufio .ScanLines )
240+ found := false
241+ out := ""
242+ for in .Scan () {
243+ if print_output {
244+ PrintlnVerbose (in .Text ())
245+ }
246+ out += in .Text () + "\n "
247+ if stringToSearch != "" {
248+ if strings .Contains (in .Text (), stringToSearch ) {
249+ found = true
250+ }
251+ }
252+ }
253+ err = oscmd .Wait ()
254+ return err , found , out
255+ }
256+
257+ func launchCommandBackground (command []string , stringToSearch string , print_output bool ) (error , bool ) {
258+ oscmd := exec .Command (command [0 ], command [1 :]... )
259+ tellCommandNotToSpawnShell (oscmd )
260+ err := oscmd .Start ()
261+ return err , false
262+ }
0 commit comments