command.c revision 1.3 1 1.3 plunky /* $NetBSD: command.c,v 1.3 2009/12/05 16:54:13 plunky Exp $ */
2 1.1 plunky
3 1.1 plunky /*-
4 1.1 plunky * Copyright (c) 2009 The NetBSD Foundation, Inc.
5 1.1 plunky * All rights reserved.
6 1.1 plunky *
7 1.1 plunky * This code is derived from software contributed to The NetBSD Foundation
8 1.1 plunky * by Iain Hibbert.
9 1.1 plunky *
10 1.1 plunky * Redistribution and use in source and binary forms, with or without
11 1.1 plunky * modification, are permitted provided that the following conditions
12 1.1 plunky * are met:
13 1.1 plunky * 1. Redistributions of source code must retain the above copyright
14 1.1 plunky * notice, this list of conditions and the following disclaimer.
15 1.1 plunky * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 plunky * notice, this list of conditions and the following disclaimer in the
17 1.1 plunky * documentation and/or other materials provided with the distribution.
18 1.1 plunky *
19 1.1 plunky * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 plunky * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 plunky * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 plunky * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 plunky * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 plunky * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 plunky * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 plunky * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 plunky * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 plunky * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 plunky * POSSIBILITY OF SUCH DAMAGE.
30 1.1 plunky */
31 1.1 plunky
32 1.1 plunky #include <sys/cdefs.h>
33 1.3 plunky __RCSID("$NetBSD: command.c,v 1.3 2009/12/05 16:54:13 plunky Exp $");
34 1.1 plunky
35 1.1 plunky #include <bluetooth.h>
36 1.1 plunky #include <err.h>
37 1.1 plunky #include <sdp.h>
38 1.1 plunky #include <stdlib.h>
39 1.1 plunky #include <string.h>
40 1.1 plunky
41 1.1 plunky #include "sdpquery.h"
42 1.1 plunky
43 1.1 plunky static sdp_session_t open_session(void);
44 1.1 plunky static void build_ssp(sdp_data_t *, int, const char **);
45 1.1 plunky
46 1.1 plunky static struct alias {
47 1.1 plunky uint16_t uuid;
48 1.1 plunky const char * name;
49 1.1 plunky const char * desc;
50 1.1 plunky } aliases[] = {
51 1.1 plunky { SDP_SERVICE_CLASS_ADVANCED_AUDIO_DISTRIBUTION,
52 1.1 plunky "A2DP", "Advanced Audio Distribution Profile" },
53 1.1 plunky { SDP_UUID_PROTOCOL_BNEP,
54 1.1 plunky "BNEP", "Bluetooth Network Encapsulation Protocol" },
55 1.1 plunky { SDP_SERVICE_CLASS_COMMON_ISDN_ACCESS,
56 1.1 plunky "CIP", "Common ISDN Access Service" },
57 1.1 plunky { SDP_SERVICE_CLASS_CORDLESS_TELEPHONY,
58 1.1 plunky "CTP", "Cordless Telephony Service" },
59 1.1 plunky { SDP_SERVICE_CLASS_DIALUP_NETWORKING,
60 1.1 plunky "DUN", "Dial Up Networking Service" },
61 1.1 plunky { SDP_SERVICE_CLASS_FAX,
62 1.1 plunky "FAX", "Fax Service" },
63 1.1 plunky { SDP_SERVICE_CLASS_OBEX_FILE_TRANSFER,
64 1.1 plunky "FTRN", "File Transfer Service" },
65 1.1 plunky { SDP_SERVICE_CLASS_GN,
66 1.1 plunky "GN", "Group ad-hoc Network Service" },
67 1.1 plunky { SDP_SERVICE_CLASS_HUMAN_INTERFACE_DEVICE,
68 1.1 plunky "HID", "Human Interface Device Service" },
69 1.1 plunky { SDP_SERVICE_CLASS_HANDSFREE,
70 1.1 plunky "HF", "Handsfree Service" },
71 1.1 plunky { SDP_SERVICE_CLASS_HEADSET,
72 1.1 plunky "HSET", "Headset Service" },
73 1.1 plunky { SDP_UUID_PROTOCOL_L2CAP,
74 1.1 plunky "L2CAP", "Logical Link Control and Adapatation Protocol" },
75 1.1 plunky { SDP_SERVICE_CLASS_LAN_ACCESS_USING_PPP,
76 1.1 plunky "LAN", "Lan access using PPP Service" },
77 1.1 plunky { SDP_SERVICE_CLASS_NAP,
78 1.1 plunky "NAP", "Network Access Point Service" },
79 1.1 plunky { SDP_UUID_PROTOCOL_OBEX,
80 1.1 plunky "OBEX", "Object Exchange Protocol" },
81 1.1 plunky { SDP_SERVICE_CLASS_OBEX_OBJECT_PUSH,
82 1.1 plunky "OPUSH", "Object Push Service" },
83 1.1 plunky { SDP_SERVICE_CLASS_PANU,
84 1.1 plunky "PANU", "Personal Area Networking User Service" },
85 1.3 plunky { SDP_SERVICE_CLASS_PNP_INFORMATION,
86 1.3 plunky "PNP", "PNP Information Service" },
87 1.1 plunky { SDP_UUID_PROTOCOL_RFCOMM,
88 1.1 plunky "RFCOMM", "RFCOMM Protocol" },
89 1.1 plunky { SDP_UUID_PROTOCOL_SDP,
90 1.1 plunky "SDP", "Service Discovery Protocol" },
91 1.1 plunky { SDP_SERVICE_CLASS_SERIAL_PORT,
92 1.1 plunky "SP", "Serial Port Service" },
93 1.1 plunky { SDP_SERVICE_CLASS_IR_MC_SYNC,
94 1.1 plunky "SYNC", "IrMC Sync Client Service" },
95 1.1 plunky };
96 1.1 plunky
97 1.1 plunky int
98 1.1 plunky do_sdp_browse(int argc, const char **argv)
99 1.1 plunky {
100 1.1 plunky #define STR(x) __STRING(x)
101 1.1 plunky const char *av = STR(SDP_SERVICE_CLASS_PUBLIC_BROWSE_GROUP);
102 1.1 plunky #undef STR
103 1.1 plunky
104 1.1 plunky if (argc > 1)
105 1.1 plunky errx(EXIT_FAILURE, "Too many arguments");
106 1.1 plunky
107 1.1 plunky if (argc == 0) {
108 1.1 plunky argc = 1;
109 1.1 plunky argv = &av;
110 1.1 plunky }
111 1.1 plunky
112 1.1 plunky return do_sdp_search(argc, argv);
113 1.1 plunky }
114 1.1 plunky
115 1.1 plunky int
116 1.1 plunky do_sdp_record(int argc, const char **argv)
117 1.1 plunky {
118 1.1 plunky sdp_session_t ss;
119 1.1 plunky sdp_data_t rsp;
120 1.1 plunky char * ep;
121 1.1 plunky unsigned long handle;
122 1.1 plunky bool rv;
123 1.1 plunky
124 1.1 plunky if (argc == 0)
125 1.1 plunky errx(EXIT_FAILURE, "Record handle required");
126 1.1 plunky
127 1.1 plunky ss = open_session();
128 1.1 plunky
129 1.1 plunky for (; argc-- > 0; argv++) {
130 1.1 plunky handle = strtoul(*argv, &ep, 0);
131 1.1 plunky if (*argv[0] == '\0' || *ep != '\0' || handle > UINT32_MAX)
132 1.1 plunky errx(EXIT_FAILURE, "Invalid handle: %s\n", *argv);
133 1.1 plunky
134 1.2 plunky rv = sdp_service_attribute(ss, (uint32_t)handle, NULL, &rsp);
135 1.1 plunky if (!rv)
136 1.1 plunky warn("%s", *argv);
137 1.1 plunky else
138 1.1 plunky print_record(&rsp);
139 1.1 plunky
140 1.1 plunky if (argc > 0)
141 1.1 plunky printf("\n\n");
142 1.1 plunky }
143 1.1 plunky
144 1.1 plunky sdp_close(ss);
145 1.1 plunky return EXIT_SUCCESS;
146 1.1 plunky }
147 1.1 plunky
148 1.1 plunky int
149 1.1 plunky do_sdp_search(int argc, const char **argv)
150 1.1 plunky {
151 1.1 plunky sdp_session_t ss;
152 1.1 plunky sdp_data_t ssp, rec, rsp;
153 1.1 plunky bool rv;
154 1.1 plunky
155 1.1 plunky if (argc < 1)
156 1.1 plunky errx(EXIT_FAILURE, "UUID required");
157 1.1 plunky
158 1.1 plunky if (argc > 12)
159 1.1 plunky errx(EXIT_FAILURE, "Too many UUIDs");
160 1.1 plunky
161 1.1 plunky build_ssp(&ssp, argc, argv);
162 1.1 plunky
163 1.1 plunky ss = open_session();
164 1.1 plunky
165 1.1 plunky rv = sdp_service_search_attribute(ss, &ssp, NULL, &rsp);
166 1.1 plunky if (!rv)
167 1.1 plunky err(EXIT_FAILURE, "sdp_service_search_attribute");
168 1.1 plunky
169 1.1 plunky while (sdp_get_seq(&rsp, &rec)) {
170 1.1 plunky if (!rv)
171 1.1 plunky printf("\n\n");
172 1.1 plunky else
173 1.1 plunky rv = false;
174 1.1 plunky
175 1.1 plunky print_record(&rec);
176 1.1 plunky }
177 1.1 plunky
178 1.1 plunky if (rsp.next != rsp.end) {
179 1.1 plunky printf("\n\nAdditional Data:\n");
180 1.1 plunky sdp_data_print(&rsp, 4);
181 1.1 plunky }
182 1.1 plunky
183 1.1 plunky sdp_close(ss);
184 1.1 plunky
185 1.1 plunky return EXIT_SUCCESS;
186 1.1 plunky }
187 1.1 plunky
188 1.1 plunky static sdp_session_t
189 1.1 plunky open_session(void)
190 1.1 plunky {
191 1.1 plunky sdp_session_t ss;
192 1.1 plunky
193 1.1 plunky if (bdaddr_any(&remote_addr))
194 1.1 plunky ss = sdp_open_local(control_socket);
195 1.1 plunky else
196 1.1 plunky ss = sdp_open(&local_addr, &remote_addr);
197 1.1 plunky
198 1.1 plunky if (ss == NULL)
199 1.1 plunky err(EXIT_FAILURE, "sdp_open");
200 1.1 plunky
201 1.1 plunky return ss;
202 1.1 plunky }
203 1.1 plunky
204 1.1 plunky /*
205 1.1 plunky * build ServiceSearchPattern from arglist
206 1.1 plunky */
207 1.1 plunky static void
208 1.1 plunky build_ssp(sdp_data_t *ssp, int argc, const char **argv)
209 1.1 plunky {
210 1.2 plunky static uint8_t data[12 * sizeof(uuid_t)];
211 1.1 plunky char * ep;
212 1.2 plunky uintmax_t umax;
213 1.2 plunky uuid_t uuid;
214 1.2 plunky uint32_t status;
215 1.1 plunky int i;
216 1.1 plunky
217 1.1 plunky ssp->next = data;
218 1.1 plunky ssp->end = data + sizeof(data);
219 1.1 plunky
220 1.1 plunky for (; argc-- > 0; argv++) {
221 1.2 plunky uuid_from_string(*argv, &uuid, &status);
222 1.2 plunky if (status != uuid_s_ok) {
223 1.2 plunky umax = strtoumax(*argv, &ep, 0);
224 1.2 plunky if (*argv[0] == '\0' || *ep != '\0') {
225 1.2 plunky for (i = 0;; i++) {
226 1.2 plunky if (i == __arraycount(aliases))
227 1.2 plunky errx(EXIT_FAILURE,
228 1.2 plunky "%s: Bad UUID", *argv);
229 1.2 plunky
230 1.2 plunky if (strcasecmp(aliases[i].name,
231 1.2 plunky *argv) == 0)
232 1.2 plunky break;
233 1.2 plunky }
234 1.2 plunky
235 1.2 plunky umax = aliases[i].uuid;
236 1.2 plunky } else if (umax > UINT32_MAX)
237 1.2 plunky errx(EXIT_FAILURE, "%s: Bad UUID", *argv);
238 1.1 plunky
239 1.2 plunky uuid = BLUETOOTH_BASE_UUID;
240 1.2 plunky uuid.time_low = (uint32_t)umax;
241 1.1 plunky }
242 1.1 plunky
243 1.2 plunky sdp_put_uuid(ssp, &uuid);
244 1.1 plunky }
245 1.1 plunky
246 1.1 plunky ssp->end = ssp->next;
247 1.1 plunky ssp->next = data;
248 1.1 plunky }
249