www.pudn.com > BlueSoleil_SDK1.0.0.zip > rmt_dev_tst.c


/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
* 
* Copyright (c) 1999-2005 IVT Corporation 
* 
* All rights reserved. 
* 
---------------------------------------------------------------------------*/ 
  
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
Module Name: 
    rmt_dev_tst.c 
Abstract: 
	Sample codes of remote device manager 
 
Revision History: 
2007-6-7   Guan Tengfei Created 
 
---------------------------------------------------------------------------*/ 
 
#include "sdk_tst.h" 
 
/*remote device handle through device discovery*/ 
BTDEVHDL s_rmt_dev_hdls[MAX_DEV_NUM] = {0}; 
/*remote devices number through device discovery*/ 
BTINT32 s_rmt_dev_num = 0;  
/*remote device's class.*/ 
BTUINT32 s_rmt_dev_cls = 0; 
/*current used remote device handle*/ 
static BTDEVHDL s_curr_dev = BTSDK_INVALID_HANDLE; 
/*event to sync device discovery*/ 
static HANDLE s_hBrowseDevEventHdl = NULL; 
 
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
Description: 
    This function is to map a string between two different code pages.  
	Help function, all arguments refer to Windows API               
Arguments: 
Return: 
        0 if two code pages are same or pointer to source string is NULL. 
		The number of characters is translated.		  
--------------------------------------------------------------------------*/ 
int MultibyteToMultibyte(BTUINT32 dwSrcCodePage, char *lpSrcStr, int cbSrcStr, 
						 BTUINT32 dwDestCodePage, char *lpDestStr, int cbDestStr) 
{ 
	short *wzTemp = NULL; 
	int nSrcLen = 0; 
 
	if (dwSrcCodePage == dwDestCodePage) 
	{ 
		return 0; 
	} 
 
	if (lpSrcStr == NULL) 
	{ 
		return 0; 
	} 
 
	if (-1 == cbSrcStr) 
	{ 
		nSrcLen = strlen(lpSrcStr) + 1; 
	} 
	else 
	{ 
		nSrcLen = cbSrcStr; 
	} 
 
	if ((NULL == lpDestStr) || (0 == cbDestStr)) 
	{ 
		return nSrcLen; 
	} 
 
	wzTemp = (short*) malloc(nSrcLen * sizeof(short)); 
	if (NULL == wzTemp) 
	{ 
		return 0; 
	} 
	memset(wzTemp, 0, nSrcLen * sizeof(short)); 
	MultiByteToWideChar(dwSrcCodePage, 0, lpSrcStr, cbSrcStr, wzTemp, nSrcLen); 
	nSrcLen = WideCharToMultiByte(dwDestCodePage, 0, wzTemp, -1, lpDestStr, cbDestStr, NULL, FALSE); 
 
	free(wzTemp); 
	wzTemp = NULL; 
	 
	return nSrcLen; 
} 
 
 
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
Description: 
	This function prints bluetooth address 
Arguments: 
	BTUINT8 *bd_addr: [in] bluetooth address 
Return: 
	void  
---------------------------------------------------------------------------*/ 
void PrintBdAddr(BTUINT8 *bd_addr) 
{	 
	int j; 
 
	if (NULL == bd_addr) 
	{ 
		return; 
	} 
 
	for (j = 5; j > 0; j--) 
	{ 
		printf("%02X:", bd_addr[j]); 
	} 
	printf("%02X", bd_addr[0]); 
} 
	 
	 
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
Description: 
	This function is to report each device discovered separately 
Arguments: 
	BTDEVHDL dev_hdl: [in] device handle 
Return: 
	void  
---------------------------------------------------------------------------*/ 
void AppInquiryInd(BTDEVHDL dev_hdl) 
{ 
	BTUINT32 dev_class = 0; 
	Btsdk_GetRemoteDeviceClass(dev_hdl, &dev_class); 
	/*Just store devices of specified device class.*/ 
	if ( (s_rmt_dev_cls == 0)||(s_rmt_dev_cls == BTSDK_DEVCLS_MASK(dev_class & DEVICE_CLASS_MASK))) 
	{ 
		s_rmt_dev_hdls[s_rmt_dev_num++] = dev_hdl; 
	}	 
} 
 
 
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
Description: 
	This function is the callback function to process inquiry complete result ind 
