2020-11-30 23:03:22

by Sonny Sasaka

[permalink] [raw]
Subject: [PATCH BlueZ v5 1/7] battery: Add the internal Battery API

This patch adds an API for internal BlueZ code to expose battery
information to org.bluez.Battery1 interface. The motivation behind this
is that there is going to be other places than GATT BAS handler that
exposes battery information, for example internal plugins and the
BatteryProvider1 D-Bus API for external clients.

Reviewed-by: Daniel Winkler <[email protected]>

---
Makefile.am | 3 +-
src/battery.c | 191 ++++++++++++++++++++++++++++++++++++++++++++++++++
src/battery.h | 15 ++++
3 files changed, 208 insertions(+), 1 deletion(-)
create mode 100644 src/battery.c
create mode 100644 src/battery.h

diff --git a/Makefile.am b/Makefile.am
index 69b95828f..95917f695 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -294,7 +294,8 @@ src_bluetoothd_SOURCES = $(builtin_sources) \
src/device.h src/device.c \
src/dbus-common.c src/dbus-common.h \
src/eir.h src/eir.c \
- src/adv_monitor.h src/adv_monitor.c
+ src/adv_monitor.h src/adv_monitor.c \
+ src/battery.h src/battery.c
src_bluetoothd_LDADD = lib/libbluetooth-internal.la \
gdbus/libgdbus-internal.la \
src/libshared-glib.la \
diff --git a/src/battery.c b/src/battery.c
new file mode 100644
index 000000000..87a6b91fb
--- /dev/null
+++ b/src/battery.c
@@ -0,0 +1,191 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ *
+ * BlueZ - Bluetooth protocol stack for Linux
+ *
+ * Copyright (C) 2020 Google LLC
+ *
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#define _GNU_SOURCE
+#include <stdbool.h>
+#include <stdint.h>
+#include <glib.h>
+
+#include "gdbus/gdbus.h"
+#include "lib/bluetooth.h"
+#include "src/shared/queue.h"
+#include "src/shared/util.h"
+#include "battery.h"
+#include "dbus-common.h"
+#include "adapter.h"
+#include "log.h"
+
+#define BATTERY_INTERFACE "org.bluez.Battery1"
+
+#define BATTERY_MAX_PERCENTAGE 100
+
+struct btd_battery {
+ char *path; /* D-Bus object path, owns pointer */
+ uint8_t percentage; /* valid between 0 to 100 inclusively */
+};
+
+static struct queue *batteries = NULL;
+
+static void battery_add(struct btd_battery *battery)
+{
+ if (!batteries)
+ batteries = queue_new();
+
+ queue_push_head(batteries, battery);
+}
+
+static void battery_remove(struct btd_battery *battery)
+{
+ queue_remove(batteries, battery);
+ if (queue_isempty(batteries)) {
+ queue_destroy(batteries, NULL);
+ batteries = NULL;
+ }
+}
+
+static bool match_path(const void *data, const void *user_data)
+{
+ const struct btd_battery *battery = data;
+ const char *path = user_data;
+
+ return g_strcmp0(battery->path, path) == 0;
+}
+
+static struct btd_battery *battery_new(const char *path)
+{
+ struct btd_battery *battery;
+
+ battery = new0(struct btd_battery, 1);
+ battery->path = g_strdup(path);
+ battery->percentage = UINT8_MAX;
+
+ return battery;
+}
+
+static void battery_free(struct btd_battery *battery)
+{
+ if (battery->path)
+ g_free(battery->path);
+
+ free(battery);
+}
+
+static gboolean property_percentage_get(const GDBusPropertyTable *property,
+ DBusMessageIter *iter, void *data)
+{
+ struct btd_battery *battery = data;
+
+ dbus_message_iter_append_basic(iter, DBUS_TYPE_BYTE,
+ &battery->percentage);
+
+ return TRUE;
+}
+
+static gboolean property_percentage_exists(const GDBusPropertyTable *property,
+ void *data)
+{
+ struct btd_battery *battery = data;
+
+ return battery->percentage <= BATTERY_MAX_PERCENTAGE;
+}
+
+static const GDBusPropertyTable battery_properties[] = {
+ { "Percentage", "y", property_percentage_get, NULL,
+ property_percentage_exists },
+ {}
+};
+
+struct btd_battery *btd_battery_register(const char *path)
+{
+ struct btd_battery *battery;
+
+ DBG("path = %s", path);
+
+ if (queue_find(batteries, match_path, path)) {
+ error("error registering battery: path exists");
+ return NULL;
+ }
+
+ if (!g_str_has_prefix(path, "/")) {
+ error("error registering battery: invalid D-Bus object path");
+ return NULL;
+ }
+
+ battery = battery_new(path);
+ battery_add(battery);
+
+ if (!g_dbus_register_interface(btd_get_dbus_connection(), battery->path,
+ BATTERY_INTERFACE, NULL, NULL,
+ battery_properties, battery, NULL)) {
+ error("error registering D-Bus interface for %s",
+ battery->path);
+
+ battery_remove(battery);
+ battery_free(battery);
+
+ return NULL;
+ }
+
+ DBG("registered Battery object: %s", battery->path);
+
+ return battery;
+}
+
+bool btd_battery_unregister(struct btd_battery *battery)
+{
+ DBG("path = %s", battery->path);
+
+ if (!queue_find(batteries, NULL, battery)) {
+ error("error unregistering battery: "
+ "battery %s is not registered",
+ battery->path);
+ return false;
+ }
+
+ if (!g_dbus_unregister_interface(btd_get_dbus_connection(),
+ battery->path, BATTERY_INTERFACE)) {
+ error("error unregistering battery %s from D-Bus interface",
+ battery->path);
+ return false;
+ }
+
+ battery_remove(battery);
+ battery_free(battery);
+
+ return true;
+}
+
+bool btd_battery_update(struct btd_battery *battery, uint8_t percentage)
+{
+ DBG("path = %s", battery->path);
+
+ if (!queue_find(batteries, NULL, battery)) {
+ error("error updating battery: battery is not registered");
+ return false;
+ }
+
+ if (percentage > BATTERY_MAX_PERCENTAGE) {
+ error("error updating battery: percentage is not valid");
+ return false;
+ }
+
+ if (battery->percentage == percentage)
+ return true;
+
+ battery->percentage = percentage;
+ g_dbus_emit_property_changed(btd_get_dbus_connection(), battery->path,
+ BATTERY_INTERFACE, "Percentage");
+
+ return true;
+}
diff --git a/src/battery.h b/src/battery.h
new file mode 100644
index 000000000..9c69b7afa
--- /dev/null
+++ b/src/battery.h
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ *
+ * BlueZ - Bluetooth protocol stack for Linux
+ *
+ * Copyright (C) 2020 Google LLC
+ *
+ *
+ */
+
+struct btd_battery;
+
+struct btd_battery *btd_battery_register(const char *path);
+bool btd_battery_unregister(struct btd_battery *battery);
+bool btd_battery_update(struct btd_battery *battery, uint8_t percentage);
--
2.26.2


