cellular: Add containment for jinlon modem reboot

To unblock all tests on jinlon, add a containment to fix a known issue
that puts the modem in a bad state and leads to test failures. The
containment can be removed once the issue has been fixed.

BUG=b:180508924
TEST=test_that --board=hatch $IP cellular_ValidateTestEnvironment.verizon

Change-Id: I4e65a54c99e3b5a3386c415958113ab9d2bb0f7b
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/autotest/+/2702434
Reviewed-by: Dinesh Kumar Sunkara <dsunkara@google.com>
Reviewed-by: Harpreet Grewal <harpreet@chromium.org>
Tested-by: Andrew Lassalle <andrewlassalle@chromium.org>
Auto-Submit: Andrew Lassalle <andrewlassalle@chromium.org>
Commit-Queue: Harpreet Grewal <harpreet@chromium.org>
diff --git a/client/cros/cellular/test_environment.py b/client/cros/cellular/test_environment.py
index b8e3945..856367c 100644
--- a/client/cros/cellular/test_environment.py
+++ b/client/cros/cellular/test_environment.py
@@ -3,14 +3,18 @@
 # found in the LICENSE file.
 
 import contextlib
+from time import sleep
 import dbus
 import logging
+import os
+import subprocess
 import sys
 import traceback
 
 import common
 from autotest_lib.client.bin import utils
 from autotest_lib.client.common_lib import error
+from autotest_lib.client.bin import utils
 from autotest_lib.client.cros import backchannel
 from autotest_lib.client.cros import upstart
 from autotest_lib.client.cros.cellular import mm
@@ -102,6 +106,26 @@
 
     def __enter__(self):
         try:
+            # TODO(b/180508924): The following containment restarts the Fibocom
+            # modem on jinlon devices after 3 seconds if the modem gets stuck.
+            # Once b/179795020 is fixed, the containment can be removed.
+            if self._enable_temp_containments and utils.get_board() == 'hatch':
+                retries = 3
+                while (retries >= 0 and not '2cb7:0007' in
+                       CellularTestEnvironment.get_usb_data()):
+                    if retries == 0:
+                        logging.info("Force rebooting Fibocom modem.")
+                        os.system(
+                                '/opt/google/modemfwd-helpers/l850gl-helper ' \
+                                '--reboot --power_enable_gpio=218'
+                        )
+                        sleep(4)
+                    else:
+                        logging.info("Waiting for usb device 2cb7:0007.")
+                        sleep(1)
+
+                    retries -= 1
+
             if upstart.has_service('modemfwd') and upstart.is_running('modemfwd'):
                 upstart.stop_job('modemfwd')
             # Temporarily disable shill autoconnect to cellular service while
@@ -158,6 +182,11 @@
                                       'Is the modem plugged in?'),
             timeout=shill_proxy.ShillProxy.DEVICE_ENUMERATION_TIMEOUT)
 
+    @staticmethod
+    def get_usb_data():
+        return subprocess.Popen('lsusb', shell=True,
+                                stdout=subprocess.PIPE).stdout
+
     def _enable_modem(self):
         modem_device = self._get_shill_cellular_device_object()
         try: