Improve JSON-RPC methods for emlid interaction

This commit is contained in:
2018-10-10 17:38:04 +02:00
committed by William Petit
parent cadd3482a2
commit e6ee5d688f
8 changed files with 233 additions and 10 deletions

View File

@ -1,11 +1,24 @@
package openwrt
import (
"bufio"
"regexp"
"strings"
)
// DhcpClient represents a dhcp client ... :)
type DhcpClient struct {
exec Executor
iface string
}
// DhcpResult contains sorted result form AskFroIP
type DhcpResult struct {
CmdRes *CommandResult
IP string
Netmask string
}
// NewDhcpClient return an UCI instance to interact with UCI
func NewDhcpClient(netIface string) *DhcpClient {
exec := &localExecutor{}
@ -32,7 +45,36 @@ func NewDhcpClientWithExecutor(netIface string, exe Executor) *DhcpClient {
// return &DhcpClient{exec, iface}
//}
// AskForIP runs a dhclient ip request with udhcpc
func (dc *DhcpClient) AskForIP() *CommandResult {
return dc.exec.Run("udhcpc", "-i", dc.iface)
func parseDhcpClientOut(out *CommandResult) *DhcpResult {
if out.ReturnCode != 0 {
return &DhcpResult{
out,
"",
"",
}
}
scanner := bufio.NewScanner(strings.NewReader(out.Stdout))
for scanner.Scan() {
line := scanner.Text()
re := regexp.MustCompile(`^udhcpc: ifconfig`)
if re.MatchString(line) {
spl := strings.Split(line, " ")
ip := spl[3]
mask := spl[5]
return &DhcpResult{
out,
ip,
mask,
}
}
}
return nil
}
// AskForIP runs a dhclient ip request with udhcpc
func (dc *DhcpClient) AskForIP() *DhcpResult {
out := dc.exec.Run("udhcpc", "-i", dc.iface)
return parseDhcpClientOut(out)
}

View File

@ -6,7 +6,7 @@ func TestDhcpClientAskForIP(t *testing.T) {
uexec := createMockExecutor("", "", 0)
dhc := NewDhcpClientWithExecutor("wlan1", uexec)
res := dhc.AskForIP()
if res.ReturnCode != 0 {
if res.CmdRes.ReturnCode != 0 {
t.Error("Error in DHCP Client !!")
}
}

View File

@ -22,6 +22,7 @@ type UCIWirelessInterface struct {
Network string
Encryption string
Key string
Country string
}
// NewUCIWirelessInterface builds a new UCIWirelessInterface instance
@ -71,10 +72,11 @@ func (wi *UCIWirelessInterface) Create(uci *UCI) *Action {
if addRes.ReturnCode != 0 {
return addRes
}
fmt.Println(wi.Index)
if wi.Index <= 0 {
confPrefix = fmt.Sprintf("wireless.@wifi-iface[%d]", wi.Index)
} else {
confPrefix = fmt.Sprintf("wireless.@wifi-iface[%d]", len(uci.Wireless.Interfaces))
} else {
confPrefix = fmt.Sprintf("wireless.@wifi-iface[%d]", wi.Index)
}
conf := make(map[string][]string)
@ -85,6 +87,8 @@ func (wi *UCIWirelessInterface) Create(uci *UCI) *Action {
conf["mode"] = append(conf["mode"], fmt.Sprintf("%s.mode", confPrefix), wi.Mode)
conf["bssid"] = append(conf["bssid"], fmt.Sprintf("%s.bssid", confPrefix), wi.Bssid)
conf["key"] = append(conf["key"], fmt.Sprintf("%s.key", confPrefix), wi.Key)
conf["disabled"] = append(conf["disabled"], fmt.Sprintf("%s.disabled", confPrefix), "0")
conf["country"] = append(conf["country"], fmt.Sprintf("%s.country", confPrefix), wi.Country)
for _, value := range conf {
if value[1] != "" {
@ -168,6 +172,7 @@ func (wi *UCIWirelessInterface) Up(uci *UCI) *Action {
// Delete remove wifi interface from UCI Configuration
func (wi *UCIWirelessInterface) Delete(uci *UCI) *Action {
toDelete := fmt.Sprintf("wireless.@wifi-iface[%d]", wi.Index)
fmt.Printf("DEBUG\n%s\nDEBUG", toDelete)
del := uci.Delete(toDelete)
if del.ReturnCode != 0 {
return del
@ -178,6 +183,8 @@ func (wi *UCIWirelessInterface) Delete(uci *UCI) *Action {
// Update add a new entry for wifi interface in UCI Configuration
func (wi *UCIWirelessInterface) Update(uci *UCI) *Action {
wi.Delete(uci)
fmt.Println("IN UPDATE !")
fmt.Println(wi)
create := wi.Create(uci)
if create.ReturnCode != 0 {
return create
@ -200,6 +207,7 @@ func (wi *UCIWirelessInterface) Connect(uci *UCI, cell *WifiCell, secret string)
wi.Encryption = cell.Encryption
wi.Mode = "sta"
wi.Key = secret
fmt.Println(wi)
res := wi.Update(uci)
if res.ReturnCode != 0 {
return res

View File

@ -33,7 +33,7 @@ func NewWifiWithExecutor(exec Executor, wIface string) *WifiScanner {
func (w *WifiScanner) getEncryption(line string) string {
var enc string
if strings.Contains(line, "PSK") {
enc = "psk"
enc = "psk2"
} else if strings.Contains(line, NONE) {
enc = NONE
} else {
@ -61,9 +61,9 @@ func (w *WifiScanner) parseWifiCells(stdout string) int {
}
if strings.Contains(line, "SSID:") {
ssid = strings.Split(line, ":")[1]
ssid = strings.Split(line, " ")[1]
ssid = strings.Trim(ssid, "\"")
ssid = strings.Trim(ssid, " ")
//ssid = strings.Trim(ssid, " ")
if ssid == "" {
ssid = " "
}