Received: by 2002:a05:6a10:9e8c:0:0:0:0 with SMTP id y12csp839303pxx; Thu, 29 Oct 2020 16:07:23 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwfylnqlHRIrI+i/vWnDumHDWOVKXLQxoE6ipgCxI18QLClZZy2QdNI3ytFroqGx4XAn4Kq X-Received: by 2002:a17:906:245b:: with SMTP id a27mr6376162ejb.345.1604012842962; Thu, 29 Oct 2020 16:07:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1604012842; cv=none; d=google.com; s=arc-20160816; b=TwkEsuhcA7hoNpWXkQowDrRpYjxD8q/eF1NS/0g/Oy9EB9NAqqi/Avi1a31k01lY3N I/aYV/UVdn5a4gQHki4Aum+0wfcUkniPZTcJTxBGLxDzs47Y+0c9PvQVT8zhO1qD4o4R cqO0QqQx5n2nHbH+YzNUfqS/OGy+F7M0T5rXiks3pbmoJRrLROiHruTEJ7ZjtzVx1vlH e4VlOeMqnvsu/IZ9JNOqHNB3I1yx2Mu0D5B3lU9s+M1VCCP7DWGHefwkrUXqjRGMYssC 72PHUWuGfzwzBYBE2TPrcyB0q6TAkIhG/yqTKRr0jZwa4li0YUvkkB+o3Z0yLr0f5RdZ 6K0w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:sender:dkim-signature; bh=EOx3zY4sv78PXdbq3qCGZpKmBh3t/np3IO11o3dWgy0=; b=xjpdnrXihchb3rwSWcXut0K1/YTCkPoGyAQP7LBB/sTpphvwR4sBn6iga0mcV4n6gs WDsxn4eK+Nm4Cjt89N5joM302OBVsLTbQLU9AqReUB3dQu1LtyeRGUXH+w8WFAkyM/Cq +jHVV/O8uJx/N0p9pYAiDTqKt4BUw14QmgwNAGybMGIrWk+AYB26pTZiQ+E4QiSkdjv0 pkt4P3qSRNgmI3nxxn3R7+BO8ybcM3DmLror3zXOc2eI2aTsh6PY0X9f4BR9/IIIpv4e Qh6mrQnSUKMwvEW999Y43smFFC+b7Icsmd6pDAm+qH/q1Z3OLhLp72+TSg+CxB2d7p70 ko2g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=Uhqd6dBZ; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id r17si3106970edo.383.2020.10.29.16.06.59; Thu, 29 Oct 2020 16:07:22 -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=@google.com header.s=20161025 header.b=Uhqd6dBZ; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725903AbgJ2XGk (ORCPT + 99 others); Thu, 29 Oct 2020 19:06:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33100 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725379AbgJ2XGk (ORCPT ); Thu, 29 Oct 2020 19:06:40 -0400 Received: from mail-pf1-x449.google.com (mail-pf1-x449.google.com [IPv6:2607:f8b0:4864:20::449]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2A14CC0613CF for ; Thu, 29 Oct 2020 16:06:40 -0700 (PDT) Received: by mail-pf1-x449.google.com with SMTP id q16so3308326pfj.7 for ; Thu, 29 Oct 2020 16:06:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=EOx3zY4sv78PXdbq3qCGZpKmBh3t/np3IO11o3dWgy0=; b=Uhqd6dBZq6Hf4V+X9j4eCYKiH+Z1xCOVIFLmZBOZAR7tDDmY1+Cl8uLM0Ka5F2Qw/3 1TahHs4im8yb50JGtKmsXrvIheUEnCSoI61yvR6WUNKkvsF4jB5WWRrmV3woUw1OhwVu FkChaWNUtB0qHyxfGaUJ05ScjUA6SRPp/qpAu7YZKm+JiypYlt3QbNt1Kh7E9i1oyVoY VQ79BlOif8oVJq+VsxzMC0y5CAGPJPmXKOV5mtk3kQjVwpzK1FA9avSlxKF6b6MxDbT9 OhVkcoCmm4jKIxzYKscWuo7+rQgRd8q0gjBqWXzwtwFgNvgz+eL57jgSRmPMMMOfGzxS komQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=EOx3zY4sv78PXdbq3qCGZpKmBh3t/np3IO11o3dWgy0=; b=Bd5fJNcxa4bdrFyw7bgfhU3EUEoqxXhfHs0DPN/paANfjL91Eb6240LrjRmMvbr/U6 f8EO4zEAUrbYU54FiqiWXQWEOCNTZ5rjQ1V9m8gBhnlYZWSSEa1Ys1Q7YEXaJLrBzTcg tFPV+EwuySdfxISykOO/SXsiPjszQdFa2tPtgHI/nLHwoldNCoZTKwnJwHCFKziq0v+M cqQ95MBbnZILSWhR4uJD2sE/MYoGnpKZ4rID4oP4wxs43VKcKT8J9b2RZWjFzAt52FDZ wJRI48S5VNvRAUd/WLXs8dXEPgnve4E5sUcpy2JH31Iwy3FsZRUAhow62O/HUpjPWh12 7y0g== X-Gm-Message-State: AOAM53338foqq6g5YVct1WxxEgwbYzHO67pij/29xYKxsCiHv4wE9PCj IUpGficiU2TLw8QUj6N8GgeHo2X5Sps076I3EBLJ Sender: "danielwinkler via sendgmr" X-Received: from danielwinkler-linux.mtv.corp.google.com ([2620:15c:202:201:f693:9fff:fef4:4e59]) (user=danielwinkler job=sendgmr) by 2002:aa7:93b6:0:b029:155:3b0b:d47a with SMTP id x22-20020aa793b60000b02901553b0bd47amr6484486pff.47.1604012799701; Thu, 29 Oct 2020 16:06:39 -0700 (PDT) Date: Thu, 29 Oct 2020 16:06:17 -0700 In-Reply-To: <20201029230623.3630069-1-danielwinkler@google.com> Message-Id: <20201029160317.Bluez.v6.5.Ic4a3667da774f5f34477d5168a68a9280657e2da@changeid> Mime-Version: 1.0 References: <20201029230623.3630069-1-danielwinkler@google.com> X-Mailer: git-send-email 2.29.1.341.ge80a0c044ae-goog Subject: [Bluez PATCH v6 05/10] advertising: Parse intervals and tx power from adv From: Daniel Winkler To: luiz.von.dentz@intel.com Cc: linux-bluetooth@vger.kernel.org, chromeos-bluetooth-upstreaming@chromium.org, Daniel Winkler , Sonny Sasaka , Alain Michaud Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org This change adds parsers for the advertising intervals and tx power properties of the LEAdvertisement1 object. It validates that each field adheres to the 5.2 spec, and that min and max intervals are compatible with each other, i.e. that min interval is less than max interval. A note here for maintainers: The tx power that is sent in the hci parameter command is an int8_t, but as far as I can tell, there is no clean way to use a signed 8-bit integer in dbus. The dbus byte type seems incompatible with negative values in high-level languages (python) without awkward usage manipulation on the client side. For this reason, I chose to use an int16_t type for the tx power dbus field, which is then downcasted to the int8_t in bluetoothd, which at least makes the signed-ness of the type crystal clear to the dbus client that uses it. This change is manually verified by ensuring the intervals and tx power parameters are correctly parsed from the LEAdvertisement1 object, and that the parse fails if the parameters are incorrect or not compatible with each other. Reviewed-by: Sonny Sasaka Reviewed-by: Alain Michaud --- Changes in v6: None Changes in v5: - Only parse new properties if experimental flag is set Changes in v4: None Changes in v3: None Changes in v2: None src/advertising.c | 111 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) diff --git a/src/advertising.c b/src/advertising.c index 7c7599552..8443251bd 100644 --- a/src/advertising.c +++ b/src/advertising.c @@ -54,6 +54,11 @@ struct btd_adv_manager { #define AD_TYPE_BROADCAST 0 #define AD_TYPE_PERIPHERAL 1 +/* BLUETOOTH SPECIFICATION Version 5.2 | Vol 4, Part E, page 2585 + * defines tx power value indicating no preference + */ +#define ADV_TX_POWER_NO_PREFERENCE 0x7F + struct btd_adv_client { struct btd_adv_manager *manager; char *owner; @@ -74,6 +79,9 @@ struct btd_adv_client { struct bt_ad *data; struct bt_ad *scan; uint8_t instance; + uint32_t min_interval; + uint32_t max_interval; + int8_t tx_power; }; struct dbus_obj_match { @@ -937,6 +945,96 @@ static bool parse_secondary(DBusMessageIter *iter, return false; } +static bool parse_min_interval(DBusMessageIter *iter, + struct btd_adv_client *client) +{ + uint32_t min_interval_ms; + + /* Only consider this property if experimental setting is applied */ + if (!(g_dbus_get_flags() & G_DBUS_FLAG_ENABLE_EXPERIMENTAL)) + return true; + + if (!iter) + return false; + + if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_UINT32) + return false; + + dbus_message_iter_get_basic(iter, &min_interval_ms); + + /* Convert ms to jiffies to be used in adv request */ + client->min_interval = min_interval_ms / 0.625; + + /* BLUETOOTH SPECIFICATION Version 5.2 | Vol 4, Part E, page 2584 + * defines acceptable interval range + */ + if (client->min_interval < 0x20 || client->min_interval > 0xFFFFFF) { + client->min_interval = 0; + return false; + } + + return true; +} + +static bool parse_max_interval(DBusMessageIter *iter, + struct btd_adv_client *client) +{ + uint32_t max_interval_ms; + + /* Only consider this property if experimental setting is applied */ + if (!(g_dbus_get_flags() & G_DBUS_FLAG_ENABLE_EXPERIMENTAL)) + return true; + + if (!iter) + return false; + + if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_UINT32) + return false; + + dbus_message_iter_get_basic(iter, &max_interval_ms); + + /* Convert ms to jiffies to be used in adv request */ + client->max_interval = max_interval_ms / 0.625; + + /* BLUETOOTH SPECIFICATION Version 5.2 | Vol 4, Part E, page 2584 + * defines acceptable interval range + */ + if (client->max_interval < 0x20 || client->max_interval > 0xFFFFFF) { + client->max_interval = 0; + return false; + } + + return true; +} + +static bool parse_tx_power(DBusMessageIter *iter, + struct btd_adv_client *client) +{ + int16_t val; + + /* Only consider this property if experimental setting is applied */ + if (!(g_dbus_get_flags() & G_DBUS_FLAG_ENABLE_EXPERIMENTAL)) + return true; + + if (!iter) + return false; + + if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_INT16) + return false; + + dbus_message_iter_get_basic(iter, &val); + + /* BLUETOOTH SPECIFICATION Version 5.2 | Vol 4, Part E, page 2585 + * defines acceptable tx power range + */ + if (val < -127 || val > 20) + return false; + + client->tx_power = val; + + return true; +} + static struct adv_parser { const char *name; bool (*func)(DBusMessageIter *iter, struct btd_adv_client *client); @@ -955,6 +1053,9 @@ static struct adv_parser { { "Discoverable", parse_discoverable }, { "DiscoverableTimeout", parse_discoverable_timeout }, { "SecondaryChannel", parse_secondary }, + { "MinInterval", parse_min_interval }, + { "MaxInterval", parse_max_interval }, + { "TxPower", parse_tx_power }, { }, }; @@ -1083,6 +1184,13 @@ static DBusMessage *parse_advertisement(struct btd_adv_client *client) goto fail; } + if (client->min_interval > client->max_interval) { + /* Min interval must not be bigger than max interval */ + error("MinInterval must be less than MaxInterval (%lu > %lu)", + client->min_interval, client->max_interval); + goto fail; + } + err = refresh_adv(client, add_adv_callback, &client->add_adv_id); if (!err) return NULL; @@ -1158,6 +1266,9 @@ static struct btd_adv_client *client_create(struct btd_adv_manager *manager, client->manager = manager; client->appearance = UINT16_MAX; + client->tx_power = ADV_TX_POWER_NO_PREFERENCE; + client->min_interval = 0; + client->max_interval = 0; return client; -- 2.29.1.341.ge80a0c044ae-goog