Skip to content

Commit 83d5253

Browse files
committed
Added no-header feature in binary upload
1 parent aefb91c commit 83d5253

File tree

6 files changed

+75
-49
lines changed

6 files changed

+75
-49
lines changed

cli/ota/encode.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ import (
3131
)
3232

3333
type encodeBinaryFlags struct {
34-
FQBN string
35-
file string
34+
FQBN string
35+
file string
3636
}
3737

3838
func initEncodeBinaryCommand() *cobra.Command {
@@ -64,8 +64,8 @@ func runEncodeCommand(flags *encodeBinaryFlags) error {
6464
}
6565

6666
params := &ota.EncodeParams{
67-
FQBN: flags.FQBN,
68-
File: flags.file,
67+
FQBN: flags.FQBN,
68+
File: flags.file,
6969
}
7070
otafile, err := ota.Encode(context.TODO(), params, cred)
7171
if err != nil {

cli/ota/massupload.go

+13-10
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@ import (
3434
)
3535

3636
type massUploadFlags struct {
37-
deviceIDs []string
38-
tags map[string]string
39-
file string
40-
deferred bool
41-
fqbn string
37+
deviceIDs []string
38+
tags map[string]string
39+
file string
40+
deferred bool
41+
fqbn string
42+
doNotApplyHeader bool
4243
}
4344

4445
func initMassUploadCommand() *cobra.Command {
@@ -64,6 +65,7 @@ func initMassUploadCommand() *cobra.Command {
6465
massUploadCommand.Flags().StringVarP(&flags.file, "file", "", "", "Binary file (.bin) to be uploaded")
6566
massUploadCommand.Flags().BoolVar(&flags.deferred, "deferred", false, "Perform a deferred OTA. It can take up to 1 week.")
6667
massUploadCommand.Flags().StringVarP(&flags.fqbn, "fqbn", "b", "", "FQBN of the devices to update")
68+
massUploadCommand.Flags().BoolVar(&flags.doNotApplyHeader, "no-header", false, "Do not apply header and compression to binary file before upload.")
6769
massUploadCommand.MarkFlagRequired("file")
6870
massUploadCommand.MarkFlagRequired("fqbn")
6971
return massUploadCommand
@@ -73,11 +75,12 @@ func runMassUploadCommand(flags *massUploadFlags) error {
7375
logrus.Infof("Uploading binary %s", flags.file)
7476

7577
params := &ota.MassUploadParams{
76-
DeviceIDs: flags.deviceIDs,
77-
Tags: flags.tags,
78-
File: flags.file,
79-
Deferred: flags.deferred,
80-
FQBN: flags.fqbn,
78+
DeviceIDs: flags.deviceIDs,
79+
Tags: flags.tags,
80+
File: flags.file,
81+
Deferred: flags.deferred,
82+
FQBN: flags.fqbn,
83+
DoNotApplyHeader: flags.doNotApplyHeader,
8184
}
8285

8386
cred, err := config.RetrieveCredentials()

cli/ota/upload.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ import (
3131
)
3232

3333
type uploadFlags struct {
34-
deviceID string
35-
file string
36-
deferred bool
34+
deviceID string
35+
file string
36+
deferred bool
37+
doNotApplyHeader bool
3738
}
3839

3940
func initUploadCommand() *cobra.Command {
@@ -52,6 +53,7 @@ func initUploadCommand() *cobra.Command {
5253
uploadCommand.Flags().StringVarP(&flags.deviceID, "device-id", "d", "", "Device ID")
5354
uploadCommand.Flags().StringVarP(&flags.file, "file", "", "", "Binary file (.bin) to be uploaded")
5455
uploadCommand.Flags().BoolVar(&flags.deferred, "deferred", false, "Perform a deferred OTA. It can take up to 1 week.")
56+
uploadCommand.Flags().BoolVar(&flags.doNotApplyHeader, "no-header", false, "Do not apply header and compression to binary file before upload.")
5557
uploadCommand.MarkFlagRequired("device-id")
5658
uploadCommand.MarkFlagRequired("file")
5759
return uploadCommand
@@ -66,9 +68,10 @@ func runUploadCommand(flags *uploadFlags) error {
6668
}
6769

6870
params := &ota.UploadParams{
69-
DeviceID: flags.deviceID,
70-
File: flags.file,
71-
Deferred: flags.deferred,
71+
DeviceID: flags.deviceID,
72+
File: flags.file,
73+
Deferred: flags.deferred,
74+
DoNotApplyHeader: flags.doNotApplyHeader,
7275
}
7376
err = ota.Upload(context.TODO(), params, cred)
7477
if err != nil {

command/ota/encode.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ import (
2626
)
2727

2828
type EncodeParams struct {
29-
FQBN string
30-
File string
29+
FQBN string
30+
File string
3131
}
3232

3333
// Encode command is used to encode a firmware OTA

command/ota/massupload.go

+24-14
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"context"
2222
"errors"
2323
"fmt"
24-
"io/ioutil"
2524
"os"
2625
"path/filepath"
2726

@@ -37,11 +36,12 @@ const (
3736
// MassUploadParams contains the parameters needed to
3837
// perform a Mass OTA upload.
3938
type MassUploadParams struct {
40-
DeviceIDs []string
41-
Tags map[string]string
42-
File string
43-
Deferred bool
44-
FQBN string
39+
DeviceIDs []string
40+
Tags map[string]string
41+
File string
42+
Deferred bool
43+
DoNotApplyHeader bool
44+
FQBN string
4545
}
4646

4747
// Result of an ota upload on a device.
@@ -59,17 +59,27 @@ func MassUpload(ctx context.Context, params *MassUploadParams, cred *config.Cred
5959
return nil, errors.New("cannot use both DeviceIDs and Tags. only one of them should be not nil")
6060
}
6161

62-
// Generate .ota file
63-
otaDir, err := ioutil.TempDir("", "")
62+
_, err := os.Stat(params.File)
6463
if err != nil {
65-
return nil, fmt.Errorf("%s: %w", "cannot create temporary folder", err)
64+
return nil, fmt.Errorf("file %s does not exists: %w", params.File, err)
6665
}
67-
otaFile := filepath.Join(otaDir, "temp.ota")
68-
defer os.RemoveAll(otaDir)
6966

70-
err = Generate(params.File, otaFile, params.FQBN)
71-
if err != nil {
72-
return nil, fmt.Errorf("%s: %w", "cannot generate .ota file", err)
67+
// Generate .ota file
68+
var otaFile string
69+
if params.DoNotApplyHeader {
70+
otaFile = params.File
71+
} else {
72+
otaDir, err := os.MkdirTemp("", "")
73+
if err != nil {
74+
return nil, fmt.Errorf("%s: %w", "cannot create temporary folder", err)
75+
}
76+
otaFile := filepath.Join(otaDir, "temp.ota")
77+
defer os.RemoveAll(otaDir)
78+
79+
err = Generate(params.File, otaFile, params.FQBN)
80+
if err != nil {
81+
return nil, fmt.Errorf("%s: %w", "cannot generate .ota file", err)
82+
}
7383
}
7484

7585
iotClient, err := iot.NewClient(cred)

command/ota/upload.go

+23-13
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ package ota
2020
import (
2121
"context"
2222
"fmt"
23-
"io/ioutil"
2423
"os"
2524
"path/filepath"
2625

@@ -38,14 +37,20 @@ const (
3837
// UploadParams contains the parameters needed to
3938
// perform an OTA upload.
4039
type UploadParams struct {
41-
DeviceID string
42-
File string
43-
Deferred bool
40+
DeviceID string
41+
File string
42+
Deferred bool
43+
DoNotApplyHeader bool
4444
}
4545

4646
// Upload command is used to upload a firmware OTA,
4747
// on a device of Arduino IoT Cloud.
4848
func Upload(ctx context.Context, params *UploadParams, cred *config.Credentials) error {
49+
_, err := os.Stat(params.File)
50+
if err != nil {
51+
return fmt.Errorf("file %s does not exists: %w", params.File, err)
52+
}
53+
4954
iotClient, err := iot.NewClient(cred)
5055
if err != nil {
5156
return err
@@ -56,16 +61,21 @@ func Upload(ctx context.Context, params *UploadParams, cred *config.Credentials)
5661
return err
5762
}
5863

59-
otaDir, err := ioutil.TempDir("", "")
60-
if err != nil {
61-
return fmt.Errorf("%s: %w", "cannot create temporary folder", err)
62-
}
63-
otaFile := filepath.Join(otaDir, "temp.ota")
64-
defer os.RemoveAll(otaDir)
64+
var otaFile string
65+
if params.DoNotApplyHeader {
66+
otaFile = params.File
67+
} else {
68+
otaDir, err := os.MkdirTemp("", "")
69+
if err != nil {
70+
return fmt.Errorf("%s: %w", "cannot create temporary folder", err)
71+
}
72+
otaFile = filepath.Join(otaDir, "temp.ota")
73+
defer os.RemoveAll(otaDir)
6574

66-
err = Generate(params.File, otaFile, dev.Fqbn)
67-
if err != nil {
68-
return fmt.Errorf("%s: %w", "cannot generate .ota file", err)
75+
err = Generate(params.File, otaFile, dev.Fqbn)
76+
if err != nil {
77+
return fmt.Errorf("%s: %w", "cannot generate .ota file", err)
78+
}
6979
}
7080

7181
file, err := os.Open(otaFile)

0 commit comments

Comments
 (0)