xref: /netbsd-src/usr.bin/sdpquery/sdpquery.c (revision ce099b40997c43048fb78bd578195f81d2456523)
1 /*	$NetBSD: sdpquery.c,v 1.3 2007/03/30 21:25:00 plunky Exp $	*/
2 
3 /*-
4  * Copyright (c) 2006 Itronix Inc.
5  * All rights reserved.
6  *
7  * Written by Iain Hibbert for Itronix Inc.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
17  * 3. The name of Itronix Inc. may not be used to endorse
18  *    or promote products derived from this software without specific
19  *    prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
25  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28  * ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 __COPYRIGHT("@(#) Copyright (c) 2006 Itronix, Inc.\n"
36 	    "All rights reserved.\n");
37 __RCSID("$NetBSD: sdpquery.c,v 1.3 2007/03/30 21:25:00 plunky Exp $");
38 
39 #include <assert.h>
40 #include <bluetooth.h>
41 #include <err.h>
42 #include <errno.h>
43 #include <sdp.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48 
49 #include "sdpquery.h"
50 
51 static void usage(void);
52 
53 const char *control_socket;
54 
55 static struct command {
56 	const char	*command;
57 	int		(*handler)(bdaddr_t *, bdaddr_t *, int, char const **);
58 	const char	*usage;
59 } commands[] = {
60 	{ "Browse",	do_sdp_browse,	"[UUID]"	},
61 	{ "Search",	do_sdp_search,	"<service>"	},
62 	{ NULL,		NULL,		NULL		}
63 };
64 
65 int
66 main(int argc, char *argv[])
67 {
68 	bdaddr_t	laddr, raddr;
69 	struct command *cmd;
70 	int		ch, local;
71 
72 	bdaddr_copy(&laddr, BDADDR_ANY);
73 	bdaddr_copy(&raddr, BDADDR_ANY);
74 	control_socket = NULL;
75 	local = 0;
76 
77 	while ((ch = getopt(argc, argv, "a:c:d:hl")) != -1) {
78 		switch (ch) {
79 		case 'a': /* remote address */
80 			if (!bt_aton(optarg, &raddr)) {
81 				struct hostent  *he = NULL;
82 
83 				if ((he = bt_gethostbyname(optarg)) == NULL)
84 					errx(EXIT_FAILURE, "%s: %s",
85 						optarg, hstrerror(h_errno));
86 
87 				bdaddr_copy(&raddr, (bdaddr_t *)he->h_addr);
88 			}
89 			break;
90 
91 		case 'c':
92 			control_socket = optarg;
93 			break;
94 
95 		case 'd': /* local device address */
96 			if (!bt_devaddr(optarg, &laddr))
97 				err(EXIT_FAILURE, "%s", optarg);
98 
99 			break;
100 
101 		case 'l': /* local sdpd */
102 			local = 1;
103 			break;
104 
105 		case 'h':
106 		default:
107 			usage();
108 			/* NOT REACHED */
109 		}
110 	}
111 
112 	argc -= optind;
113 	argv += optind;
114 
115 	optind = 0;
116 	optreset = 1;
117 
118 	if (argc < 1
119 	    || (bdaddr_any(&raddr) && !local)
120 	    || (!bdaddr_any(&raddr) && local))
121 		usage();
122 
123 	for (cmd = commands ; cmd->command != NULL; cmd++) {
124 		if (strcasecmp(*argv, cmd->command) == 0)
125 			return (*cmd->handler)(&laddr, &raddr, --argc, (char const **)++argv);
126 	}
127 
128 	errx(EXIT_FAILURE, "%s: Unknown Command", *argv);
129 }
130 
131 /* Usage */
132 static void
133 usage(void)
134 {
135 	struct command *cmd;
136 
137 	fprintf(stderr,
138 		"Usage: %s [-d device] -a bdaddr <command> [parameters..]\n"
139 		"       %s [-c path] -l <command> [parameters..]\n"
140 		"\n", getprogname(), getprogname());
141 
142 	fprintf(stderr,
143 		"Where:\n"
144 		"\t-a bdaddr    remote address\n"
145 		"\t-c path      path to control socket\n"
146 		"\t-d device    local device address\n"
147 		"\t-l           connect to the local SDP server via control socket\n"
148 		"\t-h           display usage and quit\n"
149 		"\n"
150 		"Commands:\n");
151 
152 	for (cmd = commands ; cmd->command != NULL ; cmd++)
153 		fprintf(stderr, "\t%-13s%s\n", cmd->command, cmd->usage);
154 
155 	exit(EXIT_FAILURE);
156 }
157