diff --git a/mesh/mesh-config-json.c b/mesh/mesh-config-json.c
index d49f522..036ec90 100644
--- a/mesh/mesh-config-json.c
+++ b/mesh/mesh-config-json.c
#include <string.h>
#include <unistd.h>
+#include <sys/time.h>
+
#include <ell/ell.h>
#include <json-c/json.h>
#include "mesh/util.h"
#include "mesh/mesh-config.h"
+/* To prevent local node JSON cache thrashing, minimum update times */
+#define MIN_SEQ_CACHE_TRIGGER 32
+#define MIN_SEQ_CACHE_VALUE (2 * 32)
+#define MIN_SEQ_CACHE_TIME (5 * 60)
+
#define CHECK_KEY_IDX_RANGE(x) (((x) >= 0) && ((x) <= 4095))
struct mesh_config {
json_object *jnode;
char *node_dir_path;
uint8_t uuid[16];
+ uint32_t write_seq;
+ struct timeval write_time;
};
struct write_info {
cfg->jnode = jnode;
memcpy(cfg->uuid, uuid, 16);
cfg->node_dir_path = l_strdup(cfg_path);
+ cfg->write_seq = node->seq_number;
+ gettimeofday(&cfg->write_time, NULL);
return cfg;
}
return save_config(cfg->jnode, cfg->node_dir_path);
}
-bool mesh_config_write_seq_number(struct mesh_config *cfg, uint32_t seq)
+bool mesh_config_write_seq_number(struct mesh_config *cfg, uint32_t seq,
+ bool cache)
{
- if (!cfg || !write_int(cfg->jnode, "sequenceNumber", seq))
+ int value;
+ uint32_t cached = 0;
+
+ if (!cfg)
return false;
- mesh_config_save(cfg, false, NULL, NULL);
+ if (!cache) {
+ if (!write_int(cfg->jnode, "sequenceNumber", seq))
+ return false;
+
+ return mesh_config_save(cfg, true, NULL, NULL);
+ }
+
+ if (get_int(cfg->jnode, "sequenceNumber", &value))
+ cached = (uint32_t)value;
+
+ /*
+ * When sequence number approaches value stored on disk, calculate
+ * average time between sequence number updates, then overcommit the
+ * sequence number by MIN_SEQ_CACHE_TIME seconds worth of traffic or
+ * MIN_SEQ_CACHE_VALUE (whichever is greater) to avoid frequent writes
+ * to disk and to protect against crashes.
+ *
+ * The real value will be saved when daemon shuts down properly.
+ */
+ if (seq + MIN_SEQ_CACHE_TRIGGER >= cached) {
+ struct timeval now;
+ struct timeval elapsed;
+ uint64_t elapsed_ms;
+
+ gettimeofday(&now, NULL);
+ timersub(&now, &cfg->write_time, &elapsed);
+ elapsed_ms = elapsed.tv_sec * 1000 + elapsed.tv_usec / 1000;
+
+ cached = seq + (seq - cfg->write_seq) *
+ 1000 * MIN_SEQ_CACHE_TIME / elapsed_ms;
+
+ if (cached < seq + MIN_SEQ_CACHE_VALUE)
+ cached = seq + MIN_SEQ_CACHE_VALUE;
+
+ l_debug("Seq Cache: %d -> %d", seq, cached);
+
+ cfg->write_seq = seq;
+
+ if (!write_int(cfg->jnode, "sequenceNumber", cached))
+ return false;
+
+ return mesh_config_save(cfg, false, NULL, NULL);
+ }
+
return true;
}
cfg->jnode = jnode;
memcpy(cfg->uuid, uuid, 16);
cfg->node_dir_path = l_strdup(fname);
+ cfg->write_seq = node.seq_number;
+ gettimeofday(&cfg->write_time, NULL);
+
result = cb(&node, uuid, cfg, user_data);
if (!result) {
l_free(fname_tmp);
l_free(fname_bak);
+ gettimeofday(&info->cfg->write_time, NULL);
+
if (info->cb)
info->cb(info->user_data, result);
l_free(info);
+
}
bool mesh_config_save(struct mesh_config *cfg, bool no_wait,
diff --git a/mesh/mesh-config.h b/mesh/mesh-config.h
index 44e3b3a..4172e79 100644
--- a/mesh/mesh-config.h
+++ b/mesh/mesh-config.h
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_seq_number(struct mesh_config *cfg, uint32_t seq);
+bool mesh_config_write_seq_number(struct mesh_config *cfg, uint32_t seq,
+ bool cache);
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);
diff --git a/mesh/net.c b/mesh/net.c
index 7c92cfd..7c4049e 100644
--- a/mesh/net.c
+++ b/mesh/net.c
uint32_t instant; /* Controller Instant of recent Rx */
uint32_t iv_index;
uint32_t seq_num;
- uint32_t cached_seq_num;
uint16_t src_addr;
uint16_t last_addr;
uint16_t friend_addr;
uint32_t mesh_net_next_seq_num(struct mesh_net *net)
{
- uint32_t seq = net->seq_num;
-
- net->seq_num++;
-
- /* Periodically store advanced sequence number */
- if (net->seq_num + MIN_SEQ_TRIGGER >= net->cached_seq_num) {
- net->cached_seq_num = net->seq_num +
- node_seq_cache(net->node);
- node_set_sequence_number(net->node, net->cached_seq_num);
- }
-
+ uint32_t seq = net->seq_num++;
+ node_set_sequence_number(net->node, net->seq_num);
return seq;
}
l_free(net);
}
-bool mesh_net_set_seq_num(struct mesh_net *net, uint32_t number)
+bool mesh_net_set_seq_num(struct mesh_net *net, uint32_t seq)
{
if (!net)
return false;
- net->cached_seq_num = net->seq_num = number;
+ net->seq_num = seq;
return true;
}
diff --git a/mesh/node.c b/mesh/node.c
index bff73cf..e7e58d9 100644
--- a/mesh/node.c
+++ b/mesh/node.c
#include <dirent.h>
#include <stdio.h>
-#include <sys/time.h>
-
#include <ell/ell.h>
#include "mesh/mesh-defs.h"
struct mesh_config *cfg;
char *storage_dir;
uint32_t disc_watch;
- time_t upd_sec;
uint32_t seq_number;
- uint32_t seq_min_cache;
bool provisioner;
uint16_t primary;
struct node_composition *comp;
struct mesh_node *node = data;
struct mesh_net *net = node->net;
- /* Save local node configuration */
- if (node->cfg) {
-
- /* Preserve the last sequence number */
+ /* Preserve the last sequence number */
+ if (node->cfg)
mesh_config_write_seq_number(node->cfg,
- mesh_net_get_seq_num(net));
-
- mesh_config_save(node->cfg, true, NULL, NULL);
- }
+ mesh_net_get_seq_num(net),
+ false);
free_node_resources(node);
}
bool node_set_sequence_number(struct mesh_node *node, uint32_t seq)
{
- struct timeval write_time;
-
if (!node)
return false;
node->seq_number = seq;
- /*
- * Holistically determine worst case 5 minute sequence consumption
- * so that we typically (once we reach a steady state) rewrite the
- * local node file with a new seq cache value no more than once every
- * five minutes (or more)
- */
- gettimeofday(&write_time, NULL);
- if (node->upd_sec) {
- uint32_t elapsed = write_time.tv_sec - node->upd_sec;
-
- if (elapsed < MIN_SEQ_CACHE_TIME) {
- uint32_t ideal = node->seq_min_cache;
-
- l_debug("Old Seq Cache: %d", node->seq_min_cache);
-
- ideal *= (MIN_SEQ_CACHE_TIME / elapsed);
-
- if (ideal > node->seq_min_cache + MIN_SEQ_CACHE)
- node->seq_min_cache = ideal;
- else
- node->seq_min_cache += MIN_SEQ_CACHE;
-
- l_debug("New Seq Cache: %d", node->seq_min_cache);
- }
- }
-
- node->upd_sec = write_time.tv_sec;
-
- return mesh_config_write_seq_number(node->cfg, seq);
+ return mesh_config_write_seq_number(node->cfg, node->seq_number, true);
}
uint32_t node_get_sequence_number(struct mesh_node *node)
return node->seq_number;
}
-uint32_t node_seq_cache(struct mesh_node *node)
-{
- if (node->seq_min_cache < MIN_SEQ_CACHE)
- node->seq_min_cache = MIN_SEQ_CACHE;
-
- return node->seq_min_cache;
-}
-
int node_get_element_idx(struct mesh_node *node, uint16_t ele_addr)
{
uint16_t addr;
diff --git a/mesh/node.h b/mesh/node.h
index a4cac02..be57d5e 100644
--- a/mesh/node.h
+++ b/mesh/node.h
struct mesh_config;
struct mesh_config_node;
-/* To prevent local node JSON cache thrashing, minimum update times */
-#define MIN_SEQ_TRIGGER 32
-#define MIN_SEQ_CACHE (2*MIN_SEQ_TRIGGER)
-#define MIN_SEQ_CACHE_TIME (5*60)
-
typedef void (*node_ready_func_t) (void *user_data, int status,
struct mesh_node *node);
uint8_t node_beacon_mode_get(struct mesh_node *node);
bool node_friend_mode_set(struct mesh_node *node, bool enable);
uint8_t node_friend_mode_get(struct mesh_node *node);
-uint32_t node_seq_cache(struct mesh_node *node);
const char *node_get_element_path(struct mesh_node *node, uint8_t ele_idx);
const char *node_get_owner(struct mesh_node *node);
const char *node_get_app_path(struct mesh_node *node);