2020-11-30 23:03:22

by Sonny Sasaka

[permalink] [raw]
Subject: [PATCH BlueZ v5 7/7] battery: Implement Battery Provider API

This patch implements the BatteryProvider1 and BatteryProviderManager1
API. This is a means for external clients to feed battery information to
BlueZ if they handle some profile and can decode battery reporting.

The battery information is then exposed externally via the existing
Battery1 interface. UI components can consume this API to display
Bluetooth peripherals' battery via a unified BlueZ API.

Reviewed-by: Miao-chen Chou <[email protected]>

---
profiles/battery/battery.c | 2 +-
src/adapter.c | 11 ++
src/battery.c | 387 ++++++++++++++++++++++++++++++++++++-
src/battery.h | 10 +-
4 files changed, 405 insertions(+), 5 deletions(-)

diff --git a/profiles/battery/battery.c b/profiles/battery/battery.c
index 2478816a4..81f849d57 100644
--- a/profiles/battery/battery.c
+++ b/profiles/battery/battery.c
@@ -127,7 +127,7 @@ static void batt_io_ccc_written_cb(uint16_t att_ecode, void *user_data)
}

batt->battery = btd_battery_register(device_get_path(batt->device),
- "GATT Battery Service");
+ "GATT Battery Service", NULL);

if (!batt->battery) {
batt_reset(batt);
diff --git a/src/adapter.c b/src/adapter.c
index 03d9d29e9..ec6a6a64c 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -66,6 +66,7 @@
#include "advertising.h"
#include "adv_monitor.h"
#include "eir.h"
+#include "battery.h"

#define MODE_OFF 0x00
#define MODE_CONNECTABLE 0x01
@@ -254,6 +255,8 @@ struct btd_adapter {

struct btd_adv_monitor_manager *adv_monitor_manager;

+ struct btd_battery_provider_manager *battery_provider_manager;
+
gboolean initialized;

GSList *pin_callbacks;
@@ -6339,6 +6342,9 @@ static void adapter_remove(struct btd_adapter *adapter)
btd_adv_monitor_manager_destroy(adapter->adv_monitor_manager);
adapter->adv_monitor_manager = NULL;

+ btd_battery_provider_manager_destroy(adapter->battery_provider_manager);
+ adapter->battery_provider_manager = NULL;
+
g_slist_free(adapter->pin_callbacks);
adapter->pin_callbacks = NULL;

@@ -8659,6 +8665,11 @@ static int adapter_register(struct btd_adapter *adapter)
}
}

