blob: 7e604b8cdad267c4b20f0e952b660ccc4583c304 [file] [log] [blame]
Alexander Hansen46a755f2025-10-27 16:31:08 +01001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright 2017 Google Inc
Patrick Ventureaadb30d2020-08-10 09:17:11 -07003
Pete O_o765a6d82025-07-23 21:44:14 -07004#include "config.h"
5
Patrick Ventureaadb30d2020-08-10 09:17:11 -07006#include "dbushelper.hpp"
7
8#include "dbushelper_interface.hpp"
9#include "dbusutil.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070010
Patrick Venturea83a3ec2020-08-04 09:52:05 -070011#include <phosphor-logging/log.hpp>
Patrick Ventureaadb30d2020-08-10 09:17:11 -070012#include <sdbusplus/bus.hpp>
Ed Tanousf8b6e552025-06-27 13:27:50 -070013#include <sdbusplus/exception.hpp>
Alexander Hansen1ec3d132025-10-27 17:19:50 +010014#include <xyz/openbmc_project/ObjectMapper/common.hpp>
Patrick Venturea83a3ec2020-08-04 09:52:05 -070015
Ed Tanousf8b6e552025-06-27 13:27:50 -070016#include <cstdint>
Patrick Ventureaadb30d2020-08-10 09:17:11 -070017#include <map>
Ed Tanousf8b6e552025-06-27 13:27:50 -070018#include <stdexcept>
Patrick Ventureaadb30d2020-08-10 09:17:11 -070019#include <string>
James Feist1f802f52019-02-08 13:51:43 -080020#include <variant>
Patrick Ventureaadb30d2020-08-10 09:17:11 -070021#include <vector>
22
Alexander Hansen1ec3d132025-10-27 17:19:50 +010023using ObjectMapper = sdbusplus::common::xyz::openbmc_project::ObjectMapper;
24
Patrick Ventureaadb30d2020-08-10 09:17:11 -070025namespace pid_control
26{
Patrick Venture863b9242018-03-08 08:29:23 -080027
Patrick Venture863b9242018-03-08 08:29:23 -080028using Property = std::string;
James Feist1f802f52019-02-08 13:51:43 -080029using Value = std::variant<int64_t, double, std::string, bool>;
Patrick Venture863b9242018-03-08 08:29:23 -080030using PropertyMap = std::map<Property, Value>;
31
Patrick Venture34ddc902018-10-30 11:05:17 -070032using namespace phosphor::logging;
33
Patrick Venture863b9242018-03-08 08:29:23 -080034/* TODO(venture): Basically all phosphor apps need this, maybe it should be a
35 * part of sdbusplus. There is an old version in libmapper.
36 */
Patrick Venture9b936922020-08-10 11:28:39 -070037std::string DbusHelper::getService(const std::string& intf,
Patrick Venture0df7c0f2018-06-13 09:02:13 -070038 const std::string& path)
Patrick Venture863b9242018-03-08 08:29:23 -080039{
Alexander Hansen1ec3d132025-10-27 17:19:50 +010040 auto mapper = _bus.new_method_call(
41 ObjectMapper::default_service, ObjectMapper::instance_path,
42 ObjectMapper::interface, ObjectMapper::method_names::get_object);
Patrick Venture863b9242018-03-08 08:29:23 -080043
44 mapper.append(path);
45 mapper.append(std::vector<std::string>({intf}));
46
Patrick Venture863b9242018-03-08 08:29:23 -080047 std::map<std::string, std::vector<std::string>> response;
Patrick Venture34ddc902018-10-30 11:05:17 -070048
49 try
50 {
Patrick Venture8729eb92020-08-10 10:38:44 -070051 auto responseMsg = _bus.call(mapper);
Patrick Venture34ddc902018-10-30 11:05:17 -070052
53 responseMsg.read(response);
54 }
Patrick Williamsb228bc32022-07-22 19:26:56 -050055 catch (const sdbusplus::exception_t& ex)
Patrick Venture34ddc902018-10-30 11:05:17 -070056 {
57 log<level::ERR>("ObjectMapper call failure",
58 entry("WHAT=%s", ex.what()));
59 throw;
60 }
Patrick Venture863b9242018-03-08 08:29:23 -080061
62 if (response.begin() == response.end())
63 {
64 throw std::runtime_error("Unable to find Object: " + path);
65 }
66
67 return response.begin()->first;
68}
69
Patrick Venture9b936922020-08-10 11:28:39 -070070void DbusHelper::getProperties(const std::string& service,
Patrick Venture1df9e872020-10-08 15:35:01 -070071 const std::string& path, SensorProperties* prop)
Patrick Venture863b9242018-03-08 08:29:23 -080072{
Patrick Venture8729eb92020-08-10 10:38:44 -070073 auto pimMsg = _bus.new_method_call(service.c_str(), path.c_str(),
74 propertiesintf, "GetAll");
Patrick Venture863b9242018-03-08 08:29:23 -080075
76 pimMsg.append(sensorintf);
Patrick Venture863b9242018-03-08 08:29:23 -080077
Patrick Venture7dbc5172018-10-30 12:18:45 -070078 PropertyMap propMap;
79
80 try
Patrick Venture863b9242018-03-08 08:29:23 -080081 {
Patrick Venture8729eb92020-08-10 10:38:44 -070082 auto valueResponseMsg = _bus.call(pimMsg);
Patrick Venture7dbc5172018-10-30 12:18:45 -070083 valueResponseMsg.read(propMap);
84 }
Patrick Williamsb228bc32022-07-22 19:26:56 -050085 catch (const sdbusplus::exception_t& ex)
Patrick Venture7dbc5172018-10-30 12:18:45 -070086 {
87 log<level::ERR>("GetAll Properties Failed",
88 entry("WHAT=%s", ex.what()));
89 throw;
Patrick Venture863b9242018-03-08 08:29:23 -080090 }
91
92 // The PropertyMap returned will look like this because it's always
93 // reading a Sensor.Value interface.
94 // a{sv} 3:
95 // "Value" x 24875
96 // "Unit" s "xyz.openbmc_project.Sensor.Value.Unit.DegreesC"
97 // "Scale" x -3
Patrick Venture863b9242018-03-08 08:29:23 -080098
Patrick Venture0d73b102018-05-09 10:29:42 -070099 // If no error was set, the values should all be there.
James Feistc065cf12018-07-05 10:23:11 -0700100 auto findUnit = propMap.find("Unit");
101 if (findUnit != propMap.end())
102 {
James Feist1f802f52019-02-08 13:51:43 -0800103 prop->unit = std::get<std::string>(findUnit->second);
James Feistc065cf12018-07-05 10:23:11 -0700104 }
105 auto findScale = propMap.find("Scale");
James Feist75eb7692019-02-25 12:50:02 -0800106 auto findMax = propMap.find("MaxValue");
107 auto findMin = propMap.find("MinValue");
108
109 prop->min = 0;
110 prop->max = 0;
111 prop->scale = 0;
James Feistc065cf12018-07-05 10:23:11 -0700112 if (findScale != propMap.end())
113 {
James Feist1f802f52019-02-08 13:51:43 -0800114 prop->scale = std::get<int64_t>(findScale->second);
James Feistc065cf12018-07-05 10:23:11 -0700115 }
James Feist75eb7692019-02-25 12:50:02 -0800116 if (findMax != propMap.end())
James Feistc065cf12018-07-05 10:23:11 -0700117 {
James Feist75eb7692019-02-25 12:50:02 -0800118 prop->max = std::visit(VariantToDoubleVisitor(), findMax->second);
119 }
120 if (findMin != propMap.end())
121 {
122 prop->min = std::visit(VariantToDoubleVisitor(), findMin->second);
James Feistc065cf12018-07-05 10:23:11 -0700123 }
124
James Feist1f802f52019-02-08 13:51:43 -0800125 prop->value = std::visit(VariantToDoubleVisitor(), propMap["Value"]);
Patrick Venture863b9242018-03-08 08:29:23 -0800126
Alex.Song8f73ad72021-10-07 00:18:27 +0800127 bool available = true;
128 try
129 {
130 getProperty(service, path, availabilityIntf, "Available", available);
131 }
Patrick Williamsb228bc32022-07-22 19:26:56 -0500132 catch (const sdbusplus::exception_t& ex)
Alex.Song8f73ad72021-10-07 00:18:27 +0800133 {
134 // unsupported Available property, leaving reading at 'True'
135 }
136 prop->available = available;
137
Patrick Venture863b9242018-03-08 08:29:23 -0800138 return;
139}
140
Patrick Venture9b936922020-08-10 11:28:39 -0700141bool DbusHelper::thresholdsAsserted(const std::string& service,
James Feist36b7d8e2018-10-05 15:39:01 -0700142 const std::string& path)
143{
Patrick Venture8729eb92020-08-10 10:38:44 -0700144 auto critical = _bus.new_method_call(service.c_str(), path.c_str(),
145 propertiesintf, "GetAll");
James Feist36b7d8e2018-10-05 15:39:01 -0700146 critical.append(criticalThreshInf);
147 PropertyMap criticalMap;
148
149 try
150 {
Patrick Venture8729eb92020-08-10 10:38:44 -0700151 auto msg = _bus.call(critical);
Patrick Venture4fd8cff2018-10-31 14:24:12 -0700152 msg.read(criticalMap);
James Feist36b7d8e2018-10-05 15:39:01 -0700153 }
Patrick Williams0001ee02021-10-06 14:44:22 -0500154 catch (const sdbusplus::exception_t&)
James Feist36b7d8e2018-10-05 15:39:01 -0700155 {
156 // do nothing, sensors don't have to expose critical thresholds
Jonico Eustaquioaf97d8e2024-01-02 14:35:07 -0600157#ifndef UNC_FAILSAFE
James Feist36b7d8e2018-10-05 15:39:01 -0700158 return false;
Jonico Eustaquioaf97d8e2024-01-02 14:35:07 -0600159#endif
James Feist36b7d8e2018-10-05 15:39:01 -0700160 }
161
162 auto findCriticalLow = criticalMap.find("CriticalAlarmLow");
163 auto findCriticalHigh = criticalMap.find("CriticalAlarmHigh");
164
165 bool asserted = false;
166 if (findCriticalLow != criticalMap.end())
167 {
James Feist1f802f52019-02-08 13:51:43 -0800168 asserted = std::get<bool>(findCriticalLow->second);
James Feist36b7d8e2018-10-05 15:39:01 -0700169 }
170
171 // as we are catching properties changed, a sensor could theoretically jump
172 // from one threshold to the other in one event, so check both thresholds
173 if (!asserted && findCriticalHigh != criticalMap.end())
174 {
James Feist1f802f52019-02-08 13:51:43 -0800175 asserted = std::get<bool>(findCriticalHigh->second);
James Feist36b7d8e2018-10-05 15:39:01 -0700176 }
Jonico Eustaquioaf97d8e2024-01-02 14:35:07 -0600177#ifdef UNC_FAILSAFE
178 if (!asserted)
179 {
180 auto warning = _bus.new_method_call(service.c_str(), path.c_str(),
181 propertiesintf, "GetAll");
182 warning.append(warningThreshInf);
183 PropertyMap warningMap;
184
185 try
186 {
187 auto msg = _bus.call(warning);
188 msg.read(warningMap);
189 }
190 catch (const sdbusplus::exception_t&)
191 {
192 // sensors don't have to expose non-critical thresholds
193 return false;
194 }
195 auto findWarningHigh = warningMap.find("WarningAlarmHigh");
196
197 if (findWarningHigh != warningMap.end())
198 {
199 asserted = std::get<bool>(findWarningHigh->second);
200 }
201 }
202#endif
James Feist36b7d8e2018-10-05 15:39:01 -0700203 return asserted;
204}
205
Patrick Venturea0764872020-08-08 07:48:43 -0700206} // namespace pid_control