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

📄 hcitool.c

📁 这是Linux环境下的蓝牙源代码
💻 C
📖 第 1 页 / 共 4 页
字号:
	dd = hci_open_dev(dev_id);	if (dd < 0) {		perror("HCI device open failed");		exit(1);	}	if (hci_create_connection(dd, &bdaddr, htobs(ptype),				htobs(0x0000), role, &handle, 25000) < 0)		perror("Can't create connection");	hci_close_dev(dd);}/* Close connection */static struct option dc_options[] = {	{ "help",	0, 0, 'h' },	{ 0, 0, 0, 0 }};static char *dc_help =	"Usage:\n"	"\tdc <bdaddr>\n";static void cmd_dc(int dev_id, int argc, char **argv){	struct hci_conn_info_req *cr;	bdaddr_t bdaddr;	int opt, dd;	for_each_opt(opt, dc_options, NULL) {		switch (opt) {		default:			printf(dc_help);			return;		}	}	argc -= optind;	argv += optind;	if (argc < 1) {		printf(dc_help);		return;	}	str2ba(argv[0], &bdaddr);	if (dev_id < 0) {		dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);		if (dev_id < 0) {			fprintf(stderr, "Not connected.\n");			exit(1);		}	}	dd = hci_open_dev(dev_id);	if (dd < 0) {		perror("HCI device open failed");		exit(1);	}	cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));	if (!cr) {		perror("Can't allocate memory");		exit(1);	}	bacpy(&cr->bdaddr, &bdaddr);	cr->type = ACL_LINK;	if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {		perror("Get connection info failed");		exit(1);	}	if (hci_disconnect(dd, htobs(cr->conn_info->handle),				HCI_OE_USER_ENDED_CONNECTION, 10000) < 0)		perror("Disconnect failed");	free(cr);	hci_close_dev(dd);}/* Role switch */static struct option sr_options[] = {	{ "help",	0, 0, 'h' },	{ 0, 0, 0, 0 }};static char *sr_help =	"Usage:\n"	"\tsr <bdaddr> <role>\n";static void cmd_sr(int dev_id, int argc, char **argv){	bdaddr_t bdaddr;	uint8_t role;	int opt, dd;	for_each_opt(opt, sr_options, NULL) {		switch (opt) {		default:			printf(sr_help);			return;		}	}	argc -= optind;	argv += optind;	if (argc < 2) {		printf(sr_help);		return;	}	str2ba(argv[0], &bdaddr);	switch (argv[1][0]) {	case 'm':		role = 0;		break;	case 's':		role = 1;		break;	default:		role = atoi(argv[1]);		break;	}	if (dev_id < 0) {		dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);		if (dev_id < 0) {			fprintf(stderr, "Not connected.\n");			exit(1);		}	}	dd = hci_open_dev(dev_id);	if (dd < 0) {		perror("HCI device open failed");		exit(1);	}	if (hci_switch_role(dd, &bdaddr, role, 10000) < 0) {		perror("Switch role request failed");		exit(1);	}	hci_close_dev(dd);}/* Read RSSI */static struct option rssi_options[] = {	{ "help",	0, 0, 'h' },	{ 0, 0, 0, 0 }};static char *rssi_help =	"Usage:\n"	"\trssi <bdaddr>\n";static void cmd_rssi(int dev_id, int argc, char **argv){	struct hci_conn_info_req *cr;	bdaddr_t bdaddr;	int8_t rssi;	int opt, dd;	for_each_opt(opt, rssi_options, NULL) {		switch (opt) {		default:			printf(rssi_help);			return;		}	}	argc -= optind;	argv += optind;	if (argc < 1) {		printf(rssi_help);		return;	}	str2ba(argv[0], &bdaddr);	if (dev_id < 0) {		dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);		if (dev_id < 0) {			fprintf(stderr, "Not connected.\n");			exit(1);		}	}	dd = hci_open_dev(dev_id);	if (dd < 0) {		perror("HCI device open failed");		exit(1);	}	cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));	if (!cr) {		perror("Can't allocate memory");		exit(1);	}	bacpy(&cr->bdaddr, &bdaddr);	cr->type = ACL_LINK;	if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {		perror("Get connection info failed");		exit(1);	}	if (hci_read_rssi(dd, htobs(cr->conn_info->handle), &rssi, 1000) < 0) {		perror("Read RSSI failed");		exit(1);	}	printf("RSSI return value: %d\n", rssi);	free(cr);	hci_close_dev(dd);}/* Get link quality */static struct option lq_options[] = {	{ "help",	0, 0, 'h' },	{ 0, 0, 0, 0 }};static char *lq_help =	"Usage:\n"	"\tlq <bdaddr>\n";static void cmd_lq(int dev_id, int argc, char **argv){	struct hci_conn_info_req *cr;	bdaddr_t bdaddr;	uint8_t lq;	int opt, dd;	for_each_opt(opt, lq_options, NULL) {		switch (opt) {		default:			printf(lq_help);			return;		}	}	argc -= optind;	argv += optind;	if (argc < 1) {		printf(lq_help);		return;	}	str2ba(argv[0], &bdaddr);	if (dev_id < 0) {		dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);		if (dev_id < 0) {			fprintf(stderr, "Not connected.\n");			exit(1);		}	}	dd = hci_open_dev(dev_id);	if (dd < 0) {		perror("HCI device open failed");		exit(1);	}	cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));	if (!cr) {		perror("Can't allocate memory");		exit(1);	}	bacpy(&cr->bdaddr, &bdaddr);	cr->type = ACL_LINK;	if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {		perror("Get connection info failed");		exit(1);	}	if (hci_read_link_quality(dd, htobs(cr->conn_info->handle), &lq, 1000) < 0) {		perror("HCI read_link_quality request failed");		exit(1);	}	printf("Link quality: %d\n", lq);	free(cr);	hci_close_dev(dd);}/* Get transmit power level */static struct option tpl_options[] = {	{ "help",	0, 0, 'h' },	{ 0, 0, 0, 0 }};static char *tpl_help =	"Usage:\n"	"\ttpl <bdaddr> [type]\n";static void cmd_tpl(int dev_id, int argc, char **argv){	struct hci_conn_info_req *cr;	bdaddr_t bdaddr;	uint8_t type;	int8_t level;	int opt, dd;	for_each_opt(opt, tpl_options, NULL) {		switch (opt) {		default:			printf(tpl_help);			return;		}	}	argc -= optind;	argv += optind;	if (argc < 1) {		printf(tpl_help);		return;	}	str2ba(argv[0], &bdaddr);	type = (argc > 1) ? atoi(argv[1]) : 0;	if (dev_id < 0) {		dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);		if (dev_id < 0) {			fprintf(stderr, "Not connected.\n");			exit(1);		}	}	dd = hci_open_dev(dev_id);	if (dd < 0) {		perror("HCI device open failed");		exit(1);	}	cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));	if (!cr) {		perror("Can't allocate memory");		exit(1);	}	bacpy(&cr->bdaddr, &bdaddr);	cr->type = ACL_LINK;	if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {		perror("Get connection info failed");		exit(1);	}	if (hci_read_transmit_power_level(dd, htobs(cr->conn_info->handle), type, &level, 1000) < 0) {		perror("HCI read transmit power level request failed");		exit(1);	}	printf("%s transmit power level: %d\n",		(type == 0) ? "Current" : "Maximum", level);	free(cr);	hci_close_dev(dd);}/* Get AFH channel map */static struct option afh_options[] = {	{ "help",	0, 0, 'h' },	{ 0, 0, 0, 0 }};static char *afh_help =	"Usage:\n"	"\tafh <bdaddr>\n";static void cmd_afh(int dev_id, int argc, char **argv){	struct hci_conn_info_req *cr;	bdaddr_t bdaddr;	uint16_t handle;	uint8_t mode, map[10];	int opt, dd;	for_each_opt(opt, afh_options, NULL) {		switch (opt) {		default:			printf(afh_help);			return;		}	}	argc -= optind;	argv += optind;	if (argc < 1) {		printf(afh_help);		return;	}	str2ba(argv[0], &bdaddr);	if (dev_id < 0) {		dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);		if (dev_id < 0) {			fprintf(stderr, "Not connected.\n");			exit(1);		}	}	dd = hci_open_dev(dev_id);	if (dd < 0) {		perror("HCI device open failed");		exit(1);	}	cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));	if (!cr) {		perror("Can't allocate memory");		exit(1);	}	bacpy(&cr->bdaddr, &bdaddr);	cr->type = ACL_LINK;	if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {		perror("Get connection info failed");		exit(1);	}	handle = htobs(cr->conn_info->handle);	if (hci_read_afh_map(dd, handle, &mode, map, 1000) < 0) {		perror("HCI read AFH map request failed");		exit(1);	}	if (mode == 0x01) {		int i;		printf("AFH map: 0x");		for (i = 0; i < 10; i++)			printf("%02x", map[i]);		printf("\n");	} else		printf("AFH disabled\n");	free(cr);	hci_close_dev(dd);}/* Set connection packet type */static struct option cpt_options[] = {	{ "help",	0, 0, 'h' },	{ 0, 0, 0, 0 }};static char *cpt_help =	"Usage:\n"	"\tcpt <bdaddr> <packet_types>\n";static void cmd_cpt(int dev_id, int argc, char **argv){	struct hci_conn_info_req *cr;	struct hci_request rq;	set_conn_ptype_cp cp;	evt_conn_ptype_changed rp;	bdaddr_t bdaddr;	unsigned int ptype;	int dd, opt;	for_each_opt(opt, cpt_options, NULL) {		switch (opt) {		default:			printf(cpt_help);			return;		}	}	argc -= optind;	argv += optind;	if (argc < 2) {		printf(cpt_help);		return;	}	str2ba(argv[0], &bdaddr);	hci_strtoptype(argv[1], &ptype);	if (dev_id < 0) {		dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);		if (dev_id < 0) {			fprintf(stderr, "Not connected.\n");			exit(1);		}	}	dd = hci_open_dev(dev_id);	if (dd < 0) {		perror("HCI device open failed");		exit(1);	}	cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));	if (!cr) {		perror("Can't allocate memory");		exit(1);	}	bacpy(&cr->bdaddr, &bdaddr);	cr->type = ACL_LINK;	if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {		perror("Get connection info failed");		exit(1);	}	cp.handle   = htobs(cr->conn_info->handle);	cp.pkt_type = ptype;	memset(&rq, 0, sizeof(rq));	rq.ogf    = OGF_LINK_CTL;	rq.ocf    = OCF_SET_CONN_PTYPE;	rq.cparam = &cp;	rq.clen   = SET_CONN_PTYPE_CP_SIZE;	rq.rparam = &rp;	rq.rlen   = EVT_CONN_PTYPE_CHANGED_SIZE;	rq.event  = EVT_CONN_PTYPE_CHANGED;	if (hci_send_req(dd, &rq, 100) < 0) {		perror("Packet type change failed");		exit(1);	}	free(cr);	hci_close_dev(dd);}/* Get/Set link policy settings */static struct option lp_options[] = {	{ "help",	0, 0, 'h' },	{ 0, 0, 0, 0 }};static char *lp_help =	"Usage:\n"	"\tlp <bdaddr> [link policy]\n";static void cmd_lp(int dev_id, int argc, char **argv){	struct hci_conn_info_req *cr;	bdaddr_t bdaddr;	uint16_t policy;	int opt, dd;	for_each_opt(opt, lp_options, NULL) {		switch (opt) {		default:			printf(lp_help);			return;		}	}	argc -= optind;	argv += optind;	if (argc < 1) {		printf(lp_help);		return;	}	str2ba(argv[0], &bdaddr);	if (dev_id < 0) {		dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);		if (dev_id < 0) {			fprintf(stderr, "Not connected.\n");			exit(1);		}	}	dd = hci_open_dev(dev_id);	if (dd < 0) {		perror("HCI device open failed");		exit(1);	}	cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));	if (!cr) {		perror("Can't allocate memory");		exit(1);	}	bacpy(&cr->bdaddr, &bdaddr);	cr->type = ACL_LINK;	if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {		perror("Get connection info failed");		exit(1);	}

⌨️ 快捷键说明

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