⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dunport.cxx

📁 Windows CE操作系统中适用的蓝牙驱动程序
💻 CXX
📖 第 1 页 / 共 2 页
字号:
			*p++ = L' ';
		}

		*p++ = L'\n';
		*p++ = L'\0';

		OutputDebugString (szLineHeader);
		OutputDebugString (L" ");
		OutputDebugString (szLine);
	}
}
#endif

//	@func DWORD | COM_Write | Device write routine
//  @parm DWORD | dwOpenData | value returned from CON_Open call
//  @parm LPCVOID | pBuf | buffer containing data
//  @parm DWORD | len | maximum length to write [IN BYTES, NOT WORDS!!!]
//  @rdesc	Returns -1 for error, otherwise the number of bytes written.  The
//			length returned is guaranteed to be the length requested unless an
//			error condition occurs.
//	@remark	Routine exported by a device driver.  "PRF" is the string passed
//			in as lpszType in RegisterDevice
//
extern "C" DWORD COM_Write (DWORD dwData, LPCVOID pInBuf, DWORD dwInLen) {
	DEBUGMSG(BTDUN_TRACE, (L"COM_Write+++ 0x%08x, %d bytes\n", dwData, dwInLen));
	EnterCriticalSection (&gcs);
	Handle *ph = GetHandle (dwData);
	HANDLE h = ph ? ph->h : NULL;
	int fConn = ph ? ph->fConnected : FALSE;
	LeaveCriticalSection (&gcs);

	if (! fConn) {
		if ((dwInLen >= 12) && (_strnicmp ((char *)pInBuf, "CLIENTSERVER", 12) == 0)) {
			DEBUGMSG(BTDUN_TRACE, (L"COM_Read : got CLIENTSERVER, substituting CONNECTED\n"));
			if (! WriteCommPort (h, (unsigned char *)"CONNECTED\n", 10))
				return -1;
		}

		EnterCriticalSection (&gcs);
		ph = GetHandle (dwData);
		if (ph)
			ph->fConnected = TRUE;
		LeaveCriticalSection (&gcs);

		return dwInLen;
	}

#if BTDUN_FULLTRACE
	DumpBuff (L"COM_Write", (unsigned char *)pInBuf, dwInLen);
#endif

	DWORD dwWrit = 0;
	BOOL res = WriteFile (h, pInBuf, dwInLen, &dwWrit, NULL);
	if (res) {
		DEBUGMSG(BTDUN_TRACE, (L"COM_Write--- : written %d bytes\n", dwWrit));
		return dwWrit;
	}

	DEBUGMSG(BTDUN_TRACE, (L"COM_Write--- : error writing. GLE = %d\n", GetLastError ()));
	return -1;
}

//	@func DWORD | COM_Read | Device read routine
//  @parm DWORD | dwOpenData | value returned from CON_Open call
//  @parm LPVOID | pBuf | buffer to receive data
//  @parm DWORD | len | maximum length to read [IN BYTES, not WORDS!!]
//  @rdesc	Returns 0 for end of file, -1 for error, otherwise the number of
//			bytes read.  The length returned is guaranteed to be the length
//			requested unless end of file or an error condition occurs.
//	@remark	Routine exported by a device driver.  "PRF" is the string passed
//			in as lpszType in RegisterDevice
//
extern "C" DWORD COM_Read (DWORD dwData, LPVOID pBuf, DWORD Len) {
	DEBUGMSG(BTDUN_TRACE, (L"COM_Read+++ 0x%08x, %d bytes\n", dwData, Len));

	EnterCriticalSection (&gcs);
	Handle *ph = GetHandle (dwData);
	HANDLE h = NULL;
	int fConn = FALSE;
	int fGotConnect = FALSE;

	if (ph) {
		h = ph->h;
		fConn = ph->fConnected;
		fGotConnect = ph->fGotConnect;
		ph->fGotConnect = FALSE;
	}

	LeaveCriticalSection (&gcs);

	if (fGotConnect) {
		if (Len >= 7) {
			strcpy ((char *)pBuf, "CLIENT");
			DEBUGMSG(BTDUN_TRACE, (L"COM_Read--- 0x%08x, CLIENT\n", dwData));
			return 7;
		}

		DEBUGMSG(BTDUN_TRACE, (L"COM_Read--- 0x%08x, insufficient buffer for CLIENT string\n", dwData));
		return 0;
	}

	DWORD dwRead = 0;
	BOOL res = ReadFile (h, pBuf, Len, &dwRead, NULL);
	if (! res) {
		DEBUGMSG(BTDUN_TRACE, (L"COM_Read--- : error reading. GLE = %d\n", GetLastError()));
		return -1;
	}

	DEBUGMSG(BTDUN_TRACE, (L"COM_Read : read %d bytes\n", dwRead));

#if BTDUN_FULLTRACE
	if (dwRead)
		DumpBuff (L"COM_Write", (unsigned char *)pBuf, dwRead);
#endif

	return dwRead;
}

//	@func DWORD | COM_Seek | Device seek routine
//  @parm DWORD | dwOpenData | value returned from CON_Open call
//  @parm long | pos | position to seek to (relative to type)
//  @parm DWORD | type | FILE_BEGIN, FILE_CURRENT, or FILE_END
//  @rdesc	Returns current position relative to start of file, or -1 on error
//	@remark	Routine exported by a device driver.  "PRF" is the string passed
//		 in as lpszType in RegisterDevice

