1 /**
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 #include "dbuspassive.hpp"
17 
18 #include "dbuspassiveredundancy.hpp"
19 #include "util.hpp"
20 
21 #include <chrono>
22 #include <cmath>
23 #include <memory>
24 #include <mutex>
25 #include <sdbusplus/bus.hpp>
26 #include <string>
27 #include <variant>
28 
29 std::unique_ptr<ReadInterface> DbusPassive::createDbusPassive(
30     sdbusplus::bus::bus& bus, const std::string& type, const std::string& id,
31     DbusHelperInterface* helper, const conf::SensorConfig* info,
32     const std::shared_ptr<DbusPassiveRedundancy>& redundancy)
33 {
34     if (helper == nullptr)
35     {
36         return nullptr;
37     }
38     if (!validType(type))
39     {
40         return nullptr;
41     }
42 
43     /* Need to get the scale and initial value */
44     auto tempBus = sdbusplus::bus::new_system();
45 
46     /* service == busname */
47     std::string path = getSensorPath(type, id);
48 
49     struct SensorProperties settings;
50     bool failed;
51 
52     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 
64     /* if these values are zero, they're ignored. */
65     if (info->ignoreDbusMinMax)
66     {
67         settings.min = 0;
68         settings.max = 0;
69     }
70 
71     return std::make_unique<DbusPassive>(bus, type, id, helper, settings,
72                                          failed, path, redundancy);
73 }
74 
75 DbusPassive::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) :
80     ReadInterface(),
81     _bus(bus), _signal(bus, getMatch(type, id).c_str(), dbusHandleSignal, this),
82     _id(id), _helper(helper), _failed(failed), path(path),
83     redundancy(redundancy)
84 
85 {
86     _scale = settings.scale;
87     _value = settings.value * pow(10, _scale);
88     _min = settings.min * pow(10, _scale);
89     _max = settings.max * pow(10, _scale);
90     _updated = std::chrono::high_resolution_clock::now();
91 }
92 
93 ReadReturn DbusPassive::read(void)
94 {
95     std::lock_guard<std::mutex> guard(_lock);
96 
97     struct ReadReturn r = {_value, _updated};
98 
99     return r;
100 }
101 
102 void 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 
110 bool DbusPassive::getFailed(void) const
111 {
112     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     }
120 
121     return _failed || !_functional;
122 }
123 
124 void DbusPassive::setFailed(bool value)
125 {
126     _failed = value;
127 }
128 
129 void DbusPassive::setFunctional(bool value)
130 {
131     _functional = value;
132 }
133 
134 int64_t DbusPassive::getScale(void)
135 {
136     return _scale;
137 }
138 
139 std::string DbusPassive::getID(void)
140 {
141     return _id;
142 }
143 
144 double DbusPassive::getMax(void)
145 {
146     return _max;
147 }
148 
149 double DbusPassive::getMin(void)
150 {
151     return _min;
152 }
153 
154 int handleSensorValue(sdbusplus::message::message& msg, DbusPassive* owner)
155 {
156     std::string msgSensor;
157     std::map<std::string, std::variant<int64_t, double, bool>> msgData;
158 
159     msg.read(msgSensor, msgData);
160 
161     if (msgSensor == "xyz.openbmc_project.Sensor.Value")
162     {
163         auto valPropMap = msgData.find("Value");
164         if (valPropMap != msgData.end())
165         {
166             double value =
167                 std::visit(VariantToDoubleVisitor(), valPropMap->second);
168 
169             value *= std::pow(10, owner->getScale());
170 
171             scaleSensorReading(owner->getMin(), owner->getMax(), value);
172 
173             owner->setValue(value);
174         }
175     }
176     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         {
189             asserted = std::get<bool>(criticalAlarmLow->second);
190         }
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         {
196             asserted = std::get<bool>(criticalAlarmHigh->second);
197         }
198         owner->setFailed(asserted);
199     }
200     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     }
211 
212     return 0;
213 }
214 
215 int dbusHandleSignal(sd_bus_message* msg, void* usrData, sd_bus_error* err)
216 {
217     auto sdbpMsg = sdbusplus::message::message(msg);
218     DbusPassive* obj = static_cast<DbusPassive*>(usrData);
219 
220     return handleSensorValue(sdbpMsg, obj);
221 }
222