5 #ifndef UAVCAN_PROTOCOL_DYNAMIC_NODE_ID_SERVER_DISTRIBUTED_PERSISTENT_STATE_HPP_INCLUDED 6 #define UAVCAN_PROTOCOL_DYNAMIC_NODE_ID_SERVER_DISTRIBUTED_PERSISTENT_STATE_HPP_INCLUDED 17 namespace dynamic_node_id_server
42 , log_(storage, tracer)
58 int res = log_.
init();
61 UAVCAN_TRACE(
"dynamic_node_id_server::distributed::PersistentState",
"Log init failed: %d", res);
72 const bool log_is_empty = (log_.
getLastIndex() == 0) && (last_entry->term == 0);
86 UAVCAN_TRACE(
"dynamic_node_id_server::distributed::PersistentState",
87 "Failed to init current term: %d", res);
90 if (current_term_ != 0)
101 UAVCAN_TRACE(
"dynamic_node_id_server::distributed::PersistentState",
102 "Failed to read current term: %d", res);
109 if (current_term_ < last_entry->term)
111 UAVCAN_TRACE(
"dynamic_node_id_server::distributed::PersistentState",
112 "Persistent storage is damaged: current term is less than term of the last log entry (%u < %u)",
113 unsigned(current_term_),
unsigned(last_entry->term));
128 UAVCAN_TRACE(
"dynamic_node_id_server::distributed::PersistentState",
129 "Failed to init votedFor: %d", res);
132 if (stored_voted_for != 0)
144 UAVCAN_TRACE(
"dynamic_node_id_server::distributed::PersistentState",
145 "Failed to read votedFor: %d", res);
150 return -ErrInvalidConfiguration;
173 if (term < current_term_)
176 return -ErrInvalidParam;
195 current_term_ = term;
207 return -ErrInvalidParam;
221 if (node_id.
get() != tmp)
226 voted_for_ = node_id;
237 #endif // Include guard
bool isVotedForSet() const
PersistentState(IStorageBackend &storage, IEventTracer &tracer)
virtual void onEvent(TraceCode event_code, int64_t event_argument)=0
#define UAVCAN_TRACE(...)
TraceRaftCurrentTermRestored
const Entry * getEntryAtIndex(Index index) const
IStorageBackend & storage_
const Log & getLog() const
NodeID getVotedFor() const
int get(const IStorageBackend::String &key, uint32_t &out_value) const
int setAndGetBack(const IStorageBackend::String &key, uint32_t &inout_value)
TraceRaftVotedForRestored
static IStorageBackend::String getCurrentTermKey()
int setCurrentTerm(Term term)
int setVotedFor(NodeID node_id)
TraceRaftCurrentTermUpdate
Term getCurrentTerm() const
Index getLastIndex() const
StorageType< Entry::FieldTypes::term >::Type Term
static IStorageBackend::String getVotedForKey()
virtual String get(const String &key) const =0
static const uavcan::int16_t ErrLogic
Internal logic error.