4 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
6 * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin@yahoo.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
30 * $Id: bthidcontrol.c,v 1.2 2004/02/13 21:44:41 max Exp $
34 #include <sys/queue.h>
36 #define L2CAP_SOCKET_CHECKED
37 #include <bluetooth.h>
45 #include "bthid_config.h"
46 #include "bthidcontrol.h"
48 static int do_bthid_command(bdaddr_p bdaddr, int argc, char **argv);
49 static struct bthid_command * find_bthid_command(char const *command, struct bthid_command *category);
50 static void print_bthid_command(struct bthid_command *category);
51 static void usage(void);
53 int32_t hid_sdp_query(bdaddr_t const *local, bdaddr_t const *remote, int32_t *error);
62 main(int argc, char *argv[])
68 memcpy(&bdaddr, NG_HCI_BDADDR_ANY, sizeof(bdaddr));
70 while ((opt = getopt(argc, argv, "a:c:H:hv")) != -1) {
72 case 'a': /* bdaddr */
73 if (!bt_aton(optarg, &bdaddr)) {
74 struct hostent *he = NULL;
76 if ((he = bt_gethostbyname(optarg)) == NULL)
77 errx(1, "%s: %s", optarg, hstrerror(h_errno));
79 memcpy(&bdaddr, he->h_addr, sizeof(bdaddr));
83 case 'c': /* config file */
87 case 'H': /* HIDs file */
91 case 'v': /* verbose */
108 return (do_bthid_command(&bdaddr, argc, argv));
111 /* Execute commands */
113 do_bthid_command(bdaddr_p bdaddr, int argc, char **argv)
116 struct bthid_command *c = NULL;
120 if (strcasecmp(cmd, "help") == 0) {
125 fprintf(stdout, "Supported commands:\n");
126 print_bthid_command(sdp_commands);
127 print_bthid_command(hid_commands);
128 fprintf(stdout, "\nFor more information use " \
138 c = find_bthid_command(cmd, sdp_commands);
140 c = find_bthid_command(cmd, hid_commands);
143 fprintf(stdout, "Unknown command: \"%s\"\n", cmd);
148 e = (c->handler)(bdaddr, -- argc, ++ argv);
158 fprintf(stdout, "Could not execute command \"%s\". %s\n",
159 cmd, strerror(errno));
163 fprintf(stdout, "Usage: %s\n%s\n", c->command, c->description);
166 default: assert(0); break;
170 } /* do_bthid_command */
172 /* Try to find command in specified category */
173 static struct bthid_command *
174 find_bthid_command(char const *command, struct bthid_command *category)
176 struct bthid_command *c = NULL;
178 for (c = category; c->command != NULL; c++) {
179 char *c_end = strchr(c->command, ' ');
182 int len = c_end - c->command;
184 if (strncasecmp(command, c->command, len) == 0)
186 } else if (strcasecmp(command, c->command) == 0)
191 } /* find_bthid_command */
193 /* Print commands in specified category */
195 print_bthid_command(struct bthid_command *category)
197 struct bthid_command *c = NULL;
199 for (c = category; c->command != NULL; c++)
200 fprintf(stdout, "\t%s\n", c->command);
201 } /* print_bthid_command */
208 "Usage: bthidcontrol options command\n" \
209 "Where options are:\n"
210 " -a bdaddr specify bdaddr\n" \
211 " -c file specify path to the bthidd config file\n" \
212 " -H file specify path to the bthidd HIDs file\n" \
213 " -h display usage and quit\n" \
215 " command one of the supported commands\n");