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. --- mesh/mesh-db.c | 107 +++++++++++++++++++++++-------------------------- mesh/mesh-db.h | 4 +- mesh/storage.c | 5 ++- 3 files changed, 57 insertions(+), 59 deletions(-) diff --git a/mesh/mesh-db.c b/mesh/mesh-db.c index 6486f7cff..2d518c1aa 100644 --- a/mesh/mesh-db.c +++ b/mesh/mesh-db.c @@ -501,85 +501,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: @@ -589,6 +556,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; diff --git a/mesh/mesh-db.h b/mesh/mesh-db.h index 513ad3861..52ea05528 100644 --- a/mesh/mesh-db.h +++ b/mesh/mesh-db.h @@ -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]); diff --git a/mesh/storage.c b/mesh/storage.c index d6b566a80..0b0582374 100644 --- a/mesh/storage.c +++ b/mesh/storage.c @@ -278,7 +278,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, -- 2.17.2