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