94 lines
1.9 KiB
Go
94 lines
1.9 KiB
Go
package openwrt
|
|
|
|
import "time"
|
|
|
|
// WifiCell reprensents wifi network Cell
|
|
type WifiCell struct {
|
|
Ssid string
|
|
MacAdress string
|
|
Encryption string
|
|
}
|
|
|
|
// NewWifiCell returns a new WifiCell object
|
|
func NewWifiCell(ssid string, mac string, encrypt string) *WifiCell {
|
|
return &WifiCell{
|
|
Ssid: ssid,
|
|
MacAdress: mac,
|
|
Encryption: encrypt,
|
|
}
|
|
}
|
|
|
|
func (cell *WifiCell) uciWifiConfigure(uci *UCI, secret string) *Action {
|
|
setRes := uci.Set("wireless.@wifi-iface[1].network", "PyxisNetwork")
|
|
if setRes.ReturnCode != 0 {
|
|
return setRes
|
|
}
|
|
|
|
setRes = uci.Set("wireless.@wifi-iface[1].ssid", cell.Ssid)
|
|
if setRes.ReturnCode != 0 {
|
|
return setRes
|
|
}
|
|
setRes = uci.Set("wireless.@wifi-iface[1].encryption", cell.Encryption)
|
|
if setRes.ReturnCode != 0 {
|
|
return setRes
|
|
}
|
|
setRes = uci.Set("wireless.@wifi-iface[1].device", "radio1")
|
|
if setRes.ReturnCode != 0 {
|
|
return setRes
|
|
}
|
|
setRes = uci.Set("wireless.@wifi-iface[1].mode", "sta")
|
|
if setRes.ReturnCode != 0 {
|
|
return setRes
|
|
}
|
|
setRes = uci.Set("wireless.@wifi-iface[1].bssid", cell.MacAdress)
|
|
if setRes.ReturnCode != 0 {
|
|
return setRes
|
|
}
|
|
setRes = uci.Set("wireless.@wifi-iface[1].key", secret)
|
|
if setRes.ReturnCode != 0 {
|
|
return setRes
|
|
}
|
|
|
|
return &Action{
|
|
&CommandResult{
|
|
Stdout: "",
|
|
Stderr: "",
|
|
ReturnCode: 0,
|
|
},
|
|
}
|
|
}
|
|
|
|
// Connect to wifi Cell
|
|
func (cell *WifiCell) Connect(uci *UCI, secret string) *Action {
|
|
delRes := uci.Delete("wireless.@wifi-iface[1]")
|
|
if delRes.ReturnCode != 0 {
|
|
return delRes
|
|
}
|
|
addRes := uci.AddWireless("wifi-iface")
|
|
if addRes.ReturnCode != 0 {
|
|
return addRes
|
|
}
|
|
|
|
setRes := cell.uciWifiConfigure(uci, secret)
|
|
if setRes.ReturnCode != 0 {
|
|
return setRes
|
|
}
|
|
|
|
setRes = uci.Commit()
|
|
if setRes.ReturnCode != 0 {
|
|
return setRes
|
|
}
|
|
setRes = uci.Reload()
|
|
if setRes.ReturnCode != 0 {
|
|
return setRes
|
|
}
|
|
time.Sleep(20 * time.Second)
|
|
return &Action{
|
|
&CommandResult{
|
|
Stdout: "",
|
|
Stderr: "",
|
|
ReturnCode: 0,
|
|
},
|
|
}
|
|
}
|