sdpquery.c revision 1.1 1 /* $NetBSD: sdpquery.c,v 1.1 2006/06/19 15:44:56 gdamore 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.1 2006/06/19 15:44:56 gdamore 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 { "Config", do_sdp_config, "<type>" },
63 { NULL }
64 };
65
66 int
67 main(int argc, char *argv[])
68 {
69 bdaddr_t laddr, raddr;
70 struct command *cmd;
71 int ch, local;
72
73 bdaddr_copy(&laddr, BDADDR_ANY);
74 bdaddr_copy(&raddr, BDADDR_ANY);
75 control_socket = NULL;
76 local = 0;
77
78 while ((ch = getopt(argc, argv, "a:c:d:hl")) != -1) {
79 switch (ch) {
80 case 'a': /* remote address */
81 if (!bt_aton(optarg, &raddr)) {
82 struct hostent *he = NULL;
83
84 if ((he = bt_gethostbyname(optarg)) == NULL)
85 errx(EXIT_FAILURE, "%s: %s",
86 optarg, hstrerror(h_errno));
87
88 bdaddr_copy(&raddr, (bdaddr_t *)he->h_addr);
89 }
90 break;
91
92 case 'c':
93 control_socket = optarg;
94 break;
95
96 case 'd': /* local device address */
97 if (!bt_devaddr(optarg, &laddr))
98 err(EXIT_FAILURE, "%s", optarg);
99
100 break;
101
102 case 'l': /* local sdpd */
103 local = 1;
104 break;
105
106 case 'h':
107 default:
108 usage();
109 /* NOT REACHED */
110 }
111 }
112
113 argc -= optind;
114 argv += optind;
115
116 optind = 0;
117 optreset = 1;
118
119 if (argc < 1
120 || (bdaddr_any(&raddr) && !local)
121 || (!bdaddr_any(&raddr) && local))
122 usage();
123
124 for (cmd = commands ; cmd->command != NULL; cmd++) {
125 if (strcasecmp(*argv, cmd->command) == 0)
126 return (*cmd->handler)(&laddr, &raddr, --argc, (char const **)++argv);
127 }
128
129 errx(EXIT_FAILURE, "%s: Unknown Command", *argv);
130 }
131
132 /* Usage */
133 static void
134 usage(void)
135 {
136 struct command *cmd;
137
138 fprintf(stderr,
139 "Usage: %s [-d device] -a bdaddr <command> [parameters..]\n"
140 " %s [-c path] -l <command> [parameters..]\n"
141 "\n", getprogname(), getprogname());
142
143 fprintf(stderr,
144 "Where:\n"
145 "\t-a bdaddr remote address\n"
146 "\t-c path path to control socket\n"
147 "\t-d device local device address\n"
148 "\t-l connect to the local SDP server via control socket\n"
149 "\t-h display usage and quit\n"
150 "\n"
151 "Commands:\n");
152
153 for (cmd = commands ; cmd->command != NULL ; cmd++)
154 fprintf(stderr, "\t%-13s%s\n", cmd->command, cmd->usage);
155
156 exit(EXIT_FAILURE);
157 }
158