From patchwork Fri Feb 21 02:18:09 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Inga Stotland X-Patchwork-Id: 197359 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 679B3C11D24 for ; Fri, 21 Feb 2020 02:18:17 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 3EC05206EF for ; Fri, 21 Feb 2020 02:18:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729529AbgBUCSQ (ORCPT ); Thu, 20 Feb 2020 21:18:16 -0500 Received: from mga04.intel.com ([192.55.52.120]:20563 "EHLO mga04.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728992AbgBUCSQ (ORCPT ); Thu, 20 Feb 2020 21:18:16 -0500 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga104.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 20 Feb 2020 18:18:16 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.70,466,1574150400"; d="scan'208";a="230313320" Received: from ingas-nuc1.sea.intel.com ([10.254.187.96]) by fmsmga008.fm.intel.com with ESMTP; 20 Feb 2020 18:18:15 -0800 From: Inga Stotland To: linux-bluetooth@vger.kernel.org Cc: brian.gix@intel.com, Inga Stotland Subject: [PATCH BlueZ 2/4] tools/mesh-cfgclient: Save and restore group addresses Date: Thu, 20 Feb 2020 18:18:09 -0800 Message-Id: <20200221021811.30408-3-inga.stotland@intel.com> X-Mailer: git-send-email 2.21.1 In-Reply-To: <20200221021811.30408-1-inga.stotland@intel.com> References: <20200221021811.30408-1-inga.stotland@intel.com> MIME-Version: 1.0 Sender: linux-bluetooth-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org This allows to save created virtual labels and group addresses in configuration file. The stored values can be restored upon the tool start up. --- tools/mesh/cfgcli.c | 15 +++--- tools/mesh/cfgcli.h | 6 +++ tools/mesh/mesh-db.c | 123 +++++++++++++++++++++++++++++++++++++++++++ tools/mesh/mesh-db.h | 3 ++ 4 files changed, 140 insertions(+), 7 deletions(-) diff --git a/tools/mesh/cfgcli.c b/tools/mesh/cfgcli.c index 4930c8b7b..60fce17cf 100644 --- a/tools/mesh/cfgcli.c +++ b/tools/mesh/cfgcli.c @@ -60,11 +60,6 @@ struct pending_req { uint16_t addr; }; -struct mesh_group { - uint16_t addr; - uint8_t label[16]; -}; - static struct l_queue *requests; static struct l_queue *groups; @@ -816,6 +811,8 @@ static struct mesh_group *add_group(uint16_t addr) grp->addr = addr; l_queue_insert(groups, grp, compare_group_addr, NULL); + mesh_db_add_group(grp); + return grp; } @@ -1683,6 +1680,7 @@ retry: if (!tmp) { l_queue_insert(groups, grp, compare_group_addr, NULL); print_group(grp, NULL); + mesh_db_add_group(grp); return bt_shell_noninteractive_quit(EXIT_SUCCESS); } @@ -1819,6 +1817,11 @@ static struct model_info cli_info = { .vendor_id = VENDOR_ID_INVALID }; +void cfgcli_restore_groups(struct l_queue *saved_groups) +{ + groups = saved_groups; +} + struct model_info *cfgcli_init(key_send_func_t key_send, void *user_data) { if (!key_send) @@ -1827,8 +1830,6 @@ struct model_info *cfgcli_init(key_send_func_t key_send, void *user_data) send_key_msg = key_send; key_data = user_data; requests = l_queue_new(); - groups = l_queue_new(); - bt_shell_add_submenu(&cfg_menu); return &cli_info; diff --git a/tools/mesh/cfgcli.h b/tools/mesh/cfgcli.h index 16d2e0a61..c35cb1ca8 100644 --- a/tools/mesh/cfgcli.h +++ b/tools/mesh/cfgcli.h @@ -18,8 +18,14 @@ * */ +struct mesh_group { + uint16_t addr; + uint8_t label[16]; +}; + typedef bool (*key_send_func_t) (void *user_data, uint16_t dst, uint16_t idx, bool is_appkey, bool update); struct model_info *cfgcli_init(key_send_func_t key_func, void *user_data); +void cfgcli_restore_groups(struct l_queue *groups); void cfgcli_cleanup(void); diff --git a/tools/mesh/mesh-db.c b/tools/mesh/mesh-db.c index 5dbb91440..1c8f92e20 100644 --- a/tools/mesh/mesh-db.c +++ b/tools/mesh/mesh-db.c @@ -41,6 +41,7 @@ #include "tools/mesh/keys.h" #include "tools/mesh/remote.h" +#include "tools/mesh/cfgcli.h" #include "tools/mesh/mesh-db.h" #define KEY_IDX_INVALID NET_IDX_INVALID @@ -254,6 +255,81 @@ static uint16_t node_parse_key(json_object *jarray, int i) return idx; } +static int compare_group_addr(const void *a, const void *b, void *user_data) +{ + const struct mesh_group *grp0 = a; + const struct mesh_group *grp1 = b; + + if (grp0->addr < grp1->addr) + return -1; + + if (grp0->addr > grp1->addr) + return 1; + + return 0; +} + +static void load_groups(json_object *jcfg) +{ + json_object *jgroups; + struct l_queue *groups; + int i, sz; + + json_object_object_get_ex(jcfg, "groups", &jgroups); + + if (!jgroups || json_object_get_type(jgroups) != json_type_array) + return; + + groups = l_queue_new(); + + sz = json_object_array_length(jgroups); + + for (i = 0; i < sz; ++i) { + json_object *jgroup, *jval; + struct mesh_group *grp; + uint16_t addr, addr_len; + const char *str; + + jgroup = json_object_array_get_idx(jgroups, i); + if (!jgroup) + continue; + + if (!json_object_object_get_ex(jgroup, "name", &jval)) + continue; + + str = json_object_get_string(jval); + if (strlen(str) != 10) + continue; + + if (sscanf(str + 6, "%04hx", &addr) != 1) + continue; + + if (!json_object_object_get_ex(jgroup, "address", &jval)) + continue; + + str = json_object_get_string(jval); + addr_len = strlen(str); + if (addr_len != 4 && addr_len != 32) + continue; + + if (addr_len == 32 && !IS_VIRTUAL(addr)) + continue; + + grp = l_new(struct mesh_group, 1); + + if (addr_len == 4) + sscanf(str, "%04hx", &grp->addr); + else { + str2hex(str, 32, grp->label, 16); + grp->addr = addr; + } + + l_queue_insert(groups, grp, compare_group_addr, NULL); + } + + cfgcli_restore_groups(groups); +} + static void load_remotes(json_object *jcfg) { json_object *jnodes; @@ -632,6 +708,45 @@ bool mesh_db_app_key_del(uint16_t app_idx) return delete_key(cfg->jcfg, "appKeys", app_idx); } +bool mesh_db_add_group(struct mesh_group *grp) +{ + json_object *jgroup, *jgroups, *jval; + char buf[16]; + + if (!cfg || !cfg->jcfg) + return false; + + if (!json_object_object_get_ex(cfg->jcfg, "groups", &jgroups)) + return false; + + jgroup = json_object_new_object(); + if (!jgroup) + return false; + + snprintf(buf, 11, "Group_%4.4x", grp->addr); + jval = json_object_new_string(buf); + json_object_object_add(jgroup, "name", jval); + + if (IS_VIRTUAL(grp->addr)) { + if (!add_u8_16(jgroup, grp->label, "address")) + goto fail; + } else { + snprintf(buf, 5, "%4.4x", grp->addr); + jval = json_object_new_string(buf); + if (!jval) + goto fail; + json_object_object_add(jgroup, "address", jval); + } + + json_object_array_add(jgroups, jgroup); + + return mesh_config_save((struct mesh_config *) cfg, true, NULL, NULL); + +fail: + json_object_put(jgroup); + return false; +} + bool mesh_db_add_node(uint8_t uuid[16], uint8_t num_els, uint16_t unicast, uint16_t net_idx) { @@ -803,6 +918,13 @@ bool mesh_db_create(const char *fname, const uint8_t token[8], json_object_object_add(jcfg, "appKeys", jarray); + jarray = json_object_new_array(); + if (!jarray) + goto fail; + + json_object_object_add(jcfg, "groups", jarray); + + if (!mesh_config_save((struct mesh_config *) cfg, true, NULL, NULL)) goto fail; @@ -866,6 +988,7 @@ bool mesh_db_load(const char *fname) goto fail; load_remotes(jcfg); + load_groups(jcfg); return true; fail: diff --git a/tools/mesh/mesh-db.h b/tools/mesh/mesh-db.h index 4a7b16ab4..172c2b09b 100644 --- a/tools/mesh/mesh-db.h +++ b/tools/mesh/mesh-db.h @@ -19,6 +19,8 @@ #include "mesh/mesh-config.h" +struct mesh_group; + bool mesh_db_create(const char *fname, const uint8_t token[8], const char *name); bool mesh_db_load(const char *fname); @@ -52,3 +54,4 @@ bool mesh_db_node_model_binding_add(uint16_t unicast, uint8_t ele, bool vendor, uint32_t mod_id, uint16_t app_idx); bool mesh_db_node_model_binding_del(uint16_t unicast, uint8_t ele, bool vendor, uint32_t mod_id, uint16_t app_idx); +bool mesh_db_add_group(struct mesh_group *grp); From patchwork Fri Feb 21 02:18:11 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Inga Stotland X-Patchwork-Id: 197358 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.0 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, UNWANTED_LANGUAGE_BODY, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B7890C11D00 for ; Fri, 21 Feb 2020 02:18:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 8FF4D20722 for ; Fri, 21 Feb 2020 02:18:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729541AbgBUCST (ORCPT ); Thu, 20 Feb 2020 21:18:19 -0500 Received: from mga04.intel.com ([192.55.52.120]:20563 "EHLO mga04.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728992AbgBUCST (ORCPT ); Thu, 20 Feb 2020 21:18:19 -0500 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga104.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 20 Feb 2020 18:18:18 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.70,466,1574150400"; d="scan'208";a="230313331" Received: from ingas-nuc1.sea.intel.com ([10.254.187.96]) by fmsmga008.fm.intel.com with ESMTP; 20 Feb 2020 18:18:17 -0800 From: Inga Stotland To: linux-bluetooth@vger.kernel.org Cc: brian.gix@intel.com, Inga Stotland Subject: [PATCH BlueZ 4/4] mesh: Clean up handling config model publication message Date: Thu, 20 Feb 2020 18:18:11 -0800 Message-Id: <20200221021811.30408-5-inga.stotland@intel.com> X-Mailer: git-send-email 2.21.1 In-Reply-To: <20200221021811.30408-1-inga.stotland@intel.com> References: <20200221021811.30408-1-inga.stotland@intel.com> MIME-Version: 1.0 Sender: linux-bluetooth-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org This tightens up the Config Server code that handles the processing of Config Model Publication Set and Config Model Publication Get messages. --- mesh/cfgmod-server.c | 131 +++++++++++++++---------------------------- 1 file changed, 44 insertions(+), 87 deletions(-) diff --git a/mesh/cfgmod-server.c b/mesh/cfgmod-server.c index 7111411c7..3eb7316fc 100644 --- a/mesh/cfgmod-server.c +++ b/mesh/cfgmod-server.c @@ -36,8 +36,8 @@ static void send_pub_status(struct mesh_node *node, uint16_t net_idx, uint16_t src, uint16_t dst, - uint8_t status, uint16_t ele_addr, uint16_t pub_addr, - uint32_t mod_id, uint16_t idx, bool cred_flag, + uint8_t status, uint16_t ele_addr, uint32_t mod_id, + uint16_t pub_addr, uint16_t idx, bool cred_flag, uint8_t ttl, uint8_t period, uint8_t retransmit) { uint8_t msg[16]; @@ -56,7 +56,7 @@ static void send_pub_status(struct mesh_node *node, uint16_t net_idx, msg[n++] = period; msg[n++] = retransmit; - if (mod_id < 0x10000 || mod_id > VENDOR_ID_MASK) { + if (mod_id >= VENDOR_ID_MASK) { l_put_le16(mod_id, msg + n); n += 2; } else { @@ -76,8 +76,7 @@ static bool config_pub_get(struct mesh_node *node, uint16_t net_idx, { uint32_t mod_id; uint16_t ele_addr; - int ele_idx; - struct mesh_model_pub *pub = NULL; + struct mesh_model_pub *pub; int status; if (size == 4) { @@ -90,27 +89,22 @@ static bool config_pub_get(struct mesh_node *node, uint16_t net_idx, return false; ele_addr = l_get_le16(pkt); - ele_idx = node_get_element_idx(node, ele_addr); - - if (ele_idx >= 0) - pub = mesh_model_pub_get(node, ele_addr, mod_id, &status); - else - status = MESH_STATUS_INVALID_ADDRESS; + pub = mesh_model_pub_get(node, ele_addr, mod_id, &status); if (pub && status == MESH_STATUS_SUCCESS) send_pub_status(node, net_idx, src, dst, status, ele_addr, - pub->addr, mod_id, pub->idx, pub->credential, + mod_id, pub->addr, pub->idx, pub->credential, pub->ttl, pub->period, pub->retransmit); else - send_pub_status(node, net_idx, src, dst, status, ele_addr, 0, - mod_id, 0, 0, 0, 0, 0); + send_pub_status(node, net_idx, src, dst, status, ele_addr, + mod_id, 0, 0, 0, 0, 0, 0); return true; } -static bool config_pub_set(struct mesh_node *node, uint16_t net_idx, +static void config_pub_set(struct mesh_node *node, uint16_t net_idx, uint16_t src, uint16_t dst, - const uint8_t *pkt, uint16_t size, - bool unreliable) + const uint8_t *pkt, uint8_t virt_offset, + bool vendor, bool unreliable) { uint32_t mod_id; uint16_t ele_addr, idx, ota = 0; @@ -119,93 +113,58 @@ static bool config_pub_set(struct mesh_node *node, uint16_t net_idx, uint8_t ttl, period; uint8_t retransmit; int status; - bool cred_flag, b_virt = false; - bool vendor = false; - - switch (size) { - default: - return false; - - case 11: - idx = l_get_le16(pkt + 4); - ttl = pkt[6]; - period = pkt[7]; - retransmit = pkt[8]; - mod_id = l_get_le16(pkt + 9); - mod_id |= VENDOR_ID_MASK; - break; + bool cred_flag; - case 13: - idx = l_get_le16(pkt + 4); - ttl = pkt[6]; - period = pkt[7]; - retransmit = pkt[8]; - mod_id = l_get_le16(pkt + 9) << 16; - mod_id |= l_get_le16(pkt + 11); - vendor = true; - break; + idx = l_get_le16(pkt + 4 + virt_offset); + ttl = pkt[6 + virt_offset]; + period = pkt[7 + virt_offset]; + retransmit = pkt[8 + virt_offset]; + mod_id = l_get_le16(pkt + 9 + virt_offset); - case 25: - b_virt = true; - idx = l_get_le16(pkt + 18); - ttl = pkt[20]; - period = pkt[21]; - retransmit = pkt[22]; - mod_id = l_get_le16(pkt + 23); + if (!vendor) mod_id |= VENDOR_ID_MASK; - break; - - case 27: - b_virt = true; - idx = l_get_le16(pkt + 18); - ttl = pkt[20]; - period = pkt[21]; - retransmit = pkt[22]; - mod_id = l_get_le16(pkt + 23) << 16; - mod_id |= l_get_le16(pkt + 25); - vendor = true; - break; - } + else + mod_id |= l_get_le16(pkt + 11 + virt_offset); ele_addr = l_get_le16(pkt); pub_addr = pkt + 2; - /* Doesn't accept out-of-range TTLs */ - if (ttl > TTL_MASK && ttl != DEFAULT_TTL) - return false; + /* Doesn't accept virtual seeming addresses */ + test_addr = l_get_le16(pub_addr); + if (!virt_offset && IS_VIRTUAL(test_addr)) + return; /* Get cred_flag */ cred_flag = !!(CREDFLAG_MASK & idx); - /* Ignore non-IDX bits */ + /* Get AppKey index */ idx &= APP_IDX_MASK; - /* Doesn't accept virtual seeming addresses */ - test_addr = l_get_le16(pub_addr); - if (!b_virt && test_addr > 0x7fff && test_addr < 0xc000) - return false; - status = mesh_model_pub_set(node, ele_addr, mod_id, pub_addr, idx, cred_flag, ttl, period, retransmit, - b_virt, &ota); + virt_offset != 0, &ota); l_debug("pub_set: status %d, ea %4.4x, ota: %4.4x, mod: %x, idx: %3.3x", status, ele_addr, ota, mod_id, idx); - if (IS_UNASSIGNED(ota) && !b_virt) { - ttl = period = idx = 0; + if (status != MESH_STATUS_SUCCESS) { + if (!unreliable) + send_pub_status(node, net_idx, src, dst, status, + ele_addr, mod_id, 0, 0, 0, 0, 0, 0); - /* Remove model publication from config file */ - if (status == MESH_STATUS_SUCCESS) - mesh_config_model_pub_del(node_config_get(node), - ele_addr, vendor ? mod_id : mod_id & 0x0000ffff, - vendor); - goto done; + return; } - if (status == MESH_STATUS_SUCCESS) { + if (IS_UNASSIGNED(ota) && !virt_offset) { + ttl = period = idx = 0; + + /* Remove model publication from config file */ + mesh_config_model_pub_del(node_config_get(node), ele_addr, + vendor ? mod_id : mod_id & ~VENDOR_ID_MASK, + vendor); + } else { struct mesh_config_pub db_pub = { - .virt = b_virt, + .virt = (virt_offset != 0), .addr = ota, .idx = idx, .ttl = ttl, @@ -215,21 +174,19 @@ static bool config_pub_set(struct mesh_node *node, uint16_t net_idx, .interval = ((retransmit >> 3) + 1) * 50 }; - if (b_virt) + if (virt_offset) memcpy(db_pub.virt_addr, pub_addr, 16); /* Save model publication to config file */ if (!mesh_config_model_pub_add(node_config_get(node), ele_addr, - vendor ? mod_id : mod_id & 0x0000ffff, + vendor ? mod_id : mod_id & ~VENDOR_ID_MASK, vendor, &db_pub)) status = MESH_STATUS_STORAGE_FAIL; } -done: if (!unreliable) send_pub_status(node, net_idx, src, dst, status, ele_addr, ota, mod_id, idx, cred_flag, ttl, period, retransmit); - return true; } static void send_sub_status(struct mesh_node *node, uint16_t net_idx, @@ -825,7 +782,7 @@ static bool cfg_srv_pkt(uint16_t src, uint32_t dst, uint16_t unicast, if (size != 25 && size != 27) return true; - config_pub_set(node, net_idx, src, unicast, pkt, size, + config_pub_set(node, net_idx, src, unicast, pkt, 14, size == 27, !!(opcode & OP_UNRELIABLE)); break; @@ -833,7 +790,7 @@ static bool cfg_srv_pkt(uint16_t src, uint32_t dst, uint16_t unicast, if (size != 11 && size != 13) return true; - config_pub_set(node, net_idx, src, unicast, pkt, size, + config_pub_set(node, net_idx, src, unicast, pkt, 0, size == 13, !!(opcode & OP_UNRELIABLE)); break;