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

📄 main.c

📁 BlueTooth SDK Source Code! I hope that everybody like it.
💻 C
📖 第 1 页 / 共 2 页
字号:
#include "windows.h"
#include "hf_tester.h"

extern UCHAR mp3_data[];
extern DWORD total;

#define MAX_CMDLINE_LENGTH        256
#define CMPIPT(l, r)              (g_argc = 1 && strlen(g_argv[0]) == 1 && g_argv[0][0] >= l && g_argv[0][0] <= r)
#define EQUIPT2(v)                (g_argc = 2 && strlen(g_argv[0]) == 1 && g_argv[0][0] == v)
#define PCM_BUFFER_SIZE           100000
 
UCHAR g_init_file[] = "./hf_tester.ini";        /* HF Tester configuration file */
static UCHAR g_argc;							/* number of command line parameters */
static UCHAR g_argv[8][MAX_CMDLINE_LENGTH];		/* parameter array */
static UCHAR *g_pcm_buffer = NULL;
static UCHAR *g_read_buffer = NULL;
static DWORD g_pcm_buffer_size = PCM_BUFFER_SIZE;

static void APP_EvCbk(UCHAR *bd_addr, UCHAR event, const UCHAR *param, WORD param_len);
static void APP_PairCbk(UCHAR *bd_addr, UCHAR event, const UCHAR *param);
static void GetBDAddr(UCHAR *bd_addr);
static void DemoMenu();
static UCHAR ParseCmd(UCHAR* cmd);
static void DisposeCmd();
static DWORD ShowFindDevInfo(DWORD dev_num, struct DEVICE_INFO *dev_list);

int main()    
{
	UCHAR ipt[MAX_CMDLINE_LENGTH];
	UCHAR result = BT_FAIL;

	/*register two callback function before initialize Hands-Free Tester.*/
    HFT_RegEventCBFunc((EVENT_CBK *)APP_EvCbk);
	HFT_RegPairCBFunc((PAIR_CBK *)APP_PairCbk);

	/*Initialize Hands-Free Tester: security mode 3; encryption is required*/
	printf(">> Initializing Hands-Free Tester......\n");
	result = HFT_Init(SECURITY_MODE_3, TRUE);
	if (result != BT_SUCCESS) {
		printf(">> ERROR: Hands-Free device initialization failed!\n");
		exit(0);
	} 
	printf(">> Succeed to initialize Hands-Free Tester!\n");

	/*show menu*/
	DemoMenu();
	/*the following code is used for demo: read user input command and execute it.*/
	while (1) {
        while (1) {
			memset(ipt, 0, MAX_CMDLINE_LENGTH);
			g_argc = 0;
			memset(g_argv, 0, 8 * MAX_CMDLINE_LENGTH);
			printf("\nHF_TESTER>");
            gets(ipt);
            ParseCmd(ipt);
			g_argv[0][0] = tolower(g_argv[0][0]);
			/*test the validity of user input command*/
			if (CMPIPT('0', '6') || EQUIPT2('7') || CMPIPT('a', 'f') || EQUIPT2('g'))
    			break;
			else
				printf("Invalid Command!\n");
		}
		/*dispose user input command!*/
		DisposeCmd();
		if (g_argv[0][0] == '0')
			break;
	}

	/*Dump Hands-Free Tester.*/
	HFT_Done();
	return 0;
}

/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Description:
	This function is used to dispose user input command.
Arguments:
    None.
Return Value:
	None.
