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

📄 asui.cxx

📁 这些文件包括蓝牙虚拟串口与打印机程序实例
💻 CXX
📖 第 1 页 / 共 3 页
字号:
//
// 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.


Abstract:
	Windows CE Bluetooth application sample

**/
#include <windows.h>
#include <windev.h>
#include <stdio.h>

#include <winsock2.h>

#include <ras.h>
#include <notify.h>

#include <bt_api.h>
#include <bthapi.h>
#include <initguid.h>
#include <bt_sdp.h>


#include <svsutil.hxx>
#include "..\sdpcommon\sdpcommon.h"

#include "resource.h"

#define APPNAME		L"BluetoothASUI"

#define MAX_NAME	248
#define MAX_BA		20

#define DO_NOTHING	0
#define DO_INQUIRY	1
#define DO_NAMERES	2
#define DO_SDP		3
#define DO_STOP		4
#define DO_OPTIONS	5
#define DO_SYNC		6

#define DEFAULT_AS_PORT L"COM6:"
#define RAS_NAME_BLUETOOTH	L"`Bluetooth"

struct InquiryResult {
	InquiryResult	*pNext;

	BT_ADDR			b;					// Address of the item

	unsigned int	fHaveSDP  : 1;		// Do we have SDP record for it?

	unsigned char	channel;

	InquiryResult (void) {
		memset (this, 0, sizeof(*this));
	}
};

struct Global : public SVSSynch {
	int					fState;
	HINSTANCE			hInst;
	HWND				hWnd;
	HWND				hWndHidden;

	HANDLE				hDevice;

	InquiryResult		*pDev;

	Global (void) {
		fState = DO_NOTHING;
		hInst  = NULL;
		hWnd   = NULL;
		hWndHidden = NULL;
		pDev   = NULL;
		hDevice = NULL;
	}
};

static Global *g_pState = NULL;

int		g_fHaveSav = FALSE;

#define CHECK_STOP	\
	{													\
		if ((g_pState->fState == DO_STOP) ||			\
					(! g_pState->hWnd)) {				\
			g_pState->fState = DO_NOTHING;				\
			g_pState->Unlock ();						\
														\
			SetWindowText (hWndButton, L"Inquiry");		\
			SetWindowText (hWnd, L"Bluetooth ActiveSync");			\
														\
			return 0;									\
		}												\
	}

//
//	SDP parsing is better illustrated in sdp sample
//
static HRESULT ServiceAndAttributeSearch(
    UCHAR *szResponse,             // in - response returned from SDP ServiceAttribute query
    DWORD cbResponse,            // in - length of response
    ISdpRecord ***pppSdpRecords, // out - array of pSdpRecords
    ULONG *pNumRecords           // out - number of elements in pSdpRecords
    )
{
    *pppSdpRecords = NULL;
    *pNumRecords = 0;

    ISdpStream *pIStream = NULL;

    HRESULT hres = CoCreateInstance(__uuidof(SdpStream),NULL,CLSCTX_INPROC_SERVER,
                            __uuidof(ISdpStream),(LPVOID *) &pIStream);

    if (FAILED(hres))
        return hres;  

	ULONG ulError;

    hres = pIStream->Validate (szResponse,cbResponse,&ulError);

    if (SUCCEEDED(hres)) {
        hres = pIStream->VerifySequenceOf(szResponse,cbResponse,
                                          SDP_TYPE_SEQUENCE,NULL,pNumRecords);

        if (SUCCEEDED(hres) && *pNumRecords > 0) {
            *pppSdpRecords = (ISdpRecord **) CoTaskMemAlloc(sizeof(ISdpRecord*) * (*pNumRecords));

            if (pppSdpRecords != NULL) {
                hres = pIStream->RetrieveRecords(szResponse,cbResponse,*pppSdpRecords,pNumRecords);

                if (!SUCCEEDED(hres)) {
                    CoTaskMemFree(*pppSdpRecords);
                    *pppSdpRecords = NULL;
                    *pNumRecords = 0;
                }
            }
            else {
                hres = E_OUTOFMEMORY;
            }
        }
    }

    if (pIStream != NULL) {
        pIStream->Release();
        pIStream = NULL;
    }

	return hres;
}

static int IsRfcommUuid(NodeData *pNode)  {
	if (pNode->type != SDP_TYPE_UUID)
		return FALSE;

	if (pNode->specificType == SDP_ST_UUID16)
		return (pNode->u.uuid16 == RFCOMM_PROTOCOL_UUID16);
	else if (pNode->specificType == SDP_ST_UUID32)
		return (pNode->u.uuid32 == RFCOMM_PROTOCOL_UUID16);
	else if (pNode->specificType == SDP_ST_UUID128)
		return (0 == memcmp(&RFCOMM_PROTOCOL_UUID,&pNode->u.uuid128,sizeof(GUID)));

	return FALSE;
}

