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

📄 osrbulk.cpp

📁 USBFX2LK WDM drive for OSR s USB FX2 Learning Kit
💻 CPP
📖 第 1 页 / 共 3 页
字号:
Called to do formatted ascii dump to console of a USB endpoint descriptor

Arguments:

ptr to USB endpoint descriptor,
index of this endpt in interface desc

Return Value:

none

--*/
void print_USB_ENDPOINT_DESCRIPTOR(PUSB_ENDPOINT_DESCRIPTOR ed, int i)
{
    printf(
        "------------------------------\nUSB_ENDPOINT_DESCRIPTOR for Pipe%02d\n", i
        );

    printf(
        "bLength = 0x%x\n", ed->bLength
        );

    printf(
        "bDescriptorType = 0x%x ( %s )\n", ed->bDescriptorType, usbDescriptorTypeString( ed->bDescriptorType )
        );


    if ( USB_ENDPOINT_DIRECTION_IN( ed->bEndpointAddress ) ) {
        printf(
            "bEndpointAddress= 0x%x ( INPUT )\n", ed->bEndpointAddress
            );
    } else {
        printf(
            "bEndpointAddress= 0x%x ( OUTPUT )\n", ed->bEndpointAddress
            );
    }

    printf(
        "bmAttributes= 0x%x ( %s )\n", ed->bmAttributes, usbEndPointTypeString ( ed->bmAttributes )
        );


    printf(
        "wMaxPacketSize= 0x%x, decimal %d\n", ed->wMaxPacketSize, ed->wMaxPacketSize
        );
    printf(
        "bInterval = 0x%x, decimal %d\n", ed->bInterval, ed->bInterval
        );
}

/*++
Routine Description:

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

Arguments:

handle to device

Return Value:

none

--*/
void rw_dev( HANDLE hDEV )
{
    UINT success;
    DWORD siz, nBytes;
    char buf[2048];
    PUSB_CONFIGURATION_DESCRIPTOR cd;
    PUSBD_INTERFACE_INFORMATION interfaceInformation;

    siz = sizeof(buf);

    if (hDEV == INVALID_HANDLE_VALUE) {
        NOISY(("DEV not open"));
        return;
    }

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


    if (success) {
        ULONG i;
        UINT  j, n;
        char *pch;

        pch = buf;
        n = 0;

        cd = (PUSB_CONFIGURATION_DESCRIPTOR) pch;

        print_USB_CONFIGURATION_DESCRIPTOR( cd );


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

        if (success) {
            interfaceInformation = (PUSBD_INTERFACE_INFORMATION) buf;
            print_USBD_INTERFACE_INFORMATION(interfaceInformation);

        } else {
            NOISY(("IOCTL_OSRUSBFX2_GET_INTERFACE_INFORMATION complete, error = %d nBytes = %d\n", GetLastError(), nBytes));
        }
    } else {
        NOISY(("IOCTL_OSRUSBFX2_GET_CONFIGURATION_DESCRIPTOR complete, error = %d nBytes = %d\n", GetLastError(), nBytes));
    }

    return;

}

/*++
Routine Description:

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

Arguments:

handle to device

Return Value:

none

--*/
void segment_dev( HANDLE hDEV )
{
    UINT success;
    DWORD siz, nBytes;
    unsigned char segmentToDisplay;

    if(SegmentNumber < 0 || SegmentNumber > 8) {
        NOISY(("Invalid Number"));
        return;
    }

    if (hDEV == INVALID_HANDLE_VALUE) {
        NOISY(("DEV not open"));
        return;
    }

    segmentToDisplay = (unsigned char) SegmentNumber;

    success = DeviceIoControl(hDEV,
        IOCTL_OSRUSBFX2_SET_BAR_GRAPH_DISPLAY,
        &segmentToDisplay,
        sizeof(unsigned char),
        NULL,
        0,
        &nBytes,
        NULL);

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

    return;

}

int  setSegmentLight()
{
    HANDLE hDEV = open_dev();

    if ( hDEV )
    {
        segment_dev( hDEV );
        CloseHandle(hDEV);
    }

    return 0;
}

