blob: 0d9733e670f9fc199db0e55e8515030b824a88c8 [file] [log] [blame]
Tao Linf2e94222023-10-31 17:38:17 +08001#include "dbusSensor.hpp"
2
3#include "virtualSensor.hpp"
4
5#include <sdbusplus/bus.hpp>
6#include <sdbusplus/bus/match.hpp>
7
8#include <cmath>
9static constexpr auto sensorIntf =
10 sdbusplus::common::xyz::openbmc_project::sensor::Value::interface;
11
12/** When the Entity Manager removes the sensor, the interfaceRemoveSignal sent
13 * uses the path /xyz/openbmc_project/sensors
14 * */
15static constexpr auto interfacesSensorPath = "/xyz/openbmc_project/sensors";
16
17namespace phosphor::virtual_sensor
18{
19
20DbusSensor::DbusSensor(sdbusplus::bus_t& bus, const std::string& path,
21 VirtualSensor& virtualSensor) :
Patrick Williams150d5f62024-08-16 15:21:45 -040022 bus(bus), path(path), virtualSensor(virtualSensor),
Tao Linf2e94222023-10-31 17:38:17 +080023 signalPropChange(
24 bus, sdbusplus::bus::match::rules::propertiesChanged(path, sensorIntf),
25 [this](sdbusplus::message_t& message) {
Patrick Williams150d5f62024-08-16 15:21:45 -040026 handleDbusSignalPropChange(message);
27 }),
Tao Linf2e94222023-10-31 17:38:17 +080028 signalRemove(
29 bus,
30 sdbusplus::bus::match::rules::interfacesRemoved(interfacesSensorPath),
31 [this](sdbusplus::message_t& message) {
Patrick Williams150d5f62024-08-16 15:21:45 -040032 handleDbusSignalRemove(message);
33 })
Tao Linf2e94222023-10-31 17:38:17 +080034{
35 initSensorValue();
36}
37
38double DbusSensor::getSensorValue()
39{
40 return value;
41}
42
43void DbusSensor::initSensorValue()
44{
45 try
46 {
47 // If servName is not empty, reduce one DbusCall
48 if (servName.empty())
49 {
50 value = std::numeric_limits<double>::quiet_NaN();
51 servName = getService(bus, path, sensorIntf);
52 }
53
54 if (!servName.empty())
55 {
56 signalNameOwnerChanged.reset();
57 signalNameOwnerChanged = std::make_unique<sdbusplus::bus::match_t>(
58 bus,
59 sdbusplus::bus::match::rules::nameOwnerChanged() +
60 sdbusplus::bus::match::rules::arg0namespace(servName),
61 [this](sdbusplus::message_t& message) {
Patrick Williams150d5f62024-08-16 15:21:45 -040062 handleDbusSignalNameOwnerChanged(message);
63 });
Tao Linf2e94222023-10-31 17:38:17 +080064
65 value = getDbusProperty<double>(bus, servName, path, sensorIntf,
66 "Value");
67 }
68 }
69 catch (const std::exception& e)
70 {
71 value = std::numeric_limits<double>::quiet_NaN();
72 }
73
74 return;
75}
76
77void DbusSensor::handleDbusSignalNameOwnerChanged(sdbusplus::message_t& msg)
78{
79 try
80 {
81 auto [name, oldOwner,
82 newOwner] = msg.unpack<std::string, std::string, std::string>();
83
84 if (!oldOwner.empty() && !name.empty())
85 {
86 if (name == servName)
87 {
88 // Connection removed
89
90 value = std::numeric_limits<double>::quiet_NaN();
91 virtualSensor.updateVirtualSensor();
92 }
93 }
94 }
95 catch (const std::exception& e)
96 {
97 lg2::error("Error in dbusSensor NameOwnerChanged: {PATH} {ERROR}",
98 "PATH", path, "ERROR", e);
99 }
100}
101
102void DbusSensor::handleDbusSignalPropChange(sdbusplus::message_t& msg)
103{
104 try
105 {
106 using SensorValuePropertiesVariant = sdbusplus::server::xyz::
107 openbmc_project::sensor::Value::PropertiesVariant;
108 auto [msgIfce, msgData] =
109 msg.unpack<std::string,
110 std::map<std::string, SensorValuePropertiesVariant>>();
111
Tao Linf2e94222023-10-31 17:38:17 +0800112 if (auto itr = msgData.find("Value"); itr != msgData.end())
113 {
114 value = std::get<double>(itr->second);
115 if (!std::isfinite(value))
116 {
117 value = std::numeric_limits<double>::quiet_NaN();
118 }
119
120 virtualSensor.updateVirtualSensor();
121 }
122 }
123 catch (const std::exception& e)
124 {
125 lg2::error("Error in dbusSensor PropertyChange: {PATH} {ERROR}",
126 "PATH", path, "ERROR", e);
127 }
128}
129
130void DbusSensor::handleDbusSignalRemove(sdbusplus::message_t& msg)
131{
132 try
133 {
134 auto objPath = msg.unpack<sdbusplus::message::object_path>();
135
136 if (this->path == objPath)
137 {
138 value = std::numeric_limits<double>::quiet_NaN();
139 virtualSensor.updateVirtualSensor();
140 }
141 }
142 catch (const std::exception& e)
143 {
144 lg2::error("Error in dbusSensor interfaceRemove: {PATH} {ERROR}",
145 "PATH", path, "ERROR", e);
146 }
147}
148
149} // namespace phosphor::virtual_sensor