4 * SPDX-License-Identifier: BSD-2-Clause
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 $
33 #include <sys/queue.h>
35 #define L2CAP_SOCKET_CHECKED
36 #include <bluetooth.h>
44 #include "bthid_config.h"
45 #include "bthidcontrol.h"
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;
52 int32_t hid_sdp_query(bdaddr_t const *local, bdaddr_t const *remote, int32_t *error);
61 main(int argc, char *argv[])
67 memcpy(&bdaddr, NG_HCI_BDADDR_ANY, sizeof(bdaddr));
69 while ((opt = getopt(argc, argv, "a:c:H:hv")) != -1) {
71 case 'a': /* bdaddr */
72 if (!bt_aton(optarg, &bdaddr)) {
73 struct hostent *he = NULL;
75 if ((he = bt_gethostbyname(optarg)) == NULL)
76 errx(1, "%s: %s", optarg, hstrerror(h_errno));
78 memcpy(&bdaddr, he->h_addr, sizeof(bdaddr));
82 case 'c': /* config file */
86 case 'H': /* HIDs file */
90 case 'v': /* verbose */
107 return (do_bthid_command(&bdaddr, argc, argv));
110 /* Execute commands */
112 do_bthid_command(bdaddr_p bdaddr, int argc, char **argv)
115 struct bthid_command *c = NULL;
119 if (strcasecmp(cmd, "help") == 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 " \
137 c = find_bthid_command(cmd, sdp_commands);
139 c = find_bthid_command(cmd, hid_commands);
142 fprintf(stdout, "Unknown command: \"%s\"\n", cmd);
147 e = (c->handler)(bdaddr, -- argc, ++ argv);
157 fprintf(stdout, "Could not execute command \"%s\". %s\n",
158 cmd, strerror(errno));
162 fprintf(stdout, "Usage: %s\n%s\n", c->command, c->description);
165 default: assert(0); break;
169 } /* do_bthid_command */
171 /* Try to find command in specified category */
172 static struct bthid_command *
173 find_bthid_command(char const *command, struct bthid_command *category)
175 struct bthid_command *c = NULL;
177 for (c = category; c->command != NULL; c++) {
178 char *c_end = strchr(c->command, ' ');
181 int len = c_end - c->command;
183 if (strncasecmp(command, c->command, len) == 0)
185 } else if (strcasecmp(command, c->command) == 0)
190 } /* find_bthid_command */
192 /* Print commands in specified category */
194 print_bthid_command(struct bthid_command *category)
196 struct bthid_command *c = NULL;
198 for (c = category; c->command != NULL; c++)
199 fprintf(stdout, "\t%s\n", c->command);
200 } /* print_bthid_command */
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" \
214 " command one of the supported commands\n");