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