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"""
5MBIM_CID_DEVICE_CAPS Sequence
6
7Reference:
8    [1] Universal Serial Bus Communication Class MBIM Compliance Testing: 22
9        http://www.usb.org/developers/docs/devclass_docs/MBIM-Compliance-1.0.pdf
10"""
11import common
12
13from autotest_lib.client.cros.cellular.mbim_compliance import mbim_channel
14from autotest_lib.client.cros.cellular.mbim_compliance \
15        import mbim_command_message
16from autotest_lib.client.cros.cellular.mbim_compliance import mbim_constants
17from autotest_lib.client.cros.cellular.mbim_compliance import mbim_errors
18from autotest_lib.client.cros.cellular.mbim_compliance \
19        import mbim_message_request
20from autotest_lib.client.cros.cellular.mbim_compliance \
21        import mbim_message_response
22from autotest_lib.client.cros.cellular.mbim_compliance.sequences \
23        import sequence
24
25
26class MBIMCIDDeviceCapsSequence(sequence.Sequence):
27    """
28    Implement |MBIMCIDDeviceCapsSequence|.
29    In this sequence, cid |MBIM_CID_DEVICE_CAPS| and uuid |UUID_BASIC_CONNECT|
30    are used to retrieve a MBIM command done response with a
31    |MBIM_DEVICE_CPAS_INFO| in its information buffer.
32    """
33
34    def run_internal(self):
35        """ Run the MBIM_CID_DEVICE_CAPS Sequence. """
36        # Step 1
37        # Send MBIM_COMMAND_MSG.
38        device_context = self.device_context
39        descriptor_cache = device_context.descriptor_cache
40        command_message = mbim_command_message.MBIMDeviceCapsQuery()
41        packets = mbim_message_request.generate_request_packets(
42                command_message,
43                device_context.max_control_transfer_size)
44        channel = mbim_channel.MBIMChannel(
45                device_context._device,
46                descriptor_cache.mbim_communication_interface.bInterfaceNumber,
47                descriptor_cache.interrupt_endpoint.bEndpointAddress,
48                device_context.max_control_transfer_size)
49        response_packets = channel.bidirectional_transaction(*packets)
50        channel.close()
51
52        # Step 2
53        response_message = mbim_message_response.parse_response_packets(
54                response_packets)
55
56        # Step 3
57        is_message_valid = isinstance(
58                response_message,
59                mbim_command_message.MBIMDeviceCapsInfo)
60        if ((not is_message_valid) or
61            (response_message.message_type !=
62             mbim_constants.MBIM_COMMAND_DONE) or
63            (response_message.status_codes !=
64             mbim_constants.MBIM_STATUS_SUCCESS)):
65            mbim_errors.log_and_raise(mbim_errors.MBIMComplianceAssertionError,
66                                      'mbim1.0:9.4.3')
67
68        return command_message, response_message
69