📄 cudaldongle.cpp
字号:
} // CreateDongle
/** \brief Creates a pipe object to be used for BULK/INTERRUPT IN/OUT data transfers
*
* The pipe is connected to the specified endpoint, with the specified direction.
*
* \note Delete the \ref CudalPipe object after use (always prior to destroying the \ref CudalDongle
* object).
*
* \param[out] endpointIndex
* The endpoint index ("endpoint address" & 0x0F)
* \param[in] isDirectionIn
* Data direction is in (("endpoint address" & 0x80) == 0x80)?
* Note: IN = From the device to the host, OUT = From the host to the device.
*
* \return
* A pointer to the created \ref CudalPipe object, or \c NULL if the operation failed
* (e.g.the specified endpoint does not exist)
*
* \sa CudalDongle::EnumDongles, CudalDongle::~CudalDongle, CudalDongle::CreatePipe
*/
CudalPipe* CudalDongle::CreatePipe(BYTE endpointIndex, BOOL isDirectionIn) {
CUsbIoPipe *pUsbIoPipe = new CUsbIoPipe();
CUDAL_DONGLE_INFO currentInfo;
HDEVINFO hDevInfo;
int result;
// Enter the critical section
result = WaitForSingleObject(m_hMutexEnum, 500);
if (result == WAIT_OBJECT_0 || result == WAIT_ABANDONED) {
// Create a list of attached dongles
if (hDevInfo = CUsbIo::CreateDeviceList(m_pGuid)) {
// Traverse through the list and try to find a match
int n = 0;
// If the device could be opened...
while (pUsbIoPipe->Open(n, hDevInfo, m_pGuid) == USBIO_ERR_SUCCESS) {
// Compare the dongle information
memset(¤tInfo, 0x00, sizeof(CUDAL_DONGLE_INFO));
if (CreateDongleInfo(¤tInfo, pUsbIoPipe)) {
// Attempt to bind the pipe when we have a match, and break out of the loop
if (memcmp(¤tInfo, &m_dongleInfo, sizeof(CUDAL_DONGLE_INFO)) == 0) {
BYTE endpointAddress = endpointIndex + (isDirectionIn ? 0x80 : 0x00);
if (pUsbIoPipe->Bind(n, endpointAddress, hDevInfo, m_pGuid) == USBIO_ERR_SUCCESS) {
break;
}
}
}
pUsbIoPipe->Close();
n++;
}
// Delete the dongle list
CUsbIo::DestroyDeviceList(hDevInfo);
}
// Release the enumeration mutex
ReleaseMutex(m_hMutexEnum);
}
// If we got it...
CudalPipe *pCudalPipe = NULL;
if (pUsbIoPipe && pUsbIoPipe->IsOpen()) {
return new CudalPipe(pUsbIoPipe, endpointIndex, isDirectionIn);
} else {
delete pUsbIoPipe;
return NULL;
}
} // CreatePipe
/** \brief Sends a vendor request with an IN data phase (from the device to the host)
*
* \param[in] request
* Specifies the value of the bRequest field of the SETUP packet
* \param[in] index
* Specifies the value of the wIndex field of the SETUP packet
* \param[in] value
* Specifies the value of the wValue field of the SETUP packet
* \param[in] maxLength
* Size of the caller-provided buffer, \c *pData. Use 0-65535
* \param[out] *pActualLength
* The number bytes actually transferred to \c *pData during the IN data phase. This parameter can
* be \c NULL if the transferred length is of no interrest.
* \param[out] *pData
* Pointer to a caller-provided buffer. The buffer receives the data transferred in the IN data
* phase. This parameter can be \c NULL if \c maxLength is 0.
* \param[in] recepient
* Specifies the recipient of the request (device, interface, endpoint or other)
*
* \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::VendorRequestOut
*/
USBIOERR CudalDongle::VendorRequestIn(BYTE request, WORD index, WORD value, DWORD maxLength, DWORD *pActualLength, BYTE *pData, USBIO_REQUEST_RECIPIENT recepient) {
// Provide a dummy buffer when pData = NULL
BYTE dummy;
if (!pData) pData = &dummy;
// Prepare the descriptor for the request
USBIO_CLASS_OR_VENDOR_REQUEST vendorRequest;
vendorRequest.Flags = USBIO_SHORT_TRANSFER_OK;
vendorRequest.Type = RequestTypeVendor;
vendorRequest.Recipient = recepient;
vendorRequest.RequestTypeReservedBits = 0;
vendorRequest.Request = request;
vendorRequest.Index = index;
vendorRequest.Value = value;
// Send the request
USBIOERR result = m_pUsbIo->ClassOrVendorInRequest(pData, maxLength, &vendorRequest);
if (pActualLength) *pActualLength = maxLength;
return result;
} // VendorRequestIn
/** \brief Sends a vendor request with an OUT data phase (from the host to the device)
*
* \param[in] request
* Specifies the value of the bRequest field of the SETUP packet
* \param[in] index
* Specifies the value of the wIndex field of the SETUP packet
* \param[in] value
* Specifies the value of the wValue field of the SETUP packet
* \param[in] length
* The number of bytes to be transferred from \c *pData during the OUT data phase. Use 0-65535
* \param[out] *pActualLength
* The number bytes actually transferred during the OUT data phase. This parameter can be \c NULL
* if the transferred length is of no interrest.
* \param[in] *pData
* Pointer to a caller-provided buffer that contains the data to be transferred in the OUT data
* phase. This parameter can be \c NULL if \c length is 0.
* \param[in] recepient
* Specifies the recipient of the request (device, interface, endpoint or other)
*
* \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::VendorRequestIn
*/USBIOERR CudalDongle::VendorRequestOut(BYTE request, WORD index, WORD value, DWORD length, DWORD *pActualLength, BYTE *pData, USBIO_REQUEST_RECIPIENT recepient) {
// Provide a dummy buffer when pData = NULL
BYTE dummy;
if (!pData) pData = &dummy;
// Prepare the descriptor for the request
USBIO_CLASS_OR_VENDOR_REQUEST vendorRequest;
vendorRequest.Flags = USBIO_SHORT_TRANSFER_OK;
vendorRequest.Type = RequestTypeVendor;
vendorRequest.Recipient = recepient;
vendorRequest.RequestTypeReservedBits = 0;
vendorRequest.Request = request;
vendorRequest.Index = index;
vendorRequest.Value = value;
// Send the request
USBIOERR result = m_pUsbIo->ClassOrVendorOutRequest(pData, length, &vendorRequest);
if (pActualLength) *pActualLength = length;
return result;
} // VendorRequestOut
/** \brief Sends a class request with an IN data phase (from the device to the host)
*
* \param[in] request
* Specifies the value of the bRequest field of the SETUP packet
* \param[in] index
* Specifies the value of the wIndex field of the SETUP packet
* \param[in] value
* Specifies the value of the wValue field of the SETUP packet
* \param[in] maxLength
* Size of the caller-provided buffer, \c *pData. Use 0-65535
* \param[out] *pActualLength
* The number bytes actually transferred to \c *pData during the IN data phase. This parameter can
* be \c NULL if the transferred length is of no interrest.
* \param[out] *pData
* Pointer to a caller-provided buffer. The buffer receives the data transferred in the IN data
* phase. This parameter can be \c NULL if \c maxLength is 0.
* \param[in] recepient
* Specifies the recipient of the request (device, interface, endpoint or other)
*
* \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::ClassRequestOut
*/
USBIOERR CudalDongle::ClassRequestIn(BYTE request, WORD index, WORD value, DWORD maxLength, DWORD *pActualLength, BYTE *pData, USBIO_REQUEST_RECIPIENT recepient) {
// Provide a dummy buffer when pData = NULL
BYTE dummy;
if (!pData) pData = &dummy;
// Prepare the descriptor for the request
USBIO_CLASS_OR_VENDOR_REQUEST classRequest;
classRequest.Flags = USBIO_SHORT_TRANSFER_OK;
classRequest.Type = RequestTypeClass;
classRequest.Recipient = recepient;
classRequest.RequestTypeReservedBits = 0;
classRequest.Request = request;
classRequest.Index = index;
classRequest.Value = value;
// Send the request
USBIOERR result = m_pUsbIo->ClassOrVendorInRequest(pData, maxLength, &classRequest);
if (pActualLength) *pActualLength = maxLength;
return result;
} // ClassRequestIn
/** \brief Sends a class request with an OUT data phase (from the host to the device)
*
* \param[in] request
* Specifies the value of the bRequest field of the SETUP packet
* \param[in] index
* Specifies the value of the wIndex field of the SETUP packet
* \param[in] value
* Specifies the value of the wValue field of the SETUP packet
* \param[in] length
* The number of bytes to be transferred from \c *pData during the OUT data phase. Use 0-65535
* \param[out] *pActualLength
* The number bytes actually transferred during the OUT data phase. This parameter can be \c NULL
* if the transferred length is of no interrest.
* \param[in] *pData
* Pointer to a caller-provided buffer that contains the data to be transferred in the OUT data
* phase. This parameter can be \c NULL if \c length is 0.
* \param[in] recepient
* Specifies the recipient of the request (device, interface, endpoint or other)
*
* \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::ClassRequestIn
*/
USBIOERR CudalDongle::ClassRequestOut(BYTE request, WORD index, WORD value, DWORD length, DWORD *pActualLength, BYTE *pData, USBIO_REQUEST_RECIPIENT recepient) {
// Provide a dummy buffer when pData = NULL
BYTE dummy;
if (!pData) pData = &dummy;
// Prepare the descriptor for the request
USBIO_CLASS_OR_VENDOR_REQUEST classRequest;
classRequest.Flags = USBIO_SHORT_TRANSFER_OK;
classRequest.Type = RequestTypeClass;
classRequest.Recipient = recepient;
classRequest.RequestTypeReservedBits = 0;
classRequest.Request = request;
classRequest.Index = index;
classRequest.Value = value;
// Send the request
USBIOERR result = m_pUsbIo->ClassOrVendorOutRequest(pData, length, &classRequest);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -