xref: /freebsd/usr.sbin/bluetooth/sdpcontrol/sdpcontrol.c (revision 0986ab12e44caea472245845f9a89ced4f137d73)
1 /*
2  * sdpcontrol.c
3  *
4  * Copyright (c) 2001-2003 Maksim Yevmenkin <m_evmenkin@yahoo.com>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $Id: sdpcontrol.c,v 1.1 2003/09/08 02:27:27 max Exp $
29  * $FreeBSD$
30  */
31 
32 #include <assert.h>
33 #include <bluetooth.h>
34 #include <err.h>
35 #include <errno.h>
36 #include <sdp.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
41 #include "sdpcontrol.h"
42 
43 /* Prototypes */
44 static int                  do_sdp_command	(bdaddr_p, int, char **);
45 static struct sdp_command * find_sdp_command	(char const *,
46 						 struct sdp_command *);
47 static void                 print_sdp_command	(struct sdp_command *);
48 static void                 usage		(void);
49 
50 /* Main */
51 int
52 main(int argc, char *argv[])
53 {
54 	int		n;
55 	bdaddr_t	bdaddr;
56 
57 	memset(&bdaddr, 0, sizeof(bdaddr));
58 
59 	/* Process command line arguments */
60 	while ((n = getopt(argc, argv, "a:h")) != -1) {
61 		switch (n) {
62 		case 'a':
63 			if (!bt_aton(optarg, &bdaddr)) {
64 				struct hostent  *he = NULL;
65 
66 				if ((he = bt_gethostbyname(optarg)) == NULL)
67 					errx(1, "%s: %s", optarg, hstrerror(h_errno));
68 
69 				memcpy(&bdaddr, he->h_addr, sizeof(bdaddr));
70 			}
71 			break;
72 
73 		case 'h':
74 		default:
75 			usage();
76 			/* NOT REACHED */
77 		}
78 	}
79 
80 	argc -= optind;
81 	argv += optind;
82 
83 	if (*argv == NULL)
84 		usage();
85 
86 	return (do_sdp_command(&bdaddr, argc, argv));
87 }
88 
89 /* Execute commands */
90 static int
91 do_sdp_command(bdaddr_p bdaddr, int argc, char **argv)
92 {
93 	char			*cmd = argv[0];
94 	struct sdp_command	*c = NULL;
95 	void			*xs = NULL;
96 	int			 e, help;
97 
98 	help = 0;
99 	if (strcasecmp(cmd, "help") == 0) {
100 		argc --;
101 		argv ++;
102 
103 		if (argc <= 0) {
104 			fprintf(stdout, "Supported commands:\n");
105 			print_sdp_command(sdp_commands);
106 			fprintf(stdout, "\nFor more information use " \
107 				"'help command'\n");
108 
109 			return (OK);
110 		}
111 
112 		help = 1;
113 		cmd = argv[0];
114 	}
115 
116 	c = find_sdp_command(cmd, sdp_commands);
117 	if (c == NULL) {
118 		fprintf(stdout, "Unknown command: \"%s\"\n", cmd);
119 		return (ERROR);
120 	}
121 
122 	if (!help) {
123 		if (memcmp(bdaddr, NG_HCI_BDADDR_ANY, sizeof(*bdaddr)) == 0)
124 			usage();
125 
126 		if ((xs = sdp_open(NG_HCI_BDADDR_ANY, bdaddr)) == NULL)
127 			errx(1, "Could not create SDP session object");
128 
129 		if (sdp_error(xs) == 0)
130 			e = (c->handler)(xs, -- argc, ++ argv);
131 		else
132 			e = ERROR;
133 	} else
134 		e = USAGE;
135 
136 	switch (e) {
137 	case OK:
138 	case FAILED:
139 		break;
140 
141 	case ERROR:
142 		fprintf(stdout, "Could not execute command \"%s\". %s\n",
143 			cmd, strerror(sdp_error(xs)));
144 		break;
145 
146 	case USAGE:
147 		fprintf(stdout, "Usage: %s\n%s\n", c->command, c->description);
148 		break;
149 
150 	default: assert(0); break;
151 	}
152 
153 	sdp_close(xs);
154 
155 	return (e);
156 } /* do_sdp_command */
157 
158 /* Try to find command in specified category */
159 static struct sdp_command *
160 find_sdp_command(char const *command, struct sdp_command *category)
161 {
162 	struct sdp_command	*c = NULL;
163 
164 	for (c = category; c->command != NULL; c++) {
165 		char	*c_end = strchr(c->command, ' ');
166 
167 		if (c_end != NULL) {
168 			int	len = c_end - c->command;
169 
170 			if (strncasecmp(command, c->command, len) == 0)
171 				return (c);
172 		} else if (strcasecmp(command, c->command) == 0)
173 				return (c);
174 	}
175 
176 	return (NULL);
177 } /* find_sdp_command */
178 
179 /* Print commands in specified category */
180 static void
181 print_sdp_command(struct sdp_command *category)
182 {
183 	struct sdp_command	*c = NULL;
184 
185 	for (c = category; c->command != NULL; c++)
186 		fprintf(stdout, "\t%s\n", c->command);
187 } /* print_sdp_command */
188 
189 /* Usage */
190 static void
191 usage(void)
192 {
193 	fprintf(stdout, "Usage: sdpcontrol -a BD_ADDR [-h] " \
194 			"cmd [p1] [..]]\n");
195 	exit(255);
196 } /* usage */
197 
198