1# Copyright (c) 2015 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 logging
6
7import common
8from autotest_lib.client.cros.cellular.mbim_compliance import entity
9from autotest_lib.client.cros.cellular.mbim_compliance import mbim_errors
10
11
12class Sequence(entity.Entity):
13    """ Base class for all sequences. """
14
15    def run(self, **kwargs):
16        """
17        Run the sequence.
18
19        @returns The result of the sequence. The type of value returned varies
20                 by the sequence type.
21
22        """
23        logging.info('---- Sequence (%s) begin ----', self.name())
24        result = self.run_internal(**kwargs)
25        logging.info('---- Sequence (%s) end ----', self.name())
26        return result
27
28    def run_internal(self):
29        """
30        The actual method runs the sequence.
31        Subclasses should override this method to run their own sequence.
32
33        """
34        mbim_errors.log_and_raise(NotImplementedError)
35
36
37    def name(self):
38        """ Return str name. """
39        return self.__class__.__name__
40
41
42    def detach_kernel_driver_if_active(self, interface_number):
43        """
44        Check if interfaces are occupied by kernel driver. If kernel driver is
45        active, then we can't exclusively use the inteface for tests.
46
47        @param interface_number: The bInterfaceNumber value of the interface
48                under check.
49
50        """
51        if self.device_context.device.is_kernel_driver_active(interface_number):
52            self.device_context.device.detach_kernel_driver(interface_number)
53