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

📄 btsvc.cxx

📁 This a sample Bluetooth application which illustrates the use of Windows CE Bluetooth stack
💻 CXX
📖 第 1 页 / 共 2 页
字号:
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
/*++
THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.
*/
//
//	This code implements boot-time activation of registered Bluetooth peers,
//	as well as authentication UI.
//
//	It is heavily dependent on registry key structure created by
//	public\common\oak\drivers\netui components (btmgmtui).
//
//	Please maintain synchronization between the two files.
//
//

#include <windows.h>
#include <service.h>
#include <stdio.h>

#include <windev.h>

#include <svsutil.hxx>

#include <bt_api.h>

#include "resource.h"

#include "../btenum/btenum.hxx"

#if defined (SDK_BUILD)
#include <aygshell.h>

typedef void (*tSHInputDialog)(HWND hwnd, UINT uMsg, WPARAM wParam);
tSHInputDialog gpSHInputDialog = NULL;
#endif

#define BTH_SERVICE_TIMEOUT		15000
#define BTH_SERVICE_SLEEP		5000
#define BTH_SERVICE_STORE_TO	3000
#define BTH_SERVICE_USER_TO		60000


#define APPNAME		L"BluetoothSVC"

struct pin_request {
	BT_ADDR		bt;
	HWND		hWnd;
};

static DWORD			gfServiceState = SERVICE_STATE_OFF;
static HANDLE			ghServiceThread;
static HANDLE			ghServiceExitEvent;
static HWND				ghWndHidden;
static SVSThreadPool	*gThreadPool;
static HINSTANCE		ghInst;

extern "C" BOOL WINAPI DllMain( HANDLE hInstDll, DWORD fdwReason, LPVOID lpvReserved) {
	switch(fdwReason) {
		case DLL_PROCESS_ATTACH:
			DisableThreadLibraryCalls ((HMODULE)hInstDll);
			svsutil_Initialize ();
			ghInst = (HINSTANCE)hInstDll;
			break;

		case DLL_PROCESS_DETACH:
			svsutil_DeInitialize ();
			break;
	}
	return TRUE;
}

static DWORD WINAPI GetName (LPVOID pArg) {
	pin_request *p = (pin_request *)pArg;
	BT_ADDR bt = p->bt;
	HWND hWnd = p->hWnd;

	WCHAR szName[MAX_PATH];
	unsigned int c = 0;

	if (ERROR_SUCCESS == BthRemoteNameQuery (&bt, MAX_PATH, &c, szName))
		SetWindowText (GetDlgItem (hWnd, IDC_CONNECT_STRING), szName);

	LocalFree (p);

	return FALSE;
}

static BOOL CALLBACK AuthenticationDlgProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
#if defined (SDK_BUILD)
	if (gpSHInputDialog)
		gpSHInputDialog (hWnd, uMsg, wParam);
#endif

	switch (uMsg) {
	case WM_INITDIALOG:
		{
			SetForegroundWindow (hWnd);
			SetWindowText (GetDlgItem (hWnd, IDC_PIN), L"");
			SetFocus (GetDlgItem (hWnd, IDC_PIN));

			((pin_request *)lParam)->hWnd = hWnd;
			SetWindowLong (hWnd, DWL_USER, lParam);

			pin_request *p = (pin_request *)LocalAlloc (LMEM_FIXED, sizeof(pin_request));

			if (! p) {
				EndDialog (hWnd, FALSE);
				return FALSE;
			}

			memcpy (p, (pin_request *)lParam, sizeof(pin_request));

			WCHAR szString[256];
			wsprintf (szString, L"%04x%08x", GET_NAP(p->bt), GET_SAP(p->bt));
			SetWindowText (GetDlgItem (hWnd, IDC_CONNECT_STRING), szString);

			if (! gThreadPool->ScheduleEvent (GetName, p))
				LocalFree (p);
		}
		return FALSE;

	case WM_COMMAND:
		{
			int wID = LOWORD(wParam);
			switch (wID)
			{
			case IDOK:
				{
					WCHAR szPin[64];
					WCHAR *pszPIN = szPin;

					GetWindowText (GetDlgItem (hWnd, IDC_PIN), szPin, 64);
					pin_request *p = (pin_request *)GetWindowLong (hWnd, DWL_USER);

					unsigned char pin[16];
					int cPin = 0;

					while ((*pszPIN) && (cPin < 16))
						pin[cPin++] = (unsigned char)*(pszPIN++);

					if (cPin)
						BthSetPIN (&p->bt, cPin, pin);

					EndDialog (hWnd, FALSE);
				}
				return FALSE;

			case IDCANCEL:
				{
					pin_request *p = (pin_request *)GetWindowLong (hWnd, DWL_USER);
					BthRefusePINRequest (&p->bt);
					EndDialog (hWnd, FALSE);
				}
				return FALSE;
			}
		}
		break;
	}

	return FALSE;
}

