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

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