public inbox for linux-bluetooth@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH BlueZ] battery: improve the display of the charge level
@ 2025-10-22 15:58 Roman Smirnov
  2025-10-22 17:44 ` [BlueZ] " bluez.test.bot
  2025-10-23 13:28 ` [PATCH BlueZ] " Luiz Augusto von Dentz
  0 siblings, 2 replies; 3+ messages in thread
From: Roman Smirnov @ 2025-10-22 15:58 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Roman Smirnov

At the moment, the charge level of the connected devices can
fluctuate in the range of several values. This is due to changes
in the conditions in which the battery is located (for example,
heating or cooling).

Save the last 4 charge levels. Choose the lowest one.

Fixes: https://github.com/bluez/bluez/issues/1612
---
 src/battery.c | 26 ++++++++++++++++++++++++--
 1 file changed, 24 insertions(+), 2 deletions(-)

diff --git a/src/battery.c b/src/battery.c
index 59e4fc570..370a5c1d0 100644
--- a/src/battery.c
+++ b/src/battery.c
@@ -33,10 +33,13 @@
 #define BATTERY_PROVIDER_MANAGER_INTERFACE "org.bluez.BatteryProviderManager1"
 
 #define BATTERY_MAX_PERCENTAGE 100
+#define LAST_CHARGES_SIZE 4
 
 struct btd_battery {
 	char *path; /* D-Bus object path */
 	uint8_t percentage; /* valid between 0 to 100 inclusively */
+	uint8_t *last_charges; /* last received charge percentages */
+	uint8_t lru_charge_id; /* oldest received charge id */
 	char *source; /* Descriptive source of the battery info */
 	char *provider_path; /* The provider root path, if any */
 };
