/
wifi.go
72 lines (61 loc) · 1.34 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
package rtl8720dn
import (
"fmt"
"time"
)
func (r *RTL8720DN) ConnectToAP(ssid string, password string) error {
if len(ssid) == 0 || len(password) == 0 {
return fmt.Errorf("connection failed: either ssid or password not set")
}
_, err := r.Rpc_wifi_off()
if err != nil {
return err
}
_, err = r.Rpc_wifi_on(0x00000001)
if err != nil {
return err
}
_, err = r.Rpc_wifi_disconnect()
if err != nil {
return err
}
numTry := 5
securityType := uint32(0x00400004)
for i := 0; i < numTry; i++ {
ret, err := r.Rpc_wifi_connect(ssid, password, securityType, -1, 0)
if err != nil {
return err
}
if ret != 0 {
if i == numTry-1 {
return fmt.Errorf("connection failed: rpc_wifi_connect failed")
}
time.Sleep(100 * time.Millisecond)
} else {
break
}
}
_, err = r.Rpc_tcpip_adapter_dhcpc_start(0)
if err != nil {
return err
}
for i := 0; i < 3; i++ {
_, err = r.Rpc_wifi_is_connected_to_ap()
if err != nil {
return err
}
time.Sleep(1 * time.Second)
}
return nil
}
func (r *RTL8720DN) GetIP() (ip, subnet, gateway IPAddress, err error) {
ip_info := make([]byte, 12)
_, err = r.Rpc_tcpip_adapter_get_ip_info(0, ip_info)
if err != nil {
return nil, nil, nil, err
}
ip = IPAddress(ip_info[0:4])
subnet = IPAddress(ip_info[4:8])
gateway = IPAddress(ip_info[8:12])
return ip, subnet, gateway, nil
}