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 uint32_t verbose = 0;
53 
54 /*
55  * bthidcontrol
56  */
57 
58 int
59 main(int argc, char *argv[])
60 {
61 	bdaddr_t	bdaddr;
62 	int		opt;
63 
64 	hid_init(NULL);
65 	memcpy(&bdaddr, NG_HCI_BDADDR_ANY, sizeof(bdaddr));
66 
67 	while ((opt = getopt(argc, argv, "a:c:H:hv")) != -1) {
68 		switch (opt) {
69 		case 'a': /* bdaddr */
70 			if (!bt_aton(optarg, &bdaddr)) {
71 				struct hostent  *he = NULL;
72 
73 				if ((he = bt_gethostbyname(optarg)) == NULL)
74 					errx(1, "%s: %s", optarg, hstrerror(h_errno));
75 
76 				memcpy(&bdaddr, he->h_addr, sizeof(bdaddr));
77 			}
78 			break;
79 
80 		case 'c': /* config file */
81 			config_file = optarg;
82 			break;
83 
84 		case 'H': /* HIDs file */
85 			hids_file = optarg;
86 			break;
87 
88 		case 'v': /* verbose */
89 			verbose++;
90 			break;
91 
92 		case 'h':
93 		default:
94 			usage();
95 			/* NOT REACHED */
96 		}
97 	}
98 
99 	argc -= optind;
100 	argv += optind;
101 
102 	if (*argv == NULL)
103 		usage();
104 
105 	return (do_bthid_command(&bdaddr, argc, argv));
106 } /* main */
107 
108 /* Execute commands */
109 static int
110 do_bthid_command(bdaddr_p bdaddr, int argc, char **argv)
111 {
112 	char			*cmd = argv[0];
113 	struct bthid_command	*c = NULL;
114 	int			 e, help;
115 
116 	help = 0;
117 	if (strcasecmp(cmd, "help") == 0) {
118 		argc --;
119 		argv ++;
120 
121 		if (argc <= 0) {
122 			fprintf(stdout, "Supported commands:\n");
123 			print_bthid_command(sdp_commands);
124 			print_bthid_command(hid_commands);
125 			fprintf(stdout, "\nFor more information use " \
126 					"'help command'\n");
127 
128 			return (OK);
129 		}
130 
131 		help = 1;
132 		cmd = argv[0];
133 	}
134 
135 	c = find_bthid_command(cmd, sdp_commands);
136 	if (c == NULL)
137 		c = find_bthid_command(cmd, hid_commands);
138 
139 	if (c == NULL) {
140 		fprintf(stdout, "Unknown command: \"%s\"\n", cmd);
141 		return (ERROR);
142 	}
143 
144 	if (!help)
145 		e = (c->handler)(bdaddr, -- argc, ++ argv);
146 	else
147 		e = USAGE;
148 
149 	switch (e) {
150 	case OK:
151 	case FAILED:
152 		break;
153 
154 	case ERROR:
155 		fprintf(stdout, "Could not execute command \"%s\". %s\n",
156 				cmd, strerror(errno));
157 		break;
158 
159 	case USAGE:
160 		fprintf(stdout, "Usage: %s\n%s\n", c->command, c->description);
161 		break;
162 
163 	default: assert(0); break;
164 	}
165 
166 	return (e);
167 } /* do_bthid_command */
168 
169 /* Try to find command in specified category */
170 static struct bthid_command *
171 find_bthid_command(char const *command, struct bthid_command *category)
172 {
173 	struct bthid_command	*c = NULL;
174 
175 	for (c = category; c->command != NULL; c++) {
176 		char	*c_end = strchr(c->command, ' ');
177 
178 		if (c_end != NULL) {
179 			int	len = c_end - c->command;
180 
181 			if (strncasecmp(command, c->command, len) == 0)
182 				return (c);
183 		} else if (strcasecmp(command, c->command) == 0)
184 				return (c);
185 	}
186 
187 	return (NULL);
188 } /* find_bthid_command */
189 
190 /* Print commands in specified category */
191 static void
192 print_bthid_command(struct bthid_command *category)
193 {
194 	struct bthid_command	*c = NULL;
195 
196 	for (c = category; c->command != NULL; c++)
197 		fprintf(stdout, "\t%s\n", c->command);
198 } /* print_bthid_command */
199 
200 /* Usage */
201 static void
202 usage(void)
203 {
204 	fprintf(stderr,
205 "Usage: bthidcontrol options command\n" \
206 "Where options are:\n"
207 "	-a bdaddr	specify bdaddr\n" \
208 "	-c file		specify path to the bthidd config file\n" \
209 "	-H file		specify path to the bthidd HIDs file\n" \
210 "	-h		display usage and quit\n" \
211 "	-v		be verbose\n" \
212 "	command		one of the supported commands\n");
213 	exit(255);
214 } /* usage */
215 
216