📄 usbcomm.cpp
字号:
bool TUsbDevice::disableScoChannel(void)
{
LONG result;
result = WinRTControlTransfer(deviceHandle,
REQUEST_TYPE_INTERFACE | REQUEST_TYPE_DEVICE,
11, // Set Interface request code
0, // Alternate setting
1, // Interface number
true, // HostToDevice transfer
0,
NULL,
NULL);
if (result == ERROR_SUCCESS)
return true;
else
{
savedError = result; // Save error code for debugging
return false;
}
}
//---------------------------------------------------------------------------
bool TUsbDevice::getLanguangeInfo(void)
{
LONG result;
USHORT numLangIDs;
result = WinRTGetNumberOfLanguageIds(deviceHandle, &numLangIDs);
result = WinRTGetLanguageIds(deviceHandle, numLangIDs, langID);
return result;
}
//---------------------------------------------------------------------------
bool TUsbDevice::readStrings(void)
{
LONG result;
int i;
char descString[64];
ULONG strLength;
for (i = 1; i <= 2; i++ )
{
result = WinRTGetStringDescriptorLength(deviceHandle,
langID[0], i, &strLength);
result = WinRTGetStringDescriptor(deviceHandle,
langID[0], i, strLength, descString);
}
return result;
}
//---------------------------------------------------------------------------
// Sends an HCI command to the device, returns true if successful
//
// Building this command involves creating a class specific request to
// send to the device on endpoint 0.
bool TUsbDevice::sendHCICommand(LPVOID buffer, DWORD buffSize)
{
LONG result;
ULONG nBytes; // For holding the returned byte count
result = WinRTControlTransfer(deviceHandle,
REQUEST_TYPE_CLASS | REQUEST_TYPE_DEVICE,
0, // Bluetooth request code
0, // value, not used
0, // index, not used
true, // HostToDevice transfer
buffSize,
reinterpret_cast<PUCHAR>(buffer),
&nBytes);
if (result == ERROR_SUCCESS)
return true;
else
{
savedError = result; // Save error code for debugging
return false;
}
}
//---------------------------------------------------------------------------
// Get HCI Event from the Interrupt Endpoint
//
// Polls device for an event. Buffer should be at least MAX_USB_EVENT_SIZE
// bytes long.
// Returns true if an HCI event was retrieved.
bool TUsbDevice::getHCIEvent(LPVOID buffer, DWORD& eventSize)
{
LONG result;
result = WinRTInterruptTransfer(deviceHandle,
INTERRUPT_ENDPOINT0,
MAX_USB_EVENT_SIZE,
reinterpret_cast<PUCHAR>(buffer),
&eventSize); // Bytes received
// Our device will return zero length packets if there is no
// event data. So no data is treated like no event.
// The device will normally return NAKs but this hangs the driver.
if (eventSize == 0)
{
return false;
}
// Check return code from the driver call
if (result == ERROR_SUCCESS)
return true;
else
{
savedError = result; // Save error code for debugging
return false;
}
}
//---------------------------------------------------------------------------
// SEND ACL Packet to Device
//
// Sends an ACL packet to the device. Returns true if successful.
// buffer can be up to 64K bytes in length.
//
// A zero length data packet is sent after data buffers that are exact
// multiples of 64 bytes. This is used to terminate the transfer on the
// device.
bool TUsbDevice::sendACLpacket(LPVOID buffer, DWORD buffSize)
{
LONG result;
ULONG bytesSent;
result = WinRTBulkTransfer(deviceHandle,
BULK_ENDPOINT1,
buffSize,
reinterpret_cast<PUCHAR>(buffer),
&bytesSent);
// Send a zero length data packet if necessary to terminate the transfer
if ((buffSize % 64) == 0)
{
result = WinRTBulkTransfer(deviceHandle,
BULK_ENDPOINT1, 0,
reinterpret_cast<PUCHAR>(buffer),
&bytesSent);
}
// Check return code from the driver call
if (result == ERROR_SUCCESS)
return true;
else
{
savedError = result; // Save error code for debugging
return false;
}
}
//---------------------------------------------------------------------------
// GET ACL Packet from Device
//
// Gets an ACL packet from the device. bufferLen is the size of
// of the supplied buffer. packetSize is filled with the total packet
// size retrieved. true is returned if there was data to get.
bool TUsbDevice::getACLpacket(LPVOID buffer, DWORD bufferLen, DWORD& packetSize)
{
bool result;
result = WinRTBulkTransfer(deviceHandle,
BULK_ENDPOINT0,
bufferLen,
reinterpret_cast<PUCHAR>(buffer),
&packetSize);
// Our device will return zero length packets if there is no
// event data. So no data is treated like no event.
// The device will normally return NAKs but this hangs the driver.
if (packetSize == 0)
{
return false;
}
// Check return code from the driver call
if (result == ERROR_SUCCESS)
return true;
else
{
savedError = result; // Save error code for debugging
return false;
}
}
//---------------------------------------------------------------------------
bool TUsbDevice::getAsyncHCIEvent(LPVOID buffer, DWORD& eventSize)
{
LONG result;
// initialize the overlapped struct
// These unused fields should be set to zero according to Advanced Windows.
intEpOverlapStruct.Offset = 0;
intEpOverlapStruct.OffsetHigh = 0;
// Perform an interrupt transfer.
// The function will return immediately whether data was received or not.
// If the transfer has not yet completed, the result will be
// ERROR_IO_PENDING.
// In this case, the application should wait for the event handle in
// overlapped.hEvent to become signalled. When this event is signalled,
// the transfer has completed.
result = WinRTAsynchInterruptTransfer(deviceHandle,
INTERRUPT_ENDPOINT0,
MAX_USB_EVENT_SIZE,
reinterpret_cast<PUCHAR>(buffer),
&eventSize,
&intEpOverlapStruct);
if (result == ERROR_SUCCESS)
{
// The operation completed immediately
return true;
}
else
{
// Must wait for operation to complete
savedError = result;
return false;
}
}
//---------------------------------------------------------------------------
bool TUsbDevice::pollAsyncHCIEvent(DWORD& eventSize)
{
LONG result;
result = WinRTCheckAsynchCompletion(deviceHandle,
&eventSize,
&intEpOverlapStruct);
if (result == ERROR_SUCCESS)
{
// The operation completed
return true;
}
else
{
// Must wait for operation to complete
savedError = result;
return false;
}
}
//---------------------------------------------------------------------------
// Get Asynch ACL Packet
//
// Check a bulk endpoint to get an ACL packet
bool TUsbDevice::getAsyncACLpacket(LPVOID buffer, DWORD bufferLen,
DWORD& packetSize)
{
LONG result;
// Initialize the overlapped struct
// These unused fields should be set to zero according to Advanced Windows.
bulkEpOverlapStruct.Offset = 0;
bulkEpOverlapStruct.OffsetHigh = 0;
// Attempt to perform a bulk transfer
// The function will return immediately whether data was received or not.
// If the transfer has not yet completed, the result will be
// ERROR_IO_PENDING.
// In this case, the application shoud wait for the event handle in
// overlapped.hEvent to become signalled. When this event is signalled,
// the transfer has completed.
result = WinRTAsynchBulkTransfer(deviceHandle,
BULK_ENDPOINT0,
bufferLen,
reinterpret_cast<PUCHAR>(buffer),
&packetSize,
&bulkEpOverlapStruct);
if (result == ERROR_SUCCESS)
{
// The operation completed immediately
return true;
}
else
{
// Must wait for operation to complete
savedError = result;
return false;
}
}
//---------------------------------------------------------------------------
bool TUsbDevice::pollAsyncACLpacket(DWORD& eventSize)
{
LONG result;
result = WinRTCheckAsynchCompletion(deviceHandle,
&eventSize,
&bulkEpOverlapStruct);
if (result == ERROR_SUCCESS)
{
// The operation completed
return true;
}
else
{
// Must wait for operation to complete
savedError = result;
#if 0
if (result == WINRTUSB_STATUS_DEV_NOT_RESPONDING)
{
Try restarting endpoint
clearStall(BULK_ENDPOINT0);
}
#endif
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -