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

📄 btdrt.cxx

📁 WinCE5.0部分核心源码
💻 CXX
📖 第 1 页 / 共 2 页
字号:
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// This source code is licensed under Microsoft Shared Source License
// Version 1.0 for Windows CE.
// For a copy of the license visit http://go.microsoft.com/fwlink/?LinkId=3223.
//
//------------------------------------------------------------------------------
// 
//      Bluetooth Client API Layer
// 
// 
// Module Name:
// 
//      btdrt.cxx
// 
// Abstract:
// 
//      This file implements Bluetooth Client API Layer
// 
// 
//------------------------------------------------------------------------------
#include <windows.h>
#include <svsutil.hxx>
#include <bt_buffer.h>
#include <bt_ddi.h>
#include <winsock2.h>
#include <bt_api.h>
#include <bt_ioctl.h>
#include <service.h>

extern "C" const BOOL g_fBthApiCOM;


HANDLE hDev = NULL;

int Initialize (void) {
	if (hDev)
		return TRUE;

	hDev = CreateFile (L"BTD0:", GENERIC_READ | GENERIC_WRITE,
									FILE_SHARE_READ | FILE_SHARE_WRITE,
									NULL, OPEN_EXISTING, 0, NULL);

	return (hDev != INVALID_HANDLE_VALUE) ? TRUE : FALSE;
}

int BthGetCurrentMode
(
BT_ADDR			*pbt,
unsigned char	*pmode
) {
	if (! Initialize())
		return ERROR_SERVICE_NOT_ACTIVE;

	BTAPICALL bc;
	memset (&bc, 0, sizeof(bc));

	bc.BthCurrentMode_p.bt = *pbt;

	int iRes = ((DeviceIoControl (hDev, BT_IOCTL_BthGetCurrentMode, &bc, sizeof(bc), NULL, NULL, NULL, NULL)) ? ERROR_SUCCESS : GetLastError());
	if (iRes == ERROR_SUCCESS)
		*pmode = bc.BthCurrentMode_p.mode;

	return iRes;
}

int BthGetBasebandHandles
(
int				cHandles,
unsigned short	*pHandles,
int				*pcHandlesReturned
) {
	if (! Initialize())
		return ERROR_SERVICE_NOT_ACTIVE;

	BTAPICALL bc;
	memset (&bc, 0, sizeof(bc));

	bc.BthBasebandHandles_p.cMaxHandles = cHandles;
	bc.BthBasebandHandles_p.pHandles = pHandles;

	int iRes = ((DeviceIoControl (hDev, BT_IOCTL_BthGetBasebandHandles, &bc, sizeof(bc), NULL, NULL, NULL, NULL)) ? ERROR_SUCCESS : GetLastError());

	*pcHandlesReturned = bc.BthBasebandHandles_p.cHandlesReturned;

	return iRes;
}

int BthGetBasebandConnections
(
int						cConnections,
BASEBAND_CONNECTION		*pConnections,
int						*pcConnectionsReturned
) {
	if (! Initialize())
		return ERROR_SERVICE_NOT_ACTIVE;

	BTAPICALL bc;
	memset (&bc, 0, sizeof(bc));

	BASEBAND_CONNECTION_DATA buffer[15];
	BASEBAND_CONNECTION_DATA *pConnData = buffer;

	if (cConnections > 15) {
		pConnData = new BASEBAND_CONNECTION_DATA[cConnections];
		if (! pConnData)
			return ERROR_OUTOFMEMORY;
	}

	bc.BthBasebandConnections_p.cMaxConnections = cConnections;
	bc.BthBasebandConnections_p.pConnections = pConnData;

	int iRes = ((DeviceIoControl (hDev, BT_IOCTL_BthGetBasebandConnections, &bc, sizeof(bc), NULL, NULL, NULL, NULL)) ? ERROR_SUCCESS : GetLastError());

	*pcConnectionsReturned = bc.BthBasebandConnections_p.cConnectionsReturned;

	if (iRes == ERROR_SUCCESS) {
		if (*pcConnectionsReturned > cConnections) {
			if (pConnData != buffer)
				delete[] pConnData;
			return ERROR_INSUFFICIENT_BUFFER;
		}

		// Copy back to a BASEBAND_CONNECTION structure
		for (int i = 0; i < *pcConnectionsReturned; i++) {
			pConnections[i].baAddress =  SET_NAP_SAP(pConnData[i].baAddress.NAP, pConnData[i].baAddress.SAP);
			pConnections[i].cDataPacketsPending = pConnData[i].cDataPacketsPending;
			pConnections[i].fAuthenticated = pConnData[i].fAuthenticated;
			pConnections[i].fEncrypted = pConnData[i].fEncrypted;
			pConnections[i].fLinkType = pConnData[i].fLinkType;
			pConnections[i].fMode = pConnData[i].fMode;
			pConnections[i].hConnection = pConnData[i].hConnection;
		}
	}

	if (pConnData != buffer)
		delete[] pConnData;

	return iRes;
}

