Program Listing for File regmap_manager.cpp

Return to documentation for file (regmap_manager.cpp)

#include "regmap_manager.hpp"

#include "ers/ers.h"
#include "util.hpp"

#include <format>
#include <vector>
#include <algorithm>
#include <string>
#include <algorithm>
#include <iterator>

// Convert std::u8string to std::string by casting each char8_t to char
std::string RegmapManager::u8string_to_string(const std::u8string& u8str) {
    std::string str;
    std::transform(u8str.begin(), u8str.end(), std::back_inserter(str),
                   [](char8_t c) { return static_cast<char>(c); });
    return str;
}

RegmapManager::RegmapManager()
{
    #if REGMAP_VERSION < 0x0500
        std::string regmap_file = "regmap4-symbol.yaml";
    #else
        std::string regmap_file = "regmap5-symbol.yaml";
    #endif

    // get felix path, remove bin if needed
    std::filesystem::path executable_path = std::filesystem::canonical("/proc/self/exe").parent_path();
    std::filesystem::path felix_path = executable_path.filename() == "bin" ? executable_path.parent_path() : executable_path;
    std::filesystem::path regmap_path = felix_path / "share" / "felix-star" / regmap_file;
    ERS_INFO(std::format("Reading register map {}", u8string_to_string(regmap_path.u8string())));
    m_regmap = YAML::LoadFile(regmap_path);
}


bool RegmapManager::has_key(std::string name, std::string key, const std::string& value) {
    if (!m_regmap["registers"][name]) return false;
    std::vector<std::string> values = Util::split(m_regmap["registers"][name][key].Scalar(), '|');
    return std::find(values.begin(), values.end(), value) != values.end();
}


bool RegmapManager::has_type(const std::string& name, const std::string& type) {
    return has_key(name, "type", type);
}


bool RegmapManager::has_endpoint(const std::string& name, const std::string& endpoint) {
    return has_key(name, "endpoints", endpoint);
}