2022-01-07 17:23:45 +01:00
|
|
|
/* @file
|
|
|
|
* @brief Bluetooth PACS
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2020 Intel Corporation
|
2023-02-20 14:33:43 +01:00
|
|
|
* Copyright (c) 2022-2023 Nordic Semiconductor ASA
|
2022-01-07 17:23:45 +01:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
includes: prefer <zephyr/kernel.h> over <zephyr/zephyr.h>
As of today <zephyr/zephyr.h> is 100% equivalent to <zephyr/kernel.h>.
This patch proposes to then include <zephyr/kernel.h> instead of
<zephyr/zephyr.h> since it is more clear that you are including the
Kernel APIs and (probably) nothing else. <zephyr/zephyr.h> sounds like a
catch-all header that may be confusing. Most applications need to
include a bunch of other things to compile, e.g. driver headers or
subsystem headers like BT, logging, etc.
The idea of a catch-all header in Zephyr is probably not feasible
anyway. Reason is that Zephyr is not a library, like it could be for
example `libpython`. Zephyr provides many utilities nowadays: a kernel,
drivers, subsystems, etc and things will likely grow. A catch-all header
would be massive, difficult to keep up-to-date. It is also likely that
an application will only build a small subset. Note that subsystem-level
headers may use a catch-all approach to make things easier, though.
NOTE: This patch is **NOT** removing the header, just removing its usage
in-tree. I'd advocate for its deprecation (add a #warning on it), but I
understand many people will have concerns.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-08-25 09:58:46 +02:00
|
|
|
#include <zephyr/kernel.h>
|
2022-05-06 11:12:04 +02:00
|
|
|
#include <zephyr/sys/byteorder.h>
|
2022-04-12 16:11:18 +02:00
|
|
|
#include <zephyr/sys/check.h>
|
2022-01-07 17:23:45 +01:00
|
|
|
|
2022-05-06 11:12:04 +02:00
|
|
|
#include <zephyr/device.h>
|
|
|
|
#include <zephyr/init.h>
|
2022-01-07 17:23:45 +01:00
|
|
|
|
2022-05-06 11:12:04 +02:00
|
|
|
#include <zephyr/bluetooth/bluetooth.h>
|
|
|
|
#include <zephyr/bluetooth/conn.h>
|
|
|
|
#include <zephyr/bluetooth/gatt.h>
|
|
|
|
#include <zephyr/bluetooth/audio/audio.h>
|
2022-10-20 11:25:38 +02:00
|
|
|
#include <zephyr/bluetooth/audio/pacs.h>
|
2023-03-15 22:36:16 +01:00
|
|
|
#include <zephyr/sys/slist.h>
|
2022-01-07 17:23:45 +01:00
|
|
|
#include "../host/conn_internal.h"
|
|
|
|
|
2022-11-02 14:31:13 +01:00
|
|
|
#include <zephyr/logging/log.h>
|
|
|
|
|
|
|
|
LOG_MODULE_REGISTER(bt_pacs, CONFIG_BT_PACS_LOG_LEVEL);
|
|
|
|
|
2022-10-25 08:48:54 +02:00
|
|
|
#include "common/bt_str.h"
|
2022-01-07 17:23:45 +01:00
|
|
|
|
2022-07-25 09:56:23 +02:00
|
|
|
#include "audio_internal.h"
|
2022-01-07 17:23:45 +01:00
|
|
|
#include "pacs_internal.h"
|
2023-02-20 14:33:43 +01:00
|
|
|
#include "bap_unicast_server.h"
|
2022-01-07 17:23:45 +01:00
|
|
|
|
2022-01-31 11:28:59 +01:00
|
|
|
#define PAC_NOTIFY_TIMEOUT K_MSEC(10)
|
2023-01-30 15:29:14 +01:00
|
|
|
#define READ_BUF_SEM_TIMEOUT K_MSEC(50)
|
2022-01-07 17:23:45 +01:00
|
|
|
|
2023-03-15 22:36:16 +01:00
|
|
|
#if defined(CONFIG_BT_PAC_SRC)
|
|
|
|
static uint32_t pacs_src_location;
|
|
|
|
static sys_slist_t src_pacs_list = SYS_SLIST_STATIC_INIT(&src_pacs_list);
|
|
|
|
#endif /* CONFIG_BT_PAC_SRC */
|
2022-10-18 23:52:06 +02:00
|
|
|
|
2023-03-15 22:36:16 +01:00
|
|
|
#if defined(CONFIG_BT_PAC_SNK)
|
|
|
|
static uint32_t pacs_snk_location;
|
|
|
|
static sys_slist_t snk_pacs_list = SYS_SLIST_STATIC_INIT(&snk_pacs_list);
|
|
|
|
#endif /* CONFIG_BT_PAC_SNK */
|
2022-10-18 14:46:12 +02:00
|
|
|
|
2023-01-17 01:10:16 +01:00
|
|
|
#if defined(CONFIG_BT_PAC_SNK)
|
2022-10-18 22:58:34 +02:00
|
|
|
static uint16_t snk_available_contexts;
|
2023-01-16 15:32:01 +01:00
|
|
|
static uint16_t snk_supported_contexts = BT_AUDIO_CONTEXT_TYPE_UNSPECIFIED;
|
2022-10-18 22:58:34 +02:00
|
|
|
#else
|
2022-11-24 09:33:09 +01:00
|
|
|
static uint16_t snk_available_contexts = BT_AUDIO_CONTEXT_TYPE_PROHIBITED;
|
|
|
|
static uint16_t snk_supported_contexts = BT_AUDIO_CONTEXT_TYPE_PROHIBITED;
|
2022-10-18 22:58:34 +02:00
|
|
|
#endif /* CONFIG_BT_PAC_SNK */
|
|
|
|
|
|
|
|
#if defined(CONFIG_BT_PAC_SRC)
|
|
|
|
static uint16_t src_available_contexts;
|
2023-01-16 15:32:01 +01:00
|
|
|
static uint16_t src_supported_contexts = BT_AUDIO_CONTEXT_TYPE_UNSPECIFIED;
|
2022-10-18 22:58:34 +02:00
|
|
|
#else
|
2022-11-24 09:33:09 +01:00
|
|
|
static uint16_t src_available_contexts = BT_AUDIO_CONTEXT_TYPE_PROHIBITED;
|
|
|
|
static uint16_t src_supported_contexts = BT_AUDIO_CONTEXT_TYPE_PROHIBITED;
|
2022-10-18 22:58:34 +02:00
|
|
|
#endif /* CONFIG_BT_PAC_SRC */
|
2022-01-07 17:23:45 +01:00
|
|
|
|
2023-01-30 15:29:14 +01:00
|
|
|
static K_SEM_DEFINE(read_buf_sem, 1, 1);
|
2023-01-30 16:49:57 +01:00
|
|
|
NET_BUF_SIMPLE_DEFINE_STATIC(read_buf, BT_ATT_MAX_ATTRIBUTE_LEN);
|
2022-04-12 16:11:18 +02:00
|
|
|
|
2022-10-18 22:37:32 +02:00
|
|
|
struct pac_records_build_data {
|
|
|
|
struct bt_pacs_read_rsp *rsp;
|
|
|
|
struct net_buf_simple *buf;
|
|
|
|
};
|
|
|
|
|
2022-10-20 11:25:38 +02:00
|
|
|
static bool build_pac_records(const struct bt_pacs_cap *cap, void *user_data)
|
2022-10-18 14:46:12 +02:00
|
|
|
{
|
2022-10-18 22:37:32 +02:00
|
|
|
struct pac_records_build_data *data = user_data;
|
2023-05-19 16:55:11 +02:00
|
|
|
const struct bt_audio_codec_cap *codec_cap = cap->codec_cap;
|
2022-10-18 22:37:32 +02:00
|
|
|
struct net_buf_simple *buf = data->buf;
|
2022-10-20 09:47:34 +02:00
|
|
|
struct net_buf_simple_state state;
|
2022-10-28 15:56:32 +02:00
|
|
|
struct bt_pac_codec *pac_codec;
|
2022-10-18 14:46:12 +02:00
|
|
|
|
2022-10-20 09:47:34 +02:00
|
|
|
net_buf_simple_save(buf, &state);
|
|
|
|
|
2022-10-28 15:56:32 +02:00
|
|
|
if (net_buf_simple_tailroom(buf) < sizeof(*pac_codec)) {
|
2022-10-20 09:47:34 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
2022-10-18 14:46:12 +02:00
|
|
|
|
2022-10-28 15:56:32 +02:00
|
|
|
pac_codec = net_buf_simple_add(buf, sizeof(*pac_codec));
|
2023-05-16 14:16:14 +02:00
|
|
|
pac_codec->id = codec_cap->id;
|
|
|
|
pac_codec->cid = sys_cpu_to_le16(codec_cap->cid);
|
|
|
|
pac_codec->vid = sys_cpu_to_le16(codec_cap->vid);
|
2022-10-18 14:46:12 +02:00
|
|
|
|
2023-05-18 17:37:19 +02:00
|
|
|
if (net_buf_simple_tailroom(buf) < (sizeof(struct bt_pac_ltv_data) + codec_cap->data_len)) {
|
2022-10-20 09:47:34 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
2022-10-18 22:37:32 +02:00
|
|
|
|
2023-05-18 17:37:19 +02:00
|
|
|
net_buf_simple_add_u8(buf, codec_cap->data_len);
|
|
|
|
net_buf_simple_add_mem(buf, codec_cap->data, codec_cap->data_len);
|
2022-10-18 22:37:32 +02:00
|
|
|
|
2023-05-18 17:37:19 +02:00
|
|
|
if (net_buf_simple_tailroom(buf) < (sizeof(struct bt_pac_ltv_data) + codec_cap->meta_len)) {
|
2022-10-20 09:47:34 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2023-05-18 17:37:19 +02:00
|
|
|
net_buf_simple_add_u8(buf, codec_cap->meta_len);
|
|
|
|
net_buf_simple_add_mem(buf, codec_cap->meta, codec_cap->meta_len);
|
2022-10-18 22:37:32 +02:00
|
|
|
|
|
|
|
data->rsp->num_pac++;
|
|
|
|
|
|
|
|
return true;
|
2022-10-20 09:47:34 +02:00
|
|
|
|
|
|
|
fail:
|
2023-01-30 16:34:22 +01:00
|
|
|
__ASSERT(false, "No space for %p", cap);
|
2022-10-20 09:47:34 +02:00
|
|
|
|
|
|
|
net_buf_simple_restore(buf, &state);
|
|
|
|
|
|
|
|
return false;
|
2022-10-18 14:46:12 +02:00
|
|
|
}
|
|
|
|
|
2022-10-20 11:25:38 +02:00
|
|
|
static void foreach_cap(sys_slist_t *list, bt_pacs_cap_foreach_func_t func,
|
|
|
|
void *user_data)
|
2022-10-20 10:11:27 +02:00
|
|
|
{
|
2022-10-20 11:25:38 +02:00
|
|
|
struct bt_pacs_cap *cap;
|
2022-10-20 10:11:27 +02:00
|
|
|
|
|
|
|
SYS_SLIST_FOR_EACH_CONTAINER(list, cap, _node) {
|
|
|
|
if (!func(cap, user_data)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-30 15:30:52 +01:00
|
|
|
static void get_pac_records(sys_slist_t *list, struct net_buf_simple *buf)
|
2022-01-07 17:23:45 +01:00
|
|
|
{
|
2022-10-18 22:37:32 +02:00
|
|
|
struct pac_records_build_data data;
|
2022-01-07 17:23:45 +01:00
|
|
|
|
|
|
|
/* Reset if buffer before using */
|
2022-01-31 14:28:59 +01:00
|
|
|
net_buf_simple_reset(buf);
|
2022-01-07 17:23:45 +01:00
|
|
|
|
2022-10-18 22:37:32 +02:00
|
|
|
data.rsp = net_buf_simple_add(buf, sizeof(*data.rsp));
|
|
|
|
data.rsp->num_pac = 0;
|
|
|
|
data.buf = buf;
|
2022-01-07 17:23:45 +01:00
|
|
|
|
2022-10-20 11:25:38 +02:00
|
|
|
foreach_cap(list, build_pac_records, &data);
|
2022-01-31 14:28:59 +01:00
|
|
|
}
|
|
|
|
|
2022-05-19 16:52:44 +02:00
|
|
|
static void available_context_cfg_changed(const struct bt_gatt_attr *attr, uint16_t value)
|
2022-01-07 17:23:45 +01:00
|
|
|
{
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("attr %p value 0x%04x", attr, value);
|
2022-01-07 17:23:45 +01:00
|
|
|
}
|
|
|
|
|
2022-05-19 16:52:44 +02:00
|
|
|
static ssize_t available_contexts_read(struct bt_conn *conn,
|
|
|
|
const struct bt_gatt_attr *attr, void *buf,
|
|
|
|
uint16_t len, uint16_t offset)
|
|
|
|
{
|
2022-10-18 22:58:34 +02:00
|
|
|
struct bt_pacs_context context = {
|
|
|
|
.snk = sys_cpu_to_le16(snk_available_contexts),
|
|
|
|
.src = sys_cpu_to_le16(src_available_contexts),
|
|
|
|
};
|
2022-01-07 17:23:45 +01:00
|
|
|
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("conn %p attr %p buf %p len %u offset %u", conn, attr, buf, len, offset);
|
2022-01-07 17:23:45 +01:00
|
|
|
|
|
|
|
return bt_gatt_attr_read(conn, attr, buf, len, offset, &context,
|
|
|
|
sizeof(context));
|
|
|
|
}
|
|
|
|
|
2023-07-06 14:23:25 +02:00
|
|
|
#if defined(CONFIG_BT_PACS_SUPPORTED_CONTEXT_NOTIFIABLE)
|
2022-01-07 17:23:45 +01:00
|
|
|
static void supported_context_cfg_changed(const struct bt_gatt_attr *attr,
|
|
|
|
uint16_t value)
|
|
|
|
{
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("attr %p value 0x%04x", attr, value);
|
2022-01-07 17:23:45 +01:00
|
|
|
}
|
2023-07-06 14:23:25 +02:00
|
|
|
#endif /* CONFIG_BT_PACS_SUPPORTED_CONTEXT_NOTIFIABLE */
|
2022-01-07 17:23:45 +01:00
|
|
|
|
|
|
|
static ssize_t supported_context_read(struct bt_conn *conn,
|
|
|
|
const struct bt_gatt_attr *attr,
|
|
|
|
void *buf, uint16_t len, uint16_t offset)
|
|
|
|
{
|
2022-10-18 22:58:34 +02:00
|
|
|
struct bt_pacs_context context = {
|
|
|
|
.snk = sys_cpu_to_le16(snk_supported_contexts),
|
|
|
|
.src = sys_cpu_to_le16(src_supported_contexts),
|
|
|
|
};
|
2022-01-07 17:23:45 +01:00
|
|
|
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("conn %p attr %p buf %p len %u offset %u", conn, attr, buf, len, offset);
|
2022-01-07 17:23:45 +01:00
|
|
|
|
|
|
|
return bt_gatt_attr_read(conn, attr, buf, len, offset, &context,
|
|
|
|
sizeof(context));
|
|
|
|
}
|
2022-02-08 14:43:43 +01:00
|
|
|
|
2023-03-15 22:36:16 +01:00
|
|
|
static int available_contexts_notify(void);
|
2023-07-06 14:23:25 +02:00
|
|
|
#if defined(CONFIG_BT_PACS_SUPPORTED_CONTEXT_NOTIFIABLE)
|
2023-03-15 22:36:16 +01:00
|
|
|
static int supported_contexts_notify(void);
|
2023-07-06 14:23:25 +02:00
|
|
|
#endif /* CONFIG_BT_PACS_SUPPORTED_CONTEXT_NOTIFIABLE */
|
2022-10-18 22:58:34 +02:00
|
|
|
|
|
|
|
static int set_available_contexts(uint16_t contexts, uint16_t *available,
|
2022-11-24 09:33:09 +01:00
|
|
|
uint16_t supported)
|
2022-10-18 22:58:34 +02:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (contexts & ~supported) {
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (contexts == *available) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-03-15 22:36:16 +01:00
|
|
|
err = available_contexts_notify();
|
|
|
|
if (err) {
|
2022-11-24 09:33:09 +01:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2022-10-18 22:58:34 +02:00
|
|
|
*available = contexts;
|
|
|
|
|
2022-11-24 09:33:09 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int set_supported_contexts(uint16_t contexts, uint16_t *supported,
|
|
|
|
uint16_t *available)
|
|
|
|
{
|
|
|
|
int err;
|
2023-03-15 22:36:16 +01:00
|
|
|
uint16_t tmp_supported = *supported;
|
|
|
|
uint16_t tmp_available = *available;
|
2022-11-24 09:33:09 +01:00
|
|
|
|
|
|
|
/* Ensure unspecified is always supported */
|
|
|
|
contexts |= BT_AUDIO_CONTEXT_TYPE_UNSPECIFIED;
|
|
|
|
|
|
|
|
if (*supported == contexts) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
*supported = contexts;
|
|
|
|
|
|
|
|
/* Update available contexts if needed*/
|
|
|
|
if ((contexts & *available) != *available) {
|
2023-03-15 22:36:16 +01:00
|
|
|
err = set_available_contexts(contexts & *available, available, contexts);
|
|
|
|
if (err) {
|
|
|
|
*available = tmp_available;
|
|
|
|
*supported = tmp_supported;
|
|
|
|
|
|
|
|
return err;
|
2022-11-24 09:33:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-06 14:23:25 +02:00
|
|
|
#if defined(CONFIG_BT_PACS_SUPPORTED_CONTEXT_NOTIFIABLE)
|
2023-03-15 22:36:16 +01:00
|
|
|
err = supported_contexts_notify();
|
|
|
|
if (err) {
|
|
|
|
*supported = tmp_supported;
|
|
|
|
*available = tmp_available;
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
2023-07-06 14:23:25 +02:00
|
|
|
#endif /* CONFIG_BT_PACS_SUPPORTED_CONTEXT_NOTIFIABLE */
|
2023-03-15 22:36:16 +01:00
|
|
|
|
2022-10-18 22:58:34 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-01-07 17:23:45 +01:00
|
|
|
#if defined(CONFIG_BT_PAC_SNK)
|
|
|
|
static ssize_t snk_read(struct bt_conn *conn, const struct bt_gatt_attr *attr,
|
|
|
|
void *buf, uint16_t len, uint16_t offset)
|
|
|
|
{
|
2023-01-30 15:29:14 +01:00
|
|
|
ssize_t ret_val;
|
|
|
|
int err;
|
|
|
|
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("conn %p attr %p buf %p len %u offset %u", conn, attr, buf, len, offset);
|
2022-01-07 17:23:45 +01:00
|
|
|
|
2023-01-30 15:29:14 +01:00
|
|
|
err = k_sem_take(&read_buf_sem, READ_BUF_SEM_TIMEOUT);
|
|
|
|
if (err != 0) {
|
|
|
|
LOG_DBG("Failed to take read_buf_sem: %d", err);
|
|
|
|
|
|
|
|
return BT_GATT_ERR(BT_ATT_ERR_INSUFFICIENT_RESOURCES);
|
|
|
|
}
|
|
|
|
|
2023-03-15 22:36:16 +01:00
|
|
|
get_pac_records(&snk_pacs_list, &read_buf);
|
2022-09-22 14:07:57 +02:00
|
|
|
|
2023-01-30 15:29:14 +01:00
|
|
|
ret_val = bt_gatt_attr_read(conn, attr, buf, len, offset, read_buf.data,
|
|
|
|
read_buf.len);
|
|
|
|
|
|
|
|
k_sem_give(&read_buf_sem);
|
|
|
|
|
|
|
|
return ret_val;
|
2022-01-07 17:23:45 +01:00
|
|
|
}
|
|
|
|
|
2023-07-06 14:23:25 +02:00
|
|
|
#if defined(CONFIG_BT_PAC_SNK_NOTIFIABLE)
|
2022-04-13 21:10:18 +02:00
|
|
|
static void snk_cfg_changed(const struct bt_gatt_attr *attr, uint16_t value)
|
|
|
|
{
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("attr %p value 0x%04x", attr, value);
|
2022-04-13 21:10:18 +02:00
|
|
|
}
|
2023-07-06 14:23:25 +02:00
|
|
|
#endif /* CONFIG_BT_PAC_SNK_NOTIFIABLE */
|
2022-04-13 21:10:18 +02:00
|
|
|
|
2022-10-18 22:58:34 +02:00
|
|
|
static inline int set_snk_available_contexts(uint16_t contexts)
|
|
|
|
{
|
|
|
|
return set_available_contexts(contexts, &snk_available_contexts,
|
|
|
|
snk_supported_contexts);
|
|
|
|
}
|
2022-11-24 09:33:09 +01:00
|
|
|
|
|
|
|
static inline int set_snk_supported_contexts(uint16_t contexts)
|
|
|
|
{
|
|
|
|
return set_supported_contexts(contexts, &snk_supported_contexts,
|
|
|
|
&snk_available_contexts);
|
|
|
|
}
|
2022-10-18 22:58:34 +02:00
|
|
|
#else
|
|
|
|
static inline int set_snk_available_contexts(uint16_t contexts)
|
|
|
|
{
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
2022-11-24 09:33:09 +01:00
|
|
|
|
|
|
|
static inline int set_snk_supported_contexts(uint16_t contexts)
|
|
|
|
{
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
2022-10-18 23:52:06 +02:00
|
|
|
#endif /* CONFIG_BT_PAC_SNK */
|
2022-10-18 22:58:34 +02:00
|
|
|
|
2022-04-13 21:10:18 +02:00
|
|
|
#if defined(CONFIG_BT_PAC_SNK_LOC)
|
2023-03-15 22:36:16 +01:00
|
|
|
static int pac_notify_snk_loc(void);
|
2022-01-07 17:23:45 +01:00
|
|
|
static ssize_t snk_loc_read(struct bt_conn *conn,
|
|
|
|
const struct bt_gatt_attr *attr, void *buf,
|
|
|
|
uint16_t len, uint16_t offset)
|
|
|
|
{
|
2023-03-15 22:36:16 +01:00
|
|
|
uint32_t location = sys_cpu_to_le32(pacs_snk_location);
|
2022-02-08 14:43:43 +01:00
|
|
|
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("conn %p attr %p buf %p len %u offset %u", conn, attr, buf, len, offset);
|
2022-01-07 17:23:45 +01:00
|
|
|
|
2022-10-18 23:52:06 +02:00
|
|
|
return bt_gatt_attr_read(conn, attr, buf, len, offset, &location,
|
|
|
|
sizeof(location));
|
|
|
|
}
|
|
|
|
|
2023-07-06 14:23:25 +02:00
|
|
|
#if defined(CONFIG_BT_PAC_SNK_LOC_NOTIFIABLE)
|
2022-10-18 23:52:06 +02:00
|
|
|
static void snk_loc_cfg_changed(const struct bt_gatt_attr *attr, uint16_t value)
|
|
|
|
{
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("attr %p value 0x%04x", attr, value);
|
2022-10-18 23:52:06 +02:00
|
|
|
}
|
2023-07-06 14:23:25 +02:00
|
|
|
#endif /* CONFIG_BT_PAC_SNK_LOC_NOTIFIABLE */
|
2022-02-08 14:43:43 +01:00
|
|
|
|
2022-10-18 23:52:06 +02:00
|
|
|
static int set_snk_location(enum bt_audio_location audio_location)
|
|
|
|
{
|
2023-03-15 22:36:16 +01:00
|
|
|
int err;
|
|
|
|
|
|
|
|
if (audio_location == pacs_snk_location) {
|
2022-10-18 23:52:06 +02:00
|
|
|
return 0;
|
2022-02-08 14:43:43 +01:00
|
|
|
}
|
|
|
|
|
2023-03-15 22:36:16 +01:00
|
|
|
pacs_snk_location = audio_location;
|
2022-02-08 14:43:43 +01:00
|
|
|
|
2023-03-15 22:36:16 +01:00
|
|
|
err = pac_notify_snk_loc();
|
2022-01-07 17:23:45 +01:00
|
|
|
|
2023-03-15 22:36:16 +01:00
|
|
|
return err;
|
2022-10-18 23:52:06 +02:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
static int set_snk_location(enum bt_audio_location location)
|
2022-10-18 14:46:12 +02:00
|
|
|
{
|
2022-10-18 23:52:06 +02:00
|
|
|
return -ENOTSUP;
|
2022-10-18 14:46:12 +02:00
|
|
|
}
|
2022-10-18 23:52:06 +02:00
|
|
|
#endif /* CONFIG_BT_PAC_SNK_LOC */
|
2022-10-18 14:46:12 +02:00
|
|
|
|
2022-04-13 21:10:18 +02:00
|
|
|
#if defined(CONFIG_BT_PAC_SNK_LOC_WRITEABLE)
|
2022-01-07 17:23:45 +01:00
|
|
|
static ssize_t snk_loc_write(struct bt_conn *conn,
|
|
|
|
const struct bt_gatt_attr *attr, const void *data,
|
|
|
|
uint16_t len, uint16_t offset, uint8_t flags)
|
|
|
|
{
|
2022-02-08 14:43:43 +01:00
|
|
|
int err;
|
|
|
|
enum bt_audio_location location;
|
|
|
|
|
2022-01-07 17:23:45 +01:00
|
|
|
if (offset) {
|
|
|
|
return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
|
|
|
|
}
|
|
|
|
|
2022-02-08 14:43:43 +01:00
|
|
|
if (len != sizeof(location)) {
|
2022-08-23 14:55:23 +02:00
|
|
|
return BT_GATT_ERR(BT_ATT_ERR_WRITE_REQ_REJECTED);
|
2022-01-07 17:23:45 +01:00
|
|
|
}
|
|
|
|
|
2022-02-08 14:43:43 +01:00
|
|
|
location = (enum bt_audio_location)sys_get_le32(data);
|
|
|
|
if (location > BT_AUDIO_LOCATION_MASK || location == 0) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("Invalid location value: 0x%08X", location);
|
2022-08-23 14:55:23 +02:00
|
|
|
return BT_GATT_ERR(BT_ATT_ERR_WRITE_REQ_REJECTED);
|
2022-02-08 14:43:43 +01:00
|
|
|
}
|
|
|
|
|
2022-10-18 23:52:06 +02:00
|
|
|
err = set_snk_location(location);
|
2022-02-08 14:43:43 +01:00
|
|
|
if (err != 0) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("write_location returned %d", err);
|
2022-08-23 14:55:23 +02:00
|
|
|
return BT_GATT_ERR(BT_ATT_ERR_WRITE_REQ_REJECTED);
|
2022-02-08 14:43:43 +01:00
|
|
|
}
|
2022-01-07 17:23:45 +01:00
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
2022-04-13 21:10:18 +02:00
|
|
|
#endif /* CONFIG_BT_PAC_SNK_LOC_WRITEABLE */
|
2022-01-07 17:23:45 +01:00
|
|
|
|
|
|
|
#if defined(CONFIG_BT_PAC_SRC)
|
|
|
|
static ssize_t src_read(struct bt_conn *conn, const struct bt_gatt_attr *attr,
|
|
|
|
void *buf, uint16_t len, uint16_t offset)
|
|
|
|
{
|
2023-01-30 15:29:14 +01:00
|
|
|
ssize_t ret_val;
|
|
|
|
int err;
|
|
|
|
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("conn %p attr %p buf %p len %u offset %u", conn, attr, buf, len, offset);
|
2022-01-07 17:23:45 +01:00
|
|
|
|
2023-01-30 15:29:14 +01:00
|
|
|
err = k_sem_take(&read_buf_sem, READ_BUF_SEM_TIMEOUT);
|
|
|
|
if (err != 0) {
|
|
|
|
LOG_DBG("Failed to take read_buf_sem: %d", err);
|
|
|
|
|
|
|
|
return BT_GATT_ERR(BT_ATT_ERR_INSUFFICIENT_RESOURCES);
|
|
|
|
}
|
|
|
|
|
2023-03-15 22:36:16 +01:00
|
|
|
get_pac_records(&src_pacs_list, &read_buf);
|
2022-09-22 14:07:57 +02:00
|
|
|
|
2023-01-30 15:29:14 +01:00
|
|
|
ret_val = bt_gatt_attr_read(conn, attr, buf, len, offset, read_buf.data,
|
|
|
|
read_buf.len);
|
|
|
|
|
|
|
|
k_sem_give(&read_buf_sem);
|
|
|
|
|
|
|
|
return ret_val;
|
2022-01-07 17:23:45 +01:00
|
|
|
}
|
|
|
|
|
2022-04-13 21:10:18 +02:00
|
|
|
static void src_cfg_changed(const struct bt_gatt_attr *attr, uint16_t value)
|
|
|
|
{
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("attr %p value 0x%04x", attr, value);
|
2022-04-13 21:10:18 +02:00
|
|
|
}
|
|
|
|
|
2022-10-18 22:58:34 +02:00
|
|
|
static inline int set_src_available_contexts(uint16_t contexts)
|
|
|
|
{
|
|
|
|
return set_available_contexts(contexts, &src_available_contexts,
|
|
|
|
src_supported_contexts);
|
|
|
|
}
|
2022-11-24 09:33:09 +01:00
|
|
|
|
|
|
|
static inline int set_src_supported_contexts(uint16_t contexts)
|
|
|
|
{
|
|
|
|
return set_supported_contexts(contexts, &src_supported_contexts,
|
2023-01-16 15:27:56 +01:00
|
|
|
&src_available_contexts);
|
2022-11-24 09:33:09 +01:00
|
|
|
}
|
2022-10-18 22:58:34 +02:00
|
|
|
#else
|
|
|
|
static inline int set_src_available_contexts(uint16_t contexts)
|
|
|
|
{
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
2022-11-24 09:33:09 +01:00
|
|
|
|
|
|
|
static inline int set_src_supported_contexts(uint16_t contexts)
|
|
|
|
{
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
2022-10-18 23:52:06 +02:00
|
|
|
#endif /* CONFIG_BT_PAC_SRC */
|
2022-10-18 22:58:34 +02:00
|
|
|
|
2022-04-13 21:10:18 +02:00
|
|
|
#if defined(CONFIG_BT_PAC_SRC_LOC)
|
2023-07-06 14:23:25 +02:00
|
|
|
#if defined(CONFIG_BT_PAC_SRC_LOC_NOTIFIABLE)
|
2023-03-15 22:36:16 +01:00
|
|
|
static int pac_notify_src_loc(void);
|
2023-07-06 14:23:25 +02:00
|
|
|
#endif /* CONFIG_BT_PAC_SRC_LOC_NOTIFIABLE */
|
2022-01-07 17:23:45 +01:00
|
|
|
static ssize_t src_loc_read(struct bt_conn *conn,
|
|
|
|
const struct bt_gatt_attr *attr, void *buf,
|
|
|
|
uint16_t len, uint16_t offset)
|
|
|
|
{
|
2023-03-15 22:36:16 +01:00
|
|
|
uint32_t location = sys_cpu_to_le32(pacs_src_location);
|
2022-02-08 14:43:43 +01:00
|
|
|
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("conn %p attr %p buf %p len %u offset %u", conn, attr, buf, len, offset);
|
2022-01-07 17:23:45 +01:00
|
|
|
|
2022-10-18 23:52:06 +02:00
|
|
|
return bt_gatt_attr_read(conn, attr, buf, len, offset, &location,
|
|
|
|
sizeof(location));
|
|
|
|
}
|
2022-02-08 14:43:43 +01:00
|
|
|
|
2023-07-06 14:23:25 +02:00
|
|
|
#if defined(CONFIG_BT_PAC_SRC_LOC_NOTIFIABLE)
|
2022-10-18 23:52:06 +02:00
|
|
|
static void src_loc_cfg_changed(const struct bt_gatt_attr *attr, uint16_t value)
|
|
|
|
{
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("attr %p value 0x%04x", attr, value);
|
2022-10-18 23:52:06 +02:00
|
|
|
}
|
2023-07-06 14:23:25 +02:00
|
|
|
#endif /* CONFIG_BT_PAC_SRC_LOC_NOTIFIABLE */
|
2022-10-18 23:52:06 +02:00
|
|
|
|
|
|
|
static int set_src_location(enum bt_audio_location audio_location)
|
|
|
|
{
|
2023-07-06 14:23:25 +02:00
|
|
|
int err = 0;
|
|
|
|
#if defined(CONFIG_BT_PAC_SRC_LOC_NOTIFIABLE)
|
|
|
|
enum bt_audio_location tmp_audio_location = pacs_src_location;
|
|
|
|
#endif /* CONFIG_BT_PAC_SRC_LOC_NOTIFIABLE */
|
2023-03-15 22:36:16 +01:00
|
|
|
|
|
|
|
if (audio_location == pacs_src_location) {
|
2022-10-18 23:52:06 +02:00
|
|
|
return 0;
|
2022-02-08 14:43:43 +01:00
|
|
|
}
|
|
|
|
|
2023-03-15 22:36:16 +01:00
|
|
|
pacs_src_location = audio_location;
|
2022-02-08 14:43:43 +01:00
|
|
|
|
2023-07-06 14:23:25 +02:00
|
|
|
#if defined(CONFIG_BT_PAC_SRC_LOC_NOTIFIABLE)
|
2023-03-15 22:36:16 +01:00
|
|
|
err = pac_notify_src_loc();
|
|
|
|
if (err) {
|
|
|
|
LOG_DBG("Notify failed, previous audio location restored");
|
|
|
|
audio_location = tmp_audio_location;
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
2023-07-06 14:23:25 +02:00
|
|
|
#endif /* CONFIG_BT_PAC_SRC_LOC_NOTIFIABLE */
|
2022-10-18 23:52:06 +02:00
|
|
|
|
2023-07-06 14:23:25 +02:00
|
|
|
return err;
|
2022-10-18 23:52:06 +02:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
static int set_src_location(enum bt_audio_location location)
|
|
|
|
{
|
|
|
|
return -ENOTSUP;
|
2022-01-07 17:23:45 +01:00
|
|
|
}
|
2022-10-18 23:52:06 +02:00
|
|
|
#endif /* CONFIG_BT_PAC_SRC_LOC */
|
2022-01-07 17:23:45 +01:00
|
|
|
|
2022-07-22 16:13:15 +08:00
|
|
|
#if defined(CONFIG_BT_PAC_SRC_LOC_WRITEABLE)
|
2022-01-07 17:23:45 +01:00
|
|
|
static ssize_t src_loc_write(struct bt_conn *conn,
|
|
|
|
const struct bt_gatt_attr *attr, const void *data,
|
|
|
|
uint16_t len, uint16_t offset, uint8_t flags)
|
|
|
|
{
|
2022-02-08 14:43:43 +01:00
|
|
|
int err;
|
|
|
|
uint32_t location;
|
|
|
|
|
2022-01-07 17:23:45 +01:00
|
|
|
if (offset) {
|
|
|
|
return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
|
|
|
|
}
|
|
|
|
|
2022-02-08 14:43:43 +01:00
|
|
|
if (len != sizeof(location)) {
|
2022-08-23 14:55:23 +02:00
|
|
|
return BT_GATT_ERR(BT_ATT_ERR_WRITE_REQ_REJECTED);
|
2022-01-07 17:23:45 +01:00
|
|
|
}
|
|
|
|
|
2022-02-08 14:43:43 +01:00
|
|
|
location = (enum bt_audio_location)sys_get_le32(data);
|
|
|
|
if (location > BT_AUDIO_LOCATION_MASK || location == 0) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("Invalid location value: 0x%08X", location);
|
2022-08-23 14:55:23 +02:00
|
|
|
return BT_GATT_ERR(BT_ATT_ERR_WRITE_REQ_REJECTED);
|
2022-02-08 14:43:43 +01:00
|
|
|
}
|
|
|
|
|
2022-10-18 23:52:06 +02:00
|
|
|
err = set_src_location(location);
|
2022-02-08 14:43:43 +01:00
|
|
|
if (err != 0) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("write_location returned %d", err);
|
2022-08-23 14:55:23 +02:00
|
|
|
return BT_GATT_ERR(BT_ATT_ERR_WRITE_REQ_REJECTED);
|
2022-02-08 14:43:43 +01:00
|
|
|
}
|
2022-01-07 17:23:45 +01:00
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
2022-07-22 16:13:15 +08:00
|
|
|
#endif /* CONFIG_BT_PAC_SRC_LOC_WRITEABLE */
|
2022-01-07 17:23:45 +01:00
|
|
|
|
|
|
|
BT_GATT_SERVICE_DEFINE(pacs_svc,
|
|
|
|
BT_GATT_PRIMARY_SERVICE(BT_UUID_PACS),
|
|
|
|
#if defined(CONFIG_BT_PAC_SNK)
|
2023-07-06 14:23:25 +02:00
|
|
|
#if defined(CONFIG_BT_PAC_SNK_NOTIFIABLE)
|
2022-07-25 09:56:23 +02:00
|
|
|
BT_AUDIO_CHRC(BT_UUID_PACS_SNK,
|
|
|
|
BT_GATT_CHRC_READ | BT_GATT_CHRC_NOTIFY,
|
|
|
|
BT_GATT_PERM_READ_ENCRYPT,
|
|
|
|
snk_read, NULL, NULL),
|
|
|
|
BT_AUDIO_CCC(snk_cfg_changed),
|
2023-07-06 14:23:25 +02:00
|
|
|
#else
|
|
|
|
BT_AUDIO_CHRC(BT_UUID_PACS_SNK,
|
|
|
|
BT_GATT_CHRC_READ,
|
|
|
|
BT_GATT_PERM_READ_ENCRYPT,
|
|
|
|
snk_read, NULL, NULL),
|
|
|
|
#endif /* CONFIG_BT_PAC_SNK_NOTIFIABLE */
|
2022-10-18 22:18:55 +02:00
|
|
|
#if defined(CONFIG_BT_PAC_SNK_LOC)
|
2023-07-06 14:23:25 +02:00
|
|
|
#if defined(CONFIG_BT_PAC_SNK_LOC_WRITEABLE) && defined(CONFIG_BT_PAC_SNK_LOC_NOTIFIABLE)
|
2022-07-25 09:56:23 +02:00
|
|
|
BT_AUDIO_CHRC(BT_UUID_PACS_SNK_LOC,
|
|
|
|
BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE | BT_GATT_CHRC_NOTIFY,
|
|
|
|
BT_GATT_PERM_READ_ENCRYPT | BT_GATT_PERM_WRITE_ENCRYPT,
|
|
|
|
snk_loc_read, snk_loc_write, NULL),
|
2023-07-06 14:23:25 +02:00
|
|
|
BT_AUDIO_CCC(snk_loc_cfg_changed),
|
|
|
|
#elif defined(CONFIG_BT_PAC_SNK_LOC_NOTIFIABLE)
|
2022-07-25 09:56:23 +02:00
|
|
|
BT_AUDIO_CHRC(BT_UUID_PACS_SNK_LOC,
|
|
|
|
BT_GATT_CHRC_READ | BT_GATT_CHRC_NOTIFY,
|
|
|
|
BT_GATT_PERM_READ_ENCRYPT,
|
|
|
|
snk_loc_read, NULL, NULL),
|
|
|
|
BT_AUDIO_CCC(snk_loc_cfg_changed),
|
2023-07-06 14:23:25 +02:00
|
|
|
#elif defined(CONFIG_BT_PAC_SNK_LOC_WRITEABLE)
|
|
|
|
BT_AUDIO_CHRC(BT_UUID_PACS_SNK_LOC,
|
|
|
|
BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE,
|
|
|
|
BT_GATT_PERM_READ_ENCRYPT,
|
|
|
|
snk_loc_read, snk_loc_write, NULL),
|
|
|
|
#else
|
|
|
|
BT_AUDIO_CHRC(BT_UUID_PACS_SNK_LOC,
|
|
|
|
BT_GATT_CHRC_READ,
|
|
|
|
BT_GATT_PERM_READ_ENCRYPT,
|
|
|
|
snk_loc_read, NULL, NULL),
|
|
|
|
#endif /* (CONFIG_BT_PAC_SNK_LOC_WRITEABLE && CONFIG_BT_PAC_SNK_LOC_NOTIFIABLE) */
|
2022-10-18 22:18:55 +02:00
|
|
|
#endif /* CONFIG_BT_PAC_SNK_LOC */
|
2022-01-07 17:23:45 +01:00
|
|
|
#endif /* CONFIG_BT_PAC_SNK */
|
|
|
|
#if defined(CONFIG_BT_PAC_SRC)
|
2023-07-06 14:23:25 +02:00
|
|
|
#if defined(CONFIG_BT_PAC_SRC_NOTIFIABLE)
|
2022-07-25 09:56:23 +02:00
|
|
|
BT_AUDIO_CHRC(BT_UUID_PACS_SRC,
|
|
|
|
BT_GATT_CHRC_READ | BT_GATT_CHRC_NOTIFY,
|
|
|
|
BT_GATT_PERM_READ_ENCRYPT,
|
|
|
|
src_read, NULL, NULL),
|
|
|
|
BT_AUDIO_CCC(src_cfg_changed),
|
2023-07-06 14:23:25 +02:00
|
|
|
#else
|
|
|
|
BT_AUDIO_CHRC(BT_UUID_PACS_SRC,
|
|
|
|
BT_GATT_CHRC_READ,
|
|
|
|
BT_GATT_PERM_READ_ENCRYPT,
|
|
|
|
src_read, NULL, NULL),
|
|
|
|
BT_AUDIO_CCC(src_cfg_changed),
|
|
|
|
#endif /* CONFIG_BT_PAC_SRC_NOTIFIABLE */
|
2022-10-18 22:18:55 +02:00
|
|
|
#if defined(CONFIG_BT_PAC_SRC_LOC)
|
2023-07-06 14:23:25 +02:00
|
|
|
#if defined(CONFIG_BT_PAC_SRC_LOC_WRITEABLE) && defined(CONFIG_BT_PAC_SRC_LOC_NOTIFIABLE)
|
2022-07-25 09:56:23 +02:00
|
|
|
BT_AUDIO_CHRC(BT_UUID_PACS_SRC_LOC,
|
|
|
|
BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE | BT_GATT_CHRC_NOTIFY,
|
2022-08-17 14:25:26 +02:00
|
|
|
BT_GATT_PERM_READ_ENCRYPT | BT_GATT_PERM_WRITE_ENCRYPT,
|
2022-07-25 09:56:23 +02:00
|
|
|
src_loc_read, src_loc_write, NULL),
|
2023-07-06 14:23:25 +02:00
|
|
|
BT_AUDIO_CCC(src_loc_cfg_changed),
|
|
|
|
#elif defined(CONFIG_BT_PAC_SRC_LOC_NOTIFIABLE)
|
2022-07-25 09:56:23 +02:00
|
|
|
BT_AUDIO_CHRC(BT_UUID_PACS_SRC_LOC,
|
|
|
|
BT_GATT_CHRC_READ | BT_GATT_CHRC_NOTIFY,
|
|
|
|
BT_GATT_PERM_READ_ENCRYPT,
|
|
|
|
src_loc_read, NULL, NULL),
|
|
|
|
BT_AUDIO_CCC(src_loc_cfg_changed),
|
2023-07-06 14:23:25 +02:00
|
|
|
#elif defined(CONFIG_BT_PAC_SRC_LOC_WRITEABLE)
|
|
|
|
BT_AUDIO_CHRC(BT_UUID_PACS_SRC_LOC,
|
|
|
|
BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE,
|
|
|
|
BT_GATT_PERM_READ_ENCRYPT,
|
|
|
|
src_loc_read, src_loc_write, NULL),
|
|
|
|
#else
|
|
|
|
BT_AUDIO_CHRC(BT_UUID_PACS_SRC_LOC,
|
|
|
|
BT_GATT_CHRC_READ,
|
|
|
|
BT_GATT_PERM_READ_ENCRYPT,
|
|
|
|
src_loc_read, NULL, NULL),
|
|
|
|
#endif /* (CONFIG_BT_PAC_SRC_LOC_WRITEABLE && CONFIG_BT_PAC_SRC_LOC_NOTIFIABLE) */
|
2022-10-18 22:18:55 +02:00
|
|
|
#endif /* CONFIG_BT_PAC_SRC_LOC */
|
2022-07-11 15:28:03 +02:00
|
|
|
#endif /* CONFIG_BT_PAC_SRC */
|
2022-07-25 09:56:23 +02:00
|
|
|
BT_AUDIO_CHRC(BT_UUID_PACS_AVAILABLE_CONTEXT,
|
|
|
|
BT_GATT_CHRC_READ | BT_GATT_CHRC_NOTIFY,
|
|
|
|
BT_GATT_PERM_READ_ENCRYPT,
|
|
|
|
available_contexts_read, NULL, NULL),
|
|
|
|
BT_AUDIO_CCC(available_context_cfg_changed),
|
2023-07-06 14:23:25 +02:00
|
|
|
#if defined(CONFIG_BT_PACS_SUPPORTED_CONTEXT_NOTIFIABLE)
|
2022-07-25 09:56:23 +02:00
|
|
|
BT_AUDIO_CHRC(BT_UUID_PACS_SUPPORTED_CONTEXT,
|
|
|
|
BT_GATT_CHRC_READ | BT_GATT_CHRC_NOTIFY,
|
|
|
|
BT_GATT_PERM_READ_ENCRYPT,
|
|
|
|
supported_context_read, NULL, NULL),
|
|
|
|
BT_AUDIO_CCC(supported_context_cfg_changed)
|
2023-07-06 14:23:25 +02:00
|
|
|
#else
|
|
|
|
BT_AUDIO_CHRC(BT_UUID_PACS_SUPPORTED_CONTEXT,
|
|
|
|
BT_GATT_CHRC_READ,
|
|
|
|
BT_GATT_PERM_READ_ENCRYPT,
|
|
|
|
supported_context_read, NULL, NULL),
|
|
|
|
#endif /* CONFIG_BT_PACS_SUPPORTED_CONTEXT_NOTIFIABLE */
|
2022-01-07 17:23:45 +01:00
|
|
|
);
|
|
|
|
|
2023-07-06 14:23:25 +02:00
|
|
|
#if defined(CONFIG_BT_PAC_SNK_LOC_NOTIFIABLE)
|
2023-03-15 22:36:16 +01:00
|
|
|
static int pac_notify_snk_loc(void)
|
2022-02-08 14:43:43 +01:00
|
|
|
{
|
2023-03-15 22:36:16 +01:00
|
|
|
uint32_t location_le = sys_cpu_to_le32(pacs_snk_location);
|
2022-10-18 23:52:06 +02:00
|
|
|
int err;
|
|
|
|
|
|
|
|
err = bt_gatt_notify_uuid(NULL, BT_UUID_PACS_SNK_LOC, pacs_svc.attrs, &location_le,
|
|
|
|
sizeof(location_le));
|
|
|
|
if (err != 0 && err != -ENOTCONN) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_WRN("PACS notify_loc failed: %d", err);
|
2023-03-15 22:36:16 +01:00
|
|
|
return err;
|
2022-02-08 14:43:43 +01:00
|
|
|
}
|
2023-03-15 22:36:16 +01:00
|
|
|
|
|
|
|
return 0;
|
2022-02-08 14:43:43 +01:00
|
|
|
}
|
2023-07-06 14:23:25 +02:00
|
|
|
#endif /* CONFIG_BT_PAC_SNK_LOC_NOTIFIABLE */
|
2022-02-08 14:43:43 +01:00
|
|
|
|
2023-07-06 14:23:25 +02:00
|
|
|
#if defined(CONFIG_BT_PAC_SRC_LOC_NOTIFIABLE)
|
2023-03-15 22:36:16 +01:00
|
|
|
static int pac_notify_src_loc(void)
|
2022-01-07 17:23:45 +01:00
|
|
|
{
|
2023-03-15 22:36:16 +01:00
|
|
|
uint32_t location_le = sys_cpu_to_le32(pacs_src_location);
|
2022-01-31 11:28:59 +01:00
|
|
|
int err;
|
2022-01-07 17:23:45 +01:00
|
|
|
|
2022-10-18 23:52:06 +02:00
|
|
|
err = bt_gatt_notify_uuid(NULL, BT_UUID_PACS_SRC_LOC, pacs_svc.attrs, &location_le,
|
|
|
|
sizeof(location_le));
|
2022-06-24 10:28:17 +02:00
|
|
|
if (err != 0 && err != -ENOTCONN) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_WRN("PACS notify_loc failed: %d", err);
|
2023-03-15 22:36:16 +01:00
|
|
|
return err;
|
2022-01-31 11:28:59 +01:00
|
|
|
}
|
2023-03-15 22:36:16 +01:00
|
|
|
|
|
|
|
return 0;
|
2022-01-07 17:23:45 +01:00
|
|
|
}
|
2023-07-06 14:23:25 +02:00
|
|
|
#endif /* CONFIG_BT_PAC_SRC_LOC_NOTIFIABLE */
|
2022-01-07 17:23:45 +01:00
|
|
|
|
2023-07-06 14:23:25 +02:00
|
|
|
#if defined(CONFIG_BT_PAC_SNK_NOTIFIABLE)
|
2023-03-15 22:36:16 +01:00
|
|
|
static int pac_notify_snk(void)
|
2022-02-08 14:43:43 +01:00
|
|
|
{
|
2023-03-15 22:36:16 +01:00
|
|
|
int err = 0;
|
2022-02-08 14:43:43 +01:00
|
|
|
|
2023-01-30 15:29:14 +01:00
|
|
|
err = k_sem_take(&read_buf_sem, K_NO_WAIT);
|
|
|
|
if (err != 0) {
|
|
|
|
LOG_DBG("Failed to take read_buf_sem: %d", err);
|
|
|
|
|
2023-03-15 22:36:16 +01:00
|
|
|
return err;
|
2023-01-30 15:29:14 +01:00
|
|
|
}
|
|
|
|
|
2023-03-15 22:36:16 +01:00
|
|
|
get_pac_records(&snk_pacs_list, &read_buf);
|
2022-10-20 10:19:44 +02:00
|
|
|
|
2022-10-20 10:11:27 +02:00
|
|
|
err = bt_gatt_notify_uuid(NULL, BT_UUID_PACS_SNK, pacs_svc.attrs,
|
|
|
|
read_buf.data, read_buf.len);
|
|
|
|
if (err != 0 && err != -ENOTCONN) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_WRN("PACS notify failed: %d", err);
|
2022-02-08 14:43:43 +01:00
|
|
|
}
|
2023-01-30 15:29:14 +01:00
|
|
|
|
|
|
|
k_sem_give(&read_buf_sem);
|
2023-03-15 22:36:16 +01:00
|
|
|
|
|
|
|
if (err == -ENOTCONN) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2022-10-20 10:11:27 +02:00
|
|
|
}
|
2023-07-06 14:23:25 +02:00
|
|
|
#endif /* CONFIG_BT_PAC_SNK_NOTIFIABLE */
|
2022-02-08 14:43:43 +01:00
|
|
|
|
2023-07-06 14:23:25 +02:00
|
|
|
#if defined(CONFIG_BT_PAC_SRC_NOTIFIABLE)
|
2023-03-15 22:36:16 +01:00
|
|
|
static int pac_notify_src(void)
|
2022-10-20 10:11:27 +02:00
|
|
|
{
|
|
|
|
int err = 0;
|
2022-10-20 10:19:44 +02:00
|
|
|
|
2023-01-30 15:29:14 +01:00
|
|
|
err = k_sem_take(&read_buf_sem, K_NO_WAIT);
|
|
|
|
if (err != 0) {
|
|
|
|
LOG_DBG("Failed to take read_buf_sem: %d", err);
|
|
|
|
|
2023-03-15 22:36:16 +01:00
|
|
|
return err;
|
2023-01-30 15:29:14 +01:00
|
|
|
}
|
|
|
|
|
2023-03-15 22:36:16 +01:00
|
|
|
get_pac_records(&src_pacs_list, &read_buf);
|
2022-02-08 14:43:43 +01:00
|
|
|
|
2022-10-20 10:11:27 +02:00
|
|
|
err = bt_gatt_notify_uuid(NULL, BT_UUID_PACS_SRC, pacs_svc.attrs,
|
|
|
|
read_buf.data, read_buf.len);
|
2022-06-24 10:28:17 +02:00
|
|
|
if (err != 0 && err != -ENOTCONN) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_WRN("PACS notify failed: %d", err);
|
2022-02-08 14:43:43 +01:00
|
|
|
}
|
2023-01-30 15:29:14 +01:00
|
|
|
|
|
|
|
k_sem_give(&read_buf_sem);
|
2023-03-15 22:36:16 +01:00
|
|
|
|
|
|
|
if (err == -ENOTCONN) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2022-02-08 14:43:43 +01:00
|
|
|
}
|
2023-07-06 14:23:25 +02:00
|
|
|
#endif /* CONFIG_BT_PAC_SRC_NOTIFIABLE */
|
2022-02-08 14:43:43 +01:00
|
|
|
|
2023-03-15 22:36:16 +01:00
|
|
|
static int pacs_changed(enum bt_audio_dir dir)
|
2022-01-07 17:23:45 +01:00
|
|
|
{
|
2023-03-15 22:36:16 +01:00
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
if (dir == BT_AUDIO_DIR_SINK) {
|
2023-07-06 14:23:25 +02:00
|
|
|
#if defined(CONFIG_BT_PAC_SNK_NOTIFIABLE)
|
2023-03-15 22:36:16 +01:00
|
|
|
err = pac_notify_snk();
|
2023-07-06 14:23:25 +02:00
|
|
|
#endif /* CONFIG_BT_PAC_SNK_NOTIFIABLE */
|
2023-03-15 22:36:16 +01:00
|
|
|
} else {
|
2023-07-06 14:23:25 +02:00
|
|
|
#if defined(CONFIG_BT_PAC_SRC_NOTIFIABLE)
|
2023-03-15 22:36:16 +01:00
|
|
|
err = pac_notify_src();
|
2023-07-06 14:23:25 +02:00
|
|
|
#endif /* CONFIG_BT_PAC_SRC_NOTIFIABLE */
|
2023-03-15 22:36:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
2022-01-07 17:23:45 +01:00
|
|
|
}
|
2022-02-08 14:43:43 +01:00
|
|
|
|
2023-03-15 22:36:16 +01:00
|
|
|
static int available_contexts_notify(void)
|
2022-05-19 16:52:44 +02:00
|
|
|
{
|
2022-10-18 22:58:34 +02:00
|
|
|
struct bt_pacs_context context = {
|
|
|
|
.snk = sys_cpu_to_le16(snk_available_contexts),
|
|
|
|
.src = sys_cpu_to_le16(src_available_contexts),
|
|
|
|
};
|
2022-05-19 16:52:44 +02:00
|
|
|
int err;
|
|
|
|
|
|
|
|
err = bt_gatt_notify_uuid(NULL, BT_UUID_PACS_AVAILABLE_CONTEXT, pacs_svc.attrs,
|
|
|
|
&context, sizeof(context));
|
2022-06-24 10:28:17 +02:00
|
|
|
if (err != 0 && err != -ENOTCONN) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_WRN("Available Audio Contexts notify failed: %d", err);
|
2023-03-15 22:36:16 +01:00
|
|
|
return err;
|
2022-05-19 16:52:44 +02:00
|
|
|
}
|
2023-03-15 22:36:16 +01:00
|
|
|
|
|
|
|
return 0;
|
2022-05-19 16:52:44 +02:00
|
|
|
}
|
|
|
|
|
2023-07-06 14:23:25 +02:00
|
|
|
#if defined(CONFIG_BT_PACS_SUPPORTED_CONTEXT_NOTIFIABLE)
|
2023-03-15 22:36:16 +01:00
|
|
|
static int supported_contexts_notify(void)
|
2022-11-24 09:33:09 +01:00
|
|
|
{
|
|
|
|
struct bt_pacs_context context = {
|
|
|
|
.snk = sys_cpu_to_le16(snk_supported_contexts),
|
|
|
|
.src = sys_cpu_to_le16(src_supported_contexts),
|
|
|
|
};
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = bt_gatt_notify_uuid(NULL, BT_UUID_PACS_SUPPORTED_CONTEXT, pacs_svc.attrs,
|
|
|
|
&context, sizeof(context));
|
|
|
|
if (err != 0 && err != -ENOTCONN) {
|
|
|
|
LOG_WRN("Supported Audio Contexts notify failed: %d", err);
|
2023-03-15 22:36:16 +01:00
|
|
|
|
|
|
|
return err;
|
2022-11-24 09:33:09 +01:00
|
|
|
}
|
2023-03-15 22:36:16 +01:00
|
|
|
return 0;
|
2022-11-24 09:33:09 +01:00
|
|
|
}
|
2023-07-06 14:23:25 +02:00
|
|
|
#endif /* CONFIG_BT_PACS_SUPPORTED_CONTEXT_NOTIFIABLE */
|
2022-11-24 09:33:09 +01:00
|
|
|
|
2022-04-05 11:50:38 +02:00
|
|
|
bool bt_pacs_context_available(enum bt_audio_dir dir, uint16_t context)
|
|
|
|
{
|
2022-10-18 22:58:34 +02:00
|
|
|
if (dir == BT_AUDIO_DIR_SOURCE) {
|
|
|
|
return (context & src_available_contexts) == context;
|
2022-07-15 11:21:25 +02:00
|
|
|
}
|
|
|
|
|
2022-10-18 22:58:34 +02:00
|
|
|
if (dir == BT_AUDIO_DIR_SINK) {
|
|
|
|
return (context & snk_available_contexts) == context;
|
2022-04-05 11:50:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2022-10-18 14:46:12 +02:00
|
|
|
|
2023-03-15 22:36:16 +01:00
|
|
|
static sys_slist_t *pacs_get(enum bt_audio_dir dir)
|
2022-10-18 14:46:12 +02:00
|
|
|
{
|
|
|
|
switch (dir) {
|
2022-10-20 10:11:27 +02:00
|
|
|
#if defined(CONFIG_BT_PAC_SNK)
|
2022-10-18 14:46:12 +02:00
|
|
|
case BT_AUDIO_DIR_SINK:
|
2023-03-15 22:36:16 +01:00
|
|
|
return &snk_pacs_list;
|
2022-10-20 10:11:27 +02:00
|
|
|
#endif /* CONFIG_BT_PAC_SNK */
|
|
|
|
#if defined(CONFIG_BT_PAC_SRC)
|
2022-10-18 14:46:12 +02:00
|
|
|
case BT_AUDIO_DIR_SOURCE:
|
2023-03-15 22:36:16 +01:00
|
|
|
return &src_pacs_list;
|
2022-10-20 10:11:27 +02:00
|
|
|
#endif /* CONFIG_BT_PAC_SRC */
|
|
|
|
default:
|
|
|
|
return NULL;
|
2022-10-18 14:46:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-20 11:25:38 +02:00
|
|
|
void bt_pacs_cap_foreach(enum bt_audio_dir dir, bt_pacs_cap_foreach_func_t func, void *user_data)
|
2022-10-18 14:46:12 +02:00
|
|
|
{
|
2023-03-15 22:36:16 +01:00
|
|
|
sys_slist_t *pac;
|
2022-10-18 14:46:12 +02:00
|
|
|
|
|
|
|
CHECKIF(func == NULL) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_ERR("func is NULL");
|
2022-10-18 14:46:12 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-20 11:25:38 +02:00
|
|
|
pac = pacs_get(dir);
|
|
|
|
if (!pac) {
|
2022-10-18 14:46:12 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-03-15 22:36:16 +01:00
|
|
|
foreach_cap(pac, func, user_data);
|
2022-10-18 14:46:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Register Audio Capability */
|
2022-10-20 11:25:38 +02:00
|
|
|
int bt_pacs_cap_register(enum bt_audio_dir dir, struct bt_pacs_cap *cap)
|
2022-10-18 14:46:12 +02:00
|
|
|
{
|
2023-05-16 14:16:14 +02:00
|
|
|
const struct bt_audio_codec_cap *codec_cap;
|
2023-03-15 22:36:16 +01:00
|
|
|
sys_slist_t *pac;
|
|
|
|
int err;
|
2022-10-18 14:46:12 +02:00
|
|
|
|
2023-05-16 14:16:14 +02:00
|
|
|
if (!cap || !cap->codec_cap) {
|
2022-10-18 14:46:12 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2023-05-16 14:16:14 +02:00
|
|
|
codec_cap = cap->codec_cap;
|
|
|
|
|
2022-10-20 11:25:38 +02:00
|
|
|
pac = pacs_get(dir);
|
|
|
|
if (!pac) {
|
2022-10-18 14:46:12 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2023-05-16 14:16:14 +02:00
|
|
|
LOG_DBG("cap %p dir %s codec_cap id 0x%02x codec_cap cid 0x%04x codec_cap vid 0x%04x", cap,
|
|
|
|
bt_audio_dir_str(dir), codec_cap->id, codec_cap->cid, codec_cap->vid);
|
2022-10-18 14:46:12 +02:00
|
|
|
|
2023-03-15 22:36:16 +01:00
|
|
|
sys_slist_append(pac, &cap->_node);
|
2022-10-18 14:46:12 +02:00
|
|
|
|
2023-03-15 22:36:16 +01:00
|
|
|
err = pacs_changed(dir);
|
|
|
|
if (err) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2022-10-18 14:46:12 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Unregister Audio Capability */
|
2022-10-20 11:25:38 +02:00
|
|
|
int bt_pacs_cap_unregister(enum bt_audio_dir dir, struct bt_pacs_cap *cap)
|
2022-10-18 14:46:12 +02:00
|
|
|
{
|
2023-03-15 22:36:16 +01:00
|
|
|
sys_slist_t *pac;
|
2022-10-18 14:46:12 +02:00
|
|
|
|
|
|
|
if (!cap) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2022-10-20 11:25:38 +02:00
|
|
|
pac = pacs_get(dir);
|
|
|
|
if (!pac) {
|
2022-10-18 14:46:12 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2023-01-05 11:33:47 +01:00
|
|
|
LOG_DBG("cap %p dir %s", cap, bt_audio_dir_str(dir));
|
2022-10-18 14:46:12 +02:00
|
|
|
|
2023-03-15 22:36:16 +01:00
|
|
|
if (!sys_slist_find_and_remove(pac, &cap->_node)) {
|
2022-10-18 14:46:12 +02:00
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
2023-03-15 22:36:16 +01:00
|
|
|
pacs_changed(dir);
|
2022-10-18 14:46:12 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-10-20 11:25:38 +02:00
|
|
|
int bt_pacs_set_location(enum bt_audio_dir dir, enum bt_audio_location location)
|
2022-10-18 14:46:12 +02:00
|
|
|
{
|
2022-10-18 23:52:06 +02:00
|
|
|
switch (dir) {
|
|
|
|
case BT_AUDIO_DIR_SINK:
|
2023-07-06 14:23:25 +02:00
|
|
|
#if defined(CONFIG_BT_PAC_SNK_LOC_NOTIFIABLE)
|
2022-10-18 23:52:06 +02:00
|
|
|
return set_snk_location(location);
|
2023-07-06 14:23:25 +02:00
|
|
|
#endif /* CONFIG_BT_PAC_SNK_LOC_NOTIFIABLE */
|
2022-10-18 23:52:06 +02:00
|
|
|
case BT_AUDIO_DIR_SOURCE:
|
2023-07-06 14:23:25 +02:00
|
|
|
#if defined(CONFIG_BT_PAC_SRC_LOC_NOTIFIABLE)
|
2022-10-18 23:52:06 +02:00
|
|
|
return set_src_location(location);
|
2023-07-06 14:23:25 +02:00
|
|
|
#endif /* CONFIG_BT_PAC_SRC_LOC_NOTIFIABLE */
|
2022-10-18 14:46:12 +02:00
|
|
|
}
|
|
|
|
|
2022-10-18 23:52:06 +02:00
|
|
|
return -EINVAL;
|
2022-10-18 14:46:12 +02:00
|
|
|
}
|
|
|
|
|
2022-10-20 11:25:38 +02:00
|
|
|
int bt_pacs_set_available_contexts(enum bt_audio_dir dir, enum bt_audio_context contexts)
|
2022-10-18 14:46:12 +02:00
|
|
|
{
|
2022-10-18 22:58:34 +02:00
|
|
|
switch (dir) {
|
|
|
|
case BT_AUDIO_DIR_SINK:
|
|
|
|
return set_snk_available_contexts(contexts);
|
|
|
|
case BT_AUDIO_DIR_SOURCE:
|
|
|
|
return set_src_available_contexts(contexts);
|
2022-10-18 14:46:12 +02:00
|
|
|
}
|
|
|
|
|
2022-10-18 22:58:34 +02:00
|
|
|
return -EINVAL;
|
2022-10-18 14:46:12 +02:00
|
|
|
}
|
|
|
|
|
2022-11-24 09:33:09 +01:00
|
|
|
int bt_pacs_set_supported_contexts(enum bt_audio_dir dir, enum bt_audio_context contexts)
|
|
|
|
{
|
|
|
|
switch (dir) {
|
|
|
|
case BT_AUDIO_DIR_SINK:
|
|
|
|
return set_snk_supported_contexts(contexts);
|
|
|
|
case BT_AUDIO_DIR_SOURCE:
|
|
|
|
return set_src_supported_contexts(contexts);
|
|
|
|
}
|
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2022-10-20 11:25:38 +02:00
|
|
|
enum bt_audio_context bt_pacs_get_available_contexts(enum bt_audio_dir dir)
|
2022-10-18 14:46:12 +02:00
|
|
|
{
|
2022-10-18 22:58:34 +02:00
|
|
|
switch (dir) {
|
|
|
|
case BT_AUDIO_DIR_SINK:
|
|
|
|
return snk_available_contexts;
|
|
|
|
case BT_AUDIO_DIR_SOURCE:
|
|
|
|
return src_available_contexts;
|
2022-10-18 14:46:12 +02:00
|
|
|
}
|
|
|
|
|
2022-10-18 22:58:34 +02:00
|
|
|
return BT_AUDIO_CONTEXT_TYPE_PROHIBITED;
|
2022-10-18 14:46:12 +02:00
|
|
|
}
|