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 #define L2CAP_SOCKET_CHECKED
35 #include <bluetooth.h>
43 #include "bthid_config.h"
44 #include "bthidcontrol.h"
46 static int do_bthid_command(bdaddr_p bdaddr, int argc, char **argv);
47 static struct bthid_command * find_bthid_command(char const *command, struct bthid_command *category);
48 static void print_bthid_command(struct bthid_command *category);
49 static void usage(void);
51 int32_t hid_sdp_query(bdaddr_t const *local, bdaddr_t const *remote, int32_t *error);
60 main(int argc, char *argv[])
66 memcpy(&bdaddr, NG_HCI_BDADDR_ANY, sizeof(bdaddr));
68 while ((opt = getopt(argc, argv, "a:c:H:hv")) != -1) {
70 case 'a': /* bdaddr */
71 if (!bt_aton(optarg, &bdaddr)) {
72 struct hostent *he = NULL;
74 if ((he = bt_gethostbyname(optarg)) == NULL)
75 errx(1, "%s: %s", optarg, hstrerror(h_errno));
77 memcpy(&bdaddr, he->h_addr, sizeof(bdaddr));
81 case 'c': /* config file */
85 case 'H': /* HIDs file */
89 case 'v': /* verbose */
106 return (do_bthid_command(&bdaddr, argc, argv));
109 /* Execute commands */
111 do_bthid_command(bdaddr_p bdaddr, int argc, char **argv)
114 struct bthid_command *c = NULL;
118 if (strcasecmp(cmd, "help") == 0) {
123 fprintf(stdout, "Supported commands:\n");
124 print_bthid_command(sdp_commands);
125 print_bthid_command(hid_commands);
126 fprintf(stdout, "\nFor more information use " \
136 c = find_bthid_command(cmd, sdp_commands);
138 c = find_bthid_command(cmd, hid_commands);
141 fprintf(stdout, "Unknown command: \"%s\"\n", cmd);
146 e = (c->handler)(bdaddr, -- argc, ++ argv);
156 fprintf(stdout, "Could not execute command \"%s\". %s\n",
157 cmd, strerror(errno));
161 fprintf(stdout, "Usage: %s\n%s\n", c->command, c->description);
164 default: assert(0); break;
168 } /* do_bthid_command */
170 /* Try to find command in specified category */
171 static struct bthid_command *
172 find_bthid_command(char const *command, struct bthid_command *category)
174 struct bthid_command *c = NULL;
176 for (c = category; c->command != NULL; c++) {
177 char *c_end = strchr(c->command, ' ');
180 int len = c_end - c->command;
182 if (strncasecmp(command, c->command, len) == 0)
184 } else if (strcasecmp(command, c->command) == 0)
189 } /* find_bthid_command */
191 /* Print commands in specified category */
193 print_bthid_command(struct bthid_command *category)
195 struct bthid_command *c = NULL;
197 for (c = category; c->command != NULL; c++)
198 fprintf(stdout, "\t%s\n", c->command);
199 } /* print_bthid_command */
206 "Usage: bthidcontrol options command\n" \
207 "Where options are:\n"
208 " -a bdaddr specify bdaddr\n" \
209 " -c file specify path to the bthidd config file\n" \
210 " -H file specify path to the bthidd HIDs file\n" \
211 " -h display usage and quit\n" \
213 " command one of the supported commands\n");