Home | History | Annotate | Line # | Download | only in sdpquery
sdpquery.c revision 1.6
      1 /*	$NetBSD: sdpquery.c,v 1.6 2011/08/14 13:27:47 christos 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) 2009 The NetBSD Foundation, Inc.\
     36  Copyright (c) 2006 Itronix, Inc.\
     37  All rights reserved.");
     38 __RCSID("$NetBSD: sdpquery.c,v 1.6 2011/08/14 13:27:47 christos Exp $");
     39 
     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 bdaddr_t local_addr;
     56 bdaddr_t remote_addr;
     57 
     58 bool Nflag;	/* numerical output */
     59 bool Rflag;	/* uncooked output */
     60 bool Xflag;	/* hex output */
     61 
     62 static struct command {
     63 	const char	*command;
     64 	int		(*handler)(int, char const **);
     65 	const char	*usage;
     66 } commands[] = {
     67 	{ "Browse",	do_sdp_browse,	"[group]"		},
     68 	{ "Record",	do_sdp_record,	"handle [handle...]"	},
     69 	{ "Search",	do_sdp_search,	"uuid [uuid...]"	},
     70 	{ NULL,		NULL,		NULL			}
     71 };
     72 
     73 int
     74 main(int argc, char *argv[])
     75 {
     76 	struct command *cmd;
     77 	int		ch, local;
     78 
     79 	bdaddr_copy(&local_addr, BDADDR_ANY);
     80 	bdaddr_copy(&remote_addr, BDADDR_ANY);
     81 	control_socket = NULL;
     82 	Nflag = Rflag = Xflag = false;
     83 	local = 0;
     84 
     85 	while ((ch = getopt(argc, argv, "a:c:d:hlNRX")) != -1) {
     86 		switch (ch) {
     87 		case 'a': /* remote address */
     88 			if (!bt_aton(optarg, &remote_addr)) {
     89 				struct hostent  *he = NULL;
     90 
     91 				if ((he = bt_gethostbyname(optarg)) == NULL)
     92 					errx(EXIT_FAILURE, "%s: %s",
     93 						optarg, hstrerror(h_errno));
     94 
     95 				bdaddr_copy(&remote_addr, (bdaddr_t *)he->h_addr);
     96 			}
     97 			break;
     98 
     99 		case 'c':
    100 			control_socket = optarg;
    101 			break;
    102 
    103 		case 'd': /* local device address */
    104 			if (!bt_devaddr(optarg, &local_addr))
    105 				err(EXIT_FAILURE, "%s", optarg);
    106 
    107 			break;
    108 
    109 		case 'l': /* local sdpd */
    110 			local = 1;
    111 			break;
    112 
    113 		case 'N': /* Numerical output */
    114 			Nflag = true;
    115 			break;
    116 
    117 		case 'R': /* Raw output */
    118 			Rflag = true;
    119 			break;
    120 
    121 		case 'X': /* Hex output */
    122 			Xflag = true;
    123 			break;
    124 
    125 		case 'h':
    126 		default:
    127 			usage();
    128 			/* NOT REACHED */
    129 		}
    130 	}
    131 
    132 	argc -= optind;
    133 	argv += optind;
    134 
    135 	optind = 0;
    136 	optreset = 1;
    137 
    138 	if (argc < 1
    139 	    || (bdaddr_any(&remote_addr) && !local)
    140 	    || (!bdaddr_any(&remote_addr) && local))
    141 		usage();
    142 
    143 	for (cmd = commands ; cmd->command != NULL; cmd++) {
    144 		if (strcasecmp(*argv, cmd->command) == 0)
    145 			return (*cmd->handler)(--argc, (void *)++argv);
    146 	}
    147 
    148 	usage();
    149 	return EXIT_FAILURE;
    150 }
    151 
    152 static void
    153 usage(void)
    154 {
    155 	struct command *cmd;
    156 
    157 	fprintf(stderr,
    158 		"Usage: %s [-NRX] [-d device] -a bdaddr <command> [parameters..]\n"
    159 		"       %s [-NRX] [-c path] -l <command> [parameters..]\n"
    160 		"\n", getprogname(), getprogname());
    161 
    162 	fprintf(stderr,
    163 		"Where:\n"
    164 		"\t-a bdaddr    remote address\n"
    165 		"\t-c path      path to control socket\n"
    166 		"\t-d device    local device address\n"
    167 		"\t-l           query local SDP server daemon\n"
    168 		"\t-N           print numerical values\n"
    169 		"\t-R           print raw attribute values\n"
    170 		"\t-X           print attribute values in hex\n"
    171 		"\n"
    172 		"Commands:\n");
    173 
    174 	for (cmd = commands ; cmd->command != NULL ; cmd++)
    175 		fprintf(stderr, "\t%-13s%s\n", cmd->command, cmd->usage);
    176 
    177 	exit(EXIT_FAILURE);
    178 }
    179