⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rwbulk.c

📁 usb范例代码
💻 C
📖 第 1 页 / 共 2 页
字号:
                    usage();
            }
        }
    }
}

BOOL
compare_buffs (
    PCHAR   buff1,
    PCHAR   buff2,
    int     length
)
/*++
   Routine Description:

   Called to verify read and write buffers match for loopback test

   Arguments:

   buffers to compare and length

   Return Value:

   TRUE if buffers match, else FALSE

--*/
{
    int ok = 1;

    if (memcmp(buff1, buff2, length))
    {
        ok = 0;
    }

    return ok;
}

#define NPERLN 8

void
dump (
    PUCHAR  b,
    int     len
)
/*++
   Routine Description:

   Called to do formatted ascii dump to console of the io buffer

   Arguments:

   buffer and length

   Return Value:

   none

--*/
{
    ULONG i;
    ULONG longLen = (ULONG)len / sizeof( ULONG );
    PULONG pBuf = (PULONG) b;

    // dump an ordinal ULONG for each sizeof(ULONG)'th byte
    printf("\n****** BEGIN DUMP LEN decimal %d, 0x%x\n", len,len);

    for (i = 0; i < longLen; i++)
    {
        printf("%04X ", *pBuf++);

        if (i % NPERLN == (NPERLN - 1))
        {
            printf("\n");
        }
    }

    if (i % NPERLN != 0)
    {
        printf("\n");
    }
    printf("\n****** END DUMP LEN decimal %d, 0x%x\n", len,len);
}





// Begin, routines for USB configuration dump (Cmdline "rwbulk -u" )


VOID
ShowEndpointDescriptor (
    PUSB_ENDPOINT_DESCRIPTOR    EndpointDesc
)
/*++
Routine Description:

    Called to do formatted ascii dump to console of a USB endpoint descriptor

Arguments:

    ptr to USB endpoint descriptor,

Return Value:

    none

--*/
{
    printf("--------------------\n");
    printf("Endpoint Descriptor:\n");

    if (USB_ENDPOINT_DIRECTION_IN(EndpointDesc->bEndpointAddress))
    {
        printf("bEndpointAddress:     0x%02X  IN\n",
               EndpointDesc->bEndpointAddress);
    }
    else
    {
        printf("bEndpointAddress:     0x%02X  OUT\n",
               EndpointDesc->bEndpointAddress);
    }

    switch (EndpointDesc->bmAttributes & 0x03)
    {
        case 0x00:
            printf("Transfer Type:     Control\n");
            break;

        case 0x01:
            printf("Transfer Type: Isochronous\n");
            break;

        case 0x02:
            printf("Transfer Type:        Bulk\n");
            break;

        case 0x03:
            printf("Transfer Type:   Interrupt\n");
            break;
    }

    printf("wMaxPacketSize:     0x%04X (%d)\n",
           EndpointDesc->wMaxPacketSize,
           EndpointDesc->wMaxPacketSize);

    printf("bInterval:            0x%02X\n",
           EndpointDesc->bInterval);
}


VOID
ShowInterfaceDescriptor (
    PUSB_INTERFACE_DESCRIPTOR   InterfaceDesc
)
/*++
Routine Description:

    Called to do formatted ascii dump to console of a USB interface descriptor

Arguments:

    ptr to USB interface descriptor

Return Value:

    none

--*/
{
    printf("\n---------------------\n");
    printf("Interface Descriptor:\n");

    printf("bInterfaceNumber:     0x%02X\n",
           InterfaceDesc->bInterfaceNumber);

    printf("bAlternateSetting:    0x%02X\n",
           InterfaceDesc->bAlternateSetting);

    printf("bNumEndpoints:        0x%02X\n",
           InterfaceDesc->bNumEndpoints);

    printf("bInterfaceClass:      0x%02X\n",
           InterfaceDesc->bInterfaceClass);

    printf("bInterfaceSubClass:   0x%02X\n",
           InterfaceDesc->bInterfaceSubClass);

    printf("bInterfaceProtocol:   0x%02X\n",
           InterfaceDesc->bInterfaceProtocol);

    printf("iInterface:           0x%02X\n",
           InterfaceDesc->iInterface);

}


