blob: 5cd8ca4192bf63eeeb1ca22296a9bf36956e3776 [file] [log] [blame]
Patrick Venture863b9242018-03-08 08:29:23 -08001/**
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 */
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070016#include "dbuspassive.hpp"
17
James Feist98b704e2019-06-03 16:24:53 -070018#include "dbuspassiveredundancy.hpp"
James Feist0c8223b2019-05-08 15:33:33 -070019#include "util.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070020
Patrick Venturea83a3ec2020-08-04 09:52:05 -070021#include <sdbusplus/bus.hpp>
22
Patrick Venture863b9242018-03-08 08:29:23 -080023#include <chrono>
24#include <cmath>
Patrick Venture0ef1faf2018-06-13 12:50:53 -070025#include <memory>
Patrick Venture863b9242018-03-08 08:29:23 -080026#include <mutex>
Patrick Venture0ef1faf2018-06-13 12:50:53 -070027#include <string>
James Feist1f802f52019-02-08 13:51:43 -080028#include <variant>
Patrick Venture863b9242018-03-08 08:29:23 -080029
Patrick Venture563a3562018-10-30 09:31:26 -070030std::unique_ptr<ReadInterface> DbusPassive::createDbusPassive(
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070031 sdbusplus::bus::bus& bus, const std::string& type, const std::string& id,
James Feist98b704e2019-06-03 16:24:53 -070032 DbusHelperInterface* helper, const conf::SensorConfig* info,
33 const std::shared_ptr<DbusPassiveRedundancy>& redundancy)
Patrick Venture0ef1faf2018-06-13 12:50:53 -070034{
35 if (helper == nullptr)
36 {
37 return nullptr;
38 }
Patrick Venture7af157b2018-10-30 11:24:40 -070039 if (!validType(type))
Patrick Venture0ef1faf2018-06-13 12:50:53 -070040 {
41 return nullptr;
42 }
43
Patrick Venture863b9242018-03-08 08:29:23 -080044 /* Need to get the scale and initial value */
James Feist9fa90c12019-01-11 15:35:22 -080045 auto tempBus = sdbusplus::bus::new_system();
Patrick Venturef8cb4642018-10-30 12:02:53 -070046
Patrick Venture863b9242018-03-08 08:29:23 -080047 /* service == busname */
Patrick Venture7af157b2018-10-30 11:24:40 -070048 std::string path = getSensorPath(type, id);
Patrick Venture34ddc902018-10-30 11:05:17 -070049
Patrick Venture863b9242018-03-08 08:29:23 -080050 struct SensorProperties settings;
Patrick Venturef8cb4642018-10-30 12:02:53 -070051 bool failed;
Patrick Venture863b9242018-03-08 08:29:23 -080052
Patrick Venturef8cb4642018-10-30 12:02:53 -070053 try
54 {
55 std::string service = helper->getService(tempBus, sensorintf, path);
56
57 helper->getProperties(tempBus, service, path, &settings);
58 failed = helper->thresholdsAsserted(tempBus, service, path);
59 }
60 catch (const std::exception& e)
61 {
62 return nullptr;
63 }
64
Patrick Venture6b9f5992019-09-10 09:18:28 -070065 /* if these values are zero, they're ignored. */
66 if (info->ignoreDbusMinMax)
67 {
68 settings.min = 0;
69 settings.max = 0;
70 }
71
Patrick Venturef8cb4642018-10-30 12:02:53 -070072 return std::make_unique<DbusPassive>(bus, type, id, helper, settings,
James Feist98b704e2019-06-03 16:24:53 -070073 failed, path, redundancy);
Patrick Venturef8cb4642018-10-30 12:02:53 -070074}
75
James Feist98b704e2019-06-03 16:24:53 -070076DbusPassive::DbusPassive(
77 sdbusplus::bus::bus& bus, const std::string& type, const std::string& id,
78 DbusHelperInterface* helper, const struct SensorProperties& settings,
79 bool failed, const std::string& path,
80 const std::shared_ptr<DbusPassiveRedundancy>& redundancy) :
Patrick Venturef8cb4642018-10-30 12:02:53 -070081 ReadInterface(),
82 _bus(bus), _signal(bus, getMatch(type, id).c_str(), dbusHandleSignal, this),
James Feist98b704e2019-06-03 16:24:53 -070083 _id(id), _helper(helper), _failed(failed), path(path),
84 redundancy(redundancy)
85
Patrick Venturef8cb4642018-10-30 12:02:53 -070086{
Patrick Venture863b9242018-03-08 08:29:23 -080087 _scale = settings.scale;
88 _value = settings.value * pow(10, _scale);
James Feist75eb7692019-02-25 12:50:02 -080089 _min = settings.min * pow(10, _scale);
90 _max = settings.max * pow(10, _scale);
Patrick Venture863b9242018-03-08 08:29:23 -080091 _updated = std::chrono::high_resolution_clock::now();
92}
93
94ReadReturn DbusPassive::read(void)
95{
96 std::lock_guard<std::mutex> guard(_lock);
97
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070098 struct ReadReturn r = {_value, _updated};
Patrick Venture863b9242018-03-08 08:29:23 -080099
100 return r;
101}
102
103void DbusPassive::setValue(double value)
104{
105 std::lock_guard<std::mutex> guard(_lock);
106
107 _value = value;
108 _updated = std::chrono::high_resolution_clock::now();
109}
110
James Feist36b7d8e2018-10-05 15:39:01 -0700111bool DbusPassive::getFailed(void) const
112{
James Feist98b704e2019-06-03 16:24:53 -0700113 if (redundancy)
114 {
115 const std::set<std::string>& failures = redundancy->getFailed();
116 if (failures.find(path) != failures.end())
117 {
118 return true;
119 }
120 }
James Feist4b36f262020-07-07 16:56:41 -0700121
122 return _failed || !_functional;
James Feist36b7d8e2018-10-05 15:39:01 -0700123}
124
125void DbusPassive::setFailed(bool value)
126{
127 _failed = value;
128}
129
James Feist4b36f262020-07-07 16:56:41 -0700130void DbusPassive::setFunctional(bool value)
131{
132 _functional = value;
133}
134
Patrick Venture863b9242018-03-08 08:29:23 -0800135int64_t DbusPassive::getScale(void)
136{
137 return _scale;
138}
139
Patrick Venture563a3562018-10-30 09:31:26 -0700140std::string DbusPassive::getID(void)
Patrick Venture863b9242018-03-08 08:29:23 -0800141{
142 return _id;
143}
144
James Feist75eb7692019-02-25 12:50:02 -0800145double DbusPassive::getMax(void)
146{
147 return _max;
148}
149
150double DbusPassive::getMin(void)
151{
152 return _min;
153}
154
Patrick Venture7af157b2018-10-30 11:24:40 -0700155int handleSensorValue(sdbusplus::message::message& msg, DbusPassive* owner)
Patrick Venture863b9242018-03-08 08:29:23 -0800156{
Patrick Venture863b9242018-03-08 08:29:23 -0800157 std::string msgSensor;
James Feist1f802f52019-02-08 13:51:43 -0800158 std::map<std::string, std::variant<int64_t, double, bool>> msgData;
Patrick Ventured0c75662018-06-12 19:03:21 -0700159
160 msg.read(msgSensor, msgData);
Patrick Venture863b9242018-03-08 08:29:23 -0800161
162 if (msgSensor == "xyz.openbmc_project.Sensor.Value")
163 {
164 auto valPropMap = msgData.find("Value");
165 if (valPropMap != msgData.end())
166 {
James Feist1f802f52019-02-08 13:51:43 -0800167 double value =
168 std::visit(VariantToDoubleVisitor(), valPropMap->second);
Patrick Venture863b9242018-03-08 08:29:23 -0800169
James Feistc065cf12018-07-05 10:23:11 -0700170 value *= std::pow(10, owner->getScale());
Patrick Venture863b9242018-03-08 08:29:23 -0800171
James Feist75eb7692019-02-25 12:50:02 -0800172 scaleSensorReading(owner->getMin(), owner->getMax(), value);
173
Patrick Ventured0c75662018-06-12 19:03:21 -0700174 owner->setValue(value);
Patrick Venture863b9242018-03-08 08:29:23 -0800175 }
176 }
James Feist36b7d8e2018-10-05 15:39:01 -0700177 else if (msgSensor == "xyz.openbmc_project.Sensor.Threshold.Critical")
178 {
179 auto criticalAlarmLow = msgData.find("CriticalAlarmLow");
180 auto criticalAlarmHigh = msgData.find("CriticalAlarmHigh");
181 if (criticalAlarmHigh == msgData.end() &&
182 criticalAlarmLow == msgData.end())
183 {
184 return 0;
185 }
186
187 bool asserted = false;
188 if (criticalAlarmLow != msgData.end())
189 {
James Feist1f802f52019-02-08 13:51:43 -0800190 asserted = std::get<bool>(criticalAlarmLow->second);
James Feist36b7d8e2018-10-05 15:39:01 -0700191 }
192
193 // checking both as in theory you could de-assert one threshold and
194 // assert the other at the same moment
195 if (!asserted && criticalAlarmHigh != msgData.end())
196 {
James Feist1f802f52019-02-08 13:51:43 -0800197 asserted = std::get<bool>(criticalAlarmHigh->second);
James Feist36b7d8e2018-10-05 15:39:01 -0700198 }
199 owner->setFailed(asserted);
200 }
James Feist4b36f262020-07-07 16:56:41 -0700201 else if (msgSensor ==
202 "xyz.openbmc_project.State.Decorator.OperationalStatus")
203 {
204 auto functional = msgData.find("Functional");
205 if (functional == msgData.end())
206 {
207 return 0;
208 }
209 bool asserted = std::get<bool>(functional->second);
210 owner->setFunctional(asserted);
211 }
Patrick Venture863b9242018-03-08 08:29:23 -0800212
213 return 0;
214}
Patrick Ventured0c75662018-06-12 19:03:21 -0700215
Patrick Venture7af157b2018-10-30 11:24:40 -0700216int dbusHandleSignal(sd_bus_message* msg, void* usrData, sd_bus_error* err)
Patrick Ventured0c75662018-06-12 19:03:21 -0700217{
218 auto sdbpMsg = sdbusplus::message::message(msg);
219 DbusPassive* obj = static_cast<DbusPassive*>(usrData);
220
Patrick Venture7af157b2018-10-30 11:24:40 -0700221 return handleSensorValue(sdbpMsg, obj);
Patrick Ventured0c75662018-06-12 19:03:21 -0700222}