diff --git a/fpga/include/villas/fpga/card.hpp b/fpga/include/villas/fpga/card.hpp index 8c98cd93c..e09cac06f 100644 --- a/fpga/include/villas/fpga/card.hpp +++ b/fpga/include/villas/fpga/card.hpp @@ -10,6 +10,7 @@ #pragma once +#include #include #include @@ -56,5 +57,14 @@ protected: Logger logger; }; +class CardFactory { +private: + static Logger getStaticLogger() { return villas::Log::get("card:factory"); } + +public: + static void loadIps(std::shared_ptr card, json_t *json_ips, + const std::filesystem::path &searchPath); +}; + } // namespace fpga } // namespace villas diff --git a/fpga/lib/card.cpp b/fpga/lib/card.cpp index 1048b41b4..71136571b 100644 --- a/fpga/lib/card.cpp +++ b/fpga/lib/card.cpp @@ -8,6 +8,7 @@ * SPDX-License-Identifier: Apache-2.0 */ +#include #include using namespace villas; @@ -125,3 +126,45 @@ bool Card::mapMemoryBlock(const std::shared_ptr block) { return true; } + +void CardFactory::loadIps(std::shared_ptr card, json_t *json_ips, + const std::filesystem::path &searchPath) { + auto logger = getStaticLogger(); + + // Load IPs from a separate json file + if (!json_is_string(json_ips)) { + logger->debug("FPGA IP cores config item is not a string."); + throw ConfigError(json_ips, "node-config-fpga-ips", + "FPGA IP cores config item is not a string."); + } + + if (!searchPath.empty()) { + std::filesystem::path json_ips_path = + searchPath / json_string_value(json_ips); + logger->debug("searching for FPGA IP cors config at {}", + json_ips_path.string()); + json_ips = json_load_file(json_ips_path.c_str(), 0, nullptr); + } else { + json_ips = json_load_file(json_string_value(json_ips), 0, nullptr); + } + + if (json_ips == nullptr) { + json_ips = json_load_file(json_string_value(json_ips), 0, nullptr); + logger->debug("searching for FPGA IP cors config at {}", + json_string_value(json_ips)); + if (json_ips == nullptr) { + throw ConfigError(json_ips, "node-config-fpga-ips", + "Failed to find FPGA IP cores config"); + } + } + + if (not json_is_object(json_ips)) + throw ConfigError(json_ips, "node-config-fpga-ips", + "FPGA IP core list must be an object!"); + + ip::CoreFactory::make(card.get(), json_ips); + + if (card->ips.empty()) + throw ConfigError(json_ips, "node-config-fpga-ips", + "Cannot initialize IPs of FPGA card {}", card->name); +}; diff --git a/fpga/lib/pcie_card.cpp b/fpga/lib/pcie_card.cpp index 133bf8fc9..45e8ac808 100644 --- a/fpga/lib/pcie_card.cpp +++ b/fpga/lib/pcie_card.cpp @@ -93,39 +93,7 @@ PCIeCardFactory::make(json_t *json_card, std::string card_name, return nullptr; } - // Load IPs from a separate json file - if (!json_is_string(json_ips)) { - logger->debug("FPGA IP cores config item is not a string."); - throw ConfigError(json_ips, "node-config-fpga-ips", - "FPGA IP cores config item is not a string."); - } - if (!searchPath.empty()) { - std::filesystem::path json_ips_path = - searchPath / json_string_value(json_ips); - logger->debug("searching for FPGA IP cors config at {}", - json_ips_path.string()); - json_ips = json_load_file(json_ips_path.c_str(), 0, nullptr); - } else { - json_ips = json_load_file(json_string_value(json_ips), 0, nullptr); - } - if (json_ips == nullptr) { - json_ips = json_load_file(json_string_value(json_ips), 0, nullptr); - logger->debug("searching for FPGA IP cors config at {}", - json_string_value(json_ips)); - if (json_ips == nullptr) { - throw ConfigError(json_ips, "node-config-fpga-ips", - "Failed to find FPGA IP cores config"); - } - } - - if (not json_is_object(json_ips)) - throw ConfigError(json_ips, "node-config-fpga-ips", - "FPGA IP core list must be an object!"); - - ip::CoreFactory::make(card.get(), json_ips); - if (card->ips.empty()) - throw ConfigError(json_ips, "node-config-fpga-ips", - "Cannot initialize IPs of FPGA card {}", card_name); + CardFactory::loadIps(card, json_ips, searchPath); if (not card->check()) throw RuntimeError("Checking of FPGA card {} failed", card_name);