Linux bluetooth development
 help / color / mirror / Atom feed
From: Simon Mikuda <simon.mikuda@streamunlimited.com>
To: linux-bluetooth@vger.kernel.org
Cc: Simon Mikuda <simon.mikuda@streamunlimited.com>
Subject: [PATCH BlueZ 2/3] shared/vcp: Keep Change_Counter on operations that change nothing
Date: Tue, 24 Nov 2026 15:36:00 +0100	[thread overview]
Message-ID: <20261124143601.245340-2-simon.mikuda@streamunlimited.com> (raw)
In-Reply-To: <20261124143601.245340-1-simon.mikuda@streamunlimited.com>

VCS 3.1.3 says Change_Counter shall be incremented when Volume_Setting
or Mute changes and shall not be changed otherwise. Every Volume
Control Point operation increments it and notifies Volume State, also
a Mute when already muted, a Set Absolute Volume to the current volume
or a Volume Up at 255.

Leave Volume State as it is for an operation that changes neither
Volume_Setting nor Mute.

Fixes: b8a113ecbabc ("shared/vcp: Add initial code for handling VCP")
Assisted-by: Claude:claude-opus-5-5
---
 src/shared/vcp.c | 55 ++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 51 insertions(+), 4 deletions(-)

diff --git a/src/shared/vcp.c b/src/shared/vcp.c
index d19bd9210..197084f50 100644
--- a/src/shared/vcp.c
+++ b/src/shared/vcp.c
@@ -578,6 +578,7 @@ static uint8_t vcs_rel_vol_down(struct bt_vcs *vcs, struct bt_vcp *vcp,
 	struct bt_vcp_db *vdb;
 	struct vol_state *vstate;
 	uint8_t	*change_counter;
+	uint8_t vol_set;
 
 	DBG(vcp, "Volume Down");
 
@@ -602,7 +603,14 @@ static uint8_t vcs_rel_vol_down(struct bt_vcs *vcs, struct bt_vcp *vcp,
 		return BT_ATT_ERROR_INVALID_CHANGE_COUNTER;
 	}
 
-	vstate->vol_set = MAX((vstate->vol_set - VCP_STEP_SIZE), 0);
+	vol_set = MAX((vstate->vol_set - VCP_STEP_SIZE), 0);
+
+	if (vol_set == vstate->vol_set) {
+		DBG(vcp, "Volume Down: already at minimum, no state change");
+		return 0;
+	}
+
+	vstate->vol_set = vol_set;
 	vstate->counter = -~vstate->counter; /*Increment Change Counter*/
 	vcp->volume = vstate->vol_set;
 
@@ -621,6 +629,7 @@ static uint8_t vcs_rel_vol_up(struct bt_vcs *vcs, struct bt_vcp *vcp,
 	struct bt_vcp_db *vdb;
 	struct vol_state *vstate;
 	uint8_t	*change_counter;
+	uint8_t vol_set;
 
 	DBG(vcp, "Volume Up");
 
@@ -645,7 +654,14 @@ static uint8_t vcs_rel_vol_up(struct bt_vcs *vcs, struct bt_vcp *vcp,
 		return BT_ATT_ERROR_INVALID_CHANGE_COUNTER;
 	}
 
-	vstate->vol_set = MIN((vstate->vol_set + VCP_STEP_SIZE), 255);
+	vol_set = MIN((vstate->vol_set + VCP_STEP_SIZE), 255);
+
+	if (vol_set == vstate->vol_set) {
+		DBG(vcp, "Volume Up: already at maximum, no state change");
+		return 0;
+	}
+
+	vstate->vol_set = vol_set;
 	vstate->counter = -~vstate->counter; /*Increment Change Counter*/
 	vcp->volume = vstate->vol_set;
 
@@ -664,6 +680,7 @@ static uint8_t vcs_unmute_rel_vol_down(struct bt_vcs *vcs, struct bt_vcp *vcp,
 	struct bt_vcp_db *vdb;
 	struct vol_state *vstate;
 	uint8_t	*change_counter;
+	uint8_t vol_set;
 
 	DBG(vcp, "Un Mute and Volume Down");
 
@@ -688,8 +705,15 @@ static uint8_t vcs_unmute_rel_vol_down(struct bt_vcs *vcs, struct bt_vcp *vcp,
 		return BT_ATT_ERROR_INVALID_CHANGE_COUNTER;
 	}
 
+	vol_set = MAX((vstate->vol_set - VCP_STEP_SIZE), 0);
+
+	if (vol_set == vstate->vol_set && vstate->mute == 0x00) {
+		DBG(vcp, "Un Mute and Volume Down: no state change");
+		return 0;
+	}
+
 	vstate->mute = 0x00;
-	vstate->vol_set = MAX((vstate->vol_set - VCP_STEP_SIZE), 0);
+	vstate->vol_set = vol_set;
 	vstate->counter = -~vstate->counter; /*Increment Change Counter*/
 	vcp->volume = vstate->vol_set;
 
@@ -708,6 +732,7 @@ static uint8_t vcs_unmute_rel_vol_up(struct bt_vcs *vcs, struct bt_vcp *vcp,
 	struct bt_vcp_db *vdb;
 	struct vol_state *vstate;
 	uint8_t	*change_counter;
+	uint8_t vol_set;
 
 	DBG(vcp, "UN Mute and Volume Up");
 
@@ -732,8 +757,15 @@ static uint8_t vcs_unmute_rel_vol_up(struct bt_vcs *vcs, struct bt_vcp *vcp,
 		return BT_ATT_ERROR_INVALID_CHANGE_COUNTER;
 	}
 
+	vol_set = MIN((vstate->vol_set + VCP_STEP_SIZE), 255);
+
+	if (vol_set == vstate->vol_set && vstate->mute == 0x00) {
+		DBG(vcp, "Un Mute and Volume Up: no state change");
+		return 0;
+	}
+
 	vstate->mute = 0x00;
-	vstate->vol_set = MIN((vstate->vol_set + VCP_STEP_SIZE), 255);
+	vstate->vol_set = vol_set;
 	vstate->counter = -~vstate->counter; /*Increment Change Counter*/
 	vcp->volume = vstate->vol_set;
 
@@ -776,6 +808,11 @@ static uint8_t vcs_set_absolute_vol(struct bt_vcs *vcs, struct bt_vcp *vcp,
 		return BT_ATT_ERROR_INVALID_CHANGE_COUNTER;
 	}
 
+	if (req->vol_set == vstate->vol_set) {
+		DBG(vcp, "Set Absolute Volume: no state change");
+		return 0;
+	}
+
 	vstate->vol_set = req->vol_set;
 	vstate->counter = -~vstate->counter; /*Increment Change Counter*/
 	vcp->volume = vstate->vol_set;
@@ -819,6 +856,11 @@ static uint8_t vcs_unmute(struct bt_vcs *vcs, struct bt_vcp *vcp,
 		return BT_ATT_ERROR_INVALID_CHANGE_COUNTER;
 	}
 
+	if (vstate->mute == 0x00) {
+		DBG(vcp, "Un Mute: already unmuted, no state change");
+		return 0;
+	}
+
 	vstate->mute = 0x00;
 	vstate->counter = -~vstate->counter; /*Increment Change Counter*/
 
@@ -858,6 +900,11 @@ static uint8_t vcs_mute(struct bt_vcs *vcs, struct bt_vcp *vcp,
 		return BT_ATT_ERROR_INVALID_CHANGE_COUNTER;
 	}
 
+	if (vstate->mute == 0x01) {
+		DBG(vcp, "Mute: already muted, no state change");
+		return 0;
+	}
+
 	vstate->mute = 0x01;
 	vstate->counter = -~vstate->counter; /*Increment Change Counter*/
 
-- 
2.43.0


  reply	other threads:[~2026-11-24 14:36 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24 14:35 [PATCH BlueZ 1/3] shared/vcp: Notify Volume State on Mute Simon Mikuda
2026-11-24 14:36 ` Simon Mikuda [this message]
2026-11-24 14:36 ` [PATCH BlueZ 3/3] unit/test-vcp: Add tests for Volume State changes Simon Mikuda
2026-11-24 20:43 ` [BlueZ,1/3] shared/vcp: Notify Volume State on Mute 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=20261124143601.245340-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