2018-10-24 10:26:11 +02:00
|
|
|
package owrt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Action is the result of an UCI action output and return code
|
|
|
|
type Action struct {
|
|
|
|
*CommandResult
|
|
|
|
Command string
|
|
|
|
}
|
|
|
|
|
|
|
|
// UCI "Object"
|
|
|
|
type UCI struct {
|
|
|
|
exec Executor
|
|
|
|
Wireless *UCIWirelessConf
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewUCI return an UCI instance to interact with UCI
|
|
|
|
func NewUCI() *UCI {
|
|
|
|
exec := &localExecutor{}
|
|
|
|
wireless := &UCIWirelessConf{}
|
|
|
|
return &UCI{exec, wireless}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewUCIWithExecutor returns a UCI Instance an gives you the ability to provide
|
|
|
|
// a different command executor than the default one.
|
|
|
|
func NewUCIWithExecutor(exec Executor) *UCI {
|
|
|
|
wireless := &UCIWirelessConf{}
|
|
|
|
return &UCI{exec, wireless}
|
|
|
|
}
|
|
|
|
|
|
|
|
// uciRun, private method to run the UCI command
|
|
|
|
func (u *UCI) uciRun(param ...string) *Action {
|
|
|
|
cmd := "/sbin/uci"
|
|
|
|
res := u.exec.Run(cmd, param...)
|
|
|
|
return &Action{res, fmt.Sprintf("%s %s", cmd, param)}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add add an entry to UCI configuration, specify the Module and the value
|
|
|
|
func (u *UCI) Add(module string, name string) *Action {
|
|
|
|
cmd := "uci add"
|
|
|
|
commandRes := u.exec.Run(cmd, module, name)
|
|
|
|
return &Action{commandRes, fmt.Sprintf("%s %s %s", cmd, module, name)}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete delete an entry from UCI configuration specify the entry name
|
|
|
|
func (u *UCI) Delete(entry string) *Action {
|
|
|
|
return u.uciRun("delete", entry)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set set a value ton an UCI configuration entry
|
|
|
|
func (u *UCI) Set(entry string, value string) *Action {
|
|
|
|
return u.uciRun("set", fmt.Sprintf("%s=%s", entry, value))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit the recent actions to UCI
|
|
|
|
func (u *UCI) Commit() *Action {
|
|
|
|
res := u.uciRun("commit")
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reload reload uci configuration
|
|
|
|
func (u *UCI) Reload() *Action {
|
|
|
|
cmdResult := u.exec.Run("reload_config")
|
|
|
|
|
2018-10-29 15:10:32 +01:00
|
|
|
time.Sleep(1 * time.Second)
|
2018-10-24 10:26:11 +02:00
|
|
|
|
|
|
|
return &Action{cmdResult, "reload_config"}
|
|
|
|
}
|
|
|
|
|
2018-10-25 10:24:45 +02:00
|
|
|
// Save commit and relaod configuration (writes it to files !)
|
|
|
|
func (u *UCI) Save() *Action {
|
|
|
|
commitRes := u.Commit()
|
|
|
|
if commitRes.ReturnCode != 0 {
|
|
|
|
return commitRes
|
|
|
|
}
|
|
|
|
|
|
|
|
reload := u.Reload()
|
|
|
|
return reload
|
|
|
|
}
|
|
|
|
|
2018-10-24 10:26:11 +02:00
|
|
|
// Show returns the output of uci show command
|
|
|
|
func (u *UCI) Show(target string) *Action {
|
|
|
|
cmdRes := u.uciRun("show", target)
|
|
|
|
return cmdRes
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddWireless Create a new Wireless entry in UCI configuration
|
|
|
|
func (u *UCI) AddWireless() *Action {
|
|
|
|
res := u.uciRun("add", "wireless", "wifi-iface")
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadWirelessConf scan UCI configuration and loads saved Wireless configuration
|
|
|
|
func (u *UCI) LoadWirelessConf() {
|
|
|
|
u.Wireless = NewUCIWirelessConf(u)
|
|
|
|
u.Wireless.Load()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetWifiIface returns the wifi Interface by Index
|
|
|
|
func (u *UCI) GetWifiIface(idx int) *UCIWirelessInterface {
|
|
|
|
ifaces := u.Wireless.Interfaces
|
|
|
|
if len(ifaces) < idx {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return ifaces[idx]
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetWifiIfaceByName returns the wifi Interface by Index
|
|
|
|
func (u *UCI) GetWifiIfaceByName(name string) *UCIWirelessInterface {
|
|
|
|
ifaces := u.Wireless.Interfaces
|
|
|
|
if len(ifaces) <= 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for _, ifa := range ifaces {
|
|
|
|
if ifa.Name == name {
|
|
|
|
return ifa
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-29 11:19:40 +01:00
|
|
|
// GetWifiIfaceBySSID returns the wifi Interface by Index
|
|
|
|
func (u *UCI) GetWifiIfaceBySSID(ssid string) *UCIWirelessInterface {
|
|
|
|
ifaces := u.Wireless.Interfaces
|
|
|
|
if len(ifaces) <= 0 {
|
|
|
|
fmt.Println("ifaces are empty !!! fuck")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for _, ifa := range ifaces {
|
|
|
|
if ifa.Ssid == ssid {
|
|
|
|
return ifa
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Println("iface not found !")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-24 10:26:11 +02:00
|
|
|
// GetWifiIfaces wifi interfaces in configuration
|
2018-10-29 15:10:32 +01:00
|
|
|
func (u *UCI) GetWifiIfaces() map[int]*UCIWirelessInterface {
|
2018-10-24 10:26:11 +02:00
|
|
|
return u.Wireless.Interfaces
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetWifiDevices returns the wifi devices in configuration
|
|
|
|
func (u *UCI) GetWifiDevices() []map[string]string {
|
|
|
|
return u.Wireless.Devices
|
|
|
|
}
|
2018-10-30 09:55:54 +01:00
|
|
|
|
|
|
|
// GetWifiDeviceByName returns a wifi device by his name "radio0" "radio1"
|
|
|
|
func (u *UCI) GetWifiDeviceByName(name string) map[string]string {
|
|
|
|
for _, device := range u.Wireless.Devices {
|
|
|
|
if device["Device"] == name {
|
|
|
|
return device
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|