From 3e2f9fb0a5ae977c6f833feb699ac09588c02aa5 Mon Sep 17 00:00:00 2001 From: Souvik Roy Date: Sun, 24 Nov 2024 23:33:08 -0600 Subject: [PATCH] Initial setup for vpd-tool APIs (#501) This commit includes changes and new source code files required for implementation of vpd-tool APIs. Change-Id: I821777237a3080d390e2bba151cd685ef8994bf9 Signed-off-by: Souvik Roy --- vpd-tool/include/tool_constants.hpp | 4 ++ vpd-tool/include/tool_json_utility.hpp | 53 ++++++++++++++++++++++++++ vpd-tool/include/tool_utils.hpp | 1 + vpd-tool/meson.build | 5 +++ vpd-tool/meson.options | 1 + 5 files changed, 64 insertions(+) create mode 100644 vpd-tool/include/tool_json_utility.hpp create mode 100644 vpd-tool/meson.options diff --git a/vpd-tool/include/tool_constants.hpp b/vpd-tool/include/tool_constants.hpp index ca5d8718..e2c41e39 100644 --- a/vpd-tool/include/tool_constants.hpp +++ b/vpd-tool/include/tool_constants.hpp @@ -14,5 +14,9 @@ constexpr auto inventoryManagerService = "xyz.openbmc_project.Inventory.Manager"; constexpr auto baseInventoryPath = "/xyz/openbmc_project/inventory"; constexpr auto ipzVpdInfPrefix = "com.ibm.ipzvpd."; +constexpr auto inventoryItemInf = "xyz.openbmc_project.Inventory.Item"; +constexpr auto kwdVpdInf = "com.ibm.ipzvpd.VINI"; +constexpr auto locationCodeInf = "com.ibm.ipzvpd.Location"; +constexpr auto assetInf = "xyz.openbmc_project.Inventory.Decorator.Asset"; } // namespace constants } // namespace vpd diff --git a/vpd-tool/include/tool_json_utility.hpp b/vpd-tool/include/tool_json_utility.hpp new file mode 100644 index 00000000..76ddb240 --- /dev/null +++ b/vpd-tool/include/tool_json_utility.hpp @@ -0,0 +1,53 @@ +#pragma once + +#include + +#include + +namespace vpd +{ +namespace jsonUtility +{ + +/** + * @brief API to parse respective JSON. + * + * Exception is thrown in case of JSON parse error. + * + * @param[in] i_pathToJson - Path to JSON. + * @return Parsed JSON. + * @throw std::runtime_error + */ +inline nlohmann::json getParsedJson(const std::string& i_pathToJson) +{ + if (i_pathToJson.empty()) + { + throw std::runtime_error("Path to JSON is missing"); + } + + if (!std::filesystem::exists(i_pathToJson) || + std::filesystem::is_empty(i_pathToJson)) + { + throw std::runtime_error("Incorrect File Path or empty file = " + + i_pathToJson); + } + + std::ifstream jsonFile(i_pathToJson); + if (!jsonFile) + { + throw std::runtime_error("Failed to access Json path = " + + i_pathToJson); + } + + try + { + return nlohmann::json::parse(jsonFile); + } + catch (const nlohmann::json::parse_error& e) + { + throw std::runtime_error("Failed to parse JSON file"); + } +} + +} // namespace jsonUtility +} // namespace vpd diff --git a/vpd-tool/include/tool_utils.hpp b/vpd-tool/include/tool_utils.hpp index d339e195..534aacaf 100644 --- a/vpd-tool/include/tool_utils.hpp +++ b/vpd-tool/include/tool_utils.hpp @@ -1,5 +1,6 @@ #pragma once +#include "tool_constants.hpp" #include "tool_types.hpp" #include diff --git a/vpd-tool/meson.build b/vpd-tool/meson.build index 403c9cb8..e4cc78e1 100644 --- a/vpd-tool/meson.build +++ b/vpd-tool/meson.build @@ -8,6 +8,11 @@ endif sdbusplus = dependency('sdbusplus', fallback: [ 'sdbusplus', 'sdbusplus_dep' ]) dependency_list = [CLI11_dep, sdbusplus] +conf_data = configuration_data() +conf_data.set_quoted('INVENTORY_JSON_SYM_LINK', get_option('INVENTORY_JSON_SYM_LINK')) +configure_file(output: 'config.h', + configuration : conf_data) + sources = ['src/vpd_tool_main.cpp', 'src/vpd_tool.cpp'] diff --git a/vpd-tool/meson.options b/vpd-tool/meson.options new file mode 100644 index 00000000..a2f7bbc3 --- /dev/null +++ b/vpd-tool/meson.options @@ -0,0 +1 @@ +option('INVENTORY_JSON_SYM_LINK', type: 'string', value: '/var/lib/vpd/vpd_inventory.json', description: 'Symbolic link to vpd inventory json.')