Skip to content

Commit

Permalink
Fix wifi scan (#2065)
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperl authored Jan 23, 2024
1 parent 01ea67a commit 5c3c688
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
12 changes: 10 additions & 2 deletions lib/system/base/network.toit
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ monitor NetworkState:

up [create] -> NetworkModule:
usage_++
if module_: return module_
module/NetworkModule? := null
module := module_
if module: return module
try:
module = create.call
module.connect
Expand All @@ -110,6 +110,14 @@ monitor NetworkState:
// failed with an exception.
if module: module.disconnect

up [--if-unconnected] -> NetworkModule?:
module := module_
if module:
usage_++
return module
if-unconnected.call
return null

down -> none:
usage_--
if usage_ > 0 or not module_: return
Expand Down
31 changes: 15 additions & 16 deletions system/extensions/esp32/wifi.toit
Original file line number Diff line number Diff line change
Expand Up @@ -154,19 +154,16 @@ class WifiServiceProvider extends NetworkServiceProviderBase:
channels := config.get wifi.CONFIG-SCAN-CHANNELS
passive := config.get wifi.CONFIG-SCAN-PASSIVE
period := config.get wifi.CONFIG-SCAN-PERIOD
connected := state_.up --if-unconnected=:
// If the network is unconnected, we bring up the network
// module, but keep it unconnected. We scan while keeping
// the state lock, so others can't interfere with us. They
// will have to wait their turn to bring the network up.
unconnected := WifiModule.sta this "" ""
return unconnected.scan channels passive period --close
try:
result/List? := null
module := state_.up:
inner := WifiModule.sta this "" ""
// If we are bringing up the wifi module, we scan while keeping the
// initialization lock, so others can't interfere with us. They
// will have to wait their turn to bring the network up.
result = inner.scan channels passive period
inner
// If we didn't bring up the module ourselves we haven't scanned yet.
if not result:
result = (module as WifiModule).scan channels passive period
return result
// Scan using the connected network module.
return (connected as WifiModule).scan channels passive period
finally:
state_.down

Expand Down Expand Up @@ -319,25 +316,27 @@ class WifiModule implements NetworkModule:
ap-info -> List:
return wifi-get-ap-info_ resource-group_

scan channels/ByteArray passive/bool period/int -> List:
scan channels/ByteArray passive/bool period/int --close/bool=false -> List:
if ap or not resource-group_:
throw "wifi is AP mode or not initialized"

resource := wifi-init-scan_ resource-group_
scan-events := monitor.ResourceState_ resource-group_ resource
result := []
try:
result := []
channels.do:
wifi-start-scan_ resource-group_ it passive period
state := scan-events.wait
if (state & WIFI-SCAN-DONE) == 0: throw "WIFI_SCAN_ERROR"
scan-events.clear-state WIFI-SCAN-DONE
array := wifi-read-scan_ resource-group_
result.add-all array
return result
finally:
scan-events.dispose

return result
if close:
wifi-close_ resource-group_
resource-group_ = null

on-event_ state/int:
// TODO(kasper): We should be clearing the state in the
Expand Down

0 comments on commit 5c3c688

Please sign in to comment.