LRESULT CALLBACK WndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
	switch (message) {
	case WM_CREATE:
		return FALSE;

	case WM_DESTROY:
		PostQuitMessage (0);
		break;

	case WM_USER + 1:
		DialogBoxParam (ghInst, MAKEINTRESOURCE (IDD_ENTERPIN), hWnd, AuthenticationDlgProc, lParam);
		LocalFree ((void *)lParam);
		break;

	case WM_USER + 2:
		DestroyWindow (hWnd);
		break;

	default:
        return DefWindowProc(hWnd, message, wParam, lParam);
	}

	return FALSE;
}


static DWORD WINAPI UIThread (LPVOID lpUnused) {
	WNDCLASS	wc;
	memset (&wc, 0, sizeof(wc));

	wc.lpfnWndProc 		= WndProc;
	wc.hInstance   		= ghInst;
	wc.lpszClassName 	= APPNAME;

	RegisterClass (&wc);

	ghWndHidden = CreateWindow (APPNAME, APPNAME, WS_DISABLED,
						CW_USEDEFAULT, CW_USEDEFAULT,
						CW_USEDEFAULT, CW_USEDEFAULT,
						NULL, NULL, ghInst, NULL);

	if (! ghWndHidden)
		return FALSE;

	MSG msg;

    while (GetMessage(&msg, NULL, 0, 0)) {
		TranslateMessage (&msg) ;
		DispatchMessage(&msg);
	}

	return FALSE;
}

static int EnumCallbackA (void *pContext, BTDEV *pb) {
	if (pb->fActive)
		BthEnumActivate (pb, FALSE);

	return TRUE;
}

static void StartServices (void) {
	static int fFirstTime = TRUE;

	if (fFirstTime)
		BthEnumDevices (NULL, EnumCallbackA);

	fFirstTime = FALSE;
}

static DWORD WINAPI BthServiceThread (LPVOID lpUnused) {
    for (int i = 0 ; i < 20 ; ++i) {
		if (IsAPIReady (SH_SHELL))
			break;

		Sleep (1000);
	}

#if defined (SDK_BUILD)
	if (! gpSHInputDialog) {
		HMODULE hMod = LoadLibrary (L"aygshell.dll");
		if (hMod)
			gpSHInputDialog = (tSHInputDialog)GetProcAddress (hMod, L"SHInputDialog");
	}
#endif

	// First, wait for Bluetooth stack to load
	HANDLE hAuthReq = CreateEvent (NULL, FALSE, FALSE, NULL);
	if (! hAuthReq)
		return FALSE;

	gThreadPool = new SVSThreadPool;
	if (! gThreadPool) {
		CloseHandle (hAuthReq);
		return FALSE;
	}

	HANDLE hUIThread = CreateThread (NULL, 0, UIThread, NULL, 0, NULL);
	if (! hUIThread) {
		CloseHandle (hAuthReq);
		delete gThreadPool;
		gThreadPool = NULL;
		return FALSE;
	}

	gfServiceState = SERVICE_STATE_STARTING_UP;

	for ( ; ; ) {
		if (gfServiceState == SERVICE_STATE_STARTING_UP) {
			DWORD dwRes = BthSetSecurityUI (hAuthReq, BTH_SERVICE_STORE_TO, BTH_SERVICE_USER_TO);

			if (dwRes == ERROR_SERVICE_NOT_ACTIVE) {
				dwRes = WaitForSingleObject (ghServiceExitEvent, BTH_SERVICE_SLEEP);
				if (dwRes != WAIT_TIMEOUT)
					break;

				continue;
			}

			if (dwRes != ERROR_SUCCESS)
				break;

			gfServiceState = SERVICE_STATE_ON;

			StartServices ();
		}

		HANDLE ah[2];
		ah[0] = ghServiceExitEvent;
		ah[1] = hAuthReq;

		DWORD dwRes = WaitForMultipleObjects (2, ah, FALSE, INFINITE);
		if (dwRes == (WAIT_OBJECT_0 + 1)) {
			BT_ADDR bt;
			while (ERROR_SUCCESS == BthGetPINRequest (&bt)) {
				pin_request *p = (pin_request *)LocalAlloc (LMEM_FIXED, sizeof(pin_request));
				if (p) {
					p->bt = bt;
					p->hWnd = NULL;
					PostMessage (ghWndHidden, WM_USER + 1, 0, (LPARAM)p);
				}
			}

⌨️ 快捷键说明

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