-
Notifications
You must be signed in to change notification settings - Fork 6
/
wifi.go
400 lines (329 loc) · 9.75 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
package cyw43439
// This file borrows heavily from control.rs from the reference:
// https://github.com/embassy-rs/embassy/blob/26870082427b64d3ca42691c55a2cded5eadc548/cyw43/src/control.rs
import (
"encoding/binary"
"errors"
"net"
"time"
"log/slog"
"github.com/soypat/cyw43439/whd"
)
func (d *Device) initControl(clm string) error {
// reference: https://github.com/embassy-rs/embassy/blob/26870082427b64d3ca42691c55a2cded5eadc548/cyw43/src/control.rs#L35
d.debug("initControl", slog.Int("clm_len", len(clm)))
const chunkSize = 1024
remaining := clm
offset := 0
buf8 := unsafeAsSlice[uint32, uint8](d._iovarBuf[:])[:chunkSize+20]
for len(remaining) > 0 {
chunk := remaining[:min(len(remaining), chunkSize)]
remaining = remaining[len(chunk):]
var flag uint16 = 0x1000 // Download flag handler version.
if offset == 0 {
flag |= 0x0002 // Flag begin.
}
offset += len(chunk)
if offset == len(clm) {
flag |= 0x0004 // Flag end.
}
header := whd.DownloadHeader{ // No CRC.
Flags: flag,
Type: 2, // CLM download type.
Len: uint32(len(chunk)),
}
n := copy(buf8[:8], "clmload\x00")
header.Put(_busOrder, buf8[8:20])
n += whd.DL_HEADER_LEN
n += copy(buf8[20:], chunk)
err := d.doIoctlSet(whd.WLC_SET_VAR, whd.IF_STA, buf8[:n])
if err != nil {
return err
}
}
d.debug("clmload:done")
v, err := d.get_iovar("clmload_status", whd.IF_STA)
if v != 0 || err != nil {
// return errjoin(errors.New("clmload_status failed"), err)
}
// Disable tx gloming which transfers multiple packets in one request.
// 'glom' is short for "conglomerate" which means "gather together into
// a compact mass".
d.set_iovar("bus:txglom", whd.IF_STA, 0)
d.set_iovar("apsta", whd.IF_STA, 1)
// read MAC Address:
d.get_iovar_n("cur_etheraddr", whd.IF_STA, d.mac[:6])
d.debug("MAC", slog.String("mac", d.hwaddr().String()))
countryInfo := whd.CountryInfo("XX", 0)
d.set_iovar_n("country", whd.IF_STA, countryInfo[:])
// set country takes some time, next ioctls fail if we don't wait.
time.Sleep(100 * time.Millisecond)
// Set Antenna to chip antenna.
d.set_ioctl(whd.WLC_SET_ANTDIV, whd.IF_STA, 0)
d.set_iovar("bus:txglom", whd.IF_STA, 0)
time.Sleep(100 * time.Millisecond)
d.set_iovar("ampdu_ba_wsize", whd.IF_STA, 8)
time.Sleep(100 * time.Millisecond)
d.set_iovar("ampdu_mpdu", whd.IF_STA, 4)
time.Sleep(100 * time.Millisecond)
// Ignore uninteresting/spammy events.
var evts eventMask
for i := range evts.events {
evts.events[i] = 0xff
}
evts.Disable(whd.EvRADIO)
evts.Disable(whd.EvIF)
evts.Disable(whd.EvPROBREQ_MSG)
evts.Disable(whd.EvPROBREQ_MSG_RX)
evts.Disable(whd.EvPROBRESP_MSG)
evts.Disable(whd.EvROAM)
buf := make([]byte, evts.Size())
evts.Put(buf)
d.set_iovar_n("bsscfg:event_msgs", whd.IF_STA, buf)
time.Sleep(100 * time.Millisecond)
// Set wifi up.
d.doIoctlSet(whd.WLC_UP, whd.IF_STA, nil)
time.Sleep(100 * time.Millisecond)
d.set_ioctl(whd.WLC_SET_GMODE, whd.IF_STA, 1) // Set GMODE=auto
d.set_ioctl(whd.WLC_SET_BAND, whd.IF_STA, 0) // Set BAND=any
time.Sleep(100 * time.Millisecond)
return nil
}
func (d *Device) hwaddr() net.HardwareAddr {
return net.HardwareAddr(d.mac[:6])
}
func (d *Device) MACAs6() [6]byte {
d.trace("MACAs6:call")
return d.mac
}
func (d *Device) set_power_management(mode powerManagementMode) error {
d.debug("set_power_management", slog.String("mode", mode.String()))
if !mode.IsValid() {
return errors.New("invalid power management mode")
}
mode_num := mode.mode()
if mode_num == 2 {
d.set_iovar("pm2_sleep_ret", whd.IF_STA, uint32(mode.sleep_ret_ms()))
d.set_iovar("bcn_li_bcn", whd.IF_STA, uint32(mode.beacon_period()))
d.set_iovar("bcn_li_dtim", whd.IF_STA, uint32(mode.dtim_period()))
d.set_iovar("assoc_listen", whd.IF_STA, uint32(mode.assoc()))
}
return d.set_ioctl(whd.WLC_SET_PM, whd.IF_STA, uint32(mode_num))
}
func (d *Device) join_open(ssid string) error {
d.debug("join_open", slog.String("ssid", ssid))
if len(ssid) > 32 {
return errors.New("ssid too long")
}
d.set_iovar("ampdu_ba_wsize", whd.IF_STA, 8)
d.set_ioctl(whd.WLC_SET_WSEC, whd.IF_STA, 0)
d.set_iovar2("bsscfg:sup_wpa", whd.IF_STA, 0, 0)
d.set_ioctl(whd.WLC_SET_INFRA, whd.IF_STA, 1)
d.set_ioctl(whd.WLC_SET_AUTH, whd.IF_STA, 0)
return d.wait_for_join(ssid)
}
func (d *Device) wait_for_join(ssid string) (err error) {
d.eventmask.Enable(whd.EvSET_SSID)
d.eventmask.Enable(whd.EvAUTH)
err = d.setSSID(ssid)
if err != nil {
return err
}
// Poll for async events.
deadline := time.Now().Add(10 * time.Second)
d.state = linkStateDown
for time.Until(deadline) > 0 && d.state == linkStateDown && d.state != linkStateUpWaitForSSID {
time.Sleep(270 * time.Millisecond)
err = d.check_status(d._sendIoctlBuf[:])
if err != nil {
return err
}
}
switch d.state {
case linkStateUp:
// Begin listening in for link change/down events.
d.eventmask.Enable(whd.EvLINK)
d.eventmask.Enable(whd.EvJOIN)
d.eventmask.Enable(whd.EvDISASSOC)
d.eventmask.Enable(whd.EvDEAUTH)
return nil
case linkStateAuthFailed:
return errors.New("auth failed")
case linkStateFailed:
return errors.New("SET_SSID failed")
default:
return errors.New("join failed")
}
}
type passphraseInfo struct {
length uint16
flags uint16
passphrase [64]byte
}
func (p *passphraseInfo) Put(order binary.ByteOrder, b []byte) {
order.PutUint16(b[0:2], p.length)
order.PutUint16(b[2:4], p.flags)
copy(b[4:68], p.passphrase[:])
}
func (d *Device) setPassphrase(pass string) error {
if len(pass) > 64 {
return errors.New("ssid too long")
}
var pfi = passphraseInfo{
length: uint16(len(pass)),
flags: 1,
}
copy(pfi.passphrase[:], pass)
var buf [68]byte
pfi.Put(_busOrder, buf[:])
return d.doIoctlSet(whd.WLC_SET_WSEC_PMK, whd.IF_STA, buf[:])
}
type ssidInfo struct {
length uint32
ssid [32]byte
}
func (s *ssidInfo) put(order binary.ByteOrder, b []byte) {
order.PutUint32(b[0:4], s.length)
copy(b[4:36], s.ssid[:])
}
// setSSID sets the SSID through Ioctl interface. This command
// also starts the wifi connect procedure.
func (d *Device) setSSID(ssid string) error {
if len(ssid) > 32 {
return errors.New("ssid too long")
}
var info = ssidInfo{
length: uint32(len(ssid)),
}
copy(info.ssid[:], ssid)
var buf [36]byte
info.put(_busOrder, buf[:])
return d.doIoctlSet(whd.WLC_SET_SSID, whd.IF_STA, buf[:])
}
type ssidInfoWithIndex struct {
index uint32
info ssidInfo
}
func (s *ssidInfoWithIndex) Put(order binary.ByteOrder, b []byte) {
order.PutUint32(b[0:4], s.index)
s.info.put(order, b[4:40])
}
func (d *Device) setSSIDWithIndex(ssid string, index uint32) error {
if len(ssid) > 32 {
return errors.New("ssid too long")
}
var infoIndex = ssidInfoWithIndex{
info: ssidInfo{
length: uint32(len(ssid)),
},
}
copy(infoIndex.info.ssid[:], ssid)
var buf [40]byte
infoIndex.Put(_busOrder, buf[:])
return d.set_iovar_n("bsscfg:ssid", whd.IF_STA, buf[:])
}
// IsLinkUp returns true if the wifi connection is up.
func (d *Device) IsLinkUp() bool {
return d.state == linkStateUp
}
func (d *Device) JoinWPA2(ssid, pass string) error {
d.lock()
defer d.unlock()
if ssid != "" && pass == "" {
return d.join_open(ssid)
}
d.info("joinWpa2", slog.String("ssid", ssid), slog.Int("len(pass)", len(pass)))
if err := d.set_iovar("ampdu_ba_wsize", whd.IF_STA, 8); err != nil {
return err
}
// wsec = wpa2
if err := d.set_ioctl(whd.WLC_SET_WSEC, whd.IF_STA, 4); err != nil {
return err
}
if err := d.set_iovar2("bsscfg:sup_wpa", whd.IF_STA, 0, 1); err != nil {
return err
}
if err := d.set_iovar2("bsscfg:sup_wpa2_eapver", whd.IF_STA, 0, 0xffff_ffff); err != nil {
return err
}
if err := d.set_iovar2("bsscfg:sup_wpa_tmo", whd.IF_STA, 0, 2500); err != nil {
return err
}
time.Sleep(100 * time.Millisecond)
if err := d.setPassphrase(pass); err != nil {
return err
}
// set_infra = 1
if err := d.set_ioctl(whd.WLC_SET_INFRA, whd.IF_STA, 1); err != nil {
return err
}
// set_auth = 0 (open)
if err := d.set_ioctl(whd.WLC_SET_AUTH, whd.IF_STA, 0); err != nil {
return err
}
// set_wpa_auth
if err := d.set_ioctl(whd.WLC_SET_WPA_AUTH, whd.IF_STA, 0x80); err != nil {
return err
}
return d.wait_for_join(ssid)
}
func (d *Device) StartAP(ssid, pass string, channel uint8) error {
d.lock()
defer d.unlock()
security := whd.CYW43_AUTH_OPEN
if pass != "" {
if len(pass) < whd.CYW43_MIN_PSK_LEN || len(pass) > whd.CYW43_MAX_PSK_LEN {
return errors.New("Passphrase is too short or too long")
}
security = whd.CYW43_AUTH_WPA2_AES_PSK
}
// Temporarily set wifi down
if err := d.doIoctlSet(whd.WLC_DOWN, whd.IF_STA, nil); err != nil {
return err
}
// Turn off APSTA mode
if err := d.set_iovar("apsta", whd.IF_STA, 0); err != nil {
return err
}
// Set wifi up again
if err := d.doIoctlSet(whd.WLC_UP, whd.IF_STA, nil); err != nil {
return err
}
// Turn on AP mode
if err := d.set_ioctl(whd.WLC_SET_AP, whd.IF_STA, 1); err != nil {
return err
}
// Set SSID
if err := d.setSSIDWithIndex(ssid, 0); err != nil {
return err
}
// Set channel number
if err := d.set_ioctl(whd.WLC_SET_CHANNEL, whd.IF_STA, uint32(channel)); err != nil {
return err
}
// Set security
if err := d.set_iovar2("bsscfg:wsec", whd.IF_STA, 0, uint32(security)&0xff); err != nil {
return err
}
if security != whd.CYW43_AUTH_OPEN {
// wpa_auth = WPA2_AUTH_PSK | WPA_AUTH_PSK
if err := d.set_iovar2("bsscfg:wpa_auth", whd.IF_STA, 0,
whd.CYW43_WPA_AUTH_PSK|whd.CYW43_WPA2_AUTH_PSK); err != nil {
return err
}
time.Sleep(100 * time.Millisecond)
// Set passphrase
if err := d.setPassphrase(pass); err != nil {
return err
}
}
// Change mutlicast rate from 1 Mbps to 11 Mbps
if err := d.set_iovar("2g_mrate", whd.IF_STA, 11000000/500000); err != nil {
return err
}
// Start AP (bss = BSS_UP)
if err := d.set_iovar2("bss", whd.IF_STA, 0, 1); err != nil {
return err
}
return nil
}