📄 hciuart.cxx
字号:
(dwType == REG_DWORD) && (dwSize == sizeof(dw)) && dw)
g_fCeLog = IsCeLogStatus (CELOGSTATUS_ENABLED_GENERAL);
RegCloseKey (hk);
}
if (g_fCeLog) {
#if defined (SDK_BUILD)
CELOGSDK_START ();
#endif
BTH_CELOG_START_DATA sd;
sd.eTransport = UART;
GetLocalTime (&sd.st);
wsprintf (sd.szDriverString, L"UART Driver v. 0x%08x on %s", HCI_INTERFACE_VERSION_1_1, szComPortName);
CELOGDATAFLAGGED(TRUE, CELID_RAW_UCHAR, &sd, sizeof(sd), 0, CELZONE_ALWAYSON, CELOG_FLAG_START);
}
#endif
IFDBG(DebugOut (DEBUG_HCI_INIT, L"HCI_OpenConnection - Successful\n"));
return TRUE;
}
void HCI_CloseConnection (void) {
IFDBG(DebugOut (DEBUG_HCI_INIT, L"HCI_CloseConnection - Started\n"));
if (hFile == INVALID_HANDLE_VALUE) {
IFDBG(DebugOut (DEBUG_ERROR, L"HCI_CloseConnection - not active\n"));
return;
}
CloseHandle (hFile);
CloseHandle (hWriteEvent);
CloseHandle (hReadEvent);
hFile = INVALID_HANDLE_VALUE;
hWriteEvent = hReadEvent = NULL;
#if defined (BT_USE_CELOG)
if (g_fCeLog) {
BTH_CELOG_STOP_DATA sd;
GetLocalTime (&sd.st);
CELOGDATAFLAGGED(TRUE, CELID_RAW_UCHAR, &sd, sizeof(sd), 0, CELZONE_ALWAYSON, CELOG_FLAG_STOP);
#if defined (SDK_BUILD)
CELOGSDK_STOP ();
#endif
g_fCeLog = FALSE;
}
#endif
IFDBG(DebugOut (DEBUG_HCI_INIT, L"HCI_CloseConnection - Successful\n"));
return;
}
static BOOL WriteCommPort (unsigned char *pBuffer, unsigned int cSize) {
DWORD dwFilledSoFar = 0;
while ((int)dwFilledSoFar < cSize) {
DWORD dwWrit = 0;
#if ! defined (UNDER_CE)
OVERLAPPED o;
memset (&o, 0, sizeof(o));
o.hEvent = hWriteEvent;
while (! WriteFile (hFile, &(pBuffer[dwFilledSoFar]), cSize - dwFilledSoFar, &dwWrit, &o)) {
if ((GetLastError() == ERROR_IO_PENDING) &&
GetOverlappedResult (hFile, &o, &dwWrit, TRUE))
break;
if (hFile != INVALID_HANDLE_VALUE)
IFDBG(DebugOut (DEBUG_ERROR, L"Error writing COM port: GetLastError = 0x%08x (%d)\n", GetLastError (), GetLastError ()));
return FALSE;
}
#else
if ((! WriteFile (hFile, &(pBuffer[dwFilledSoFar]), cSize - dwFilledSoFar, &dwWrit, NULL)) &&
(dwWrit == 0)) {
#if defined (DEBUG) || defined (_DEBUG) || defined (RETAILLOG)
if (hFile != INVALID_HANDLE_VALUE)
IFDBG(DebugOut (DEBUG_ERROR, L"Error writing COM port: GetLastError = 0x%08x (%d)\n", GetLastError (), GetLastError ()));
#endif
return FALSE;
}
#endif
#if defined (BT_USE_CELOG)
if (g_fCeLog && dwWrit) {
CELOGDATAFLAGGED(TRUE, CELID_RAW_UCHAR, &pBuffer[dwFilledSoFar], (unsigned short)dwWrit, 0, CELZONE_ALWAYSON, CELOG_FLAG_RAW_OUT);
}
#endif
dwFilledSoFar += dwWrit;
}
return TRUE;
}
static BOOL ReadCommPort (unsigned char *pBuffer, DWORD dwLen) {
DWORD dwFilledSoFar = 0;
while (dwFilledSoFar < dwLen) {
OVERLAPPED o;
memset (&o, 0, sizeof(o));
o.hEvent = hReadEvent;
DWORD dwRead = 0;
#if ! defined (UNDER_CE)
while (! ReadFile (hFile, &pBuffer[dwFilledSoFar], dwLen - dwFilledSoFar, &dwRead, &o)) {
if ((GetLastError() == ERROR_IO_PENDING) &&
GetOverlappedResult (hFile, &o, &dwRead, TRUE))
break;
if (hFile != INVALID_HANDLE_VALUE)
IFDBG(DebugOut (DEBUG_ERROR, L"Error reading COM port: GetLastError = 0x%08x (%d)\n", GetLastError (), GetLastError ()));
return FALSE;
}
#else
if ((! ReadFile (hFile, &pBuffer[dwFilledSoFar], dwLen - dwFilledSoFar, &dwRead, NULL)) &&
(dwRead == 0)) {
#if defined (DEBUG) || defined (_DEBUG) || defined (RETAILLONG)
if (hFile != INVALID_HANDLE_VALUE)
IFDBG(DebugOut (DEBUG_ERROR, L"Error reading COM port: GetLastError = 0x%08x (%d)\n", GetLastError (), GetLastError ()));
#endif
return FALSE;
}
#endif
#if defined (BT_USE_CELOG)
if (g_fCeLog && dwRead) {
CELOGDATAFLAGGED(TRUE, CELID_RAW_UCHAR, pBuffer + dwFilledSoFar, (unsigned short)dwRead, 0, CELZONE_ALWAYSON, CELOG_FLAG_RAW_IN);
}
#endif
dwFilledSoFar += dwRead;
}
return TRUE;
}
int HCI_WritePacket (HCI_TYPE eType, BD_BUFFER *pBuff) {
IFDBG(DebugOut (DEBUG_HCI_TRANSPORT, L"HCI_WritePacket type 0x%02x len %d\n", eType, BufferTotal (pBuff)));
IFDBG(DumpBuff (DEBUG_HCI_DUMP, pBuff->pBuffer + pBuff->cStart, BufferTotal (pBuff)));
#if defined (DEBUG) || defined (_DEBUG)
ASSERT (pBuff->cStart == DEBUG_WRITE_BUFFER_HEADER);
ASSERT (pBuff->cSize - pBuff->cEnd >= DEBUG_WRITE_BUFFER_TRAILER);
#endif
ASSERT (! (pBuff->cStart & 0x3));
if (((int)BufferTotal (pBuff) > PACKET_SIZE_W) || (pBuff->cStart < 1)) {
IFDBG(DebugOut (DEBUG_ERROR, L"[UART] Packet too big (%d, should be <= %d), or no space for header!\n", BufferTotal (pBuff), PACKET_SIZE_W));
return FALSE;
}
if (hFile == INVALID_HANDLE_VALUE) {
DebugOut (DEBUG_ERROR, L"HCI_WritePacket - not active\n");
return FALSE;
}
pBuff->pBuffer[--pBuff->cStart] = (unsigned char)eType;
if (! WriteCommPort (pBuff->pBuffer + pBuff->cStart, BufferTotal (pBuff)))
return FALSE;
IFDBG(DebugOut (DEBUG_HCI_TRANSPORT, L"HCI_WritePacket : DONE type 0x%02x len %d\n", eType, BufferTotal (pBuff)));
return TRUE;
}
int HCI_ReadPacket (HCI_TYPE *peType, BD_BUFFER *pBuff) {
IFDBG(DebugOut (DEBUG_HCI_TRANSPORT, L"HCI_ReadPacket\n"));
if (hFile == INVALID_HANDLE_VALUE) {
IFDBG(DebugOut (DEBUG_ERROR, L"HCI_ReadPacket - not active\n"));
return FALSE;
}
pBuff->cStart = 3;
pBuff->cEnd = pBuff->cSize;
if (BufferTotal (pBuff) < 257) {
IFDBG(DebugOut (DEBUG_ERROR, L"HCI_ReadPacket - failed buffer too small (%d bytes)\n", BufferTotal (pBuff)));
return FALSE;
}
if (! ReadCommPort (pBuff->pBuffer + pBuff->cStart, 1))
return FALSE;
++pBuff->cStart;
int dwPacketSize = 0;
switch (pBuff->pBuffer[pBuff->cStart - 1]) {
case 0x01: // Command packet
IFDBG(DebugOut (DEBUG_ERROR, L"HCI_ReadPacket - unexpected packet type (command)\n"));
break;
case 0x02: // ACL packet
if (! ReadCommPort (pBuff->pBuffer + pBuff->cStart, 4))
return FALSE;
pBuff->cEnd = pBuff->cStart + 4 + (pBuff->pBuffer[pBuff->cStart + 2] | (pBuff->pBuffer[pBuff->cStart + 3] << 8));
if (pBuff->cEnd > pBuff->cSize) {
IFDBG(DebugOut (DEBUG_ERROR, L"HCI_ReadPacket - failed: buffer too small\n"));
return FALSE;
}
if (! ReadCommPort (pBuff->pBuffer + pBuff->cStart + 4, BufferTotal (pBuff) - 4))
return FALSE;
*peType = DATA_PACKET_ACL;
IFDBG(DebugOut (DEBUG_HCI_DUMP, L"HCI_ReadPacket: ACL\n"));
IFDBG(DumpBuff (DEBUG_HCI_DUMP, pBuff->pBuffer + pBuff->cStart, BufferTotal (pBuff)));
return TRUE;
case 0x03: // SCO packet
if (! ReadCommPort (pBuff->pBuffer + pBuff->cStart, 3))
return FALSE;
pBuff->cEnd = pBuff->cStart + 3 + pBuff->pBuffer[pBuff->cStart + 2];
if (pBuff->cEnd > pBuff->cSize) {
IFDBG(DebugOut (DEBUG_ERROR, L"HCI_ReadPacket - failed: buffer too small\n"));
return FALSE;
}
if (! ReadCommPort (pBuff->pBuffer + pBuff->cStart + 3, BufferTotal (pBuff) - 3))
return FALSE;
*peType = DATA_PACKET_SCO;
IFDBG(DebugOut (DEBUG_HCI_DUMP, L"HCI_ReadPacket: SCO\n"));
IFDBG(DumpBuff (DEBUG_HCI_DUMP, pBuff->pBuffer + pBuff->cStart, BufferTotal (pBuff)));
return TRUE;
case 0x04: // HCI Event
if (! ReadCommPort (pBuff->pBuffer + pBuff->cStart, 2))
return FALSE;
pBuff->cEnd = pBuff->cStart + 2 + pBuff->pBuffer[pBuff->cStart + 1];
if (pBuff->cEnd > pBuff->cSize) {
IFDBG(DebugOut (DEBUG_ERROR, L"HCI_ReadPacket - failed: buffer too small\n"));
return FALSE;
}
if (! ReadCommPort (pBuff->pBuffer + pBuff->cStart + 2, BufferTotal (pBuff) - 2))
return FALSE;
*peType = EVENT_PACKET;
IFDBG(DebugOut (DEBUG_HCI_DUMP, L"HCI_ReadPacket: Event\n"));
IFDBG(DumpBuff (DEBUG_HCI_DUMP, pBuff->pBuffer + pBuff->cStart, BufferTotal (pBuff)));
return TRUE;
}
IFDBG(DebugOut (DEBUG_ERROR, L"HCI_ReadPacket - unknown packet type\n"));
#if defined (DEBUG) || defined (_DEBUG)
IFDBG(DebugOut (DEBUG_ERROR, L"Dumping the failing buffer...\n"));
unsigned char buff[128];
buff[0] = pBuff->pBuffer[pBuff->cStart - 1];
int buff_size = 128;
int buff_offset = 1;
while (buff_offset < buff_size) {
OVERLAPPED o;
memset (&o, 0, sizeof(o));
o.hEvent = hReadEvent;
DWORD dwRead = 0;
#if ! defined (UNDER_CE)
while (! ReadFile (hFile, &buff[buff_offset], buff_size - buff_offset, &dwRead, &o)) {
if ((GetLastError() == ERROR_IO_PENDING) &&
GetOverlappedResult (hFile, &o, &dwRead, TRUE))
break;
if (hFile != INVALID_HANDLE_VALUE)
IFDBG(DebugOut (DEBUG_ERROR, L"Error reading COM port: GetLastError = 0x%08x (%d)\n", GetLastError (), GetLastError ()));
break;
}
#else
if ((! ReadFile (hFile, &buff[buff_offset], buff_size - buff_offset, &dwRead, NULL)) &&
(dwRead == 0)) {
if (hFile != INVALID_HANDLE_VALUE)
IFDBG(DebugOut (DEBUG_ERROR, L"Error writing COM port: GetLastError = 0x%08x (%d)\n", GetLastError (), GetLastError ()));
break;
}
#endif
if (dwRead == 0)
break;
buff_offset += dwRead;
}
IFDBG(DumpBuff (DEBUG_ERROR, buff, buff_offset));
#endif
return FALSE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -