blob: 11c4a45ff17b16e938b1c66421bd4cf82534fdcf [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
79 self._context_managers = []
80 if use_backchannel:
81 self._context_managers.append(backchannel.Backchannel())
82 if shutdown_other_devices:
83 self._context_managers.append(
84 cell_tools.OtherDeviceShutdownContext('cellular'))
85
86
87 def __enter__(self):
88 try:
89 self._nested = contextlib.nested(*self._context_managers)
90 self._nested.__enter__()
91
Thieu Le99a39082014-09-09 16:00:13 -070092 self._initialize_shill()
93
94 # Perform SIM verification now to ensure that we can enable the
95 # modem in _initialize_modem_components(). ModemManager does not
96 # allow enabling a modem without a SIM.
97 self._verify_sim()
98 self._initialize_modem_components()
99
Thieu Le98327a42014-08-21 18:11:41 -0700100 self._setup_logging()
101
102 self._verify_backchannel()
Thieu Le98327a42014-08-21 18:11:41 -0700103 self._wait_for_modem_registration()
104 self._verify_cellular_service()
105
106 return self
Thieu Le5fe5f512014-09-03 12:52:10 -0700107 except (error.TestError, dbus.DBusException,
108 shill_proxy.ShillProxyError) as e:
Thieu Le98327a42014-08-21 18:11:41 -0700109 except_type, except_value, except_traceback = sys.exc_info()
110 lines = traceback.format_exception(except_type, except_value,
111 except_traceback)
112 logging.error('Error during test initialization:\n' +
113 ''.join(lines))
114 self.__exit__(*sys.exc_info())
115 raise error.TestError('INIT_ERROR: %s' % str(e))
Thieu Le5fe5f512014-09-03 12:52:10 -0700116 except:
117 self.__exit__(*sys.exc_info())
118 raise
Thieu Le98327a42014-08-21 18:11:41 -0700119
120
121 def __exit__(self, exception, value, traceback):
122 return self._nested.__exit__(exception, value, traceback)
123
124
Thieu Le99a39082014-09-09 16:00:13 -0700125 def _get_shill_cellular_device_object(self):
Thieu Lee3b3fcf2014-09-08 13:56:15 -0700126 modem_device = self.shill.find_cellular_device_object()
127 if not modem_device:
128 raise error.TestError('Cannot find cellular device in shill. '
129 'Is the modem plugged in?')
Thieu Le99a39082014-09-09 16:00:13 -0700130 return modem_device
131
132
133 def _enable_modem(self):
134 modem_device = self._get_shill_cellular_device_object()
Thieu Lee3b3fcf2014-09-08 13:56:15 -0700135 try:
136 modem_device.Enable()
137 except dbus.DBusException as e:
138 if (e.get_dbus_name() !=
139 shill_proxy.ShillProxy.ERROR_IN_PROGRESS):
140 raise
141
142 utils.poll_for_condition(
143 lambda: modem_device.GetProperties()['Powered'],
144 exception=error.TestError(
145 'Failed to enable modem.'),
146 timeout=shill_proxy.ShillProxy.DEVICE_ENABLE_DISABLE_TIMEOUT)
147
148
Thieu Lea2aeab32014-09-15 14:29:43 -0700149 def _is_unsupported_error(self, e):
150 return (e.get_dbus_name() ==
151 shill_proxy.ShillProxy.ERROR_NOT_SUPPORTED or
152 (e.get_dbus_name() ==
153 shill_proxy.ShillProxy.ERROR_FAILURE and
154 'operation not supported' in e.get_dbus_message()))
155
Thieu Le98327a42014-08-21 18:11:41 -0700156 def _reset_modem(self):
Thieu Le99a39082014-09-09 16:00:13 -0700157 modem_device = self._get_shill_cellular_device_object()
Thieu Le98327a42014-08-21 18:11:41 -0700158 try:
Thieu Lea2aeab32014-09-15 14:29:43 -0700159 # Cromo/MBIM modems do not support being reset.
Thieu Le98327a42014-08-21 18:11:41 -0700160 self.shill.reset_modem(modem_device, expect_service=False)
161 except dbus.DBusException as e:
Thieu Lea2aeab32014-09-15 14:29:43 -0700162 if not self._is_unsupported_error(e):
Thieu Le98327a42014-08-21 18:11:41 -0700163 raise
164
165
Thieu Le99a39082014-09-09 16:00:13 -0700166 def _initialize_shill(self):
167 """Get access to shill."""
Thieu Le98327a42014-08-21 18:11:41 -0700168 # CellularProxy.get_proxy() checks to see if shill is running and
169 # responding to DBus requests. It returns None if that's not the case.
170 self.shill = cellular_proxy.CellularProxy.get_proxy(self.bus)
171 if self.shill is None:
172 raise error.TestError('Cannot connect to shill, is shill running?')
173
174 # Keep this around to support older tests that haven't migrated to
175 # cellular_proxy.
176 self.flim = flimflam.FlimFlam()
177
Thieu Le99a39082014-09-09 16:00:13 -0700178
179 def _initialize_modem_components(self):
180 """Reset the modem and get access to modem components."""
Thieu Lee3b3fcf2014-09-08 13:56:15 -0700181 # Enable modem first so shill initializes the modemmanager proxies so
182 # we can call reset on it.
183 self._enable_modem()
184 self._reset_modem()
185
Thieu Le98327a42014-08-21 18:11:41 -0700186 # PickOneModem() makes sure there's a modem manager and that there is
187 # one and only one modem.
Thieu Le98327a42014-08-21 18:11:41 -0700188 self.modem_manager, modem_path = mm.PickOneModem('')
189 self.modem = self.modem_manager.GetModem(modem_path)
190 if self.modem is None:
191 raise error.TestError('Cannot get modem object at %s.' % modem_path)
192
193
194 def _setup_logging(self):
195 self.shill.set_logging_for_cellular_test()
196 self.modem_manager.SetDebugLogging()
197
198
Thieu Le99a39082014-09-09 16:00:13 -0700199 def _verify_sim(self):
200 """Verify SIM is valid.
201
202 Make sure a SIM in inserted and that it is not locked.
203
204 @raise error.TestError if SIM does not exist or is locked.
205
206 """
207 modem_device = self._get_shill_cellular_device_object()
208 props = modem_device.GetProperties()
209
210 # No SIM in CDMA modems.
211 family = props[
212 cellular_proxy.CellularProxy.DEVICE_PROPERTY_TECHNOLOGY_FAMILY]
213 if (family ==
214 cellular_proxy.CellularProxy.
215 DEVICE_PROPERTY_TECHNOLOGY_FAMILY_CDMA):
216 return
217
218 # Make sure there is a SIM.
219 if not props[cellular_proxy.CellularProxy.DEVICE_PROPERTY_SIM_PRESENT]:
220 raise error.TestError('There is no SIM in the modem.')
221
222 # Make sure SIM is not locked.
223 lock_status = props.get(
224 cellular_proxy.CellularProxy.DEVICE_PROPERTY_SIM_LOCK_STATUS,
225 None)
226 if lock_status is None:
227 raise error.TestError('Failed to read SIM lock status.')
228 locked = lock_status.get(
229 cellular_proxy.CellularProxy.PROPERTY_KEY_SIM_LOCK_ENABLED,
230 None)
231 if locked is None:
232 raise error.TestError('Failed to read SIM LockEnabled status.')
233 elif locked:
234 raise error.TestError(
235 'SIM is locked, test requires an unlocked SIM.')
236
237
Thieu Le98327a42014-08-21 18:11:41 -0700238 def _verify_backchannel(self):
239 """Verify backchannel is on an ethernet device.
240
241 @raise error.TestError if backchannel is not on an ethernet device.
242
243 """
Thieu Le8bded2b2014-09-03 15:38:46 -0700244 if not backchannel.is_backchannel_using_ethernet():
245 raise error.TestError('An ethernet connection is required between '
246 'the test server and the device under test.')
Thieu Le98327a42014-08-21 18:11:41 -0700247
248
Thieu Le98327a42014-08-21 18:11:41 -0700249 def _wait_for_modem_registration(self):
250 """Wait for the modem to register with the network.
251
Thieu Le98327a42014-08-21 18:11:41 -0700252 @raise error.TestError if modem is not registered.
253
254 """
Thieu Lee3b3fcf2014-09-08 13:56:15 -0700255 utils.poll_for_condition(
256 self.modem.ModemIsRegistered,
257 exception=error.TestError(
258 'Modem failed to register with the network.'),
259 timeout=cellular_proxy.CellularProxy.SERVICE_REGISTRATION_TIMEOUT)
Thieu Le98327a42014-08-21 18:11:41 -0700260
261
262 def _verify_cellular_service(self):
263 """Make sure a cellular service exists.
264
265 The cellular service should not be connected to the network.
266
267 @raise error.TestError if cellular service does not exist or if
268 there are multiple cellular services.
269
270 """
271 service = self.shill.wait_for_cellular_service_object()
272
273 try:
274 service.Disconnect()
275 except dbus.DBusException as e:
276 if (e.get_dbus_name() !=
277 cellular_proxy.CellularProxy.ERROR_NOT_CONNECTED):
278 raise
Thieu Lee3b3fcf2014-09-08 13:56:15 -0700279 success, state, _ = self.shill.wait_for_property_in(
Thieu Le98327a42014-08-21 18:11:41 -0700280 service,
281 cellular_proxy.CellularProxy.SERVICE_PROPERTY_STATE,
282 ('idle',),
283 cellular_proxy.CellularProxy.SERVICE_DISCONNECT_TIMEOUT)
284 if not success:
285 raise error.TestError(
Thieu Lee3b3fcf2014-09-08 13:56:15 -0700286 'Cellular service needs to start in the "idle" state. '
287 'Current state is "%s". '
288 'Modem disconnect may have failed.' %
289 state)
Thieu Le98327a42014-08-21 18:11:41 -0700290
291
292class CellularOTATestEnvironment(CellularTestEnvironment):
293 """Setup and verify cellular over-the-air (OTA) test environment. """
294 def __init__(self, **kwargs):
295 super(CellularOTATestEnvironment, self).__init__(**kwargs)
296
297
298class CellularPseudoMMTestEnvironment(CellularTestEnvironment):
299 """Setup and verify cellular pseudomodem test environment. """
300 def __init__(self, pseudomm_args=None, **kwargs):
301 """
302 @param pseudomm_args: Tuple of arguments passed to the pseudomodem, see
303 pseudomodem_context.py for description of each argument in the
304 tuple: (flags_map, block_output, bus)
305
306 """
307 super(CellularPseudoMMTestEnvironment, self).__init__(**kwargs)
308 self._context_managers.append(
Thieu Le6724f282014-09-04 15:36:41 -0700309 pseudomodem_context.PseudoModemManagerContext(
310 True, bus=self.bus, *pseudomm_args))
Thieu Le98327a42014-08-21 18:11:41 -0700311
312
313class CellularWardModemTestEnvironment(CellularTestEnvironment):
314 """Setup and verify cellular ward modem test environment. """
315 def __init__(self, wardmodem_modem=None, **kwargs):
316 """
317 @param wardmodem_modem: Customized ward modem to use instead of the
318 default implementation, see wardmodem.py.
319
320 """
321 super(CellularWardModemTestEnvironment, self).__init__(**kwargs)
322 self._context_managers.append(
323 wardmodem.WardModemContext(args=['--modem', wardmodem_modem]))