VirtualBox

Changeset 60860 in vbox for trunk/src/VBox/ValidationKit


Ignore:
Timestamp:
May 6, 2016 12:14:10 PM (9 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
107070
Message:

ValidationKit/usb/UsbTest: New test to check that the reported device speed matches what we want to test

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/ValidationKit/utils/usb/UsbTest.cpp

    r60522 r60860  
    4040#include <iprt/string.h>
    4141#include <iprt/test.h>
     42
     43#include <iprt/linux/sysfs.h>
    4244
    4345#include <unistd.h>
     
    138140typedef USBTESTDESC *PUSBTESTDESC;
    139141
     142/**
     143 * USB speed values.
     144 */
     145typedef enum USBTESTSPEED
     146{
     147    USBTESTSPEED_ANY = 0,
     148    USBTESTSPEED_UNKNOWN,
     149    USBTESTSPEED_LOW,
     150    USBTESTSPEED_FULL,
     151    USBTESTSPEED_HIGH,
     152    USBTESTSPEED_SUPER
     153} USBTESTSPEED;
     154
    140155/*********************************************************************************************************************************
    141156*   Global Variables                                                                                                             *
     
    151166    {"--device",           'd', RTGETOPT_REQ_STRING },
    152167    {"--help",             'h', RTGETOPT_REQ_NOTHING},
    153     {"--exclude",          'e', RTGETOPT_REQ_UINT32}
     168    {"--exclude",          'e', RTGETOPT_REQ_UINT32},
     169    {"--expected-speed",   's', RTGETOPT_REQ_STRING }
    154170};
    155171
     
    186202/** The test handle. */
    187203static RTTEST g_hTest;
     204/** The expected device speed. */
     205static USBTESTSPEED g_enmSpeed = USBTESTSPEED_ANY;
    188206
    189207/**
     
    253271                pszHelp = "Exclude the given test id from the list";
    254272                break;
     273            case 's':
     274                pszHelp = "The device speed to expect";
     275                break;
    255276            default:
    256277                pszHelp = "Option undocumented";
     
    259280        char szOpt[256];
    260281        RTStrPrintf(szOpt, sizeof(szOpt), "%s, -%c", g_aCmdOptions[i].pszLong, g_aCmdOptions[i].iShort);
    261         RTStrmPrintf(pStrm, "  %-20s%s\n", szOpt, pszHelp);
     282        RTStrmPrintf(pStrm, "  %-30s%s\n", szOpt, pszHelp);
    262283    }
     284}
     285
     286/**
     287 * Searches for a USB test device and returns the bus and device ID and the device speed.
     288 */
     289static int usbTestDeviceQueryBusAndDevId(uint16_t *pu16BusId, uint16_t *pu16DevId, USBTESTSPEED *penmSpeed)
     290{
     291    bool fFound = false;
     292
     293#define USBTEST_USB_DEV_SYSFS "/sys/bus/usb/devices/"
     294
     295    PRTDIR pDirUsb = NULL;
     296    int rc = RTDirOpen(&pDirUsb, USBTEST_USB_DEV_SYSFS);
     297    if (RT_SUCCESS(rc))
     298    {
     299        do
     300        {
     301            RTDIRENTRY DirUsbBus;
     302            rc = RTDirRead(pDirUsb, &DirUsbBus, NULL);
     303            if (   RT_SUCCESS(rc)
     304                && RTStrNCmp(DirUsbBus.szName, "usb", 3)
     305                && RTLinuxSysFsExists(USBTEST_USB_DEV_SYSFS "%s/idVendor", DirUsbBus.szName))
     306            {
     307                int64_t idVendor  = 0;
     308                int64_t idProduct = 0;
     309                int64_t iBusId    = 0;
     310                int64_t iDevId    = 0;
     311                char    aszSpeed[20];
     312
     313                rc = RTLinuxSysFsReadIntFile(16, &idVendor, USBTEST_USB_DEV_SYSFS "%s/idVendor", DirUsbBus.szName);
     314                if (RT_SUCCESS(rc))
     315                    rc = RTLinuxSysFsReadIntFile(16, &idProduct, USBTEST_USB_DEV_SYSFS "%s/idProduct", DirUsbBus.szName);
     316                if (RT_SUCCESS(rc))
     317                    rc = RTLinuxSysFsReadIntFile(16, &iBusId, USBTEST_USB_DEV_SYSFS "%s/busnum", DirUsbBus.szName);
     318                if (RT_SUCCESS(rc))
     319                    rc = RTLinuxSysFsReadIntFile(16, &iDevId, USBTEST_USB_DEV_SYSFS "%s/devnum", DirUsbBus.szName);
     320                if (RT_SUCCESS(rc))
     321                    rc = RTLinuxSysFsReadStrFile(&aszSpeed[0], sizeof(aszSpeed), NULL, USBTEST_USB_DEV_SYSFS "%s/speed", DirUsbBus.szName);
     322
     323                if (   RT_SUCCESS(rc)
     324                    && idVendor == 0x0525
     325                    && idProduct == 0xa4a0)
     326                {
     327                    if (penmSpeed)
     328                    {
     329                        /* Parse the speed. */
     330                        if (!RTStrCmp(&aszSpeed[0], "1.5"))
     331                            *penmSpeed = USBTESTSPEED_LOW;
     332                        else if (!RTStrCmp(&aszSpeed[0], "12"))
     333                            *penmSpeed = USBTESTSPEED_FULL;
     334                        else if (!RTStrCmp(&aszSpeed[0], "480"))
     335                            *penmSpeed = USBTESTSPEED_HIGH;
     336                        else if (   !RTStrCmp(&aszSpeed[0], "5000")
     337                                 || !RTStrCmp(&aszSpeed[0], "10000"))
     338                            *penmSpeed = USBTESTSPEED_SUPER;
     339                        else
     340                            *penmSpeed = USBTESTSPEED_UNKNOWN;
     341                    }
     342
     343                    if (pu16BusId)
     344                        *pu16BusId = (uint16_t)iBusId;
     345                    if (pu16DevId)
     346                        *pu16DevId = (uint16_t)iDevId;
     347                    fFound = true;
     348                    break;
     349                }
     350            }
     351            else if (rc != VERR_NO_MORE_FILES)
     352                rc = VINF_SUCCESS;
     353
     354        } while (   RT_SUCCESS(rc)
     355                 && !fFound);
     356
     357        if (rc == VERR_NO_MORE_FILES)
     358            rc = VINF_SUCCESS;
     359
     360        RTDirClose(pDirUsb);
     361    }
     362
     363    if (RT_SUCCESS(rc) && !fFound)
     364        rc = VERR_NOT_FOUND;
     365
     366    return rc;
    263367}
    264368
     
    439543                pszDevice = ValueUnion.psz;
    440544                break;
     545            case 's':
     546                if (!RTStrICmp(ValueUnion.psz, "Low"))
     547                    g_enmSpeed = USBTESTSPEED_LOW;
     548                else if (!RTStrICmp(ValueUnion.psz, "Full"))
     549                    g_enmSpeed = USBTESTSPEED_FULL;
     550                else if (!RTStrICmp(ValueUnion.psz, "High"))
     551                    g_enmSpeed = USBTESTSPEED_HIGH;
     552                else if (!RTStrICmp(ValueUnion.psz, "Super"))
     553                    g_enmSpeed = USBTESTSPEED_SUPER;
     554                else
     555                {
     556                    RTTestPrintf(g_hTest, RTTESTLVL_FAILURE, "Invalid speed passed to --expected-speed\n");
     557                    RTTestErrorInc(g_hTest);
     558                    return RTGetOptPrintError(VERR_INVALID_PARAMETER, &ValueUnion);
     559                }
     560                break;
    441561            case 'e':
    442562                if (ValueUnion.u32 < RT_ELEMENTS(g_aTests))
     
    471591
    472592    if (pszDevice)
     593    {
     594        /* First check that the requested speed matches. */
     595        if (g_enmSpeed != USBTESTSPEED_ANY)
     596        {
     597            RTTestSub(g_hTest, "Checking correct device speed");
     598
     599            USBTESTSPEED enmSpeed = USBTESTSPEED_UNKNOWN;
     600            rc = usbTestDeviceQueryBusAndDevId(NULL, NULL, &enmSpeed);
     601            if (RT_SUCCESS(rc))
     602            {
     603                if (enmSpeed == g_enmSpeed)
     604                    RTTestPassed(g_hTest, "Reported device speed matches requested speed\n");
     605                else
     606                    RTTestFailed(g_hTest, "Reported device speed doesn'match requested speed (%u vs %u)\n",
     607                                 enmSpeed, g_enmSpeed);
     608            }
     609            else
     610                RTTestFailed(g_hTest, "Failed to query device speed with rc=%Rrc\n", rc);
     611
     612            RTTestSubDone(g_hTest);
     613        }
    473614        usbTestExec(pszDevice);
     615    }
    474616
    475617    RTEXITCODE rcExit = RTTestSummaryAndDestroy(g_hTest);
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette