Skip to content

Commit 3ed09d3

Browse files
committed
wifinina: implement ConnectModeAP
Signed-off-by: deadprogram <[email protected]>
1 parent 22581df commit 3ed09d3

File tree

1 file changed

+83
-7
lines changed

1 file changed

+83
-7
lines changed

wifinina/wifinina.go

+83-7
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ const (
4747
statusConnectFailed connectionStatus = 4
4848
statusConnectionLost connectionStatus = 5
4949
statusDisconnected connectionStatus = 6
50+
statusAPListening connectionStatus = 7
51+
statusAPConnected connectionStatus = 8
52+
statusAPFailed connectionStatus = 9
5053

5154
encTypeTKIP encryptionType = 2
5255
encTypeCCMP encryptionType = 4
@@ -299,6 +302,61 @@ func (w *wifinina) connectToAP() error {
299302
return netlink.ErrConnectTimeout
300303
}
301304

305+
func (w *wifinina) startAP() error {
306+
timeout := w.params.ConnectTimeout
307+
if timeout == 0 {
308+
timeout = netlink.DefaultConnectTimeout
309+
}
310+
311+
if len(w.params.Ssid) == 0 {
312+
return netlink.ErrMissingSSID
313+
}
314+
315+
if debugging(debugBasic) {
316+
fmt.Printf("Starting Wifi AP as SSID '%s'...", w.params.Ssid)
317+
}
318+
319+
start := time.Now()
320+
321+
// Start the connection process
322+
switch {
323+
case w.params.Passphrase != "":
324+
w.setPassphraseForAP(w.params.Ssid, w.params.Passphrase)
325+
default:
326+
w.setNetworkForAP(w.params.Ssid)
327+
}
328+
329+
// Check if we are listening
330+
for {
331+
status := w.getConnectionStatus()
332+
switch status {
333+
case statusAPListening:
334+
if debugging(debugBasic) {
335+
fmt.Printf("LISTENING\r\n")
336+
}
337+
if w.notifyCb != nil {
338+
w.notifyCb(netlink.EventNetUp)
339+
}
340+
return nil
341+
case statusAPFailed:
342+
if debugging(debugBasic) {
343+
fmt.Printf("FAILED (%s)\r\n", w.reason())
344+
}
345+
return netlink.ErrConnectFailed
346+
}
347+
if time.Since(start) > timeout {
348+
break
349+
}
350+
time.Sleep(1 * time.Second)
351+
}
352+
353+
if debugging(debugBasic) {
354+
fmt.Printf("FAILED (timed out)\r\n")
355+
}
356+
357+
return netlink.ErrConnectTimeout
358+
}
359+
302360
func (w *wifinina) netDisconnect() {
303361
w.disconnect()
304362
}
@@ -380,7 +438,12 @@ func (w *wifinina) showIP() {
380438
}
381439

382440
func (w *wifinina) networkDown() bool {
383-
return w.getConnectionStatus() != statusConnected
441+
switch w.getConnectionStatus() {
442+
case statusConnected, statusAPListening, statusAPConnected:
443+
return false
444+
default:
445+
return true
446+
}
384447
}
385448

386449
func (w *wifinina) watchdog() {
@@ -418,15 +481,28 @@ func (w *wifinina) netConnect(reset bool) error {
418481
}
419482
w.showDevice()
420483

484+
retry:
421485
for i := 0; w.params.Retries == 0 || i < w.params.Retries; i++ {
422-
if err := w.connectToAP(); err != nil {
423-
switch err {
424-
case netlink.ErrConnectTimeout, netlink.ErrConnectFailed:
425-
continue
486+
switch w.params.ConnectMode {
487+
case netlink.ConnectModeAP:
488+
if err := w.startAP(); err != nil {
489+
switch err {
490+
case netlink.ErrConnectTimeout, netlink.ErrConnectFailed:
491+
continue
492+
}
493+
return err
494+
}
495+
break retry
496+
default:
497+
if err := w.connectToAP(); err != nil {
498+
switch err {
499+
case netlink.ErrConnectTimeout, netlink.ErrConnectFailed:
500+
continue
501+
}
502+
return err
426503
}
427-
return err
504+
break retry
428505
}
429-
break
430506
}
431507

432508
if w.networkDown() {

0 commit comments

Comments
 (0)