Skip to content

Commit

Permalink
implement set and reset for snitch
Browse files Browse the repository at this point in the history
  • Loading branch information
jbbjarnason committed Jun 21, 2024
1 parent 94c5beb commit 0c5c718
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 23 deletions.
97 changes: 74 additions & 23 deletions libs/snitch/inc/public/tfc/snitch.hpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
#pragma once

#include <chrono>
#include <cstdint>
#include <string_view>
#include <unordered_map>

#include <fmt/core.h>
#include <fmt/args.h>
#include <fmt/core.h>
#include <boost/asio/steady_timer.hpp>

#include <tfc/progbase.hpp>
#include <tfc/logger.hpp>
#include <tfc/stx/concepts.hpp>
#include <tfc/stx/basic_fixed_string.hpp>
#include <tfc/progbase.hpp>
#include <tfc/snitch/common.hpp>
#include <tfc/snitch/format_extension.hpp>
#include <tfc/snitch/details/dbus_client.hpp>
#include <tfc/snitch/format_extension.hpp>
#include <tfc/stx/basic_fixed_string.hpp>
#include <tfc/stx/concepts.hpp>

namespace tfc::snitch {

namespace asio = boost::asio;

struct variance {
bool resettable{};
level_e lvl{ level_e::unknown };
Expand All @@ -39,10 +44,9 @@ class alarm {
/// \example alarm<{}, "short desc", "long desc"> warn(fmt::arg("key", "value"), fmt::arg("key2", 1));
/// \param unique_id A unique identifier for the alarm within this process
/// \param default_args Default fmt::arg values to populate the alarm with, e.g. fmt::arg("index", 1) for tank 1 etc.
alarm(std::shared_ptr<sdbusplus::asio::connection> conn, std::string_view unique_id, named_arg auto&& ... default_args) :
conn_{ std::move(conn) },
given_id_{ unique_id },
default_values_{ std::make_pair(default_args.name, fmt::format("{}", default_args.value))... } {
alarm(std::shared_ptr<sdbusplus::asio::connection> conn, std::string_view unique_id, named_arg auto&&... default_args)
: conn_{ std::move(conn) }, given_id_{ unique_id },
default_values_{ std::make_pair(default_args.name, fmt::format("{}", default_args.value))... } {
static_assert(detail::check_all_arguments_named(description), "All arguments must be named");
static_assert(detail::check_all_arguments_no_format(description), "All arguments may not have format specifiers");
[[maybe_unused]] static constexpr int num_args = sizeof...(default_args);
Expand All @@ -56,7 +60,7 @@ class alarm {
~alarm() = default;

template <stx::invocable callback_t>
requires (var.resettable)
requires(var.resettable)
void on_try_reset(callback_t&& callback) {
dbus_client_.on_try_reset_alarm([this, callback](api::alarm_id_t id) {
if (active_ && id == alarm_id_) {
Expand All @@ -70,7 +74,7 @@ class alarm {
});
}

void set(named_arg auto&& ... args) {
void set(named_arg auto&&... args) {
fmt::dynamic_format_arg_store<fmt::format_context> store;
for (auto const& [key, value] : default_values_) {
store.push_back(fmt::arg(key.c_str(), value));
Expand All @@ -79,31 +83,79 @@ class alarm {
logger_.debug(fmt::vformat(description, store));
logger_.debug(fmt::vformat(details, store));

std::unordered_map<std::string, std::string> params;
// for (auto const& arg : store) {
// params[key] = fmt::format("{}", value);
// }
auto params{ default_values_ };
params.emplace(std::make_pair(args.name, fmt::format("{}", args.value))...);

if (active_) {
logger_.info("alarm already active, not doing anything");
}

active_ = true;

if (alarm_id_) {
dbus_client_.set_alarm(alarm_id_.value(), params, [this](std::error_code const& ec) {
if (ec) {
logger_.error("Failed to set alarm: {}", ec.message());
// I am both inclined to reset active flag here aswell as not ... I think I will not
// active_ = false;
}
});
} else {
retry_timer_.expires_after(std::chrono::seconds(1));
retry_timer_.async_wait([this, args...](std::error_code const& ec) {
if (ec) {
logger_.error("Retry set timer failed: {}", ec.message());
return;
}
logger_.debug("Retrying set alarm");
set(args...);
});
}
}
private:
void register_alarm() {
dbus_client_.register_alarm(tfc_id_, description, details, var.resettable, var.lvl, [this](std::error_code const& ec, api::alarm_id_t id) {

void reset() {
if (!active_) {
logger_.info("alarm already inactive, not doing anything");
return;
}
active_ = false;
if (!alarm_id_) {
logger_.info("NO alarm ID, forget that alarm ever happened, the timings would be off so would not be valuable information");
return;
}
dbus_client_.reset_alarm(alarm_id_.value(), [this](std::error_code const& ec) {
if (ec) {
logger_.error("Failed to register alarm: {}", ec.message());
return;
logger_.error("Failed to reset alarm: {}", ec.message());
// I am both inclined to set active flag here aswell as not ... I think I will not
// active_ = true;
}
alarm_id_ = id;
});
}

private:
void register_alarm() {
dbus_client_.register_alarm(tfc_id_, description, details, var.resettable, var.lvl,
[this](std::error_code const& ec, api::alarm_id_t id) {
if (ec) {
logger_.error("Failed to register alarm: {}", ec.message());
return;
}
alarm_id_ = id;
});
}
bool active_{};
std::shared_ptr<sdbusplus::asio::connection> conn_;
std::string given_id_;
std::string tfc_id_{ fmt::format("{}.{}.{}", base::get_exe_name(), base::get_proc_name(), given_id_) };
std::unordered_map<std::string, std::string> default_values_;
std::optional<api::alarm_id_t> alarm_id_{};
asio::steady_timer retry_timer_{ conn_->get_io_context() };
detail::dbus_client dbus_client_{ conn_ };
logger::logger logger_{ fmt::format("snitch.{}", given_id_) };
};



template <stx::basic_fixed_string description, stx::basic_fixed_string details = "">
using info = alarm<variance{ .resettable = false, .lvl = level_e::info }, description, details>;
template <stx::basic_fixed_string description, stx::basic_fixed_string details = "">
Expand All @@ -115,5 +167,4 @@ using warning_resettable = warning_latched<description, details>;
template <stx::basic_fixed_string description, stx::basic_fixed_string details = "">
using error = alarm<variance{ .resettable = false, .lvl = level_e::error }, description, details>;


} // namespace tfc::snitch
} // namespace tfc::snitch
1 change: 1 addition & 0 deletions libs/snitch/inc/public/tfc/snitch/format_extension.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <cstddef>
#include <string_view>
#include <span>

#include <fmt/core.h>

Expand Down

0 comments on commit 0c5c718

Please sign in to comment.