Arguments: 
	void 
Return: 
	void  
---------------------------------------------------------------------------*/ 
void AppInqCompInd(void) 
{ 
	/*notify the thread which starts device discovery*/ 
	s_hBrowseDevEventHdl = OpenEvent(EVENT_ALL_ACCESS, FALSE, "completeBrowseDevice"); 
	SetEvent(s_hBrowseDevEventHdl); 
} 
 
 
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
Description: 
	This function is the callback function for connection complete event 
Arguments: 
	BTDEVHDL dev_hdl: [in] device handle 
Return: 
	void  
---------------------------------------------------------------------------*/ 
void AppConnCompleteInd(BTDEVHDL dev_hdl) 
{ 
	BTUINT32 dev_class = 0; 
	Btsdk_GetRemoteDeviceClass(dev_hdl, &dev_class); 
} 
 
 
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
Description: 
	This function is the callback funciton for connection event 
Arguments: 
	BTCONNHDL conn_hdl: [in] connection handle 
	BTUINT16 event: [in] event 
	BTUINT8 *arg: [in] reserved 
Return: 
	void  
---------------------------------------------------------------------------*/ 
void AppConnEventInd(BTCONNHDL conn_hdl, BTUINT16 event, BTUINT8 *arg) 
{ 
	PBtSdkConnectionPropertyStru prop = (PBtSdkConnectionPropertyStru)arg; 
	BTUINT8 bd_addr[6] = {0}; 
	BTUINT8 dev_name[120] = {0}; 
 
	if (prop != NULL) 
	{ 
		Btsdk_GetRemoteDeviceAddress(prop->device_handle, bd_addr); 
		Btsdk_GetRemoteDeviceName(prop->device_handle, dev_name, NULL); 
	} 
	switch (event) 
	{ 
	case BTSDK_APP_EV_CONN_IND: 
		printf("Receive BTSDK_APP_EV_CONN_IND from %s\n", dev_name); 
		break; 
	case BTSDK_APP_EV_CONN_CFM: 
		printf("Receive BTSDK_APP_EV_CONN_CFM to %s\n", dev_name); 
		break; 
	case BTSDK_APP_EV_DISC_IND: 
		printf("Receive BTSDK_APP_EV_DISC_IND from %s\n", dev_name); 
		break; 
	case BTSDK_APP_EV_DISC_CFM: 
		printf("Receive BTSDK_APP_EV_DISC_CFM to %s\n", dev_name); 
		break; 
	default: 
		printf("Receive unknown message %04x\n", event); 
		break; 
	} 
} 
 
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
Description: 
	This function registers callbacks. 
Arguments: 
	void 
Return: 
	void  
---------------------------------------------------------------------------*/ 
void RegAppIndCallback(void) 
{ 
	BtSdkCallbackStru cb = {0}; 
 
	/* inquiry result ind*/ 
	cb.type = BTSDK_INQUIRY_RESULT_IND; 
	cb.func = (void*)AppInquiryInd; 
	Btsdk_RegisterCallback4ThirdParty(&cb); 
 
	/* inquiry complete result ind*/ 
	cb.type = BTSDK_INQUIRY_COMPLETE_IND; 
	cb.func = (void*)AppInqCompInd; 
	Btsdk_RegisterCallback4ThirdParty(&cb); 
 
	/* connection event ind*/ 
	cb.type = BTSDK_CONNECTION_EVENT_IND; 
	cb.func = (void*)AppConnEventInd; 
	Btsdk_RegisterCallback4ThirdParty(&cb); 
} 
 
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
Description: 
	This function unregisters callbacks. 
Arguments: 
	void 
Return: 
	void  
---------------------------------------------------------------------------*/ 
void UnRegAppIndCallback(void) 
{ 
	BtSdkCallbackStru cb; 
 
	cb.type = BTSDK_INQUIRY_RESULT_IND; 
	cb.func = NULL; 
	Btsdk_RegisterCallback4ThirdParty(&cb); 
	cb.type = BTSDK_INQUIRY_COMPLETE_IND; 
	cb.func = NULL;	 
	Btsdk_RegisterCallback4ThirdParty(&cb); 
	cb.type = BTSDK_CONNECTION_EVENT_IND; 
	cb.func = NULL; 
	Btsdk_RegisterCallback4ThirdParty(&cb); 
} 
 
 
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
Description: 
	This function is to display remote devices 
