2018-09-20 11:22:03 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-09-26 12:05:55 +02:00
|
|
|
"context"
|
2018-09-20 11:22:03 +02:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"strings"
|
2018-09-26 12:05:55 +02:00
|
|
|
"time"
|
2018-09-20 11:22:03 +02:00
|
|
|
|
2018-09-21 12:31:52 +02:00
|
|
|
"forge.cadoles.com/Pyxis/orion/emlid"
|
|
|
|
"forge.cadoles.com/Pyxis/orion/emlid/updater"
|
2018-09-20 11:22:03 +02:00
|
|
|
)
|
|
|
|
|
2018-10-16 12:29:46 +02:00
|
|
|
const stepSleep = 5
|
|
|
|
|
2018-09-20 11:22:03 +02:00
|
|
|
const (
|
|
|
|
phaseConfigureWifi = "configure-wifi"
|
|
|
|
phaseUpdateThenReboot = "update-then-reboot"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
phase = phaseConfigureWifi
|
|
|
|
host = "192.168.42.1"
|
|
|
|
ssid = ""
|
2018-09-21 12:31:52 +02:00
|
|
|
security = string(updater.SecurityWPAPSK)
|
2018-09-20 11:22:03 +02:00
|
|
|
password = ""
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
phases := []string{phaseConfigureWifi, phaseUpdateThenReboot}
|
|
|
|
flag.StringVar(
|
|
|
|
&phase, "phase", phase,
|
|
|
|
fmt.Sprintf("The configuration phase. Available: %v", strings.Join(phases, ", ")),
|
|
|
|
)
|
|
|
|
flag.StringVar(&host, "host", host, "ReachRS module host")
|
|
|
|
flag.StringVar(&ssid, "ssid", ssid, "The WiFi SSID to connect the module to")
|
|
|
|
flag.StringVar(&security, "security", security, "The WiFi security algorithm")
|
|
|
|
flag.StringVar(&password, "password", password, "The WiFi password")
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
switch phase {
|
|
|
|
case phaseConfigureWifi:
|
|
|
|
configureWifi()
|
|
|
|
case phaseUpdateThenReboot:
|
|
|
|
updateThenReboot()
|
|
|
|
default:
|
|
|
|
log.Fatalf("unknown phase '%s'", phase)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("done")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-09-21 12:31:52 +02:00
|
|
|
func connect() *updater.Client {
|
2018-09-20 11:22:03 +02:00
|
|
|
|
2018-09-21 12:31:52 +02:00
|
|
|
c := updater.NewClient(
|
|
|
|
emlid.WithEndpoint(host, 80),
|
2018-09-20 11:22:03 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
log.Printf("connecting to module '%s'", host)
|
2018-09-21 12:31:52 +02:00
|
|
|
if err := c.Connect(); err != nil {
|
2018-09-20 11:22:03 +02:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("connected")
|
|
|
|
|
2018-09-21 12:31:52 +02:00
|
|
|
return c
|
2018-09-20 11:22:03 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func configureWifi() {
|
|
|
|
|
|
|
|
if ssid == "" {
|
|
|
|
log.Fatal("you must provide a WiFi SSID with the -ssid flag")
|
|
|
|
}
|
|
|
|
|
2018-09-21 12:31:52 +02:00
|
|
|
c := connect()
|
|
|
|
defer c.Close()
|
2018-09-20 11:22:03 +02:00
|
|
|
|
2018-09-26 12:05:55 +02:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2018-09-20 11:22:03 +02:00
|
|
|
log.Println("checking module status")
|
2018-09-26 12:05:55 +02:00
|
|
|
|
|
|
|
ctx, testResultsCancel := context.WithTimeout(ctx, 5*time.Second)
|
|
|
|
defer testResultsCancel()
|
|
|
|
results, err := c.TestResults(ctx)
|
2018-09-20 11:22:03 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("device: '%s'", results.Device)
|
|
|
|
log.Printf("lora activated ? %v", results.Lora)
|
|
|
|
log.Printf("mpu activated ? %v", results.MPU)
|
|
|
|
log.Printf("stc activated ? %v", results.STC)
|
|
|
|
log.Printf("ublox activated ? %v", results.UBlox)
|
|
|
|
|
2018-10-16 12:29:46 +02:00
|
|
|
sleep(stepSleep)
|
|
|
|
|
2018-09-20 11:22:03 +02:00
|
|
|
log.Printf("adding wifi network '%s'", ssid)
|
|
|
|
|
2018-09-26 12:05:55 +02:00
|
|
|
ctx, addWifiCancel := context.WithTimeout(ctx, 5*time.Second)
|
|
|
|
defer addWifiCancel()
|
|
|
|
done, err := c.AddWifiNetwork(ctx, ssid, updater.WifiSecurity(security), password)
|
2018-09-20 11:22:03 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !done {
|
|
|
|
log.Fatal("couldnt add wifi network")
|
|
|
|
}
|
|
|
|
|
2018-10-16 12:29:46 +02:00
|
|
|
sleep(stepSleep)
|
|
|
|
|
2018-09-20 11:22:03 +02:00
|
|
|
log.Println("connecting module to wifi network")
|
2018-09-26 12:05:55 +02:00
|
|
|
ctx, joinWifiCancel := context.WithTimeout(ctx, 5*time.Second)
|
|
|
|
defer joinWifiCancel()
|
|
|
|
if err := c.JoinWifiNetwork(ctx, ssid, true); err != nil {
|
2018-09-20 11:22:03 +02:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
log.Printf("you can now switch to the wifi network and start phase '%s'", phaseUpdateThenReboot)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateThenReboot() {
|
2018-09-21 12:31:52 +02:00
|
|
|
|
|
|
|
c := connect()
|
|
|
|
defer c.Close()
|
2018-09-20 11:22:03 +02:00
|
|
|
|
2018-10-16 12:29:46 +02:00
|
|
|
log.Println("checking module status")
|
|
|
|
ctx, testResultsCancel := context.WithTimeout(context.Background(), 20*time.Second)
|
|
|
|
defer testResultsCancel()
|
|
|
|
results, err := c.TestResults(ctx)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error while checking device state: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("device: '%s'", results.Device)
|
|
|
|
log.Printf("lora activated ? %v", results.Lora)
|
|
|
|
log.Printf("mpu activated ? %v", results.MPU)
|
|
|
|
log.Printf("stc activated ? %v", results.STC)
|
|
|
|
log.Printf("ublox activated ? %v", results.UBlox)
|
|
|
|
|
|
|
|
sleep(stepSleep)
|
2018-09-26 12:05:55 +02:00
|
|
|
|
2018-09-20 11:22:03 +02:00
|
|
|
log.Println("checking time sync")
|
2018-10-16 12:29:46 +02:00
|
|
|
ctx, timeSyncedCancel := context.WithTimeout(context.Background(), 20*time.Second)
|
2018-09-26 12:05:55 +02:00
|
|
|
defer timeSyncedCancel()
|
|
|
|
synced, err := c.TimeSynced(ctx)
|
2018-09-20 11:22:03 +02:00
|
|
|
if err != nil {
|
2018-10-16 12:29:46 +02:00
|
|
|
log.Fatalf("error while checking time sync status: %s", err)
|
2018-09-20 11:22:03 +02:00
|
|
|
}
|
|
|
|
log.Printf("time synced ? %v", synced)
|
|
|
|
|
2018-10-16 12:29:46 +02:00
|
|
|
sleep(stepSleep)
|
|
|
|
|
|
|
|
log.Println("checking for upgrade")
|
|
|
|
ctx, upgradeAvailableCancel := context.WithTimeout(context.Background(), 20*time.Second)
|
|
|
|
defer upgradeAvailableCancel()
|
|
|
|
_, _, err = c.ReceiverUpgradeAvailable(ctx)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error while checking for upgrade: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sleep(stepSleep)
|
|
|
|
|
2018-09-20 11:22:03 +02:00
|
|
|
log.Println("checking reachview version")
|
2018-10-16 12:29:46 +02:00
|
|
|
ctx, reachviewVersionCancel := context.WithTimeout(context.Background(), 20*time.Second)
|
2018-09-26 12:05:55 +02:00
|
|
|
defer reachviewVersionCancel()
|
|
|
|
version, err := c.ReachViewVersion(ctx)
|
2018-09-20 11:22:03 +02:00
|
|
|
if err != nil {
|
2018-10-16 12:29:46 +02:00
|
|
|
log.Fatalf("error while checking version: %s", err)
|
2018-09-20 11:22:03 +02:00
|
|
|
}
|
|
|
|
log.Printf("reachview version ? '%s'", version)
|
|
|
|
|
2018-10-16 12:29:46 +02:00
|
|
|
sleep(stepSleep)
|
2018-09-20 11:22:03 +02:00
|
|
|
|
2018-10-16 12:29:46 +02:00
|
|
|
log.Println("skipping update")
|
|
|
|
if err := c.SkipUpdate(); err != nil {
|
|
|
|
log.Fatalf("error while skipping update: %s", err)
|
2018-09-20 11:22:03 +02:00
|
|
|
}
|
|
|
|
|
2018-10-16 12:29:46 +02:00
|
|
|
sleep(stepSleep)
|
|
|
|
|
2018-09-20 11:22:03 +02:00
|
|
|
log.Println("rebooting device")
|
2018-10-16 12:29:46 +02:00
|
|
|
ctx, rebootCancel := context.WithTimeout(context.Background(), 20*time.Second)
|
2018-09-26 12:05:55 +02:00
|
|
|
defer rebootCancel()
|
|
|
|
if err := c.RebootNow(ctx, true); err != nil {
|
2018-10-16 12:29:46 +02:00
|
|
|
log.Fatalf("error while rebooting: %s", err)
|
2018-09-20 11:22:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-10-16 12:29:46 +02:00
|
|
|
|
|
|
|
func sleep(seconds int) {
|
|
|
|
log.Printf("sleeping for %d seconds...", seconds)
|
|
|
|
time.Sleep(time.Duration(seconds) * time.Second)
|
|
|
|
}
|