| Alexander Hansen | cc37235 | 2025-01-14 14:15:39 +0100 | [diff] [blame] | 1 | #include "common/include/software_config.hpp" | 
|  | 2 |  | 
|  | 3 | #include <phosphor-logging/lg2.hpp> | 
|  | 4 | #include <xyz/openbmc_project/ObjectMapper/client.hpp> | 
|  | 5 |  | 
|  | 6 | #include <regex> | 
|  | 7 | #include <stdexcept> | 
|  | 8 |  | 
|  | 9 | PHOSPHOR_LOG2_USING; | 
|  | 10 |  | 
|  | 11 | using namespace phosphor::software::config; | 
|  | 12 |  | 
|  | 13 | SoftwareConfig::SoftwareConfig(const std::string& objPath, uint32_t vendorIANA, | 
|  | 14 | const std::string& compatible, | 
|  | 15 | const std::string& configType, | 
|  | 16 | const std::string& name) : | 
|  | 17 | objectPath(objPath), configName(name), configType(configType), | 
|  | 18 | vendorIANA(vendorIANA), compatibleHardware(compatible) | 
|  | 19 | { | 
| Daniel Hsu | 886e14e | 2025-06-09 18:44:22 +0800 | [diff] [blame] | 20 | std::regex reCompatible("([a-zA-Z0-9_])+(\\.([a-zA-Z0-9_])+)+"); | 
| Alexander Hansen | cc37235 | 2025-01-14 14:15:39 +0100 | [diff] [blame] | 21 | std::cmatch m; | 
|  | 22 |  | 
|  | 23 | if (name.empty()) | 
|  | 24 | { | 
|  | 25 | throw std::invalid_argument( | 
|  | 26 | "invalid EM config 'Name' string: '" + name + "'"); | 
|  | 27 | } | 
|  | 28 |  | 
|  | 29 | // check compatible string with regex | 
|  | 30 | if (!std::regex_match(compatible.c_str(), m, reCompatible)) | 
|  | 31 | { | 
|  | 32 | throw std::invalid_argument( | 
|  | 33 | "invalid compatible string: '" + compatible + "'"); | 
|  | 34 | } | 
|  | 35 | } | 
|  | 36 |  | 
| Alexander Hansen | cc37235 | 2025-01-14 14:15:39 +0100 | [diff] [blame] | 37 | sdbusplus::async::task<std::string> SoftwareConfig::getInventoryItemObjectPath( | 
|  | 38 | sdbusplus::async::context& ctx) | 
| Alexander Hansen | cc37235 | 2025-01-14 14:15:39 +0100 | [diff] [blame] | 39 | { | 
|  | 40 | std::vector<std::string> allInterfaces = { | 
|  | 41 | "xyz.openbmc_project.Inventory.Item.Board", | 
|  | 42 | "xyz.openbmc_project.Inventory.Item.Chassis", | 
|  | 43 | }; | 
|  | 44 |  | 
|  | 45 | auto client = sdbusplus::client::xyz::openbmc_project::ObjectMapper<>(ctx) | 
|  | 46 | .service("xyz.openbmc_project.ObjectMapper") | 
|  | 47 | .path("/xyz/openbmc_project/object_mapper"); | 
|  | 48 | auto res = co_await client.get_sub_tree( | 
|  | 49 | "/xyz/openbmc_project/inventory/system", 0, allInterfaces); | 
|  | 50 |  | 
|  | 51 | for (auto& [path, v] : res) | 
|  | 52 | { | 
|  | 53 | debug("inventory item at path {PATH}", "PATH", path); | 
|  | 54 |  | 
|  | 55 | // check if their path is a parent of our path | 
|  | 56 | if (objectPath.starts_with(path)) | 
|  | 57 | { | 
|  | 58 | debug("found associated inventory item for {NAME}: {PATH}", "NAME", | 
|  | 59 | configName, "PATH", path); | 
|  | 60 | co_return path; | 
|  | 61 | } | 
|  | 62 | } | 
|  | 63 |  | 
|  | 64 | error("could not find associated inventory item for {NAME}", "NAME", | 
|  | 65 | configName); | 
|  | 66 |  | 
|  | 67 | co_return ""; | 
|  | 68 | } |