Skip to content

Commit 7d0fbf8

Browse files
authored
Manage ESP32 OTA (#133)
1 parent be00cb0 commit 7d0fbf8

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed

Diff for: command/ota/generate.go

+25-6
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@ import (
2222
"fmt"
2323
"io/ioutil"
2424
"os"
25+
"strings"
2526

2627
inota "github.com/arduino/arduino-cloud-cli/internal/ota"
2728
)
2829

2930
var (
30-
arduinoVendorID = "2341"
31-
fqbnToPID = map[string]string{
31+
arduinoVendorID = "2341"
32+
arduinoFqbnToPID = map[string]string{
3233
"arduino:samd:nano_33_iot": "8057",
3334
"arduino:samd:mkr1000": "804E",
3435
"arduino:samd:mkrgsm1400": "8052",
@@ -39,13 +40,31 @@ var (
3940
"arduino:mbed_nicla:nicla_vision": "025F",
4041
"arduino:mbed_opta:opta": "0064",
4142
}
43+
esp32MagicNumberPart1 = "4553"
44+
esp32MagicNumberPart2 = "5033"
4245
)
4346

4447
// Generate takes a .bin file and generates a .ota file.
4548
func Generate(binFile string, outFile string, fqbn string) error {
46-
productID, ok := fqbnToPID[fqbn]
47-
if !ok {
48-
return errors.New("fqbn not valid")
49+
50+
// We are going to put a magic number in the ota .bin file, the fw will check the magic number once the binary is received
51+
var magicNumberPart1, magicNumberPart2 string
52+
53+
// The ota update is available for Arduino boards and ESP32 boards
54+
55+
// Esp32 boards have a wide range of vid and pid, we don't map all of them
56+
// If the fqbn is the one of an ESP32 board, we force a default magic number that matches the same default expected on the fw side
57+
if strings.HasPrefix(fqbn, "esp32") {
58+
magicNumberPart1 = esp32MagicNumberPart1
59+
magicNumberPart2 = esp32MagicNumberPart2
60+
} else {
61+
//For Arduino Boards we use vendorId and productID to form the magic number
62+
magicNumberPart1 = arduinoVendorID
63+
productID, ok := arduinoFqbnToPID[fqbn]
64+
if !ok {
65+
return errors.New("fqbn not valid")
66+
}
67+
magicNumberPart2 = productID
4968
}
5069

5170
data, err := ioutil.ReadFile(binFile)
@@ -59,7 +78,7 @@ func Generate(binFile string, outFile string, fqbn string) error {
5978
}
6079
defer out.Close()
6180

62-
enc := inota.NewEncoder(out, arduinoVendorID, productID)
81+
enc := inota.NewEncoder(out, magicNumberPart1, magicNumberPart2)
6382
err = enc.Encode(data)
6483
if err != nil {
6584
return fmt.Errorf("failed to encode binary file: %w", err)

Diff for: internal/ota/encoder.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,18 @@ type Encoder struct {
3333
w io.Writer
3434

3535
// vendorID is the ID of the board vendor.
36-
vendorID string
36+
magicNumberPart1 string
3737

3838
// productID is the ID of the board model.
39-
productID string
39+
magicNumberPart2 string
4040
}
4141

4242
// NewEncoder creates a new ota encoder.
43-
func NewEncoder(w io.Writer, vendorID, productID string) *Encoder {
43+
func NewEncoder(w io.Writer, magicNumberPart1, magicNumberPart2 string) *Encoder {
4444
return &Encoder{
45-
w: w,
46-
vendorID: vendorID,
47-
productID: productID,
45+
w: w,
46+
magicNumberPart1: magicNumberPart1,
47+
magicNumberPart2: magicNumberPart2,
4848
}
4949
}
5050

@@ -53,17 +53,17 @@ func NewEncoder(w io.Writer, vendorID, productID string) *Encoder {
5353
func (e *Encoder) Encode(data []byte) error {
5454
// Compute the magic number (VID/PID)
5555
magicNumber := make([]byte, 4)
56-
vid, err := strconv.ParseUint(e.vendorID, 16, 16)
56+
magicNumberPart1, err := strconv.ParseUint(e.magicNumberPart1, 16, 16)
5757
if err != nil {
5858
return fmt.Errorf("cannot parse vendorID: %w", err)
5959
}
60-
pid, err := strconv.ParseUint(e.productID, 16, 16)
60+
magicNumberPart2, err := strconv.ParseUint(e.magicNumberPart2, 16, 16)
6161
if err != nil {
6262
return fmt.Errorf("cannot parse productID: %w", err)
6363
}
6464

65-
binary.LittleEndian.PutUint16(magicNumber[0:2], uint16(pid))
66-
binary.LittleEndian.PutUint16(magicNumber[2:4], uint16(vid))
65+
binary.LittleEndian.PutUint16(magicNumber[0:2], uint16(magicNumberPart2))
66+
binary.LittleEndian.PutUint16(magicNumber[2:4], uint16(magicNumberPart1))
6767

6868
// Version field (byte array of size 8)
6969
version := Version{

0 commit comments

Comments
 (0)