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