summaryrefslogtreecommitdiff
path: root/mesh/mesh-config-json.c
diff options
context:
space:
mode:
authorInga Stotland <inga.stotland@intel.com>2019-07-14 16:23:16 -0700
committerBrian Gix <brian.gix@intel.com>2019-07-15 14:48:04 -0700
commitc4bf0626fb621559ecaea7e1eadf22d386b7ee63 (patch)
tree2d9499be4b18a0454860e9b90d47b2c59eb0d7a0 /mesh/mesh-config-json.c
parentd8b2bef41b36971f567885cdefbf50d3269a9631 (diff)
downloadbluez-c4bf0626fb621559ecaea7e1eadf22d386b7ee63.tar.gz
mesh: Replace storage_save_config with mesh_config_save_config
This moves writing out of node configuration from storage.c down to mesh-config-<format-specific>.c to allow for more generic storage layout. New generalized API in mesh-config.h: mesh_config_save_config(struct mesh_config *cfg, bool no_wait, mesh_config_status_func_t cb, void *user_data) replaces the old one in storage.h: storage_save_config(struct mesh_node *node, bool no_wait, mesh_status_func_t cb, void *user_data) Currently, only JSON format is supported for storing node configuration: mesh_config_save_config is implemented in mesh-config-json.c
Diffstat (limited to 'mesh/mesh-config-json.c')
-rw-r--r--mesh/mesh-config-json.c70
1 files changed, 63 insertions, 7 deletions
diff --git a/mesh/mesh-config-json.c b/mesh/mesh-config-json.c
index 4a9cf7c6e..249946eef 100644
--- a/mesh/mesh-config-json.c
+++ b/mesh/mesh-config-json.c
@@ -46,6 +46,15 @@ struct mesh_config {
uint8_t uuid[16];
};
+struct write_info {
+ struct mesh_config *cfg;
+ void *user_data;
+ mesh_config_status_func_t cb;
+};
+
+static const char *bak_ext = ".bak";
+static const char *tmp_ext = ".tmp";
+
static bool get_int(json_object *jobj, const char *keyword, int *value)
{
json_object *jvalue;
@@ -1926,7 +1935,7 @@ bool mesh_config_model_sub_del_all(struct mesh_config *cfg, uint16_t addr,
}
bool mesh_config_load_node(const char *cfg_path, const uint8_t uuid[16],
- mesh_config_node_cb cb, void *user_data)
+ mesh_config_node_func_t cb, void *user_data)
{
int fd;
char *str;
@@ -2011,23 +2020,19 @@ void mesh_config_release(struct mesh_config *cfg)
l_free(cfg);
}
-bool mesh_config_save_config(struct mesh_config *cfg, const char *fname)
+static bool save_config(json_object *jnode, const char *fname)
{
FILE *outfile;
const char *str;
bool result = false;
- if (!cfg)
- return false;
-
outfile = fopen(fname, "w");
if (!outfile) {
l_error("Failed to save configuration to %s", fname);
return false;
}
- str = json_object_to_json_string_ext(cfg->jnode,
- JSON_C_TO_STRING_PRETTY);
+ str = json_object_to_json_string_ext(jnode, JSON_C_TO_STRING_PRETTY);
if (fwrite(str, sizeof(char), strlen(str), outfile) < strlen(str))
l_warn("Incomplete write of mesh configuration");
@@ -2038,3 +2043,54 @@ bool mesh_config_save_config(struct mesh_config *cfg, const char *fname)
return result;
}
+
+static void idle_save_config(void *user_data)
+{
+ struct write_info *info = user_data;
+ char *fname_tmp, *fname_bak, *fname_cfg;
+ bool result = false;
+
+ fname_cfg = info->cfg->node_path;
+ 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(info->cfg->jnode, 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);
+
+ if (info->cb)
+ info->cb(info->user_data, result);
+
+ l_free(info);
+}
+
+bool mesh_config_save_config(struct mesh_config *cfg, bool no_wait,
+ mesh_config_status_func_t cb, void *user_data)
+{
+ struct write_info *info;
+
+ if (!cfg)
+ return false;
+
+ info = l_new(struct write_info, 1);
+ info->cfg = cfg;
+ info->cb = cb;
+ info->user_data = user_data;
+
+ if (no_wait)
+ idle_save_config(info);
+ else
+ l_idle_oneshot(idle_save_config, info, NULL);
+
+ return true;
+}