blob: 601f5cdb9d9d87473cbea3232adf4574b4e67432 [file] [log] [blame]
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -05001#pragma once
2
Brandon Kim9cf85622019-06-19 12:05:08 -07003#include "config.h"
4
Patrick Venture46470a32018-09-07 19:26:25 -07005#include "sensorhandler.hpp"
Patrick Venture0b02be92018-08-31 11:55:55 -07006
Patrick Venture586d35b2018-09-07 19:56:18 -07007#include <cmath>
Vernon Mauerye08fbff2019-04-03 09:19:34 -07008#include <ipmid/api.hpp>
Vernon Mauery33250242019-03-12 16:49:26 -07009#include <ipmid/types.hpp>
Vernon Mauery6a98fe72019-03-11 15:57:48 -070010#include <ipmid/utils.hpp>
Tony Leec5324252019-10-31 17:24:16 +080011#include <phosphor-logging/elog-errors.hpp>
12#include <phosphor-logging/log.hpp>
William A. Kennington III4c008022018-10-12 17:18:14 -070013#include <sdbusplus/message/types.hpp>
Patrick Venture0b02be92018-08-31 11:55:55 -070014
Lei YU97140502021-09-17 13:49:43 +080015#ifdef FEATURE_SENSORS_CACHE
Lei YUa55e9ea2021-09-18 15:15:17 +080016
Lei YU97140502021-09-17 13:49:43 +080017extern ipmi::sensor::SensorCacheMap sensorCacheMap;
Lei YUa55e9ea2021-09-18 15:15:17 +080018
19// The signal's message type is 0x04 from DBus spec:
20// https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-messages
21static constexpr auto msgTypeSignal = 0x04;
22
Lei YU97140502021-09-17 13:49:43 +080023#endif
24
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -050025namespace ipmi
26{
27namespace sensor
28{
29
30using Assertion = uint16_t;
31using Deassertion = uint16_t;
32using AssertionSet = std::pair<Assertion, Deassertion>;
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -050033using Service = std::string;
34using Path = std::string;
35using Interface = std::string;
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -050036using ServicePath = std::pair<Path, Service>;
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -050037using Interfaces = std::vector<Interface>;
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -050038using MapperResponseType = std::map<Path, std::map<Service, Interfaces>>;
Lei YU8e8152c2021-12-06 20:11:08 +080039using PropertyMap = ipmi::PropertyMap;
40
Tony Leec5324252019-10-31 17:24:16 +080041using namespace phosphor::logging;
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -050042
43/** @brief get the D-Bus service and service path
44 * @param[in] bus - The Dbus bus object
45 * @param[in] interface - interface to the service
46 * @param[in] path - interested path in the list of objects
47 * @return pair of service path and service
48 */
Patrick Williams5d82f472022-07-22 19:26:53 -050049ServicePath getServiceAndPath(sdbusplus::bus_t& bus,
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -050050 const std::string& interface,
51 const std::string& path = std::string());
52
53/** @brief Make assertion set from input data
54 * @param[in] cmdData - Input sensor data
55 * @return pair of assertion and deassertion set
56 */
57AssertionSet getAssertionSet(const SetSensorReadingReq& cmdData);
58
59/** @brief send the message to DBus
60 * @param[in] msg - message to send
61 * @return failure status in IPMI error code
62 */
Dhruvaraj Subhashchandran2a444d02017-08-07 01:45:14 -050063ipmi_ret_t updateToDbus(IpmiUpdateData& msg);
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -050064
Tom Joseph816e92b2017-09-06 19:23:00 +053065namespace get
66{
67
Tom Josephb0adbcd2018-01-24 11:51:29 +053068/** @brief Populate sensor name from the D-Bus property associated with the
69 * sensor. In the example entry from the yaml, the name of the D-bus
70 * property "AttemptsLeft" is the sensor name.
71 *
72 * 0x07:
73 * sensorType: 195
74 * path: /xyz/openbmc_project/state/host0
75 * sensorReadingType: 0x6F
76 * serviceInterface: org.freedesktop.DBus.Properties
77 * readingType: readingAssertion
78 * sensorNamePattern: nameProperty
79 * interfaces:
80 * xyz.openbmc_project.Control.Boot.RebootAttempts:
81 * AttemptsLeft:
82 * Offsets:
83 * 0xFF:
84 * type: uint32_t
85 *
86 *
87 * @param[in] sensorInfo - Dbus info related to sensor.
88 *
89 * @return On success return the sensor name for the sensor.
90 */
91inline SensorName nameProperty(const Info& sensorInfo)
92{
93 return sensorInfo.propertyInterfaces.begin()->second.begin()->first;
94}
95
96/** @brief Populate sensor name from the D-Bus object associated with the
97 * sensor. If the object path is /system/chassis/motherboard/dimm0 then
98 * the leaf dimm0 is considered as the sensor name.
99 *
100 * @param[in] sensorInfo - Dbus info related to sensor.
101 *
102 * @return On success return the sensor name for the sensor.
103 */
104inline SensorName nameLeaf(const Info& sensorInfo)
105{
106 return sensorInfo.sensorPath.substr(
Patrick Venture0b02be92018-08-31 11:55:55 -0700107 sensorInfo.sensorPath.find_last_of('/') + 1,
108 sensorInfo.sensorPath.length());
Tom Josephb0adbcd2018-01-24 11:51:29 +0530109}
110
111/** @brief Populate sensor name from the D-Bus object associated with the
Lotus Xu2101a442020-12-24 16:01:56 +0800112 * sensor and the property.
113 * If the object path is /xyz/openbmc_project/inventory/Fan0 and
114 * the property is Present, the leaf Fan0 and the Property is
115 * joined to Fan0_Present as the sensor name.
116 *
117 * @param[in] sensorInfo - Dbus info related to sensor.
118 *
119 * @return On success return the sensor name for the sensor.
120 */
121inline SensorName nameLeafProperty(const Info& sensorInfo)
122{
123 return nameLeaf(sensorInfo) + "_" + nameProperty(sensorInfo);
124}
125
126/** @brief Populate sensor name from the D-Bus object associated with the
Tom Josephb0adbcd2018-01-24 11:51:29 +0530127 * sensor. If the object path is /system/chassis/motherboard/cpu0/core0
128 * then the sensor name is cpu0_core0. The leaf and the parent is put
129 * together to get the sensor name.
130 *
131 * @param[in] sensorInfo - Dbus info related to sensor.
132 *
133 * @return On success return the sensor name for the sensor.
134 */
135SensorName nameParentLeaf(const Info& sensorInfo);
136
Tom Joseph816e92b2017-09-06 19:23:00 +0530137/**
138 * @brief Helper function to map the dbus info to sensor's assertion status
139 * for the get sensor reading command.
140 *
141 * @param[in] sensorInfo - Dbus info related to sensor.
142 * @param[in] path - Dbus object path.
143 * @param[in] interface - Dbus interface.
144 *
145 * @return Response for get sensor reading command.
146 */
147GetSensorResponse mapDbusToAssertion(const Info& sensorInfo,
148 const InstancePath& path,
149 const DbusInterface& interface);
150
Lei YU8c2c0482021-09-16 17:28:28 +0800151#ifndef FEATURE_SENSORS_CACHE
Tom Joseph816e92b2017-09-06 19:23:00 +0530152/**
153 * @brief Map the Dbus info to sensor's assertion status in the Get sensor
154 * reading command response.
155 *
156 * @param[in] sensorInfo - Dbus info related to sensor.
157 *
158 * @return Response for get sensor reading command.
159 */
160GetSensorResponse assertion(const Info& sensorInfo);
161
Tom Josephe4014fc2017-09-06 23:57:36 +0530162/**
163 * @brief Maps the Dbus info to the reading field in the Get sensor reading
164 * command response.
165 *
166 * @param[in] sensorInfo - Dbus info related to sensor.
167 *
168 * @return Response for get sensor reading command.
169 */
170GetSensorResponse eventdata2(const Info& sensorInfo);
171
Tom Joseph295f17e2017-09-07 00:09:46 +0530172/**
173 * @brief readingAssertion is a case where the entire assertion state field
174 * serves as the sensor value.
175 *
176 * @tparam T - type of the dbus property related to sensor.
177 * @param[in] sensorInfo - Dbus info related to sensor.
178 *
179 * @return Response for get sensor reading command.
180 */
Patrick Venture0b02be92018-08-31 11:55:55 -0700181template <typename T>
Tom Joseph295f17e2017-09-07 00:09:46 +0530182GetSensorResponse readingAssertion(const Info& sensorInfo)
183{
Patrick Williams5d82f472022-07-22 19:26:53 -0500184 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
Patrick Venture0b02be92018-08-31 11:55:55 -0700185 GetSensorResponse response{};
Tom Joseph295f17e2017-09-07 00:09:46 +0530186
Jeremy Kerr3dc35582020-05-17 15:47:07 +0800187 enableScanning(&response);
188
Patrick Venture0b02be92018-08-31 11:55:55 -0700189 auto service = ipmi::getService(bus, sensorInfo.sensorInterface,
Tom Joseph295f17e2017-09-07 00:09:46 +0530190 sensorInfo.sensorPath);
191
192 auto propValue = ipmi::getDbusProperty(
Patrick Venture0b02be92018-08-31 11:55:55 -0700193 bus, service, sensorInfo.sensorPath,
194 sensorInfo.propertyInterfaces.begin()->first,
195 sensorInfo.propertyInterfaces.begin()->second.begin()->first);
Tom Joseph295f17e2017-09-07 00:09:46 +0530196
Sui Chen4cc42552019-09-11 10:28:35 -0700197 setAssertionBytes(static_cast<uint16_t>(std::get<T>(propValue)), &response);
Tom Joseph295f17e2017-09-07 00:09:46 +0530198
199 return response;
200}
201
Tom Josephe05b2922017-09-07 00:43:16 +0530202/** @brief Map the Dbus info to the reading field in the Get sensor reading
203 * command response
204 *
205 * @tparam T - type of the dbus property related to sensor.
206 * @param[in] sensorInfo - Dbus info related to sensor.
207 *
208 * @return Response for get sensor reading command.
209 */
Patrick Venture0b02be92018-08-31 11:55:55 -0700210template <typename T>
Tom Josephe05b2922017-09-07 00:43:16 +0530211GetSensorResponse readingData(const Info& sensorInfo)
212{
Patrick Williams5d82f472022-07-22 19:26:53 -0500213 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
Tom Josephe05b2922017-09-07 00:43:16 +0530214
Sui Chen4cc42552019-09-11 10:28:35 -0700215 GetSensorResponse response{};
216
217 enableScanning(&response);
Tom Josephe05b2922017-09-07 00:43:16 +0530218
Patrick Venture0b02be92018-08-31 11:55:55 -0700219 auto service = ipmi::getService(bus, sensorInfo.sensorInterface,
Tom Josephe05b2922017-09-07 00:43:16 +0530220 sensorInfo.sensorPath);
221
Brandon Kim9cf85622019-06-19 12:05:08 -0700222#ifdef UPDATE_FUNCTIONAL_ON_FAIL
223 // Check the OperationalStatus interface for functional property
224 if (sensorInfo.propertyInterfaces.begin()->first ==
225 "xyz.openbmc_project.Sensor.Value")
226 {
227 bool functional = true;
228 try
229 {
230 auto funcValue = ipmi::getDbusProperty(
231 bus, service, sensorInfo.sensorPath,
232 "xyz.openbmc_project.State.Decorator.OperationalStatus",
233 "Functional");
234 functional = std::get<bool>(funcValue);
235 }
236 catch (...)
237 {
238 // No-op if Functional property could not be found since this
239 // check is only valid for Sensor.Value read for hwmonio
240 }
241 if (!functional)
242 {
243 throw SensorFunctionalError();
244 }
245 }
246#endif
247
Tom Josephe05b2922017-09-07 00:43:16 +0530248 auto propValue = ipmi::getDbusProperty(
Patrick Venture0b02be92018-08-31 11:55:55 -0700249 bus, service, sensorInfo.sensorPath,
250 sensorInfo.propertyInterfaces.begin()->first,
251 sensorInfo.propertyInterfaces.begin()->second.begin()->first);
Tom Josephe05b2922017-09-07 00:43:16 +0530252
Vernon Maueryf442e112019-04-09 11:44:36 -0700253 double value = std::get<T>(propValue) *
Patrick Venture586d35b2018-09-07 19:56:18 -0700254 std::pow(10, sensorInfo.scale - sensorInfo.exponentR);
Tony Leec5324252019-10-31 17:24:16 +0800255 int32_t rawData =
256 (value - sensorInfo.scaledOffset) / sensorInfo.coefficientM;
Tom Josephe05b2922017-09-07 00:43:16 +0530257
Tony Leec5324252019-10-31 17:24:16 +0800258 constexpr uint8_t sensorUnitsSignedBits = 2 << 6;
259 constexpr uint8_t signedDataFormat = 0x80;
260 // if sensorUnits1 [7:6] = 10b, sensor is signed
Willy Tu9154caa2021-12-02 02:28:54 -0800261 int32_t minClamp;
262 int32_t maxClamp;
Tony Leec5324252019-10-31 17:24:16 +0800263 if ((sensorInfo.sensorUnits1 & sensorUnitsSignedBits) == signedDataFormat)
264 {
Willy Tu9154caa2021-12-02 02:28:54 -0800265 minClamp = std::numeric_limits<int8_t>::lowest();
266 maxClamp = std::numeric_limits<int8_t>::max();
Tony Leec5324252019-10-31 17:24:16 +0800267 }
268 else
269 {
Willy Tu9154caa2021-12-02 02:28:54 -0800270 minClamp = std::numeric_limits<uint8_t>::lowest();
271 maxClamp = std::numeric_limits<uint8_t>::max();
Tony Leec5324252019-10-31 17:24:16 +0800272 }
Willy Tu9154caa2021-12-02 02:28:54 -0800273 setReading(static_cast<uint8_t>(std::clamp(rawData, minClamp, maxClamp)),
274 &response);
Tom Josephe05b2922017-09-07 00:43:16 +0530275
Konstantin Aladyshevf93b29c2021-05-05 14:49:14 +0300276 if (!std::isfinite(value))
277 {
278 response.readingOrStateUnavailable = 1;
279 }
280
Konstantin Aladyshev778f6592021-11-02 11:28:25 +0300281 bool critAlarmHigh;
282 try
283 {
284 critAlarmHigh = std::get<bool>(ipmi::getDbusProperty(
285 bus, service, sensorInfo.sensorPath,
286 "xyz.openbmc_project.Sensor.Threshold.Critical",
287 "CriticalAlarmHigh"));
288 }
289 catch (const std::exception& e)
290 {
291 critAlarmHigh = false;
292 }
293 bool critAlarmLow;
294 try
295 {
296 critAlarmLow = std::get<bool>(ipmi::getDbusProperty(
297 bus, service, sensorInfo.sensorPath,
298 "xyz.openbmc_project.Sensor.Threshold.Critical",
299 "CriticalAlarmLow"));
300 }
301 catch (const std::exception& e)
302 {
303 critAlarmLow = false;
304 }
305 bool warningAlarmHigh;
306 try
307 {
308 warningAlarmHigh = std::get<bool>(ipmi::getDbusProperty(
309 bus, service, sensorInfo.sensorPath,
310 "xyz.openbmc_project.Sensor.Threshold.Warning",
311 "WarningAlarmHigh"));
312 }
313 catch (const std::exception& e)
314 {
315 warningAlarmHigh = false;
316 }
317 bool warningAlarmLow;
318 try
319 {
320 warningAlarmLow = std::get<bool>(ipmi::getDbusProperty(
321 bus, service, sensorInfo.sensorPath,
Tim Leed09db492022-05-30 10:22:34 +0800322 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningAlarmLow"));
Konstantin Aladyshev778f6592021-11-02 11:28:25 +0300323 }
324 catch (const std::exception& e)
325 {
326 warningAlarmLow = false;
327 }
328 response.thresholdLevelsStates =
George Liu33d90e12022-05-16 12:54:20 +0800329 (static_cast<uint8_t>(critAlarmHigh) << 3) |
330 (static_cast<uint8_t>(critAlarmLow) << 2) |
331 (static_cast<uint8_t>(warningAlarmHigh) << 1) |
332 (static_cast<uint8_t>(warningAlarmLow));
Konstantin Aladyshev778f6592021-11-02 11:28:25 +0300333
Tom Josephe05b2922017-09-07 00:43:16 +0530334 return response;
335}
336
Lei YU8c2c0482021-09-16 17:28:28 +0800337#else
338
Lei YU8c2c0482021-09-16 17:28:28 +0800339/**
340 * @brief Map the Dbus info to sensor's assertion status in the Get sensor
341 * reading command response.
342 *
343 * @param[in] id - The sensor id
344 * @param[in] sensorInfo - Dbus info related to sensor.
345 * @param[in] msg - Dbus message from match callback.
346 *
347 * @return Response for get sensor reading command.
348 */
349std::optional<GetSensorResponse> assertion(uint8_t id, const Info& sensorInfo,
Lei YU8e8152c2021-12-06 20:11:08 +0800350 const PropertyMap& properties);
Lei YU8c2c0482021-09-16 17:28:28 +0800351
352/**
353 * @brief Maps the Dbus info to the reading field in the Get sensor reading
354 * command response.
355 *
356 * @param[in] id - The sensor id
357 * @param[in] sensorInfo - Dbus info related to sensor.
358 * @param[in] msg - Dbus message from match callback.
359 *
360 * @return Response for get sensor reading command.
361 */
362std::optional<GetSensorResponse> eventdata2(uint8_t id, const Info& sensorInfo,
Lei YU8e8152c2021-12-06 20:11:08 +0800363 const PropertyMap& properties);
Lei YU8c2c0482021-09-16 17:28:28 +0800364
365/**
366 * @brief readingAssertion is a case where the entire assertion state field
367 * serves as the sensor value.
368 *
369 * @tparam T - type of the dbus property related to sensor.
370 * @param[in] id - The sensor id
371 * @param[in] sensorInfo - Dbus info related to sensor.
372 * @param[in] msg - Dbus message from match callback.
373 *
374 * @return Response for get sensor reading command.
375 */
376template <typename T>
Lei YU8e8152c2021-12-06 20:11:08 +0800377std::optional<GetSensorResponse> readingAssertion(uint8_t id,
378 const Info& sensorInfo,
379 const PropertyMap& properties)
Lei YU8c2c0482021-09-16 17:28:28 +0800380{
Lei YU7d720342021-09-18 18:39:09 +0800381 GetSensorResponse response{};
Lei YU7d720342021-09-18 18:39:09 +0800382 enableScanning(&response);
383
Lei YU7d720342021-09-18 18:39:09 +0800384 auto iter = properties.find(
385 sensorInfo.propertyInterfaces.begin()->second.begin()->first);
386 if (iter == properties.end())
387 {
388 return {};
389 }
390
391 setAssertionBytes(static_cast<uint16_t>(std::get<T>(iter->second)),
392 &response);
393
394 if (!sensorCacheMap[id].has_value())
395 {
396 sensorCacheMap[id] = SensorData{};
397 }
398 sensorCacheMap[id]->response = response;
399 return response;
Lei YU8c2c0482021-09-16 17:28:28 +0800400}
401
402/** @brief Get sensor reading from the dbus message from match
403 *
404 * @tparam T - type of the dbus property related to sensor.
405 * @param[in] id - The sensor id
406 * @param[in] sensorInfo - Dbus info related to sensor.
407 * @param[in] msg - Dbus message from match callback.
408 *
409 * @return Response for get sensor reading command.
410 */
411template <typename T>
412std::optional<GetSensorResponse> readingData(uint8_t id, const Info& sensorInfo,
Lei YU8e8152c2021-12-06 20:11:08 +0800413 const PropertyMap& properties)
Lei YU8c2c0482021-09-16 17:28:28 +0800414{
Lei YU8e8152c2021-12-06 20:11:08 +0800415 auto iter = properties.find("Functional");
416 if (iter != properties.end())
Lei YUa55e9ea2021-09-18 15:15:17 +0800417 {
Lei YU8e8152c2021-12-06 20:11:08 +0800418 sensorCacheMap[id]->functional = std::get<bool>(iter->second);
Lei YUa55e9ea2021-09-18 15:15:17 +0800419 }
Lei YU8e8152c2021-12-06 20:11:08 +0800420 iter = properties.find("Available");
421 if (iter != properties.end())
422 {
423 sensorCacheMap[id]->available = std::get<bool>(iter->second);
424 }
425#ifdef UPDATE_FUNCTIONAL_ON_FAIL
426 if (sensorCacheMap[id])
427 {
428 if (!sensorCacheMap[id]->functional)
429 {
430 throw SensorFunctionalError();
431 }
432 }
433#endif
Lei YU97140502021-09-17 13:49:43 +0800434
435 GetSensorResponse response{};
436
437 enableScanning(&response);
438
Lei YU8e8152c2021-12-06 20:11:08 +0800439 iter = properties.find(
Lei YU97140502021-09-17 13:49:43 +0800440 sensorInfo.propertyInterfaces.begin()->second.begin()->first);
441 if (iter == properties.end())
442 {
443 return {};
444 }
445
446 double value = std::get<T>(iter->second) *
447 std::pow(10, sensorInfo.scale - sensorInfo.exponentR);
448 int32_t rawData =
449 (value - sensorInfo.scaledOffset) / sensorInfo.coefficientM;
450
451 constexpr uint8_t sensorUnitsSignedBits = 2 << 6;
452 constexpr uint8_t signedDataFormat = 0x80;
453 // if sensorUnits1 [7:6] = 10b, sensor is signed
454 if ((sensorInfo.sensorUnits1 & sensorUnitsSignedBits) == signedDataFormat)
455 {
456 if (rawData > std::numeric_limits<int8_t>::max() ||
457 rawData < std::numeric_limits<int8_t>::lowest())
458 {
459 log<level::ERR>("Value out of range");
460 throw std::out_of_range("Value out of range");
461 }
462 setReading(static_cast<int8_t>(rawData), &response);
463 }
464 else
465 {
466 if (rawData > std::numeric_limits<uint8_t>::max() ||
467 rawData < std::numeric_limits<uint8_t>::lowest())
468 {
469 log<level::ERR>("Value out of range");
470 throw std::out_of_range("Value out of range");
471 }
472 setReading(static_cast<uint8_t>(rawData), &response);
473 }
474
475 if (!std::isfinite(value))
476 {
477 response.readingOrStateUnavailable = 1;
478 }
479
480 if (!sensorCacheMap[id].has_value())
481 {
482 sensorCacheMap[id] = SensorData{};
483 }
484 sensorCacheMap[id]->response = response;
485
486 return response;
Lei YU8c2c0482021-09-16 17:28:28 +0800487}
488
489#endif // FEATURE_SENSORS_CACHE
490
Patrick Venture0b02be92018-08-31 11:55:55 -0700491} // namespace get
Tom Joseph816e92b2017-09-06 19:23:00 +0530492
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500493namespace set
494{
495
496/** @brief Make a DBus message for a Dbus call
497 * @param[in] updateInterface - Interface name
498 * @param[in] sensorPath - Path of the sensor
499 * @param[in] command - command to be executed
500 * @param[in] sensorInterface - DBus interface of sensor
501 * @return a dbus message
502 */
503IpmiUpdateData makeDbusMsg(const std::string& updateInterface,
504 const std::string& sensorPath,
505 const std::string& command,
506 const std::string& sensorInterface);
507
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500508/** @brief Update d-bus based on assertion type sensor data
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500509 * @param[in] cmdData - input sensor data
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500510 * @param[in] sensorInfo - sensor d-bus info
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500511 * @return a IPMI error code
512 */
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500513ipmi_ret_t assertion(const SetSensorReadingReq& cmdData,
514 const Info& sensorInfo);
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500515
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500516/** @brief Update d-bus based on a reading assertion
517 * @tparam T - type of d-bus property mapping this sensor
518 * @param[in] cmdData - input sensor data
519 * @param[in] sensorInfo - sensor d-bus info
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500520 * @return a IPMI error code
521 */
Patrick Venture0b02be92018-08-31 11:55:55 -0700522template <typename T>
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500523ipmi_ret_t readingAssertion(const SetSensorReadingReq& cmdData,
524 const Info& sensorInfo)
525{
Patrick Venture0b02be92018-08-31 11:55:55 -0700526 auto msg =
527 makeDbusMsg("org.freedesktop.DBus.Properties", sensorInfo.sensorPath,
528 "Set", sensorInfo.sensorInterface);
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500529
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500530 const auto& interface = sensorInfo.propertyInterfaces.begin();
531 msg.append(interface->first);
532 for (const auto& property : interface->second)
533 {
534 msg.append(property.first);
Andrew Geissler6467ed22020-05-16 16:03:53 -0500535 std::variant<T> value = static_cast<T>((cmdData.assertOffset8_14 << 8) |
536 cmdData.assertOffset0_7);
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500537 msg.append(value);
538 }
539 return updateToDbus(msg);
540}
541
Emily Shaffercc941e12017-06-14 13:06:26 -0700542/** @brief Update d-bus based on a discrete reading
543 * @param[in] cmdData - input sensor data
544 * @param[in] sensorInfo - sensor d-bus info
545 * @return an IPMI error code
546 */
Patrick Venture0b02be92018-08-31 11:55:55 -0700547template <typename T>
Emily Shaffercc941e12017-06-14 13:06:26 -0700548ipmi_ret_t readingData(const SetSensorReadingReq& cmdData,
549 const Info& sensorInfo)
550{
Patrick Venture0b02be92018-08-31 11:55:55 -0700551 T raw_value =
552 (sensorInfo.coefficientM * cmdData.reading) + sensorInfo.scaledOffset;
Tom Joseph22102152018-03-02 18:46:39 +0530553
Patrick Venture586d35b2018-09-07 19:56:18 -0700554 raw_value *= std::pow(10, sensorInfo.exponentR - sensorInfo.scale);
Tom Joseph22102152018-03-02 18:46:39 +0530555
Patrick Venture0b02be92018-08-31 11:55:55 -0700556 auto msg =
557 makeDbusMsg("org.freedesktop.DBus.Properties", sensorInfo.sensorPath,
558 "Set", sensorInfo.sensorInterface);
Emily Shaffercc941e12017-06-14 13:06:26 -0700559
560 const auto& interface = sensorInfo.propertyInterfaces.begin();
561 msg.append(interface->first);
562
Emily Shaffercc941e12017-06-14 13:06:26 -0700563 for (const auto& property : interface->second)
564 {
565 msg.append(property.first);
Vernon Mauery16b86932019-05-01 08:36:11 -0700566 std::variant<T> value = raw_value;
Emily Shaffercc941e12017-06-14 13:06:26 -0700567 msg.append(value);
568 }
569 return updateToDbus(msg);
570}
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500571
572/** @brief Update d-bus based on eventdata type sensor data
573 * @param[in] cmdData - input sensor data
574 * @param[in] sensorInfo - sensor d-bus info
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500575 * @return a IPMI error code
576 */
Patrick Venture0b02be92018-08-31 11:55:55 -0700577ipmi_ret_t eventdata(const SetSensorReadingReq& cmdData, const Info& sensorInfo,
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500578 uint8_t data);
579
580/** @brief Update d-bus based on eventdata1 type sensor data
581 * @param[in] cmdData - input sensor data
582 * @param[in] sensorInfo - sensor d-bus info
583 * @return a IPMI error code
584 */
585inline ipmi_ret_t eventdata1(const SetSensorReadingReq& cmdData,
586 const Info& sensorInfo)
587{
588 return eventdata(cmdData, sensorInfo, cmdData.eventData1);
589}
590
591/** @brief Update d-bus based on eventdata2 type sensor data
592 * @param[in] cmdData - input sensor data
593 * @param[in] sensorInfo - sensor d-bus info
594 * @return a IPMI error code
595 */
596inline ipmi_ret_t eventdata2(const SetSensorReadingReq& cmdData,
597 const Info& sensorInfo)
598{
599 return eventdata(cmdData, sensorInfo, cmdData.eventData2);
600}
601
602/** @brief Update d-bus based on eventdata3 type sensor data
603 * @param[in] cmdData - input sensor data
604 * @param[in] sensorInfo - sensor d-bus info
605 * @return a IPMI error code
606 */
607inline ipmi_ret_t eventdata3(const SetSensorReadingReq& cmdData,
608 const Info& sensorInfo)
609{
610 return eventdata(cmdData, sensorInfo, cmdData.eventData3);
611}
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500612
Patrick Venture0b02be92018-08-31 11:55:55 -0700613} // namespace set
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500614
615namespace notify
616{
617
618/** @brief Make a DBus message for a Dbus call
619 * @param[in] updateInterface - Interface name
620 * @param[in] sensorPath - Path of the sensor
621 * @param[in] command - command to be executed
622 * @param[in] sensorInterface - DBus interface of sensor
623 * @return a dbus message
624 */
625IpmiUpdateData makeDbusMsg(const std::string& updateInterface,
626 const std::string& sensorPath,
627 const std::string& command,
628 const std::string& sensorInterface);
629
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500630/** @brief Update d-bus based on assertion type sensor data
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500631 * @param[in] interfaceMap - sensor interface
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500632 * @param[in] cmdData - input sensor data
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500633 * @param[in] sensorInfo - sensor d-bus info
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500634 * @return a IPMI error code
635 */
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500636ipmi_ret_t assertion(const SetSensorReadingReq& cmdData,
637 const Info& sensorInfo);
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500638
Patrick Venture0b02be92018-08-31 11:55:55 -0700639} // namespace notify
Tom Joseph816e92b2017-09-06 19:23:00 +0530640
641namespace inventory
642{
643
644namespace get
645{
646
Lei YUff8c9b42021-10-15 14:20:57 +0800647#ifndef FEATURE_SENSORS_CACHE
648
Tom Joseph816e92b2017-09-06 19:23:00 +0530649/**
650 * @brief Map the Dbus info to sensor's assertion status in the Get sensor
651 * reading command response.
652 *
653 * @param[in] sensorInfo - Dbus info related to sensor.
654 *
655 * @return Response for get sensor reading command.
656 */
657GetSensorResponse assertion(const Info& sensorInfo);
658
Lei YUff8c9b42021-10-15 14:20:57 +0800659#else
660
661/**
662 * @brief Map the Dbus info to sensor's assertion status in the Get sensor
663 * reading command response.
664 *
665 * @param[in] id - The sensor id
666 * @param[in] sensorInfo - Dbus info related to sensor.
667 * @param[in] msg - Dbus message from match callback.
668 *
669 * @return Response for get sensor reading command.
670 */
671std::optional<GetSensorResponse> assertion(uint8_t id, const Info& sensorInfo,
Lei YU8e8152c2021-12-06 20:11:08 +0800672 const PropertyMap& properties);
Lei YUff8c9b42021-10-15 14:20:57 +0800673
674#endif
675
Tom Joseph816e92b2017-09-06 19:23:00 +0530676} // namespace get
677
678} // namespace inventory
Patrick Venture0b02be92018-08-31 11:55:55 -0700679} // namespace sensor
680} // namespace ipmi