forked from dolfly/termux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wifi.go
110 lines (94 loc) · 2.7 KB
/
wifi.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package termux
import (
"bytes"
"encoding/json"
"fmt"
)
// WifiEnable sets the current enabled state of the device Wi-Fi
func WifiEnable(enabled bool) error {
buf := bytes.NewBuffer([]byte{})
exec(nil, buf, "WifiEnable", map[string]interface{}{
"enabled": enabled,
}, "")
res := buf.Bytes()
return checkErr(res)
}
// WifiConnection represents the current connection info of the device Wi-Fi interface
type WifiConnection struct {
BSSID string `json:"bssid"`
FreqMHZ int `json:"frequency_mhz"`
IP string `json:"ip"`
LinkSpeedMbps int `json:"link_speed_mbps"`
MACAddr string `json:"mac_address"`
NetworkID int `json:"network_id"`
RSSI int `json:"rssi"`
SSID string `json:"ssid"`
SSIDHidden bool `json:"ssid_hidden"`
SupplicantState string `json:"supplicant_state"`
}
func (w WifiConnection) String() string {
return fmt.Sprintf("BSSID: %v\n"+
"FreqMHZ: %v\n"+
"IP: %v\n"+
"LinkSpeed: %v Mbps\n"+
"MACAddr: %v\n"+
"NetworkID: %v\n"+
"RSSI: %v\n"+
"SSID: %v\n"+
"SSIDHidden: %v\n"+
"SupplicantState: %v",
w.BSSID, w.FreqMHZ, w.IP, w.LinkSpeedMbps, w.MACAddr,
w.NetworkID, w.RSSI, w.SSID, w.SSIDHidden, w.SupplicantState)
}
// WifiConnectionState returns the current Wi-Fi connection state of the device
func WifiConnectionState() (*WifiConnection, error) {
buf := bytes.NewBuffer([]byte{})
if err := exec(nil, buf, "WifiConnectionInfo", nil, ""); err != nil {
return nil, err
}
res := buf.Bytes()
if err := checkErr(res); err != nil {
return nil, err
}
ret := new(WifiConnection)
if err := json.Unmarshal(res, ret); err != nil {
return nil, err
}
return ret, nil
}
// WifiAP represents a discovered AP in a Wi-Fi scan
type WifiAP struct {
BSSID string `json:"bssid"`
FreqMHZ int `json:"frequency_mhz"`
RSSI int `json:"rssi"`
SSID string `json:"ssid"`
TimeStamp int64 `json:"timestamp"`
// CenterFreqMHZ not used for 20Mhz bands
CenterFreqMHZ int `json:"center_frequency_mhz"`
ChannelBandwidth string `json:"channel_bandwidth_mhz"`
}
func (w WifiAP) String() string {
return fmt.Sprintf("BSSID: %v\n"+
"FreqMHZ: %v\n"+
"RSSI: %v\n"+
"SSID: %v\n"+
"TimeStamp: %v\n"+
"CenterFreqMHZ: %v\n"+
"ChannelBandwidth: %v",
w.BSSID, w.FreqMHZ, w.RSSI, w.SSID, w.TimeStamp,
w.CenterFreqMHZ, w.ChannelBandwidth)
}
// WifiScan scans for available networks
func WifiScan() ([]WifiAP, error) {
buf := bytes.NewBuffer([]byte{})
execAction("WifiScanInfo", nil, buf, "list", nil)
res := buf.Bytes()
if err := checkErr(res); err != nil {
return nil, err
}
l := make([]WifiAP, 0)
if err := json.Unmarshal(res, &l); err != nil {
return nil, err
}
return l, nil
}