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

📄 btctl.c

📁 affix是一个Open Source的蓝牙协议栈
💻 C
📖 第 1 页 / 共 4 页
字号:
		}		gettimeofday(&tv_end, NULL);					sec = tv_end.tv_sec - tv_start.tv_sec;		usec = (1000000 * sec) + tv_end.tv_usec - tv_start.tv_usec;		rsec = usec/1000000;		rusec = (usec - (rsec * 1000000))/10000;		speed = (double)(size)/((double)(rsec) + (double)(rusec)/100);		printf("%d bytes sent in %ld.%ld secs (%.2f B/s)\n", size, rsec, rusec, speed);	}	close(fd);	return 0;}void sig_handler(int sig){}int cmd_periodic_inquiry(struct command *cmd){	int		fd;	unsigned char	buf[HCI_MAX_EVENT_SIZE];	unsigned int	max_period_len, min_period_len, length;	int		err;	__u8		num;	struct Inquiry_Result_Event	*ir = (void*)buf;	struct Inquiry_Complete_Event	*ic = (void*)buf;	/* Default values */	length = 3;	min_period_len = 4;	max_period_len = 5;	if (cmd->cmd == 0) {		if (__argv[optind]) {			sscanf(__argv[optind++], "%d", &max_period_len);			if(__argv[optind]) {					sscanf(__argv[optind++], "%d", &min_period_len);					if(__argv[optind]) {						sscanf(__argv[optind++], "%d", &length);					}			}		}	}	num=0; /* Unlimited */		fd = hci_open(btdev);	if (fd < 0) {		printf("Unable to open device %s: %s\n", btdev, strerror(errno));		return -1;	}	switch (cmd->cmd) {		case 1:			{				err = HCI_ExitPeriodicInquiryMode(fd);				if (err) {					fprintf(stderr, "%s\n", hci_error(err));					exit(1);				}				return 0;			}	}	err = HCI_PeriodicInquiryMode(fd, max_period_len, min_period_len, length, num);	if (err) {		fprintf(stderr, "%s\n", hci_error(err));		exit(1);	}	hci_event_mask(fd, COMMAND_STATUS_MASK|INQUIRY_RESULT_MASK|INQUIRY_COMPLETE_MASK);		signal(SIGINT, sig_handler);	signal(SIGTERM, sig_handler);	do {		err = hci_recv_event(fd, buf, sizeof(buf), 20);		if (err < 0) {			//fprintf(stderr, "%s\n", hci_error(err));			fprintf(stderr, "Exit from Periodic Inquire Mode ...\n");			HCI_ExitPeriodicInquiryMode(fd);			exit(1);		}				if (ir->hdr.EventCode == HCI_E_INQUIRY_RESULT) {			int	i;			for (i = 0; i < ir->Num_Responses; i++) {				printf("Found bda: %s\n", bda2str(&ir->Results[i].bda)); 							}		}	} while (1);//ic->hdr.EventCode != HCI_E_INQUIRY_COMPLETE);	return ic->Status;}int cmd_link_info(struct command *cmd){	int		err, fd, interval;	BD_ADDR		bda;	__u8		lq;	__s8		RSSI;	__s8		cpl;	int		handle;	if (__argv[optind] == NULL) {		printf("parameters missing\n");		print_usage(cmd);		return 1;	}	err = btdev_get_bda(&bda, __argv[optind++]);	if (err) {		printf("Incorrect address given\n");		return 1;	}	interval = 0; /* No loop */	if (cmd->cmd == 3) {		if (__argv[optind] != NULL) {			sscanf(__argv[optind], "%d", &interval);			if (interval <= 0 || interval > 100) {				printf("Interval value 1 - 100\n");				exit(1);			}		}	}	fd = hci_open(btdev);	if (fd < 0) {		printf("Unable to open device %s: %s\n", btdev, strerror(errno));		return -1;	}	handle = hci_get_conn(fd, &bda);	if (handle < 0) {		printf("Connection not found\n");		return 1;	}	switch (cmd->cmd) {		case 0: /* lq */			{				err = HCI_GetLinkQuality(fd, handle, &lq);				if (err) {					fprintf(stderr, "%s\n", hci_error(err));					exit(1);				}				printf("Link quality: %d\n", lq);				break;			}		case 1: /* RSSI  dBm = 10*log(P/Pref) Pref = 1 milliwatt */			{				err = HCI_ReadRSSI(fd, handle, &RSSI);				if (err) {					fprintf(stderr, "%s\n", hci_error(err));					exit(1);				}				printf("RSSI: %d\n", RSSI);				break;			}		case 2: /* Transmit Power Level */			{				err = HCI_ReadTransmitPowerLevel(fd, handle, 0, &cpl);				/* 0 means current transmit power level */				if (err) {					fprintf(stderr, "%s\n", hci_error(err));					exit(1);				}				printf("Current Transmit Power Level: %d\n", cpl);				break;			}		case 3: /* All HCI info parameters */			{				do {					err = HCI_ReadRSSI(fd, handle, &RSSI);					if (err) {						fprintf(stderr, "%s\n", hci_error(err));						exit(1);					}					err = HCI_GetLinkQuality(fd, handle, &lq);					if (err) {						fprintf(stderr, "%s\n", hci_error(err));						exit(1);					}										err = HCI_ReadTransmitPowerLevel(fd, handle, 0, &cpl);					/* 0 means current transmit power level */					if (err) {						fprintf(stderr, "%s\n", hci_error(err));						exit(1);					}					printf("RSSI: %d, LQ: %d, CTPL: %d\n", RSSI, lq, cpl);					sleep(interval);				} while (interval);				break;			}	}	//pause();	hci_close(fd);	return 0;}int cmd_switch(struct command *cmd){	int	err, fd;	int	role;	BD_ADDR	bda;	fd = hci_open(btdev);	if (fd < 0) {		printf("Unable to open device %s: %s\n", btdev, strerror(errno));		return -1;	}	err = btdev_get_bda(&bda, __argv[optind++]);	if (err) {		printf("Incorrect address given\n");		return 1;	}	if (cmd->cmd == 1) {		/* policy */		int	handle;		int	policy;				handle = hci_get_conn(fd, &bda);		if (handle < 0) {			printf("Connection not found\n");			return 1;		}		if (__argv[optind]) {			err = sscanf(__argv[optind++], "%x", &policy);			if (err <= 0)				policy = 0x0001;		} else			policy = 0x0001;		err = HCI_WriteLinkPolicySettings(fd, handle, policy);	} else {		role = atoi(__argv[optind++]);		err = HCI_SwitchRole(fd, &bda, role);	}	if (err) {		fprintf(stderr, "%s\n", hci_error(err));		exit(1);	}	hci_close(fd);	return 0;}int cmd_pkttype(struct command *cmd){	int	err, value, fd;		fd = hci_open(btdev);	if (fd < 0) {		printf("Unable to open device %s: %s\n", btdev, strerror(errno));		return -1;	}	if (__argv[optind] == NULL)		value = 0; //set default	else {		if (__argv[optind][0] == '0')			err = sscanf(__argv[optind], "%x", &value);		else {			str2mask(pkt_type_map, argv2str(__argv + optind), &value);		}	}	err = hci_set_pkttype(fd, value);	if (err) {		fprintf(stderr, "%s\n", hci_error(err));		exit(1);	}	hci_close(fd);	return 0;}int cmd_control(struct command *cmd){	int	err = 0, fd;	fd = hci_open(btdev);	if (fd < 0) {		printf("Unable to open device %s: %s\n", btdev, strerror(errno));		return -1;	}	switch (cmd->cmd) {		case 0:	/* page_to */			{				__u16	timeo;				if (__argv[optind] == NULL) {					// read name					err = HCI_ReadPageTimeout(fd, &timeo);				} else {					sscanf(__argv[optind], "%hu", &timeo);					printf("Timeo: %u\n", timeo);					err = HCI_WritePageTimeout(fd, timeo);				}				if (!err && __argv[optind] == NULL) {					// read name					printf("Timeo: %u\n", timeo);				}			}			break;		case 1:	/* hdisc */			{				struct sockaddr_affix	sa;;				if (__argv[optind] == NULL) {					printf("Bluetooth address missed\n");					return -1;				}					if (strstr(__argv[optind], "0x")) {					err = sscanf(__argv[optind++], "%hx", &sa.port);					if (err <= 0) {						printf("format error\n");						return 1;					}				} else {					err = btdev_get_bda(&sa.bda, __argv[optind++]);					if (err) {						printf("Incorrect address given\n");						return 1;					}				}				printf("Disconnecting %s ...\n", bda2str(&sa.bda));				err = hci_disconnect(&sa);			}			break;		case 2:	/* hconnect */			{				int			err, sock;				struct sockaddr_affix	sa;					if (__argv[optind] == NULL) {					printf("Bluetooth address missed\n");					return -1;				}						err = btdev_get_bda(&sa.bda, __argv[optind++]);				if (err) {					printf("Incorrect address given\n");					return -1;				}				sock = socket(PF_AFFIX, SOCK_SEQPACKET, BTPROTO_HCIACL);				if (sock < 0) {					printf("unable to create affix socket\n");					return -1;				}				sa.family = PF_AFFIX;				sa.devnum = HCIDEV_ANY;					err = connect(sock, (struct sockaddr*)&sa, sizeof(sa));				if (err) {					printf("unable to create connection\n");					return -1;				}				return 0;				/*				INQUIRY_ITEM	dev;								err = btdev_get_bda(&dev.bda, __argv[optind++]);				if (err) {					printf("Incorrect address given\n");					return 1;				}				dev.PS_Repetition_Mode = 0x00;				dev.PS_Mode = 0x00;				dev.Clock_Offset = 0x00;				err = __HCI_CreateConnection(fd, &dev, 						HCI_PT_DM1|HCI_PT_DH1|HCI_PT_DM3|HCI_PT_DH3|HCI_PT_DM5|HCI_PT_DH5, 0); */			}			break;		case 3:	/* hpkt_type */			{				int	handle, type;				BD_ADDR	bda;				if (strstr(__argv[optind], "0x")) {					err = sscanf(__argv[optind++], "%x", &handle);					if (err <= 0) {						printf("format error\n");						return 1;					}				} else {					err = btdev_get_bda(&bda, __argv[optind++]);					if (err) {						printf("Incorrect address given\n");						return 1;					}					handle = hci_get_conn(fd, &bda);					if (handle < 0) {						printf("Connection not found\n");						return 1;					}				}				if (__argv[optind]) {					err = sscanf(__argv[optind++], "%x", &type);					if (err <= 0)						type = HCI_PT_DM1|HCI_PT_DH1|HCI_PT_DM3|HCI_PT_DH3|HCI_PT_DM5|HCI_PT_DH5;				} else					type = HCI_PT_DM1|HCI_PT_DH1|HCI_PT_DM3|HCI_PT_DH3|HCI_PT_DM5|HCI_PT_DH5;									printf("Changing pkt_type, handle %#hx ...\n", handle);				err = HCI_ChangeConnectionPacketType(fd, handle, type);			}		case 4:	/* read broadcast retransmissions */			{				__u8 Num;				err = HCI_Read_Num_Broadcast_Retransmissions(fd,&Num);				printf("\nRetransmissions : %d\n", Num);			}			break;		case 5:	/* write broadcast retransmissions */			{				int num = 0;				if (strstr(__argv[optind], "0x")) {					err = sscanf(__argv[optind++], "%x", &num);					if (err <= 0) {						printf("format error\n");						return 1;					}				}				err = HCI_Write_Num_Broadcast_Retransmissions(fd,num);			}			break;		default:			break;	}	if (err) {		fprintf(stderr, "%s\n", hci_error(err));		exit(1);	}	hci_close(fd);	return 0;}int cmd_pair(struct command *cmd){	int			err, fd, sock;	struct sockaddr_affix	sa;	int			handle;	err = btdev_get_bda(&sa.bda, __argv[optind++]);	if (err) {		printf("Incorrect address given\n");		return 1;	}	fd = hci_open(btdev);	if (fd < 0) {		printf("Unable to open device %s: %s\n", btdev, strerror(errno));		return -1;	}	sock = socket(PF_AFFIX, SOCK_SEQPACKET, BTPROTO_HCIACL);	if (sock < 0) {		printf("unable to create affix socket\n");		return -1;	}	sa.family = PF_AFFIX;	sa.devnum = HCIDEV_ANY;		err = connect(sock, (struct sockaddr*)&sa, sizeof(sa));	if (err) {		printf("unable to create connection\n");		return -1;	}	handle = hci_get_conn(fd, &sa.bda);	if (handle < 0) {		printf("Connection not found\n");		return 1;	}	hci_remove_key(&sa.bda);	err = HCI_AuthenticationRequested(fd, handle);	if (err) {		fprintf(stderr, "%s\n", hci_error(err));		exit(1);	}	printf("Pairing completed: %s\n", err?"error":"successful");	close(sock);	hci_close(fd);	return 0;}

⌨️ 快捷键说明

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