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 "dbushelper_interface.hpp"
19 #include "dbuspassiveredundancy.hpp"
20 #include "dbusutil.hpp"
21 #include "util.hpp"
22 
23 #include <sdbusplus/bus.hpp>
24 
25 #include <chrono>
26 #include <cmath>
27 #include <memory>
28 #include <mutex>
29 #include <string>
30 #include <variant>
31 
32 namespace pid_control
33 {
34 
35 std::unique_ptr<ReadInterface> DbusPassive::createDbusPassive(
36     sdbusplus::bus::bus& bus, const std::string& type, const std::string& id,
37     std::unique_ptr<DbusHelperInterface> helper, const conf::SensorConfig* info,
38     const std::shared_ptr<DbusPassiveRedundancy>& redundancy)
39 {
40     if (helper == nullptr)
41     {
42         return nullptr;
43     }
44     if (!validType(type))
45     {
46         return nullptr;
47     }
48 
49     /* Need to get the scale and initial value */
50     /* service == busname */
51     std::string path = getSensorPath(type, id);
52 
53     struct SensorProperties settings;
54     bool failed;
55 
56     try
57     {
58         std::string service = helper->getService(sensorintf, path);
59 
60         helper->getProperties(service, path, &settings);
61         failed = helper->thresholdsAsserted(service, path);
62     }
63     catch (const std::exception& e)
64     {
65         return nullptr;
66     }
67 
68     /* if these values are zero, they're ignored. */
69     if (info->ignoreDbusMinMax)
70     {
71         settings.min = 0;
72         settings.max = 0;
73     }
74 
75     return std::make_unique<DbusPassive>(bus, type, id, std::move(helper),
76                                          settings, failed, path, redundancy);
77 }
78 
79 DbusPassive::DbusPassive(
80     sdbusplus::bus::bus& bus, const std::string& type, const std::string& id,
81     std::unique_ptr<DbusHelperInterface> helper,
82     const struct SensorProperties& settings, bool failed,
83     const std::string& path,
84     const std::shared_ptr<DbusPassiveRedundancy>& redundancy) :
85     ReadInterface(),
86     _bus(bus), _signal(bus, getMatch(type, id).c_str(), dbusHandleSignal, this),
87     _id(id), _helper(std::move(helper)), _failed(failed), path(path),
88     redundancy(redundancy)
89 
90 {
91     _scale = settings.scale;
92     _value = settings.value * pow(10, _scale);
93     _min = settings.min * pow(10, _scale);
94     _max = settings.max * pow(10, _scale);
95     _updated = std::chrono::high_resolution_clock::now();
96 }
97 
98 ReadReturn DbusPassive::read(void)
99 {
100     std::lock_guard<std::mutex> guard(_lock);
101 
102     struct ReadReturn r = {_value, _updated};
103 
104     return r;
105 }
106 
107 void DbusPassive::setValue(double value)
108 {
109     std::lock_guard<std::mutex> guard(_lock);
110 
111     _value = value;
112     _updated = std::chrono::high_resolution_clock::now();
113 }
114 
115 bool DbusPassive::getFailed(void) const
116 {
117     if (redundancy)
118     {
119         const std::set<std::string>& failures = redundancy->getFailed();
120         if (failures.find(path) != failures.end())
121         {
122             return true;
123         }
124     }
125 
126     return _failed || !_functional;
127 }
128 
129 void DbusPassive::setFailed(bool value)
130 {
131     _failed = value;
132 }
133 
134 void DbusPassive::setFunctional(bool value)
135 {
136     _functional = value;
137 }
138 
139 int64_t DbusPassive::getScale(void)
140 {
141     return _scale;
142 }
143 
144 std::string DbusPassive::getID(void)
145 {
146     return _id;
147 }
148 
149 double DbusPassive::getMax(void)
150 {
151     return _max;
152 }
153 
154 double DbusPassive::getMin(void)
155 {
156     return _min;
157 }
158 
159 int handleSensorValue(sdbusplus::message::message& msg, DbusPassive* owner)
160 {
161     std::string msgSensor;
162     std::map<std::string, std::variant<int64_t, double, bool>> msgData;
163 
164     msg.read(msgSensor, msgData);
165 
166     if (msgSensor == "xyz.openbmc_project.Sensor.Value")
167     {
168         auto valPropMap = msgData.find("Value");
169         if (valPropMap != msgData.end())
170         {
171             double value =
172                 std::visit(VariantToDoubleVisitor(), valPropMap->second);
173 
174             value *= std::pow(10, owner->getScale());
175 
176             scaleSensorReading(owner->getMin(), owner->getMax(), value);
177 
178             owner->setValue(value);
179         }
180     }
181     else if (msgSensor == "xyz.openbmc_project.Sensor.Threshold.Critical")
182     {
183         auto criticalAlarmLow = msgData.find("CriticalAlarmLow");
184         auto criticalAlarmHigh = msgData.find("CriticalAlarmHigh");
185         if (criticalAlarmHigh == msgData.end() &&
186             criticalAlarmLow == msgData.end())
187         {
188             return 0;
189         }
190 
191         bool asserted = false;
192         if (criticalAlarmLow != msgData.end())
193         {
194             asserted = std::get<bool>(criticalAlarmLow->second);
195         }
196 
197         // checking both as in theory you could de-assert one threshold and
198         // assert the other at the same moment
199         if (!asserted && criticalAlarmHigh != msgData.end())
200         {
201             asserted = std::get<bool>(criticalAlarmHigh->second);
202         }
203         owner->setFailed(asserted);
204     }
205     else if (msgSensor ==
206              "xyz.openbmc_project.State.Decorator.OperationalStatus")
207     {
208         auto functional = msgData.find("Functional");
209         if (functional == msgData.end())
210         {
211             return 0;
212         }
213         bool asserted = std::get<bool>(functional->second);
214         owner->setFunctional(asserted);
215     }
216 
217     return 0;
218 }
219 
220 int dbusHandleSignal(sd_bus_message* msg, void* usrData, sd_bus_error* err)
221 {
222     auto sdbpMsg = sdbusplus::message::message(msg);
223     DbusPassive* obj = static_cast<DbusPassive*>(usrData);
224 
225     return handleSensorValue(sdbpMsg, obj);
226 }
227 
228 } // namespace pid_control
229