blob: 2d50c723aeab3463db18bb7fa3e9cda314c88773 [file] [log] [blame]
Patrick Ventureaadb30d2020-08-10 09:17:11 -07001/**
2 * Copyright 2017 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "dbushelper.hpp"
18
19#include "dbushelper_interface.hpp"
20#include "dbusutil.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070021
Patrick Venturea83a3ec2020-08-04 09:52:05 -070022#include <phosphor-logging/log.hpp>
Patrick Ventureaadb30d2020-08-10 09:17:11 -070023#include <sdbusplus/bus.hpp>
Ed Tanousf8b6e552025-06-27 13:27:50 -070024#include <sdbusplus/exception.hpp>
Patrick Venturea83a3ec2020-08-04 09:52:05 -070025
Ed Tanousf8b6e552025-06-27 13:27:50 -070026#include <cstdint>
Patrick Ventureaadb30d2020-08-10 09:17:11 -070027#include <map>
Ed Tanousf8b6e552025-06-27 13:27:50 -070028#include <stdexcept>
Patrick Ventureaadb30d2020-08-10 09:17:11 -070029#include <string>
James Feist1f802f52019-02-08 13:51:43 -080030#include <variant>
Patrick Ventureaadb30d2020-08-10 09:17:11 -070031#include <vector>
32
33namespace pid_control
34{
Patrick Venture863b9242018-03-08 08:29:23 -080035
Patrick Venture863b9242018-03-08 08:29:23 -080036using Property = std::string;
James Feist1f802f52019-02-08 13:51:43 -080037using Value = std::variant<int64_t, double, std::string, bool>;
Patrick Venture863b9242018-03-08 08:29:23 -080038using PropertyMap = std::map<Property, Value>;
39
Patrick Venture34ddc902018-10-30 11:05:17 -070040using namespace phosphor::logging;
41
Patrick Venture863b9242018-03-08 08:29:23 -080042/* TODO(venture): Basically all phosphor apps need this, maybe it should be a
43 * part of sdbusplus. There is an old version in libmapper.
44 */
Patrick Venture9b936922020-08-10 11:28:39 -070045std::string DbusHelper::getService(const std::string& intf,
Patrick Venture0df7c0f2018-06-13 09:02:13 -070046 const std::string& path)
Patrick Venture863b9242018-03-08 08:29:23 -080047{
Patrick Williamsbd63bca2024-08-16 15:21:10 -040048 auto mapper =
49 _bus.new_method_call("xyz.openbmc_project.ObjectMapper",
50 "/xyz/openbmc_project/object_mapper",
51 "xyz.openbmc_project.ObjectMapper", "GetObject");
Patrick Venture863b9242018-03-08 08:29:23 -080052
53 mapper.append(path);
54 mapper.append(std::vector<std::string>({intf}));
55
Patrick Venture863b9242018-03-08 08:29:23 -080056 std::map<std::string, std::vector<std::string>> response;
Patrick Venture34ddc902018-10-30 11:05:17 -070057
58 try
59 {
Patrick Venture8729eb92020-08-10 10:38:44 -070060 auto responseMsg = _bus.call(mapper);
Patrick Venture34ddc902018-10-30 11:05:17 -070061
62 responseMsg.read(response);
63 }
Patrick Williamsb228bc32022-07-22 19:26:56 -050064 catch (const sdbusplus::exception_t& ex)
Patrick Venture34ddc902018-10-30 11:05:17 -070065 {
66 log<level::ERR>("ObjectMapper call failure",
67 entry("WHAT=%s", ex.what()));
68 throw;
69 }
Patrick Venture863b9242018-03-08 08:29:23 -080070
71 if (response.begin() == response.end())
72 {
73 throw std::runtime_error("Unable to find Object: " + path);
74 }
75
76 return response.begin()->first;
77}
78
Patrick Venture9b936922020-08-10 11:28:39 -070079void DbusHelper::getProperties(const std::string& service,
Patrick Venture1df9e872020-10-08 15:35:01 -070080 const std::string& path, SensorProperties* prop)
Patrick Venture863b9242018-03-08 08:29:23 -080081{
Patrick Venture8729eb92020-08-10 10:38:44 -070082 auto pimMsg = _bus.new_method_call(service.c_str(), path.c_str(),
83 propertiesintf, "GetAll");
Patrick Venture863b9242018-03-08 08:29:23 -080084
85 pimMsg.append(sensorintf);
Patrick Venture863b9242018-03-08 08:29:23 -080086
Patrick Venture7dbc5172018-10-30 12:18:45 -070087 PropertyMap propMap;
88
89 try
Patrick Venture863b9242018-03-08 08:29:23 -080090 {
Patrick Venture8729eb92020-08-10 10:38:44 -070091 auto valueResponseMsg = _bus.call(pimMsg);
Patrick Venture7dbc5172018-10-30 12:18:45 -070092 valueResponseMsg.read(propMap);
93 }
Patrick Williamsb228bc32022-07-22 19:26:56 -050094 catch (const sdbusplus::exception_t& ex)
Patrick Venture7dbc5172018-10-30 12:18:45 -070095 {
96 log<level::ERR>("GetAll Properties Failed",
97 entry("WHAT=%s", ex.what()));
98 throw;
Patrick Venture863b9242018-03-08 08:29:23 -080099 }
100
101 // The PropertyMap returned will look like this because it's always
102 // reading a Sensor.Value interface.
103 // a{sv} 3:
104 // "Value" x 24875
105 // "Unit" s "xyz.openbmc_project.Sensor.Value.Unit.DegreesC"
106 // "Scale" x -3
Patrick Venture863b9242018-03-08 08:29:23 -0800107
Patrick Venture0d73b102018-05-09 10:29:42 -0700108 // If no error was set, the values should all be there.
James Feistc065cf12018-07-05 10:23:11 -0700109 auto findUnit = propMap.find("Unit");
110 if (findUnit != propMap.end())
111 {
James Feist1f802f52019-02-08 13:51:43 -0800112 prop->unit = std::get<std::string>(findUnit->second);
James Feistc065cf12018-07-05 10:23:11 -0700113 }
114 auto findScale = propMap.find("Scale");
James Feist75eb7692019-02-25 12:50:02 -0800115 auto findMax = propMap.find("MaxValue");
116 auto findMin = propMap.find("MinValue");
117
118 prop->min = 0;
119 prop->max = 0;
120 prop->scale = 0;
James Feistc065cf12018-07-05 10:23:11 -0700121 if (findScale != propMap.end())
122 {
James Feist1f802f52019-02-08 13:51:43 -0800123 prop->scale = std::get<int64_t>(findScale->second);
James Feistc065cf12018-07-05 10:23:11 -0700124 }
James Feist75eb7692019-02-25 12:50:02 -0800125 if (findMax != propMap.end())
James Feistc065cf12018-07-05 10:23:11 -0700126 {
James Feist75eb7692019-02-25 12:50:02 -0800127 prop->max = std::visit(VariantToDoubleVisitor(), findMax->second);
128 }
129 if (findMin != propMap.end())
130 {
131 prop->min = std::visit(VariantToDoubleVisitor(), findMin->second);
James Feistc065cf12018-07-05 10:23:11 -0700132 }
133
James Feist1f802f52019-02-08 13:51:43 -0800134 prop->value = std::visit(VariantToDoubleVisitor(), propMap["Value"]);
Patrick Venture863b9242018-03-08 08:29:23 -0800135
Alex.Song8f73ad72021-10-07 00:18:27 +0800136 bool available = true;
137 try
138 {
139 getProperty(service, path, availabilityIntf, "Available", available);
140 }
Patrick Williamsb228bc32022-07-22 19:26:56 -0500141 catch (const sdbusplus::exception_t& ex)
Alex.Song8f73ad72021-10-07 00:18:27 +0800142 {
143 // unsupported Available property, leaving reading at 'True'
144 }
145 prop->available = available;
146
Patrick Venture863b9242018-03-08 08:29:23 -0800147 return;
148}
149
Patrick Venture9b936922020-08-10 11:28:39 -0700150bool DbusHelper::thresholdsAsserted(const std::string& service,
James Feist36b7d8e2018-10-05 15:39:01 -0700151 const std::string& path)
152{
Patrick Venture8729eb92020-08-10 10:38:44 -0700153 auto critical = _bus.new_method_call(service.c_str(), path.c_str(),
154 propertiesintf, "GetAll");
James Feist36b7d8e2018-10-05 15:39:01 -0700155 critical.append(criticalThreshInf);
156 PropertyMap criticalMap;
157
158 try
159 {
Patrick Venture8729eb92020-08-10 10:38:44 -0700160 auto msg = _bus.call(critical);
Patrick Venture4fd8cff2018-10-31 14:24:12 -0700161 msg.read(criticalMap);
James Feist36b7d8e2018-10-05 15:39:01 -0700162 }
Patrick Williams0001ee02021-10-06 14:44:22 -0500163 catch (const sdbusplus::exception_t&)
James Feist36b7d8e2018-10-05 15:39:01 -0700164 {
165 // do nothing, sensors don't have to expose critical thresholds
Jonico Eustaquioaf97d8e2024-01-02 14:35:07 -0600166#ifndef UNC_FAILSAFE
James Feist36b7d8e2018-10-05 15:39:01 -0700167 return false;
Jonico Eustaquioaf97d8e2024-01-02 14:35:07 -0600168#endif
James Feist36b7d8e2018-10-05 15:39:01 -0700169 }
170
171 auto findCriticalLow = criticalMap.find("CriticalAlarmLow");
172 auto findCriticalHigh = criticalMap.find("CriticalAlarmHigh");
173
174 bool asserted = false;
175 if (findCriticalLow != criticalMap.end())
176 {
James Feist1f802f52019-02-08 13:51:43 -0800177 asserted = std::get<bool>(findCriticalLow->second);
James Feist36b7d8e2018-10-05 15:39:01 -0700178 }
179
180 // as we are catching properties changed, a sensor could theoretically jump
181 // from one threshold to the other in one event, so check both thresholds
182 if (!asserted && findCriticalHigh != criticalMap.end())
183 {
James Feist1f802f52019-02-08 13:51:43 -0800184 asserted = std::get<bool>(findCriticalHigh->second);
James Feist36b7d8e2018-10-05 15:39:01 -0700185 }
Jonico Eustaquioaf97d8e2024-01-02 14:35:07 -0600186#ifdef UNC_FAILSAFE
187 if (!asserted)
188 {
189 auto warning = _bus.new_method_call(service.c_str(), path.c_str(),
190 propertiesintf, "GetAll");
191 warning.append(warningThreshInf);
192 PropertyMap warningMap;
193
194 try
195 {
196 auto msg = _bus.call(warning);
197 msg.read(warningMap);
198 }
199 catch (const sdbusplus::exception_t&)
200 {
201 // sensors don't have to expose non-critical thresholds
202 return false;
203 }
204 auto findWarningHigh = warningMap.find("WarningAlarmHigh");
205
206 if (findWarningHigh != warningMap.end())
207 {
208 asserted = std::get<bool>(findWarningHigh->second);
209 }
210 }
211#endif
James Feist36b7d8e2018-10-05 15:39:01 -0700212 return asserted;
213}
214
Patrick Venturea0764872020-08-08 07:48:43 -0700215} // namespace pid_control