diff --git a/libuavcan/include/uavcan/protocol/dynamic_node_id_allocation_server.hpp b/libuavcan/include/uavcan/protocol/dynamic_node_id_allocation_server.hpp index e04b8e7043..0d9a57255b 100644 --- a/libuavcan/include/uavcan/protocol/dynamic_node_id_allocation_server.hpp +++ b/libuavcan/include/uavcan/protocol/dynamic_node_id_allocation_server.hpp @@ -76,6 +76,14 @@ public: class IDynamicNodeIDAllocationServerEventTracer { public: +#if UAVCAN_TOSTRING + /** + * It is safe to call this function with any argument. + * If the event code is out of range, an assertion failure will be triggered and an error text will be returned. + */ + static const char* getEventName(uint16_t code); +#endif + /** * The server invokes this method every time it believes that a noteworthy event has happened. * The table of event codes can be found in the server sources. @@ -658,7 +666,7 @@ public: } }; -} // namespace dynamic_node_id_impl +} // namespace dynamic_node_id_server_impl /** * diff --git a/libuavcan/src/protocol/uc_dynamic_node_id_allocation_server.cpp b/libuavcan/src/protocol/uc_dynamic_node_id_allocation_server.cpp index 98b0ef0106..5707b165e0 100644 --- a/libuavcan/src/protocol/uc_dynamic_node_id_allocation_server.cpp +++ b/libuavcan/src/protocol/uc_dynamic_node_id_allocation_server.cpp @@ -15,6 +15,51 @@ namespace uavcan { +/* + * IDynamicNodeIDAllocationServerEventTracer + */ +#if UAVCAN_TOSTRING +const char* IDynamicNodeIDAllocationServerEventTracer::getEventName(uint16_t code) +{ + using namespace dynamic_node_id_server_impl; + + // import re + // make_strings = lambda s: ',\n'.join('"%s"' % x for x in re.findall(r'\ \ \ \ Trace([A-Za-z0-9]+),', s)) + static const char* const Strings[NumTraceEventCodes] = + { + "Error", + "LogLastIndexRestored", + "LogAppend", + "LogRemove", + "CurrentTermRestored", + "CurrentTermUpdate", + "VotedForRestored", + "VotedForUpdate", + "DiscoveryBroadcast", + "NewServerDiscovered", + "DiscoveryReceived", + "ClusterSizeInited", + "InvalidClusterSizeReceived", + "RaftCoreInited", + "RaftStateSwitch", + "RaftModeSwitch", + "RaftNewLogEntry", + "RaftRequestIgnored", + "RaftVoteRequestReceived", + "RaftVoteRequestSucceeded", + "RaftVoteRequestInitiation", + "RaftPersistStateUpdateError", + "RaftCommitIndexUpdate", + "RaftNewerTermInResponse", + "RaftNewEntryCommitted", + "RaftAppendEntriesCallFailure" + }; + uavcan::StaticAssert::check(); + UAVCAN_ASSERT(code < NumTraceEventCodes); + return (code < NumTraceEventCodes) ? Strings[code] : "INVALID_EVENT_CODE"; +} +#endif + namespace dynamic_node_id_server_impl { /* diff --git a/libuavcan/test/protocol/dynamic_node_id_allocation_server.cpp b/libuavcan/test/protocol/dynamic_node_id_allocation_server.cpp index 55a74f93f4..5de7b7a213 100644 --- a/libuavcan/test/protocol/dynamic_node_id_allocation_server.cpp +++ b/libuavcan/test/protocol/dynamic_node_id_allocation_server.cpp @@ -57,9 +57,9 @@ class EventTracer : public uavcan::IDynamicNodeIDAllocationServerEventTracer { const std::string id_; - virtual void onEvent(uavcan::uint16_t event_code, uavcan::int64_t event_argument) + virtual void onEvent(uavcan::uint16_t code, uavcan::int64_t argument) { - std::cout << "EVENT [" << id_ << "]\t" << event_code << "\t" << event_argument << std::endl; + std::cout << "EVENT [" << id_ << "]\t" << code << "\t" << getEventName(code) << "\t" << argument << std::endl; } public: @@ -874,6 +874,23 @@ TEST(DynamicNodeIDAllocationServer, RaftCoreBasic) } +TEST(DynamicNodeIDAllocationServer, EventCodeToString) +{ + using uavcan::IDynamicNodeIDAllocationServerEventTracer; + using namespace uavcan::dynamic_node_id_server_impl; + + // Simply checking some error codes + ASSERT_STREQ("Error", + IDynamicNodeIDAllocationServerEventTracer::getEventName(TraceError)); + ASSERT_STREQ("RaftModeSwitch", + IDynamicNodeIDAllocationServerEventTracer::getEventName(TraceRaftModeSwitch)); + ASSERT_STREQ("RaftAppendEntriesCallFailure", + IDynamicNodeIDAllocationServerEventTracer::getEventName(TraceRaftAppendEntriesCallFailure)); + ASSERT_STREQ("DiscoveryReceived", + IDynamicNodeIDAllocationServerEventTracer::getEventName(TraceDiscoveryReceived)); +} + + TEST(DynamicNodeIDAllocationServer, ObjectSizes) { std::cout << "Log: " << sizeof(uavcan::dynamic_node_id_server_impl::Log) << std::endl;