1# Copyright (c) 2014 The Chromium OS Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5""" 6This module provides bindings for PseudoModem Manager. 7 8""" 9 10import dbus 11import logging 12 13import common 14 15from autotest_lib.client.cros.networking import mm1_proxy 16 17from autotest_lib.client.bin import utils 18from autotest_lib.client.cros.cellular import mm1_constants 19from autotest_lib.client.cros.cellular.pseudomodem import pm_constants 20 21 22class PseudoMMProxy(mm1_proxy.ModemManager1Proxy): 23 """A wrapper around a DBus proxy for PseudoModem Manager.""" 24 25 # Used for software message propagation latencies. 26 SHORT_TIMEOUT_SECONDS = 2 27 28 @property 29 def iface_testing(self): 30 """@return org.chromium.Pseudomodem.Testing DBus interface.""" 31 return dbus.Interface( 32 self._bus.get_object(mm1_constants.I_MODEM_MANAGER, 33 pm_constants.TESTING_PATH), 34 pm_constants.I_TESTING) 35 36 37 def iface_ism(self, machine_name, timeout_seconds=SHORT_TIMEOUT_SECONDS): 38 """ 39 Get the testing interface of the given interactive state machine. 40 41 @param machine_name: The name of the interactive state machine. 42 @param timeout_seconds: Max number of seconds to wait until interactive 43 state machine becomes available. 44 @return dbus.Interface for the testing interface of 45 InteractiveScanningMachine. 46 @raise mm1_proxy.ModemManager1ProxyError if a valid DBus object can't 47 be found. 48 49 """ 50 def _get_machine(ignore_error): 51 machine = self._bus.get_object( 52 mm1_constants.I_MODEM_MANAGER, 53 '/'.join([pm_constants.TESTING_PATH, machine_name])) 54 if machine is None: 55 return None 56 57 i_machine = dbus.Interface(machine, pm_constants.I_TESTING_ISM) 58 # Only way to know if this DBus object is valid is to call a 59 # method on it. 60 try: 61 i_machine.IsWaiting() # Ignore result. 62 return i_machine 63 except dbus.exceptions.DBusException as e: 64 if ignore_error: 65 return None 66 logging.debug(e) 67 raise mm1_proxy.ModemManager1ProxyError( 68 'Failed to obtain a valid object for interactive ' 69 'state machine %s. DBus error: %s', 70 machine_name, 71 repr(e)) 72 73 try: 74 utils.poll_for_condition( 75 lambda: _get_machine(True), timeout=timeout_seconds) 76 except utils.TimeoutError as e: 77 pass 78 79 return _get_machine(False) 80