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

📄 cudaldongle.cpp

📁 reference about wireless design which is helpful to everyone
💻 CPP
📖 第 1 页 / 共 3 页
字号:
    if (pActualLength) *pActualLength = length;
    return result;
    
} // ClassRequestOut




/** \brief	Gets the device descriptor from the device
 *
 * \param[out]      *pDesc
 *      Pointer to a caller-provided \c USB_DEVICE_DESCRIPTOR structure
 *
 * \return
 *		A \ref USBIOERR code (see "usbio_i.h" for more details). If the operation succeeds that would be 
 *      \c USBIO_ERR_SUCCESS = 0.
 * 
 * \sa CudalDongle::GetConfigurationDescriptor, CudalDongle::GetStringDescriptor
 */
USBIOERR CudalDongle::GetDeviceDescriptor(USB_DEVICE_DESCRIPTOR *pDesc) {
    return m_pUsbIo->GetDeviceDescriptor(pDesc);
} // GetDeviceDescriptor




/** \brief	Gets a configuration descriptor from the device
 *
 * If the total size of the configuration descriptor is not known it can be retrieved in a two step
 * process. With a first call to this function the fixed part of the descriptor which is defined by
 * \c USB_CONFIGURATION_DESCRIPTOR is retrieved. The total size of the descriptor is indicated by the
 * \c wTotalLength field of the structure. In a second step a buffer of the required size can be
 * allocated and the complete descriptor can be retrieved with another call to this function. The 
 * complete descriptor must be parsed manually.
 *
 * \param[out]      *pDesc
 *      Pointer to a caller-provided buffer that receives the requested descriptor
 * \param[in]       maxLength
 *      The size of the \c *pDesc buffer
 * \param[out]      *pActualLength
 *      The number of valid bytes in the \c *pDesc buffer after the transfer. This parameter can be
 *       \c NULL if the value is of no interrest.
 * \param[in]       index
 *      Zero-based index of the configuration descriptor to get. Note that this number does not 
 *      necessarily correspond to the configuration value number found in the descriptor.
 *
 * \return
 *		A \ref USBIOERR code (see "usbio_i.h" for more details). If the operation succeeds that would be 
 *      \c USBIO_ERR_SUCCESS = 0.
 * 
 * \sa CudalDongle::GetDeviceDescriptor, CudalDongle::GetStringDescriptor
 */
USBIOERR CudalDongle::GetConfigurationDescriptor(USB_CONFIGURATION_DESCRIPTOR *pDesc, DWORD maxLength, DWORD *pActualLength, BYTE index) {
    USBIOERR result;
    result = m_pUsbIo->GetConfigurationDescriptor(pDesc, maxLength, index);
    if (pActualLength) *pActualLength = maxLength;
    return result;
} // GetConfigurationDescriptor




/** \brief	Gets a string descriptor from the device
 *
 * The string is converted to the ANSI code page, and null-terminated
 *
 * \param[out]      *pAnsiString
 *      Pointer to a caller-provided buffer that receives the string
 * \param[in]       maxLength
 *      The size of the provided buffer
 * \param[out]      *pActualLength
 *      The number of valid bytes in the \c *pDesc buffer after the transfer, including the appended
 *      NULL terminator. This parameter can be \c NULL if the value is of no interrest.
 * \param[in]       index
 *      Zero-based index of the string descriptor to get
 *
 * \return
 *		A \ref USBIOERR code (see "usbio_i.h" for more details). If the operation succeeds that would be 
 *      \c USBIO_ERR_SUCCESS = 0.
 * 
 * \sa CudalDongle::GetDeviceDescriptor, CudalDongle::GetConfigurationDescriptor
 */
USBIOERR CudalDongle::GetStringDescriptor(char *pAnsiString, DWORD maxLength, DWORD *pActualLength, BYTE index) {
    BYTE pBuffer[256];
    char pTempString[129];
    USBIOERR result;

    // Provide a dummy buffer when pActualLength = NULL
    DWORD dummy;
    if (!pActualLength) pActualLength = &dummy;

    // Get the string descriptor from the device
    USB_STRING_DESCRIPTOR *pStringDesc = (USB_STRING_DESCRIPTOR*) pBuffer;
    DWORD length;
    result = m_pUsbIo->GetStringDescriptor(pStringDesc, length = 256, index);
    if (result == USBIO_ERR_SUCCESS) {
        
        // Convert from unicode to ANSI-format and terminate it
        *pActualLength = WideCharToMultiByte(CP_ACP, 0, pStringDesc->bString, (pStringDesc->bLength - 2) / 2, pTempString, maxLength - 1, NULL, NULL);
        if (*pActualLength < maxLength) {
	        pAnsiString[*pActualLength] = '\0';
	        *pActualLength += 1;
        } else {
	        pAnsiString[maxLength - 1] = '\0';
            *pActualLength = maxLength;
        }
    } else {    
        pAnsiString[0] = '\0';
        *pActualLength = 0;
    }
    return result;
    
} // GetStringDescriptor




/** \brief	Sends a get status request to the device
 *
 * Refer to the USB specification, chapter 9 for more information on this function's parameters and the 
 * returned status value.
 *
 * \param[out]      *pStatusValue
 *      Pointer to a caller-provided varible that receives the status value
 * \param[in]       recipient
 *      Specifies the recipient of the request (device, interface, endpoint or other)
 * \param[in]       index
 *      Specifies the index value for the get status request. The values are defined by the device.
 *
 * \return
 *		A \ref USBIOERR code (see "usbio_i.h" for more details). If the operation succeeds that would be 
 *      \c USBIO_ERR_SUCCESS = 0.
 */
