Apply selected wifi network in wifi daemon
This commit is contained in:
parent
c62093a2a7
commit
89e9898d1e
2
TODO.md
2
TODO.md
|
@ -4,7 +4,7 @@
|
|||
- at
|
||||
|
||||
- user settings:
|
||||
- wifi networks
|
||||
- ✓ wifi networks
|
||||
- ✓ wwan enabled/disabled, plus apn
|
||||
- screen brightness level
|
||||
|
||||
|
|
|
@ -10,6 +10,8 @@
|
|||
$machine ? <interface ?ifname _ wireless up up carrier _> [
|
||||
$config <configure-interface $ifname <dhcp>>
|
||||
]
|
||||
|
||||
$config ? <user-setting <?s <selected-wifi-network _ _ _>>> [ $machine += $s ]
|
||||
]
|
||||
|
||||
? <run-service <daemon <wifi-daemon ?ifname>>> [
|
||||
|
|
|
@ -7,7 +7,7 @@ import preserves.schema
|
|||
import time
|
||||
import re
|
||||
|
||||
from syndicate import patterns as P, relay, turn, dataspace, Symbol
|
||||
from syndicate import patterns as P, relay, turn, dataspace, Symbol, stringify
|
||||
from syndicate.actor import find_loop
|
||||
from syndicate.during import During
|
||||
|
||||
|
@ -121,6 +121,54 @@ def main(args):
|
|||
send_command('LEVEL 3')
|
||||
send_command('LIST_NETWORKS', cleanout_networks)
|
||||
|
||||
class NetworkState:
|
||||
def __init__(self, ssid, auth, machine_ds, facet):
|
||||
turn.log.info(f'Selected network ssid={repr(ssid)} auth={auth.VARIANT}')
|
||||
self.ssid = ssid
|
||||
self.auth = auth
|
||||
self.machine_ds = machine_ds
|
||||
self.facet = facet
|
||||
self.wanted = True
|
||||
self.network_id = None
|
||||
self.association_state = turn.field(
|
||||
initial_value=network.WifiAssociationState.inProgress(),
|
||||
name='association_state')
|
||||
turn.publish_dataflow(lambda: (
|
||||
machine_ds, network.WifiAssociation(ifname, self.ssid, self.association_state.value)))
|
||||
send_command('ADD_NETWORK', self.on_network_created)
|
||||
|
||||
def on_network_created(self, new_network_id):
|
||||
turn.log.info(f'Added network id={new_network_id}')
|
||||
self.network_id = new_network_id
|
||||
if self.wanted:
|
||||
send_command(f'SET_NETWORK {self.network_id} ssid {stringify(self.ssid)}')
|
||||
if self.auth.VARIANT == Symbol('psk'):
|
||||
send_command(f'SET_NETWORK {self.network_id} psk {stringify(self.auth.password)}')
|
||||
send_command(f'ENABLE_NETWORK {self.network_id}', self.on_network_enabled)
|
||||
else:
|
||||
send_command(f'REMOVE_NETWORK {self.network_id}')
|
||||
self.network_id = None
|
||||
|
||||
def on_network_enabled(self, reply):
|
||||
turn.log.info(f'Network enabled: {repr(reply)}')
|
||||
def update_association_state():
|
||||
self.association_state.value = network.WifiAssociationState.ready()
|
||||
turn.external(self.facet, update_association_state)
|
||||
|
||||
def on_no_longer_wanted(self):
|
||||
self.wanted = False
|
||||
if self.network_id is not None:
|
||||
send_command(f'REMOVE_NETWORK {self.network_id}')
|
||||
self.network_id = None
|
||||
|
||||
@dataspace.during(machine_ds,
|
||||
P.quote(network.SelectedWifiNetwork(ifname, P.uCAPTURE, P.uCAPTURE)))
|
||||
def handle_selected_network(ssid, auth):
|
||||
auth = network.WifiAuthentication.try_decode(auth)
|
||||
if auth is None: return
|
||||
state = NetworkState(ssid, auth, machine_ds, facet)
|
||||
turn.on_stop(state.on_no_longer_wanted)
|
||||
|
||||
def handle_event(e):
|
||||
if e[0] == '<':
|
||||
# It's an event
|
||||
|
|
Loading…
Reference in New Issue