-
-
Notifications
You must be signed in to change notification settings - Fork 5
Add device create-lora command #64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8378986
Add device create-lora command
polldo e912a25
Update mocks
polldo d2b78dd
Add lora provisioning binaries
polldo fa6283d
Update cli/device/createlora.go
45ad86e
Update command/device/createlora.go
215b2ef
Update command/device/createlora.go
8386d34
Update command/device/createlora.go
b099590
Replace panic with error
polldo 51b1db0
Update readme
polldo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// This file is part of arduino-cloud-cli. | ||
// | ||
// Copyright (C) 2021 ARDUINO SA (http://www.arduino.cc/) | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as published | ||
// by the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
package device | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/arduino/arduino-cli/cli/errorcodes" | ||
"github.com/arduino/arduino-cli/cli/feedback" | ||
"github.com/arduino/arduino-cloud-cli/command/device" | ||
"github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var createLoraFlags struct { | ||
port string | ||
name string | ||
fqbn string | ||
frequencyPlan string | ||
} | ||
|
||
func initCreateLoraCommand() *cobra.Command { | ||
createLoraCommand := &cobra.Command{ | ||
Use: "create-lora", | ||
Short: "Create a LoRa device", | ||
Long: "Create a LoRa device for Arduino IoT Cloud", | ||
Run: runCreateLoraCommand, | ||
} | ||
createLoraCommand.Flags().StringVarP(&createLoraFlags.port, "port", "p", "", "Device port") | ||
createLoraCommand.Flags().StringVarP(&createLoraFlags.name, "name", "n", "", "Device name") | ||
createLoraCommand.Flags().StringVarP(&createLoraFlags.fqbn, "fqbn", "b", "", "Device fqbn") | ||
createLoraCommand.Flags().StringVarP(&createLoraFlags.frequencyPlan, "frequency-plan", "f", "", | ||
"ID of the LoRa frequency plan to use. Run the 'device list-frequency-plans' command to obtain a list of valid plans.") | ||
createLoraCommand.MarkFlagRequired("name") | ||
createLoraCommand.MarkFlagRequired("frequency-plan") | ||
return createLoraCommand | ||
} | ||
|
||
func runCreateLoraCommand(cmd *cobra.Command, args []string) { | ||
logrus.Infof("Creating LoRa device with name %s", createLoraFlags.name) | ||
|
||
params := &device.CreateLoraParams{ | ||
CreateParams: device.CreateParams{ | ||
Name: createLoraFlags.name, | ||
}, | ||
FrequencyPlan: createLoraFlags.frequencyPlan, | ||
} | ||
if createLoraFlags.port != "" { | ||
params.Port = &createLoraFlags.port | ||
} | ||
if createLoraFlags.fqbn != "" { | ||
params.Fqbn = &createLoraFlags.fqbn | ||
} | ||
|
||
dev, err := device.CreateLora(params) | ||
if err != nil { | ||
feedback.Errorf("Error during device create-lora: %v", err) | ||
os.Exit(errorcodes.ErrGeneric) | ||
} | ||
|
||
feedback.PrintResult(createLoraResult{dev}) | ||
} | ||
|
||
type createLoraResult struct { | ||
device *device.DeviceLoraInfo | ||
} | ||
|
||
func (r createLoraResult) Data() interface{} { | ||
return r.device | ||
} | ||
|
||
func (r createLoraResult) String() string { | ||
return fmt.Sprintf( | ||
"name: %s\nid: %s\nboard: %s\nserial-number: %s\nfqbn: %s"+ | ||
"\napp-eui: %s\napp-key: %s\neui: %s", | ||
r.device.Name, | ||
r.device.ID, | ||
r.device.Board, | ||
r.device.Serial, | ||
r.device.FQBN, | ||
r.device.AppEUI, | ||
r.device.AppKey, | ||
r.device.EUI, | ||
) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
package device | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"time" | ||
|
||
"github.com/arduino/arduino-cloud-cli/arduino/cli" | ||
"github.com/arduino/arduino-cloud-cli/internal/config" | ||
"github.com/arduino/arduino-cloud-cli/internal/iot" | ||
iotclient "github.com/arduino/iot-client-go" | ||
"github.com/sirupsen/logrus" | ||
"go.bug.st/serial" | ||
) | ||
|
||
const ( | ||
deveuiUploadAttempts = 3 | ||
deveuiUploadWait = 1000 | ||
|
||
serialEUIAttempts = 4 | ||
serialEUIWait = 2000 | ||
serialEUITimeout = 3500 | ||
serialEUIBaudrate = 9600 | ||
|
||
// dev-eui is an IEEE EUI64 address, so it must have length of 8 bytes. | ||
// It's retrieved as hexadecimal string, thus 16 chars are expected | ||
deveuiLength = 16 | ||
) | ||
|
||
// DeviceLoraInfo contains the most interesting | ||
// parameters of an Arduino IoT Cloud LoRa device. | ||
type DeviceLoraInfo struct { | ||
DeviceInfo | ||
AppEUI string `json:"app-eui"` | ||
AppKey string `json:"app-key"` | ||
EUI string `json:"eui"` | ||
} | ||
|
||
// CreateLoRaParams contains the parameters needed | ||
// to provision a LoRa device. | ||
type CreateLoraParams struct { | ||
CreateParams | ||
FrequencyPlan string | ||
} | ||
|
||
// CreateLora command is used to provision a new LoRa arduino device | ||
// and to add it to Arduino IoT Cloud. | ||
func CreateLora(params *CreateLoraParams) (*DeviceLoraInfo, error) { | ||
comm, err := cli.NewCommander() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ports, err := comm.BoardList() | ||
if err != nil { | ||
return nil, err | ||
} | ||
board := boardFromPorts(ports, ¶ms.CreateParams) | ||
if board == nil { | ||
err = errors.New("no board found") | ||
return nil, err | ||
} | ||
|
||
bin, err := deveuiBinary(board.fqbn) | ||
if err != nil { | ||
return nil, fmt.Errorf("fqbn not supported for LoRa provisioning: %w", err) | ||
} | ||
|
||
logrus.Infof("%s", "Uploading deveui sketch on the LoRa board") | ||
errMsg := "Error while uploading the LoRa provisioning binary" | ||
err = retry(deveuiUploadAttempts, deveuiUploadWait*time.Millisecond, errMsg, func() error { | ||
return comm.UploadBin(board.fqbn, bin, board.port) | ||
}) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to upload LoRa provisioning binary: %w", err) | ||
} | ||
|
||
eui, err := extractEUI(board.port) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
conf, err := config.Retrieve() | ||
if err != nil { | ||
return nil, err | ||
} | ||
iotClient, err := iot.NewClient(conf.Client, conf.Secret) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
logrus.Info("Creating a new device on the cloud") | ||
dev, err := iotClient.DeviceLoraCreate(params.Name, board.serial, board.dType, eui, params.FrequencyPlan) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
devInfo, err := getDeviceLoraInfo(iotClient, dev) | ||
if err != nil { | ||
errDel := iotClient.DeviceDelete(dev.DeviceId) | ||
if errDel != nil { // Oh no | ||
return nil, fmt.Errorf( | ||
"device was successfully provisioned and configured on IoT-API but " + | ||
"now we can't fetch its information nor delete it - please check " + | ||
"it on the web application.\n\nFetch error: " + err.Error() + | ||
"\nDeletion error: " + errDel.Error(), | ||
) | ||
} | ||
return nil, fmt.Errorf("%s: %w", "cannot provision LoRa device", err) | ||
} | ||
return devInfo, nil | ||
} | ||
|
||
// deveuiBinary gets the absolute path of the deveui binary corresponding to the | ||
// provisioned board's fqbn. It is contained in the local binaries folder. | ||
func deveuiBinary(fqbn string) (string, error) { | ||
// Use local binaries until they are uploaded online | ||
bin := filepath.Join("./binaries/", "getdeveui."+strings.ReplaceAll(fqbn, ":", ".")+".bin") | ||
bin, err := filepath.Abs(bin) | ||
if err != nil { | ||
return "", fmt.Errorf("getting the deveui binary: %w", err) | ||
} | ||
if _, err := os.Stat(bin); os.IsNotExist(err) { | ||
err = fmt.Errorf("%s: %w", "deveui binary not found", err) | ||
return "", err | ||
} | ||
return bin, nil | ||
} | ||
|
||
// extractEUI extracts the EUI from the provisioned lora board. | ||
func extractEUI(port string) (string, error) { | ||
var ser serial.Port | ||
|
||
logrus.Infof("%s\n", "Connecting to the board through serial port") | ||
errMsg := "Error while connecting to the board" | ||
err := retry(serialEUIAttempts, serialEUIWait*time.Millisecond, errMsg, func() error { | ||
var err error | ||
ser, err = serial.Open(port, &serial.Mode{BaudRate: serialEUIBaudrate}) | ||
return err | ||
polldo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to extract deveui from the board: %w", err) | ||
} | ||
|
||
err = ser.SetReadTimeout(serialEUITimeout * time.Millisecond) | ||
if err != nil { | ||
return "", fmt.Errorf("setting serial read timeout: %w", err) | ||
} | ||
|
||
buff := make([]byte, deveuiLength) | ||
n, err := ser.Read(buff) | ||
if err != nil { | ||
return "", fmt.Errorf("reading from serial: %w", err) | ||
} | ||
|
||
if n < deveuiLength { | ||
return "", errors.New("cannot read eui from the device") | ||
} | ||
eui := string(buff) | ||
return eui, nil | ||
} | ||
|
||
func getDeviceLoraInfo(iotClient iot.Client, loraDev *iotclient.ArduinoLoradevicev1) (*DeviceLoraInfo, error) { | ||
dev, err := iotClient.DeviceShow(loraDev.DeviceId) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot retrieve device from the cloud: %w", err) | ||
} | ||
|
||
devInfo := &DeviceLoraInfo{ | ||
DeviceInfo: DeviceInfo{ | ||
Name: dev.Name, | ||
ID: dev.Id, | ||
Board: dev.Type, | ||
Serial: dev.Serial, | ||
FQBN: dev.Fqbn, | ||
}, | ||
AppEUI: loraDev.AppEui, | ||
AppKey: loraDev.AppKey, | ||
EUI: loraDev.Eui, | ||
} | ||
return devInfo, nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.