Skip to content

Commit

Permalink
🎨 Committing clang-format changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jbbjarnason committed Jun 24, 2024
1 parent 79cfb03 commit 004b593
Show file tree
Hide file tree
Showing 15 changed files with 349 additions and 316 deletions.
47 changes: 22 additions & 25 deletions exes/themis/inc/alarm_database.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
#include <sqlite_modern_cpp.h>
#include <sqlite_modern_cpp/log.h>

#include <tfc/dbus/exception.hpp>
#include <tfc/logger.hpp>
#include <tfc/progbase.hpp>
#include <tfc/snitch/common.hpp>
#include <tfc/dbus/exception.hpp>

namespace tfc::themis {

Expand Down Expand Up @@ -125,7 +125,7 @@ CREATE TABLE IF NOT EXISTS AlarmVariables(
add_alarm_translation(alarm_id, "en", description, details);

// Reset the alarm if high on register
if (is_alarm_active(alarm_id)){
if (is_alarm_active(alarm_id)) {
reset_alarm(alarm_id);
}
db_ << "COMMIT;";
Expand Down Expand Up @@ -224,9 +224,7 @@ ON Alarms.sha1sum = AlarmTranslations.sha1sum;
return count;
}

[[nodiscard]] auto is_some_alarm_active() -> bool {
return active_alarm_count() > 0;
}
[[nodiscard]] auto is_some_alarm_active() -> bool { return active_alarm_count() > 0; }

/**
* @brief Set an alarm in the database
Expand Down Expand Up @@ -306,26 +304,25 @@ WHERE activation_time >= {} AND activation_time <= {})",

std::vector<tfc::snitch::api::activation> activations;

db_ << populated_query >> [&](std::uint64_t activation_id, snitch::api::alarm_id_t alarm_id,
std::int64_t activation_time, std::optional<std::int64_t> reset_time,
bool activation_level, std::optional<std::string> primary_details,
std::optional<std::string> primary_description, std::optional<std::string> backup_details,
std::optional<std::string> backup_description,
bool alarm_latching, std::int8_t alarm_level) {
if (!backup_description.has_value() || !backup_details.has_value()) {
throw dbus_error("Backup message not found for alarm translation. This should never happen.");
}
std::string details = primary_details.value_or(backup_details.value());
std::string description = primary_description.value_or(backup_description.value());
bool in_locale = primary_description.has_value() && primary_details.has_value();
std::optional<time_point> final_reset_time = std::nullopt;
if (reset_time.has_value()) {
final_reset_time = timepoint_from_milliseconds(reset_time.value());
}
activations.emplace_back(alarm_id, activation_id, description, details, activation_level,
static_cast<tfc::snitch::level_e>(alarm_level), alarm_latching,
timepoint_from_milliseconds(activation_time), final_reset_time, in_locale);
};
db_ << populated_query >>
[&](std::uint64_t activation_id, snitch::api::alarm_id_t alarm_id, std::int64_t activation_time,
std::optional<std::int64_t> reset_time, bool activation_level, std::optional<std::string> primary_details,
std::optional<std::string> primary_description, std::optional<std::string> backup_details,
std::optional<std::string> backup_description, bool alarm_latching, std::int8_t alarm_level) {
if (!backup_description.has_value() || !backup_details.has_value()) {
throw dbus_error("Backup message not found for alarm translation. This should never happen.");
}
std::string details = primary_details.value_or(backup_details.value());
std::string description = primary_description.value_or(backup_description.value());
bool in_locale = primary_description.has_value() && primary_details.has_value();
std::optional<time_point> final_reset_time = std::nullopt;
if (reset_time.has_value()) {
final_reset_time = timepoint_from_milliseconds(reset_time.value());
}
activations.emplace_back(alarm_id, activation_id, description, details, activation_level,
static_cast<tfc::snitch::level_e>(alarm_level), alarm_latching,
timepoint_from_milliseconds(activation_time), final_reset_time, in_locale);
};
for (auto& activation : activations) {
// TODO: This is not great would be better to do this in a large query
// As of now we are doing a query for each activation to get the variables
Expand Down
115 changes: 58 additions & 57 deletions exes/themis/inc/dbus_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@
#include <fmt/format.h>
#include <boost/asio/io_context.hpp>
#include <glaze/json.hpp>
#include <tfc/stx/glaze_meta.hpp>
#include <sdbusplus/asio/connection.hpp>
#include <sdbusplus/asio/object_server.hpp>
#include <sdbusplus/asio/property.hpp>
#include <tfc/dbus/match_rules.hpp>
#include <tfc/dbus/exception.hpp>

#include <tfc/dbus/match_rules.hpp>
#include <tfc/stx/glaze_meta.hpp>

namespace tfc::themis {
using namespace tfc::snitch::api::dbus;
Expand All @@ -27,87 +26,89 @@ class interface {
connection_ = connection;
object_server_ = std::make_unique<sdbusplus::asio::object_server>(connection_);
connection_->request_name(service_name.data());
interface_ =
object_server_->add_unique_interface(object_path.data(), interface_name.data());
interface_ = object_server_->add_unique_interface(object_path.data(), interface_name.data());

interface_->register_method(std::string(methods::list_alarms),
[&]() -> std::string {
return glz::write_json(database.list_alarms());
});
[&]() -> std::string { return glz::write_json(database.list_alarms()); });

interface_->register_method(std::string(methods::register_alarm),
[&](const sdbusplus::message_t& msg, std::string tfc_id, const std::string& description, const std::string& details, bool latching, std::underlying_type_t<snitch::level_e> alarm_level) -> std::uint64_t {
// TODO: User supplied alarm_level needs more verification
return database.register_alarm_en(tfc_id, description, details, latching, static_cast<tfc::snitch::level_e>(alarm_level));
});
interface_->register_method(
std::string(methods::register_alarm),
[&](const sdbusplus::message_t& msg, std::string tfc_id, const std::string& description, const std::string& details,
bool latching, std::underlying_type_t<snitch::level_e> alarm_level) -> std::uint64_t {
// TODO: User supplied alarm_level needs more verification
return database.register_alarm_en(tfc_id, description, details, latching,
static_cast<tfc::snitch::level_e>(alarm_level));
});

interface_->register_method(std::string(methods::set_alarm),
[&](snitch::api::alarm_id_t alarm_id, const std::unordered_map<std::string, std::string>& args) -> std::uint64_t {
return database.set_alarm(alarm_id, args);
});
interface_->register_method(
std::string(methods::set_alarm),
[&](snitch::api::alarm_id_t alarm_id, const std::unordered_map<std::string, std::string>& args) -> std::uint64_t {
return database.set_alarm(alarm_id, args);
});
interface_->register_method(std::string(methods::reset_alarm),
[&](snitch::api::alarm_id_t alarm_id) -> void {
database.reset_alarm(alarm_id);
});
interface_->register_method(std::string(methods::try_reset),
[&](snitch::api::alarm_id_t alarm_id) -> void {
if (database.is_alarm_active(alarm_id)){
auto message = interface_->new_signal(signals::try_reset.data());
message.append(alarm_id);
message.signal_send();
} else {
throw dbus_error("Alarm is not active");
}
});
interface_->register_method(std::string(methods::try_reset_all),
[&]() -> void {
if(database.is_some_alarm_active()){
auto message = interface_->new_signal(signals::try_reset_all.data());
message.signal_send();
} else {
throw dbus_error("No alarm is active");
}
});
using tfc::snitch::api::active_e;
[&](snitch::api::alarm_id_t alarm_id) -> void { database.reset_alarm(alarm_id); });
interface_->register_method(std::string(methods::try_reset), [&](snitch::api::alarm_id_t alarm_id) -> void {
if (database.is_alarm_active(alarm_id)) {
auto message = interface_->new_signal(signals::try_reset.data());
message.append(alarm_id);
message.signal_send();
} else {
throw dbus_error("Alarm is not active");
}
});
interface_->register_method(std::string(methods::try_reset_all), [&]() -> void {
if (database.is_some_alarm_active()) {
auto message = interface_->new_signal(signals::try_reset_all.data());
message.signal_send();
} else {
throw dbus_error("No alarm is active");
}
});
using tfc::snitch::level_e;
using tfc::snitch::api::active_e;
interface_->register_method(std::string(methods::list_activations),
[&](const std::string& locale, std::uint64_t start_count, std::uint64_t count, std::underlying_type_t<level_e> alarm_level, std::underlying_type_t<active_e> active, int64_t start, int64_t end) -> std::string {
auto cstart = tfc::themis::alarm_database::timepoint_from_milliseconds(start);
auto cend = tfc::themis::alarm_database::timepoint_from_milliseconds(end);
return glz::write_json(database.list_activations(locale, start_count, count, static_cast<tfc::snitch::level_e>(alarm_level), static_cast<tfc::snitch::api::active_e>(active), cstart, cend));
});
[&](const std::string& locale, std::uint64_t start_count, std::uint64_t count,
std::underlying_type_t<level_e> alarm_level, std::underlying_type_t<active_e> active,
int64_t start, int64_t end) -> std::string {
auto cstart = tfc::themis::alarm_database::timepoint_from_milliseconds(start);
auto cend = tfc::themis::alarm_database::timepoint_from_milliseconds(end);
return glz::write_json(database.list_activations(
locale, start_count, count, static_cast<tfc::snitch::level_e>(alarm_level),
static_cast<tfc::snitch::api::active_e>(active), cstart, cend));
});

// Signal alarm_id, current_activation, ack_status
interface_->register_signal<std::tuple<std::uint64_t, bool, bool>>(std::string(signals::alarm_activation_changed));
interface_->register_signal<std::uint64_t>(std::string(signals::try_reset));
interface_->register_signal<void>(std::string(signals::try_reset_all));
fmt::println(stderr, "{}", match_rule_);
name_lost_match_ = std::make_unique<sdbusplus::bus::match::match>(
*connection_, match_rule_.data(), std::bind_front(&interface::match_callback, this));
name_lost_match_ = std::make_unique<sdbusplus::bus::match::match>(*connection_, match_rule_.data(),
std::bind_front(&interface::match_callback, this));
interface_->initialize();

}

private:
static constexpr std::string_view dbus_interface_ = "org.freedesktop.DBus"sv;
static constexpr std::string_view dbus_name_ = "org.freedesktop.DBus"sv;
static constexpr std::string_view dbus_path_ = "/org/freedesktop/DBus"sv;
static constexpr std::string_view name_owner_changed_ = "NameOwnerChanged"sv;
static constexpr std::string_view match_rule_ = tfc::dbus::match::rules::make_match_rule<dbus_name_, dbus_interface_, dbus_path_, name_owner_changed_, tfc::dbus::match::rules::type::signal>();
static constexpr std::string_view match_rule_ = tfc::dbus::match::rules::
make_match_rule<dbus_name_, dbus_interface_, dbus_path_, name_owner_changed_, tfc::dbus::match::rules::type::signal>();
auto match_callback(sdbusplus::message_t& msg) -> void {
std::string name;
std::string old_owner;
std::string new_owner;
msg.read(name, old_owner, new_owner);
if (name == old_owner){
if (name == old_owner) {
// Someone has left the chat.
auto iterator = dbus_ids_to_monitor_.find(name);
if (iterator != dbus_ids_to_monitor_.end()){
if (iterator != dbus_ids_to_monitor_.end()) {
// We knew that guy
//TODO: Decide what to do here.
//TODO: Could cleanup the alarm state, and place a warning into the logs that this process is not handling his alarms anymore.
//TODO: Could also just ignore it, and let the alarm state be as it is.
//TODO: There is also the question of alarms that are active when the system is shut down. Who cleans them up?
// TODO: Decide what to do here.
// TODO: Could cleanup the alarm state, and place a warning into the logs that this process is not handling his
// alarms anymore.
// TODO: Could also just ignore it, and let the alarm state be as it is.
// TODO: There is also the question of alarms that are active when the system is shut down. Who cleans them up?
fmt::println(stderr, "name: {}, old_owner: {}, new_owner: {}", name, old_owner, new_owner);
}
}
Expand All @@ -116,6 +117,6 @@ class interface {
std::unique_ptr<sdbusplus::asio::dbus_interface> interface_;
std::unique_ptr<sdbusplus::asio::object_server> object_server_;
std::unique_ptr<sdbusplus::bus::match::match> name_lost_match_;
std::unordered_map<std::string, std::vector<uint64_t>> dbus_ids_to_monitor_; // Alarm members and monitoring parties
std::unordered_map<std::string, std::vector<uint64_t>> dbus_ids_to_monitor_; // Alarm members and monitoring parties
};
} // namespace tfc::ipc_ruler
} // namespace tfc::themis
2 changes: 1 addition & 1 deletion exes/themis/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <boost/program_options.hpp>
#include <boost/asio.hpp>
#include <boost/program_options.hpp>
#include <sdbusplus/asio/connection.hpp>

#include <tfc/dbus/sd_bus.hpp>
Expand Down
Loading

0 comments on commit 004b593

Please sign in to comment.