blob: 8a90959690029aea0f0b0d09ee93e8619321580a [file] [log] [blame]
#pragma once
#include "boottime_api/node_config.h"
#include "dbus_handler.hpp"
#include "psm_handler.hpp"
#include <fmt/printf.h>
#include <string_view>
#include <vector>
namespace boot_time_monitor
{
/**
* @brief Contains helper functions for generating configuration objects and
* names.
*
* This namespace provides utility functions to create configuration structures
* (like PSMConfig and DbusConfig) and standardized node names based on system
* properties like the number of hosts or BMCs and their indices.
*/
namespace gen
{
namespace btm = boot_time_monitor;
/**
* @brief Generates the PSM (Platform State Manager) configuration for a
* specific node.
*
* This function determines the correct D-Bus service names and object paths
* for monitoring host state and OS status based on whether the system is
* single-host or multi-host. For multi-host systems, it uses the node's
* index within the provided list to construct unique identifiers.
*
* @param nodeConfigs A vector containing the configurations of all nodes.
* @param nodeConfig The configuration of the specific node for which to
* generate the PSM config.
* @return psm::PSMConfig The generated PSM configuration containing D-Bus
* service names and object paths. Returns an empty
* config if the node is not found or the list is empty.
*/
inline psm::PSMConfig
GenPSMConfig(const std::vector<btm::NodeConfig>& nodeConfigs,
const btm::NodeConfig& nodeConfig)
{
if (nodeConfigs.empty())
{
fmt::print("[{}] node list is empty!\n", __FUNCTION__);
return {};
}
// Singlehost rule
if (nodeConfigs.size() == 1)
{
return {
.hostStateDbusService = "xyz.openbmc_project.State.Host",
.hostStateDbusObjPath = "/xyz/openbmc_project/state/host0",
.osStateDbusService = "xyz.openbmc_project.State.OperatingSystem",
.osStateDbusObjPath = "/xyz/openbmc_project/state/os",
};
}
// Multihost platforms have a different PSM Dbus Service and Path rule.
// Create dBus path depends on its relative indices.
auto it = std::find(nodeConfigs.begin(), nodeConfigs.end(), nodeConfig);
if (it == nodeConfigs.end())
{
fmt::print("[{}] node config not found!\n", __FUNCTION__);
return {};
}
int64_t idx = std::distance(nodeConfigs.begin(), it) + 1;
return {
.hostStateDbusService = fmt::format("xyz.openbmc_project.State.Host{}",
idx),
.hostStateDbusObjPath = fmt::format("/xyz/openbmc_project/state/host{}",
idx),
.osStateDbusService =
fmt::format("xyz.openbmc_project.State.OperatingSystem{}", idx),
.osStateDbusObjPath = "/xyz/openbmc_project/state/os",
};
}
/**
* @brief Generates the D-Bus configuration for a specific node.
*
* Constructs the D-Bus object path for the boot time monitor interfaces
* based on the node's name.
*
* @param nodeConfig The configuration of the node.
* @return dbus::DbusConfig The generated D-Bus configuration containing the
* object path.
*/
inline dbus::DbusConfig GenDbusConfig(const NodeConfig& nodeConfig)
{
return {.dbusObjPath = fmt::format("/xyz/openbmc_project/time/boot/{}",
nodeConfig.node_name)};
}
/**
* @brief Generates a standard host node name based on its index.
*
* @param idx The zero-based index of the host.
* @return std::string The generated host node name (e.g., "host0", "host1").
*/
inline std::string GenHostNodeName(int idx)
{
return fmt::format("host{}", idx);
}
/**
* @brief Generates a standard BMC node name based on the total number of BMCs
* and its index.
* @param bmcAmount The total number of BMCs in the system.
* @param idx The zero-based index of this BMC.
* @return std::string The generated BMC node name (e.g., "bmc" for single BMC,
* "bmc0", "bmc1" for multiple).
*/
inline std::string GenBmcNodeName(int64_t bmcAmount, int idx)
{
if (bmcAmount == 1)
{
return "bmc";
}
return fmt::format("bmc{}", idx);
}
} // namespace gen
} // namespace boot_time_monitor