xref: /freebsd/usr.sbin/bluetooth/sdpd/audio_source.c (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
121be80aeSHans Petter Selasky /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
321be80aeSHans Petter Selasky  *
421be80aeSHans Petter Selasky  * Copyright (c) 2019 Hans Petter Selasky <hselasky@freebsd.org>
521be80aeSHans Petter Selasky  * All rights reserved.
621be80aeSHans Petter Selasky  *
721be80aeSHans Petter Selasky  * Redistribution and use in source and binary forms, with or without
821be80aeSHans Petter Selasky  * modification, are permitted provided that the following conditions
921be80aeSHans Petter Selasky  * are met:
1021be80aeSHans Petter Selasky  * 1. Redistributions of source code must retain the above copyright
1121be80aeSHans Petter Selasky  *    notice, this list of conditions and the following disclaimer.
1221be80aeSHans Petter Selasky  * 2. Redistributions in binary form must reproduce the above copyright
1321be80aeSHans Petter Selasky  *    notice, this list of conditions and the following disclaimer in the
1421be80aeSHans Petter Selasky  *    documentation and/or other materials provided with the distribution.
1521be80aeSHans Petter Selasky  *
1621be80aeSHans Petter Selasky  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1721be80aeSHans Petter Selasky  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1821be80aeSHans Petter Selasky  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1921be80aeSHans Petter Selasky  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2021be80aeSHans Petter Selasky  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2121be80aeSHans Petter Selasky  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2221be80aeSHans Petter Selasky  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2321be80aeSHans Petter Selasky  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2421be80aeSHans Petter Selasky  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2521be80aeSHans Petter Selasky  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2621be80aeSHans Petter Selasky  * SUCH DAMAGE.
2721be80aeSHans Petter Selasky  */
2821be80aeSHans Petter Selasky 
2921be80aeSHans Petter Selasky #include <sys/queue.h>
3021be80aeSHans Petter Selasky #define	L2CAP_SOCKET_CHECKED
3121be80aeSHans Petter Selasky #include <bluetooth.h>
3221be80aeSHans Petter Selasky #include <sdp.h>
3321be80aeSHans Petter Selasky #include <string.h>
3421be80aeSHans Petter Selasky #include "profile.h"
3521be80aeSHans Petter Selasky #include "provider.h"
3621be80aeSHans Petter Selasky 
3721be80aeSHans Petter Selasky static int32_t
audio_source_profile_create_service_class_id_list(uint8_t * buf,uint8_t const * const eob,uint8_t const * data,uint32_t datalen)3821be80aeSHans Petter Selasky audio_source_profile_create_service_class_id_list(
3921be80aeSHans Petter Selasky     uint8_t *buf, uint8_t const *const eob,
4021be80aeSHans Petter Selasky     uint8_t const *data, uint32_t datalen)
4121be80aeSHans Petter Selasky {
4221be80aeSHans Petter Selasky 	static const uint16_t service_classes[] = {
4321be80aeSHans Petter Selasky 		SDP_SERVICE_CLASS_AUDIO_SOURCE,
4421be80aeSHans Petter Selasky 	};
4521be80aeSHans Petter Selasky 
4621be80aeSHans Petter Selasky 	return (common_profile_create_service_class_id_list(
4721be80aeSHans Petter Selasky 	    buf, eob,
4821be80aeSHans Petter Selasky 	    (uint8_t const *)service_classes,
4921be80aeSHans Petter Selasky 	    sizeof(service_classes)));
5021be80aeSHans Petter Selasky }
5121be80aeSHans Petter Selasky 
5221be80aeSHans Petter Selasky static int32_t
audio_source_profile_create_protocol_descriptor_list(uint8_t * buf,uint8_t const * const eob,uint8_t const * data,uint32_t datalen)5321be80aeSHans Petter Selasky audio_source_profile_create_protocol_descriptor_list(
5421be80aeSHans Petter Selasky     uint8_t *buf, uint8_t const *const eob,
5521be80aeSHans Petter Selasky     uint8_t const *data, uint32_t datalen)
5621be80aeSHans Petter Selasky {
5721be80aeSHans Petter Selasky 	provider_p provider = (provider_p) data;
5821be80aeSHans Petter Selasky 	sdp_audio_source_profile_p audio_source = (sdp_audio_source_profile_p) provider->data;
5921be80aeSHans Petter Selasky 
6021be80aeSHans Petter Selasky 	if (buf + 18 > eob)
6121be80aeSHans Petter Selasky 		return (-1);
6221be80aeSHans Petter Selasky 
6321be80aeSHans Petter Selasky 	SDP_PUT8(SDP_DATA_SEQ8, buf);
6421be80aeSHans Petter Selasky 	SDP_PUT8(16, buf);
6521be80aeSHans Petter Selasky 
6621be80aeSHans Petter Selasky 	SDP_PUT8(SDP_DATA_SEQ8, buf);
6721be80aeSHans Petter Selasky 	SDP_PUT8(6, buf);
6821be80aeSHans Petter Selasky 
6921be80aeSHans Petter Selasky 	SDP_PUT8(SDP_DATA_UUID16, buf);
7021be80aeSHans Petter Selasky 	SDP_PUT16(SDP_UUID_PROTOCOL_L2CAP, buf);
7121be80aeSHans Petter Selasky 
7221be80aeSHans Petter Selasky 	SDP_PUT8(SDP_DATA_UINT16, buf);
7321be80aeSHans Petter Selasky 	SDP_PUT16(audio_source->psm, buf);
7421be80aeSHans Petter Selasky 
7521be80aeSHans Petter Selasky 	SDP_PUT8(SDP_DATA_SEQ8, buf);
7621be80aeSHans Petter Selasky 	SDP_PUT8(6, buf);
7721be80aeSHans Petter Selasky 
7821be80aeSHans Petter Selasky 	SDP_PUT8(SDP_DATA_UUID16, buf);
7921be80aeSHans Petter Selasky 	SDP_PUT16(SDP_UUID_PROTOCOL_AVDTP, buf);
8021be80aeSHans Petter Selasky 
8121be80aeSHans Petter Selasky 	SDP_PUT8(SDP_DATA_UINT16, buf);
8221be80aeSHans Petter Selasky 	SDP_PUT16(audio_source->protover, buf);
8321be80aeSHans Petter Selasky 
8421be80aeSHans Petter Selasky 	return (18);
8521be80aeSHans Petter Selasky }
8621be80aeSHans Petter Selasky 
8721be80aeSHans Petter Selasky static int32_t
audio_source_profile_create_browse_group_list(uint8_t * buf,uint8_t const * const eob,uint8_t const * data,uint32_t datalen)8821be80aeSHans Petter Selasky audio_source_profile_create_browse_group_list(
8921be80aeSHans Petter Selasky     uint8_t *buf, uint8_t const *const eob,
9021be80aeSHans Petter Selasky     uint8_t const *data, uint32_t datalen)
9121be80aeSHans Petter Selasky {
9221be80aeSHans Petter Selasky 
9321be80aeSHans Petter Selasky 	if (buf + 5 > eob)
9421be80aeSHans Petter Selasky 		return (-1);
9521be80aeSHans Petter Selasky 
9621be80aeSHans Petter Selasky 	SDP_PUT8(SDP_DATA_SEQ8, buf);
9721be80aeSHans Petter Selasky 	SDP_PUT8(3, buf);
9821be80aeSHans Petter Selasky 
9921be80aeSHans Petter Selasky 	SDP_PUT8(SDP_DATA_UUID16, buf);
10021be80aeSHans Petter Selasky 	SDP_PUT16(SDP_SERVICE_CLASS_PUBLIC_BROWSE_GROUP, buf);
10121be80aeSHans Petter Selasky 
10221be80aeSHans Petter Selasky 	return (5);
10321be80aeSHans Petter Selasky }
10421be80aeSHans Petter Selasky 
10521be80aeSHans Petter Selasky static int32_t
audio_source_profile_create_bluetooth_profile_descriptor_list(uint8_t * buf,uint8_t const * const eob,uint8_t const * data,uint32_t datalen)10621be80aeSHans Petter Selasky audio_source_profile_create_bluetooth_profile_descriptor_list(
10721be80aeSHans Petter Selasky     uint8_t *buf, uint8_t const *const eob,
10821be80aeSHans Petter Selasky     uint8_t const *data, uint32_t datalen)
10921be80aeSHans Petter Selasky {
11021be80aeSHans Petter Selasky 	static const uint16_t profile_descriptor_list[] = {
11121be80aeSHans Petter Selasky 		SDP_SERVICE_CLASS_ADVANCED_AUDIO_DISTRIBUTION,
11221be80aeSHans Petter Selasky 		0x0100
11321be80aeSHans Petter Selasky 	};
11421be80aeSHans Petter Selasky 
11521be80aeSHans Petter Selasky 	return (common_profile_create_bluetooth_profile_descriptor_list(
11621be80aeSHans Petter Selasky 	    buf, eob,
11721be80aeSHans Petter Selasky 	    (uint8_t const *)profile_descriptor_list,
11821be80aeSHans Petter Selasky 	    sizeof(profile_descriptor_list)));
11921be80aeSHans Petter Selasky }
12021be80aeSHans Petter Selasky 
12121be80aeSHans Petter Selasky static int32_t
audio_source_profile_create_service_name(uint8_t * buf,uint8_t const * const eob,uint8_t const * data,uint32_t datalen)12221be80aeSHans Petter Selasky audio_source_profile_create_service_name(
12321be80aeSHans Petter Selasky     uint8_t *buf, uint8_t const *const eob,
12421be80aeSHans Petter Selasky     uint8_t const *data, uint32_t datalen)
12521be80aeSHans Petter Selasky {
12621be80aeSHans Petter Selasky 	static const char service_name[] = "Audio SRC";
12721be80aeSHans Petter Selasky 
12821be80aeSHans Petter Selasky 	return (common_profile_create_string8(
12921be80aeSHans Petter Selasky 	    buf, eob,
13021be80aeSHans Petter Selasky 	    (uint8_t const *)service_name, strlen(service_name)));
13121be80aeSHans Petter Selasky }
13221be80aeSHans Petter Selasky 
13321be80aeSHans Petter Selasky static int32_t
audio_source_create_supported_features(uint8_t * buf,uint8_t const * const eob,uint8_t const * data,uint32_t datalen)13421be80aeSHans Petter Selasky audio_source_create_supported_features(
13521be80aeSHans Petter Selasky     uint8_t *buf, uint8_t const *const eob,
13621be80aeSHans Petter Selasky     uint8_t const *data, uint32_t datalen)
13721be80aeSHans Petter Selasky {
13821be80aeSHans Petter Selasky 	provider_p provider = (provider_p) data;
13921be80aeSHans Petter Selasky 	sdp_audio_source_profile_p audio_source = (sdp_audio_source_profile_p) provider->data;
14021be80aeSHans Petter Selasky 
14121be80aeSHans Petter Selasky 	if (buf + 3 > eob)
14221be80aeSHans Petter Selasky 		return (-1);
14321be80aeSHans Petter Selasky 
14421be80aeSHans Petter Selasky 	SDP_PUT8(SDP_DATA_UINT16, buf);
14521be80aeSHans Petter Selasky 	SDP_PUT16(audio_source->features, buf);
14621be80aeSHans Petter Selasky 
14721be80aeSHans Petter Selasky 	return (3);
14821be80aeSHans Petter Selasky }
14921be80aeSHans Petter Selasky 
15021be80aeSHans Petter Selasky static int32_t
audio_source_profile_valid(uint8_t const * data,uint32_t datalen)15121be80aeSHans Petter Selasky audio_source_profile_valid(uint8_t const *data, uint32_t datalen)
15221be80aeSHans Petter Selasky {
15321be80aeSHans Petter Selasky 
15421be80aeSHans Petter Selasky 	if (datalen < sizeof(struct sdp_audio_source_profile))
15521be80aeSHans Petter Selasky 		return (0);
15621be80aeSHans Petter Selasky 	return (1);
15721be80aeSHans Petter Selasky }
15821be80aeSHans Petter Selasky 
15921be80aeSHans Petter Selasky static const attr_t audio_source_profile_attrs[] = {
16021be80aeSHans Petter Selasky 	{SDP_ATTR_SERVICE_RECORD_HANDLE,
16121be80aeSHans Petter Selasky 	common_profile_create_service_record_handle},
16221be80aeSHans Petter Selasky 	{SDP_ATTR_SERVICE_CLASS_ID_LIST,
16321be80aeSHans Petter Selasky 	audio_source_profile_create_service_class_id_list},
16421be80aeSHans Petter Selasky 	{SDP_ATTR_PROTOCOL_DESCRIPTOR_LIST,
16521be80aeSHans Petter Selasky 	audio_source_profile_create_protocol_descriptor_list},
16621be80aeSHans Petter Selasky 	{SDP_ATTR_BROWSE_GROUP_LIST,
16721be80aeSHans Petter Selasky 	audio_source_profile_create_browse_group_list},
16821be80aeSHans Petter Selasky 	{SDP_ATTR_LANGUAGE_BASE_ATTRIBUTE_ID_LIST,
16921be80aeSHans Petter Selasky 	common_profile_create_language_base_attribute_id_list},
17021be80aeSHans Petter Selasky 	{SDP_ATTR_BLUETOOTH_PROFILE_DESCRIPTOR_LIST,
17121be80aeSHans Petter Selasky 	audio_source_profile_create_bluetooth_profile_descriptor_list},
17221be80aeSHans Petter Selasky 	{SDP_ATTR_PRIMARY_LANGUAGE_BASE_ID + SDP_ATTR_SERVICE_NAME_OFFSET,
17321be80aeSHans Petter Selasky 	audio_source_profile_create_service_name},
17421be80aeSHans Petter Selasky 	{SDP_ATTR_PRIMARY_LANGUAGE_BASE_ID + SDP_ATTR_PROVIDER_NAME_OFFSET,
17521be80aeSHans Petter Selasky 	common_profile_create_service_provider_name},
17621be80aeSHans Petter Selasky 	{SDP_ATTR_SUPPORTED_FEATURES,
17721be80aeSHans Petter Selasky 	audio_source_create_supported_features},
17821be80aeSHans Petter Selasky 	{}				/* end entry */
17921be80aeSHans Petter Selasky };
18021be80aeSHans Petter Selasky 
18121be80aeSHans Petter Selasky profile_t audio_source_profile_descriptor = {
18221be80aeSHans Petter Selasky 	SDP_SERVICE_CLASS_AUDIO_SOURCE,
18321be80aeSHans Petter Selasky 	sizeof(sdp_audio_source_profile_t),
18421be80aeSHans Petter Selasky 	audio_source_profile_valid,
18521be80aeSHans Petter Selasky 	(attr_t const *const)&audio_source_profile_attrs
18621be80aeSHans Petter Selasky };
187