Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp1679086ybt; Thu, 2 Jul 2020 11:04:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwW20h03Rx2LTD/kWyqR69H22SlBZ3qAIpGIvCXCvTnKiaji3aP8ttAMxu/+W7kzVFMtNN5 X-Received: by 2002:a17:906:7c3:: with SMTP id m3mr27759601ejc.30.1593713054797; Thu, 02 Jul 2020 11:04:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1593713054; cv=none; d=google.com; s=arc-20160816; b=lXJLZIlZbXHOC//OThNo/3kfzANpA6D+8tu+HXBAn09TbMIcWd2HyTpsOCkt67XI5c xeVLeMOmrCP0BOVPSLHOoSaARHbafeMuS/69T4FeiB5JvQ9V0v4ocw0+R4P4AmbT7KUI v63+SLtrpBTqrvtmnWe/MJYAsgQEiuXEPHGCEM5TZWJ602GV0erQfF5gHaIUfUmyUuNB KmsSdJv2N0Ro+74S3NYH/tu+ozp69jL4rf3LDJUWt5eMSNWIGZy8k4+UGVD5PX6KrTg7 0LAazcKBfHj3SLz7KwHuxqIBCJG067qTHh3WVFWvy13ytO1Z4CjCOgoJP9JeRUM/pnGV 9Q8A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature; bh=PhEY+ZLmgzCOdXaoAmlFY6h7wEgm4AErb9qvafvGMiM=; b=zhhmEMPnD45WAxQX6P70+G3YYd+pIjHS4Fqgx8T/CoGUEBnEXXRRgb9ugGkukatz3I KRjZygCstoD9W6iCsK+gQ1Bcg/9UOnyvAHjVnvgYnz/kAmrXZJrFxOo1z3rCtp0ZVIJ5 YXWVYO2JzlLUp2VWFRKXmfO9wzwmZQ7zR8W8wBvfFkBh6WV7XidrMoz3COcqesy9C9cq 81wKbBbFi+S0QYz6TFMtabFPaVai14N0efKCSeQchhsdDtohfEKEkC59sjeNkGwCQ30g 6Rn0QwqE/KJKO14dOViX4cQmSDt1sD689RgtP8uS/BczQVgf0w6Fj/AUfV8F3A65d1ws qskA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=HxuUPhip; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d14si5907303eje.95.2020.07.02.11.03.36; Thu, 02 Jul 2020 11:04:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=HxuUPhip; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727976AbgGBSCM (ORCPT + 99 others); Thu, 2 Jul 2020 14:02:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52308 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727968AbgGBSCM (ORCPT ); Thu, 2 Jul 2020 14:02:12 -0400 Received: from mail-io1-xd44.google.com (mail-io1-xd44.google.com [IPv6:2607:f8b0:4864:20::d44]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3848FC08C5C1 for ; Thu, 2 Jul 2020 11:02:12 -0700 (PDT) Received: by mail-io1-xd44.google.com with SMTP id v6so16164320iob.4 for ; Thu, 02 Jul 2020 11:02:12 -0700 (PDT) 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; bh=PhEY+ZLmgzCOdXaoAmlFY6h7wEgm4AErb9qvafvGMiM=; b=HxuUPhipxqBckpMBVK/bvcxHX7JLyRH3ekGJZnJVovoFYP4nVz734pEpCQzLS/YVCh soUiQTwzBIYcetILqZrKXHJqxyUots4KojV4muOslHISdEYFeqRhxFtYET3tdkDkhtvz ivWQkZegip1ibgkG+5FaLP9EjyDmJrCtH7vsk= 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; bh=PhEY+ZLmgzCOdXaoAmlFY6h7wEgm4AErb9qvafvGMiM=; b=q/piSfzxRUCRkA7BezaqJMquoIBBHgTNNWqQ+9ETn9WosrPlB8N4mTej7wCMu26h8X HjvJgd/3ogP3rl8As6ax8+lmU00p/B8nJS0bc7FUdXeZRT0vSS/BmR19KIXpTN2lxnXV CvOuQxR4i+rL+jBwFEcDvNGZk8AzMwmm8v1JYsEVy0GRXvOjk7xiud2wWqYowf14pMcz z8e5tULisGNWDxZ/OtwSG8vPStIPbGBAgyBRwpGYWUythxfr0YTEfTcRteb7Hu9CKoOY WTv24TQwXmPyG/h2Pn/hbPz0oxGG6SqMTKxSgQGmPiZPNVPJ/HtIHFaSLGqqkEag/kNb XFlA== X-Gm-Message-State: AOAM530xc+/et7hOZFibicOLSxJgoffYGHuaBHksjDbLtZJuB/q/7sC/ TYwa3YJZi7fLHOH3MWVr5laMB8HClPn3Hg== X-Received: by 2002:a05:6602:2d89:: with SMTP id k9mr8592389iow.41.1593712931214; Thu, 02 Jul 2020 11:02:11 -0700 (PDT) Received: from localhost.localdomain (c-73-231-41-185.hsd1.ca.comcast.net. [73.231.41.185]) by smtp.googlemail.com with ESMTPSA id u6sm5097647ilg.32.2020.07.02.11.02.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Jul 2020 11:02:10 -0700 (PDT) From: Sonny Sasaka To: linux-bluetooth@vger.kernel.org Cc: Sonny Sasaka Subject: [PATCH v3] client: Add battery percentage to info command Date: Thu, 2 Jul 2020 11:01:55 -0700 Message-Id: <20200702180155.24260-1-sonnysasaka@chromium.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: References: Sender: linux-bluetooth-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org This adds the Battery Percentage to the info command based on org.bluez.Battery1 API. Example usage: [bluetooth]# info XX:XX:XX:XX:XX:XX Device XX:XX:XX:XX:XX:XX (random) Name: ... Alias: ... ... Modalias: ... Battery Percentage: 0x64 (100) --- client/main.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/client/main.c b/client/main.c index 422da5593..c0b351aed 100644 --- a/client/main.c +++ b/client/main.c @@ -65,6 +65,7 @@ static struct adapter *default_ctrl; static GDBusProxy *default_dev; static GDBusProxy *default_attr; static GList *ctrl_list; +static GList *battery_proxies; static const char *agent_arguments[] = { "on", @@ -107,7 +108,9 @@ static void disconnect_handler(DBusConnection *connection, void *user_data) bt_shell_set_prompt(PROMPT_OFF); g_list_free_full(ctrl_list, proxy_leak); + g_list_free_full(battery_proxies, proxy_leak); ctrl_list = NULL; + battery_proxies = NULL; default_ctrl = NULL; } @@ -271,7 +274,7 @@ static void print_iter(const char *label, const char *name, break; case DBUS_TYPE_BYTE: dbus_message_iter_get_basic(iter, &byte); - bt_shell_printf("%s%s: 0x%02x\n", label, name, byte); + bt_shell_printf("%s%s: 0x%02x (%d)\n", label, name, byte, byte); break; case DBUS_TYPE_VARIANT: dbus_message_iter_recurse(iter, &subiter); @@ -309,14 +312,20 @@ static void print_iter(const char *label, const char *name, } } -static void print_property(GDBusProxy *proxy, const char *name) +static void print_property_with_label(GDBusProxy *proxy, const char *name, + const char *label) { DBusMessageIter iter; if (g_dbus_proxy_get_property(proxy, name, &iter) == FALSE) return; - print_iter("\t", name, &iter); + print_iter("\t", label ? label : name, &iter); +} + +static void print_property(GDBusProxy *proxy, const char *name) +{ + print_property_with_label(proxy, name, NULL); } static void print_uuid(const char *uuid) @@ -445,6 +454,16 @@ done: g_free(desc); } +static void battery_added(GDBusProxy *proxy) +{ + battery_proxies = g_list_append(battery_proxies, proxy); +} + +static void battery_removed(GDBusProxy *proxy) +{ + battery_proxies = g_list_remove(battery_proxies, proxy); +} + static void device_added(GDBusProxy *proxy) { DBusMessageIter iter; @@ -539,6 +558,8 @@ static void proxy_added(GDBusProxy *proxy, void *user_data) gatt_add_manager(proxy); } else if (!strcmp(interface, "org.bluez.LEAdvertisingManager1")) { ad_manager_added(proxy); + } else if (!strcmp(interface, "org.bluez.Battery1")) { + battery_added(proxy); } } @@ -630,6 +651,8 @@ static void proxy_removed(GDBusProxy *proxy, void *user_data) gatt_remove_manager(proxy); } else if (!strcmp(interface, "org.bluez.LEAdvertisingManager1")) { ad_unregister(dbus_conn, NULL); + } else if (!strcmp(interface, "org.bluez.Battery1")) { + battery_removed(proxy); } } @@ -763,6 +786,20 @@ static struct adapter *find_ctrl_by_address(GList *source, const char *address) return NULL; } +static GDBusProxy *find_battery_by_path(GList *source, const char *path) +{ + GList *list; + + for (list = g_list_first(source); list; list = g_list_next(list)) { + GDBusProxy *proxy = list->data; + + if (strcmp(g_dbus_proxy_get_path(proxy), path) == 0) + return proxy; + } + + return NULL; +} + static GDBusProxy *find_proxy_by_address(GList *source, const char *address) { GList *list; @@ -1606,6 +1643,7 @@ static struct GDBusProxy *find_device(int argc, char *argv[]) static void cmd_info(int argc, char *argv[]) { GDBusProxy *proxy; + GDBusProxy *battery_proxy; DBusMessageIter iter; const char *address; @@ -1647,6 +1685,11 @@ static void cmd_info(int argc, char *argv[]) print_property(proxy, "AdvertisingFlags"); print_property(proxy, "AdvertisingData"); + battery_proxy = find_battery_by_path(battery_proxies, + g_dbus_proxy_get_path(proxy)); + print_property_with_label(battery_proxy, "Percentage", + "Battery Percentage"); + return bt_shell_noninteractive_quit(EXIT_SUCCESS); } -- 2.17.1