static int GetChannel (NodeData *pChannelNode) {
	if (pChannelNode->specificType == SDP_ST_UINT8)
		return pChannelNode->u.uint8;
	else if (pChannelNode->specificType == SDP_ST_INT8)
		return pChannelNode->u.int8;
	else if (pChannelNode->specificType == SDP_ST_UINT16)
		return pChannelNode->u.uint16;
	else if (pChannelNode->specificType == SDP_ST_INT16)
		return pChannelNode->u.int16;
	else if (pChannelNode->specificType == SDP_ST_UINT32)
		return pChannelNode->u.uint32;
	else if (pChannelNode->specificType == SDP_ST_INT32)
		return pChannelNode->u.int32;

	return 0;
}

static HRESULT FindRFCOMMChannel (unsigned char *pStream, int cStream, unsigned char *pChann) {
	ISdpRecord **pRecordArg;
	int cRecordArg = 0;

	*pChann = 0;

	HRESULT hr = ServiceAndAttributeSearch (pStream, cStream, &pRecordArg, (ULONG *)&cRecordArg);

	if (FAILED(hr))
		return hr;

	for (int i = 0; (! *pChann) && (i < cRecordArg); i++) {
	    ISdpRecord *pRecord = pRecordArg[i];    // particular record to examine in this loop
	    CNodeDataFreeString protocolList;     // contains SDP_ATTRIB_PROTOCOL_DESCRIPTOR_LIST data, if available

		if (ERROR_SUCCESS != pRecord->GetAttribute(SDP_ATTRIB_PROTOCOL_DESCRIPTOR_LIST,&protocolList) ||
		    (protocolList.type != SDP_TYPE_CONTAINER))
			continue;

		ISdpNodeContainer *pRecordContainer = protocolList.u.container;
		int cProtocols = 0;
		NodeData protocolDescriptor; // information about a specific protocol (i.e. L2CAP, RFCOMM, ...)

		pRecordContainer->GetNodeCount((DWORD *)&cProtocols);
		for (int j = 0; (! *pChann) && (j < cProtocols); j++) {
			pRecordContainer->GetNode(j,&protocolDescriptor);

			if (protocolDescriptor.type != SDP_TYPE_CONTAINER)
				continue;

			ISdpNodeContainer *pProtocolContainer = protocolDescriptor.u.container;
			int cProtocolAtoms = 0;
			pProtocolContainer->GetNodeCount((DWORD *)&cProtocolAtoms);

			for (int k = 0; (! *pChann) && (k < cProtocolAtoms); k++) {
				NodeData nodeAtom;  // individual data element, such as what protocol this is or RFCOMM channel id.

				pProtocolContainer->GetNode(k,&nodeAtom);

				if (IsRfcommUuid(&nodeAtom))  {
					if (k+1 == cProtocolAtoms) {
						// misformatted response.  Channel ID should follow RFCOMM uuid
						break;
					}

					NodeData channelID;
					pProtocolContainer->GetNode(k+1,&channelID);

					*pChann = (unsigned char)GetChannel(&channelID);
					break; // formatting error
				}
			}
		}
	}

    for (i = 0; i < cRecordArg; i++) 
		pRecordArg[i]->Release();

    CoTaskMemFree(pRecordArg);

	return (*pChann != 0) ? NO_ERROR : E_FAIL;
}

static void CleanInquiryData (void) {
	while (g_pState->pDev) {
		InquiryResult *pNext = g_pState->pDev->pNext;
		delete g_pState->pDev;
		g_pState->pDev = pNext;
	}
}