+ if (g_dbus_get_flags() & G_DBUS_FLAG_ENABLE_EXPERIMENTAL) {
+ adapter->battery_provider_manager =
+ btd_battery_provider_manager_create(adapter);
+ }
+
db = btd_gatt_database_get_db(adapter->database);
adapter->db_id = gatt_db_register(db, services_modified,
services_modified,
diff --git a/src/battery.c b/src/battery.c
index 8613d6e23..77fee22b6 100644
--- a/src/battery.c
+++ b/src/battery.c
@@ -24,9 +24,13 @@
#include "battery.h"
#include "dbus-common.h"
#include "adapter.h"
+#include "device.h"
#include "log.h"
+#include "error.h"

#define BATTERY_INTERFACE "org.bluez.Battery1"
+#define BATTERY_PROVIDER_INTERFACE "org.bluez.BatteryProvider1"
+#define BATTERY_PROVIDER_MANAGER_INTERFACE "org.bluez.BatteryProviderManager1"

#define BATTERY_MAX_PERCENTAGE 100

@@ -34,10 +38,27 @@ struct btd_battery {
char *path; /* D-Bus object path */
uint8_t percentage; /* valid between 0 to 100 inclusively */
char *source; /* Descriptive source of the battery info */
+ char *provider_path; /* The provider root path, if any */
+};
+
+struct btd_battery_provider_manager {
+ struct btd_adapter *adapter; /* Does not own pointer */
+ struct queue *battery_providers;
+};
+
+struct battery_provider {
+ struct btd_battery_provider_manager *manager; /* Does not own pointer */
+
+ char *owner; /* Owner D-Bus address */
+ char *path; /* D-Bus object path */
+
+ GDBusClient *client;
};

static struct queue *batteries = NULL;

+static void provider_disconnect_cb(DBusConnection *conn, void *user_data);
+
static void battery_add(struct btd_battery *battery)
{
if (!batteries)
@@ -63,7 +84,8 @@ static bool match_path(const void *data, const void *user_data)
return g_strcmp0(battery->path, path) == 0;
}

-static struct btd_battery *battery_new(const char *path, const char *source)
+static struct btd_battery *battery_new(const char *path, const char *source,
+ const char *provider_path)
{
struct btd_battery *battery;

@@ -72,6 +94,8 @@ static struct btd_battery *battery_new(const char *path, const char *source)
battery->percentage = UINT8_MAX;
if (source)
battery->source = g_strdup(source);
+ if (provider_path)
+ battery->provider_path = g_strdup(provider_path);

return battery;
}
@@ -133,7 +157,8 @@ static const GDBusPropertyTable battery_properties[] = {
{}
};

-struct btd_battery *btd_battery_register(const char *path, const char *source)
+struct btd_battery *btd_battery_register(const char *path, const char *source,
+ const char *provider_path)
{
struct btd_battery *battery;

@@ -149,7 +174,7 @@ struct btd_battery *btd_battery_register(const char *path, const char *source)
return NULL;
}

- battery = battery_new(path, source);
+ battery = battery_new(path, source, provider_path);
battery_add(battery);

if (!g_dbus_register_interface(btd_get_dbus_connection(), battery->path,
@@ -216,3 +241,359 @@ bool btd_battery_update(struct btd_battery *battery, uint8_t percentage)

return true;
}
+
+static struct btd_battery *find_battery_by_path(const char *path)
+{
+ return queue_find(batteries, match_path, path);
+}
+
+static void provided_battery_property_changed_cb(GDBusProxy *proxy,
+ const char *name,
+ DBusMessageIter *iter,
+ void *user_data)
+{
+ uint8_t percentage;
+ const char *export_path;
+ DBusMessageIter dev_iter;
+
+ if (g_dbus_proxy_get_property(proxy, "Device", &dev_iter) == FALSE)
+ return;
+
+ dbus_message_iter_get_basic(&dev_iter, &export_path);
+
+ if (strcmp(name, "Percentage") != 0)
+ return;
+
+ if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_BYTE)
+ return;
+
+ dbus_message_iter_get_basic(iter, &percentage);
+
+ DBG("battery percentage changed on %s, percentage = %d",
+ g_dbus_proxy_get_path(proxy), percentage);
+
+ btd_battery_update(find_battery_by_path(export_path), percentage);
+}
+
+static void provided_battery_added_cb(GDBusProxy *proxy, void *user_data)
+{
+ struct battery_provider *provider = user_data;
+ struct btd_battery *battery;
+ struct btd_device *device;
+ const char *path = g_dbus_proxy_get_path(proxy);
+ const char *export_path;
+ const char *source = NULL;
+ uint8_t percentage;
+ DBusMessageIter iter;
+
+ if (g_dbus_proxy_get_property(proxy, "Device", &iter) == FALSE) {
+ warn("Battery object %s does not specify device path", path);
+ return;
+ }
+
+ dbus_message_iter_get_basic(&iter, &export_path);
+
+ if (strcmp(g_dbus_proxy_get_interface(proxy),
+ BATTERY_PROVIDER_INTERFACE) != 0)
+ return;
+
+ device = btd_adapter_find_device_by_path(provider->manager->adapter,
+ export_path);
+ if (!device || device_is_temporary(device)) {
+ warn("Ignoring non-existent device path for battery %s",
+ export_path);
+ return;
+ }
+
+ if (find_battery_by_path(export_path)) {
+ DBG("Battery for %s is already provided, ignoring the new one",
+ export_path);
+ return;
+ }
+
+ g_dbus_proxy_set_property_watch(
+ proxy, provided_battery_property_changed_cb, provider);
+
+ if (g_dbus_proxy_get_property(proxy, "Source", &iter) == TRUE)
+ dbus_message_iter_get_basic(&iter, &source);
+
+ battery = btd_battery_register(export_path, source, provider->path);
+
+ DBG("provided battery added %s", path);
+
+ /* Percentage property may not be immediately available, that's okay
+ * since we monitor changes to this property.
+ */
+ if (g_dbus_proxy_get_property(proxy, "Percentage", &iter) == FALSE)
+ return;
+
+ dbus_message_iter_get_basic(&iter, &percentage);
+
+ btd_battery_update(battery, percentage);
+}
+
+static void provided_battery_removed_cb(GDBusProxy *proxy, void *user_data)
+{
+ struct battery_provider *provider = user_data;
+ struct btd_battery *battery;
+ const char *export_path;
+ DBusMessageIter iter;
+
+ if (g_dbus_proxy_get_property(proxy, "Device", &iter) == FALSE)
+ return;
+
+ dbus_message_iter_get_basic(&iter, &export_path);
+
+ if (strcmp(g_dbus_proxy_get_interface(proxy),
+ BATTERY_PROVIDER_INTERFACE) != 0)
+ return;
+
+ DBG("provided battery removed %s", g_dbus_proxy_get_path(proxy));
+
+ battery = find_battery_by_path(export_path);
+ if (!battery)
+ return;
+
+ if (g_strcmp0(battery->provider_path, provider->path) != 0)
+ return;
+
+ g_dbus_proxy_set_property_watch(proxy, NULL, NULL);
+
+ btd_battery_unregister(battery);
+}
+
+static bool match_provider_path(const void *data, const void *user_data)
+{
+ const struct battery_provider *provider = data;
+ const char *path = user_data;
+
+ return strcmp(provider->path, path) == 0;
+}
+
+static void unregister_if_path_has_prefix(void *data, void *user_data)
+{
+ struct btd_battery *battery = data;
+ struct battery_provider *provider = user_data;
+
+ if (g_strcmp0(battery->provider_path, provider->path) == 0)
+ btd_battery_unregister(battery);
+}
+
+static void battery_provider_free(gpointer data)
+{
+ struct battery_provider *provider = data;
+
+ /* Unregister batteries under the root path of provider->path */
+ queue_foreach(batteries, unregister_if_path_has_prefix, provider);
+
+ if (provider->owner)
+ g_free(provider->owner);
+
+ if (provider->path)
+ g_free(provider->path);
+
+ if (provider->client) {
+ g_dbus_client_set_disconnect_watch(provider->client, NULL,
+ NULL);
+ g_dbus_client_set_proxy_handlers(provider->client, NULL, NULL,
+ NULL, NULL);
+ g_dbus_client_unref(provider->client);
+ }
+
+ free(provider);
+}
+
+static struct battery_provider *
+battery_provider_new(DBusConnection *conn,
+ struct btd_battery_provider_manager *manager,
+ const char *path, const char *sender)
+{
+ struct battery_provider *provider;
+
+ provider = new0(struct battery_provider, 1);
+ provider->manager = manager;
+ provider->owner = g_strdup(sender);
+ provider->path = g_strdup(path);
+
+ provider->client = g_dbus_client_new_full(conn, sender, path, path);
+
+ if (!provider->client) {
+ error("error creating D-Bus client %s", path);
+ battery_provider_free(provider);
+ return NULL;
+ }
+
+ g_dbus_client_set_disconnect_watch(provider->client,
+ provider_disconnect_cb, provider);
+
+ g_dbus_client_set_proxy_handlers(provider->client,
+ provided_battery_added_cb,
+ provided_battery_removed_cb, NULL,
+ provider);
+
+ return provider;
+}
+
+static void provider_disconnect_cb(DBusConnection *conn, void *user_data)
+{
+ struct battery_provider *provider = user_data;
+ struct btd_battery_provider_manager *manager = provider->manager;
+
+ DBG("battery provider client disconnected %s root path %s",
+ provider->owner, provider->path);
+
+ if (!queue_find(manager->battery_providers, NULL, provider)) {
+ warn("Disconnection on a non-existing provider %s",
+ provider->path);
+ return;
+ }
+
+ queue_remove(manager->battery_providers, provider);
+ battery_provider_free(provider);
+}
+
+static DBusMessage *register_battery_provider(DBusConnection *conn,
+ DBusMessage *msg, void *user_data)
+{
+ struct btd_battery_provider_manager *manager = user_data;
+ const char *sender = dbus_message_get_sender(msg);
+ DBusMessageIter args;
+ const char *path;
+ struct battery_provider *provider;
+
+ if (!dbus_message_iter_init(msg, &args))
+ return btd_error_invalid_args(msg);
+
+ if (dbus_message_iter_get_arg_type(&args) != DBUS_TYPE_OBJECT_PATH)
+ return btd_error_invalid_args(msg);
+
+ dbus_message_iter_get_basic(&args, &path);
+
+ DBG("register battery provider path = %s", path);
+
+ if (!g_str_has_prefix(path, "/"))
+ return btd_error_invalid_args(msg);
+
+ if (queue_find(manager->battery_providers, match_provider_path, path)) {
+ return dbus_message_new_error(msg,
+ ERROR_INTERFACE ".AlreadyExists",
+ "Provider already exists");
+ }
+
+ provider = battery_provider_new(conn, manager, path, sender);
+ queue_push_head(manager->battery_providers, provider);
+
+ return dbus_message_new_method_return(msg);
+}
+
+static DBusMessage *unregister_battery_provider(DBusConnection *conn,
+ DBusMessage *msg,
+ void *user_data)
+{
+ struct btd_battery_provider_manager *manager = user_data;
+ const char *sender = dbus_message_get_sender(msg);
+ DBusMessageIter args;
+ const char *path;
+ struct battery_provider *provider;
+
+ if (!dbus_message_iter_init(msg, &args))
+ return btd_error_invalid_args(msg);
+
+ if (dbus_message_iter_get_arg_type(&args) != DBUS_TYPE_OBJECT_PATH)
+ return btd_error_invalid_args(msg);
+
+ dbus_message_iter_get_basic(&args, &path);
+
+ DBG("unregister battery provider path = %s", path);
+
+ provider = queue_find(manager->battery_providers, match_provider_path,
+ path);
+ if (!provider || strcmp(provider->owner, sender) != 0) {
+ return dbus_message_new_error(msg,
+ ERROR_INTERFACE ".DoesNotExist",
+ "Provider does not exist");
+ }
+
+ queue_remove(manager->battery_providers, provider);
+ battery_provider_free(provider);
+
+ return dbus_message_new_method_return(msg);
+}
+
+static const GDBusMethodTable methods[] = {
+ { GDBUS_EXPERIMENTAL_METHOD("RegisterBatteryProvider",
+ GDBUS_ARGS({ "provider", "o" }), NULL,
+ register_battery_provider) },
+ { GDBUS_EXPERIMENTAL_METHOD("UnregisterBatteryProvider",
+ GDBUS_ARGS({ "provider", "o" }), NULL,
+ unregister_battery_provider) },
+ {}
+};
+
+static struct btd_battery_provider_manager *
+manager_new(struct btd_adapter *adapter)
+{
+ struct btd_battery_provider_manager *manager;
+
+ DBG("");
+
+ manager = new0(struct btd_battery_provider_manager, 1);
+ manager->adapter = adapter;
+ manager->battery_providers = queue_new();
+
+ return manager;
+}
+
+static void manager_free(struct btd_battery_provider_manager *manager)
+{
+ if (!manager)
+ return;
+
+ DBG("");
+
+ queue_destroy(manager->battery_providers, battery_provider_free);
+
+ free(manager);
+}
+
+struct btd_battery_provider_manager *
+btd_battery_provider_manager_create(struct btd_adapter *adapter)
+{
+ struct btd_battery_provider_manager *manager;
+
+ if (!adapter)
+ return NULL;
+
+ manager = manager_new(adapter);
+ if (!manager)
+ return NULL;
+
+ if (!g_dbus_register_interface(btd_get_dbus_connection(),
+ adapter_get_path(manager->adapter),
+ BATTERY_PROVIDER_MANAGER_INTERFACE,
+ methods, NULL, NULL, manager, NULL)) {
+ error("error registering " BATTERY_PROVIDER_MANAGER_INTERFACE
+ " interface");
+ manager_free(manager);
+ return NULL;
+ }
+
+ info("Battery Provider Manager created");
+
+ return manager;
+}
+
+void btd_battery_provider_manager_destroy(
+ struct btd_battery_provider_manager *manager)
+{
+ if (!manager)
+ return;
+
+ g_dbus_unregister_interface(btd_get_dbus_connection(),
+ adapter_get_path(manager->adapter),
+ BATTERY_PROVIDER_MANAGER_INTERFACE);
+
+ info("Battery Provider Manager destroyed");
+
+ manager_free(manager);
+}
diff --git a/src/battery.h b/src/battery.h
index ff63454cd..271659474 100644
--- a/src/battery.h
+++ b/src/battery.h
@@ -8,8 +8,16 @@
*
*/

+struct btd_adapter;
struct btd_battery;
+struct btd_battery_provider_manager;

-struct btd_battery *btd_battery_register(const char *path, const char *source);
+struct btd_battery *btd_battery_register(const char *path, const char *source,
+ const char *provider_path);
bool btd_battery_unregister(struct btd_battery *battery);
bool btd_battery_update(struct btd_battery *battery, uint8_t percentage);
+
+struct btd_battery_provider_manager *
+btd_battery_provider_manager_create(struct btd_adapter *adapter);
+void btd_battery_provider_manager_destroy(
+ struct btd_battery_provider_manager *manager);
--
2.26.2

2020-11-30 23:05:09

by Sonny Sasaka

[permalink] [raw]
Subject: [PATCH BlueZ v5 4/7] doc: Add Battery Provider API doc

This patch add the documentation of the Battery Provider which lets
external clients feed battery information to BlueZ if they are able to
decode battery reporting via any profile. BlueZ UI clients can then use
the org.bluez.Battery1 API as a single source of battery information
coming from many different profiles.

Reviewed-by: Miao-chen Chou <[email protected]>

---
doc/battery-api.txt | 55 +++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 55 insertions(+)

diff --git a/doc/battery-api.txt b/doc/battery-api.txt
index dc7dbeda2..9a6b4fd39 100644
--- a/doc/battery-api.txt
+++ b/doc/battery-api.txt
@@ -12,3 +12,58 @@ Object path [variable prefix]/{hci0,hci1,...}/dev_XX_XX_XX_XX_XX_XX
Properties byte Percentage [readonly]

The percentage of battery left as an unsigned 8-bit integer.
+
+ string Source [readonly, optional, experimental]
+
+ Describes where the battery information comes from
+ This property is informational only and may be useful
+ for debugging purposes.
+ Providers from BatteryProvider1 may make use of this
+ property to indicate where the battery report comes from
+ (e.g. "HFP 1.7", "HID", or the profile UUID).
+
+
+Battery Provider Manager hierarchy
+==================================
+A battery provider starts by registering itself as a battery provider with the
+RegisterBatteryProvider method passing an object path as the provider ID. Then,
+it can start exposing org.bluez.BatteryProvider1 objects having the path
+starting with the given provider ID. It can also remove objects at any time.
+The objects and their properties exposed by battery providers will be reflected
+on org.bluez.Battery1 interface.
+
+BlueZ will stop monitoring these exposed and removed objects after
+UnregisterBatteryProvider is called for that provider ID.
+
+Service org.bluez
+Interface org.bluez.BatteryProviderManager1 [experimental]
+Object path /org/bluez/{hci0,hci1,...}
+
+Methods void RegisterBatteryProvider(object provider)
+
+ This registers a battery provider. A registered
+ battery provider can then expose objects with
+ org.bluez.BatteryProvider1 interface described below.
+
+ void UnregisterBatteryProvider(object provider)
+
+ This unregisters a battery provider. After
+ unregistration, the BatteryProvider1 objects provided
+ by this client are ignored by BlueZ.
+
+
+Battery Provider hierarchy
+==========================
+
+Service <client D-Bus address>
+Interface org.bluez.BatteryProvider1 [experimental]
+Object path {provider_root}/{unique battery object path}
+
+Properties Objects provided on this interface contain the same properties
+ as org.bluez.Battery1 interface. Additionally, this interface
+ needs to have the Device property indicating the object path
+ of the device this battery provides.
+
+ object Device [readonly]
+
+ The object path of the device that has this battery.
--
2.26.2

2020-11-30 23:05:09

by Sonny Sasaka

[permalink] [raw]
Subject: [PATCH BlueZ v5 3/7] battery: Add Source property to Battery API

As Battery API will be generalized for other battery reporting
protocols, the Source property is useful for diagnostics purposes.

Reviewed-by: Miao-chen Chou <[email protected]>

---
profiles/battery/battery.c | 3 ++-
src/battery.c | 35 +++++++++++++++++++++++++++++++----
src/battery.h | 2 +-
3 files changed, 34 insertions(+), 6 deletions(-)

diff --git a/profiles/battery/battery.c b/profiles/battery/battery.c
index e1a61dd0b..2478816a4 100644
--- a/profiles/battery/battery.c
+++ b/profiles/battery/battery.c
@@ -126,7 +126,8 @@ static void batt_io_ccc_written_cb(uint16_t att_ecode, void *user_data)
return;
}

