From ee85d2d73a0631c5a19ce81fda8576f6de55faa3 Mon Sep 17 00:00:00 2001 From: Pavel Kirienko Date: Sat, 9 May 2015 12:26:22 +0300 Subject: [PATCH] Panic broadcaster moved to header --- .../uavcan/protocol/panic_broadcaster.hpp | 48 +++++++++++++-- .../src/protocol/uc_panic_broadcaster.cpp | 61 ------------------- 2 files changed, 42 insertions(+), 67 deletions(-) delete mode 100644 libuavcan/src/protocol/uc_panic_broadcaster.cpp diff --git a/libuavcan/include/uavcan/protocol/panic_broadcaster.hpp b/libuavcan/include/uavcan/protocol/panic_broadcaster.hpp index c05a55fe2e..2a15b5d1d6 100644 --- a/libuavcan/include/uavcan/protocol/panic_broadcaster.hpp +++ b/libuavcan/include/uavcan/protocol/panic_broadcaster.hpp @@ -5,6 +5,7 @@ #ifndef UAVCAN_PROTOCOL_PANIC_BROADCASTER_HPP_INCLUDED #define UAVCAN_PROTOCOL_PANIC_BROADCASTER_HPP_INCLUDED +#include #include #include #include @@ -19,9 +20,19 @@ class UAVCAN_EXPORT PanicBroadcaster : private TimerBase Publisher pub_; protocol::Panic msg_; - void publishOnce(); + void publishOnce() + { + const int res = pub_.broadcast(msg_); + if (res < 0) + { + pub_.getNode().registerInternalFailure("Panic pub failed"); + } + } - virtual void handleTimerEvent(const TimerEvent&); + virtual void handleTimerEvent(const TimerEvent&) + { + publishOnce(); + } public: explicit PanicBroadcaster(INode& node) @@ -37,16 +48,41 @@ public: * @param short_reason Short ASCII string that describes the reason of the panic, 7 characters max. * If the string exceeds 7 characters, it will be truncated. */ - void panic(const char* short_reason_description); + void panic(const char* short_reason_description) + { + msg_.reason_text.clear(); + const char* p = short_reason_description; + while (p && *p) + { + if (msg_.reason_text.size() == msg_.reason_text.capacity()) + { + break; + } + msg_.reason_text.push_back(protocol::Panic::FieldTypes::reason_text::ValueType(*p)); + p++; + } + + UAVCAN_TRACE("PanicBroadcaster", "Panicking with reason '%s'", getReason().c_str()); + + publishOnce(); + startPeriodic(MonotonicDuration::fromMSec(protocol::Panic::BROADCASTING_INTERVAL_MS)); + } /** * Stop broadcasting immediately. */ - void dontPanic(); // Where's my towel + void dontPanic() // Where's my towel + { + stop(); + msg_.reason_text.clear(); + } - bool isPanicking() const; + bool isPanicking() const { return isRunning(); } - const typename protocol::Panic::FieldTypes::reason_text& getReason() const; + const typename protocol::Panic::FieldTypes::reason_text& getReason() const + { + return msg_.reason_text; + } }; } diff --git a/libuavcan/src/protocol/uc_panic_broadcaster.cpp b/libuavcan/src/protocol/uc_panic_broadcaster.cpp deleted file mode 100644 index 8c73929586..0000000000 --- a/libuavcan/src/protocol/uc_panic_broadcaster.cpp +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright (C) 2014 Pavel Kirienko - */ - -#include -#include - -namespace uavcan -{ - -void PanicBroadcaster::publishOnce() -{ - const int res = pub_.broadcast(msg_); - if (res < 0) - { - pub_.getNode().registerInternalFailure("Panic pub failed"); - } -} - -void PanicBroadcaster::handleTimerEvent(const TimerEvent&) -{ - publishOnce(); -} - -void PanicBroadcaster::panic(const char* short_reason_description) -{ - msg_.reason_text.clear(); - const char* p = short_reason_description; - while (p && *p) - { - if (msg_.reason_text.size() == msg_.reason_text.capacity()) - { - break; - } - msg_.reason_text.push_back(protocol::Panic::FieldTypes::reason_text::ValueType(*p)); - p++; - } - - UAVCAN_TRACE("PanicBroadcaster", "Panicking with reason '%s'", getReason().c_str()); - - publishOnce(); - startPeriodic(MonotonicDuration::fromMSec(protocol::Panic::BROADCASTING_INTERVAL_MS)); -} - -void PanicBroadcaster::dontPanic() -{ - stop(); - msg_.reason_text.clear(); -} - -bool PanicBroadcaster::isPanicking() const -{ - return isRunning(); -} - -const typename protocol::Panic::FieldTypes::reason_text& PanicBroadcaster::getReason() const -{ - return msg_.reason_text; -} - -}