command.c revision 1.2 1 1.2 plunky /* $NetBSD: command.c,v 1.2 2009/10/06 19:21:17 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.2 plunky __RCSID("$NetBSD: command.c,v 1.2 2009/10/06 19:21:17 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.1 plunky { SDP_UUID_PROTOCOL_RFCOMM,
86 1.1 plunky "RFCOMM", "RFCOMM Protocol" },
87 1.1 plunky { SDP_UUID_PROTOCOL_SDP,
88 1.1 plunky "SDP", "Service Discovery Protocol" },
89 1.1 plunky { SDP_SERVICE_CLASS_SERIAL_PORT,
90 1.1 plunky "SP", "Serial Port Service" },
91 1.1 plunky { SDP_SERVICE_CLASS_IR_MC_SYNC,
92 1.1 plunky "SYNC", "IrMC Sync Client Service" },
93 1.1 plunky };
94 1.1 plunky
95 1.1 plunky int
96 1.1 plunky do_sdp_browse(int argc, const char **argv)
97 1.1 plunky {
98 1.1 plunky #define STR(x) __STRING(x)
99 1.1 plunky const char *av = STR(SDP_SERVICE_CLASS_PUBLIC_BROWSE_GROUP);
100 1.1 plunky #undef STR
101 1.1 plunky
102 1.1 plunky if (argc > 1)
103 1.1 plunky errx(EXIT_FAILURE, "Too many arguments");
104 1.1 plunky
105 1.1 plunky if (argc == 0) {
106 1.1 plunky argc = 1;
107 1.1 plunky argv = &av;
108 1.1 plunky }
109 1.1 plunky
110 1.1 plunky return do_sdp_search(argc, argv);
111 1.1 plunky }
112 1.1 plunky
113 1.1 plunky int
114 1.1 plunky do_sdp_record(int argc, const char **argv)
115 1.1 plunky {
116 1.1 plunky sdp_session_t ss;
117 1.1 plunky sdp_data_t rsp;
118 1.1 plunky char * ep;
119 1.1 plunky unsigned long handle;
120 1.1 plunky bool rv;
121 1.1 plunky
122 1.1 plunky if (argc == 0)
123 1.1 plunky errx(EXIT_FAILURE, "Record handle required");
124 1.1 plunky
125 1.1 plunky ss = open_session();
126 1.1 plunky
127 1.1 plunky for (; argc-- > 0; argv++) {
128 1.1 plunky handle = strtoul(*argv, &ep, 0);
129 1.1 plunky if (*argv[0] == '\0' || *ep != '\0' || handle > UINT32_MAX)
130 1.1 plunky errx(EXIT_FAILURE, "Invalid handle: %s\n", *argv);
131 1.1 plunky
132 1.2 plunky rv = sdp_service_attribute(ss, (uint32_t)handle, NULL, &rsp);
133 1.1 plunky if (!rv)
134 1.1 plunky warn("%s", *argv);
135 1.1 plunky else
136 1.1 plunky print_record(&rsp);
137 1.1 plunky
138 1.1 plunky if (argc > 0)
139 1.1 plunky printf("\n\n");
140 1.1 plunky }
141 1.1 plunky
142 1.1 plunky sdp_close(ss);
143 1.1 plunky return EXIT_SUCCESS;
144 1.1 plunky }
145 1.1 plunky
146 1.1 plunky int
147 1.1 plunky do_sdp_search(int argc, const char **argv)
148 1.1 plunky {
149 1.1 plunky sdp_session_t ss;
150 1.1 plunky sdp_data_t ssp, rec, rsp;
151 1.1 plunky bool rv;
152 1.1 plunky
153 1.1 plunky if (argc < 1)
154 1.1 plunky errx(EXIT_FAILURE, "UUID required");
155 1.1 plunky
156 1.1 plunky if (argc > 12)
157 1.1 plunky errx(EXIT_FAILURE, "Too many UUIDs");
158 1.1 plunky
159 1.1 plunky build_ssp(&ssp, argc, argv);
160 1.1 plunky
161 1.1 plunky ss = open_session();
162 1.1 plunky
163 1.1 plunky rv = sdp_service_search_attribute(ss, &ssp, NULL, &rsp);
164 1.1 plunky if (!rv)
165 1.1 plunky err(EXIT_FAILURE, "sdp_service_search_attribute");
166 1.1 plunky
167 1.1 plunky while (sdp_get_seq(&rsp, &rec)) {
168 1.1 plunky if (!rv)
169 1.1 plunky printf("\n\n");
170 1.1 plunky else
171 1.1 plunky rv = false;
172 1.1 plunky
173 1.1 plunky print_record(&rec);
174 1.1 plunky }
175 1.1 plunky
176 1.1 plunky if (rsp.next != rsp.end) {
177 1.1 plunky printf("\n\nAdditional Data:\n");
178 1.1 plunky sdp_data_print(&rsp, 4);
179 1.1 plunky }
180 1.1 plunky
181 1.1 plunky sdp_close(ss);
182 1.1 plunky
183 1.1 plunky return EXIT_SUCCESS;
184 1.1 plunky }
185 1.1 plunky
186 1.1 plunky static sdp_session_t
187 1.1 plunky open_session(void)
188 1.1 plunky {
189 1.1 plunky sdp_session_t ss;
190 1.1 plunky
191 1.1 plunky if (bdaddr_any(&remote_addr))
192 1.1 plunky ss = sdp_open_local(control_socket);
193 1.1 plunky else
194 1.1 plunky ss = sdp_open(&local_addr, &remote_addr);
195 1.1 plunky
196 1.1 plunky if (ss == NULL)
197 1.1 plunky err(EXIT_FAILURE, "sdp_open");
198 1.1 plunky
199 1.1 plunky return ss;
200 1.1 plunky }
201 1.1 plunky
202 1.1 plunky /*
203 1.1 plunky * build ServiceSearchPattern from arglist
204 1.1 plunky */
205 1.1 plunky static void
206 1.1 plunky build_ssp(sdp_data_t *ssp, int argc, const char **argv)
207 1.1 plunky {
208 1.2 plunky static uint8_t data[12 * sizeof(uuid_t)];
209 1.1 plunky char * ep;
210 1.2 plunky uintmax_t umax;
211 1.2 plunky uuid_t uuid;
212 1.2 plunky uint32_t status;
213 1.1 plunky int i;
214 1.1 plunky
215 1.1 plunky ssp->next = data;
216 1.1 plunky ssp->end = data + sizeof(data);
217 1.1 plunky
218 1.1 plunky for (; argc-- > 0; argv++) {
219 1.2 plunky uuid_from_string(*argv, &uuid, &status);
220 1.2 plunky if (status != uuid_s_ok) {
221 1.2 plunky umax = strtoumax(*argv, &ep, 0);
222 1.2 plunky if (*argv[0] == '\0' || *ep != '\0') {
223 1.2 plunky for (i = 0;; i++) {
224 1.2 plunky if (i == __arraycount(aliases))
225 1.2 plunky errx(EXIT_FAILURE,
226 1.2 plunky "%s: Bad UUID", *argv);
227 1.2 plunky
228 1.2 plunky if (strcasecmp(aliases[i].name,
229 1.2 plunky *argv) == 0)
230 1.2 plunky break;
231 1.2 plunky }
232 1.2 plunky
233 1.2 plunky umax = aliases[i].uuid;
234 1.2 plunky } else if (umax > UINT32_MAX)
235 1.2 plunky errx(EXIT_FAILURE, "%s: Bad UUID", *argv);
236 1.1 plunky
237 1.2 plunky uuid = BLUETOOTH_BASE_UUID;
238 1.2 plunky uuid.time_low = (uint32_t)umax;
239 1.1 plunky }
240 1.1 plunky
241 1.2 plunky sdp_put_uuid(ssp, &uuid);
242 1.1 plunky }
243 1.1 plunky
244 1.1 plunky ssp->end = ssp->next;
245 1.1 plunky ssp->next = data;
246 1.1 plunky }
247