4 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
6 * Copyright (c) 2001-2003 Maksim Yevmenkin <m_evmenkin@yahoo.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * $Id: sdpcontrol.c,v 1.1 2003/09/08 02:27:27 max Exp $
35 #define L2CAP_SOCKET_CHECKED
36 #include <bluetooth.h>
44 #include "sdpcontrol.h"
47 static int do_sdp_command (bdaddr_p, char const *, int,
49 static struct sdp_command * find_sdp_command (char const *,
50 struct sdp_command *);
51 static void print_sdp_command (struct sdp_command *);
52 static void usage (void);
56 main(int argc, char *argv[])
58 char const *control = SDP_LOCAL_PATH;
62 memset(&bdaddr, 0, sizeof(bdaddr));
65 /* Process command line arguments */
66 while ((n = getopt(argc, argv, "a:c:lh")) != -1) {
68 case 'a': /* bdaddr */
69 if (!bt_aton(optarg, &bdaddr)) {
70 struct hostent *he = NULL;
72 if ((he = bt_gethostbyname(optarg)) == NULL)
73 errx(1, "%s: %s", optarg, hstrerror(h_errno));
75 memcpy(&bdaddr, he->h_addr, sizeof(bdaddr));
79 case 'c': /* control socket */
83 case 'l': /* local sdpd */
100 return (do_sdp_command(&bdaddr, control, local, argc, argv));
103 /* Execute commands */
105 do_sdp_command(bdaddr_p bdaddr, char const *control, int local,
106 int argc, char **argv)
109 struct sdp_command *c = NULL;
114 if (strcasecmp(cmd, "help") == 0) {
119 fprintf(stdout, "Supported commands:\n");
120 print_sdp_command(sdp_commands);
121 fprintf(stdout, "\nFor more information use " \
131 c = find_sdp_command(cmd, sdp_commands);
133 fprintf(stdout, "Unknown command: \"%s\"\n", cmd);
139 if (memcmp(bdaddr, NG_HCI_BDADDR_ANY, sizeof(*bdaddr)) == 0)
142 xs = sdp_open(NG_HCI_BDADDR_ANY, bdaddr);
144 xs = sdp_open_local(control);
147 errx(1, "Could not create SDP session object");
148 if (sdp_error(xs) == 0)
149 e = (c->handler)(xs, -- argc, ++ argv);
161 fprintf(stdout, "Could not execute command \"%s\". %s\n",
162 cmd, strerror(sdp_error(xs)));
166 fprintf(stdout, "Usage: %s\n%s\n", c->command, c->description);
169 default: assert(0); break;
175 } /* do_sdp_command */
177 /* Try to find command in specified category */
178 static struct sdp_command *
179 find_sdp_command(char const *command, struct sdp_command *category)
181 struct sdp_command *c = NULL;
183 for (c = category; c->command != NULL; c++) {
184 char *c_end = strchr(c->command, ' ');
187 int len = c_end - c->command;
189 if (strncasecmp(command, c->command, len) == 0)
191 } else if (strcasecmp(command, c->command) == 0)
196 } /* find_sdp_command */
198 /* Print commands in specified category */
200 print_sdp_command(struct sdp_command *category)
202 struct sdp_command *c = NULL;
204 for (c = category; c->command != NULL; c++)
205 fprintf(stdout, "\t%s\n", c->command);
206 } /* print_sdp_command */
213 "Usage: sdpcontrol options command\n" \
214 "Where options are:\n"
215 " -a address address to connect to\n" \
216 " -c path path to the control socket (default is %s)\n" \
217 " -h display usage and quit\n" \
218 " -l connect to the local SDP server via control socket\n" \
219 " command one of the supported commands\n", SDP_LOCAL_PATH);