int BthGetAddress
(
unsigned short	handle,
BT_ADDR			*pba
) {
	if (! Initialize())
		return ERROR_SERVICE_NOT_ACTIVE;

	BTAPICALL bc;
	memset (&bc, 0, sizeof(bc));

	bc.BthGetAddress_p.handle = handle;

	int iRes = ((DeviceIoControl (hDev, BT_IOCTL_BthGetAddress, &bc, sizeof(bc), NULL, NULL, NULL, NULL)) ? ERROR_SUCCESS : GetLastError());
	if (iRes == ERROR_SUCCESS)
		*pba = bc.BthGetAddress_p.ba;

	return iRes;
}

int BthWriteScanEnableMask
(
unsigned char	mask
) {
	if (! Initialize())
		return ERROR_SERVICE_NOT_ACTIVE;

	BTAPICALL bc;
	memset (&bc, 0, sizeof(bc));

	bc.BthScanEnableMask_p.mask = mask;

	return ((DeviceIoControl (hDev, BT_IOCTL_BthWriteScanEnableMask, &bc, sizeof(bc), NULL, NULL, NULL, NULL)) ? ERROR_SUCCESS : GetLastError());
}

int BthReadScanEnableMask
(
unsigned char	*pmask
) {
	if (! Initialize())
		return ERROR_SERVICE_NOT_ACTIVE;

	BTAPICALL bc;
	memset (&bc, 0, sizeof(bc));

	if (DeviceIoControl (hDev, BT_IOCTL_BthReadScanEnableMask, &bc, sizeof(bc), NULL, NULL, NULL, NULL)) {
		*pmask = bc.BthScanEnableMask_p.mask;
		return ERROR_SUCCESS;
	}

	return GetLastError ();
}

int BthWriteAuthenticationEnable
(
unsigned char	ae
) {
	if (! Initialize())
		return ERROR_SERVICE_NOT_ACTIVE;

	BTAPICALL bc;
	memset (&bc, 0, sizeof(bc));

	bc.BthAuthenticationEnable_p.ae = ae;

	return ((DeviceIoControl (hDev, BT_IOCTL_BthWriteAuthenticationEnable, &bc, sizeof(bc), NULL, NULL, NULL, NULL)) ? ERROR_SUCCESS : GetLastError());
}

int BthReadAuthenticationEnable
(
unsigned char	*pae
) {
	if (! Initialize())
		return ERROR_SERVICE_NOT_ACTIVE;

	BTAPICALL bc;
	memset (&bc, 0, sizeof(bc));

	if (DeviceIoControl (hDev, BT_IOCTL_BthReadAuthenticationEnable, &bc, sizeof(bc), NULL, NULL, NULL, NULL)) {
		*pae = bc.BthAuthenticationEnable_p.ae;
		return ERROR_SUCCESS;
	}

	return GetLastError ();
}

int BthWriteLinkPolicySettings
(
BT_ADDR			*pbt,
unsigned short	lps
) {
	if (! Initialize())
		return ERROR_SERVICE_NOT_ACTIVE;

	BTAPICALL bc;
	memset (&bc, 0, sizeof(bc));

	bc.BthLinkPolicySettings_p.bt = *pbt;
	bc.BthLinkPolicySettings_p.lps = lps;

	return ((DeviceIoControl (hDev, BT_IOCTL_BthWriteLinkPolicySettings, &bc, sizeof(bc), NULL, NULL, NULL, NULL)) ? ERROR_SUCCESS : GetLastError());
}

int BthReadLinkPolicySettings
(
BT_ADDR			*pbt,
unsigned short	*plps
) {
	if (! Initialize())
		return ERROR_SERVICE_NOT_ACTIVE;

	BTAPICALL bc;
	memset (&bc, 0, sizeof(bc));

	bc.BthLinkPolicySettings_p.bt = *pbt;

	if (DeviceIoControl (hDev, BT_IOCTL_BthReadLinkPolicySettings, &bc, sizeof(bc), NULL, NULL, NULL, NULL)) {
		*plps = bc.BthLinkPolicySettings_p.lps;
		return ERROR_SUCCESS;
	}

	return GetLastError ();
}

