blob: 0b27090b04e819be06a433f7208b4115a0d6f276 [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
Vernon Mauerye08fbff2019-04-03 09:19:34 -07007#include <ipmid/api.hpp>
Vernon Mauery33250242019-03-12 16:49:26 -07008#include <ipmid/types.hpp>
Vernon Mauery6a98fe72019-03-11 15:57:48 -07009#include <ipmid/utils.hpp>
Tony Leec5324252019-10-31 17:24:16 +080010#include <phosphor-logging/elog-errors.hpp>
George Liu9b745a82024-07-19 09:09:36 +080011#include <phosphor-logging/lg2.hpp>
William A. Kennington III4c008022018-10-12 17:18:14 -070012#include <sdbusplus/message/types.hpp>
Patrick Venture0b02be92018-08-31 11:55:55 -070013
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -050014#include <cmath>
15
Lei YU97140502021-09-17 13:49:43 +080016#ifdef FEATURE_SENSORS_CACHE
Lei YUa55e9ea2021-09-18 15:15:17 +080017
Lei YU97140502021-09-17 13:49:43 +080018extern ipmi::sensor::SensorCacheMap sensorCacheMap;
Lei YUa55e9ea2021-09-18 15:15:17 +080019
20// The signal's message type is 0x04 from DBus spec:
21// https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-messages
22static constexpr auto msgTypeSignal = 0x04;
23
Lei YU97140502021-09-17 13:49:43 +080024#endif
25
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -050026namespace ipmi
27{
28namespace sensor
29{
30
31using Assertion = uint16_t;
32using Deassertion = uint16_t;
33using AssertionSet = std::pair<Assertion, Deassertion>;
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -050034using Service = std::string;
35using Path = std::string;
36using Interface = std::string;
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -050037using ServicePath = std::pair<Path, Service>;
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -050038using Interfaces = std::vector<Interface>;
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -050039using MapperResponseType = std::map<Path, std::map<Service, Interfaces>>;
Lei YU8e8152c2021-12-06 20:11:08 +080040using PropertyMap = ipmi::PropertyMap;
41
Tony Leec5324252019-10-31 17:24:16 +080042using namespace phosphor::logging;
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -050043
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -050044/** @brief Make assertion set from input data
45 * @param[in] cmdData - Input sensor data
46 * @return pair of assertion and deassertion set
47 */
48AssertionSet getAssertionSet(const SetSensorReadingReq& cmdData);
49
50/** @brief send the message to DBus
51 * @param[in] msg - message to send
52 * @return failure status in IPMI error code
53 */
George Liu23c868c2025-07-04 09:31:35 +080054ipmi::Cc updateToDbus(IpmiUpdateData& msg);
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -050055
Tom Joseph816e92b2017-09-06 19:23:00 +053056namespace get
57{
58
Tom Josephb0adbcd2018-01-24 11:51:29 +053059/** @brief Populate sensor name from the D-Bus property associated with the
60 * sensor. In the example entry from the yaml, the name of the D-bus
61 * property "AttemptsLeft" is the sensor name.
62 *
63 * 0x07:
64 * sensorType: 195
65 * path: /xyz/openbmc_project/state/host0
66 * sensorReadingType: 0x6F
67 * serviceInterface: org.freedesktop.DBus.Properties
68 * readingType: readingAssertion
69 * sensorNamePattern: nameProperty
70 * interfaces:
71 * xyz.openbmc_project.Control.Boot.RebootAttempts:
72 * AttemptsLeft:
73 * Offsets:
74 * 0xFF:
75 * type: uint32_t
76 *
77 *
78 * @param[in] sensorInfo - Dbus info related to sensor.
79 *
80 * @return On success return the sensor name for the sensor.
81 */
82inline SensorName nameProperty(const Info& sensorInfo)
83{
84 return sensorInfo.propertyInterfaces.begin()->second.begin()->first;
85}
86
87/** @brief Populate sensor name from the D-Bus object associated with the
88 * sensor. If the object path is /system/chassis/motherboard/dimm0 then
89 * the leaf dimm0 is considered as the sensor name.
90 *
91 * @param[in] sensorInfo - Dbus info related to sensor.
92 *
93 * @return On success return the sensor name for the sensor.
94 */
95inline SensorName nameLeaf(const Info& sensorInfo)
96{
97 return sensorInfo.sensorPath.substr(
Patrick Venture0b02be92018-08-31 11:55:55 -070098 sensorInfo.sensorPath.find_last_of('/') + 1,
99 sensorInfo.sensorPath.length());
Tom Josephb0adbcd2018-01-24 11:51:29 +0530100}
101
102/** @brief Populate sensor name from the D-Bus object associated with the
Lotus Xu2101a442020-12-24 16:01:56 +0800103 * sensor and the property.
104 * If the object path is /xyz/openbmc_project/inventory/Fan0 and
105 * the property is Present, the leaf Fan0 and the Property is
106 * joined to Fan0_Present as the sensor name.
107 *
108 * @param[in] sensorInfo - Dbus info related to sensor.
109 *
110 * @return On success return the sensor name for the sensor.
111 */
112inline SensorName nameLeafProperty(const Info& sensorInfo)
113{
114 return nameLeaf(sensorInfo) + "_" + nameProperty(sensorInfo);
115}
116
117/** @brief Populate sensor name from the D-Bus object associated with the
Tom Josephb0adbcd2018-01-24 11:51:29 +0530118 * sensor. If the object path is /system/chassis/motherboard/cpu0/core0
119 * then the sensor name is cpu0_core0. The leaf and the parent is put
120 * together to get the sensor name.
121 *
122 * @param[in] sensorInfo - Dbus info related to sensor.
123 *
124 * @return On success return the sensor name for the sensor.
125 */
126SensorName nameParentLeaf(const Info& sensorInfo);
127
Tom Joseph816e92b2017-09-06 19:23:00 +0530128/**
129 * @brief Helper function to map the dbus info to sensor's assertion status
130 * for the get sensor reading command.
131 *
132 * @param[in] sensorInfo - Dbus info related to sensor.
133 * @param[in] path - Dbus object path.
134 * @param[in] interface - Dbus interface.
135 *
136 * @return Response for get sensor reading command.
137 */
138GetSensorResponse mapDbusToAssertion(const Info& sensorInfo,
139 const InstancePath& path,
140 const DbusInterface& interface);
141
Lei YU8c2c0482021-09-16 17:28:28 +0800142#ifndef FEATURE_SENSORS_CACHE
Tom Joseph816e92b2017-09-06 19:23:00 +0530143/**
144 * @brief Map the Dbus info to sensor's assertion status in the Get sensor
145 * reading command response.
146 *
147 * @param[in] sensorInfo - Dbus info related to sensor.
148 *
149 * @return Response for get sensor reading command.
150 */
151GetSensorResponse assertion(const Info& sensorInfo);
152
Tom Josephe4014fc2017-09-06 23:57:36 +0530153/**
154 * @brief Maps the Dbus info to the reading field in the Get sensor reading
155 * command response.
156 *
157 * @param[in] sensorInfo - Dbus info related to sensor.
158 *
159 * @return Response for get sensor reading command.
160 */
161GetSensorResponse eventdata2(const Info& sensorInfo);
162
Tom Joseph295f17e2017-09-07 00:09:46 +0530163/**
164 * @brief readingAssertion is a case where the entire assertion state field
165 * serves as the sensor value.
166 *
167 * @tparam T - type of the dbus property related to sensor.
168 * @param[in] sensorInfo - Dbus info related to sensor.
169 *
170 * @return Response for get sensor reading command.
171 */
Patrick Venture0b02be92018-08-31 11:55:55 -0700172template <typename T>
Tom Joseph295f17e2017-09-07 00:09:46 +0530173GetSensorResponse readingAssertion(const Info& sensorInfo)
174{
Patrick Williams5d82f472022-07-22 19:26:53 -0500175 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
Patrick Venture0b02be92018-08-31 11:55:55 -0700176 GetSensorResponse response{};
Tom Joseph295f17e2017-09-07 00:09:46 +0530177
George Liue6b2be52025-08-19 13:57:43 +0800178 enableScanning(response);
George Liu08d3add2024-11-18 15:22:43 +0800179 try
180 {
181 auto service = ipmi::getService(bus, sensorInfo.sensorInterface,
182 sensorInfo.sensorPath);
183 auto propValue = ipmi::getDbusProperty(
184 bus, service, sensorInfo.sensorPath,
185 sensorInfo.propertyInterfaces.begin()->first,
186 sensorInfo.propertyInterfaces.begin()->second.begin()->first);
Jeremy Kerr3dc35582020-05-17 15:47:07 +0800187
George Liu08d3add2024-11-18 15:22:43 +0800188 setAssertionBytes(static_cast<uint16_t>(std::get<T>(propValue)),
George Liue6b2be52025-08-19 13:57:43 +0800189 response);
George Liu08d3add2024-11-18 15:22:43 +0800190 }
191 catch (const std::exception& e)
192 {
193 lg2::error(
194 "Failed to call readingAssertion, path: {PATH}, interface: {INTERFACE}: {ERROR}",
195 "PATH", sensorInfo.sensorPath, "INTERFACE",
196 sensorInfo.sensorInterface, "ERROR", e);
197 }
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
George Liue6b2be52025-08-19 13:57:43 +0800217 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
George Liu08d3add2024-11-18 15:22:43 +0800248 double value{};
249 try
250 {
251 auto propValue = ipmi::getDbusProperty(
252 bus, service, sensorInfo.sensorPath,
253 sensorInfo.propertyInterfaces.begin()->first,
254 sensorInfo.propertyInterfaces.begin()->second.begin()->first);
Tom Josephe05b2922017-09-07 00:43:16 +0530255
George Liu08d3add2024-11-18 15:22:43 +0800256 value = std::get<T>(propValue) *
257 std::pow(10, sensorInfo.scale - sensorInfo.exponentR);
258 }
259 catch (const std::exception& e)
260 {
261 lg2::error(
262 "Failed to call readingData, path: {PATH}, interface: {INTERFACE}: {ERROR}",
263 "PATH", sensorInfo.sensorPath, "INTERFACE",
264 sensorInfo.sensorInterface, "ERROR", e);
265 return response;
266 }
267
Patrick Williams1318a5e2024-08-16 15:19:54 -0400268 int32_t rawData =
269 (value - sensorInfo.scaledOffset) / sensorInfo.coefficientM;
Tom Josephe05b2922017-09-07 00:43:16 +0530270
Tony Leec5324252019-10-31 17:24:16 +0800271 constexpr uint8_t sensorUnitsSignedBits = 2 << 6;
272 constexpr uint8_t signedDataFormat = 0x80;
273 // if sensorUnits1 [7:6] = 10b, sensor is signed
Willy Tu9154caa2021-12-02 02:28:54 -0800274 int32_t minClamp;
275 int32_t maxClamp;
Tony Leec5324252019-10-31 17:24:16 +0800276 if ((sensorInfo.sensorUnits1 & sensorUnitsSignedBits) == signedDataFormat)
277 {
Willy Tu9154caa2021-12-02 02:28:54 -0800278 minClamp = std::numeric_limits<int8_t>::lowest();
279 maxClamp = std::numeric_limits<int8_t>::max();
Tony Leec5324252019-10-31 17:24:16 +0800280 }
281 else
282 {
Willy Tu9154caa2021-12-02 02:28:54 -0800283 minClamp = std::numeric_limits<uint8_t>::lowest();
284 maxClamp = std::numeric_limits<uint8_t>::max();
Tony Leec5324252019-10-31 17:24:16 +0800285 }
Willy Tu9154caa2021-12-02 02:28:54 -0800286 setReading(static_cast<uint8_t>(std::clamp(rawData, minClamp, maxClamp)),
George Liue6b2be52025-08-19 13:57:43 +0800287 response);
Tom Josephe05b2922017-09-07 00:43:16 +0530288
Konstantin Aladyshevf93b29c2021-05-05 14:49:14 +0300289 if (!std::isfinite(value))
290 {
291 response.readingOrStateUnavailable = 1;
292 }
293
Konstantin Aladyshev778f6592021-11-02 11:28:25 +0300294 bool critAlarmHigh;
295 try
296 {
297 critAlarmHigh = std::get<bool>(ipmi::getDbusProperty(
298 bus, service, sensorInfo.sensorPath,
299 "xyz.openbmc_project.Sensor.Threshold.Critical",
300 "CriticalAlarmHigh"));
301 }
302 catch (const std::exception& e)
303 {
304 critAlarmHigh = false;
305 }
306 bool critAlarmLow;
307 try
308 {
309 critAlarmLow = std::get<bool>(ipmi::getDbusProperty(
310 bus, service, sensorInfo.sensorPath,
311 "xyz.openbmc_project.Sensor.Threshold.Critical",
312 "CriticalAlarmLow"));
313 }
314 catch (const std::exception& e)
315 {
316 critAlarmLow = false;
317 }
318 bool warningAlarmHigh;
319 try
320 {
321 warningAlarmHigh = std::get<bool>(ipmi::getDbusProperty(
322 bus, service, sensorInfo.sensorPath,
323 "xyz.openbmc_project.Sensor.Threshold.Warning",
324 "WarningAlarmHigh"));
325 }
326 catch (const std::exception& e)
327 {
328 warningAlarmHigh = false;
329 }
330 bool warningAlarmLow;
331 try
332 {
333 warningAlarmLow = std::get<bool>(ipmi::getDbusProperty(
334 bus, service, sensorInfo.sensorPath,
Tim Leed09db492022-05-30 10:22:34 +0800335 "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningAlarmLow"));
Konstantin Aladyshev778f6592021-11-02 11:28:25 +0300336 }
337 catch (const std::exception& e)
338 {
339 warningAlarmLow = false;
340 }
341 response.thresholdLevelsStates =
YouPengWu19c962b2025-10-17 19:00:18 +0800342 0xC0 | (static_cast<uint8_t>(warningAlarmLow) << 0) |
343 (static_cast<uint8_t>(critAlarmLow) << 1) |
344 (static_cast<uint8_t>(warningAlarmHigh) << 3) |
345 (static_cast<uint8_t>(critAlarmHigh) << 4);
Tom Josephe05b2922017-09-07 00:43:16 +0530346 return response;
347}
348
Lei YU8c2c0482021-09-16 17:28:28 +0800349#else
350
Lei YU8c2c0482021-09-16 17:28:28 +0800351/**
352 * @brief Map the Dbus info to sensor's assertion status in the Get sensor
353 * reading command response.
354 *
355 * @param[in] id - The sensor id
356 * @param[in] sensorInfo - Dbus info related to sensor.
357 * @param[in] msg - Dbus message from match callback.
358 *
359 * @return Response for get sensor reading command.
360 */
361std::optional<GetSensorResponse> assertion(uint8_t id, const Info& sensorInfo,
Lei YU8e8152c2021-12-06 20:11:08 +0800362 const PropertyMap& properties);
Lei YU8c2c0482021-09-16 17:28:28 +0800363
364/**
365 * @brief Maps the Dbus info to the reading field in the Get sensor reading
366 * command response.
367 *
368 * @param[in] id - The sensor id
369 * @param[in] sensorInfo - Dbus info related to sensor.
370 * @param[in] msg - Dbus message from match callback.
371 *
372 * @return Response for get sensor reading command.
373 */
374std::optional<GetSensorResponse> eventdata2(uint8_t id, const Info& sensorInfo,
Lei YU8e8152c2021-12-06 20:11:08 +0800375 const PropertyMap& properties);
Lei YU8c2c0482021-09-16 17:28:28 +0800376
377/**
378 * @brief readingAssertion is a case where the entire assertion state field
379 * serves as the sensor value.
380 *
381 * @tparam T - type of the dbus property related to sensor.
382 * @param[in] id - The sensor id
383 * @param[in] sensorInfo - Dbus info related to sensor.
384 * @param[in] msg - Dbus message from match callback.
385 *
386 * @return Response for get sensor reading command.
387 */
388template <typename T>
Patrick Williams1318a5e2024-08-16 15:19:54 -0400389std::optional<GetSensorResponse> readingAssertion(
390 uint8_t id, const Info& sensorInfo, const PropertyMap& properties)
Lei YU8c2c0482021-09-16 17:28:28 +0800391{
Lei YU7d720342021-09-18 18:39:09 +0800392 GetSensorResponse response{};
George Liue6b2be52025-08-19 13:57:43 +0800393 enableScanning(response);
Lei YU7d720342021-09-18 18:39:09 +0800394
Lei YU7d720342021-09-18 18:39:09 +0800395 auto iter = properties.find(
396 sensorInfo.propertyInterfaces.begin()->second.begin()->first);
397 if (iter == properties.end())
398 {
399 return {};
400 }
401
402 setAssertionBytes(static_cast<uint16_t>(std::get<T>(iter->second)),
George Liue6b2be52025-08-19 13:57:43 +0800403 response);
Lei YU7d720342021-09-18 18:39:09 +0800404
405 if (!sensorCacheMap[id].has_value())
406 {
407 sensorCacheMap[id] = SensorData{};
408 }
409 sensorCacheMap[id]->response = response;
410 return response;
Lei YU8c2c0482021-09-16 17:28:28 +0800411}
412
413/** @brief Get sensor reading from the dbus message from match
414 *
415 * @tparam T - type of the dbus property related to sensor.
416 * @param[in] id - The sensor id
417 * @param[in] sensorInfo - Dbus info related to sensor.
418 * @param[in] msg - Dbus message from match callback.
419 *
420 * @return Response for get sensor reading command.
421 */
422template <typename T>
423std::optional<GetSensorResponse> readingData(uint8_t id, const Info& sensorInfo,
Lei YU8e8152c2021-12-06 20:11:08 +0800424 const PropertyMap& properties)
Lei YU8c2c0482021-09-16 17:28:28 +0800425{
Lei YU8e8152c2021-12-06 20:11:08 +0800426 auto iter = properties.find("Functional");
427 if (iter != properties.end())
Lei YUa55e9ea2021-09-18 15:15:17 +0800428 {
Lei YU8e8152c2021-12-06 20:11:08 +0800429 sensorCacheMap[id]->functional = std::get<bool>(iter->second);
Lei YUa55e9ea2021-09-18 15:15:17 +0800430 }
Lei YU8e8152c2021-12-06 20:11:08 +0800431 iter = properties.find("Available");
432 if (iter != properties.end())
433 {
434 sensorCacheMap[id]->available = std::get<bool>(iter->second);
435 }
436#ifdef UPDATE_FUNCTIONAL_ON_FAIL
437 if (sensorCacheMap[id])
438 {
439 if (!sensorCacheMap[id]->functional)
440 {
441 throw SensorFunctionalError();
442 }
443 }
444#endif
Lei YU97140502021-09-17 13:49:43 +0800445
446 GetSensorResponse response{};
447
George Liue6b2be52025-08-19 13:57:43 +0800448 enableScanning(response);
Lei YU97140502021-09-17 13:49:43 +0800449
Lei YU8e8152c2021-12-06 20:11:08 +0800450 iter = properties.find(
Lei YU97140502021-09-17 13:49:43 +0800451 sensorInfo.propertyInterfaces.begin()->second.begin()->first);
452 if (iter == properties.end())
453 {
454 return {};
455 }
456
457 double value = std::get<T>(iter->second) *
458 std::pow(10, sensorInfo.scale - sensorInfo.exponentR);
Patrick Williams1318a5e2024-08-16 15:19:54 -0400459 int32_t rawData =
460 (value - sensorInfo.scaledOffset) / sensorInfo.coefficientM;
Lei YU97140502021-09-17 13:49:43 +0800461
462 constexpr uint8_t sensorUnitsSignedBits = 2 << 6;
463 constexpr uint8_t signedDataFormat = 0x80;
464 // if sensorUnits1 [7:6] = 10b, sensor is signed
465 if ((sensorInfo.sensorUnits1 & sensorUnitsSignedBits) == signedDataFormat)
466 {
467 if (rawData > std::numeric_limits<int8_t>::max() ||
468 rawData < std::numeric_limits<int8_t>::lowest())
469 {
George Liu9b745a82024-07-19 09:09:36 +0800470 lg2::error("Value out of range");
Lei YU97140502021-09-17 13:49:43 +0800471 throw std::out_of_range("Value out of range");
472 }
George Liue6b2be52025-08-19 13:57:43 +0800473 setReading(static_cast<int8_t>(rawData), response);
Lei YU97140502021-09-17 13:49:43 +0800474 }
475 else
476 {
477 if (rawData > std::numeric_limits<uint8_t>::max() ||
478 rawData < std::numeric_limits<uint8_t>::lowest())
479 {
George Liu9b745a82024-07-19 09:09:36 +0800480 lg2::error("Value out of range");
Lei YU97140502021-09-17 13:49:43 +0800481 throw std::out_of_range("Value out of range");
482 }
George Liue6b2be52025-08-19 13:57:43 +0800483 setReading(static_cast<uint8_t>(rawData), response);
Lei YU97140502021-09-17 13:49:43 +0800484 }
485
486 if (!std::isfinite(value))
487 {
488 response.readingOrStateUnavailable = 1;
489 }
490
491 if (!sensorCacheMap[id].has_value())
492 {
493 sensorCacheMap[id] = SensorData{};
494 }
495 sensorCacheMap[id]->response = response;
496
497 return response;
Lei YU8c2c0482021-09-16 17:28:28 +0800498}
499
500#endif // FEATURE_SENSORS_CACHE
501
Patrick Venture0b02be92018-08-31 11:55:55 -0700502} // namespace get
Tom Joseph816e92b2017-09-06 19:23:00 +0530503
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500504namespace set
505{
506
507/** @brief Make a DBus message for a Dbus call
508 * @param[in] updateInterface - Interface name
509 * @param[in] sensorPath - Path of the sensor
510 * @param[in] command - command to be executed
511 * @param[in] sensorInterface - DBus interface of sensor
512 * @return a dbus message
513 */
514IpmiUpdateData makeDbusMsg(const std::string& updateInterface,
515 const std::string& sensorPath,
516 const std::string& command,
517 const std::string& sensorInterface);
518
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500519/** @brief Update d-bus based on assertion type sensor data
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500520 * @param[in] cmdData - input sensor data
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500521 * @param[in] sensorInfo - sensor d-bus info
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500522 * @return a IPMI error code
523 */
George Liu23c868c2025-07-04 09:31:35 +0800524ipmi::Cc assertion(const SetSensorReadingReq& cmdData, const Info& sensorInfo);
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500525
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500526/** @brief Update d-bus based on a reading assertion
527 * @tparam T - type of d-bus property mapping this sensor
528 * @param[in] cmdData - input sensor data
529 * @param[in] sensorInfo - sensor d-bus info
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500530 * @return a IPMI error code
531 */
Patrick Venture0b02be92018-08-31 11:55:55 -0700532template <typename T>
George Liu23c868c2025-07-04 09:31:35 +0800533ipmi::Cc readingAssertion(const SetSensorReadingReq& cmdData,
534 const Info& sensorInfo)
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500535{
Patrick Williams1318a5e2024-08-16 15:19:54 -0400536 auto msg =
537 makeDbusMsg("org.freedesktop.DBus.Properties", sensorInfo.sensorPath,
538 "Set", sensorInfo.sensorInterface);
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500539
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500540 const auto& interface = sensorInfo.propertyInterfaces.begin();
541 msg.append(interface->first);
542 for (const auto& property : interface->second)
543 {
544 msg.append(property.first);
Patrick Williams1318a5e2024-08-16 15:19:54 -0400545 std::variant<T> value = static_cast<T>(
546 (cmdData.assertOffset8_14 << 8) | cmdData.assertOffset0_7);
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500547 msg.append(value);
548 }
549 return updateToDbus(msg);
550}
551
Emily Shaffercc941e12017-06-14 13:06:26 -0700552/** @brief Update d-bus based on a discrete reading
553 * @param[in] cmdData - input sensor data
554 * @param[in] sensorInfo - sensor d-bus info
555 * @return an IPMI error code
556 */
Patrick Venture0b02be92018-08-31 11:55:55 -0700557template <typename T>
George Liu23c868c2025-07-04 09:31:35 +0800558ipmi::Cc readingData(const SetSensorReadingReq& cmdData, const Info& sensorInfo)
Emily Shaffercc941e12017-06-14 13:06:26 -0700559{
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500560 T raw_value = (sensorInfo.coefficientM * cmdData.reading) +
561 sensorInfo.scaledOffset;
Tom Joseph22102152018-03-02 18:46:39 +0530562
Patrick Venture586d35b2018-09-07 19:56:18 -0700563 raw_value *= std::pow(10, sensorInfo.exponentR - sensorInfo.scale);
Tom Joseph22102152018-03-02 18:46:39 +0530564
Patrick Williams1318a5e2024-08-16 15:19:54 -0400565 auto msg =
566 makeDbusMsg("org.freedesktop.DBus.Properties", sensorInfo.sensorPath,
567 "Set", sensorInfo.sensorInterface);
Emily Shaffercc941e12017-06-14 13:06:26 -0700568
569 const auto& interface = sensorInfo.propertyInterfaces.begin();
570 msg.append(interface->first);
571
Emily Shaffercc941e12017-06-14 13:06:26 -0700572 for (const auto& property : interface->second)
573 {
574 msg.append(property.first);
Vernon Mauery16b86932019-05-01 08:36:11 -0700575 std::variant<T> value = raw_value;
Emily Shaffercc941e12017-06-14 13:06:26 -0700576 msg.append(value);
577 }
578 return updateToDbus(msg);
579}
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500580
581/** @brief Update d-bus based on eventdata type sensor data
582 * @param[in] cmdData - input sensor data
583 * @param[in] sensorInfo - sensor d-bus info
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500584 * @return a IPMI error code
585 */
George Liu23c868c2025-07-04 09:31:35 +0800586ipmi::Cc eventdata(const SetSensorReadingReq& cmdData, const Info& sensorInfo,
587 uint8_t data);
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500588
589/** @brief Update d-bus based on eventdata1 type sensor data
590 * @param[in] cmdData - input sensor data
591 * @param[in] sensorInfo - sensor d-bus info
592 * @return a IPMI error code
593 */
George Liu23c868c2025-07-04 09:31:35 +0800594inline ipmi::Cc eventdata1(const SetSensorReadingReq& cmdData,
595 const Info& sensorInfo)
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500596{
597 return eventdata(cmdData, sensorInfo, cmdData.eventData1);
598}
599
600/** @brief Update d-bus based on eventdata2 type sensor data
601 * @param[in] cmdData - input sensor data
602 * @param[in] sensorInfo - sensor d-bus info
603 * @return a IPMI error code
604 */
George Liu23c868c2025-07-04 09:31:35 +0800605inline ipmi::Cc eventdata2(const SetSensorReadingReq& cmdData,
606 const Info& sensorInfo)
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500607{
608 return eventdata(cmdData, sensorInfo, cmdData.eventData2);
609}
610
611/** @brief Update d-bus based on eventdata3 type sensor data
612 * @param[in] cmdData - input sensor data
613 * @param[in] sensorInfo - sensor d-bus info
614 * @return a IPMI error code
615 */
George Liu23c868c2025-07-04 09:31:35 +0800616inline ipmi::Cc eventdata3(const SetSensorReadingReq& cmdData,
617 const Info& sensorInfo)
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500618{
619 return eventdata(cmdData, sensorInfo, cmdData.eventData3);
620}
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500621
Patrick Venture0b02be92018-08-31 11:55:55 -0700622} // namespace set
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500623
624namespace notify
625{
626
627/** @brief Make a DBus message for a Dbus call
628 * @param[in] updateInterface - Interface name
629 * @param[in] sensorPath - Path of the sensor
630 * @param[in] command - command to be executed
631 * @param[in] sensorInterface - DBus interface of sensor
632 * @return a dbus message
633 */
634IpmiUpdateData makeDbusMsg(const std::string& updateInterface,
635 const std::string& sensorPath,
636 const std::string& command,
637 const std::string& sensorInterface);
638
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500639/** @brief Update d-bus based on assertion type sensor data
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500640 * @param[in] interfaceMap - sensor interface
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500641 * @param[in] cmdData - input sensor data
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500642 * @param[in] sensorInfo - sensor d-bus info
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500643 * @return a IPMI error code
644 */
George Liu23c868c2025-07-04 09:31:35 +0800645ipmi::Cc assertion(const SetSensorReadingReq& cmdData, const Info& sensorInfo);
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500646
Patrick Venture0b02be92018-08-31 11:55:55 -0700647} // namespace notify
Tom Joseph816e92b2017-09-06 19:23:00 +0530648
649namespace inventory
650{
651
652namespace get
653{
654
Lei YUff8c9b42021-10-15 14:20:57 +0800655#ifndef FEATURE_SENSORS_CACHE
656
Tom Joseph816e92b2017-09-06 19:23:00 +0530657/**
658 * @brief Map the Dbus info to sensor's assertion status in the Get sensor
659 * reading command response.
660 *
661 * @param[in] sensorInfo - Dbus info related to sensor.
662 *
663 * @return Response for get sensor reading command.
664 */
665GetSensorResponse assertion(const Info& sensorInfo);
666
Lei YUff8c9b42021-10-15 14:20:57 +0800667#else
668
669/**
670 * @brief Map the Dbus info to sensor's assertion status in the Get sensor
671 * reading command response.
672 *
673 * @param[in] id - The sensor id
674 * @param[in] sensorInfo - Dbus info related to sensor.
675 * @param[in] msg - Dbus message from match callback.
676 *
677 * @return Response for get sensor reading command.
678 */
679std::optional<GetSensorResponse> assertion(uint8_t id, const Info& sensorInfo,
Lei YU8e8152c2021-12-06 20:11:08 +0800680 const PropertyMap& properties);
Lei YUff8c9b42021-10-15 14:20:57 +0800681
682#endif
683
Tom Joseph816e92b2017-09-06 19:23:00 +0530684} // namespace get
685
686} // namespace inventory
Patrick Venture0b02be92018-08-31 11:55:55 -0700687} // namespace sensor
688} // namespace ipmi