blob: 6c1e1c08f916501f7342c60c477c1facf2e899b5 [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 */
Jonico Eustaquioaf97d8e2024-01-02 14:35:07 -060016#include "config.h"
Patrick Ventureaadb30d2020-08-10 09:17:11 -070017
18#include "dbushelper.hpp"
19
20#include "dbushelper_interface.hpp"
21#include "dbusutil.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070022
Patrick Venturea83a3ec2020-08-04 09:52:05 -070023#include <phosphor-logging/log.hpp>
Patrick Ventureaadb30d2020-08-10 09:17:11 -070024#include <sdbusplus/bus.hpp>
Patrick Venturea83a3ec2020-08-04 09:52:05 -070025
Patrick Ventureaadb30d2020-08-10 09:17:11 -070026#include <map>
27#include <string>
James Feist1f802f52019-02-08 13:51:43 -080028#include <variant>
Patrick Ventureaadb30d2020-08-10 09:17:11 -070029#include <vector>
30
31namespace pid_control
32{
Patrick Venture863b9242018-03-08 08:29:23 -080033
Patrick Venture863b9242018-03-08 08:29:23 -080034using Property = std::string;
James Feist1f802f52019-02-08 13:51:43 -080035using Value = std::variant<int64_t, double, std::string, bool>;
Patrick Venture863b9242018-03-08 08:29:23 -080036using PropertyMap = std::map<Property, Value>;
37
Patrick Venture34ddc902018-10-30 11:05:17 -070038using namespace phosphor::logging;
39
Patrick Venture863b9242018-03-08 08:29:23 -080040/* TODO(venture): Basically all phosphor apps need this, maybe it should be a
41 * part of sdbusplus. There is an old version in libmapper.
42 */
Patrick Venture9b936922020-08-10 11:28:39 -070043std::string DbusHelper::getService(const std::string& intf,
Patrick Venture0df7c0f2018-06-13 09:02:13 -070044 const std::string& path)
Patrick Venture863b9242018-03-08 08:29:23 -080045{
Patrick Williamsbd63bca2024-08-16 15:21:10 -040046 auto mapper =
47 _bus.new_method_call("xyz.openbmc_project.ObjectMapper",
48 "/xyz/openbmc_project/object_mapper",
49 "xyz.openbmc_project.ObjectMapper", "GetObject");
Patrick Venture863b9242018-03-08 08:29:23 -080050
51 mapper.append(path);
52 mapper.append(std::vector<std::string>({intf}));
53
Patrick Venture863b9242018-03-08 08:29:23 -080054 std::map<std::string, std::vector<std::string>> response;
Patrick Venture34ddc902018-10-30 11:05:17 -070055
56 try
57 {
Patrick Venture8729eb92020-08-10 10:38:44 -070058 auto responseMsg = _bus.call(mapper);
Patrick Venture34ddc902018-10-30 11:05:17 -070059
60 responseMsg.read(response);
61 }
Patrick Williamsb228bc32022-07-22 19:26:56 -050062 catch (const sdbusplus::exception_t& ex)
Patrick Venture34ddc902018-10-30 11:05:17 -070063 {
64 log<level::ERR>("ObjectMapper call failure",
65 entry("WHAT=%s", ex.what()));
66 throw;
67 }
Patrick Venture863b9242018-03-08 08:29:23 -080068
69 if (response.begin() == response.end())
70 {
71 throw std::runtime_error("Unable to find Object: " + path);
72 }
73
74 return response.begin()->first;
75}
76
Patrick Venture9b936922020-08-10 11:28:39 -070077void DbusHelper::getProperties(const std::string& service,
Patrick Venture1df9e872020-10-08 15:35:01 -070078 const std::string& path, SensorProperties* prop)
Patrick Venture863b9242018-03-08 08:29:23 -080079{
Patrick Venture8729eb92020-08-10 10:38:44 -070080 auto pimMsg = _bus.new_method_call(service.c_str(), path.c_str(),
81 propertiesintf, "GetAll");
Patrick Venture863b9242018-03-08 08:29:23 -080082
83 pimMsg.append(sensorintf);
Patrick Venture863b9242018-03-08 08:29:23 -080084
Patrick Venture7dbc5172018-10-30 12:18:45 -070085 PropertyMap propMap;
86
87 try
Patrick Venture863b9242018-03-08 08:29:23 -080088 {
Patrick Venture8729eb92020-08-10 10:38:44 -070089 auto valueResponseMsg = _bus.call(pimMsg);
Patrick Venture7dbc5172018-10-30 12:18:45 -070090 valueResponseMsg.read(propMap);
91 }
Patrick Williamsb228bc32022-07-22 19:26:56 -050092 catch (const sdbusplus::exception_t& ex)
Patrick Venture7dbc5172018-10-30 12:18:45 -070093 {
94 log<level::ERR>("GetAll Properties Failed",
95 entry("WHAT=%s", ex.what()));
96 throw;
Patrick Venture863b9242018-03-08 08:29:23 -080097 }
98
99 // The PropertyMap returned will look like this because it's always
100 // reading a Sensor.Value interface.
101 // a{sv} 3:
102 // "Value" x 24875
103 // "Unit" s "xyz.openbmc_project.Sensor.Value.Unit.DegreesC"
104 // "Scale" x -3
Patrick Venture863b9242018-03-08 08:29:23 -0800105
Patrick Venture0d73b102018-05-09 10:29:42 -0700106 // If no error was set, the values should all be there.
James Feistc065cf12018-07-05 10:23:11 -0700107 auto findUnit = propMap.find("Unit");
108 if (findUnit != propMap.end())
109 {
James Feist1f802f52019-02-08 13:51:43 -0800110 prop->unit = std::get<std::string>(findUnit->second);
James Feistc065cf12018-07-05 10:23:11 -0700111 }
112 auto findScale = propMap.find("Scale");
James Feist75eb7692019-02-25 12:50:02 -0800113 auto findMax = propMap.find("MaxValue");
114 auto findMin = propMap.find("MinValue");
115
116 prop->min = 0;
117 prop->max = 0;
118 prop->scale = 0;
James Feistc065cf12018-07-05 10:23:11 -0700119 if (findScale != propMap.end())
120 {
James Feist1f802f52019-02-08 13:51:43 -0800121 prop->scale = std::get<int64_t>(findScale->second);
James Feistc065cf12018-07-05 10:23:11 -0700122 }
James Feist75eb7692019-02-25 12:50:02 -0800123 if (findMax != propMap.end())
James Feistc065cf12018-07-05 10:23:11 -0700124 {
James Feist75eb7692019-02-25 12:50:02 -0800125 prop->max = std::visit(VariantToDoubleVisitor(), findMax->second);
126 }
127 if (findMin != propMap.end())
128 {
129 prop->min = std::visit(VariantToDoubleVisitor(), findMin->second);
James Feistc065cf12018-07-05 10:23:11 -0700130 }
131
James Feist1f802f52019-02-08 13:51:43 -0800132 prop->value = std::visit(VariantToDoubleVisitor(), propMap["Value"]);
Patrick Venture863b9242018-03-08 08:29:23 -0800133
Alex.Song8f73ad72021-10-07 00:18:27 +0800134 bool available = true;
135 try
136 {
137 getProperty(service, path, availabilityIntf, "Available", available);
138 }
Patrick Williamsb228bc32022-07-22 19:26:56 -0500139 catch (const sdbusplus::exception_t& ex)
Alex.Song8f73ad72021-10-07 00:18:27 +0800140 {
141 // unsupported Available property, leaving reading at 'True'
142 }
143 prop->available = available;
144
Patrick Venture863b9242018-03-08 08:29:23 -0800145 return;
146}
147
Patrick Venture9b936922020-08-10 11:28:39 -0700148bool DbusHelper::thresholdsAsserted(const std::string& service,
James Feist36b7d8e2018-10-05 15:39:01 -0700149 const std::string& path)
150{
Patrick Venture8729eb92020-08-10 10:38:44 -0700151 auto critical = _bus.new_method_call(service.c_str(), path.c_str(),
152 propertiesintf, "GetAll");
James Feist36b7d8e2018-10-05 15:39:01 -0700153 critical.append(criticalThreshInf);
154 PropertyMap criticalMap;
155
156 try
157 {
Patrick Venture8729eb92020-08-10 10:38:44 -0700158 auto msg = _bus.call(critical);
Patrick Venture4fd8cff2018-10-31 14:24:12 -0700159 msg.read(criticalMap);
James Feist36b7d8e2018-10-05 15:39:01 -0700160 }
Patrick Williams0001ee02021-10-06 14:44:22 -0500161 catch (const sdbusplus::exception_t&)
James Feist36b7d8e2018-10-05 15:39:01 -0700162 {
163 // do nothing, sensors don't have to expose critical thresholds
Jonico Eustaquioaf97d8e2024-01-02 14:35:07 -0600164#ifndef UNC_FAILSAFE
James Feist36b7d8e2018-10-05 15:39:01 -0700165 return false;
Jonico Eustaquioaf97d8e2024-01-02 14:35:07 -0600166#endif
James Feist36b7d8e2018-10-05 15:39:01 -0700167 }
168
169 auto findCriticalLow = criticalMap.find("CriticalAlarmLow");
170 auto findCriticalHigh = criticalMap.find("CriticalAlarmHigh");
171
172 bool asserted = false;
173 if (findCriticalLow != criticalMap.end())
174 {
James Feist1f802f52019-02-08 13:51:43 -0800175 asserted = std::get<bool>(findCriticalLow->second);
James Feist36b7d8e2018-10-05 15:39:01 -0700176 }
177
178 // as we are catching properties changed, a sensor could theoretically jump
179 // from one threshold to the other in one event, so check both thresholds
180 if (!asserted && findCriticalHigh != criticalMap.end())
181 {
James Feist1f802f52019-02-08 13:51:43 -0800182 asserted = std::get<bool>(findCriticalHigh->second);
James Feist36b7d8e2018-10-05 15:39:01 -0700183 }
Jonico Eustaquioaf97d8e2024-01-02 14:35:07 -0600184#ifdef UNC_FAILSAFE
185 if (!asserted)
186 {
187 auto warning = _bus.new_method_call(service.c_str(), path.c_str(),
188 propertiesintf, "GetAll");
189 warning.append(warningThreshInf);
190 PropertyMap warningMap;
191
192 try
193 {
194 auto msg = _bus.call(warning);
195 msg.read(warningMap);
196 }
197 catch (const sdbusplus::exception_t&)
198 {
199 // sensors don't have to expose non-critical thresholds
200 return false;
201 }
202 auto findWarningHigh = warningMap.find("WarningAlarmHigh");
203
204 if (findWarningHigh != warningMap.end())
205 {
206 asserted = std::get<bool>(findWarningHigh->second);
207 }
208 }
209#endif
James Feist36b7d8e2018-10-05 15:39:01 -0700210 return asserted;
211}
212
Patrick Venturea0764872020-08-08 07:48:43 -0700213} // namespace pid_control