2018-09-24 08:49:21 +02:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
2018-10-10 17:38:04 +02:00
|
|
|
"context"
|
2018-10-05 16:12:07 +02:00
|
|
|
"fmt"
|
2018-10-16 12:29:46 +02:00
|
|
|
"log"
|
2018-09-24 08:49:21 +02:00
|
|
|
"net/http"
|
2018-10-05 16:12:07 +02:00
|
|
|
"time"
|
2018-09-24 08:49:21 +02:00
|
|
|
|
2018-10-10 17:38:04 +02:00
|
|
|
"forge.cadoles.com/Pyxis/orion/emlid"
|
|
|
|
"forge.cadoles.com/Pyxis/orion/emlid/updater"
|
2018-09-24 17:38:17 +02:00
|
|
|
"forge.cadoles.com/Pyxis/orion/openwrt"
|
2018-09-24 08:49:21 +02:00
|
|
|
"github.com/gorilla/rpc"
|
|
|
|
"github.com/gorilla/rpc/json"
|
2018-10-10 17:38:04 +02:00
|
|
|
"github.com/pkg/errors"
|
2018-09-24 08:49:21 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// OrionService is the JSON-RPC API
|
2018-09-24 17:38:17 +02:00
|
|
|
type OrionService struct {
|
|
|
|
UCI *openwrt.UCI
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewOrionService create a new OrionService !
|
|
|
|
func NewOrionService() *OrionService {
|
|
|
|
uci := openwrt.NewUCI()
|
|
|
|
return &OrionService{
|
|
|
|
UCI: uci,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// UCIArgs argument structure for exported method OwrtListWifiDevices
|
|
|
|
type UCIArgs struct{}
|
2018-09-24 08:49:21 +02:00
|
|
|
|
2018-09-24 17:38:17 +02:00
|
|
|
// UCIResponse is the response structure for exposed method OwrtListWifiDevices
|
|
|
|
type UCIResponse struct {
|
|
|
|
Devices []map[string]string
|
2018-09-24 08:49:21 +02:00
|
|
|
}
|
|
|
|
|
2018-09-24 17:38:17 +02:00
|
|
|
// OwrtListWifiDevices offers an RPC Method to list Wifi nics in a OpenWRT device.
|
|
|
|
func (o *OrionService) OwrtListWifiDevices(r *http.Request, args *UCIArgs, reply *UCIResponse) error {
|
|
|
|
o.UCI.LoadWirelessConf()
|
|
|
|
devs := o.UCI.GetWifiDevices()
|
|
|
|
reply.Devices = devs
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListIfaceArgs argument structure for exported method OwrtListWifiDevices
|
|
|
|
type ListIfaceArgs struct{}
|
|
|
|
|
|
|
|
// ListIfaceResponse is the response structure for exposed method OwrtListWifiDevices
|
|
|
|
type ListIfaceResponse struct {
|
|
|
|
Interfaces []*openwrt.UCIWirelessInterface
|
2018-09-24 08:49:21 +02:00
|
|
|
}
|
|
|
|
|
2018-09-24 17:38:17 +02:00
|
|
|
// OwrtListWifiInterfaces offers an RPC Method to list wifi interfaces in a OpenWRT device.
|
|
|
|
func (o *OrionService) OwrtListWifiInterfaces(r *http.Request, args *ListIfaceArgs, reply *ListIfaceResponse) error {
|
|
|
|
o.UCI.LoadWirelessConf()
|
|
|
|
reply.Interfaces = o.UCI.GetWifiIfaces()
|
2018-09-24 08:49:21 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-05 16:12:07 +02:00
|
|
|
// CreateIfaceArgs argument structure for exported method OwrtCreateWifiInterface
|
|
|
|
type CreateIfaceArgs struct {
|
|
|
|
Iface *openwrt.UCIWirelessInterface
|
|
|
|
Cleanup bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateIfaceResponse argument structure for exported method OwrtCreateWifiInterface
|
|
|
|
type CreateIfaceResponse struct {
|
|
|
|
Iface *openwrt.UCIWirelessInterface
|
|
|
|
Errors []*openwrt.Action
|
|
|
|
}
|
|
|
|
|
|
|
|
// OwrtCreateWifiInterface Create a WifiInterface in openwrt
|
2018-10-08 15:39:16 +02:00
|
|
|
func (o *OrionService) OwrtCreateWifiInterface(r *http.Request,
|
|
|
|
args *CreateIfaceArgs,
|
|
|
|
reply *CreateIfaceResponse) error {
|
2018-10-05 16:12:07 +02:00
|
|
|
reply.Iface = nil
|
2018-10-10 17:38:04 +02:00
|
|
|
|
2018-10-08 15:39:16 +02:00
|
|
|
if args.Cleanup {
|
2018-10-05 16:12:07 +02:00
|
|
|
o.UCI.LoadWirelessConf()
|
|
|
|
w := o.UCI.GetWifiIfaces()
|
|
|
|
for _, iface := range w {
|
|
|
|
iface.Delete(o.UCI)
|
|
|
|
}
|
|
|
|
o.UCI.Commit()
|
|
|
|
o.UCI.Reload()
|
|
|
|
time.Sleep(3 * time.Second)
|
|
|
|
}
|
|
|
|
|
|
|
|
o.UCI.LoadWirelessConf()
|
|
|
|
create := args.Iface.Create(o.UCI)
|
|
|
|
if create.ReturnCode != 0 {
|
|
|
|
reply.Errors = append(reply.Errors, create)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
create = args.Iface.SysAdd(o.UCI)
|
|
|
|
if create.ReturnCode == 233 {
|
|
|
|
delete := args.Iface.SysDel(o.UCI)
|
|
|
|
if delete.ReturnCode != 0 {
|
|
|
|
reply.Errors = append(reply.Errors, delete)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
create = args.Iface.SysAdd(o.UCI)
|
|
|
|
if create.ReturnCode != 0 {
|
|
|
|
reply.Errors = append(reply.Errors, create)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
create = args.Iface.Up(o.UCI)
|
|
|
|
if create.ReturnCode != 0 {
|
|
|
|
reply.Errors = append(reply.Errors, create)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
create = args.Iface.Save(o.UCI)
|
|
|
|
if create.ReturnCode != 0 {
|
|
|
|
reply.Errors = append(reply.Errors, create)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
reply.Iface = args.Iface
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConnectIfaceArgs argument structure for exported method OwrtCreateWifiInterface
|
|
|
|
type ConnectIfaceArgs struct {
|
|
|
|
Iface *openwrt.UCIWirelessInterface
|
|
|
|
SSID string
|
|
|
|
Key string
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConnectIfaceResponse argument structure for exported method OwrtCreateWifiInterface
|
|
|
|
type ConnectIfaceResponse struct {
|
|
|
|
status int
|
|
|
|
Errors []string
|
|
|
|
}
|
|
|
|
|
|
|
|
// OwrtConnectWifiInterface connects a given Wifi Interface to a given SSID
|
2018-10-08 15:39:16 +02:00
|
|
|
func (o *OrionService) OwrtConnectWifiInterface(r *http.Request,
|
|
|
|
args *ConnectIfaceArgs,
|
|
|
|
reply *ConnectIfaceResponse) error {
|
2018-10-05 16:12:07 +02:00
|
|
|
o.UCI.LoadWirelessConf()
|
|
|
|
iface := o.UCI.GetWifiIface(args.Iface.Index)
|
|
|
|
cells := iface.Scan()
|
|
|
|
|
|
|
|
for _, cell := range cells {
|
|
|
|
if cell.Ssid == args.SSID {
|
|
|
|
cn := iface.Connect(o.UCI, cell, args.Key)
|
|
|
|
if cn.ReturnCode != 0 {
|
|
|
|
reply.status = cn.ReturnCode
|
|
|
|
reply.Errors = append(reply.Errors, cn.Stdout, cn.Stderr, cn.Command)
|
|
|
|
}
|
|
|
|
reply.status = 0
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
reply.status = 1
|
|
|
|
msg := fmt.Sprintf("Wifi Cell with SSID %s is not available !", args.SSID)
|
|
|
|
reply.Errors = append(reply.Errors, msg)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-10 17:38:04 +02:00
|
|
|
// OrionBox describe an fresh Orion box (base or rover)
|
|
|
|
type OrionBox struct {
|
2018-10-11 11:35:51 +02:00
|
|
|
Address string
|
|
|
|
NewAddress string
|
|
|
|
SSID string
|
|
|
|
Security string
|
|
|
|
WifiKey string
|
2018-10-10 17:38:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// OrionServer describe the Orion master server
|
|
|
|
type OrionServer struct {
|
|
|
|
Address string
|
|
|
|
SSID string
|
|
|
|
Security string
|
|
|
|
WifiKey string
|
|
|
|
ClientIface *openwrt.UCIWirelessInterface
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateOrionBoxArgs argument structure for exported method OwrtCreateWifiInterface
|
|
|
|
type UpdateOrionBoxArgs struct {
|
|
|
|
Box *OrionBox
|
|
|
|
Server *OrionServer
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateOrionBoxResponse argument structure for exported method OwrtCreateWifiInterface
|
|
|
|
type UpdateOrionBoxResponse struct {
|
|
|
|
IP string
|
|
|
|
Netmask string
|
|
|
|
Synced bool
|
|
|
|
Version string
|
2018-10-15 16:42:35 +02:00
|
|
|
Status *updater.UpdateStatus
|
|
|
|
Results *updater.TestResults
|
2018-10-10 17:38:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Connect wifi interface to a Orion box wifi hotspot!
|
|
|
|
func (o *OrionService) connectBox(box *OrionBox, server *OrionServer) error {
|
|
|
|
o.UCI.LoadWirelessConf()
|
|
|
|
if server == nil {
|
|
|
|
return fmt.Errorf("Server definition is empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
if box == nil {
|
|
|
|
return fmt.Errorf("Box definitioni is emtpy")
|
|
|
|
}
|
|
|
|
iface := server.ClientIface
|
|
|
|
cells := iface.Scan()
|
|
|
|
for _, cell := range cells {
|
|
|
|
if cell.Ssid == box.SSID {
|
|
|
|
cn := iface.Connect(o.UCI, cell, box.WifiKey)
|
|
|
|
if cn.ReturnCode != 0 {
|
|
|
|
return fmt.Errorf("%s\n%s", cn.Stdout, cn.Stderr)
|
|
|
|
}
|
|
|
|
dhcli := openwrt.NewDhcpClient(iface.SysDevName)
|
|
|
|
dhres := dhcli.AskForIP()
|
|
|
|
if dhres.CmdRes.ReturnCode != 0 {
|
|
|
|
return fmt.Errorf("%s\n%s", cn.Stdout, cn.Stderr)
|
|
|
|
}
|
2018-10-11 11:35:51 +02:00
|
|
|
re := o.UCI.Reload()
|
|
|
|
if re.ReturnCode != 0 {
|
|
|
|
return fmt.Errorf("%s\n%s", re.Stdout, re.Stderr)
|
|
|
|
}
|
2018-10-10 17:38:04 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fmt.Errorf("Wifi cell with SSID %s is not available", box.SSID)
|
|
|
|
}
|
|
|
|
|
2018-10-17 15:32:01 +02:00
|
|
|
func (o *OrionService) discoverService(rqContext context.Context) ([]emlid.Service, error) {
|
|
|
|
ctx, cancelDiscover := context.WithTimeout(rqContext, 55*time.Second)
|
|
|
|
defer cancelDiscover()
|
|
|
|
return emlid.Discover(ctx)
|
2018-10-11 11:35:51 +02:00
|
|
|
}
|
2018-10-10 17:38:04 +02:00
|
|
|
|
2018-10-17 15:32:01 +02:00
|
|
|
func (o *OrionService) connectUpdater(rqContext context.Context, box *OrionBox) (*updater.Client, error) {
|
2018-10-11 11:35:51 +02:00
|
|
|
var boxCli *updater.Client
|
2018-10-17 15:32:01 +02:00
|
|
|
service, err := o.discoverService(rqContext)
|
|
|
|
fmt.Printf("DEBUG len = %d\n", len(service))
|
2018-10-15 16:42:35 +02:00
|
|
|
if len(service) == 0 {
|
2018-10-17 15:32:01 +02:00
|
|
|
fmt.Println("DEBUG Connecting to box with EndPoint !")
|
2018-10-11 11:35:51 +02:00
|
|
|
boxCli = updater.NewClient(
|
2018-10-15 16:42:35 +02:00
|
|
|
emlid.WithEndpoint(box.Address, 80),
|
2018-10-11 11:35:51 +02:00
|
|
|
)
|
2018-10-17 15:32:01 +02:00
|
|
|
} else if err == nil {
|
|
|
|
fmt.Println("DEBUG Connecting to box with Service !")
|
2018-10-11 11:35:51 +02:00
|
|
|
boxCli = updater.NewClient(
|
2018-10-15 16:42:35 +02:00
|
|
|
emlid.WithService(service[0]),
|
2018-10-11 11:35:51 +02:00
|
|
|
)
|
2018-10-15 16:42:35 +02:00
|
|
|
} else {
|
2018-10-17 15:32:01 +02:00
|
|
|
fmt.Printf("Non mais là c'est vraiment étrange !")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
//fmt.Printf("NAME: %s, IP: %s", service[0].Name, service[0].AddrV4)
|
|
|
|
return boxCli, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// setupMasterWifi connects to the box, add wifi network and join it !
|
|
|
|
func (o *OrionService) setupMasterWifi(rqContext context.Context, box *OrionBox, server *OrionServer) error {
|
|
|
|
if err := o.connectBox(box, server); err != nil {
|
|
|
|
return errors.Wrap(err, "Connect to box failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
if server.Security == "" {
|
|
|
|
server.Security = string(updater.SecurityWPAPSK)
|
2018-10-11 11:35:51 +02:00
|
|
|
}
|
2018-10-10 17:38:04 +02:00
|
|
|
|
2018-10-17 15:32:01 +02:00
|
|
|
boxCli, err := o.connectUpdater(rqContext, box)
|
|
|
|
|
2018-10-10 17:38:04 +02:00
|
|
|
if err := boxCli.Connect(); err != nil {
|
2018-10-11 11:35:51 +02:00
|
|
|
return errors.Wrap(err, "Connecting to Box failed")
|
2018-10-10 17:38:04 +02:00
|
|
|
}
|
|
|
|
defer boxCli.Close()
|
|
|
|
|
2018-10-17 15:32:01 +02:00
|
|
|
fmt.Println("Running Tests !")
|
2018-10-15 16:42:35 +02:00
|
|
|
ctx, testResultsCancel := context.WithTimeout(rqContext, 55*time.Second)
|
|
|
|
defer testResultsCancel()
|
|
|
|
_, err = boxCli.TestResults(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Minimal test failed")
|
|
|
|
}
|
|
|
|
|
2018-10-17 15:32:01 +02:00
|
|
|
fmt.Println("Add Wifi !")
|
2018-10-11 11:35:51 +02:00
|
|
|
ctx, addWifiCancel := context.WithTimeout(rqContext, 55*time.Second)
|
2018-10-10 17:38:04 +02:00
|
|
|
defer addWifiCancel()
|
|
|
|
|
2018-10-11 11:35:51 +02:00
|
|
|
done, err := boxCli.AddWifiNetwork(ctx, server.SSID, updater.WifiSecurity(server.Security), server.WifiKey)
|
2018-10-10 17:38:04 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "AddWifiNetworkFailed")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !done {
|
|
|
|
return fmt.Errorf("Impossible to add wifi network")
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, joinWifiCancel := context.WithTimeout(ctx, 55*time.Second)
|
|
|
|
defer joinWifiCancel()
|
|
|
|
|
2018-10-17 15:32:01 +02:00
|
|
|
fmt.Println("Join Wifi !")
|
2018-10-11 11:35:51 +02:00
|
|
|
if err := boxCli.JoinWifiNetwork(ctx, server.SSID, true); err != nil {
|
2018-10-10 17:38:04 +02:00
|
|
|
return errors.Wrap(err, "Time sync failed")
|
|
|
|
}
|
2018-10-11 11:35:51 +02:00
|
|
|
return nil
|
|
|
|
}
|
2018-10-10 17:38:04 +02:00
|
|
|
|
2018-10-11 11:35:51 +02:00
|
|
|
// updateAndReboot connects to the box with the New adress and run basic tests on version dans updates
|
|
|
|
func (o *OrionService) updateAndReboot(rqContext context.Context, box *OrionBox, server *OrionServer, reply *UpdateOrionBoxResponse) error {
|
2018-10-17 15:32:01 +02:00
|
|
|
time.Sleep(3 * time.Second)
|
|
|
|
boxCli, err := o.connectUpdater(rqContext, box)
|
2018-10-11 11:35:51 +02:00
|
|
|
if err != nil {
|
2018-10-17 15:32:01 +02:00
|
|
|
return errors.Wrap(err, "Problem connecting to updater")
|
2018-10-11 11:35:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := boxCli.Connect(); err != nil {
|
2018-10-17 15:32:01 +02:00
|
|
|
fmt.Println("Waiting 3 seconds on error")
|
|
|
|
time.Sleep(3 * time.Second)
|
|
|
|
boxCli, clerr := o.connectUpdater(rqContext, box)
|
|
|
|
if clerr != nil {
|
|
|
|
return errors.Wrap(err, "Problem connecting to updater")
|
|
|
|
}
|
2018-10-15 16:42:35 +02:00
|
|
|
if err := boxCli.Connect(); err != nil {
|
|
|
|
return errors.Wrap(err, "Connecting to Box on master wifi network failed")
|
|
|
|
}
|
2018-10-11 11:35:51 +02:00
|
|
|
}
|
|
|
|
defer boxCli.Close()
|
|
|
|
|
|
|
|
ctx, timeSyncedCancel := context.WithTimeout(rqContext, 55*time.Second)
|
2018-10-10 17:38:04 +02:00
|
|
|
defer timeSyncedCancel()
|
|
|
|
synced, err := boxCli.TimeSynced(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Time sync failed")
|
|
|
|
}
|
|
|
|
reply.Synced = synced
|
|
|
|
|
2018-10-11 11:35:51 +02:00
|
|
|
ctx, reachviewVersionCancel := context.WithTimeout(rqContext, 55*time.Second)
|
2018-10-10 17:38:04 +02:00
|
|
|
defer reachviewVersionCancel()
|
|
|
|
version, err := boxCli.ReachViewVersion(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
reply.Version = version
|
|
|
|
|
2018-10-15 16:42:35 +02:00
|
|
|
ctx, updateCancel := context.WithTimeout(rqContext, 55*time.Second)
|
|
|
|
defer updateCancel()
|
|
|
|
status, err := boxCli.Update(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "System update failed")
|
|
|
|
}
|
|
|
|
reply.Status = status
|
|
|
|
|
2018-10-16 12:29:46 +02:00
|
|
|
if err := boxCli.SkipUpdate(); err != nil {
|
|
|
|
log.Fatalf("error while skipping update: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-10-11 11:35:51 +02:00
|
|
|
ctx, rebootCancel := context.WithTimeout(rqContext, 55*time.Second)
|
2018-10-10 17:38:04 +02:00
|
|
|
defer rebootCancel()
|
|
|
|
return boxCli.RebootNow(ctx, true)
|
2018-10-11 11:35:51 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateOrionBox starts provisionning process for an Orion box (base or rover)
|
|
|
|
func (o *OrionService) UpdateOrionBox(r *http.Request,
|
|
|
|
args *UpdateOrionBoxArgs,
|
|
|
|
reply *UpdateOrionBoxResponse) error {
|
|
|
|
|
2018-10-17 15:32:01 +02:00
|
|
|
// if err := o.setupMasterWifi(r.Context(), args.Box, args.Server); err != nil {
|
|
|
|
// return err
|
|
|
|
// }
|
|
|
|
o.setupMasterWifi(r.Context(), args.Box, args.Server)
|
2018-10-11 11:35:51 +02:00
|
|
|
return o.updateAndReboot(r.Context(), args.Box, args.Server, reply)
|
2018-10-10 17:38:04 +02:00
|
|
|
}
|
|
|
|
|
2018-09-24 08:49:21 +02:00
|
|
|
// NewServer returns a new configured JSON-RPC server
|
|
|
|
func NewServer() *rpc.Server {
|
|
|
|
server := rpc.NewServer()
|
|
|
|
server.RegisterCodec(json.NewCodec(), "application/json")
|
2018-10-05 16:12:07 +02:00
|
|
|
if err := server.RegisterService(NewOrionService(), "Orion"); err != nil {
|
2018-09-24 08:49:21 +02:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return server
|
|
|
|
}
|