@@ -47,6 +47,9 @@ const (
47
47
statusConnectFailed connectionStatus = 4
48
48
statusConnectionLost connectionStatus = 5
49
49
statusDisconnected connectionStatus = 6
50
+ statusAPListening connectionStatus = 7
51
+ statusAPConnected connectionStatus = 8
52
+ statusAPFailed connectionStatus = 9
50
53
51
54
encTypeTKIP encryptionType = 2
52
55
encTypeCCMP encryptionType = 4
@@ -299,6 +302,61 @@ func (w *wifinina) connectToAP() error {
299
302
return netlink .ErrConnectTimeout
300
303
}
301
304
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
+
302
360
func (w * wifinina ) netDisconnect () {
303
361
w .disconnect ()
304
362
}
@@ -380,7 +438,12 @@ func (w *wifinina) showIP() {
380
438
}
381
439
382
440
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
+ }
384
447
}
385
448
386
449
func (w * wifinina ) watchdog () {
@@ -418,15 +481,28 @@ func (w *wifinina) netConnect(reset bool) error {
418
481
}
419
482
w .showDevice ()
420
483
484
+ retry:
421
485
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
426
503
}
427
- return err
504
+ break retry
428
505
}
429
- break
430
506
}
431
507
432
508
if w .networkDown () {
0 commit comments