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

📄 phoneui.cxx

📁 Windows CE操作系统中适用的蓝牙驱动程序
💻 CXX
📖 第 1 页 / 共 3 页
字号:

	if (ERROR_SUCCESS == iRet) {
		union {
			CHAR buf[5000];
			SOCKADDR_BTH	__unused;	// properly align buffer to BT_ADDR requirements
		};

		LPWSAQUERYSET pwsaResults = (LPWSAQUERYSET) buf;
		DWORD dwSize  = sizeof(buf);

		memset(pwsaResults,0,sizeof(WSAQUERYSET));
		pwsaResults->dwSize      = sizeof(WSAQUERYSET);
		pwsaResults->dwNameSpace = NS_BTH;
		pwsaResults->lpBlob      = NULL;

		iRet = BthNsLookupServiceNext (hLookup, 0, &dwSize, pwsaResults);
		if (iRet == ERROR_SUCCESS) {	// Success - got the stream
			unsigned char cChannel = 0;
			if (ERROR_SUCCESS == FindRFCOMMChannel (pwsaResults->lpBlob->pBlobData,
					pwsaResults->lpBlob->cbSize, &cChannel))
				iResult = cChannel;
		}

		BthNsLookupServiceEnd(hLookup);
	}

	CoUninitialize ();
	return iResult;
}

static DWORD WINAPI DoInquiry (LPVOID lpUnused) {
	g_pState->Lock ();

	HWND hWnd        = g_pState->hWnd;

	if ((! g_pState->hWnd) || (g_pState->fState == DO_STOP)) {
		g_pState->Unlock ();

		return 0;
	}
	
	if (g_pState->fState != DO_NOTHING) {
		g_pState->fState = DO_STOP;
		g_pState->Unlock ();

		SetWindowText (hWnd, L"Stopping...");

		return 0;
	}

	CleanInquiryData ();

	g_pState->fState = DO_INQUIRY;

	g_pState->Unlock ();

	HWND hWndButton  = GetDlgItem (hWnd, IDC_INQUIRY);
	HWND hWndDevList = GetDlgItem (hWnd, IDC_DEVICELIST);

	int fLan = (SendMessage (GetDlgItem (hWnd, IDC_LAN), BM_GETCHECK, 0, 0) == BST_CHECKED);

	SetWindowText (hWndButton, L"STOP");
	SetWindowText (hWnd, L"Inquiry Running...");
	SendMessage (hWndDevList, LB_RESETCONTENT, 0, 0);

	HANDLE hLookup;
	int iErr = PerformInquiry(hWndDevList, FALSE, &hLookup);

	if (iErr == ERROR_SUCCESS)
		iErr = PerformInquiry(hWndDevList, TRUE, &hLookup);

	BthNsLookupServiceEnd(hLookup);

	if (iErr != ERROR_SUCCESS) {
		SetWindowText (hWndButton, L"Inquiry");
		SetWindowText (hWnd, L"Phone UI");

		g_pState->fState = DO_NOTHING;

		WCHAR szString[64];
		wsprintf (szString, L"Bluetooth hardware error %d\n", iErr);
		MessageBox(hWnd, szString, L"Error", MB_OK | MB_TOPMOST);
		return 0;
	}

	SetWindowText (hWnd, L"SDP Queries");

	g_pState->Lock ();
	CHECK_STOP;

	g_pState->fState = DO_SDP;

	while (g_pState->hWnd && (g_pState->fState != DO_STOP)) {
		InquiryResult *pRes = g_pState->pDev;
		while (pRes && pRes->fHaveSDP)
			pRes = pRes->pNext;

		if (! pRes)
			break;

		pRes->fHaveSDP = TRUE;
		BT_ADDR b = pRes->b;

		g_pState->Unlock ();

		WCHAR szTitle[64];
		wsprintf (szTitle, L"SDP %04x%08x", GET_NAP(b), GET_SAP(b));
		SetWindowText (hWnd, szTitle);

		int channel = DoSDP (&b, fLan);

		if (channel) {
			for (int i = 0 ; ; ++i) {
				int iData = SendMessage (hWndDevList, LB_GETITEMDATA, (WPARAM)i, (LPARAM)0);
				if (iData == LB_ERR)
					break;

				if (iData == (int)pRes) {
					WCHAR szOldName[MAX_NAME + 64];
					WCHAR szNewName[MAX_NAME + 128];
					if (SendMessage(hWndDevList, LB_GETTEXTLEN, (WPARAM)i, (LPARAM)0) <= (MAX_NAME + 64)) {
						SendMessage (hWndDevList, LB_GETTEXT, (WPARAM)i, (LPARAM)szOldName);
						wsprintf (szNewName, L"%s (c = %d)", szOldName, channel);

						SendMessage (hWndDevList, LB_DELETESTRING, (WPARAM)i, (LPARAM)0);
						SendMessage (hWndDevList, LB_INSERTSTRING, (WPARAM)i, (LPARAM)szNewName);
						SendMessage (hWndDevList, LB_SETITEMDATA,  (WPARAM)i, (LPARAM)pRes);
					}

					break;
				}
			}

		}

		g_pState->Lock ();
		if (channel) {
			InquiryResult *pX = g_pState->pDev;
			while (pX && (pX != pRes))
				pX = pX->pNext;

			if (pX)
				pX->channel = channel;
		}
	}

	g_pState->fState = DO_NOTHING;
	g_pState->Unlock ();

	SetWindowText (hWndButton, L"Inquiry");
	SetWindowText (hWnd, L"Phone UI");
	return 0;
}

