Bluetooth: Audio: Rename struct bt_micp and cleanup

Rename struct bt_micp to bt_mic_ctlr as it is only
used ofr microphone controller now.

This also allow cleanup of the bt_micp struct, thus
removing the common internal micp_internal.h header file
and reducing the size of the implementation by not
using a union of the structs.

Signed-off-by: Emil Gydesen <emil.gydesen@nordicsemi.no>
This commit is contained in:
Emil Gydesen 2022-06-21 19:49:13 +02:00 committed by Carles Cufí
commit 28f6e37ba8
6 changed files with 299 additions and 341 deletions

View file

@ -41,10 +41,10 @@ extern "C" {
#define BT_MICP_MUTE_MUTED 0x01 #define BT_MICP_MUTE_MUTED 0x01
#define BT_MICP_MUTE_DISABLED 0x02 #define BT_MICP_MUTE_DISABLED 0x02
/** @brief Opaque Microphone Input Control Profile instance. */ /** @brief Opaque Microphone Controller instance. */
struct bt_micp; struct bt_micp_mic_ctlr;
/** @brief Register parameters structure for Microphone Input Control Profile instance */ /** @brief Register parameters structure for Microphone Input Control Service */
struct bt_micp_mic_dev_register_param { struct bt_micp_mic_dev_register_param {
#if defined(CONFIG_BT_MICP_MIC_DEV_AICS) #if defined(CONFIG_BT_MICP_MIC_DEV_AICS)
/** Register parameter structure for Audio Input Control Services */ /** Register parameter structure for Audio Input Control Services */
@ -59,10 +59,11 @@ struct bt_micp_mic_dev_register_param {
* @brief Microphone Input Control Profile included services * @brief Microphone Input Control Profile included services
* *
* Used for to represent the Microphone Input Control Profile included service * Used for to represent the Microphone Input Control Profile included service
* instances, for either a client or a server instance. The instance pointers * instances, for either a Microphone Controller or a Microphone Device.
* either represent local server instances, or remote service instances. * The instance pointers either represent local service instances,
* or remote service instances.
*/ */
struct bt_micp_included { struct bt_micp_included {
/** Number of Audio Input Control Service instances */ /** Number of Audio Input Control Service instances */
uint8_t aics_cnt; uint8_t aics_cnt;
/** Array of pointers to Audio Input Control Service instances */ /** Array of pointers to Audio Input Control Service instances */
@ -70,11 +71,10 @@ struct bt_micp_included {
}; };
/** /**
* @brief Initialize the Microphone Input Control Profile * @brief Initialize the Microphone Input Control Profile Microphone Device
* *
* This will enable the Microphone Input Control Profile instance and make it * This will enable the Microphone Input Control Service instance and make it
* discoverable by clients. * discoverable by Microphone Controllers.
* This can only be done as the server.
* *
* @param param Pointer to an initialization structure. * @param param Pointer to an initialization structure.
* *
@ -83,13 +83,12 @@ struct bt_micp_included {
int bt_micp_mic_dev_register(struct bt_micp_mic_dev_register_param *param); int bt_micp_mic_dev_register(struct bt_micp_mic_dev_register_param *param);
/** /**
* @brief Get Microphone Input Control Profile included services * @brief Get Microphone Device included services
* *
* Returns a pointer to a struct that contains information about the * Returns a pointer to a struct that contains information about the
* Microphone Input Control Profile included services instances, such as * Microphone Device included Audio Input Control Service instances.
* pointers to the Audio Input Control Service instances.
* *
* Requires that @kconfig{CONFIG_BT_MICP_MIC_DEV_AICS} * Requires that @kconfig{CONFIG_BT_MICP_MIC_DEV_AICS} is enabled.
* *
* @param included Pointer to store the result in. * @param included Pointer to store the result in.
* *
@ -99,51 +98,42 @@ int bt_micp_mic_dev_included_get(struct bt_micp_included *included);
struct bt_micp_mic_dev_cb { struct bt_micp_mic_dev_cb {
/** /**
* @brief Callback function for Microphone Input Control Profile mute. * @brief Callback function for Microphone Device mute.
* *
* Called when the value is read with bt_micp_mic_dev_mute_get(), * Called when the value is read with bt_micp_mic_dev_mute_get(),
* or if the value is changed by either the server or client. * or if the value is changed by either the Microphone Device or a
* Microphone Controller.
* *
* @param micp Microphone Input Control Profile instance pointer. * @param mute The mute setting of the Microphone Input Control Service.
* @param mute The mute setting of the Microphone Input Control Profile instance.
*/ */
void (*mute)(uint8_t mute); void (*mute)(uint8_t mute);
}; };
/** /**
* @brief Unmute the server. * @brief Unmute the Microphone Device.
*
* @param micp Microphone Input Control Profile instance pointer.
* *
* @return 0 on success, GATT error value on fail. * @return 0 on success, GATT error value on fail.
*/ */
int bt_micp_mic_dev_unmute(void); int bt_micp_mic_dev_unmute(void);
/** /**
* @brief Mute the server. * @brief Mute the Microphone Device.
*
* @param micp Microphone Input Control Profile instance pointer.
* *
* @return 0 on success, GATT error value on fail. * @return 0 on success, GATT error value on fail.
*/ */
int bt_micp_mic_dev_mute(void); int bt_micp_mic_dev_mute(void);
/** /**
* @brief Disable the mute functionality. * @brief Disable the mute functionality on the Microphone Device.
* *
* Can be reenabled by called @ref bt_micp_mic_dev_mute or @ref bt_micp_mic_dev_unmute. * Can be reenabled by called @ref bt_micp_mic_dev_mute or @ref bt_micp_mic_dev_unmute.
* This can only be done as the server.
*
* @param micp Microphone Input Control Profile instance pointer.
* *
* @return 0 on success, GATT error value on fail. * @return 0 on success, GATT error value on fail.
*/ */
int bt_micp_mic_dev_disable(void); int bt_micp_mic_dev_disable(void);
/** /**
* @brief Read the mute state of a Microphone Input Control Profile instance. * @brief Read the mute state on the Microphone Device.
*
* @param micp Microphone Input Control Profile instance pointer.
* *
* @return 0 on success, GATT error value on fail. * @return 0 on success, GATT error value on fail.
*/ */
@ -154,42 +144,42 @@ struct bt_micp_mic_ctlr_cb {
* @brief Callback function for Microphone Input Control Profile mute. * @brief Callback function for Microphone Input Control Profile mute.
* *
* Called when the value is read, * Called when the value is read,
* or if the value is changed by either the server or client. * or if the value is changed by either the Microphone Device or a
* Microphone Controller.
* *
* @param micp Microphone Input Control Profile instance pointer. * @param mic_ctlr Microphone Controller instance pointer.
* @param err Error value. 0 on success, GATT error or errno on fail. * @param err Error value. 0 on success, GATT error or errno on fail.
* For notifications, this will always be 0. * For notifications, this will always be 0.
* @param mute The mute setting of the Microphone Input Control Profile instance. * @param mute The mute setting of the Microphone Input Control Service.
*/ */
void (*mute)(struct bt_micp *micp, int err, uint8_t mute); void (*mute)(struct bt_micp_mic_ctlr *mic_ctlr, int err, uint8_t mute);
/** /**
* @brief Callback function for bt_micp_mic_ctlr_discover(). * @brief Callback function for bt_micp_mic_ctlr_discover().
* *
* This callback is only used for the client. * @param mic_ctlr Microphone Controller instance pointer.
*
* @param micp Microphone Input Control Profile instance pointer.
* @param err Error value. 0 on success, GATT error or errno on fail. * @param err Error value. 0 on success, GATT error or errno on fail.
* @param aics_count Number of Audio Input Control Service instances on * @param aics_count Number of Audio Input Control Service instances on
* peer device. * peer device.
*/ */
void (*discover)(struct bt_micp *micp, int err, uint8_t aics_count); void (*discover)(struct bt_micp_mic_ctlr *mic_ctlr, int err,
uint8_t aics_count);
/** /**
* @brief Callback function for Microphone Input Control Profile mute/unmute. * @brief Callback function for Microphone Input Control Profile mute/unmute.
* *
* @param micp Microphone Input Control Profile instance pointer. * @param mic_ctlr Microphone Controller instance pointer.
* @param err Error value. 0 on success, GATT error or errno on fail. * @param err Error value. 0 on success, GATT error or errno on fail.
*/ */
void (*mute_written)(struct bt_micp *micp, int err); void (*mute_written)(struct bt_micp_mic_ctlr *mic_ctlr, int err);
/** /**
* @brief Callback function for Microphone Input Control Profile mute/unmute. * @brief Callback function for Microphone Input Control Profile mute/unmute.
* *
* @param micp Microphone Input Control Profile instance pointer. * @param mic_ctlr Microphone Controller instance pointer.
* @param err Error value. 0 on success, GATT error or errno on fail. * @param err Error value. 0 on success, GATT error or errno on fail.
*/ */
void (*unmute_written)(struct bt_micp *micp, int err); void (*unmute_written)(struct bt_micp_mic_ctlr *mic_ctlr, int err);
#if defined(CONFIG_BT_MICP_MIC_CTLR_AICS) #if defined(CONFIG_BT_MICP_MIC_CTLR_AICS)
/** Audio Input Control Service client callback */ /** Audio Input Control Service client callback */
@ -206,74 +196,72 @@ struct bt_micp_mic_ctlr_cb {
* *
* Requires that @kconfig{CONFIG_BT_MICP_MIC_CTLR_AICS} is enabled. * Requires that @kconfig{CONFIG_BT_MICP_MIC_CTLR_AICS} is enabled.
* *
* @param micp Microphone Input Control Profile instance pointer. * @param mic_ctlr Microphone Controller instance pointer.
* @param[out] included Pointer to store the result in. * @param[out] included Pointer to store the result in.
* *
* @return 0 if success, errno on failure. * @return 0 if success, errno on failure.
*/ */
int bt_micp_mic_ctlr_included_get(struct bt_micp *micp, int bt_micp_mic_ctlr_included_get(struct bt_micp_mic_ctlr *mic_ctlr,
struct bt_micp_included *included); struct bt_micp_included *included);
/** /**
* @brief Get the connection pointer of a client instance * @brief Get the connection pointer of a Microphone Controller instance
* *
* Get the Bluetooth connection pointer of a Microphone Input Control Profile * Get the Bluetooth connection pointer of a Microphone Controller instance.
* client instance.
* *
* @param micp Microphone Input Control Profile client instance pointer. * @param mic_ctlr Microphone Controller instance pointer.
* @param conn Connection pointer. * @param conn Connection pointer.
* *
* @return 0 if success, errno on failure. * @return 0 if success, errno on failure.
*/ */
int bt_micp_mic_ctlr_conn_get(const struct bt_micp *micp, int bt_micp_mic_ctlr_conn_get(const struct bt_micp_mic_ctlr *mic_ctlr,
struct bt_conn **conn); struct bt_conn **conn);
/** /**
* @brief Discover Microphone Input Control Profile instance * @brief Discover Microphone Input Control Service
* *
* This will start a GATT discovery and setup handles and subscriptions. * This will start a GATT discovery and setup handles and subscriptions.
* This shall be called once before any other actions can be executed for the * This shall be called once before any other actions can be executed for the
* peer device, and the @ref bt_micp_mic_ctlr_cb.discover callback will notify * peer device, and the @ref bt_micp_mic_ctlr_cb.discover callback will notify
* when it is possible to start remote operations. * when it is possible to start remote operations.
* * *
* This shall only be done as the client.
*
* @param conn The connection to initialize the profile for. * @param conn The connection to initialize the profile for.
* @param[out] micp Valid remote instance object on success. * @param[out] mic_ctlr Valid remote instance object on success.
* *
* @return 0 on success, GATT error value on fail. * @return 0 on success, GATT error value on fail.
*/ */
int bt_micp_mic_ctlr_discover(struct bt_conn *conn, struct bt_micp **micp); int bt_micp_mic_ctlr_discover(struct bt_conn *conn,
struct bt_micp_mic_ctlr **mic_ctlr);
/** /**
* @brief Unmute the server. * @brief Unmute a remote Microphone Device.
* *
* @param micp Microphone Input Control Profile instance pointer. * @param mic_ctlr Microphone Controller instance pointer.
* *
* @return 0 on success, GATT error value on fail. * @return 0 on success, GATT error value on fail.
*/ */
int bt_micp_mic_ctlr_unmute(struct bt_micp *micp); int bt_micp_mic_ctlr_unmute(struct bt_micp_mic_ctlr *mic_ctlr);
/** /**
* @brief Mute the server. * @brief Mute a remote Microphone Device.
* *
* @param micp Microphone Input Control Profile instance pointer. * @param mic_ctlr Microphone Controller instance pointer.
* *
* @return 0 on success, GATT error value on fail. * @return 0 on success, GATT error value on fail.
*/ */
int bt_micp_mic_ctlr_mute(struct bt_micp *micp); int bt_micp_mic_ctlr_mute(struct bt_micp_mic_ctlr *mic_ctlr);
/** /**
* @brief Read the mute state of a Microphone Input Control Profile instance. * @brief Read the mute state of a remote Microphone Device.
* *
* @param micp Microphone Input Control Profile instance pointer. * @param mic_ctlr Microphone Controller instance pointer.
* *
* @return 0 on success, GATT error value on fail. * @return 0 on success, GATT error value on fail.
*/ */
int bt_micp_mic_ctlr_mute_get(struct bt_micp *micp); int bt_micp_mic_ctlr_mute_get(struct bt_micp_mic_ctlr *mic_ctlr);
/** /**
* @brief Registers the callbacks used by Microphone Input Control Profile client. * @brief Registers the callbacks used by Microphone Controller.
* *
* This can only be done as the client. * This can only be done as the client.
* *

View file

@ -1,54 +0,0 @@
/*
* Copyright (c) 2020-2022 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef ZEPHYR_INCLUDE_BLUETOOTH_AUDIO_MICP_INTERNAL_
#define ZEPHYR_INCLUDE_BLUETOOTH_AUDIO_MICP_INTERNAL_
#include <zephyr/types.h>
#include <zephyr/bluetooth/gatt.h>
#if defined(CONFIG_BT_MICP_MIC_DEV)
struct bt_micp_server {
uint8_t mute;
struct bt_micp_mic_dev_cb *cb;
struct bt_gatt_service *service_p;
struct bt_aics *aics_insts[CONFIG_BT_MICP_MIC_DEV_AICS_INSTANCE_COUNT];
};
#endif /* CONFIG_BT_MICP_MIC_DEV */
#if defined(CONFIG_BT_MICP_MIC_CTLR)
struct bt_micp_mic_ctlr {
uint16_t start_handle;
uint16_t end_handle;
uint16_t mute_handle;
struct bt_gatt_subscribe_params mute_sub_params;
struct bt_gatt_discover_params mute_sub_disc_params;
bool busy;
uint8_t mute_val_buf[1]; /* Mute value is a single octet */
struct bt_gatt_write_params write_params;
struct bt_gatt_read_params read_params;
struct bt_gatt_discover_params discover_params;
struct bt_conn *conn;
uint8_t aics_inst_cnt;
struct bt_aics *aics[CONFIG_BT_MICP_MIC_CTLR_MAX_AICS_INST];
};
#endif /* CONFIG_BT_MICP_MIC_CTLR */
/* Struct used as a common type for the api */
struct bt_micp {
bool client_instance;
union {
#if defined(CONFIG_BT_MICP_MIC_DEV)
struct bt_micp_server srv;
#endif /* CONFIG_BT_MICP_MIC_DEV */
#if defined(CONFIG_BT_MICP_MIC_CTLR)
struct bt_micp_mic_ctlr cli;
#endif /* CONFIG_BT_MICP_MIC_CTLR */
};
};
#endif /* ZEPHYR_INCLUDE_BLUETOOTH_AUDIO_MICS_INTERNAL_ */

View file

@ -20,8 +20,6 @@
#include <zephyr/bluetooth/gatt.h> #include <zephyr/bluetooth/gatt.h>
#include <zephyr/bluetooth/audio/micp.h> #include <zephyr/bluetooth/audio/micp.h>
#include "micp_internal.h"
#define BT_DBG_ENABLED IS_ENABLED(CONFIG_BT_DEBUG_MICP_MIC_CTLR) #define BT_DBG_ENABLED IS_ENABLED(CONFIG_BT_DEBUG_MICP_MIC_CTLR)
#define LOG_MODULE_NAME bt_micp_mic_ctlr #define LOG_MODULE_NAME bt_micp_mic_ctlr
#include "common/log.h" #include "common/log.h"
@ -29,7 +27,25 @@
/* Callback functions */ /* Callback functions */
static struct bt_micp_mic_ctlr_cb *micp_mic_ctlr_cb; static struct bt_micp_mic_ctlr_cb *micp_mic_ctlr_cb;
static struct bt_micp micp_insts[CONFIG_BT_MAX_CONN]; struct bt_micp_mic_ctlr {
uint16_t start_handle;
uint16_t end_handle;
uint16_t mute_handle;
struct bt_gatt_subscribe_params mute_sub_params;
struct bt_gatt_discover_params mute_sub_disc_params;
bool busy;
uint8_t mute_val_buf[1]; /* Mute value is a single octet */
struct bt_gatt_write_params write_params;
struct bt_gatt_read_params read_params;
struct bt_gatt_discover_params discover_params;
struct bt_conn *conn;
uint8_t aics_inst_cnt;
struct bt_aics *aics[CONFIG_BT_MICP_MIC_CTLR_MAX_AICS_INST];
};
static struct bt_micp_mic_ctlr mic_ctlrs[CONFIG_BT_MAX_CONN];
static struct bt_uuid *mics_uuid = BT_UUID_MICS; static struct bt_uuid *mics_uuid = BT_UUID_MICS;
static uint8_t mute_notify_handler(struct bt_conn *conn, static uint8_t mute_notify_handler(struct bt_conn *conn,
@ -37,13 +53,13 @@ static uint8_t mute_notify_handler(struct bt_conn *conn,
const void *data, uint16_t length) const void *data, uint16_t length)
{ {
uint8_t *mute_val; uint8_t *mute_val;
struct bt_micp *micp_inst; struct bt_micp_mic_ctlr *mic_ctlr;
if (conn == NULL) { if (conn == NULL) {
return BT_GATT_ITER_CONTINUE; return BT_GATT_ITER_CONTINUE;
} }
micp_inst = &micp_insts[bt_conn_index(conn)]; mic_ctlr = &mic_ctlrs[bt_conn_index(conn)];
if (data != NULL) { if (data != NULL) {
if (length == sizeof(*mute_val)) { if (length == sizeof(*mute_val)) {
@ -51,7 +67,7 @@ static uint8_t mute_notify_handler(struct bt_conn *conn,
BT_DBG("Mute %u", *mute_val); BT_DBG("Mute %u", *mute_val);
if (micp_mic_ctlr_cb != NULL && if (micp_mic_ctlr_cb != NULL &&
micp_mic_ctlr_cb->mute != NULL) { micp_mic_ctlr_cb->mute != NULL) {
micp_mic_ctlr_cb->mute(micp_inst, 0, *mute_val); micp_mic_ctlr_cb->mute(mic_ctlr, 0, *mute_val);
} }
} else { } else {
BT_DBG("Invalid length %u (expected %zu)", BT_DBG("Invalid length %u (expected %zu)",
@ -69,9 +85,9 @@ static uint8_t micp_mic_ctlr_read_mute_cb(struct bt_conn *conn, uint8_t err,
{ {
uint8_t cb_err = err; uint8_t cb_err = err;
uint8_t mute_val = 0; uint8_t mute_val = 0;
struct bt_micp *micp_inst = &micp_insts[bt_conn_index(conn)]; struct bt_micp_mic_ctlr *mic_ctlr = &mic_ctlrs[bt_conn_index(conn)];
micp_inst->cli.busy = false; mic_ctlr->busy = false;
if (err > 0) { if (err > 0) {
BT_DBG("err: 0x%02X", err); BT_DBG("err: 0x%02X", err);
@ -87,7 +103,7 @@ static uint8_t micp_mic_ctlr_read_mute_cb(struct bt_conn *conn, uint8_t err,
} }
if (micp_mic_ctlr_cb != NULL && micp_mic_ctlr_cb->mute != NULL) { if (micp_mic_ctlr_cb != NULL && micp_mic_ctlr_cb->mute != NULL) {
micp_mic_ctlr_cb->mute(micp_inst, cb_err, mute_val); micp_mic_ctlr_cb->mute(mic_ctlr, cb_err, mute_val);
} }
return BT_GATT_ITER_STOP; return BT_GATT_ITER_STOP;
@ -96,36 +112,36 @@ static uint8_t micp_mic_ctlr_read_mute_cb(struct bt_conn *conn, uint8_t err,
static void micp_mic_ctlr_write_mics_mute_cb(struct bt_conn *conn, uint8_t err, static void micp_mic_ctlr_write_mics_mute_cb(struct bt_conn *conn, uint8_t err,
struct bt_gatt_write_params *params) struct bt_gatt_write_params *params)
{ {
struct bt_micp *micp_inst = &micp_insts[bt_conn_index(conn)]; struct bt_micp_mic_ctlr *mic_ctlr = &mic_ctlrs[bt_conn_index(conn)];
uint8_t mute_val = micp_inst->cli.mute_val_buf[0]; uint8_t mute_val = mic_ctlr->mute_val_buf[0];
BT_DBG("Write %s (0x%02X)", err ? "failed" : "successful", err); BT_DBG("Write %s (0x%02X)", err ? "failed" : "successful", err);
micp_inst->cli.busy = false; mic_ctlr->busy = false;
if (mute_val == BT_MICP_MUTE_UNMUTED) { if (mute_val == BT_MICP_MUTE_UNMUTED) {
if (micp_mic_ctlr_cb != NULL && if (micp_mic_ctlr_cb != NULL &&
micp_mic_ctlr_cb->unmute_written != NULL) { micp_mic_ctlr_cb->unmute_written != NULL) {
micp_mic_ctlr_cb->unmute_written(micp_inst, err); micp_mic_ctlr_cb->unmute_written(mic_ctlr, err);
} }
} else { } else {
if (micp_mic_ctlr_cb != NULL && if (micp_mic_ctlr_cb != NULL &&
micp_mic_ctlr_cb->mute_written != NULL) { micp_mic_ctlr_cb->mute_written != NULL) {
micp_mic_ctlr_cb->mute_written(micp_inst, err); micp_mic_ctlr_cb->mute_written(mic_ctlr, err);
} }
} }
} }
#if defined(CONFIG_BT_MICP_MIC_CTLR_AICS) #if defined(CONFIG_BT_MICP_MIC_CTLR_AICS)
static struct bt_micp *lookup_micp_by_aics(const struct bt_aics *aics) static struct bt_micp_mic_ctlr *lookup_micp_by_aics(const struct bt_aics *aics)
{ {
__ASSERT(aics != NULL, "AICS pointer cannot be NULL"); __ASSERT(aics != NULL, "AICS pointer cannot be NULL");
for (int i = 0; i < ARRAY_SIZE(micp_insts); i++) { for (int i = 0; i < ARRAY_SIZE(mic_ctlrs); i++) {
for (int j = 0; j < ARRAY_SIZE(micp_insts[i].cli.aics); j++) { for (int j = 0; j < ARRAY_SIZE(mic_ctlrs[i].aics); j++) {
if (micp_insts[i].cli.aics[j] == aics) { if (mic_ctlrs[i].aics[j] == aics) {
return &micp_insts[i]; return &mic_ctlrs[i];
} }
} }
} }
@ -135,19 +151,19 @@ static struct bt_micp *lookup_micp_by_aics(const struct bt_aics *aics)
static void aics_discover_cb(struct bt_aics *inst, int err) static void aics_discover_cb(struct bt_aics *inst, int err)
{ {
struct bt_micp *micp_inst = lookup_micp_by_aics(inst); struct bt_micp_mic_ctlr *mic_ctlr = lookup_micp_by_aics(inst);
if (err == 0) { if (err == 0) {
/* Continue discovery of included services */ /* Continue discovery of included services */
err = bt_gatt_discover(micp_inst->cli.conn, err = bt_gatt_discover(mic_ctlr->conn,
&micp_inst->cli.discover_params); &mic_ctlr->discover_params);
} }
if (err != 0) { if (err != 0) {
BT_DBG("Discover failed (err %d)", err); BT_DBG("Discover failed (err %d)", err);
if (micp_mic_ctlr_cb != NULL && if (micp_mic_ctlr_cb != NULL &&
micp_mic_ctlr_cb->discover != NULL) { micp_mic_ctlr_cb->discover != NULL) {
micp_mic_ctlr_cb->discover(micp_inst, err, 0); micp_mic_ctlr_cb->discover(mic_ctlr, err, 0);
} }
} }
} }
@ -157,17 +173,17 @@ static uint8_t micp_discover_include_func(
struct bt_conn *conn, const struct bt_gatt_attr *attr, struct bt_conn *conn, const struct bt_gatt_attr *attr,
struct bt_gatt_discover_params *params) struct bt_gatt_discover_params *params)
{ {
struct bt_micp *micp_inst = &micp_insts[bt_conn_index(conn)]; struct bt_micp_mic_ctlr *mic_ctlr = &mic_ctlrs[bt_conn_index(conn)];
if (attr == NULL) { if (attr == NULL) {
BT_DBG("Discover include complete for MICP: %u AICS", BT_DBG("Discover include complete for MICS: %u AICS",
micp_inst->cli.aics_inst_cnt); mic_ctlr->aics_inst_cnt);
(void)memset(params, 0, sizeof(*params)); (void)memset(params, 0, sizeof(*params));
if (micp_mic_ctlr_cb != NULL && if (micp_mic_ctlr_cb != NULL &&
micp_mic_ctlr_cb->discover != NULL) { micp_mic_ctlr_cb->discover != NULL) {
micp_mic_ctlr_cb->discover(micp_inst, 0, micp_mic_ctlr_cb->discover(mic_ctlr, 0,
micp_inst->cli.aics_inst_cnt); mic_ctlr->aics_inst_cnt);
} }
return BT_GATT_ITER_STOP; return BT_GATT_ITER_STOP;
@ -181,7 +197,7 @@ static uint8_t micp_discover_include_func(
BT_DBG("Include UUID %s", bt_uuid_str(include->uuid)); BT_DBG("Include UUID %s", bt_uuid_str(include->uuid));
if (bt_uuid_cmp(include->uuid, BT_UUID_AICS) == 0 && if (bt_uuid_cmp(include->uuid, BT_UUID_AICS) == 0 &&
micp_inst->cli.aics_inst_cnt < CONFIG_BT_MICP_MIC_CTLR_MAX_AICS_INST) { mic_ctlr->aics_inst_cnt < CONFIG_BT_MICP_MIC_CTLR_MAX_AICS_INST) {
uint8_t inst_idx; uint8_t inst_idx;
int err; int err;
struct bt_aics_discover_param param = { struct bt_aics_discover_param param = {
@ -192,16 +208,16 @@ static uint8_t micp_discover_include_func(
/* Update discover params so we can continue where we /* Update discover params so we can continue where we
* left off after bt_aics_discover * left off after bt_aics_discover
*/ */
micp_inst->cli.discover_params.start_handle = attr->handle + 1; mic_ctlr->discover_params.start_handle = attr->handle + 1;
inst_idx = micp_inst->cli.aics_inst_cnt++; inst_idx = mic_ctlr->aics_inst_cnt++;
err = bt_aics_discover(conn, micp_inst->cli.aics[inst_idx], err = bt_aics_discover(conn, mic_ctlr->aics[inst_idx],
&param); &param);
if (err != 0) { if (err != 0) {
BT_DBG("AICS Discover failed (err %d)", err); BT_DBG("AICS Discover failed (err %d)", err);
if (micp_mic_ctlr_cb != NULL && if (micp_mic_ctlr_cb != NULL &&
micp_mic_ctlr_cb->discover != NULL) { micp_mic_ctlr_cb->discover != NULL) {
micp_mic_ctlr_cb->discover(micp_inst, err, micp_mic_ctlr_cb->discover(mic_ctlr, err,
0); 0);
} }
} }
@ -221,33 +237,33 @@ static uint8_t micp_discover_func(struct bt_conn *conn,
const struct bt_gatt_attr *attr, const struct bt_gatt_attr *attr,
struct bt_gatt_discover_params *params) struct bt_gatt_discover_params *params)
{ {
struct bt_micp *micp_inst = &micp_insts[bt_conn_index(conn)]; struct bt_micp_mic_ctlr *mic_ctlr = &mic_ctlrs[bt_conn_index(conn)];
if (attr == NULL) { if (attr == NULL) {
int err = 0; int err = 0;
BT_DBG("Setup complete for MICP"); BT_DBG("Discovery complete");
(void)memset(params, 0, sizeof(*params)); (void)memset(params, 0, sizeof(*params));
if (CONFIG_BT_MICP_MIC_CTLR_MAX_AICS_INST > 0) { if (CONFIG_BT_MICP_MIC_CTLR_MAX_AICS_INST > 0) {
/* Discover included services */ /* Discover included services */
micp_inst->cli.discover_params.start_handle = micp_inst->cli.start_handle; mic_ctlr->discover_params.start_handle = mic_ctlr->start_handle;
micp_inst->cli.discover_params.end_handle = micp_inst->cli.end_handle; mic_ctlr->discover_params.end_handle = mic_ctlr->end_handle;
micp_inst->cli.discover_params.type = BT_GATT_DISCOVER_INCLUDE; mic_ctlr->discover_params.type = BT_GATT_DISCOVER_INCLUDE;
micp_inst->cli.discover_params.func = micp_discover_include_func; mic_ctlr->discover_params.func = micp_discover_include_func;
err = bt_gatt_discover(conn, err = bt_gatt_discover(conn,
&micp_inst->cli.discover_params); &mic_ctlr->discover_params);
if (err != 0) { if (err != 0) {
BT_DBG("Discover failed (err %d)", err); BT_DBG("Discover AICS failed (err %d)", err);
if (micp_mic_ctlr_cb != NULL && if (micp_mic_ctlr_cb != NULL &&
micp_mic_ctlr_cb->discover != NULL) { micp_mic_ctlr_cb->discover != NULL) {
micp_mic_ctlr_cb->discover(micp_inst, err, 0); micp_mic_ctlr_cb->discover(mic_ctlr, err, 0);
} }
} }
} else { } else {
if (micp_mic_ctlr_cb != NULL && if (micp_mic_ctlr_cb != NULL &&
micp_mic_ctlr_cb->discover != NULL) { micp_mic_ctlr_cb->discover != NULL) {
micp_mic_ctlr_cb->discover(micp_inst, err, 0); micp_mic_ctlr_cb->discover(mic_ctlr, err, 0);
} }
} }
return BT_GATT_ITER_STOP; return BT_GATT_ITER_STOP;
@ -261,9 +277,9 @@ static uint8_t micp_discover_func(struct bt_conn *conn,
if (bt_uuid_cmp(chrc->uuid, BT_UUID_MICS_MUTE) == 0) { if (bt_uuid_cmp(chrc->uuid, BT_UUID_MICS_MUTE) == 0) {
BT_DBG("Mute"); BT_DBG("Mute");
micp_inst->cli.mute_handle = chrc->value_handle; mic_ctlr->mute_handle = chrc->value_handle;
sub_params = &micp_inst->cli.mute_sub_params; sub_params = &mic_ctlr->mute_sub_params;
sub_params->disc_params = &micp_inst->cli.mute_sub_disc_params; sub_params->disc_params = &mic_ctlr->mute_sub_disc_params;
} }
if (sub_params != NULL) { if (sub_params != NULL) {
@ -271,7 +287,7 @@ static uint8_t micp_discover_func(struct bt_conn *conn,
/* With ccc_handle == 0 it will use auto discovery */ /* With ccc_handle == 0 it will use auto discovery */
sub_params->ccc_handle = 0; sub_params->ccc_handle = 0;
sub_params->end_handle = micp_inst->cli.end_handle; sub_params->end_handle = mic_ctlr->end_handle;
sub_params->value = BT_GATT_CCC_NOTIFY; sub_params->value = BT_GATT_CCC_NOTIFY;
sub_params->value_handle = chrc->value_handle; sub_params->value_handle = chrc->value_handle;
sub_params->notify = mute_notify_handler; sub_params->notify = mute_notify_handler;
@ -294,13 +310,13 @@ static uint8_t primary_discover_func(struct bt_conn *conn,
const struct bt_gatt_attr *attr, const struct bt_gatt_attr *attr,
struct bt_gatt_discover_params *params) struct bt_gatt_discover_params *params)
{ {
struct bt_micp *micp_inst = &micp_insts[bt_conn_index(conn)]; struct bt_micp_mic_ctlr *mic_ctlr = &mic_ctlrs[bt_conn_index(conn)];
if (attr == NULL) { if (attr == NULL) {
BT_DBG("Could not find a MICS instance on the server"); BT_DBG("Could not find a MICS instance on the server");
if (micp_mic_ctlr_cb != NULL && if (micp_mic_ctlr_cb != NULL &&
micp_mic_ctlr_cb->discover != NULL) { micp_mic_ctlr_cb->discover != NULL) {
micp_mic_ctlr_cb->discover(micp_inst, -ENODATA, 0); micp_mic_ctlr_cb->discover(mic_ctlr, -ENODATA, 0);
} }
return BT_GATT_ITER_STOP; return BT_GATT_ITER_STOP;
} }
@ -313,22 +329,22 @@ static uint8_t primary_discover_func(struct bt_conn *conn,
int err; int err;
BT_DBG("Primary discover complete"); BT_DBG("Primary discover complete");
micp_inst->cli.start_handle = attr->handle + 1; mic_ctlr->start_handle = attr->handle + 1;
micp_inst->cli.end_handle = prim_service->end_handle; mic_ctlr->end_handle = prim_service->end_handle;
/* Discover characteristics */ /* Discover characteristics */
micp_inst->cli.discover_params.uuid = NULL; mic_ctlr->discover_params.uuid = NULL;
micp_inst->cli.discover_params.start_handle = micp_inst->cli.start_handle; mic_ctlr->discover_params.start_handle = mic_ctlr->start_handle;
micp_inst->cli.discover_params.end_handle = micp_inst->cli.end_handle; mic_ctlr->discover_params.end_handle = mic_ctlr->end_handle;
micp_inst->cli.discover_params.type = BT_GATT_DISCOVER_CHARACTERISTIC; mic_ctlr->discover_params.type = BT_GATT_DISCOVER_CHARACTERISTIC;
micp_inst->cli.discover_params.func = micp_discover_func; mic_ctlr->discover_params.func = micp_discover_func;
err = bt_gatt_discover(conn, &micp_inst->cli.discover_params); err = bt_gatt_discover(conn, &mic_ctlr->discover_params);
if (err != 0) { if (err != 0) {
BT_DBG("Discover failed (err %d)", err); BT_DBG("Discover failed (err %d)", err);
if (micp_mic_ctlr_cb != NULL && if (micp_mic_ctlr_cb != NULL &&
micp_mic_ctlr_cb->discover != NULL) { micp_mic_ctlr_cb->discover != NULL) {
micp_mic_ctlr_cb->discover(micp_inst, err, 0); micp_mic_ctlr_cb->discover(mic_ctlr, err, 0);
} }
} }
@ -338,15 +354,15 @@ static uint8_t primary_discover_func(struct bt_conn *conn,
return BT_GATT_ITER_CONTINUE; return BT_GATT_ITER_CONTINUE;
} }
static void micp_mic_ctlr_reset(struct bt_micp *micp) static void micp_mic_ctlr_reset(struct bt_micp_mic_ctlr *mic_ctlr)
{ {
micp->cli.start_handle = 0; mic_ctlr->start_handle = 0;
micp->cli.end_handle = 0; mic_ctlr->end_handle = 0;
micp->cli.mute_handle = 0; mic_ctlr->mute_handle = 0;
micp->cli.aics_inst_cnt = 0; mic_ctlr->aics_inst_cnt = 0;
if (micp->cli.conn != NULL) { if (mic_ctlr->conn != NULL) {
struct bt_conn *conn = micp->cli.conn; struct bt_conn *conn = mic_ctlr->conn;
/* It's okay if this fails. In case of disconnect, we can't /* It's okay if this fails. In case of disconnect, we can't
* unsubscribe and it will just fail. * unsubscribe and it will just fail.
@ -354,19 +370,19 @@ static void micp_mic_ctlr_reset(struct bt_micp *micp)
* function, we will unsubscribe (regardless of bonding state) * function, we will unsubscribe (regardless of bonding state)
* to accommodate the new discovery values. * to accommodate the new discovery values.
*/ */
(void)bt_gatt_unsubscribe(conn, &micp->cli.mute_sub_params); (void)bt_gatt_unsubscribe(conn, &mic_ctlr->mute_sub_params);
bt_conn_unref(conn); bt_conn_unref(conn);
micp->cli.conn = NULL; mic_ctlr->conn = NULL;
} }
} }
static void disconnected(struct bt_conn *conn, uint8_t reason) static void disconnected(struct bt_conn *conn, uint8_t reason)
{ {
struct bt_micp *micp = &micp_insts[bt_conn_index(conn)]; struct bt_micp_mic_ctlr *mic_ctlr = &mic_ctlrs[bt_conn_index(conn)];
if (micp->cli.conn == conn) { if (mic_ctlr->conn == conn) {
micp_mic_ctlr_reset(micp); micp_mic_ctlr_reset(mic_ctlr);
} }
} }
@ -374,9 +390,9 @@ BT_CONN_CB_DEFINE(conn_callbacks) = {
.disconnected = disconnected, .disconnected = disconnected,
}; };
int bt_micp_mic_ctlr_discover(struct bt_conn *conn, struct bt_micp **micp) int bt_micp_mic_ctlr_discover(struct bt_conn *conn, struct bt_micp_mic_ctlr **mic_ctlr_out)
{ {
struct bt_micp *micp_inst; struct bt_micp_mic_ctlr *mic_ctlr;
int err; int err;
/* /*
@ -394,24 +410,24 @@ int bt_micp_mic_ctlr_discover(struct bt_conn *conn, struct bt_micp **micp)
return -EINVAL; return -EINVAL;
} }
micp_inst = &micp_insts[bt_conn_index(conn)]; mic_ctlr = &mic_ctlrs[bt_conn_index(conn)];
(void)memset(&micp_inst->cli.discover_params, 0, (void)memset(&mic_ctlr->discover_params, 0,
sizeof(micp_inst->cli.discover_params)); sizeof(mic_ctlr->discover_params));
micp_mic_ctlr_reset(micp_inst); micp_mic_ctlr_reset(mic_ctlr);
#if defined(CONFIG_BT_MICP_MIC_CTLR_AICS) #if defined(CONFIG_BT_MICP_MIC_CTLR_AICS)
static bool initialized; static bool initialized;
if (!initialized) { if (!initialized) {
for (int i = 0; i < ARRAY_SIZE(micp_inst->cli.aics); i++) { for (int i = 0; i < ARRAY_SIZE(mic_ctlr->aics); i++) {
micp_inst->cli.aics[i] = bt_aics_client_free_instance_get(); mic_ctlr->aics[i] = bt_aics_client_free_instance_get();
if (micp_inst->cli.aics[i] == NULL) { if (mic_ctlr->aics[i] == NULL) {
return -ENOMEM; return -ENOMEM;
} }
bt_aics_client_cb_register(micp_inst->cli.aics[i], bt_aics_client_cb_register(mic_ctlr->aics[i],
&micp_mic_ctlr_cb->aics_cb); &micp_mic_ctlr_cb->aics_cb);
} }
} }
@ -419,17 +435,16 @@ int bt_micp_mic_ctlr_discover(struct bt_conn *conn, struct bt_micp **micp)
initialized = true; initialized = true;
#endif /* CONFIG_BT_MICP_MIC_CTLR_AICS */ #endif /* CONFIG_BT_MICP_MIC_CTLR_AICS */
micp_inst->cli.conn = bt_conn_ref(conn); mic_ctlr->conn = bt_conn_ref(conn);
micp_inst->client_instance = true; mic_ctlr->discover_params.func = primary_discover_func;
micp_inst->cli.discover_params.func = primary_discover_func; mic_ctlr->discover_params.uuid = mics_uuid;
micp_inst->cli.discover_params.uuid = mics_uuid; mic_ctlr->discover_params.type = BT_GATT_DISCOVER_PRIMARY;
micp_inst->cli.discover_params.type = BT_GATT_DISCOVER_PRIMARY; mic_ctlr->discover_params.start_handle = BT_ATT_FIRST_ATTRIBUTE_HANDLE;
micp_inst->cli.discover_params.start_handle = BT_ATT_FIRST_ATTRIBUTE_HANDLE; mic_ctlr->discover_params.end_handle = BT_ATT_LAST_ATTRIBUTE_HANDLE;
micp_inst->cli.discover_params.end_handle = BT_ATT_LAST_ATTRIBUTE_HANDLE;
err = bt_gatt_discover(conn, &micp_inst->cli.discover_params); err = bt_gatt_discover(conn, &mic_ctlr->discover_params);
if (err == 0) { if (err == 0) {
*micp = micp_inst; *mic_ctlr_out = mic_ctlr;
} }
return err; return err;
@ -450,9 +465,9 @@ int bt_micp_mic_ctlr_cb_register(struct bt_micp_mic_ctlr_cb *cb)
aics_cb = &cb->aics_cb; aics_cb = &cb->aics_cb;
} }
for (int i = 0; i < ARRAY_SIZE(micp_insts); i++) { for (int i = 0; i < ARRAY_SIZE(mic_ctlrs); i++) {
for (int j = 0; j < ARRAY_SIZE(micp_insts[i].cli.aics); j++) { for (int j = 0; j < ARRAY_SIZE(mic_ctlrs[i].aics); j++) {
struct bt_aics *aics = micp_insts[i].cli.aics[j]; struct bt_aics *aics = mic_ctlrs[i].aics[j];
if (aics != NULL) { if (aics != NULL) {
bt_aics_client_cb_register(aics, aics_cb); bt_aics_client_cb_register(aics, aics_cb);
@ -466,11 +481,11 @@ int bt_micp_mic_ctlr_cb_register(struct bt_micp_mic_ctlr_cb *cb)
return 0; return 0;
} }
int bt_micp_mic_ctlr_included_get(struct bt_micp *micp, int bt_micp_mic_ctlr_included_get(struct bt_micp_mic_ctlr *mic_ctlr,
struct bt_micp_included *included) struct bt_micp_included *included)
{ {
CHECKIF(micp == NULL) { CHECKIF(mic_ctlr == NULL) {
BT_DBG("NULL micp"); BT_DBG("NULL mic_ctlr");
return -EINVAL; return -EINVAL;
} }
@ -478,100 +493,95 @@ int bt_micp_mic_ctlr_included_get(struct bt_micp *micp,
return -EINVAL; return -EINVAL;
} }
included->aics_cnt = micp->cli.aics_inst_cnt; included->aics_cnt = mic_ctlr->aics_inst_cnt;
included->aics = micp->cli.aics; included->aics = mic_ctlr->aics;
return 0; return 0;
} }
int bt_micp_mic_ctlr_conn_get(const struct bt_micp *micp, struct bt_conn **conn) int bt_micp_mic_ctlr_conn_get(const struct bt_micp_mic_ctlr *mic_ctlr, struct bt_conn **conn)
{ {
CHECKIF(micp == NULL) { CHECKIF(mic_ctlr == NULL) {
BT_DBG("NULL micp pointer"); BT_DBG("NULL mic_ctlr pointer");
return -EINVAL; return -EINVAL;
} }
if (!micp->client_instance) { if (mic_ctlr->conn == NULL) {
BT_DBG("micp pointer shall be client instance"); BT_DBG("mic_ctlr pointer not associated with a connection. "
return -EINVAL;
}
if (micp->cli.conn == NULL) {
BT_DBG("micp pointer not associated with a connection. "
"Do discovery first"); "Do discovery first");
return -ENOTCONN; return -ENOTCONN;
} }
*conn = micp->cli.conn; *conn = mic_ctlr->conn;
return 0; return 0;
} }
int bt_micp_mic_ctlr_mute_get(struct bt_micp *micp) int bt_micp_mic_ctlr_mute_get(struct bt_micp_mic_ctlr *mic_ctlr)
{ {
int err; int err;
CHECKIF(micp == NULL) { CHECKIF(mic_ctlr == NULL) {
BT_DBG("NULL micp"); BT_DBG("NULL mic_ctlr");
return -EINVAL; return -EINVAL;
} }
if (micp->cli.mute_handle == 0) { if (mic_ctlr->mute_handle == 0) {
BT_DBG("Handle not set"); BT_DBG("Handle not set");
return -EINVAL; return -EINVAL;
} else if (micp->cli.busy) { } else if (mic_ctlr->busy) {
return -EBUSY; return -EBUSY;
} }
micp->cli.read_params.func = micp_mic_ctlr_read_mute_cb; mic_ctlr->read_params.func = micp_mic_ctlr_read_mute_cb;
micp->cli.read_params.handle_count = 1; mic_ctlr->read_params.handle_count = 1;
micp->cli.read_params.single.handle = micp->cli.mute_handle; mic_ctlr->read_params.single.handle = mic_ctlr->mute_handle;
micp->cli.read_params.single.offset = 0U; mic_ctlr->read_params.single.offset = 0U;
err = bt_gatt_read(micp->cli.conn, &micp->cli.read_params); err = bt_gatt_read(mic_ctlr->conn, &mic_ctlr->read_params);
if (err == 0) { if (err == 0) {
micp->cli.busy = true; mic_ctlr->busy = true;
} }
return err; return err;
} }
int bt_micp_mic_ctlr_write_mute(struct bt_micp *micp, bool mute) int bt_micp_mic_ctlr_write_mute(struct bt_micp_mic_ctlr *mic_ctlr, bool mute)
{ {
int err; int err;
CHECKIF(micp == NULL) { CHECKIF(mic_ctlr == NULL) {
BT_DBG("NULL micp"); BT_DBG("NULL mic_ctlr");
return -EINVAL; return -EINVAL;
} }
if (micp->cli.mute_handle == 0) { if (mic_ctlr->mute_handle == 0) {
BT_DBG("Handle not set"); BT_DBG("Handle not set");
return -EINVAL; return -EINVAL;
} else if (micp->cli.busy) { } else if (mic_ctlr->busy) {
return -EBUSY; return -EBUSY;
} }
micp->cli.mute_val_buf[0] = mute; mic_ctlr->mute_val_buf[0] = mute;
micp->cli.write_params.offset = 0; mic_ctlr->write_params.offset = 0;
micp->cli.write_params.data = micp->cli.mute_val_buf; mic_ctlr->write_params.data = mic_ctlr->mute_val_buf;
micp->cli.write_params.length = sizeof(mute); mic_ctlr->write_params.length = sizeof(mute);
micp->cli.write_params.handle = micp->cli.mute_handle; mic_ctlr->write_params.handle = mic_ctlr->mute_handle;
micp->cli.write_params.func = micp_mic_ctlr_write_mics_mute_cb; mic_ctlr->write_params.func = micp_mic_ctlr_write_mics_mute_cb;
err = bt_gatt_write(micp->cli.conn, &micp->cli.write_params); err = bt_gatt_write(mic_ctlr->conn, &mic_ctlr->write_params);
if (err == 0) { if (err == 0) {
micp->cli.busy = true; mic_ctlr->busy = true;
} }
return err; return err;
} }
int bt_micp_mic_ctlr_mute(struct bt_micp *micp) int bt_micp_mic_ctlr_mute(struct bt_micp_mic_ctlr *mic_ctlr)
{ {
return bt_micp_mic_ctlr_write_mute(micp, true); return bt_micp_mic_ctlr_write_mute(mic_ctlr, true);
} }
int bt_micp_mic_ctlr_unmute(struct bt_micp *micp) int bt_micp_mic_ctlr_unmute(struct bt_micp_mic_ctlr *mic_ctlr)
{ {
return bt_micp_mic_ctlr_write_mute(micp, false); return bt_micp_mic_ctlr_write_mute(mic_ctlr, false);
} }

View file

@ -19,13 +19,18 @@
#include <zephyr/bluetooth/audio/micp.h> #include <zephyr/bluetooth/audio/micp.h>
#include <zephyr/bluetooth/audio/aics.h> #include <zephyr/bluetooth/audio/aics.h>
#include "micp_internal.h"
#define BT_DBG_ENABLED IS_ENABLED(CONFIG_BT_DEBUG_MICP_MIC_DEV) #define BT_DBG_ENABLED IS_ENABLED(CONFIG_BT_DEBUG_MICP_MIC_DEV)
#define LOG_MODULE_NAME bt_micp #define LOG_MODULE_NAME bt_micp
#include "common/log.h" #include "common/log.h"
static struct bt_micp micp_inst; struct bt_micp_server {
uint8_t mute;
struct bt_micp_mic_dev_cb *cb;
struct bt_gatt_service *service_p;
struct bt_aics *aics_insts[CONFIG_BT_MICP_MIC_DEV_AICS_INSTANCE_COUNT];
};
static struct bt_micp_server micp_inst;
static void mute_cfg_changed(const struct bt_gatt_attr *attr, uint16_t value) static void mute_cfg_changed(const struct bt_gatt_attr *attr, uint16_t value)
{ {
@ -36,10 +41,10 @@ static ssize_t read_mute(struct bt_conn *conn,
const struct bt_gatt_attr *attr, void *buf, const struct bt_gatt_attr *attr, void *buf,
uint16_t len, uint16_t offset) uint16_t len, uint16_t offset)
{ {
BT_DBG("Mute %u", micp_inst.srv.mute); BT_DBG("Mute %u", micp_inst.mute);
return bt_gatt_attr_read(conn, attr, buf, len, offset, return bt_gatt_attr_read(conn, attr, buf, len, offset,
&micp_inst.srv.mute, sizeof(micp_inst.srv.mute)); &micp_inst.mute, sizeof(micp_inst.mute));
} }
static ssize_t write_mute(struct bt_conn *conn, const struct bt_gatt_attr *attr, static ssize_t write_mute(struct bt_conn *conn, const struct bt_gatt_attr *attr,
@ -52,7 +57,7 @@ static ssize_t write_mute(struct bt_conn *conn, const struct bt_gatt_attr *attr,
return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET); return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
} }
if (len != sizeof(micp_inst.srv.mute)) { if (len != sizeof(micp_inst.mute)) {
return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN); return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
} }
@ -61,21 +66,21 @@ static ssize_t write_mute(struct bt_conn *conn, const struct bt_gatt_attr *attr,
return BT_GATT_ERR(BT_MICP_ERR_VAL_OUT_OF_RANGE); return BT_GATT_ERR(BT_MICP_ERR_VAL_OUT_OF_RANGE);
} }
if (conn != NULL && micp_inst.srv.mute == BT_MICP_MUTE_DISABLED) { if (conn != NULL && micp_inst.mute == BT_MICP_MUTE_DISABLED) {
return BT_GATT_ERR(BT_MICP_ERR_MUTE_DISABLED); return BT_GATT_ERR(BT_MICP_ERR_MUTE_DISABLED);
} }
BT_DBG("%u", *val); BT_DBG("%u", *val);
if (*val != micp_inst.srv.mute) { if (*val != micp_inst.mute) {
micp_inst.srv.mute = *val; micp_inst.mute = *val;
bt_gatt_notify_uuid(NULL, BT_UUID_MICS_MUTE, bt_gatt_notify_uuid(NULL, BT_UUID_MICS_MUTE,
micp_inst.srv.service_p->attrs, micp_inst.service_p->attrs,
&micp_inst.srv.mute, sizeof(micp_inst.srv.mute)); &micp_inst.mute, sizeof(micp_inst.mute));
if (micp_inst.srv.cb != NULL && micp_inst.srv.cb->mute != NULL) { if (micp_inst.cb != NULL && micp_inst.cb->mute != NULL) {
micp_inst.srv.cb->mute(micp_inst.srv.mute); micp_inst.cb->mute(micp_inst.mute);
} }
} }
@ -112,20 +117,20 @@ static int prepare_aics_inst(struct bt_micp_mic_dev_register_param *param)
for (j = 0, i = 0; i < ARRAY_SIZE(mics_attrs); i++) { for (j = 0, i = 0; i < ARRAY_SIZE(mics_attrs); i++) {
if (bt_uuid_cmp(mics_attrs[i].uuid, BT_UUID_GATT_INCLUDE) == 0) { if (bt_uuid_cmp(mics_attrs[i].uuid, BT_UUID_GATT_INCLUDE) == 0) {
micp_inst.srv.aics_insts[j] = bt_aics_free_instance_get(); micp_inst.aics_insts[j] = bt_aics_free_instance_get();
if (micp_inst.srv.aics_insts[j] == NULL) { if (micp_inst.aics_insts[j] == NULL) {
BT_DBG("Could not get free AICS instances[%u]", j); BT_DBG("Could not get free AICS instances[%u]", j);
return -ENOMEM; return -ENOMEM;
} }
err = bt_aics_register(micp_inst.srv.aics_insts[j], err = bt_aics_register(micp_inst.aics_insts[j],
&param->aics_param[j]); &param->aics_param[j]);
if (err != 0) { if (err != 0) {
BT_DBG("Could not register AICS instance[%u]: %d", j, err); BT_DBG("Could not register AICS instance[%u]: %d", j, err);
return err; return err;
} }
mics_attrs[i].user_data = bt_aics_svc_decl_get(micp_inst.srv.aics_insts[j]); mics_attrs[i].user_data = bt_aics_svc_decl_get(micp_inst.aics_insts[j]);
j++; j++;
if (j == CONFIG_BT_MICP_MIC_DEV_AICS_INSTANCE_COUNT) { if (j == CONFIG_BT_MICP_MIC_DEV_AICS_INSTANCE_COUNT) {
@ -163,14 +168,14 @@ int bt_micp_mic_dev_register(struct bt_micp_mic_dev_register_param *param)
#endif /* CONFIG_BT_MICP_MIC_DEV_AICS */ #endif /* CONFIG_BT_MICP_MIC_DEV_AICS */
mics_svc = (struct bt_gatt_service)BT_GATT_SERVICE(mics_attrs); mics_svc = (struct bt_gatt_service)BT_GATT_SERVICE(mics_attrs);
micp_inst.srv.service_p = &mics_svc; micp_inst.service_p = &mics_svc;
err = bt_gatt_service_register(&mics_svc); err = bt_gatt_service_register(&mics_svc);
if (err != 0) { if (err != 0) {
BT_ERR("MICS service register failed: %d", err); BT_ERR("MICS service register failed: %d", err);
} }
micp_inst.srv.cb = param->cb; micp_inst.cb = param->cb;
registered = true; registered = true;
@ -195,8 +200,8 @@ int bt_micp_mic_dev_included_get(struct bt_micp_included *included)
} }
#if defined(CONFIG_BT_MICP_MIC_DEV_AICS) #if defined(CONFIG_BT_MICP_MIC_DEV_AICS)
included->aics_cnt = ARRAY_SIZE(micp_inst.srv.aics_insts); included->aics_cnt = ARRAY_SIZE(micp_inst.aics_insts);
included->aics = micp_inst.srv.aics_insts; included->aics = micp_inst.aics_insts;
#endif /* CONFIG_BT_MICP_MIC_DEV_AICS */ #endif /* CONFIG_BT_MICP_MIC_DEV_AICS */
return 0; return 0;
@ -220,8 +225,8 @@ int bt_micp_mic_dev_mute(void)
int bt_micp_mic_dev_mute_get(void) int bt_micp_mic_dev_mute_get(void)
{ {
if (micp_inst.srv.cb && micp_inst.srv.cb->mute) { if (micp_inst.cb && micp_inst.cb->mute) {
micp_inst.srv.cb->mute(micp_inst.srv.mute); micp_inst.cb->mute(micp_inst.mute);
} }
return 0; return 0;

View file

@ -1,5 +1,5 @@
/** @file /** @file
* @brief Bluetooth MICP client shell. * @brief Bluetooth MICP Microphone Controller shell.
* *
* Copyright (c) 2020 Bose Corporation * Copyright (c) 2020 Bose Corporation
* Copyright (c) 2020-2022 Nordic Semiconductor ASA * Copyright (c) 2020-2022 Nordic Semiconductor ASA
@ -16,28 +16,31 @@
#include "bt.h" #include "bt.h"
static struct bt_micp *micp; static struct bt_micp_mic_ctlr *mic_ctlr;
#if defined(CONFIG_BT_MICP_MIC_CTLR_AICS) #if defined(CONFIG_BT_MICP_MIC_CTLR_AICS)
static struct bt_micp_included micp_included; static struct bt_micp_included micp_included;
#endif /* CONFIG_BT_MICP_MIC_CTLR_AICS */ #endif /* CONFIG_BT_MICP_MIC_CTLR_AICS */
static void micp_mic_ctlr_discover_cb(struct bt_micp *micp, int err, uint8_t aics_count) static void micp_mic_ctlr_discover_cb(struct bt_micp_mic_ctlr *mic_ctlr,
int err, uint8_t aics_count)
{ {
if (err != 0) { if (err != 0) {
shell_error(ctx_shell, "MICP discover failed (%d)", err); shell_error(ctx_shell, "Discovery failed (%d)", err);
} else { } else {
shell_print(ctx_shell, "MICP discover done with %u AICS", shell_print(ctx_shell, "Discovery done with %u AICS",
aics_count); aics_count);
#if defined(CONFIG_BT_MICP_MIC_CTLR_AICS) #if defined(CONFIG_BT_MICP_MIC_CTLR_AICS)
if (bt_micp_mic_ctlr_included_get(micp, &micp_included) != 0) { if (bt_micp_mic_ctlr_included_get(mic_ctlr,
shell_error(ctx_shell, "Could not get MICP context"); &micp_included) != 0) {
shell_error(ctx_shell, "Could not get included services");
} }
#endif /* CONFIG_BT_MICP_MIC_CTLR_AICS */ #endif /* CONFIG_BT_MICP_MIC_CTLR_AICS */
} }
} }
static void micp_mic_ctlr_mute_written_cb(struct bt_micp *micp, int err) static void micp_mic_ctlr_mute_written_cb(struct bt_micp_mic_ctlr *mic_ctlr,
int err)
{ {
if (err != 0) { if (err != 0) {
shell_error(ctx_shell, "Mute write failed (%d)", err); shell_error(ctx_shell, "Mute write failed (%d)", err);
@ -46,7 +49,8 @@ static void micp_mic_ctlr_mute_written_cb(struct bt_micp *micp, int err)
} }
} }
static void micp_mic_ctlr_unmute_written_cb(struct bt_micp *micp, int err) static void micp_mic_ctlr_unmute_written_cb(struct bt_micp_mic_ctlr *mic_ctlr,
int err)
{ {
if (err != 0) { if (err != 0) {
shell_error(ctx_shell, "Unmute write failed (%d)", err); shell_error(ctx_shell, "Unmute write failed (%d)", err);
@ -55,7 +59,8 @@ static void micp_mic_ctlr_unmute_written_cb(struct bt_micp *micp, int err)
} }
} }
static void micp_mic_ctlr_mute_cb(struct bt_micp *micp, int err, uint8_t mute) static void micp_mic_ctlr_mute_cb(struct bt_micp_mic_ctlr *mic_ctlr, int err,
uint8_t mute)
{ {
if (err != 0) { if (err != 0) {
shell_error(ctx_shell, "Mute get failed (%d)", err); shell_error(ctx_shell, "Mute get failed (%d)", err);
@ -228,7 +233,7 @@ static int cmd_micp_mic_ctlr_discover(const struct shell *sh, size_t argc,
return -ENOTCONN; return -ENOTCONN;
} }
result = bt_micp_mic_ctlr_discover(default_conn, &micp); result = bt_micp_mic_ctlr_discover(default_conn, &mic_ctlr);
if (result != 0) { if (result != 0) {
shell_print(sh, "Fail: %d", result); shell_print(sh, "Fail: %d", result);
} }
@ -241,11 +246,11 @@ static int cmd_micp_mic_ctlr_mute_get(const struct shell *sh, size_t argc,
{ {
int result; int result;
if (micp == NULL) { if (mic_ctlr == NULL) {
return -ENOENT; return -ENOENT;
} }
result = bt_micp_mic_ctlr_mute_get(micp); result = bt_micp_mic_ctlr_mute_get(mic_ctlr);
if (result != 0) { if (result != 0) {
shell_print(sh, "Fail: %d", result); shell_print(sh, "Fail: %d", result);
@ -259,11 +264,11 @@ static int cmd_micp_mic_ctlr_mute(const struct shell *sh, size_t argc,
{ {
int result; int result;
if (micp == NULL) { if (mic_ctlr == NULL) {
return -ENOENT; return -ENOENT;
} }
result = bt_micp_mic_ctlr_mute(micp); result = bt_micp_mic_ctlr_mute(mic_ctlr);
if (result != 0) { if (result != 0) {
shell_print(sh, "Fail: %d", result); shell_print(sh, "Fail: %d", result);
@ -277,11 +282,11 @@ static int cmd_micp_mic_ctlr_unmute(const struct shell *sh, size_t argc,
{ {
int result; int result;
if (micp == NULL) { if (mic_ctlr == NULL) {
return -ENOENT; return -ENOENT;
} }
result = bt_micp_mic_ctlr_unmute(micp); result = bt_micp_mic_ctlr_unmute(mic_ctlr);
if (result != 0) { if (result != 0) {
shell_print(sh, "Fail: %d", result); shell_print(sh, "Fail: %d", result);
@ -303,7 +308,7 @@ static int cmd_micp_mic_ctlr_aics_input_state_get(const struct shell *sh,
return -ENOEXEC; return -ENOEXEC;
} }
if (micp == NULL) { if (mic_ctlr == NULL) {
return -ENOENT; return -ENOENT;
} }
@ -327,7 +332,7 @@ static int cmd_micp_mic_ctlr_aics_gain_setting_get(const struct shell *sh,
return -ENOEXEC; return -ENOEXEC;
} }
if (micp == NULL) { if (mic_ctlr == NULL) {
return -ENOENT; return -ENOENT;
} }
@ -351,7 +356,7 @@ static int cmd_micp_mic_ctlr_aics_input_type_get(const struct shell *sh,
return -ENOEXEC; return -ENOEXEC;
} }
if (micp == NULL) { if (mic_ctlr == NULL) {
return -ENOENT; return -ENOENT;
} }
@ -375,7 +380,7 @@ static int cmd_micp_mic_ctlr_aics_input_status_get(const struct shell *sh,
return -ENOEXEC; return -ENOEXEC;
} }
if (micp == NULL) { if (mic_ctlr == NULL) {
return -ENOENT; return -ENOENT;
} }
@ -399,7 +404,7 @@ static int cmd_micp_mic_ctlr_aics_input_unmute(const struct shell *sh,
return -ENOEXEC; return -ENOEXEC;
} }
if (micp == NULL) { if (mic_ctlr == NULL) {
return -ENOENT; return -ENOENT;
} }
@ -423,7 +428,7 @@ static int cmd_micp_mic_ctlr_aics_input_mute(const struct shell *sh,
return -ENOEXEC; return -ENOEXEC;
} }
if (micp == NULL) { if (mic_ctlr == NULL) {
return -ENOENT; return -ENOENT;
} }
@ -447,7 +452,7 @@ static int cmd_micp_mic_ctlr_aics_manual_input_gain_set(const struct shell *sh,
return -ENOEXEC; return -ENOEXEC;
} }
if (micp == NULL) { if (mic_ctlr == NULL) {
return -ENOENT; return -ENOENT;
} }
@ -472,7 +477,7 @@ static int cmd_micp_mic_ctlr_aics_automatic_input_gain_set(const struct shell *s
return -ENOEXEC; return -ENOEXEC;
} }
if (micp == NULL) { if (mic_ctlr == NULL) {
return -ENOENT; return -ENOENT;
} }
@ -503,7 +508,7 @@ static int cmd_micp_mic_ctlr_aics_gain_set(const struct shell *sh, size_t argc,
return -ENOEXEC; return -ENOEXEC;
} }
if (micp == NULL) { if (mic_ctlr == NULL) {
return -ENOENT; return -ENOENT;
} }
@ -527,7 +532,7 @@ static int cmd_micp_mic_ctlr_aics_input_description_get(const struct shell *sh,
return -ENOEXEC; return -ENOEXEC;
} }
if (micp == NULL) { if (mic_ctlr == NULL) {
return -ENOENT; return -ENOENT;
} }
@ -552,7 +557,7 @@ static int cmd_micp_mic_ctlr_aics_input_description_set(const struct shell *sh,
return -ENOEXEC; return -ENOEXEC;
} }
if (micp == NULL) { if (mic_ctlr == NULL) {
return -ENOENT; return -ENOENT;
} }
@ -583,13 +588,13 @@ SHELL_STATIC_SUBCMD_SET_CREATE(micp_mic_ctlr_cmds,
"Discover MICS on remote device", "Discover MICS on remote device",
cmd_micp_mic_ctlr_discover, 1, 0), cmd_micp_mic_ctlr_discover, 1, 0),
SHELL_CMD_ARG(mute_get, NULL, SHELL_CMD_ARG(mute_get, NULL,
"Read the mute state of the MICP server.", "Read the mute state of the Microphone Device server.",
cmd_micp_mic_ctlr_mute_get, 1, 0), cmd_micp_mic_ctlr_mute_get, 1, 0),
SHELL_CMD_ARG(mute, NULL, SHELL_CMD_ARG(mute, NULL,
"Mute the MICP server", "Mute the Microphone Device server",
cmd_micp_mic_ctlr_mute, 1, 0), cmd_micp_mic_ctlr_mute, 1, 0),
SHELL_CMD_ARG(unmute, NULL, SHELL_CMD_ARG(unmute, NULL,
"Unmute the MICP server", "Unmute the Microphone Device server",
cmd_micp_mic_ctlr_unmute, 1, 0), cmd_micp_mic_ctlr_unmute, 1, 0),
#if defined(CONFIG_BT_MICP_MIC_CTLR_AICS) #if defined(CONFIG_BT_MICP_MIC_CTLR_AICS)
SHELL_CMD_ARG(aics_input_state_get, NULL, SHELL_CMD_ARG(aics_input_state_get, NULL,
@ -634,5 +639,5 @@ SHELL_STATIC_SUBCMD_SET_CREATE(micp_mic_ctlr_cmds,
); );
SHELL_CMD_ARG_REGISTER(micp_mic_ctlr, &micp_mic_ctlr_cmds, SHELL_CMD_ARG_REGISTER(micp_mic_ctlr, &micp_mic_ctlr_cmds,
"Bluetooth MICP client shell commands", "Bluetooth Microphone Controller shell commands",
cmd_micp_mic_ctlr, 1, 1); cmd_micp_mic_ctlr, 1, 1);

View file

@ -15,7 +15,7 @@
extern enum bst_result_t bst_result; extern enum bst_result_t bst_result;
static struct bt_micp *micp; static struct bt_micp_mic_ctlr *mic_ctlr;
static struct bt_micp_included micp_included; static struct bt_micp_included micp_included;
static volatile bool g_bt_init; static volatile bool g_bt_init;
static volatile bool g_is_connected; static volatile bool g_is_connected;
@ -119,11 +119,12 @@ static void aics_write_cb(struct bt_aics *inst, int err)
g_write_complete = true; g_write_complete = true;
} }
static void micp_mic_ctlr_discover_cb(struct bt_micp *micp, int err, static void micp_mic_ctlr_discover_cb(struct bt_micp_mic_ctlr *mic_ctlr,
int err,
uint8_t aics_count) uint8_t aics_count)
{ {
if (err != 0) { if (err != 0) {
FAIL("MICP could not be discovered (%d)\n", err); FAIL("MICS could not be discovered (%d)\n", err);
return; return;
} }
@ -131,30 +132,33 @@ static void micp_mic_ctlr_discover_cb(struct bt_micp *micp, int err,
g_discovery_complete = true; g_discovery_complete = true;
} }
static void micp_mic_ctlr_mute_written_cb(struct bt_micp *micp, int err) static void micp_mic_ctlr_mute_written_cb(struct bt_micp_mic_ctlr *mic_ctlr,
int err)
{ {
if (err != 0) { if (err != 0) {
FAIL("MICP mute write failed (%d)\n", err); FAIL("mic_ctlr mute write failed (%d)\n", err);
return; return;
} }
g_write_complete = true; g_write_complete = true;
} }
static void micp_mic_ctlr_unmute_written_cb(struct bt_micp *micp, int err) static void micp_mic_ctlr_unmute_written_cb(struct bt_micp_mic_ctlr *mic_ctlr,
int err)
{ {
if (err != 0) { if (err != 0) {
FAIL("MICP unmute write failed (%d)\n", err); FAIL("mic_ctlr unmute write failed (%d)\n", err);
return; return;
} }
g_write_complete = true; g_write_complete = true;
} }
static void micp_mic_ctlr_mute_cb(struct bt_micp *micp, int err, uint8_t mute) static void micp_mic_ctlr_mute_cb(struct bt_micp_mic_ctlr *mic_ctlr, int err,
uint8_t mute)
{ {
if (err != 0) { if (err != 0) {
FAIL("MICP mute read failed (%d)\n", err); FAIL("mic_ctlr mute read failed (%d)\n", err);
return; return;
} }
@ -388,22 +392,22 @@ static void test_main(void)
printk("Scanning successfully started\n"); printk("Scanning successfully started\n");
WAIT_FOR_COND(g_is_connected); WAIT_FOR_COND(g_is_connected);
err = bt_micp_mic_ctlr_discover(default_conn, &micp); err = bt_micp_mic_ctlr_discover(default_conn, &mic_ctlr);
if (err != 0) { if (err != 0) {
FAIL("Failed to discover MICP %d", err); FAIL("Failed to discover MICS %d", err);
} }
WAIT_FOR_COND(g_discovery_complete); WAIT_FOR_COND(g_discovery_complete);
err = bt_micp_mic_ctlr_included_get(micp, &micp_included); err = bt_micp_mic_ctlr_included_get(mic_ctlr, &micp_included);
if (err != 0) { if (err != 0) {
FAIL("Failed to get MICP context (err %d)\n", err); FAIL("Failed to get mic_ctlr context (err %d)\n", err);
return; return;
} }
printk("Getting MICP client conn\n"); printk("Getting mic_ctlr conn\n");
err = bt_micp_mic_ctlr_conn_get(micp, &cached_conn); err = bt_micp_mic_ctlr_conn_get(mic_ctlr, &cached_conn);
if (err != 0) { if (err != 0) {
FAIL("Failed to get MICP client conn (err %d)\n", err); FAIL("Failed to get mic_ctlr conn (err %d)\n", err);
return; return;
} }
if (cached_conn != default_conn) { if (cached_conn != default_conn) {
@ -411,37 +415,37 @@ static void test_main(void)
return; return;
} }
printk("Getting MICP mute state\n"); printk("Getting mic_ctlr mute state\n");
g_cb = false; g_cb = false;
err = bt_micp_mic_ctlr_mute_get(micp); err = bt_micp_mic_ctlr_mute_get(mic_ctlr);
if (err != 0) { if (err != 0) {
FAIL("Could not get MICP mute state (err %d)\n", err); FAIL("Could not get mic_ctlr mute state (err %d)\n", err);
return; return;
} }
WAIT_FOR_COND(g_cb); WAIT_FOR_COND(g_cb);
printk("MICP mute state received\n"); printk("mic_ctlr mute state received\n");
printk("Muting MICP\n"); printk("Muting mic_ctlr\n");
expected_mute = 1; expected_mute = 1;
g_write_complete = g_cb = false; g_write_complete = g_cb = false;
err = bt_micp_mic_ctlr_mute(micp); err = bt_micp_mic_ctlr_mute(mic_ctlr);
if (err != 0) { if (err != 0) {
FAIL("Could not mute MICP (err %d)\n", err); FAIL("Could not mute mic_ctlr (err %d)\n", err);
return; return;
} }
WAIT_FOR_COND(g_mute == expected_mute && g_cb && g_write_complete); WAIT_FOR_COND(g_mute == expected_mute && g_cb && g_write_complete);
printk("MICP muted\n"); printk("mic_ctlr muted\n");
printk("Unmuting MICP\n"); printk("Unmuting mic_ctlr\n");
expected_mute = 0; expected_mute = 0;
g_write_complete = g_cb = false; g_write_complete = g_cb = false;
err = bt_micp_mic_ctlr_unmute(micp); err = bt_micp_mic_ctlr_unmute(mic_ctlr);
if (err != 0) { if (err != 0) {
FAIL("Could not unmute MICP (err %d)\n", err); FAIL("Could not unmute mic_ctlr (err %d)\n", err);
return; return;
} }
WAIT_FOR_COND(g_mute == expected_mute && g_cb && g_write_complete); WAIT_FOR_COND(g_mute == expected_mute && g_cb && g_write_complete);
printk("MICP unmuted\n"); printk("mic_ctlr unmuted\n");
if (CONFIG_BT_MICP_MIC_CTLR_MAX_AICS_INST > 0 && g_aics_count > 0) { if (CONFIG_BT_MICP_MIC_CTLR_MAX_AICS_INST > 0 && g_aics_count > 0) {
if (test_aics()) { if (test_aics()) {
@ -449,7 +453,7 @@ static void test_main(void)
} }
} }
PASS("MICP client Passed\n"); PASS("mic_ctlr Passed\n");
} }
static const struct bst_test_instance test_micp[] = { static const struct bst_test_instance test_micp[] = {