Received: by 2002:a25:31c3:0:0:0:0:0 with SMTP id x186csp25474ybx; Tue, 29 Oct 2019 13:45:37 -0700 (PDT) X-Received: by 2002:a17:906:4c52:: with SMTP id d18mr5385056ejw.291.1572381697881; Tue, 29 Oct 2019 13:41:37 -0700 (PDT) X-Google-Smtp-Source: APXvYqwW0F0BM4wIyDuM1AwGMylFhPfewBlufk6RwTY33ddynt05BeX50F4KMFMX+18hV3s3ZSSH X-Received: by 2002:a17:906:4c52:: with SMTP id d18mr5384962ejw.291.1572381696856; Tue, 29 Oct 2019 13:41:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1572381696; cv=none; d=google.com; s=arc-20160816; b=TkBQTMNN/PPm30klTQfsul5HExZfFg02vaPfWNaXvHcWBc1VyBrSyrfoyEPdW5sM25 46/Dramr37Rv1+HWxZ3nOClSCRGMIXeCX37fP01OlH7ZfihgjZFHfNBnknU9UAgKleLO mZMk+vLEDPXo0suWSZsKjvkEcvq89pDLUYKgGlvTUzqjxFQg209h/rV995wndSxw1LHP TchQByaqBG1PJdH7zn9URYrtcsxFnD1OI659tnzGhVoDaqG5xN/Ln7eWMMpezZbD+l67 ooo8D5JTLlEa9OAOOqvjc4DJHQU1l9yR/Xk3jMkZWHPVbt9jdlOdYsH6947sxFvcQD9e UQgQ== 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=UF2CA4fMZa6u7WUhubMgLpSa2nQcMHLd4qxBDCAHFkA=; b=KS3pcCe4dO/dcgdST9lBHeyp6XAQZBFjXIdfNlclDX7Ql3+EmV27JRG2qeuU27rxbJ 8Ov3kpVKAt3BkAbEnTJTq8IWTYW1U8pn9ctR+Zwe5VWWs8LmwFINJhOgCVxxZ2U8JEn0 il09SER3Ds51Y7E7gg3h12gVveWph/9Fjwqkwi5OL6w45F5tfVj4b04qhf6fMNa70wUz TUQqLjUXdN+m/KhvhfewV+t+AqEgyU+Jkt80p/E8rfDraDzHRnTxpH/Hv6fONL9PZZpv tDr3WIVHnIlnk7vTJ2n//GOp11NBa4/r1NIS4o9K1OMvtJkcPrr+G/HGa91MgfM/5mPe IjPA== 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 d10si2817464edk.122.2019.10.29.13.41.12; Tue, 29 Oct 2019 13:41:36 -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 S1727768AbfJ2UlC (ORCPT + 99 others); Tue, 29 Oct 2019 16:41:02 -0400 Received: from mga01.intel.com ([192.55.52.88]:15196 "EHLO mga01.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727766AbfJ2UlB (ORCPT ); Tue, 29 Oct 2019 16:41:01 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga101.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 29 Oct 2019 13:41:01 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.68,245,1569308400"; d="scan'208";a="401277184" Received: from ingas-nuc1.sea.intel.com ([10.255.229.102]) by fmsmga006.fm.intel.com with ESMTP; 29 Oct 2019 13:41:00 -0700 From: Inga Stotland To: linux-bluetooth@vger.kernel.org Cc: brian.gix@intel.com, Inga Stotland Subject: [PATCH BlueZ 04/10] tools/mesh: move remote node processing to a separate file Date: Tue, 29 Oct 2019 13:40:48 -0700 Message-Id: <20191029204054.30599-5-inga.stotland@intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20191029204054.30599-1-inga.stotland@intel.com> References: <20191029204054.30599-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 confines the functionality related to remote node housekeeping into a separate file remote.c --- Makefile.tools | 2 + tools/mesh-cfgclient.c | 76 +++---------------------- tools/mesh/remote.c | 124 +++++++++++++++++++++++++++++++++++++++++ tools/mesh/remote.h | 31 +++++++++++ 4 files changed, 165 insertions(+), 68 deletions(-) create mode 100644 tools/mesh/remote.c create mode 100644 tools/mesh/remote.h diff --git a/Makefile.tools b/Makefile.tools index 12687e495..0b4ddf576 100644 --- a/Makefile.tools +++ b/Makefile.tools @@ -332,7 +332,9 @@ tools_mesh_cfgclient_SOURCES = tools/mesh-cfgclient.c \ tools/mesh/cfgcli.h tools/mesh/cfgcli.c \ tools/mesh/keys.h tools/mesh/keys.c \ tools/mesh/util.h tools/mesh/util.c \ + tools/mesh/remote.h tools/mesh/remote.c \ tools/mesh/agent.h tools/mesh/agent.c + tools_mesh_cfgclient_LDADD = lib/libbluetooth-internal.la src/libshared-ell.la \ $(ell_ldadd) -lreadline endif diff --git a/tools/mesh-cfgclient.c b/tools/mesh-cfgclient.c index f225d021f..d21e55fac 100644 --- a/tools/mesh-cfgclient.c +++ b/tools/mesh-cfgclient.c @@ -39,6 +39,7 @@ #include "tools/mesh/cfgcli.h" #include "tools/mesh/keys.h" #include "tools/mesh/model.h" +#include "tools/mesh/remote.h" #define PROMPT_ON COLOR_BLUE "[mesh-cfgclient]" COLOR_OFF "# " #define PROMPT_OFF "Waiting to connect to bluetooth-meshd..." @@ -84,13 +85,6 @@ struct unprov_device { uint8_t uuid[16]; }; -struct remote_node { - uint16_t unicast; - uint16_t net_idx; - uint8_t uuid[16]; - uint8_t num_ele; -}; - static struct l_dbus *dbus; static struct l_queue *node_proxies; @@ -99,7 +93,6 @@ static struct meshcfg_node *local; static struct model_info *cfgcli; static struct l_queue *devices; -static struct l_queue *nodes; static bool prov_in_progress; static const char *caps[2] = {"out-numeric", "in-numeric"}; @@ -192,37 +185,6 @@ static void print_device(void *a, void *b) l_free(str); } -static bool match_node_addr(const void *a, const void *b) -{ - const struct remote_node *rmt = a; - uint16_t addr = L_PTR_TO_UINT(b); - - if (addr >= rmt->unicast && - addr <= (rmt->unicast + rmt->num_ele - 1)) - return true; - - return false; -} - -static uint16_t get_primary_subnet_idx(uint16_t addr) -{ - struct remote_node *rmt; - - rmt = l_queue_find(nodes, match_node_addr, L_UINT_TO_PTR(addr)); - - if (!rmt) { - bt_shell_printf("Net key not found: trying %4.4x\n", - PRIMARY_NET_IDX); - return prov_net_idx; - /* - * TODO: When the remote node recovery from storage is - * implemented, return NET_IDX_INVALID" here. - */ - } - - return rmt->net_idx; -} - struct send_data { const char *ele_path; bool rmt; @@ -278,7 +240,7 @@ static bool send_msg(void *user_data, uint16_t dst, uint16_t idx, method_name = is_dev_key ? "DevKeySend" : "Send"; if (is_dev_key) { - net_idx_tx = get_primary_subnet_idx(dst); + net_idx_tx = remote_get_subnet_idx(dst); if (net_idx_tx == NET_IDX_INVALID) return false; } @@ -319,7 +281,7 @@ static bool send_key(void *user_data, uint16_t dst, uint16_t key_idx, uint16_t net_idx; const char *method_name = (!is_appkey) ? "AddNetKey" : "AddAppKey"; - net_idx = get_primary_subnet_idx(dst); + net_idx = remote_get_subnet_idx(dst); if (net_idx == NET_IDX_INVALID) return false; @@ -544,6 +506,7 @@ static void create_net_reply(struct l_dbus_proxy *proxy, bt_shell_printf("Created new node with token %s\n", str); l_free(str); + remote_add_node(app.uuid, 0x0001, 1, PRIMARY_NET_IDX); keys_add_net_key(PRIMARY_NET_IDX); l_dbus_proxy_method_call(net_proxy, "Attach", attach_node_setup, @@ -641,26 +604,9 @@ static void cmd_list_unprov(int argc, char *argv[]) l_queue_foreach(devices, print_device, NULL); } -static void print_node(void *a, void *b) -{ - struct remote_node *node = a; - char *str; - - bt_shell_printf(COLOR_YELLOW "Mesh node:\n" COLOR_OFF); - str = l_util_hexstring_upper(node->uuid, 16); - bt_shell_printf("\t" COLOR_GREEN "UUID = %s\n" COLOR_OFF, str); - l_free(str); - bt_shell_printf("\t" COLOR_GREEN "primary = %4.4x\n" COLOR_OFF, - node->unicast); - bt_shell_printf("\t" COLOR_GREEN "elements = %u\n" COLOR_OFF, - node->num_ele); - bt_shell_printf("\t" COLOR_GREEN "net_key_idx = %3.3x\n" COLOR_OFF, - node->net_idx); -} - static void cmd_list_nodes(int argc, char *argv[]) { - l_queue_foreach(nodes, print_node, NULL); + remote_print_all(); } static void add_node_reply(struct l_dbus_proxy *proxy, @@ -972,7 +918,6 @@ static struct l_dbus_message *add_node_cmplt_call(struct l_dbus *dbus, uint8_t cnt; uint32_t n; uint8_t *uuid; - struct remote_node *node; if (!prov_in_progress) return l_dbus_message_new_error(msg, dbus_err_fail, NULL); @@ -991,15 +936,11 @@ static struct l_dbus_message *add_node_cmplt_call(struct l_dbus *dbus, return l_dbus_message_new_error(msg, dbus_err_args, NULL); } - node = l_new(struct remote_node, 1); - memcpy(node->uuid, uuid, 16); - node->unicast = unicast; - node->num_ele = cnt; - node->net_idx = prov_net_idx; + remote_add_node(uuid, unicast, cnt, prov_net_idx); bt_shell_printf("Provisioning done:\n"); - print_node(node, NULL); - l_queue_push_tail(nodes, node); + remote_print_node(unicast); + remove_device(uuid); prov_address = unicast + cnt; @@ -1227,7 +1168,6 @@ int main(int argc, char *argv[]) node_proxies = l_queue_new(); devices = l_queue_new(); - nodes = l_queue_new(); status = bt_shell_run(); diff --git a/tools/mesh/remote.c b/tools/mesh/remote.c new file mode 100644 index 000000000..46839772f --- /dev/null +++ b/tools/mesh/remote.c @@ -0,0 +1,124 @@ +/* + * + * BlueZ - Bluetooth protocol stack for Linux + * + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include + +#include "src/shared/shell.h" +#include "src/shared/util.h" + +#include "mesh/mesh-defs.h" +#include "tools/mesh/remote.h" + +static struct l_queue *nodes; + +void remote_add_node(const uint8_t uuid[16], uint16_t unicast, + uint8_t ele_cnt, uint16_t net_idx) +{ + struct remote_node *rmt = l_new(struct remote_node, 1); + + memcpy(rmt->uuid, uuid, 16); + rmt->unicast = unicast; + rmt->num_ele = ele_cnt; + rmt->net_keys = l_queue_new(); + + l_queue_push_tail(rmt->net_keys, L_UINT_TO_PTR(net_idx)); + + if (!nodes) + nodes = l_queue_new(); + + l_queue_push_tail(nodes, rmt); +} + +static bool match_node_addr(const void *a, const void *b) +{ + const struct remote_node *rmt = a; + uint16_t addr = L_PTR_TO_UINT(b); + + if (addr >= rmt->unicast && + addr <= (rmt->unicast + rmt->num_ele - 1)) + return true; + + return false; +} + +uint16_t remote_get_subnet_idx(uint16_t addr) +{ + struct remote_node *rmt; + uint32_t net_idx; + + rmt = l_queue_find(nodes, match_node_addr, L_UINT_TO_PTR(addr)); + + if (!rmt || l_queue_isempty(rmt->net_keys)) + return NET_IDX_INVALID; + + net_idx = L_PTR_TO_UINT(l_queue_peek_head(rmt->net_keys)); + + return (uint16_t) net_idx; +} + +static void print_subnet(void *net_key, void *user_data) +{ + uint16_t net_idx = L_PTR_TO_UINT(net_key); + + bt_shell_printf("%3.3x ", net_idx); +} + +static void print_node(void *rmt, void *user_data) +{ + struct remote_node *node = rmt; + char *str; + + bt_shell_printf(COLOR_YELLOW "Mesh node:\n" COLOR_OFF); + str = l_util_hexstring_upper(node->uuid, 16); + bt_shell_printf("\t" COLOR_GREEN "UUID = %s\n" COLOR_OFF, str); + l_free(str); + bt_shell_printf("\t" COLOR_GREEN "primary = %4.4x\n" COLOR_OFF, + node->unicast); + bt_shell_printf("\t" COLOR_GREEN "elements = %u\n" COLOR_OFF, + node->num_ele); + bt_shell_printf("\t" COLOR_GREEN "net_keys = "); + l_queue_foreach(node->net_keys, print_subnet, NULL); + bt_shell_printf("\n" COLOR_OFF); +} + +void remote_print_node(uint16_t addr) +{ + struct remote_node *rmt; + + if (!nodes) + return; + + rmt = l_queue_find(nodes, match_node_addr, L_UINT_TO_PTR(addr)); + if (!rmt) + return; + + print_node(rmt, NULL); +} + +void remote_print_all(void) +{ + if (!nodes) + return; + + l_queue_foreach(nodes, print_node, NULL); +} diff --git a/tools/mesh/remote.h b/tools/mesh/remote.h new file mode 100644 index 000000000..dce1619c0 --- /dev/null +++ b/tools/mesh/remote.h @@ -0,0 +1,31 @@ +/* + * + * BlueZ - Bluetooth protocol stack for Linux + * + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + */ + +struct remote_node { + uint16_t unicast; + struct l_queue *net_keys; + uint8_t uuid[16]; + uint8_t num_ele; +}; + +void remote_add_node(const uint8_t uuid[16], uint16_t unicast, + uint8_t ele_cnt, uint16_t net_idx); +uint16_t remote_get_subnet_idx(uint16_t addr); +void remote_print_node(uint16_t addr); +void remote_print_all(void); -- 2.21.0