4 * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin@yahoo.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
28 * $Id: bthidcontrol.c,v 1.2 2004/02/13 21:44:41 max Exp $
32 #include <sys/queue.h>
34 #include <bluetooth.h>
42 #include "bthid_config.h"
43 #include "bthidcontrol.h"
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);
50 int32_t hid_sdp_query(bdaddr_t const *local, bdaddr_t const *remote, int32_t *error);
57 main(int argc, char *argv[])
63 memcpy(&bdaddr, NG_HCI_BDADDR_ANY, sizeof(bdaddr));
65 while ((opt = getopt(argc, argv, "a:c:H:h")) != -1) {
67 case 'a': /* bdaddr */
68 if (!bt_aton(optarg, &bdaddr)) {
69 struct hostent *he = NULL;
71 if ((he = bt_gethostbyname(optarg)) == NULL)
72 errx(1, "%s: %s", optarg, hstrerror(h_errno));
74 memcpy(&bdaddr, he->h_addr, sizeof(bdaddr));
78 case 'c': /* config file */
82 case 'H': /* HIDs file */
99 return (do_bthid_command(&bdaddr, argc, argv));
102 /* Execute commands */
104 do_bthid_command(bdaddr_p bdaddr, int argc, char **argv)
107 struct bthid_command *c = NULL;
111 if (strcasecmp(cmd, "help") == 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 " \
129 c = find_bthid_command(cmd, sdp_commands);
131 c = find_bthid_command(cmd, hid_commands);
134 fprintf(stdout, "Unknown command: \"%s\"\n", cmd);
139 e = (c->handler)(bdaddr, -- argc, ++ argv);
149 fprintf(stdout, "Could not execute command \"%s\". %s\n",
150 cmd, strerror(errno));
154 fprintf(stdout, "Usage: %s\n%s\n", c->command, c->description);
157 default: assert(0); break;
161 } /* do_bthid_command */
163 /* Try to find command in specified category */
164 static struct bthid_command *
165 find_bthid_command(char const *command, struct bthid_command *category)
167 struct bthid_command *c = NULL;
169 for (c = category; c->command != NULL; c++) {
170 char *c_end = strchr(c->command, ' ');
173 int len = c_end - c->command;
175 if (strncasecmp(command, c->command, len) == 0)
177 } else if (strcasecmp(command, c->command) == 0)
182 } /* find_bthid_command */
184 /* Print commands in specified category */
186 print_bthid_command(struct bthid_command *category)
188 struct bthid_command *c = NULL;
190 for (c = category; c->command != NULL; c++)
191 fprintf(stdout, "\t%s\n", c->command);
192 } /* print_bthid_command */
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");