xref: /freebsd/usr.sbin/bluetooth/sdpcontrol/sdpcontrol.c (revision 39ee7a7a6bdd1557b1c3532abf60d139798ac88b)
1 /*
2  * sdpcontrol.c
3  *
4  * Copyright (c) 2001-2003 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: sdpcontrol.c,v 1.1 2003/09/08 02:27:27 max Exp $
29  * $FreeBSD$
30  */
31 
32 #include <assert.h>
33 #define L2CAP_SOCKET_CHECKED
34 #include <bluetooth.h>
35 #include <err.h>
36 #include <errno.h>
37 #include <sdp.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42 #include "sdpcontrol.h"
43 
44 /* Prototypes */
45 static int                  do_sdp_command	(bdaddr_p, char const *, int,
46 						 int, char **);
47 static struct sdp_command * find_sdp_command	(char const *,
48 						 struct sdp_command *);
49 static void                 print_sdp_command	(struct sdp_command *);
50 static void                 usage		(void);
51 
52 /* Main */
53 int
54 main(int argc, char *argv[])
55 {
56 	char const	*control = SDP_LOCAL_PATH;
57 	int		 n, local;
58 	bdaddr_t	 bdaddr;
59 
60 	memset(&bdaddr, 0, sizeof(bdaddr));
61 	local = 0;
62 
63 	/* Process command line arguments */
64 	while ((n = getopt(argc, argv, "a:c:lh")) != -1) {
65 		switch (n) {
66 		case 'a': /* bdaddr */
67 			if (!bt_aton(optarg, &bdaddr)) {
68 				struct hostent  *he = NULL;
69 
70 				if ((he = bt_gethostbyname(optarg)) == NULL)
71 					errx(1, "%s: %s", optarg, hstrerror(h_errno));
72 
73 				memcpy(&bdaddr, he->h_addr, sizeof(bdaddr));
74 			}
75 			break;
76 
77 		case 'c': /* control socket */
78 			control = optarg;
79 			break;
80 
81 		case 'l': /* local sdpd */
82 			local = 1;
83 			break;
84 
85 		case 'h':
86 		default:
87 			usage();
88 			/* NOT REACHED */
89 		}
90 	}
91 
92 	argc -= optind;
93 	argv += optind;
94 
95 	if (*argv == NULL)
96 		usage();
97 
98 	return (do_sdp_command(&bdaddr, control, local, argc, argv));
99 }
100 
101 /* Execute commands */
102 static int
103 do_sdp_command(bdaddr_p bdaddr, char const *control, int local,
104 		int argc, char **argv)
105 {
106 	char			*cmd = argv[0];
107 	struct sdp_command	*c = NULL;
108 	void			*xs = NULL;
109 	int			 e, help;
110 
111 	help = 0;
112 	if (strcasecmp(cmd, "help") == 0) {
113 		argc --;
114 		argv ++;
115 
116 		if (argc <= 0) {
117 			fprintf(stdout, "Supported commands:\n");
118 			print_sdp_command(sdp_commands);
119 			fprintf(stdout, "\nFor more information use " \
120 				"'help command'\n");
121 
122 			return (OK);
123 		}
124 
125 		help = 1;
126 		cmd = argv[0];
127 	}
128 
129 	c = find_sdp_command(cmd, sdp_commands);
130 	if (c == NULL) {
131 		fprintf(stdout, "Unknown command: \"%s\"\n", cmd);
132 		return (ERROR);
133 	}
134 
135 	if (!help) {
136 		if (!local) {
137 			if (memcmp(bdaddr, NG_HCI_BDADDR_ANY, sizeof(*bdaddr)) == 0)
138 				usage();
139 
140 			xs = sdp_open(NG_HCI_BDADDR_ANY, bdaddr);
141 		} else
142 			xs = sdp_open_local(control);
143 
144 		if (xs == NULL)
145 			errx(1, "Could not create SDP session object");
146 		if (sdp_error(xs) == 0)
147 			e = (c->handler)(xs, -- argc, ++ argv);
148 		else
149 			e = ERROR;
150 	} else
151 		e = USAGE;
152 
153 	switch (e) {
154 	case OK:
155 	case FAILED:
156 		break;
157 
158 	case ERROR:
159 		fprintf(stdout, "Could not execute command \"%s\". %s\n",
160 			cmd, strerror(sdp_error(xs)));
161 		break;
162 
163 	case USAGE:
164 		fprintf(stdout, "Usage: %s\n%s\n", c->command, c->description);
165 		break;
166 
167 	default: assert(0); break;
168 	}
169 
170 	sdp_close(xs);
171 
172 	return (e);
173 } /* do_sdp_command */
174 
175 /* Try to find command in specified category */
176 static struct sdp_command *
177 find_sdp_command(char const *command, struct sdp_command *category)
178 {
179 	struct sdp_command	*c = NULL;
180 
181 	for (c = category; c->command != NULL; c++) {
182 		char	*c_end = strchr(c->command, ' ');
183 
184 		if (c_end != NULL) {
185 			int	len = c_end - c->command;
186 
187 			if (strncasecmp(command, c->command, len) == 0)
188 				return (c);
189 		} else if (strcasecmp(command, c->command) == 0)
190 				return (c);
191 	}
192 
193 	return (NULL);
194 } /* find_sdp_command */
195 
196 /* Print commands in specified category */
197 static void
198 print_sdp_command(struct sdp_command *category)
199 {
200 	struct sdp_command	*c = NULL;
201 
202 	for (c = category; c->command != NULL; c++)
203 		fprintf(stdout, "\t%s\n", c->command);
204 } /* print_sdp_command */
205 
206 /* Usage */
207 static void
208 usage(void)
209 {
210 	fprintf(stderr,
211 "Usage: sdpcontrol options command\n" \
212 "Where options are:\n"
213 "	-a address	address to connect to\n" \
214 "	-c path		path to the control socket (default is %s)\n" \
215 "	-h		display usage and quit\n" \
216 "	-l		connect to the local SDP server via control socket\n" \
217 "	command		one of the supported commands\n", SDP_LOCAL_PATH);
218 	exit(255);
219 } /* usage */
220 
221