📄 bthamb.cxx
字号:
return FALSE;
}
DCB dcb;
dcb.DCBlength = sizeof(dcb);
//After power on/Reset, Ericsson module is running at 56K. However, the input clock for baudrate generator
// what we are using is four times of regular 1.8432 Mhz. We sould set up the baurdate to 14.4 K initially.
dcb.BaudRate = 14400;
dcb.fBinary = TRUE;
dcb.fParity = FALSE;
dcb.fOutxCtsFlow = TRUE;
dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
dcb.fOutX = FALSE;
dcb.fInX = FALSE;
dcb.fOutxDsrFlow = FALSE;
dcb.fDsrSensitivity = FALSE;
dcb.fDtrControl = DTR_CONTROL_ENABLE;
dcb.fTXContinueOnXoff = FALSE;
dcb.fErrorChar = FALSE;
dcb.fNull = FALSE;
dcb.fAbortOnError = TRUE;
// dcb.wReserved = 0;
dcb.ByteSize =8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
dcb.XonChar = 0x11;
dcb.XoffChar = 0x13;
dcb.XonLim = 3000 ;
dcb.XoffLim = 9000 ;
if (! SetCommState(hFile, &dcb)) {
IFDBG(DebugOut (DEBUG_ERROR, L"BthAmb:Failed SetCommState in UART HCI Interface. GetLastError = 0x%08x\n", GetLastError ()));
CloseHandle (hFile);
hFile = INVALID_HANDLE_VALUE;
return FALSE;
}
//
// After issue software reset to Ericsson, the baudrate will be configured to registry setting and currently is 115K.
//
if(!UART_SetBaudRate(hFile, dwBaud))
{
IFDBG(DebugOut (DEBUG_HCI_INIT, L"BthAmb:HCI_OpenConnection-Raise baud rate %d fail\n", dwBaud));
CloseHandle (hFile);
hFile = INVALID_HANDLE_VALUE;
return FALSE;
}
hWriteEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
hReadEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
IFDBG(DebugOut (DEBUG_HCI_INIT, L"BthAmb:HCI_OpenConnection - Successful\n"));
return TRUE;
}
void HCI_CloseConnection (void) {
IFDBG(DebugOut (DEBUG_HCI_INIT, L"BthAmb:HCI_CloseConnection - Started\n"));
if (hFile == INVALID_HANDLE_VALUE) {
IFDBG(DebugOut (DEBUG_ERROR, L"BthAmb:HCI_CloseConnection - not active\n"));
return;
}
CloseHandle (hFile);
CloseHandle (hWriteEvent);
CloseHandle (hReadEvent);
hFile = INVALID_HANDLE_VALUE;
hWriteEvent = hReadEvent = NULL;
IFDBG(DebugOut (DEBUG_HCI_INIT, L"BthAmb: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"BthAmb: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"BthAmb:Error writing COM port: GetLastError = 0x%08x (%d)\n", GetLastError (), GetLastError ()));
#endif
return FALSE;
}
#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"BthAmb: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"BthAmb:Error writing COM port: GetLastError = 0x%08x (%d)\n", GetLastError (), GetLastError ()));
#endif
return FALSE;
}
#endif
dwFilledSoFar += dwRead;
}
return TRUE;
}
int HCI_WritePacket (HCI_TYPE eType, BD_BUFFER *pBuff) {
IFDBG(DebugOut (DEBUG_HCI_TRANSPORT, L"BthAmb: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"BthAmb:[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"BthAmb: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"BthAmb: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"BthAmb:HCI_ReadPacket\n"));
if (hFile == INVALID_HANDLE_VALUE) {
IFDBG(DebugOut (DEBUG_ERROR, L"BthAmb:HCI_ReadPacket - not active\n"));
return FALSE;
}
pBuff->cStart = 3;
pBuff->cEnd = pBuff->cSize;
if (BufferTotal (pBuff) < 257) {
IFDBG(DebugOut (DEBUG_ERROR, L"BthAmb: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"BthAmb: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"BthAmb: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"BthAmb: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"BthAmb: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"BthAmb: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"BthAmb: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"BthAmb:HCI_ReadPacket: Event\n"));
IFDBG(DumpBuff (DEBUG_HCI_DUMP, pBuff->pBuffer + pBuff->cStart, BufferTotal (pBuff)));
return TRUE;
}
IFDBG(DebugOut (DEBUG_ERROR, L"BthAmb:HCI_ReadPacket - unknown packet type\n"));
#if defined (DEBUG) || defined (_DEBUG)
IFDBG(DebugOut (DEBUG_ERROR, L"BthAmb: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"BthAmb: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"BthAmb: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 + -