blob: 8bc6b06a2ca49a61cd4eb586f9d6204793a04307 [file] [log] [blame]
Kamalkumar Patel3c50c822024-01-30 07:14:40 -06001#include "platform_config.hpp"
2
Riya Dixit89644442024-03-31 05:39:59 -05003#include <phosphor-logging/lg2.hpp>
4
5PHOSPHOR_LOG2_USING;
6
Kamalkumar Patel3c50c822024-01-30 07:14:40 -06007namespace pldm
8{
9namespace responder
10{
11
12namespace platform_config
13{
14/** @brief callback function invoked when interfaces get added from
15 * Entity manager
16 *
17 * @param[in] msg - Data associated with subscribed signal
18 */
19void Handler::systemCompatibleCallback(sdbusplus::message_t& msg)
20{
21 sdbusplus::message::object_path path;
22
23 pldm::utils::InterfaceMap interfaceMap;
24
25 msg.read(path, interfaceMap);
26
27 if (!interfaceMap.contains(compatibleInterface))
28 {
29 return;
30 }
31 // Get the "Name" property value of the
32 // "xyz.openbmc_project.Inventory.Decorator.Compatible" interface
33 const auto& properties = interfaceMap.at(compatibleInterface);
34
35 if (!properties.contains(namesProperty))
36 {
37 return;
38 }
39 auto names =
40 std::get<pldm::utils::Interfaces>(properties.at(namesProperty));
41
42 std::string systemType;
43 if (!names.empty())
44 {
45 // get only the first system type
46 systemType = names.front();
Archana Kakani62dd8ff2024-02-12 10:00:40 -060047 if (sysTypeCallback)
48 {
Archana Kakani46f352e2024-03-17 08:21:08 -050049 sysTypeCallback(systemType, true);
Archana Kakani62dd8ff2024-02-12 10:00:40 -060050 }
Kamalkumar Patel3c50c822024-01-30 07:14:40 -060051 }
52
53 if (!systemType.empty())
54 {
55 systemCompatibleMatchCallBack.reset();
56 }
57}
58
59/** @brief Method to get the system type information
60 *
61 * @return - the system type information
62 */
63std::optional<std::filesystem::path> Handler::getPlatformName()
64{
65 if (!systemType.empty())
66 {
67 return fs::path{systemType};
68 }
69
70 namespace fs = std::filesystem;
Kamalkumar Patel2edc34b2024-02-23 03:27:01 -060071 static const std::string entityMangerService =
72 "xyz.openbmc_project.EntityManager";
Kamalkumar Patel3c50c822024-01-30 07:14:40 -060073
74 static constexpr auto searchpath = "/xyz/openbmc_project/";
75 int depth = 0;
76 std::vector<std::string> systemCompatible = {compatibleInterface};
Kamalkumar Patel3c50c822024-01-30 07:14:40 -060077
Kamalkumar Patel2edc34b2024-02-23 03:27:01 -060078 try
Kamalkumar Patel3c50c822024-01-30 07:14:40 -060079 {
Kamalkumar Patel2edc34b2024-02-23 03:27:01 -060080 pldm::utils::GetSubTreeResponse response =
81 pldm::utils::DBusHandler().getSubtree(searchpath, depth,
82 systemCompatible);
83 auto& bus = pldm::utils::DBusHandler::getBus();
84
85 for (const auto& [objectPath, serviceMap] : response)
Kamalkumar Patel3c50c822024-01-30 07:14:40 -060086 {
Kamalkumar Patel2edc34b2024-02-23 03:27:01 -060087 try
Kamalkumar Patel3c50c822024-01-30 07:14:40 -060088 {
Kamalkumar Patel2edc34b2024-02-23 03:27:01 -060089 auto record = std::find_if(
90 serviceMap.begin(), serviceMap.end(),
91 [](auto map) { return map.first == entityMangerService; });
92
93 if (record != serviceMap.end())
94 {
95 auto method = bus.new_method_call(
96 entityMangerService.c_str(), objectPath.c_str(),
97 "org.freedesktop.DBus.Properties", "Get");
98 method.append(compatibleInterface, namesProperty);
99 auto propSystemList =
100 bus.call(method, dbusTimeout).unpack<PropertyValue>();
101 auto systemList =
102 std::get<std::vector<std::string>>(propSystemList);
103
104 if (!systemList.empty())
105 {
106 systemType = systemList.at(0);
107 // once systemtype received,then resetting a callback
108 systemCompatibleMatchCallBack.reset();
109 return fs::path{systemType};
110 }
111 }
112 }
113 catch (const std::exception& e)
114 {
115 error(
Riya Dixit89644442024-03-31 05:39:59 -0500116 "Failed to get Names property at '{PATH}' on interface '{INTERFACE}', error - {ERROR}",
Kamalkumar Patel2edc34b2024-02-23 03:27:01 -0600117 "PATH", objectPath, "INTERFACE", compatibleInterface,
118 "ERROR", e);
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600119 }
120 }
Kamalkumar Patel2edc34b2024-02-23 03:27:01 -0600121 }
122 catch (const std::exception& e)
123 {
Riya Dixit89644442024-03-31 05:39:59 -0500124 error(
125 "Failed to make a d-bus call to get platform name, error - {ERROR}",
126 "ERROR", e);
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600127 }
128 return std::nullopt;
129}
130
Archana Kakani62dd8ff2024-02-12 10:00:40 -0600131void Handler::registerSystemTypeCallback(SystemTypeCallback callback)
132{
133 sysTypeCallback = callback;
134}
135
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600136} // namespace platform_config
137
138} // namespace responder
139
140} // namespace pldm