@@ -92,6 +95,12 @@ static struct btd_battery *battery_new(const char *path, const char *source,
 	battery = new0(struct btd_battery, 1);
 	battery->path = g_strdup(path);
 	battery->percentage = UINT8_MAX;
+	battery->last_charges = new0(uint8_t, LAST_CHARGES_SIZE);
+	battery->lru_charge_id = 0;
+
+	for (uint8_t id = 0; id < LAST_CHARGES_SIZE; id++)
+		battery->last_charges[id] = UINT8_MAX;
+
 	if (source)
 		battery->source = g_strdup(source);
 	if (provider_path)
@@ -105,6 +114,9 @@ static void battery_free(struct btd_battery *battery)
 	if (battery->path)
 		g_free(battery->path);
 
+	if (battery->last_charges)
+		g_free(battery->last_charges);
+
 	if (battery->source)
 		g_free(battery->source);
 
@@ -219,6 +231,8 @@ bool btd_battery_unregister(struct btd_battery *battery)
 
 bool btd_battery_update(struct btd_battery *battery, uint8_t percentage)
 {
+	uint8_t min_charge = BATTERY_MAX_PERCENTAGE;
+
 	DBG("path = %s", battery->path);
 
 	if (!queue_find(batteries, NULL, battery)) {
@@ -231,10 +245,18 @@ bool btd_battery_update(struct btd_battery *battery, uint8_t percentage)
 		return false;
 	}
 
-	if (battery->percentage == percentage)
+	battery->last_charges[battery->lru_charge_id] = percentage;
+	battery->lru_charge_id = (battery->lru_charge_id + 1) % LAST_CHARGES_SIZE;
+
+	for (uint8_t id = 0; id < LAST_CHARGES_SIZE; id++) {
+		if (battery->last_charges[id] < min_charge)
+			min_charge = battery->last_charges[id];
+	}
+
+	if (battery->percentage == min_charge)
 		return true;
 
-	battery->percentage = percentage;
+	battery->percentage = min_charge;
 	g_dbus_emit_property_changed(btd_get_dbus_connection(), battery->path,
 				     BATTERY_INTERFACE, "Percentage");
 
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* RE: [BlueZ] battery: improve the display of the charge level
  2025-10-22 15:58 [PATCH BlueZ] battery: improve the display of the charge level Roman Smirnov
@ 2025-10-22 17:44 ` bluez.test.bot
  2025-10-23 13:28 ` [PATCH BlueZ] " Luiz Augusto von Dentz
  1 sibling, 0 replies; 3+ messages in thread
From: bluez.test.bot @ 2025-10-22 17:44 UTC (permalink / raw)
  To: linux-bluetooth, r.smirnov

[-- Attachment #1: Type: text/plain, Size: 1262 bytes --]

This is automated email and please do not reply to this email!

Dear submitter,

Thank you for submitting the patches to the linux bluetooth mailing list.
This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/project/bluetooth/list/?series=1014597

---Test result---

Test Summary:
CheckPatch                    PENDING   0.40 seconds
GitLint                       PENDING   0.37 seconds
BuildEll                      PASS      20.39 seconds
BluezMake                     PASS      2763.28 seconds
MakeCheck                     PASS      20.13 seconds
MakeDistcheck                 PASS      193.56 seconds
CheckValgrind                 PASS      245.36 seconds
CheckSmatch                   PASS      315.53 seconds
bluezmakeextell               PASS      131.63 seconds
IncrementalBuild              PENDING   0.41 seconds
ScanBuild                     PASS      973.58 seconds

Details
##############################
Test: CheckPatch - PENDING
Desc: Run checkpatch.pl script
Output:

##############################
Test: GitLint - PENDING
Desc: Run gitlint
Output:

##############################
Test: IncrementalBuild - PENDING
Desc: Incremental build with the patches in the series
Output:



---
Regards,
Linux Bluetooth


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH BlueZ] battery: improve the display of the charge level
  2025-10-22 15:58 [PATCH BlueZ] battery: improve the display of the charge level Roman Smirnov
  2025-10-22 17:44 ` [BlueZ] " bluez.test.bot
@ 2025-10-23 13:28 ` Luiz Augusto von Dentz
  1 sibling, 0 replies; 3+ messages in thread
From: Luiz Augusto von Dentz @ 2025-10-23 13:28 UTC (permalink / raw)
  To: Roman Smirnov; +Cc: linux-bluetooth

Hi Roman,

On Wed, Oct 22, 2025 at 11:59 AM Roman Smirnov <r.smirnov@omp.ru> wrote:
>
> At the moment, the charge level of the connected devices can
> fluctuate in the range of several values. This is due to changes
> in the conditions in which the battery is located (for example,
> heating or cooling).
>
> Save the last 4 charge levels. Choose the lowest one.

This sounds more like a work around a device that doesn't calibrate
its sensor and end up with fluctuating value, so Id only apply such
logic to them but in order to do that we need some logic that detects
it is fluctuating to begin with, perhaps by checking if the battery is
discharging and its value increases over time it is probably an
indication that it is fluctuating. Btw, I wonder if such logic exists
in other subsystems that display the battery level so we can follow a
more tried method rather than inventing our own.

> Fixes: https://github.com/bluez/bluez/issues/1612
> ---
>  src/battery.c | 26 ++++++++++++++++++++++++--
>  1 file changed, 24 insertions(+), 2 deletions(-)
>
> diff --git a/src/battery.c b/src/battery.c
> index 59e4fc570..370a5c1d0 100644
> --- a/src/battery.c
> +++ b/src/battery.c
> @@ -33,10 +33,13 @@
>  #define BATTERY_PROVIDER_MANAGER_INTERFACE "org.bluez.BatteryProviderManager1"
>
>  #define BATTERY_MAX_PERCENTAGE 100
> +#define LAST_CHARGES_SIZE 4
>
>  struct btd_battery {
>         char *path; /* D-Bus object path */
>         uint8_t percentage; /* valid between 0 to 100 inclusively */
> +       uint8_t *last_charges; /* last received charge percentages */
> +       uint8_t lru_charge_id; /* oldest received charge id */
>         char *source; /* Descriptive source of the battery info */
>         char *provider_path; /* The provider root path, if any */
>  };
> @@ -92,6 +95,12 @@ static struct btd_battery *battery_new(const char *path, const char *source,
>         battery = new0(struct btd_battery, 1);
>         battery->path = g_strdup(path);
>         battery->percentage = UINT8_MAX;
> +       battery->last_charges = new0(uint8_t, LAST_CHARGES_SIZE);
> +       battery->lru_charge_id = 0;
> +
> +       for (uint8_t id = 0; id < LAST_CHARGES_SIZE; id++)
> +               battery->last_charges[id] = UINT8_MAX;
> +
>         if (source)
>                 battery->source = g_strdup(source);
>         if (provider_path)
> @@ -105,6 +114,9 @@ static void battery_free(struct btd_battery *battery)
>         if (battery->path)
>                 g_free(battery->path);
>
> +       if (battery->last_charges)
> +               g_free(battery->last_charges);
> +
>         if (battery->source)
>                 g_free(battery->source);
>
> @@ -219,6 +231,8 @@ bool btd_battery_unregister(struct btd_battery *battery)
>
>  bool btd_battery_update(struct btd_battery *battery, uint8_t percentage)
>  {
> +       uint8_t min_charge = BATTERY_MAX_PERCENTAGE;
> +
>         DBG("path = %s", battery->path);
>
>         if (!queue_find(batteries, NULL, battery)) {
> @@ -231,10 +245,18 @@ bool btd_battery_update(struct btd_battery *battery, uint8_t percentage)
>                 return false;
>         }
>
> -       if (battery->percentage == percentage)
> +       battery->last_charges[battery->lru_charge_id] = percentage;
> +       battery->lru_charge_id = (battery->lru_charge_id + 1) % LAST_CHARGES_SIZE;
> +
> +       for (uint8_t id = 0; id < LAST_CHARGES_SIZE; id++) {
> +               if (battery->last_charges[id] < min_charge)
> +                       min_charge = battery->last_charges[id];
> +       }
> +
> +       if (battery->percentage == min_charge)
>                 return true;
>
> -       battery->percentage = percentage;
> +       battery->percentage = min_charge;
>         g_dbus_emit_property_changed(btd_get_dbus_connection(), battery->path,
>                                      BATTERY_INTERFACE, "Percentage");
>
> --
> 2.43.0
>
>


-- 
Luiz Augusto von Dentz

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-10-23 13:28 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-10-22 15:58 [PATCH BlueZ] battery: improve the display of the charge level Roman Smirnov
2025-10-22 17:44 ` [BlueZ] " bluez.test.bot
2025-10-23 13:28 ` [PATCH BlueZ] " Luiz Augusto von Dentz

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox