📄 btctl.c
字号:
/* Affix - Bluetooth Protocol Stack for Linux Copyright (C) 2001 Nokia Corporation Original Author: Dmitry Kasatkin <dmitry.kasatkin@nokia.com> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *//* $Id: btctl.c,v 1.165 2004/02/19 14:15:15 kassatki Exp $ btctl - driver control program Fixes: Dmitry Kasatkin <dmitry.kasatkin@nokia.com>*/#include <affix/config.h>#include <sys/socket.h>#include <sys/ioctl.h>#include <sys/types.h>#include <sys/stat.h>#include <sys/time.h>#include <sys/resource.h>#include <sys/errno.h>#include <fcntl.h>#include <unistd.h>#include <syslog.h>#include <signal.h>#include <stdlib.h>#include <stdio.h>#include <stdarg.h>#include <getopt.h>#include <string.h>#include <termios.h>#include <setjmp.h>//#include <readline/readline.h>#include <affix/bluetooth.h>#include <affix/btcore.h>#include "btctl.h"int argc;char **argv;int argind;char btdev[IFNAMSIZ] = "bt0";int nameset;int ftpmode = 0;char confdir[80];btdev_list _devcache;btdev_list *devcache;int showall = 0;int uart_rate = -1;char *uart_map = "/etc/affix/device.map";int verbose;int cmd_inquiry(struct btctl_command *cmd){ int fd, i; __u32 length; int err; INQUIRY_ITEM devs[20]; __u8 num; switch (cmd->cmd) { case 2: btdev_cache_print(devcache, DEVSTATE_ALL); return 0; case 3: btdev_cache_free(devcache); btdev_cache_save(devcache); return 0; } if (argv[argind]) { sscanf(argv[argind], "%x", &length); } else length = 8; fd = hci_open(btdev); if (fd < 0) { printf("Unable to open device %s: %s\n", btdev, strerror(errno)); return -1; } printf("Searching %d sec ...\n", length); err = HCI_Inquiry(fd, length, 20, devs, &num); if (err) { fprintf(stderr, "%s\n", hci_error(err)); exit(1); } printf("done.\n"); if (num == 0) { printf("No devices found.\n"); } else { btdev_cache_reload(devcache); btdev_cache_retire(devcache); for (i = 0; i < num; i++) __btdev_cache_add(devcache, devs[i].bda, devs[i].Class_of_Device, NULL); btdev_cache_save(devcache); btdev_cache_print(devcache, DEVSTATE_RANGE); } return 0;}int cmd_discovery(struct btctl_command *cmd){ int fd, i; __u32 length; int err; INQUIRY_ITEM devs[20]; char *devnames[20]; char namebuf[248]; __u8 num; if (argv[argind]) { sscanf(argv[argind], "%x", &length); } else length = 8; fd = hci_open(btdev); if (fd < 0) { printf("Unable to open device %s: %s\n", btdev, strerror(errno)); return -1; } printf("Searching %d sec ...\n", length); err = HCI_Inquiry(fd, length, 20, devs, &num); if (err) { fprintf(stderr, "%s\n", hci_error(err)); exit(1); } if (num == 0) { printf("done.\nNo devices found.\n"); } else { printf("Searching done. Resolving names ...\n"); for (i = 0; i < num; i++) { devs[i].Clock_Offset |= 0x8000; err = HCI_RemoteNameRequest(fd, &devs[i], namebuf); if (!err) devnames[i] = strdup(namebuf); else devnames[i] = NULL; } printf("done.\n"); btdev_cache_reload(devcache); btdev_cache_retire(devcache); for (i = 0; i < num; i++) { __btdev_cache_add(devcache, devs[i].bda, devs[i].Class_of_Device, devnames[i]); if (devnames[i]) free(devnames[i]); } btdev_cache_save(devcache); btdev_cache_print(devcache, DEVSTATE_RANGE); } return 0;}int cmd_bdaddr(struct btctl_command *cmd){ int err, fd; BD_ADDR bda; fd = hci_open(btdev); if (fd < 0) { printf("Unable to open device %s: %s\n", btdev, strerror(errno)); return -1; } err = HCI_ReadBDAddr(fd, &bda); if (err) { fprintf(stderr, "%s\n", hci_error(err)); exit(1); } printf("Local Address: %s\n", bda2str(&bda)); pause(); hci_close(fd); return 0;}int cmd_name(struct btctl_command *cmd){ int err, fd; char name[248]; fd = hci_open(btdev); if (fd < 0) { printf("Unable to open device %s: %s\n", btdev, strerror(errno)); return -1; } if (argv[argind] == NULL) { // read name err = HCI_ReadLocalName(fd, name); } else { strncpy(name, argv[argind], 248); err = HCI_ChangeLocalName(fd, name); } if (err) { fprintf(stderr, "%s\n", hci_error(err)); exit(1); } if (argv[argind] == NULL) { // read name printf("Name: %s\n", name); } hci_close(fd); return 0;}int cmd_scan(struct btctl_command *cmd){ int err, fd, flag; __u8 mode = HCI_SCAN_OFF; char *param; if (!argv[argind]) { printf("scan mode missed\n"); return -1; } fd = hci_open(btdev); if (fd < 0) { printf("Unable to open device %s: %s\n", btdev, strerror(errno)); return -1; } while (argv[argind]) { param = argv[argind]; if (param[0] == '+') flag = 1, param++; else if (param[0] == '-') flag = 0, param++; else flag = 1; if (strcmp(param, "disc") == 0) flag ? (mode |= HCI_SCAN_INQUIRY) : (mode &= ~HCI_SCAN_INQUIRY); else if (strcmp(param, "conn") == 0) flag ? (mode |= HCI_SCAN_PAGE) : (mode &= ~HCI_SCAN_PAGE); argind++; } err = HCI_WriteScanEnable(fd, mode); if (err) { fprintf(stderr, "%s\n", hci_error(err)); exit(1); } hci_close(fd); return 0;}int cmd_pincode(struct btctl_command *cmd){ int err; BD_ADDR bda; int Length; __u8 Code[16]; if (cmd->cmd == 1 && (argv[argind] == NULL || argv[argind+1] == NULL)) { printf("Parameters missed\n"); return -1; } if (argv[argind]) { if (strcasecmp(argv[argind], "default") == 0) memset(&bda, 0, 6); else { err = get_bda(&bda, argv[argind]); if (err) { printf("Incorrect address given\n"); return 1; } } if (cmd->cmd == 1) { Length = strlen(argv[argind+1]); strncpy((char*)Code, argv[argind+1], 16); err = hci_add_pin(&bda, Length, Code); } else err = hci_remove_pin(&bda); } else err = hci_remove_pin(NULL); if (err) { fprintf(stderr, "%s\n", hci_error(err)); exit(1); } return err;}int cmd_key(struct btctl_command *cmd){ int err, i; BD_ADDR bda; btdev_struct *btdev; btdev_cache_reload(devcache); if (argv[argind]) { err = get_bda(&bda, argv[argind]); if (err) { printf("Incorrect address given\n"); return 1; } err = hci_remove_key(&bda); btdev = btdev_cache_lookup(devcache, &bda); if (btdev) btdev->flags &= ~BTDEV_KEY; } else { err = hci_remove_key(NULL); /* remove all keys */ for (i = 0; (btdev = s_list_nth_data(devcache->head, i)); i++) btdev->flags &= ~BTDEV_KEY; } btdev_cache_save(devcache); return err;}int cmd_security(struct btctl_command *cmd){ int err, fd, sec_flags; char buf[80]; if (!argv[argind]) { print_usage(cmd); return 1; } argv2str(buf, &argv[argind]); if (!str2mask(sec_mode_map, buf, &sec_flags)) { printf("format error\n"); return -1; } fd = hci_open(btdev); if (fd < 0) { printf("Unable to open device %s: %s\n", btdev, strerror(errno)); return -1; } err = HCI_WriteSecurityMode(fd, sec_flags); if (err) { fprintf(stderr, "%s\n", hci_error(err)); exit(1); } close(fd); return 0;}int cmd_remotename(struct btctl_command *cmd){ int err, fd; BD_ADDR bda; INQUIRY_ITEM dev; char Name[248]; if (argv[argind] == NULL) { printf("Bluetooth address missed\n"); return -1; } err = get_bda(&bda, argv[argind]); if (err) { printf("Wrong address format\n"); return 1; } fd = hci_open(btdev); if (fd < 0) { printf("Unable to open device %s: %s\n", btdev, strerror(errno)); return fd; } dev.bda = bda; dev.PS_Repetition_Mode = 0x00; dev.PS_Mode = 0x00; dev.Clock_Offset = 0x00; err = HCI_RemoteNameRequest(fd, &dev, Name); if (err) { fprintf(stderr, "%s\n", hci_error(err)); exit(1); } printf("Remote name: %s\n", Name); return 0;}int cmd_role(struct btctl_command *cmd){ int err, fd, role_flags = 0; if (argv[argind] == NULL) { print_usage(cmd); return -1; } fd = hci_open(btdev); if (fd < 0) { printf("Unable to open device %s: %s\n", btdev, strerror(errno)); return -1; } if (strcmp(argv[argind++], "deny") == 0) role_flags |= HCI_ROLE_DENY_SWITCH; if (strcmp(argv[argind++], "master") == 0) role_flags |= HCI_ROLE_BECOME_MASTER; err = hci_set_role(fd, role_flags); if (err) { fprintf(stderr, "%s\n", hci_error(err)); exit(1); } hci_close(fd); return 0;}int cmd_class(struct btctl_command *cmd){ int err, fd; __u32 cod; __u32 value; char buf[80]; fd = hci_open(btdev); if (fd < 0) { printf("Unable to open device %s: %s\n", btdev, strerror(errno)); return -1; } if (argv[argind] == NULL) { // read name err = HCI_ReadClassOfDevice(fd, &cod); if (!err) { parse_cod(buf, cod); printf("Class: 0x%06X, %s\n", cod, buf); } } else { if (strstr(argv[argind], "0x")) { err = sscanf(argv[argind], "%x ", &value); if (err > 0) cod = value; } else { // parse command line argv2str(buf, &argv[argind]); err = str2cod(buf, &cod); if (err) { printf("bad arguments\n"); return -1; } } err = HCI_WriteClassOfDevice(fd, cod); } if (err) { fprintf(stderr, "%s\n", hci_error(err)); exit(1); } hci_close(fd); return 0;}int cmd_auth(struct btctl_command *cmd){ int err, fd; int handle; BD_ADDR bda; if (argv[argind] == NULL) { print_usage(cmd); return -1; } fd = hci_open(btdev); if (fd < 0) { printf("Unable to open device %s: %s\n", btdev, strerror(errno)); return -1; } err = get_bda(&bda, argv[argind]); if (err) { printf("Wrong address format\n"); return 1; } handle = hci_get_conn(fd, &bda); if (handle < 0) { printf("Connection not found\n"); return 1; } err = HCI_AuthenticationRequested(fd, handle); if (err) { fprintf(stderr, "%s\n", hci_error(err)); exit(1); } hci_close(fd); return 0;}/* * L2CAP */void print_data(__u8 *p, int len){ int i,j; char buf[81]; if (len > 100) return; for (i = 0; i < len; i++) { if ( ((i % 16) == 0) && i > 0) printf("%s\n", buf); j = (i % 16) * 3; sprintf( &(buf[j]), "%02x ", p[i]); } if (len) printf("%s\n", buf);}int cmd_ping(struct btctl_command *cmd){ int err, fd, i; BD_ADDR bda; struct sockaddr_affix saddr; int size; __u8 *data; if (argv[argind] == NULL || argv[argind+1] == NULL) { printf("parameters missing\n"); print_usage(cmd); return 1; } err = get_bda(&bda, argv[argind++]); if (err) { printf("Incorrect address given\n"); return 1; } printf("Connecting to host %s ...\n", bda2str(&bda));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -