From 4acce826451f95c8432046323cd1bfc98528056e Mon Sep 17 00:00:00 2001 From: Inga Stotland Date: Thu, 2 Apr 2020 18:26:54 -0700 Subject: [PATCH] tools/mescfg-client: Use local routines for config write This removes dependencies on internal structures of mesh/mesh-config-json.c. Change-Id: Ibd8c2f2bf132d70faadd4591ff8fec755ab20096 Signed-off-by: Abhay Agarwal --- 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 2681155..f717c50 100755 --- a/Makefile.tools +++ b/Makefile.tools @@ -308,7 +308,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 src/libshared-glib.la \ diff --git a/tools/mesh/mesh-db.c b/tools/mesh/mesh-db.c index 3b2fbc7..d936f1f 100644 --- a/tools/mesh/mesh-db.c +++ b/tools/mesh/mesh-db.c @@ -52,11 +52,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) { @@ -432,7 +494,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) @@ -463,7 +525,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) @@ -502,7 +564,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) @@ -647,7 +709,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); @@ -683,7 +745,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) @@ -694,7 +756,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) @@ -737,7 +799,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); @@ -875,7 +937,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; @@ -921,7 +983,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]) @@ -968,7 +1030,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], @@ -1021,14 +1083,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; } @@ -1087,7 +1148,7 @@ bool mesh_db_load(const char *fname) return true; fail: - mesh_config_release((struct mesh_config *)cfg); - cfg = NULL; + release_config(); + return false; } -- 2.7.4