int BthEnterHoldMode
(
BT_ADDR			*pbt,
unsigned short	hold_mode_max,
unsigned short	hold_mode_min,
unsigned short	*pinterval
) {
	if (! Initialize())
		return ERROR_SERVICE_NOT_ACTIVE;

	BTAPICALL bc;
	memset (&bc, 0, sizeof(bc));

	bc.BthHold_p.ba = *pbt;
	bc.BthHold_p.hold_mode_max = hold_mode_max;
	bc.BthHold_p.hold_mode_min = hold_mode_min;

	int iRes = ((DeviceIoControl (hDev, BT_IOCTL_BthEnterHoldMode, &bc, sizeof(bc), NULL, NULL, NULL, NULL)) ? ERROR_SUCCESS : GetLastError());
	if ((iRes == ERROR_SUCCESS) && pinterval)
		*pinterval = bc.BthHold_p.interval;

	return iRes;
}

int BthEnterSniffMode
(
BT_ADDR			*pbt,
unsigned short	sniff_mode_max,
unsigned short	sniff_mode_min,
unsigned short	sniff_attempt,
unsigned short	sniff_timeout,
unsigned short	*pinterval
) {
	if (! Initialize())
		return ERROR_SERVICE_NOT_ACTIVE;

	BTAPICALL bc;
	memset (&bc, 0, sizeof(bc));

	bc.BthSniff_p.ba = *pbt;
	bc.BthSniff_p.sniff_mode_max = sniff_mode_max;
	bc.BthSniff_p.sniff_mode_min = sniff_mode_min;
	bc.BthSniff_p.sniff_attempt = sniff_attempt;
	bc.BthSniff_p.sniff_timeout = sniff_timeout;

	int iRes = ((DeviceIoControl (hDev, BT_IOCTL_BthEnterSniffMode, &bc, sizeof(bc), NULL, NULL, NULL, NULL)) ? ERROR_SUCCESS : GetLastError());

	if ((iRes == ERROR_SUCCESS) && pinterval)
		*pinterval = bc.BthSniff_p.interval;

	return iRes;
}


int BthExitSniffMode
(
BT_ADDR			*pbt
) {
	if (! Initialize())
		return ERROR_SERVICE_NOT_ACTIVE;

	BTAPICALL bc;
	memset (&bc, 0, sizeof(bc));

	bc.BthSniff_p.ba = *pbt;

	return ((DeviceIoControl (hDev, BT_IOCTL_BthExitSniffMode, &bc, sizeof(bc), NULL, NULL, NULL, NULL)) ? ERROR_SUCCESS : GetLastError());
}

int BthEnterParkMode
(
BT_ADDR			*pbt,
unsigned short beacon_max,
unsigned short beacon_min,
unsigned short	*pinterval
) {
	if (! Initialize())
		return ERROR_SERVICE_NOT_ACTIVE;

	BTAPICALL bc;
	memset (&bc, 0, sizeof(bc));

	bc.BthPark_p.ba = *pbt;
	bc.BthPark_p.beacon_max = beacon_max;
	bc.BthPark_p.beacon_min = beacon_min;

	int  iRes = ((DeviceIoControl (hDev, BT_IOCTL_BthEnterParkMode, &bc, sizeof(bc), NULL, NULL, NULL, NULL)) ? ERROR_SUCCESS : GetLastError());

	if ((iRes == ERROR_SUCCESS) && pinterval)
		*pinterval = bc.BthPark_p.interval;

	return iRes;
}

int BthExitParkMode
(
BT_ADDR			*pba
) {
	if (! Initialize())
		return ERROR_SERVICE_NOT_ACTIVE;

	BTAPICALL bc;
	memset (&bc, 0, sizeof(bc));

	bc.BthPark_p.ba = *pba;

	return ((DeviceIoControl (hDev, BT_IOCTL_BthExitParkMode, &bc, sizeof(bc), NULL, NULL, NULL, NULL)) ? ERROR_SUCCESS : GetLastError());
}


int BthWritePageTimeout
(
unsigned short	timeout
) {
	if (! Initialize())
		return ERROR_SERVICE_NOT_ACTIVE;

	BTAPICALL bc;
	memset (&bc, 0, sizeof(bc));

	bc.BthPageTimeout_p.timeout = timeout;

	return ((DeviceIoControl (hDev, BT_IOCTL_BthWritePageTimeout, &bc, sizeof(bc), NULL, NULL, NULL, NULL)) ? ERROR_SUCCESS : GetLastError());
}

int BthReadPageTimeout
(
unsigned short	*ptimeout
) {
	if (! Initialize())
		return ERROR_SERVICE_NOT_ACTIVE;

	BTAPICALL bc;
	memset (&bc, 0, sizeof(bc));

	if (DeviceIoControl (hDev, BT_IOCTL_BthReadPageTimeout, &bc, sizeof(bc), NULL, NULL, NULL, NULL)) {
		*ptimeout = bc.BthPageTimeout_p.timeout;
		return ERROR_SUCCESS;
	}

	return GetLastError ();
}

