2016-10-01 00:03:10 +02:00
|
|
|
#!/usr/bin/python3
|
|
|
|
from gi.repository import GLib
|
|
|
|
|
|
|
|
import dbus
|
|
|
|
import dbus.service
|
|
|
|
import dbus.mainloop.glib
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import threading
|
|
|
|
import time
|
2018-05-15 20:43:12 +02:00
|
|
|
import collections
|
2018-07-31 16:37:08 +02:00
|
|
|
import datetime
|
2019-04-20 22:29:03 +02:00
|
|
|
import weakref
|
2016-10-01 00:03:10 +02:00
|
|
|
|
|
|
|
from abc import ABCMeta, abstractmethod
|
|
|
|
from enum import Enum
|
|
|
|
|
2020-09-11 01:12:27 +02:00
|
|
|
from config import ctx
|
2017-03-26 03:16:54 +02:00
|
|
|
|
2020-10-14 18:55:57 +02:00
|
|
|
IWD_STORAGE_DIR = '/tmp/iwd'
|
|
|
|
IWD_CONFIG_DIR = '/tmp'
|
2016-10-01 00:03:10 +02:00
|
|
|
|
|
|
|
DBUS_OBJECT_MANAGER = 'org.freedesktop.DBus.ObjectManager'
|
|
|
|
DBUS_PROPERTIES = 'org.freedesktop.DBus.Properties'
|
|
|
|
|
|
|
|
IWD_SERVICE = 'net.connman.iwd'
|
|
|
|
IWD_WIPHY_INTERFACE = 'net.connman.iwd.Adapter'
|
|
|
|
IWD_AGENT_INTERFACE = 'net.connman.iwd.Agent'
|
|
|
|
IWD_AGENT_MANAGER_INTERFACE = 'net.connman.iwd.AgentManager'
|
|
|
|
IWD_DEVICE_INTERFACE = 'net.connman.iwd.Device'
|
2018-07-31 16:37:08 +02:00
|
|
|
IWD_KNOWN_NETWORK_INTERFACE = 'net.connman.iwd.KnownNetwork'
|
2016-10-01 00:03:10 +02:00
|
|
|
IWD_NETWORK_INTERFACE = 'net.connman.iwd.Network'
|
2019-10-24 17:43:22 +02:00
|
|
|
IWD_WSC_INTERFACE = 'net.connman.iwd.SimpleConfiguration'
|
2017-05-20 01:39:40 +02:00
|
|
|
IWD_SIGNAL_AGENT_INTERFACE = 'net.connman.iwd.SignalLevelAgent'
|
2018-06-30 01:29:18 +02:00
|
|
|
IWD_AP_INTERFACE = 'net.connman.iwd.AccessPoint'
|
2018-07-17 23:16:05 +02:00
|
|
|
IWD_ADHOC_INTERFACE = 'net.connman.iwd.AdHoc'
|
2018-09-05 06:08:15 +02:00
|
|
|
IWD_STATION_INTERFACE = 'net.connman.iwd.Station'
|
2020-10-09 20:08:52 +02:00
|
|
|
IWD_P2P_INTERFACE = 'net.connman.iwd.p2p.Device'
|
|
|
|
IWD_P2P_PEER_INTERFACE = 'net.connman.iwd.p2p.Peer'
|
|
|
|
IWD_P2P_SERVICE_MANAGER_INTERFACE = 'net.connman.iwd.p2p.ServiceManager'
|
|
|
|
IWD_P2P_WFD_INTERFACE = 'net.connman.iwd.p2p.Display'
|
2021-08-12 22:21:55 +02:00
|
|
|
IWD_STATION_DEBUG_INTERFACE = 'net.connman.iwd.StationDebug'
|
2016-10-01 00:03:10 +02:00
|
|
|
|
2019-10-29 00:08:54 +01:00
|
|
|
IWD_AGENT_MANAGER_PATH = '/net/connman/iwd'
|
2016-10-01 00:03:10 +02:00
|
|
|
IWD_TOP_LEVEL_PATH = '/'
|
|
|
|
|
|
|
|
class UnknownDBusEx(Exception): pass
|
|
|
|
class InProgressEx(dbus.DBusException): pass
|
|
|
|
class FailedEx(dbus.DBusException): pass
|
|
|
|
class AbortedEx(dbus.DBusException): pass
|
|
|
|
class NotAvailableEx(dbus.DBusException): pass
|
2019-10-24 17:54:45 +02:00
|
|
|
class InvalidArgumentsEx(dbus.DBusException): pass
|
2018-12-03 15:40:45 +01:00
|
|
|
class InvalidFormatEx(dbus.DBusException): pass
|
2016-10-01 00:03:10 +02:00
|
|
|
class AlreadyExistsEx(dbus.DBusException): pass
|
|
|
|
class NotFoundEx(dbus.DBusException): pass
|
|
|
|
class NotSupportedEx(dbus.DBusException): pass
|
|
|
|
class NoAgentEx(dbus.DBusException): pass
|
|
|
|
class NotConnectedEx(dbus.DBusException): pass
|
2017-10-26 22:26:00 +02:00
|
|
|
class NotConfiguredEx(dbus.DBusException): pass
|
2016-10-01 00:03:10 +02:00
|
|
|
class NotImplementedEx(dbus.DBusException): pass
|
2018-06-28 01:33:15 +02:00
|
|
|
class ServiceSetOverlapEx(dbus.DBusException): pass
|
2018-06-29 06:02:55 +02:00
|
|
|
class AlreadyProvisionedEx(dbus.DBusException): pass
|
|
|
|
class NotHiddenEx(dbus.DBusException): pass
|
2016-10-01 00:03:10 +02:00
|
|
|
class CanceledEx(dbus.DBusException):
|
|
|
|
_dbus_error_name = 'net.connman.iwd.Error.Canceled'
|
|
|
|
|
|
|
|
|
|
|
|
_dbus_ex_to_py = {
|
2019-10-24 17:54:45 +02:00
|
|
|
'Canceled' : CanceledEx,
|
|
|
|
'InProgress' : InProgressEx,
|
|
|
|
'Failed' : FailedEx,
|
|
|
|
'Aborted' : AbortedEx,
|
|
|
|
'NotAvailable' : NotAvailableEx,
|
|
|
|
'InvalidArguments' : InvalidArgumentsEx,
|
|
|
|
'InvalidFormat' : InvalidFormatEx,
|
|
|
|
'AlreadyExists' : AlreadyExistsEx,
|
|
|
|
'NotFound' : NotFoundEx,
|
|
|
|
'NotSupported' : NotSupportedEx,
|
|
|
|
'NoAgent' : NoAgentEx,
|
|
|
|
'NotConnected' : NotConnectedEx,
|
|
|
|
'NotConfigured' : NotConfiguredEx,
|
|
|
|
'NotImplemented' : NotImplementedEx,
|
|
|
|
'ServiceSetOverlap' : ServiceSetOverlapEx,
|
|
|
|
'AlreadyProvisioned' : AlreadyProvisionedEx,
|
|
|
|
'NotHidden' : NotHiddenEx,
|
2016-10-01 00:03:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def _convert_dbus_ex(dbus_ex):
|
|
|
|
ex_name = dbus_ex.get_dbus_name()
|
|
|
|
ex_short_name = ex_name[ex_name.rfind(".") + 1:]
|
|
|
|
if ex_short_name in _dbus_ex_to_py:
|
|
|
|
return _dbus_ex_to_py[ex_short_name](dbus_ex)
|
|
|
|
else:
|
|
|
|
return UnknownDBusEx(ex_name + ': ' + dbus_ex.get_dbus_message())
|
|
|
|
|
|
|
|
|
|
|
|
class AsyncOpAbstract(object):
|
|
|
|
__metaclass__ = ABCMeta
|
|
|
|
|
|
|
|
_is_completed = False
|
|
|
|
_exception = None
|
|
|
|
|
|
|
|
def _success(self):
|
|
|
|
self._is_completed = True
|
|
|
|
|
|
|
|
def _failure(self, ex):
|
|
|
|
self._is_completed = True
|
|
|
|
self._exception = _convert_dbus_ex(ex)
|
|
|
|
|
|
|
|
def _wait_for_async_op(self):
|
2021-08-13 18:24:38 +02:00
|
|
|
ctx.non_block_wait(lambda s: s._is_completed, 30, self, exception=None)
|
2016-10-01 00:03:10 +02:00
|
|
|
|
|
|
|
self._is_completed = False
|
|
|
|
if self._exception is not None:
|
2017-09-30 04:28:16 +02:00
|
|
|
tmp = self._exception
|
|
|
|
self._exception = None
|
|
|
|
raise tmp
|
2016-10-01 00:03:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
class IWDDBusAbstract(AsyncOpAbstract):
|
|
|
|
__metaclass__ = ABCMeta
|
|
|
|
|
2020-11-17 21:53:02 +01:00
|
|
|
def __init__(self, object_path = None, properties = None, service=IWD_SERVICE, namespace=ctx):
|
|
|
|
self._bus = namespace.get_bus()
|
2020-12-10 18:45:21 +01:00
|
|
|
self._namespace = namespace
|
2016-10-01 00:03:10 +02:00
|
|
|
|
|
|
|
self._object_path = object_path
|
2020-09-17 23:32:44 +02:00
|
|
|
proxy = self._bus.get_object(service, self._object_path)
|
2016-10-01 00:03:10 +02:00
|
|
|
self._iface = dbus.Interface(proxy, self._iface_name)
|
2018-12-14 20:15:26 +01:00
|
|
|
self._prop_proxy = dbus.Interface(proxy, DBUS_PROPERTIES)
|
2016-10-01 00:03:10 +02:00
|
|
|
|
|
|
|
if properties is None:
|
|
|
|
self._properties = self._prop_proxy.GetAll(self._iface_name)
|
|
|
|
else:
|
|
|
|
self._properties = properties
|
|
|
|
|
|
|
|
self._prop_proxy.connect_to_signal("PropertiesChanged",
|
|
|
|
self._property_changed_handler,
|
|
|
|
DBUS_PROPERTIES,
|
|
|
|
path_keyword="path")
|
|
|
|
|
|
|
|
def _property_changed_handler(self, interface, changed, invalidated, path):
|
|
|
|
if interface == self._iface_name and path == self._object_path:
|
|
|
|
for name, value in changed.items():
|
|
|
|
self._properties[name] = value
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def __str__(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class DeviceState(Enum):
|
|
|
|
'''Conection state of a device'''
|
|
|
|
connected = 'connected'
|
|
|
|
disconnected = 'disconnected'
|
|
|
|
connecting = 'connecting'
|
|
|
|
disconnecting = 'disconnecting'
|
2017-03-26 03:16:57 +02:00
|
|
|
roaming = 'roaming'
|
2016-10-01 00:03:10 +02:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.value
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_str(cls, string):
|
|
|
|
return getattr(cls, string, None)
|
|
|
|
|
|
|
|
|
|
|
|
class NetworkType(Enum):
|
|
|
|
'''Network security type'''
|
|
|
|
open = 'open'
|
|
|
|
psk = 'psk'
|
|
|
|
eap = '8021x'
|
2019-08-21 21:06:16 +02:00
|
|
|
hotspot = 'hotspot'
|
2016-10-01 00:03:10 +02:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return str(self.value)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_string(cls, string):
|
|
|
|
type = None
|
|
|
|
for attr in dir(cls):
|
|
|
|
if (str(getattr(cls, attr)) == string):
|
|
|
|
type = getattr(cls, attr)
|
|
|
|
break
|
|
|
|
return type
|
|
|
|
|
|
|
|
|
2017-05-20 01:39:40 +02:00
|
|
|
class SignalAgent(dbus.service.Object):
|
|
|
|
def __init__(self, passphrase = None):
|
|
|
|
self._path = '/test/agent/' + str(int(round(time.time() * 1000)))
|
|
|
|
|
2020-11-17 21:53:02 +01:00
|
|
|
dbus.service.Object.__init__(self, ctx.get_bus(), self._path)
|
2017-05-20 01:39:40 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def path(self):
|
|
|
|
return self._path
|
|
|
|
|
|
|
|
@dbus.service.method(IWD_SIGNAL_AGENT_INTERFACE,
|
|
|
|
in_signature='', out_signature='')
|
|
|
|
def Release(self):
|
|
|
|
print("SignalAgent released")
|
|
|
|
|
|
|
|
@dbus.service.method(IWD_SIGNAL_AGENT_INTERFACE,
|
|
|
|
in_signature='oy', out_signature='')
|
2018-08-14 03:33:04 +02:00
|
|
|
def Changed(self, path, level):
|
2017-05-20 01:39:40 +02:00
|
|
|
self.handle_new_level(str(path), int(level))
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def handle_new_level(self, path, level):
|
|
|
|
pass
|
|
|
|
|
2020-09-14 23:04:05 +02:00
|
|
|
class AdHocDevice(IWDDBusAbstract):
|
|
|
|
'''
|
|
|
|
Class represents an AdHoc device object: net.connman.iwd.AdHoc
|
|
|
|
'''
|
|
|
|
_iface_name = IWD_ADHOC_INTERFACE
|
|
|
|
|
|
|
|
@property
|
|
|
|
def started(self):
|
|
|
|
return self._properties['Started']
|
|
|
|
|
|
|
|
@property
|
|
|
|
def connected_peers(self):
|
|
|
|
return self._properties['ConnectedPeers']
|
|
|
|
|
2021-08-12 22:38:08 +02:00
|
|
|
class StationDebug(IWDDBusAbstract):
|
|
|
|
'''
|
|
|
|
Class represents net.connman.iwd.StationDebug
|
|
|
|
'''
|
|
|
|
_iface_name = IWD_STATION_DEBUG_INTERFACE
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self._debug_props = None
|
|
|
|
self._debug_iface = None
|
2021-08-13 21:47:09 +02:00
|
|
|
self._last_event = None
|
|
|
|
self._last_event_data = []
|
2021-08-12 22:38:08 +02:00
|
|
|
|
|
|
|
IWDDBusAbstract.__init__(self, *args, **kwargs)
|
|
|
|
|
2021-08-13 21:47:09 +02:00
|
|
|
self._iface.connect_to_signal("Event", self._event_handler)
|
|
|
|
|
|
|
|
def _event_handler(self, event, data):
|
|
|
|
self._last_event = event
|
|
|
|
self._last_event_data = data
|
|
|
|
|
2021-08-12 22:38:08 +02:00
|
|
|
@property
|
|
|
|
def autoconnect(self):
|
|
|
|
return self._properties['AutoConnect']
|
2017-05-20 01:39:40 +02:00
|
|
|
|
2021-08-13 01:12:29 +02:00
|
|
|
def scan(self, frequencies):
|
|
|
|
frequencies = dbus.Array([dbus.UInt16(f) for f in frequencies])
|
|
|
|
self._iface.Scan(frequencies)
|
|
|
|
|
2021-08-13 21:47:09 +02:00
|
|
|
def wait_for_event(self, event, timeout=10):
|
|
|
|
try:
|
|
|
|
if self._last_event is not None:
|
|
|
|
return self._last_event_data
|
|
|
|
|
|
|
|
ctx.non_block_wait(lambda s, e : s._last_event == e, timeout, self, event,
|
|
|
|
exception=TimeoutError("waiting for StationDebug.Event"))
|
|
|
|
|
|
|
|
return self._last_event_data
|
|
|
|
finally:
|
|
|
|
# Consume the event
|
|
|
|
self._last_event_data = None
|
|
|
|
self._last_event = None
|
|
|
|
|
|
|
|
|
2016-10-01 00:03:10 +02:00
|
|
|
class Device(IWDDBusAbstract):
|
|
|
|
'''
|
|
|
|
Class represents a network device object: net.connman.iwd.Device
|
|
|
|
with its properties and methods
|
|
|
|
'''
|
|
|
|
_iface_name = IWD_DEVICE_INTERFACE
|
2020-10-09 20:08:54 +02:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self._wps_manager_if = None
|
|
|
|
self._station_if = None
|
|
|
|
self._station_props = None
|
2021-08-12 22:38:08 +02:00
|
|
|
self._station_debug = None
|
2020-10-09 20:08:54 +02:00
|
|
|
IWDDBusAbstract.__init__(self, *args, **kwargs)
|
2016-10-05 01:44:25 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def _wps_manager(self):
|
|
|
|
if self._wps_manager_if is None:
|
|
|
|
_wps_manager_if =\
|
|
|
|
dbus.Interface(self._bus.get_object(IWD_SERVICE,
|
|
|
|
self.device_path),
|
|
|
|
IWD_WSC_INTERFACE)
|
|
|
|
return _wps_manager_if
|
2016-10-01 00:03:10 +02:00
|
|
|
|
2018-09-05 06:08:15 +02:00
|
|
|
@property
|
|
|
|
def _station(self):
|
|
|
|
if self._station_if is None:
|
2018-09-18 21:07:23 +02:00
|
|
|
self._station_if = dbus.Interface(self._bus.get_object(IWD_SERVICE,
|
2018-09-05 06:08:15 +02:00
|
|
|
self.device_path),
|
|
|
|
IWD_STATION_INTERFACE)
|
2018-09-18 21:07:23 +02:00
|
|
|
return self._station_if
|
|
|
|
|
|
|
|
def _station_properties(self):
|
|
|
|
if self._station_props is not None:
|
|
|
|
return self._station_props
|
|
|
|
|
|
|
|
if self._properties['Mode'] != 'station':
|
|
|
|
self._prop_proxy.Set(IWD_DEVICE_INTERFACE, 'Mode', 'station')
|
|
|
|
|
|
|
|
self._station_prop_if = \
|
|
|
|
dbus.Interface(self._bus.get_object(IWD_SERVICE,
|
|
|
|
self.device_path),
|
|
|
|
DBUS_PROPERTIES)
|
|
|
|
|
|
|
|
self._station_props = self._station_prop_if.GetAll(IWD_STATION_INTERFACE)
|
|
|
|
|
|
|
|
self._station_prop_if.connect_to_signal("PropertiesChanged",
|
|
|
|
self.__station_property_changed_handler,
|
|
|
|
DBUS_PROPERTIES, path_keyword="path")
|
|
|
|
|
|
|
|
return self._station_props
|
|
|
|
|
|
|
|
def __station_property_changed_handler(self, interface, changed,
|
|
|
|
invalidated, path):
|
|
|
|
if interface == IWD_STATION_INTERFACE and path == self._object_path:
|
|
|
|
for name, value in changed.items():
|
|
|
|
self._station_props[name] = value
|
2018-09-05 06:08:15 +02:00
|
|
|
|
2016-10-01 00:03:10 +02:00
|
|
|
@property
|
|
|
|
def device_path(self):
|
|
|
|
'''
|
|
|
|
Device's dbus path.
|
|
|
|
|
|
|
|
@rtype: string
|
|
|
|
'''
|
|
|
|
return self._object_path
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
'''
|
|
|
|
Device's interface name.
|
|
|
|
|
|
|
|
@rtype: string
|
|
|
|
'''
|
|
|
|
return self._properties['Name']
|
|
|
|
|
|
|
|
@property
|
|
|
|
def address(self):
|
|
|
|
'''
|
|
|
|
Interface's hardware address in the XX:XX:XX:XX:XX:XX format.
|
|
|
|
|
|
|
|
@rtype: string
|
|
|
|
'''
|
|
|
|
return self._properties['Address']
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
'''
|
|
|
|
Reflects the general network connection state.
|
|
|
|
|
|
|
|
@rtype: object (State)
|
|
|
|
'''
|
2018-09-18 21:07:23 +02:00
|
|
|
props = self._station_properties()
|
|
|
|
return DeviceState.from_str(props['State'])
|
2016-10-01 00:03:10 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def connected_network(self):
|
|
|
|
'''
|
|
|
|
net.connman.iwd.Network object representing the
|
|
|
|
network the device is currently connected to or to
|
|
|
|
which a connection is in progress.
|
|
|
|
|
|
|
|
@rtype: object (Network)
|
|
|
|
'''
|
2018-09-18 21:07:23 +02:00
|
|
|
props = self._station_properties()
|
|
|
|
return props.get('ConnectedNetwork')
|
2016-10-01 00:03:10 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def powered(self):
|
|
|
|
'''
|
|
|
|
True if the interface is UP. If false, the device's radio is
|
|
|
|
powered down and no other actions can be performed on the device.
|
|
|
|
|
|
|
|
@rtype: boolean
|
|
|
|
'''
|
|
|
|
return bool(self._properties['Powered'])
|
|
|
|
|
|
|
|
@property
|
|
|
|
def scanning(self):
|
|
|
|
'''
|
|
|
|
Reflects whether the device is currently scanning
|
|
|
|
for networks. net.connman.iwd.Network objects are
|
|
|
|
updated when this property goes from true to false.
|
|
|
|
|
|
|
|
@rtype: boolean
|
|
|
|
'''
|
2018-09-18 21:07:23 +02:00
|
|
|
props = self._station_properties()
|
|
|
|
return bool(props['Scanning'])
|
2016-10-01 00:03:10 +02:00
|
|
|
|
2021-08-12 22:38:08 +02:00
|
|
|
@property
|
|
|
|
def autoconnect(self):
|
|
|
|
if not self._station_debug:
|
|
|
|
self._station_debug = StationDebug(self._object_path)
|
|
|
|
|
|
|
|
return self._station_debug.autoconnect
|
|
|
|
|
|
|
|
@autoconnect.setter
|
|
|
|
def autoconnect(self, value):
|
|
|
|
if not self._station_debug:
|
|
|
|
self._station_debug = StationDebug(self._object_path)
|
|
|
|
|
|
|
|
self._station_debug._prop_proxy.Set(IWD_STATION_DEBUG_INTERFACE,
|
|
|
|
'AutoConnect', value)
|
|
|
|
|
2016-10-01 00:03:10 +02:00
|
|
|
def scan(self):
|
|
|
|
'''Schedule a network scan.
|
|
|
|
|
|
|
|
Possible exception: BusyEx
|
|
|
|
FailedEx
|
|
|
|
'''
|
2018-09-05 17:39:57 +02:00
|
|
|
self._iface.Scan(dbus_interface=IWD_STATION_INTERFACE,
|
2016-10-01 00:03:10 +02:00
|
|
|
reply_handler=self._success,
|
|
|
|
error_handler=self._failure)
|
|
|
|
|
|
|
|
self._wait_for_async_op()
|
|
|
|
|
|
|
|
def disconnect(self):
|
|
|
|
'''Disconnect from the network
|
|
|
|
|
|
|
|
Possible exception: BusyEx
|
|
|
|
FailedEx
|
|
|
|
NotConnectedEx
|
|
|
|
'''
|
2018-09-05 17:39:57 +02:00
|
|
|
self._iface.Disconnect(dbus_interface=IWD_STATION_INTERFACE,
|
2016-10-01 00:03:10 +02:00
|
|
|
reply_handler=self._success,
|
|
|
|
error_handler=self._failure)
|
|
|
|
|
|
|
|
self._wait_for_async_op()
|
|
|
|
|
2021-08-13 01:12:31 +02:00
|
|
|
def get_ordered_networks(self, scan_if_needed = True, full_scan = False):
|
2016-10-01 00:03:10 +02:00
|
|
|
'''Return the list of networks found in the most recent
|
|
|
|
scan, sorted by their user interface importance
|
|
|
|
score as calculated by iwd. If the device is
|
|
|
|
currently connected to a network, that network is
|
|
|
|
always first on the list, followed by any known
|
|
|
|
networks that have been used at least once before,
|
|
|
|
followed by any other known networks and any other
|
|
|
|
detected networks as the last group. Within these
|
|
|
|
groups the maximum relative signal-strength is the
|
|
|
|
main sorting factor.
|
|
|
|
'''
|
|
|
|
ordered_networks = []
|
2018-09-05 17:39:57 +02:00
|
|
|
for bus_obj in self._station.GetOrderedNetworks():
|
2020-12-10 18:45:21 +01:00
|
|
|
ordered_network = OrderedNetwork(bus_obj, self._bus, self._namespace)
|
2016-10-01 00:03:10 +02:00
|
|
|
ordered_networks.append(ordered_network)
|
2019-10-28 20:51:59 +01:00
|
|
|
|
2020-05-06 23:32:14 +02:00
|
|
|
if len(ordered_networks) > 0:
|
|
|
|
return ordered_networks
|
|
|
|
elif not scan_if_needed:
|
2019-10-28 20:51:59 +01:00
|
|
|
return None
|
|
|
|
|
2021-08-12 22:38:09 +02:00
|
|
|
condition = 'not obj.scanning'
|
|
|
|
IWD._wait_for_object_condition(self, condition)
|
|
|
|
|
|
|
|
try:
|
2021-08-13 01:12:31 +02:00
|
|
|
if full_scan:
|
|
|
|
self.scan()
|
|
|
|
else:
|
|
|
|
self.debug_scan(ctx.get_frequencies())
|
2021-08-12 22:38:09 +02:00
|
|
|
except InProgressEx:
|
|
|
|
pass
|
2020-05-06 23:32:14 +02:00
|
|
|
|
|
|
|
condition = 'obj.scanning'
|
2020-11-16 23:25:08 +01:00
|
|
|
IWD._wait_for_object_condition(self, condition)
|
2020-05-06 23:32:14 +02:00
|
|
|
condition = 'not obj.scanning'
|
2020-11-16 23:25:08 +01:00
|
|
|
IWD._wait_for_object_condition(self, condition)
|
2020-05-06 23:32:14 +02:00
|
|
|
|
|
|
|
for bus_obj in self._station.GetOrderedNetworks():
|
2020-12-10 18:45:21 +01:00
|
|
|
ordered_network = OrderedNetwork(bus_obj, self._bus, self._namespace)
|
2020-05-06 23:32:14 +02:00
|
|
|
ordered_networks.append(ordered_network)
|
|
|
|
|
2020-05-07 21:20:21 +02:00
|
|
|
if len(ordered_networks) > 0:
|
|
|
|
return ordered_networks
|
|
|
|
|
|
|
|
return None
|
2016-10-01 00:03:10 +02:00
|
|
|
|
2021-08-13 01:12:31 +02:00
|
|
|
def get_ordered_network(self, network, scan_if_needed = True, full_scan = False):
|
2018-12-14 20:15:27 +01:00
|
|
|
'''Returns a single network from ordered network call, or None if the
|
|
|
|
network wasn't found. If the network is not found an exception is
|
|
|
|
raised, this removes the need to extra asserts in autotests.
|
|
|
|
'''
|
2020-05-06 23:32:14 +02:00
|
|
|
ordered_networks = self.get_ordered_networks(scan_if_needed)
|
2018-12-14 20:15:27 +01:00
|
|
|
|
2019-10-28 20:51:59 +01:00
|
|
|
if not ordered_networks:
|
|
|
|
raise Exception('Network %s not found' % network)
|
|
|
|
|
2018-12-14 20:15:27 +01:00
|
|
|
for n in ordered_networks:
|
|
|
|
if n.name == network:
|
|
|
|
return n
|
|
|
|
|
|
|
|
raise Exception('Network %s not found' % network)
|
|
|
|
|
2016-10-05 01:44:25 +02:00
|
|
|
def wps_push_button(self):
|
|
|
|
self._wps_manager.PushButton(dbus_interface=IWD_WSC_INTERFACE,
|
|
|
|
reply_handler=self._success,
|
|
|
|
error_handler=self._failure)
|
|
|
|
self._wait_for_async_op()
|
|
|
|
|
2018-09-24 19:02:34 +02:00
|
|
|
def wps_generate_pin(self):
|
|
|
|
return self._wps_manager.GeneratePin()
|
|
|
|
|
|
|
|
def wps_start_pin(self, pin):
|
|
|
|
self._wps_manager.StartPin(pin, reply_handler=self._success,
|
|
|
|
error_handler=self._failure)
|
|
|
|
|
2016-10-05 01:44:25 +02:00
|
|
|
def wps_cancel(self):
|
|
|
|
self._wps_manager.Cancel(dbus_interface=IWD_WSC_INTERFACE,
|
|
|
|
reply_handler=self._success,
|
|
|
|
error_handler=self._failure)
|
|
|
|
self._wait_for_async_op()
|
|
|
|
|
2017-05-20 01:39:40 +02:00
|
|
|
def register_signal_agent(self, signal_agent, levels):
|
2018-09-05 06:08:15 +02:00
|
|
|
self._station.RegisterSignalLevelAgent(signal_agent.path,
|
|
|
|
dbus.Array(levels, 'n'),
|
|
|
|
dbus_interface=IWD_STATION_INTERFACE,
|
|
|
|
reply_handler=self._success,
|
|
|
|
error_handler=self._failure)
|
2017-05-20 01:39:40 +02:00
|
|
|
self._wait_for_async_op()
|
|
|
|
|
|
|
|
def unregister_signal_agent(self, signal_agent):
|
2018-09-05 06:08:15 +02:00
|
|
|
self._station.UnregisterSignalLevelAgent(signal_agent.path,
|
|
|
|
dbus_interface=IWD_STATION_INTERFACE,
|
|
|
|
reply_handler=self._success,
|
|
|
|
error_handler=self._failure)
|
2017-05-20 01:39:40 +02:00
|
|
|
self._wait_for_async_op()
|
|
|
|
|
2020-11-02 19:28:51 +01:00
|
|
|
def start_ap(self, ssid, psk=None):
|
2019-05-13 23:29:27 +02:00
|
|
|
try:
|
|
|
|
self._prop_proxy.Set(IWD_DEVICE_INTERFACE, 'Mode', 'ap')
|
|
|
|
except Exception as e:
|
|
|
|
raise _convert_dbus_ex(e)
|
|
|
|
|
2018-06-30 01:29:18 +02:00
|
|
|
self._ap_iface = dbus.Interface(self._bus.get_object(IWD_SERVICE,
|
|
|
|
self.device_path),
|
|
|
|
IWD_AP_INTERFACE)
|
2020-11-02 19:28:51 +01:00
|
|
|
if psk:
|
|
|
|
self._ap_iface.Start(ssid, psk, reply_handler=self._success,
|
|
|
|
error_handler=self._failure)
|
|
|
|
else:
|
|
|
|
self._ap_iface.StartProfile(ssid, reply_handler=self._success,
|
|
|
|
error_handler=self._failure)
|
2017-10-21 01:27:22 +02:00
|
|
|
self._wait_for_async_op()
|
|
|
|
|
|
|
|
def stop_ap(self):
|
2018-06-30 01:29:18 +02:00
|
|
|
self._prop_proxy.Set(IWD_DEVICE_INTERFACE, 'Mode', 'station')
|
2017-10-21 01:27:22 +02:00
|
|
|
|
2018-06-28 01:33:15 +02:00
|
|
|
def connect_hidden_network(self, name):
|
|
|
|
'''Connect to a hidden network
|
|
|
|
Possible exception: BusyEx
|
|
|
|
FailedEx
|
2019-10-24 17:54:45 +02:00
|
|
|
InvalidArgumentsEx
|
2018-06-28 01:33:15 +02:00
|
|
|
NotConfiguredEx
|
|
|
|
NotConnectedEx
|
|
|
|
NotFoundEx
|
|
|
|
ServiceSetOverlapEx
|
|
|
|
'''
|
2018-09-05 17:39:57 +02:00
|
|
|
self._iface.ConnectHiddenNetwork(name,
|
|
|
|
dbus_interface=IWD_STATION_INTERFACE,
|
2018-06-28 01:33:15 +02:00
|
|
|
reply_handler=self._success,
|
|
|
|
error_handler=self._failure)
|
2018-07-22 14:15:15 +02:00
|
|
|
self._wait_for_async_op()
|
2018-06-28 01:33:15 +02:00
|
|
|
|
2020-06-10 03:22:20 +02:00
|
|
|
def connect_hidden_network_async(self, name, reply_handler, error_handler):
|
|
|
|
'''Connect to a hidden network
|
|
|
|
Possible exception: BusyEx
|
|
|
|
FailedEx
|
|
|
|
InvalidArgumentsEx
|
|
|
|
NotConfiguredEx
|
|
|
|
NotConnectedEx
|
|
|
|
NotFoundEx
|
|
|
|
ServiceSetOverlapEx
|
|
|
|
'''
|
|
|
|
self._iface.ConnectHiddenNetwork(name,
|
|
|
|
dbus_interface=IWD_STATION_INTERFACE,
|
|
|
|
reply_handler=reply_handler,
|
|
|
|
error_handler=error_handler)
|
|
|
|
|
2018-07-17 23:16:05 +02:00
|
|
|
def start_adhoc(self, ssid, psk=None):
|
|
|
|
self._prop_proxy.Set(IWD_DEVICE_INTERFACE, 'Mode', 'ad-hoc')
|
|
|
|
self._adhoc_iface = dbus.Interface(self._bus.get_object(IWD_SERVICE,
|
|
|
|
self.device_path),
|
|
|
|
IWD_ADHOC_INTERFACE)
|
|
|
|
if not psk:
|
|
|
|
self._adhoc_iface.StartOpen(ssid, reply_handler=self._success,
|
|
|
|
error_handler=self._failure)
|
|
|
|
else:
|
|
|
|
self._adhoc_iface.Start(ssid, psk, reply_handler=self._success,
|
|
|
|
error_handler=self._failure)
|
2018-06-28 01:33:15 +02:00
|
|
|
self._wait_for_async_op()
|
|
|
|
|
2020-09-14 23:04:05 +02:00
|
|
|
return AdHocDevice(self.device_path)
|
|
|
|
|
2018-07-17 23:16:05 +02:00
|
|
|
def stop_adhoc(self):
|
|
|
|
self._prop_proxy.Set(IWD_DEVICE_INTERFACE, 'Mode', 'station')
|
|
|
|
|
2021-08-12 22:21:55 +02:00
|
|
|
def connect_bssid(self, address):
|
|
|
|
self._station_debug_if = dbus.Interface(self._bus.get_object(IWD_SERVICE,
|
|
|
|
self.device_path),
|
|
|
|
IWD_STATION_DEBUG_INTERFACE)
|
|
|
|
self._station_debug_if.ConnectBssid(dbus.ByteArray.fromhex(address.replace(':', '')))
|
|
|
|
|
2021-08-12 22:21:56 +02:00
|
|
|
def roam(self, address):
|
|
|
|
self._station_debug_if = dbus.Interface(self._bus.get_object(IWD_SERVICE,
|
|
|
|
self.device_path),
|
|
|
|
IWD_STATION_DEBUG_INTERFACE)
|
|
|
|
self._station_debug_if.Roam(dbus.ByteArray.fromhex(address.replace(':', '')))
|
|
|
|
|
2021-08-13 01:12:29 +02:00
|
|
|
def debug_scan(self, frequencies):
|
|
|
|
if not self._station_debug:
|
|
|
|
self._station_debug = StationDebug(self._object_path)
|
|
|
|
|
|
|
|
self._station_debug.scan(frequencies)
|
|
|
|
|
2021-08-13 21:47:09 +02:00
|
|
|
def wait_for_event(self, event, timeout=10):
|
|
|
|
if not self._station_debug:
|
|
|
|
self._station_debug = StationDebug(self._object_path)
|
|
|
|
|
|
|
|
self._station_debug.wait_for_event(event, timeout)
|
|
|
|
|
2016-10-05 01:26:41 +02:00
|
|
|
def __str__(self, prefix = ''):
|
|
|
|
return prefix + 'Device: ' + self.device_path + '\n'\
|
|
|
|
+ prefix + '\tName:\t\t' + self.name + '\n'\
|
|
|
|
+ prefix + '\tAddress:\t' + self.address + '\n'\
|
|
|
|
+ prefix + '\tState:\t\t' + str(self.state) + '\n'\
|
|
|
|
+ prefix + '\tPowered:\t' + str(self.powered) + '\n'\
|
|
|
|
+ prefix + '\tConnected net:\t' + str(self.connected_network) +\
|
|
|
|
'\n'
|
2016-10-01 00:03:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Network(IWDDBusAbstract):
|
|
|
|
'''Class represents a network object: net.connman.iwd.Network'''
|
|
|
|
_iface_name = IWD_NETWORK_INTERFACE
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
'''
|
|
|
|
Network SSID.
|
|
|
|
|
|
|
|
@rtype: string
|
|
|
|
'''
|
|
|
|
return self._properties['Name']
|
|
|
|
|
|
|
|
@property
|
|
|
|
def connected(self):
|
|
|
|
'''
|
|
|
|
Reflects whether the device is connected to this network.
|
|
|
|
|
|
|
|
@rtype: boolean
|
|
|
|
'''
|
|
|
|
return bool(self._properties['Connected'])
|
|
|
|
|
2018-08-09 20:14:01 +02:00
|
|
|
def connect(self, wait=True):
|
2016-10-01 00:03:10 +02:00
|
|
|
'''
|
|
|
|
Connect to the network. Request the device implied by the object
|
|
|
|
path to connect to specified network.
|
|
|
|
|
|
|
|
Possible exception: AbortedEx
|
|
|
|
BusyEx
|
|
|
|
FailedEx
|
|
|
|
NoAgentEx
|
|
|
|
NotSupportedEx
|
|
|
|
TimeoutEx
|
|
|
|
|
|
|
|
@rtype: void
|
|
|
|
'''
|
|
|
|
|
|
|
|
self._iface.Connect(dbus_interface=self._iface_name,
|
|
|
|
reply_handler=self._success,
|
|
|
|
error_handler=self._failure)
|
|
|
|
|
2018-08-09 20:14:01 +02:00
|
|
|
if wait:
|
|
|
|
self._wait_for_async_op()
|
2016-10-01 00:03:10 +02:00
|
|
|
|
|
|
|
def __str__(self, prefix = ''):
|
|
|
|
return prefix + 'Network:\n' \
|
|
|
|
+ prefix + '\tName:\t' + self.name + '\n' \
|
|
|
|
+ prefix + '\tConnected:\t' + str(self.connected)
|
|
|
|
|
|
|
|
|
2018-07-31 16:37:08 +02:00
|
|
|
class KnownNetwork(IWDDBusAbstract):
|
|
|
|
'''Class represents a known network object: net.connman.iwd.KnownNetwork'''
|
|
|
|
_iface_name = IWD_KNOWN_NETWORK_INTERFACE
|
|
|
|
|
|
|
|
def forget(self):
|
|
|
|
'''
|
|
|
|
Removes information saved by IWD about this network
|
|
|
|
causing it to be treated as if IWD had never connected
|
|
|
|
to it before.
|
|
|
|
'''
|
|
|
|
self._iface.Forget(dbus_interface=self._iface_name,
|
|
|
|
reply_handler=self._success,
|
|
|
|
error_handler=self._failure)
|
|
|
|
self._wait_for_async_op()
|
2016-10-01 00:03:10 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
'''Contains the Name (SSID) of the network.'''
|
2018-08-09 16:33:46 +02:00
|
|
|
return str(self._properties['Name'])
|
2016-10-01 00:03:10 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def type(self):
|
|
|
|
'''Contains the type of the network.'''
|
2018-07-31 16:37:08 +02:00
|
|
|
return NetworkType(self._properties['Type'])
|
2016-10-01 00:03:10 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def last_connected_time(self):
|
|
|
|
'''
|
|
|
|
Contains the last time this network has been connected to.
|
2018-07-31 16:37:08 +02:00
|
|
|
If the network is known, but has never been successfully
|
|
|
|
connected to, this attribute is set to None.
|
2016-10-01 00:03:10 +02:00
|
|
|
|
2018-07-31 16:37:08 +02:00
|
|
|
@rtype: datetime
|
2016-10-01 00:03:10 +02:00
|
|
|
'''
|
2018-07-31 16:37:08 +02:00
|
|
|
if 'LastConnectedTime' not in self._properties:
|
|
|
|
return None
|
2016-10-01 00:03:10 +02:00
|
|
|
|
2018-07-31 16:37:08 +02:00
|
|
|
val = self._properties['LastConnectedTime']
|
|
|
|
return datetime.datetime.strptime(val, "%Y-%m-%dT%H:%M:%SZ")
|
2016-10-01 00:03:10 +02:00
|
|
|
|
2016-10-05 01:26:38 +02:00
|
|
|
def __str__(self, prefix = ''):
|
|
|
|
return prefix + 'Known Network:\n' \
|
|
|
|
+ prefix + '\tName:\t' + self.name + '\n' \
|
|
|
|
+ prefix + '\tType:\t' + str(self.type) + '\n' \
|
2018-07-31 16:37:08 +02:00
|
|
|
+ prefix + '\tLast connected:\t' + str(self.last_connected_time)
|
2016-10-01 00:03:10 +02:00
|
|
|
|
|
|
|
class OrderedNetwork(object):
|
|
|
|
'''Represents a network found in the scan'''
|
|
|
|
|
2020-12-10 18:45:21 +01:00
|
|
|
def __init__(self, o_n_tuple, bus, namespace=ctx):
|
2020-11-17 21:53:02 +01:00
|
|
|
self._bus = bus
|
2020-12-10 18:45:21 +01:00
|
|
|
self._network_object = Network(o_n_tuple[0], namespace=namespace)
|
2018-09-13 20:28:38 +02:00
|
|
|
self._network_proxy = dbus.Interface(self._bus.get_object(IWD_SERVICE,
|
|
|
|
o_n_tuple[0]),
|
|
|
|
DBUS_PROPERTIES)
|
|
|
|
self._properties = self._network_proxy.GetAll(IWD_NETWORK_INTERFACE)
|
|
|
|
|
|
|
|
self._name = self._properties['Name']
|
|
|
|
self._signal_strength = o_n_tuple[1]
|
|
|
|
self._type = NetworkType.from_string(str(self._properties['Type']))
|
2016-10-01 00:03:10 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def network_object(self):
|
|
|
|
'''
|
|
|
|
net.connman.iwd.Network object representing the network.
|
|
|
|
|
|
|
|
@rtype: Network
|
|
|
|
'''
|
|
|
|
return self._network_object
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
'''
|
|
|
|
Device's interface name.
|
|
|
|
|
|
|
|
@rtype: string
|
|
|
|
'''
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def signal_strength(self):
|
|
|
|
'''
|
|
|
|
Network's maximum signal strength expressed in 100 * dBm.
|
|
|
|
The value is the range of 0 (strongest signal) to
|
|
|
|
-10000 (weakest signal)
|
|
|
|
|
|
|
|
@rtype: number
|
|
|
|
'''
|
|
|
|
return self._signal_strength
|
|
|
|
|
|
|
|
@property
|
|
|
|
def type(self):
|
|
|
|
'''
|
|
|
|
Contains the type of the network.
|
|
|
|
|
|
|
|
@rtype: NetworkType
|
|
|
|
'''
|
|
|
|
return self._type
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return 'Ordered Network:\n'\
|
2018-09-13 20:28:38 +02:00
|
|
|
'\tName:\t\t' + self._name + '\n'\
|
|
|
|
'\tNetwork Type:\t' + str(self._type) + '\n'\
|
2016-10-01 00:03:10 +02:00
|
|
|
'\tSignal Strength:'\
|
|
|
|
+ ('None' if self.signal_strength is None else\
|
|
|
|
str(self.signal_strength)) + '\n'\
|
|
|
|
'\tObject: \n' + self.network_object.__str__('\t\t')
|
|
|
|
|
2020-11-03 20:06:41 +01:00
|
|
|
agent_count = 0
|
2016-10-01 00:03:10 +02:00
|
|
|
|
|
|
|
class PSKAgent(dbus.service.Object):
|
|
|
|
|
2020-11-17 21:53:02 +01:00
|
|
|
def __init__(self, passphrases=[], users=[], namespace=ctx):
|
2020-11-03 20:06:41 +01:00
|
|
|
global agent_count
|
|
|
|
|
2018-04-18 07:03:38 +02:00
|
|
|
if type(passphrases) != list:
|
|
|
|
passphrases = [passphrases]
|
2018-04-18 07:03:39 +02:00
|
|
|
self.passphrases = passphrases
|
|
|
|
if type(users) != list:
|
|
|
|
users = [users]
|
|
|
|
self.users = users
|
2020-11-03 20:06:41 +01:00
|
|
|
self._path = '/test/agent/%s' % agent_count
|
|
|
|
|
|
|
|
agent_count += 1
|
2016-10-01 00:03:10 +02:00
|
|
|
|
2020-11-17 21:53:02 +01:00
|
|
|
dbus.service.Object.__init__(self, namespace.get_bus(), self._path)
|
2016-10-01 00:03:10 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def path(self):
|
|
|
|
return self._path
|
|
|
|
|
|
|
|
@dbus.service.method(IWD_AGENT_INTERFACE, in_signature='', out_signature='')
|
|
|
|
def Release(self):
|
|
|
|
print("Agent released")
|
|
|
|
|
2018-04-18 07:03:39 +02:00
|
|
|
@dbus.service.method(IWD_AGENT_INTERFACE, in_signature='s',
|
|
|
|
out_signature='')
|
|
|
|
def Cancel(self, reason):
|
|
|
|
print("Cancel: " + reason)
|
|
|
|
|
|
|
|
|
2016-10-01 00:03:10 +02:00
|
|
|
@dbus.service.method(IWD_AGENT_INTERFACE, in_signature='o',
|
|
|
|
out_signature='s')
|
|
|
|
def RequestPassphrase(self, path):
|
2018-04-18 07:03:39 +02:00
|
|
|
print('Requested PSK for ' + path)
|
2016-10-01 00:03:10 +02:00
|
|
|
|
2018-04-18 07:03:39 +02:00
|
|
|
if not self.passphrases:
|
2016-10-01 00:03:10 +02:00
|
|
|
raise CanceledEx("canceled")
|
|
|
|
|
2018-04-18 07:03:39 +02:00
|
|
|
return self.passphrases.pop(0)
|
2016-10-01 00:03:10 +02:00
|
|
|
|
2018-04-18 07:03:39 +02:00
|
|
|
@dbus.service.method(IWD_AGENT_INTERFACE, in_signature='o',
|
|
|
|
out_signature='s')
|
|
|
|
def RequestPrivateKeyPassphrase(self, path):
|
|
|
|
print('Requested private-key passphrase for ' + path)
|
|
|
|
|
|
|
|
if not self.passphrases:
|
|
|
|
raise CanceledEx("canceled")
|
|
|
|
|
|
|
|
return self.passphrases.pop(0)
|
|
|
|
|
|
|
|
@dbus.service.method(IWD_AGENT_INTERFACE, in_signature='o',
|
|
|
|
out_signature='ss')
|
|
|
|
def RequestUserNameAndPassword(self, path):
|
|
|
|
print('Requested the user name and password for ' + path)
|
|
|
|
|
|
|
|
if not self.users:
|
|
|
|
raise CanceledEx("canceled")
|
|
|
|
|
|
|
|
return self.users.pop(0)
|
|
|
|
|
|
|
|
@dbus.service.method(IWD_AGENT_INTERFACE, in_signature='os',
|
|
|
|
out_signature='s')
|
|
|
|
def RequestUserPassword(self, path, req_user):
|
|
|
|
print('Requested the password for ' + path + ' for user ' + req_user)
|
|
|
|
|
|
|
|
if not self.users:
|
|
|
|
raise CanceledEx("canceled")
|
|
|
|
|
|
|
|
user, passwd = self.users.pop(0)
|
|
|
|
if user != req_user:
|
|
|
|
raise CanceledEx("canceled")
|
|
|
|
|
|
|
|
return passwd
|
2016-10-01 00:03:10 +02:00
|
|
|
|
|
|
|
|
2020-10-09 20:08:52 +02:00
|
|
|
class P2PDevice(IWDDBusAbstract):
|
|
|
|
_iface_name = IWD_P2P_INTERFACE
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self._discovery_request = False
|
|
|
|
self._peer_dict = {}
|
|
|
|
IWDDBusAbstract.__init__(self, *args, **kwargs)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
return str(self._properties['Name'])
|
|
|
|
|
|
|
|
@name.setter
|
|
|
|
def name(self, name):
|
|
|
|
self._prop_proxy.Set(self._iface_name, 'Name', name)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def enabled(self):
|
|
|
|
return bool(self._properties['Enabled'])
|
|
|
|
|
|
|
|
@enabled.setter
|
|
|
|
def enabled(self, enabled):
|
|
|
|
self._prop_proxy.Set(self._iface_name, 'Enabled', enabled)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def discovery_request(self):
|
|
|
|
return self._discovery_request
|
|
|
|
|
|
|
|
@discovery_request.setter
|
|
|
|
def discovery_request(self, req):
|
|
|
|
if self._discovery_request == bool(req):
|
|
|
|
return
|
|
|
|
|
|
|
|
if bool(req):
|
|
|
|
self._iface.RequestDiscovery()
|
|
|
|
else:
|
|
|
|
self._iface.ReleaseDiscovery()
|
|
|
|
|
|
|
|
self._discovery_request = bool(req)
|
|
|
|
|
|
|
|
def get_peers(self):
|
|
|
|
old_dict = self._peer_dict
|
|
|
|
self._peer_dict = {}
|
|
|
|
|
|
|
|
for path, rssi in self._iface.GetPeers():
|
2021-06-04 03:50:49 +02:00
|
|
|
self._peer_dict[path] = old_dict[path] if path in old_dict else P2PPeer(path, namespace=self._namespace)
|
2020-10-09 20:08:52 +02:00
|
|
|
self._peer_dict[path].rssi = rssi
|
|
|
|
|
|
|
|
return self._peer_dict
|
|
|
|
|
|
|
|
|
|
|
|
class P2PPeer(IWDDBusAbstract):
|
|
|
|
_iface_name = IWD_P2P_PEER_INTERFACE
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
return str(self._properties['Name'])
|
|
|
|
|
|
|
|
@property
|
|
|
|
def category(self):
|
|
|
|
return str(self._properties['DeviceCategory'])
|
|
|
|
|
|
|
|
@property
|
|
|
|
def subcategory(self):
|
|
|
|
return str(self._properties['DeviceSubcategory'])
|
|
|
|
|
|
|
|
@property
|
|
|
|
def connected(self):
|
|
|
|
return bool(self._properties['Connected'])
|
|
|
|
|
|
|
|
@property
|
|
|
|
def connected_interface(self):
|
|
|
|
return str(self._properties['ConnectedInterface'])
|
|
|
|
|
|
|
|
@property
|
|
|
|
def connected_ip(self):
|
|
|
|
return str(self._properties['ConnectedIP'])
|
|
|
|
|
|
|
|
def connect(self, wait=True, pin=None):
|
|
|
|
if pin is None:
|
|
|
|
self._iface.PushButton(dbus_interface=IWD_WSC_INTERFACE,
|
|
|
|
reply_handler=self._success,
|
|
|
|
error_handler=self._failure)
|
|
|
|
else:
|
|
|
|
self._iface.StartPin(pin,
|
|
|
|
dbus_interface=IWD_WSC_INTERFACE,
|
|
|
|
reply_handler=self._success,
|
|
|
|
error_handler=self._failure)
|
|
|
|
|
|
|
|
if wait:
|
|
|
|
self._wait_for_async_op()
|
|
|
|
return (self.connected_interface, self.connected_ip)
|
|
|
|
|
|
|
|
def disconnect(self):
|
|
|
|
self._iface.Disconnect()
|
|
|
|
|
|
|
|
|
2018-05-15 20:43:12 +02:00
|
|
|
class DeviceList(collections.Mapping):
|
2018-06-28 01:33:14 +02:00
|
|
|
def __init__(self, iwd):
|
2018-05-15 20:43:12 +02:00
|
|
|
self._dict = {}
|
2020-10-09 20:08:52 +02:00
|
|
|
self._p2p_dict = {}
|
2020-11-17 21:53:02 +01:00
|
|
|
self._namespace = iwd.namespace
|
2018-05-15 20:43:12 +02:00
|
|
|
|
|
|
|
iwd._object_manager.connect_to_signal("InterfacesAdded",
|
2018-06-28 01:33:14 +02:00
|
|
|
self._interfaces_added_handler)
|
2018-05-15 20:43:12 +02:00
|
|
|
iwd._object_manager.connect_to_signal("InterfacesRemoved",
|
2018-06-28 01:33:14 +02:00
|
|
|
self._interfaces_removed_handler)
|
|
|
|
|
|
|
|
objects = iwd._object_manager.GetManagedObjects()
|
2018-05-15 20:43:12 +02:00
|
|
|
|
|
|
|
for path in objects:
|
|
|
|
for interface in objects[path]:
|
|
|
|
if interface == IWD_DEVICE_INTERFACE:
|
2020-11-17 21:53:02 +01:00
|
|
|
self._dict[path] = Device(path, objects[path][interface],
|
|
|
|
namespace=self._namespace)
|
2020-10-09 20:08:52 +02:00
|
|
|
elif interface == IWD_P2P_INTERFACE:
|
2020-11-17 21:53:02 +01:00
|
|
|
self._p2p_dict[path] = P2PDevice(path, objects[path][interface],
|
|
|
|
namespace=self._namespace)
|
2018-05-15 20:43:12 +02:00
|
|
|
|
|
|
|
def __getitem__(self, key):
|
|
|
|
return self._dict.__getitem__(key)
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
return self._dict.__iter__()
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return self._dict.__len__()
|
|
|
|
|
|
|
|
def __delitem__(self, key):
|
|
|
|
self._dict.pop(key).remove()
|
|
|
|
|
|
|
|
def _interfaces_added_handler(self, path, interfaces):
|
2018-06-28 01:33:14 +02:00
|
|
|
if IWD_DEVICE_INTERFACE in interfaces:
|
2020-11-17 21:53:02 +01:00
|
|
|
self._dict[path] = Device(path, interfaces[IWD_DEVICE_INTERFACE],
|
|
|
|
namespace=self._namespace)
|
2020-10-09 20:08:52 +02:00
|
|
|
elif IWD_P2P_INTERFACE in interfaces:
|
2020-11-17 21:53:02 +01:00
|
|
|
self._p2p_dict[path] = P2PDevice(path, interfaces[IWD_P2P_INTERFACE],
|
|
|
|
namespace=self._namespace)
|
2018-05-15 20:43:12 +02:00
|
|
|
|
|
|
|
def _interfaces_removed_handler(self, path, interfaces):
|
2018-06-28 01:33:14 +02:00
|
|
|
if IWD_DEVICE_INTERFACE in interfaces:
|
|
|
|
del self._dict[path]
|
2020-10-09 20:08:52 +02:00
|
|
|
elif IWD_P2P_INTERFACE in interfaces:
|
|
|
|
del self._p2p_dict[path]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def p2p_dict(self):
|
|
|
|
return self._p2p_dict
|
2018-05-15 20:43:12 +02:00
|
|
|
|
|
|
|
|
2016-10-01 00:03:10 +02:00
|
|
|
class IWD(AsyncOpAbstract):
|
2020-09-11 01:12:27 +02:00
|
|
|
'''
|
|
|
|
Start an IWD instance. By default IWD should already be running, but
|
|
|
|
some tests do require starting IWD using this constructor (by passing
|
|
|
|
start_iwd_daemon=True)
|
|
|
|
'''
|
2016-10-01 00:03:10 +02:00
|
|
|
_object_manager_if = None
|
|
|
|
_agent_manager_if = None
|
2016-12-20 19:40:27 +01:00
|
|
|
_iwd_proc = None
|
2018-05-15 20:43:12 +02:00
|
|
|
_devices = None
|
2020-11-16 23:25:08 +01:00
|
|
|
_default_instance = None
|
2020-09-11 01:12:27 +02:00
|
|
|
psk_agent = None
|
2016-10-01 00:03:10 +02:00
|
|
|
|
2020-12-10 18:45:20 +01:00
|
|
|
def __init__(self, start_iwd_daemon = False, iwd_config_dir = '/tmp',
|
|
|
|
iwd_storage_dir = '/tmp/iwd', namespace=ctx):
|
2020-11-17 21:53:02 +01:00
|
|
|
self.namespace = namespace
|
|
|
|
self._bus = namespace.get_bus()
|
2016-10-01 00:03:10 +02:00
|
|
|
|
2017-04-26 02:31:39 +02:00
|
|
|
if start_iwd_daemon:
|
2020-11-17 21:53:02 +01:00
|
|
|
if self.namespace.is_process_running('iwd'):
|
|
|
|
raise Exception("IWD requested to start but is already running")
|
|
|
|
|
2020-12-10 18:45:20 +01:00
|
|
|
self._iwd_proc = self.namespace.start_iwd(iwd_config_dir,
|
|
|
|
iwd_storage_dir)
|
2016-10-05 01:26:37 +02:00
|
|
|
|
2021-08-13 18:24:38 +02:00
|
|
|
ctx.non_block_wait(self._bus.name_has_owner, 20, IWD_SERVICE,
|
|
|
|
exception=TimeoutError('IWD has failed to start'))
|
2016-10-05 01:26:37 +02:00
|
|
|
|
2018-06-28 01:33:14 +02:00
|
|
|
self._devices = DeviceList(self)
|
2018-05-15 20:43:12 +02:00
|
|
|
|
2019-04-20 22:29:03 +02:00
|
|
|
# Weak to make sure the test's reference to @self is the only counted
|
2020-11-16 23:25:08 +01:00
|
|
|
# reference so that __del__ gets called when it's released. This is only
|
|
|
|
# done for the root namespace in order to allow testutil to function
|
|
|
|
# correctly in non-namespace tests.
|
|
|
|
if self.namespace.name == "root":
|
|
|
|
IWD._default_instance = weakref.ref(self)
|
2019-04-20 22:29:03 +02:00
|
|
|
|
2016-12-20 19:40:27 +01:00
|
|
|
def __del__(self):
|
2020-09-11 01:12:27 +02:00
|
|
|
if self.psk_agent:
|
|
|
|
self.unregister_psk_agent(self.psk_agent)
|
2016-12-20 19:40:27 +01:00
|
|
|
|
2018-06-28 01:33:14 +02:00
|
|
|
self._object_manager_if = None
|
|
|
|
self._agent_manager_if = None
|
2018-07-31 16:37:08 +02:00
|
|
|
self._known_networks = None
|
2018-06-28 01:33:14 +02:00
|
|
|
self._devices = None
|
|
|
|
|
2020-09-11 01:12:27 +02:00
|
|
|
if self._iwd_proc is not None:
|
2020-11-17 21:53:02 +01:00
|
|
|
self.namespace.stop_process(self._iwd_proc)
|
2020-09-11 01:12:27 +02:00
|
|
|
self._iwd_proc = None
|
2019-09-09 19:21:52 +02:00
|
|
|
|
2021-02-25 23:00:55 +01:00
|
|
|
self.namespace = None
|
|
|
|
|
2016-10-01 00:03:10 +02:00
|
|
|
@property
|
|
|
|
def _object_manager(self):
|
|
|
|
if self._object_manager_if is None:
|
|
|
|
self._object_manager_if = \
|
|
|
|
dbus.Interface(self._bus.get_object(IWD_SERVICE,
|
|
|
|
IWD_TOP_LEVEL_PATH),
|
|
|
|
DBUS_OBJECT_MANAGER)
|
|
|
|
return self._object_manager_if
|
|
|
|
|
|
|
|
@property
|
|
|
|
def _agent_manager(self):
|
|
|
|
if self._agent_manager_if is None:
|
|
|
|
self._agent_manager_if =\
|
|
|
|
dbus.Interface(self._bus.get_object(IWD_SERVICE,
|
|
|
|
IWD_AGENT_MANAGER_PATH),
|
|
|
|
IWD_AGENT_MANAGER_INTERFACE)
|
|
|
|
return self._agent_manager_if
|
|
|
|
|
2020-11-16 23:25:08 +01:00
|
|
|
@staticmethod
|
|
|
|
def _wait_for_object_condition(obj, condition_str, max_wait = 50):
|
2021-08-13 18:24:38 +02:00
|
|
|
def _eval_wrap(obj, condition_str):
|
|
|
|
return eval(condition_str)
|
2021-01-25 19:05:28 +01:00
|
|
|
|
2021-08-13 18:24:38 +02:00
|
|
|
ctx.non_block_wait(_eval_wrap, max_wait, obj, condition_str,
|
|
|
|
exception=TimeoutError('[' + condition_str + ']'\
|
|
|
|
' condition was not met in '\
|
|
|
|
+ str(max_wait) + ' sec'))
|
2016-10-01 00:03:10 +02:00
|
|
|
|
2020-11-16 23:25:08 +01:00
|
|
|
def wait_for_object_condition(self, *args, **kwargs):
|
|
|
|
self._wait_for_object_condition(*args, **kwargs)
|
|
|
|
|
2021-02-05 20:06:36 +01:00
|
|
|
def wait_for_object_change(self, obj, from_str, to_str, max_wait = 50):
|
|
|
|
'''
|
|
|
|
Expects condition 'from_str' to evaluate true while waiting for 'to_str'. If
|
|
|
|
at any point during the wait 'from_str' evaluates false, an exception is
|
|
|
|
raised.
|
|
|
|
|
|
|
|
This allows an object to be checked for a state transition without any
|
|
|
|
intermediate state changes.
|
|
|
|
'''
|
2021-08-13 18:24:38 +02:00
|
|
|
def _eval_from_to(obj, from_str, to_str):
|
|
|
|
# If neither the initial or expected condition evaluate the
|
|
|
|
# object must be in another unexpected state.
|
|
|
|
if not eval(from_str) and not eval(to_str):
|
|
|
|
raise Exception('unexpected condition between [%s] and [%s]' %
|
|
|
|
(from_str, to_str))
|
|
|
|
|
|
|
|
# Initial condition does not evaluate but expected does, pass
|
|
|
|
if not eval(from_str) and eval(to_str):
|
|
|
|
return True
|
2021-02-05 20:06:36 +01:00
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Does initial condition pass?
|
|
|
|
if not eval(from_str):
|
|
|
|
raise Exception("initial condition [%s] not met" % from_str)
|
|
|
|
|
2021-08-13 18:24:38 +02:00
|
|
|
ctx.non_block_wait(_eval_from_to, max_wait, obj, from_str, to_str,
|
|
|
|
exception=TimeoutError('[' + to_str + ']'\
|
2021-02-05 20:06:36 +01:00
|
|
|
' condition was not met in '\
|
2021-08-13 18:24:38 +02:00
|
|
|
+ str(max_wait) + ' sec'))
|
2021-02-05 20:06:36 +01:00
|
|
|
|
2017-05-14 04:10:06 +02:00
|
|
|
def wait(self, time):
|
2021-08-13 18:24:38 +02:00
|
|
|
ctx.non_block_wait(lambda : False, time, exception=False)
|
2017-05-14 04:10:06 +02:00
|
|
|
|
2016-10-01 00:03:10 +02:00
|
|
|
@staticmethod
|
2020-10-14 18:55:57 +02:00
|
|
|
def clear_storage(storage_dir=IWD_STORAGE_DIR):
|
|
|
|
os.system('rm -rf ' + storage_dir + '/*')
|
|
|
|
os.system('rm -rf ' + storage_dir + '/hotspot/*')
|
2020-11-04 17:48:08 +01:00
|
|
|
os.system('rm -rf ' + storage_dir + '/ap/*')
|
2016-10-01 00:03:10 +02:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def create_in_storage(file_name, file_content):
|
|
|
|
fo = open(IWD_STORAGE_DIR + '/' + file_name, 'w')
|
|
|
|
|
2018-12-14 20:15:26 +01:00
|
|
|
fo.write(file_content)
|
2016-10-01 00:03:10 +02:00
|
|
|
fo.close()
|
|
|
|
|
|
|
|
@staticmethod
|
2021-08-13 21:55:06 +02:00
|
|
|
def copy_to_storage(source, storage_dir=IWD_STORAGE_DIR, name=None):
|
2016-10-01 00:03:10 +02:00
|
|
|
import shutil
|
|
|
|
|
|
|
|
assert not os.path.isabs(source)
|
2019-06-27 17:57:33 +02:00
|
|
|
|
2021-08-13 21:55:06 +02:00
|
|
|
if name:
|
|
|
|
storage_dir += '/%s' % name
|
|
|
|
|
2020-09-17 23:32:42 +02:00
|
|
|
shutil.copy(source, storage_dir)
|
2019-07-03 19:32:24 +02:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def copy_to_hotspot(source):
|
2019-07-15 18:19:44 +02:00
|
|
|
if not os.path.exists(IWD_STORAGE_DIR + "/hotspot"):
|
|
|
|
os.mkdir(IWD_STORAGE_DIR + "/hotspot")
|
2019-07-03 19:32:24 +02:00
|
|
|
|
2020-11-04 17:48:09 +01:00
|
|
|
IWD.copy_to_storage(source, IWD_STORAGE_DIR + "/hotspot")
|
2016-10-01 00:03:10 +02:00
|
|
|
|
2020-11-04 17:48:08 +01:00
|
|
|
@staticmethod
|
|
|
|
def copy_to_ap(source):
|
|
|
|
if not os.path.exists(IWD_STORAGE_DIR + "/ap"):
|
|
|
|
os.mkdir(IWD_STORAGE_DIR + "/ap")
|
|
|
|
|
|
|
|
IWD.copy_to_storage(source, IWD_STORAGE_DIR + '/ap/')
|
|
|
|
|
2018-08-09 16:33:46 +02:00
|
|
|
@staticmethod
|
|
|
|
def remove_from_storage(file_name):
|
|
|
|
os.system('rm -rf ' + IWD_STORAGE_DIR + '/\'' + file_name + '\'')
|
|
|
|
|
2020-10-09 20:08:52 +02:00
|
|
|
def list_devices(self, wait_to_appear = 0, max_wait = 50, p2p = False):
|
2018-05-15 20:43:12 +02:00
|
|
|
if not wait_to_appear:
|
2020-10-09 20:08:52 +02:00
|
|
|
return list(self._devices.values() if not p2p else self._devices.p2p_dict.values())
|
2018-05-15 20:43:12 +02:00
|
|
|
|
2021-08-13 18:24:38 +02:00
|
|
|
ctx.non_block_wait(lambda s, n: len(s._devices) >= n, max_wait, self, wait_to_appear,
|
|
|
|
exception=TimeoutError('IWD has no associated devices'))
|
2018-05-15 20:43:12 +02:00
|
|
|
|
2020-10-24 00:19:00 +02:00
|
|
|
return list(self._devices.values() if not p2p else self._devices.p2p_dict.values())[:wait_to_appear]
|
2020-10-09 20:08:52 +02:00
|
|
|
|
|
|
|
def list_p2p_devices(self, *args, **kwargs):
|
|
|
|
return self.list_devices(*args, **kwargs, p2p=True)
|
2016-10-01 00:03:10 +02:00
|
|
|
|
|
|
|
def list_known_networks(self):
|
2018-07-31 16:37:08 +02:00
|
|
|
'''Returns the list of KnownNetwork objects.'''
|
|
|
|
objects = self._object_manager.GetManagedObjects()
|
2016-10-01 00:03:10 +02:00
|
|
|
known_network_list = []
|
|
|
|
|
2018-07-31 16:37:08 +02:00
|
|
|
for path in objects:
|
|
|
|
for interface in objects[path]:
|
|
|
|
if interface == IWD_KNOWN_NETWORK_INTERFACE:
|
|
|
|
known_network_list.append(
|
|
|
|
KnownNetwork(path, objects[path][interface]))
|
2016-10-01 00:03:10 +02:00
|
|
|
|
|
|
|
return known_network_list
|
|
|
|
|
|
|
|
def register_psk_agent(self, psk_agent):
|
|
|
|
self._agent_manager.RegisterAgent(
|
|
|
|
psk_agent.path,
|
|
|
|
dbus_interface=IWD_AGENT_MANAGER_INTERFACE,
|
|
|
|
reply_handler=self._success,
|
|
|
|
error_handler=self._failure)
|
|
|
|
self._wait_for_async_op()
|
2020-09-11 01:12:27 +02:00
|
|
|
self.psk_agent = psk_agent
|
2016-10-01 00:03:10 +02:00
|
|
|
|
|
|
|
def unregister_psk_agent(self, psk_agent):
|
|
|
|
self._agent_manager.UnregisterAgent(
|
|
|
|
psk_agent.path,
|
|
|
|
dbus_interface=IWD_AGENT_MANAGER_INTERFACE,
|
|
|
|
reply_handler=self._success,
|
|
|
|
error_handler=self._failure)
|
|
|
|
self._wait_for_async_op()
|
2020-09-11 01:12:27 +02:00
|
|
|
self.psk_agent = None
|
2019-04-20 22:29:03 +02:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_instance():
|
2020-11-16 23:25:08 +01:00
|
|
|
return IWD._default_instance()
|