blob: fb2ff27e0a2f28d450ae8c004d9e877638fe626a [file] [log] [blame]
George Liu6f777cd2021-06-10 09:16:28 +08001#include "occ_dbus.hpp"
2
3#include "utils.hpp"
4
George Liu6f777cd2021-06-10 09:16:28 +08005#include <phosphor-logging/log.hpp>
6
Patrick Williams48002492024-02-13 21:43:32 -06007#include <format>
George Liub5ca1012021-09-10 12:53:11 +08008#include <iostream>
9
George Liu6f777cd2021-06-10 09:16:28 +080010namespace open_power
11{
12namespace occ
13{
14namespace dbus
15{
16
17using namespace phosphor::logging;
Matt Spinler5901abd2021-09-23 13:50:03 -050018using namespace std::string_literals;
19const auto defaultChassisPath =
20 "/xyz/openbmc_project/inventory/system/chassis"s;
21const auto chassisInterface = "xyz.openbmc_project.Inventory.Item.Chassis"s;
22
George Liu0ad9dd82021-06-22 14:46:14 +080023bool OccDBusSensors::setMaxValue(const std::string& path, double value)
George Liu6f777cd2021-06-10 09:16:28 +080024{
George Liu0ad9dd82021-06-22 14:46:14 +080025 if (path.empty())
26 {
27 return false;
28 }
29
George Liuf3a4a692021-12-28 13:59:51 +080030 if (!sensors.contains(path))
George Liu6f777cd2021-06-10 09:16:28 +080031 {
32 sensors.emplace(
33 path, std::make_unique<SensorIntf>(utils::getBus(), path.c_str()));
34 }
35
36 sensors.at(path)->maxValue(value);
George Liu0ad9dd82021-06-22 14:46:14 +080037 return true;
George Liu6f777cd2021-06-10 09:16:28 +080038}
39
40double OccDBusSensors::getMaxValue(const std::string& path) const
41{
42 if (sensors.find(path) != sensors.end())
43 {
44 return sensors.at(path)->maxValue();
45 }
46
47 throw std::invalid_argument("Failed to get MaxValue property.");
48}
49
George Liu0ad9dd82021-06-22 14:46:14 +080050bool OccDBusSensors::setMinValue(const std::string& path, double value)
George Liu6f777cd2021-06-10 09:16:28 +080051{
George Liu0ad9dd82021-06-22 14:46:14 +080052 if (path.empty())
53 {
54 return false;
55 }
56
George Liuf3a4a692021-12-28 13:59:51 +080057 if (!sensors.contains(path))
George Liu6f777cd2021-06-10 09:16:28 +080058 {
59 sensors.emplace(
60 path, std::make_unique<SensorIntf>(utils::getBus(), path.c_str()));
61 }
62
63 sensors.at(path)->minValue(value);
George Liu0ad9dd82021-06-22 14:46:14 +080064 return true;
George Liu6f777cd2021-06-10 09:16:28 +080065}
66
67double OccDBusSensors::getMinValue(const std::string& path) const
68{
69 if (sensors.find(path) != sensors.end())
70 {
71 return sensors.at(path)->minValue();
72 }
73
74 throw std::invalid_argument("Failed to get MinValue property.");
75}
76
George Liu0ad9dd82021-06-22 14:46:14 +080077bool OccDBusSensors::setValue(const std::string& path, double value)
George Liu6f777cd2021-06-10 09:16:28 +080078{
George Liu0ad9dd82021-06-22 14:46:14 +080079 if (path.empty())
80 {
81 return false;
82 }
83
George Liuf3a4a692021-12-28 13:59:51 +080084 if (!sensors.contains(path))
George Liu6f777cd2021-06-10 09:16:28 +080085 {
86 sensors.emplace(
87 path, std::make_unique<SensorIntf>(utils::getBus(), path.c_str()));
88 }
89
90 sensors.at(path)->value(value);
George Liu0ad9dd82021-06-22 14:46:14 +080091 return true;
George Liu6f777cd2021-06-10 09:16:28 +080092}
93
94double OccDBusSensors::getValue(const std::string& path) const
95{
96 if (sensors.find(path) != sensors.end())
97 {
98 return sensors.at(path)->value();
99 }
100
101 throw std::invalid_argument("Failed to get Value property.");
102}
103
George Liu0ad9dd82021-06-22 14:46:14 +0800104bool OccDBusSensors::setUnit(const std::string& path, const std::string& value)
George Liu6f777cd2021-06-10 09:16:28 +0800105{
George Liu0ad9dd82021-06-22 14:46:14 +0800106 if (path.empty())
107 {
108 return false;
109 }
110
George Liuf3a4a692021-12-28 13:59:51 +0800111 if (!sensors.contains(path))
George Liu6f777cd2021-06-10 09:16:28 +0800112 {
113 sensors.emplace(
114 path, std::make_unique<SensorIntf>(utils::getBus(), path.c_str()));
115 }
116
117 try
118 {
119 sensors.at(path)->unit(SensorIntf::convertUnitFromString(value));
120 }
121 catch (const std::exception& e)
122 {
123 log<level::ERR>("set Unit propety failed", entry("ERROR=%s", e.what()));
George Liu0ad9dd82021-06-22 14:46:14 +0800124 return false;
George Liu6f777cd2021-06-10 09:16:28 +0800125 }
George Liu0ad9dd82021-06-22 14:46:14 +0800126
127 return true;
George Liu6f777cd2021-06-10 09:16:28 +0800128}
129
130std::string OccDBusSensors::getUnit(const std::string& path) const
131{
132 if (sensors.find(path) != sensors.end())
133 {
134 try
135 {
136 return SensorIntf::convertUnitToString(sensors.at(path)->unit());
137 }
138 catch (const std::exception& e)
139 {
140 log<level::ERR>("get Unit propety failed",
141 entry("ERROR=%s", e.what()));
142 }
143 }
144
145 throw std::invalid_argument("Failed to get Unit property.");
146}
147
George Liu0ad9dd82021-06-22 14:46:14 +0800148bool OccDBusSensors::setOperationalStatus(const std::string& path, bool value)
George Liu6f777cd2021-06-10 09:16:28 +0800149{
George Liu0ad9dd82021-06-22 14:46:14 +0800150 if (path.empty())
151 {
152 return false;
153 }
154
George Liuf3a4a692021-12-28 13:59:51 +0800155 if (!operationalStatus.contains(path))
George Liu6f777cd2021-06-10 09:16:28 +0800156 {
157 operationalStatus.emplace(path, std::make_unique<OperationalStatusIntf>(
158 utils::getBus(), path.c_str()));
159 }
160
161 operationalStatus.at(path)->functional(value);
George Liu0ad9dd82021-06-22 14:46:14 +0800162 return true;
George Liu6f777cd2021-06-10 09:16:28 +0800163}
164
165bool OccDBusSensors::getOperationalStatus(const std::string& path) const
166{
167 if (operationalStatus.find(path) != operationalStatus.end())
168 {
169 return operationalStatus.at(path)->functional();
170 }
171
172 throw std::invalid_argument("Failed to get OperationalStatus property.");
173}
174
Matt Spinler5901abd2021-09-23 13:50:03 -0500175void OccDBusSensors::setChassisAssociation(const std::string& path)
176{
177 using AssociationsEntry = std::tuple<std::string, std::string, std::string>;
178 using AssociationsProperty = std::vector<AssociationsEntry>;
179 using PropVariant = sdbusplus::xyz::openbmc_project::Association::server::
180 Definitions::PropertiesVariant;
181
182 if (chassisPath.empty())
183 {
184 chassisPath = getChassisPath();
185 }
186
187 AssociationsProperty associations{
188 AssociationsEntry{"chassis", "all_sensors", chassisPath}};
189 PropVariant value{std::move(associations)};
190
191 std::map<std::string, PropVariant> properties;
192 properties.emplace("Associations", std::move(value));
193
194 chassisAssociations.emplace(
195 path, std::make_unique<AssociationIntf>(utils::getBus(), path.c_str(),
196 properties));
197}
198
199std::string OccDBusSensors::getChassisPath()
200{
201 try
202 {
203 auto paths = utils::getSubtreePaths(std::vector{chassisInterface});
204
205 // For now, support either 1 chassis, or multiple as long as one
206 // of them has the standard name, which we will use. If this ever
207 // fails, then someone would have to figure out how to identify the
208 // chassis the OCCs are on.
209 if (paths.size() == 1)
210 {
211 return paths[0];
212 }
213 else if (std::find(paths.begin(), paths.end(), defaultChassisPath) ==
214 paths.end())
215 {
216 log<level::ERR>(
Patrick Williams48002492024-02-13 21:43:32 -0600217 std::format(
Matt Spinler5901abd2021-09-23 13:50:03 -0500218 "Could not find a chassis out of {} chassis objects",
219 paths.size())
220 .c_str());
221 // Can't throw an exception here, the sdeventplus timer
222 // just catches it.
223 abort();
224 }
225 }
226 catch (const std::exception& e)
227 {
228 log<level::ERR>(
Patrick Williams48002492024-02-13 21:43:32 -0600229 std::format("Error looking up chassis objects: {}", e.what())
Matt Spinler5901abd2021-09-23 13:50:03 -0500230 .c_str());
231 abort();
232 }
233
234 return defaultChassisPath;
235}
236
Matt Spinlerace67d82021-10-18 13:41:57 -0500237bool OccDBusSensors::hasDvfsTemp(const std::string& path) const
238{
239 return dvfsTemps.find(path) != dvfsTemps.end();
240}
241
242void OccDBusSensors::setDvfsTemp(const std::string& path, double value)
243{
Patrick Williamsa49c9872023-05-10 07:50:35 -0500244 dvfsTemps[path] = std::make_unique<SensorIntf>(utils::getBus(),
245 path.c_str());
Matt Spinlerace67d82021-10-18 13:41:57 -0500246 dvfsTemps[path]->value(value);
247}
248
George Liu6f777cd2021-06-10 09:16:28 +0800249} // namespace dbus
250} // namespace occ
251} // namespace open_power