Home | History | Annotate | Line # | Download | only in sdpquery
sdpquery.c revision 1.5
      1  1.5   plunky /*	$NetBSD: sdpquery.c,v 1.5 2009/05/12 18:37:50 plunky Exp $	*/
      2  1.1  gdamore 
      3  1.1  gdamore /*-
      4  1.1  gdamore  * Copyright (c) 2006 Itronix Inc.
      5  1.1  gdamore  * All rights reserved.
      6  1.1  gdamore  *
      7  1.1  gdamore  * Written by Iain Hibbert for Itronix Inc.
      8  1.1  gdamore  *
      9  1.1  gdamore  * Redistribution and use in source and binary forms, with or without
     10  1.1  gdamore  * modification, are permitted provided that the following conditions
     11  1.1  gdamore  * are met:
     12  1.1  gdamore  * 1. Redistributions of source code must retain the above copyright
     13  1.1  gdamore  *    notice, this list of conditions and the following disclaimer.
     14  1.1  gdamore  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1  gdamore  *    notice, this list of conditions and the following disclaimer in the
     16  1.1  gdamore  *    documentation and/or other materials provided with the distribution.
     17  1.1  gdamore  * 3. The name of Itronix Inc. may not be used to endorse
     18  1.1  gdamore  *    or promote products derived from this software without specific
     19  1.1  gdamore  *    prior written permission.
     20  1.1  gdamore  *
     21  1.1  gdamore  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
     22  1.1  gdamore  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23  1.1  gdamore  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24  1.1  gdamore  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
     25  1.1  gdamore  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     26  1.1  gdamore  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     27  1.1  gdamore  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     28  1.1  gdamore  * ON ANY THEORY OF LIABILITY, WHETHER IN
     29  1.1  gdamore  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30  1.1  gdamore  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31  1.1  gdamore  * POSSIBILITY OF SUCH DAMAGE.
     32  1.1  gdamore  */
     33  1.1  gdamore 
     34  1.1  gdamore #include <sys/cdefs.h>
     35  1.5   plunky __COPYRIGHT("@(#) Copyright (c) 2009 The NetBSD Foundation, Inc.\
     36  1.5   plunky  Copyright (c) 2006 Itronix, Inc.\
     37  1.5   plunky  All rights reserved.");
     38  1.5   plunky __RCSID("$NetBSD: sdpquery.c,v 1.5 2009/05/12 18:37:50 plunky Exp $");
     39  1.1  gdamore 
     40  1.1  gdamore #include <bluetooth.h>
     41  1.1  gdamore #include <err.h>
     42  1.1  gdamore #include <errno.h>
     43  1.1  gdamore #include <sdp.h>
     44  1.1  gdamore #include <stdio.h>
     45  1.1  gdamore #include <stdlib.h>
     46  1.1  gdamore #include <string.h>
     47  1.1  gdamore #include <unistd.h>
     48  1.1  gdamore 
     49  1.1  gdamore #include "sdpquery.h"
     50  1.1  gdamore 
     51  1.1  gdamore static void usage(void);
     52  1.1  gdamore 
     53  1.1  gdamore const char *control_socket;
     54  1.1  gdamore 
     55  1.5   plunky bdaddr_t local_addr;
     56  1.5   plunky bdaddr_t remote_addr;
     57  1.5   plunky 
     58  1.5   plunky bool Nflag;	/* numerical output */
     59  1.5   plunky bool Rflag;	/* uncooked output */
     60  1.5   plunky bool Xflag;	/* hex output */
     61  1.5   plunky 
     62  1.1  gdamore static struct command {
     63  1.1  gdamore 	const char	*command;
     64  1.5   plunky 	int		(*handler)(int, char const **);
     65  1.1  gdamore 	const char	*usage;
     66  1.1  gdamore } commands[] = {
     67  1.5   plunky 	{ "Browse",	do_sdp_browse,	"[group]"		},
     68  1.5   plunky 	{ "Record",	do_sdp_record,	"handle [handle...]"	},
     69  1.5   plunky 	{ "Search",	do_sdp_search,	"uuid [uuid...]"	},
     70  1.5   plunky 	{ NULL,		NULL,		NULL			}
     71  1.1  gdamore };
     72  1.1  gdamore 
     73  1.1  gdamore int
     74  1.1  gdamore main(int argc, char *argv[])
     75  1.1  gdamore {
     76  1.1  gdamore 	struct command *cmd;
     77  1.1  gdamore 	int		ch, local;
     78  1.1  gdamore 
     79  1.5   plunky 	bdaddr_copy(&local_addr, BDADDR_ANY);
     80  1.5   plunky 	bdaddr_copy(&remote_addr, BDADDR_ANY);
     81  1.1  gdamore 	control_socket = NULL;
     82  1.5   plunky 	Nflag = Rflag = Xflag = false;
     83  1.1  gdamore 	local = 0;
     84  1.1  gdamore 
     85  1.5   plunky 	while ((ch = getopt(argc, argv, "a:c:d:hlNRX")) != -1) {
     86  1.1  gdamore 		switch (ch) {
     87  1.1  gdamore 		case 'a': /* remote address */
     88  1.5   plunky 			if (!bt_aton(optarg, &remote_addr)) {
     89  1.1  gdamore 				struct hostent  *he = NULL;
     90  1.1  gdamore 
     91  1.1  gdamore 				if ((he = bt_gethostbyname(optarg)) == NULL)
     92  1.1  gdamore 					errx(EXIT_FAILURE, "%s: %s",
     93  1.1  gdamore 						optarg, hstrerror(h_errno));
     94  1.1  gdamore 
     95  1.5   plunky 				bdaddr_copy(&remote_addr, (bdaddr_t *)he->h_addr);
     96  1.1  gdamore 			}
     97  1.1  gdamore 			break;
     98  1.1  gdamore 
     99  1.1  gdamore 		case 'c':
    100  1.1  gdamore 			control_socket = optarg;
    101  1.1  gdamore 			break;
    102  1.1  gdamore 
    103  1.1  gdamore 		case 'd': /* local device address */
    104  1.5   plunky 			if (!bt_devaddr(optarg, &local_addr))
    105  1.1  gdamore 				err(EXIT_FAILURE, "%s", optarg);
    106  1.1  gdamore 
    107  1.1  gdamore 			break;
    108  1.1  gdamore 
    109  1.1  gdamore 		case 'l': /* local sdpd */
    110  1.1  gdamore 			local = 1;
    111  1.1  gdamore 			break;
    112  1.1  gdamore 
    113  1.5   plunky 		case 'N': /* Numerical output */
    114  1.5   plunky 			Nflag = true;
    115  1.5   plunky 			break;
    116  1.5   plunky 
    117  1.5   plunky 		case 'R': /* Raw output */
    118  1.5   plunky 			Rflag = true;
    119  1.5   plunky 			break;
    120  1.5   plunky 
    121  1.5   plunky 		case 'X': /* Hex output */
    122  1.5   plunky 			Xflag = true;
    123  1.5   plunky 			break;
    124  1.5   plunky 
    125  1.1  gdamore 		case 'h':
    126  1.1  gdamore 		default:
    127  1.1  gdamore 			usage();
    128  1.1  gdamore 			/* NOT REACHED */
    129  1.1  gdamore 		}
    130  1.1  gdamore 	}
    131  1.1  gdamore 
    132  1.1  gdamore 	argc -= optind;
    133  1.1  gdamore 	argv += optind;
    134  1.1  gdamore 
    135  1.1  gdamore 	optind = 0;
    136  1.1  gdamore 	optreset = 1;
    137  1.1  gdamore 
    138  1.1  gdamore 	if (argc < 1
    139  1.5   plunky 	    || (bdaddr_any(&remote_addr) && !local)
    140  1.5   plunky 	    || (!bdaddr_any(&remote_addr) && local))
    141  1.1  gdamore 		usage();
    142  1.1  gdamore 
    143  1.1  gdamore 	for (cmd = commands ; cmd->command != NULL; cmd++) {
    144  1.1  gdamore 		if (strcasecmp(*argv, cmd->command) == 0)
    145  1.5   plunky 			return (*cmd->handler)(--argc, (char const **)++argv);
    146  1.1  gdamore 	}
    147  1.1  gdamore 
    148  1.5   plunky 	usage();
    149  1.5   plunky 	return EXIT_FAILURE;
    150  1.1  gdamore }
    151  1.1  gdamore 
    152  1.1  gdamore static void
    153  1.1  gdamore usage(void)
    154  1.1  gdamore {
    155  1.1  gdamore 	struct command *cmd;
    156  1.1  gdamore 
    157  1.1  gdamore 	fprintf(stderr,
    158  1.5   plunky 		"Usage: %s [-NRX] [-d device] -a bdaddr <command> [parameters..]\n"
    159  1.5   plunky 		"       %s [-NRX] [-c path] -l <command> [parameters..]\n"
    160  1.1  gdamore 		"\n", getprogname(), getprogname());
    161  1.1  gdamore 
    162  1.1  gdamore 	fprintf(stderr,
    163  1.1  gdamore 		"Where:\n"
    164  1.1  gdamore 		"\t-a bdaddr    remote address\n"
    165  1.1  gdamore 		"\t-c path      path to control socket\n"
    166  1.1  gdamore 		"\t-d device    local device address\n"
    167  1.5   plunky 		"\t-l           query local SDP server daemon\n"
    168  1.5   plunky 		"\t-N           print numerical values\n"
    169  1.5   plunky 		"\t-R           print raw attribute values\n"
    170  1.5   plunky 		"\t-X           print attribute values in hex\n"
    171  1.1  gdamore 		"\n"
    172  1.1  gdamore 		"Commands:\n");
    173  1.1  gdamore 
    174  1.1  gdamore 	for (cmd = commands ; cmd->command != NULL ; cmd++)
    175  1.1  gdamore 		fprintf(stderr, "\t%-13s%s\n", cmd->command, cmd->usage);
    176  1.1  gdamore 
    177  1.1  gdamore 	exit(EXIT_FAILURE);
    178  1.1  gdamore }
    179