blob: c4973cddee6757688247be34d5cd29a591986302 [file] [log] [blame]
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +02001#include "sensor.hpp"
2
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +02003#include <boost/container/flat_map.hpp>
4#include <phosphor-logging/log.hpp>
5#include <sdbusplus/asio/property.hpp>
6#include <sdbusplus/bus/match.hpp>
7
8#include <functional>
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +02009
10Sensor::Sensor(interfaces::Sensor::Id sensorId, boost::asio::io_context& ioc,
11 const std::shared_ptr<sdbusplus::asio::connection>& bus) :
12 sensorId(std::move(sensorId)),
13 ioc(ioc), bus(bus)
14{}
15
16Sensor::Id Sensor::makeId(std::string_view service, std::string_view path)
17{
18 return Id("Sensor", service, path);
19}
20
21Sensor::Id Sensor::id() const
22{
23 return sensorId;
24}
25
26void Sensor::async_read()
27{
28 uniqueCall([this](auto lock) { async_read(std::move(lock)); });
29}
30
31void Sensor::async_read(std::shared_ptr<utils::UniqueCall::Lock> lock)
32{
33 makeSignalMonitor();
34
35 sdbusplus::asio::getProperty<double>(
36 *bus, sensorId.service, sensorId.path,
37 "xyz.openbmc_project.Sensor.Value", "Value",
38 [lock, id = sensorId,
39 weakSelf = weak_from_this()](boost::system::error_code ec) {
Wludzik, Jozef5ade2b12020-11-16 14:00:23 +010040 phosphor::logging::log<phosphor::logging::level::WARNING>(
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020041 "DBus 'GetProperty' call failed on Sensor Value",
42 phosphor::logging::entry("sensor=%s, ec=%lu", id.str().c_str(),
43 ec.value()));
Krzysztof Grobelnyb5645942020-09-29 11:52:45 +020044 },
45 [lock, weakSelf = weak_from_this()](double newValue) {
46 if (auto self = weakSelf.lock())
47 {
48 self->updateValue(newValue);
49 }
50 });
51}
52
53void Sensor::registerForUpdates(
54 const std::weak_ptr<interfaces::SensorListener>& weakListener)
55{
56 if (auto listener = weakListener.lock())
57 {
58 listeners.emplace_back(weakListener);
59
60 if (value)
61 {
62 listener->sensorUpdated(*this, timestamp, *value);
63 }
64 else
65 {
66 async_read();
67 }
68 }
69}
70
71void Sensor::updateValue(double newValue)
72{
73 timestamp = std::time(0);
74
75 if (value == newValue)
76 {
77 for (const auto& weakListener : listeners)
78 {
79 if (auto listener = weakListener.lock())
80 {
81 listener->sensorUpdated(*this, timestamp);
82 }
83 }
84 }
85 else
86 {
87 value = newValue;
88
89 for (const auto& weakListener : listeners)
90 {
91 if (auto listener = weakListener.lock())
92 {
93 listener->sensorUpdated(*this, timestamp, *value);
94 }
95 }
96 }
97}
98
99void Sensor::makeSignalMonitor()
100{
101 if (signalMonitor)
102 {
103 return;
104 }
105
106 using namespace std::string_literals;
107
108 const auto param = "type='signal',member='PropertiesChanged',path='"s +
109 sensorId.path +
110 "',arg0='xyz.openbmc_project.Sensor.Value'"s;
111
112 signalMonitor = std::make_unique<sdbusplus::bus::match::match>(
113 *bus, param,
114 [weakSelf = weak_from_this()](sdbusplus::message::message& message) {
115 signalProc(weakSelf, message);
116 });
117}
118
119void Sensor::signalProc(const std::weak_ptr<Sensor>& weakSelf,
120 sdbusplus::message::message& message)
121{
122 if (auto self = weakSelf.lock())
123 {
124 std::string iface;
125 boost::container::flat_map<std::string, ValueVariant>
126 changed_properties;
127 std::vector<std::string> invalidated_properties;
128
129 message.read(iface, changed_properties, invalidated_properties);
130
131 if (iface == "xyz.openbmc_project.Sensor.Value")
132 {
133 const auto it = changed_properties.find("Value");
134 if (it != changed_properties.end())
135 {
136 if (auto val = std::get_if<double>(&it->second))
137 {
138 self->updateValue(*val);
139 }
140 else
141 {
142 phosphor::logging::log<phosphor::logging::level::ERR>(
143 "Failed to receive Value from Sensor "
144 "PropertiesChanged signal",
145 phosphor::logging::entry("sensor=%s",
146 self->sensorId.path.c_str()));
147 }
148 }
149 }
150 }
151}