|
| 1 | +package device |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/arduino/arduino-cloud-cli/arduino/cli" |
| 12 | + "github.com/arduino/arduino-cloud-cli/internal/config" |
| 13 | + "github.com/arduino/arduino-cloud-cli/internal/iot" |
| 14 | + iotclient "github.com/arduino/iot-client-go" |
| 15 | + "github.com/sirupsen/logrus" |
| 16 | + "go.bug.st/serial" |
| 17 | +) |
| 18 | + |
| 19 | +const ( |
| 20 | + deveuiUploadAttempts = 3 |
| 21 | + deveuiUploadWait = 1000 |
| 22 | + |
| 23 | + serialEUIAttempts = 4 |
| 24 | + serialEUIWait = 2000 |
| 25 | + serialEUITimeout = 3500 |
| 26 | + serialEUIBaudrate = 9600 |
| 27 | + |
| 28 | + // dev-eui is an IEEE EUI64 address, so it must have length of 8 bytes. |
| 29 | + // It's retrieved as hexadecimal string, thus 16 chars are expected |
| 30 | + deveuiLength = 16 |
| 31 | +) |
| 32 | + |
| 33 | +// DeviceLoraInfo contains the most interesting |
| 34 | +// parameters of an Arduino IoT Cloud LoRa device. |
| 35 | +type DeviceLoraInfo struct { |
| 36 | + DeviceInfo |
| 37 | + AppEUI string `json:"app-eui"` |
| 38 | + AppKey string `json:"app-key"` |
| 39 | + EUI string `json:"eui"` |
| 40 | +} |
| 41 | + |
| 42 | +// CreateLoRaParams contains the parameters needed |
| 43 | +// to provision a LoRa device. |
| 44 | +type CreateLoraParams struct { |
| 45 | + CreateParams |
| 46 | + FrequencyPlan string |
| 47 | +} |
| 48 | + |
| 49 | +// CreateLora command is used to provision a new LoRa arduino device |
| 50 | +// and to add it to Arduino IoT Cloud. |
| 51 | +func CreateLora(params *CreateLoraParams) (*DeviceLoraInfo, error) { |
| 52 | + comm, err := cli.NewCommander() |
| 53 | + if err != nil { |
| 54 | + return nil, err |
| 55 | + } |
| 56 | + |
| 57 | + ports, err := comm.BoardList() |
| 58 | + if err != nil { |
| 59 | + return nil, err |
| 60 | + } |
| 61 | + board := boardFromPorts(ports, ¶ms.CreateParams) |
| 62 | + if board == nil { |
| 63 | + err = errors.New("no board found") |
| 64 | + return nil, err |
| 65 | + } |
| 66 | + |
| 67 | + bin, err := deveuiBinary(board.fqbn) |
| 68 | + if err != nil { |
| 69 | + return nil, fmt.Errorf("fqbn not supported for LoRa provisioning: %w", err) |
| 70 | + } |
| 71 | + |
| 72 | + logrus.Infof("%s", "Uploading deveui sketch on the LoRa board") |
| 73 | + errMsg := "Error while uploading the LoRa provisioning binary" |
| 74 | + err = retry(deveuiUploadAttempts, deveuiUploadWait*time.Millisecond, errMsg, func() error { |
| 75 | + return comm.UploadBin(board.fqbn, bin, board.port) |
| 76 | + }) |
| 77 | + if err != nil { |
| 78 | + return nil, fmt.Errorf("failed to upload LoRa provisioning binary: %w", err) |
| 79 | + } |
| 80 | + |
| 81 | + eui, err := extractEUI(board.port) |
| 82 | + if err != nil { |
| 83 | + return nil, err |
| 84 | + } |
| 85 | + |
| 86 | + conf, err := config.Retrieve() |
| 87 | + if err != nil { |
| 88 | + return nil, err |
| 89 | + } |
| 90 | + iotClient, err := iot.NewClient(conf.Client, conf.Secret) |
| 91 | + if err != nil { |
| 92 | + return nil, err |
| 93 | + } |
| 94 | + |
| 95 | + logrus.Info("Creating a new device on the cloud") |
| 96 | + dev, err := iotClient.DeviceLoraCreate(params.Name, board.serial, board.dType, eui, params.FrequencyPlan) |
| 97 | + if err != nil { |
| 98 | + return nil, err |
| 99 | + } |
| 100 | + |
| 101 | + devInfo, err := getDeviceLoraInfo(iotClient, dev) |
| 102 | + if err != nil { |
| 103 | + errDel := iotClient.DeviceDelete(dev.DeviceId) |
| 104 | + if errDel != nil { // Oh no |
| 105 | + return nil, fmt.Errorf( |
| 106 | + "device was successfully provisioned and configured on IoT-API but " + |
| 107 | + "now we can't fetch its information nor delete it - please check " + |
| 108 | + "it on the web application.\n\nFetch error: " + err.Error() + |
| 109 | + "\nDeletion error: " + errDel.Error(), |
| 110 | + ) |
| 111 | + } |
| 112 | + return nil, fmt.Errorf("%s: %w", "cannot provision LoRa device", err) |
| 113 | + } |
| 114 | + return devInfo, nil |
| 115 | +} |
| 116 | + |
| 117 | +// deveuiBinary gets the absolute path of the deveui binary corresponding to the |
| 118 | +// provisioned board's fqbn. It is contained in the local binaries folder. |
| 119 | +func deveuiBinary(fqbn string) (string, error) { |
| 120 | + // Use local binaries until they are uploaded online |
| 121 | + bin := filepath.Join("./binaries/", "getdeveui."+strings.ReplaceAll(fqbn, ":", ".")+".bin") |
| 122 | + bin, err := filepath.Abs(bin) |
| 123 | + if err != nil { |
| 124 | + return "", fmt.Errorf("getting the deveui binary: %w", err) |
| 125 | + } |
| 126 | + if _, err := os.Stat(bin); os.IsNotExist(err) { |
| 127 | + err = fmt.Errorf("%s: %w", "deveui binary not found", err) |
| 128 | + return "", err |
| 129 | + } |
| 130 | + return bin, nil |
| 131 | +} |
| 132 | + |
| 133 | +// extractEUI extracts the EUI from the provisioned lora board. |
| 134 | +func extractEUI(port string) (string, error) { |
| 135 | + var ser serial.Port |
| 136 | + |
| 137 | + logrus.Infof("%s\n", "Connecting to the board through serial port") |
| 138 | + errMsg := "Error while connecting to the board" |
| 139 | + err := retry(serialEUIAttempts, serialEUIWait*time.Millisecond, errMsg, func() error { |
| 140 | + var err error |
| 141 | + ser, err = serial.Open(port, &serial.Mode{BaudRate: serialEUIBaudrate}) |
| 142 | + return err |
| 143 | + }) |
| 144 | + if err != nil { |
| 145 | + return "", fmt.Errorf("failed to extract deveui from the board: %w", err) |
| 146 | + } |
| 147 | + |
| 148 | + err = ser.SetReadTimeout(serialEUITimeout * time.Millisecond) |
| 149 | + if err != nil { |
| 150 | + return "", fmt.Errorf("setting serial read timeout: %w", err) |
| 151 | + } |
| 152 | + |
| 153 | + buff := make([]byte, deveuiLength) |
| 154 | + n, err := ser.Read(buff) |
| 155 | + if err != nil { |
| 156 | + return "", fmt.Errorf("reading from serial: %w", err) |
| 157 | + } |
| 158 | + |
| 159 | + if n < deveuiLength { |
| 160 | + return "", errors.New("cannot read eui from the device") |
| 161 | + } |
| 162 | + eui := string(buff) |
| 163 | + return eui, nil |
| 164 | +} |
| 165 | + |
| 166 | +func getDeviceLoraInfo(iotClient iot.Client, loraDev *iotclient.ArduinoLoradevicev1) (*DeviceLoraInfo, error) { |
| 167 | + dev, err := iotClient.DeviceShow(loraDev.DeviceId) |
| 168 | + if err != nil { |
| 169 | + return nil, fmt.Errorf("cannot retrieve device from the cloud: %w", err) |
| 170 | + } |
| 171 | + |
| 172 | + devInfo := &DeviceLoraInfo{ |
| 173 | + DeviceInfo: DeviceInfo{ |
| 174 | + Name: dev.Name, |
| 175 | + ID: dev.Id, |
| 176 | + Board: dev.Type, |
| 177 | + Serial: dev.Serial, |
| 178 | + FQBN: dev.Fqbn, |
| 179 | + }, |
| 180 | + AppEUI: loraDev.AppEui, |
| 181 | + AppKey: loraDev.AppKey, |
| 182 | + EUI: loraDev.Eui, |
| 183 | + } |
| 184 | + return devInfo, nil |
| 185 | +} |
0 commit comments