Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp883146ybt; Wed, 1 Jul 2020 12:23:08 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw3IEeyA7DuVvHYMiHXHW1NVjth3iw5rECP9Pic0PAvlhEMLQ+/dQgJwmUt9VanAogDPk3k X-Received: by 2002:a05:6402:1614:: with SMTP id f20mr29638587edv.129.1593631388737; Wed, 01 Jul 2020 12:23:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1593631388; cv=none; d=google.com; s=arc-20160816; b=Sm3+0CRQZ9MCshC4vO+HwDmAChc94yjxl75gLHXoYY3l7yOcY4YLd4Tpe3S5mNX6cO NoDUHkNquQEC3kdCAJEmz3cH8IlVkMqHPRSn/umzxfx1jAid5AdnpFphfVcs1DsCveBq oIiHAHZmQVhJGNHzwJ9Xzj08wNJl0Jfk56FbwI6iLtTZVxX1g4uJugcOkhU2qo7Sl2+e QcRX3CIfxlx5vexrl03oD57yROrZuLpbt7/7fkg1RaFzciFO4EvCWRv+OreLFXtjV8q6 eIMz+Ga9guVfkma5331sA3gHOdJQM6vhl977UURcA5Fy8yusVXDemclbVvNbXzw2/ZgQ 18CA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature; bh=6TlhrelhU8fdJUNlU1isvDCohx4f+8EV5ngRMY2hFfw=; b=TYj+jlF24HjkY68BiiT5yolK1OCldPPss70xxZhB5h1M/CKEe4xb0b4+aRyJcsd+sA 4Ee/H1B1Pb+7xh9qcWi/kz28NTKOPI1QGJe4gGHolhnK9tnU02VbtHue0gugF+nUIlfA WVFLUHd6hsxgrnlFEUJFlk3jKgv6RsXGuMdRAS28YVQ0wyz0CaJb3EdY1Z1MyWlOgwVh jh7o09yxSC9kckcNSVgDyWJAlT6RwYUtQD13nwEqwU0bTkmv3ZGlv9ezwp+WYWcxv6Xu Cm8s3eeQT5GnNuEYk++ZnoYfJHJv1CCU+Y9xihZiJWj/7t0Py1Q5U8icz7oUs0cRnXeX 7n9g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=NQuG+Pir; 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 a14si4691329edn.360.2020.07.01.12.22.32; Wed, 01 Jul 2020 12:23:08 -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=NQuG+Pir; 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 S1725875AbgGATWa (ORCPT + 99 others); Wed, 1 Jul 2020 15:22:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39884 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725771AbgGATW3 (ORCPT ); Wed, 1 Jul 2020 15:22:29 -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 C520CC08C5C1 for ; Wed, 1 Jul 2020 12:22:29 -0700 (PDT) Received: by mail-io1-xd44.google.com with SMTP id y2so26274539ioy.3 for ; Wed, 01 Jul 2020 12:22:29 -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; bh=6TlhrelhU8fdJUNlU1isvDCohx4f+8EV5ngRMY2hFfw=; b=NQuG+PirxnU/5vrjbV6BgXfINOlX4vcGT30WLG/GuqaFuFg12VkWnFPXqMxkLo0yTT Km77VknOYqThXBKAADpUuHXkWa91hw+kmvD1/1bxkf1sEOaAzpM4pDYsBP77LVuwvWdM kDYTLR/Qs/RCDlNYDD1lzlyd0uZEphasb1G8U= 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; bh=6TlhrelhU8fdJUNlU1isvDCohx4f+8EV5ngRMY2hFfw=; b=ofJftQTpUGibPjIOAnq6O7YSoMhDiGoRspvT5J5ZxIFISqNBGenQyYayA4K1gV6hiN cPs9eniDNwybt36rd2gXcKjn5iHlkFkwZr8ay2nes2febfNttZKi1FKZKEMY2j2YzVpQ 55FUl4t+yGTSgE680dTu8TbZ3t2Y3LTQumBme8G19fqOiF92zLFjD6dNFN0F2PnuRf3K 27Ml+yQLCYzJmrpeDBpPaUjUeQubGwlsc2KNHUNoUQmmvr50dygKGhZDzc1+K4mPOAuj 7KtkCCbkKvVtDcBY4HGaGNa751e3s0ZDylj9HYaWu6o9i1q7Sxj62VZo6BT9rLgoZ/gm Q5Nw== X-Gm-Message-State: AOAM531jS/jMrXLuL5L8Gmafl+OguB2sLV3YtR3FaH3rD95sAzjORiNA jr97tyaQXA2KPOP4EQAraUWmtyoUS0ooVg== X-Received: by 2002:a6b:90c:: with SMTP id t12mr3862455ioi.152.1593631348754; Wed, 01 Jul 2020 12:22:28 -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 d77sm3862513ill.67.2020.07.01.12.22.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Jul 2020 12:22:28 -0700 (PDT) From: Sonny Sasaka To: linux-bluetooth@vger.kernel.org Cc: Sonny Sasaka Subject: [PATCH v2] client: Add battery percentage to info command Date: Wed, 1 Jul 2020 12:22:02 -0700 Message-Id: <20200701192202.9508-1-sonnysasaka@chromium.org> X-Mailer: git-send-email 2.17.1 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: 100% --- client/main.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/client/main.c b/client/main.c index 422da5593..4b787240e 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; } @@ -445,6 +448,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 +552,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 +645,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 +780,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,8 +1637,10 @@ 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; + unsigned char percentage; proxy = find_device(argc, argv); if (!proxy) @@ -1647,6 +1680,16 @@ 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)); + if (battery_proxy && g_dbus_proxy_get_property( + battery_proxy, "Percentage", &iter)) { + dbus_message_iter_get_basic(&iter, &percentage); + bt_shell_printf("\tBattery Percentage: %d%%\n", percentage); + } else { + bt_shell_printf("\tNo battery information\n"); + } + return bt_shell_noninteractive_quit(EXIT_SUCCESS); } -- 2.17.1