Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp3610486pxu; Tue, 8 Dec 2020 17:15:02 -0800 (PST) X-Google-Smtp-Source: ABdhPJzJSHf6SR4nbwravUvsqNW7P87feWa3S7Kq04oQytxnXbnoAgySQdGUhF1ww1kABQQ94r36 X-Received: by 2002:a17:906:16d6:: with SMTP id t22mr85237ejd.154.1607476502600; Tue, 08 Dec 2020 17:15:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1607476502; cv=none; d=google.com; s=arc-20160816; b=Yv9Y2IcFhdRJpR2a/tjeKDBRbwTqNhUi7yuh4CJqPnH/A4otduLZFozC+8VpfX8nqh P8pgV43KiEkciz5zBCRIQiRrUlRaqrOKjSR/P3unaMaoVtgsoBZL1r5KtNyCpMpzZi5K m0w4vtDA/wAPNi86IRWDkifIzhtVrnnROg4WhlpH+NXO90Kr1nWp2VUkCyl1yMQ3fxNS z/8VRdz4M3tNEcXoJ7FswH4wyhLrGJuY0l5wrPut/BA1IxnzMZwuq4Uj4WGc1PlZ5qrL SSf6yORYk3gQuKc8mzw+X2wqTDW/WMs93nJPhrg9noUmM5yWx27eSQyw7e7xloWWSvF+ eOgA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=57I8J+9Sz1z5XdfhWdkGcileeg/1hRiY8h7G9YrC+8w=; b=jtke45pFBKl+R1VuZcMuTMoDYA+m6ZHF/rK6pRfXFC+39sZIb9HVNyH7QvCba7Vp1q OWcHRyR4ipDvapbaGJFnc7vbgniD56j76wiFQ/K/oxVxnanvj35tdknJcDhedaNNAIzw We1AEgyYuPMk8q2irz6BcsinVSQbPJcCFFlqQMxSb1CYjIJqqz+rDmkNkEae5JT4xFHC aCrB+UfSjPD8p47vygIH0DaRMvV0hSRjEEKF+FvBHTTxedSqMeGk7w6Ag68wl1pAG9xM 8ZOKGCkIPMtDedAyjzvtFu8LUMA5l5hfO2Rt5Ww63B0x91rDzFTd4egBBSKyP1F7eTVj d+Ig== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=eacvLjFh; 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 10si80645ejn.746.2020.12.08.17.14.27; Tue, 08 Dec 2020 17:15:02 -0800 (PST) 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=eacvLjFh; 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 S1726316AbgLIBB1 (ORCPT + 99 others); Tue, 8 Dec 2020 20:01:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53466 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726311AbgLIBB0 (ORCPT ); Tue, 8 Dec 2020 20:01:26 -0500 Received: from mail-pl1-x633.google.com (mail-pl1-x633.google.com [IPv6:2607:f8b0:4864:20::633]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A9DA9C0613D6 for ; Tue, 8 Dec 2020 17:00:46 -0800 (PST) Received: by mail-pl1-x633.google.com with SMTP id 4so49544plk.5 for ; Tue, 08 Dec 2020 17:00:46 -0800 (PST) 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 :mime-version:content-transfer-encoding; bh=57I8J+9Sz1z5XdfhWdkGcileeg/1hRiY8h7G9YrC+8w=; b=eacvLjFhNnhnAcM4COcFMhmfUvwmsFAFcrtKu+F7dk2mYtruQ7jFT9S21kIsbBfMb+ EwIgGUe4l6h96y/Ugux+4+JOnZkRuk9vu2nkRpMpFVeGPySbxV653zH393ug8kPF8MUZ BL7NgJZ8Q3XlHO2g60H15YznGQvF37qDynlPI= 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:mime-version:content-transfer-encoding; bh=57I8J+9Sz1z5XdfhWdkGcileeg/1hRiY8h7G9YrC+8w=; b=jiAFRFsVcDLuvj5e2NWjExdfQ0CMwYff7FTDAnj2U4PA7nBe3IdgASipSdP7cawOmB lfsdtMYLFxdkdCNARzJAUzsOqrE1cOzFgN/QfXcn+VciqgZaxLokqwd77FKBFmJ6YXKZ OBadlgZ5q7JpKRMPva7sOvdBdhboOHa9GeO6u8toSZ2zrB8Z6Yp8QgUcloQ0EqiLWutS LbVFc0KkABV4sgPExOc1tD0V0AecitQ38jDy3sGpFtv4g4AO95OMO1hnBCsny4Sol/Q/ 0XV+hTt8YKJR1bz91B9mvtgMUxJMKC7vAZzvIrCvIotllO2AEB82je0wXRnej7zzbpOS wqUg== X-Gm-Message-State: AOAM532hcFvCeYRBqlyEMCfn3rfHmK2/f+LZQVsv005NLgRntHvGUY0M Iy/cnh9apYJrojFxmTpbidiXa6rDnEgu6Q== X-Received: by 2002:a17:902:52a:b029:da:989f:6c01 with SMTP id 39-20020a170902052ab02900da989f6c01mr493409plf.45.1607475645875; Tue, 08 Dec 2020 17:00:45 -0800 (PST) Received: from sonnysasaka-chrome.mtv.corp.google.com ([2620:15c:202:201:4a0f:cfff:fe66:e60c]) by smtp.gmail.com with ESMTPSA id 5sm28052pfw.12.2020.12.08.17.00.45 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 08 Dec 2020 17:00:45 -0800 (PST) From: Sonny Sasaka To: linux-bluetooth@vger.kernel.org Cc: Sonny Sasaka , Alain Michaud Subject: [PATCH BlueZ 2/2] input/hog: Cache the HID report map Date: Tue, 8 Dec 2020 17:00:30 -0800 Message-Id: <20201209010030.342632-2-sonnysasaka@chromium.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20201209010030.342632-1-sonnysasaka@chromium.org> References: <20201209010030.342632-1-sonnysasaka@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org To optimize BLE HID devices reconnection response, we can cache the report map so that the subsequent reconnections do not need round trip time to read the report map. Reviewed-by: Alain Michaud --- profiles/input/hog-lib.c | 142 ++++++++++++++++++++++++++------------- 1 file changed, 96 insertions(+), 46 deletions(-) diff --git a/profiles/input/hog-lib.c b/profiles/input/hog-lib.c index ee811d301..1e198ea64 100644 --- a/profiles/input/hog-lib.c +++ b/profiles/input/hog-lib.c @@ -95,6 +95,8 @@ struct bt_hog { struct queue *bas; GSList *instances; struct queue *gatt_op; + uint8_t report_map[HOG_REPORT_MAP_MAX_SIZE]; + ssize_t report_map_len; }; struct report { @@ -276,6 +278,8 @@ static void find_included(struct bt_hog *hog, GAttrib *attrib, free(req); } +static void uhid_create(struct bt_hog *hog); + static void report_value_cb(const guint8 *pdu, guint16 len, gpointer user_data) { struct report *report = user_data; @@ -924,57 +928,17 @@ static char *item2string(char *str, uint8_t *buf, uint8_t len) return str; } -static void report_map_read_cb(guint8 status, const guint8 *pdu, guint16 plen, - gpointer user_data) +static void uhid_create(struct bt_hog *hog) { - struct gatt_request *req = user_data; - struct bt_hog *hog = req->user_data; - uint8_t value[HOG_REPORT_MAP_MAX_SIZE]; struct uhid_event ev; - ssize_t vlen; - char itemstr[20]; /* 5x3 (data) + 4 (continuation) + 1 (null) */ - int i, err; GError *gerr = NULL; + int i, err; - destroy_gatt_req(req); - - DBG("HoG inspecting report map"); - - if (status != 0) { - error("Report Map read failed: %s", att_ecode2str(status)); - return; - } - - vlen = dec_read_resp(pdu, plen, value, sizeof(value)); - if (vlen < 0) { - error("ATT protocol error"); + if (!hog->report_map_len) { + warn("Failed to initiate UHID_CREATE without report map"); return; } - DBG("Report MAP:"); - for (i = 0; i < vlen;) { - ssize_t ilen = 0; - bool long_item = false; - - if (get_descriptor_item_info(&value[i], vlen - i, &ilen, - &long_item)) { - /* Report ID is short item with prefix 100001xx */ - if (!long_item && (value[i] & 0xfc) == 0x84) - hog->has_report_id = TRUE; - - DBG("\t%s", item2string(itemstr, &value[i], ilen)); - - i += ilen; - } else { - error("Report Map parsing failed at %d", i); - - /* Just print remaining items at once and break */ - DBG("\t%s", item2string(itemstr, &value[i], vlen - i)); - break; - } - } - - /* create uHID device */ memset(&ev, 0, sizeof(ev)); ev.type = UHID_CREATE; @@ -1004,8 +968,8 @@ static void report_map_read_cb(guint8 status, const guint8 *pdu, guint16 plen, ev.u.create.version = hog->version; ev.u.create.country = hog->bcountrycode; ev.u.create.bus = BUS_BLUETOOTH; - ev.u.create.rd_data = value; - ev.u.create.rd_size = vlen; + ev.u.create.rd_data = hog->report_map; + ev.u.create.rd_size = hog->report_map_len; err = bt_uhid_send(hog->uhid, &ev); if (err < 0) { @@ -1018,6 +982,62 @@ static void report_map_read_cb(guint8 status, const guint8 *pdu, guint16 plen, bt_uhid_register(hog->uhid, UHID_SET_REPORT, set_report, hog); hog->uhid_created = true; +} + +static void report_map_read_cb(guint8 status, const guint8 *pdu, guint16 plen, + gpointer user_data) +{ + struct gatt_request *req = user_data; + struct bt_hog *hog = req->user_data; + ssize_t vlen; + char itemstr[20]; /* 5x3 (data) + 4 (continuation) + 1 (null) */ + int i; + + destroy_gatt_req(req); + + DBG("HoG inspecting report map"); + + if (status != 0) { + error("Report Map read failed: %s", att_ecode2str(status)); + return; + } + + vlen = dec_read_resp(pdu, plen, hog->report_map, + sizeof(hog->report_map)); + if (vlen < 0) { + error("ATT protocol error"); + return; + } + + hog->report_map_len = vlen; + + DBG("Report MAP:"); + for (i = 0; i < vlen;) { + ssize_t ilen = 0; + bool long_item = false; + + if (get_descriptor_item_info(&hog->report_map[i], vlen - i, + &ilen, &long_item)) { + /* Report ID is short item with prefix 100001xx */ + if (!long_item && (hog->report_map[i] & 0xfc) == 0x84) + hog->has_report_id = TRUE; + + DBG("\t%s", item2string(itemstr, &hog->report_map[i], + ilen)); + + i += ilen; + } else { + error("Report Map parsing failed at %d", i); + + /* Just print remaining items at once and break */ + DBG("\t%s", item2string(itemstr, &hog->report_map[i], + vlen - i)); + break; + } + } + + /* create uHID device */ + uhid_create(hog); DBG("HoG created uHID device"); } @@ -1602,6 +1622,12 @@ bool bt_hog_attach(struct bt_hog *hog, void *gatt) bt_hog_attach(instance, gatt); } + /* Try to initiate UHID_CREATE if we already have the report map to + * avoid re-reading the report map from the peer device. + */ + if (hog->report_map_len > 0) + uhid_create(hog); + if (!hog->uhid_created) { DBG("HoG discovering characteristics"); if (hog->attr) @@ -1627,6 +1653,29 @@ bool bt_hog_attach(struct bt_hog *hog, void *gatt) return true; } +static void uhid_destroy(struct bt_hog *hog) +{ + int err; + struct uhid_event ev; + + if (!hog->uhid_created) + return; + + bt_uhid_unregister_all(hog->uhid); + + memset(&ev, 0, sizeof(ev)); + ev.type = UHID_DESTROY; + + err = bt_uhid_send(hog->uhid, &ev); + + if (err < 0) { + error("bt_uhid_send: %s", strerror(-err)); + return; + } + + hog->uhid_created = false; +} + void bt_hog_detach(struct bt_hog *hog) { GSList *l; @@ -1660,6 +1709,7 @@ void bt_hog_detach(struct bt_hog *hog) queue_foreach(hog->gatt_op, (void *) cancel_gatt_req, NULL); g_attrib_unref(hog->attrib); hog->attrib = NULL; + uhid_destroy(hog); } int bt_hog_set_control_point(struct bt_hog *hog, bool suspend) -- 2.26.2