From patchwork Wed Nov 11 00:37:16 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sonny Sasaka X-Patchwork-Id: 11895909 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=-9.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, 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 889B7C55ABD for ; Wed, 11 Nov 2020 00:37:47 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1AF7020679 for ; Wed, 11 Nov 2020 00:37:47 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="iYYhzMAZ" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732252AbgKKAhp (ORCPT ); Tue, 10 Nov 2020 19:37:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42910 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732209AbgKKAho (ORCPT ); Tue, 10 Nov 2020 19:37:44 -0500 Received: from mail-pl1-x641.google.com (mail-pl1-x641.google.com [IPv6:2607:f8b0:4864:20::641]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AAD8FC0613D1 for ; Tue, 10 Nov 2020 16:37:44 -0800 (PST) Received: by mail-pl1-x641.google.com with SMTP id k7so66431plk.3 for ; Tue, 10 Nov 2020 16:37:44 -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=+bk/zaKkKssK84GHRxHMA/rKqU6DwlFfzMwIUdK3ls4=; b=iYYhzMAZQOjccJZRdNK57oYyNbz2oKcO+ATE6ClUr9YHdlDo/YSX2qk6H5EA3Wd/D+ EXMyXq/MG4RHeBbrwIkS1VN4eePJlGelvm84XRvkDZvXDsvYPy+cMfl2MY2+LTzFsCYp YPd3PLmhTBgy/YiPflrdVVW8Otqq9xmfmpves= 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=+bk/zaKkKssK84GHRxHMA/rKqU6DwlFfzMwIUdK3ls4=; b=UhmBg+iY3YV1D/b9ekpnLgGUa3TC6UAMse2kDNQqWkU6XCVTd/NraIvgYiWLo/cknR 0rzU1TZ4nOC+TLEfYesmBQFK0WvfS7digNdAkAp+FoEjmHxKD2sFPlgP6TpB8Z4OxgDo RJZjZ6wIylxEWKOQHz5aBVv3NnLYRNRSuv9RoCczbHPmd/PB3uzcfUztfJF62y+JuiWO 86ov5kG/JRjjc5wWto3+CPlDH8G3tnow7rbXcrxSFrgkByEuYOoMV/RAX43r4uRGw6NG d6QtX49GUB90p24UY+i47NI1ujeSdTBnbBCA58m+RElcW93WhwH7DPrQ0dAZha9aLDNb D3Kg== X-Gm-Message-State: AOAM531UYgvCktKpMRZXnPmDDTTVenJm8/vfxf9UA+8qXH6j1gas81Yf Z2a/hVXwNE0KCr1gDh/EMhmW7vmH513Hzw== X-Google-Smtp-Source: ABdhPJwOjrCscMRc9NSLECI6aVs7rRP1VODmCaJLdNnhuO2+tDCIAdywzTfYZoCGTNyLjjsz/OJXxg== X-Received: by 2002:a17:902:7606:b029:d3:d2dd:2b3b with SMTP id k6-20020a1709027606b02900d3d2dd2b3bmr19118740pll.67.1605055063946; Tue, 10 Nov 2020 16:37:43 -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 b16sm180655pju.16.2020.11.10.16.37.42 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 10 Nov 2020 16:37:43 -0800 (PST) From: Sonny Sasaka To: linux-bluetooth@vger.kernel.org Cc: Sonny Sasaka , Miao-chen Chou Subject: [PATCH BlueZ 3/7] battery: Add Source property to Battery API Date: Tue, 10 Nov 2020 16:37:16 -0800 Message-Id: <20201111003720.1727-4-sonnysasaka@chromium.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20201111003720.1727-1-sonnysasaka@chromium.org> References: <20201111003720.1727-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 b2adf41fa..a2769f687 100644 --- a/src/battery.c +++ b/src/battery.c @@ -30,8 +30,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; @@ -59,13 +60,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; } @@ -75,6 +78,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); } @@ -97,13 +103,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; @@ -119,7 +146,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);