From 16c8f3660b5d34710adda691a647e8d87c30af53 Mon Sep 17 00:00:00 2001 From: Inga Stotland Date: Fri, 8 May 2020 17:00:22 -0700 Subject: [PATCH] mesh: Avoid saving duplicate fields in node config This modifies miscellaneous utility functions in mesh-config-json.c: when writing a new value to a node configuration file, delete the existing field containing an old value first. Change-Id: Iaed77e58eb33bbb3193d366ee909c75306ab40fc Signed-off-by: Abhay Agarwal --- mesh/mesh-config-json.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/mesh/mesh-config-json.c b/mesh/mesh-config-json.c index 29c532e..bb6866c 100644 --- a/mesh/mesh-config-json.c +++ b/mesh/mesh-config-json.c @@ -106,7 +106,7 @@ static bool get_int(json_object *jobj, const char *keyword, int *value) return true; } -static bool add_u64_value(json_object *jobject, const char *desc, +static bool add_u64_value(json_object *jobj, const char *desc, const uint8_t u64[8]) { json_object *jstring; @@ -117,11 +117,12 @@ static bool add_u64_value(json_object *jobject, const char *desc, if (!jstring) return false; - json_object_object_add(jobject, desc, jstring); + json_object_object_del(jobj, desc); + json_object_object_add(jobj, desc, jstring); return true; } -static bool add_key_value(json_object *jobject, const char *desc, +static bool add_key_value(json_object *jobj, const char *desc, const uint8_t key[16]) { json_object *jstring; @@ -132,7 +133,8 @@ static bool add_key_value(json_object *jobject, const char *desc, if (!jstring) return false; - json_object_object_add(jobject, desc, jstring); + json_object_object_del(jobj, desc); + json_object_object_add(jobj, desc, jstring); return true; } @@ -1399,6 +1401,7 @@ static bool write_uint16_hex(json_object *jobj, const char *desc, if (!jstring) return false; + json_object_object_del(jobj, desc); json_object_object_add(jobj, desc, jstring); return true; } @@ -1413,21 +1416,21 @@ static bool write_uint32_hex(json_object *jobj, const char *desc, uint32_t val) if (!jstring) return false; + json_object_object_del(jobj, desc); json_object_object_add(jobj, desc, jstring); return true; } -static bool write_int(json_object *jobj, const char *keyword, int val) +static bool write_int(json_object *jobj, const char *desc, int val) { json_object *jvalue; - json_object_object_del(jobj, keyword); - jvalue = json_object_new_int(val); if (!jvalue) return false; - json_object_object_add(jobj, keyword, jvalue); + json_object_object_del(jobj, desc); + json_object_object_add(jobj, desc, jvalue); return true; } @@ -1443,7 +1446,7 @@ static const char *mode_to_string(int mode) } } -static bool write_mode(json_object *jobj, const char *keyword, int value) +static bool write_mode(json_object *jobj, const char *desc, int value) { json_object *jstring; @@ -1452,7 +1455,8 @@ static bool write_mode(json_object *jobj, const char *keyword, int value) if (!jstring) return false; - json_object_object_add(jobj, keyword, jstring); + json_object_object_del(jobj, desc); + json_object_object_add(jobj, desc, jstring); return true; } -- 2.7.4