1# Copyright 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"""
5Loopback NTB-16/32 Sequence
6
7Reference:
8    [1] Universal Serial Bus Communication Class MBIM Compliance Testing: 20
9        http://www.usb.org/developers/docs/devclass_docs/MBIM-Compliance-1.0.pdf
10"""
11import array
12
13import common
14from autotest_lib.client.cros.cellular.mbim_compliance import mbim_data_transfer
15from autotest_lib.client.cros.cellular.mbim_compliance.sequences \
16        import sequence
17
18class LoopbackSequence(sequence.Sequence):
19    """
20    Data loopback sequence used for data transfer testing.
21
22    In this sequence, we send out an IPv4 ping packet to the device which is
23    in |connected| state and fetch the repsonse packet received from the device.
24
25    """
26    # Payload to be used for our test. This is an IPv4 ICMP ping packet
27    DATA_PAYLOAD = array.array('B', [0x45, 0x00, 0x00, 0x54, 0xB4, 0x5A, 0x00,
28                                     0x00, 0x40, 0x01, 0x42, 0xF2, 0xC0, 0xA8,
29                                     0x01, 0x0B, 0xC0, 0xA8, 0x01, 0x01, 0x00,
30                                     0x00, 0x54, 0xC0, 0x3C, 0xD7, 0x00, 0x08,
31                                     0x6A, 0x6F, 0xB5, 0x54, 0x00, 0x00, 0x00,
32                                     0x00, 0x8B, 0xC9, 0x04, 0x00, 0x00, 0x00,
33                                     0x00, 0x00, 0x10, 0x11, 0x12, 0x13, 0x14,
34                                     0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B,
35                                     0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22,
36                                     0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29,
37                                     0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
38                                     0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37])
39
40    def run_internal(self, ntb_format):
41        """
42        Run the MBIM Loopback Sequence.
43
44        Need to run the |connect| sequence before invoking this loopback
45        sequence.
46
47        @param ntb_format: Whether to send/receive an NTB16 or NTB32 frame.
48                Possible values: NTB_FORMAT_16, NTB_FORMAT_32 (mbim_constants)
49        @returns tuple of (nth, ndp, ndp_entries, payload) where,
50                nth - NTH header object received.
51                ndp - NDP header object received.
52                ndp_entries - Array of NDP entry header objects.
53                payload - Array of packets where each packet is a byte array.
54
55        """
56        # Step 1 is to run |connect| sequence which is expected to be run
57        # before calling this to avoid calling sequences within another
58        # sequence.
59
60        # Step 2
61        data_transfer = mbim_data_transfer.MBIMDataTransfer(self.device_context)
62        data_transfer.send_data_packets(ntb_format, [self.DATA_PAYLOAD])
63
64        # Step 3
65        nth, ndp, ndp_entries, payload = data_transfer.receive_data_packets(
66                ntb_format)
67
68        return (nth, ndp, ndp_entries, payload)
69