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