1 #include "dbusUtils.hpp"
2 
3 #include <sdbusplus/bus.hpp>
4 #include <sdbusplus/bus/match.hpp>
5 
6 const char* sensorIntf = "xyz.openbmc_project.Sensor.Value";
7 
8 int handleDbusSignal(sd_bus_message* msg, void* usrData, sd_bus_error* err);
9 
10 class DbusSensor
11 {
12   public:
13     DbusSensor() = delete;
14     virtual ~DbusSensor() = default;
15 
16     /** @brief Constructs DbusSensor
17      *
18      * @param[in] bus     - Handle to system dbus
19      * @param[in] path    - The Dbus path of sensor
20      */
21     DbusSensor(sdbusplus::bus_t& bus, const std::string& path, void* ctx) :
22         bus(bus), path(path),
23         signal(
24             bus,
25             sdbusplus::bus::match::rules::propertiesChanged(path, sensorIntf),
26             handleDbusSignal, ctx)
27     {}
28 
29     /** @brief Get sensor value property from D-bus interface */
30     double getSensorValue()
31     {
32         if (servName.empty())
33         {
34             servName = getService(bus, path, sensorIntf);
35             if (servName.empty())
36             {
37                 return std::numeric_limits<double>::quiet_NaN();
38             }
39         }
40 
41         try
42         {
43             return getDbusProperty<double>(bus, servName, path, sensorIntf,
44                                            "Value");
45         }
46         catch (const std::exception& e)
47         {
48             return std::numeric_limits<double>::quiet_NaN();
49         }
50     }
51 
52   private:
53     /** @brief sdbusplus bus client connection. */
54     sdbusplus::bus_t& bus;
55     /** @brief complete path for sensor */
56     std::string path{};
57     /** @brief service name for the sensor daemon */
58     std::string servName{};
59     /** @brief signal for sensor value change */
60     sdbusplus::bus::match_t signal;
61 };
62