Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp1579115ybt; Thu, 2 Jul 2020 08:44:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyG6oH2aK+JceMsVIi6O8eNHIINhNKZM8Mjud2UwzudnOIcPVEKcFUuHbnEDCPgEAs/c/0V X-Received: by 2002:a50:ba8b:: with SMTP id x11mr36550091ede.201.1593704652571; Thu, 02 Jul 2020 08:44:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1593704652; cv=none; d=google.com; s=arc-20160816; b=km6Eesng0Z4a6vlGNW2T8C0xvj/vCn9b/oZh+MHPBr+yfqDssc6mqAjpwAsDKESt68 Sm1kNz00WwGVaKSBROo0nRgT09GVmWOOcqSMs17I8IrbghKBgJnbooP4/daYtqXgtQZJ c0Yn1oZ3xChnX9Teb7cJ31/GyVsUn/VofnTjdHmDwZPiTD/oRJxAOYcHdMDIZCetND2D c1IGbdBHRcdwOcr2SUz1ggSDF2CCJQsWYbjPmnqBBRzB3cblcOcQM1bfzpbXKFxj0Wue sF7pSRhdJUzjKeYEGd3SnqpJLHqHKolbAgtVH0UchptU9iHIZq8Z+C6tr5GcmJNGQA9F zY9g== 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=wacwb6gdlVm4ErQQEHazoKnIUKGvN0uOle2VWL6Ms4E=; b=EQifYHnGWsa1syIlaIZNYvgB1qpDhv81KnJL4X1qRctAiLeJCh5bSFJFWItgoZZL6E FVRevznBX2HxFaDyZuJO5yVJuwzYnIf/YXeUVZuYCgSE9iJXzPSOxH0mMTB0BX8Z3F6e fWwxXERwhff3lQOuGJLFTil4MuhqJB2x4sq8Jw3CNO0sCObCWwfCgqDRRsHHdkX9QxZM /VB++lDhFSzEyLK/a8JkzvZqaUeFUk6dF6F4HUSZj5ST8bz/XUAS1UtHm+ZceOjsuzs1 RZj/7msGxT2QoI/nwJDIBISSk9MQDhC14o8C72qBQHe/8q3I3gab+bvMPHlEsIwFV3Z4 tsQg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=mn9sqSRj; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d16si5779201ejp.131.2020.07.02.08.43.36; Thu, 02 Jul 2020 08:44:12 -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=@gmail.com header.s=20161025 header.b=mn9sqSRj; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728374AbgGBPnE (ORCPT + 99 others); Thu, 2 Jul 2020 11:43:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59036 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726353AbgGBPnD (ORCPT ); Thu, 2 Jul 2020 11:43:03 -0400 Received: from mail-ot1-x343.google.com (mail-ot1-x343.google.com [IPv6:2607:f8b0:4864:20::343]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6C552C08C5C1 for ; Thu, 2 Jul 2020 08:43:03 -0700 (PDT) Received: by mail-ot1-x343.google.com with SMTP id g37so715400otb.9 for ; Thu, 02 Jul 2020 08:43:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=wacwb6gdlVm4ErQQEHazoKnIUKGvN0uOle2VWL6Ms4E=; b=mn9sqSRjZ4t8Ol5Dr/3tVs9e8IIWHTvavbBa6p1ZnP7kF6kjbSkOC4Ccnu1u47y81U Ppr344czHUSkpMNKfFtX9e8MZsQa2I1wgD3BwLVVC4H3Mz2T9gePqc3bzkDi/p05s9Cv Du2N9JdnARhUAkN+QAMOB8LgwcBNR076rNMbEVA2Sgpn+kQbzIEg2umhOC4pxQXl0rA2 rhc2SKUlFvT3EOV++GyvCmUgaHDZd87RO11siv/vFTiS04HL7KUOqG8k/NAZWWCg0GJK Pt6Y+JyAs2dkxKb9aLgbcOLGAJz8ao3BV2+jyrxRtHzmTg7zk+iYYwFUM7dA78C0eCm3 Kpqg== 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=wacwb6gdlVm4ErQQEHazoKnIUKGvN0uOle2VWL6Ms4E=; b=j4WeUUQjdLHT1o4oZkM/TgrUmG3CsDHQc1tpm5cN1PeXOgDUanPW1GOurHvTD0XbFf N4CwgQZUBWwNxIs9f1eS0YWHyIrQC1XDEVQRAiURfddiHxgI2UhCfAN+XA0g73Kixrgu uU8V7kBnzTSQ+pyljN3jefgR02AvUP86KCDz/NZOcbbvqc5+0GuSz9+bMZZNuzYGFiBx US4XEuOPJE5+ylUOWV7ex2Zp7QPPdMAHYj1ykN/xy6a2Vld+UaJ4MSYfnteQWGcU0N/b wiOX+JxbGKncyd3lOI41vK3Q7RKG3OA+t3m88KUhALptTFvmxW7vFzLcEV7K1tUOLLtT wYrA== X-Gm-Message-State: AOAM531kuJIdkjgB35ogh307NYCls/ubw+lyS32oxBNdfLizwswlGzif QhSPAr+o43FvAOv+hcBhuxCQ7gi4sjXZTDdPuxM= X-Received: by 2002:a9d:3da5:: with SMTP id l34mr26580484otc.88.1593704582694; Thu, 02 Jul 2020 08:43:02 -0700 (PDT) MIME-Version: 1.0 References: <20200701192202.9508-1-sonnysasaka@chromium.org> In-Reply-To: From: Luiz Augusto von Dentz Date: Thu, 2 Jul 2020 08:42:50 -0700 Message-ID: Subject: Re: [PATCH v2] client: Add battery percentage to info command To: Sonny Sasaka 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 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