Arguments: 
	BTUINT32 dev_class: [in] device class 
Return: 
	void  
---------------------------------------------------------------------------*/ 
void DisplayRemoteDevices(BTUINT32 dev_class) 
{ 
	int i = 0; 
	int j = 0; 
	BTUINT8 szDevName[BTSDK_DEVNAME_LEN] = { 0 }; 
	BTUINT8 szTmp[32] = { 0 }; 
	BTUINT16 usLen = 0; 
	BTUINT32 ulDevClass = 0; 
	BTUINT8 szBdAddr[BD_ADDR_LEN] = {0}; 
	char cQuote = ' '; 
 
	printf("Remote devices searched:\n"); 
	printf("number  device name %21hc device address %4hc device class\n", cQuote, cQuote); 
 
	for (i = 0; i < s_rmt_dev_num; i++) /* s_rmt_dev_num is get by inquiry callback */ 
	{ 
		Btsdk_GetRemoteDeviceClass(s_rmt_dev_hdls[i], &ulDevClass); 
		if ((dev_class != 0) && (dev_class != BTSDK_DEVCLS_MASK(ulDevClass & DEVICE_CLASS_MASK))) 
		{			 
			for (j=i; j 0; j --) 
		{ 
			printf("%02X:", szBdAddr[j]); 
		} 
		printf("%02X%3hc", szBdAddr[0], cQuote); 
 
		Btsdk_GetRemoteDeviceClass(s_rmt_dev_hdls[i], &ulDevClass); 
		printf("0X%08X\r\n", ulDevClass); 
	} 
} 
 
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
Description: 
	This function is to get stored handles of remote device. 
Arguments: 
Return: 
	void  
---------------------------------------------------------------------------*/ 
void GetAllRmtDevHdl() 
{ 
	BTSDKHANDLE hEnumHdl = BTSDK_INVALID_HANDLE; 
	INT i = 0; 
	s_rmt_dev_num = 0; 
	memset(s_rmt_dev_hdls, 0, sizeof(s_rmt_dev_hdls)); 
 
	hEnumHdl = Btsdk_StartEnumRemoteDevice(BTSDK_ERD_FLAG_NOLIMIT, 0); 
	if (BTSDK_INVALID_HANDLE != hEnumHdl) 
	{ 
		for (i = 0; i < MAX_DEV_NUM; i++) 
		{ 
			s_rmt_dev_hdls[i] = Btsdk_EnumRemoteDevice(hEnumHdl, NULL); 
			if (BTSDK_INVALID_HANDLE == s_rmt_dev_hdls[i]) 
			{ 
				s_rmt_dev_hdls[i] = BTSDK_INVALID_HANDLE; 
				break; 
			} 
			s_rmt_dev_num++; 
		} 
		Btsdk_EndEnumRemoteDevice(hEnumHdl); 
		hEnumHdl = BTSDK_INVALID_HANDLE; 
	} 
 
} 
 
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
Description: 
	This function is to select device you want. 
Arguments: 
	BTUINT32 dev_class: [in] device class 
Return: 
	BTDEVHDL  
---------------------------------------------------------------------------*/ 
BTDEVHDL SelectRemoteDevice(BTUINT32 dev_class) 
{ 
	int nIdx = 0; 
	char szChoice[4] = {0}; 
 
	GetAllRmtDevHdl(); 
	/*show remote devices. If there is no device shown, search for them at first*/ 
	if (0 == s_rmt_dev_num) 
	{ 
		StartSearchDevice(dev_class); 
	} 
	else 
	{ 
		DisplayRemoteDevices(dev_class); 
	} 
	 
	printf("Select the target device :\n");  
	printf("if there is no expected device, please press 'a' to search again!\n"); 
	printf("if you want to exit this procedure, please press 'q' to quit.\n"); 
	 
	do 
	{ 
		printf("Target device number = "); 
		scanf(" %s", szChoice); 
		getchar(); 
		if ('a' == szChoice[0]) 
		{ 
			StartSearchDevice(dev_class);		 
			continue; 
		} 
		if(('q' == szChoice[0]) || ('Q' == szChoice[0])) 
		{ 
			printf("\nUser abort the operation.\n"); 
			return BTSDK_INVALID_HANDLE; 
 		} 
		nIdx = atoi(szChoice); 
		if((nIdx <= 0) || (nIdx > s_rmt_dev_num)) 
		{ 
			printf("%d is not a valid datum, please select again.\n", nIdx); 
			continue; 
		} 
		else 
		{ 
			break; 
		} 
	} while (1); 
 
	return (s_rmt_dev_hdls[nIdx - 1]); 
} 
 
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
Description: 
	This function searches for devices 
Arguments: 
	BTUINT32 device_class: [in] device class 
Return: 
	void  
---------------------------------------------------------------------------*/ 
void StartSearchDevice(BTUINT32 device_class) 
{ 
	/*init remote devices info discovered.remote device handle and number will be set in AppInquiryInd*/ 
	memset(s_rmt_dev_hdls, 0, sizeof(s_rmt_dev_hdls)); 
	s_rmt_dev_num = 0; 
	s_rmt_dev_cls = device_class; 
 
	s_hBrowseDevEventHdl = CreateEvent(NULL, FALSE, FALSE, "completeBrowseDevice"); 
	printf("Please wait for a while searching for remote devices......\n"); 
	 
	if (BTSDK_OK == Btsdk_StartDeviceDiscovery(0, MAX_DEV_NUM, 45)) 
	{ 
		/*wait BTSDK_INQUIRY_COMPLETE_IND. When complete inquiry, AppInqCompInd will notify*/ 
		WaitForSingleObject(s_hBrowseDevEventHdl, INFINITE);		 
		 
		DisplayRemoteDevices(0); 
		printf("Search for remote devices completely.\n"); 
	} 
	else 
	{ 
		printf("Fail to initiate device searching!\n"); 
	} 
 
	if (NULL != s_hBrowseDevEventHdl) 
	{ 
		CloseHandle(s_hBrowseDevEventHdl); 
		s_hBrowseDevEventHdl = NULL; 
	} 
} 
 
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
Description: 
	This function prints basic information of remote device 
Arguments: 
	BTDEVHDL dev_hdl: [in] device handle 
Return: 
	void  
---------------------------------------------------------------------------*/ 
void Test_Print_Remote_Basic_Info(BTDEVHDL dev_hdl) 
{ 
	int j = 0; 
	BTUINT32 ulResult = BTSDK_OK; 
	BTUINT8 szDevName[BTSDK_DEVNAME_LEN] = { 0 }; 
	BtSdkRemoteDevicePropertyStru struDevProperty = { 0 }; 
 
	ulResult = Btsdk_GetRemoteDeviceProperty(dev_hdl, &struDevProperty); 
	PrintErrorMessage(ulResult, BTSDK_TRUE); 
	if(BTSDK_OK == ulResult) 
	{ 
		MultibyteToMultibyte(CP_UTF8, struDevProperty.name, -1, CP_ACP, szDevName, BTSDK_DEVNAME_LEN); 
		printf("The basic information of the selected device is :\n"); 
		printf("Name = \"%s\"\n", szDevName); 
		printf("BD Addr: "); 
		for(j = 5; j > 0; j--) 
		{ 
			printf("%02X:", struDevProperty.bd_addr[j]); 
		} 
		printf("%02X\n", struDevProperty.bd_addr[0]); 
		printf("Device Class: %08lX\n", struDevProperty.dev_class);		 
	} 
} 
 
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
Description: 
	This function pairs a remote device 
Arguments: 
	BTDEVHDL dev_hdl: [in] device handle 
Return: 
	void  
---------------------------------------------------------------------------*/ 
void Test_Btsdk_PairDevice(BTDEVHDL dev_hdl) 
{ 
	BTUINT32 ulResult = BTSDK_OK; 
	printf("Pairing...\n"); 
	ulResult = Btsdk_PairDevice(dev_hdl); 
	if (BTSDK_OK == ulResult) 
	{ 
		printf("Succeed in pairing with the remote device.\n"); 
	} 
	else 
	{ 
		PrintErrorMessage(ulResult,  BTSDK_TRUE); 
	} 
} 
 
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
Description: 
	This function unpairs a remote device 
Arguments: 
	BTDEVHDL dev_hdl: [in] device handle 
Return: 
	void  
---------------------------------------------------------------------------*/ 
void Test_Btsdk_UnPairDevice(BTDEVHDL dev_hdl) 
{ 
	BTUINT32 ulResult = BTSDK_OK; 
	printf("Unpairing with the remote device...\n"); 
	ulResult = Btsdk_UnPairDevice(dev_hdl); 
	if (BTSDK_OK == ulResult) 
	{ 
		printf("Unpair with the remote device successfully.\n"); 
	} 
	else 
	{ 
		PrintErrorMessage(ulResult,  BTSDK_TRUE); 
	} 
} 
 
 
 
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
Description: 
	This function deletes a remote device 
Arguments: 
	BTDEVHDL dev_hdl: [in] device handle 
Return: 
	void  
---------------------------------------------------------------------------*/ 
void Test_Btsdk_DelRemoteDevice(BTDEVHDL dev_hdl) 
{ 
	BTUINT32 ulResult = BTSDK_OK; 
	int i = 0; 
 
	ulResult  = Btsdk_DeleteRemoteDeviceByHandle(dev_hdl); 
	PrintErrorMessage(ulResult, BTSDK_TRUE); 
	 
	if (BTSDK_OK == ulResult) 
	{ 
		for (i = 0; i < s_rmt_dev_num; i++) 
		{ 
			if (s_rmt_dev_hdls[i] == s_curr_dev) 
			{ 
				break; 
			} 
		} 
	 
		if ( i != s_rmt_dev_num) 
		{ 
			for (; i < s_rmt_dev_num - 1; i++) 
			{ 
				s_rmt_dev_hdls[i] = s_rmt_dev_hdls[i + 1]; 
			} 
			s_curr_dev = 0; 
			printf("Delete the remote device successfully.\n"); 
		} 
	} 
} 
 
 
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
Description: 
	This function shows the remote device manager sample menu 
Arguments: 
Return: 
	void  
---------------------------------------------------------------------------*/ 
void RmtDevShowMenu() 
{ 
	printf("*****************************************\n"); 
	printf("*  Remote Device Manager Testing Menu   *\n"); 
	printf("* <1> Search remote devices             *\n"); 
	printf("* <2> Select a remote device            *\n"); 
	printf("* <3> Get a remote device's basic Info  *\n"); 
	printf("* <4> Pair with the selected device     *\n"); 
	printf("* <5> Unpair with the selected device   *\n"); 
	printf("* <6> Delete the selected device        *\n"); 
	printf("*  Return to the upper menu          *\n"); 
	printf("*****************************************\n");     
	printf(">"); 
} 
 
 
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
Description: 
	This function is to execute user's choice 
Arguments: 
	BTUINT8 choice: [in] user's choice 
Return: 
	void  
---------------------------------------------------------------------------*/ 
void RmtDevExecCmd(BTUINT8 choice) 
{ 
	switch (choice)  
	{ 
	case '1': 
		StartSearchDevice(0);/*search all devices*/ 
		break; 
	case '2': 
		s_curr_dev = SelectRemoteDevice(0); 
		if (BTSDK_INVALID_HANDLE != s_curr_dev) 
		{ 
			printf("You have succeeded in getting a remote device's handle."); 
		} 
		break; 
	case '3': 
		Test_Print_Remote_Basic_Info(s_curr_dev); 
		break; 
	case '4': 
		Test_Btsdk_PairDevice(s_curr_dev); 
		break; 
	case '5': 
		Test_Btsdk_UnPairDevice(s_curr_dev); 
		break; 
	case '6': 
		Test_Btsdk_DelRemoteDevice(s_curr_dev); 
		break; 
	default: 
		printf("Invalid command.\n"); 
		break; 
	} 
} 
 
 
 
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
Description: 
	This function is the main function of remote device manager 
Arguments: 
	void 
Return: 
	void  
---------------------------------------------------------------------------*/ 
void TestRmtDevMgr(void) 
{ 
	BTUINT8 cChoice = 0; 
 
	printf("Please carry out the following menu step by step because of the continuity in function.\n"); 
	RmtDevShowMenu();	 
	while (cChoice != 'r') 
	{ 
		scanf(" %c", &cChoice); 
		getchar();		 
		if (cChoice == '\n') 
		{ 
			printf(">>"); 
		} 
		else if('r' == cChoice) 
		{ 
			break; 
		} 
		else 
		{ 
			RmtDevExecCmd(cChoice); 
			printf("\n"); 
			RmtDevShowMenu(); 
		} 
	} 
}