2022-06-17 10:24:03 +02:00
|
|
|
/* Bluetooth MICP client - Microphone Control Profile - Client */
|
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
|
|
|
|
*/
|
|
|
|
|
2022-05-06 11:12:04 +02:00
|
|
|
#include <zephyr/zephyr.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>
|
2021-05-03 17:28:01 +02:00
|
|
|
|
2022-06-17 10:24:03 +02:00
|
|
|
#include "micp_internal.h"
|
2021-06-08 13:37:46 +02:00
|
|
|
|
2022-06-17 10:24:03 +02:00
|
|
|
#define BT_DBG_ENABLED IS_ENABLED(CONFIG_BT_DEBUG_MICP_CLIENT)
|
|
|
|
#define LOG_MODULE_NAME bt_micp_client
|
2021-05-03 17:28:01 +02:00
|
|
|
#include "common/log.h"
|
|
|
|
|
|
|
|
/* Callback functions */
|
2022-06-17 10:24:03 +02:00
|
|
|
static struct bt_micp_cb *micp_client_cb;
|
2021-05-03 17:28:01 +02:00
|
|
|
|
2022-06-17 10:24:03 +02:00
|
|
|
static struct bt_micp micp_insts[CONFIG_BT_MAX_CONN];
|
2021-05-03 17:28:01 +02:00
|
|
|
static struct bt_uuid *mics_uuid = BT_UUID_MICS;
|
|
|
|
|
|
|
|
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-17 10:24:03 +02:00
|
|
|
struct bt_micp *micp_inst;
|
2021-12-03 16:33:22 +01:00
|
|
|
|
|
|
|
if (conn == NULL) {
|
|
|
|
return BT_GATT_ITER_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2022-06-17 10:24:03 +02:00
|
|
|
micp_inst = &micp_insts[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;
|
|
|
|
BT_DBG("Mute %u", *mute_val);
|
2022-06-17 10:24:03 +02:00
|
|
|
if (micp_client_cb != NULL &&
|
|
|
|
micp_client_cb->mute != NULL) {
|
|
|
|
micp_client_cb->mute(micp_inst, 0, *mute_val);
|
2021-05-03 17:28:01 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
BT_DBG("Invalid length %u (expected %zu)",
|
|
|
|
length, sizeof(*mute_val));
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return BT_GATT_ITER_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2022-06-17 10:24:03 +02:00
|
|
|
static uint8_t micp_client_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-17 10:24:03 +02:00
|
|
|
struct bt_micp *micp_inst = &micp_insts[bt_conn_index(conn)];
|
2021-05-03 17:28:01 +02:00
|
|
|
|
2022-06-17 10:24:03 +02:00
|
|
|
micp_inst->cli.busy = false;
|
2021-05-03 17:28:01 +02:00
|
|
|
|
|
|
|
if (err > 0) {
|
|
|
|
BT_DBG("err: 0x%02X", err);
|
|
|
|
} else if (data != NULL) {
|
2021-06-24 13:10:11 +02:00
|
|
|
if (length == sizeof(mute_val)) {
|
|
|
|
mute_val = ((uint8_t *)data)[0];
|
|
|
|
BT_DBG("Mute %u", mute_val);
|
2021-05-03 17:28:01 +02:00
|
|
|
} else {
|
|
|
|
BT_DBG("Invalid length %u (expected %zu)",
|
2021-06-24 13:10:11 +02:00
|
|
|
length, sizeof(mute_val));
|
2021-05-03 17:28:01 +02:00
|
|
|
cb_err = BT_ATT_ERR_INVALID_ATTRIBUTE_LEN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-17 10:24:03 +02:00
|
|
|
if (micp_client_cb != NULL && micp_client_cb->mute != NULL) {
|
|
|
|
micp_client_cb->mute(micp_inst, cb_err, mute_val);
|
2021-05-03 17:28:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return BT_GATT_ITER_STOP;
|
|
|
|
}
|
|
|
|
|
2022-06-17 10:24:03 +02:00
|
|
|
static void micp_client_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-17 10:24:03 +02:00
|
|
|
struct bt_micp *micp_inst = &micp_insts[bt_conn_index(conn)];
|
|
|
|
uint8_t mute_val = micp_inst->cli.mute_val_buf[0];
|
2021-05-03 17:28:01 +02:00
|
|
|
|
|
|
|
BT_DBG("Write %s (0x%02X)", err ? "failed" : "successful", err);
|
|
|
|
|
2022-06-17 10:24:03 +02:00
|
|
|
micp_inst->cli.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) {
|
|
|
|
if (micp_client_cb != NULL &&
|
|
|
|
micp_client_cb->unmute_write != NULL) {
|
|
|
|
micp_client_cb->unmute_write(micp_inst, err);
|
2021-05-03 17:28:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2022-06-17 10:24:03 +02:00
|
|
|
if (micp_client_cb != NULL &&
|
|
|
|
micp_client_cb->mute_write != NULL) {
|
|
|
|
micp_client_cb->mute_write(micp_inst, err);
|
2021-05-03 17:28:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-17 10:24:03 +02:00
|
|
|
#if defined(CONFIG_BT_MICP_CLIENT_AICS)
|
|
|
|
static struct bt_micp *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-17 10:24:03 +02:00
|
|
|
for (int i = 0; i < ARRAY_SIZE(micp_insts); i++) {
|
|
|
|
for (int j = 0; j < ARRAY_SIZE(micp_insts[i].cli.aics); j++) {
|
|
|
|
if (micp_insts[i].cli.aics[j] == aics) {
|
|
|
|
return &micp_insts[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-17 10:24:03 +02:00
|
|
|
struct bt_micp *micp_inst = lookup_micp_by_aics(inst);
|
2021-05-03 17:28:01 +02:00
|
|
|
|
|
|
|
if (err == 0) {
|
|
|
|
/* Continue discovery of included services */
|
2022-06-17 10:24:03 +02:00
|
|
|
err = bt_gatt_discover(micp_inst->cli.conn,
|
|
|
|
&micp_inst->cli.discover_params);
|
2021-05-03 17:28:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (err != 0) {
|
|
|
|
BT_DBG("Discover failed (err %d)", err);
|
2022-06-17 10:24:03 +02:00
|
|
|
if (micp_client_cb != NULL &&
|
|
|
|
micp_client_cb->discover != NULL) {
|
|
|
|
micp_client_cb->discover(micp_inst, err, 0);
|
2021-05-03 17:28:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-06-17 10:24:03 +02:00
|
|
|
#endif /* CONFIG_BT_MICP_CLIENT_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-17 10:24:03 +02:00
|
|
|
struct bt_micp *micp_inst = &micp_insts[bt_conn_index(conn)];
|
2021-05-03 17:28:01 +02:00
|
|
|
|
|
|
|
if (attr == NULL) {
|
2022-06-17 10:24:03 +02:00
|
|
|
BT_DBG("Discover include complete for MICP: %u AICS",
|
|
|
|
micp_inst->cli.aics_inst_cnt);
|
2021-05-03 17:28:01 +02:00
|
|
|
(void)memset(params, 0, sizeof(*params));
|
|
|
|
|
2022-06-17 10:24:03 +02:00
|
|
|
if (micp_client_cb != NULL &&
|
|
|
|
micp_client_cb->discover != NULL) {
|
|
|
|
micp_client_cb->discover(micp_inst, 0, 0);
|
2021-05-03 17:28:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return BT_GATT_ITER_STOP;
|
|
|
|
}
|
|
|
|
|
|
|
|
BT_DBG("[ATTRIBUTE] handle 0x%04X", attr->handle);
|
|
|
|
|
|
|
|
if (params->type == BT_GATT_DISCOVER_INCLUDE) {
|
|
|
|
struct bt_gatt_include *include = (struct bt_gatt_include *)attr->user_data;
|
|
|
|
|
|
|
|
BT_DBG("Include UUID %s", bt_uuid_str(include->uuid));
|
|
|
|
|
|
|
|
if (bt_uuid_cmp(include->uuid, BT_UUID_AICS) == 0 &&
|
2022-06-17 10:24:03 +02:00
|
|
|
micp_inst->cli.aics_inst_cnt < CONFIG_BT_MICP_CLIENT_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-17 10:24:03 +02:00
|
|
|
micp_inst->cli.discover_params.start_handle = attr->handle + 1;
|
2021-05-03 17:28:01 +02:00
|
|
|
|
2022-06-17 10:24:03 +02:00
|
|
|
inst_idx = micp_inst->cli.aics_inst_cnt++;
|
|
|
|
err = bt_aics_discover(conn, micp_inst->cli.aics[inst_idx],
|
2021-05-03 17:28:01 +02:00
|
|
|
¶m);
|
|
|
|
if (err != 0) {
|
|
|
|
BT_DBG("AICS Discover failed (err %d)", err);
|
2022-06-17 10:24:03 +02:00
|
|
|
if (micp_client_cb != NULL &&
|
|
|
|
micp_client_cb->discover != NULL) {
|
|
|
|
micp_client_cb->discover(micp_inst, 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-17 10:24:03 +02:00
|
|
|
struct bt_micp *micp_inst = &micp_insts[bt_conn_index(conn)];
|
2021-05-03 17:28:01 +02:00
|
|
|
|
|
|
|
if (attr == NULL) {
|
|
|
|
int err = 0;
|
|
|
|
|
2022-06-17 10:24:03 +02:00
|
|
|
BT_DBG("Setup complete for MICP");
|
2021-05-03 17:28:01 +02:00
|
|
|
(void)memset(params, 0, sizeof(*params));
|
2022-06-17 10:24:03 +02:00
|
|
|
if (CONFIG_BT_MICP_CLIENT_MAX_AICS_INST > 0) {
|
2021-05-03 17:28:01 +02:00
|
|
|
/* Discover included services */
|
2022-06-17 10:24:03 +02:00
|
|
|
micp_inst->cli.discover_params.start_handle = micp_inst->cli.start_handle;
|
|
|
|
micp_inst->cli.discover_params.end_handle = micp_inst->cli.end_handle;
|
|
|
|
micp_inst->cli.discover_params.type = BT_GATT_DISCOVER_INCLUDE;
|
|
|
|
micp_inst->cli.discover_params.func = micp_discover_include_func;
|
2021-05-03 17:28:01 +02:00
|
|
|
|
|
|
|
err = bt_gatt_discover(conn,
|
2022-06-17 10:24:03 +02:00
|
|
|
&micp_inst->cli.discover_params);
|
2021-05-03 17:28:01 +02:00
|
|
|
if (err != 0) {
|
|
|
|
BT_DBG("Discover failed (err %d)", err);
|
2022-06-17 10:24:03 +02:00
|
|
|
if (micp_client_cb != NULL &&
|
|
|
|
micp_client_cb->discover != NULL) {
|
|
|
|
micp_client_cb->discover(micp_inst, err, 0);
|
2021-05-03 17:28:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2022-06-17 10:24:03 +02:00
|
|
|
if (micp_client_cb != NULL &&
|
|
|
|
micp_client_cb->discover != NULL) {
|
|
|
|
micp_client_cb->discover(micp_inst, err, 0);
|
2021-05-03 17:28:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return BT_GATT_ITER_STOP;
|
|
|
|
}
|
|
|
|
|
|
|
|
BT_DBG("[ATTRIBUTE] handle 0x%04X", attr->handle);
|
|
|
|
|
|
|
|
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) {
|
|
|
|
BT_DBG("Mute");
|
2022-06-17 10:24:03 +02:00
|
|
|
micp_inst->cli.mute_handle = chrc->value_handle;
|
|
|
|
sub_params = &micp_inst->cli.mute_sub_params;
|
|
|
|
sub_params->disc_params = &micp_inst->cli.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-17 10:24:03 +02:00
|
|
|
sub_params->end_handle = micp_inst->cli.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) {
|
|
|
|
BT_DBG("Subscribed to handle 0x%04X",
|
|
|
|
attr->handle);
|
|
|
|
} else {
|
|
|
|
BT_DBG("Could not subscribe to handle 0x%04X: %d",
|
|
|
|
attr->handle, err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-17 10:24:03 +02:00
|
|
|
struct bt_micp *micp_inst = &micp_insts[bt_conn_index(conn)];
|
2021-05-03 17:28:01 +02:00
|
|
|
|
|
|
|
if (attr == NULL) {
|
|
|
|
BT_DBG("Could not find a MICS instance on the server");
|
2022-06-17 10:24:03 +02:00
|
|
|
if (micp_client_cb != NULL &&
|
|
|
|
micp_client_cb->discover != NULL) {
|
|
|
|
micp_client_cb->discover(micp_inst, -ENODATA, 0);
|
2021-05-03 17:28:01 +02:00
|
|
|
}
|
|
|
|
return BT_GATT_ITER_STOP;
|
|
|
|
}
|
|
|
|
|
|
|
|
BT_DBG("[ATTRIBUTE] handle 0x%04X", attr->handle);
|
|
|
|
|
|
|
|
if (params->type == BT_GATT_DISCOVER_PRIMARY) {
|
|
|
|
struct bt_gatt_service_val *prim_service =
|
|
|
|
(struct bt_gatt_service_val *)attr->user_data;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
BT_DBG("Primary discover complete");
|
2022-06-17 10:24:03 +02:00
|
|
|
micp_inst->cli.start_handle = attr->handle + 1;
|
|
|
|
micp_inst->cli.end_handle = prim_service->end_handle;
|
2021-05-03 17:28:01 +02:00
|
|
|
|
|
|
|
/* Discover characteristics */
|
2022-06-17 10:24:03 +02:00
|
|
|
micp_inst->cli.discover_params.uuid = NULL;
|
|
|
|
micp_inst->cli.discover_params.start_handle = micp_inst->cli.start_handle;
|
|
|
|
micp_inst->cli.discover_params.end_handle = micp_inst->cli.end_handle;
|
|
|
|
micp_inst->cli.discover_params.type = BT_GATT_DISCOVER_CHARACTERISTIC;
|
|
|
|
micp_inst->cli.discover_params.func = micp_discover_func;
|
2021-05-03 17:28:01 +02:00
|
|
|
|
2022-06-17 10:24:03 +02:00
|
|
|
err = bt_gatt_discover(conn, &micp_inst->cli.discover_params);
|
2021-05-03 17:28:01 +02:00
|
|
|
if (err != 0) {
|
|
|
|
BT_DBG("Discover failed (err %d)", err);
|
2022-06-17 10:24:03 +02:00
|
|
|
if (micp_client_cb != NULL &&
|
|
|
|
micp_client_cb->discover != NULL) {
|
|
|
|
micp_client_cb->discover(micp_inst, err, 0);
|
2021-05-03 17:28:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return BT_GATT_ITER_STOP;
|
|
|
|
}
|
|
|
|
|
|
|
|
return BT_GATT_ITER_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2022-06-17 10:24:03 +02:00
|
|
|
static void micp_client_reset(struct bt_micp *micp)
|
2021-05-03 17:28:01 +02:00
|
|
|
{
|
2022-06-17 10:24:03 +02:00
|
|
|
micp->cli.start_handle = 0;
|
|
|
|
micp->cli.end_handle = 0;
|
|
|
|
micp->cli.mute_handle = 0;
|
|
|
|
micp->cli.aics_inst_cnt = 0;
|
2021-05-03 17:28:01 +02:00
|
|
|
|
2022-06-17 10:24:03 +02:00
|
|
|
if (micp->cli.conn != NULL) {
|
|
|
|
struct bt_conn *conn = micp->cli.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-17 10:24:03 +02:00
|
|
|
(void)bt_gatt_unsubscribe(conn, &micp->cli.mute_sub_params);
|
2022-03-07 19:50:22 +01:00
|
|
|
|
|
|
|
bt_conn_unref(conn);
|
2022-06-17 10:24:03 +02:00
|
|
|
micp->cli.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-17 10:24:03 +02:00
|
|
|
struct bt_micp *micp = &micp_insts[bt_conn_index(conn)];
|
2022-03-07 19:50:22 +01:00
|
|
|
|
2022-06-17 10:24:03 +02:00
|
|
|
if (micp->cli.conn == conn) {
|
|
|
|
micp_client_reset(micp);
|
2022-03-07 19:50:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BT_CONN_CB_DEFINE(conn_callbacks) = {
|
|
|
|
.disconnected = disconnected,
|
|
|
|
};
|
|
|
|
|
2022-06-17 10:24:03 +02:00
|
|
|
int bt_micp_discover(struct bt_conn *conn, struct bt_micp **micp)
|
2021-05-03 17:28:01 +02:00
|
|
|
{
|
2022-06-17 10:24:03 +02:00
|
|
|
struct bt_micp *micp_inst;
|
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) {
|
|
|
|
BT_DBG("NULL conn");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2022-06-17 10:24:03 +02:00
|
|
|
micp_inst = &micp_insts[bt_conn_index(conn)];
|
2021-05-03 17:28:01 +02:00
|
|
|
|
2022-06-17 10:24:03 +02:00
|
|
|
(void)memset(&micp_inst->cli.discover_params, 0,
|
|
|
|
sizeof(micp_inst->cli.discover_params));
|
|
|
|
micp_client_reset(micp_inst);
|
2021-05-03 17:28:01 +02:00
|
|
|
|
2022-06-20 14:03:05 +02:00
|
|
|
#if defined(CONFIG_BT_MICP_CLIENT_AICS)
|
|
|
|
static bool initialized;
|
2021-05-03 17:28:01 +02:00
|
|
|
|
2022-06-20 14:03:05 +02:00
|
|
|
if (!initialized) {
|
|
|
|
for (int i = 0; i < ARRAY_SIZE(micp_inst->cli.aics); i++) {
|
|
|
|
micp_inst->cli.aics[i] = bt_aics_client_free_instance_get();
|
2021-05-03 17:28:01 +02:00
|
|
|
|
2022-06-20 14:03:05 +02:00
|
|
|
if (micp_inst->cli.aics[i] == NULL) {
|
|
|
|
return -ENOMEM;
|
2021-05-03 17:28:01 +02:00
|
|
|
}
|
2022-06-20 14:03:05 +02:00
|
|
|
|
|
|
|
bt_aics_client_cb_register(micp_inst->cli.aics[i],
|
|
|
|
&micp_client_cb->aics_cb);
|
2021-05-03 17:28:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-20 14:03:05 +02:00
|
|
|
initialized = true;
|
|
|
|
#endif /* CONFIG_BT_MICP_CLIENT_AICS */
|
|
|
|
|
2022-06-17 10:24:03 +02:00
|
|
|
micp_inst->cli.conn = bt_conn_ref(conn);
|
|
|
|
micp_inst->client_instance = true;
|
|
|
|
micp_inst->cli.discover_params.func = primary_discover_func;
|
|
|
|
micp_inst->cli.discover_params.uuid = mics_uuid;
|
|
|
|
micp_inst->cli.discover_params.type = BT_GATT_DISCOVER_PRIMARY;
|
|
|
|
micp_inst->cli.discover_params.start_handle = BT_ATT_FIRST_ATTRIBUTE_HANDLE;
|
|
|
|
micp_inst->cli.discover_params.end_handle = BT_ATT_LAST_ATTRIBUTE_HANDLE;
|
2021-05-03 17:28:01 +02:00
|
|
|
|
2022-06-17 10:24:03 +02:00
|
|
|
err = bt_gatt_discover(conn, &micp_inst->cli.discover_params);
|
2021-06-08 13:42:11 +02:00
|
|
|
if (err == 0) {
|
2022-06-17 10:24:03 +02:00
|
|
|
*micp = micp_inst;
|
2021-06-08 13:42:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
2021-05-03 17:28:01 +02:00
|
|
|
}
|
|
|
|
|
2022-06-17 10:24:03 +02:00
|
|
|
int bt_micp_client_cb_register(struct bt_micp_cb *cb)
|
2021-05-03 17:28:01 +02:00
|
|
|
{
|
2022-06-17 10:24:03 +02:00
|
|
|
#if defined(CONFIG_BT_MICP_CLIENT_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) {
|
|
|
|
CHECKIF(cb->aics_cb.discover != NULL) {
|
|
|
|
BT_ERR("AICS discover callback shall not be set");
|
|
|
|
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-17 10:24:03 +02:00
|
|
|
for (int i = 0; i < ARRAY_SIZE(micp_insts); i++) {
|
|
|
|
for (int j = 0; j < ARRAY_SIZE(micp_insts[i].cli.aics); j++) {
|
|
|
|
struct bt_aics *aics = micp_insts[i].cli.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-17 10:24:03 +02:00
|
|
|
#endif /* CONFIG_BT_MICP_CLIENT_AICS */
|
2021-05-03 17:28:01 +02:00
|
|
|
|
2022-06-17 10:24:03 +02:00
|
|
|
micp_client_cb = cb;
|
2021-05-03 17:28:01 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-06-17 10:24:03 +02:00
|
|
|
int bt_micp_client_included_get(struct bt_micp *micp,
|
|
|
|
struct bt_micp_included *included)
|
2021-05-03 17:28:01 +02:00
|
|
|
{
|
2022-06-17 10:24:03 +02:00
|
|
|
CHECKIF(micp == NULL) {
|
|
|
|
BT_DBG("NULL micp");
|
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-17 10:24:03 +02:00
|
|
|
included->aics_cnt = micp->cli.aics_inst_cnt;
|
|
|
|
included->aics = micp->cli.aics;
|
2021-05-03 17:28:01 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-06-17 10:24:03 +02:00
|
|
|
int bt_micp_client_conn_get(const struct bt_micp *micp, struct bt_conn **conn)
|
2021-06-09 10:20:00 +02:00
|
|
|
{
|
2022-06-17 10:24:03 +02:00
|
|
|
CHECKIF(micp == NULL) {
|
|
|
|
BT_DBG("NULL micp pointer");
|
2021-06-09 10:20:00 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2022-06-17 10:24:03 +02:00
|
|
|
if (!micp->client_instance) {
|
|
|
|
BT_DBG("micp pointer shall be client instance");
|
2021-06-09 10:20:00 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2022-06-17 10:24:03 +02:00
|
|
|
if (micp->cli.conn == NULL) {
|
|
|
|
BT_DBG("micp pointer not associated with a connection. "
|
2021-06-09 10:20:00 +02:00
|
|
|
"Do discovery first");
|
|
|
|
return -ENOTCONN;
|
|
|
|
}
|
|
|
|
|
2022-06-17 10:24:03 +02:00
|
|
|
*conn = micp->cli.conn;
|
2021-06-09 10:20:00 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-06-17 10:24:03 +02:00
|
|
|
int bt_micp_client_mute_get(struct bt_micp *micp)
|
2021-05-03 17:28:01 +02:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
2022-06-17 10:24:03 +02:00
|
|
|
CHECKIF(micp == NULL) {
|
|
|
|
BT_DBG("NULL micp");
|
2021-05-03 17:28:01 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2022-06-17 10:24:03 +02:00
|
|
|
if (micp->cli.mute_handle == 0) {
|
2021-05-03 17:28:01 +02:00
|
|
|
BT_DBG("Handle not set");
|
|
|
|
return -EINVAL;
|
2022-06-17 10:24:03 +02:00
|
|
|
} else if (micp->cli.busy) {
|
2021-05-03 17:28:01 +02:00
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
2022-06-17 10:24:03 +02:00
|
|
|
micp->cli.read_params.func = micp_client_read_mute_cb;
|
|
|
|
micp->cli.read_params.handle_count = 1;
|
|
|
|
micp->cli.read_params.single.handle = micp->cli.mute_handle;
|
|
|
|
micp->cli.read_params.single.offset = 0U;
|
2021-05-03 17:28:01 +02:00
|
|
|
|
2022-06-17 10:24:03 +02:00
|
|
|
err = bt_gatt_read(micp->cli.conn, &micp->cli.read_params);
|
2021-05-03 17:28:01 +02:00
|
|
|
if (err == 0) {
|
2022-06-17 10:24:03 +02:00
|
|
|
micp->cli.busy = true;
|
2021-05-03 17:28:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2022-06-17 10:24:03 +02:00
|
|
|
int bt_micp_client_write_mute(struct bt_micp *micp, bool mute)
|
2021-05-03 17:28:01 +02:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
2022-06-17 10:24:03 +02:00
|
|
|
CHECKIF(micp == NULL) {
|
|
|
|
BT_DBG("NULL micp");
|
2021-05-03 17:28:01 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2022-06-17 10:24:03 +02:00
|
|
|
if (micp->cli.mute_handle == 0) {
|
2021-05-03 17:28:01 +02:00
|
|
|
BT_DBG("Handle not set");
|
|
|
|
return -EINVAL;
|
2022-06-17 10:24:03 +02:00
|
|
|
} else if (micp->cli.busy) {
|
2021-05-03 17:28:01 +02:00
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
2022-06-17 10:24:03 +02:00
|
|
|
micp->cli.mute_val_buf[0] = mute;
|
|
|
|
micp->cli.write_params.offset = 0;
|
|
|
|
micp->cli.write_params.data = micp->cli.mute_val_buf;
|
|
|
|
micp->cli.write_params.length = sizeof(mute);
|
|
|
|
micp->cli.write_params.handle = micp->cli.mute_handle;
|
|
|
|
micp->cli.write_params.func = micp_client_write_mics_mute_cb;
|
2021-05-03 17:28:01 +02:00
|
|
|
|
2022-06-17 10:24:03 +02:00
|
|
|
err = bt_gatt_write(micp->cli.conn, &micp->cli.write_params);
|
2021-05-03 17:28:01 +02:00
|
|
|
if (err == 0) {
|
2022-06-17 10:24:03 +02:00
|
|
|
micp->cli.busy = true;
|
2021-05-03 17:28:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2022-06-17 10:24:03 +02:00
|
|
|
int bt_micp_client_mute(struct bt_micp *micp)
|
2021-05-03 17:28:01 +02:00
|
|
|
{
|
2022-06-17 10:24:03 +02:00
|
|
|
return bt_micp_client_write_mute(micp, true);
|
2021-05-03 17:28:01 +02:00
|
|
|
}
|
|
|
|
|
2022-06-17 10:24:03 +02:00
|
|
|
int bt_micp_client_unmute(struct bt_micp *micp)
|
2021-05-03 17:28:01 +02:00
|
|
|
{
|
2022-06-17 10:24:03 +02:00
|
|
|
return bt_micp_client_write_mute(micp, false);
|
2021-05-03 17:28:01 +02:00
|
|
|
}
|