From patchwork Fri Apr 3 01:26:54 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Stotland, Inga" X-Patchwork-Id: 11471729 X-Patchwork-Delegate: brian.gix@gmail.com Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id C8E3414B4 for ; Fri, 3 Apr 2020 01:26:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id A20462077D for ; Fri, 3 Apr 2020 01:26:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389422AbgDCB05 (ORCPT ); Thu, 2 Apr 2020 21:26:57 -0400 Received: from mga06.intel.com ([134.134.136.31]:19526 "EHLO mga06.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389108AbgDCB05 (ORCPT ); Thu, 2 Apr 2020 21:26:57 -0400 IronPort-SDR: 98j8STYkS/nw+SrB6un8oLThesEDEGOBpf0tRuBb+wBMvY1c6HR+CdObPf6p7OgWk1FtSodFjS AQdDpobCaW4g== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Apr 2020 18:26:57 -0700 IronPort-SDR: qafVxho77lhYEZ1gVEIIuxko6mwO4t6sKPMlavJ/Wbzq7w9e1XGKqoWJNVAKbtHJ1JaJtWAHl+ WLFalETbhm1w== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,337,1580803200"; d="scan'208";a="396569397" Received: from ingas-nuc1.sea.intel.com ([10.255.231.70]) by orsmga004.jf.intel.com with ESMTP; 02 Apr 2020 18:26:56 -0700 From: Inga Stotland To: linux-bluetooth@vger.kernel.org Cc: brian.gix@intel.com, Inga Stotland Subject: [PATCH BlueZ v2 1/2] tools/mescfg-client: Use local routines for config write Date: Thu, 2 Apr 2020 18:26:54 -0700 Message-Id: <20200403012655.17517-1-inga.stotland@intel.com> X-Mailer: git-send-email 2.21.1 MIME-Version: 1.0 Sender: linux-bluetooth-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org This removes dependencies on internal structures of mesh/mesh-config-json.c. --- Makefile.tools | 1 - tools/mesh/mesh-db.c | 95 ++++++++++++++++++++++++++++++++++++-------- 2 files changed, 78 insertions(+), 18 deletions(-) diff --git a/Makefile.tools b/Makefile.tools index f43764adc..9b9236609 100644 --- a/Makefile.tools +++ b/Makefile.tools @@ -336,7 +336,6 @@ tools_mesh_cfgclient_SOURCES = tools/mesh-cfgclient.c \ tools/mesh/agent.h tools/mesh/agent.c \ tools/mesh/mesh-db.h tools/mesh/mesh-db.c \ mesh/util.h mesh/util.c \ - mesh/mesh-config.h mesh/mesh-config-json.c \ mesh/crypto.h mesh/crypto.c tools_mesh_cfgclient_LDADD = lib/libbluetooth-internal.la src/libshared-ell.la \ diff --git a/tools/mesh/mesh-db.c b/tools/mesh/mesh-db.c index 05e96e554..5dd381232 100644 --- a/tools/mesh/mesh-db.c +++ b/tools/mesh/mesh-db.c @@ -50,11 +50,73 @@ struct mesh_db { json_object *jcfg; char *cfg_fname; uint8_t token[8]; - uint8_t pad[12]; struct timeval write_time; }; -struct mesh_db *cfg; +static struct mesh_db *cfg; +static const char *bak_ext = ".bak"; +static const char *tmp_ext = ".tmp"; + +static bool save_config_file(const char *fname) +{ + FILE *outfile; + const char *str; + bool result = false; + + outfile = fopen(fname, "w"); + if (!outfile) { + l_error("Failed to save configuration to %s", cfg->cfg_fname); + return false; + } + + str = json_object_to_json_string_ext(cfg->jcfg, + JSON_C_TO_STRING_PRETTY); + + if (fwrite(str, sizeof(char), strlen(str), outfile) < strlen(str)) + l_warn("Incomplete write of mesh configuration"); + else + result = true; + + fclose(outfile); + + return result; +} + +static bool save_config(void) +{ + char *fname_tmp, *fname_bak, *fname_cfg; + bool result = false; + + fname_cfg = cfg->cfg_fname; + fname_tmp = l_strdup_printf("%s%s", fname_cfg, tmp_ext); + fname_bak = l_strdup_printf("%s%s", fname_cfg, bak_ext); + remove(fname_tmp); + + result = save_config_file(fname_tmp); + + if (result) { + remove(fname_bak); + rename(fname_cfg, fname_bak); + rename(fname_tmp, fname_cfg); + } + + remove(fname_tmp); + + l_free(fname_tmp); + l_free(fname_bak); + + gettimeofday(&cfg->write_time, NULL); + + return result; +} + +static void release_config(void) +{ + l_free(cfg->cfg_fname); + json_object_put(cfg->jcfg); + l_free(cfg); + cfg = NULL; +} static json_object *get_node_by_unicast(uint16_t unicast) { @@ -430,7 +492,7 @@ static bool add_node_key(json_object *jobj, const char *desc, uint16_t idx) json_object_object_add(jkey, "index", jval); json_object_array_add(jarray, jkey); - return mesh_config_save((struct mesh_config *) cfg, true, NULL, NULL); + return save_config(); } bool mesh_db_node_net_key_add(uint16_t unicast, uint16_t idx) @@ -461,7 +523,7 @@ bool mesh_db_node_ttl_set(uint16_t unicast, uint8_t ttl) if (!write_int(jnode, "defaultTTL", ttl)) return false; - return mesh_config_save((struct mesh_config *) cfg, true, NULL, NULL); + return save_config(); } static void jarray_key_del(json_object *jarray, int16_t idx) @@ -500,7 +562,7 @@ static bool delete_key(json_object *jobj, const char *desc, uint16_t idx) jarray_key_del(jarray, idx); - return mesh_config_save((struct mesh_config *) cfg, true, NULL, NULL); + return save_config(); } bool mesh_db_node_net_key_del(uint16_t unicast, uint16_t net_idx) @@ -645,7 +707,7 @@ bool mesh_db_net_key_add(uint16_t net_idx) json_object_array_add(jarray, jkey); - return mesh_config_save((struct mesh_config *) cfg, true, NULL, NULL); + return save_config(); fail: json_object_put(jkey); @@ -681,7 +743,7 @@ bool mesh_db_net_key_phase_set(uint16_t net_idx, uint8_t phase) json_object_object_add(jkey, "phase", jval); - return mesh_config_save((struct mesh_config *) cfg, true, NULL, NULL); + return save_config(); } bool mesh_db_app_key_add(uint16_t net_idx, uint16_t app_idx) @@ -692,7 +754,7 @@ bool mesh_db_app_key_add(uint16_t net_idx, uint16_t app_idx) if (!add_app_key(cfg->jcfg, net_idx, app_idx)) return false; - return mesh_config_save((struct mesh_config *) cfg, true, NULL, NULL); + return save_config(); } bool mesh_db_app_key_del(uint16_t app_idx) @@ -735,7 +797,7 @@ bool mesh_db_add_group(struct mesh_group *grp) json_object_array_add(jgroups, jgroup); - return mesh_config_save((struct mesh_config *) cfg, true, NULL, NULL); + return save_config(); fail: json_object_put(jgroup); @@ -873,7 +935,7 @@ bool mesh_db_add_node(uint8_t uuid[16], uint8_t num_els, uint16_t unicast, json_object_array_add(jnodes, jnode); - if (!mesh_config_save((struct mesh_config *) cfg, true, NULL, NULL)) + if (!save_config()) goto fail; return true; @@ -919,7 +981,7 @@ bool mesh_db_del_node(uint16_t unicast) json_object_array_del_idx(jarray, i, 1); - return mesh_config_save((struct mesh_config *) cfg, true, NULL, NULL); + return save_config(); } bool mesh_db_get_token(uint8_t token[8]) @@ -966,7 +1028,7 @@ bool mesh_db_set_addr_range(uint16_t low, uint16_t high) if (!write_uint16_hex(cfg->jcfg, "high", high)) return false; - return mesh_config_save((struct mesh_config *) cfg, true, NULL, NULL); + return save_config(); } bool mesh_db_create(const char *fname, const uint8_t token[8], @@ -1019,14 +1081,13 @@ bool mesh_db_create(const char *fname, const uint8_t token[8], json_object_object_add(jcfg, "appKeys", jarray); - if (!mesh_config_save((struct mesh_config *) cfg, true, NULL, NULL)) + if (!save_config()) goto fail; return true; fail: - mesh_config_release((struct mesh_config *)cfg); - cfg = NULL; + release_config(); return false; } @@ -1085,7 +1146,7 @@ bool mesh_db_load(const char *fname) return true; fail: - mesh_config_release((struct mesh_config *)cfg); - cfg = NULL; + release_config(); + return false; } From patchwork Fri Apr 3 01:26:55 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Stotland, Inga" X-Patchwork-Id: 11471731 X-Patchwork-Delegate: brian.gix@gmail.com Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D5D32912 for ; Fri, 3 Apr 2020 01:27:00 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id B42A72082F for ; Fri, 3 Apr 2020 01:27:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389477AbgDCB1A (ORCPT ); Thu, 2 Apr 2020 21:27:00 -0400 Received: from mga06.intel.com ([134.134.136.31]:19526 "EHLO mga06.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389108AbgDCB1A (ORCPT ); Thu, 2 Apr 2020 21:27:00 -0400 IronPort-SDR: J0Y+f1KKjg34/oJFhA+zwo/TCdjxplgcmq1zMYTwGAz0Jqy9unEKpEWfGsERbqqGN6i9PQZphm Vbla1hAieQhA== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Apr 2020 18:26:59 -0700 IronPort-SDR: YYKmE2wIn5U1gnovMKnQkartRAEc+vS71HDQUspFrhPk1PxT2SQagGMlislzlI0eAVgPWgqrGW RN5ejrUb1x7Q== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,337,1580803200"; d="scan'208";a="396569401" Received: from ingas-nuc1.sea.intel.com ([10.255.231.70]) by orsmga004.jf.intel.com with ESMTP; 02 Apr 2020 18:26:57 -0700 From: Inga Stotland To: linux-bluetooth@vger.kernel.org Cc: brian.gix@intel.com, Inga Stotland Subject: [PATCH BlueZ v2 2/2] tools/mesh-cfgclient: Store "index" as integer in config Date: Thu, 2 Apr 2020 18:26:55 -0700 Message-Id: <20200403012655.17517-2-inga.stotland@intel.com> X-Mailer: git-send-email 2.21.1 In-Reply-To: <20200403012655.17517-1-inga.stotland@intel.com> References: <20200403012655.17517-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 Write "index", "elementIndex", "boundNetKey" as integers rather than hex strings in configuration file. --- tools/mesh/mesh-db.c | 164 +++++++++++++++---------------------------- 1 file changed, 56 insertions(+), 108 deletions(-) diff --git a/tools/mesh/mesh-db.c b/tools/mesh/mesh-db.c index 5dd381232..3bd6b0d06 100644 --- a/tools/mesh/mesh-db.c +++ b/tools/mesh/mesh-db.c @@ -152,30 +152,6 @@ static json_object *get_node_by_unicast(uint16_t unicast) return NULL; } -static json_object *get_key_object(json_object *jarray, uint16_t idx) -{ - int i, sz = json_object_array_length(jarray); - - for (i = 0; i < sz; ++i) { - json_object *jentry, *jval; - const char *str; - uint16_t jidx; - - jentry = json_object_array_get_idx(jarray, i); - if (!json_object_object_get_ex(jentry, "index", &jval)) - return NULL; - - str = json_object_get_string(jval); - if (sscanf(str, "%04hx", &jidx) != 1) - return NULL; - - if (jidx == idx) - return jentry; - } - - return NULL; -} - static bool get_int(json_object *jobj, const char *keyword, int *value) { json_object *jvalue; @@ -184,8 +160,11 @@ static bool get_int(json_object *jobj, const char *keyword, int *value) return false; *value = json_object_get_int(jvalue); - if (errno == EINVAL) + if (errno == EINVAL) { + l_error("Error: %s should contain an integer value\n", + keyword); return false; + } return true; } @@ -204,8 +183,27 @@ static bool write_int(json_object *jobj, const char *keyword, int val) return true; } +static json_object *get_key_object(json_object *jarray, uint16_t idx) +{ + int i, sz = json_object_array_length(jarray); + + for (i = 0; i < sz; ++i) { + json_object *jentry; + int jidx; + + jentry = json_object_array_get_idx(jarray, i); + if (!get_int(jentry, "index", &jidx)) + return NULL; + + if (jidx == idx) + return jentry; + } + + return NULL; +} + static bool write_uint16_hex(json_object *jobj, const char *desc, - uint16_t value) + uint16_t value) { json_object *jstring; char buf[5]; @@ -252,8 +250,8 @@ static json_object *get_node_by_uuid(json_object *jcfg, uint8_t uuid[16]) return NULL; } -static bool add_u8_8(json_object *jobj, const uint8_t value[8], - const char *desc) +static bool add_u8_8(json_object *jobj, const char *desc, + const uint8_t value[8]) { json_object *jstring; char buf[17]; @@ -267,8 +265,8 @@ static bool add_u8_8(json_object *jobj, const uint8_t value[8], return true; } -static bool add_u8_16(json_object *jobj, const uint8_t value[16], - const char *desc) +static bool add_u8_16(json_object *jobj, const char *desc, + const uint8_t value[16]) { json_object *jstring; char buf[33]; @@ -282,7 +280,7 @@ static bool add_u8_16(json_object *jobj, const uint8_t value[16], return true; } -static bool add_string(json_object *jobj, const char *str, const char *desc) +static bool add_string(json_object *jobj, const char *desc, const char *str) { json_object *jstring = json_object_new_string(str); @@ -313,22 +311,17 @@ static bool get_token(json_object *jobj, uint8_t token[8]) static uint16_t node_parse_key(json_object *jarray, int i) { - json_object *jkey, *jval; - const char *str; - uint16_t idx; + json_object *jkey; + int idx; jkey = json_object_array_get_idx(jarray, i); if (!jkey) return KEY_IDX_INVALID; - if (!json_object_object_get_ex(jkey, "index", &jval)) + if (!get_int(jkey, "index", &idx)) return KEY_IDX_INVALID; - str = json_object_get_string(jval); - if (sscanf(str, "%04hx", &idx) != 1) - return KEY_IDX_INVALID; - - return idx; + return (uint16_t)idx; } static int compare_group_addr(const void *a, const void *b, void *user_data) @@ -439,8 +432,7 @@ static void load_remotes(json_object *jcfg) static bool add_app_key(json_object *jobj, uint16_t net_idx, uint16_t app_idx) { - json_object *jval, *jkey, *jarray; - char buf[5]; + json_object *jkey, *jarray; json_object_object_get_ex(jobj, "appKeys", &jarray); if (!jarray || json_object_get_type(jarray) != json_type_array) @@ -448,20 +440,12 @@ static bool add_app_key(json_object *jobj, uint16_t net_idx, uint16_t app_idx) jkey = json_object_new_object(); - snprintf(buf, 5, "%4.4x", net_idx); - jval = json_object_new_string(buf); - if (!jval) + if (!write_int(jkey, "boundNetKey", (int)net_idx)) goto fail; - json_object_object_add(jkey, "boundNetKey", jval); - - snprintf(buf, 5, "%4.4x", app_idx); - jval = json_object_new_string(buf); - if (!jval) + if (!write_int(jkey, "index", (int)app_idx)) goto fail; - json_object_object_add(jkey, "index", jval); - json_object_array_add(jarray, jkey); return true; @@ -472,8 +456,7 @@ fail: static bool add_node_key(json_object *jobj, const char *desc, uint16_t idx) { - json_object *jval, *jkey, *jarray; - char buf[5]; + json_object *jkey, *jarray; json_object_object_get_ex(jobj, desc, &jarray); if (!jarray || json_object_get_type(jarray) != json_type_array) @@ -481,15 +464,11 @@ static bool add_node_key(json_object *jobj, const char *desc, uint16_t idx) jkey = json_object_new_object(); - snprintf(buf, 5, "%4.4x", idx); - - jval = json_object_new_string(buf); - if (!jval) { + if (!write_int(jkey, "index", (int)idx)) { json_object_put(jkey); return false; } - json_object_object_add(jkey, "index", jval); json_object_array_add(jarray, jkey); return save_config(); @@ -531,18 +510,12 @@ static void jarray_key_del(json_object *jarray, int16_t idx) int i, sz = json_object_array_length(jarray); for (i = 0; i < sz; ++i) { - json_object *jentry, *jval; - uint16_t val; - const char *str; + json_object *jentry; + int val; jentry = json_object_array_get_idx(jarray, i); - if (!json_object_object_get_ex(jentry, "index", &jval)) - continue; - - str = json_object_get_string(jval); - - if (sscanf(str, "%04hx", &val) != 1) + if (!get_int(jentry, "index", &val)) continue; if (val == idx) { @@ -609,8 +582,8 @@ bool mesh_db_node_app_key_del(uint16_t unicast, uint16_t idx) static bool load_keys(json_object *jobj) { - json_object *jarray, *jentry, *jval; - uint16_t net_idx, app_idx; + json_object *jarray, *jentry; + int net_idx, app_idx; int i, key_cnt; json_object_object_get_ex(jobj, "netKeys", &jarray); @@ -622,20 +595,14 @@ static bool load_keys(json_object *jobj) return false; for (i = 0; i < key_cnt; ++i) { - const char *str; int phase; jentry = json_object_array_get_idx(jarray, i); - if (!json_object_object_get_ex(jentry, "index", &jval)) - return false; - - str = json_object_get_string(jval); - - if (sscanf(str, "%04hx", &net_idx) != 1) + if (!get_int(jentry, "index", &net_idx)) return false; - keys_add_net_key(net_idx); + keys_add_net_key((uint16_t) net_idx); if (!get_int(jentry, "phase", &phase)) return false; @@ -652,26 +619,16 @@ static bool load_keys(json_object *jobj) return false; for (i = 0; i < key_cnt; ++i) { - const char *str; jentry = json_object_array_get_idx(jarray, i); - if (!json_object_object_get_ex(jentry, "boundNetKey", &jval)) + if (!get_int(jentry, "boundNetKey", &net_idx)) return false; - str = json_object_get_string(jval); - - if (sscanf(str, "%04hx", &net_idx) != 1) + if (!get_int(jentry, "index", &app_idx)) return false; - if (!json_object_object_get_ex(jentry, "index", &jval)) - return false; - - str = json_object_get_string(jval); - - if (sscanf(str, "%04hx", &app_idx) != 1) - return false; - keys_add_app_key(net_idx, app_idx); + keys_add_app_key((uint16_t) net_idx, (uint16_t) app_idx); } return true; @@ -679,8 +636,7 @@ static bool load_keys(json_object *jobj) bool mesh_db_net_key_add(uint16_t net_idx) { - json_object *jval, *jkey, *jarray; - char buf[5]; + json_object *jkey, *jarray; if (!cfg || !cfg->jcfg) return false; @@ -694,14 +650,9 @@ bool mesh_db_net_key_add(uint16_t net_idx) jkey = json_object_new_object(); - snprintf(buf, 5, "%4.4x", net_idx); - - jval = json_object_new_string(buf); - if (!jval) + if (!write_int(jkey, "index", net_idx)) goto fail; - json_object_object_add(jkey, "index", jval); - if (!write_int(jkey, "phase", KEY_REFRESH_PHASE_NONE)) goto fail; @@ -785,14 +736,11 @@ bool mesh_db_add_group(struct mesh_group *grp) json_object_object_add(jgroup, "name", jval); if (IS_VIRTUAL(grp->addr)) { - if (!add_u8_16(jgroup, grp->label, "address")) + if (!add_u8_16(jgroup, "address", grp->label)) goto fail; } else { - snprintf(buf, 5, "%4.4x", grp->addr); - jval = json_object_new_string(buf); - if (!jval) + if (!write_uint16_hex(jgroup, "address", grp->addr)) goto fail; - json_object_object_add(jgroup, "address", jval); } json_object_array_add(jgroups, jgroup); @@ -891,7 +839,7 @@ bool mesh_db_add_node(uint8_t uuid[16], uint8_t num_els, uint16_t unicast, if (!jnode) return false; - if (!add_u8_16(jnode, uuid, "uuid")) + if (!add_u8_16(jnode, "uuid", uuid)) goto fail; jelements = json_object_new_array(); @@ -1052,15 +1000,15 @@ bool mesh_db_create(const char *fname, const uint8_t token[8], cfg->cfg_fname = l_strdup(fname); memcpy(cfg->token, token, 8); - if (!add_u8_8(jcfg, token, "token")) + if (!add_u8_8(jcfg, "token", token)) goto fail; l_getrandom(uuid, 16); - if (!add_u8_16(jcfg, uuid, "uuid")) + if (!add_u8_16(jcfg, "uuid", uuid)) goto fail; - if (mesh_name && !add_string(jcfg, mesh_name, "name")) + if (mesh_name && !add_string(jcfg, "name", mesh_name)) goto fail; jarray = json_object_new_array();