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

📄 rwiso.c

📁 usb范例代码
💻 C
📖 第 1 页 / 共 3 页
字号:
{
    if (ghStreamDev)
    {
        IsoStream(ghStreamDev, TRUE);

        ghStreamDev = NULL;
    }
}

//
// End, routines for Iso Streaming



void SelectAlternateInterface()
{
    HANDLE  hDEV;
    ULONG   success;
    int     siz;
    int     nBytes;
    UCHAR   buf[2];

    hDEV = open_dev();

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

        buf[0] = AltInterface;

        buf[1] = AltSetting;

        nBytes = 0;

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

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

        CloseHandle(hDEV);
    }
}



void
Usage()
/*++
Routine Description:

    Called by main() to dump usage info to the console when
    the app is called with no parms or with an invalid parm

Arguments:

    None

Return Value:

    None

--*/
{
    printf("Usage for Read/Write test:\n");
    printf("-r [n] where n is number of bytes to read\n");
    printf("-w [n] where n is number of bytes to write\n");
    printf("-c [n] where n is number of iterations (default = 1)\n");
    printf("-i [s] where s is the input pipe\n");
    printf("-o [s] where s is the output pipe\n");
    printf("-v verbose -- dumps read data\n");

    printf("\nUsage for USB and Endpoint info:\n");
    printf("-u to dump USB configuration and pipe info \n");
    printf("-g [s] Run Isochronous test stream for 's' seconds \n");

}


BOOL
Parse (
    int argc,
    char *argv[]
)
/*++
Routine Description:

    Called by main() to parse command line parms

Arguments:

    argc and argv that was passed to main()

Return Value:

    Sets global flags as per user function request

--*/
{
    int i;

    if (argc < 2 )
    {
        // give usage if invoked with no parms
        Usage();
        return FALSE;
    }

    for (i = 1; i < argc; i++)
    {
        if (argv[i][0] == '-' ||
            argv[i][0] == '/')
        {
            switch (argv[i][1])
            {
                case 'r':
                case 'R':
                    if (++i == argc)
                    {
                        Usage();
                        return FALSE;
                    }
                    else
                    {
                        ReadLen = atoi(argv[i]);
                        fRead = TRUE;
                    }
                    break;

                case 'w':
                case 'W':
                    if (++i == argc)
                    {
                        Usage();
                        return FALSE;
                    }
                    else
                    {
                        WriteLen = atoi(argv[i]);
                        fWrite = TRUE;
                    }
                    break;

                case 'c':
                case 'C':
                    if (++i == argc)
                    {
                        Usage();
                        return FALSE;
                    }
                    else
                    {
                        IterationCount = atoi(argv[i]);
                    }
                    break;

                case 'i':
                case 'I':
                    if (++i == argc)
                    {
                        Usage();
                        return FALSE;
                    }
                    else
                    {
                         strncpy(inPipe,
                                 argv[i],
                                 sizeof(inPipe)-1);

                        inPipe[sizeof(inPipe)-1] = '\0';
                    }
                    break;

                case 'u':
                case 'U':
                    fDumpUsbConfig = TRUE;
                    break;

                case 'v':
                case 'V':
                    fDumpReadData = TRUE;
                    break;

                case 'o':
                case 'O':
                    if (++i == argc)
                    {
                        Usage();
                        return FALSE;
                    }
                    else
                    {
                         strncpy(outPipe,
                                 argv[i],
                                 sizeof(outPipe)-1);

                         outPipe[sizeof(outPipe)-1] = '\0';
                    }
                    break;

                case 'g':
                case 'G':
                    if (++i == argc)
                    {
                        Usage();
                        return FALSE;
                    }
                    else
                    {
                        gMS = 1000 * atoi(argv[i]);
                        StartIsoStream();
                    }
                    break;

                case 'x':
                case 'X':
                    StopIsoStream();
                    break;

                case 'a':
                case 'A':
                    if (i+2 >= argc)
                    {
                        Usage();
                        return FALSE;
                    }
                    else
                    {
                        SelectAltInterface = TRUE;
                        AltInterface = (UCHAR)atoi(argv[i+1]);
                        AltSetting   = (UCHAR)atoi(argv[i+2]);
                        i += 2;
                    }
                    break;

                default:
                    Usage();
                    return FALSE;
            }
        }
        else
        {
            Usage();
            return FALSE;
        }
    }

    return TRUE;
}


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

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

Arguments:

    argc, argv  standard console  'c' app arguments

Return Value:

    Zero

--*/

{
    char *pinBuf = NULL, *poutBuf = NULL;
    ULONG nBytesRead = 0, nBytesWrite = 0, nBytes = 0;
    ULONG i, j;
    int ok;
    ULONG 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;

    if (!Parse(argc, argv))
    {
        return 0;
    }

    if (SelectAltInterface)
    {
        printf("Interface %d\nSetting %d\n",
               AltInterface, AltSetting);

        SelectAlternateInterface();
    }

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

    }

    // Stop iso stream if we started one
    //
    StopIsoStream();

    return 0;
}

⌨️ 快捷键说明

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