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 #include <bluetooth.h>
41 #include "sdpcontrol.h"
44 static int do_sdp_command (bdaddr_p, char const *, int,
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);
53 main(int argc, char *argv[])
55 char const *control = SDP_LOCAL_PATH;
59 memset(&bdaddr, 0, sizeof(bdaddr));
62 /* Process command line arguments */
63 while ((n = getopt(argc, argv, "a:c:lh")) != -1) {
65 case 'a': /* bdaddr */
66 if (!bt_aton(optarg, &bdaddr)) {
67 struct hostent *he = NULL;
69 if ((he = bt_gethostbyname(optarg)) == NULL)
70 errx(1, "%s: %s", optarg, hstrerror(h_errno));
72 memcpy(&bdaddr, he->h_addr, sizeof(bdaddr));
76 case 'c': /* control socket */
80 case 'l': /* local sdpd */
97 return (do_sdp_command(&bdaddr, control, local, argc, argv));
100 /* Execute commands */
102 do_sdp_command(bdaddr_p bdaddr, char const *control, int local,
103 int argc, char **argv)
106 struct sdp_command *c = NULL;
111 if (strcasecmp(cmd, "help") == 0) {
116 fprintf(stdout, "Supported commands:\n");
117 print_sdp_command(sdp_commands);
118 fprintf(stdout, "\nFor more information use " \
128 c = find_sdp_command(cmd, sdp_commands);
130 fprintf(stdout, "Unknown command: \"%s\"\n", cmd);
136 if (memcmp(bdaddr, NG_HCI_BDADDR_ANY, sizeof(*bdaddr)) == 0)
139 xs = sdp_open(NG_HCI_BDADDR_ANY, bdaddr);
141 xs = sdp_open_local(control);
144 errx(1, "Could not create SDP session object");
145 if (sdp_error(xs) == 0)
146 e = (c->handler)(xs, -- argc, ++ argv);
158 fprintf(stdout, "Could not execute command \"%s\". %s\n",
159 cmd, strerror(sdp_error(xs)));
163 fprintf(stdout, "Usage: %s\n%s\n", c->command, c->description);
166 default: assert(0); break;
172 } /* do_sdp_command */
174 /* Try to find command in specified category */
175 static struct sdp_command *
176 find_sdp_command(char const *command, struct sdp_command *category)
178 struct sdp_command *c = NULL;
180 for (c = category; c->command != NULL; c++) {
181 char *c_end = strchr(c->command, ' ');
184 int len = c_end - c->command;
186 if (strncasecmp(command, c->command, len) == 0)
188 } else if (strcasecmp(command, c->command) == 0)
193 } /* find_sdp_command */
195 /* Print commands in specified category */
197 print_sdp_command(struct sdp_command *category)
199 struct sdp_command *c = NULL;
201 for (c = category; c->command != NULL; c++)
202 fprintf(stdout, "\t%s\n", c->command);
203 } /* print_sdp_command */
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);