blob: 3a6d6d4224f952bf47a86dc0bee3dbeea48967e1 [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>
Patrick Venture863b9242018-03-08 08:29:23 -080026
Patrick Venture563a3562018-10-30 09:31:26 -070027std::unique_ptr<ReadInterface> DbusPassive::createDbusPassive(
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070028 sdbusplus::bus::bus& bus, const std::string& type, const std::string& id,
29 DbusHelperInterface* helper)
Patrick Venture0ef1faf2018-06-13 12:50:53 -070030{
31 if (helper == nullptr)
32 {
33 return nullptr;
34 }
Patrick Venture7af157b2018-10-30 11:24:40 -070035 if (!validType(type))
Patrick Venture0ef1faf2018-06-13 12:50:53 -070036 {
37 return nullptr;
38 }
39
Patrick Venture863b9242018-03-08 08:29:23 -080040 /* Need to get the scale and initial value */
41 auto tempBus = sdbusplus::bus::new_default();
Patrick Venturef8cb4642018-10-30 12:02:53 -070042
Patrick Venture863b9242018-03-08 08:29:23 -080043 /* service == busname */
Patrick Venture7af157b2018-10-30 11:24:40 -070044 std::string path = getSensorPath(type, id);
Patrick Venture34ddc902018-10-30 11:05:17 -070045
Patrick Venture863b9242018-03-08 08:29:23 -080046 struct SensorProperties settings;
Patrick Venturef8cb4642018-10-30 12:02:53 -070047 bool failed;
Patrick Venture863b9242018-03-08 08:29:23 -080048
Patrick Venturef8cb4642018-10-30 12:02:53 -070049 try
50 {
51 std::string service = helper->getService(tempBus, sensorintf, path);
52
53 helper->getProperties(tempBus, service, path, &settings);
54 failed = helper->thresholdsAsserted(tempBus, service, path);
55 }
56 catch (const std::exception& e)
57 {
58 return nullptr;
59 }
60
61 return std::make_unique<DbusPassive>(bus, type, id, helper, settings,
62 failed);
63}
64
65DbusPassive::DbusPassive(sdbusplus::bus::bus& bus, const std::string& type,
66 const std::string& id, DbusHelperInterface* helper,
67 const struct SensorProperties& settings, bool failed) :
68 ReadInterface(),
69 _bus(bus), _signal(bus, getMatch(type, id).c_str(), dbusHandleSignal, this),
70 _id(id), _helper(helper), _failed(failed)
71{
Patrick Venture863b9242018-03-08 08:29:23 -080072 _scale = settings.scale;
73 _value = settings.value * pow(10, _scale);
74 _updated = std::chrono::high_resolution_clock::now();
75}
76
77ReadReturn DbusPassive::read(void)
78{
79 std::lock_guard<std::mutex> guard(_lock);
80
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070081 struct ReadReturn r = {_value, _updated};
Patrick Venture863b9242018-03-08 08:29:23 -080082
83 return r;
84}
85
86void DbusPassive::setValue(double value)
87{
88 std::lock_guard<std::mutex> guard(_lock);
89
90 _value = value;
91 _updated = std::chrono::high_resolution_clock::now();
92}
93
James Feist36b7d8e2018-10-05 15:39:01 -070094bool DbusPassive::getFailed(void) const
95{
96 return _failed;
97}
98
99void DbusPassive::setFailed(bool value)
100{
101 _failed = value;
102}
103
Patrick Venture863b9242018-03-08 08:29:23 -0800104int64_t DbusPassive::getScale(void)
105{
106 return _scale;
107}
108
Patrick Venture563a3562018-10-30 09:31:26 -0700109std::string DbusPassive::getID(void)
Patrick Venture863b9242018-03-08 08:29:23 -0800110{
111 return _id;
112}
113
Patrick Venture7af157b2018-10-30 11:24:40 -0700114int handleSensorValue(sdbusplus::message::message& msg, DbusPassive* owner)
Patrick Venture863b9242018-03-08 08:29:23 -0800115{
Patrick Venture863b9242018-03-08 08:29:23 -0800116 std::string msgSensor;
James Feist36b7d8e2018-10-05 15:39:01 -0700117 std::map<std::string, sdbusplus::message::variant<int64_t, double, bool>>
118 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 Feistd7a55bf2018-10-11 14:40:07 -0700127 double value = sdbusplus::message::variant_ns::apply_visitor(
128 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 {
148 asserted = sdbusplus::message::variant_ns::get<bool>(
149 criticalAlarmLow->second);
150 }
151
152 // checking both as in theory you could de-assert one threshold and
153 // assert the other at the same moment
154 if (!asserted && criticalAlarmHigh != msgData.end())
155 {
156 asserted = sdbusplus::message::variant_ns::get<bool>(
157 criticalAlarmHigh->second);
158 }
159 owner->setFailed(asserted);
160 }
Patrick Venture863b9242018-03-08 08:29:23 -0800161
162 return 0;
163}
Patrick Ventured0c75662018-06-12 19:03:21 -0700164
Patrick Venture7af157b2018-10-30 11:24:40 -0700165int dbusHandleSignal(sd_bus_message* msg, void* usrData, sd_bus_error* err)
Patrick Ventured0c75662018-06-12 19:03:21 -0700166{
167 auto sdbpMsg = sdbusplus::message::message(msg);
168 DbusPassive* obj = static_cast<DbusPassive*>(usrData);
169
Patrick Venture7af157b2018-10-30 11:24:40 -0700170 return handleSensorValue(sdbpMsg, obj);
Patrick Ventured0c75662018-06-12 19:03:21 -0700171}