1177059Semax/*
2177059Semax * nap.c
3177059Semax */
4177059Semax
5177059Semax/*-
6177059Semax * Copyright (c) 2008 Maksim Yevmenkin <m_evmenkin@yahoo.com>
7177059Semax * All rights reserved.
8177059Semax *
9177059Semax * Redistribution and use in source and binary forms, with or without
10177059Semax * modification, are permitted provided that the following conditions
11177059Semax * are met:
12177059Semax * 1. Redistributions of source code must retain the above copyright
13177059Semax *    notice, this list of conditions and the following disclaimer.
14177059Semax * 2. Redistributions in binary form must reproduce the above copyright
15177059Semax *    notice, this list of conditions and the following disclaimer in the
16177059Semax *    documentation and/or other materials provided with the distribution.
17177059Semax *
18177059Semax * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19177059Semax * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20177059Semax * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21177059Semax * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22177059Semax * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23177059Semax * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24177059Semax * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25177059Semax * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26177059Semax * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27177059Semax * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28177059Semax * SUCH DAMAGE.
29177059Semax *
30177059Semax * $Id: nap.c,v 1.1 2008/03/11 00:02:42 max Exp $
31177059Semax * $FreeBSD: releng/10.3/usr.sbin/bluetooth/sdpd/nap.c 177358 2008-03-18 18:21:39Z emax $
32177059Semax */
33177059Semax
34177059Semax#include <sys/queue.h>
35177059Semax#include <bluetooth.h>
36177059Semax#include <sdp.h>
37177059Semax#include <string.h>
38177059Semax#include "profile.h"
39177059Semax#include "provider.h"
40177059Semax
41177059Semaxstatic int32_t
42177059Semaxnap_profile_create_service_class_id_list(
43177059Semax		uint8_t *buf, uint8_t const * const eob,
44177059Semax		uint8_t const *data, uint32_t datalen)
45177059Semax{
46177059Semax	static uint16_t	service_classes[] = {
47177059Semax		SDP_SERVICE_CLASS_NAP
48177059Semax	};
49177059Semax
50177059Semax	return (common_profile_create_service_class_id_list(
51177059Semax			buf, eob,
52177059Semax			(uint8_t const *) service_classes,
53177059Semax			sizeof(service_classes)));
54177059Semax}
55177059Semax
56177059Semaxstatic int32_t
57177059Semaxnap_profile_create_bluetooth_profile_descriptor_list(
58177059Semax		uint8_t *buf, uint8_t const * const eob,
59177059Semax		uint8_t const *data, uint32_t datalen)
60177059Semax{
61177059Semax	static uint16_t	profile_descriptor_list[] = {
62177059Semax		SDP_SERVICE_CLASS_NAP,
63177059Semax		0x0100
64177059Semax	};
65177059Semax
66177059Semax	return (common_profile_create_bluetooth_profile_descriptor_list(
67177059Semax			buf, eob,
68177059Semax			(uint8_t const *) profile_descriptor_list,
69177059Semax			sizeof(profile_descriptor_list)));
70177059Semax}
71177059Semax
72177059Semaxstatic int32_t
73177059Semaxnap_profile_create_service_name(
74177059Semax		uint8_t *buf, uint8_t const * const eob,
75177059Semax		uint8_t const *data, uint32_t datalen)
76177059Semax{
77177059Semax	static char	service_name[] = "Network Access Point";
78177059Semax
79177059Semax	return (common_profile_create_string8(
80177059Semax			buf, eob,
81177059Semax			(uint8_t const *) service_name, strlen(service_name)));
82177059Semax}
83177059Semax
84177059Semaxstatic int32_t
85177059Semaxnap_profile_create_service_description(
86177059Semax		uint8_t *buf, uint8_t const * const eob,
87177059Semax		uint8_t const *data, uint32_t datalen)
88177059Semax{
89177059Semax	static char	service_descr[] = "Personal Ad-hoc Network Service";
90177059Semax
91177059Semax	return (common_profile_create_string8(
92177059Semax			buf, eob,
93177059Semax			(uint8_t const *) service_descr,
94177059Semax			strlen(service_descr)));
95177059Semax}
96177059Semax
97177059Semaxstatic int32_t
98177059Semaxnap_profile_create_protocol_descriptor_list(
99177059Semax		uint8_t *buf, uint8_t const * const eob,
100177059Semax		uint8_t const *data, uint32_t datalen)
101177059Semax{
102177358Semax	provider_p		provider = (provider_p) data;
103177358Semax	sdp_nap_profile_p	nap = (sdp_nap_profile_p) provider->data;
104177358Semax
105177059Semax	return (bnep_profile_create_protocol_descriptor_list(
106177358Semax			buf, eob, (uint8_t const *) &nap->psm,
107177358Semax			sizeof(nap->psm)));
108177059Semax}
109177059Semax
110177059Semaxstatic int32_t
111177059Semaxnap_profile_create_security_description(
112177059Semax		uint8_t *buf, uint8_t const * const eob,
113177059Semax		uint8_t const *data, uint32_t datalen)
114177059Semax{
115177059Semax	provider_p		provider = (provider_p) data;
116177059Semax	sdp_nap_profile_p	nap = (sdp_nap_profile_p) provider->data;
117177059Semax
118177059Semax	return (bnep_profile_create_security_description(buf, eob,
119177059Semax			(uint8_t const *) &nap->security_description,
120177059Semax			sizeof(nap->security_description)));
121177059Semax}
122177059Semax
123177059Semaxstatic int32_t
124177059Semaxnap_profile_create_net_access_type(
125177059Semax		uint8_t *buf, uint8_t const * const eob,
126177059Semax		uint8_t const *data, uint32_t datalen)
127177059Semax{
128177059Semax	provider_p		provider = (provider_p) data;
129177059Semax	sdp_nap_profile_p	nap = (sdp_nap_profile_p) provider->data;
130177059Semax
131177059Semax	if (buf + 3 > eob)
132177059Semax		return (-1);
133177059Semax
134177059Semax	SDP_PUT8(SDP_DATA_UINT16, buf);
135177059Semax	SDP_PUT16(nap->net_access_type, buf);
136177059Semax
137177059Semax	return (3);
138177059Semax}
139177059Semax
140177059Semaxstatic int32_t
141177059Semaxnap_profile_create_max_net_access_rate(
142177059Semax		uint8_t *buf, uint8_t const * const eob,
143177059Semax		uint8_t const *data, uint32_t datalen)
144177059Semax{
145177059Semax	provider_p		provider = (provider_p) data;
146177059Semax	sdp_nap_profile_p	nap = (sdp_nap_profile_p) provider->data;
147177059Semax
148177059Semax	if (buf + 3 > eob)
149177059Semax		return (-1);
150177059Semax
151177059Semax	SDP_PUT8(SDP_DATA_UINT16, buf);
152177059Semax	SDP_PUT16(nap->max_net_access_rate, buf);
153177059Semax
154177059Semax	return (3);
155177059Semax}
156177059Semax
157177358Semaxstatic int32_t
158177358Semaxnap_profile_create_service_availability(
159177358Semax		uint8_t *buf, uint8_t const * const eob,
160177358Semax		uint8_t const *data, uint32_t datalen)
161177358Semax{
162177358Semax	provider_p		provider = (provider_p) data;
163177358Semax	sdp_nap_profile_p	nap = (sdp_nap_profile_p) provider->data;
164177358Semax
165177358Semax	return (common_profile_create_service_availability(buf, eob,
166177358Semax			&nap->load_factor, 1));
167177358Semax}
168177358Semax
169177358Semaxstatic int32_t
170177358Semaxnap_profile_data_valid(uint8_t const *data, uint32_t datalen)
171177358Semax{
172177358Semax	sdp_nap_profile_p	nap = (sdp_nap_profile_p) data;
173177358Semax
174177358Semax	return ((nap->psm == 0)? 0 : 1);
175177358Semax}
176177358Semax
177177059Semaxstatic attr_t	nap_profile_attrs[] = {
178177059Semax	{ SDP_ATTR_SERVICE_RECORD_HANDLE,
179177059Semax	  common_profile_create_service_record_handle },
180177059Semax	{ SDP_ATTR_SERVICE_CLASS_ID_LIST,
181177059Semax	  nap_profile_create_service_class_id_list },
182177059Semax	{ SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST,
183177059Semax	  nap_profile_create_protocol_descriptor_list },
184177059Semax	{ SDP_ATTR_LANGUAGE_BASE_ATTRIBUTE_ID_LIST,
185177059Semax	  common_profile_create_language_base_attribute_id_list },
186177358Semax	{ SDP_ATTR_SERVICE_AVAILABILITY,
187177358Semax	  nap_profile_create_service_availability },
188177059Semax	{ SDP_ATTR_BLUETOOTH_PROFILE_DESCRIPTOR_LIST,
189177059Semax	  nap_profile_create_bluetooth_profile_descriptor_list },
190177059Semax	{ SDP_ATTR_PRIMARY_LANGUAGE_BASE_ID + SDP_ATTR_SERVICE_NAME_OFFSET,
191177059Semax	  nap_profile_create_service_name },
192177059Semax	{ SDP_ATTR_PRIMARY_LANGUAGE_BASE_ID + SDP_ATTR_SERVICE_DESCRIPTION_OFFSET,
193177059Semax	  nap_profile_create_service_description },
194177059Semax	{ SDP_ATTR_SECURITY_DESCRIPTION,
195177059Semax	  nap_profile_create_security_description },
196177059Semax	{ SDP_ATTR_NET_ACCESS_TYPE,
197177059Semax	  nap_profile_create_net_access_type },
198177059Semax	{ SDP_ATTR_MAX_NET_ACCESS_RATE,
199177059Semax	  nap_profile_create_max_net_access_rate },
200177059Semax	{ 0, NULL } /* end entry */
201177059Semax};
202177059Semax
203177059Semaxprofile_t	nap_profile_descriptor = {
204177059Semax	SDP_SERVICE_CLASS_NAP,
205177059Semax	sizeof(sdp_nap_profile_t),
206177358Semax	nap_profile_data_valid,
207177059Semax	(attr_t const * const) &nap_profile_attrs
208177059Semax};
209177059Semax
210