|
1 | 1 | /* |
2 | 2 | * Copyright 2023 NXP |
| 3 | + * Copyright (c) 2024 Nordic Semiconductor ASA |
3 | 4 | * |
4 | 5 | * SPDX-License-Identifier: Apache-2.0 |
5 | 6 | */ |
|
22 | 23 | #include <zephyr/kernel.h> |
23 | 24 | #include <zephyr/logging/log.h> |
24 | 25 | #include <zephyr/sys/byteorder.h> |
| 26 | +#include <zephyr/sys/check.h> |
| 27 | +#include <zephyr/sys/util_macro.h> |
25 | 28 | #include <zephyr/types.h> |
26 | 29 |
|
27 | 30 | #include "audio_internal.h" |
28 | 31 |
|
29 | 32 | LOG_MODULE_REGISTER(bt_tmap, CONFIG_BT_TMAP_LOG_LEVEL); |
30 | 33 |
|
31 | 34 | /* 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) |
33 | 38 |
|
34 | 39 | static uint16_t tmap_role; |
35 | 40 | static const struct bt_tmap_cb *cb; |
@@ -155,10 +160,61 @@ static ssize_t read_role(struct bt_conn *conn, |
155 | 160 | static struct bt_gatt_attr svc_attrs[] = { BT_TMAS_SERVICE_DEFINITION }; |
156 | 161 | static struct bt_gatt_service tmas; |
157 | 162 |
|
| 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 | + |
158 | 208 | int bt_tmap_register(enum bt_tmap_role role) |
159 | 209 | { |
160 | 210 | int err; |
161 | 211 |
|
| 212 | + CHECKIF(!valid_tmap_role(role)) { |
| 213 | + LOG_DBG("Invalid role: %d", role); |
| 214 | + |
| 215 | + return -EINVAL; |
| 216 | + } |
| 217 | + |
162 | 218 | tmas = (struct bt_gatt_service)BT_GATT_SERVICE(svc_attrs); |
163 | 219 |
|
164 | 220 | err = bt_gatt_service_register(&tmas); |
|
0 commit comments