2016-05-13 23:18:23 +02:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import dbus
|
|
|
|
import logging
|
|
|
|
import traceback
|
|
|
|
|
2016-06-04 01:13:43 +02:00
|
|
|
IWD_SERVICE = 'net.connman.iwd'
|
|
|
|
|
|
|
|
IWD_AGENT_MANAGER_INTERFACE = 'net.connman.iwd.AgentManager'
|
|
|
|
IWD_DEVICE_INTERFACE = 'net.connman.iwd.Device'
|
|
|
|
IWD_NETWORK_INTERFACE = 'net.connman.iwd.Network'
|
|
|
|
IWD_AGENT_INTERFACE = 'net.connman.iwd.Agent'
|
|
|
|
IWD_WSC_INTERFACE = 'net.connman.iwd.WiFiSimpleConfiguration'
|
|
|
|
|
|
|
|
IWD_TOP_LEVEL_PATH = "/"
|
|
|
|
|
2016-05-13 23:18:23 +02:00
|
|
|
# get all the available networks
|
2016-06-01 20:23:48 +02:00
|
|
|
def getObjectList(bus):
|
2016-05-13 23:18:23 +02:00
|
|
|
logger.debug(sys._getframe().f_code.co_name)
|
2016-06-04 01:13:43 +02:00
|
|
|
manager = dbus.Interface(bus.get_object(IWD_SERVICE, "/"),
|
2016-06-01 20:23:48 +02:00
|
|
|
"org.freedesktop.DBus.ObjectManager")
|
|
|
|
return manager.GetManagedObjects()
|
2016-05-13 23:18:23 +02:00
|
|
|
|
|
|
|
# get all the available networks
|
2016-06-01 20:23:48 +02:00
|
|
|
def getNetworkList(objects, bus):
|
2016-05-13 23:18:23 +02:00
|
|
|
logger.debug(sys._getframe().f_code.co_name)
|
2016-06-01 20:23:48 +02:00
|
|
|
networkList = []
|
|
|
|
for path in objects:
|
2016-06-04 01:13:43 +02:00
|
|
|
if IWD_DEVICE_INTERFACE not in objects[path]:
|
2016-06-01 20:23:48 +02:00
|
|
|
continue
|
|
|
|
for path2 in objects:
|
|
|
|
if not path2.startswith(path) or \
|
2016-06-04 01:13:43 +02:00
|
|
|
IWD_NETWORK_INTERFACE not in objects[path2]:
|
2016-06-01 20:23:48 +02:00
|
|
|
continue
|
|
|
|
networkList.append(path2)
|
|
|
|
return networkList
|
2016-05-13 23:18:23 +02:00
|
|
|
|
|
|
|
def connect(networkToConnect, self, mainloop, bus):
|
|
|
|
logger.debug(sys._getframe().f_code.co_name)
|
|
|
|
logger.debug(" %s", networkToConnect)
|
2016-06-04 01:13:43 +02:00
|
|
|
network = dbus.Interface(bus.get_object(IWD_SERVICE, networkToConnect),
|
|
|
|
IWD_NETWORK_INTERFACE)
|
2016-05-13 23:18:23 +02:00
|
|
|
try:
|
|
|
|
network.Connect()
|
|
|
|
except:
|
|
|
|
errorMsg = "Could not connect to network %s", networkToConnect
|
|
|
|
logger.error(traceback.print_exc(file=sys.stdout))
|
|
|
|
self.assertTrue(False, errorMsg)
|
2016-05-17 02:02:30 +02:00
|
|
|
return False
|
2016-05-13 23:18:23 +02:00
|
|
|
|
|
|
|
logger.info("Successfully connected to: %s", networkToConnect)
|
2016-05-17 02:02:30 +02:00
|
|
|
return True
|
2016-05-13 23:18:23 +02:00
|
|
|
|
|
|
|
# try to disconnect from the device.
|
|
|
|
def disconnect(deviceToDisconnect, mainloop, bus):
|
|
|
|
logger.debug(sys._getframe().f_code.co_name)
|
2016-06-04 01:13:43 +02:00
|
|
|
device = dbus.Interface(bus.get_object(IWD_SERVICE, deviceToDisconnect),
|
|
|
|
IWD_DEVICE_INTERFACE)
|
2016-05-13 23:18:23 +02:00
|
|
|
try:
|
|
|
|
device.Disconnect()
|
|
|
|
except:
|
|
|
|
errorMsg = "Failed to disconnect from device %s", deviceToDisconnect
|
|
|
|
logger.error(traceback.print_exc(file=sys.stdout))
|
|
|
|
assertTrue(False, errorMsg)
|
2016-05-17 02:02:30 +02:00
|
|
|
return False
|
2016-05-13 23:18:23 +02:00
|
|
|
|
|
|
|
logger.info("Successfully disconnected from: %s", deviceToDisconnect)
|
2016-05-17 02:02:30 +02:00
|
|
|
return True
|
2016-05-13 23:18:23 +02:00
|
|
|
|
|
|
|
# get the 1st network found to connect to
|
2016-06-01 20:23:48 +02:00
|
|
|
def getNetworkToConnectTo(objects):
|
2016-05-13 23:18:23 +02:00
|
|
|
logger.debug(sys._getframe().f_code.co_name)
|
2016-06-01 20:23:48 +02:00
|
|
|
networkList = []
|
|
|
|
for path in objects:
|
|
|
|
for path2 in objects:
|
|
|
|
if not path2.startswith(path) or \
|
2016-06-04 01:13:43 +02:00
|
|
|
IWD_NETWORK_INTERFACE not in objects[path2]:
|
2016-06-01 20:23:48 +02:00
|
|
|
continue
|
|
|
|
return path2
|
2016-05-13 23:18:23 +02:00
|
|
|
return ""
|
|
|
|
|
|
|
|
# return the currently connected device by
|
|
|
|
# checking the 'ConnectedNetwork' property
|
|
|
|
def getCurrentlyConnectedDevice():
|
|
|
|
logger.debug(sys._getframe().f_code.co_name)
|
|
|
|
bus = dbus.SystemBus()
|
2016-06-04 01:13:43 +02:00
|
|
|
manager = dbus.Interface(bus.get_object(IWD_SERVICE, "/"),
|
2016-05-13 23:18:23 +02:00
|
|
|
"net.connman.iwd.Manager")
|
2016-06-01 20:23:48 +02:00
|
|
|
objects = getObjectList(bus)
|
|
|
|
for path in objects:
|
2016-06-04 01:13:43 +02:00
|
|
|
if IWD_DEVICE_INTERFACE not in objects[path]:
|
2016-06-01 20:23:48 +02:00
|
|
|
continue
|
2016-06-04 01:13:43 +02:00
|
|
|
device = objects[path][IWD_DEVICE_INTERFACE]
|
2016-06-01 20:23:48 +02:00
|
|
|
for key in device.keys():
|
2016-05-13 23:18:23 +02:00
|
|
|
if key in ["ConnectedNetwork"]:
|
2016-06-01 20:23:48 +02:00
|
|
|
return path
|
2016-05-13 23:18:23 +02:00
|
|
|
return ""
|
|
|
|
|
|
|
|
# get name of the network currently connected to
|
|
|
|
def getCurrentlyConnectedNetworkName():
|
|
|
|
logger.debug(sys._getframe().f_code.co_name)
|
|
|
|
bus = dbus.SystemBus()
|
2016-06-01 20:23:48 +02:00
|
|
|
deviceList = getObjectList(bus)
|
2016-05-13 23:18:23 +02:00
|
|
|
networkList = getNetworkList(deviceList, bus)
|
2016-06-01 20:23:48 +02:00
|
|
|
for path in deviceList:
|
|
|
|
for path2 in deviceList:
|
|
|
|
if not path2.startswith(path) or \
|
2016-06-04 01:13:43 +02:00
|
|
|
IWD_NETWORK_INTERFACE not in deviceList[path2]:
|
2016-06-01 20:23:48 +02:00
|
|
|
continue
|
2016-06-04 01:13:43 +02:00
|
|
|
network = deviceList[path2][IWD_NETWORK_INTERFACE]
|
2016-06-01 20:23:48 +02:00
|
|
|
for key in network.keys():
|
|
|
|
name = ""
|
2016-05-13 23:18:23 +02:00
|
|
|
if key in ["Connected"]:
|
|
|
|
# this check is needed in case when we are testing
|
|
|
|
# connectivity with multiple networks. The previously
|
|
|
|
# connected network will still have the 'Connected' property
|
|
|
|
# even though it will be set to 0.
|
2016-06-01 20:23:48 +02:00
|
|
|
val = network[key]
|
|
|
|
if network[key] == 0: # if "Connected is 0"
|
2016-05-13 23:18:23 +02:00
|
|
|
continue
|
2016-06-01 20:23:48 +02:00
|
|
|
for key2 in network.keys():
|
|
|
|
if key2 in ["Name"]:
|
|
|
|
return network[key2]
|
2016-05-13 23:18:23 +02:00
|
|
|
return ""
|
|
|
|
|
|
|
|
# get name of the network
|
2016-06-01 20:23:48 +02:00
|
|
|
def getNetworkName(deviceList):
|
2016-05-13 23:18:23 +02:00
|
|
|
logger.debug(sys._getframe().f_code.co_name)
|
2016-06-01 20:23:48 +02:00
|
|
|
for path in deviceList:
|
2016-06-04 01:13:43 +02:00
|
|
|
if IWD_DEVICE_INTERFACE not in deviceList[path]:
|
2016-06-01 20:23:48 +02:00
|
|
|
continue
|
|
|
|
for path2 in deviceList:
|
|
|
|
if not path2.startswith(path) or \
|
2016-06-04 01:13:43 +02:00
|
|
|
IWD_NETWORK_INTERFACE not in deviceList[path2]:
|
2016-06-01 20:23:48 +02:00
|
|
|
continue
|
2016-06-04 01:13:43 +02:00
|
|
|
network = deviceList[path2][IWD_NETWORK_INTERFACE]
|
2016-06-01 20:23:48 +02:00
|
|
|
for key in network.keys():
|
|
|
|
if key in ["Name"]:
|
|
|
|
return network[key]
|
2016-05-13 23:18:23 +02:00
|
|
|
return ""
|
|
|
|
|
|
|
|
def initLogger():
|
|
|
|
global logger
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# logging levels include DEBUG, INFO, WARNING, ERROR, CRITICAL
|
|
|
|
logging.basicConfig(level=logging.DEBUG)
|