4 * Copyright (c) 2001-2003 Maksim Yevmenkin <m_evmenkin@yahoo.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
28 * $Id: sdpcontrol.c,v 1.1 2003/09/08 02:27:27 max Exp $
33 #define L2CAP_SOCKET_CHECKED
34 #include <bluetooth.h>
42 #include "sdpcontrol.h"
45 static int do_sdp_command (bdaddr_p, char const *, int,
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);
54 main(int argc, char *argv[])
56 char const *control = SDP_LOCAL_PATH;
60 memset(&bdaddr, 0, sizeof(bdaddr));
63 /* Process command line arguments */
64 while ((n = getopt(argc, argv, "a:c:lh")) != -1) {
66 case 'a': /* bdaddr */
67 if (!bt_aton(optarg, &bdaddr)) {
68 struct hostent *he = NULL;
70 if ((he = bt_gethostbyname(optarg)) == NULL)
71 errx(1, "%s: %s", optarg, hstrerror(h_errno));
73 memcpy(&bdaddr, he->h_addr, sizeof(bdaddr));
77 case 'c': /* control socket */
81 case 'l': /* local sdpd */
98 return (do_sdp_command(&bdaddr, control, local, argc, argv));
101 /* Execute commands */
103 do_sdp_command(bdaddr_p bdaddr, char const *control, int local,
104 int argc, char **argv)
107 struct sdp_command *c = NULL;
112 if (strcasecmp(cmd, "help") == 0) {
117 fprintf(stdout, "Supported commands:\n");
118 print_sdp_command(sdp_commands);
119 fprintf(stdout, "\nFor more information use " \
129 c = find_sdp_command(cmd, sdp_commands);
131 fprintf(stdout, "Unknown command: \"%s\"\n", cmd);
137 if (memcmp(bdaddr, NG_HCI_BDADDR_ANY, sizeof(*bdaddr)) == 0)
140 xs = sdp_open(NG_HCI_BDADDR_ANY, bdaddr);
142 xs = sdp_open_local(control);
145 errx(1, "Could not create SDP session object");
146 if (sdp_error(xs) == 0)
147 e = (c->handler)(xs, -- argc, ++ argv);
159 fprintf(stdout, "Could not execute command \"%s\". %s\n",
160 cmd, strerror(sdp_error(xs)));
164 fprintf(stdout, "Usage: %s\n%s\n", c->command, c->description);
167 default: assert(0); break;
173 } /* do_sdp_command */
175 /* Try to find command in specified category */
176 static struct sdp_command *
177 find_sdp_command(char const *command, struct sdp_command *category)
179 struct sdp_command *c = NULL;
181 for (c = category; c->command != NULL; c++) {
182 char *c_end = strchr(c->command, ' ');
185 int len = c_end - c->command;
187 if (strncasecmp(command, c->command, len) == 0)
189 } else if (strcasecmp(command, c->command) == 0)
194 } /* find_sdp_command */
196 /* Print commands in specified category */
198 print_sdp_command(struct sdp_command *category)
200 struct sdp_command *c = NULL;
202 for (c = category; c->command != NULL; c++)
203 fprintf(stdout, "\t%s\n", c->command);
204 } /* print_sdp_command */
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);