blob: e0182465f1ce8ef72c2ce50d3905928793db7c3a [file] [log] [blame]
George Liu6f777cd2021-06-10 09:16:28 +08001#include "occ_dbus.hpp"
2
3#include "utils.hpp"
4
Chris Cain37abe9b2024-10-31 17:20:31 -05005#include <phosphor-logging/lg2.hpp>
George Liu6f777cd2021-06-10 09:16:28 +08006
George Liub5ca1012021-09-10 12:53:11 +08007#include <iostream>
8
George Liu6f777cd2021-06-10 09:16:28 +08009namespace open_power
10{
11namespace occ
12{
13namespace dbus
14{
15
Matt Spinler5901abd2021-09-23 13:50:03 -050016using namespace std::string_literals;
17const auto defaultChassisPath =
18 "/xyz/openbmc_project/inventory/system/chassis"s;
19const auto chassisInterface = "xyz.openbmc_project.Inventory.Item.Chassis"s;
20
George Liu0ad9dd82021-06-22 14:46:14 +080021bool OccDBusSensors::setMaxValue(const std::string& path, double value)
George Liu6f777cd2021-06-10 09:16:28 +080022{
George Liu0ad9dd82021-06-22 14:46:14 +080023 if (path.empty())
24 {
25 return false;
26 }
27
George Liuf3a4a692021-12-28 13:59:51 +080028 if (!sensors.contains(path))
George Liu6f777cd2021-06-10 09:16:28 +080029 {
30 sensors.emplace(
31 path, std::make_unique<SensorIntf>(utils::getBus(), path.c_str()));
32 }
33
34 sensors.at(path)->maxValue(value);
George Liu0ad9dd82021-06-22 14:46:14 +080035 return true;
George Liu6f777cd2021-06-10 09:16:28 +080036}
37
38double OccDBusSensors::getMaxValue(const std::string& path) const
39{
40 if (sensors.find(path) != sensors.end())
41 {
42 return sensors.at(path)->maxValue();
43 }
44
45 throw std::invalid_argument("Failed to get MaxValue property.");
46}
47
George Liu0ad9dd82021-06-22 14:46:14 +080048bool OccDBusSensors::setMinValue(const std::string& path, double value)
George Liu6f777cd2021-06-10 09:16:28 +080049{
George Liu0ad9dd82021-06-22 14:46:14 +080050 if (path.empty())
51 {
52 return false;
53 }
54
George Liuf3a4a692021-12-28 13:59:51 +080055 if (!sensors.contains(path))
George Liu6f777cd2021-06-10 09:16:28 +080056 {
57 sensors.emplace(
58 path, std::make_unique<SensorIntf>(utils::getBus(), path.c_str()));
59 }
60
61 sensors.at(path)->minValue(value);
George Liu0ad9dd82021-06-22 14:46:14 +080062 return true;
George Liu6f777cd2021-06-10 09:16:28 +080063}
64
65double OccDBusSensors::getMinValue(const std::string& path) const
66{
67 if (sensors.find(path) != sensors.end())
68 {
69 return sensors.at(path)->minValue();
70 }
71
72 throw std::invalid_argument("Failed to get MinValue property.");
73}
74
George Liu0ad9dd82021-06-22 14:46:14 +080075bool OccDBusSensors::setValue(const std::string& path, double value)
George Liu6f777cd2021-06-10 09:16:28 +080076{
George Liu0ad9dd82021-06-22 14:46:14 +080077 if (path.empty())
78 {
79 return false;
80 }
81
George Liuf3a4a692021-12-28 13:59:51 +080082 if (!sensors.contains(path))
George Liu6f777cd2021-06-10 09:16:28 +080083 {
84 sensors.emplace(
85 path, std::make_unique<SensorIntf>(utils::getBus(), path.c_str()));
86 }
87
88 sensors.at(path)->value(value);
George Liu0ad9dd82021-06-22 14:46:14 +080089 return true;
George Liu6f777cd2021-06-10 09:16:28 +080090}
91
92double OccDBusSensors::getValue(const std::string& path) const
93{
94 if (sensors.find(path) != sensors.end())
95 {
96 return sensors.at(path)->value();
97 }
98
99 throw std::invalid_argument("Failed to get Value property.");
100}
101
George Liu0ad9dd82021-06-22 14:46:14 +0800102bool OccDBusSensors::setUnit(const std::string& path, const std::string& value)
George Liu6f777cd2021-06-10 09:16:28 +0800103{
George Liu0ad9dd82021-06-22 14:46:14 +0800104 if (path.empty())
105 {
106 return false;
107 }
108
George Liuf3a4a692021-12-28 13:59:51 +0800109 if (!sensors.contains(path))
George Liu6f777cd2021-06-10 09:16:28 +0800110 {
111 sensors.emplace(
112 path, std::make_unique<SensorIntf>(utils::getBus(), path.c_str()));
113 }
114
115 try
116 {
117 sensors.at(path)->unit(SensorIntf::convertUnitFromString(value));
118 }
119 catch (const std::exception& e)
120 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500121 lg2::error("set Unit propety failed: error={ERR}", "ERR", e.what());
George Liu0ad9dd82021-06-22 14:46:14 +0800122 return false;
George Liu6f777cd2021-06-10 09:16:28 +0800123 }
George Liu0ad9dd82021-06-22 14:46:14 +0800124
125 return true;
George Liu6f777cd2021-06-10 09:16:28 +0800126}
127
128std::string OccDBusSensors::getUnit(const std::string& path) const
129{
130 if (sensors.find(path) != sensors.end())
131 {
132 try
133 {
134 return SensorIntf::convertUnitToString(sensors.at(path)->unit());
135 }
136 catch (const std::exception& e)
137 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500138 lg2::error("get Unit propety failed: error={ERR}", "ERR", e.what());
George Liu6f777cd2021-06-10 09:16:28 +0800139 }
140 }
141
142 throw std::invalid_argument("Failed to get Unit property.");
143}
144
George Liu0ad9dd82021-06-22 14:46:14 +0800145bool OccDBusSensors::setOperationalStatus(const std::string& path, bool value)
George Liu6f777cd2021-06-10 09:16:28 +0800146{
George Liu0ad9dd82021-06-22 14:46:14 +0800147 if (path.empty())
148 {
149 return false;
150 }
151
George Liuf3a4a692021-12-28 13:59:51 +0800152 if (!operationalStatus.contains(path))
George Liu6f777cd2021-06-10 09:16:28 +0800153 {
154 operationalStatus.emplace(path, std::make_unique<OperationalStatusIntf>(
155 utils::getBus(), path.c_str()));
156 }
157
158 operationalStatus.at(path)->functional(value);
George Liu0ad9dd82021-06-22 14:46:14 +0800159 return true;
George Liu6f777cd2021-06-10 09:16:28 +0800160}
161
162bool OccDBusSensors::getOperationalStatus(const std::string& path) const
163{
164 if (operationalStatus.find(path) != operationalStatus.end())
165 {
166 return operationalStatus.at(path)->functional();
167 }
168
169 throw std::invalid_argument("Failed to get OperationalStatus property.");
170}
171
Chris Cain3523cc02024-10-30 17:19:09 -0500172void OccDBusSensors::setChassisAssociation(
173 const std::string& path, const std::vector<std::string>& fTypes)
Matt Spinler5901abd2021-09-23 13:50:03 -0500174{
175 using AssociationsEntry = std::tuple<std::string, std::string, std::string>;
176 using AssociationsProperty = std::vector<AssociationsEntry>;
177 using PropVariant = sdbusplus::xyz::openbmc_project::Association::server::
178 Definitions::PropertiesVariant;
179
180 if (chassisPath.empty())
181 {
182 chassisPath = getChassisPath();
183 }
184
Chris Cain3523cc02024-10-30 17:19:09 -0500185 AssociationsProperty associations;
186 for (const auto& fType : fTypes)
187 {
188 associations.emplace_back("chassis", fType, chassisPath);
189 }
Matt Spinler5901abd2021-09-23 13:50:03 -0500190 PropVariant value{std::move(associations)};
191
192 std::map<std::string, PropVariant> properties;
193 properties.emplace("Associations", std::move(value));
194
195 chassisAssociations.emplace(
196 path, std::make_unique<AssociationIntf>(utils::getBus(), path.c_str(),
197 properties));
198}
199
200std::string OccDBusSensors::getChassisPath()
201{
202 try
203 {
204 auto paths = utils::getSubtreePaths(std::vector{chassisInterface});
205
206 // For now, support either 1 chassis, or multiple as long as one
207 // of them has the standard name, which we will use. If this ever
208 // fails, then someone would have to figure out how to identify the
209 // chassis the OCCs are on.
210 if (paths.size() == 1)
211 {
212 return paths[0];
213 }
214 else if (std::find(paths.begin(), paths.end(), defaultChassisPath) ==
215 paths.end())
216 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500217 lg2::error("Could not find a chassis out of {NUM} chassis objects",
218 "NUM", paths.size());
Matt Spinler5901abd2021-09-23 13:50:03 -0500219 // Can't throw an exception here, the sdeventplus timer
220 // just catches it.
221 abort();
222 }
223 }
224 catch (const std::exception& e)
225 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500226 lg2::error("Error looking up chassis objects: {ERR}", "ERR", e.what());
Matt Spinler5901abd2021-09-23 13:50:03 -0500227 abort();
228 }
229
230 return defaultChassisPath;
231}
232
Matt Spinlerace67d82021-10-18 13:41:57 -0500233bool OccDBusSensors::hasDvfsTemp(const std::string& path) const
234{
235 return dvfsTemps.find(path) != dvfsTemps.end();
236}
237
238void OccDBusSensors::setDvfsTemp(const std::string& path, double value)
239{
Patrick Williamsd7542c82024-08-16 15:20:28 -0400240 dvfsTemps[path] =
241 std::make_unique<SensorIntf>(utils::getBus(), path.c_str());
Matt Spinlerace67d82021-10-18 13:41:57 -0500242 dvfsTemps[path]->value(value);
243}
244
George Liu6f777cd2021-06-10 09:16:28 +0800245} // namespace dbus
246} // namespace occ
247} // namespace open_power