Skip to content

Commit 8c38d1b

Browse files
Thalleykartben
authored andcommitted
Bluetooth: TMAP: Add role validation for bt_tmap_register
Add validation that the device supports the role set by bt_tmap_register. Signed-off-by: Emil Gydesen <[email protected]>
1 parent abe2e0d commit 8c38d1b

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

subsys/bluetooth/audio/Kconfig.tmap

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,25 @@
44
#
55
# SPDX-License-Identifier: Apache-2.0
66
#
7+
config BT_TMAP_CG_SUPPORTED
8+
def_bool (BT_CAP_INITIATOR && BT_CAP_COMMANDER && BT_BAP_UNICAST_CLIENT && BT_AUDIO_RX && \
9+
BT_AUDIO_TX && BT_VCP_VOL_CTLR && BT_TBS)
10+
11+
config BT_TMAP_CT_SUPPORTED
12+
def_bool BT_CAP_ACCEPTOR && BT_BAP_UNICAST_SERVER
13+
14+
config BT_TMAP_UMS_SUPPORTED
15+
def_bool (BT_CAP_INITIATOR && BT_CAP_COMMANDER && BT_BAP_UNICAST_CLIENT && BT_AUDIO_TX && \
16+
BT_VCP_VOL_CTLR && BT_MCS)
17+
18+
config BT_TMAP_UMR_SUPPORTED
19+
def_bool BT_CAP_ACCEPTOR && BT_BAP_UNICAST_SERVER && BT_VCP_VOL_REND
20+
21+
config BT_TMAP_BMS_SUPPORTED
22+
def_bool BT_CAP_INITIATOR && BT_BAP_BROADCAST_SOURCE
23+
24+
config BT_TMAP_BMR_SUPPORTED
25+
def_bool BT_CAP_ACCEPTOR && BT_BAP_BROADCAST_SINK && BT_VCP_VOL_REND
726

827
config BT_TMAP
928
bool "Telephony and Media Audio Profile"

subsys/bluetooth/audio/tmap.c

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright 2023 NXP
3+
* Copyright (c) 2024 Nordic Semiconductor ASA
34
*
45
* SPDX-License-Identifier: Apache-2.0
56
*/
@@ -22,14 +23,18 @@
2223
#include <zephyr/kernel.h>
2324
#include <zephyr/logging/log.h>
2425
#include <zephyr/sys/byteorder.h>
26+
#include <zephyr/sys/check.h>
27+
#include <zephyr/sys/util_macro.h>
2528
#include <zephyr/types.h>
2629

2730
#include "audio_internal.h"
2831

2932
LOG_MODULE_REGISTER(bt_tmap, CONFIG_BT_TMAP_LOG_LEVEL);
3033

3134
/* Hex value if all TMAP role bits are set */
32-
#define TMAP_ALL_ROLES 0x3F
35+
#define TMAP_ALL_ROLES \
36+
(BT_TMAP_ROLE_CG | BT_TMAP_ROLE_CT | BT_TMAP_ROLE_UMS | BT_TMAP_ROLE_UMR | \
37+
BT_TMAP_ROLE_BMS | BT_TMAP_ROLE_BMR)
3338

3439
static uint16_t tmap_role;
3540
static const struct bt_tmap_cb *cb;
@@ -155,10 +160,61 @@ static ssize_t read_role(struct bt_conn *conn,
155160
static struct bt_gatt_attr svc_attrs[] = { BT_TMAS_SERVICE_DEFINITION };
156161
static struct bt_gatt_service tmas;
157162

163+
static bool valid_tmap_role(enum bt_tmap_role role)
164+
{
165+
if (role == 0 || (role & TMAP_ALL_ROLES) != role) {
166+
LOG_DBG("Invalid role %d", role);
167+
}
168+
169+
if ((role & BT_TMAP_ROLE_CG) != 0 && !IS_ENABLED(CONFIG_BT_TMAP_CG_SUPPORTED)) {
170+
LOG_DBG("Device does not support the CG role");
171+
172+
return false;
173+
}
174+
175+
if ((role & BT_TMAP_ROLE_CT) != 0 && !IS_ENABLED(CONFIG_BT_TMAP_CT_SUPPORTED)) {
176+
LOG_DBG("Device does not support the CT role");
177+
178+
return false;
179+
}
180+
181+
if ((role & BT_TMAP_ROLE_UMS) != 0 && !IS_ENABLED(CONFIG_BT_TMAP_UMS_SUPPORTED)) {
182+
LOG_DBG("Device does not support the UMS role");
183+
184+
return false;
185+
}
186+
187+
if ((role & BT_TMAP_ROLE_UMR) != 0 && !IS_ENABLED(CONFIG_BT_TMAP_UMR_SUPPORTED)) {
188+
LOG_DBG("Device does not support the UMR role");
189+
190+
return false;
191+
}
192+
193+
if ((role & BT_TMAP_ROLE_BMS) != 0 && !IS_ENABLED(CONFIG_BT_TMAP_BMS_SUPPORTED)) {
194+
LOG_DBG("Device does not support the BMS role");
195+
196+
return false;
197+
}
198+
199+
if ((role & BT_TMAP_ROLE_BMR) != 0 && !IS_ENABLED(CONFIG_BT_TMAP_BMR_SUPPORTED)) {
200+
LOG_DBG("Device does not support the BMR role");
201+
202+
return false;
203+
}
204+
205+
return true;
206+
}
207+
158208
int bt_tmap_register(enum bt_tmap_role role)
159209
{
160210
int err;
161211

212+
CHECKIF(!valid_tmap_role(role)) {
213+
LOG_DBG("Invalid role: %d", role);
214+
215+
return -EINVAL;
216+
}
217+
162218
tmas = (struct bt_gatt_service)BT_GATT_SERVICE(svc_attrs);
163219

164220
err = bt_gatt_service_register(&tmas);

0 commit comments

Comments
 (0)