blob: 76dad690092aa8a5399b9a880d0404cf4e83e21d [file] [log] [blame]
Patrick Venture3a5071a2018-09-12 13:27:42 -07001#include "sensordatahandler.hpp"
2
3#include "sensorhandler.hpp"
Patrick Venture3a5071a2018-09-12 13:27:42 -07004
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -05005#include <bitset>
Vernon Mauerybdda8002019-02-26 10:18:51 -08006#include <filesystem>
Vernon Mauery33250242019-03-12 16:49:26 -07007#include <ipmid/types.hpp>
Vernon Mauery6a98fe72019-03-11 15:57:48 -07008#include <ipmid/utils.hpp>
William A. Kennington III4c008022018-10-12 17:18:14 -07009#include <optional>
Patrick Venture3a5071a2018-09-12 13:27:42 -070010#include <phosphor-logging/elog-errors.hpp>
11#include <phosphor-logging/log.hpp>
William A. Kennington III4c008022018-10-12 17:18:14 -070012#include <sdbusplus/message/types.hpp>
Patrick Venture3a5071a2018-09-12 13:27:42 -070013#include <xyz/openbmc_project/Common/error.hpp>
14
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -050015namespace ipmi
16{
17namespace sensor
18{
19
20using namespace phosphor::logging;
21using InternalFailure =
22 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
23
24static constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
25static constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
26static constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper";
27
28/** @brief get the D-Bus service and service path
29 * @param[in] bus - The Dbus bus object
30 * @param[in] interface - interface to the service
31 * @param[in] path - interested path in the list of objects
32 * @return pair of service path and service
33 */
34ServicePath getServiceAndPath(sdbusplus::bus::bus& bus,
35 const std::string& interface,
36 const std::string& path)
37{
38 auto depth = 0;
Patrick Venture0b02be92018-08-31 11:55:55 -070039 auto mapperCall = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
40 MAPPER_INTERFACE, "GetSubTree");
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -050041 mapperCall.append("/");
42 mapperCall.append(depth);
43 mapperCall.append(std::vector<Interface>({interface}));
44
45 auto mapperResponseMsg = bus.call(mapperCall);
46 if (mapperResponseMsg.is_method_error())
47 {
Dhruvaraj Subhashchandran18e99992017-08-09 09:10:47 -050048 log<level::ERR>("Mapper GetSubTree failed",
Joseph Reynolds510eb9c2018-05-30 11:51:28 -050049 entry("PATH=%s", path.c_str()),
50 entry("INTERFACE=%s", interface.c_str()));
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -050051 elog<InternalFailure>();
52 }
53
54 MapperResponseType mapperResponse;
55 mapperResponseMsg.read(mapperResponse);
56 if (mapperResponse.empty())
57 {
Dhruvaraj Subhashchandran18e99992017-08-09 09:10:47 -050058 log<level::ERR>("Invalid mapper response",
Joseph Reynolds510eb9c2018-05-30 11:51:28 -050059 entry("PATH=%s", path.c_str()),
60 entry("INTERFACE=%s", interface.c_str()));
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -050061 elog<InternalFailure>();
62 }
63
64 if (path.empty())
65 {
Patrick Venture0b02be92018-08-31 11:55:55 -070066 // Get the first one if the path is not in list.
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -050067 return std::make_pair(mapperResponse.begin()->first,
68 mapperResponse.begin()->second.begin()->first);
69 }
70 const auto& iter = mapperResponse.find(path);
71 if (iter == mapperResponse.end())
72 {
Gunnar Millsc9fa69e2018-04-08 16:35:25 -050073 log<level::ERR>("Couldn't find D-Bus path",
Joseph Reynolds510eb9c2018-05-30 11:51:28 -050074 entry("PATH=%s", path.c_str()),
75 entry("INTERFACE=%s", interface.c_str()));
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -050076 elog<InternalFailure>();
77 }
78 return std::make_pair(iter->first, iter->second.begin()->first);
79}
80
81AssertionSet getAssertionSet(const SetSensorReadingReq& cmdData)
82{
83 Assertion assertionStates =
84 (static_cast<Assertion>(cmdData.assertOffset8_14)) << 8 |
85 cmdData.assertOffset0_7;
86 Deassertion deassertionStates =
87 (static_cast<Deassertion>(cmdData.deassertOffset8_14)) << 8 |
88 cmdData.deassertOffset0_7;
89 return std::make_pair(assertionStates, deassertionStates);
90}
91
92ipmi_ret_t updateToDbus(IpmiUpdateData& msg)
93{
94 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
95 try
96 {
97 auto serviceResponseMsg = bus.call(msg);
98 if (serviceResponseMsg.is_method_error())
99 {
100 log<level::ERR>("Error in D-Bus call");
101 return IPMI_CC_UNSPECIFIED_ERROR;
102 }
103 }
104 catch (InternalFailure& e)
105 {
106 commit<InternalFailure>();
107 return IPMI_CC_UNSPECIFIED_ERROR;
108 }
109 return IPMI_CC_OK;
110}
111
Tom Joseph816e92b2017-09-06 19:23:00 +0530112namespace get
113{
114
Tom Josephb0adbcd2018-01-24 11:51:29 +0530115SensorName nameParentLeaf(const Info& sensorInfo)
116{
117 const auto pos = sensorInfo.sensorPath.find_last_of('/');
118 const auto leaf = sensorInfo.sensorPath.substr(pos + 1);
119
120 const auto remaining = sensorInfo.sensorPath.substr(0, pos);
121
122 const auto parentPos = remaining.find_last_of('/');
123 auto parent = remaining.substr(parentPos + 1);
124
125 parent += "_" + leaf;
126 return parent;
127}
128
Tom Joseph816e92b2017-09-06 19:23:00 +0530129GetSensorResponse mapDbusToAssertion(const Info& sensorInfo,
130 const InstancePath& path,
131 const DbusInterface& interface)
132{
133 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
Patrick Venture0b02be92018-08-31 11:55:55 -0700134 GetSensorResponse response{};
Tom Joseph816e92b2017-09-06 19:23:00 +0530135 auto responseData = reinterpret_cast<GetReadingResponse*>(response.data());
136
137 auto service = ipmi::getService(bus, interface, path);
138
139 const auto& interfaceList = sensorInfo.propertyInterfaces;
140
141 for (const auto& interface : interfaceList)
142 {
143 for (const auto& property : interface.second)
144 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700145 auto propValue = ipmi::getDbusProperty(
146 bus, service, path, interface.first, property.first);
Tom Joseph816e92b2017-09-06 19:23:00 +0530147
Dhruvaraj Subhashchandrane245e4e2017-10-03 03:58:05 -0500148 for (const auto& value : std::get<OffsetValueMap>(property.second))
Tom Joseph816e92b2017-09-06 19:23:00 +0530149 {
150 if (propValue == value.second.assert)
151 {
152 setOffset(value.first, responseData);
153 break;
154 }
Tom Joseph816e92b2017-09-06 19:23:00 +0530155 }
156 }
157 }
158
159 return response;
160}
161
162GetSensorResponse assertion(const Info& sensorInfo)
163{
Patrick Venture0b02be92018-08-31 11:55:55 -0700164 return mapDbusToAssertion(sensorInfo, sensorInfo.sensorPath,
Tom Joseph816e92b2017-09-06 19:23:00 +0530165 sensorInfo.sensorInterface);
166}
167
Tom Josephe4014fc2017-09-06 23:57:36 +0530168GetSensorResponse eventdata2(const Info& sensorInfo)
169{
170 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
Patrick Venture0b02be92018-08-31 11:55:55 -0700171 GetSensorResponse response{};
Tom Josephe4014fc2017-09-06 23:57:36 +0530172 auto responseData = reinterpret_cast<GetReadingResponse*>(response.data());
173
Patrick Venture0b02be92018-08-31 11:55:55 -0700174 auto service = ipmi::getService(bus, sensorInfo.sensorInterface,
Tom Josephe4014fc2017-09-06 23:57:36 +0530175 sensorInfo.sensorPath);
176
177 const auto& interfaceList = sensorInfo.propertyInterfaces;
178
179 for (const auto& interface : interfaceList)
180 {
181 for (const auto& property : interface.second)
182 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700183 auto propValue =
184 ipmi::getDbusProperty(bus, service, sensorInfo.sensorPath,
185 interface.first, property.first);
Tom Josephe4014fc2017-09-06 23:57:36 +0530186
Dhruvaraj Subhashchandrane245e4e2017-10-03 03:58:05 -0500187 for (const auto& value : std::get<OffsetValueMap>(property.second))
Tom Josephe4014fc2017-09-06 23:57:36 +0530188 {
189 if (propValue == value.second.assert)
190 {
191 setReading(value.first, responseData);
192 break;
193 }
194 }
195 }
196 }
197
198 return response;
199}
200
Patrick Venture0b02be92018-08-31 11:55:55 -0700201} // namespace get
Tom Joseph816e92b2017-09-06 19:23:00 +0530202
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500203namespace set
204{
205
206IpmiUpdateData makeDbusMsg(const std::string& updateInterface,
207 const std::string& sensorPath,
208 const std::string& command,
209 const std::string& sensorInterface)
210{
211 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
212 using namespace std::string_literals;
213
Patrick Venture0b02be92018-08-31 11:55:55 -0700214 auto dbusService = getService(bus, sensorInterface, sensorPath);
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500215
Patrick Venture0b02be92018-08-31 11:55:55 -0700216 return bus.new_method_call(dbusService.c_str(), sensorPath.c_str(),
217 updateInterface.c_str(), command.c_str());
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500218}
219
Patrick Venture0b02be92018-08-31 11:55:55 -0700220ipmi_ret_t eventdata(const SetSensorReadingReq& cmdData, const Info& sensorInfo,
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500221 uint8_t data)
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500222{
Patrick Venture0b02be92018-08-31 11:55:55 -0700223 auto msg =
224 makeDbusMsg("org.freedesktop.DBus.Properties", sensorInfo.sensorPath,
225 "Set", sensorInfo.sensorInterface);
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500226
227 const auto& interface = sensorInfo.propertyInterfaces.begin();
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500228 msg.append(interface->first);
229 for (const auto& property : interface->second)
230 {
231 msg.append(property.first);
Dhruvaraj Subhashchandrane245e4e2017-10-03 03:58:05 -0500232 const auto& iter = std::get<OffsetValueMap>(property.second).find(data);
233 if (iter == std::get<OffsetValueMap>(property.second).end())
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500234 {
235 log<level::ERR>("Invalid event data");
236 return IPMI_CC_PARM_OUT_OF_RANGE;
237 }
238 msg.append(iter->second.assert);
239 }
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500240 return updateToDbus(msg);
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500241}
242
Patrick Venture0b02be92018-08-31 11:55:55 -0700243ipmi_ret_t assertion(const SetSensorReadingReq& cmdData, const Info& sensorInfo)
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500244{
Brad Bishop06a0abf2018-02-26 20:46:00 -0500245 std::bitset<16> assertionSet(getAssertionSet(cmdData).first);
246 std::bitset<16> deassertionSet(getAssertionSet(cmdData).second);
247 auto bothSet = assertionSet ^ deassertionSet;
248
249 const auto& interface = sensorInfo.propertyInterfaces.begin();
250
251 for (const auto& property : interface->second)
252 {
William A. Kennington III4c008022018-10-12 17:18:14 -0700253 std::optional<Value> tmp;
Brad Bishop06a0abf2018-02-26 20:46:00 -0500254 for (const auto& value : std::get<OffsetValueMap>(property.second))
255 {
256 if (bothSet.size() <= value.first || !bothSet.test(value.first))
257 {
258 // A BIOS shouldn't do this but ignore if they do.
259 continue;
260 }
261
262 if (assertionSet.test(value.first))
263 {
264 tmp = value.second.assert;
265 break;
266 }
267 if (deassertionSet.test(value.first))
268 {
269 tmp = value.second.deassert;
270 break;
271 }
272 }
273
William A. Kennington III4c008022018-10-12 17:18:14 -0700274 if (tmp)
Brad Bishop06a0abf2018-02-26 20:46:00 -0500275 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700276 auto msg = makeDbusMsg("org.freedesktop.DBus.Properties",
277 sensorInfo.sensorPath, "Set",
278 sensorInfo.sensorInterface);
Brad Bishop06a0abf2018-02-26 20:46:00 -0500279 msg.append(interface->first);
280 msg.append(property.first);
William A. Kennington III4c008022018-10-12 17:18:14 -0700281 msg.append(*tmp);
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500282
Brad Bishop06a0abf2018-02-26 20:46:00 -0500283 auto rc = updateToDbus(msg);
284 if (rc)
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500285 {
Brad Bishop06a0abf2018-02-26 20:46:00 -0500286 return rc;
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500287 }
288 }
289 }
Brad Bishop06a0abf2018-02-26 20:46:00 -0500290
291 return IPMI_CC_OK;
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500292}
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500293
Patrick Venture0b02be92018-08-31 11:55:55 -0700294} // namespace set
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500295
296namespace notify
297{
298
299IpmiUpdateData makeDbusMsg(const std::string& updateInterface,
300 const std::string& sensorPath,
301 const std::string& command,
302 const std::string& sensorInterface)
303{
304 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
305 using namespace std::string_literals;
306
Dhruvaraj Subhashchandranf915f852017-09-14 07:01:48 -0500307 static const auto dbusPath = "/xyz/openbmc_project/inventory"s;
308 std::string dbusService = ipmi::getService(bus, updateInterface, dbusPath);
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500309
Patrick Venture0b02be92018-08-31 11:55:55 -0700310 return bus.new_method_call(dbusService.c_str(), dbusPath.c_str(),
311 updateInterface.c_str(), command.c_str());
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500312}
313
Patrick Venture0b02be92018-08-31 11:55:55 -0700314ipmi_ret_t assertion(const SetSensorReadingReq& cmdData, const Info& sensorInfo)
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500315{
Patrick Venture0b02be92018-08-31 11:55:55 -0700316 auto msg = makeDbusMsg(sensorInfo.sensorInterface, sensorInfo.sensorPath,
317 "Notify", sensorInfo.sensorInterface);
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500318
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500319 std::bitset<16> assertionSet(getAssertionSet(cmdData).first);
320 std::bitset<16> deassertionSet(getAssertionSet(cmdData).second);
321 ipmi::sensor::ObjectMap objects;
322 ipmi::sensor::InterfaceMap interfaces;
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500323 for (const auto& interface : sensorInfo.propertyInterfaces)
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500324 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700325 // For a property like functional state the result will be
326 // calculated based on the true value of all conditions.
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500327 for (const auto& property : interface.second)
328 {
329 ipmi::sensor::PropertyMap props;
330 bool valid = false;
Dhruvaraj Subhashchandrane245e4e2017-10-03 03:58:05 -0500331 auto result = true;
332 for (const auto& value : std::get<OffsetValueMap>(property.second))
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500333 {
334 if (assertionSet.test(value.first))
335 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700336 // Skip update if skipOn is ASSERT
Dhruvaraj Subhashchandrane84841c2017-08-22 07:40:27 -0500337 if (SkipAssertion::ASSERT == value.second.skip)
338 {
339 return IPMI_CC_OK;
340 }
Vernon Maueryf442e112019-04-09 11:44:36 -0700341 result = result && std::get<bool>(value.second.assert);
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500342 valid = true;
343 }
344 else if (deassertionSet.test(value.first))
345 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700346 // Skip update if skipOn is DEASSERT
Dhruvaraj Subhashchandrane84841c2017-08-22 07:40:27 -0500347 if (SkipAssertion::DEASSERT == value.second.skip)
348 {
349 return IPMI_CC_OK;
350 }
Vernon Maueryf442e112019-04-09 11:44:36 -0700351 result = result && std::get<bool>(value.second.deassert);
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500352 valid = true;
353 }
354 }
Dhruvaraj Subhashchandrane245e4e2017-10-03 03:58:05 -0500355 for (const auto& value :
Patrick Venture0b02be92018-08-31 11:55:55 -0700356 std::get<PreReqOffsetValueMap>(property.second))
Dhruvaraj Subhashchandrane245e4e2017-10-03 03:58:05 -0500357 {
358 if (assertionSet.test(value.first))
359 {
Vernon Maueryf442e112019-04-09 11:44:36 -0700360 result = result && std::get<bool>(value.second.assert);
Dhruvaraj Subhashchandrane245e4e2017-10-03 03:58:05 -0500361 }
362 else if (deassertionSet.test(value.first))
363 {
Vernon Maueryf442e112019-04-09 11:44:36 -0700364 result = result && std::get<bool>(value.second.deassert);
Dhruvaraj Subhashchandrane245e4e2017-10-03 03:58:05 -0500365 }
366 }
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500367 if (valid)
368 {
Dhruvaraj Subhashchandrane245e4e2017-10-03 03:58:05 -0500369 props.emplace(property.first, result);
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500370 interfaces.emplace(interface.first, std::move(props));
371 }
372 }
373 }
Dhruvaraj Subhashchandrane84841c2017-08-22 07:40:27 -0500374
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500375 objects.emplace(sensorInfo.sensorPath, std::move(interfaces));
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500376 msg.append(std::move(objects));
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500377 return updateToDbus(msg);
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500378}
Tom Joseph816e92b2017-09-06 19:23:00 +0530379
Patrick Venture0b02be92018-08-31 11:55:55 -0700380} // namespace notify
Tom Joseph816e92b2017-09-06 19:23:00 +0530381
382namespace inventory
383{
384
385namespace get
386{
387
388GetSensorResponse assertion(const Info& sensorInfo)
389{
Vernon Mauery185b9f82018-07-20 10:52:36 -0700390 namespace fs = std::filesystem;
Tom Joseph816e92b2017-09-06 19:23:00 +0530391
392 fs::path path{ipmi::sensor::inventoryRoot};
393 path += sensorInfo.sensorPath;
394
395 return ipmi::sensor::get::mapDbusToAssertion(
Patrick Venture0b02be92018-08-31 11:55:55 -0700396 sensorInfo, path.string(),
397 sensorInfo.propertyInterfaces.begin()->first);
Tom Joseph816e92b2017-09-06 19:23:00 +0530398}
399
Patrick Venture0b02be92018-08-31 11:55:55 -0700400} // namespace get
Tom Joseph816e92b2017-09-06 19:23:00 +0530401
402} // namespace inventory
Patrick Venture0b02be92018-08-31 11:55:55 -0700403} // namespace sensor
404} // namespace ipmi