extern "C" DWORD COM_Seek (DWORD dwData, long pos, DWORD type)
{
	DEBUGMSG(BTDUN_TRACE, (L"COM_Seek+++ 0x%08x, pos : %d type : %d\n", pos, type));
	EnterCriticalSection (&gcs);
	Handle *ph = GetHandle (dwData);
	HANDLE h = ph ? ph->h : NULL;
	LeaveCriticalSection (&gcs);

	DWORD dwRes = SetFilePointer (h, pos, NULL, type);
	if (dwRes == 0xffffffff) {
		DEBUGMSG(BTDUN_TRACE, (L"COM_Seek--- failed : GLE = %d\n", GetLastError ()));
		return (DWORD)-1;
	}

	DEBUGMSG(BTDUN_TRACE, (L"COM_Seek--- : success: new position %d\n", dwRes));

	return dwRes;
}

//	@func void | COM_PowerUp | Device powerup routine
//	@comm	Called to restore device from suspend mode.  You cannot call any
//			routines aside from those in your dll in this call.
extern "C" void COM_PowerUp(void)
{
	return;
}
//	@func void | COM_PowerDown | Device powerdown routine
//	@comm	Called to suspend device.  You cannot call any routines aside from
//			those in your dll in this call.
extern "C" void COM_PowerDown(void)
{
	return;
}

int ProcessRead (HANDLE h, DWORD dwData) {
	unsigned char buf[256];

	DWORD dwRead = 0;
	BOOL res = ReadFile (h, buf, sizeof(buf), &dwRead, NULL);
	if (! res)
		return FALSE;

	DEBUGMSG(BTDUN_TRACE, (L"ProcessRead : read %d bytes\n", dwRead));

#if defined (FULLTRACE)
	if (dwRead)
		DumpBuff (L"ProcessRead", (unsigned char *)buf, dwRead);
#endif

	if ((dwRead >= 4) && (_strnicmp ((char *)buf, "ATDT", 4) == 0)) {
		strcpy ((char *)buf, "CLIENT");
		DEBUGMSG(BTDUN_TRACE, (L"ProcessRead : got ATDT, substituting CLIENT\n"));

		EnterCriticalSection (&gcs);

		Handle *ph = GetHandle (dwData);
		if (ph)
			ph->fGotConnect = TRUE;

		LeaveCriticalSection (&gcs);

		return FALSE;
	}

	DWORD dwWrit = 0;
	WriteFile (h, "OK\n", 3, &dwWrit, NULL);
	DEBUGMSG(BTDUN_TRACE, (L"ProcessRead : sending OK\n"));
	return TRUE;
}

//	@func BOOL | COM_IOControl | Device IO control routine
//  @parm DWORD | dwOpenData | value returned from CON_Open call
//  @parm DWORD | dwCode | io control code to be performed
//  @parm PBYTE | pBufIn | input data to the device
//  @parm DWORD | dwLenIn | number of bytes being passed in
//  @parm PBYTE | pBufOut | output data from the device
//  @parm DWORD | dwLenOut |maximum number of bytes to receive from device
//  @parm PDWORD | pdwActualOut | actual number of bytes received from device
//  @rdesc	Returns TRUE for success, FALSE for failure
//	@remark	Routine exported by a device driver.  "PRF" is the string passed
//		in as lpszType in RegisterDevice
extern "C" BOOL COM_IOControl (DWORD dwData,
									DWORD dwIoControlCode,
									LPVOID lpInBuf,
									DWORD nInBufSize,
									LPVOID lpOutBuf,
									DWORD nOutBufSize,
									LPDWORD lpBytesReturned) {
	DEBUGMSG(BTDUN_TRACE, (L"COM_IOControl : 0x%08x code 0x%08x in %d bytes out %d bytes\n", dwData, dwIoControlCode, nInBufSize, nOutBufSize));

	for ( ; ; ) {
		EnterCriticalSection (&gcs);
		Handle *ph = GetHandle (dwData);
		HANDLE h = ph ? ph->h : NULL;
		int fConn = ph ? ph->fConnected : FALSE;
		LeaveCriticalSection (&gcs);

		DWORD dwRes = DeviceIoControl (h, dwIoControlCode, lpInBuf, nInBufSize, lpOutBuf, nOutBufSize, lpBytesReturned, NULL);
		DEBUGMSG(BTDUN_TRACE, (L"COM_IOControl : DeviceIoControl returned %d/GLE=%d (%d bytes)\n", dwRes, GetLastError (), lpBytesReturned ? *lpBytesReturned : 0));
		if ((! fConn) && (dwIoControlCode == IOCTL_SERIAL_WAIT_ON_MASK) &&
			dwRes && (*(DWORD *)lpOutBuf & EV_RXCHAR)) {
			if (ProcessRead (h, dwData)) {
				*(DWORD *)lpOutBuf &= ~EV_RXCHAR;
				if (*(DWORD *)lpOutBuf == 0) {
					DEBUGMSG(BTDUN_TRACE, (L"COM_IOControl : skipping EV_RXCHAR\n"));
					continue;
				}
			}
		}

		if (dwRes && (dwIoControlCode == IOCTL_SERIAL_GET_MODEMSTATUS)) {
			DWORD dwStatus = *(DWORD *)lpOutBuf;
			if (fConn && ((dwStatus & MS_RLSD_ON) == 0)) {
				DEBUGMSG(1, (L"COM_IOControl : Line dropped! Time to reinitialize!\n"));

				EnterCriticalSection (&gcs);
				ph = GetHandle (dwData);
				if (ph) {
					ph->fConnected = FALSE;
					ph->fGotConnect = FALSE;
				}
				LeaveCriticalSection (&gcs);
			}
		}
		return dwRes;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -