Received: by 2002:a25:f815:0:0:0:0:0 with SMTP id u21csp1558297ybd; Wed, 26 Jun 2019 20:34:12 -0700 (PDT) X-Google-Smtp-Source: APXvYqwZelJsFmrWhdvN3uqgvZ8G6H20ZYfwNZop/R4+UyvMsau3dT7cqoePbxhpl41lYSi/yYBQ X-Received: by 2002:a17:902:d915:: with SMTP id c21mr1913746plz.335.1561606452780; Wed, 26 Jun 2019 20:34:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1561606452; cv=none; d=google.com; s=arc-20160816; b=wU8uQllnAmVXJ+YSY5DLg/ZGjV/zH5sReT2kzWMhY6Tob+TL9DE/WLt/HEy+xlKjkw 1b7hgbv4qhTkWsfc02hd1MldnZ/25zDGnJ3+gwdXWStRanx6SVlql2/eV1lnIlXKWGYE ihvHUV+6hH/ALN8D1CCFADabmTb/E/wVW3BILwUVbXbLW/M/HOxznBluMKgYi4yLobrN yK9Bq8RpYi9kwXRzBOW6Jp9p7Fyze0tT5pV5T1czXhDcAn8CT17E4EAAVDWMMItunhlo 6OgXxJ/+0BQilyUaefoJUYkFmDbXHCgzJamWUbV4oyhlU+6cdYfcVme4S8BMLfyc9why JCpg== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=zcUnIbawohgGvcQXRm7wpCWlvwIBeFUdIpc3jiIBdpM=; b=Q6u3qzd5OpGNCP39U8g+9iZE7LRg6I4IgTF/xRMy+iKce6iNXhW7h6yLmBcbySDe8H RXpEwGxueKq4zrZnhGg/hknjF/lUDC9dg22/TBBtoVKteLFVJLI1ahCUpv28NinYVv5W PlPuVJTfcyk1x7Ev6ggasv9fZzZd9/PoL9ETvn0x44yfNzxwsxn0Boz17i5+gYqR3hLe +DJCQ7utVJQ/mVrLGbqzmFAgmes+ioKfBtc8uZrEfF+ZUQWJJKarZ0UVkqiNzusbKWsy ZcSyqM7T3cvd4h7ZbZSyU5P1BKpfQq0/jTGxfq9HPnNXmte9J0e6zSyJFXipt5+fKSWz jTJg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-bluetooth-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w16si1052767plp.329.2019.06.26.20.33.46; Wed, 26 Jun 2019 20:34:12 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-bluetooth-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-bluetooth-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726892AbfF0Dd2 (ORCPT + 99 others); Wed, 26 Jun 2019 23:33:28 -0400 Received: from mga12.intel.com ([192.55.52.136]:10350 "EHLO mga12.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726447AbfF0Dd2 (ORCPT ); Wed, 26 Jun 2019 23:33:28 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga003.jf.intel.com ([10.7.209.27]) by fmsmga106.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 26 Jun 2019 20:33:27 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.63,422,1557212400"; d="scan'208";a="164586107" Received: from ingas-nuc1.sea.intel.com ([10.255.82.90]) by orsmga003.jf.intel.com with ESMTP; 26 Jun 2019 20:33:27 -0700 From: Inga Stotland To: linux-bluetooth@vger.kernel.org Cc: brian.gix@intel.com, johan.hedberg@gmail.com, luiz.dentz@gmail.com, Inga Stotland Subject: [PATCH BlueZ 3/4] mesh: Fix and clean up model publicaation code Date: Wed, 26 Jun 2019 20:33:19 -0700 Message-Id: <20190627033320.8898-4-inga.stotland@intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190627033320.8898-1-inga.stotland@intel.com> References: <20190627033320.8898-1-inga.stotland@intel.com> 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 proper checks for model publication removal: the publication is not virtual and the publication address is set to zero, i.e., UNASSIGNED_ADDRESS. Also removes double memory allocation for model publication and miscellaneous redundancies. --- mesh/model.c | 62 ++++++++++++++++++++++------------------------------ mesh/model.h | 2 +- 2 files changed, 27 insertions(+), 37 deletions(-) diff --git a/mesh/model.c b/mesh/model.c index 219371090..bcd607909 100644 --- a/mesh/model.c +++ b/mesh/model.c @@ -593,7 +593,7 @@ static struct mesh_virtual *add_virtual(const uint8_t *v) return virt; } -static int set_pub(struct mesh_model *mod, const uint8_t *mod_addr, +static int set_pub(struct mesh_model *mod, const uint8_t *pub_addr, uint16_t idx, bool cred_flag, uint8_t ttl, uint8_t period, uint8_t retransmit, bool b_virt, uint16_t *dst) @@ -605,11 +605,11 @@ static int set_pub(struct mesh_model *mod, const uint8_t *mod_addr, if (b_virt) *dst = 0; else - *dst = l_get_le16(mod_addr); + *dst = l_get_le16(pub_addr); } if (b_virt) { - virt = add_virtual(mod_addr); + virt = add_virtual(pub_addr); if (!virt) return MESH_STATUS_STORAGE_FAIL; @@ -634,28 +634,18 @@ static int set_pub(struct mesh_model *mod, const uint8_t *mod_addr, grp = virt->v_addr; mod->pub->addr = virt->id; } else { - grp = l_get_le16(mod_addr); + grp = l_get_le16(pub_addr); mod->pub->addr = grp; } if (dst) *dst = grp; - if (IS_UNASSIGNED(grp) && mod->pub) { - l_free(mod->pub); - mod->pub = NULL; - /* Remove publication if Pub Addr is 0x0000 */ - } else { - - if (!mod->pub) - mod->pub = l_new(struct mesh_model_pub, 1); - - mod->pub->credential = cred_flag; - mod->pub->idx = idx; - mod->pub->ttl = ttl; - mod->pub->period = period; - mod->pub->retransmit = retransmit; - } + mod->pub->credential = cred_flag; + mod->pub->idx = idx; + mod->pub->ttl = ttl; + mod->pub->period = period; + mod->pub->retransmit = retransmit; return MESH_STATUS_SUCCESS; } @@ -985,7 +975,7 @@ bool mesh_model_send(struct mesh_node *node, uint16_t src, uint16_t target, } int mesh_model_pub_set(struct mesh_node *node, uint16_t addr, uint32_t id, - const uint8_t *mod_addr, uint16_t idx, bool cred_flag, + const uint8_t *pub_addr, uint16_t idx, bool cred_flag, uint8_t ttl, uint8_t period, uint8_t retransmit, bool b_virt, uint16_t *dst) { @@ -1002,28 +992,28 @@ int mesh_model_pub_set(struct mesh_node *node, uint16_t addr, uint32_t id, if (!appkey_have_key(node_get_net(node), idx)) return MESH_STATUS_INVALID_APPKEY; - status = set_pub(mod, mod_addr, idx, cred_flag, ttl, period, retransmit, - b_virt, dst); - - if (status != MESH_STATUS_SUCCESS) - return status; - /* * If the publication address is set to unassigned address value, * remove the publication */ - if (IS_UNASSIGNED(*dst)) + if (!b_virt && IS_UNASSIGNED(l_get_le16(pub_addr))) { remove_pub(node, mod); - - /* Internal model, call registered callbacks */ - if (mod->cbs && mod->cbs->pub) { - mod->cbs->pub(mod->pub); return MESH_STATUS_SUCCESS; } - /* External model */ - config_update_model_pub_period(node, mod->ele_idx, id, + status = set_pub(mod, pub_addr, idx, cred_flag, ttl, period, retransmit, + b_virt, dst); + + if (status != MESH_STATUS_SUCCESS) + return status; + + if (!mod->cbs) + /* External model */ + config_update_model_pub_period(node, mod->ele_idx, id, pub_period_to_ms(period)); + else + /* Internal model, call registered callbacks */ + mod->cbs->pub(mod->pub); return MESH_STATUS_SUCCESS; } @@ -1373,6 +1363,7 @@ struct mesh_model *mesh_model_setup(struct mesh_node *node, uint8_t ele_idx, struct mesh_db_model *db_mod = data; struct mesh_model *mod; struct mesh_net *net; + struct mesh_db_pub *pub = db_mod->pub; uint32_t i; if (db_mod->num_bindings > MAX_BINDINGS) { @@ -1409,8 +1400,7 @@ struct mesh_model *mesh_model_setup(struct mesh_node *node, uint8_t ele_idx, } /* Add publication if present */ - if (db_mod->pub) { - struct mesh_db_pub *pub = db_mod->pub; + if (pub && (pub->virt || !(IS_UNASSIGNED(pub->addr)))) { uint8_t mod_addr[2]; uint8_t *pub_addr; uint8_t retransmit = (pub->count << 5) + @@ -1418,7 +1408,7 @@ struct mesh_model *mesh_model_setup(struct mesh_node *node, uint8_t ele_idx, /* Add publication */ l_put_le16(pub->addr, &mod_addr); - pub_addr = pub->virt ? pub->virt_addr : (uint8_t *) &mod_addr; + pub_addr = pub->virt ? pub->virt_addr : mod_addr; if (set_pub(mod, pub_addr, pub->idx, pub->credential, pub->ttl, pub->period, retransmit, pub->virt, NULL) != diff --git a/mesh/model.h b/mesh/model.h index f0f97ee0b..a6951293f 100644 --- a/mesh/model.h +++ b/mesh/model.h @@ -95,7 +95,7 @@ struct mesh_model *mesh_model_setup(struct mesh_node *node, uint8_t ele_idx, struct mesh_model_pub *mesh_model_pub_get(struct mesh_node *node, uint16_t addr, uint32_t mod_id, int *status); int mesh_model_pub_set(struct mesh_node *node, uint16_t addr, uint32_t id, - const uint8_t *mod_addr, uint16_t idx, bool cred_flag, + const uint8_t *pub_addr, uint16_t idx, bool cred_flag, uint8_t ttl, uint8_t period, uint8_t retransmit, bool b_virt, uint16_t *dst); -- 2.21.0