1 /*- 2 * bthidcontrol.c 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 * 6 * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin@yahoo.com> 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 * 30 * $Id: bthidcontrol.c,v 1.2 2004/02/13 21:44:41 max Exp $ 31 */ 32 33 #include <sys/queue.h> 34 #include <assert.h> 35 #define L2CAP_SOCKET_CHECKED 36 #include <bluetooth.h> 37 #include <err.h> 38 #include <errno.h> 39 #include <stdio.h> 40 #include <stdlib.h> 41 #include <string.h> 42 #include <unistd.h> 43 #include <usbhid.h> 44 #include "bthid_config.h" 45 #include "bthidcontrol.h" 46 47 static int do_bthid_command(bdaddr_p bdaddr, int argc, char **argv); 48 static struct bthid_command * find_bthid_command(char const *command, struct bthid_command *category); 49 static void print_bthid_command(struct bthid_command *category); 50 static void usage(void) __dead2; 51 52 int32_t hid_sdp_query(bdaddr_t const *local, bdaddr_t const *remote, int32_t *error); 53 54 uint32_t verbose = 0; 55 56 /* 57 * bthidcontrol 58 */ 59 60 int 61 main(int argc, char *argv[]) 62 { 63 bdaddr_t bdaddr; 64 int opt; 65 66 hid_init(NULL); 67 memcpy(&bdaddr, NG_HCI_BDADDR_ANY, sizeof(bdaddr)); 68 69 while ((opt = getopt(argc, argv, "a:c:H:hv")) != -1) { 70 switch (opt) { 71 case 'a': /* bdaddr */ 72 if (!bt_aton(optarg, &bdaddr)) { 73 struct hostent *he = NULL; 74 75 if ((he = bt_gethostbyname(optarg)) == NULL) 76 errx(1, "%s: %s", optarg, hstrerror(h_errno)); 77 78 memcpy(&bdaddr, he->h_addr, sizeof(bdaddr)); 79 } 80 break; 81 82 case 'c': /* config file */ 83 config_file = optarg; 84 break; 85 86 case 'H': /* HIDs file */ 87 hids_file = optarg; 88 break; 89 90 case 'v': /* verbose */ 91 verbose++; 92 break; 93 94 case 'h': 95 default: 96 usage(); 97 /* NOT REACHED */ 98 } 99 } 100 101 argc -= optind; 102 argv += optind; 103 104 if (*argv == NULL) 105 usage(); 106 107 return (do_bthid_command(&bdaddr, argc, argv)); 108 } /* main */ 109 110 /* Execute commands */ 111 static int 112 do_bthid_command(bdaddr_p bdaddr, int argc, char **argv) 113 { 114 char *cmd = argv[0]; 115 struct bthid_command *c = NULL; 116 int e, help; 117 118 help = 0; 119 if (strcasecmp(cmd, "help") == 0) { 120 argc --; 121 argv ++; 122 123 if (argc <= 0) { 124 fprintf(stdout, "Supported commands:\n"); 125 print_bthid_command(sdp_commands); 126 print_bthid_command(hid_commands); 127 fprintf(stdout, "\nFor more information use " \ 128 "'help command'\n"); 129 130 return (OK); 131 } 132 133 help = 1; 134 cmd = argv[0]; 135 } 136 137 c = find_bthid_command(cmd, sdp_commands); 138 if (c == NULL) 139 c = find_bthid_command(cmd, hid_commands); 140 141 if (c == NULL) { 142 fprintf(stdout, "Unknown command: \"%s\"\n", cmd); 143 return (ERROR); 144 } 145 146 if (!help) 147 e = (c->handler)(bdaddr, -- argc, ++ argv); 148 else 149 e = USAGE; 150 151 switch (e) { 152 case OK: 153 case FAILED: 154 break; 155 156 case ERROR: 157 fprintf(stdout, "Could not execute command \"%s\". %s\n", 158 cmd, strerror(errno)); 159 break; 160 161 case USAGE: 162 fprintf(stdout, "Usage: %s\n%s\n", c->command, c->description); 163 break; 164 165 default: assert(0); break; 166 } 167 168 return (e); 169 } /* do_bthid_command */ 170 171 /* Try to find command in specified category */ 172 static struct bthid_command * 173 find_bthid_command(char const *command, struct bthid_command *category) 174 { 175 struct bthid_command *c = NULL; 176 177 for (c = category; c->command != NULL; c++) { 178 char *c_end = strchr(c->command, ' '); 179 180 if (c_end != NULL) { 181 int len = c_end - c->command; 182 183 if (strncasecmp(command, c->command, len) == 0) 184 return (c); 185 } else if (strcasecmp(command, c->command) == 0) 186 return (c); 187 } 188 189 return (NULL); 190 } /* find_bthid_command */ 191 192 /* Print commands in specified category */ 193 static void 194 print_bthid_command(struct bthid_command *category) 195 { 196 struct bthid_command *c = NULL; 197 198 for (c = category; c->command != NULL; c++) 199 fprintf(stdout, "\t%s\n", c->command); 200 } /* print_bthid_command */ 201 202 /* Usage */ 203 static void 204 usage(void) 205 { 206 fprintf(stderr, 207 "Usage: bthidcontrol options command\n" \ 208 "Where options are:\n" 209 " -a bdaddr specify bdaddr\n" \ 210 " -c file specify path to the bthidd config file\n" \ 211 " -H file specify path to the bthidd HIDs file\n" \ 212 " -h display usage and quit\n" \ 213 " -v be verbose\n" \ 214 " command one of the supported commands\n"); 215 exit(255); 216 } /* usage */ 217 218