- batt->battery = btd_battery_register(device_get_path(batt->device));
+ batt->battery = btd_battery_register(device_get_path(batt->device),
+ "GATT Battery Service");

if (!batt->battery) {
batt_reset(batt);
diff --git a/src/battery.c b/src/battery.c
index 87a6b91fb..8613d6e23 100644
--- a/src/battery.c
+++ b/src/battery.c
@@ -31,8 +31,9 @@
#define BATTERY_MAX_PERCENTAGE 100

struct btd_battery {
- char *path; /* D-Bus object path, owns pointer */
+ char *path; /* D-Bus object path */
uint8_t percentage; /* valid between 0 to 100 inclusively */
+ char *source; /* Descriptive source of the battery info */
};

static struct queue *batteries = NULL;
@@ -62,13 +63,15 @@ static bool match_path(const void *data, const void *user_data)
return g_strcmp0(battery->path, path) == 0;
}

-static struct btd_battery *battery_new(const char *path)
+static struct btd_battery *battery_new(const char *path, const char *source)
{
struct btd_battery *battery;

battery = new0(struct btd_battery, 1);
battery->path = g_strdup(path);
battery->percentage = UINT8_MAX;
+ if (source)
+ battery->source = g_strdup(source);

return battery;
}
@@ -78,6 +81,9 @@ static void battery_free(struct btd_battery *battery)
if (battery->path)
g_free(battery->path);

