blob: 93293a28b2827b86d2d16b7cfe41abf8e74d39e7 [file] [log] [blame]
Thieu Le98327a42014-08-21 18:11:41 -07001# 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
5import contextlib
6import dbus
7import logging
8import sys
9import traceback
10
11import common
Thieu Lee3b3fcf2014-09-08 13:56:15 -070012from autotest_lib.client.bin import utils
Thieu Le98327a42014-08-21 18:11:41 -070013from autotest_lib.client.common_lib import error
14from autotest_lib.client.cros import backchannel
15from autotest_lib.client.cros.cellular import cell_tools
16from autotest_lib.client.cros.cellular import mm
17from autotest_lib.client.cros.cellular.pseudomodem import pseudomodem_context
18from autotest_lib.client.cros.cellular.wardmodem import wardmodem
19from autotest_lib.client.cros.networking import cellular_proxy
Thieu Le5fe5f512014-09-03 12:52:10 -070020from autotest_lib.client.cros.networking import shill_proxy
Thieu Le98327a42014-08-21 18:11:41 -070021
22# Import 'flimflam_test_path' first in order to import flimflam.
23# pylint: disable=W0611
24from autotest_lib.client.cros import flimflam_test_path
25import flimflam
26
27class CellularTestEnvironment(object):
28 """Setup and verify cellular test environment.
29
30 This context manager configures the following:
31 - Sets up backchannel.
32 - Shuts down other devices except cellular.
33 - Shill and MM logging is enabled appropriately for cellular.
34 - Initializes members that tests should use to access test environment
35 (eg. |shill|, |flimflam|, |modem_manager|, |modem|).
36
37 Then it verifies the following is valid:
38 - The backchannel is using an Ethernet device.
39 - The SIM is inserted and valid.
40 - There is one and only one modem in the device.
41 - The modem is registered to the network.
42 - There is a cellular service in shill and it's not connected.
43
44 Don't use this base class directly, use the appropriate subclass.
45
46 Setup for over-the-air tests:
47 with CellularOTATestEnvironment() as test_env:
48 # Test body
49
50 Setup for pseudomodem tests:
51 with CellularPseudoMMTestEnvironment(
52 pseudomm_args=({'family': '3GPP'})) as test_env:
53 # Test body
54
55 Setup for wardmodem tests:
56 with CellularWardModemTestEnvironment(
57 wardmodem_modem='e362') as test_env:
58 # Test body
59
60 """
61
62 def __init__(self, use_backchannel=True, shutdown_other_devices=True):
63 """
64 @param use_backchannel: Set up the backchannel that can be used to
65 communicate with the DUT.
66 @param shutdown_other_devices: If True, shutdown all devices except
67 cellular.
68
69 """
70 # Tests should use this main loop instead of creating their own.
71 self.mainloop = dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
72 self.bus = dbus.SystemBus(mainloop=self.mainloop)
73
74 self.shill = None
75 self.flim = None # Only use this for legacy tests.
76 self.modem_manager = None
77 self.modem = None
78
Thieu Le996e0a02014-09-15 12:36:08 -070079 self._nested = None
Thieu Le98327a42014-08-21 18:11:41 -070080 self._context_managers = []
81 if use_backchannel:
82 self._context_managers.append(backchannel.Backchannel())
83 if shutdown_other_devices:
84 self._context_managers.append(
85 cell_tools.OtherDeviceShutdownContext('cellular'))
86
87
Thieu Le996e0a02014-09-15 12:36:08 -070088 @contextlib.contextmanager
89 def _disable_shill_autoconnect(self):
90 self._enable_shill_cellular_autoconnect(False)
91 yield
92 self._enable_shill_cellular_autoconnect(True)
93
94
Thieu Le98327a42014-08-21 18:11:41 -070095 def __enter__(self):
96 try:
Thieu Le996e0a02014-09-15 12:36:08 -070097 # Temporarily disable shill autoconnect to cellular service while
98 # the test environment is setup to prevent a race condition
99 # between disconnecting the modem in _verify_cellular_service()
100 # and shill autoconnect.
101 with self._disable_shill_autoconnect():
102 self._nested = contextlib.nested(*self._context_managers)
103 self._nested.__enter__()
Thieu Le98327a42014-08-21 18:11:41 -0700104
Thieu Le996e0a02014-09-15 12:36:08 -0700105 self._initialize_shill()
Thieu Le99a39082014-09-09 16:00:13 -0700106
Thieu Le996e0a02014-09-15 12:36:08 -0700107 # Perform SIM verification now to ensure that we can enable the
108 # modem in _initialize_modem_components(). ModemManager does not
109 # allow enabling a modem without a SIM.
110 self._verify_sim()
111 self._initialize_modem_components()
Thieu Le99a39082014-09-09 16:00:13 -0700112
Thieu Le996e0a02014-09-15 12:36:08 -0700113 self._setup_logging()
Thieu Le98327a42014-08-21 18:11:41 -0700114
Thieu Le996e0a02014-09-15 12:36:08 -0700115 self._verify_backchannel()
116 self._wait_for_modem_registration()
117 self._verify_cellular_service()
Thieu Le98327a42014-08-21 18:11:41 -0700118
Thieu Le996e0a02014-09-15 12:36:08 -0700119 return self
Thieu Le5fe5f512014-09-03 12:52:10 -0700120 except (error.TestError, dbus.DBusException,
121 shill_proxy.ShillProxyError) as e:
Thieu Le98327a42014-08-21 18:11:41 -0700122 except_type, except_value, except_traceback = sys.exc_info()
123 lines = traceback.format_exception(except_type, except_value,
124 except_traceback)
125 logging.error('Error during test initialization:\n' +
126 ''.join(lines))
127 self.__exit__(*sys.exc_info())
128 raise error.TestError('INIT_ERROR: %s' % str(e))
Thieu Le5fe5f512014-09-03 12:52:10 -0700129 except:
130 self.__exit__(*sys.exc_info())
131 raise
Thieu Le98327a42014-08-21 18:11:41 -0700132
133
134 def __exit__(self, exception, value, traceback):
Thieu Le996e0a02014-09-15 12:36:08 -0700135 if self._nested:
136 return self._nested.__exit__(exception, value, traceback)
Thieu Le98327a42014-08-21 18:11:41 -0700137
138
Thieu Le99a39082014-09-09 16:00:13 -0700139 def _get_shill_cellular_device_object(self):
Thieu Lee3b3fcf2014-09-08 13:56:15 -0700140 modem_device = self.shill.find_cellular_device_object()
141 if not modem_device:
142 raise error.TestError('Cannot find cellular device in shill. '
143 'Is the modem plugged in?')
Thieu Le99a39082014-09-09 16:00:13 -0700144 return modem_device
145
146
147 def _enable_modem(self):
148 modem_device = self._get_shill_cellular_device_object()
Thieu Lee3b3fcf2014-09-08 13:56:15 -0700149 try:
150 modem_device.Enable()
151 except dbus.DBusException as e:
152 if (e.get_dbus_name() !=
153 shill_proxy.ShillProxy.ERROR_IN_PROGRESS):
154 raise
155
156 utils.poll_for_condition(
157 lambda: modem_device.GetProperties()['Powered'],
158 exception=error.TestError(
159 'Failed to enable modem.'),
160 timeout=shill_proxy.ShillProxy.DEVICE_ENABLE_DISABLE_TIMEOUT)
161
162
Thieu Le996e0a02014-09-15 12:36:08 -0700163 def _enable_shill_cellular_autoconnect(self, enable):
164 shill = cellular_proxy.CellularProxy.get_proxy(self.bus)
165 shill.manager.SetProperty(
166 shill_proxy.ShillProxy.
167 MANAGER_PROPERTY_NO_AUTOCONNECT_TECHNOLOGIES,
168 '' if enable else 'cellular')
169
170
Thieu Lea2aeab32014-09-15 14:29:43 -0700171 def _is_unsupported_error(self, e):
172 return (e.get_dbus_name() ==
173 shill_proxy.ShillProxy.ERROR_NOT_SUPPORTED or
174 (e.get_dbus_name() ==
175 shill_proxy.ShillProxy.ERROR_FAILURE and
176 'operation not supported' in e.get_dbus_message()))
177
Thieu Le996e0a02014-09-15 12:36:08 -0700178
Thieu Le98327a42014-08-21 18:11:41 -0700179 def _reset_modem(self):
Thieu Le99a39082014-09-09 16:00:13 -0700180 modem_device = self._get_shill_cellular_device_object()
Thieu Le98327a42014-08-21 18:11:41 -0700181 try:
Thieu Lea2aeab32014-09-15 14:29:43 -0700182 # Cromo/MBIM modems do not support being reset.
Thieu Le98327a42014-08-21 18:11:41 -0700183 self.shill.reset_modem(modem_device, expect_service=False)
184 except dbus.DBusException as e:
Thieu Lea2aeab32014-09-15 14:29:43 -0700185 if not self._is_unsupported_error(e):
Thieu Le98327a42014-08-21 18:11:41 -0700186 raise
187
188
Thieu Le99a39082014-09-09 16:00:13 -0700189 def _initialize_shill(self):
190 """Get access to shill."""
Thieu Le98327a42014-08-21 18:11:41 -0700191 # CellularProxy.get_proxy() checks to see if shill is running and
192 # responding to DBus requests. It returns None if that's not the case.
193 self.shill = cellular_proxy.CellularProxy.get_proxy(self.bus)
194 if self.shill is None:
195 raise error.TestError('Cannot connect to shill, is shill running?')
196
197 # Keep this around to support older tests that haven't migrated to
198 # cellular_proxy.
199 self.flim = flimflam.FlimFlam()
200
Thieu Le99a39082014-09-09 16:00:13 -0700201
202 def _initialize_modem_components(self):
203 """Reset the modem and get access to modem components."""
Thieu Lee3b3fcf2014-09-08 13:56:15 -0700204 # Enable modem first so shill initializes the modemmanager proxies so
205 # we can call reset on it.
206 self._enable_modem()
207 self._reset_modem()
208
Thieu Le98327a42014-08-21 18:11:41 -0700209 # PickOneModem() makes sure there's a modem manager and that there is
210 # one and only one modem.
Thieu Le98327a42014-08-21 18:11:41 -0700211 self.modem_manager, modem_path = mm.PickOneModem('')
212 self.modem = self.modem_manager.GetModem(modem_path)
213 if self.modem is None:
214 raise error.TestError('Cannot get modem object at %s.' % modem_path)
215
216
217 def _setup_logging(self):
218 self.shill.set_logging_for_cellular_test()
219 self.modem_manager.SetDebugLogging()
220
221
Thieu Le99a39082014-09-09 16:00:13 -0700222 def _verify_sim(self):
223 """Verify SIM is valid.
224
225 Make sure a SIM in inserted and that it is not locked.
226
227 @raise error.TestError if SIM does not exist or is locked.
228
229 """
230 modem_device = self._get_shill_cellular_device_object()
231 props = modem_device.GetProperties()
232
233 # No SIM in CDMA modems.
234 family = props[
235 cellular_proxy.CellularProxy.DEVICE_PROPERTY_TECHNOLOGY_FAMILY]
236 if (family ==
237 cellular_proxy.CellularProxy.
238 DEVICE_PROPERTY_TECHNOLOGY_FAMILY_CDMA):
239 return
240
241 # Make sure there is a SIM.
242 if not props[cellular_proxy.CellularProxy.DEVICE_PROPERTY_SIM_PRESENT]:
243 raise error.TestError('There is no SIM in the modem.')
244
245 # Make sure SIM is not locked.
246 lock_status = props.get(
247 cellular_proxy.CellularProxy.DEVICE_PROPERTY_SIM_LOCK_STATUS,
248 None)
249 if lock_status is None:
250 raise error.TestError('Failed to read SIM lock status.')
251 locked = lock_status.get(
252 cellular_proxy.CellularProxy.PROPERTY_KEY_SIM_LOCK_ENABLED,
253 None)
254 if locked is None:
255 raise error.TestError('Failed to read SIM LockEnabled status.')
256 elif locked:
257 raise error.TestError(
258 'SIM is locked, test requires an unlocked SIM.')
259
260
Thieu Le98327a42014-08-21 18:11:41 -0700261 def _verify_backchannel(self):
262 """Verify backchannel is on an ethernet device.
263
264 @raise error.TestError if backchannel is not on an ethernet device.
265
266 """
Thieu Le8bded2b2014-09-03 15:38:46 -0700267 if not backchannel.is_backchannel_using_ethernet():
268 raise error.TestError('An ethernet connection is required between '
269 'the test server and the device under test.')
Thieu Le98327a42014-08-21 18:11:41 -0700270
271
Thieu Le98327a42014-08-21 18:11:41 -0700272 def _wait_for_modem_registration(self):
273 """Wait for the modem to register with the network.
274
Thieu Le98327a42014-08-21 18:11:41 -0700275 @raise error.TestError if modem is not registered.
276
277 """
Thieu Lee3b3fcf2014-09-08 13:56:15 -0700278 utils.poll_for_condition(
279 self.modem.ModemIsRegistered,
280 exception=error.TestError(
281 'Modem failed to register with the network.'),
282 timeout=cellular_proxy.CellularProxy.SERVICE_REGISTRATION_TIMEOUT)
Thieu Le98327a42014-08-21 18:11:41 -0700283
284
285 def _verify_cellular_service(self):
286 """Make sure a cellular service exists.
287
288 The cellular service should not be connected to the network.
289
290 @raise error.TestError if cellular service does not exist or if
291 there are multiple cellular services.
292
293 """
294 service = self.shill.wait_for_cellular_service_object()
295
296 try:
297 service.Disconnect()
298 except dbus.DBusException as e:
299 if (e.get_dbus_name() !=
300 cellular_proxy.CellularProxy.ERROR_NOT_CONNECTED):
301 raise
Thieu Lee3b3fcf2014-09-08 13:56:15 -0700302 success, state, _ = self.shill.wait_for_property_in(
Thieu Le98327a42014-08-21 18:11:41 -0700303 service,
304 cellular_proxy.CellularProxy.SERVICE_PROPERTY_STATE,
305 ('idle',),
306 cellular_proxy.CellularProxy.SERVICE_DISCONNECT_TIMEOUT)
307 if not success:
308 raise error.TestError(
Thieu Lee3b3fcf2014-09-08 13:56:15 -0700309 'Cellular service needs to start in the "idle" state. '
310 'Current state is "%s". '
311 'Modem disconnect may have failed.' %
312 state)
Thieu Le98327a42014-08-21 18:11:41 -0700313
314
315class CellularOTATestEnvironment(CellularTestEnvironment):
316 """Setup and verify cellular over-the-air (OTA) test environment. """
317 def __init__(self, **kwargs):
318 super(CellularOTATestEnvironment, self).__init__(**kwargs)
319
320
321class CellularPseudoMMTestEnvironment(CellularTestEnvironment):
322 """Setup and verify cellular pseudomodem test environment. """
323 def __init__(self, pseudomm_args=None, **kwargs):
324 """
325 @param pseudomm_args: Tuple of arguments passed to the pseudomodem, see
326 pseudomodem_context.py for description of each argument in the
327 tuple: (flags_map, block_output, bus)
328
329 """
330 super(CellularPseudoMMTestEnvironment, self).__init__(**kwargs)
331 self._context_managers.append(
Thieu Le6724f282014-09-04 15:36:41 -0700332 pseudomodem_context.PseudoModemManagerContext(
333 True, bus=self.bus, *pseudomm_args))
Thieu Le98327a42014-08-21 18:11:41 -0700334
335
336class CellularWardModemTestEnvironment(CellularTestEnvironment):
337 """Setup and verify cellular ward modem test environment. """
338 def __init__(self, wardmodem_modem=None, **kwargs):
339 """
340 @param wardmodem_modem: Customized ward modem to use instead of the
341 default implementation, see wardmodem.py.
342
343 """
344 super(CellularWardModemTestEnvironment, self).__init__(**kwargs)
345 self._context_managers.append(
346 wardmodem.WardModemContext(args=['--modem', wardmodem_modem]))