blob: 6d80f9a21e6b98d851efa1c4b560662c09dfe93b [file] [log] [blame]
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -05001#pragma once
2
Patrick Venture46470a32018-09-07 19:26:25 -07003#include "sensorhandler.hpp"
Patrick Venture0b02be92018-08-31 11:55:55 -07004
Patrick Venture586d35b2018-09-07 19:56:18 -07005#include <cmath>
Vernon Mauerye08fbff2019-04-03 09:19:34 -07006#include <ipmid/api.hpp>
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 <sdbusplus/message/types.hpp>
Patrick Venture0b02be92018-08-31 11:55:55 -070010
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -050011namespace ipmi
12{
13namespace sensor
14{
15
William A. Kennington III4c008022018-10-12 17:18:14 -070016namespace variant_ns = sdbusplus::message::variant_ns;
17
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -050018using Assertion = uint16_t;
19using Deassertion = uint16_t;
20using AssertionSet = std::pair<Assertion, Deassertion>;
21
22using Service = std::string;
23using Path = std::string;
24using Interface = std::string;
25
26using ServicePath = std::pair<Path, Service>;
27
28using Interfaces = std::vector<Interface>;
29
30using MapperResponseType = std::map<Path, std::map<Service, Interfaces>>;
31
32/** @brief get the D-Bus service and service path
33 * @param[in] bus - The Dbus bus object
34 * @param[in] interface - interface to the service
35 * @param[in] path - interested path in the list of objects
36 * @return pair of service path and service
37 */
38ServicePath getServiceAndPath(sdbusplus::bus::bus& bus,
39 const std::string& interface,
40 const std::string& path = std::string());
41
42/** @brief Make assertion set from input data
43 * @param[in] cmdData - Input sensor data
44 * @return pair of assertion and deassertion set
45 */
46AssertionSet getAssertionSet(const SetSensorReadingReq& cmdData);
47
48/** @brief send the message to DBus
49 * @param[in] msg - message to send
50 * @return failure status in IPMI error code
51 */
Dhruvaraj Subhashchandran2a444d02017-08-07 01:45:14 -050052ipmi_ret_t updateToDbus(IpmiUpdateData& msg);
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -050053
Tom Joseph816e92b2017-09-06 19:23:00 +053054namespace get
55{
56
Tom Josephb0adbcd2018-01-24 11:51:29 +053057/** @brief Populate sensor name from the D-Bus property associated with the
58 * sensor. In the example entry from the yaml, the name of the D-bus
59 * property "AttemptsLeft" is the sensor name.
60 *
61 * 0x07:
62 * sensorType: 195
63 * path: /xyz/openbmc_project/state/host0
64 * sensorReadingType: 0x6F
65 * serviceInterface: org.freedesktop.DBus.Properties
66 * readingType: readingAssertion
67 * sensorNamePattern: nameProperty
68 * interfaces:
69 * xyz.openbmc_project.Control.Boot.RebootAttempts:
70 * AttemptsLeft:
71 * Offsets:
72 * 0xFF:
73 * type: uint32_t
74 *
75 *
76 * @param[in] sensorInfo - Dbus info related to sensor.
77 *
78 * @return On success return the sensor name for the sensor.
79 */
80inline SensorName nameProperty(const Info& sensorInfo)
81{
82 return sensorInfo.propertyInterfaces.begin()->second.begin()->first;
83}
84
85/** @brief Populate sensor name from the D-Bus object associated with the
86 * sensor. If the object path is /system/chassis/motherboard/dimm0 then
87 * the leaf dimm0 is considered as the sensor name.
88 *
89 * @param[in] sensorInfo - Dbus info related to sensor.
90 *
91 * @return On success return the sensor name for the sensor.
92 */
93inline SensorName nameLeaf(const Info& sensorInfo)
94{
95 return sensorInfo.sensorPath.substr(
Patrick Venture0b02be92018-08-31 11:55:55 -070096 sensorInfo.sensorPath.find_last_of('/') + 1,
97 sensorInfo.sensorPath.length());
Tom Josephb0adbcd2018-01-24 11:51:29 +053098}
99
100/** @brief Populate sensor name from the D-Bus object associated with the
101 * sensor. If the object path is /system/chassis/motherboard/cpu0/core0
102 * then the sensor name is cpu0_core0. The leaf and the parent is put
103 * together to get the sensor name.
104 *
105 * @param[in] sensorInfo - Dbus info related to sensor.
106 *
107 * @return On success return the sensor name for the sensor.
108 */
109SensorName nameParentLeaf(const Info& sensorInfo);
110
Tom Joseph816e92b2017-09-06 19:23:00 +0530111/**
112 * @brief Helper function to map the dbus info to sensor's assertion status
113 * for the get sensor reading command.
114 *
115 * @param[in] sensorInfo - Dbus info related to sensor.
116 * @param[in] path - Dbus object path.
117 * @param[in] interface - Dbus interface.
118 *
119 * @return Response for get sensor reading command.
120 */
121GetSensorResponse mapDbusToAssertion(const Info& sensorInfo,
122 const InstancePath& path,
123 const DbusInterface& interface);
124
125/**
126 * @brief Map the Dbus info to sensor's assertion status in the Get sensor
127 * reading command response.
128 *
129 * @param[in] sensorInfo - Dbus info related to sensor.
130 *
131 * @return Response for get sensor reading command.
132 */
133GetSensorResponse assertion(const Info& sensorInfo);
134
Tom Josephe4014fc2017-09-06 23:57:36 +0530135/**
136 * @brief Maps the Dbus info to the reading field in the Get sensor reading
137 * command response.
138 *
139 * @param[in] sensorInfo - Dbus info related to sensor.
140 *
141 * @return Response for get sensor reading command.
142 */
143GetSensorResponse eventdata2(const Info& sensorInfo);
144
Tom Joseph295f17e2017-09-07 00:09:46 +0530145/**
146 * @brief readingAssertion is a case where the entire assertion state field
147 * serves as the sensor value.
148 *
149 * @tparam T - type of the dbus property related to sensor.
150 * @param[in] sensorInfo - Dbus info related to sensor.
151 *
152 * @return Response for get sensor reading command.
153 */
Patrick Venture0b02be92018-08-31 11:55:55 -0700154template <typename T>
Tom Joseph295f17e2017-09-07 00:09:46 +0530155GetSensorResponse readingAssertion(const Info& sensorInfo)
156{
157 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
Patrick Venture0b02be92018-08-31 11:55:55 -0700158 GetSensorResponse response{};
Tom Joseph295f17e2017-09-07 00:09:46 +0530159 auto responseData = reinterpret_cast<GetReadingResponse*>(response.data());
160
Patrick Venture0b02be92018-08-31 11:55:55 -0700161 auto service = ipmi::getService(bus, sensorInfo.sensorInterface,
Tom Joseph295f17e2017-09-07 00:09:46 +0530162 sensorInfo.sensorPath);
163
164 auto propValue = ipmi::getDbusProperty(
Patrick Venture0b02be92018-08-31 11:55:55 -0700165 bus, service, sensorInfo.sensorPath,
166 sensorInfo.propertyInterfaces.begin()->first,
167 sensorInfo.propertyInterfaces.begin()->second.begin()->first);
Tom Joseph295f17e2017-09-07 00:09:46 +0530168
William A. Kennington III4c008022018-10-12 17:18:14 -0700169 setAssertionBytes(static_cast<uint16_t>(variant_ns::get<T>(propValue)),
170 responseData);
Tom Joseph295f17e2017-09-07 00:09:46 +0530171
172 return response;
173}
174
Tom Josephe05b2922017-09-07 00:43:16 +0530175/** @brief Map the Dbus info to the reading field in the Get sensor reading
176 * command response
177 *
178 * @tparam T - type of the dbus property related to sensor.
179 * @param[in] sensorInfo - Dbus info related to sensor.
180 *
181 * @return Response for get sensor reading command.
182 */
Patrick Venture0b02be92018-08-31 11:55:55 -0700183template <typename T>
Tom Josephe05b2922017-09-07 00:43:16 +0530184GetSensorResponse readingData(const Info& sensorInfo)
185{
186 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
Patrick Venture0b02be92018-08-31 11:55:55 -0700187 GetSensorResponse response{};
Tom Josephe05b2922017-09-07 00:43:16 +0530188 auto responseData = reinterpret_cast<GetReadingResponse*>(response.data());
189
190 enableScanning(responseData);
191
Patrick Venture0b02be92018-08-31 11:55:55 -0700192 auto service = ipmi::getService(bus, sensorInfo.sensorInterface,
Tom Josephe05b2922017-09-07 00:43:16 +0530193 sensorInfo.sensorPath);
194
195 auto propValue = ipmi::getDbusProperty(
Patrick Venture0b02be92018-08-31 11:55:55 -0700196 bus, service, sensorInfo.sensorPath,
197 sensorInfo.propertyInterfaces.begin()->first,
198 sensorInfo.propertyInterfaces.begin()->second.begin()->first);
Tom Josephe05b2922017-09-07 00:43:16 +0530199
William A. Kennington III4c008022018-10-12 17:18:14 -0700200 double value = variant_ns::get<T>(propValue) *
Patrick Venture586d35b2018-09-07 19:56:18 -0700201 std::pow(10, sensorInfo.scale - sensorInfo.exponentR);
Tom Josephe05b2922017-09-07 00:43:16 +0530202
Patrick Venture0b02be92018-08-31 11:55:55 -0700203 auto rawData = static_cast<uint8_t>((value - sensorInfo.scaledOffset) /
204 sensorInfo.coefficientM);
Tom Joseph13b87a32018-02-16 09:37:43 +0530205
206 setReading(rawData, responseData);
Tom Josephe05b2922017-09-07 00:43:16 +0530207
208 return response;
209}
210
Patrick Venture0b02be92018-08-31 11:55:55 -0700211} // namespace get
Tom Joseph816e92b2017-09-06 19:23:00 +0530212
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500213namespace set
214{
215
216/** @brief Make a DBus message for a Dbus call
217 * @param[in] updateInterface - Interface name
218 * @param[in] sensorPath - Path of the sensor
219 * @param[in] command - command to be executed
220 * @param[in] sensorInterface - DBus interface of sensor
221 * @return a dbus message
222 */
223IpmiUpdateData makeDbusMsg(const std::string& updateInterface,
224 const std::string& sensorPath,
225 const std::string& command,
226 const std::string& sensorInterface);
227
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500228/** @brief Update d-bus based on assertion type sensor data
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500229 * @param[in] cmdData - input sensor data
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500230 * @param[in] sensorInfo - sensor d-bus info
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500231 * @return a IPMI error code
232 */
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500233ipmi_ret_t assertion(const SetSensorReadingReq& cmdData,
234 const Info& sensorInfo);
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500235
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500236/** @brief Update d-bus based on a reading assertion
237 * @tparam T - type of d-bus property mapping this sensor
238 * @param[in] cmdData - input sensor data
239 * @param[in] sensorInfo - sensor d-bus info
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500240 * @return a IPMI error code
241 */
Patrick Venture0b02be92018-08-31 11:55:55 -0700242template <typename T>
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500243ipmi_ret_t readingAssertion(const SetSensorReadingReq& cmdData,
244 const Info& sensorInfo)
245{
Patrick Venture0b02be92018-08-31 11:55:55 -0700246 auto msg =
247 makeDbusMsg("org.freedesktop.DBus.Properties", sensorInfo.sensorPath,
248 "Set", sensorInfo.sensorInterface);
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500249
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500250 const auto& interface = sensorInfo.propertyInterfaces.begin();
251 msg.append(interface->first);
252 for (const auto& property : interface->second)
253 {
254 msg.append(property.first);
255 sdbusplus::message::variant<T> value =
256 (cmdData.assertOffset8_14 << 8) | cmdData.assertOffset0_7;
257 msg.append(value);
258 }
259 return updateToDbus(msg);
260}
261
Emily Shaffercc941e12017-06-14 13:06:26 -0700262/** @brief Update d-bus based on a discrete reading
263 * @param[in] cmdData - input sensor data
264 * @param[in] sensorInfo - sensor d-bus info
265 * @return an IPMI error code
266 */
Patrick Venture0b02be92018-08-31 11:55:55 -0700267template <typename T>
Emily Shaffercc941e12017-06-14 13:06:26 -0700268ipmi_ret_t readingData(const SetSensorReadingReq& cmdData,
269 const Info& sensorInfo)
270{
Patrick Venture0b02be92018-08-31 11:55:55 -0700271 T raw_value =
272 (sensorInfo.coefficientM * cmdData.reading) + sensorInfo.scaledOffset;
Tom Joseph22102152018-03-02 18:46:39 +0530273
Patrick Venture586d35b2018-09-07 19:56:18 -0700274 raw_value *= std::pow(10, sensorInfo.exponentR - sensorInfo.scale);
Tom Joseph22102152018-03-02 18:46:39 +0530275
Patrick Venture0b02be92018-08-31 11:55:55 -0700276 auto msg =
277 makeDbusMsg("org.freedesktop.DBus.Properties", sensorInfo.sensorPath,
278 "Set", sensorInfo.sensorInterface);
Emily Shaffercc941e12017-06-14 13:06:26 -0700279
280 const auto& interface = sensorInfo.propertyInterfaces.begin();
281 msg.append(interface->first);
282
Emily Shaffercc941e12017-06-14 13:06:26 -0700283 for (const auto& property : interface->second)
284 {
285 msg.append(property.first);
286 sdbusplus::message::variant<T> value = raw_value;
287 msg.append(value);
288 }
289 return updateToDbus(msg);
290}
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500291
292/** @brief Update d-bus based on eventdata type sensor data
293 * @param[in] cmdData - input sensor data
294 * @param[in] sensorInfo - sensor d-bus info
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500295 * @return a IPMI error code
296 */
Patrick Venture0b02be92018-08-31 11:55:55 -0700297ipmi_ret_t eventdata(const SetSensorReadingReq& cmdData, const Info& sensorInfo,
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500298 uint8_t data);
299
300/** @brief Update d-bus based on eventdata1 type sensor data
301 * @param[in] cmdData - input sensor data
302 * @param[in] sensorInfo - sensor d-bus info
303 * @return a IPMI error code
304 */
305inline ipmi_ret_t eventdata1(const SetSensorReadingReq& cmdData,
306 const Info& sensorInfo)
307{
308 return eventdata(cmdData, sensorInfo, cmdData.eventData1);
309}
310
311/** @brief Update d-bus based on eventdata2 type sensor data
312 * @param[in] cmdData - input sensor data
313 * @param[in] sensorInfo - sensor d-bus info
314 * @return a IPMI error code
315 */
316inline ipmi_ret_t eventdata2(const SetSensorReadingReq& cmdData,
317 const Info& sensorInfo)
318{
319 return eventdata(cmdData, sensorInfo, cmdData.eventData2);
320}
321
322/** @brief Update d-bus based on eventdata3 type sensor data
323 * @param[in] cmdData - input sensor data
324 * @param[in] sensorInfo - sensor d-bus info
325 * @return a IPMI error code
326 */
327inline ipmi_ret_t eventdata3(const SetSensorReadingReq& cmdData,
328 const Info& sensorInfo)
329{
330 return eventdata(cmdData, sensorInfo, cmdData.eventData3);
331}
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500332
Patrick Venture0b02be92018-08-31 11:55:55 -0700333} // namespace set
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500334
335namespace notify
336{
337
338/** @brief Make a DBus message for a Dbus call
339 * @param[in] updateInterface - Interface name
340 * @param[in] sensorPath - Path of the sensor
341 * @param[in] command - command to be executed
342 * @param[in] sensorInterface - DBus interface of sensor
343 * @return a dbus message
344 */
345IpmiUpdateData makeDbusMsg(const std::string& updateInterface,
346 const std::string& sensorPath,
347 const std::string& command,
348 const std::string& sensorInterface);
349
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500350/** @brief Update d-bus based on assertion type sensor data
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500351 * @param[in] interfaceMap - sensor interface
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500352 * @param[in] cmdData - input sensor data
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500353 * @param[in] sensorInfo - sensor d-bus info
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500354 * @return a IPMI error code
355 */
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500356ipmi_ret_t assertion(const SetSensorReadingReq& cmdData,
357 const Info& sensorInfo);
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500358
Patrick Venture0b02be92018-08-31 11:55:55 -0700359} // namespace notify
Tom Joseph816e92b2017-09-06 19:23:00 +0530360
361namespace inventory
362{
363
364namespace get
365{
366
367/**
368 * @brief Map the Dbus info to sensor's assertion status in the Get sensor
369 * reading command response.
370 *
371 * @param[in] sensorInfo - Dbus info related to sensor.
372 *
373 * @return Response for get sensor reading command.
374 */
375GetSensorResponse assertion(const Info& sensorInfo);
376
377} // namespace get
378
379} // namespace inventory
Patrick Venture0b02be92018-08-31 11:55:55 -0700380} // namespace sensor
381} // namespace ipmi