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

📄 hcitool.c

📁 这是Linux环境下的蓝牙源代码
💻 C
📖 第 1 页 / 共 4 页
字号:
				strncpy(name, tmp, 249);				free(tmp);				nc = 1;			} else				nc = 0;		} else			nc = 0;		if (!extcls && !extinf && !extoui) {			ba2str(&(info+i)->bdaddr, addr);			if (nc) {				printf("\t%s\t%s\n", addr, name);				continue;			}			if (hci_read_remote_name_with_clock_offset(dd,					&(info+i)->bdaddr,					(info+i)->pscan_rep_mode,					(info+i)->clock_offset | 0x8000,					sizeof(name), name, 100000) < 0)				strcpy(name, "n/a");			for (n = 0; n < 248 && name[n]; n++) {				if ((unsigned char) name[i] < 32 || name[i] == 127)					name[i] = '.';			}			name[248] = '\0';			printf("\t%s\t%s\n", addr, name);			continue;		}		ba2str(&(info+i)->bdaddr, addr);		printf("BD Address:\t%s [mode %d, clkoffset 0x%4.4x]\n", addr,			(info+i)->pscan_rep_mode, btohs((info+i)->clock_offset));		if (extoui) {			ba2oui(&(info+i)->bdaddr, oui);			comp = ouitocomp(oui);			if (comp) {				printf("OUI company:\t%s (%s)\n", comp, oui);				free(comp);			}		}		cc = 0;		if (extinf) {			cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));			if (cr) {				bacpy(&cr->bdaddr, &(info+i)->bdaddr);				cr->type = ACL_LINK;				if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {					handle = 0;					cc = 1;				} else {					handle = htobs(cr->conn_info->handle);					cc = 0;				}				free(cr);			}			if (cc) {				if (hci_create_connection(dd, &(info+i)->bdaddr,						htobs(di.pkt_type & ACL_PTYPE_MASK),						(info+i)->clock_offset | 0x8000,						0x01, &handle, 25000) < 0) {					handle = 0;					cc = 0;				}			}		}		if (handle > 0 || !nc) {			if (hci_read_remote_name_with_clock_offset(dd,					&(info+i)->bdaddr,					(info+i)->pscan_rep_mode,					(info+i)->clock_offset | 0x8000,					sizeof(name), name, 100000) < 0) {				if (!nc)					strcpy(name, "n/a");			} else {				for (n = 0; n < 248 && name[n]; n++) {					if ((unsigned char) name[i] < 32 || name[i] == 127)						name[i] = '.';				}				name[248] = '\0';				nc = 0;			}		}		if (strlen(name) > 0)			printf("Device name:\t%s%s\n", name, nc ? " [cached]" : "");		if (extcls) {			memcpy(cls, (info+i)->dev_class, 3);			printf("Device class:\t");			if ((cls[1] & 0x1f) > sizeof(major_classes) / sizeof(char *))				printf("Invalid");			else				printf("%s, %s", major_classes[cls[1] & 0x1f],					get_minor_device_name(cls[1] & 0x1f, cls[0] >> 2));			printf(" (0x%2.2x%2.2x%2.2x)\n", cls[2], cls[1], cls[0]);		}		if (extinf && handle > 0) {			if (hci_read_remote_version(dd, handle, &version, 20000) == 0) {				char *ver = lmp_vertostr(version.lmp_ver);				printf("Manufacturer:\t%s (%d)\n",					bt_compidtostr(version.manufacturer),					version.manufacturer);				printf("LMP version:\t%s (0x%x) [subver 0x%x]\n",					ver ? ver : "n/a",					version.lmp_ver, version.lmp_subver);				if (ver)					bt_free(ver);			}			if (hci_read_remote_features(dd, handle, features, 20000) == 0) {				char *tmp = lmp_featurestostr(features, "\t\t", 63);				printf("LMP features:\t0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x"					" 0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x\n",					features[0], features[1],					features[2], features[3],					features[4], features[5],					features[6], features[7]);				printf("%s\n", tmp);				bt_free(tmp);			}			if (cc) {				usleep(10000);				hci_disconnect(dd, handle, HCI_OE_USER_ENDED_CONNECTION, 10000);			}		}		printf("\n");	}	bt_free(info);	hci_close_dev(dd);}/* Remote name */static struct option name_options[] = {	{ "help",	0, 0, 'h' },	{ 0, 0, 0, 0 }};static char *name_help =	"Usage:\n"	"\tname <bdaddr>\n";static void cmd_name(int dev_id, int argc, char **argv){	bdaddr_t bdaddr;	char name[248];	int opt, dd;	for_each_opt(opt, name_options, NULL) {		switch (opt) {		default:			printf(name_help);			return;		}	}	argc -= optind;	argv += optind;	if (argc < 1) {		printf(name_help);		return;	}	str2ba(argv[0], &bdaddr);	if (dev_id < 0) {		dev_id = hci_get_route(&bdaddr);		if (dev_id < 0) {			fprintf(stderr, "Device is not available.\n");			exit(1);		}	}	dd = hci_open_dev(dev_id);	if (dd < 0) {		perror("HCI device open failed");		exit(1);	}	if (hci_read_remote_name(dd, &bdaddr, sizeof(name), name, 25000) == 0)		printf("%s\n", name);	hci_close_dev(dd);}/* Info about remote device */static struct option info_options[] = {	{ "help",	0, 0, 'h' },	{ 0, 0, 0, 0 }};static char *info_help =	"Usage:\n"	"\tinfo <bdaddr>\n";static void cmd_info(int dev_id, int argc, char **argv){	bdaddr_t bdaddr;	uint16_t handle;	uint8_t max_page, features[8];	char name[249], oui[9], *comp;	struct hci_version version;	struct hci_dev_info di;	struct hci_conn_info_req *cr;	int opt, dd, cc = 0;	for_each_opt(opt, info_options, NULL) {		switch (opt) {		default:			printf(info_help);			return;		}	}	argc -= optind;	argv += optind;	if (argc < 1) {		printf(info_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)		dev_id = hci_get_route(&bdaddr);	if (dev_id < 0) {		fprintf(stderr, "Device is not available or not connected.\n");		exit(1);	}	if (hci_devinfo(dev_id, &di) < 0) {		perror("Can't get device info");		exit(1);	}	printf("Requesting information ...\n");	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 get connection info");		close(dd);		exit(1);	}	bacpy(&cr->bdaddr, &bdaddr);	cr->type = ACL_LINK;	if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {		if (hci_create_connection(dd, &bdaddr,					htobs(di.pkt_type & ACL_PTYPE_MASK),					0, 0x01, &handle, 25000) < 0) {			perror("Can't create connection");			close(dd);			exit(1);		}		cc = 1;	} else		handle = htobs(cr->conn_info->handle);	printf("\tBD Address:  %s\n", argv[0]);	ba2oui(&bdaddr, oui);	comp = ouitocomp(oui);	if (comp) {		printf("\tOUI Company: %s (%s)\n", comp, oui);		free(comp);	}	if (hci_read_remote_name(dd, &bdaddr, sizeof(name), name, 25000) == 0)		printf("\tDevice Name: %s\n", name);	if (hci_read_remote_version(dd, handle, &version, 20000) == 0) {		char *ver = lmp_vertostr(version.lmp_ver);		printf("\tLMP Version: %s (0x%x) LMP Subversion: 0x%x\n"			"\tManufacturer: %s (%d)\n",			ver ? ver : "n/a",			version.lmp_ver,			version.lmp_subver,			bt_compidtostr(version.manufacturer),			version.manufacturer);		if (ver)			bt_free(ver);	}	if (hci_read_remote_features(dd, handle, features, 20000) == 0) {		char *tmp = lmp_featurestostr(features, "\t\t", 63);		printf("\tFeatures: 0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x\n%s\n",			features[0], features[1], features[2], features[3],			features[4], features[5], features[6], features[7], tmp);		bt_free(tmp);	}	if ((di.features[7] & LMP_EXT_FEAT) && (features[7] & LMP_EXT_FEAT)) {		if (hci_read_remote_ext_features(dd, handle, 0,					&max_page, features, 20000) == 0)			if (max_page > 0)				printf("\tExtended features: %d page%s\n",					max_page, max_page > 1 ? "s" : "");	}	if (cc) {		usleep(10000);		hci_disconnect(dd, handle, HCI_OE_USER_ENDED_CONNECTION, 10000);	}	hci_close_dev(dd);}/* Start periodic inquiry */static struct option spinq_options[] = {	{ "help",	0, 0, 'h' },	{ 0, 0, 0, 0 }};static char *spinq_help =	"Usage:\n"	"\tspinq\n";static void cmd_spinq(int dev_id, int argc, char **argv){	uint8_t lap[3] = { 0x33, 0x8b, 0x9e };	struct hci_request rq;	periodic_inquiry_cp cp;	int opt, dd;	for_each_opt(opt, spinq_options, NULL) {		switch (opt) {		default:			printf(spinq_help);			return;		}	}	if (dev_id < 0)		dev_id = hci_get_route(NULL);	dd = hci_open_dev(dev_id);	if (dd < 0) {		perror("Device open failed");		exit(EXIT_FAILURE);	}	memset(&cp, 0, sizeof(cp));	memcpy(cp.lap, lap, 3);	cp.max_period = htobs(16);	cp.min_period = htobs(10);	cp.length     = 8;	cp.num_rsp    = 0;	memset(&rq, 0, sizeof(rq));	rq.ogf    = OGF_LINK_CTL;	rq.ocf    = OCF_PERIODIC_INQUIRY;	rq.cparam = &cp;	rq.clen   = PERIODIC_INQUIRY_CP_SIZE;	if (hci_send_req(dd, &rq, 100) < 0) {		perror("Periodic inquiry failed");		exit(EXIT_FAILURE);	}	hci_close_dev(dd);}/* Exit periodic inquiry */static struct option epinq_options[] = {	{ "help",	0, 0, 'h' },	{ 0, 0, 0, 0 }};static char *epinq_help =	"Usage:\n"	"\tspinq\n";static void cmd_epinq(int dev_id, int argc, char **argv){	int opt, dd;	for_each_opt(opt, epinq_options, NULL) {		switch (opt) {		default:			printf(epinq_help);			return;		}	}	if (dev_id < 0)		dev_id = hci_get_route(NULL);	dd = hci_open_dev(dev_id);	if (dd < 0) {		perror("Device open failed");		exit(EXIT_FAILURE);	}	if (hci_send_cmd(dd, OGF_LINK_CTL,				OCF_EXIT_PERIODIC_INQUIRY, 0, NULL) < 0) {		perror("Exit periodic inquiry failed");		exit(EXIT_FAILURE);	}	hci_close_dev(dd);}/* Send arbitrary HCI commands */static struct option cmd_options[] = {	{ "help",	0, 0, 'h' },	{ 0, 0, 0, 0 }};static char *cmd_help =	"Usage:\n"	"\tcmd <ogf> <ocf> [parameters]\n"	"Example:\n"	"\tcmd 0x03 0x0013 0x41 0x42 0x43 0x44\n";static void cmd_cmd(int dev_id, int argc, char **argv){	unsigned char buf[HCI_MAX_EVENT_SIZE], *ptr = buf;	struct hci_filter flt;	hci_event_hdr *hdr;	int i, opt, len, dd;	uint16_t ocf;	uint8_t ogf;	for_each_opt(opt, cmd_options, NULL) {		switch (opt) {		default:			printf(cmd_help);			return;		}	}	argc -= optind;	argv += optind;	if (argc < 2) {		printf(cmd_help);		return;	}	if (dev_id < 0)		dev_id = hci_get_route(NULL);	errno = 0;	ogf = strtol(argv[0], NULL, 16);	ocf = strtol(argv[1], NULL, 16);	if (errno == ERANGE || (ogf > 0x3f) || (ocf > 0x3ff)) {		printf(cmd_help);		return;	}	for (i = 2, len = 0; i < argc && len < sizeof(buf); i++, len++)		*ptr++ = (uint8_t) strtol(argv[i], NULL, 16);	dd = hci_open_dev(dev_id);	if (dd < 0) {		perror("Device open failed");		exit(EXIT_FAILURE);	}	/* Setup filter */	hci_filter_clear(&flt);	hci_filter_set_ptype(HCI_EVENT_PKT, &flt);	hci_filter_all_events(&flt);	if (setsockopt(dd, SOL_HCI, HCI_FILTER, &flt, sizeof(flt)) < 0) {		perror("HCI filter setup failed");		exit(EXIT_FAILURE);	}	printf("< HCI Command: ogf 0x%02x, ocf 0x%04x, plen %d\n", ogf, ocf, len);	hex_dump("  ", 20, buf, len); fflush(stdout);	if (hci_send_cmd(dd, ogf, ocf, len, buf) < 0) {		perror("Send failed");		exit(EXIT_FAILURE);	}	len = read(dd, buf, sizeof(buf));	if (len < 0) {		perror("Read failed");		exit(EXIT_FAILURE);	}	hdr = (void *)(buf + 1);	ptr = buf + (1 + HCI_EVENT_HDR_SIZE);	len -= (1 + HCI_EVENT_HDR_SIZE);	printf("> HCI Event: 0x%02x plen %d\n", hdr->evt, hdr->plen);	hex_dump("  ", 20, ptr, len); fflush(stdout);	hci_close_dev(dd);}/* Display active connections */static struct option con_options[] = {	{ "help",	0, 0, 'h' },	{ 0, 0, 0, 0 }};static char *con_help =	"Usage:\n"	"\tcon\n";static void cmd_con(int dev_id, int argc, char **argv){	int opt;	for_each_opt(opt, con_options, NULL) {		switch (opt) {		default:			printf(con_help);			return;		}	}	printf("Connections:\n");	hci_for_each_dev(HCI_UP, conn_list, dev_id);}/* Create connection */static struct option cc_options[] = {	{ "help",	0, 0, 'h' },	{ "role",	1, 0, 'r' },	{ "ptype",	1, 0, 'p' },	{ 0, 0, 0, 0 }};static char *cc_help =	"Usage:\n"	"\tcc [--role=m|s] [--ptype=pkt_types] <bdaddr>\n"	"Example:\n"	"\tcc --ptype=dm1,dh3,dh5 01:02:03:04:05:06\n"	"\tcc --role=m 01:02:03:04:05:06\n";static void cmd_cc(int dev_id, int argc, char **argv){	bdaddr_t bdaddr;	uint16_t handle;	uint8_t role;	unsigned int ptype;	int dd, opt;	role = 0x01;	ptype = HCI_DM1 | HCI_DM3 | HCI_DM5 | HCI_DH1 | HCI_DH3 | HCI_DH5;	for_each_opt(opt, cc_options, NULL) {		switch (opt) {		case 'p':			hci_strtoptype(optarg, &ptype);			break;		case 'r':			role = optarg[0] == 'm' ? 0 : 1;			break;		default:			printf(cc_help);			return;		}	}	argc -= optind;	argv += optind;	if (argc < 1) {		printf(cc_help);		return;	}	str2ba(argv[0], &bdaddr);	if (dev_id < 0) {		dev_id = hci_get_route(&bdaddr);		if (dev_id < 0) {			fprintf(stderr, "Device is not available.\n");			exit(1);		}	}

⌨️ 快捷键说明

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