4 * Copyright (c) 2001-2003 Maksim Yevmenkin <m_evmenkin@yahoo.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
28 * $Id: search.c,v 1.2 2003/09/08 17:35:15 max Exp $
32 #include <netinet/in.h>
33 #include <bluetooth.h>
38 #include "sdpcontrol.h"
40 /* List of the attributes we are looking for */
41 static uint32_t attrs[] =
43 SDP_ATTR_RANGE( SDP_ATTR_SERVICE_RECORD_HANDLE,
44 SDP_ATTR_SERVICE_RECORD_HANDLE),
45 SDP_ATTR_RANGE( SDP_ATTR_SERVICE_CLASS_ID_LIST,
46 SDP_ATTR_SERVICE_CLASS_ID_LIST),
47 SDP_ATTR_RANGE( SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST,
48 SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST),
49 SDP_ATTR_RANGE( SDP_ATTR_BLUETOOTH_PROFILE_DESCRIPTOR_LIST,
50 SDP_ATTR_BLUETOOTH_PROFILE_DESCRIPTOR_LIST)
52 #define attrs_len (sizeof(attrs)/sizeof(attrs[0]))
54 /* Buffer for the attributes */
55 #define NRECS 25 /* request this much records from the SDP server */
56 #define BSIZE 256 /* one attribute buffer size */
57 static uint8_t buffer[NRECS * attrs_len][BSIZE];
60 static sdp_attr_t values[NRECS * attrs_len];
61 #define values_len (sizeof(values)/sizeof(values[0]))
64 * Print Service Class ID List
66 * The ServiceClassIDList attribute consists of a data element sequence in
67 * which each data element is a UUID representing the service classes that
68 * a given service record conforms to. The UUIDs are listed in order from
69 * the most specific class to the most general class. The ServiceClassIDList
70 * must contain at least one service class UUID.
74 print_service_class_id_list(uint8_t const *start, uint8_t const *end)
76 uint32_t type, len, value;
78 if (end - start < 2) {
79 fprintf(stderr, "Invalid Service Class ID List. " \
80 "Too short, len=%zd\n", end - start);
84 SDP_GET8(type, start);
91 SDP_GET16(len, start);
95 SDP_GET32(len, start);
99 fprintf(stderr, "Invalid Service Class ID List. " \
100 "Not a sequence, type=%#x\n", type);
105 while (start < end) {
106 SDP_GET8(type, start);
108 case SDP_DATA_UUID16:
109 SDP_GET16(value, start);
110 fprintf(stdout, "\t%s (%#4.4x)\n",
111 sdp_uuid2desc(value), value);
114 case SDP_DATA_UUID32:
115 SDP_GET32(value, start);
116 fprintf(stdout, "\t%#8.8x\n", value);
119 case SDP_DATA_UUID128: {
122 SDP_GET_UUID128(&uuid, start);
123 fprintf(stdout, "\t%#8.8x-%4.4x-%4.4x-%4.4x-%4.4x%8.8x\n",
124 ntohl(*(uint32_t *)&uuid.b[0]),
125 ntohs(*(uint16_t *)&uuid.b[4]),
126 ntohs(*(uint16_t *)&uuid.b[6]),
127 ntohs(*(uint16_t *)&uuid.b[8]),
128 ntohs(*(uint16_t *)&uuid.b[10]),
129 ntohl(*(uint32_t *)&uuid.b[12]));
133 fprintf(stderr, "Invalid Service Class ID List. " \
134 "Not a UUID, type=%#x\n", type);
139 } /* print_service_class_id_list */
142 * Print Protocol Descriptor List
144 * If the ProtocolDescriptorList describes a single stack, it takes the form
145 * of a data element sequence in which each element of the sequence is a
146 * protocol descriptor. Each protocol descriptor is, in turn, a data element
147 * sequence whose first element is a UUID identifying the protocol and whose
148 * successive elements are protocol-specific parameters. The protocol
149 * descriptors are listed in order from the lowest layer protocol to the
150 * highest layer protocol used to gain access to the service. If it is possible
151 * for more than one kind of protocol stack to be used to gain access to the
152 * service, the ProtocolDescriptorList takes the form of a data element
153 * alternative where each member is a data element sequence as described above.
157 print_protocol_descriptor(uint8_t const *start, uint8_t const *end)
166 uint32_t type, len, param;
168 /* Get Protocol UUID */
169 SDP_GET8(type, start);
171 case SDP_DATA_UUID16:
172 SDP_GET16(value.uint16, start);
173 fprintf(stdout, "\t%s (%#4.4x)\n", sdp_uuid2desc(value.uint16),
177 case SDP_DATA_UUID32:
178 SDP_GET32(value.uint32, start);
179 fprintf(stdout, "\t%#8.8x\n", value.uint32);
182 case SDP_DATA_UUID128:
183 SDP_GET_UUID128(&value.int128, start);
184 fprintf(stdout, "\t%#8.8x-%4.4x-%4.4x-%4.4x-%4.4x%8.8x\n",
185 ntohl(*(uint32_t *)&value.int128.b[0]),
186 ntohs(*(uint16_t *)&value.int128.b[4]),
187 ntohs(*(uint16_t *)&value.int128.b[6]),
188 ntohs(*(uint16_t *)&value.int128.b[8]),
189 ntohs(*(uint16_t *)&value.int128.b[10]),
190 ntohl(*(uint32_t *)&value.int128.b[12]));
194 fprintf(stderr, "Invalid Protocol Descriptor. " \
195 "Not a UUID, type=%#x\n", type);
200 /* Protocol specific parameters */
201 for (param = 1; start < end; param ++) {
202 fprintf(stdout, "\t\tProtocol specific parameter #%d: ", param);
204 SDP_GET8(type, start);
207 fprintf(stdout, "nil\n");
213 SDP_GET8(value.uint8, start);
214 fprintf(stdout, "u/int8/bool %u\n", value.uint8);
217 case SDP_DATA_UINT16:
219 case SDP_DATA_UUID16:
220 SDP_GET16(value.uint16, start);
221 fprintf(stdout, "u/int/uuid16 %u\n", value.uint16);
224 case SDP_DATA_UINT32:
226 case SDP_DATA_UUID32:
227 SDP_GET32(value.uint32, start);
228 fprintf(stdout, "u/int/uuid32 %u\n", value.uint32);
231 case SDP_DATA_UINT64:
233 SDP_GET64(value.uint64, start);
234 fprintf(stdout, "u/int64 %ju\n", value.uint64);
237 case SDP_DATA_UINT128:
238 case SDP_DATA_INT128:
239 SDP_GET128(&value.int128, start);
240 fprintf(stdout, "u/int128 %#8.8x%8.8x%8.8x%8.8x\n",
241 *(uint32_t *)&value.int128.b[0],
242 *(uint32_t *)&value.int128.b[4],
243 *(uint32_t *)&value.int128.b[8],
244 *(uint32_t *)&value.int128.b[12]);
247 case SDP_DATA_UUID128:
248 SDP_GET_UUID128(&value.int128, start);
249 fprintf(stdout, "uuid128 %#8.8x-%4.4x-%4.4x-%4.4x-%4.4x%8.8x\n",
250 ntohl(*(uint32_t *)&value.int128.b[0]),
251 ntohs(*(uint16_t *)&value.int128.b[4]),
252 ntohs(*(uint16_t *)&value.int128.b[6]),
253 ntohs(*(uint16_t *)&value.int128.b[8]),
254 ntohs(*(uint16_t *)&value.int128.b[10]),
255 ntohl(*(uint32_t *)&value.int128.b[12]));
260 SDP_GET8(len, start);
261 fprintf(stdout, "%*.*s\n", len, len, (char *) start);
267 SDP_GET16(len, start);
268 fprintf(stdout, "%*.*s\n", len, len, (char *) start);
274 SDP_GET32(len, start);
275 fprintf(stdout, "%*.*s\n", len, len, (char *) start);
281 SDP_GET8(len, start);
282 for (; len > 0; start ++, len --)
283 fprintf(stdout, "%#2.2x ", *start);
284 fprintf(stdout, "\n");
289 SDP_GET16(len, start);
290 for (; len > 0; start ++, len --)
291 fprintf(stdout, "%#2.2x ", *start);
292 fprintf(stdout, "\n");
297 SDP_GET32(len, start);
298 for (; len > 0; start ++, len --)
299 fprintf(stdout, "%#2.2x ", *start);
300 fprintf(stdout, "\n");
304 fprintf(stderr, "Invalid Protocol Descriptor. " \
305 "Unknown data type: %#02x\n", type);
310 } /* print_protocol_descriptor */
313 print_protocol_descriptor_list(uint8_t const *start, uint8_t const *end)
317 if (end - start < 2) {
318 fprintf(stderr, "Invalid Protocol Descriptor List. " \
319 "Too short, len=%zd\n", end - start);
323 SDP_GET8(type, start);
326 SDP_GET8(len, start);
330 SDP_GET16(len, start);
334 SDP_GET32(len, start);
338 fprintf(stderr, "Invalid Protocol Descriptor List. " \
339 "Not a sequence, type=%#x\n", type);
344 while (start < end) {
345 SDP_GET8(type, start);
348 SDP_GET8(len, start);
352 SDP_GET16(len, start);
356 SDP_GET32(len, start);
360 fprintf(stderr, "Invalid Protocol Descriptor List. " \
361 "Not a sequence, type=%#x\n", type);
366 print_protocol_descriptor(start, start + len);
369 } /* print_protocol_descriptor_list */
372 * Print Bluetooth Profile Descriptor List
374 * The BluetoothProfileDescriptorList attribute consists of a data element
375 * sequence in which each element is a profile descriptor that contains
376 * information about a Bluetooth profile to which the service represented by
377 * this service record conforms. Each profile descriptor is a data element
378 * sequence whose first element is the UUID assigned to the profile and whose
379 * second element is a 16-bit profile version number. Each version of a profile
380 * is assigned a 16-bit unsigned integer profile version number, which consists
381 * of two 8-bit fields. The higher-order 8 bits contain the major version
382 * number field and the lower-order 8 bits contain the minor version number
387 print_bluetooth_profile_descriptor_list(uint8_t const *start, uint8_t const *end)
389 uint32_t type, len, value;
391 if (end - start < 2) {
392 fprintf(stderr, "Invalid Bluetooth Profile Descriptor List. " \
393 "Too short, len=%zd\n", end - start);
397 SDP_GET8(type, start);
400 SDP_GET8(len, start);
404 SDP_GET16(len, start);
408 SDP_GET32(len, start);
412 fprintf(stderr, "Invalid Bluetooth Profile Descriptor List. " \
413 "Not a sequence, type=%#x\n", type);
418 while (start < end) {
419 SDP_GET8(type, start);
422 SDP_GET8(len, start);
426 SDP_GET16(len, start);
430 SDP_GET32(len, start);
434 fprintf(stderr, "Invalid Bluetooth Profile " \
435 "Descriptor List. " \
436 "Not a sequence, type=%#x\n", type);
442 SDP_GET8(type, start);
444 case SDP_DATA_UUID16:
445 SDP_GET16(value, start);
446 fprintf(stdout, "\t%s (%#4.4x) ",
447 sdp_uuid2desc(value), value);
450 case SDP_DATA_UUID32:
451 SDP_GET32(value, start);
452 fprintf(stdout, "\t%#8.8x ", value);
455 case SDP_DATA_UUID128: {
458 SDP_GET_UUID128(&uuid, start);
459 fprintf(stdout, "\t%#8.8x-%4.4x-%4.4x-%4.4x-%4.4x%8.8x ",
460 ntohl(*(uint32_t *)&uuid.b[0]),
461 ntohs(*(uint16_t *)&uuid.b[4]),
462 ntohs(*(uint16_t *)&uuid.b[6]),
463 ntohs(*(uint16_t *)&uuid.b[8]),
464 ntohs(*(uint16_t *)&uuid.b[10]),
465 ntohl(*(uint32_t *)&uuid.b[12]));
469 fprintf(stderr, "Invalid Bluetooth Profile " \
470 "Descriptor List. " \
471 "Not a UUID, type=%#x\n", type);
477 SDP_GET8(type, start);
478 if (type != SDP_DATA_UINT16) {
479 fprintf(stderr, "Invalid Bluetooth Profile " \
480 "Descriptor List. " \
481 "Invalid version type=%#x\n", type);
485 SDP_GET16(value, start);
486 fprintf(stdout, "ver. %d.%d\n",
487 (value >> 8) & 0xff, value & 0xff);
489 } /* print_bluetooth_profile_descriptor_list */
491 /* Perform SDP search command */
493 do_sdp_search(void *xs, int argc, char **argv)
496 int32_t n, type, value;
499 /* Parse command line arguments */
502 n = strtoul(argv[0], &ep, 16);
504 switch (tolower(argv[0][0])) {
505 case 'c': /* CIP/CTP */
506 switch (tolower(argv[0][1])) {
508 service = SDP_SERVICE_CLASS_COMMON_ISDN_ACCESS;
512 service = SDP_SERVICE_CLASS_CORDLESS_TELEPHONY;
521 case 'd': /* DialUp Networking */
522 service = SDP_SERVICE_CLASS_DIALUP_NETWORKING;
525 case 'f': /* Fax/OBEX File Transfer */
526 switch (tolower(argv[0][1])) {
528 service = SDP_SERVICE_CLASS_FAX;
532 service = SDP_SERVICE_CLASS_OBEX_FILE_TRANSFER;
542 service = SDP_SERVICE_CLASS_GN;
545 case 'h': /* Headset/HID */
546 switch (tolower(argv[0][1])) {
548 service = SDP_SERVICE_CLASS_HUMAN_INTERFACE_DEVICE;
552 service = SDP_SERVICE_CLASS_HEADSET;
561 case 'l': /* LAN Access Using PPP */
562 service = SDP_SERVICE_CLASS_LAN_ACCESS_USING_PPP;
566 service = SDP_SERVICE_CLASS_NAP;
569 case 'o': /* OBEX Object Push */
570 service = SDP_SERVICE_CLASS_OBEX_OBJECT_PUSH;
573 case 's': /* Serial Port */
574 service = SDP_SERVICE_CLASS_SERIAL_PORT;
582 service = (uint16_t) n;
589 /* Initialize attribute values array */
590 for (n = 0; n < values_len; n ++) {
591 values[n].flags = SDP_ATTR_INVALID;
593 values[n].vlen = BSIZE;
594 values[n].value = buffer[n];
597 /* Do SDP Service Search Attribute Request */
598 n = sdp_search(xs, 1, &service, attrs_len, attrs, values_len, values);
602 /* Print attributes values */
603 for (n = 0; n < values_len; n ++) {
604 if (values[n].flags != SDP_ATTR_OK)
607 switch (values[n].attr) {
608 case SDP_ATTR_SERVICE_RECORD_HANDLE:
609 fprintf(stdout, "\n");
610 if (values[n].vlen == 5) {
611 SDP_GET8(type, values[n].value);
612 if (type == SDP_DATA_UINT32) {
613 SDP_GET32(value, values[n].value);
614 fprintf(stdout, "Record Handle: " \
617 fprintf(stderr, "Invalid type=%#x " \
619 "attribute!\n", type);
621 fprintf(stderr, "Invalid size=%d for Record " \
622 "Handle attribute\n",
626 case SDP_ATTR_SERVICE_CLASS_ID_LIST:
627 fprintf(stdout, "Service Class ID List:\n");
628 print_service_class_id_list(values[n].value,
629 values[n].value + values[n].vlen);
632 case SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST:
633 fprintf(stdout, "Protocol Descriptor List:\n");
634 print_protocol_descriptor_list(values[n].value,
635 values[n].value + values[n].vlen);
638 case SDP_ATTR_BLUETOOTH_PROFILE_DESCRIPTOR_LIST:
639 fprintf(stdout, "Bluetooth Profile Descriptor List:\n");
640 print_bluetooth_profile_descriptor_list(values[n].value,
641 values[n].value + values[n].vlen);
645 fprintf(stderr, "Unexpected attribute ID=%#4.4x\n",
652 } /* do_sdp_search */
654 /* Perform SDP browse command */
656 do_sdp_browse(void *xs, int argc, char **argv)
661 #define STR(x) _STR(x)
663 static char const * const av[] = {
664 STR(SDP_SERVICE_CLASS_PUBLIC_BROWSE_GROUP),
674 return (do_sdp_search(xs, argc, argv));
678 } /* do_sdp_browse */
680 /* List of SDP commands */
681 struct sdp_command sdp_commands[] = {
684 "Browse for services. The <Group> parameter is a 16-bit UUID of the group\n" \
685 "to browse. If omitted <Group> is set to Public Browse Group.\n\n" \
686 "\t<Group> - xxxx; 16-bit UUID of the group to browse\n",
691 "Search for the <Service>. The <Service> parameter is a 16-bit UUID of the\n" \
692 "service to search for. For some services it is possible to use service name\n"\
693 "instead of service UUID\n\n" \
694 "\t<Service> - xxxx; 16-bit UUID of the service to search for\n\n" \
695 "\tKnown service names\n" \
696 "\t===================\n" \
697 "\tCIP - Common ISDN Access\n" \
698 "\tCTP - Cordless Telephony\n" \
699 "\tDUN - DialUp Networking\n" \
701 "\tFTRN - OBEX File Transfer\n" \
703 "\tHID - Human Interface Device\n" \
704 "\tHSET - Headset\n" \
705 "\tLAN - LAN Access Using PPP\n" \
706 "\tNAP - Network Access Point\n" \
707 "\tOPUSH - OBEX Object Push\n" \
708 "\tSP - Serial Port\n",