zephyr/subsys/bluetooth/audio/pacs.c

866 lines
22 KiB
C
Raw Normal View History

/* @file
* @brief Bluetooth PACS
*/
/*
* Copyright (c) 2020 Intel Corporation
* Copyright (c) 2022 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/kernel.h>
#include <zephyr/sys/byteorder.h>
#include <zephyr/sys/check.h>
#include <zephyr/device.h>
#include <zephyr/init.h>
#include <zephyr/bluetooth/bluetooth.h>
#include <zephyr/bluetooth/conn.h>
#include <zephyr/bluetooth/gatt.h>
#include <zephyr/bluetooth/audio/audio.h>
#include <zephyr/bluetooth/audio/capabilities.h>
#include "../host/conn_internal.h"
#define BT_DBG_ENABLED IS_ENABLED(CONFIG_BT_DEBUG_PACS)
#define LOG_MODULE_NAME bt_pacs
#include "common/log.h"
#include "audio_internal.h"
#include "pacs_internal.h"
#include "unicast_server.h"
#define PAC_NOTIFY_TIMEOUT K_MSEC(10)
static sys_slist_t snks;
static sys_slist_t srcs;
#if defined(CONFIG_BT_PAC_SNK)
static uint16_t snk_available_contexts;
static const uint16_t snk_supported_contexts = CONFIG_BT_PACS_SNK_CONTEXT;
#else
static const uint16_t snk_available_contexts = BT_AUDIO_CONTEXT_TYPE_PROHIBITED;
static const uint16_t snk_supported_contexts = BT_AUDIO_CONTEXT_TYPE_PROHIBITED;
#endif /* CONFIG_BT_PAC_SNK */
#if defined(CONFIG_BT_PAC_SRC)
static uint16_t src_available_contexts;
static const uint16_t src_supported_contexts = CONFIG_BT_PACS_SRC_CONTEXT;
#else
static const uint16_t src_available_contexts = BT_AUDIO_CONTEXT_TYPE_PROHIBITED;
static const uint16_t src_supported_contexts = BT_AUDIO_CONTEXT_TYPE_PROHIBITED;
#endif /* CONFIG_BT_PAC_SRC */
NET_BUF_SIMPLE_DEFINE_STATIC(read_buf, CONFIG_BT_L2CAP_TX_MTU);
static ssize_t pac_data_add(struct net_buf_simple *buf, size_t count,
struct bt_codec_data *data)
{
size_t len = 0;
for (size_t i = 0; i < count; i++) {
struct bt_pac_ltv *ltv;
struct bt_data *d = &data[i].data;
const size_t ltv_len = sizeof(*ltv) + d->data_len;
if (net_buf_simple_tailroom(buf) < ltv_len) {
return -ENOMEM;
}
ltv = net_buf_simple_add(buf, sizeof(*ltv));
ltv->len = d->data_len + sizeof(ltv->type);
ltv->type = d->type;
net_buf_simple_add_mem(buf, d->data, d->data_len);
len += ltv_len;
}
return len;
}
struct pac_records_build_data {
struct bt_pacs_read_rsp *rsp;
struct net_buf_simple *buf;
};
static bool build_pac_records(const struct bt_audio_capability *capability, void *user_data)
{
struct pac_records_build_data *data = user_data;
struct bt_codec *codec = capability->codec;
struct net_buf_simple *buf = data->buf;
struct net_buf_simple_state state;
struct bt_pac_ltv_data *cc, *meta;
struct bt_pac *pac;
ssize_t len;
net_buf_simple_save(buf, &state);
if (net_buf_simple_tailroom(buf) < sizeof(*pac)) {
goto fail;
}
pac = net_buf_simple_add(buf, sizeof(*pac));
pac->codec.id = codec->id;
pac->codec.cid = sys_cpu_to_le16(codec->cid);
pac->codec.vid = sys_cpu_to_le16(codec->vid);
if (net_buf_simple_tailroom(buf) < sizeof(*cc)) {
goto fail;
}
cc = net_buf_simple_add(buf, sizeof(*cc));
len = pac_data_add(buf, codec->data_count, codec->data);
if (len < 0 || len > UINT8_MAX) {
goto fail;
}
cc->len = len;
if (net_buf_simple_tailroom(buf) < sizeof(*meta)) {
goto fail;
}
meta = net_buf_simple_add(buf, sizeof(*meta));
len = pac_data_add(buf, codec->meta_count, codec->meta);
if (len < 0 || len > UINT8_MAX) {
goto fail;
}
meta->len = len;
data->rsp->num_pac++;
return true;
fail:
__ASSERT(true, "No space for %p", capability);
net_buf_simple_restore(buf, &state);
return false;
}
static void get_pac_records(struct bt_conn *conn, enum bt_audio_dir dir,
struct net_buf_simple *buf)
{
struct pac_records_build_data data;
/* Reset if buffer before using */
net_buf_simple_reset(buf);
data.rsp = net_buf_simple_add(buf, sizeof(*data.rsp));
data.rsp->num_pac = 0;
data.buf = buf;
bt_audio_foreach_capability(dir, build_pac_records, &data);
}
static void available_context_cfg_changed(const struct bt_gatt_attr *attr, uint16_t value)
{
BT_DBG("attr %p value 0x%04x", attr, value);
}
static ssize_t available_contexts_read(struct bt_conn *conn,
const struct bt_gatt_attr *attr, void *buf,
uint16_t len, uint16_t offset)
{
struct bt_pacs_context context = {
.snk = sys_cpu_to_le16(snk_available_contexts),
.src = sys_cpu_to_le16(src_available_contexts),
};
BT_DBG("conn %p attr %p buf %p len %u offset %u", conn, attr, buf, len,
offset);
return bt_gatt_attr_read(conn, attr, buf, len, offset, &context,
sizeof(context));
}
static void supported_context_cfg_changed(const struct bt_gatt_attr *attr,
uint16_t value)
{
BT_DBG("attr %p value 0x%04x", attr, value);
}
static ssize_t supported_context_read(struct bt_conn *conn,
const struct bt_gatt_attr *attr,
void *buf, uint16_t len, uint16_t offset)
{
struct bt_pacs_context context = {
.snk = sys_cpu_to_le16(snk_supported_contexts),
.src = sys_cpu_to_le16(src_supported_contexts),
};
BT_DBG("conn %p attr %p buf %p len %u offset %u", conn, attr, buf, len,
offset);
return bt_gatt_attr_read(conn, attr, buf, len, offset, &context,
sizeof(context));
}
static void available_contexts_notify(struct k_work *work);
static K_WORK_DELAYABLE_DEFINE(available_contexts_work, available_contexts_notify);
static int set_available_contexts(uint16_t contexts, uint16_t *available,
const uint16_t supported)
{
int err;
if (contexts & ~supported) {
return -ENOTSUP;
}
if (contexts == *available) {
return 0;
}
*available = contexts;
err = k_work_reschedule(&available_contexts_work, PAC_NOTIFY_TIMEOUT);
if (err < 0) {
return err;
}
return 0;
}
#if defined(CONFIG_BT_PAC_SNK_LOC) || defined(CONFIG_BT_PAC_SRC_LOC)
static void pac_notify_loc(struct k_work *work);
#if defined(CONFIG_BT_PAC_SNK_LOC)
static enum bt_audio_location sink_location;
#endif /* CONFIG_BT_PAC_SNK_LOC */
#if defined(CONFIG_BT_PAC_SRC_LOC)
static enum bt_audio_location source_location;
#endif /* CONFIG_BT_PAC_SRC_LOC */
static int publish_location(struct bt_conn *conn,
enum bt_audio_dir dir,
enum bt_audio_location *location)
{
if (0) {
#if defined(CONFIG_BT_PAC_SNK_LOC)
} else if (dir == BT_AUDIO_DIR_SINK) {
*location = sink_location;
#endif /* CONFIG_BT_PAC_SNK_LOC */
#if defined(CONFIG_BT_PAC_SRC_LOC)
} else if (dir == BT_AUDIO_DIR_SOURCE) {
*location = source_location;
#endif /* CONFIG_BT_PAC_SRC_LOC */
} else {
BT_ERR("Invalid endpoint dir: %u", dir);
return -EINVAL;
}
return 0;
}
static int get_pac_loc(struct bt_conn *conn, enum bt_audio_dir dir,
enum bt_audio_location *location)
{
int err;
err = publish_location(conn, dir, location);
if (err != 0 || *location == 0) {
BT_DBG("err (%d) or invalid location value (%u)",
err, *location);
return -ENODATA;
}
return 0;
}
#endif /* CONFIG_BT_PAC_SNK_LOC || CONFIG_BT_PAC_SRC_LOC */
static void pac_notify(struct k_work *work);
#if defined(CONFIG_BT_PAC_SNK)
static K_WORK_DELAYABLE_DEFINE(snks_work, pac_notify);
static ssize_t snk_read(struct bt_conn *conn, const struct bt_gatt_attr *attr,
void *buf, uint16_t len, uint16_t offset)
{
BT_DBG("conn %p attr %p buf %p len %u offset %u", conn, attr, buf, len,
offset);
get_pac_records(conn, BT_AUDIO_DIR_SINK, &read_buf);
return bt_gatt_attr_read(conn, attr, buf, len, offset, read_buf.data,
read_buf.len);
}
static void snk_cfg_changed(const struct bt_gatt_attr *attr, uint16_t value)
{
BT_DBG("attr %p value 0x%04x", attr, value);
}
static inline int set_snk_available_contexts(uint16_t contexts)
{
return set_available_contexts(contexts, &snk_available_contexts,
snk_supported_contexts);
}
#else
static inline int set_snk_available_contexts(uint16_t contexts)
{
return -ENOTSUP;
}
#if defined(CONFIG_BT_PAC_SNK_LOC)
static K_WORK_DELAYABLE_DEFINE(snks_loc_work, pac_notify_loc);
static ssize_t snk_loc_read(struct bt_conn *conn,
const struct bt_gatt_attr *attr, void *buf,
uint16_t len, uint16_t offset)
{
int err;
enum bt_audio_location location;
uint32_t location_32;
uint32_t location_32_le;
BT_DBG("conn %p attr %p buf %p len %u offset %u", conn, attr, buf, len,
offset);
err = get_pac_loc(NULL, BT_AUDIO_DIR_SINK, &location);
if (err != 0) {
BT_DBG("get_pac_loc returned %d", err);
return BT_GATT_ERR(BT_ATT_ERR_UNLIKELY);
}
location_32 = (uint32_t)location;
if (location_32 > BT_AUDIO_LOCATION_MASK || location_32 == 0) {
BT_ERR("Invalid location value: 0x%08X", location_32);
return BT_GATT_ERR(BT_ATT_ERR_UNLIKELY);
}
location_32_le = sys_cpu_to_le32(location_32);
return bt_gatt_attr_read(conn, attr, buf, len, offset,
&location_32_le, sizeof(location_32_le));
}
#if defined(CONFIG_BT_PAC_SNK_LOC) || defined(CONFIG_BT_PAC_SRC_LOC)
#if defined(CONFIG_BT_PAC_SNK_LOC_WRITEABLE) || defined(CONFIG_BT_PAC_SRC_LOC_WRITEABLE)
static int write_location(struct bt_conn *conn, enum bt_audio_dir dir,
enum bt_audio_location location)
{
return bt_audio_capability_set_location(dir, location);
}
#endif /* CONFIG_BT_PAC_SNK_LOC_WRITEABLE || CONFIG_BT_PAC_SRC_LOC_WRITEABLE */
#endif /* CONFIG_BT_PAC_SNK_LOC || CONFIG_BT_PAC_SRC_LOC */
#if defined(CONFIG_BT_PAC_SNK_LOC_WRITEABLE)
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)
{
int err;
enum bt_audio_location location;
if (offset) {
return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
}
if (len != sizeof(location)) {
return BT_GATT_ERR(BT_ATT_ERR_WRITE_REQ_REJECTED);
}
location = (enum bt_audio_location)sys_get_le32(data);
if (location > BT_AUDIO_LOCATION_MASK || location == 0) {
BT_DBG("Invalid location value: 0x%08X", location);
return BT_GATT_ERR(BT_ATT_ERR_WRITE_REQ_REJECTED);
}
err = write_location(conn, BT_AUDIO_DIR_SINK, location);
if (err != 0) {
BT_DBG("write_location returned %d", err);
return BT_GATT_ERR(BT_ATT_ERR_WRITE_REQ_REJECTED);
}
return len;
}
#endif /* CONFIG_BT_PAC_SNK_LOC_WRITEABLE */
static void snk_loc_cfg_changed(const struct bt_gatt_attr *attr, uint16_t value)
{
BT_DBG("attr %p value 0x%04x", attr, value);
}
#endif /* CONFIG_BT_PAC_SNK_LOC */
#endif /* CONFIG_BT_PAC_SNK */
#if defined(CONFIG_BT_PAC_SRC)
static K_WORK_DELAYABLE_DEFINE(srcs_work, pac_notify);
static ssize_t src_read(struct bt_conn *conn, const struct bt_gatt_attr *attr,
void *buf, uint16_t len, uint16_t offset)
{
BT_DBG("conn %p attr %p buf %p len %u offset %u", conn, attr, buf, len,
offset);
get_pac_records(conn, BT_AUDIO_DIR_SOURCE, &read_buf);
return bt_gatt_attr_read(conn, attr, buf, len, offset, read_buf.data,
read_buf.len);
}
static void src_cfg_changed(const struct bt_gatt_attr *attr, uint16_t value)
{
BT_DBG("attr %p value 0x%04x", attr, value);
}
static inline int set_src_available_contexts(uint16_t contexts)
{
return set_available_contexts(contexts, &src_available_contexts,
src_supported_contexts);
}
#else
static inline int set_src_available_contexts(uint16_t contexts)
{
return -ENOTSUP;
}
#if defined(CONFIG_BT_PAC_SRC_LOC)
static K_WORK_DELAYABLE_DEFINE(srcs_loc_work, pac_notify_loc);
static ssize_t src_loc_read(struct bt_conn *conn,
const struct bt_gatt_attr *attr, void *buf,
uint16_t len, uint16_t offset)
{
int err;
enum bt_audio_location location;
uint32_t location_32;
uint32_t location_32_le;
BT_DBG("conn %p attr %p buf %p len %u offset %u", conn, attr, buf, len,
offset);
err = get_pac_loc(NULL, BT_AUDIO_DIR_SOURCE, &location);
if (err != 0) {
BT_DBG("get_pac_loc returned %d", err);
return BT_GATT_ERR(BT_ATT_ERR_UNLIKELY);
}
location_32 = (uint32_t)location;
if (location_32 > BT_AUDIO_LOCATION_MASK || location_32 == 0) {
BT_ERR("Invalid location value: 0x%08X", location_32);
return BT_GATT_ERR(BT_ATT_ERR_UNLIKELY);
}
location_32_le = sys_cpu_to_le32(location_32);
return bt_gatt_attr_read(conn, attr, buf, len, offset,
&location_32_le, sizeof(location_32_le));
}
#if defined(CONFIG_BT_PAC_SRC_LOC_WRITEABLE)
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)
{
int err;
uint32_t location;
if (offset) {
return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
}
if (len != sizeof(location)) {
return BT_GATT_ERR(BT_ATT_ERR_WRITE_REQ_REJECTED);
}
location = (enum bt_audio_location)sys_get_le32(data);
if (location > BT_AUDIO_LOCATION_MASK || location == 0) {
BT_DBG("Invalid location value: 0x%08X", location);
return BT_GATT_ERR(BT_ATT_ERR_WRITE_REQ_REJECTED);
}
err = write_location(conn, BT_AUDIO_DIR_SOURCE, location);
if (err != 0) {
BT_DBG("write_location returned %d", err);
return BT_GATT_ERR(BT_ATT_ERR_WRITE_REQ_REJECTED);
}
return len;
}
#endif /* CONFIG_BT_PAC_SRC_LOC_WRITEABLE */
static void src_loc_cfg_changed(const struct bt_gatt_attr *attr, uint16_t value)
{
BT_DBG("attr %p value 0x%04x", attr, value);
}
#endif /* CONFIG_BT_PAC_SRC_LOC */
#endif /* CONFIG_BT_PAC_SRC */
BT_GATT_SERVICE_DEFINE(pacs_svc,
BT_GATT_PRIMARY_SERVICE(BT_UUID_PACS),
#if defined(CONFIG_BT_PAC_SNK)
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),
#if defined(CONFIG_BT_PAC_SNK_LOC)
#if 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_CHRC_NOTIFY,
BT_GATT_PERM_READ_ENCRYPT | BT_GATT_PERM_WRITE_ENCRYPT,
snk_loc_read, snk_loc_write, NULL),
#else
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),
#endif /* CONFIG_BT_PAC_SNK_LOC_WRITEABLE */
BT_AUDIO_CCC(snk_loc_cfg_changed),
#endif /* CONFIG_BT_PAC_SNK_LOC */
#endif /* CONFIG_BT_PAC_SNK */
#if defined(CONFIG_BT_PAC_SRC)
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),
#if defined(CONFIG_BT_PAC_SRC_LOC)
#if 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_CHRC_NOTIFY,
BT_GATT_PERM_READ_ENCRYPT | BT_GATT_PERM_WRITE_ENCRYPT,
src_loc_read, src_loc_write, NULL),
#else
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),
#endif /* CONFIG_BT_PAC_SRC_LOC_WRITEABLE */
BT_AUDIO_CCC(src_loc_cfg_changed),
#endif /* CONFIG_BT_PAC_SRC_LOC */
#endif /* CONFIG_BT_PAC_SRC */
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),
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)
);
static struct k_work_delayable *bt_pacs_get_work(enum bt_audio_dir dir)
{
switch (dir) {
#if defined(CONFIG_BT_PAC_SNK)
case BT_AUDIO_DIR_SINK:
return &snks_work;
#endif /* CONFIG_BT_PAC_SNK */
#if defined(CONFIG_BT_PAC_SRC)
case BT_AUDIO_DIR_SOURCE:
return &srcs_work;
#endif /* CONFIG_BT_PAC_SRC */
default:
return NULL;
}
}
#if defined(CONFIG_BT_PAC_SNK_LOC) || defined(CONFIG_BT_PAC_SRC_LOC)
static struct k_work_delayable *bt_pacs_get_loc_work(enum bt_audio_dir dir)
{
switch (dir) {
#if defined(CONFIG_BT_PAC_SNK)
case BT_AUDIO_DIR_SINK:
return &snks_loc_work;
#endif /* CONFIG_BT_PAC_SNK */
#if defined(CONFIG_BT_PAC_SRC)
case BT_AUDIO_DIR_SOURCE:
return &srcs_loc_work;
#endif /* CONFIG_BT_PAC_SRC */
default:
return NULL;
}
}
static void pac_notify_loc(struct k_work *work)
{
uint32_t location, location_le;
enum bt_audio_dir dir;
int err;
#if defined(CONFIG_BT_PAC_SNK)
if (work == &snks_loc_work.work) {
dir = BT_AUDIO_DIR_SINK;
}
#endif /* CONFIG_BT_PAC_SNK */
#if defined(CONFIG_BT_PAC_SRC)
if (work == &srcs_loc_work.work) {
dir = BT_AUDIO_DIR_SOURCE;
}
#endif /* CONFIG_BT_PAC_SRC */
/* TODO: We can skip this if we are not connected to any devices */
err = get_pac_loc(NULL, dir, &location);
if (err != 0) {
BT_DBG("get_pac_loc returned %d, won't notify", err);
return;
}
location_le = sys_cpu_to_le32(location);
if (dir == BT_AUDIO_DIR_SINK) {
err = bt_gatt_notify_uuid(NULL, BT_UUID_PACS_SNK_LOC,
pacs_svc.attrs,
&location_le,
sizeof(location_le));
} else {
err = bt_gatt_notify_uuid(NULL, BT_UUID_PACS_SRC_LOC,
pacs_svc.attrs,
&location_le,
sizeof(location_le));
}
if (err != 0 && err != -ENOTCONN) {
BT_WARN("PACS notify_loc failed: %d", err);
}
}
#endif /* CONFIG_BT_PAC_SNK_LOC || CONFIG_BT_PAC_SRC_LOC */
static void pac_notify(struct k_work *work)
{
int err = 0;
#if defined(CONFIG_BT_PAC_SNK)
if (work == &snks_work.work) {
get_pac_records(NULL, BT_AUDIO_DIR_SINK, &read_buf);
err = bt_gatt_notify_uuid(NULL, BT_UUID_PACS_SNK,
pacs_svc.attrs, read_buf.data,
read_buf.len);
}
#endif /* CONFIG_BT_PAC_SNK */
#if defined(CONFIG_BT_PAC_SRC)
if (work == &srcs_work.work) {
get_pac_records(NULL, BT_AUDIO_DIR_SOURCE, &read_buf);
err = bt_gatt_notify_uuid(NULL, BT_UUID_PACS_SRC,
pacs_svc.attrs, read_buf.data,
read_buf.len);
}
#endif /* CONFIG_BT_PAC_SRC */
if (err != 0 && err != -ENOTCONN) {
BT_WARN("PACS notify failed: %d", err);
}
}
static void bt_pacs_capabilities_changed(enum bt_audio_dir dir)
{
struct k_work_delayable *work;
work = bt_pacs_get_work(dir);
if (!work) {
return;
}
k_work_reschedule(work, PAC_NOTIFY_TIMEOUT);
}
#if defined(CONFIG_BT_PAC_SNK_LOC) || defined(CONFIG_BT_PAC_SRC_LOC)
/******* PUBLIC API *******/
static int bt_audio_pacs_location_changed(enum bt_audio_dir dir)
{
struct k_work_delayable *work;
work = bt_pacs_get_loc_work(dir);
if (!work) {
return -EINVAL;
}
k_work_reschedule(work, PAC_NOTIFY_TIMEOUT);
return 0;
}
#endif /* CONFIG_BT_PAC_SNK_LOC || CONFIG_BT_PAC_SRC_LOC */
static void available_contexts_notify(struct k_work *work)
{
struct bt_pacs_context context = {
.snk = sys_cpu_to_le16(snk_available_contexts),
.src = sys_cpu_to_le16(src_available_contexts),
};
int err;
err = bt_gatt_notify_uuid(NULL, BT_UUID_PACS_AVAILABLE_CONTEXT, pacs_svc.attrs,
&context, sizeof(context));
if (err != 0 && err != -ENOTCONN) {
BT_WARN("Available Audio Contexts notify failed: %d", err);
}
}
bool bt_pacs_context_available(enum bt_audio_dir dir, uint16_t context)
{
if (dir == BT_AUDIO_DIR_SOURCE) {
return (context & src_available_contexts) == context;
}
if (dir == BT_AUDIO_DIR_SINK) {
return (context & snk_available_contexts) == context;
}
return false;
}
static sys_slist_t *bt_audio_capability_get(enum bt_audio_dir dir)
{
switch (dir) {
case BT_AUDIO_DIR_SINK:
return &snks;
case BT_AUDIO_DIR_SOURCE:
return &srcs;
}
return NULL;
}
void bt_audio_foreach_capability(enum bt_audio_dir dir, bt_audio_foreach_capability_func_t func,
void *user_data)
{
struct bt_audio_capability *cap;
sys_slist_t *lst;
CHECKIF(func == NULL) {
BT_ERR("func is NULL");
return;
}
lst = bt_audio_capability_get(dir);
if (!lst) {
return;
}
SYS_SLIST_FOR_EACH_CONTAINER(lst, cap, _node) {
if (!func(cap, user_data)) {
break;
}
}
}
/* Register Audio Capability */
int bt_audio_capability_register(enum bt_audio_dir dir, struct bt_audio_capability *cap)
{
sys_slist_t *lst;
if (!cap || !cap->codec) {
return -EINVAL;
}
lst = bt_audio_capability_get(dir);
if (!lst) {
return -EINVAL;
}
BT_DBG("cap %p dir 0x%02x codec 0x%02x codec cid 0x%04x "
"codec vid 0x%04x", cap, dir, cap->codec->id,
cap->codec->cid, cap->codec->vid);
sys_slist_append(lst, &cap->_node);
#if defined(CONFIG_BT_PACS)
bt_pacs_capabilities_changed(dir);
#endif /* CONFIG_BT_PACS */
return 0;
}
/* Unregister Audio Capability */
int bt_audio_capability_unregister(enum bt_audio_dir dir, struct bt_audio_capability *cap)
{
sys_slist_t *lst;
if (!cap) {
return -EINVAL;
}
lst = bt_audio_capability_get(dir);
if (!lst) {
return -EINVAL;
}
BT_DBG("cap %p dir 0x%02x", cap, dir);
if (!sys_slist_find_and_remove(lst, &cap->_node)) {
return -ENOENT;
}
#if defined(CONFIG_BT_PACS)
bt_pacs_capabilities_changed(dir);
#endif /* CONFIG_BT_PACS */
return 0;
}
#if defined(CONFIG_BT_PAC_SNK_LOC) || defined(CONFIG_BT_PAC_SRC_LOC)
int bt_audio_capability_set_location(enum bt_audio_dir dir,
enum bt_audio_location location)
{
if (0) {
#if defined(CONFIG_BT_PAC_SNK_LOC)
} else if (dir == BT_AUDIO_DIR_SINK) {
sink_location = location;
#endif /* CONFIG_BT_PAC_SNK_LOC */
#if defined(CONFIG_BT_PAC_SRC_LOC)
} else if (dir == BT_AUDIO_DIR_SOURCE) {
source_location = location;
#endif /* CONFIG_BT_PAC_SRC_LOC */
} else {
BT_ERR("Invalid endpoint dir: %u", dir);
return -EINVAL;
}
if (IS_ENABLED(CONFIG_BT_PAC_SNK_LOC) ||
IS_ENABLED(CONFIG_BT_PAC_SRC_LOC)) {
int err;
err = bt_audio_pacs_location_changed(dir);
if (err) {
BT_DBG("Location for dir %d wasn't notified: %d",
dir, err);
return err;
}
}
return 0;
}
#endif /* CONFIG_BT_PAC_SNK_LOC || CONFIG_BT_PAC_SRC_LOC */
int bt_audio_capability_set_available_contexts(enum bt_audio_dir dir,
enum bt_audio_context contexts)
{
switch (dir) {
case BT_AUDIO_DIR_SINK:
return set_snk_available_contexts(contexts);
case BT_AUDIO_DIR_SOURCE:
return set_src_available_contexts(contexts);
}
return -EINVAL;
}
enum bt_audio_context bt_audio_capability_get_available_contexts(enum bt_audio_dir dir)
{
switch (dir) {
case BT_AUDIO_DIR_SINK:
return snk_available_contexts;
case BT_AUDIO_DIR_SOURCE:
return src_available_contexts;
}
return BT_AUDIO_CONTEXT_TYPE_PROHIBITED;
}