diff --git a/cmd/server/rpc/server.go b/cmd/server/rpc/server.go index aab226e..778e844 100644 --- a/cmd/server/rpc/server.go +++ b/cmd/server/rpc/server.go @@ -160,10 +160,11 @@ func (o *OrionService) OwrtConnectWifiInterface(r *http.Request, // OrionBox describe an fresh Orion box (base or rover) type OrionBox struct { - Address string - SSID string - Security string - WifiKey string + Address string + NewAddress string + SSID string + Security string + WifiKey string } // OrionServer describe the Orion master server @@ -200,9 +201,6 @@ func (o *OrionService) connectBox(box *OrionBox, server *OrionServer) error { if box == nil { return fmt.Errorf("Box definitioni is emtpy") } - fmt.Println("DEBUG") - fmt.Println(server) - fmt.Println("DEBUG") iface := server.ClientIface cells := iface.Scan() for _, cell := range cells { @@ -217,59 +215,59 @@ func (o *OrionService) connectBox(box *OrionBox, server *OrionServer) error { if dhres.CmdRes.ReturnCode != 0 { return fmt.Errorf("%s\n%s", cn.Stdout, cn.Stderr) } + re := o.UCI.Reload() + if re.ReturnCode != 0 { + return fmt.Errorf("%s\n%s", re.Stdout, re.Stderr) + } return nil } } return fmt.Errorf("Wifi cell with SSID %s is not available", box.SSID) } -// UpdateOrionBox starts provisionning process for an Orion box (base or rover) -func (o *OrionService) UpdateOrionBox(r *http.Request, - args *UpdateOrionBoxArgs, - reply *UpdateOrionBoxResponse) error { +func (o *OrionService) discoverService() ([]emlid.Service, error) { + service, err := emlid.Discover(20 * time.Second) + if err != nil { + return service, err + } + if len(service) == 0 { + return service, fmt.Errorf("Discovery don't find any Orion Box") + } + return service, err - if err := o.connectBox(args.Box, args.Server); err != nil { - reply.Errors = append(reply.Errors, err.Error()) +} + +// 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") } - re := o.UCI.Reload() - if re.ReturnCode != 0 { - return fmt.Errorf("%s\n%s", re.Stdout, re.Stderr) + if server.Security == "" { + server.Security = string(updater.SecurityWPAPSK) } - ssid := args.Server.SSID - security := args.Server.Security - wifiKey := args.Server.WifiKey - - if security == "" { - security = string(updater.SecurityWPAPSK) + var boxCli *updater.Client + service, err := o.discoverService() + if err != nil { + boxCli = updater.NewClient( + emlid.WithService(service[0]), + ) + } else { + boxCli = updater.NewClient( + emlid.WithEndpoint(box.Address, 80), + ) } - //services, err := emlid.Discover(20 * time.Second) - //if err != nil { - // return err - //} - - //if len(services) == 0 { - // return fmt.Errorf("Discovery don't find any Orion Box") - //} - - //fmt.Println(services) - boxCli := updater.NewClient( - //emlid.WithService(services[0]), - emlid.WithEndpoint(args.Box.Address, 80), - ) - if err := boxCli.Connect(); err != nil { - return errors.Wrap(err, "Box connect to master failed") + return errors.Wrap(err, "Connecting to Box failed") } defer boxCli.Close() - ctx, addWifiCancel := context.WithTimeout(r.Context(), 55*time.Second) + ctx, addWifiCancel := context.WithTimeout(rqContext, 55*time.Second) defer addWifiCancel() - done, err := boxCli.AddWifiNetwork(ctx, ssid, updater.WifiSecurity(security), wifiKey) + done, err := boxCli.AddWifiNetwork(ctx, server.SSID, updater.WifiSecurity(server.Security), server.WifiKey) if err != nil { return errors.Wrap(err, "AddWifiNetworkFailed") } @@ -281,11 +279,32 @@ func (o *OrionService) UpdateOrionBox(r *http.Request, ctx, joinWifiCancel := context.WithTimeout(ctx, 55*time.Second) defer joinWifiCancel() - if err := boxCli.JoinWifiNetwork(ctx, ssid, true); err != nil { + if err := boxCli.JoinWifiNetwork(ctx, server.SSID, true); err != nil { return errors.Wrap(err, "Time sync failed") } + return nil +} - ctx, timeSyncedCancel := context.WithTimeout(r.Context(), 55*time.Second) +// 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 { + var boxCli *updater.Client + service, err := o.discoverService() + if err != nil { + boxCli = updater.NewClient( + emlid.WithService(service[0]), + ) + } else { + boxCli = updater.NewClient( + emlid.WithEndpoint(box.NewAddress, 80), + ) + } + + if err := boxCli.Connect(); err != nil { + return errors.Wrap(err, "Connecting to Box on master wifi network failed") + } + defer boxCli.Close() + + ctx, timeSyncedCancel := context.WithTimeout(rqContext, 55*time.Second) defer timeSyncedCancel() synced, err := boxCli.TimeSynced(ctx) if err != nil { @@ -293,7 +312,7 @@ func (o *OrionService) UpdateOrionBox(r *http.Request, } reply.Synced = synced - ctx, reachviewVersionCancel := context.WithTimeout(r.Context(), 55*time.Second) + ctx, reachviewVersionCancel := context.WithTimeout(rqContext, 55*time.Second) defer reachviewVersionCancel() version, err := boxCli.ReachViewVersion(ctx) if err != nil { @@ -301,9 +320,22 @@ func (o *OrionService) UpdateOrionBox(r *http.Request, } reply.Version = version - ctx, rebootCancel := context.WithTimeout(r.Context(), 55*time.Second) + ctx, rebootCancel := context.WithTimeout(rqContext, 55*time.Second) defer rebootCancel() return boxCli.RebootNow(ctx, true) + +} + +// UpdateOrionBox starts provisionning process for an Orion box (base or rover) +func (o *OrionService) UpdateOrionBox(r *http.Request, + args *UpdateOrionBoxArgs, + reply *UpdateOrionBoxResponse) error { + + if err := o.setupMasterWifi(r.Context(), args.Box, args.Server); err != nil { + return err + } + + return o.updateAndReboot(r.Context(), args.Box, args.Server, reply) } // NewServer returns a new configured JSON-RPC server diff --git a/cmd/server/rpc/testdata/test_rest.rest b/cmd/server/rpc/testdata/test_rest.rest new file mode 100644 index 0000000..1ff82ce --- /dev/null +++ b/cmd/server/rpc/testdata/test_rest.rest @@ -0,0 +1,75 @@ +POST http://192.168.100.1:8888/rpc HTTP/1.1 +content-type: application/json + +{ + "id": 1, + "method": "Orion.OwrtCreateWifiInterface", + "params": [ { + "Iface": { + "Name": "PyxisNetwork", + "Network": "lan", + "Index": 0, + "Ssid": "Pyxis", + "SysDevName": "wlan0", + "Encryption": "psk2", + "Key": "A2Z3E4R5T6Y7U", + "Device": "radio0", + "DevicePath": "soc/soc:pcie/pci0000:00/0000:00:01.0/0000:01:00.0", + "Mode": "ap" + }, + "Cleanup": true + } + ] +} + +### +POST http://192.168.100.1:8888/rpc HTTP/1.1 +content-type: application/json + +{ + "id": 1, + "method": "Orion.OwrtCreateWifiInterface", + "params": [ { + "Iface": { + "Name": "PyxisClient", + "Index": 1, + "SysDevName": "wlan1", + "Device": "radio1", + "DevicePath": "soc/soc:pcie/pci0000:00/0000:00:02.0/0000:02:00.0", + "Mode": "sta" + }, + "Cleanup": false + }] +} + +### +POST http://192.168.100.1:8888/rpc HTTP/1.1 +content-type: application/json + +{ + "id": 1, + "method": "Orion.UpdateOrionBox", + "params": [ { + "Box": { + "Address": "192.168.42.1", + "NewAddress": "192.168.100.213", + "Security": "", + "SSID" : "reach:2a:03", + "WifiKey": "emlidreach" + }, + "Server": { + "Address": "192.168.42.1", + "SSID": "Pyxis", + "Security": "", + "WifiKey": "A2Z3E4R5T6Y7U", + "ClientIface" : { + "Name": "PyxisClient", + "Index": 1, + "SysDevName": "wlan1", + "Device": "radio1", + "DevicePath": "soc/soc:pcie/pci0000:00/0000:00:02.0/0000:02:00.0", + "Mode": "sta" + } + } + } ] +} \ No newline at end of file