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