📄 btmodem.cxx
字号:
DWORD dwWrit = 0;
char *pbuffer = buffer;
DWORD dwWritFails = 0;
while (dwRead && WriteFile (hDest, pbuffer, dwRead, &dwWrit, NULL) && (dwWritFails < MAX_WRITE_FAILS)) {
pbuffer += dwWrit;
dwRead -= dwWrit;
if (dwWrit == 0)
dwWritFails++;
else
dwWritFails = 0;
dwWrit = 0;
}
#if defined (DUMPCOMM)
if (dwRead != 0)
RETAILMSG(1, (L"Bluetooth modem gateway: %s Failed to write everything." CRLF, name));
#endif
}
#if defined (DUMPCOMM)
if (ModemState & EV_ERR)
RETAILMSG(1, (L"Bluetooth gateway: %s thread: EV_ERR" CRLF, name));
if (ModemState & EV_CTS)
RETAILMSG(1, (L"Bluetooth gateway: %s thread: EV_CTS" CRLF, name));
if (ModemState & EV_DSR)
RETAILMSG(1, (L"Bluetooth gateway: %s thread: EV_DSR" CRLF, name));
if (ModemState & EV_RLSD)
RETAILMSG(1, (L"Bluetooth gateway: %s thread: EV_RLSD" CRLF, name));
if (ModemState & EV_RING)
RETAILMSG(1, (L"Bluetooth gateway: %s thread: EV_RING" CRLF, name));
#endif
if (fBluetoothThread && (ModemState & EV_RLSD)) {
DWORD dwModem = 0;
GetCommModemStatus (hSource, &dwModem);
if (! (dwModem & MS_RLSD_ON)) {
if (HangUpModem (hDest))
continue;
}
}
if (ModemState & (EV_CTS | EV_DSR)) {
DWORD dwModem = 0;
GetCommModemStatus (hSource, &dwModem);
#if defined (DUMPCOMM)
RETAILMSG(1, (L"Bluetooth modem gateway: %s modem status: 0x%08x" CRLF, name, dwModem));
#endif
if (ModemState & EV_CTS)
EscapeCommFunction (hDest, (dwModem & MS_CTS_ON) ? SETDTR : CLRDTR);
if (ModemState & EV_DSR)
EscapeCommFunction (hDest, (dwModem & MS_DSR_ON) ? SETRTS : CLRRTS);
}
}
SetEvent (ghServiceExitEvent);
RETAILMSG(1, (L"Bluetooth modem gateway: Exiting %s..." CRLF, name));
return 0;
}
static DWORD WINAPI ComServiceThread (LPVOID lpNull) {
gfServiceState = SERVICE_STATE_STARTING_UP;
#if ! defined (SDK_BUILD)
int fHaveStack = FALSE;
for (int i = 0 ; i < 20 ; ++i) {
HANDLE hBthApiEvent = OpenEvent (EVENT_ALL_ACCESS, FALSE, BTH_NAMEDEVENT_STACK_INITED);
if (hBthApiEvent) {
if (WAIT_OBJECT_0 == WaitForSingleObject (hBthApiEvent, BTH_MODEM_TIMEOUT)) {
fHaveStack = TRUE;
CloseHandle (hBthApiEvent);
break;
}
CloseHandle (hBthApiEvent);
}
Sleep (1000);
}
if (! fHaveStack) {
gfServiceState = SERVICE_STATE_OFF;
RETAILMSG(1, (L"Bluetooth modem gateway: Initialization failed: no bluetooth stack detected" CRLF));
return 0;
}
#endif
WCHAR szModemPortName[_MAX_PATH];
WCHAR szBthPortName[_MAX_PATH];
// Set up pre-configured stuff
// Modem
DWORD dwModemBaud = BTH_MODEM_BAUD;
DWORD dwByteSize = 8;
DWORD dwParity = NOPARITY;
DWORD dwStopBits = ONESTOPBIT;
DWORD dwFlowControl = BTH_MODEM_HARDWARE;
int fUseBaud = FALSE;
int fUseByteSize = FALSE;
int fUseParity = FALSE;
int fUseStopBits = FALSE;
int fUseFlow = FALSE;
DCB dcb;
int fUseDCB = FALSE;
// Bluetooth
int fAuthenticate = TRUE;
int fEncrypt = TRUE;
int channel = 0xfe;
int mtu = BTH_MODEM_MTU;
// First, read the configuration parameters
HKEY hk;
if (ERROR_SUCCESS != RegOpenKeyEx (HKEY_LOCAL_MACHINE, BTH_MODEM_REGBASE, 0, KEY_READ, &hk)) {
gfServiceState = SERVICE_STATE_OFF;
RETAILMSG(1, (L"Bluetooth modem gateway: Initialization failed: no configuration" CRLF));
return 0;
}
DWORD dwSize = sizeof(szModemPortName) - sizeof(WCHAR);
DWORD dwType = 0;
szModemPortName[sizeof(szModemPortName)/sizeof(szModemPortName[0])-1] = '\0';
if ((ERROR_SUCCESS != RegQueryValueEx (hk, L"ModemPortName", NULL, &dwType, (LPBYTE)szModemPortName, &dwSize)) ||
(dwType != REG_SZ) || (dwSize >= sizeof(szModemPortName))) {
RegCloseKey (hk);
RETAILMSG(1, (L"Bluetooth modem gateway: Initialization failed: modem port name not specified or invalid" CRLF));
gfServiceState = SERVICE_STATE_OFF;
return 0;
}
dwSize = sizeof (dwModemBaud);
if (ERROR_SUCCESS == RegQueryValueEx (hk, L"ModemBaud", NULL, &dwType, (LPBYTE)&dwModemBaud, &dwSize)) {
if ((dwType != REG_DWORD) || (dwSize != sizeof(dwModemBaud))) {
RegCloseKey (hk);
RETAILMSG(1, (L"Bluetooth modem gateway: Initialization failed: modem baud rate invalid" CRLF));
gfServiceState = SERVICE_STATE_OFF;
return 0;
}
fUseBaud = TRUE;
}
dwSize = sizeof (dwByteSize);
if (ERROR_SUCCESS == RegQueryValueEx (hk, L"ModemByteSize", NULL, &dwType, (LPBYTE)&dwByteSize, &dwSize)) {
if ((dwType != REG_DWORD) || (dwSize != sizeof(dwByteSize)) || (dwByteSize < 4) || (dwByteSize > 8)) {
RegCloseKey (hk);
RETAILMSG(1, (L"Bluetooth modem gateway: Initialization failed: modem byte size invalid" CRLF));
gfServiceState = SERVICE_STATE_OFF;
return 0;
}
fUseByteSize = TRUE;
}
dwSize = sizeof (dwParity);
if (ERROR_SUCCESS == RegQueryValueEx (hk, L"ModemParity", NULL, &dwType, (LPBYTE)&dwParity, &dwSize)) {
if ((dwType != REG_DWORD) || (dwSize != sizeof(dwParity)) || (dwParity > SPACEPARITY)) {
RegCloseKey (hk);
RETAILMSG(1, (L"Bluetooth modem gateway: Initialization failed: modem parity invalid" CRLF));
gfServiceState = SERVICE_STATE_OFF;
return 0;
}
fUseParity = TRUE;
}
dwSize = sizeof (dwStopBits);
if (ERROR_SUCCESS == RegQueryValueEx (hk, L"ModemStopBits", NULL, &dwType, (LPBYTE)&dwStopBits, &dwSize)) {
if ((dwType != REG_DWORD) || (dwSize != sizeof(dwStopBits)) || (dwStopBits > TWOSTOPBITS)) {
RegCloseKey (hk);
RETAILMSG(1, (L"Bluetooth modem gateway: Initialization failed: modem stop bits invalid" CRLF));
gfServiceState = SERVICE_STATE_OFF;
return 0;
}
fUseStopBits = TRUE;
}
dwSize = sizeof (dwFlowControl);
if (ERROR_SUCCESS == RegQueryValueEx (hk, L"ModemFlowControl", NULL, &dwType, (LPBYTE)&dwFlowControl, &dwSize)) {
if ((dwType != REG_DWORD) || (dwSize != sizeof(dwFlowControl)) || (dwFlowControl > BTH_MODEM_NONE)) {
RegCloseKey (hk);
RETAILMSG(1, (L"Bluetooth modem gateway: Initialization failed: modem flow control invalid" CRLF));
gfServiceState = SERVICE_STATE_OFF;
return 0;
}
fUseFlow = TRUE;
}
DCB dcb2;
dwSize = sizeof (dcb2);
if (ERROR_SUCCESS == RegQueryValueEx (hk, L"ModemDCB", NULL, &dwType, (LPBYTE)&dcb2, &dwSize)) {
if ((dwType != REG_BINARY) || (dwSize != sizeof(dcb2)) || (dcb2.DCBlength != sizeof(dcb2))) {
RegCloseKey (hk);
gfServiceState = SERVICE_STATE_OFF;
RETAILMSG(1, (L"Bluetooth modem gateway: Initialization failed: modem DCB invalid" CRLF));
return 0;
}
memcpy (&dcb, &dcb2, sizeof(dcb));
fUseDCB = TRUE;
}
dwSize = sizeof(szBthPortName) - sizeof(WCHAR);
dwType = 0;
szBthPortName[sizeof(szBthPortName)/sizeof(szBthPortName[0])-1] = '\0';
if ((ERROR_SUCCESS != RegQueryValueEx (hk, L"BthPortName", NULL, &dwType, (LPBYTE)szBthPortName, &dwSize)) ||
(dwType != REG_SZ) || (dwSize >= sizeof(szBthPortName))) {
RegCloseKey (hk);
gfServiceState = SERVICE_STATE_OFF;
RETAILMSG(1, (L"Bluetooth modem gateway: Initialization failed: Bluetooth port name not specified or invalid" CRLF));
return 0;
}
dwSize = sizeof (mtu);
if (ERROR_SUCCESS == RegQueryValueEx (hk, L"MTU", NULL, &dwType, (LPBYTE)&mtu, &dwSize)) {
if ((dwType != REG_DWORD) || (dwSize != sizeof(mtu))) {
RegCloseKey (hk);
RETAILMSG(1, (L"Bluetooth modem gateway: Initialization failed: MTU invalid" CRLF));
gfServiceState = SERVICE_STATE_OFF;
return 0;
}
}
dwSize = sizeof (channel);
if (ERROR_SUCCESS == RegQueryValueEx (hk, L"channel", NULL, &dwType, (LPBYTE)&channel, &dwSize)) {
if ((dwType != REG_DWORD) || (dwSize != sizeof(channel)) || (channel < 1) || ((channel > 31) && (channel != 0xfe))) {
RegCloseKey (hk);
RETAILMSG(1, (L"Bluetooth modem gateway: Initialization failed: channel invalid" CRLF));
gfServiceState = SERVICE_STATE_OFF;
return 0;
}
}
dwSize = sizeof (fAuthenticate);
if (ERROR_SUCCESS == RegQueryValueEx (hk, L"Authenticate", NULL, &dwType, (LPBYTE)&fAuthenticate, &dwSize)) {
if ((dwType != REG_DWORD) || (dwSize != sizeof(fAuthenticate))) {
RegCloseKey (hk);
RETAILMSG(1, (L"Bluetooth modem gateway: Initialization failed: authentication flag invalid" CRLF));
gfServiceState = SERVICE_STATE_OFF;
return 0;
}
}
dwSize = sizeof (fEncrypt);
if (ERROR_SUCCESS == RegQueryValueEx (hk, L"Encrypt", NULL, &dwType, (LPBYTE)&fEncrypt, &dwSize)) {
if ((dwType != REG_DWORD) || (dwSize != sizeof(fEncrypt))) {
RegCloseKey (hk);
RETAILMSG(1, (L"Bluetooth modem gateway: Initialization failed: encryption flag invalid" CRLF));
gfServiceState = SERVICE_STATE_OFF;
return 0;
}
}
RegCloseKey (hk);
if (((szBthPortName[0] != 'C') && (szBthPortName[0] != 'c')) ||
((szBthPortName[1] != 'O') && (szBthPortName[1] != 'o')) ||
((szBthPortName[2] != 'M') && (szBthPortName[2] != 'm')) ||
((szBthPortName[4] != ':') && (szBthPortName[4] != ':')) ||
((szBthPortName[5] != '\0') && (szBthPortName[5] != '\0')) ||
(szBthPortName[3] < '0') || (szBthPortName[3] > '9')) {
RETAILMSG(1, (L"Bluetooth modem gateway: Initialization failed: encryption flag invalid" CRLF));
gfServiceState = SERVICE_STATE_OFF;
return 0;
}
int port = szBthPortName[3] - '0';
// Open and configure the modem port
HANDLE hModemComPort = CreateFile(szModemPortName,
GENERIC_READ | GENERIC_WRITE,
0, // comm devices must be opened w/exclusive-access
NULL, // no security attrs
OPEN_EXISTING, // comm devices must use OPEN_EXISTING
FILE_ATTRIBUTE_NORMAL, // overlapped I/O
NULL // hTemplate must be NULL for comm devices
);
if (hModemComPort == INVALID_HANDLE_VALUE) {
gfServiceState = SERVICE_STATE_OFF;
RETAILMSG(1, (L"Bluetooth modem gateway: Initialization failed: could not open modem port (error %d)" CRLF, GetLastError ()));
return 0;
}
if (! fUseDCB) {
if (! GetCommState (hModemComPort, &dcb)) {
RETAILMSG(1, (L"Bluetooth modem gateway: Initialization failed: could setup modem port (error %d)" CRLF, GetLastError ()));
CloseHandle (hModemComPort);
gfServiceState = SERVICE_STATE_OFF;
return 0;
}
}
if (fUseBaud)
dcb.BaudRate = dwModemBaud;
if (fUseByteSize)
dcb.ByteSize = (unsigned char)dwByteSize;
if (fUseStopBits)
dcb.StopBits = (unsigned char)dwStopBits;
if (fUseParity) {
dcb.fParity = (dwParity != NOPARITY);
dcb.Parity = (unsigned char)dwParity;
}
if (fUseFlow) {
dcb.fDsrSensitivity = FALSE;
dcb.fDtrControl = DTR_CONTROL_ENABLE;
if (dwFlowControl == BTH_MODEM_HARDWARE) {
// Enable RTS/CTS Flow Control
dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
dcb.fOutxCtsFlow = 1;
dcb.fOutX = 0;
dcb.fInX = 0;
} else if (dwFlowControl == BTH_MODEM_SOFTWARE) {
// Enable XON/XOFF Flow Control
dcb.fRtsControl = RTS_CONTROL_ENABLE;
dcb.fOutxCtsFlow = 0;
dcb.fOutX = 1;
dcb.fInX = 1;
} else {
dcb.fRtsControl = RTS_CONTROL_ENABLE;
dcb.fOutxCtsFlow = 0;
dcb.fOutX = 0;
dcb.fInX = 0;
}
}
if (! SetupComm (hModemComPort, 20000, 20000)) {
RETAILMSG(1, (L"Bluetooth modem gateway: Initialization failed: could setup modem port (error %d)" CRLF, GetLastError ()));
CloseHandle (hModemComPort);
gfServiceState = SERVICE_STATE_OFF;
return 0;
}
// purge any information in the buffer
if ( ! PurgeComm (hModemComPort, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR )) {
RETAILMSG(1, (L"Bluetooth modem gateway: Initialization failed: could not purge modem port (error %d)" CRLF, GetLastError ()));
CloseHandle (hModemComPort);
gfServiceState = SERVICE_STATE_OFF;
return 0;
}
COMMTIMEOUTS commTimeouts;
commTimeouts.ReadTotalTimeoutMultiplier = 1;
commTimeouts.ReadIntervalTimeout = 50;
commTimeouts.ReadTotalTimeoutConstant = 50;
commTimeouts.WriteTotalTimeoutMultiplier = 5;
commTimeouts.WriteTotalTimeoutConstant = 500;
if (! SetCommTimeouts (hModemComPort, &commTimeouts)) {
RETAILMSG(1, (L"Bluetooth modem gateway: Initialization failed: could not configure timeouts on modem port (error %d)" CRLF, GetLastError ()));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -