Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp1207441pxb; Thu, 4 Mar 2021 06:11:01 -0800 (PST) X-Google-Smtp-Source: ABdhPJxIYknnOgOBWTXfVYZTsl+XlkfUThZ0Kl9q3iZDnxwwHFrJVGUZKadcX/5MD129G/3r2/lk X-Received: by 2002:aa7:c98f:: with SMTP id c15mr4627789edt.231.1614867061339; Thu, 04 Mar 2021 06:11:01 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614867061; cv=none; d=google.com; s=arc-20160816; b=riP4QvD2wpYqervXjRyXeStgQqYoLDkvxCGZS1Ub71mflfczhdWOJAD0TRRq4Gjvmt cMBrydCMEFj4NyJIxss3FtDPHvxOcz0g1acjjG41OWPiscPnoWEljmn4a7A8EGKiX0xG bFDOiC5VVb1VUFDxcYIf04zoD5sV9lVbjWwIBDvK5qICYQq1GqDjqXo5jC0E2Kjhr608 aM6ipNyA169HAKi4Y9chINg47TKhckn16dRfaWTS2lt8+T6WtRtNd8WVV+IFxmrPkK5I RqF5lQ4G7Dn1NYg2spRey28HGoN/3L2bOclXxSBTe3EOqxPY32StjIzmbihFGamZHMhv eisg== 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=J9XdCOPNAU52LaRLQilbw5tfe+mZa/UPh1feVHooDCY=; b=paO18UabWXv8SyaoGjpA9bb8iZRSVI5MSa+3zMR0CAxoqgGmy0DP7fKMG+i+ZH4+kE 1SSzVbrLSOduwSWAErwYnFWyE7DR8pHmbVu6JHmJbgTOPwjJEGz1ikynOZBNcTDlnnJx YJ1nbh6Ii70DWkDbWjbS2KlRRkZdyGsw+R55Yc8ZyslgxyPJu+mOli4VeTVDNmzuNiW6 SVr/ns0K9TSUqtCC2q15Eno/9VG7s5kgbqf5pgczDFdk6thGOsBb4TGr3mRXdMbnzd46 CSn5CFvuJeit4GfW0iom3/1z+FIHDA6oeF9qSsKRmmKa4tL6BqK/UuXLHR1wnVS8vFRc PPtQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=mBlzEFf1; 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 z9si2669585ejr.490.2021.03.04.06.10.38; Thu, 04 Mar 2021 06:11:01 -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=@google.com header.s=20161025 header.b=mBlzEFf1; 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 S1348829AbhCDAd4 (ORCPT + 99 others); Wed, 3 Mar 2021 19:33:56 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40892 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244561AbhCCTV1 (ORCPT ); Wed, 3 Mar 2021 14:21:27 -0500 Received: from mail-qt1-x849.google.com (mail-qt1-x849.google.com [IPv6:2607:f8b0:4864:20::849]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1F361C06175F for ; Wed, 3 Mar 2021 11:20:25 -0800 (PST) Received: by mail-qt1-x849.google.com with SMTP id a41so16865178qtk.0 for ; Wed, 03 Mar 2021 11:20:25 -0800 (PST) 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=J9XdCOPNAU52LaRLQilbw5tfe+mZa/UPh1feVHooDCY=; b=mBlzEFf12zNqqm/7r6iT9KZOTPJnRehkfwpG0UL9Sgys13IzwyT/ORq1UwL7Qho9uS oX4F707X6Am08BFlVd9LzGdrZWA2dcog0SdEx4qU+7d3EB8oWl22Kd3N2lQjBD1rtrll vFIabB9qakfrjYG4IrYCxxJYUbR6UkkVwg8tOL5EMPq65BWgTXS6hVeTVsY4AjnYStRu tOOwptEtWpKhNTSG4V6SkUifHy8TFURP70ePSWWCfWAXfT0W4yO7g5GsYzJXYFkVeUu2 mevCIsAqI5Tu3WL9Ft8uZ/u2x3ymgIA98CHKg1Wfco97XkU+1sbzHbVzIzEWdO398xU8 ebcA== 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=J9XdCOPNAU52LaRLQilbw5tfe+mZa/UPh1feVHooDCY=; b=B+zaPaFQxUX2SWhTMZkexgUsXHE0apgKL+4ktcuQx2a7aBG9Ny2RirT77GfM1zC4Ou WTqPipjGXB+Yv+3uD2w40AlDcmvdIPKdLCyWhUmzeY9o4OStjy1N8DqYlFHJ06fcpw8F fxBYxzcVP8nAINQF2taX7EoDEYPbh80lOxQNzNSQV43qlv4GZvDkTapxhwEC5OaUNtDl EtWtCzMtqfvM5kuf//5GYhTVjP5TydTB09FeDG02f8rGpc0JCDmvpMl+AEPbl088/Nkc VURbsDx8K2RC0DtDovkHWQx2Lsal67imUcfgG2NI+mgIHU3xImRWwp55ci68u59oD5ia Qs5w== X-Gm-Message-State: AOAM532sI9m+KMZK9YN6E89F5DNAFe2ljeZgn0G+AKYudbhA1irF6k/c BSQnDV620yGFsheAnv7qPlVVi4oAkYD52ifb/1ENK/BnmBKzjWdewpShiPGQ+5Fz6LFK21SAn2k yi80SwfoGL1OAA6uorc3h3t9GgnOLe90yfo9DzqxOUBcJCh4JelQGPAU02unsm8pVqeny9EcFji 3zG0LS3zyFuyPFsAmE Sender: "danielwinkler via sendgmr" X-Received: from danielwinkler-linux.mtv.corp.google.com ([2620:15c:202:201:45cc:69de:aba1:a948]) (user=danielwinkler job=sendgmr) by 2002:a0c:a905:: with SMTP id y5mr676164qva.55.1614799224240; Wed, 03 Mar 2021 11:20:24 -0800 (PST) Date: Wed, 3 Mar 2021 11:20:09 -0800 In-Reply-To: <20210303192012.1695032-1-danielwinkler@google.com> Message-Id: <20210303111817.Bluez.1.I1411482bfff45aecdec1bc8c895fc7148ee3f50c@changeid> Mime-Version: 1.0 References: <20210303192012.1695032-1-danielwinkler@google.com> X-Mailer: git-send-email 2.30.1.766.gb4fecdf3b7-goog Subject: [Bluez PATCH 1/3] advertising: Generate advertising data earlier in pipeline From: Daniel Winkler To: linux-bluetooth@vger.kernel.org, luiz.dentz@gmail.com Cc: chromeos-bluetooth-upstreaming@chromium.org, Daniel Winkler , Alain Michaud , Sonny Sasaka Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org This change moves the advertising data generation to the beginning of the registration pipeline. This is necessary for the following patch, which will need to know whether the scan response data is existent so that the parameter request can be populated correctly. Reviewed-by: Alain Michaud Reviewed-by: Sonny Sasaka --- src/advertising.c | 79 +++++++++++++++++++++++++---------------------- 1 file changed, 42 insertions(+), 37 deletions(-) diff --git a/src/advertising.c b/src/advertising.c index 15a343e52..f3dc357a1 100644 --- a/src/advertising.c +++ b/src/advertising.c @@ -80,6 +80,10 @@ struct btd_adv_client { uint32_t flags; struct bt_ad *data; struct bt_ad *scan; + uint8_t *adv_data; + uint8_t *scan_rsp; + size_t adv_data_len; + size_t scan_rsp_len; uint8_t instance; uint32_t min_interval; uint32_t max_interval; @@ -141,6 +145,16 @@ static void client_free(void *data) bt_ad_unref(client->data); bt_ad_unref(client->scan); + if (client->adv_data) { + free(client->adv_data); + client->adv_data = NULL; + } + + if (client->scan_rsp) { + free(client->scan_rsp); + client->scan_rsp = NULL; + } + g_dbus_proxy_unref(client->proxy); if (client->owner) @@ -915,6 +929,22 @@ static int refresh_extended_adv(struct btd_adv_client *client, flags |= MGMT_ADV_PARAM_TX_POWER; } + client->adv_data = generate_adv_data(client, &flags, + &client->adv_data_len); + if (!client->adv_data || + (client->adv_data_len > calc_max_adv_len(client, flags))) { + error("Advertising data too long or couldn't be generated."); + return -EINVAL; + } + + client->scan_rsp = generate_scan_rsp(client, &flags, + &client->scan_rsp_len); + if (!client->scan_rsp && client->scan_rsp_len) { + error("Scan data couldn't be generated."); + free(client->adv_data); + return -EINVAL; + } + cp.flags = htobl(flags); mgmt_ret = mgmt_send(client->manager->mgmt, MGMT_OP_ADD_EXT_ADV_PARAMS, @@ -1222,11 +1252,6 @@ static void add_adv_params_callback(uint8_t status, uint16_t length, const struct mgmt_rp_add_ext_adv_params *rp = param; struct mgmt_cp_add_ext_adv_data *cp = NULL; uint8_t param_len; - uint8_t *adv_data = NULL; - size_t adv_data_len; - uint8_t *scan_rsp = NULL; - size_t scan_rsp_len = -1; - uint32_t flags = 0; unsigned int mgmt_ret; dbus_int16_t tx_power; @@ -1248,23 +1273,8 @@ static void add_adv_params_callback(uint8_t status, uint16_t length, client->instance = rp->instance; - flags = get_adv_flags(client); - - adv_data = generate_adv_data(client, &flags, &adv_data_len); - if (!adv_data || (adv_data_len > rp->max_adv_data_len)) { - error("Advertising data too long or couldn't be generated."); - goto fail; - } - - scan_rsp = generate_scan_rsp(client, &flags, &scan_rsp_len); - if ((!scan_rsp && scan_rsp_len) || - scan_rsp_len > rp->max_scan_rsp_len) { - error("Scan data couldn't be generated."); - goto fail; - } - - param_len = sizeof(struct mgmt_cp_add_advertising) + adv_data_len + - scan_rsp_len; + param_len = sizeof(struct mgmt_cp_add_advertising) + + client->adv_data_len + client->scan_rsp_len; cp = malloc0(param_len); if (!cp) { @@ -1273,15 +1283,11 @@ static void add_adv_params_callback(uint8_t status, uint16_t length, } cp->instance = client->instance; - cp->adv_data_len = adv_data_len; - cp->scan_rsp_len = scan_rsp_len; - memcpy(cp->data, adv_data, adv_data_len); - memcpy(cp->data + adv_data_len, scan_rsp, scan_rsp_len); - - free(adv_data); - free(scan_rsp); - adv_data = NULL; - scan_rsp = NULL; + cp->adv_data_len = client->adv_data_len; + cp->scan_rsp_len = client->scan_rsp_len; + memcpy(cp->data, client->adv_data, client->adv_data_len); + memcpy(cp->data + client->adv_data_len, client->scan_rsp, + client->scan_rsp_len); /* Submit request to update instance data */ mgmt_ret = mgmt_send(client->manager->mgmt, MGMT_OP_ADD_EXT_ADV_DATA, @@ -1305,12 +1311,6 @@ static void add_adv_params_callback(uint8_t status, uint16_t length, return; fail: - if (adv_data) - free(adv_data); - - if (scan_rsp) - free(scan_rsp); - if (cp) free(cp); @@ -1454,6 +1454,11 @@ static struct btd_adv_client *client_create(struct btd_adv_manager *manager, if (!client->scan) goto fail; + client->adv_data = NULL; + client->scan_rsp = NULL; + client->adv_data_len = 0; + client->scan_rsp_len = 0; + client->manager = manager; client->appearance = UINT16_MAX; client->tx_power = ADV_TX_POWER_NO_PREFERENCE; -- 2.30.1.766.gb4fecdf3b7-goog