+ if (battery->source)
+ g_free(battery->source);
+
free(battery);
}

@@ -100,13 +106,34 @@ static gboolean property_percentage_exists(const GDBusPropertyTable *property,
return battery->percentage <= BATTERY_MAX_PERCENTAGE;
}

+static gboolean property_source_get(const GDBusPropertyTable *property,
+ DBusMessageIter *iter, void *data)
+{
+ struct btd_battery *battery = data;
+
+ dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
+ &battery->source);
+
+ return TRUE;
+}
+
+static gboolean property_source_exists(const GDBusPropertyTable *property,
+ void *data)
+{
+ struct btd_battery *battery = data;
+
+ return battery->source != NULL;
+}
+
static const GDBusPropertyTable battery_properties[] = {
{ "Percentage", "y", property_percentage_get, NULL,
property_percentage_exists },
+ { "Source", "s", property_source_get, NULL, property_source_exists,
+ G_DBUS_PROPERTY_FLAG_EXPERIMENTAL },
{}
};

-struct btd_battery *btd_battery_register(const char *path)
+struct btd_battery *btd_battery_register(const char *path, const char *source)
{
struct btd_battery *battery;

@@ -122,7 +149,7 @@ struct btd_battery *btd_battery_register(const char *path)
return NULL;
}