---------------------------------------------------------------------------*/
static void DisposeCmd()
{
	UCHAR ret = BT_FAIL;

	switch (g_argv[0][0]) {
	case '1':  /*inquiry and pair*/
		{ 
		    DWORD num_dev = 4;
			struct DEVICE_INFO dev_list[4] = {0};
			DWORD index = 0;
 
			/* 
			Search for nearby Hands-Free devices.
			Maximum amount of time is 12.8 sec (10 * 1.28 sec); 
			Name of the HF device is required; 
			The maximum number of responses is 4.
			*/
			printf("\n>> Please wait while searching for Hands-Free devices......\n");
		    ret = HFT_InquiryRequest(10, TRUE, &num_dev, dev_list);
			if (ret == BT_FAIL) {
			    printf("\n>> Inquiry failed!\n");
				break;
			}
			index = ShowFindDevInfo(num_dev, dev_list);
			/*pair with the selected Hands-Free device.*/
			if (index != 0)
				ret =HFT_PairRequest(dev_list[index - 1].bd_addr);
			if (ret == BT_SUCCESS)
				printf("\n>> Pair succeed!\n");
			else
			    printf("\n>> Pair failed!\n");
		}
		break;   
	case '2':  /*connect to HF*/
		{  
			UCHAR bd_addr[BD_ADDR_LEN] = {0};
		    /*
			Get one Bluetooth Baseband Address from the paired Hands-Free devices.
			Which bd_addr would be get is implementation dependent.
			In the demo function GetBDAddr, only one paired bd_addr would be available.
			*/
		    GetBDAddr(bd_addr);
		    ret = HFT_ConnectWithHF(bd_addr);
		    if (ret != BT_SUCCESS)
			    printf("\n>> Connecting with Hands-Free device failed!\nHF_TESTER>");	
		} 
		break;
	case '3':  /*establish SCO connection*/
		ret = HFT_SetupScoConn(SCO_PKT_TYPE_HV1);
		if (ret != BT_SUCCESS)
			printf("\n>> Establishing Audio Connection failed!\nHF_TESTER>");	
		break;
	case '4':  /*release SCO connection*/
		ret = HFT_ReleaseScoConn();
		if (ret != BT_SUCCESS)
			printf("\n>> Releasing Audio Connection failed!\nHF_TESTER>");	
		break;  
	case '5':  /*release the connection with HF*/
		HFT_DisconnectWithHF();
		break; 
	case '6':  /*reset the HF Tester configuration file*/
	    WritePrivateProfileString("HANDS-FREE", "PinCode", "0000", g_init_file);
	    WritePrivateProfileString("HANDS-FREE", "LinkKey", "", g_init_file);
		WritePrivateProfileString("HANDS-FREE", "BDAddress", "", g_init_file);
		printf(">> Hands-Free Tester has been reset.\n");
        printf(">> Current PIN code is 0000.\nHF_TESTER>");
		break;
	case '7':  /*transmit extended AT Command*/
		{ 
			UCHAR userdef_recode[257] = {0};
		    sprintf(userdef_recode, "\r\n%s\r\n", g_argv[1]);
		    HFT_SendExtendATCmd(userdef_recode);
		}  
		break;
	case 'a':  /*start PCM data save*/
		if (g_pcm_buffer) {
			HFT_StopPCMDataSave();
		    FREE(g_pcm_buffer);
		    g_pcm_buffer = NULL;
		}
		g_pcm_buffer = NEW(PCM_BUFFER_SIZE);
		ret = HFT_StartPCMDataSave(g_pcm_buffer, PCM_BUFFER_SIZE);
		if (ret == BT_SUCCESS)
			printf("\n>> Saving PCM data...\nHF_TESTER>");
	    else
			printf("\n>> Saving PCM data failed!\nHF_TESTER>");
	    break;
	case 'b':  /*stop PCM data save*/
		{
		    DWORD save_bytes = HFT_StopPCMDataSave();
		    printf("\n>> %ld bytes have been saved to PCM data buffer!\nHF_TESTER>", save_bytes);
		}
		break;
	case 'c':  /*start sending specific PCM data */
		ret = HFT_StartPCMDataSend(mp3_data, total);
		if (ret == BT_SUCCESS)
			printf("\n>> Sending PCM data...\nHF_TESTER>");
	    else
			printf("\n>> Sending PCM data failed!\nHF_TESTER>");
		break;
	case 'd':  /*start sending saved PCM data*/
		ret = HFT_StartPCMDataSend(g_pcm_buffer, g_pcm_buffer_size);
		if (ret == BT_SUCCESS)
			printf("\n>> Sending PCM data...\nHF_TESTER>");
	    else
			printf("\n>> Sending PCM data failed!\nHF_TESTER>");
		break;
	case 'e':
		{
		    DWORD send_bytes = HFT_StopPCMDataSend();
		    printf("\n>> %ld bytes have been sent to Hands-Free device!\nHF_TESTER>", send_bytes);
		}
		break;
	case 'f':  /*get current saved PCM data size*/
		{
		    DWORD save_bytes = HFT_GetCurrentPCMDataSize();
		    printf("\n>> %ld bytes have been saved to PCM data buffer!\nHF_TESTER>", save_bytes);
		}
		break;
	case 'g':  /*read PCM data from PCM data buffer*/
		{
			DWORD bytes_to_read = 0;
			DWORD read_bytes = 0;

			bytes_to_read = atol(g_argv[1]);
			if (bytes_to_read != 0) {
				if (g_read_buffer) {
				    FREE(g_read_buffer);
					g_read_buffer = NULL;
				}
                g_read_buffer = NEW(bytes_to_read);
				/*copy PCM data form PCM data buffer to the user specific buffer.*/
		        HFT_ReadPCMDataFromBuffer(g_read_buffer, bytes_to_read, &read_bytes);
				printf("\n>> %ld bytes have been read from PCM data buffer!\nHF_TESTER>", read_bytes);
				/*send the copied PCM data to Hands-Free device. It is for testing the read PCM data.*/
				ret = HFT_StartPCMDataSend(g_read_buffer, read_bytes);
        		if (ret == BT_SUCCESS)
		        	printf("\n>> Sending PCM data...\nHF_TESTER>");
	            else
			        printf("\n>> Sending PCM data failed!\nHF_TESTER>");
			}
		    break;
		}
	case '0':  /*quit*/
		if (g_pcm_buffer) {
			FREE(g_pcm_buffer);
			g_pcm_buffer = NULL;
		}
		if (g_read_buffer) {
		    FREE(g_read_buffer);
			g_read_buffer = NULL;
		}
		printf("\n>>Waiting for the application to quit...\n");
		break;
	default:
		break;
	}
}

