Home | History | Annotate | Download | only in host
      1 #!/usr/bin/env python
      2 #
      3 # Copyright (C) 2018 The Android Open Source Project
      4 #
      5 # Licensed under the Apache License, Version 2.0 (the "License");
      6 # you may not use this file except in compliance with the License.
      7 # You may obtain a copy of the License at
      8 #
      9 #      http://www.apache.org/licenses/LICENSE-2.0
     10 #
     11 # Unless required by applicable law or agreed to in writing, software
     12 # distributed under the License is distributed on an "AS IS" BASIS,
     13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14 # See the License for the specific language governing permissions and
     15 # limitations under the License.
     16 #
     17 
     18 import logging
     19 import time
     20 import usb1
     21 
     22 from vts.runners.host import asserts
     23 from vts.runners.host import base_test
     24 from vts.runners.host import const
     25 from vts.runners.host import test_runner
     26 from vts.testcases.template.hal_hidl_host_test import hal_hidl_host_test
     27 from vts.utils.python.controllers import adb
     28 
     29 
     30 class UsbGadgetHidlTest(hal_hidl_host_test.HalHidlHostTest):
     31     """A host-side test for USB Gadget HAL.
     32 
     33     This test requires Android framework to run.
     34     """
     35 
     36     TEST_HAL_SERVICES = {"android.hardware.usb.gadget (at] 1.0::IUsbGadget"}
     37 
     38     def setUpClass(self):
     39         """Creates an adb session and reads sysprop values."""
     40         super(UsbGadgetHidlTest, self).setUpClass()
     41 
     42         self.adb = self.dut.adb
     43         try:
     44             self.adb.root()
     45             self.adb.wait_for_device()
     46         except adb.AdbError as e:
     47             logging.exception(e)
     48         self.serialno = self.adb.shell("\"getprop ro.serialno\"")
     49 
     50     def checkProtocol(self, usb_class, usb_sub_class, usb_protocol):
     51         """Queries the host USB bus to see if the interface is present.
     52 
     53         Args:
     54             usb_class: usbClass id of the interface.
     55             usb_sub_class: usbSubClass id of the interface.
     56             usb_protocol: usbProtocol id of the interface.
     57 
     58         Returns:
     59             True if the usb interface was present. False otherwise.
     60         """
     61         with usb1.USBContext() as context:
     62             for device in context.getDeviceIterator(skip_on_error=True):
     63                 logging.info("ID %04x:%04x ", device.getVendorID(),
     64                              device.getProductID())
     65                 for config in device.iterConfigurations():
     66                     logging.info("config: %d", config.getConfigurationValue())
     67                     interfaces_list = iter(config)
     68                     for interface in interfaces_list:
     69                         altsettings_list = iter(interface)
     70                         for altsetting in altsettings_list:
     71                             logging.info("interfaceNum:%d altSetting:%d "
     72                                          "class:%d subclass:%d protocol:%d",
     73                                          altsetting.getNumber(),
     74                                          altsetting.getAlternateSetting(),
     75                                          altsetting.getClass(),
     76                                          altsetting.getSubClass(),
     77                                          altsetting.getProtocol())
     78                             if altsetting.getClass() == usb_class and \
     79                                 altsetting.getSubClass() == usb_sub_class and \
     80                                 altsetting.getProtocol() == usb_protocol:
     81                                 return True
     82         return False
     83 
     84     def testAdb(self):
     85         """Check for ADB"""
     86         asserts.assertTrue(self.checkProtocol(255, 66, 1), "ADB not present")
     87 
     88     def testMtp(self):
     89         """Check for MTP.
     90 
     91         Enables mtp and checks the host to see if mtp interface is present.
     92         MTP: https://en.wikipedia.org/wiki/Media_Transfer_Protocol.
     93         """
     94         self.adb.shell("svc usb setFunctions mtp true")
     95         time.sleep(3)
     96         asserts.assertTrue(self.checkProtocol(6, 1, 1), "MTP not present")
     97 
     98     def testPtp(self):
     99         """Check for PTP.
    100 
    101         Enables ptp and checks the host to see if ptp interface is present.
    102         PTP: https://en.wikipedia.org/wiki/Picture_Transfer_Protocol.
    103         """
    104         self.adb.shell("svc usb setFunctions ptp true")
    105         time.sleep(3)
    106         asserts.assertTrue(self.checkProtocol(6, 1, 1), "PTP not present")
    107 
    108     def testMIDI(self):
    109         """Check for MIDI.
    110 
    111         Enables midi and checks the host to see if midi interface is present.
    112         MIDI: https://en.wikipedia.org/wiki/MIDI.
    113         """
    114         self.adb.shell("svc usb setFunctions midi true")
    115         time.sleep(3)
    116         asserts.assertTrue(self.checkProtocol(1, 3, 0), "MIDI not present")
    117 
    118     def testRndis(self):
    119         """Check for RNDIS.
    120 
    121         Enables rndis and checks the host to see if rndis interface is present.
    122         RNDIS: https://en.wikipedia.org/wiki/RNDIS.
    123         """
    124         self.adb.shell("svc usb setFunctions rndis true")
    125         time.sleep(3)
    126         asserts.assertTrue(self.checkProtocol(10, 0, 0), "RNDIS not present")
    127 
    128 
    129 if __name__ == "__main__":
    130     test_runner.main()
    131