static DWORD PerformInquiry(HWND hWndDevList, int fDoNames, HANDLE *phLookup) {
	int iRet = ERROR_SUCCESS;

	WSAQUERYSET		wsaq;
	memset (&wsaq, 0, sizeof(wsaq));
	wsaq.dwSize      = sizeof(wsaq);
	wsaq.dwNameSpace = NS_BTH;
	wsaq.lpcsaBuffer = NULL;

	if (!fDoNames) {
		// perform initial device inquiry
		iRet = BthNsLookupServiceBegin (&wsaq, LUP_CONTAINERS, phLookup);
	} else {
		// reset iterator to front of list to find names
		DWORD dwUnused;
		iRet = BthNsLookupServiceNext (*phLookup, BTHNS_LUP_RESET_ITERATOR, &dwUnused, NULL);
	}
	
	if (iRet != ERROR_SUCCESS)
		return iRet;

	HANDLE hLookup = *phLookup;


	while ((iRet == ERROR_SUCCESS) && (g_pState->fState == DO_INQUIRY) && g_pState->hWnd) {
		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, (fDoNames ? LUP_RETURN_NAME : 0) | LUP_RETURN_ADDR, &dwSize, pwsaResults);

		if (iRet == ERROR_SUCCESS) {

			InquiryResult *pRes = NULL;
			g_pState->Lock();
	
			if (fDoNames) {
				pRes = g_pState->pDev;
				while (pRes && (pRes->b != ((SOCKADDR_BTH *)pwsaResults->lpcsaBuffer->RemoteAddr.lpSockaddr)->btAddr))
					pRes = pRes->pNext;
			} else {
				pRes = new InquiryResult;
				if (pRes) {
					pRes->b = ((SOCKADDR_BTH *)pwsaResults->lpcsaBuffer->RemoteAddr.lpSockaddr)->btAddr;
					pRes->pNext = g_pState->pDev;
					g_pState->pDev = pRes;
				}
			}
			g_pState->Unlock ();

			if (! pRes)
				continue;


			int fHaveName = fDoNames && (pwsaResults->lpszServiceInstanceName && *(pwsaResults->lpszServiceInstanceName));

			WCHAR szDevice[MAX_NAME+25];
			wsprintf (szDevice, L"%s%s%04x%08x%s", fHaveName ? pwsaResults->lpszServiceInstanceName : L"", 
				fHaveName ? L"(" : L"", GET_NAP(pRes->b), GET_SAP(pRes->b), fHaveName ? L")" : L"");

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

						if (iData == (int)pRes) {
							if (SendMessage(hWndDevList, LB_GETTEXTLEN, (WPARAM)i, (LPARAM)0) <= (MAX_NAME + 64)) {
								SendMessage (hWndDevList, LB_DELETESTRING, (WPARAM)i, (LPARAM)0);
								SendMessage (hWndDevList, LB_INSERTSTRING, (WPARAM)i, (LPARAM)szDevice);
								SendMessage (hWndDevList, LB_SETITEMDATA,  (WPARAM)i, (LPARAM)pRes);
							}

							break;
						}
					}
				}
			} else {
				int iNdx = SendMessage (hWndDevList, LB_ADDSTRING, 0, (LPARAM)szDevice);
				if (iNdx >= LB_OKAY)
					SendMessage (hWndDevList, LB_SETITEMDATA, (WPARAM)iNdx, (LPARAM)pRes);
			}
		} else	//BthNsLookupServiceNext returns SOCKET_ERROR and sets last error
			iRet = GetLastError();
	}

	return ((iRet == WSA_E_NO_MORE) ? ERROR_SUCCESS : iRet);
}

static int DoSDP (BT_ADDR *pb) {
	CoInitializeEx (0, COINIT_MULTITHREADED);

	int iResult = 0;

	BTHNS_RESTRICTIONBLOB RBlob;

	memset (&RBlob, 0, sizeof(RBlob));

	RBlob.type = SDP_SERVICE_SEARCH_ATTRIBUTE_REQUEST;
	RBlob.numRange = 1;
	RBlob.pRange[0].minAttribute = SDP_ATTRIB_PROTOCOL_DESCRIPTOR_LIST;
	RBlob.pRange[0].maxAttribute = SDP_ATTRIB_PROTOCOL_DESCRIPTOR_LIST;
	RBlob.uuids[0].uuidType = SDP_ST_UUID16;
	RBlob.uuids[0].u.uuid16 = SerialPortServiceClassID_UUID16;

	BLOB blob;
	blob.cbSize = sizeof(RBlob);
	blob.pBlobData = (BYTE *)&RBlob;

	SOCKADDR_BTH	sa;

	memset (&sa, 0, sizeof(sa));

	*(BT_ADDR *)(&sa.btAddr) = *pb;
	sa.addressFamily = AF_BT;

	CSADDR_INFO		csai;

	memset (&csai, 0, sizeof(csai));
	csai.RemoteAddr.lpSockaddr = (sockaddr *)&sa;
	csai.RemoteAddr.iSockaddrLength = sizeof(sa);

	WSAQUERYSET		wsaq;

	memset (&wsaq, 0, sizeof(wsaq));
	wsaq.dwSize      = sizeof(wsaq);
	wsaq.dwNameSpace = NS_BTH;
	wsaq.lpBlob      = &blob;
	wsaq.lpcsaBuffer = &csai;

	HANDLE hLookup;
	int iRet = BthNsLookupServiceBegin (&wsaq, 0, &hLookup);

	if (ERROR_SUCCESS == iRet) {
		CHAR buf[5000];
		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 ();

⌨️ 快捷键说明

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