static BOOL CALLBACK DlgProc2 (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
	switch (uMsg) {
	case WM_INITDIALOG:
		SetWindowText (GetDlgItem (hWnd, IDC_CHANNEL), L"");
		SetFocus (GetDlgItem (hWnd, IDC_CHANNEL));

		return 0;

	case WM_COMMAND:
		{
			int wID = LOWORD(wParam);
			switch (wID)
			{
			case IDOK:
				{
					WCHAR szChannel[64];
					GetWindowText (GetDlgItem (hWnd, IDC_CHANNEL), szChannel, 64);
					int c = _wtoi (szChannel);
					EndDialog (hWnd, c);
				}
				return 0;

			case IDCANCEL:
				EndDialog (hWnd, 0);
				return 0;
			}
		}
		break;
	}

	return 0;
}

static int GetBA (WCHAR *pp, BT_ADDR *pba) {
	while (*pp == ' ')
		++pp;

	for (int i = 0 ; i < 4 ; ++i, ++pp) {
		if (! iswxdigit (*pp))
			return FALSE;

		int c = *pp;
		if (c >= 'a')
			c = c - 'a' + 0xa;
		else if (c >= 'A')
			c = c - 'A' + 0xa;
		else c = c - '0';

		if ((c < 0) || (c > 16))
			return FALSE;

		*pba = *pba * 16 + c;
	}

	for (i = 0 ; i < 8 ; ++i, ++pp) {
		if (! iswxdigit (*pp))
			return FALSE;

		int c = *pp;
		if (c >= 'a')
			c = c - 'a' + 0xa;
		else if (c >= 'A')
			c = c - 'A' + 0xa;
		else c = c - '0';

		if ((c < 0) || (c > 16))
			return FALSE;

		*pba = *pba * 16 + c;
	}

	if ((*pp != ' ') && (*pp != '\0'))
		return FALSE;

	return TRUE;
}

static void StopDevice (void) {
	if (g_pState->hDevice) {
		DeregisterDevice (g_pState->hDevice);
		g_pState->hDevice = NULL;
	}
}

static int IsRegistered (void) {
	HKEY hk;

	if (ERROR_SUCCESS != RegOpenKeyEx (HKEY_LOCAL_MACHINE, L"software\\microsoft\\bluetooth\\phoneui\\device", 0, KEY_ALL_ACCESS, &hk))
		return FALSE;

	RegCloseKey (hk);

	return TRUE;
}

static void Deregister (void) {
	StopDevice ();

	RegDeleteKey (HKEY_LOCAL_MACHINE, L"software\\microsoft\\bluetooth\\phoneui\\device");
}

