mesh: Save newly added or updated app key to config file
authorInga Stotland <inga.stotland@intel.com>
Thu, 14 Feb 2019 03:45:26 +0000 (19:45 -0800)
committerAnupam Roy <anupam.r@samsung.com>
Tue, 17 Dec 2019 14:14:48 +0000 (19:44 +0530)
This separates mesh_db_app_key_add() into distinct functions:
mesh_db_app_key_add() and mesh_db_app_key_update() which will be called
based on whether an application key is newly added or updated.

Change-Id: I897fe6ba17c4994f55c6514fb3c9e5e4602835e3
Signed-off-by: Anupam Roy <anupam.r@samsung.com>
mesh/mesh-db.c
mesh/mesh-db.h
mesh/storage.c

index 469b24a..5a771f6 100644 (file)
@@ -500,85 +500,52 @@ bool mesh_db_write_device_key(json_object *jnode, uint8_t *key)
 }
 
 bool mesh_db_app_key_add(json_object *jobj, uint16_t net_idx, uint16_t app_idx,
-                        const uint8_t key[16], bool update)
+                                                       const uint8_t key[16])
 {
        json_object *jarray, *jentry = NULL, *jstring = NULL;
        char buf[5];
 
        json_object_object_get_ex(jobj, "appKeys", &jarray);
-       if (!jarray && update)
+       if (jarray)
                return false;
 
        if (jarray)
                jentry = get_key_object(jarray, app_idx);
 
-       /* The key entry should exist if the key is updated */
-       if (!jentry  && update)
+       /* Do not allow direct overrwrite */
+       if (jentry)
                return false;
 
-       if (jentry) {
-               uint8_t buf[16];
-               json_object *jvalue;
-               char *str;
-
-               json_object_object_get_ex(jentry, "key", &jvalue);
-               if (!jvalue)
-                       return false;
-
-               str = (char *)json_object_get_string(jvalue);
-               if (!str2hex(str, strlen(str), buf, sizeof(buf)))
-                       return false;
-
-               /* If the same key, return success */
-               if (memcmp(key, buf, 16) == 0)
-                       return true;
-
+       jentry = json_object_new_object();
+       if (!jentry)
                return false;
-       }
 
-       if (!update) {
-               jentry = json_object_new_object();
-               if (!jentry)
-                       goto fail;
+       snprintf(buf, 5, "%4.4x", app_idx);
+       jstring = json_object_new_string(buf);
+       if (!jstring)
+               goto fail;
 
-               snprintf(buf, 5, "%4.4x", app_idx);
-               jstring = json_object_new_string(buf);
-               if (!jstring)
-                       goto fail;
+       json_object_object_add(jentry, "index", jstring);
 
-               json_object_object_add(jentry, "index", jstring);
+       snprintf(buf, 5, "%4.4x", net_idx);
+       jstring = json_object_new_string(buf);
+       if (!jstring)
+               goto fail;
 
-               snprintf(buf, 5, "%4.4x", net_idx);
-               jstring = json_object_new_string(buf);
-               if (!jstring)
-                       goto fail;
+       json_object_object_add(jentry, "boundNetKey", jstring);
 
-               json_object_object_add(jentry, "boundNetKey", jstring);
+       if (!add_key_value(jentry, "key", key))
+               goto fail;
 
-               if (!add_key_value(jentry, "key", key))
+       if (!jarray) {
+               jarray = json_object_new_array();
+               if (!jarray)
                        goto fail;
-
-               if (!jarray) {
-                       jarray = json_object_new_array();
-                       if (!jarray)
-                               goto fail;
-                       json_object_object_add(jobj, "appKeys", jarray);
-               }
-
-               json_object_array_add(jarray, jentry);
-
-       } else {
-
-               if (!json_object_object_get_ex(jentry, "key", &jstring))
-                       return false;
-
-               json_object_object_add(jentry, "oldKey", jstring);
-               json_object_object_del(jentry, "key");
-
-               if (!add_key_value(jentry, "key", key))
-                       return false;
+               json_object_object_add(jobj, "appKeys", jarray);
        }
 
+       json_object_array_add(jarray, jentry);
+
        return true;
 fail:
 
@@ -588,6 +555,32 @@ fail:
        return false;
 }
 
+bool mesh_db_app_key_update(json_object *jobj, uint16_t app_idx,
+                                                       const uint8_t key[16])
+{
+       json_object *jarray, *jentry = NULL, *jstring = NULL;
+       const char *str;
+
+       json_object_object_get_ex(jobj, "appKeys", &jarray);
+       if (!jarray)
+               return false;
+
+       /* The key entry should exist if the key is updated */
+       jentry = get_key_object(jarray, app_idx);
+       if (!jentry)
+               return false;
+
+       if (!json_object_object_get_ex(jentry, "key", &jstring))
+               return false;
+
+       str = json_object_get_string(jstring);
+       jstring = json_object_new_string(str);
+       json_object_object_add(jentry, "oldKey", jstring);
+       json_object_object_del(jentry, "key");
+
+       return add_key_value(jentry, "key", key);
+}
+
 bool mesh_db_app_key_del(json_object *jobj, uint16_t net_idx, uint16_t idx)
 {
        json_object *jarray, *jarray_new;
index 513ad38..52ea055 100644 (file)
@@ -130,7 +130,9 @@ bool mesh_db_model_binding_add(json_object *jnode, uint8_t ele_idx, bool vendor,
 bool mesh_db_model_binding_del(json_object *jnode, uint8_t ele_idx, bool vendor,
                                        uint32_t mod_id, uint16_t app_idx);
 bool mesh_db_app_key_add(json_object *jnode, uint16_t net_idx, uint16_t app_idx,
-                                       const uint8_t key[16], bool update);
+                                                       const uint8_t key[16]);
+bool mesh_db_app_key_update(json_object *jobj, uint16_t app_idx,
+                                                       const uint8_t key[16]);
 bool mesh_db_app_key_del(json_object *jobj, uint16_t net_idx, uint16_t idx);
 bool mesh_db_net_key_add(json_object *jobj, uint16_t net_idx,
                                                        const uint8_t key[16]);
index 890069a..6c4ae33 100644 (file)
@@ -277,7 +277,10 @@ bool storage_app_key_add(struct mesh_net *net, uint16_t net_idx,
        if (!jnode)
                return false;
 
-       return mesh_db_app_key_add(jnode, net_idx, app_idx, key, update);
+       if (update)
+               return mesh_db_app_key_update(jnode, app_idx, key);
+
+       return mesh_db_app_key_add(jnode, net_idx, app_idx, key);
 }
 
 bool storage_app_key_del(struct mesh_net *net, uint16_t net_idx,