Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp1341618ybv; Thu, 20 Feb 2020 18:18:50 -0800 (PST) X-Google-Smtp-Source: APXvYqwvkngOFxzj1yVgAUx3BQ2tek/zj4vgbnWaEYs5Wc4aCX/2QtOtLejT9bViHjwrt84di9G1 X-Received: by 2002:a9d:53cb:: with SMTP id i11mr26862074oth.158.1582251529929; Thu, 20 Feb 2020 18:18:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1582251529; cv=none; d=google.com; s=arc-20160816; b=mgwl9ILP5PtyzQz35c7oGH/g6RN7+aDuSWYtD2sJCy0Z6LVZKXdpNRBYLeYi9/n/LQ YhQk/OThRHjP9RAbbUEqcBCHURKmfNyysq+GTRnXEwQZHrVk+hxd2yAbvORobL/qwGgO QL0VNJNwmGDi4ibQWHU+YNTglxLFQzrSDkIDvwTEaWWYUXv6qYIK7NexBZAJOL3r2GwB 0p3ZBHvhiPsJyoGgBgmybH0iTy+gNeK3t6eHvInAyCedpEihCChmrt7cpKaYM7j477LS Rba71sauSJhtn748V2hhxzj494LBhW9U1YGE9myTy3o+ez1Te9zC8X7xuQNg02uDEULO QvQw== 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=0wmbQ5W6F0i6+4YiIVeFRWlKusjZT1yx8M0iMsxVd9o=; b=xNLsJlShYtNbhZsjpsxHllhEq7K5M+23mNzdc/2Cew/gGRY8+o2CiQtvWh5F48QPEe e2dlDXr4NNmwtztCokGxZSzQf7CkjWOQibekZAEUotdyKbwdHkrV7R5PdphPZnGAUS3F +OjN8xzLb2sgFAP3qlg7Z9vKMqa4pCpRa755LgjEGL/P4ioD5XM4SXfoAVFGa4mkawKf 7bS21im2aIMnwHagAXWqZq6lLQltZnvSYDu7cIRwlIWK0X8LOZvuZELxx3Mn/UKa61z9 tCFBOryMJV3yTZDEZ1pSFpa7pPBwAQm27zNf4OTkJcqJSDwKzjc6VkpyzdSCiqd2t76E gCww== 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 l84si93501oig.5.2020.02.20.18.18.22; Thu, 20 Feb 2020 18:18:49 -0800 (PST) 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 S1729516AbgBUCSQ (ORCPT + 99 others); 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 Content-Transfer-Encoding: 8bit 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); -- 2.21.1