From: Simon Mikuda <simon.mikuda@streamunlimited.com>
To: linux-bluetooth@vger.kernel.org
Cc: Simon Mikuda <simon.mikuda@streamunlimited.com>
Subject: [PATCH BlueZ v2 2/4] shared/vcp: Add bt_vcp_get_mute and bt_vcp_set_mute
Date: Wed, 25 Nov 2026 18:02:05 +0100 [thread overview]
Message-ID: <20261125170207.1648671-2-simon.mikuda@streamunlimited.com> (raw)
In-Reply-To: <20261125170207.1648671-1-simon.mikuda@streamunlimited.com>
Keep the Mute field of Volume State next to the volume. As a client,
setting it writes Mute or Unmute to the remote Volume Control Point. As
a server, it updates the local Volume State and notifies it.
The volume callback now also runs when only the mute state changes, so
its user sees a remote client muting or unmuting the server.
Assisted-by: Claude:claude-opus-5-5
---
src/shared/vcp.c | 113 +++++++++++++++++++++++++++++++++++++++++++++++
src/shared/vcp.h | 3 ++
2 files changed, 116 insertions(+)
diff --git a/src/shared/vcp.c b/src/shared/vcp.c
index c7f74956e..1dae20d46 100644
--- a/src/shared/vcp.c
+++ b/src/shared/vcp.c
@@ -181,6 +181,8 @@ struct bt_vcp_notify {
struct bt_vcp_client_op {
uint8_t volume;
bool resend;
+ uint8_t mute;
+ bool resend_mute;
bool wait_reply;
bool wait_notify;
unsigned int timeout_id;
@@ -216,6 +218,7 @@ struct bt_vcp {
uint8_t volume;
uint8_t volume_counter;
+ uint8_t mute;
struct bt_vcp_client_op pending_op;
@@ -977,6 +980,18 @@ struct vocs_op_handler {
{}
};
+static void vcp_mute_changed(struct bt_vcp *vcp, uint8_t mute)
+{
+ if (vcp->mute == mute)
+ return;
+
+ vcp->mute = mute;
+
+ /* The volume callback reports any Volume State change */
+ if (vcp->volume_changed)
+ vcp->volume_changed(vcp, vcp->volume);
+}
+
static void vcs_cp_write(struct gatt_db_attribute *attrib,
unsigned int id, uint16_t offset,
const uint8_t *value, size_t len,
@@ -991,6 +1006,7 @@ static void vcs_cp_write(struct gatt_db_attribute *attrib,
};
uint8_t *vcp_op;
struct vcs_op_handler *handler;
+ struct vol_state *vstate;
uint8_t ret = BT_ATT_ERROR_REQUEST_NOT_SUPPORTED;
DBG(vcp, "VCP Control Point Write");
@@ -1032,6 +1048,10 @@ static void vcs_cp_write(struct gatt_db_attribute *attrib,
DBG(vcp, "%s", handler->str);
ret = handler->func(vcs, vcp, &iov);
+
+ vstate = vdb_get_vstate(vcs->vdb);
+ if (!ret && vstate)
+ vcp_mute_changed(vcp, vstate->mute);
} else {
DBG(vcp, "Unknown opcode 0x%02x", *vcp_op);
ret = BT_ATT_ERROR_OPCODE_NOT_SUPPORTED;
@@ -2045,6 +2065,8 @@ static void vcp_set_volume_complete(struct bt_vcp *vcp)
{
bool resend = vcp->pending_op.resend;
uint8_t volume = vcp->pending_op.volume;
+ bool resend_mute = vcp->pending_op.resend_mute;
+ uint8_t mute = vcp->pending_op.mute;
vcp_client_op_clear(&vcp->pending_op);
@@ -2055,6 +2077,12 @@ static void vcp_set_volume_complete(struct bt_vcp *vcp)
DBG(vcp, "set pending volume 0x%x", volume);
bt_vcp_set_volume(vcp, volume);
}
+
+ /* If the volume request above is in flight, this one waits for it */
+ if (resend_mute) {
+ DBG(vcp, "set pending mute 0x%x", mute);
+ bt_vcp_set_mute(vcp, mute);
+ }
}
static void vcp_vstate_notify(struct bt_vcp *vcp, uint16_t value_handle,
@@ -2076,6 +2104,7 @@ static void vcp_vstate_notify(struct bt_vcp *vcp, uint16_t value_handle,
vcp->volume = vstate->vol_set;
vcp->volume_counter = vstate->counter;
+ vcp->mute = vstate->mute;
if (vcp->volume_changed)
vcp->volume_changed(vcp, vcp->volume);
@@ -2207,6 +2236,89 @@ uint8_t bt_vcp_get_volume(struct bt_vcp *vcp)
return vcp->volume;
}
+static bool vcp_set_mute_client(struct bt_vcp *vcp, uint8_t mute)
+{
+ struct bt_vcs_param req;
+ uint16_t value_handle;
+ struct bt_vcs *vcs = vcp_get_vcs(vcp);
+
+ if (!vcs || !vcs->vol_cp) {
+ DBG(vcp, "error: vol_cp characteristic not available");
+ return false;
+ }
+
+ if (!gatt_db_attribute_get_char_data(vcs->vol_cp, NULL, &value_handle,
+ NULL, NULL, NULL)) {
+ DBG(vcp, "error: vol_cp characteristic not available");
+ return false;
+ }
+
+ /* Like volume, wait for the op in flight: it changes the counter */
+ if (vcp->pending_op.timeout_id) {
+ vcp->pending_op.mute = mute;
+ vcp->pending_op.resend_mute = true;
+ return true;
+ } else if (vcp->mute == mute) {
+ return true;
+ }
+
+ req.op = mute ? BT_VCS_MUTE : BT_VCS_UNMUTE;
+ req.change_counter = vcp->volume_counter;
+
+ if (!bt_gatt_client_write_value(vcp->client, value_handle, (void *)&req,
+ sizeof(req), vcp_volume_cp_sent, vcp,
+ NULL)) {
+ DBG(vcp, "error writing mute");
+ return false;
+ }
+
+ vcp->pending_op.timeout_id = timeout_add(VCP_CLIENT_OP_TIMEOUT,
+ vcp_set_volume_timeout, vcp, NULL);
+ vcp->pending_op.wait_notify = true;
+ vcp->pending_op.wait_reply = true;
+ return true;
+}
+
+static bool vcp_set_mute_server(struct bt_vcp *vcp, uint8_t mute)
+{
+ struct bt_vcp_db *vdb = vcp_get_vdb(vcp);
+ struct vol_state *vstate;
+
+ if (!vdb) {
+ DBG(vcp, "error: VDB not available");
+ return false;
+ }
+
+ vstate = vdb_get_vstate(vdb);
+ if (!vstate) {
+ DBG(vcp, "error: VSTATE not available");
+ return false;
+ }
+
+ if (vstate->mute == mute)
+ return true;
+
+ vstate->mute = mute;
+ vstate->counter = -~vstate->counter; /*Increment Change Counter*/
+ gatt_db_attribute_notify(vdb->vcs->vs, (void *) vstate,
+ sizeof(struct vol_state), bt_vcp_get_att(vcp));
+ vcp_mute_changed(vcp, mute);
+ return true;
+}
+
+bool bt_vcp_set_mute(struct bt_vcp *vcp, uint8_t mute)
+{
+ if (vcp->client)
+ return vcp_set_mute_client(vcp, mute);
+ else
+ return vcp_set_mute_server(vcp, mute);
+}
+
+uint8_t bt_vcp_get_mute(struct bt_vcp *vcp)
+{
+ return vcp->mute;
+}
+
static void vcp_voffset_state_notify(struct bt_vcp *vcp, uint16_t value_handle,
const uint8_t *value, uint16_t length,
void *user_data)
@@ -2320,6 +2432,7 @@ static void read_vol_state(struct bt_vcp *vcp, bool success, uint8_t att_ecode,
vcp->volume = vs->vol_set;
vcp->volume_counter = vs->counter;
+ vcp->mute = vs->mute;
}
static void read_vol_offset_state(struct bt_vcp *vcp, bool success,
diff --git a/src/shared/vcp.h b/src/shared/vcp.h
index e031beafd..807d2bdbe 100644
--- a/src/shared/vcp.h
+++ b/src/shared/vcp.h
@@ -42,6 +42,9 @@ void bt_vcp_detach(struct bt_vcp *vcp);
uint8_t bt_vcp_get_volume(struct bt_vcp *vcp);
bool bt_vcp_set_volume(struct bt_vcp *vcp, uint8_t volume);
+uint8_t bt_vcp_get_mute(struct bt_vcp *vcp);
+bool bt_vcp_set_mute(struct bt_vcp *vcp, uint8_t mute);
+
bool bt_vcp_set_debug(struct bt_vcp *vcp, bt_vcp_debug_func_t cb,
void *user_data, bt_vcp_destroy_func_t destroy);
--
2.43.0
next prev parent reply other threads:[~2026-11-25 17:02 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-25 17:02 [PATCH BlueZ v2 1/4] doc/org.bluez.MediaTransport: Add Mute property Simon Mikuda
2026-11-25 17:02 ` Simon Mikuda [this message]
2026-11-25 17:02 ` [PATCH BlueZ v2 3/4] audio/transport: " Simon Mikuda
2026-11-25 17:02 ` [PATCH BlueZ v2 4/4] client/player: Add transport.mute command Simon Mikuda
2026-11-25 23:58 ` [BlueZ,v2,1/4] doc/org.bluez.MediaTransport: Add Mute property bluez.test.bot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261125170207.1648671-2-simon.mikuda@streamunlimited.com \
--to=simon.mikuda@streamunlimited.com \
--cc=linux-bluetooth@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox