@@ -5,10 +5,10 @@ import (
5
5
"fmt"
6
6
"github.com/mattn/go-shellwords"
7
7
"io"
8
+ "io/ioutil"
8
9
"os"
9
10
"os/exec"
10
11
"path/filepath"
11
- "regexp"
12
12
"runtime"
13
13
"strings"
14
14
"time"
@@ -45,10 +45,12 @@ func main_load(args []string) {
45
45
com_port := args [2 ]
46
46
verbosity := args [3 ]
47
47
48
- ble_compliance := ""
48
+ ble_compliance_string := ""
49
+ ble_compliance_offset := ""
49
50
if len (args ) >= 5 {
50
51
// Called by post 1.0.6 platform.txt
51
- ble_compliance = args [4 ]
52
+ ble_compliance_string = args [4 ]
53
+ ble_compliance_offset = args [5 ]
52
54
}
53
55
54
56
if verbosity == "quiet" {
@@ -73,14 +75,11 @@ func main_load(args []string) {
73
75
74
76
dfu_search_command := []string {dfu , dfu_flags , "-l" }
75
77
76
- var cmd_output string
77
-
78
78
for counter < 100 && board_found == false {
79
79
if counter % 10 == 0 {
80
80
PrintlnVerbose ("Waiting for device..." )
81
81
}
82
- err , found , output := launchCommandAndWaitForOutput (dfu_search_command , "sensor_core" , false )
83
- cmd_output = output
82
+ err , found , _ := launchCommandAndWaitForOutput (dfu_search_command , "sensor_core" , false )
84
83
if err != nil {
85
84
fmt .Println (err )
86
85
os .Exit (1 )
@@ -103,14 +102,45 @@ func main_load(args []string) {
103
102
os .Exit (1 )
104
103
}
105
104
106
- if ble_compliance != "" {
105
+ if ble_compliance_string != "" {
106
+
107
+ // obtain a temporary filename
108
+ tmpfile , _ := ioutil .TempFile (os .TempDir (), "dfu" )
109
+ tmpfile .Close ()
110
+ os .Remove (tmpfile .Name ())
111
+
112
+ // reset DFU interface counter
113
+ dfu_reset_command := []string {dfu , dfu_flags , "-U" , tmpfile .Name (), "--alt" , "8" , "-K" , "1" }
114
+
115
+ err , _ , _ := launchCommandAndWaitForOutput (dfu_reset_command , "" , false )
116
+ if err != nil {
117
+ fmt .Println (err )
118
+ os .Exit (1 )
119
+ }
120
+
121
+ os .Remove (tmpfile .Name ())
122
+
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 }
125
+
126
+ err , _ , _ = launchCommandAndWaitForOutput (dfu_ble_dump_command , "" , false )
127
+ if err != nil {
128
+ fmt .Println (err )
129
+ os .Exit (1 )
130
+ }
131
+
107
132
// check for BLE library compliance
108
- ble_version := extractBLEversionFromDFU (cmd_output )
109
- if ble_compliance != ble_version {
133
+ found := searchBLEversionInDFU (tmpfile .Name (), ble_compliance_string )
134
+
135
+ // remove the temporary file
136
+ os .Remove (tmpfile .Name ())
137
+
138
+ if ! found {
110
139
fmt .Println ("BLE firmware version is not in sync with CurieBLE library" )
111
140
fmt .Println ("Update it using \" Burn Bootloader\" menu" )
112
141
os .Exit (1 )
113
142
}
143
+
114
144
}
115
145
116
146
dfu_download := []string {dfu , dfu_flags , "-D" , bin_file_name , "-v" , "--alt" , "7" , "-R" }
@@ -190,21 +220,9 @@ func main() {
190
220
os .Exit (1 )
191
221
}
192
222
193
- func extractBLEversionFromDFU (command string ) string {
194
- in := bufio .NewScanner (strings .NewReader (command ))
195
- in .Split (bufio .ScanLines )
196
- for in .Scan () {
197
- if strings .Contains (in .Text (), "ble_core" ) {
198
- re := regexp .MustCompile (`ver=([0-9]+)` )
199
- ver := re .FindStringSubmatch (in .Text ())
200
- if len (ver ) > 1 {
201
- return ver [1 ]
202
- } else {
203
- return ""
204
- }
205
- }
206
- }
207
- return ""
223
+ func searchBLEversionInDFU (file string , string_to_search string ) bool {
224
+ read , _ := ioutil .ReadFile (file )
225
+ return strings .Contains (string (read ), string_to_search )
208
226
}
209
227
210
228
func launchCommandAndWaitForOutput (command []string , stringToSearch string , print_output bool ) (error , bool , string ) {
0 commit comments