Path: blob/main/usr.sbin/bluetooth/bthidcontrol/bthidcontrol.c
103636 views
/*-1* bthidcontrol.c2*3* SPDX-License-Identifier: BSD-2-Clause4*5* Copyright (c) 2004 Maksim Yevmenkin <[email protected]>6* All rights reserved.7*8* Redistribution and use in source and binary forms, with or without9* modification, are permitted provided that the following conditions10* are met:11* 1. Redistributions of source code must retain the above copyright12* notice, this list of conditions and the following disclaimer.13* 2. Redistributions in binary form must reproduce the above copyright14* notice, this list of conditions and the following disclaimer in the15* documentation and/or other materials provided with the distribution.16*17* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND18* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE19* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE20* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE21* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL22* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS23* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)24* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT25* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY26* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF27* SUCH DAMAGE.28*29* $Id: bthidcontrol.c,v 1.2 2004/02/13 21:44:41 max Exp $30*/3132#include <sys/queue.h>33#include <assert.h>34#define L2CAP_SOCKET_CHECKED35#include <bluetooth.h>36#include <err.h>37#include <errno.h>38#include <stdio.h>39#include <stdlib.h>40#include <string.h>41#include <unistd.h>42#include <usbhid.h>43#include "bthid_config.h"44#include "bthidcontrol.h"4546static int do_bthid_command(bdaddr_p bdaddr, int argc, char **argv);47static struct bthid_command * find_bthid_command(char const *command, struct bthid_command *category);48static void print_bthid_command(struct bthid_command *category);49static void usage(void) __dead2;5051int32_t hid_sdp_query(bdaddr_t const *local, bdaddr_t const *remote, int32_t *error);5253uint32_t verbose = 0;5455/*56* bthidcontrol57*/5859int60main(int argc, char *argv[])61{62bdaddr_t bdaddr;63int opt;6465hid_init(NULL);66memcpy(&bdaddr, NG_HCI_BDADDR_ANY, sizeof(bdaddr));6768while ((opt = getopt(argc, argv, "a:c:H:hv")) != -1) {69switch (opt) {70case 'a': /* bdaddr */71if (!bt_aton(optarg, &bdaddr)) {72struct hostent *he = NULL;7374if ((he = bt_gethostbyname(optarg)) == NULL)75errx(1, "%s: %s", optarg, hstrerror(h_errno));7677memcpy(&bdaddr, he->h_addr, sizeof(bdaddr));78}79break;8081case 'c': /* config file */82config_file = optarg;83break;8485case 'H': /* HIDs file */86hids_file = optarg;87break;8889case 'v': /* verbose */90verbose++;91break;9293case 'h':94default:95usage();96/* NOT REACHED */97}98}99100argc -= optind;101argv += optind;102103if (*argv == NULL)104usage();105106return (do_bthid_command(&bdaddr, argc, argv));107} /* main */108109/* Execute commands */110static int111do_bthid_command(bdaddr_p bdaddr, int argc, char **argv)112{113char *cmd = argv[0];114struct bthid_command *c = NULL;115int e, help;116117help = 0;118if (strcasecmp(cmd, "help") == 0) {119argc --;120argv ++;121122if (argc <= 0) {123fprintf(stdout, "Supported commands:\n");124print_bthid_command(sdp_commands);125print_bthid_command(hid_commands);126fprintf(stdout, "\nFor more information use " \127"'help command'\n");128129return (OK);130}131132help = 1;133cmd = argv[0];134}135136c = find_bthid_command(cmd, sdp_commands);137if (c == NULL)138c = find_bthid_command(cmd, hid_commands);139140if (c == NULL) {141fprintf(stdout, "Unknown command: \"%s\"\n", cmd);142return (ERROR);143}144145if (!help)146e = (c->handler)(bdaddr, -- argc, ++ argv);147else148e = USAGE;149150switch (e) {151case OK:152case FAILED:153break;154155case ERROR:156fprintf(stdout, "Could not execute command \"%s\". %s\n",157cmd, strerror(errno));158break;159160case USAGE:161fprintf(stdout, "Usage: %s\n%s\n", c->command, c->description);162break;163164default: assert(0); break;165}166167return (e);168} /* do_bthid_command */169170/* Try to find command in specified category */171static struct bthid_command *172find_bthid_command(char const *command, struct bthid_command *category)173{174struct bthid_command *c = NULL;175176for (c = category; c->command != NULL; c++) {177char *c_end = strchr(c->command, ' ');178179if (c_end != NULL) {180int len = c_end - c->command;181182if (strncasecmp(command, c->command, len) == 0)183return (c);184} else if (strcasecmp(command, c->command) == 0)185return (c);186}187188return (NULL);189} /* find_bthid_command */190191/* Print commands in specified category */192static void193print_bthid_command(struct bthid_command *category)194{195struct bthid_command *c = NULL;196197for (c = category; c->command != NULL; c++)198fprintf(stdout, "\t%s\n", c->command);199} /* print_bthid_command */200201/* Usage */202static void203usage(void)204{205fprintf(stderr,206"Usage: bthidcontrol options command\n" \207"Where options are:\n"208" -a bdaddr specify bdaddr\n" \209" -c file specify path to the bthidd config file\n" \210" -H file specify path to the bthidd HIDs file\n" \211" -h display usage and quit\n" \212" -v be verbose\n" \213" command one of the supported commands\n");214exit(255);215} /* usage */216217218219