USBIOERR CudalDongle::GetStatus(WORD *pStatusValue, USBIO_REQUEST_RECIPIENT recipient, BYTE index) {
    WORD statusValue;
    USBIOERR result;

    result = m_pUsbIo->GetStatus(statusValue, recipient, index);
    *pStatusValue = statusValue;
    return result;
} // GetStatus




/** \brief	Sets the device to the configured state
 *
 * The device has to be configured before any data transfer from or to its endpoints can take place. 
 * Only those endpoints that are included in the configuration will be activated and can be subsequently 
 * used for data transfers.
 *
 * \note Unless something else is specified, \ref CudalDongle::CreateDongle() will by default set 
 * configuration index 0. with alternate setting 0 for all interfaces.
 *
 * \param[in]       configurationIndex
 *      A zero-based index for the device configuration to be set. Note that this number does not 
 *      necessarily correspond to the configuration value number found in the descriptor. It is, however,
 *      the same index that is passed to \ref GetConfigurationDescriptor().
 * \param[in]       *pAlternateSettingIndexes
 *      An array of alternate setting indexes, one for each interface in the selected configuration.
 *      Alternatively, use the default value \c NULL, which is equivalent to an array of zeroes.
 *
 * \return
 *		A \ref USBIOERR code (see "usbio_i.h" for more details). If the operation succeeds that would be 
 *      \c USBIO_ERR_SUCCESS = 0.
 * 
 * \sa CudalDongle::Unconfigure, CudalDongle::GetConfigurationDescriptor
 */
USBIOERR CudalDongle::SetConfiguration(BYTE configurationIndex, BYTE *pAlternateSettingIndexes) {

    // Get the configuration descriptor, first to get the total length...
    USB_CONFIGURATION_DESCRIPTOR configDesc;
    USBIOERR result = GetConfigurationDescriptor(&configDesc, sizeof(USB_CONFIGURATION_DESCRIPTOR), NULL, configurationIndex);

    if (result == USBIO_ERR_SUCCESS) {

        // Create the configuration structure
        USBIO_SET_CONFIGURATION config;
        config.ConfigurationIndex = configurationIndex;
	    config.NbOfInterfaces = configDesc.bNumInterfaces;
        for (int n = 0; n < min(config.NbOfInterfaces, USBIO_MAX_INTERFACES); n++) {
            config.InterfaceList[n].InterfaceIndex = n;
            config.InterfaceList[n].AlternateSettingIndex = pAlternateSettingIndexes ? pAlternateSettingIndexes[n] : 0;
	        config.InterfaceList[n].MaximumTransferSize = 4096;
        }

        // Set the new configuration
	    result = m_pUsbIo->SetConfiguration(&config);
    }

    return result;

} // SetConfiguration




/** \brief	Gets the current configuration value
 *
 * The returned value equals the bConfigurationValue parameter from the currently selected configuration
 * descriptor. Note that this value is not necessarily the same as the index passed to 
 * \ref CudalDongle::SetConfiguration() and \ref CudalDongle::GetConfigurationDescriptor()
 * 
 * \param[out]      *pConfigurationValue
 *      Pointer to a caller-provided varible that receives the configration value
 *
 * \return
 *		A \ref USBIOERR code (see "usbio_i.h" for more details). If the operation succeeds that would be 
 *      \c USBIO_ERR_SUCCESS = 0.
 * 
 * \sa CudalDongle::SetConfiguration, CudalDongle::GetConfigurationDescriptor
 */
USBIOERR CudalDongle::GetConfiguration(BYTE *pConfigurationValue) {
    return m_pUsbIo->GetConfiguration(*pConfigurationValue);
} // GetConfiguration




/** \brief	Sets the device to the unconfigured state
 *
 * \return
 *		A \ref USBIOERR code (see "usbio_i.h" for more details). If the operation succeeds that would be 
 *      \c USBIO_ERR_SUCCESS = 0.
 * 
 * \sa CudalDongle::SetConfiguration
 */
USBIOERR CudalDongle::Unconfigure() {
    return m_pUsbIo->UnconfigureDevice();
} // Unconfigure




/** \brief	Changes the alternate setting of an interface 
 *
 * \param[in]       interfaceIndex
 *      The index of the interface to be changed
 * \param[in]       alternateSettingIndex
 *      The new alternate setting
 *
 * \return
 *		A \ref USBIOERR code (see "usbio_i.h" for more details). If the operation succeeds that would be 
 *      \c USBIO_ERR_SUCCESS = 0.
 * 
 * \sa CudalDongle::GetInterface, CudalDongle::SetConfiguration
 */
USBIOERR CudalDongle::SetInterface(BYTE interfaceIndex, BYTE alternateSettingIndex) {
    USBIO_INTERFACE_SETTING interfaceSetting;
    interfaceSetting.InterfaceIndex = interfaceIndex;
    interfaceSetting.AlternateSettingIndex = alternateSettingIndex;
    interfaceSetting.MaximumTransferSize = 4096;

    return m_pUsbIo->SetInterface(&interfaceSetting);
} // SetInterface




/** \brief	Gets the current alternate setting of an interface 
 *
 * \param[in]       interfaceIndex
 *      The index of the interface to be queried
 * \param[out]      *pAlternateSettingIndex
 *      Pointer to a caller-provided varible that receives the current alternate setting
 *
 * \return
 *		A \ref USBIOERR code (see "usbio_i.h" for more details). If the operation succeeds that would be 
 *      \c USBIO_ERR_SUCCESS = 0.
 * 
 * \sa CudalDongle::SetInterface
 */
USBIOERR CudalDongle::GetInterface(BYTE interfaceIndex, BYTE *pAlternateSettingIndex) {
    return m_pUsbIo->GetInterface(*pAlternateSettingIndex, interfaceIndex);
} // GetInterface

⌨️ 快捷键说明

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