blob: 6d8aa03c9bdeb9ebd18a227cba756912633a1efe [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
18#include "dbus/util.hpp"
19
Patrick Venture863b9242018-03-08 08:29:23 -080020#include <chrono>
21#include <cmath>
Patrick Venture0ef1faf2018-06-13 12:50:53 -070022#include <memory>
Patrick Venture863b9242018-03-08 08:29:23 -080023#include <mutex>
Patrick Venture0ef1faf2018-06-13 12:50:53 -070024#include <sdbusplus/bus.hpp>
25#include <string>
James Feist1f802f52019-02-08 13:51:43 -080026#include <variant>
Patrick Venture863b9242018-03-08 08:29:23 -080027
Patrick Venture563a3562018-10-30 09:31:26 -070028std::unique_ptr<ReadInterface> DbusPassive::createDbusPassive(
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070029 sdbusplus::bus::bus& bus, const std::string& type, const std::string& id,
30 DbusHelperInterface* helper)
Patrick Venture0ef1faf2018-06-13 12:50:53 -070031{
32 if (helper == nullptr)
33 {
34 return nullptr;
35 }
Patrick Venture7af157b2018-10-30 11:24:40 -070036 if (!validType(type))
Patrick Venture0ef1faf2018-06-13 12:50:53 -070037 {
38 return nullptr;
39 }
40
Patrick Venture863b9242018-03-08 08:29:23 -080041 /* Need to get the scale and initial value */
James Feist9fa90c12019-01-11 15:35:22 -080042 auto tempBus = sdbusplus::bus::new_system();
Patrick Venturef8cb4642018-10-30 12:02:53 -070043
Patrick Venture863b9242018-03-08 08:29:23 -080044 /* service == busname */
Patrick Venture7af157b2018-10-30 11:24:40 -070045 std::string path = getSensorPath(type, id);
Patrick Venture34ddc902018-10-30 11:05:17 -070046
Patrick Venture863b9242018-03-08 08:29:23 -080047 struct SensorProperties settings;
Patrick Venturef8cb4642018-10-30 12:02:53 -070048 bool failed;
Patrick Venture863b9242018-03-08 08:29:23 -080049
Patrick Venturef8cb4642018-10-30 12:02:53 -070050 try
51 {
52 std::string service = helper->getService(tempBus, sensorintf, path);
53
54 helper->getProperties(tempBus, service, path, &settings);
55 failed = helper->thresholdsAsserted(tempBus, service, path);
56 }
57 catch (const std::exception& e)
58 {
59 return nullptr;
60 }
61
62 return std::make_unique<DbusPassive>(bus, type, id, helper, settings,
63 failed);
64}
65
66DbusPassive::DbusPassive(sdbusplus::bus::bus& bus, const std::string& type,
67 const std::string& id, DbusHelperInterface* helper,
68 const struct SensorProperties& settings, bool failed) :
69 ReadInterface(),
70 _bus(bus), _signal(bus, getMatch(type, id).c_str(), dbusHandleSignal, this),
71 _id(id), _helper(helper), _failed(failed)
72{
Patrick Venture863b9242018-03-08 08:29:23 -080073 _scale = settings.scale;
74 _value = settings.value * pow(10, _scale);
75 _updated = std::chrono::high_resolution_clock::now();
76}
77
78ReadReturn DbusPassive::read(void)
79{
80 std::lock_guard<std::mutex> guard(_lock);
81
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070082 struct ReadReturn r = {_value, _updated};
Patrick Venture863b9242018-03-08 08:29:23 -080083
84 return r;
85}
86
87void DbusPassive::setValue(double value)
88{
89 std::lock_guard<std::mutex> guard(_lock);
90
91 _value = value;
92 _updated = std::chrono::high_resolution_clock::now();
93}
94
James Feist36b7d8e2018-10-05 15:39:01 -070095bool DbusPassive::getFailed(void) const
96{
97 return _failed;
98}
99
100void DbusPassive::setFailed(bool value)
101{
102 _failed = value;
103}
104
Patrick Venture863b9242018-03-08 08:29:23 -0800105int64_t DbusPassive::getScale(void)
106{
107 return _scale;
108}
109
Patrick Venture563a3562018-10-30 09:31:26 -0700110std::string DbusPassive::getID(void)
Patrick Venture863b9242018-03-08 08:29:23 -0800111{
112 return _id;
113}
114
Patrick Venture7af157b2018-10-30 11:24:40 -0700115int handleSensorValue(sdbusplus::message::message& msg, DbusPassive* owner)
Patrick Venture863b9242018-03-08 08:29:23 -0800116{
Patrick Venture863b9242018-03-08 08:29:23 -0800117 std::string msgSensor;
James Feist1f802f52019-02-08 13:51:43 -0800118 std::map<std::string, std::variant<int64_t, double, bool>> msgData;
Patrick Ventured0c75662018-06-12 19:03:21 -0700119
120 msg.read(msgSensor, msgData);
Patrick Venture863b9242018-03-08 08:29:23 -0800121
122 if (msgSensor == "xyz.openbmc_project.Sensor.Value")
123 {
124 auto valPropMap = msgData.find("Value");
125 if (valPropMap != msgData.end())
126 {
James Feist1f802f52019-02-08 13:51:43 -0800127 double value =
128 std::visit(VariantToDoubleVisitor(), valPropMap->second);
Patrick Venture863b9242018-03-08 08:29:23 -0800129
James Feistc065cf12018-07-05 10:23:11 -0700130 value *= std::pow(10, owner->getScale());
Patrick Venture863b9242018-03-08 08:29:23 -0800131
Patrick Ventured0c75662018-06-12 19:03:21 -0700132 owner->setValue(value);
Patrick Venture863b9242018-03-08 08:29:23 -0800133 }
134 }
James Feist36b7d8e2018-10-05 15:39:01 -0700135 else if (msgSensor == "xyz.openbmc_project.Sensor.Threshold.Critical")
136 {
137 auto criticalAlarmLow = msgData.find("CriticalAlarmLow");
138 auto criticalAlarmHigh = msgData.find("CriticalAlarmHigh");
139 if (criticalAlarmHigh == msgData.end() &&
140 criticalAlarmLow == msgData.end())
141 {
142 return 0;
143 }
144
145 bool asserted = false;
146 if (criticalAlarmLow != msgData.end())
147 {
James Feist1f802f52019-02-08 13:51:43 -0800148 asserted = std::get<bool>(criticalAlarmLow->second);
James Feist36b7d8e2018-10-05 15:39:01 -0700149 }
150
151 // checking both as in theory you could de-assert one threshold and
152 // assert the other at the same moment
153 if (!asserted && criticalAlarmHigh != msgData.end())
154 {
James Feist1f802f52019-02-08 13:51:43 -0800155 asserted = std::get<bool>(criticalAlarmHigh->second);
James Feist36b7d8e2018-10-05 15:39:01 -0700156 }
157 owner->setFailed(asserted);
158 }
Patrick Venture863b9242018-03-08 08:29:23 -0800159
160 return 0;
161}
Patrick Ventured0c75662018-06-12 19:03:21 -0700162
Patrick Venture7af157b2018-10-30 11:24:40 -0700163int dbusHandleSignal(sd_bus_message* msg, void* usrData, sd_bus_error* err)
Patrick Ventured0c75662018-06-12 19:03:21 -0700164{
165 auto sdbpMsg = sdbusplus::message::message(msg);
166 DbusPassive* obj = static_cast<DbusPassive*>(usrData);
167
Patrick Venture7af157b2018-10-30 11:24:40 -0700168 return handleSensorValue(sdbpMsg, obj);
Patrick Ventured0c75662018-06-12 19:03:21 -0700169}