Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp1680924ybt; Thu, 2 Jul 2020 11:06:26 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzdiAUHYFAe1xgPS67JW2feqEwfPtziiOsHMdSLPVBgZ9BH8YRHx1RfvGmjwJlC9kitwqVA X-Received: by 2002:aa7:da90:: with SMTP id q16mr28671627eds.44.1593713186496; Thu, 02 Jul 2020 11:06:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1593713186; cv=none; d=google.com; s=arc-20160816; b=ebrqkWyXhAF4IQ9WPH4d5gIOvM6NaRwHJYMBX84eoKQjeK+6kuYnuxVAshmHpMk20O THXAS5n1Ly1tSxCWJ6X0Afp0fnjP6aolW8MCUi4XP+Kd0Bbm+3y+MS7NtjyANYJRPrZh 7jTGXdCU8lsTOanjzeqrCThM1860Ofc5XXKir7anNvk8QAgC8+dgyzwAVaFD7fSmwuEG +Fj6dUAWqrlwlUqp3HJdXnNSjFYjvJbg7GU/FSXTwY8udHFjf5nmIVfI9hdKJzRpYHLE 6tBqSZxDNig59bN14pjMi6zCRp6lMWtxkahkcPio5s/Hg5naKU/VrVt9U75jTiLz/ofq fXKA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=Luqimbir/57tQriFXcTbh2hWaUyPr2MwVfjaS7PtjWY=; b=ugvJzNYpFxc/Ln1tqpP/LLCc4HkN2IDZR4HWkho7hJDUPylUs24BpsxQwnqeyB2tgX ZJUtdUwwTVVxcf6Y/M6fElPiQ4ulMQPtOiYRGnLySEHN3xiAFIaEOn2vzu43cB+cN0a6 JYbI1e768E0xOhjr1Vgfet46QRs+9tT09ZmRsGFtPWsh79y7yZ9pyUp6dig/3ULTDK/8 IsQIeJnNEnIuE0fSIXS8nt9hwfpXT5AEmISz9i1k3xQRQ+LjNlJr9cSU0gvHN+17Xud9 Vq7GOAveg+klo34DKLuoyYHQSWpwMkJDAzD/ofwiVgu/ExcvWRVTPGfFcIUMMvD4VjOZ nRtQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=UKJI1gBf; 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 g14si1297473edv.102.2020.07.02.11.06.01; Thu, 02 Jul 2020 11:06:26 -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=UKJI1gBf; 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 S1727909AbgGBSF5 (ORCPT + 99 others); Thu, 2 Jul 2020 14:05:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52880 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726349AbgGBSF5 (ORCPT ); Thu, 2 Jul 2020 14:05:57 -0400 Received: from mail-oi1-x244.google.com (mail-oi1-x244.google.com [IPv6:2607:f8b0:4864:20::244]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E3CFBC08C5C1 for ; Thu, 2 Jul 2020 11:05:56 -0700 (PDT) Received: by mail-oi1-x244.google.com with SMTP id r8so24382073oij.5 for ; Thu, 02 Jul 2020 11:05:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=Luqimbir/57tQriFXcTbh2hWaUyPr2MwVfjaS7PtjWY=; b=UKJI1gBfEXwejkMPGNb1EiHRVaOAnGVyBacr6FwZSP4QBFjetEm9BR9RlTGSDNAESM MX2PvENNMbE6F2d7chHuxHyTabMznjrRxME10gnWhN1UM1ZQXw/ZtvuiLTWzo2G4l3nN hqAl7WqArT9YJ9z8Hn+6WBH/7Nnr294L3EmfY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=Luqimbir/57tQriFXcTbh2hWaUyPr2MwVfjaS7PtjWY=; b=WpBFHw0xuz5wFAuylZhP/cibFBLowThgyDpSsWw7FmSymgC136lSnd035MPiffQF8Y QtDFIQ33UWTIGM8DEaf6kmsjXCccOi/On/D1e4zZSbxBEUkfNbZN6Ob5F4mDUSD4SBH4 ObNNqNHq1DsByZSCatVCrpkI5ssLY7SaYJKSipJ2AlRQZbGG1OAPwzROwtpESk3oYDsY Vg6q7ZKKH/u/enSbmXJtl6GTXRgc/aMe71VoKEnmHeE2HlJvQAe3BoKoJN9GIrOwhR28 6bxOwbUXz+JRofbEChDT+cwgl6YGWhfZygqbNrjDL0kYPmxD+8v9nWyQ6G0yCib/4Z1I aHuQ== X-Gm-Message-State: AOAM532iyTlTNLg1T+t0FztlTI6WPKSZ69Yca+3jiqYxTAxMfctWSSIS 1XLoqH0vTh+/J8nhC+N100us+R/rCFi4XROyLiwgu1B2zz0= X-Received: by 2002:aca:c157:: with SMTP id r84mr16965301oif.136.1593713154852; Thu, 02 Jul 2020 11:05:54 -0700 (PDT) MIME-Version: 1.0 References: <20200701192202.9508-1-sonnysasaka@chromium.org> In-Reply-To: From: Sonny Sasaka Date: Thu, 2 Jul 2020 11:05:43 -0700 Message-ID: Subject: Re: [PATCH v2] client: Add battery percentage to info command To: Luiz Augusto von Dentz Cc: "linux-bluetooth@vger.kernel.org" Content-Type: text/plain; charset="UTF-8" Sender: linux-bluetooth-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org Hi Luiz, I just sent another patch following your feedback. Please take another look. Thanks! On Thu, Jul 2, 2020 at 8:43 AM Luiz Augusto von Dentz wrote: > > Hi Sonny, > > On Wed, Jul 1, 2020 at 11:06 PM Sonny Sasaka wrote: > > > > Hi Luiz, > > > > On Wed, Jul 1, 2020 at 10:20 PM Luiz Augusto von Dentz > > wrote: > > > > > > Hi Sonny, > > > > > > On Wed, Jul 1, 2020 at 12:24 PM Sonny Sasaka wrote: > > > > > > > > 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)); > > > > > > I'd replace the lines below with just print_property(battery_proxy, > > > "Percentage"); and just make print_property check for NULL proxy if it > > > doesn't already. > > I tried this but the result doesn't look quite good: > > Device XX:XX:XX:XX:XX:XX (random) > > Name: ... > > ... > > Modalias: ... > > Percentage: 0x64 > > > > The name "Percentage" is not clear (what kind of percentage does it > > mean) when shown in the "info" command. And also the format for byte > > data type is hex and there is no percent sign. I tried to modify the > > print_property function to support custom format but the code gets > > overly complex and I think it's more straightforward to use custom > > logic like in this patch. > > Just make it always print both the decimal and (hex). > > > > > > > > + 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 > > > > > > > > > > > > > -- > > > Luiz Augusto von Dentz > > > > -- > Luiz Augusto von Dentz