1121054Semax/*
2121054Semax * rfcomm_sdp.c
3121054Semax *
4121054Semax * Copyright (c) 2003 Maksim Yevmenkin <m_evmenkin@yahoo.com>
5121054Semax * All rights reserved.
6121054Semax *
7121054Semax * Redistribution and use in source and binary forms, with or without
8121054Semax * modification, are permitted provided that the following conditions
9121054Semax * are met:
10121054Semax * 1. Redistributions of source code must retain the above copyright
11121054Semax *    notice, this list of conditions and the following disclaimer.
12121054Semax * 2. Redistributions in binary form must reproduce the above copyright
13121054Semax *    notice, this list of conditions and the following disclaimer in the
14121054Semax *    documentation and/or other materials provided with the distribution.
15121054Semax *
16121054Semax * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17121054Semax * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18121054Semax * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19121054Semax * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20121054Semax * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21121054Semax * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22121054Semax * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23121054Semax * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24121054Semax * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25121054Semax * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26121054Semax * SUCH DAMAGE.
27121054Semax *
28121054Semax * $Id: rfcomm_sdp.c,v 1.1 2003/09/07 18:15:55 max Exp $
29121054Semax * $FreeBSD$
30121054Semax */
31121054Semax
32121054Semax#include <bluetooth.h>
33121054Semax#include <errno.h>
34121054Semax#include <sdp.h>
35121054Semax#include <stdio.h>
36121054Semax
37121054Semax#undef	PROTOCOL_DESCRIPTOR_LIST_BUFFER_SIZE
38121054Semax#define	PROTOCOL_DESCRIPTOR_LIST_BUFFER_SIZE	256
39121054Semax
40121054Semax#undef	PROTOCOL_DESCRIPTOR_LIST_MINIMAL_SIZE
41121054Semax#define	PROTOCOL_DESCRIPTOR_LIST_MINIMAL_SIZE	12
42121054Semax
43128077Semaxstatic int rfcomm_proto_list_parse (uint8_t const *start, uint8_t const *end,
44121054Semax					int *channel, int *error);
45121054Semax
46121054Semax/*
47121054Semax * Lookup RFCOMM channel number in the Protocol Descriptor List
48121054Semax */
49121054Semax
50121054Semax#undef	rfcomm_channel_lookup_exit
51121054Semax#define	rfcomm_channel_lookup_exit(e) { \
52121054Semax	if (error != NULL) \
53121054Semax		*error = (e); \
54121054Semax	if (ss != NULL) { \
55121054Semax		sdp_close(ss); \
56121054Semax		ss = NULL; \
57121054Semax	} \
58121054Semax	return (((e) == 0)? 0 : -1); \
59121054Semax}
60121054Semax
61121054Semaxint
62121054Semaxrfcomm_channel_lookup(bdaddr_t const *local, bdaddr_t const *remote,
63121054Semax			int service, int *channel, int *error)
64121054Semax{
65128077Semax	uint8_t		 buffer[PROTOCOL_DESCRIPTOR_LIST_BUFFER_SIZE];
66128077Semax	void		*ss    = NULL;
67128077Semax	uint16_t	 serv  = (uint16_t) service;
68128077Semax	uint32_t	 attr  = SDP_ATTR_RANGE(
69121054Semax					SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST,
70121054Semax					SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST);
71128077Semax	sdp_attr_t	 proto = { SDP_ATTR_INVALID,0,sizeof(buffer),buffer };
72128077Semax	uint32_t	 type, len;
73121054Semax
74121054Semax	if (local == NULL)
75121054Semax		local = NG_HCI_BDADDR_ANY;
76121054Semax	if (remote == NULL || channel == NULL)
77121054Semax		rfcomm_channel_lookup_exit(EINVAL);
78121054Semax
79121054Semax	if ((ss = sdp_open(local, remote)) == NULL)
80121054Semax		rfcomm_channel_lookup_exit(ENOMEM);
81121054Semax	if (sdp_error(ss) != 0)
82121054Semax		rfcomm_channel_lookup_exit(sdp_error(ss));
83121054Semax
84121054Semax	if (sdp_search(ss, 1, &serv, 1, &attr, 1, &proto) != 0)
85121054Semax		rfcomm_channel_lookup_exit(sdp_error(ss));
86121054Semax	if (proto.flags != SDP_ATTR_OK)
87121054Semax		rfcomm_channel_lookup_exit(ENOATTR);
88121054Semax
89121054Semax	sdp_close(ss);
90121054Semax	ss = NULL;
91121054Semax
92121054Semax	/*
93121054Semax	 * If it is possible for more than one kind of protocol stack to be
94121054Semax	 * used to gain access to the service, the ProtocolDescriptorList
95121054Semax	 * takes the form of a data element alternative. We always use the
96121054Semax	 * first protocol stack.
97121054Semax	 *
98121054Semax	 * A minimal Protocol Descriptor List for RFCOMM based service would
99121054Semax	 * look like
100121054Semax	 *
101121054Semax	 * seq8 len8			- 2 bytes
102121054Semax	 *	seq8 len8		- 2 bytes
103121054Semax	 *		uuid16 value16	- 3 bytes	L2CAP
104121054Semax	 *	seq8 len8		- 2 bytes
105121054Semax	 *		uuid16 value16	- 3 bytes	RFCOMM
106121054Semax	 *		uint8  value8	- 2 bytes	RFCOMM param #1
107121054Semax	 *				=========
108121054Semax	 *				 14 bytes
109121054Semax	 *
110121054Semax	 * Lets not count first [seq8 len8] wrapper, so the minimal size of
111121054Semax	 * the Protocol Descriptor List (the data we are actually interested
112121054Semax	 * in) for RFCOMM based service would be 12 bytes.
113121054Semax	 */
114121054Semax
115121054Semax	if (proto.vlen < PROTOCOL_DESCRIPTOR_LIST_MINIMAL_SIZE)
116121054Semax		rfcomm_channel_lookup_exit(EINVAL);
117121054Semax
118121054Semax	SDP_GET8(type, proto.value);
119121054Semax
120121054Semax	if (type == SDP_DATA_ALT8) {
121121054Semax		SDP_GET8(len, proto.value);
122121054Semax	} else if (type == SDP_DATA_ALT16) {
123121054Semax		SDP_GET16(len, proto.value);
124121054Semax	} else if (type == SDP_DATA_ALT32) {
125121054Semax		SDP_GET32(len, proto.value);
126121054Semax	} else
127121054Semax		len = 0;
128121054Semax
129121054Semax	if (len > 0)
130121054Semax		SDP_GET8(type, proto.value);
131121054Semax
132121054Semax	switch (type) {
133121054Semax	case SDP_DATA_SEQ8:
134121054Semax		SDP_GET8(len, proto.value);
135121054Semax		break;
136121054Semax
137121054Semax	case SDP_DATA_SEQ16:
138121054Semax		SDP_GET16(len, proto.value);
139121054Semax		break;
140121054Semax
141121054Semax	case SDP_DATA_SEQ32:
142121054Semax		SDP_GET32(len, proto.value);
143121054Semax		break;
144121054Semax
145121054Semax	default:
146121054Semax		rfcomm_channel_lookup_exit(ENOATTR);
147121054Semax		/* NOT REACHED */
148121054Semax	}
149121054Semax
150121054Semax	if (len < PROTOCOL_DESCRIPTOR_LIST_MINIMAL_SIZE)
151121054Semax		rfcomm_channel_lookup_exit(EINVAL);
152121054Semax
153121054Semax	return (rfcomm_proto_list_parse(proto.value,
154121054Semax					buffer + proto.vlen, channel, error));
155121054Semax}
156121054Semax
157121054Semax/*
158121054Semax * Parse protocol descriptor list
159121054Semax *
160121054Semax * The ProtocolDescriptorList attribute describes one or more protocol
161121054Semax * stacks that may be used to gain access to the service described by
162121054Semax * the service record. If the ProtocolDescriptorList describes a single
163121054Semax * stack, it takes the form of a data element sequence in which each
164121054Semax * element of the sequence is a protocol descriptor.
165121054Semax */
166121054Semax
167121054Semax#undef	rfcomm_proto_list_parse_exit
168121054Semax#define	rfcomm_proto_list_parse_exit(e) { \
169121054Semax	if (error != NULL) \
170121054Semax		*error = (e); \
171121054Semax	return (((e) == 0)? 0 : -1); \
172121054Semax}
173121054Semax
174121054Semaxstatic int
175128077Semaxrfcomm_proto_list_parse(uint8_t const *start, uint8_t const *end,
176121054Semax			int *channel, int *error)
177121054Semax{
178121054Semax	int	type, len, value;
179121054Semax
180121054Semax	while (start < end) {
181121054Semax
182121054Semax		/*
183121054Semax		 * Parse protocol descriptor
184121054Semax		 *
185121054Semax		 * A protocol descriptor identifies a communications protocol
186121054Semax		 * and provides protocol specific parameters. A protocol
187121054Semax		 * descriptor is represented as a data element sequence. The
188121054Semax		 * first data element in the sequence must be the UUID that
189121054Semax		 * identifies the protocol. Additional data elements optionally
190121054Semax		 * provide protocol specific information, such as the L2CAP
191121054Semax		 * protocol/service multiplexer (PSM) and the RFCOMM server
192121054Semax		 * channel number (CN).
193121054Semax		 */
194121054Semax
195121054Semax		/* We must have at least one byte (type) */
196121054Semax		if (end - start < 1)
197121054Semax			rfcomm_proto_list_parse_exit(EINVAL)
198121054Semax
199121054Semax		SDP_GET8(type, start);
200121054Semax		switch (type) {
201121054Semax		case SDP_DATA_SEQ8:
202121054Semax			SDP_GET8(len, start);
203121054Semax			break;
204121054Semax
205121054Semax		case SDP_DATA_SEQ16:
206121054Semax			SDP_GET16(len, start);
207121054Semax			break;
208121054Semax
209121054Semax		case SDP_DATA_SEQ32:
210121054Semax			SDP_GET32(len, start);
211121054Semax			break;
212121054Semax
213121054Semax		default:
214121054Semax			rfcomm_proto_list_parse_exit(ENOATTR)
215121054Semax			/* NOT REACHED */
216121054Semax		}
217121054Semax
218121054Semax		/* We must have at least 3 bytes (type + UUID16) */
219121054Semax		if (end - start < 3)
220121054Semax			rfcomm_proto_list_parse_exit(EINVAL);
221121054Semax
222121054Semax		/* Get protocol UUID */
223128077Semax		SDP_GET8(type, start); len -= sizeof(uint8_t);
224121054Semax		switch (type) {
225121054Semax		case SDP_DATA_UUID16:
226128077Semax			SDP_GET16(value, start); len -= sizeof(uint16_t);
227121054Semax			if (value != SDP_UUID_PROTOCOL_RFCOMM)
228121054Semax				goto next_protocol;
229121054Semax			break;
230121054Semax
231121054Semax		case SDP_DATA_UUID32:  /* XXX FIXME can we have 32-bit UUID */
232121054Semax		case SDP_DATA_UUID128: /* XXX FIXME can we have 128-bit UUID */
233121054Semax		default:
234121054Semax			rfcomm_proto_list_parse_exit(ENOATTR);
235121054Semax			/* NOT REACHED */
236121054Semax		}
237121054Semax
238121054Semax		/*
239121054Semax		 * First protocol specific parameter for RFCOMM procotol must
240121054Semax		 * be uint8 that represents RFCOMM channel number. So we must
241121054Semax		 * have at least two bytes.
242121054Semax		 */
243121054Semax
244121054Semax		if (end - start < 2)
245121054Semax			rfcomm_proto_list_parse_exit(EINVAL);
246121054Semax
247121054Semax		SDP_GET8(type, start);
248121054Semax		if (type != SDP_DATA_UINT8)
249121054Semax			rfcomm_proto_list_parse_exit(ENOATTR);
250121054Semax
251121054Semax		SDP_GET8(*channel, start);
252121054Semax
253121054Semax		rfcomm_proto_list_parse_exit(0);
254121054Semax		/* NOT REACHED */
255121054Semaxnext_protocol:
256121054Semax		start += len;
257121054Semax	}
258121054Semax
259121054Semax	/*
260121054Semax	 * If we got here then it means we could not find RFCOMM protocol
261121054Semax	 * descriptor, but the reply format was actually valid.
262121054Semax	 */
263121054Semax
264121054Semax	rfcomm_proto_list_parse_exit(ENOATTR);
265121054Semax}
266121054Semax
267