blob: 02d1beb2ea7efacfb7ebc3007bf468e9495489a0 [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 Venture863b9242018-03-08 08:29:23 -080021#include <chrono>
22#include <cmath>
Patrick Venture0ef1faf2018-06-13 12:50:53 -070023#include <memory>
Patrick Venture863b9242018-03-08 08:29:23 -080024#include <mutex>
Patrick Venture0ef1faf2018-06-13 12:50:53 -070025#include <sdbusplus/bus.hpp>
26#include <string>
James Feist1f802f52019-02-08 13:51:43 -080027#include <variant>
Patrick Venture863b9242018-03-08 08:29:23 -080028
Patrick Venture563a3562018-10-30 09:31:26 -070029std::unique_ptr<ReadInterface> DbusPassive::createDbusPassive(
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070030 sdbusplus::bus::bus& bus, const std::string& type, const std::string& id,
James Feist98b704e2019-06-03 16:24:53 -070031 DbusHelperInterface* helper, const conf::SensorConfig* info,
32 const std::shared_ptr<DbusPassiveRedundancy>& redundancy)
Patrick Venture0ef1faf2018-06-13 12:50:53 -070033{
34 if (helper == nullptr)
35 {
36 return nullptr;
37 }
Patrick Venture7af157b2018-10-30 11:24:40 -070038 if (!validType(type))
Patrick Venture0ef1faf2018-06-13 12:50:53 -070039 {
40 return nullptr;
41 }
42
Patrick Venture863b9242018-03-08 08:29:23 -080043 /* Need to get the scale and initial value */
James Feist9fa90c12019-01-11 15:35:22 -080044 auto tempBus = sdbusplus::bus::new_system();
Patrick Venturef8cb4642018-10-30 12:02:53 -070045
Patrick Venture863b9242018-03-08 08:29:23 -080046 /* service == busname */
Patrick Venture7af157b2018-10-30 11:24:40 -070047 std::string path = getSensorPath(type, id);
Patrick Venture34ddc902018-10-30 11:05:17 -070048
Patrick Venture863b9242018-03-08 08:29:23 -080049 struct SensorProperties settings;
Patrick Venturef8cb4642018-10-30 12:02:53 -070050 bool failed;
Patrick Venture863b9242018-03-08 08:29:23 -080051
Patrick Venturef8cb4642018-10-30 12:02:53 -070052 try
53 {
54 std::string service = helper->getService(tempBus, sensorintf, path);
55
56 helper->getProperties(tempBus, service, path, &settings);
57 failed = helper->thresholdsAsserted(tempBus, service, path);
58 }
59 catch (const std::exception& e)
60 {
61 return nullptr;
62 }
63
Patrick Venture6b9f5992019-09-10 09:18:28 -070064 /* if these values are zero, they're ignored. */
65 if (info->ignoreDbusMinMax)
66 {
67 settings.min = 0;
68 settings.max = 0;
69 }
70
Patrick Venturef8cb4642018-10-30 12:02:53 -070071 return std::make_unique<DbusPassive>(bus, type, id, helper, settings,
James Feist98b704e2019-06-03 16:24:53 -070072 failed, path, redundancy);
Patrick Venturef8cb4642018-10-30 12:02:53 -070073}
74
James Feist98b704e2019-06-03 16:24:53 -070075DbusPassive::DbusPassive(
76 sdbusplus::bus::bus& bus, const std::string& type, const std::string& id,
77 DbusHelperInterface* helper, const struct SensorProperties& settings,
78 bool failed, const std::string& path,
79 const std::shared_ptr<DbusPassiveRedundancy>& redundancy) :
Patrick Venturef8cb4642018-10-30 12:02:53 -070080 ReadInterface(),
81 _bus(bus), _signal(bus, getMatch(type, id).c_str(), dbusHandleSignal, this),
James Feist98b704e2019-06-03 16:24:53 -070082 _id(id), _helper(helper), _failed(failed), path(path),
83 redundancy(redundancy)
84
Patrick Venturef8cb4642018-10-30 12:02:53 -070085{
Patrick Venture863b9242018-03-08 08:29:23 -080086 _scale = settings.scale;
87 _value = settings.value * pow(10, _scale);
James Feist75eb7692019-02-25 12:50:02 -080088 _min = settings.min * pow(10, _scale);
89 _max = settings.max * pow(10, _scale);
Patrick Venture863b9242018-03-08 08:29:23 -080090 _updated = std::chrono::high_resolution_clock::now();
91}
92
93ReadReturn DbusPassive::read(void)
94{
95 std::lock_guard<std::mutex> guard(_lock);
96
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070097 struct ReadReturn r = {_value, _updated};
Patrick Venture863b9242018-03-08 08:29:23 -080098
99 return r;
100}
101
102void DbusPassive::setValue(double value)
103{
104 std::lock_guard<std::mutex> guard(_lock);
105
106 _value = value;
107 _updated = std::chrono::high_resolution_clock::now();
108}
109
James Feist36b7d8e2018-10-05 15:39:01 -0700110bool DbusPassive::getFailed(void) const
111{
James Feist98b704e2019-06-03 16:24:53 -0700112 if (redundancy)
113 {
114 const std::set<std::string>& failures = redundancy->getFailed();
115 if (failures.find(path) != failures.end())
116 {
117 return true;
118 }
119 }
James Feist4b36f262020-07-07 16:56:41 -0700120
121 return _failed || !_functional;
James Feist36b7d8e2018-10-05 15:39:01 -0700122}
123
124void DbusPassive::setFailed(bool value)
125{
126 _failed = value;
127}
128
James Feist4b36f262020-07-07 16:56:41 -0700129void DbusPassive::setFunctional(bool value)
130{
131 _functional = value;
132}
133
Patrick Venture863b9242018-03-08 08:29:23 -0800134int64_t DbusPassive::getScale(void)
135{
136 return _scale;
137}
138
Patrick Venture563a3562018-10-30 09:31:26 -0700139std::string DbusPassive::getID(void)
Patrick Venture863b9242018-03-08 08:29:23 -0800140{
141 return _id;
142}
143
James Feist75eb7692019-02-25 12:50:02 -0800144double DbusPassive::getMax(void)
145{
146 return _max;
147}
148
149double DbusPassive::getMin(void)
150{
151 return _min;
152}
153
Patrick Venture7af157b2018-10-30 11:24:40 -0700154int handleSensorValue(sdbusplus::message::message& msg, DbusPassive* owner)
Patrick Venture863b9242018-03-08 08:29:23 -0800155{
Patrick Venture863b9242018-03-08 08:29:23 -0800156 std::string msgSensor;
James Feist1f802f52019-02-08 13:51:43 -0800157 std::map<std::string, std::variant<int64_t, double, bool>> msgData;
Patrick Ventured0c75662018-06-12 19:03:21 -0700158
159 msg.read(msgSensor, msgData);
Patrick Venture863b9242018-03-08 08:29:23 -0800160
161 if (msgSensor == "xyz.openbmc_project.Sensor.Value")
162 {
163 auto valPropMap = msgData.find("Value");
164 if (valPropMap != msgData.end())
165 {
James Feist1f802f52019-02-08 13:51:43 -0800166 double value =
167 std::visit(VariantToDoubleVisitor(), valPropMap->second);
Patrick Venture863b9242018-03-08 08:29:23 -0800168
James Feistc065cf12018-07-05 10:23:11 -0700169 value *= std::pow(10, owner->getScale());
Patrick Venture863b9242018-03-08 08:29:23 -0800170
James Feist75eb7692019-02-25 12:50:02 -0800171 scaleSensorReading(owner->getMin(), owner->getMax(), value);
172
Patrick Ventured0c75662018-06-12 19:03:21 -0700173 owner->setValue(value);
Patrick Venture863b9242018-03-08 08:29:23 -0800174 }
175 }
James Feist36b7d8e2018-10-05 15:39:01 -0700176 else if (msgSensor == "xyz.openbmc_project.Sensor.Threshold.Critical")
177 {
178 auto criticalAlarmLow = msgData.find("CriticalAlarmLow");
179 auto criticalAlarmHigh = msgData.find("CriticalAlarmHigh");
180 if (criticalAlarmHigh == msgData.end() &&
181 criticalAlarmLow == msgData.end())
182 {
183 return 0;
184 }
185
186 bool asserted = false;
187 if (criticalAlarmLow != msgData.end())
188 {
James Feist1f802f52019-02-08 13:51:43 -0800189 asserted = std::get<bool>(criticalAlarmLow->second);
James Feist36b7d8e2018-10-05 15:39:01 -0700190 }
191
192 // checking both as in theory you could de-assert one threshold and
193 // assert the other at the same moment
194 if (!asserted && criticalAlarmHigh != msgData.end())
195 {
James Feist1f802f52019-02-08 13:51:43 -0800196 asserted = std::get<bool>(criticalAlarmHigh->second);
James Feist36b7d8e2018-10-05 15:39:01 -0700197 }
198 owner->setFailed(asserted);
199 }
James Feist4b36f262020-07-07 16:56:41 -0700200 else if (msgSensor ==
201 "xyz.openbmc_project.State.Decorator.OperationalStatus")
202 {
203 auto functional = msgData.find("Functional");
204 if (functional == msgData.end())
205 {
206 return 0;
207 }
208 bool asserted = std::get<bool>(functional->second);
209 owner->setFunctional(asserted);
210 }
Patrick Venture863b9242018-03-08 08:29:23 -0800211
212 return 0;
213}
Patrick Ventured0c75662018-06-12 19:03:21 -0700214
Patrick Venture7af157b2018-10-30 11:24:40 -0700215int dbusHandleSignal(sd_bus_message* msg, void* usrData, sd_bus_error* err)
Patrick Ventured0c75662018-06-12 19:03:21 -0700216{
217 auto sdbpMsg = sdbusplus::message::message(msg);
218 DbusPassive* obj = static_cast<DbusPassive*>(usrData);
219
Patrick Venture7af157b2018-10-30 11:24:40 -0700220 return handleSensorValue(sdbpMsg, obj);
Patrick Ventured0c75662018-06-12 19:03:21 -0700221}