From patchwork Mon Nov 30 20:03:01 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sonny Sasaka X-Patchwork-Id: 334785 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-20.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 64C79C64E7B for ; Mon, 30 Nov 2020 20:04:09 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1A77620825 for ; Mon, 30 Nov 2020 20:04:09 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="nLJBsivC" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729954AbgK3UED (ORCPT ); Mon, 30 Nov 2020 15:04:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43466 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729876AbgK3UED (ORCPT ); Mon, 30 Nov 2020 15:04:03 -0500 Received: from mail-pj1-x1044.google.com (mail-pj1-x1044.google.com [IPv6:2607:f8b0:4864:20::1044]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 29CB7C0613D6 for ; Mon, 30 Nov 2020 12:03:23 -0800 (PST) Received: by mail-pj1-x1044.google.com with SMTP id t12so254162pjq.5 for ; Mon, 30 Nov 2020 12:03:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=4lpY+Sh0Q2EQRKqw0QFdfTD5oYIKy1v6u1iamItNIGQ=; b=nLJBsivCnSzLjZhspzhcAKrNWVgajfq7BHyZ9eLOkgx2D0UViFNnLtUWoxVvW/4mP6 pCfUJhHcEnMPvzVKqmvvyS3fOloFejp1l4FQqZw/F2/YCctTcbbIY3KpeOpp8+RQTLgt gL7BeCpjmylcN6+SFgv7W0gycbV8+DrrjpKnk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=4lpY+Sh0Q2EQRKqw0QFdfTD5oYIKy1v6u1iamItNIGQ=; b=iZ8m92GhBlNghJ4v+MJb0IP6WDeJPkMlnp70cMlf7nIB9K8+G6A0kyLLyfRbRz/Gp0 SDOWU756grcxee9yOk3nw1QYAhoIJpYGbpfuzFhlCmHIPrpf9qm6qAY97iXoJDgDmRfy 2NJDh1XUosQRNm51URlRRRu4YGt0LkYiw8DLgoAg8YpjevtdVaKc0CFpwCa5EOzjydz7 6PsIBqIQyksKx0NENKdiL7tvrscOMB7AdORQU3NcRHmaGvN3wBqP7+J8OVDqr1zBE07U ACggmpt4Z1AyjV8jMyjpIyhAmune0oXs5o4bm4D1h8PPasS8mNyKKtcQXIWVvxoBwp3y QaWg== X-Gm-Message-State: AOAM533LjOg/zb+r47SomtZ21SMX2SVn+5GYVKvhLLZ/tyclx9eH/Gvl UZjj1VwzHqpWdTMA+WWaDunokOVN0YF25Q== X-Google-Smtp-Source: ABdhPJy/bK/ll2i6XW2EwKK1wS07+jD72fttotz7A6geXYhuzc0iDlW+N90Cd/pNfpnD5ZJoyNXyfg== X-Received: by 2002:a17:902:70cc:b029:d7:e8ad:26d4 with SMTP id l12-20020a17090270ccb02900d7e8ad26d4mr20531852plt.33.1606766601759; Mon, 30 Nov 2020 12:03:21 -0800 (PST) Received: from sonnysasaka-chrome.mtv.corp.google.com ([2620:15c:202:201:4a0f:cfff:fe66:e60c]) by smtp.gmail.com with ESMTPSA id y24sm17117184pfn.176.2020.11.30.12.03.20 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 30 Nov 2020 12:03:21 -0800 (PST) From: Sonny Sasaka To: linux-bluetooth@vger.kernel.org Cc: Sonny Sasaka , Daniel Winkler Subject: [PATCH BlueZ v4 1/7] battery: Add the internal Battery API Date: Mon, 30 Nov 2020 12:03:01 -0800 Message-Id: <20201130200307.386410-1-sonnysasaka@chromium.org> X-Mailer: git-send-email 2.26.2 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org 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 --- 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 +#endif + +#define _GNU_SOURCE +#include +#include +#include + +#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); From patchwork Mon Nov 30 20:03:02 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sonny Sasaka X-Patchwork-Id: 335209 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-17.4 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, UNWANTED_LANGUAGE_BODY, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8640DC71156 for ; Mon, 30 Nov 2020 20:04:09 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 49754206DF for ; Mon, 30 Nov 2020 20:04:09 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="Pej01EcF" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729965AbgK3UEG (ORCPT ); Mon, 30 Nov 2020 15:04:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43474 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729876AbgK3UEG (ORCPT ); Mon, 30 Nov 2020 15:04:06 -0500 Received: from mail-pf1-x442.google.com (mail-pf1-x442.google.com [IPv6:2607:f8b0:4864:20::442]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 138C8C0613D2 for ; Mon, 30 Nov 2020 12:03:26 -0800 (PST) Received: by mail-pf1-x442.google.com with SMTP id y7so11103778pfq.11 for ; Mon, 30 Nov 2020 12:03:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=FTvIzX7GCozO99X7SEFRSY0nTaCgNWs0wH/hjeI4kek=; b=Pej01EcF2N7SlQKFDfxgsnC7eW6WJfaKWbVvCmnHUxeLpetZ8ikHPJEfhXgWRnH1UZ yalwD3cw725JW4W32OUGcTMQOnJvKlFKPhiM2wdNCjTL8DCo+YJ/4maVZnMAUBbmko6d P1W9zI5BvZqOD5PQogODLKClwgyjXbnHEHVpk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=FTvIzX7GCozO99X7SEFRSY0nTaCgNWs0wH/hjeI4kek=; b=pT9+yfmR9J5UW0Co9IyZcd8/LQe8X5DgCPhiiV07RkqseRZNEnqU5AuryQRWf6LuAL G6UwuohMIleeIJyjRe7irX0VHXMr2pc7Ra1yqLwZsH5iUAmPelxc5nmgRnH0mBAdq6ze hZFl3RuoI7HwA4KgE40sCZNEM3TztDDs1ihp3RQi/asQayh7B06g1mCiWfeH52cWp+N2 YE84VNdmhmVPIhIno1ydO0a6vvEKNlIX3lkODEdYsoG52vmuOo+Wb28HwSMmgt32Lh9p y/fpR3xo1SvI4Akz9FRYoCEaGY+d3DdUoV6ldzNJM2Gdk7dI5eJKFE6NmcLNaMxciUQv 7o5w== X-Gm-Message-State: AOAM5339AaXKJig5/KVVrPF9oYV7GMY9HANgHgUMWPALYpoU6pxO0Kjm bZ/3ufR17aJHdnQQwXSScy+P0fG0y0bIxg== X-Google-Smtp-Source: ABdhPJzQFA6QG1tF47NQAy8kqLktmPuSl2yvhzIhIr5cnWfAt0IJn/Tsr+tEy4aQPi2p0E3GiZ74GQ== X-Received: by 2002:a62:88d4:0:b029:198:c01:f2a9 with SMTP id l203-20020a6288d40000b02901980c01f2a9mr20153549pfd.45.1606766604728; Mon, 30 Nov 2020 12:03:24 -0800 (PST) Received: from sonnysasaka-chrome.mtv.corp.google.com ([2620:15c:202:201:4a0f:cfff:fe66:e60c]) by smtp.gmail.com with ESMTPSA id y24sm17117184pfn.176.2020.11.30.12.03.23 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 30 Nov 2020 12:03:24 -0800 (PST) From: Sonny Sasaka To: linux-bluetooth@vger.kernel.org Cc: Sonny Sasaka , Daniel Winkler Subject: [PATCH BlueZ v4 2/7] profiles/battery: Refactor to use battery library Date: Mon, 30 Nov 2020 12:03:02 -0800 Message-Id: <20201130200307.386410-2-sonnysasaka@chromium.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20201130200307.386410-1-sonnysasaka@chromium.org> References: <20201130200307.386410-1-sonnysasaka@chromium.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org This refactors profiles/battery to use the internal battery library that handles the D-Bus intricacies so that profiles/battery only handles the GATT BAS concerns. Reviewed-by: Daniel Winkler --- profiles/battery/battery.c | 51 +++++++++++--------------------------- 1 file changed, 15 insertions(+), 36 deletions(-) diff --git a/profiles/battery/battery.c b/profiles/battery/battery.c index 13c80d05c..e1a61dd0b 100644 --- a/profiles/battery/battery.c +++ b/profiles/battery/battery.c @@ -23,14 +23,11 @@ #include -#include "gdbus/gdbus.h" - #include "lib/bluetooth.h" #include "lib/hci.h" #include "lib/sdp.h" #include "lib/uuid.h" -#include "src/dbus-common.h" #include "src/shared/util.h" #include "src/shared/att.h" #include "src/shared/queue.h" @@ -42,6 +39,7 @@ #include "src/profile.h" #include "src/service.h" #include "src/log.h" +#include "src/battery.h" #include "attrib/att.h" #define BATTERY_INTERFACE "org.bluez.Battery1" @@ -50,7 +48,7 @@ /* Generic Attribute/Access Service */ struct batt { - char *path; /* D-Bus path of device */ + struct btd_battery *battery; struct btd_device *device; struct gatt_db *db; struct bt_gatt_client *client; @@ -69,6 +67,8 @@ static void batt_free(struct batt *batt) bt_gatt_client_unref(batt->client); btd_device_unref(batt->device); g_free (batt->initial_value); + if (batt->battery) + btd_battery_unregister(batt->battery); g_free(batt); } @@ -81,11 +81,9 @@ static void batt_reset(struct batt *batt) batt->client = NULL; g_free (batt->initial_value); batt->initial_value = NULL; - if (batt->path) { - g_dbus_unregister_interface(btd_get_dbus_connection(), - batt->path, BATTERY_INTERFACE); - g_free(batt->path); - batt->path = NULL; + if (batt->battery) { + btd_battery_unregister(batt->battery); + batt->battery = NULL; } } @@ -98,8 +96,11 @@ static void parse_battery_level(struct batt *batt, if (batt->percentage != percentage) { batt->percentage = percentage; DBG("Battery Level updated: %d%%", percentage); - g_dbus_emit_property_changed(btd_get_dbus_connection(), batt->path, - BATTERY_INTERFACE, "Percentage"); + if (!batt->battery) { + warn("Trying to update an unregistered battery"); + return; + } + btd_battery_update(batt->battery, batt->percentage); } } @@ -115,22 +116,6 @@ static void batt_io_value_cb(uint16_t value_handle, const uint8_t *value, } } -static gboolean property_get_percentage( - const GDBusPropertyTable *property, - DBusMessageIter *iter, void *data) -{ - struct batt *batt = data; - - dbus_message_iter_append_basic(iter, DBUS_TYPE_BYTE, &batt->percentage); - - return TRUE; -} - -static const GDBusPropertyTable battery_properties[] = { - { "Percentage", "y", property_get_percentage }, - { } -}; - static void batt_io_ccc_written_cb(uint16_t att_ecode, void *user_data) { struct batt *batt = user_data; @@ -141,13 +126,9 @@ static void batt_io_ccc_written_cb(uint16_t att_ecode, void *user_data) return; } - if (g_dbus_register_interface(btd_get_dbus_connection(), - batt->path, BATTERY_INTERFACE, - NULL, NULL, - battery_properties, batt, - NULL) == FALSE) { - error("Unable to register %s interface for %s", - BATTERY_INTERFACE, batt->path); + batt->battery = btd_battery_register(device_get_path(batt->device)); + + if (!batt->battery) { batt_reset(batt); return; } @@ -321,8 +302,6 @@ static int batt_accept(struct btd_service *service) return -1; } - batt->path = g_strdup (device_get_path(device)); - btd_service_connecting_complete(service, 0); return 0; From patchwork Mon Nov 30 20:03:03 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sonny Sasaka X-Patchwork-Id: 335210 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-20.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D16C7C71155 for ; Mon, 30 Nov 2020 20:04:09 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7BDB120825 for ; Mon, 30 Nov 2020 20:04:09 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="MfplBdiP" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729969AbgK3UEH (ORCPT ); Mon, 30 Nov 2020 15:04:07 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43476 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729876AbgK3UEG (ORCPT ); Mon, 30 Nov 2020 15:04:06 -0500 Received: from mail-pj1-x1041.google.com (mail-pj1-x1041.google.com [IPv6:2607:f8b0:4864:20::1041]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7BF5EC0617A6 for ; Mon, 30 Nov 2020 12:03:26 -0800 (PST) Received: by mail-pj1-x1041.google.com with SMTP id p21so377805pjv.0 for ; Mon, 30 Nov 2020 12:03:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=qnjOyIqD02WVy6ZZ2x5bkuCTpr4hjumX5jR/HYlXCOk=; b=MfplBdiPStG1Nw6OuxiwO5EDOzoD11LfAogWHW6vc4EWr8VdobccQFOOb7dKh6l3tr 5/s0964WiZ5Cm4wQ6QB6zdQ/W4DFiLtg0kIE1Z4YdIVvE3MdzjvC8pefgeM2SHBgzKsc RL6m+WBKRAuhpiltToFdD1yKPpJeEs29imNbo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=qnjOyIqD02WVy6ZZ2x5bkuCTpr4hjumX5jR/HYlXCOk=; b=tUUE6a+/2/gEtVRTmGr1ledkB6Mm+HYg5YjPQXEr2VjjvkN28laqYMuT2X5E3XBraX NnrjDA3nKbSJhYonaHfcxaFcCG7FB+gMHoQdpHTjw+pP1EAaTpQr1tPa0TZDfEb9g4c0 tj2dcVYNI6dH4Qsj4ScS5JAvVeA/LWEbvZVqD3j0vyvhj/2MEHAG7leGPgOiUOdm531d Bk12HeVWK5IGGm+n1TIqpuyYbmS2kbmnuYPlsuutkX9H/plkPnUZ8AeAbbBujMaPvnZB 05O1BG5jooltqIS6dEbDA6A93rRNXd/5tlSHY24wZycIIocAHIatasr9ptwwXCl7LDv2 ncuA== X-Gm-Message-State: AOAM533hh7FI+G/NGzEJdfV2b2+rUG3w3K+CWXHy21QwLShL1FyyzGjo sIK+sgdOUzxbX/lGMXpGlwotu3NStQdpYg== X-Google-Smtp-Source: ABdhPJzDJ+tZYpjYzct53xxqkZF6Z8PTbKBJBiNLNBp63/w88iKXXtAaaP/Eu2eV+Zpwzgjffm4x7Q== X-Received: by 2002:a17:90a:c6:: with SMTP id v6mr519587pjd.95.1606766605777; Mon, 30 Nov 2020 12:03:25 -0800 (PST) Received: from sonnysasaka-chrome.mtv.corp.google.com ([2620:15c:202:201:4a0f:cfff:fe66:e60c]) by smtp.gmail.com with ESMTPSA id y24sm17117184pfn.176.2020.11.30.12.03.24 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 30 Nov 2020 12:03:25 -0800 (PST) From: Sonny Sasaka To: linux-bluetooth@vger.kernel.org Cc: Sonny Sasaka , Miao-chen Chou Subject: [PATCH BlueZ v4 3/7] battery: Add Source property to Battery API Date: Mon, 30 Nov 2020 12:03:03 -0800 Message-Id: <20201130200307.386410-3-sonnysasaka@chromium.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20201130200307.386410-1-sonnysasaka@chromium.org> References: <20201130200307.386410-1-sonnysasaka@chromium.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org As Battery API will be generalized for other battery reporting protocols, the Source property is useful for diagnostics purposes. Reviewed-by: Miao-chen Chou --- 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); From patchwork Mon Nov 30 20:03:04 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sonny Sasaka X-Patchwork-Id: 334784 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-20.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 03CFEC83011 for ; Mon, 30 Nov 2020 20:04:10 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B0B73206DF for ; Mon, 30 Nov 2020 20:04:09 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="ZHXrymvG" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729972AbgK3UEJ (ORCPT ); Mon, 30 Nov 2020 15:04:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43484 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727685AbgK3UEI (ORCPT ); Mon, 30 Nov 2020 15:04:08 -0500 Received: from mail-pf1-x42a.google.com (mail-pf1-x42a.google.com [IPv6:2607:f8b0:4864:20::42a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A63E8C0617A7 for ; Mon, 30 Nov 2020 12:03:28 -0800 (PST) Received: by mail-pf1-x42a.google.com with SMTP id b10so9722671pfo.4 for ; Mon, 30 Nov 2020 12:03:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=kh50OcTzrFmgdURg93OTnhuU/F5aplwBjH9orxXDq7k=; b=ZHXrymvG+HCPPaUA8Jhpy/HpmMT+jmJwkdmRokoKjuNFPrTEnbdmu0msgOTbViU7RB tQcV1VUKUGiibzv3Vbi556w+G9MIOfqkGt+aqxA5JRhMZqc/NhHKkpC6ZeMgsFWsS/ui AVTZ0ncsX6ONYI/zZjwGJ/L8xqLnXL/yHZ2is= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=kh50OcTzrFmgdURg93OTnhuU/F5aplwBjH9orxXDq7k=; b=oi8PHih0niGXernLP5+Uk74/PMTadh594LM5fp8ijIBn4N8x43j579k4OF9YkdYX1P n98z26GDcX0Cu8USDbu0JXdaH1S4BmdIeXrfHp56lUkbojXCvsCod34mRqLBufrvgP6J IbgTYkxoYWkQCQuluGEgS0UlL9Ird/ecw662lWVDa0+piYg2OkPvN2hBvGWn72ADxOcN j04ecRxDMOmvLcigdFp8R39rP2omceGSeRi6G1dLw/mO3tdN9mNzJYadn8p8LA5zhtjX HRXpOCyt0ckuqhNbzjXawkO3ZXAFMy+kAb7FLJQ0DdC930PI7AJIRtNykCn+p7P7AMyC Uzhw== X-Gm-Message-State: AOAM5301ByOoI2oleeRiqC0CGwHMO9SH2wsrMzWqsW/eLXciUVqX7/pa ujIlo9byb3n9x/HPP3OA+mYEecEK4SAjYw== X-Google-Smtp-Source: ABdhPJwedhvIlyrWsZZe6OFhadYr/GypNyC5QIkKLD5bEQv2q5wOWjUAkufjK/aGnT8eTivh3zlX7w== X-Received: by 2002:aa7:8254:0:b029:19a:c192:5ddc with SMTP id e20-20020aa782540000b029019ac1925ddcmr13114659pfn.26.1606766607300; Mon, 30 Nov 2020 12:03:27 -0800 (PST) Received: from sonnysasaka-chrome.mtv.corp.google.com ([2620:15c:202:201:4a0f:cfff:fe66:e60c]) by smtp.gmail.com with ESMTPSA id y24sm17117184pfn.176.2020.11.30.12.03.25 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 30 Nov 2020 12:03:26 -0800 (PST) From: Sonny Sasaka To: linux-bluetooth@vger.kernel.org Cc: Sonny Sasaka , Miao-chen Chou Subject: [PATCH BlueZ v4 4/7] doc: Add Battery Provider API doc Date: Mon, 30 Nov 2020 12:03:04 -0800 Message-Id: <20201130200307.386410-4-sonnysasaka@chromium.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20201130200307.386410-1-sonnysasaka@chromium.org> References: <20201130200307.386410-1-sonnysasaka@chromium.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org 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 --- 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 +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. From patchwork Mon Nov 30 20:03:05 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sonny Sasaka X-Patchwork-Id: 335208 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-20.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5040FC63777 for ; Mon, 30 Nov 2020 20:04:28 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id F0A6E206C1 for ; Mon, 30 Nov 2020 20:04:27 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="kkoY7A9W" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387730AbgK3UEK (ORCPT ); Mon, 30 Nov 2020 15:04:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43490 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727685AbgK3UEK (ORCPT ); Mon, 30 Nov 2020 15:04:10 -0500 Received: from mail-pf1-x444.google.com (mail-pf1-x444.google.com [IPv6:2607:f8b0:4864:20::444]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DA527C0613D3 for ; Mon, 30 Nov 2020 12:03:29 -0800 (PST) Received: by mail-pf1-x444.google.com with SMTP id b10so9722708pfo.4 for ; Mon, 30 Nov 2020 12:03:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=2AOX3JjGk6r22Q8PnBEHUiCVd9fNzB32v3Zc3lXH8gg=; b=kkoY7A9Wj8u16rFgLTo2scZDrmJD5DwmXdGY7oYFMpZ39hy7QfX0aPyChLgG6JPmDW i+miVUZnk/5DnrPDzSKo3nUvQRToWGxaHqTbVl2PKGCJr/HSs3eZ3dmChm+pkrQc3UlI 0uL2Tebw+GtcO8RgouMsDuPdbCZleg4qPoS6A= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=2AOX3JjGk6r22Q8PnBEHUiCVd9fNzB32v3Zc3lXH8gg=; b=S+oLkfRPJiVc+4cYocoaOF474cFyIXlQlFTT128rbpR+iujHTcvUpFkLI3QQxA26NA pkxIblmSTlcLxbDy2jbtfYbn4jcBQoVVVwPJ8lrlln7iCYgkRZ1guFHxOepY5GLTJXwF vFiOvImgjte+a3dC+Hrkyjs+b5w6Q7z/irTTisiG8BK3e65iJrdianKAddfXziJvL0/t IdF0HvdHv343SSVZTmNvHkobNbxZmplpuqbc0ekg2MidTKybux3R+PmH7FUGusPPwYvW 3mqKVJiPE7mfJmRUoWnMeKhiQiOugH8Z+JfS7CKZ1mArNPVtw2ka1TQplhXBubsFuEnK MDow== X-Gm-Message-State: AOAM5331TnmLI5il5zTNb2/5z5KW36Go4yhsnsE6FMmQrQpWp9JOUI7n 1NrZxhZGG3ognENKb60MM5YtFM8nb0O2ew== X-Google-Smtp-Source: ABdhPJxEq7JTxONZIVzG4BzbVJ81hh9UjdyfOpLpLak/8iRJw8YkrWCfaY4+CiD+CNBQW3cczu+f/Q== X-Received: by 2002:a05:6a00:225c:b029:18b:d208:a366 with SMTP id i28-20020a056a00225cb029018bd208a366mr20258769pfu.5.1606766608315; Mon, 30 Nov 2020 12:03:28 -0800 (PST) Received: from sonnysasaka-chrome.mtv.corp.google.com ([2620:15c:202:201:4a0f:cfff:fe66:e60c]) by smtp.gmail.com with ESMTPSA id y24sm17117184pfn.176.2020.11.30.12.03.27 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 30 Nov 2020 12:03:27 -0800 (PST) From: Sonny Sasaka To: linux-bluetooth@vger.kernel.org Cc: Sonny Sasaka , Miao-chen Chou Subject: [PATCH BlueZ v4 5/7] test: Add test app for Battery Provider API Date: Mon, 30 Nov 2020 12:03:05 -0800 Message-Id: <20201130200307.386410-5-sonnysasaka@chromium.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20201130200307.386410-1-sonnysasaka@chromium.org> References: <20201130200307.386410-1-sonnysasaka@chromium.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org The python test app simulates an application registering to BlueZ as a Battery Provider providing three fake batteries drained periodically. Reviewed-by: Miao-chen Chou --- test/example-battery-provider | 232 ++++++++++++++++++++++++++++++++++ 1 file changed, 232 insertions(+) create mode 100755 test/example-battery-provider diff --git a/test/example-battery-provider b/test/example-battery-provider new file mode 100755 index 000000000..1522a5e07 --- /dev/null +++ b/test/example-battery-provider @@ -0,0 +1,232 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: LGPL-2.1-or-later + +import dbus +import dbus.exceptions +import dbus.mainloop.glib +import dbus.service + +try: + from gi.repository import GObject +except ImportError: + import gobject as GObject +import sys + +mainloop = None +app = None +bus = None + +BLUEZ_SERVICE_NAME = 'org.bluez' +DBUS_OM_IFACE = 'org.freedesktop.DBus.ObjectManager' +DBUS_PROP_IFACE = 'org.freedesktop.DBus.Properties' + +BATTERY_PROVIDER_MANAGER_IFACE = 'org.bluez.BatteryProviderManager1' +BATTERY_PROVIDER_IFACE = 'org.bluez.BatteryProvider1' +BATTERY_PROVIDER_PATH = '/path/to/provider' + +BATTERY_PATH1 = '11_11_11_11_11_11' +BATTERY_PATH2 = '22_22_22_22_22_22' +BATTERY_PATH3 = '33_33_33_33_33_33' + +class InvalidArgsException(dbus.exceptions.DBusException): + _dbus_error_name = 'org.freedesktop.DBus.Error.InvalidArgs' + + +class Application(dbus.service.Object): + def __init__(self, bus): + self.path = BATTERY_PROVIDER_PATH + self.services = [] + self.batteries = [] + dbus.service.Object.__init__(self, bus, self.path) + + def get_path(self): + return dbus.ObjectPath(self.path) + + def add_battery(self, battery): + self.batteries.append(battery) + self.InterfacesAdded(battery.get_path(), battery.get_properties()) + GObject.timeout_add(1000, drain_battery, battery) + + def remove_battery(self, battery): + self.batteries.remove(battery) + self.InterfacesRemoved(battery.get_path(), [BATTERY_PROVIDER_IFACE]) + + @dbus.service.method(DBUS_OM_IFACE, out_signature='a{oa{sa{sv}}}') + def GetManagedObjects(self): + response = {} + print('GetManagedObjects called') + + for battery in self.batteries: + response[battery.get_path()] = battery.get_properties() + + return response + + @dbus.service.signal(DBUS_OM_IFACE, signature='oa{sa{sv}}') + def InterfacesAdded(self, object_path, interfaces_and_properties): + return + + @dbus.service.signal(DBUS_OM_IFACE, signature='oas') + def InterfacesRemoved(self, object_path, interfaces): + return + + +class Battery(dbus.service.Object): + """ + org.bluez.BatteryProvider1 interface implementation + """ + def __init__(self, bus, dev, percentage, source = None): + self.path = BATTERY_PROVIDER_PATH + '/dev_' + dev + self.dev_path = '/org/bluez/hci0/dev_' + dev + self.bus = bus + self.percentage = percentage + self.source = source + dbus.service.Object.__init__(self, bus, self.path) + + def get_battery_properties(self): + properties = {} + if self.percentage != None: + properties['Percentage'] = dbus.Byte(self.percentage) + if self.source != None: + properties['Source'] = self.source + properties['Device'] = dbus.ObjectPath(self.dev_path) + return properties + + def get_properties(self): + return { BATTERY_PROVIDER_IFACE: self.get_battery_properties() } + + def get_path(self): + return dbus.ObjectPath(self.path) + + def set_percentage(self, percentage): + if percentage < 0 or percentage > 100: + print('percentage not valid') + return + + self.percentage = percentage + print('battery %s percentage %d' % (self.path, self.percentage)) + self.PropertiesChanged( + BATTERY_PROVIDER_IFACE, self.get_battery_properties()) + + @dbus.service.method(DBUS_PROP_IFACE, + in_signature='s', + out_signature='a{sv}') + def GetAll(self, interface): + if interface != BATTERY_PROVIDER_IFACE: + raise InvalidArgsException() + + return self.get_properties()[BATTERY_PROVIDER_IFACE] + + @dbus.service.signal(DBUS_PROP_IFACE, signature='sa{sv}') + def PropertiesChanged(self, interface, properties): + return + + +def add_late_battery(): + app.add_battery(Battery(bus, BATTERY_PATH3, 70, 'Protocol 2')) + + +def drain_battery(battery): + new_percentage = 100 + if battery.percentage != None: + new_percentage = battery.percentage - 5 + if new_percentage < 0: + new_percentage = 0 + + battery.set_percentage(new_percentage) + + if new_percentage <= 0: + return False + + return True + +def register_provider_cb(): + print('Battery Provider registered') + + # Battery added early right after RegisterBatteryProvider succeeds + app.add_battery(Battery(bus, BATTERY_PATH2, None)) + # Battery added later + GObject.timeout_add(5000, add_late_battery) + + +def register_provider_error_cb(error): + print('Failed to register Battery Provider: ' + str(error)) + mainloop.quit() + + +def find_manager(bus): + remote_om = dbus.Interface(bus.get_object(BLUEZ_SERVICE_NAME, '/'), + DBUS_OM_IFACE) + objects = remote_om.GetManagedObjects() + + for o, props in objects.items(): + if BATTERY_PROVIDER_MANAGER_IFACE in props.keys(): + return o + + return None + + +def unregister_provider_cb(): + print('Battery Provider unregistered') + + +def unregister_provider_error_cb(error): + print('Failed to unregister Battery Provider: ' + str(error)) + + +def unregister_battery_provider(battery_provider_manager): + battery_provider_manager.UnregisterBatteryProvider(BATTERY_PROVIDER_PATH, + reply_handler=unregister_provider_cb, + error_handler=unregister_provider_error_cb) + + +def remove_battery(app, battery): + app.remove_battery(battery) + + +""" +Simulates an application registering to BlueZ as a Battery Provider providing +fake batteries drained periodically. +""" +def main(): + global mainloop, bus, app + + dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) + + bus = dbus.SystemBus() + + manager_path = find_manager(bus) + if not manager_path: + print('BatteryProviderManager1 interface not found') + return + + print('BatteryProviderManager1 path = ', manager_path) + + battery_provider_manager = dbus.Interface( + bus.get_object(BLUEZ_SERVICE_NAME, manager_path), + BATTERY_PROVIDER_MANAGER_IFACE) + + app = Application(bus) + + # Battery pre-added before RegisterBatteryProvider + battery1 = Battery(bus, BATTERY_PATH1, 87, 'Protocol 1') + app.add_battery(battery1) + + mainloop = GObject.MainLoop() + + print('Registering Battery Provider...') + + battery_provider_manager.RegisterBatteryProvider(BATTERY_PROVIDER_PATH, + reply_handler=register_provider_cb, + error_handler=register_provider_error_cb) + + # Unregister the Battery Provider after an arbitrary amount of time + GObject.timeout_add( + 12000, unregister_battery_provider, battery_provider_manager) + # Simulate battery removal by a provider + GObject.timeout_add(8000, remove_battery, app, battery1) + + mainloop.run() + + +if __name__ == '__main__': + main() From patchwork Mon Nov 30 20:03:06 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sonny Sasaka X-Patchwork-Id: 335207 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-20.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D0E6CC64E7B for ; Mon, 30 Nov 2020 20:04:28 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 766E8206C1 for ; Mon, 30 Nov 2020 20:04:28 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="iW/DhWZ4" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388322AbgK3UER (ORCPT ); Mon, 30 Nov 2020 15:04:17 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43496 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726400AbgK3UER (ORCPT ); Mon, 30 Nov 2020 15:04:17 -0500 Received: from mail-pf1-x444.google.com (mail-pf1-x444.google.com [IPv6:2607:f8b0:4864:20::444]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 00B7AC061A04 for ; Mon, 30 Nov 2020 12:03:31 -0800 (PST) Received: by mail-pf1-x444.google.com with SMTP id w202so11116584pff.10 for ; Mon, 30 Nov 2020 12:03:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Osr2t5lC/rtahR1ayaZEYydm7foHMd951I0Pg1nz0qM=; b=iW/DhWZ47eo8QPMYbW1mU+bhKdB6vsNEkVUh8iSJpIwUKSItYX1lX/Dl9X2JynAE8T /19cOsuqPavOTMlyVBDkioNyq6aNNMjQZ4ENn1nXzT36iTHwNaXJDLCbXqef1S9Yextk metu5ERLMR3pcKoGG3UHfLQj+B/Yiu3FG7MdU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Osr2t5lC/rtahR1ayaZEYydm7foHMd951I0Pg1nz0qM=; b=Wi6JzMSO3ugBoToiTgC825w/Wk1BbvTcCe6+OtZ+DU59UzAF2+2oy5rfPgwYKDjvxt jjEFVxwrrkQ3ZAQW0otp1jfyA7GQ+VQcE1oOlvFIVF7+JF46bDk8yOY9ESaT4/PVyhWf DsYv4oHaYktshgmLzkin1I6r5UjmteJRELVlu/EUaOoh1VhAaa4Ggbw25lAPgLSJHh6/ KrLNNLF6hPpwoEDNYIRF6rqBrDvq/Di7JIKtadkUA98mmq467DzD6/JaXN7uSwdbwUV1 +nyXlj/juq1NVe9UxCOLrlZAJ6XJyaeF5etpqDs+4USszPu6x8Wcpo960drW7gfVPEKS ndgg== X-Gm-Message-State: AOAM5316NoIkLaxFSiNrgfONqIO/tfvqrjtiEZPqoZ9HhAH0GSap8wI0 cbdufjWyzNoLybxwhNOq05kxeXVr5wxVnA== X-Google-Smtp-Source: ABdhPJwESw0IpGDiRVqvO5GBtCy41upiKjPoJ+CJAwB6qHkEvIjLhA4Bs2cvQZ5D1IyH2Ym8m8AYrQ== X-Received: by 2002:a63:5418:: with SMTP id i24mr19168570pgb.165.1606766609656; Mon, 30 Nov 2020 12:03:29 -0800 (PST) Received: from sonnysasaka-chrome.mtv.corp.google.com ([2620:15c:202:201:4a0f:cfff:fe66:e60c]) by smtp.gmail.com with ESMTPSA id y24sm17117184pfn.176.2020.11.30.12.03.28 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 30 Nov 2020 12:03:28 -0800 (PST) From: Sonny Sasaka To: linux-bluetooth@vger.kernel.org Cc: Sonny Sasaka , Miao-chen Chou Subject: [PATCH BlueZ v4 6/7] adapter: Add a public function to find a device by path Date: Mon, 30 Nov 2020 12:03:06 -0800 Message-Id: <20201130200307.386410-6-sonnysasaka@chromium.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20201130200307.386410-1-sonnysasaka@chromium.org> References: <20201130200307.386410-1-sonnysasaka@chromium.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org The public function is motivated by the Battery Provider API code which needs to probe whether a device exists. Reviewed-by: Miao-chen Chou --- src/adapter.c | 33 ++++++++++++++++++++++++--------- src/adapter.h | 2 ++ 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/adapter.c b/src/adapter.c index 56d0c6eaa..03d9d29e9 100644 --- a/src/adapter.c +++ b/src/adapter.c @@ -872,6 +872,30 @@ struct btd_device *btd_adapter_find_device(struct btd_adapter *adapter, return device; } +static int device_path_cmp(gconstpointer a, gconstpointer b) +{ + const struct btd_device *device = a; + const char *path = b; + const char *dev_path = device_get_path(device); + + return strcasecmp(dev_path, path); +} + +struct btd_device *btd_adapter_find_device_by_path(struct btd_adapter *adapter, + const char *path) +{ + GSList *list; + + if (!adapter) + return NULL; + + list = g_slist_find_custom(adapter->devices, path, device_path_cmp); + if (!list) + return NULL; + + return list->data; +} + static void uuid_to_uuid128(uuid_t *uuid128, const uuid_t *uuid) { if (uuid->type == SDP_UUID16) @@ -3192,15 +3216,6 @@ static gboolean property_get_roles(const GDBusPropertyTable *property, return TRUE; } -static int device_path_cmp(gconstpointer a, gconstpointer b) -{ - const struct btd_device *device = a; - const char *path = b; - const char *dev_path = device_get_path(device); - - return strcasecmp(dev_path, path); -} - static DBusMessage *remove_device(DBusConnection *conn, DBusMessage *msg, void *user_data) { diff --git a/src/adapter.h b/src/adapter.h index e5750a37b..60b5e3bcc 100644 --- a/src/adapter.h +++ b/src/adapter.h @@ -83,6 +83,8 @@ sdp_list_t *btd_adapter_get_services(struct btd_adapter *adapter); struct btd_device *btd_adapter_find_device(struct btd_adapter *adapter, const bdaddr_t *dst, uint8_t dst_type); +struct btd_device *btd_adapter_find_device_by_path(struct btd_adapter *adapter, + const char *path); const char *adapter_get_path(struct btd_adapter *adapter); const bdaddr_t *btd_adapter_get_address(struct btd_adapter *adapter); From patchwork Mon Nov 30 20:03:07 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sonny Sasaka X-Patchwork-Id: 334783 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-20.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 91606C64E8A for ; Mon, 30 Nov 2020 20:04:28 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 37CBE206DF for ; Mon, 30 Nov 2020 20:04:28 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="XPvRfoJS" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388333AbgK3UEN (ORCPT ); Mon, 30 Nov 2020 15:04:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43504 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388322AbgK3UEN (ORCPT ); Mon, 30 Nov 2020 15:04:13 -0500 Received: from mail-pg1-x535.google.com (mail-pg1-x535.google.com [IPv6:2607:f8b0:4864:20::535]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B93F5C061A47 for ; Mon, 30 Nov 2020 12:03:32 -0800 (PST) Received: by mail-pg1-x535.google.com with SMTP id w16so10725436pga.9 for ; Mon, 30 Nov 2020 12:03:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Yzih2yHkUv+QElMJRHZIWXlalNtCAkPaD/ASJVteeio=; b=XPvRfoJSdycj5/QWkHfEVifSqVPRbdurJt2SLL6oYo0srUg9mOf0FiRg9Q08IAITsY BDA/sjn17ISQBlcdNPV9Royz58lF8vZXsfw56J5TzKKRApxBzxSaw9h+Pxov8tJXsQeF ki17KMWBiM+RKWooHvrdUXJPm5MhMvQgC05FE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Yzih2yHkUv+QElMJRHZIWXlalNtCAkPaD/ASJVteeio=; b=dUixrTMnRer65IuV4FYlSH9OtcRUZfy6QJAR2TLTMQTZrd7pw5LCc+F9LAG4lhaE0H 3HVz3fphbL66QR9jGlUgq5h68f0vp2U71cB2c4Bx9aBgh7KYWgej0OR0PimiTpHPdhlz jlKNYUOx2dVIEvbxKLbrvhSvIgsNs/mHwP3DHgcLmQjMmUDyZu1GqYeqcTQgLR1ABNlS yppL3Pf/GmepuGKW7Pz56QA/yaU9rOI9JXASXqI6RDID8ukF0ac6gY8JH+9VgwM2wLD/ QlNUgt7atg8Kf1G9hldPt00x/UoWB9s3s9UAtuGdyQDgeo07PZtzo+Exz3Hjk8cKWRS8 hgnw== X-Gm-Message-State: AOAM5315ieyX7CtNcK+ne+R2odZiLefuM4g95p/rM7V0rubO+XdJ3Su6 9iVYp2Gyk5Fg9m2pYjMFhoJXROt1QYVWzQ== X-Google-Smtp-Source: ABdhPJyI3nfrxUMUdDgLMTB58RJqtKFYWgJLPgCxGG6UAifA1N+5UD0BHWxZB/uIuBQ6A/bsM+IcTQ== X-Received: by 2002:a65:6383:: with SMTP id h3mr19153054pgv.309.1606766610743; Mon, 30 Nov 2020 12:03:30 -0800 (PST) Received: from sonnysasaka-chrome.mtv.corp.google.com ([2620:15c:202:201:4a0f:cfff:fe66:e60c]) by smtp.gmail.com with ESMTPSA id y24sm17117184pfn.176.2020.11.30.12.03.29 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 30 Nov 2020 12:03:30 -0800 (PST) From: Sonny Sasaka To: linux-bluetooth@vger.kernel.org Cc: Sonny Sasaka , Miao-chen Chou Subject: [PATCH BlueZ v4 7/7] battery: Implement Battery Provider API Date: Mon, 30 Nov 2020 12:03:07 -0800 Message-Id: <20201130200307.386410-7-sonnysasaka@chromium.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20201130200307.386410-1-sonnysasaka@chromium.org> References: <20201130200307.386410-1-sonnysasaka@chromium.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org 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 --- 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);