Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp3837396ybt; Tue, 30 Jun 2020 12:08:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJykt1+yB76P6mYl6ToCXX8OrAOgizcokh3h/Bcf9l+JtQc4zkVYHepj1D/NMDrYrKIznN/C X-Received: by 2002:a17:906:3a04:: with SMTP id z4mr13848212eje.441.1593543653260; Tue, 30 Jun 2020 12:00:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1593543653; cv=none; d=google.com; s=arc-20160816; b=B9ujGhaEEZ1j0EYh8SYa0XfxurdizJhhasMuLp13jRFCPP49W2BmuSRD0WSUJ8Je3w XSh460y32lb6fCMh4fZPWQdeieAls+YtUs2qWM/UoLA8EMtO83nlzBJQTAruLxFBnl9u ehCI7r7wCR1BYzt2AUuyhi6rjVJliwLQDwG+iTvmiC5DyGR99hrBZz/briE07apnA2/1 ynSot+3LPuz/7ShRY7U3yjfDXR8OZ0MMJfaDWl8MNmP3QmT0i272J4ZNF/8Owgg87+w5 wP4Wvl558WMJgOuAzI/jpUBrInqnFQcDUuXwtb7uxH++FE4HTwy/a8O1bNsIf4l0liRa HPrA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=tFSRSERRZijSFK7hRNe5Kg4vhWLebcNBDc/HGzP5nvo=; b=AYQdlvOye+1CiKB9zdSZiUdTJ7gzVIhOgX+KTcP70zW82FYL14oyuOCpXCnsM8xZIX tvgm46E2l7i1vgwOROtXVnPS5DaKodLka6bDV/EQsASr8fnJB/hgELetDIn79l5rwYYk odszTyCyQiRmz9SAo2LWb91CqbV/wrU0j8nv+TTWhsW7hteQ/QzZhO7KZlwzKnBl59ws QqfMVJwaUUNHi5J6Clsb7YK0AaIGSm7tSJ00uLr88Cun3a8889RBlLF4ElttncN1jGXe oXqBSZUMMJFlcXzUDr/GIPZx/uxnor6aJ5dV/thkQlJWGdUnKBUMBqN9N56t/3vOgWYS aoPA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=HnUjlPya; 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 t7si2321090ejx.225.2020.06.30.12.00.26; Tue, 30 Jun 2020 12:00:53 -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=HnUjlPya; 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 S1730338AbgF3RrU (ORCPT + 99 others); Tue, 30 Jun 2020 13:47:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55924 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726813AbgF3RrU (ORCPT ); Tue, 30 Jun 2020 13:47:20 -0400 Received: from mail-pf1-x443.google.com (mail-pf1-x443.google.com [IPv6:2607:f8b0:4864:20::443]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 20C62C061755 for ; Tue, 30 Jun 2020 10:47:20 -0700 (PDT) Received: by mail-pf1-x443.google.com with SMTP id j1so9735605pfe.4 for ; Tue, 30 Jun 2020 10:47:20 -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:mime-version :content-transfer-encoding; bh=tFSRSERRZijSFK7hRNe5Kg4vhWLebcNBDc/HGzP5nvo=; b=HnUjlPya3iLGWP7NtsZaQx/gpDW4DyxWcsrfcHYKarn+GsaGar5Mnp4ddrVqu3XT/B CGWX0UiahrC29hK/Ff4FnZnJEFlrZxE2jGdAWwQjfCpdlbj8tjAs/6xeiN5obpZ3jmXL gLLYIo9bQq2W+2QtU6PBsL7Hk2DejIgROjjqc= 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:mime-version :content-transfer-encoding; bh=tFSRSERRZijSFK7hRNe5Kg4vhWLebcNBDc/HGzP5nvo=; b=faITIJ31OxMTxdDCuAcSyqvzaULW2vjB+v672ICnfj8LkpZgl2O3IvETmlklk3Y7UG kpCqrqGY1zW1ZcS6cSJHyKcUm1bcyBArDm6ZbdCwcSRe0ERvuje78z4yOalrltsOBovA 20d9nMJe6PfjGUrvMVo1Grg8PJg8xHvaumg5mts7dGIlTVWyctE8dneMVODhBo8dPptD OpqRj1x1dltaEWY4lwFhcSJ1ED9F3tZsZ46mNkth5GiI9tqOlFJqux27GUx93AoLgAaK 2dyplyzS2jMjQIGrxCnBT1Mcz3All1m4/C0dNjO7uCw92TfzJuzxkL8WdUllnEvHaO4a DI1Q== X-Gm-Message-State: AOAM5302YW1p9gGKNtuWU9Fn35qmWCUt9GYeHG/1JFc5qwuarSJGjeHd prJR5TCE/mTpZI7423UGED2ZdRycayc= X-Received: by 2002:a63:457:: with SMTP id 84mr16203374pge.219.1593539239383; Tue, 30 Jun 2020 10:47:19 -0700 (PDT) Received: from sonnysasaka-chrome.mtv.corp.google.com ([2620:15c:202:201:b7e2:9910:bd78:608d]) by smtp.gmail.com with ESMTPSA id u8sm2850417pjn.24.2020.06.30.10.47.18 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 30 Jun 2020 10:47:18 -0700 (PDT) From: Sonny Sasaka To: linux-bluetooth@vger.kernel.org Cc: Sonny Sasaka Subject: [PATCH 1/1] client: Add battery command Date: Tue, 30 Jun 2020 10:47:07 -0700 Message-Id: <20200630174707.214317-1-sonnysasaka@chromium.org> X-Mailer: git-send-email 2.26.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-bluetooth-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org This adds the "battery" command to show battery information of a peer device based on org.bluez.Battery1 API. Example usage: [bluetooth]# battery XX:XX:XX:XX:XX:XX Percentage: 100% --- client/main.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/client/main.c b/client/main.c index 422da5593..cc5eb7ce8 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,19 @@ 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; @@ -1650,6 +1680,35 @@ static void cmd_info(int argc, char *argv[]) return bt_shell_noninteractive_quit(EXIT_SUCCESS); } +static void cmd_battery(int argc, char *argv[]) +{ + DBusMessageIter iter; + GDBusProxy *device_proxy; + GDBusProxy *battery_proxy; + unsigned char percentage; + + device_proxy = find_device(argc, argv); + if (!device_proxy) + return bt_shell_noninteractive_quit(EXIT_FAILURE); + + battery_proxy = find_battery_by_path(battery_proxies, + g_dbus_proxy_get_path(device_proxy)); + if (!battery_proxy) { + bt_shell_printf("Device %s does not have battery information\n", + argv[1]); + return bt_shell_noninteractive_quit(EXIT_FAILURE); + } + + if (g_dbus_proxy_get_property(battery_proxy, "Percentage", &iter) == + FALSE) + return bt_shell_noninteractive_quit(EXIT_FAILURE); + + dbus_message_iter_get_basic(&iter, &percentage); + bt_shell_printf("Percentage: %d%%\n", percentage); + + return bt_shell_noninteractive_quit(EXIT_SUCCESS); +} + static void pair_reply(DBusMessage *message, void *user_data) { DBusError error; @@ -2785,6 +2844,8 @@ static const struct bt_shell_menu main_menu = { dev_generator }, { "disconnect", "[dev]", cmd_disconn, "Disconnect device", dev_generator }, + { "battery", "[dev]", cmd_battery, "Show device battery", + dev_generator }, { } }, }; -- 2.26.2