mesh: Use mesh_config APIs to store node configuration
authorInga Stotland <inga.stotland@intel.com>
Sun, 14 Jul 2019 23:23:17 +0000 (16:23 -0700)
committerAnupam Roy <anupam.r@samsung.com>
Tue, 17 Dec 2019 16:19:11 +0000 (21:49 +0530)
This eliminates storage_set...() routines as an intermediate
layer between mesh_config layer and the rest of the daemon when
storing updated node configuration values.
For the JSON based implementation, each call to mesh_config_write...()
routines results in writing to the node configuration file.

Change-Id: Ia42ed3d2f5a49efa2665fea15384ea06389dc49d
Signed-off-by: Anupam Roy <anupam.r@samsung.com>
mesh/appkey.c
mesh/cfgmod-server.c
mesh/mesh-config-json.c
mesh/mesh-config.h
mesh/model.c
mesh/net.c
mesh/node.c
mesh/storage.c
mesh/storage.h

index b62c7b8..67a01f9 100644 (file)
@@ -30,7 +30,7 @@
 #include "mesh/crypto.h"
 #include "mesh/util.h"
 #include "mesh/model.h"
-#include "mesh/storage.h"
+#include "mesh/mesh-config.h"
 #include "mesh/appkey.h"
 
 struct mesh_app_key {
@@ -367,6 +367,7 @@ int appkey_key_update(struct mesh_net *net, uint16_t net_idx, uint16_t app_idx,
        struct mesh_app_key *key;
        struct l_queue *app_keys;
        uint8_t phase = KEY_REFRESH_PHASE_NONE;
+       struct mesh_node *node;
 
        app_keys = mesh_net_get_app_keys(net);
        if (!app_keys)
@@ -394,7 +395,10 @@ int appkey_key_update(struct mesh_net *net, uint16_t net_idx, uint16_t app_idx,
        if (!set_key(key, app_idx, new_key, true))
                return MESH_STATUS_INSUFF_RESOURCES;
 
-       if (!storage_app_key_add(net, net_idx, app_idx, new_key, true))
+       node = mesh_net_node_get(net);
+
+       if (!mesh_config_app_key_update(node_config_get(node), app_idx,
+                                                               new_key))
                return MESH_STATUS_STORAGE_FAIL;
 
        return MESH_STATUS_SUCCESS;
@@ -405,6 +409,7 @@ int appkey_key_add(struct mesh_net *net, uint16_t net_idx, uint16_t app_idx,
 {
        struct mesh_app_key *key;
        struct l_queue *app_keys;
+       struct mesh_node *node;
 
        app_keys = mesh_net_get_app_keys(net);
        if (!app_keys)
@@ -431,7 +436,10 @@ int appkey_key_add(struct mesh_net *net, uint16_t net_idx, uint16_t app_idx,
                return MESH_STATUS_INSUFF_RESOURCES;
        }
 
-       if (!storage_app_key_add(net, net_idx, app_idx, new_key, false)) {
+       node = mesh_net_node_get(net);
+
+       if (!mesh_config_app_key_add(node_config_get(node), net_idx, app_idx,
+                                                               new_key)) {
                appkey_key_free(key);
                return MESH_STATUS_STORAGE_FAIL;
        }
@@ -450,6 +458,7 @@ int appkey_key_delete(struct mesh_net *net, uint16_t net_idx,
 {
        struct mesh_app_key *key;
        struct l_queue *app_keys;
+       struct mesh_node *node;
 
        app_keys = mesh_net_get_app_keys(net);
        if (!app_keys)
@@ -468,7 +477,9 @@ int appkey_key_delete(struct mesh_net *net, uint16_t net_idx,
        l_queue_remove(app_keys, key);
        appkey_key_free(key);
 
-       if (!storage_app_key_del(net, net_idx, app_idx))
+       node = mesh_net_node_get(net);
+
+       if (!mesh_config_app_key_del(node_config_get(node), net_idx, app_idx))
                return MESH_STATUS_STORAGE_FAIL;
 
        return MESH_STATUS_SUCCESS;
index c73e63b..033ab41 100644 (file)
@@ -876,7 +876,9 @@ static bool cfg_srv_pkt(uint16_t src, uint32_t dst,
 
                count = (pkt[0] >> 5) + 1;
                interval = ((pkt[0] & 0x1f) + 1) * 10;
-               if (storage_set_transmit_params(node, count, interval))
+
+               if (mesh_config_write_net_transmit(node_config_get(node), count,
+                                                               interval))
                        mesh_net_transmit_params_set(net, count, interval);
                /* Fall Through */
 
index 32250d8..a6acda6 100644 (file)
@@ -57,6 +57,30 @@ struct write_info {
 static const char *bak_ext = ".bak";
 static const char *tmp_ext = ".tmp";
 
+static bool save_config(json_object *jnode, 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", fname);
+               return false;
+       }
+
+       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");
+       else
+               result = true;
+
+       fclose(outfile);
+
+       return result;
+}
+
 static bool get_int(json_object *jobj, const char *keyword, int *value)
 {
        json_object *jvalue;
@@ -513,7 +537,8 @@ bool mesh_config_net_key_add(struct mesh_config *cfg, uint16_t idx,
 
        json_object_array_add(jarray, jentry);
 
-       return true;
+       return save_config(jnode, cfg->node_path);
+
 fail:
        if (jentry)
                json_object_put(jentry);
@@ -554,7 +579,7 @@ bool mesh_config_net_key_update(struct mesh_config *cfg, uint16_t idx,
        json_object_object_add(jentry, "keyRefresh",
                                json_object_new_int(KEY_REFRESH_PHASE_ONE));
 
-       return true;
+       return save_config(jnode, cfg->node_path);
 }
 
 bool mesh_config_net_key_del(struct mesh_config *cfg, uint16_t idx)
@@ -566,6 +591,7 @@ bool mesh_config_net_key_del(struct mesh_config *cfg, uint16_t idx)
 
        jnode = cfg->jnode;
 
+       /* TODO: Decide if we treat this as an error: no network keys??? */
        if (!json_object_object_get_ex(jnode, "netKeys", &jarray))
                return true;
 
@@ -575,7 +601,9 @@ bool mesh_config_net_key_del(struct mesh_config *cfg, uint16_t idx)
 
        if (json_object_array_length(jarray) == 1) {
                json_object_object_del(jnode, "netKeys");
-               return true;
+               /* TODO: Do we raise an error here? */
+               l_warn("Removing the last network key! Zero keys left.");
+               return save_config(jnode, cfg->node_path);
        }
 
        /*
@@ -590,23 +618,23 @@ bool mesh_config_net_key_del(struct mesh_config *cfg, uint16_t idx)
        json_object_object_del(jnode, "netKeys");
        json_object_object_add(jnode, "netKeys", jarray_new);
 
-       return true;
+       return save_config(jnode, cfg->node_path);
 }
 
 bool mesh_config_write_device_key(struct mesh_config *cfg, uint8_t *key)
 {
-       if (!cfg)
+       if (!cfg || !add_key_value(cfg->jnode, "deviceKey", key))
                return false;
 
-       return add_key_value(cfg->jnode, "deviceKey", key);
+       return save_config(cfg->jnode, cfg->node_path);
 }
 
 bool mesh_config_write_token(struct mesh_config *cfg, uint8_t *token)
 {
-       if (!cfg)
+       if (!cfg || !add_u64_value(cfg->jnode, "token", token))
                return false;
 
-       return add_u64_value(cfg->jnode, "token", token);
+       return save_config(cfg->jnode, cfg->node_path);
 }
 
 bool mesh_config_app_key_add(struct mesh_config *cfg, uint16_t net_idx,
@@ -658,7 +686,8 @@ bool mesh_config_app_key_add(struct mesh_config *cfg, uint16_t net_idx,
 
        json_object_array_add(jarray, jentry);
 
-       return true;
+       return save_config(jnode, cfg->node_path);
+
 fail:
 
        if (jentry)
@@ -692,9 +721,14 @@ bool mesh_config_app_key_update(struct mesh_config *cfg, uint16_t app_idx,
        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);
+       /* TODO: "Rewind" if add_key_value fails */
+       if (!add_key_value(jentry, "key", key))
+               return false;
+
+       return save_config(jnode, cfg->node_path);
 }
 
 bool mesh_config_app_key_del(struct mesh_config *cfg, uint16_t net_idx,
@@ -731,7 +765,7 @@ bool mesh_config_app_key_del(struct mesh_config *cfg, uint16_t net_idx,
        json_object_object_del(jnode, "appKeys");
        json_object_object_add(jnode, "appKeys", jarray_new);
 
-       return true;
+       return save_config(jnode, cfg->node_path);
 }
 
 bool mesh_config_model_binding_add(struct mesh_config *cfg, uint8_t ele_idx,
@@ -771,7 +805,7 @@ bool mesh_config_model_binding_add(struct mesh_config *cfg, uint8_t ele_idx,
 
        json_object_array_add(jarray, jstring);
 
-       return true;
+       return save_config(jnode, cfg->node_path);
 }
 
 bool mesh_config_model_binding_del(struct mesh_config *cfg, uint8_t ele_idx,
@@ -815,7 +849,7 @@ bool mesh_config_model_binding_del(struct mesh_config *cfg, uint8_t ele_idx,
        json_object_object_del(jmodel, "bind");
        json_object_object_add(jmodel, "bind", jarray_new);
 
-       return true;
+       return save_config(jnode, cfg->node_path);
 }
 
 static void free_model(void *data)
@@ -1347,22 +1381,12 @@ static bool write_uint16_hex(json_object *jobj, const char *desc,
        return true;
 }
 
-bool mesh_config_write_uint16_hex(struct mesh_config *cfg, const char *desc,
-                                                               uint16_t value)
-{
-       if (!cfg)
-               return false;
-
-       return write_uint16_hex(cfg->jnode, desc, value);
-}
-
-static bool write_uint32_hex(json_object *jobj, const char *desc,
-                                                               uint32_t value)
+static bool write_uint32_hex(json_object *jobj, const char *desc, uint32_t val)
 {
        json_object *jstring;
        char buf[9];
 
-       snprintf(buf, 9, "%8.8x", value);
+       snprintf(buf, 9, "%8.8x", val);
        jstring = json_object_new_string(buf);
        if (!jstring)
                return false;
@@ -1371,22 +1395,13 @@ static bool write_uint32_hex(json_object *jobj, const char *desc,
        return true;
 }
 
-bool mesh_config_write_uint32_hex(struct mesh_config *cfg, const char *desc,
-                                                               uint32_t value)
-{
-       if (!cfg)
-               return false;
-
-       return write_uint32_hex(cfg->jnode, desc, value);
-}
-
-static bool write_int(json_object *jobj, const char *keyword, int value)
+static bool write_int(json_object *jobj, const char *keyword, int val)
 {
        json_object *jvalue;
 
        json_object_object_del(jobj, keyword);
 
-       jvalue = json_object_new_int(value);
+       jvalue = json_object_new_int(val);
        if (!jvalue)
                return false;
 
@@ -1394,35 +1409,6 @@ static bool write_int(json_object *jobj, const char *keyword, int value)
        return true;
 }
 
-bool mesh_config_write_int(struct mesh_config *cfg, const char *keyword,
-                                                               int value)
-{
-       if (!cfg)
-               return false;
-
-       return write_int(cfg->jnode, keyword, value);
-}
-
-bool mesh_config_write_bool(struct mesh_config *cfg, const char *keyword,
-                                                               bool value)
-{
-       json_object *jnode, *jvalue;
-
-       if (!cfg)
-               return false;
-
-       jnode = cfg->jnode;
-
-       json_object_object_del(jnode, keyword);
-
-       jvalue = json_object_new_boolean(value);
-       if (!jvalue)
-               return false;
-
-       json_object_object_add(jnode, keyword, jvalue);
-       return true;
-}
-
 static const char *mode_to_string(int mode)
 {
        switch (mode) {
@@ -1452,10 +1438,10 @@ static bool write_mode(json_object *jobj, const char *keyword, int value)
 bool mesh_config_write_mode(struct mesh_config *cfg, const char *keyword,
                                                                int value)
 {
-       if (!cfg)
+       if (!cfg || !write_mode(cfg->jnode, keyword, value))
                return false;
 
-       return write_mode(cfg->jnode, keyword, value);
+       return save_config(cfg->jnode, cfg->node_path);
 }
 
 static bool write_relay_mode(json_object *jobj, uint8_t mode,
@@ -1486,14 +1472,22 @@ fail:
        return false;
 }
 
+bool mesh_config_write_unicast(struct mesh_config *cfg, uint16_t unicast)
+{
+       if (!cfg || !write_uint16_hex(cfg->jnode, "unicastAddress", unicast))
+               return false;
+
+       return save_config(cfg->jnode, cfg->node_path);
+}
+
 bool mesh_config_write_relay_mode(struct mesh_config *cfg, uint8_t mode,
                                        uint8_t count, uint16_t interval)
 {
 
-       if (!cfg)
+       if (!cfg || !write_relay_mode(cfg->jnode, mode, count, interval))
                return false;
 
-       return write_relay_mode(cfg->jnode, mode, count, interval);
+       return save_config(cfg->jnode, cfg->node_path);
 }
 
 bool mesh_config_write_net_transmit(struct mesh_config *cfg, uint8_t cnt,
@@ -1503,9 +1497,8 @@ bool mesh_config_write_net_transmit(struct mesh_config *cfg, uint8_t cnt,
 
        if (!cfg)
                return false;
-       jnode = cfg->jnode;
 
-       json_object_object_del(jnode, "retransmit");
+       jnode = cfg->jnode;
 
        jretransmit = json_object_new_object();
        if (jretransmit)
@@ -1517,9 +1510,11 @@ bool mesh_config_write_net_transmit(struct mesh_config *cfg, uint8_t cnt,
        if (!write_int(jretransmit, "interval", interval))
                goto fail;
 
+       json_object_object_del(jnode, "retransmit");
        json_object_object_add(jnode, "retransmit", jretransmit);
 
-       return true;
+       return save_config(cfg->jnode, cfg->node_path);
+
 fail:
        json_object_put(jretransmit);
        return false;
@@ -1543,7 +1538,7 @@ bool mesh_config_write_iv_index(struct mesh_config *cfg, uint32_t idx,
        if (!write_int(jnode, "IVupdate", tmp))
                return false;
 
-       return true;
+       return save_config(jnode, cfg->node_path);
 }
 
 static void add_model(void *a, void *b)
@@ -1721,7 +1716,7 @@ bool mesh_config_net_key_set_phase(struct mesh_config *cfg, uint16_t idx,
                finish_key_refresh(jnode, idx);
        }
 
-       return true;
+       return save_config(jnode, cfg->node_path);
 }
 
 bool mesh_config_model_pub_add(struct mesh_config *cfg, uint16_t addr,
@@ -1785,7 +1780,8 @@ bool mesh_config_model_pub_add(struct mesh_config *cfg, uint16_t addr,
        json_object_object_add(jpub, "retransmit", jretransmit);
        json_object_object_add(jmodel, "publish", jpub);
 
-       return true;
+       return save_config(jnode, cfg->node_path);
+
 fail:
        json_object_put(jpub);
        return false;
@@ -1813,11 +1809,11 @@ static bool delete_model_property(json_object *jnode, uint16_t addr,
 bool mesh_config_model_pub_del(struct mesh_config *cfg, uint16_t addr,
                                                uint32_t mod_id, bool vendor)
 {
-       if (!cfg)
+       if (!cfg || !delete_model_property(cfg->jnode, addr, mod_id, vendor,
+                                                               "publish"))
                return false;
 
-       return delete_model_property(cfg->jnode, addr, mod_id, vendor,
-                                                               "publish");
+       return save_config(cfg->jnode, cfg->node_path);
 }
 
 bool mesh_config_model_sub_add(struct mesh_config *cfg, uint16_t addr,
@@ -1868,7 +1864,7 @@ bool mesh_config_model_sub_add(struct mesh_config *cfg, uint16_t addr,
 
        json_object_array_add(jarray, jstring);
 
-       return true;
+       return save_config(jnode, cfg->node_path);
 }
 
 bool mesh_config_model_sub_del(struct mesh_config *cfg, uint16_t addr,
@@ -1923,17 +1919,34 @@ bool mesh_config_model_sub_del(struct mesh_config *cfg, uint16_t addr,
        json_object_object_del(jmodel, "subscribe");
        json_object_object_add(jmodel, "subscribe", jarray_new);
 
-       return true;
+       return save_config(jnode, cfg->node_path);
 }
 
 bool mesh_config_model_sub_del_all(struct mesh_config *cfg, uint16_t addr,
                                                uint32_t mod_id, bool vendor)
 {
-       if (!cfg)
+       if (!cfg || !delete_model_property(cfg->jnode, addr, mod_id, vendor,
+                                                               "subscribe"))
                return false;
 
-       return delete_model_property(cfg->jnode, addr, mod_id, vendor,
-                                                               "subscribe");
+       return save_config(cfg->jnode, cfg->node_path);
+}
+
+bool mesh_config_write_seq_number(struct mesh_config *cfg, uint32_t seq)
+{
+       if (!cfg || !write_int(cfg->jnode, "sequenceNumber", seq))
+               return false;
+
+       mesh_config_save_config(cfg, false, NULL, NULL);
+       return true;
+}
+
+bool mesh_config_write_ttl(struct mesh_config *cfg, uint8_t ttl)
+{
+       if (!cfg || !write_int(cfg->jnode, "defaultTTL", ttl))
+               return false;
+
+       return save_config(cfg->jnode, cfg->node_path);
 }
 
 bool mesh_config_load_node(const char *cfg_path, const uint8_t uuid[16],
@@ -2022,30 +2035,6 @@ void mesh_config_release(struct mesh_config *cfg)
        l_free(cfg);
 }
 
-static bool save_config(json_object *jnode, 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", fname);
-               return false;
-       }
-
-       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");
-       else
-               result = true;
-
-       fclose(outfile);
-
-       return result;
-}
-
 static void idle_save_config(void *user_data)
 {
        struct write_info *info = user_data;
index 5241dde..83ba33b 100644 (file)
@@ -127,16 +127,11 @@ bool mesh_config_write_network_key(struct mesh_config *cfg, uint16_t idx,
                                uint8_t *key, uint8_t *new_key, int phase);
 bool mesh_config_write_app_key(struct mesh_config *cfg, uint16_t net_idx,
                        uint16_t app_idx, uint8_t *key, uint8_t *new_key);
-bool mesh_config_write_int(struct mesh_config *cfg,
-                                               const char *keyword, int value);
-bool mesh_config_write_uint16_hex(struct mesh_config *cfg, const char *desc,
-                                                               uint16_t value);
-bool mesh_config_write_uint32_hex(struct mesh_config *cfg, const char *desc,
-                                                               uint32_t value);
-bool mesh_config_write_bool(struct mesh_config *cfg, const char *keyword,
-                                                               bool value);
+bool mesh_config_write_seq_number(struct mesh_config *cfg, uint32_t seq);
+bool mesh_config_write_unicast(struct mesh_config *cfg, uint16_t unicast);
 bool mesh_config_write_relay_mode(struct mesh_config *cfg, uint8_t mode,
                                        uint8_t count, uint16_t interval);
+bool mesh_config_write_ttl(struct mesh_config *cfg, uint8_t ttl);
 bool mesh_config_write_mode(struct mesh_config *cfg, const char *keyword,
                                                                int value);
 bool mesh_config_model_binding_add(struct mesh_config *cfg, uint8_t ele_idx,
index 0f10727..ef62a22 100644 (file)
@@ -532,7 +532,8 @@ static int update_binding(struct mesh_node *node, uint16_t addr, uint32_t id,
 {
        int status;
        struct mesh_model *mod;
-       bool is_present;
+       bool is_present, is_vendor;
+       uint8_t ele_idx;
 
        mod = find_model(node, addr, id, &status);
        if (!mod) {
@@ -540,7 +541,8 @@ static int update_binding(struct mesh_node *node, uint16_t addr, uint32_t id,
                return status;
        }
 
-       id = (id >= VENDOR_ID_MASK) ? (id & 0xffff) : id;
+       is_vendor = id < VENDOR_ID_MASK && id > 0xffff;
+       id = !is_vendor ? (id & 0xffff) : id;
 
        if (id == CONFIG_SRV_MODEL || id == CONFIG_CLI_MODEL)
                return MESH_STATUS_INVALID_MODEL;
@@ -556,10 +558,12 @@ static int update_binding(struct mesh_node *node, uint16_t addr, uint32_t id,
        if (is_present && !unbind)
                return MESH_STATUS_SUCCESS;
 
+       ele_idx = (uint8_t) node_get_element_idx(node, addr);
+
        if (unbind) {
                model_unbind_idx(node, mod, app_idx);
-
-               if (!storage_model_bind(node, addr, id, app_idx, true))
+               if (!mesh_config_model_binding_del(node_config_get(node),
+                                       ele_idx, is_vendor, id, app_idx))
                        return MESH_STATUS_STORAGE_FAIL;
 
                return MESH_STATUS_SUCCESS;
@@ -568,7 +572,8 @@ static int update_binding(struct mesh_node *node, uint16_t addr, uint32_t id,
        if (l_queue_length(mod->bindings) >= MAX_BINDINGS)
                return MESH_STATUS_INSUFF_RESOURCES;
 
-       if (!storage_model_bind(node, addr, id, app_idx, false))
+       if (!mesh_config_model_binding_add(node_config_get(node),
+                                       ele_idx, is_vendor, id, app_idx))
                return MESH_STATUS_STORAGE_FAIL;
 
        model_bind_idx(node, mod, app_idx);
index 1654152..1ccd509 100644 (file)
@@ -31,7 +31,7 @@
 #include "mesh/net.h"
 #include "mesh/mesh-io.h"
 #include "mesh/friend.h"
-#include "mesh/storage.h"
+#include "mesh/mesh-config.h"
 #include "mesh/model.h"
 #include "mesh/appkey.h"
 
@@ -969,7 +969,7 @@ int mesh_net_del_key(struct mesh_net *net, uint16_t idx)
        l_queue_remove(net->subnets, subnet);
        subnet_free(subnet);
 
-       if (!storage_net_key_del(net, idx))
+       if (!mesh_config_net_key_del(node_config_get(net->node), idx))
                return MESH_STATUS_STORAGE_FAIL;
 
        return MESH_STATUS_SUCCESS;
@@ -1022,7 +1022,7 @@ int mesh_net_add_key(struct mesh_net *net, uint16_t idx, const uint8_t *value)
        if (!subnet)
                return MESH_STATUS_INSUFF_RESOURCES;
 
-       if (!storage_net_key_add(net, idx, value, false)) {
+       if (!mesh_config_net_key_add(node_config_get(net->node), idx, value)) {
                l_queue_remove(net->subnets, subnet);
                subnet_free(subnet);
                return MESH_STATUS_STORAGE_FAIL;
@@ -2660,7 +2660,8 @@ static int key_refresh_phase_two(struct mesh_net *net, uint16_t idx)
        else
                l_queue_foreach(net->friends, frnd_kr_phase2, net);
 
-       storage_set_key_refresh_phase(net, idx, KEY_REFRESH_PHASE_TWO);
+       mesh_config_net_key_set_phase(node_config_get(net->node), idx,
+                                               KEY_REFRESH_PHASE_TWO);
 
        return MESH_STATUS_SUCCESS;
 }
@@ -2695,7 +2696,8 @@ static int key_refresh_finish(struct mesh_net *net, uint16_t idx)
        else
                l_queue_foreach(net->friends, frnd_kr_phase3, net);
 
-       storage_set_key_refresh_phase(net, idx, KEY_REFRESH_PHASE_NONE);
+       mesh_config_net_key_set_phase(node_config_get(net->node), idx,
+                                                       KEY_REFRESH_PHASE_NONE);
 
        return MESH_STATUS_SUCCESS;
 }
@@ -2747,7 +2749,8 @@ static void update_iv_kr_state(struct mesh_subnet *subnet, uint32_t iv_index,
                        net->iv_upd_state = IV_UPD_NORMAL;
                }
 
-               storage_set_iv_index(net, iv_index, net->iv_upd_state);
+               mesh_config_write_iv_index(node_config_get(net->node), iv_index,
+                                                       net->iv_upd_state);
 
                /* Figure out the key refresh phase */
                if (kr_transition) {
@@ -2769,7 +2772,8 @@ static void update_iv_kr_state(struct mesh_subnet *subnet, uint32_t iv_index,
                net->iv_upd_state = IV_UPD_UPDATING;
                net->iv_update_timeout = l_timeout_create(IV_IDX_UPD_MIN,
                                                        iv_upd_to, net, NULL);
-               storage_set_iv_index(net, iv_index, net->iv_upd_state);
+               mesh_config_write_iv_index(node_config_get(net->node), iv_index,
+                                                       net->iv_upd_state);
        } else if (iv_update && iv_index != net->iv_index) {
                l_error("Update attempted too soon (iv idx already updated)");
                return;
@@ -2782,7 +2786,8 @@ static void update_iv_kr_state(struct mesh_subnet *subnet, uint32_t iv_index,
        if (iv_index > net->iv_index) {
                l_queue_clear(net->msg_cache, mesh_msg_free);
                net->iv_index = iv_index;
-               storage_set_iv_index(net, iv_index, net->iv_upd_state);
+               mesh_config_write_iv_index(node_config_get(net->node), iv_index,
+                                                       net->iv_upd_state);
        }
 
        /* Figure out the key refresh phase */
@@ -3120,7 +3125,8 @@ bool mesh_net_iv_index_update(struct mesh_net *net)
        mesh_net_flush_msg_queues(net);
        net->iv_upd_state = IV_UPD_UPDATING;
        net->iv_index++;
-       if (!storage_set_iv_index(net, net->iv_index, IV_UPD_UPDATING))
+       if (!mesh_config_write_iv_index(node_config_get(net->node),
+                                       net->iv_index, IV_UPD_UPDATING))
                return false;
 
        l_queue_foreach(net->subnets, set_network_beacon, net);
@@ -3737,7 +3743,7 @@ int mesh_net_update_key(struct mesh_net *net, uint16_t idx,
 
        l_info("key refresh phase 1: Key ID %d", subnet->net_key_upd);
 
-       if (!storage_net_key_add(net, idx, value, true))
+       if (!mesh_config_net_key_update(node_config_get(net->node), idx, value))
                return MESH_STATUS_STORAGE_FAIL;
 
        subnet->kr_phase = KEY_REFRESH_PHASE_ONE;
index cd5ca7b..2082eb1 100644 (file)
@@ -489,7 +489,8 @@ static void cleanup_node(void *data)
        if (node->cfg) {
 
                /* Preserve the last sequence number */
-               storage_write_sequence_number(net, mesh_net_get_seq_num(net));
+               mesh_config_write_seq_number(node->cfg,
+                                               mesh_net_get_seq_num(net));
 
                mesh_config_save_config(node->cfg, true, NULL, NULL);
        }
@@ -534,7 +535,8 @@ bool node_app_key_delete(struct mesh_net *net, uint16_t addr,
 
                mesh_model_app_key_delete(node, ele->models, app_idx);
        }
-       return true;
+
+       return mesh_config_app_key_del(node->cfg, net_idx, app_idx);
 }
 
 uint16_t node_get_primary(struct mesh_node *node)
@@ -615,7 +617,7 @@ bool node_default_ttl_set(struct mesh_node *node, uint8_t ttl)
        if (!node)
                return false;
 
-       res = storage_set_ttl(node, ttl);
+       res = mesh_config_write_ttl(node->cfg, ttl);
 
        if (res) {
                node->ttl = ttl;
@@ -662,7 +664,7 @@ bool node_set_sequence_number(struct mesh_node *node, uint32_t seq)
 
        node->upd_sec = write_time.tv_sec;
 
-       return storage_write_sequence_number(node->net, seq);
+       return mesh_config_write_seq_number(node->cfg, seq);
 }
 
 uint32_t node_get_sequence_number(struct mesh_node *node)
@@ -739,7 +741,7 @@ bool node_relay_mode_set(struct mesh_node *node, bool enable, uint8_t cnt,
        if (!node || node->relay.mode == MESH_MODE_UNSUPPORTED)
                return false;
 
-       res = storage_set_relay(node, enable, cnt, interval);
+       res = mesh_config_write_relay_mode(node->cfg, enable, cnt, interval);
 
        if (res) {
                node->relay.mode = enable ? MESH_MODE_ENABLED :
@@ -761,7 +763,7 @@ bool node_proxy_mode_set(struct mesh_node *node, bool enable)
                return false;
 
        proxy = enable ? MESH_MODE_ENABLED : MESH_MODE_DISABLED;
-       res = storage_set_mode(node, proxy, "proxy");
+       res = mesh_config_write_mode(node->cfg, "proxy", proxy);
 
        if (res) {
                node->proxy = proxy;
@@ -788,7 +790,7 @@ bool node_beacon_mode_set(struct mesh_node *node, bool enable)
                return false;
 
        beacon = enable ? MESH_MODE_ENABLED : MESH_MODE_DISABLED;
-       res = storage_set_mode(node, beacon, "beacon");
+       res = mesh_config_write_mode(node->cfg, "beacon", beacon);
 
        if (res) {
                node->beacon = beacon;
@@ -815,7 +817,7 @@ bool node_friend_mode_set(struct mesh_node *node, bool enable)
                return false;
 
        friend = enable ? MESH_MODE_ENABLED : MESH_MODE_DISABLED;
-       res = storage_set_mode(node, friend, "friend");
+       res = mesh_config_write_mode(node->cfg, "friend", friend);
 
        if (res) {
                node->friend = friend;
@@ -1468,13 +1470,12 @@ static bool add_local_node(struct mesh_node *node, uint16_t unicast, bool kr,
 
        l_queue_push_tail(nodes, node);
 
-       if (!storage_set_iv_index(node->net, iv_idx, ivu))
+       if (!mesh_config_write_iv_index(node->cfg, iv_idx, ivu))
                return false;
 
        mesh_net_set_iv_index(node->net, iv_idx, ivu);
 
-       if (!mesh_config_write_uint16_hex(node->cfg, "unicastAddress",
-                                                               unicast))
+       if (!mesh_config_write_unicast(node->cfg, unicast))
                return false;
 
        l_getrandom(node->token, sizeof(node->token));
index 14607de..60b0b46 100644 (file)
@@ -74,176 +74,6 @@ static bool parse_config(char *in_file, char *out_dir, const uint8_t uuid[16])
        return result;
 }
 
-bool storage_set_ttl(struct mesh_node *node, uint8_t ttl)
-{
-       if (!mesh_config_write_int(node_config_get(node), "defaultTTL", ttl))
-               return false;
-
-       mesh_config_save_config(node_config_get(node), true, NULL, NULL);
-       return true;
-}
-
-bool storage_set_relay(struct mesh_node *node, bool enable,
-                               uint8_t count, uint8_t interval)
-{
-       if (!mesh_config_write_relay_mode(node_config_get(node), enable, count,
-                                                               interval))
-               return false;
-
-       mesh_config_save_config(node_config_get(node), true, NULL, NULL);
-       return true;
-}
-
-bool storage_set_transmit_params(struct mesh_node *node, uint8_t count,
-                                                       uint8_t interval)
-{
-       if (!mesh_config_write_net_transmit(node_config_get(node), count,
-                                                               interval))
-               return false;
-
-       mesh_config_save_config(node_config_get(node), true, NULL, NULL);
-       return true;
-}
-
-bool storage_set_mode(struct mesh_node *node, uint8_t mode,
-                                               const char *mode_name)
-{
-       if (!mesh_config_write_mode(node_config_get(node), mode_name, mode))
-               return false;
-
-       mesh_config_save_config(node_config_get(node), true, NULL, NULL);
-       return true;
-}
-
-bool storage_model_bind(struct mesh_node *node, uint16_t addr, uint32_t mod_id,
-                               uint16_t app_idx, bool unbind)
-{
-       struct mesh_config *cfg;
-       int ele_idx;
-       bool stored, is_vendor = (mod_id > 0xffff);
-
-       ele_idx = node_get_element_idx(node, addr);
-       if (ele_idx < 0)
-               return false;
-
-       cfg = node_config_get(node);
-
-       if (unbind)
-               stored = mesh_config_model_binding_del(cfg, ele_idx, is_vendor,
-                                                       mod_id, app_idx);
-       else
-               stored = mesh_config_model_binding_add(cfg, ele_idx, is_vendor,
-                                                       mod_id, app_idx);
-
-       if (stored)
-               mesh_config_save_config(cfg, true, NULL, NULL);
-
-       return stored;
-}
-
-bool storage_app_key_add(struct mesh_net *net, uint16_t net_idx,
-                       uint16_t app_idx, const uint8_t key[16], bool update)
-{
-       struct mesh_config *cfg;
-       struct mesh_node *node = mesh_net_node_get(net);
-       bool stored;
-
-       cfg = node_config_get(node);
-
-       if (update)
-               stored = mesh_config_app_key_update(cfg, app_idx, key);
-       else
-               stored = mesh_config_app_key_add(cfg, net_idx, app_idx, key);
-
-       if (stored)
-               mesh_config_save_config(cfg, true, NULL, NULL);
-
-       return stored;
-}
-
-bool storage_app_key_del(struct mesh_net *net, uint16_t net_idx,
-                                       uint16_t app_idx)
-{
-       struct mesh_config *cfg;
-       struct mesh_node *node = mesh_net_node_get(net);
-
-       cfg = node_config_get(node);
-
-       if (!mesh_config_app_key_del(cfg, net_idx, app_idx))
-               return false;
-
-       mesh_config_save_config(cfg, true, NULL, NULL);
-       return true;
-}
-
-bool storage_net_key_add(struct mesh_net *net, uint16_t net_idx,
-                                       const uint8_t key[16], bool update)
-{
-       struct mesh_node *node = mesh_net_node_get(net);
-       struct mesh_config *cfg = node_config_get(node);
-       bool stored;
-
-       if (!update)
-               stored = mesh_config_net_key_add(cfg, net_idx, key);
-       else
-               stored = mesh_config_net_key_update(cfg, net_idx, key);
-
-       if (stored)
-               mesh_config_save_config(cfg, true, NULL, NULL);
-
-       return stored;
-}
-
-bool storage_net_key_del(struct mesh_net *net, uint16_t net_idx)
-{
-       struct mesh_node *node = mesh_net_node_get(net);
-       struct mesh_config *cfg = node_config_get(node);
-
-       if (!mesh_config_net_key_del(cfg, net_idx))
-               return false;
-
-       mesh_config_save_config(cfg, true, NULL, NULL);
-       return true;
-}
-
-bool storage_set_iv_index(struct mesh_net *net, uint32_t iv_index,
-                                                               bool update)
-{
-       struct mesh_node *node = mesh_net_node_get(net);
-       struct mesh_config *cfg = node_config_get(node);
-
-       if (!mesh_config_write_iv_index(cfg, iv_index, update))
-               return false;
-
-       mesh_config_save_config(cfg, true, NULL, NULL);
-       return true;
-}
-
-bool storage_set_key_refresh_phase(struct mesh_net *net, uint16_t net_idx,
-                                                               uint8_t phase)
-{
-       struct mesh_node *node = mesh_net_node_get(net);
-       struct mesh_config *cfg = node_config_get(node);
-
-       if (!mesh_config_net_key_set_phase(cfg, net_idx, phase))
-               return false;
-
-       mesh_config_save_config(cfg, true, NULL, NULL);
-       return true;
-}
-
-bool storage_write_sequence_number(struct mesh_net *net, uint32_t seq)
-{
-       struct mesh_node *node = mesh_net_node_get(net);
-       struct mesh_config *cfg = node_config_get(node);
-
-       if (!mesh_config_write_int(cfg, "sequenceNumber", seq))
-               return false;
-
-       mesh_config_save_config(cfg, false, NULL, NULL);
-       return true;
-}
-
 static int create_dir(const char *dir_name)
 {
        struct stat st;
index f70544a..21fd3f5 100644 (file)
@@ -25,27 +25,3 @@ bool storage_load_nodes(const char *dir);
 bool storage_create_node_config(struct mesh_node *node, const uint8_t uuid[16],
                                        struct mesh_config_node *db_node);
 void storage_remove_node_config(struct mesh_node *node);
-bool storage_model_bind(struct mesh_node *node, uint16_t addr, uint32_t id,
-                                               uint16_t app_idx, bool unbind);
-
-bool storage_set_ttl(struct mesh_node *node, uint8_t ttl);
-bool storage_set_relay(struct mesh_node *node, bool enable, uint8_t count,
-                                                       uint8_t interval);
-bool storage_set_transmit_params(struct mesh_node *node, uint8_t count,
-                                                       uint8_t interval);
-bool storage_set_mode(struct mesh_node *node, uint8_t mode,
-                                               const char *mode_name);
-bool storage_net_key_add(struct mesh_net *net, uint16_t net_idx,
-                                       const uint8_t key[16], bool update);
-bool storage_net_key_del(struct mesh_net *net, uint16_t net_idx);
-bool storage_app_key_add(struct mesh_net *net, uint16_t net_idx,
-                       uint16_t app_idx, const uint8_t key[16], bool update);
-bool storage_app_key_del(struct mesh_net *net, uint16_t net_idx,
-                                                       uint16_t app_idx);
-bool storage_write_sequence_number(struct mesh_net *net, uint32_t seq);
-bool storage_set_iv_index(struct mesh_net *net, uint32_t iv_index,
-                                                               bool update);
-bool storage_set_device_key(struct mesh_node *node, uint8_t dev_key[16]);
-bool storage_set_unicast(struct mesh_node *node, uint16_t unicast);
-bool storage_set_key_refresh_phase(struct mesh_net *net, uint16_t net_idx,
-                                                               uint8_t phase);