/*++
Routine Description:

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

Arguments:

none

Return Value:

none

--*/
int  dumpUsbConfig()
{

    HANDLE hDEV = open_dev();

    if ( hDEV )
    {
        rw_dev( hDEV );
        CloseHandle(hDEV);
    }

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



/*++
Routine Description:

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

Arguments:

argc, argv  standard console  'c' app arguments

Return Value:

Zero

--*/
int _cdecl main(int argc,char *argv[])
{
    char *pinBuf = NULL, *poutBuf = NULL;
    DWORD nBytesRead, nBytesWrite, nBytes;
    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;
    OVERLAPPED  rOverlapped;
    OVERLAPPED  wOverlapped;
    HANDLE      handleArray[2];
    HANDLE      hDEV;    
    UCHAR       bigBuf[512];


    parse(argc, argv );

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

    if ( fShowSegment ) {
        setSegmentLight();
        return 0;
    }

    hDEV = open_dev();

    if(hDEV == INVALID_HANDLE_VALUE) {
        return 1;
    }

    if(fRead) {
        memset(&rOverlapped,0,sizeof(rOverlapped));
        rOverlapped.hEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
        if(!rOverlapped.hEvent) {
            printf("CreateEvent Error %d.\n",GetLastError());
            return 1;
        }
        handleArray[0] = rOverlapped.hEvent;
    }

    if(fWrite) {
        memset(&wOverlapped,0,sizeof(wOverlapped));
        wOverlapped.hEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
        if(!wOverlapped.hEvent) {
            printf("CreateEvent Error %d.\n",GetLastError());
            return 1;
        }
        handleArray[1] = wOverlapped.hEvent;
    }

    // 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 = hDEV;

            pinBuf = new CHAR[ReadLen];

        }

        if (fWrite) {

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

            hWrite = hDEV;
            poutBuf = new CHAR[WriteLen];
        }

        if(fRead) {
            //
            // Reset the Pipe.
            //
            USBFX2_PIPE_ENUM    pipeEnum = USBFx2BulkInPipe;
            ULONG               bytesReturned = 0;
            if(!DeviceIoControl(hRead,IOCTL_OSRUSBFX2_RESET_PIPE,
                            &pipeEnum,sizeof(USBFX2_PIPE_ENUM),
                            NULL,0,
                            &bytesReturned,NULL)) {
                DWORD error = GetLastError();
                printf("Failed To Reset  USBFx2BulkInPipe. %d\n",error);
                goto loopExit;
            }
        }
        if(fWrite) {
            //
            // Reset the Pipe.
            //
            USBFX2_PIPE_ENUM    pipeEnum = USBFx2BulkOutPipe;
            ULONG               bytesReturned = 0;
            if(!DeviceIoControl(hWrite,IOCTL_OSRUSBFX2_RESET_PIPE,
                            &pipeEnum,sizeof(USBFX2_PIPE_ENUM),
                            NULL,0,
                            &bytesReturned,NULL)) {
                DWORD error = GetLastError();
                printf("Failed To Reset  USBFx2BulkOutPipe. %d\n",error);
                goto loopExit;
            }
        }

        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
                //

                if(WriteFile(hWrite,
                    poutBuf,
                    WriteLen,
                    &nBytesWrite,
                    &wOverlapped)) {
                        successes++;
                    } else {
                        DWORD status = GetLastError();
                        if(status != ERROR_IO_PENDING) {
                            errors++;
                            printf("WriteFile Error %d.\n",status);
                            goto loopExit;
                        } else {
                            successes++;
                        }
                    }

            }

            if (fRead && pinBuf) {

                success = ReadFile(hRead,
                    pinBuf,
                    ReadLen,
                    &nBytesRead,
                    &rOverlapped);

                if(success) {
                    successes++;
                } else {
                    DWORD status = GetLastError();
                    if(status != ERROR_IO_PENDING) {
                        errors++;
                        printf("ReadFile Error %d.\n",status);
                        goto loopExit;
                    } else {
                        successes++;
                    }
                }

                GetOverlappedResult(hRead,&rOverlapped,&nBytesRead,TRUE);
                GetOverlappedResult(hWrite,&wOverlapped,&nBytesWrite,TRUE);

                ResetEvent(handleArray[0]);
                ResetEvent(handleArray[1]);

                assert(nBytesWrite == WriteLen);

                sprintf((char *)bigBuf,"W,R (%04.4d) : request %06.6d bytes -- %06.6d bytes written %06.6d bytes read",
                    i, ReadLen, nBytesWrite, nBytesRead );
                
                SetConsoleTitle((char *)bigBuf);

                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);
                }
            }

        }

loopExit:
        printf("successes = %d, errors = %d\n",successes++,errors++);

        if (pinBuf) {
            delete []pinBuf;
            pinBuf = NULL;
        }

        if (poutBuf) {
            delete []poutBuf;
            pinBuf = NULL;
        }

    }           

    if(hDEV !=  INVALID_HANDLE_VALUE)
        CloseHandle(hDEV);

    return 0;
}


⌨️ 快捷键说明

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