Skip to content

Commit 11357de

Browse files
committed
Rework BLE FW check routine
This new version uses a patched version of dfu-utils (https://github.com/arduino/dfu-utils-cross/tree/allow_partial_dump) to extract a piece of BLE firmware and check if it contains a particular string platform.txt needs to be modified in this way: tools.arduino101load.ble.fw.string="ATP-1.0" tools.arduino101load.ble.fw.position=141312 tools.arduino101load.upload.pattern="{cmd.path}" "{runtime.tools.arduino101load.path}/x86/bin" {build.path}/{build.project_name}.bin {serial.port} "{upload.verbose}" {ble.fw.string} {ble.fw.position}
1 parent 5f69761 commit 11357de

File tree

1 file changed

+43
-25
lines changed

1 file changed

+43
-25
lines changed

main.go

+43-25
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import (
55
"fmt"
66
"github.com/mattn/go-shellwords"
77
"io"
8+
"io/ioutil"
89
"os"
910
"os/exec"
1011
"path/filepath"
11-
"regexp"
1212
"runtime"
1313
"strings"
1414
"time"
@@ -45,10 +45,12 @@ func main_load(args []string) {
4545
com_port := args[2]
4646
verbosity := args[3]
4747

48-
ble_compliance := ""
48+
ble_compliance_string := ""
49+
ble_compliance_offset := ""
4950
if len(args) >= 5 {
5051
// 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]
5254
}
5355

5456
if verbosity == "quiet" {
@@ -73,14 +75,11 @@ func main_load(args []string) {
7375

7476
dfu_search_command := []string{dfu, dfu_flags, "-l"}
7577

76-
var cmd_output string
77-
7878
for counter < 100 && board_found == false {
7979
if counter%10 == 0 {
8080
PrintlnVerbose("Waiting for device...")
8181
}
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)
8483
if err != nil {
8584
fmt.Println(err)
8685
os.Exit(1)
@@ -103,14 +102,45 @@ func main_load(args []string) {
103102
os.Exit(1)
104103
}
105104

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+
107132
// 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 {
110139
fmt.Println("BLE firmware version is not in sync with CurieBLE library")
111140
fmt.Println("Update it using \"Burn Bootloader\" menu")
112141
os.Exit(1)
113142
}
143+
114144
}
115145

116146
dfu_download := []string{dfu, dfu_flags, "-D", bin_file_name, "-v", "--alt", "7", "-R"}
@@ -190,21 +220,9 @@ func main() {
190220
os.Exit(1)
191221
}
192222

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)
208226
}
209227

210228
func launchCommandAndWaitForOutput(command []string, stringToSearch string, print_output bool) (error, bool, string) {

0 commit comments

Comments
 (0)