Home | History | Annotate | Line # | Download | only in sdpquery
sdpquery.c revision 1.4
      1 /*	$NetBSD: sdpquery.c,v 1.4 2008/07/21 14:19:26 lukem 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.  All rights reserved.");
     36 __RCSID("$NetBSD: sdpquery.c,v 1.4 2008/07/21 14:19:26 lukem Exp $");
     37 
     38 #include <assert.h>
     39 #include <bluetooth.h>
     40 #include <err.h>
     41 #include <errno.h>
     42 #include <sdp.h>
     43 #include <stdio.h>
     44 #include <stdlib.h>
     45 #include <string.h>
     46 #include <unistd.h>
     47 
     48 #include "sdpquery.h"
     49 
     50 static void usage(void);
     51 
     52 const char *control_socket;
     53 
     54 static struct command {
     55 	const char	*command;
     56 	int		(*handler)(bdaddr_t *, bdaddr_t *, int, char const **);
     57 	const char	*usage;
     58 } commands[] = {
     59 	{ "Browse",	do_sdp_browse,	"[UUID]"	},
     60 	{ "Search",	do_sdp_search,	"<service>"	},
     61 	{ NULL,		NULL,		NULL		}
     62 };
     63 
     64 int
     65 main(int argc, char *argv[])
     66 {
     67 	bdaddr_t	laddr, raddr;
     68 	struct command *cmd;
     69 	int		ch, local;
     70 
     71 	bdaddr_copy(&laddr, BDADDR_ANY);
     72 	bdaddr_copy(&raddr, BDADDR_ANY);
     73 	control_socket = NULL;
     74 	local = 0;
     75 
     76 	while ((ch = getopt(argc, argv, "a:c:d:hl")) != -1) {
     77 		switch (ch) {
     78 		case 'a': /* remote address */
     79 			if (!bt_aton(optarg, &raddr)) {
     80 				struct hostent  *he = NULL;
     81 
     82 				if ((he = bt_gethostbyname(optarg)) == NULL)
     83 					errx(EXIT_FAILURE, "%s: %s",
     84 						optarg, hstrerror(h_errno));
     85 
     86 				bdaddr_copy(&raddr, (bdaddr_t *)he->h_addr);
     87 			}
     88 			break;
     89 
     90 		case 'c':
     91 			control_socket = optarg;
     92 			break;
     93 
     94 		case 'd': /* local device address */
     95 			if (!bt_devaddr(optarg, &laddr))
     96 				err(EXIT_FAILURE, "%s", optarg);
     97 
     98 			break;
     99 
    100 		case 'l': /* local sdpd */
    101 			local = 1;
    102 			break;
    103 
    104 		case 'h':
    105 		default:
    106 			usage();
    107 			/* NOT REACHED */
    108 		}
    109 	}
    110 
    111 	argc -= optind;
    112 	argv += optind;
    113 
    114 	optind = 0;
    115 	optreset = 1;
    116 
    117 	if (argc < 1
    118 	    || (bdaddr_any(&raddr) && !local)
    119 	    || (!bdaddr_any(&raddr) && local))
    120 		usage();
    121 
    122 	for (cmd = commands ; cmd->command != NULL; cmd++) {
    123 		if (strcasecmp(*argv, cmd->command) == 0)
    124 			return (*cmd->handler)(&laddr, &raddr, --argc, (char const **)++argv);
    125 	}
    126 
    127 	errx(EXIT_FAILURE, "%s: Unknown Command", *argv);
    128 }
    129 
    130 /* Usage */
    131 static void
    132 usage(void)
    133 {
    134 	struct command *cmd;
    135 
    136 	fprintf(stderr,
    137 		"Usage: %s [-d device] -a bdaddr <command> [parameters..]\n"
    138 		"       %s [-c path] -l <command> [parameters..]\n"
    139 		"\n", getprogname(), getprogname());
    140 
    141 	fprintf(stderr,
    142 		"Where:\n"
    143 		"\t-a bdaddr    remote address\n"
    144 		"\t-c path      path to control socket\n"
    145 		"\t-d device    local device address\n"
    146 		"\t-l           connect to the local SDP server via control socket\n"
    147 		"\t-h           display usage and quit\n"
    148 		"\n"
    149 		"Commands:\n");
    150 
    151 	for (cmd = commands ; cmd->command != NULL ; cmd++)
    152 		fprintf(stderr, "\t%-13s%s\n", cmd->command, cmd->usage);
    153 
    154 	exit(EXIT_FAILURE);
    155 }
    156