int BthWriteCOD
(
unsigned int	cod
) {
	if (! Initialize())
		return ERROR_SERVICE_NOT_ACTIVE;

	BTAPICALL bc;
	memset (&bc, 0, sizeof(bc));

	bc.BthCOD_p.cod = cod;

	return ((DeviceIoControl (hDev, BT_IOCTL_BthWriteCOD, &bc, sizeof(bc), NULL, NULL, NULL, NULL)) ? ERROR_SUCCESS : GetLastError());
}

int BthReadCOD
(
unsigned int	*pcod
) {
	if (! Initialize())
		return ERROR_SERVICE_NOT_ACTIVE;

	BTAPICALL bc;
	memset (&bc, 0, sizeof(bc));

	if (DeviceIoControl (hDev, BT_IOCTL_BthReadCOD, &bc, sizeof(bc), NULL, NULL, NULL, NULL)) {
		*pcod = bc.BthCOD_p.cod;
		return ERROR_SUCCESS;
	}

	return GetLastError ();
}

int BthGetRemoteCOD
(
BT_ADDR* pbt,
unsigned int* pcod
) {
	if (! Initialize())
		return ERROR_SERVICE_NOT_ACTIVE;

	BTAPICALL bc;
	memset (&bc, 0, sizeof(bc));

	bc.BthRemoteCOD_p.ba = *pbt;
	
	if (DeviceIoControl (hDev, BT_IOCTL_BthGetRemoteCOD, &bc, sizeof(bc), NULL, NULL, NULL, NULL)) {
		*pcod = bc.BthRemoteCOD_p.cod;
		return ERROR_SUCCESS;
	}

	return GetLastError ();
}

int BthReadLocalVersion
(
unsigned char	*phci_version,
unsigned short	*phci_revision,
unsigned char	*plmp_version,
unsigned short	*plmp_subversion,
unsigned short	*pmanufacturer,
unsigned char	*plmp_features
) {
	if (! Initialize())
		return ERROR_SERVICE_NOT_ACTIVE;

	BTAPICALL bc;
	memset (&bc, 0, sizeof(bc));

	if (DeviceIoControl (hDev, BT_IOCTL_BthReadLocalVersion, &bc, sizeof(bc), NULL, NULL, NULL, NULL)) {
		*phci_version = bc.BthReadVersion_p.hci_version;
		*phci_revision = bc.BthReadVersion_p.hci_revision;
		*plmp_version = bc.BthReadVersion_p.lmp_version;
		*plmp_subversion = bc.BthReadVersion_p.lmp_subversion;
		*pmanufacturer = bc.BthReadVersion_p.manufacturer_name;
		memcpy (plmp_features, bc.BthReadVersion_p.lmp_features, 8);

		return ERROR_SUCCESS;
	}

	return GetLastError ();
}

int BthReadRemoteVersion
(
BT_ADDR			*pba,
unsigned char	*plmp_version,
unsigned short	*plmp_subversion,
unsigned short	*pmanufacturer,
unsigned char	*plmp_features
) {
	if (! Initialize())
		return ERROR_SERVICE_NOT_ACTIVE;

	BTAPICALL bc;
	memset (&bc, 0, sizeof(bc));

	bc.BthReadVersion_p.bt = *pba;

	if (DeviceIoControl (hDev, BT_IOCTL_BthReadRemoteVersion, &bc, sizeof(bc), NULL, NULL, NULL, NULL)) {
		*plmp_version = bc.BthReadVersion_p.lmp_version;
		*plmp_subversion = bc.BthReadVersion_p.lmp_subversion;
		*pmanufacturer = bc.BthReadVersion_p.manufacturer_name;
		memcpy (plmp_features, bc.BthReadVersion_p.lmp_features, 8);

		return ERROR_SUCCESS;
	}

	return GetLastError ();
}


int BthPerformInquiry
(
unsigned int	LAP,
unsigned char	length,
unsigned char	num_responses,
unsigned int	cBuffer,
unsigned int	*pcDiscoveredDevices,
BthInquiryResult*inquiry_list
) {
	if (! Initialize())
		return ERROR_SERVICE_NOT_ACTIVE;

	BTAPICALL bc;
	memset (&bc, 0, sizeof(bc));

	bc.BthPerformInquiry_p.LAP = LAP;
	bc.BthPerformInquiry_p.length = length;
	bc.BthPerformInquiry_p.num_responses = num_responses;
	bc.BthPerformInquiry_p.cBuffer = cBuffer;
	bc.BthPerformInquiry_p.pcDiscoveredDevices = pcDiscoveredDevices;
	bc.BthPerformInquiry_p.inquiry_list = inquiry_list;

	return ((DeviceIoControl (hDev, BT_IOCTL_BthPerformInquiry, &bc, sizeof(bc), NULL, NULL, NULL, NULL)) ? ERROR_SUCCESS : GetLastError());

⌨️ 快捷键说明

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