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