VOID
ShowConfigurationDescriptor (
    PUSB_CONFIGURATION_DESCRIPTOR   ConfigDesc
)
/*++
Routine Description:

    Called to do formatted ascii dump to console of a USB config descriptor

Arguments:

    ptr to USB configuration descriptor

Return Value:

    none

--*/
{
    printf("=========================\n");
    printf("Configuration Descriptor:\n");

    printf("wTotalLength:       0x%04X\n",
           ConfigDesc->wTotalLength);

    printf("bNumInterfaces:       0x%02X\n",
           ConfigDesc->bNumInterfaces);

    printf("bConfigurationValue:  0x%02X\n",
           ConfigDesc->bConfigurationValue);

    printf("iConfiguration:       0x%02X\n",
           ConfigDesc->iConfiguration);

    printf("bmAttributes:         0x%02X\n",
           ConfigDesc->bmAttributes);

    if (ConfigDesc->bmAttributes & 0x80)
    {
        printf("  Bus Powered\n");
    }

    if (ConfigDesc->bmAttributes & 0x40)
    {
        printf("  Self Powered\n");
    }

    if (ConfigDesc->bmAttributes & 0x20)
    {
        printf("  Remote Wakeup\n");
    }

    printf("MaxPower:             0x%02X (%d Ma)\n",
           ConfigDesc->MaxPower,
           ConfigDesc->MaxPower * 2);

}


VOID
ShowConfigDesc (
    PUSB_CONFIGURATION_DESCRIPTOR   ConfigDesc
)
{
    PUCHAR                  descEnd;
    PUSB_COMMON_DESCRIPTOR  commonDesc;
    BOOLEAN                 ShowUnknown;

    descEnd = (PUCHAR)ConfigDesc + ConfigDesc->wTotalLength;

    commonDesc = (PUSB_COMMON_DESCRIPTOR)ConfigDesc;

    while ((PUCHAR)commonDesc + sizeof(USB_COMMON_DESCRIPTOR) < descEnd &&
           (PUCHAR)commonDesc + commonDesc->bLength <= descEnd)
    {
        ShowUnknown = FALSE;

        switch (commonDesc->bDescriptorType)
        {
            case USB_CONFIGURATION_DESCRIPTOR_TYPE:
                if (commonDesc->bLength != sizeof(USB_CONFIGURATION_DESCRIPTOR))
                {
                    ShowUnknown = TRUE;
                    break;
                }
                ShowConfigurationDescriptor((PUSB_CONFIGURATION_DESCRIPTOR)commonDesc);
                break;

            case USB_INTERFACE_DESCRIPTOR_TYPE:
                if (commonDesc->bLength != sizeof(USB_INTERFACE_DESCRIPTOR))
                {
                    ShowUnknown = TRUE;
                    break;
                }
                ShowInterfaceDescriptor((PUSB_INTERFACE_DESCRIPTOR)commonDesc);
                break;

            case USB_ENDPOINT_DESCRIPTOR_TYPE:
                if (commonDesc->bLength != sizeof(USB_ENDPOINT_DESCRIPTOR))
                {
                    ShowUnknown = TRUE;
                    break;
                }
                ShowEndpointDescriptor((PUSB_ENDPOINT_DESCRIPTOR)commonDesc);
                break;

            default:
                ShowUnknown = TRUE;
                break;
        }

        if (ShowUnknown)
        {
            // ShowUnknownDescriptor(commonDesc);
        }

        (PUCHAR)commonDesc += commonDesc->bLength;
    }
}


void dumpUsbConfig()
/*++
Routine Description:

    Called to do formatted ascii dump to console of  USB
    configuration, interface, and endpoint descriptors
    (Cmdline "rwiso -u" )

Arguments:

    none

Return Value:

    none

--*/
{
    HANDLE  hDEV;
    ULONG   success;
    int     siz;
    int     nBytes;
    char    buf[4096];

    PUSB_CONFIGURATION_DESCRIPTOR configDesc;

    hDEV = open_dev();

    if (hDEV && hDEV != INVALID_HANDLE_VALUE)
    {
        siz = sizeof(buf);

        nBytes = 0;

        success = DeviceIoControl(hDEV,
                                  IOCTL_BULKUSB_GET_CONFIG_DESCRIPTOR,
                                  buf,
                                  siz,
                                  buf,
                                  siz,
                                  &nBytes,
                                  NULL);

        NOISY(("request complete, success = %d nBytes = %d\n", success, nBytes));

        configDesc = (PUSB_CONFIGURATION_DESCRIPTOR)buf;

        if (success && configDesc->wTotalLength == nBytes)
        {
            ShowConfigDesc(configDesc);
        }

        CloseHandle(hDEV);
    }
}