/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Description:
	This function is the event processing callback function registered to 
	hf tester library.
Arguments:
	bd_addr[IN]		Bluetooth BaseBand Address of the under tested Hands-Free device.
	event[IN]		event code.
	param[IN]       parameter for the event
	param_len[IN]   the length of the parameter.
Return Value:
	None.
---------------------------------------------------------------------------*/
static void APP_EvCbk(UCHAR *bd_addr, UCHAR event, const UCHAR *param, WORD param_len)
{
	UCHAR buf[31];
	UCHAR extend_cmd[257];
	DWORD trans_bytes = 0;

	switch (event) {
	case HF_ANSWER_INCOMING_CALL: /*HF answer the incoming call*/
		printf("\n>> ATCMD RX: (ATA)\n");
		printf(">> HF has answered the incoming call!\nHF_TESTER>");
		break;
	case HF_CANCEL_CALL: /*HF cancel the incoming call or the outgoing call or the ongoing call*/
		printf("\n>> ATCMD RX: (AT+CHUP)\n");
		printf(">> HF has cancelled the incoming call or outgoing call or ongoing call!\nHF_TESTER>");
		break;
	case VOICE_RECOG_ACTIVATE_REQ: /*HF request AG to activate the voice recognition procedure*/
		printf("\n>> ATCMD RX: (AT+BVRA=1)\n");
		printf(">> HF initiated voice recognition activation request!\nHF_TESTER>");
		break;
	case VOICE_RECOG_DEACTIVATE_REQ: /*HF request AG to deactivate the voice recognition procedure*/
		printf("\n>> ATCMD RX: (AT+BVRA=0)\n");

⌨️ 快捷键说明

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