bthidcontrol.c revision 128080
1128080Semax/* 2128080Semax * bthidcontrol.c 3128080Semax * 4128080Semax * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin@yahoo.com> 5128080Semax * All rights reserved. 6128080Semax * 7128080Semax * Redistribution and use in source and binary forms, with or without 8128080Semax * modification, are permitted provided that the following conditions 9128080Semax * are met: 10128080Semax * 1. Redistributions of source code must retain the above copyright 11128080Semax * notice, this list of conditions and the following disclaimer. 12128080Semax * 2. Redistributions in binary form must reproduce the above copyright 13128080Semax * notice, this list of conditions and the following disclaimer in the 14128080Semax * documentation and/or other materials provided with the distribution. 15128080Semax * 16128080Semax * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17128080Semax * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18128080Semax * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19128080Semax * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20128080Semax * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21128080Semax * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22128080Semax * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23128080Semax * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24128080Semax * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25128080Semax * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26128080Semax * SUCH DAMAGE. 27128080Semax * 28128080Semax * $Id: bthidcontrol.c,v 1.2 2004/02/13 21:44:41 max Exp $ 29128080Semax * $FreeBSD: head/usr.sbin/bluetooth/bthidcontrol/bthidcontrol.c 128080 2004-04-10 00:18:00Z emax $ 30128080Semax */ 31128080Semax 32128080Semax#include <sys/queue.h> 33128080Semax#include <assert.h> 34128080Semax#include <bluetooth.h> 35128080Semax#include <err.h> 36128080Semax#include <errno.h> 37128080Semax#include <stdio.h> 38128080Semax#include <stdlib.h> 39128080Semax#include <string.h> 40128080Semax#include <unistd.h> 41128080Semax#include <usbhid.h> 42128080Semax#include "bthid_config.h" 43128080Semax#include "bthidcontrol.h" 44128080Semax 45128080Semaxstatic int do_bthid_command(bdaddr_p bdaddr, int argc, char **argv); 46128080Semaxstatic struct bthid_command * find_bthid_command(char const *command, struct bthid_command *category); 47128080Semaxstatic void print_bthid_command(struct bthid_command *category); 48128080Semaxstatic void usage(void); 49128080Semax 50128080Semaxint32_t hid_sdp_query(bdaddr_t const *local, bdaddr_t const *remote, int32_t *error); 51128080Semax 52128080Semax/* 53128080Semax * bthidcontrol 54128080Semax */ 55128080Semax 56128080Semaxint 57128080Semaxmain(int argc, char *argv[]) 58128080Semax{ 59128080Semax bdaddr_t bdaddr; 60128080Semax int opt; 61128080Semax 62128080Semax hid_init(NULL); 63128080Semax memcpy(&bdaddr, NG_HCI_BDADDR_ANY, sizeof(bdaddr)); 64128080Semax 65128080Semax while ((opt = getopt(argc, argv, "a:c:H:h")) != -1) { 66128080Semax switch (opt) { 67128080Semax case 'a': /* bdaddr */ 68128080Semax if (!bt_aton(optarg, &bdaddr)) { 69128080Semax struct hostent *he = NULL; 70128080Semax 71128080Semax if ((he = bt_gethostbyname(optarg)) == NULL) 72128080Semax errx(1, "%s: %s", optarg, hstrerror(h_errno)); 73128080Semax 74128080Semax memcpy(&bdaddr, he->h_addr, sizeof(bdaddr)); 75128080Semax } 76128080Semax break; 77128080Semax 78128080Semax case 'c': /* config file */ 79128080Semax config_file = optarg; 80128080Semax break; 81128080Semax 82128080Semax case 'H': /* HIDs file */ 83128080Semax hids_file = optarg; 84128080Semax break; 85128080Semax 86128080Semax case 'h': 87128080Semax default: 88128080Semax usage(); 89128080Semax /* NOT REACHED */ 90128080Semax } 91128080Semax } 92128080Semax 93128080Semax argc -= optind; 94128080Semax argv += optind; 95128080Semax 96128080Semax if (*argv == NULL) 97128080Semax usage(); 98128080Semax 99128080Semax return (do_bthid_command(&bdaddr, argc, argv)); 100128080Semax} /* main */ 101128080Semax 102128080Semax/* Execute commands */ 103128080Semaxstatic int 104128080Semaxdo_bthid_command(bdaddr_p bdaddr, int argc, char **argv) 105128080Semax{ 106128080Semax char *cmd = argv[0]; 107128080Semax struct bthid_command *c = NULL; 108128080Semax int e, help; 109128080Semax 110128080Semax help = 0; 111128080Semax if (strcasecmp(cmd, "help") == 0) { 112128080Semax argc --; 113128080Semax argv ++; 114128080Semax 115128080Semax if (argc <= 0) { 116128080Semax fprintf(stdout, "Supported commands:\n"); 117128080Semax print_bthid_command(sdp_commands); 118128080Semax print_bthid_command(hid_commands); 119128080Semax fprintf(stdout, "\nFor more information use " \ 120128080Semax "'help command'\n"); 121128080Semax 122128080Semax return (OK); 123128080Semax } 124128080Semax 125128080Semax help = 1; 126128080Semax cmd = argv[0]; 127128080Semax } 128128080Semax 129128080Semax c = find_bthid_command(cmd, sdp_commands); 130128080Semax if (c == NULL) 131128080Semax c = find_bthid_command(cmd, hid_commands); 132128080Semax 133128080Semax if (c == NULL) { 134128080Semax fprintf(stdout, "Unknown command: \"%s\"\n", cmd); 135128080Semax return (ERROR); 136128080Semax } 137128080Semax 138128080Semax if (!help) 139128080Semax e = (c->handler)(bdaddr, -- argc, ++ argv); 140128080Semax else 141128080Semax e = USAGE; 142128080Semax 143128080Semax switch (e) { 144128080Semax case OK: 145128080Semax case FAILED: 146128080Semax break; 147128080Semax 148128080Semax case ERROR: 149128080Semax fprintf(stdout, "Could not execute command \"%s\". %s\n", 150128080Semax cmd, strerror(errno)); 151128080Semax break; 152128080Semax 153128080Semax case USAGE: 154128080Semax fprintf(stdout, "Usage: %s\n%s\n", c->command, c->description); 155128080Semax break; 156128080Semax 157128080Semax default: assert(0); break; 158128080Semax } 159128080Semax 160128080Semax return (e); 161128080Semax} /* do_bthid_command */ 162128080Semax 163128080Semax/* Try to find command in specified category */ 164128080Semaxstatic struct bthid_command * 165128080Semaxfind_bthid_command(char const *command, struct bthid_command *category) 166128080Semax{ 167128080Semax struct bthid_command *c = NULL; 168128080Semax 169128080Semax for (c = category; c->command != NULL; c++) { 170128080Semax char *c_end = strchr(c->command, ' '); 171128080Semax 172128080Semax if (c_end != NULL) { 173128080Semax int len = c_end - c->command; 174128080Semax 175128080Semax if (strncasecmp(command, c->command, len) == 0) 176128080Semax return (c); 177128080Semax } else if (strcasecmp(command, c->command) == 0) 178128080Semax return (c); 179128080Semax } 180128080Semax 181128080Semax return (NULL); 182128080Semax} /* find_bthid_command */ 183128080Semax 184128080Semax/* Print commands in specified category */ 185128080Semaxstatic void 186128080Semaxprint_bthid_command(struct bthid_command *category) 187128080Semax{ 188128080Semax struct bthid_command *c = NULL; 189128080Semax 190128080Semax for (c = category; c->command != NULL; c++) 191128080Semax fprintf(stdout, "\t%s\n", c->command); 192128080Semax} /* print_bthid_command */ 193128080Semax 194128080Semax/* Usage */ 195128080Semaxstatic void 196128080Semaxusage(void) 197128080Semax{ 198128080Semax fprintf(stderr, 199128080Semax"Usage: bthidcontrol options command\n" \ 200128080Semax"Where options are:\n" 201128080Semax" -a bdaddr specify bdaddr\n" \ 202128080Semax" -c file specify path to the bthidd config file\n" \ 203128080Semax" -H file specify path to the bthidd HIDs file\n" \ 204128080Semax" -h display usage and quit\n" \ 205128080Semax" command one of the supported commands\n"); 206128080Semax exit(255); 207128080Semax} /* usage */ 208128080Semax 209