//  End, routines for USB configuration and pipe info dump  (Cmdline "rwbulk -u" )



int _cdecl
main(
    int     argc,
    char   *argv[]
)
/*++
Routine Description:

Entry point to rwbulk.exe
Parses cmdline, performs user-requested tests

Arguments:

argc, argv  standard console  'c' app arguments

Return Value:

Zero

--*/

{
    PCHAR pinBuf = NULL;
    PCHAR poutBuf = NULL;
    int nBytesRead = 0, nBytesWrite = 0, nBytes = 0;
    ULONG i, j;
    int ok;
    UINT success;
    HANDLE hRead = INVALID_HANDLE_VALUE, hWrite = INVALID_HANDLE_VALUE;
    char buf[1024];
    clock_t start, finish;
    ULONG totalBytes = 0L;
    double seconds;
    ULONG fail = 0L;

    parse(argc, argv);

    // dump USB configuation and pipe info
    if(fDumpUsbConfig)
    {
        dumpUsbConfig();
    }


    // doing a read, write, or both test
    if ((fRead) || (fWrite))
    {
        if (fRead)
        {
            //
            // open the output file
            //
            if (fDumpReadData)
            { // round size to sizeof ULONG for readable dumping
                while (ReadLen % sizeof(ULONG))
                {
                    ReadLen++;
                }
            }

            hRead = open_file(inPipe);

            pinBuf = malloc(ReadLen);
        }

        if (fWrite)
        {
            if (fDumpReadData)
            { // round size to sizeof ULONG for readable dumping
                while(WriteLen % sizeof(ULONG))
                {
                    WriteLen++;
                }
            }

            hWrite = open_file( outPipe);
            poutBuf = malloc(WriteLen);
        }

        for (i = 0; i < IterationCount; i++)
        {
            if (fWrite && poutBuf && hWrite != INVALID_HANDLE_VALUE)
            {

                PULONG pOut = (PULONG)poutBuf;
                ULONG  numLongs = WriteLen / sizeof( ULONG );
                //
                // put some data in the output buffer
                //

                for (j=0; j<numLongs; j++)
                {
                    *(pOut+j) = j;
                }

                //
                // send the write
                //

                WriteFile(hWrite,
                          poutBuf,
                          WriteLen,
                          &nBytesWrite,
                          NULL);

                printf("<%s> W (%04.4d) : request %06.6d bytes -- %06.6d bytes written\n",
                       outPipe, i, WriteLen, nBytesWrite);
                assert(nBytesWrite == WriteLen);
            }

            if (fRead && pinBuf)
            {
                success = ReadFile(hRead,
                                   pinBuf,
                                   ReadLen,
                                   &nBytesRead,
                                   NULL);
                if (success)
                {
                    printf("<%s> R (%04.4d) : request %06.6d bytes -- %06.6d bytes read\n",
                           inPipe, i, ReadLen, nBytesRead);
                }
                else
                {
                    printf("ReadFile failed with error code %d\n", GetLastError());
                }
                if (fWrite)
                {
                    //
                    // validate the input buffer against what
                    // we sent to the 82930 (loopback test)
                    //

                    ok = compare_buffs(pinBuf, poutBuf,  nBytesRead);

                    if (fDumpReadData)
                    {
                        printf("Dumping read buffer\n");
                        dump( pinBuf, nBytesRead );

                        printf("Dumping write buffer\n");
                        dump( poutBuf, nBytesRead );
                    }

                    assert(ok);

                    if (ok != 1)
                    {
                        fail++;
                    }

                    assert(ReadLen == WriteLen);
                    assert(nBytesRead == ReadLen);
                    assert(nBytesWrite == WriteLen);
                }
            }
        }

        if (pinBuf)
        {
            free(pinBuf);
        }

        if (poutBuf)
        {
            free(poutBuf);
        }

        // close devices if needed
        if(hRead != INVALID_HANDLE_VALUE)
        {
            CloseHandle(hRead);
        }

        if(hWrite != INVALID_HANDLE_VALUE)
        {
            CloseHandle(hWrite);
        }
    }

    return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -