blob: 4c4bee6de2184c6b522c0e999a2ad273fddd9b41 [file] [log] [blame] [edit]
#pragma once
#include "include/dbus_utility.hpp"
#include <boost/container/flat_map.hpp>
#include <nlohmann/json.hpp>
#include <phosphor-logging/lg2.hpp>
#include <sdbusplus/asio/property.hpp>
#include <chrono>
#include <ctime>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <regex>
#include <variant>
#include <vector>
#ifdef UNIT_TEST
#include "test/include/mock_dbus_utility.hpp"
#endif
using PageIdMap = boost::container::flat_map<uint8_t, std::string>;
using PropertyValue = std::variant<uint8_t, uint16_t, std::string>;
using json = nlohmann::json;
namespace fs = std::filesystem;
namespace util
{
inline const std::string_view kSumLogName = "sumLog";
inline const std::string_view kSumBBLogName = "sumBBLog";
const uint64_t kMaxLogSize = static_cast<uint64_t>(20 * 1024); // 20 KB
/**
* @brief Find files in a directory that match a given string.
*
* @param dirPath The path to the directory to search in.
* @param matchString The string to match against file names.
* @return A vector of paths to the found files.
*/
std::vector<fs::path> findFiles(const fs::path& dirPath,
const std::string& matchString);
/**
* @brief Get the board name and part number from a JSON object.
*
* @param configuration The JSON object containing the board name and part
* number.
* @return A pair containing the board name and part number.
*/
inline std::pair<std::string, std::vector<std::string>>
getBoardAndPartNumberPrefixes(const json& configuration)
{
std::string boardName = configuration.value("Board", "");
std::vector<std::string> partNumberPrefixes =
configuration.value("PartNumberPrefixes", std::vector<std::string>());
return {boardName, partNumberPrefixes};
}
/**
* @brief Check if the part number in a file name matches the part number in the
* configuration.
*
* @param fileName The path to the file.
* @param configuration The JSON object containing the part number.
* @return True if the part number matches, false otherwise.
*/
bool checkPartNumber(std::string_view fileName, const json& configuration);
/**
* @brief Summarize a log message and append it to a log file. Make sure the log
* file does not exceed a certain size.
*
* @param msg The message to summarize.
* @param logDirPath The path to the directory containing the log file.
* @param filename The name of the log file.
*/
void summarizeLog(DbusUtil* dbusUtil, std::string_view msg,
const fs::path& logDirPath, std::string_view filename);
/**
* @brief Store a power fault message in a log file if there is any.
*
* @param message The message to store.
* @param powerFault True if it is a power fault, false otherwise.
* @param logPath The path to the log file.
*/
inline void storePowerFaultIfAny(std::string_view message,
const bool powerFault, const fs::path& logPath)
{
if (!powerFault)
{
lg2::info("No power fault detected");
return;
}
summarizeLog(getDbusUtil(), message, logPath.parent_path(), kSumBBLogName);
}
/**
* @brief Get the value of a D-Bus property.
*
* @param serviceName The name of the D-Bus service.
* @param objectPath The path to the D-Bus object.
* @param interface The name of the D-Bus interface.
* @param propertyName The name of the property.
* @return A pair containing a boolean indicating if the property value was
* successfully retrieved and the property value.
*/
std::pair<bool, PropertyValue> getProperty(const std::string& serviceName,
const std::string& objectPath,
const std::string& interface,
const std::string& propertyName);
/**
* @brief Open an I2C device.
*
* @param bus The I2C bus number.
* @param address The I2C device address.
* @return The file descriptor of the opened device, or -1 if an error occurred.
*/
int i2cOpenDevice(uint16_t bus, uint16_t address);
/**
* @brief Write a byte to an I2C device.
*
* @param bus The I2C bus number.
* @param address The I2C device address.
* @param command The command byte.
* @param value The value byte.
*/
void i2cWriteByte(uint8_t bus, uint8_t address, uint8_t command, uint8_t value);
/**
* @brief Write data to an I2C device and read data from it.
*
* @param bus The I2C bus number.
* @param address The I2C device address.
* @param writeBuf The buffer containing the data to write.
* @param writeLen The length of the data to write.
* @param recvLen The length of the data to read.
* @return The read data.
*/
std::vector<uint8_t> i2cWriteRead(uint8_t bus, uint8_t address,
const std::vector<uint8_t>& writeBuf,
uint16_t writeLen, uint16_t recvLen);
/**
* @brief Read a byte from an I2C device.
*
* @param bus The I2C bus number.
* @param address The I2C device address.
* @param reg The register address.
* @return The read byte.
*/
std::uint8_t i2cReadByte(uint8_t bus, uint8_t address, uint8_t reg);
/**
* @brief Read multiple bytes from an I2C device.
*
* @param bus The I2C bus number.
* @param address The I2C device address.
* @param reg The register address.
* @param length The number of bytes to read.
* @return The read bytes.
*/
std::vector<uint8_t> i2cReadBytes(uint8_t bus, uint8_t address, uint8_t reg,
uint16_t length);
/**
* @brief Convert a linear value to a floating-point value.
*
* @param value The linear value.
* @return The converted floating-point value.
*/
double convertFromLinear(uint16_t value);
/**
* @brief Convert a linear value to a floating-point value using a given
* exponent.
*
* @param value The linear value.
* @param exponent The exponent.
* @return The converted floating-point value.
*/
double convertFromVoutLinear(uint16_t value, int8_t exponent);
/**
* @brief Convert a direct value to a floating-point value using a given slope,
* intercept, and resolution.
*
* @param value The direct value.
* @param m The slope.
* @param b The intercept.
* @param r The resolution.
* @return The converted floating-point value.
*/
double convertFromDirect(uint16_t value, int16_t m, int16_t b, int8_t r);
/**
* @brief Parse a decimal value from a JSON element.
*
* @param element The JSON element.
* @return The parsed decimal value.
*/
uint16_t parseDec(const json& element);
/**
* @brief Parse a hexadecimal byte array from a JSON element.
*
* @param element The JSON element.
* @return The parsed hexadecimal byte array.
*/
std::vector<uint8_t> parseHexByteArray(const json& element);
/**
* @brief Parse a hexadecimal byte from a JSON element.
*
* @param element The JSON element.
* @return The parsed hexadecimal byte.
*/
uint8_t parseHexByte(const json& element);
/**
* @brief Convert two bytes into a word.
*
* @param upperByte The upper byte.
* @param lowerByte The lower byte.
* @return The converted word.
*/
uint16_t bytesIntoWord(std::string_view upperByte, std::string_view lowerByte);
/**
* @brief Convert a byte array to a hexadecimal string.
*
* @param buf The byte array.
* @param len The length of the byte array.
* @return The hexadecimal string.
*/
std::string byteArrayToHexString(uint8_t* buf, uint16_t len);
/**
* @brief Get the current timestamp in the PST time zone.
*
* @return The current timestamp.
*/
std::string getCurrentTimestamp();
} // namespace util