static int GetRegistration (WCHAR *szOut, int cSize, int *pfAuth, int *pfEncrypt, int *pfLan) {
	*pfEncrypt = FALSE;
	*pfAuth    = FALSE;
	*pfLan     = FALSE;

	if (cSize < 64)
		return FALSE;

	wcscpy (szOut, L"No bonding");

	HKEY hk;

	if (ERROR_SUCCESS != RegOpenKeyEx (HKEY_LOCAL_MACHINE, L"software\\microsoft\\bluetooth\\phoneui\\device", 0, KEY_ALL_ACCESS, &hk))
		return TRUE;

	BT_ADDR b;
	unsigned char c;

	DWORD dwC;
	DWORD dwType;
	DWORD dwSize = sizeof(dwC);

	if ((ERROR_SUCCESS != RegQueryValueEx (hk, L"channel", NULL, &dwType, (BYTE *)&dwC, &dwSize)) ||
		(dwType != REG_DWORD) || (dwSize > sizeof(dwC))) {
		RegCloseKey (hk);

		return TRUE;
	}

	c = (unsigned char)dwC;

	WCHAR szString[256];
	dwSize = sizeof(szString);

	if ((ERROR_SUCCESS != RegQueryValueEx (hk, L"device", NULL, &dwType, (BYTE *)szString, &dwSize)) ||
		(dwType != REG_SZ) || (dwSize > sizeof(szString))) {
		RegCloseKey (hk);

		return TRUE;
	}

	DWORD dw = FALSE;
	dwSize = sizeof(dw);

	if ((ERROR_SUCCESS != RegQueryValueEx (hk, L"authenticate", NULL, &dwType, (BYTE *)&dw, &dwSize)) ||
		(dwType != REG_DWORD) || (dwSize != sizeof(dw)) || (! dw))
		dw = FALSE;

	if (dw)
		*pfAuth = TRUE;

	dw = FALSE;
	dwSize = sizeof(dw);

	if ((ERROR_SUCCESS != RegQueryValueEx (hk, L"encrypt", NULL, &dwType, (BYTE *)&dw, &dwSize)) ||
		(dwType != REG_DWORD) || (dwSize != sizeof(dw)) || (! dw))
		dw = FALSE;

	if (dw)
		*pfEncrypt = TRUE;

	dw = FALSE;
	dwSize = sizeof(dw);

	if ((ERROR_SUCCESS != RegQueryValueEx (hk, L"lan", NULL, &dwType, (BYTE *)&dw, &dwSize)) ||
		(dwType != REG_DWORD) || (dwSize != sizeof(dw)) || (! dw))
		dw = FALSE;

	if (dw)
		*pfLan = TRUE;

	RegCloseKey (hk);

	if ((c <= 0) || (c > 31)) {
		wcscpy (szOut, L"Bad channel!");
		return TRUE;
	}

	if (! GetBA (szString, &b)) {
		wcscpy (szOut, L"Bad address!");
		return TRUE;
	}

	if (ERROR_SUCCESS != RegOpenKeyEx (HKEY_LOCAL_MACHINE, *pfLan ? L"ExtModems\\bluetooth_dcn" : L"ExtModems\\bluetooth_dun", 0, KEY_READ, &hk)) {
		wcscpy (szOut, L"No modem key!");

		return TRUE;
	}

	dwSize = sizeof(szString);
	if ((ERROR_SUCCESS != RegQueryValueEx (hk, L"port", NULL, &dwType, (BYTE *)szString, &dwSize)) ||
		(dwType != REG_SZ) || (dwSize > sizeof(szString))) {
		wcscpy (szOut, L"No modem port!");
		RegCloseKey (hk);

		return TRUE;
	}

	RegCloseKey (hk);

	if ((toupper (szString[0]) != 'C') || (toupper (szString[1]) != 'O') ||
		(toupper (szString[2]) != 'M') || (szString[3] < '0') || (szString[3] > '9') ||
		(szString[4] != ':') || (szString[5] != '\0')) {
		wcscpy (szOut, L"Bad modem port!");
		return TRUE;
	}

	DWORD ndx = szString[3] - '0';

	wsprintf (szOut, L"%04x%08x %d %s on COM%d:", GET_NAP(b), GET_SAP(b), c, g_pState->hDevice ? L"Running" : L"Stopped", ndx);

	return TRUE;
}

static void CreateDevice (void) {
	HKEY hk;

	BT_ADDR b;
	unsigned char c;
	unsigned int uiportflags = 0;

	if (ERROR_SUCCESS != RegOpenKeyEx (HKEY_LOCAL_MACHINE, L"software\\microsoft\\bluetooth\\phoneui\\device", 0, KEY_ALL_ACCESS, &hk))
		return;

	DWORD dwC;
	DWORD dwSize = sizeof(dwC);
	DWORD dwType;
	if ((ERROR_SUCCESS != RegQueryValueEx (hk, L"channel", NULL, &dwType, (BYTE *)&dwC, &dwSize)) ||
		(dwType != REG_DWORD) || (dwSize > sizeof(dwC))) {
		RegCloseKey (hk);

		return;
	}

	c = (unsigned char)dwC;

	WCHAR szString[256];
	dwSize = sizeof(szString);

	if ((ERROR_SUCCESS != RegQueryValueEx (hk, L"device", NULL, &dwType, (BYTE *)szString, &dwSize)) ||
		(dwType != REG_SZ) || (dwSize > sizeof(szString))) {
		RegCloseKey (hk);

		return;

⌨️ 快捷键说明

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