2023-02-21 21:05:11 +05:30
|
|
|
/* Bluetooth MICP - Microphone Control Profile - Microphone Controller */
|
2021-05-03 17:28:01 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2020 Bose Corporation
|
2022-06-17 10:24:03 +02:00
|
|
|
* Copyright (c) 2020-2022 Nordic Semiconductor ASA
|
2021-05-03 17:28:01 +02: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>
|
2021-05-03 17:28:01 +02:00
|
|
|
#include <zephyr/types.h>
|
|
|
|
|
2022-05-06 11:12:04 +02:00
|
|
|
#include <zephyr/sys/check.h>
|
2021-05-03 17:28:01 +02:00
|
|
|
|
2022-05-06 11:12:04 +02:00
|
|
|
#include <zephyr/device.h>
|
|
|
|
#include <zephyr/init.h>
|
2021-05-03 17:28:01 +02:00
|
|
|
|
2022-05-06 11:12:04 +02:00
|
|
|
#include <zephyr/bluetooth/bluetooth.h>
|
|
|
|
#include <zephyr/bluetooth/conn.h>
|
|
|
|
#include <zephyr/bluetooth/gatt.h>
|
2022-06-17 09:06:36 +02:00
|
|
|
#include <zephyr/bluetooth/audio/micp.h>
|
2022-11-02 14:31:13 +01:00
|
|
|
#include <zephyr/logging/log.h>
|
|
|
|
|
2023-08-18 23:32:02 +02:00
|
|
|
#include "micp_internal.h"
|
|
|
|
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_MODULE_REGISTER(bt_micp_mic_ctlr, CONFIG_BT_MICP_MIC_CTLR_LOG_LEVEL);
|
|
|
|
|
2022-10-25 08:48:54 +02:00
|
|
|
#include "common/bt_str.h"
|
2021-05-03 17:28:01 +02:00
|
|
|
|
|
|
|
/* Callback functions */
|
2022-06-21 17:45:11 +02:00
|
|
|
static struct bt_micp_mic_ctlr_cb *micp_mic_ctlr_cb;
|
2021-05-03 17:28:01 +02:00
|
|
|
|
2022-06-21 19:49:13 +02:00
|
|
|
static struct bt_micp_mic_ctlr mic_ctlrs[CONFIG_BT_MAX_CONN];
|
2023-12-05 17:52:21 +08:00
|
|
|
static const struct bt_uuid *mics_uuid = BT_UUID_MICS;
|
2021-05-03 17:28:01 +02:00
|
|
|
|
|
|
|
static uint8_t mute_notify_handler(struct bt_conn *conn,
|
|
|
|
struct bt_gatt_subscribe_params *params,
|
|
|
|
const void *data, uint16_t length)
|
|
|
|
{
|
|
|
|
uint8_t *mute_val;
|
2022-06-21 19:49:13 +02:00
|
|
|
struct bt_micp_mic_ctlr *mic_ctlr;
|
2021-12-03 16:33:22 +01:00
|
|
|
|
|
|
|
if (conn == NULL) {
|
|
|
|
return BT_GATT_ITER_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2022-06-21 19:49:13 +02:00
|
|
|
mic_ctlr = &mic_ctlrs[bt_conn_index(conn)];
|
2021-05-03 17:28:01 +02:00
|
|
|
|
|
|
|
if (data != NULL) {
|
|
|
|
if (length == sizeof(*mute_val)) {
|
|
|
|
mute_val = (uint8_t *)data;
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("Mute %u", *mute_val);
|
2022-06-21 17:45:11 +02:00
|
|
|
if (micp_mic_ctlr_cb != NULL &&
|
|
|
|
micp_mic_ctlr_cb->mute != NULL) {
|
2022-06-21 19:49:13 +02:00
|
|
|
micp_mic_ctlr_cb->mute(mic_ctlr, 0, *mute_val);
|
2021-05-03 17:28:01 +02:00
|
|
|
}
|
|
|
|
} else {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("Invalid length %u (expected %zu)", length, sizeof(*mute_val));
|
2021-05-03 17:28:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return BT_GATT_ITER_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2022-06-21 17:45:11 +02:00
|
|
|
static uint8_t micp_mic_ctlr_read_mute_cb(struct bt_conn *conn, uint8_t err,
|
2021-05-03 17:28:01 +02:00
|
|
|
struct bt_gatt_read_params *params,
|
|
|
|
const void *data, uint16_t length)
|
|
|
|
{
|
|
|
|
uint8_t cb_err = err;
|
2021-06-24 13:10:11 +02:00
|
|
|
uint8_t mute_val = 0;
|
2022-06-21 19:49:13 +02:00
|
|
|
struct bt_micp_mic_ctlr *mic_ctlr = &mic_ctlrs[bt_conn_index(conn)];
|
2021-05-03 17:28:01 +02:00
|
|
|
|
2022-06-21 19:49:13 +02:00
|
|
|
mic_ctlr->busy = false;
|
2021-05-03 17:28:01 +02:00
|
|
|
|
|
|
|
if (err > 0) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("err: 0x%02X", err);
|
2021-05-03 17:28:01 +02:00
|
|
|
} else if (data != NULL) {
|
2021-06-24 13:10:11 +02:00
|
|
|
if (length == sizeof(mute_val)) {
|
|
|
|
mute_val = ((uint8_t *)data)[0];
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("Mute %u", mute_val);
|
2021-05-03 17:28:01 +02:00
|
|
|
} else {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("Invalid length %u (expected %zu)", length, sizeof(mute_val));
|
2021-05-03 17:28:01 +02:00
|
|
|
cb_err = BT_ATT_ERR_INVALID_ATTRIBUTE_LEN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-21 17:45:11 +02:00
|
|
|
if (micp_mic_ctlr_cb != NULL && micp_mic_ctlr_cb->mute != NULL) {
|
2022-06-21 19:49:13 +02:00
|
|
|
micp_mic_ctlr_cb->mute(mic_ctlr, cb_err, mute_val);
|
2021-05-03 17:28:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return BT_GATT_ITER_STOP;
|
|
|
|
}
|
|
|
|
|
2022-06-21 17:45:11 +02:00
|
|
|
static void micp_mic_ctlr_write_mics_mute_cb(struct bt_conn *conn, uint8_t err,
|
2021-05-03 17:28:01 +02:00
|
|
|
struct bt_gatt_write_params *params)
|
|
|
|
{
|
2022-06-21 19:49:13 +02:00
|
|
|
struct bt_micp_mic_ctlr *mic_ctlr = &mic_ctlrs[bt_conn_index(conn)];
|
|
|
|
uint8_t mute_val = mic_ctlr->mute_val_buf[0];
|
2021-05-03 17:28:01 +02:00
|
|
|
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("Write %s (0x%02X)", err ? "failed" : "successful", err);
|
2021-05-03 17:28:01 +02:00
|
|
|
|
2022-06-21 19:49:13 +02:00
|
|
|
mic_ctlr->busy = false;
|
2021-05-03 17:28:01 +02:00
|
|
|
|
2022-06-17 10:24:03 +02:00
|
|
|
if (mute_val == BT_MICP_MUTE_UNMUTED) {
|
2022-06-21 17:45:11 +02:00
|
|
|
if (micp_mic_ctlr_cb != NULL &&
|
|
|
|
micp_mic_ctlr_cb->unmute_written != NULL) {
|
2022-06-21 19:49:13 +02:00
|
|
|
micp_mic_ctlr_cb->unmute_written(mic_ctlr, err);
|
2021-05-03 17:28:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2022-06-21 17:45:11 +02:00
|
|
|
if (micp_mic_ctlr_cb != NULL &&
|
|
|
|
micp_mic_ctlr_cb->mute_written != NULL) {
|
2022-06-21 19:49:13 +02:00
|
|
|
micp_mic_ctlr_cb->mute_written(mic_ctlr, err);
|
2021-05-03 17:28:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-21 17:45:11 +02:00
|
|
|
#if defined(CONFIG_BT_MICP_MIC_CTLR_AICS)
|
2022-06-21 19:49:13 +02:00
|
|
|
static struct bt_micp_mic_ctlr *lookup_micp_by_aics(const struct bt_aics *aics)
|
2021-07-17 15:57:13 +02:00
|
|
|
{
|
|
|
|
__ASSERT(aics != NULL, "AICS pointer cannot be NULL");
|
|
|
|
|
2022-06-21 19:49:13 +02:00
|
|
|
for (int i = 0; i < ARRAY_SIZE(mic_ctlrs); i++) {
|
|
|
|
for (int j = 0; j < ARRAY_SIZE(mic_ctlrs[i].aics); j++) {
|
|
|
|
if (mic_ctlrs[i].aics[j] == aics) {
|
|
|
|
return &mic_ctlrs[i];
|
2021-07-17 15:57:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-06-07 17:35:52 +02:00
|
|
|
static void aics_discover_cb(struct bt_aics *inst, int err)
|
2021-05-03 17:28:01 +02:00
|
|
|
{
|
2022-06-21 19:49:13 +02:00
|
|
|
struct bt_micp_mic_ctlr *mic_ctlr = lookup_micp_by_aics(inst);
|
2021-05-03 17:28:01 +02:00
|
|
|
|
|
|
|
if (err == 0) {
|
|
|
|
/* Continue discovery of included services */
|
2022-06-21 19:49:13 +02:00
|
|
|
err = bt_gatt_discover(mic_ctlr->conn,
|
|
|
|
&mic_ctlr->discover_params);
|
2021-05-03 17:28:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (err != 0) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("Discover failed (err %d)", err);
|
2022-06-21 17:45:11 +02:00
|
|
|
if (micp_mic_ctlr_cb != NULL &&
|
|
|
|
micp_mic_ctlr_cb->discover != NULL) {
|
2022-06-21 19:49:13 +02:00
|
|
|
micp_mic_ctlr_cb->discover(mic_ctlr, err, 0);
|
2021-05-03 17:28:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-06-21 17:45:11 +02:00
|
|
|
#endif /* CONFIG_BT_MICP_MIC_CTLR_AICS */
|
2021-05-03 17:28:01 +02:00
|
|
|
|
2022-06-17 10:24:03 +02:00
|
|
|
static uint8_t micp_discover_include_func(
|
2021-05-03 17:28:01 +02:00
|
|
|
struct bt_conn *conn, const struct bt_gatt_attr *attr,
|
|
|
|
struct bt_gatt_discover_params *params)
|
|
|
|
{
|
2022-06-21 19:49:13 +02:00
|
|
|
struct bt_micp_mic_ctlr *mic_ctlr = &mic_ctlrs[bt_conn_index(conn)];
|
2021-05-03 17:28:01 +02:00
|
|
|
|
|
|
|
if (attr == NULL) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("Discover include complete for MICS: %u AICS", mic_ctlr->aics_inst_cnt);
|
2021-05-03 17:28:01 +02:00
|
|
|
(void)memset(params, 0, sizeof(*params));
|
|
|
|
|
2022-06-21 17:45:11 +02:00
|
|
|
if (micp_mic_ctlr_cb != NULL &&
|
|
|
|
micp_mic_ctlr_cb->discover != NULL) {
|
2022-06-21 19:49:13 +02:00
|
|
|
micp_mic_ctlr_cb->discover(mic_ctlr, 0,
|
|
|
|
mic_ctlr->aics_inst_cnt);
|
2021-05-03 17:28:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return BT_GATT_ITER_STOP;
|
|
|
|
}
|
|
|
|
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("[ATTRIBUTE] handle 0x%04X", attr->handle);
|
2021-05-03 17:28:01 +02:00
|
|
|
|
|
|
|
if (params->type == BT_GATT_DISCOVER_INCLUDE) {
|
|
|
|
struct bt_gatt_include *include = (struct bt_gatt_include *)attr->user_data;
|
|
|
|
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("Include UUID %s", bt_uuid_str(include->uuid));
|
2021-05-03 17:28:01 +02:00
|
|
|
|
|
|
|
if (bt_uuid_cmp(include->uuid, BT_UUID_AICS) == 0 &&
|
2022-06-21 19:49:13 +02:00
|
|
|
mic_ctlr->aics_inst_cnt < CONFIG_BT_MICP_MIC_CTLR_MAX_AICS_INST) {
|
2021-05-03 17:28:01 +02:00
|
|
|
uint8_t inst_idx;
|
|
|
|
int err;
|
|
|
|
struct bt_aics_discover_param param = {
|
|
|
|
.start_handle = include->start_handle,
|
|
|
|
.end_handle = include->end_handle,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Update discover params so we can continue where we
|
|
|
|
* left off after bt_aics_discover
|
|
|
|
*/
|
2022-06-21 19:49:13 +02:00
|
|
|
mic_ctlr->discover_params.start_handle = attr->handle + 1;
|
2021-05-03 17:28:01 +02:00
|
|
|
|
2022-06-21 19:49:13 +02:00
|
|
|
inst_idx = mic_ctlr->aics_inst_cnt++;
|
|
|
|
err = bt_aics_discover(conn, mic_ctlr->aics[inst_idx],
|
2021-05-03 17:28:01 +02:00
|
|
|
¶m);
|
|
|
|
if (err != 0) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("AICS Discover failed (err %d)", err);
|
2022-06-21 17:45:11 +02:00
|
|
|
if (micp_mic_ctlr_cb != NULL &&
|
|
|
|
micp_mic_ctlr_cb->discover != NULL) {
|
2022-06-21 19:49:13 +02:00
|
|
|
micp_mic_ctlr_cb->discover(mic_ctlr, err,
|
2021-06-08 16:42:37 +02:00
|
|
|
0);
|
2021-05-03 17:28:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return BT_GATT_ITER_STOP;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return BT_GATT_ITER_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief This will discover all characteristics on the server, retrieving the
|
|
|
|
* handles of the writeable characteristics and subscribing to all notify and
|
|
|
|
* indicate characteristics.
|
|
|
|
*/
|
2022-06-17 10:24:03 +02:00
|
|
|
static uint8_t micp_discover_func(struct bt_conn *conn,
|
2021-05-03 17:28:01 +02:00
|
|
|
const struct bt_gatt_attr *attr,
|
|
|
|
struct bt_gatt_discover_params *params)
|
|
|
|
{
|
2022-06-21 19:49:13 +02:00
|
|
|
struct bt_micp_mic_ctlr *mic_ctlr = &mic_ctlrs[bt_conn_index(conn)];
|
2021-05-03 17:28:01 +02:00
|
|
|
|
|
|
|
if (attr == NULL) {
|
|
|
|
int err = 0;
|
|
|
|
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("Discovery complete");
|
2021-05-03 17:28:01 +02:00
|
|
|
(void)memset(params, 0, sizeof(*params));
|
2022-06-21 17:45:11 +02:00
|
|
|
if (CONFIG_BT_MICP_MIC_CTLR_MAX_AICS_INST > 0) {
|
2021-05-03 17:28:01 +02:00
|
|
|
/* Discover included services */
|
2022-06-21 19:49:13 +02:00
|
|
|
mic_ctlr->discover_params.start_handle = mic_ctlr->start_handle;
|
|
|
|
mic_ctlr->discover_params.end_handle = mic_ctlr->end_handle;
|
|
|
|
mic_ctlr->discover_params.type = BT_GATT_DISCOVER_INCLUDE;
|
|
|
|
mic_ctlr->discover_params.func = micp_discover_include_func;
|
2021-05-03 17:28:01 +02:00
|
|
|
|
|
|
|
err = bt_gatt_discover(conn,
|
2022-06-21 19:49:13 +02:00
|
|
|
&mic_ctlr->discover_params);
|
2021-05-03 17:28:01 +02:00
|
|
|
if (err != 0) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("Discover AICS failed (err %d)", err);
|
2022-06-21 17:45:11 +02:00
|
|
|
if (micp_mic_ctlr_cb != NULL &&
|
|
|
|
micp_mic_ctlr_cb->discover != NULL) {
|
2022-06-21 19:49:13 +02:00
|
|
|
micp_mic_ctlr_cb->discover(mic_ctlr, err, 0);
|
2021-05-03 17:28:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2022-06-21 17:45:11 +02:00
|
|
|
if (micp_mic_ctlr_cb != NULL &&
|
|
|
|
micp_mic_ctlr_cb->discover != NULL) {
|
2022-06-21 19:49:13 +02:00
|
|
|
micp_mic_ctlr_cb->discover(mic_ctlr, err, 0);
|
2021-05-03 17:28:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return BT_GATT_ITER_STOP;
|
|
|
|
}
|
|
|
|
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("[ATTRIBUTE] handle 0x%04X", attr->handle);
|
2021-05-03 17:28:01 +02:00
|
|
|
|
|
|
|
if (params->type == BT_GATT_DISCOVER_CHARACTERISTIC) {
|
|
|
|
struct bt_gatt_chrc *chrc = (struct bt_gatt_chrc *)attr->user_data;
|
|
|
|
struct bt_gatt_subscribe_params *sub_params = NULL;
|
|
|
|
|
|
|
|
if (bt_uuid_cmp(chrc->uuid, BT_UUID_MICS_MUTE) == 0) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("Mute");
|
2022-06-21 19:49:13 +02:00
|
|
|
mic_ctlr->mute_handle = chrc->value_handle;
|
|
|
|
sub_params = &mic_ctlr->mute_sub_params;
|
|
|
|
sub_params->disc_params = &mic_ctlr->mute_sub_disc_params;
|
2021-05-03 17:28:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (sub_params != NULL) {
|
|
|
|
int err;
|
|
|
|
|
|
|
|
/* With ccc_handle == 0 it will use auto discovery */
|
|
|
|
sub_params->ccc_handle = 0;
|
2022-06-21 19:49:13 +02:00
|
|
|
sub_params->end_handle = mic_ctlr->end_handle;
|
2021-05-03 17:28:01 +02:00
|
|
|
sub_params->value = BT_GATT_CCC_NOTIFY;
|
|
|
|
sub_params->value_handle = chrc->value_handle;
|
|
|
|
sub_params->notify = mute_notify_handler;
|
|
|
|
|
|
|
|
err = bt_gatt_subscribe(conn, sub_params);
|
|
|
|
if (err == 0) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("Subscribed to handle 0x%04X", attr->handle);
|
2021-05-03 17:28:01 +02:00
|
|
|
} else {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("Could not subscribe to handle 0x%04X: %d", attr->handle,
|
|
|
|
err);
|
2021-05-03 17:28:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return BT_GATT_ITER_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t primary_discover_func(struct bt_conn *conn,
|
|
|
|
const struct bt_gatt_attr *attr,
|
|
|
|
struct bt_gatt_discover_params *params)
|
|
|
|
{
|
2022-06-21 19:49:13 +02:00
|
|
|
struct bt_micp_mic_ctlr *mic_ctlr = &mic_ctlrs[bt_conn_index(conn)];
|
2021-05-03 17:28:01 +02:00
|
|
|
|
|
|
|
if (attr == NULL) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("Could not find a MICS instance on the server");
|
2022-06-21 17:45:11 +02:00
|
|
|
if (micp_mic_ctlr_cb != NULL &&
|
|
|
|
micp_mic_ctlr_cb->discover != NULL) {
|
2022-06-21 19:49:13 +02:00
|
|
|
micp_mic_ctlr_cb->discover(mic_ctlr, -ENODATA, 0);
|
2021-05-03 17:28:01 +02:00
|
|
|
}
|
|
|
|
return BT_GATT_ITER_STOP;
|
|
|
|
}
|
|
|
|
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("[ATTRIBUTE] handle 0x%04X", attr->handle);
|
2021-05-03 17:28:01 +02:00
|
|
|
|
|
|
|
if (params->type == BT_GATT_DISCOVER_PRIMARY) {
|
|
|
|
struct bt_gatt_service_val *prim_service =
|
|
|
|
(struct bt_gatt_service_val *)attr->user_data;
|
|
|
|
int err;
|
|
|
|
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("Primary discover complete");
|
2022-06-21 19:49:13 +02:00
|
|
|
mic_ctlr->start_handle = attr->handle + 1;
|
|
|
|
mic_ctlr->end_handle = prim_service->end_handle;
|
2021-05-03 17:28:01 +02:00
|
|
|
|
|
|
|
/* Discover characteristics */
|
2022-06-21 19:49:13 +02:00
|
|
|
mic_ctlr->discover_params.uuid = NULL;
|
|
|
|
mic_ctlr->discover_params.start_handle = mic_ctlr->start_handle;
|
|
|
|
mic_ctlr->discover_params.end_handle = mic_ctlr->end_handle;
|
|
|
|
mic_ctlr->discover_params.type = BT_GATT_DISCOVER_CHARACTERISTIC;
|
|
|
|
mic_ctlr->discover_params.func = micp_discover_func;
|
2021-05-03 17:28:01 +02:00
|
|
|
|
2022-06-21 19:49:13 +02:00
|
|
|
err = bt_gatt_discover(conn, &mic_ctlr->discover_params);
|
2021-05-03 17:28:01 +02:00
|
|
|
if (err != 0) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("Discover failed (err %d)", err);
|
2022-06-21 17:45:11 +02:00
|
|
|
if (micp_mic_ctlr_cb != NULL &&
|
|
|
|
micp_mic_ctlr_cb->discover != NULL) {
|
2022-06-21 19:49:13 +02:00
|
|
|
micp_mic_ctlr_cb->discover(mic_ctlr, err, 0);
|
2021-05-03 17:28:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return BT_GATT_ITER_STOP;
|
|
|
|
}
|
|
|
|
|
|
|
|
return BT_GATT_ITER_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2022-06-21 19:49:13 +02:00
|
|
|
static void micp_mic_ctlr_reset(struct bt_micp_mic_ctlr *mic_ctlr)
|
2021-05-03 17:28:01 +02:00
|
|
|
{
|
2022-06-21 19:49:13 +02:00
|
|
|
mic_ctlr->start_handle = 0;
|
|
|
|
mic_ctlr->end_handle = 0;
|
|
|
|
mic_ctlr->mute_handle = 0;
|
|
|
|
mic_ctlr->aics_inst_cnt = 0;
|
2021-05-03 17:28:01 +02:00
|
|
|
|
2022-06-21 19:49:13 +02:00
|
|
|
if (mic_ctlr->conn != NULL) {
|
|
|
|
struct bt_conn *conn = mic_ctlr->conn;
|
2022-03-07 19:50:22 +01:00
|
|
|
|
|
|
|
/* It's okay if this fails. In case of disconnect, we can't
|
|
|
|
* unsubscribe and it will just fail.
|
|
|
|
* In case that we reset due to another call of the discover
|
|
|
|
* function, we will unsubscribe (regardless of bonding state)
|
|
|
|
* to accommodate the new discovery values.
|
|
|
|
*/
|
2022-06-21 19:49:13 +02:00
|
|
|
(void)bt_gatt_unsubscribe(conn, &mic_ctlr->mute_sub_params);
|
2022-03-07 19:50:22 +01:00
|
|
|
|
|
|
|
bt_conn_unref(conn);
|
2022-06-21 19:49:13 +02:00
|
|
|
mic_ctlr->conn = NULL;
|
2021-06-08 16:42:37 +02:00
|
|
|
}
|
2021-05-03 17:28:01 +02:00
|
|
|
}
|
|
|
|
|
2022-03-07 19:50:22 +01:00
|
|
|
static void disconnected(struct bt_conn *conn, uint8_t reason)
|
|
|
|
{
|
2022-06-21 19:49:13 +02:00
|
|
|
struct bt_micp_mic_ctlr *mic_ctlr = &mic_ctlrs[bt_conn_index(conn)];
|
2022-03-07 19:50:22 +01:00
|
|
|
|
2022-06-21 19:49:13 +02:00
|
|
|
if (mic_ctlr->conn == conn) {
|
|
|
|
micp_mic_ctlr_reset(mic_ctlr);
|
2022-03-07 19:50:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BT_CONN_CB_DEFINE(conn_callbacks) = {
|
|
|
|
.disconnected = disconnected,
|
|
|
|
};
|
|
|
|
|
2022-06-21 19:49:13 +02:00
|
|
|
int bt_micp_mic_ctlr_discover(struct bt_conn *conn, struct bt_micp_mic_ctlr **mic_ctlr_out)
|
2021-05-03 17:28:01 +02:00
|
|
|
{
|
2022-06-21 19:49:13 +02:00
|
|
|
struct bt_micp_mic_ctlr *mic_ctlr;
|
2021-06-08 13:42:11 +02:00
|
|
|
int err;
|
|
|
|
|
2021-05-03 17:28:01 +02:00
|
|
|
/*
|
|
|
|
* This will initiate a discover procedure. The procedure will do the
|
|
|
|
* following sequence:
|
|
|
|
* 1) Primary discover for the MICS
|
|
|
|
* 2) Characteristic discover of the MICS
|
|
|
|
* 3) Discover services included in MICS (AICS)
|
2022-03-16 21:07:43 +00:00
|
|
|
* 4) For each included service found; discovery of the characteristics
|
2021-05-03 17:28:01 +02:00
|
|
|
* 5) When everything above have been discovered, the callback is called
|
|
|
|
*/
|
|
|
|
|
|
|
|
CHECKIF(conn == NULL) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("NULL conn");
|
2021-05-03 17:28:01 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2022-06-21 19:49:13 +02:00
|
|
|
mic_ctlr = &mic_ctlrs[bt_conn_index(conn)];
|
2021-05-03 17:28:01 +02:00
|
|
|
|
2022-06-21 19:49:13 +02:00
|
|
|
(void)memset(&mic_ctlr->discover_params, 0,
|
|
|
|
sizeof(mic_ctlr->discover_params));
|
|
|
|
micp_mic_ctlr_reset(mic_ctlr);
|
2021-05-03 17:28:01 +02:00
|
|
|
|
2022-06-21 17:45:11 +02:00
|
|
|
#if defined(CONFIG_BT_MICP_MIC_CTLR_AICS)
|
2022-06-20 14:03:05 +02:00
|
|
|
static bool initialized;
|
2021-05-03 17:28:01 +02:00
|
|
|
|
2022-06-20 14:03:05 +02:00
|
|
|
if (!initialized) {
|
2022-06-21 19:49:13 +02:00
|
|
|
for (int i = 0; i < ARRAY_SIZE(mic_ctlr->aics); i++) {
|
|
|
|
mic_ctlr->aics[i] = bt_aics_client_free_instance_get();
|
2021-05-03 17:28:01 +02:00
|
|
|
|
2022-06-21 19:49:13 +02:00
|
|
|
if (mic_ctlr->aics[i] == NULL) {
|
2022-06-20 14:03:05 +02:00
|
|
|
return -ENOMEM;
|
2021-05-03 17:28:01 +02:00
|
|
|
}
|
2022-06-20 14:03:05 +02:00
|
|
|
|
2022-06-21 19:49:13 +02:00
|
|
|
bt_aics_client_cb_register(mic_ctlr->aics[i],
|
2022-06-21 17:45:11 +02:00
|
|
|
&micp_mic_ctlr_cb->aics_cb);
|
2021-05-03 17:28:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-20 14:03:05 +02:00
|
|
|
initialized = true;
|
2022-06-21 17:45:11 +02:00
|
|
|
#endif /* CONFIG_BT_MICP_MIC_CTLR_AICS */
|
2022-06-20 14:03:05 +02:00
|
|
|
|
2022-06-21 19:49:13 +02:00
|
|
|
mic_ctlr->conn = bt_conn_ref(conn);
|
|
|
|
mic_ctlr->discover_params.func = primary_discover_func;
|
|
|
|
mic_ctlr->discover_params.uuid = mics_uuid;
|
|
|
|
mic_ctlr->discover_params.type = BT_GATT_DISCOVER_PRIMARY;
|
|
|
|
mic_ctlr->discover_params.start_handle = BT_ATT_FIRST_ATTRIBUTE_HANDLE;
|
|
|
|
mic_ctlr->discover_params.end_handle = BT_ATT_LAST_ATTRIBUTE_HANDLE;
|
2021-05-03 17:28:01 +02:00
|
|
|
|
2022-06-21 19:49:13 +02:00
|
|
|
err = bt_gatt_discover(conn, &mic_ctlr->discover_params);
|
2021-06-08 13:42:11 +02:00
|
|
|
if (err == 0) {
|
2022-06-21 19:49:13 +02:00
|
|
|
*mic_ctlr_out = mic_ctlr;
|
2021-06-08 13:42:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
2021-05-03 17:28:01 +02:00
|
|
|
}
|
|
|
|
|
2022-06-21 17:45:11 +02:00
|
|
|
int bt_micp_mic_ctlr_cb_register(struct bt_micp_mic_ctlr_cb *cb)
|
2021-05-03 17:28:01 +02:00
|
|
|
{
|
2022-06-21 17:45:11 +02:00
|
|
|
#if defined(CONFIG_BT_MICP_MIC_CTLR_AICS)
|
2021-07-17 15:57:13 +02:00
|
|
|
struct bt_aics_cb *aics_cb = NULL;
|
2021-05-03 17:28:01 +02:00
|
|
|
|
2021-07-17 15:57:13 +02:00
|
|
|
if (cb != NULL) {
|
2022-08-09 14:26:15 +02:00
|
|
|
/* Ensure that the cb->aics_cb.discover is the aics_discover_cb */
|
|
|
|
CHECKIF(cb->aics_cb.discover != NULL &&
|
|
|
|
cb->aics_cb.discover != aics_discover_cb) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_ERR("AICS discover callback shall not be set");
|
2021-07-17 15:57:13 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
cb->aics_cb.discover = aics_discover_cb;
|
2021-05-03 17:28:01 +02:00
|
|
|
|
2021-07-17 15:57:13 +02:00
|
|
|
aics_cb = &cb->aics_cb;
|
|
|
|
}
|
2021-05-03 17:28:01 +02:00
|
|
|
|
2022-06-21 19:49:13 +02:00
|
|
|
for (int i = 0; i < ARRAY_SIZE(mic_ctlrs); i++) {
|
|
|
|
for (int j = 0; j < ARRAY_SIZE(mic_ctlrs[i].aics); j++) {
|
|
|
|
struct bt_aics *aics = mic_ctlrs[i].aics[j];
|
2021-05-03 17:28:01 +02:00
|
|
|
|
2021-07-17 15:57:13 +02:00
|
|
|
if (aics != NULL) {
|
|
|
|
bt_aics_client_cb_register(aics, aics_cb);
|
2021-05-03 17:28:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-06-21 17:45:11 +02:00
|
|
|
#endif /* CONFIG_BT_MICP_MIC_CTLR_AICS */
|
2021-05-03 17:28:01 +02:00
|
|
|
|
2022-06-21 17:45:11 +02:00
|
|
|
micp_mic_ctlr_cb = cb;
|
2021-05-03 17:28:01 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-06-21 19:49:13 +02:00
|
|
|
int bt_micp_mic_ctlr_included_get(struct bt_micp_mic_ctlr *mic_ctlr,
|
2022-06-21 17:45:11 +02:00
|
|
|
struct bt_micp_included *included)
|
2021-05-03 17:28:01 +02:00
|
|
|
{
|
2022-06-21 19:49:13 +02:00
|
|
|
CHECKIF(mic_ctlr == NULL) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("NULL mic_ctlr");
|
2021-05-03 17:28:01 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2021-06-07 13:01:54 +02:00
|
|
|
CHECKIF(included == NULL) {
|
2021-05-03 17:28:01 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2022-06-21 19:49:13 +02:00
|
|
|
included->aics_cnt = mic_ctlr->aics_inst_cnt;
|
|
|
|
included->aics = mic_ctlr->aics;
|
2021-05-03 17:28:01 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-06-21 19:49:13 +02:00
|
|
|
int bt_micp_mic_ctlr_conn_get(const struct bt_micp_mic_ctlr *mic_ctlr, struct bt_conn **conn)
|
2021-06-09 10:20:00 +02:00
|
|
|
{
|
2022-06-21 19:49:13 +02:00
|
|
|
CHECKIF(mic_ctlr == NULL) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("NULL mic_ctlr pointer");
|
2021-06-09 10:20:00 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2022-06-21 19:49:13 +02:00
|
|
|
if (mic_ctlr->conn == NULL) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("mic_ctlr pointer not associated with a connection. "
|
2021-06-09 10:20:00 +02:00
|
|
|
"Do discovery first");
|
|
|
|
return -ENOTCONN;
|
|
|
|
}
|
|
|
|
|
2022-06-21 19:49:13 +02:00
|
|
|
*conn = mic_ctlr->conn;
|
2021-06-09 10:20:00 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-06-21 19:49:13 +02:00
|
|
|
int bt_micp_mic_ctlr_mute_get(struct bt_micp_mic_ctlr *mic_ctlr)
|
2021-05-03 17:28:01 +02:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
2022-06-21 19:49:13 +02:00
|
|
|
CHECKIF(mic_ctlr == NULL) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("NULL mic_ctlr");
|
2021-05-03 17:28:01 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2022-06-21 19:49:13 +02:00
|
|
|
if (mic_ctlr->mute_handle == 0) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("Handle not set");
|
2021-05-03 17:28:01 +02:00
|
|
|
return -EINVAL;
|
2022-06-21 19:49:13 +02:00
|
|
|
} else if (mic_ctlr->busy) {
|
2021-05-03 17:28:01 +02:00
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
2022-06-21 19:49:13 +02:00
|
|
|
mic_ctlr->read_params.func = micp_mic_ctlr_read_mute_cb;
|
|
|
|
mic_ctlr->read_params.handle_count = 1;
|
|
|
|
mic_ctlr->read_params.single.handle = mic_ctlr->mute_handle;
|
|
|
|
mic_ctlr->read_params.single.offset = 0U;
|
2021-05-03 17:28:01 +02:00
|
|
|
|
2022-06-21 19:49:13 +02:00
|
|
|
err = bt_gatt_read(mic_ctlr->conn, &mic_ctlr->read_params);
|
2021-05-03 17:28:01 +02:00
|
|
|
if (err == 0) {
|
2022-06-21 19:49:13 +02:00
|
|
|
mic_ctlr->busy = true;
|
2021-05-03 17:28:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2022-06-21 19:49:13 +02:00
|
|
|
int bt_micp_mic_ctlr_write_mute(struct bt_micp_mic_ctlr *mic_ctlr, bool mute)
|
2021-05-03 17:28:01 +02:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
2022-06-21 19:49:13 +02:00
|
|
|
CHECKIF(mic_ctlr == NULL) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("NULL mic_ctlr");
|
2021-05-03 17:28:01 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2022-06-21 19:49:13 +02:00
|
|
|
if (mic_ctlr->mute_handle == 0) {
|
2022-11-02 14:31:13 +01:00
|
|
|
LOG_DBG("Handle not set");
|
2021-05-03 17:28:01 +02:00
|
|
|
return -EINVAL;
|
2022-06-21 19:49:13 +02:00
|
|
|
} else if (mic_ctlr->busy) {
|
2021-05-03 17:28:01 +02:00
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
2022-06-21 19:49:13 +02:00
|
|
|
mic_ctlr->mute_val_buf[0] = mute;
|
|
|
|
mic_ctlr->write_params.offset = 0;
|
|
|
|
mic_ctlr->write_params.data = mic_ctlr->mute_val_buf;
|
|
|
|
mic_ctlr->write_params.length = sizeof(mute);
|
|
|
|
mic_ctlr->write_params.handle = mic_ctlr->mute_handle;
|
|
|
|
mic_ctlr->write_params.func = micp_mic_ctlr_write_mics_mute_cb;
|
2021-05-03 17:28:01 +02:00
|
|
|
|
2022-06-21 19:49:13 +02:00
|
|
|
err = bt_gatt_write(mic_ctlr->conn, &mic_ctlr->write_params);
|
2021-05-03 17:28:01 +02:00
|
|
|
if (err == 0) {
|
2022-06-21 19:49:13 +02:00
|
|
|
mic_ctlr->busy = true;
|
2021-05-03 17:28:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2022-06-21 19:49:13 +02:00
|
|
|
int bt_micp_mic_ctlr_mute(struct bt_micp_mic_ctlr *mic_ctlr)
|
2021-05-03 17:28:01 +02:00
|
|
|
{
|
2022-06-21 19:49:13 +02:00
|
|
|
return bt_micp_mic_ctlr_write_mute(mic_ctlr, true);
|
2021-05-03 17:28:01 +02:00
|
|
|
}
|
|
|
|
|
2022-06-21 19:49:13 +02:00
|
|
|
int bt_micp_mic_ctlr_unmute(struct bt_micp_mic_ctlr *mic_ctlr)
|
2021-05-03 17:28:01 +02:00
|
|
|
{
|
2022-06-21 19:49:13 +02:00
|
|
|
return bt_micp_mic_ctlr_write_mute(mic_ctlr, false);
|
2021-05-03 17:28:01 +02:00
|
|
|
}
|