diff options
author | Marcel Holtmann <marcel@holtmann.org> | 2005-05-16 11:51:27 +0000 |
---|---|---|
committer | Marcel Holtmann <marcel@holtmann.org> | 2005-05-16 11:51:27 +0000 |
commit | fd1420fe08910f599bbb3bc84840318d1b99f748 (patch) | |
tree | 7d995762121ce0b92d032c9c88d7464fec2f767e | |
parent | 186a4118ec7cc9f105f4e23b49e71c2b0e46cfa4 (diff) |
Add tool for the CSR BCCMD interface
-rw-r--r-- | tools/Makefile.am | 5 | ||||
-rw-r--r-- | tools/bccmd.c | 193 |
2 files changed, 197 insertions, 1 deletions
diff --git a/tools/Makefile.am b/tools/Makefile.am index e9574f3e..66839dd7 100644 --- a/tools/Makefile.am +++ b/tools/Makefile.am @@ -22,7 +22,7 @@ sbin_PROGRAMS = hciattach hciconfig $(hid2hci_programs) bin_PROGRAMS = hcitool l2ping sdptool ciptool $(dfutool_programs) -noinst_PROGRAMS = hcisecfilter ppporc pskey +noinst_PROGRAMS = hcisecfilter ppporc pskey bccmd hciconfig_SOURCES = hciconfig.c csr.h csr.c hciconfig_LDADD = @BLUEZ_LIBS@ $(top_builddir)/common/libtextfile.a @@ -41,6 +41,9 @@ ppporc_LDADD = @BLUEZ_LIBS@ pskey_SOURCES = pskey.c csr.h csr.c pskey_LDADD = @BLUEZ_LIBS@ +bccmd_SOURCES = bccmd.c csr.h csr.c +bccmd_LDADD = @BLUEZ_LIBS@ + if HID2HCI hid2hci_LDADD = @USB_LIBS@ endif diff --git a/tools/bccmd.c b/tools/bccmd.c new file mode 100644 index 00000000..4b4a1fb4 --- /dev/null +++ b/tools/bccmd.c @@ -0,0 +1,193 @@ +/* + * + * BlueZ - Bluetooth protocol stack for Linux + * + * Copyright (C) 2004-2005 Marcel Holtmann <marcel@holtmann.org> + * + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. + * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY + * CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + * ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, + * COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS + * SOFTWARE IS DISCLAIMED. + * + * + * $Id$ + */ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include <stdio.h> +#include <errno.h> +#include <stdlib.h> +#include <getopt.h> +#include <sys/socket.h> + +#include <bluetooth/bluetooth.h> +#include <bluetooth/hci.h> +#include <bluetooth/hci_lib.h> + +#include "csr.h" + +static int cmd_keylen(int dd, int argc, char *argv[]) +{ + uint8_t buf[8]; + uint16_t handle, keylen; + int err; + + if (argc < 1) { + errno = EINVAL; + return -1; + } + + if (argc > 1) { + errno = E2BIG; + return -1; + } + + handle = atoi(argv[0]); + + memset(buf, 0, sizeof(buf)); + buf[0] = handle & 0xff; + buf[1] = handle >> 8; + + err = csr_read_varid_complex(dd, 0x4711, + CSR_VARID_CRYPT_KEY_LENGTH, buf, sizeof(buf)); + if (err < 0) { + errno = -err; + return -1; + } + + handle = buf[0] | (buf[1] << 8); + keylen = buf[2] | (buf[3] << 8); + + printf("Crypt key length: %d bit\n", keylen * 8); + + return 0; +} + +static struct { + char *str; + int (*func)(int dd, int argc, char **argv); + char *arg; + char *doc; +} commands[] = { + { "keylen", cmd_keylen, "<handle>", "Get current crypt key length" }, + { NULL }, +}; + +static void usage(void) +{ + int i; + + printf("bccmd - Utility for the CSR BCCMD interface\n\n"); + printf("Usage:\n" + "\tbccmd [-i <dev>] <command>\n\n"); + + printf("Commands:\n"); + for (i = 0; commands[i].str; i++) + printf("\t%s\t%s\t%s\n", commands[i].str, + commands[i].arg, commands[i].doc); +} + +static struct option main_options[] = { + { "help", 0, 0, 'h' }, + { "device", 1, 0, 'i' }, + { 0, 0, 0, 0 } +}; + +int main(int argc, char *argv[]) +{ + struct hci_dev_info di; + struct hci_version ver; + int i, err, dd, opt, dev = 0; + + while ((opt=getopt_long(argc, argv, "+i:h", main_options, NULL)) != -1) { + switch (opt) { + case 'i': + dev = hci_devid(optarg); + if (dev < 0) { + perror("Invalid device"); + exit(1); + } + break; + + case 'h': + default: + usage(); + exit(0); + } + } + + argc -= optind; + argv += optind; + optind = 0; + + if (argc < 1) { + usage(); + exit(1); + } + + dd = hci_open_dev(dev); + if (dd < 0) { + fprintf(stderr, "Can't open device hci%d: %s (%d)\n", + dev, strerror(errno), errno); + exit(1); + } + + if (hci_devinfo(dev, &di) < 0) { + fprintf(stderr, "Can't get device info for hci%d: %s (%d)\n", + dev, strerror(errno), errno); + hci_close_dev(dd); + exit(1); + } + + if (hci_read_local_version(dd, &ver, 1000) < 0) { + fprintf(stderr, "Can't read version info for hci%d: %s (%d)\n", + dev, strerror(errno), errno); + hci_close_dev(dd); + exit(1); + } + + if (ver.manufacturer != 10) { + fprintf(stderr, "Unsupported manufacturer\n"); + hci_close_dev(dd); + exit(1); + } + + for (i = 0; commands[i].str; i++) { + if (strcasecmp(commands[i].str, argv[0])) + continue; + + err = commands[i].func(dd, argc - 1, argv + 1); + + hci_close_dev(dd); + + if (err < 0) { + fprintf(stderr, "Can't execute command: %s (%d)\n", + strerror(errno), errno); + exit(1); + } + + exit(0); + } + + fprintf(stderr, "Unsupported command\n"); + + hci_close_dev(dd); + + exit(1); +} |