blob: 5ba6bf8b92288e8526a46d3a05f30f77c609b798 [file]
#pragma once
#include "include/common_utility.hpp"
#include "include/power_rail/max34451_utility.hpp"
#include "nlohmann/json.hpp"
#include <boost/algorithm/string.hpp>
#include <phosphor-logging/lg2.hpp>
#include <cstdint>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <optional>
#include <string>
#include <vector>
namespace powerfault
{
using json = nlohmann::json;
using PowerFaultRail = std::vector<std::string>;
using BitToPowerRailMap = std::map<uint32_t, std::string>;
namespace fs = std::filesystem;
constexpr uint8_t kCpldClearFlag = 0x00;
constexpr uint8_t kCpldReadFlag = 0x02;
constexpr uint8_t kCpldEraseFlag = 0x04;
constexpr uint8_t kCpldDisableFlag = 0x04;
constexpr uint8_t kCpldBusyFlag = 0x02;
constexpr uint8_t kCpldUfmComponentFaultFlag = 0x04;
constexpr std::string_view kZiggurat = "Ziggurat";
constexpr std::string_view kCPLDM = "CPLD-M";
constexpr std::string_view kCPLD = "CPLD";
constexpr std::string_view kSequencer = "Sequencer";
constexpr std::string_view kReg = "Reg";
constexpr std::string_view kReverseFlag = "ReverseFlag";
constexpr std::string_view kPage = "Page";
constexpr std::string_view kBit = "Bit";
constexpr std::string_view kName = "Name";
constexpr std::string_view kType = "Type";
constexpr std::string_view kSource = "Source";
constexpr std::string_view kBus = "Bus";
constexpr std::string_view kAddress = "Address";
constexpr std::string_view kCPLDRegisters = "CPLDRegisters";
constexpr std::string_view kSequencerRegisters = "SequencerRegisters";
constexpr std::string_view kBitToPowerRailMap = "BitToPowerRailMap";
constexpr std::string_view kPageToPowerRailMap = "PageToPowerRailMap";
constexpr std::string_view kRegister = "Register";
constexpr std::string_view kCPLDRuntimeFlag = "CPLD_RUNTIME_FLAG";
constexpr std::string_view kSetCPLDFlag = "SetCPLDFlag";
struct RailInfo
{
std::string name;
std::string type;
std::string source;
uint16_t bus{};
uint8_t address{};
uint8_t reg = 0;
uint8_t readLen = 0;
uint8_t reverse_flag = 0;
uint16_t page = 0;
uint16_t bit = 0;
RailInfo() = default;
RailInfo(const std::string& n, const std::string& t, const std::string& s,
uint16_t bus, uint8_t address) :
name(n), type(t), source(s), bus(bus), address(address)
{}
};
struct ProcessResult
{
bool success{};
bool hasErrorInZiggurat{};
bool runtime{};
PowerFaultRail powerFaultRails;
std::map<uint8_t, uint8_t> cpldRegValues;
};
struct CPLDResult
{
bool success;
bool runtime;
bool hasErrorInZiggurat;
PowerFaultRail powerFaultRails;
std::map<uint8_t, uint8_t> cpldRegValues;
};
/**
* @brief Reads a flag from a CPLD register.
*
* @param bus The bus number.
* @param addr The address of the CPLD.
* @param reg The register to read.
* @return The value of the CPLD flag.
*/
uint8_t readCPLDFlag(uint8_t bus, uint8_t addr, uint8_t reg);
/**
* @brief Executes an action on the CPLD.
*
* @param bus The bus number.
* @param addr The address of the CPLD.
* @param action The action to execute.
*/
void cpldExecuteAction(uint8_t bus, uint8_t addr, uint8_t action);
/**
* @brief Operates the UFM (User Flash Memory) on the CPLD.
*
* @param bus The bus number.
* @param addr The address of the CPLD.
* @param action The action to perform.
* @return true if the operation is successful, false otherwise.
*/
bool operateUFM(uint8_t bus, uint8_t addr, uint8_t action);
/**
* @brief Processes a rail configuration from JSON.
*
* @param rail The JSON object containing the rail configuration.
* @return An optional RailInfo object.
*/
std::optional<RailInfo> processRail(const nlohmann::json& rail);
/**
* @brief Reads priority files from the specified directory.
*
* @param priorityDirPath The directory path.
* @return A pair of vectors containing the priority files.
*/
std::pair<std::vector<std::string>, std::vector<std::string>>
readPriorityFiles(const fs::path& priorityDirPath);
/**
* @brief Reads register map files from the specified directory.
*
* @param registerMapDirPath The directory path.
* @return A pair of JSON objects containing the register maps.
*/
std::pair<json, json> readRegisterMapFiles(const fs::path& registerMapDirPath);
/**
* @brief Collects power rail configurations from the register map.
*
* @param registerMap The JSON object containing the register map.
* @param registerType The type of register.
* @return A map of power rail configurations.
*/
std::map<std::string, json> collectPowerRails(const json& registerMap,
std::string_view registerType);
/**
* @brief Processes a list of priority rails.
*
* @param priorityList The list of priority rails.
* @param railInfoMap Map of rail names to RailInfo.
* @param logDirPath The directory path for logs.
* @param cpldRegValues Map for CPLD register values.
* @return ProcessResult object.
*/
ProcessResult
processPriorityList(const std::vector<std::string>& priorityList,
const std::map<std::string, RailInfo>& railInfoMap,
const fs::path& logDirPath,
const std::map<uint8_t, uint8_t>& cpldRegValues);
/**
* @brief Handles a CPLD rail.
*
* @param railInfo The rail information.
* @param cpldRegValues Map for CPLD register values.
* @param logFile The output file stream.
* @return ProcessResult object.
*/
ProcessResult handleCPLDRail(const RailInfo& railInfo,
std::map<uint8_t, uint8_t> cpldRegValues,
std::ofstream& logFile);
/**
* @brief Handles a sequencer rail.
*
* @param railInfo The rail information.
* @param logFile The output file stream.
* @return ProcessResult object.
*/
ProcessResult handleSequencerRail(const RailInfo& railInfo,
std::ofstream& logFile);
/**
* @brief Gets the CPLD register range.
*
* @param railInfoMap Map of rail names to RailInfo.
* @param cpldType The CPLD type.
* @return A pair containing the minimum and maximum register values.
*/
std::pair<uint8_t, uint8_t>
getCPLDRegisterRange(const std::map<std::string, RailInfo>& railInfoMap,
std::string_view cpldType);
/**
* @brief Gets the CPLD bus and address.
*
* @param railInfoMap Map of rail names to RailInfo.
* @param source The source type.
* @return A pair containing the bus and address.
*/
std::pair<uint8_t, uint8_t>
getCPLDBusAndAddress(const std::map<std::string, RailInfo>& railInfoMap,
std::string_view source);
/**
* @brief Reads registers from the CPLD.
*
* @param bus The bus number.
* @param address The address of the CPLD.
* @param minReg The minimum register.
* @param regRange The range of registers.
* @return A map of register values.
*/
std::map<uint8_t, uint8_t> readCPLDRegisters(uint8_t bus, uint8_t address,
uint8_t minReg, size_t regRange,
const bool& setCPLDFlag);
/**
* @brief Gets a map of rail information.
*
* @param registerMapDirPath The directory path for register maps.
* @return A pair of a boolean and a map of rail names to RailInfo.
*/
std::pair<bool, std::map<std::string, RailInfo>>
getRailInfoMap(const fs::path& registerMapDirPath);
/**
* @brief Gets values of CPLD registers.
*
* @param railInfoMap Map of rail names to RailInfo.
* @return A map of register values.
*/
std::map<uint8_t, uint8_t>
getCPLDRegValues(const std::map<std::string, RailInfo>& railInfoMap,
const bool& setCPLDFlag);
/**
* @brief Logs the state of power rails.
*
* @param priorityDirPath The directory path for priorities.
* @param registerMapDirPath The directory path for register maps.
* @param logDirPath The directory path for logs.
* @param specifiedRailName The specified rail name.
* @return true if successful, false otherwise.
*/
bool logRailState(const fs::path& priorityDirPath,
const fs::path& registerMapDirPath,
const fs::path& logDirPath,
std::string_view specifiedRailName);
/**
* @brief Converts MAX34451 page value to string.
*
* @param page The MAX34451 page value.
* @return The string representation.
*/
std::string fromMax34451Mb(uint8_t page);
/**
* @brief Gets the power rail name from a bit value.
*
* @param bit The bit value.
* @param bitToPowerRailMap The map of bit values to power rails.
* @return The power rail name.
*/
void showPowerFault(bool runtime, const PowerFaultRail& powerFaultRails,
const fs::path& logDirPath);
} // namespace powerfault