xref: /freebsd/usr.sbin/bluetooth/bthidcontrol/bthidcontrol.c (revision 9f23cbd6cae82fd77edfad7173432fa8dccd0a95)
1 /*-
2  * bthidcontrol.c
3  *
4  * SPDX-License-Identifier: BSD-2-Clause
5  *
6  * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin@yahoo.com>
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
17  *
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
28  * SUCH DAMAGE.
29  *
30  * $Id: bthidcontrol.c,v 1.2 2004/02/13 21:44:41 max Exp $
31  * $FreeBSD$
32  */
33 
34 #include <sys/queue.h>
35 #include <assert.h>
36 #define L2CAP_SOCKET_CHECKED
37 #include <bluetooth.h>
38 #include <err.h>
39 #include <errno.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44 #include <usbhid.h>
45 #include "bthid_config.h"
46 #include "bthidcontrol.h"
47 
48 static int do_bthid_command(bdaddr_p bdaddr, int argc, char **argv);
49 static struct bthid_command * find_bthid_command(char const *command, struct bthid_command *category);
50 static void print_bthid_command(struct bthid_command *category);
51 static void usage(void) __dead2;
52 
53 int32_t hid_sdp_query(bdaddr_t const *local, bdaddr_t const *remote, int32_t *error);
54 
55 uint32_t verbose = 0;
56 
57 /*
58  * bthidcontrol
59  */
60 
61 int
62 main(int argc, char *argv[])
63 {
64 	bdaddr_t	bdaddr;
65 	int		opt;
66 
67 	hid_init(NULL);
68 	memcpy(&bdaddr, NG_HCI_BDADDR_ANY, sizeof(bdaddr));
69 
70 	while ((opt = getopt(argc, argv, "a:c:H:hv")) != -1) {
71 		switch (opt) {
72 		case 'a': /* bdaddr */
73 			if (!bt_aton(optarg, &bdaddr)) {
74 				struct hostent  *he = NULL;
75 
76 				if ((he = bt_gethostbyname(optarg)) == NULL)
77 					errx(1, "%s: %s", optarg, hstrerror(h_errno));
78 
79 				memcpy(&bdaddr, he->h_addr, sizeof(bdaddr));
80 			}
81 			break;
82 
83 		case 'c': /* config file */
84 			config_file = optarg;
85 			break;
86 
87 		case 'H': /* HIDs file */
88 			hids_file = optarg;
89 			break;
90 
91 		case 'v': /* verbose */
92 			verbose++;
93 			break;
94 
95 		case 'h':
96 		default:
97 			usage();
98 			/* NOT REACHED */
99 		}
100 	}
101 
102 	argc -= optind;
103 	argv += optind;
104 
105 	if (*argv == NULL)
106 		usage();
107 
108 	return (do_bthid_command(&bdaddr, argc, argv));
109 } /* main */
110 
111 /* Execute commands */
112 static int
113 do_bthid_command(bdaddr_p bdaddr, int argc, char **argv)
114 {
115 	char			*cmd = argv[0];
116 	struct bthid_command	*c = NULL;
117 	int			 e, help;
118 
119 	help = 0;
120 	if (strcasecmp(cmd, "help") == 0) {
121 		argc --;
122 		argv ++;
123 
124 		if (argc <= 0) {
125 			fprintf(stdout, "Supported commands:\n");
126 			print_bthid_command(sdp_commands);
127 			print_bthid_command(hid_commands);
128 			fprintf(stdout, "\nFor more information use " \
129 					"'help command'\n");
130 
131 			return (OK);
132 		}
133 
134 		help = 1;
135 		cmd = argv[0];
136 	}
137 
138 	c = find_bthid_command(cmd, sdp_commands);
139 	if (c == NULL)
140 		c = find_bthid_command(cmd, hid_commands);
141 
142 	if (c == NULL) {
143 		fprintf(stdout, "Unknown command: \"%s\"\n", cmd);
144 		return (ERROR);
145 	}
146 
147 	if (!help)
148 		e = (c->handler)(bdaddr, -- argc, ++ argv);
149 	else
150 		e = USAGE;
151 
152 	switch (e) {
153 	case OK:
154 	case FAILED:
155 		break;
156 
157 	case ERROR:
158 		fprintf(stdout, "Could not execute command \"%s\". %s\n",
159 				cmd, strerror(errno));
160 		break;
161 
162 	case USAGE:
163 		fprintf(stdout, "Usage: %s\n%s\n", c->command, c->description);
164 		break;
165 
166 	default: assert(0); break;
167 	}
168 
169 	return (e);
170 } /* do_bthid_command */
171 
172 /* Try to find command in specified category */
173 static struct bthid_command *
174 find_bthid_command(char const *command, struct bthid_command *category)
175 {
176 	struct bthid_command	*c = NULL;
177 
178 	for (c = category; c->command != NULL; c++) {
179 		char	*c_end = strchr(c->command, ' ');
180 
181 		if (c_end != NULL) {
182 			int	len = c_end - c->command;
183 
184 			if (strncasecmp(command, c->command, len) == 0)
185 				return (c);
186 		} else if (strcasecmp(command, c->command) == 0)
187 				return (c);
188 	}
189 
190 	return (NULL);
191 } /* find_bthid_command */
192 
193 /* Print commands in specified category */
194 static void
195 print_bthid_command(struct bthid_command *category)
196 {
197 	struct bthid_command	*c = NULL;
198 
199 	for (c = category; c->command != NULL; c++)
200 		fprintf(stdout, "\t%s\n", c->command);
201 } /* print_bthid_command */
202 
203 /* Usage */
204 static void
205 usage(void)
206 {
207 	fprintf(stderr,
208 "Usage: bthidcontrol options command\n" \
209 "Where options are:\n"
210 "	-a bdaddr	specify bdaddr\n" \
211 "	-c file		specify path to the bthidd config file\n" \
212 "	-H file		specify path to the bthidd HIDs file\n" \
213 "	-h		display usage and quit\n" \
214 "	-v		be verbose\n" \
215 "	command		one of the supported commands\n");
216 	exit(255);
217 } /* usage */
218 
219