- battery = battery_new(path);
+ battery = battery_new(path, source);
battery_add(battery);

if (!g_dbus_register_interface(btd_get_dbus_connection(), battery->path,
diff --git a/src/battery.h b/src/battery.h
index 9c69b7afa..ff63454cd 100644
--- a/src/battery.h
+++ b/src/battery.h
@@ -10,6 +10,6 @@

struct btd_battery;

-struct btd_battery *btd_battery_register(const char *path);
+struct btd_battery *btd_battery_register(const char *path, const char *source);
bool btd_battery_unregister(struct btd_battery *battery);
bool btd_battery_update(struct btd_battery *battery, uint8_t percentage);
--
2.26.2

2020-11-30 23:11:57

by bluez.test.bot

[permalink] [raw]
Subject: RE: [BlueZ,v5,1/7] battery: Add the internal Battery API

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=393559

---Test result---

##############################
Test: CheckPatch - FAIL
Output:
battery: Add the internal Battery API
ERROR:INITIALISED_STATIC: do not initialise statics to NULL
#71: FILE: src/battery.c:38:
+static struct queue *batteries = NULL;

- total: 1 errors, 0 warnings, 215 lines checked

NOTE: For some of the reported defects, checkpatch may be able to
mechanically convert to the typical style using --fix or --fix-inplace.

"[PATCH] battery: Add the internal Battery API" has style problems, please review.

NOTE: Ignored message types: COMMIT_MESSAGE COMPLEX_MACRO CONST_STRUCT FILE_PATH_CHANGES MISSING_SIGN_OFF PREFER_PACKED SPLIT_STRING SSCANF_TO_KSTRTO

NOTE: If any of the errors are false positives, please report
them to the maintainer, see CHECKPATCH in MAINTAINERS.

test: Add test app for Battery Provider API
ERROR:EXECUTE_PERMISSIONS: do not set execute permissions for source files
#12: FILE: test/example-battery-provider

- total: 1 errors, 0 warnings, 232 lines checked

NOTE: For some of the reported defects, checkpatch may be able to
mechanically convert to the typical style using --fix or --fix-inplace.

"[PATCH] test: Add test app for Battery Provider API" has style problems, please review.

NOTE: Ignored message types: COMMIT_MESSAGE COMPLEX_MACRO CONST_STRUCT FILE_PATH_CHANGES MISSING_SIGN_OFF PREFER_PACKED SPLIT_STRING SSCANF_TO_KSTRTO

NOTE: If any of the errors are false positives, please report
them to the maintainer, see CHECKPATCH in MAINTAINERS.


##############################
Test: CheckGitLint - PASS

##############################
Test: CheckBuild - PASS

##############################
Test: MakeCheck - PASS



---
Regards,
Linux Bluetooth

2020-12-01 00:31:15

by Luiz Augusto von Dentz

[permalink] [raw]
Subject: Re: [BlueZ,v5,1/7] battery: Add the internal Battery API

Hi Sonny,

On Mon, Nov 30, 2020 at 3:13 PM <[email protected]> wrote:
>
> 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=393559
>
> ---Test result---
>
> ##############################
> Test: CheckPatch - FAIL
> Output:
> battery: Add the internal Battery API
> ERROR:INITIALISED_STATIC: do not initialise statics to NULL
> #71: FILE: src/battery.c:38:
> +static struct queue *batteries = NULL;
>
> - total: 1 errors, 0 warnings, 215 lines checked
>
> NOTE: For some of the reported defects, checkpatch may be able to
> mechanically convert to the typical style using --fix or --fix-inplace.
>
> "[PATCH] battery: Add the internal Battery API" has style problems, please review.
>
> NOTE: Ignored message types: COMMIT_MESSAGE COMPLEX_MACRO CONST_STRUCT FILE_PATH_CHANGES MISSING_SIGN_OFF PREFER_PACKED SPLIT_STRING SSCANF_TO_KSTRTO
>
> NOTE: If any of the errors are false positives, please report
> them to the maintainer, see CHECKPATCH in MAINTAINERS.
>
> test: Add test app for Battery Provider API
> ERROR:EXECUTE_PERMISSIONS: do not set execute permissions for source files
> #12: FILE: test/example-battery-provider
>
> - total: 1 errors, 0 warnings, 232 lines checked
>
> NOTE: For some of the reported defects, checkpatch may be able to
> mechanically convert to the typical style using --fix or --fix-inplace.
>
> "[PATCH] test: Add test app for Battery Provider API" has style problems, please review.
>
> NOTE: Ignored message types: COMMIT_MESSAGE COMPLEX_MACRO CONST_STRUCT FILE_PATH_CHANGES MISSING_SIGN_OFF PREFER_PACKED SPLIT_STRING SSCANF_TO_KSTRTO
>
> NOTE: If any of the errors are false positives, please report
> them to the maintainer, see CHECKPATCH in MAINTAINERS.
>
>
> ##############################
> Test: CheckGitLint - PASS
>
> ##############################
> Test: CheckBuild - PASS
>
> ##############################
> Test: MakeCheck - PASS

Applied, thanks.

--
Luiz Augusto von Dentz

2020-12-01 16:27:50

by Bastien Nocera

[permalink] [raw]
Subject: Re: [PATCH BlueZ v5 4/7] doc: Add Battery Provider API doc

On Mon, 2020-11-30 at 13:55 -0800, Sonny Sasaka wrote:
> This patch add the documentation of the Battery Provider which lets
> external clients feed battery information to BlueZ if they are able
> to
> decode battery reporting via any profile. BlueZ UI clients can then
> use
> the org.bluez.Battery1 API as a single source of battery information
> coming from many different profiles.
>
> Reviewed-by: Miao-chen Chou <[email protected]>
>
> ---
>  doc/battery-api.txt | 55
> +++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 55 insertions(+)
>
> diff --git a/doc/battery-api.txt b/doc/battery-api.txt
> index dc7dbeda2..9a6b4fd39 100644
> --- a/doc/battery-api.txt
> +++ b/doc/battery-api.txt
> @@ -12,3 +12,58 @@ Object path  [variable
> prefix]/{hci0,hci1,...}/dev_XX_XX_XX_XX_XX_XX
>  Properties     byte Percentage [readonly]
>  
>                         The percentage of battery left as an unsigned
> 8-bit integer.
> +
> +               string Source [readonly, optional, experimental]
> +
> +                       Describes where the battery information comes
> from
> +                       This property is informational only and may
> be useful
> +                       for debugging purposes.
> +                       Providers from BatteryProvider1 may make use
> of this
> +                       property to indicate where the battery report
> comes from
> +                       (e.g. "HFP 1.7", "HID", or the profile UUID).
> +
> +
> +Battery Provider Manager hierarchy
> +==================================
> +A battery provider starts by registering itself as a battery
> provider with the
> +RegisterBatteryProvider method passing an object path as the
> provider ID. Then,
> +it can start exposing org.bluez.BatteryProvider1 objects having the
> path
> +starting with the given provider ID. It can also remove objects at
> any time.
> +The objects and their properties exposed by battery providers will
> be reflected
> +on org.bluez.Battery1 interface.
> +
> +BlueZ will stop monitoring these exposed and removed objects after
> +UnregisterBatteryProvider is called for that provider ID.
> +
> +Service                org.bluez
> +Interface      org.bluez.BatteryProviderManager1 [experimental]
> +Object path    /org/bluez/{hci0,hci1,...}
> +
> +Methods                void RegisterBatteryProvider(object provider)
> +
> +                       This registers a battery provider. A
> registered
> +                       battery provider can then expose objects with
> +                       org.bluez.BatteryProvider1 interface
> described below.
> +
> +               void UnregisterBatteryProvider(object provider)
> +
> +                       This unregisters a battery provider. After
> +                       unregistration, the BatteryProvider1 objects
> provided
> +                       by this client are ignored by BlueZ.
> +
> +
> +Battery Provider hierarchy
> +==========================
> +
> +Service                <client D-Bus address>
> +Interface      org.bluez.BatteryProvider1 [experimental]
> +Object path    {provider_root}/{unique battery object path}
> +
> +Properties     Objects provided on this interface contain the same properties
> +               as org.bluez.Battery1 interface. Additionally, this interface
> +               needs to have the Device property indicating the object path
> +               of the device this battery provides.

"this interface needs to have a Device property indicating the object
path this battery information pertains to."
or
"this interface needs to have a Device property indicating the object
path the battery information is provided for."

> +
> +               object Device [readonly]
> +
> +                       The object path of the device that has this
> battery.


2020-12-01 16:31:08

by Bastien Nocera

[permalink] [raw]
Subject: Re: [BlueZ,v5,1/7] battery: Add the internal Battery API

On Mon, 2020-11-30 at 16:27 -0800, Luiz Augusto von Dentz wrote:
> Hi Sonny,
> <snip>
> Applied, thanks.

Missed it :/
Any chance you could update the org.bluez.BatteryProvider1 docs as per
my review?

Cheers

2020-12-01 19:16:36

by Sonny Sasaka

[permalink] [raw]
Subject: Re: [BlueZ,v5,1/7] battery: Add the internal Battery API

Hi Bastien,

I will send a separate patch to update the doc.

On Tue, Dec 1, 2020 at 8:29 AM Bastien Nocera <[email protected]> wrote:
>
> On Mon, 2020-11-30 at 16:27 -0800, Luiz Augusto von Dentz wrote:
> > Hi Sonny,
> > <snip>
> > Applied, thanks.
>
> Missed it :/
> Any chance you could update the org.bluez.BatteryProvider1 docs as per
> my review?
>
> Cheers
>