usb.py 3.4 KB
Newer Older
A
air9 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
#!/usr/bin/env python
# coding: utf-8

# Copyright (c) 2020 Huawei Technologies Co., Ltd.
# oec-hardware is licensed under the Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#     http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
# PURPOSE.
# See the Mulan PSL v2 for more details.
# Create: 2020-04-01

import os
import sys
import time

from hwcert.test import Test
from hwcert.commandUI import CommandUI
from hwcert.command import Command, CertCommandError
from hwcert.device import CertDevice, Device


class UsbTest(Test):

    def __init__(self):
        Test.__init__(self)
        self.requirements = ["usbutils"]
        self.ui = CommandUI()

    def test(self):
        print("USB device:")
        Command("lsusb -t").echo()
        print("")
        sys.stdout.flush()
        plugged_device = self.get_usb()

        print("USB device plug/unplug test begin...")
        while True:
            print("#############")
            while True:
                print("Please plug in a USB device.")
                if self.ui.prompt_confirm("Done well?"):
                    break
            time.sleep(1)

            new_plugged = self.get_usb()
            if len(new_plugged) <= len(plugged_device):
                print("Error: no USB device add.")
                return False

            new_device = None
            for device in new_plugged:
                if device not in plugged_device:
                    print("Found new USB device.\n")
                    new_device = device
                    break

            if not new_device:
                print("Error: new USB device not found.")
                return False

            print("USB device:")
            Command("lsusb -t").echo()
            print("")
            sys.stdout.flush()
            plugged_device = new_plugged
            while True:
                print("Please unplug the USB device you plugged in just now.")
                if self.ui.prompt_confirm("Done well?"):
                    break
            time.sleep(1)

            new_plugged = self.get_usb()
            if len(new_plugged) >= len(plugged_device):
                print("Error: no USB device unplug.")
                return False

            if new_device in new_plugged:
                print("Error: the USB device can still be found.")
                return False
            else:
                print("USB device unplugged.\n")

            print("USB device:")
            Command("lsusb -t").echo()
            print("#############\n")
            sys.stdout.flush()
            plugged_device = new_plugged

            if self.ui.prompt_confirm("All usb sockets have been tested?"):
                return True

    def get_usb(self):
        devices = CertDevice().get_devices()
        usb_devices = list()
        for device in devices:
            if (device.get_property("SUBSYSTEM") != "usb" or \
                device.get_property("DEVTYPE") != "usb_device" or \
                device.get_property("ID_BUS") != "usb" or \
                device.get_property("BUSNUM") == "" or device.get_property("DEVNUM") == ""):
                continue
            else:
                usb_devices.append(device.path)
        return usb_devices