xref: /freebsd/lib/libsdp/util.c (revision 74bf4e164ba5851606a27d4feff27717452583e5)
1 /*
2  * util.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: util.c,v 1.5 2003/09/08 02:29:35 max Exp $
29  * $FreeBSD$
30  */
31 
32 #include <bluetooth.h>
33 #include <stdio.h>
34 #include <sdp.h>
35 
36 /*
37  * SDP attribute description
38  */
39 
40 struct sdp_attr_desc {
41 	uint32_t	 attr;
42 	char const	*desc;
43 };
44 typedef struct sdp_attr_desc	sdp_attr_desc_t;
45 typedef struct sdp_attr_desc *	sdp_attr_desc_p;
46 
47 static sdp_attr_desc_t	sdp_uuids_desc[] = {
48 { SDP_UUID_PROTOCOL_SDP, "SDP", },
49 { SDP_UUID_PROTOCOL_UDP, "UDP", },
50 { SDP_UUID_PROTOCOL_RFCOMM, "RFCOMM", },
51 { SDP_UUID_PROTOCOL_TCP, "TCP", },
52 { SDP_UUID_PROTOCOL_TCS_BIN, "TCS BIN", },
53 { SDP_UUID_PROTOCOL_TCS_AT, "TCS AT", },
54 { SDP_UUID_PROTOCOL_OBEX, "OBEX", },
55 { SDP_UUID_PROTOCOL_IP, "IP", },
56 { SDP_UUID_PROTOCOL_FTP, "FTP", },
57 { SDP_UUID_PROTOCOL_HTTP, "HTTP", },
58 { SDP_UUID_PROTOCOL_WSP, "WSP", },
59 { SDP_UUID_PROTOCOL_BNEP, "BNEP", },
60 { SDP_UUID_PROTOCOL_UPNP, "UPNP", },
61 { SDP_UUID_PROTOCOL_HIDP, "HIDP", },
62 { SDP_UUID_PROTOCOL_HARDCOPY_CONTROL_CHANNEL, "Hardcopy Control Channel", },
63 { SDP_UUID_PROTOCOL_HARDCOPY_DATA_CHANNEL, "Hardcopy Data Channel", },
64 { SDP_UUID_PROTOCOL_HARDCOPY_NOTIFICATION, "Hardcopy Notification", },
65 { SDP_UUID_PROTOCOL_AVCTP, "AVCTP", },
66 { SDP_UUID_PROTOCOL_AVDTP, "AVDTP", },
67 { SDP_UUID_PROTOCOL_CMPT, "CMPT", },
68 { SDP_UUID_PROTOCOL_UDI_C_PLANE, "UDI C-Plane", },
69 { SDP_UUID_PROTOCOL_L2CAP, "L2CAP", },
70 /* Service Class IDs/Bluetooth Profile IDs */
71 { SDP_SERVICE_CLASS_SERVICE_DISCOVERY_SERVER, "Service Discovery Server", },
72 { SDP_SERVICE_CLASS_BROWSE_GROUP_DESCRIPTOR, "Browse Group Descriptor", },
73 { SDP_SERVICE_CLASS_PUBLIC_BROWSE_GROUP, "Public Browse Group", },
74 { SDP_SERVICE_CLASS_SERIAL_PORT, "Serial Port", },
75 { SDP_SERVICE_CLASS_LAN_ACCESS_USING_PPP, "LAN Access Using PPP", },
76 { SDP_SERVICE_CLASS_DIALUP_NETWORKING, "Dial-Up Networking", },
77 { SDP_SERVICE_CLASS_IR_MC_SYNC, "IrMC Sync", },
78 { SDP_SERVICE_CLASS_OBEX_OBJECT_PUSH, "OBEX Object Push", },
79 { SDP_SERVICE_CLASS_OBEX_FILE_TRANSFER, "OBEX File Transfer", },
80 { SDP_SERVICE_CLASS_IR_MC_SYNC_COMMAND, "IrMC Sync Command", },
81 { SDP_SERVICE_CLASS_HEADSET, "Headset", },
82 { SDP_SERVICE_CLASS_CORDLESS_TELEPHONY, "Cordless Telephony", },
83 { SDP_SERVICE_CLASS_AUDIO_SOURCE, "Audio Source", },
84 { SDP_SERVICE_CLASS_AUDIO_SINK, "Audio Sink", },
85 { SDP_SERVICE_CLASS_AV_REMOTE_CONTROL_TARGET, "A/V Remote Control Target", },
86 { SDP_SERVICE_CLASS_ADVANCED_AUDIO_DISTRIBUTION, "Advanced Audio Distribution", },
87 { SDP_SERVICE_CLASS_AV_REMOTE_CONTROL, "A/V Remote Control", },
88 { SDP_SERVICE_CLASS_VIDEO_CONFERENCING, "Video Conferencing", },
89 { SDP_SERVICE_CLASS_INTERCOM, "Intercom", },
90 { SDP_SERVICE_CLASS_FAX, "Fax", },
91 { SDP_SERVICE_CLASS_HEADSET_AUDIO_GATEWAY, "Headset Audio Gateway", },
92 { SDP_SERVICE_CLASS_WAP, "WAP", },
93 { SDP_SERVICE_CLASS_WAP_CLIENT, "WAP Client", },
94 { SDP_SERVICE_CLASS_PANU, "PANU", },
95 { SDP_SERVICE_CLASS_NAP, "Network Access Point", },
96 { SDP_SERVICE_CLASS_GN, "GN", },
97 { SDP_SERVICE_CLASS_DIRECT_PRINTING, "Direct Printing", },
98 { SDP_SERVICE_CLASS_REFERENCE_PRINTING, "Reference Printing", },
99 { SDP_SERVICE_CLASS_IMAGING, "Imaging", },
100 { SDP_SERVICE_CLASS_IMAGING_RESPONDER, "Imaging Responder", },
101 { SDP_SERVICE_CLASS_IMAGING_AUTOMATIC_ARCHIVE, "Imaging Automatic Archive", },
102 { SDP_SERVICE_CLASS_IMAGING_REFERENCED_OBJECTS, "Imaging Referenced Objects", },
103 { SDP_SERVICE_CLASS_HANDSFREE, "Handsfree", },
104 { SDP_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, "Handsfree Audio Gateway", },
105 { SDP_SERVICE_CLASS_DIRECT_PRINTING_REFERENCE_OBJECTS, "Direct Printing Reference Objects", },
106 { SDP_SERVICE_CLASS_REFLECTED_UI, "Reflected UI", },
107 { SDP_SERVICE_CLASS_BASIC_PRINTING, "Basic Printing", },
108 { SDP_SERVICE_CLASS_PRINTING_STATUS, "Printing Status", },
109 { SDP_SERVICE_CLASS_HUMAN_INTERFACE_DEVICE, "Human Interface Device", },
110 { SDP_SERVICE_CLASS_HARDCOPY_CABLE_REPLACEMENT, "Hardcopy Cable Replacement", },
111 { SDP_SERVICE_CLASS_HCR_PRINT, "HCR Print", },
112 { SDP_SERVICE_CLASS_HCR_SCAN, "HCR Scan", },
113 { SDP_SERVICE_CLASS_COMMON_ISDN_ACCESS, "Common ISDN Access", },
114 { SDP_SERVICE_CLASS_VIDEO_CONFERENCING_GW, "Video Conferencing Gateway", },
115 { SDP_SERVICE_CLASS_UDI_MT, "UDI MT", },
116 { SDP_SERVICE_CLASS_UDI_TA, "UDI TA", },
117 { SDP_SERVICE_CLASS_AUDIO_VIDEO, "Audio/Video", },
118 { SDP_SERVICE_CLASS_SIM_ACCESS, "SIM Access", },
119 { SDP_SERVICE_CLASS_PNP_INFORMATION, "PNP Information", },
120 { SDP_SERVICE_CLASS_GENERIC_NETWORKING, "Generic Networking", },
121 { SDP_SERVICE_CLASS_GENERIC_FILE_TRANSFER, "Generic File Transfer", },
122 { SDP_SERVICE_CLASS_GENERIC_AUDIO, "Generic Audio", },
123 { SDP_SERVICE_CLASS_GENERIC_TELEPHONY, "Generic Telephony", },
124 { SDP_SERVICE_CLASS_UPNP, "UPNP", },
125 { SDP_SERVICE_CLASS_UPNP_IP, "UPNP IP", },
126 { SDP_SERVICE_CLASS_ESDP_UPNP_IP_PAN, "ESDP UPNP IP PAN", },
127 { SDP_SERVICE_CLASS_ESDP_UPNP_IP_LAP, "ESDP UPNP IP LAP", },
128 { SDP_SERVICE_CLASS_ESDP_UPNP_L2CAP, "ESDP UPNP L2CAP", },
129 { 0xffff, NULL, }
130 };
131 
132 static sdp_attr_desc_t	sdp_attrs_desc[] = {
133 { SDP_ATTR_SERVICE_RECORD_HANDLE,
134   "Record handle",
135   },
136 { SDP_ATTR_SERVICE_CLASS_ID_LIST,
137   "Service Class ID list",
138   },
139 { SDP_ATTR_SERVICE_RECORD_STATE,
140   "Service Record State",
141   },
142 { SDP_ATTR_SERVICE_ID,
143   "Service ID",
144   },
145 { SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST,
146   "Protocol Descriptor List",
147   },
148 { SDP_ATTR_BROWSE_GROUP_LIST,
149   "Browse Group List",
150   },
151 { SDP_ATTR_LANGUAGE_BASE_ATTRIBUTE_ID_LIST,
152   "Language Base Attribute ID List",
153   },
154 { SDP_ATTR_SERVICE_INFO_TIME_TO_LIVE,
155   "Service Info Time-To-Live",
156   },
157 { SDP_ATTR_SERVICE_AVAILABILITY,
158   "Service Availability",
159   },
160 { SDP_ATTR_BLUETOOTH_PROFILE_DESCRIPTOR_LIST,
161   "Bluetooh Profile Descriptor List",
162   },
163 { SDP_ATTR_DOCUMENTATION_URL,
164   "Documentation URL",
165   },
166 { SDP_ATTR_CLIENT_EXECUTABLE_URL,
167   "Client Executable URL",
168   },
169 { SDP_ATTR_ICON_URL,
170   "Icon URL",
171   },
172 { SDP_ATTR_ADDITIONAL_PROTOCOL_DESCRIPTOR_LISTS,
173   "Additional Protocol Descriptor Lists" },
174 { SDP_ATTR_GROUP_ID,
175 /*SDP_ATTR_IP_SUBNET,
176   SDP_ATTR_VERSION_NUMBER_LIST*/
177   "Group ID/IP Subnet/Version Number List",
178   },
179 { SDP_ATTR_SERVICE_DATABASE_STATE,
180   "Service Database State",
181   },
182 { SDP_ATTR_SERVICE_VERSION,
183   "Service Version",
184   },
185 { SDP_ATTR_EXTERNAL_NETWORK,
186 /*SDP_ATTR_NETWORK,
187   SDP_ATTR_SUPPORTED_DATA_STORES_LIST*/
188   "External Network/Network/Supported Data Stores List",
189   },
190 { SDP_ATTR_FAX_CLASS1_SUPPORT,
191 /*SDP_ATTR_REMOTE_AUDIO_VOLUME_CONTROL*/
192   "Fax Class1 Support/Remote Audio Volume Control",
193   },
194 { SDP_ATTR_FAX_CLASS20_SUPPORT,
195 /*SDP_ATTR_SUPPORTED_FORMATS_LIST*/
196   "Fax Class20 Support/Supported Formats List",
197   },
198 { SDP_ATTR_FAX_CLASS2_SUPPORT,
199   "Fax Class2 Support",
200   },
201 { SDP_ATTR_AUDIO_FEEDBACK_SUPPORT,
202   "Audio Feedback Support",
203   },
204 { SDP_ATTR_NETWORK_ADDRESS,
205   "Network Address",
206   },
207 { SDP_ATTR_WAP_GATEWAY,
208   "WAP Gateway",
209   },
210 { SDP_ATTR_HOME_PAGE_URL,
211   "Home Page URL",
212   },
213 { SDP_ATTR_WAP_STACK_TYPE,
214   "WAP Stack Type",
215   },
216 { SDP_ATTR_SECURITY_DESCRIPTION,
217   "Security Description",
218   },
219 { SDP_ATTR_NET_ACCESS_TYPE,
220   "Net Access Type",
221   },
222 { SDP_ATTR_MAX_NET_ACCESS_RATE,
223   "Max Net Access Rate",
224   },
225 { SDP_ATTR_IPV4_SUBNET,
226   "IPv4 Subnet",
227   },
228 { SDP_ATTR_IPV6_SUBNET,
229   "IPv6 Subnet",
230   },
231 { SDP_ATTR_SUPPORTED_CAPABALITIES,
232   "Supported Capabalities",
233   },
234 { SDP_ATTR_SUPPORTED_FEATURES,
235   "Supported Features",
236   },
237 { SDP_ATTR_SUPPORTED_FUNCTIONS,
238   "Supported Functions",
239   },
240 { SDP_ATTR_TOTAL_IMAGING_DATA_CAPACITY,
241   "Total Imaging Data Capacity",
242   },
243 { 0xffff, NULL, }
244 };
245 
246 char const * const
247 sdp_attr2desc(uint16_t attr)
248 {
249 	register sdp_attr_desc_p	a = sdp_attrs_desc;
250 
251 	for (; a->desc != NULL; a++)
252 		if (attr == a->attr)
253 			break;
254 
255 	return ((a->desc != NULL)? a->desc : "Unknown");
256 }
257 
258 char const * const
259 sdp_uuid2desc(uint16_t uuid)
260 {
261 	register sdp_attr_desc_p	a = sdp_uuids_desc;
262 
263 	for (; a->desc != NULL; a++)
264 		if (uuid == a->attr)
265 			break;
266 
267 	return ((a->desc != NULL)? a->desc : "Unknown");
268 }
269 
270 void
271 sdp_print(uint32_t level, uint8_t const *start, uint8_t const *end)
272 {
273 	union {
274 		int8_t		int8;
275 		int16_t		int16;
276 		int32_t		int32;
277 		int64_t		int64;
278 		int128_t	int128;
279 		uint8_t		uint8;
280 		uint16_t	uint16;
281 		uint32_t	uint32;
282 		uint64_t	uint64;
283 	}			value;
284 	uint8_t			type;
285 	uint32_t		i;
286 
287 	if (start == NULL || end == NULL)
288 		return;
289 
290 	while (start < end) {
291 		for (i = 0; i < level; i++)
292 			printf("\t");
293 
294 		SDP_GET8(type, start);
295 
296 		switch (type) {
297 		case SDP_DATA_NIL:
298 			printf("nil\n");
299 			break;
300 
301 		case SDP_DATA_UINT8:
302 			SDP_GET8(value.uint8, start);
303 			printf("uint8 %u\n", value.uint8);
304 			break;
305 		case SDP_DATA_UINT16:
306 			SDP_GET16(value.uint16, start);
307 			printf("uint16 %u\n", value.uint16);
308 			break;
309 		case SDP_DATA_UINT32:
310 			SDP_GET32(value.uint32, start);
311 			printf("uint32 %u\n", value.uint32);
312 			break;
313 		case SDP_DATA_UINT64:
314 			SDP_GET64(value.uint64, start);
315 			printf("uint64 %ju\n", value.uint64);
316 			break;
317 
318 		case SDP_DATA_UINT128:
319 		case SDP_DATA_INT128:
320 		case SDP_DATA_UUID128:
321 			SDP_GET128(&value.int128, start);
322 			printf("int128/uuid128 %#8.8x-%4.4x-%4.4x-%4.4x-%4.4x%8.8x\n",
323 				*(uint32_t *)&value.int128.b[0],
324 				*(uint16_t *)&value.int128.b[4],
325 				*(uint16_t *)&value.int128.b[6],
326 				*(uint16_t *)&value.int128.b[8],
327 				*(uint16_t *)&value.int128.b[10],
328 				*(uint32_t *)&value.int128.b[12]);
329 			break;
330 
331 		case SDP_DATA_INT8:
332 			SDP_GET8(value.int8, start);
333 			printf("int8 %d\n", value.int8);
334 			break;
335 		case SDP_DATA_INT16:
336 			SDP_GET16(value.int16, start);
337 			printf("int16 %d\n", value.int16);
338 			break;
339 		case SDP_DATA_INT32:
340 			SDP_GET32(value.int32, start);
341 			printf("int32 %d\n", value.int32);
342 			break;
343 		case SDP_DATA_INT64:
344 			SDP_GET64(value.int64, start);
345 			printf("int64 %ju\n", value.int64);
346 			break;
347 
348 		case SDP_DATA_UUID16:
349 			SDP_GET16(value.uint16, start);
350 			printf("uuid16 %#4.4x - %s\n", value.uint16,
351 				sdp_uuid2desc(value.uint16));
352 			break;
353 		case SDP_DATA_UUID32:
354 			SDP_GET32(value.uint32, start);
355 			printf("uuid32 %#8.8x\n", value.uint32);
356 			break;
357 
358 		case SDP_DATA_STR8:
359 			SDP_GET8(value.uint8, start);
360 			printf("str8 %*.*s\n", value.uint8, value.uint8, start);
361 			start += value.uint8;
362 			break;
363 		case SDP_DATA_STR16:
364 			SDP_GET16(value.uint16, start);
365 			printf("str16 %*.*s\n", value.uint16, value.uint16, start);
366 			start += value.uint16;
367 			break;
368 		case SDP_DATA_STR32:
369 			SDP_GET32(value.uint32, start);
370 			printf("str32 %*.*s\n", value.uint32, value.uint32, start);
371 			start += value.uint32;
372 			break;
373 
374 		case SDP_DATA_BOOL:
375 			SDP_GET8(value.uint8, start);
376 			printf("bool %d\n", value.uint8);
377 			break;
378 
379 		case SDP_DATA_SEQ8:
380 			SDP_GET8(value.uint8, start);
381 			printf("seq8 %d\n", value.uint8);
382 			sdp_print(level + 1, start, start + value.uint8);
383 			start += value.uint8;
384 			break;
385 		case SDP_DATA_SEQ16:
386 			SDP_GET16(value.uint16, start);
387 			printf("seq16 %d\n", value.uint16);
388 			sdp_print(level + 1, start, start + value.uint16);
389 			start += value.uint16;
390 			break;
391 		case SDP_DATA_SEQ32:
392 			SDP_GET32(value.uint32, start);
393 			printf("seq32 %d\n", value.uint32);
394 			sdp_print(level + 1, start, start + value.uint32);
395 			start += value.uint32;
396 			break;
397 
398 		case SDP_DATA_ALT8:
399 			SDP_GET8(value.uint8, start);
400 			printf("alt8 %d\n", value.uint8);
401 			sdp_print(level + 1, start, start + value.uint8);
402 			start += value.uint8;
403 			break;
404 		case SDP_DATA_ALT16:
405 			SDP_GET16(value.uint16, start);
406 			printf("alt16 %d\n", value.uint16);
407 			sdp_print(level + 1, start, start + value.uint16);
408 			start += value.uint16;
409 			break;
410 		case SDP_DATA_ALT32:
411 			SDP_GET32(value.uint32, start);
412 			printf("alt32 %d\n", value.uint32);
413 			sdp_print(level + 1, start, start + value.uint32);
414 			start += value.uint32;
415 			break;
416 
417 		case SDP_DATA_URL8:
418 			SDP_GET8(value.uint8, start);
419 			printf("url8 %*.*s\n", value.uint8, value.uint8, start);
420 			start += value.uint8;
421 			break;
422 		case SDP_DATA_URL16:
423 			SDP_GET16(value.uint16, start);
424 			printf("url16 %*.*s\n", value.uint16, value.uint16, start);
425 			start += value.uint16;
426 			break;
427 		case SDP_DATA_URL32:
428 			SDP_GET32(value.uint32, start);
429 			printf("url32 %*.*s\n", value.uint32, value.uint32, start);
430 			start += value.uint32;
431 			break;
432 
433 		default:
434 			printf("unknown data type: %#02x\n", *start ++);
435 			break;
436 		}
437 	}
438 }
439 
440