2018-09-19 14:39:00 +02:00
|
|
|
package openwrt
|
|
|
|
|
2018-09-20 10:59:11 +02:00
|
|
|
import "time"
|
2018-09-19 14:39:00 +02:00
|
|
|
|
|
|
|
// WifiCell reprensents wifi network Cell
|
|
|
|
type WifiCell struct {
|
2018-09-20 10:59:11 +02:00
|
|
|
Ssid string
|
|
|
|
MacAdress string
|
|
|
|
Encryption string
|
2018-09-19 14:39:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewWifiCell returns a new WifiCell object
|
2018-09-20 10:59:11 +02:00
|
|
|
func NewWifiCell(ssid string, mac string, encrypt string) *WifiCell {
|
2018-09-19 14:39:00 +02:00
|
|
|
return &WifiCell{
|
2018-09-20 10:59:11 +02:00
|
|
|
Ssid: ssid,
|
|
|
|
MacAdress: mac,
|
|
|
|
Encryption: encrypt,
|
2018-09-19 14:39:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-20 10:59:11 +02:00
|
|
|
// 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 := uci.Set("wireless.@wifi-iface[1].network", "PyxisNetwork")
|
|
|
|
if setRes.ReturnCode != 0 {
|
|
|
|
return setRes
|
2018-09-19 14:39:00 +02:00
|
|
|
}
|
|
|
|
|
2018-09-20 10:59:11 +02:00
|
|
|
setRes = uci.Set("wireless.@wifi-iface[1].ssid", cell.Ssid)
|
|
|
|
if setRes.ReturnCode != 0 {
|
|
|
|
return setRes
|
|
|
|
}
|
|
|
|
setRes = uci.Set("wireless.@wifi-iface[1].encryption", "psk2")
|
|
|
|
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
|
|
|
|
}
|
|
|
|
setRes = uci.Commit()
|
|
|
|
if setRes.ReturnCode != 0 {
|
|
|
|
return setRes
|
|
|
|
}
|
|
|
|
setRes = uci.Reload()
|
|
|
|
if setRes.ReturnCode != 0 {
|
|
|
|
return setRes
|
|
|
|
}
|
|
|
|
time.Sleep(20)
|
|
|
|
return &Action{
|
|
|
|
&CommandResult{
|
|
|
|
Stdout: "",
|
|
|
|
Stderr: "",
|
|
|
|
ReturnCode: 0,
|
|
|
|
},
|
|
|
|
}
|
2018-09-19 14:39:00 +02:00
|
|
|
}
|