xref: /openbmc/phosphor-bmc-code-mgmt/common/src/software_config.cpp (revision cc37235521b98887b31d07e8aedd0d86320eeb1d)
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 
SoftwareConfig(const std::string & objPath,uint32_t vendorIANA,const std::string & compatible,const std::string & configType,const std::string & name)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 {
20     std::regex reCompatible("([a-zA-Z0-9])+(\\.([a-zA-Z0-9])+)+");
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 
37 // NOLINTBEGIN(readability-static-accessed-through-instance)
getInventoryItemObjectPath(sdbusplus::async::context & ctx)38 sdbusplus::async::task<std::string> SoftwareConfig::getInventoryItemObjectPath(
39     sdbusplus::async::context& ctx)
40 // NOLINTEND(readability-static-accessed-through-instance)
41 {
42     std::vector<std::string> allInterfaces = {
43         "xyz.openbmc_project.Inventory.Item.Board",
44         "xyz.openbmc_project.Inventory.Item.Chassis",
45     };
46 
47     auto client = sdbusplus::client::xyz::openbmc_project::ObjectMapper<>(ctx)
48                       .service("xyz.openbmc_project.ObjectMapper")
49                       .path("/xyz/openbmc_project/object_mapper");
50     auto res = co_await client.get_sub_tree(
51         "/xyz/openbmc_project/inventory/system", 0, allInterfaces);
52 
53     for (auto& [path, v] : res)
54     {
55         debug("inventory item at path {PATH}", "PATH", path);
56 
57         // check if their path is a parent of our path
58         if (objectPath.starts_with(path))
59         {
60             debug("found associated inventory item for {NAME}: {PATH}", "NAME",
61                   configName, "PATH", path);
62             co_return path;
63         }
64     }
65 
66     error("could not find associated inventory item for {NAME}", "NAME",
67           configName);
68 
69     co_return "";
70 }
71