xref: /openbmc/phosphor-pid-control/dbus/dbuspassive.cpp (revision 7af157b10ef4c1a0d09be0e310825909ca630cd0)
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 "dbus/util.hpp"
19 
20 #include <chrono>
21 #include <cmath>
22 #include <memory>
23 #include <mutex>
24 #include <sdbusplus/bus.hpp>
25 #include <string>
26 
27 std::unique_ptr<ReadInterface> DbusPassive::createDbusPassive(
28     sdbusplus::bus::bus& bus, const std::string& type, const std::string& id,
29     DbusHelperInterface* helper)
30 {
31     if (helper == nullptr)
32     {
33         return nullptr;
34     }
35     if (!validType(type))
36     {
37         return nullptr;
38     }
39 
40     return std::make_unique<DbusPassive>(bus, type, id, helper);
41 }
42 
43 DbusPassive::DbusPassive(sdbusplus::bus::bus& bus, const std::string& type,
44                          const std::string& id, DbusHelperInterface* helper) :
45     ReadInterface(),
46     _bus(bus), _signal(bus, getMatch(type, id).c_str(), dbusHandleSignal, this),
47     _id(id), _helper(helper)
48 {
49     /* Need to get the scale and initial value */
50     auto tempBus = sdbusplus::bus::new_default();
51     /* service == busname */
52     std::string path = getSensorPath(type, id);
53 
54     /* getService can except, should this be in the factory? */
55     std::string service = _helper->getService(tempBus, sensorintf, path);
56 
57     struct SensorProperties settings;
58     _helper->getProperties(tempBus, service, path, &settings);
59 
60     _scale = settings.scale;
61     _value = settings.value * pow(10, _scale);
62     _updated = std::chrono::high_resolution_clock::now();
63     _failed = _helper->thresholdsAsserted(tempBus, service, path);
64 }
65 
66 ReadReturn DbusPassive::read(void)
67 {
68     std::lock_guard<std::mutex> guard(_lock);
69 
70     struct ReadReturn r = {_value, _updated};
71 
72     return r;
73 }
74 
75 void DbusPassive::setValue(double value)
76 {
77     std::lock_guard<std::mutex> guard(_lock);
78 
79     _value = value;
80     _updated = std::chrono::high_resolution_clock::now();
81 }
82 
83 bool DbusPassive::getFailed(void) const
84 {
85     return _failed;
86 }
87 
88 void DbusPassive::setFailed(bool value)
89 {
90     _failed = value;
91 }
92 
93 int64_t DbusPassive::getScale(void)
94 {
95     return _scale;
96 }
97 
98 std::string DbusPassive::getID(void)
99 {
100     return _id;
101 }
102 
103 int handleSensorValue(sdbusplus::message::message& msg, DbusPassive* owner)
104 {
105     std::string msgSensor;
106     std::map<std::string, sdbusplus::message::variant<int64_t, double, bool>>
107         msgData;
108 
109     msg.read(msgSensor, msgData);
110 
111     if (msgSensor == "xyz.openbmc_project.Sensor.Value")
112     {
113         auto valPropMap = msgData.find("Value");
114         if (valPropMap != msgData.end())
115         {
116             double value = sdbusplus::message::variant_ns::apply_visitor(
117                 VariantToDoubleVisitor(), valPropMap->second);
118 
119             value *= std::pow(10, owner->getScale());
120 
121             owner->setValue(value);
122         }
123     }
124     else if (msgSensor == "xyz.openbmc_project.Sensor.Threshold.Critical")
125     {
126         auto criticalAlarmLow = msgData.find("CriticalAlarmLow");
127         auto criticalAlarmHigh = msgData.find("CriticalAlarmHigh");
128         if (criticalAlarmHigh == msgData.end() &&
129             criticalAlarmLow == msgData.end())
130         {
131             return 0;
132         }
133 
134         bool asserted = false;
135         if (criticalAlarmLow != msgData.end())
136         {
137             asserted = sdbusplus::message::variant_ns::get<bool>(
138                 criticalAlarmLow->second);
139         }
140 
141         // checking both as in theory you could de-assert one threshold and
142         // assert the other at the same moment
143         if (!asserted && criticalAlarmHigh != msgData.end())
144         {
145             asserted = sdbusplus::message::variant_ns::get<bool>(
146                 criticalAlarmHigh->second);
147         }
148         owner->setFailed(asserted);
149     }
150 
151     return 0;
152 }
153 
154 int dbusHandleSignal(sd_bus_message* msg, void* usrData, sd_bus_error* err)
155 {
156     auto sdbpMsg = sdbusplus::message::message(msg);
157     DbusPassive* obj = static_cast<DbusPassive*>(usrData);
158 
159     return handleSensorValue(sdbpMsg, obj);
160 }
161