17452a867SVijay Khemka #include "dbusUtils.hpp"
27452a867SVijay Khemka 
37452a867SVijay Khemka #include <sdbusplus/bus.hpp>
4b57d7370SPatrick Williams #include <sdbusplus/bus/match.hpp>
57452a867SVijay Khemka 
67452a867SVijay Khemka const char* sensorIntf = "xyz.openbmc_project.Sensor.Value";
77452a867SVijay Khemka 
851f898e2SVijay Khemka int handleDbusSignal(sd_bus_message* msg, void* usrData, sd_bus_error* err);
951f898e2SVijay Khemka 
107452a867SVijay Khemka class DbusSensor
117452a867SVijay Khemka {
127452a867SVijay Khemka   public:
137452a867SVijay Khemka     DbusSensor() = delete;
147452a867SVijay Khemka     virtual ~DbusSensor() = default;
157452a867SVijay Khemka 
167452a867SVijay Khemka     /** @brief Constructs DbusSensor
177452a867SVijay Khemka      *
187452a867SVijay Khemka      * @param[in] bus     - Handle to system dbus
197452a867SVijay Khemka      * @param[in] path    - The Dbus path of sensor
207452a867SVijay Khemka      */
21*8e11cccbSPatrick Williams     DbusSensor(sdbusplus::bus_t& bus, const std::string& path, void* ctx) :
2251f898e2SVijay Khemka         bus(bus), path(path),
2351f898e2SVijay Khemka         signal(
2451f898e2SVijay Khemka             bus,
2551f898e2SVijay Khemka             sdbusplus::bus::match::rules::propertiesChanged(path, sensorIntf),
2651f898e2SVijay Khemka             handleDbusSignal, ctx)
271204b433SGeorge Liu     {}
287452a867SVijay Khemka 
297452a867SVijay Khemka     /** @brief Get sensor value property from D-bus interface */
307452a867SVijay Khemka     double getSensorValue()
317452a867SVijay Khemka     {
32f8db7225SMatt Spinler         if (servName.empty())
33f8db7225SMatt Spinler         {
34f8db7225SMatt Spinler             servName = getService(bus, path, sensorIntf);
35f8db7225SMatt Spinler             if (servName.empty())
36f8db7225SMatt Spinler             {
37f8db7225SMatt Spinler                 return std::numeric_limits<double>::quiet_NaN();
38f8db7225SMatt Spinler             }
39f8db7225SMatt Spinler         }
40f8db7225SMatt Spinler 
41a55a907eSThu Ba Nguyen         try
42a55a907eSThu Ba Nguyen         {
437452a867SVijay Khemka             return getDbusProperty<double>(bus, servName, path, sensorIntf,
447452a867SVijay Khemka                                            "Value");
457452a867SVijay Khemka         }
46a55a907eSThu Ba Nguyen         catch (const std::exception& e)
47a55a907eSThu Ba Nguyen         {
48a55a907eSThu Ba Nguyen             return std::numeric_limits<double>::quiet_NaN();
49a55a907eSThu Ba Nguyen         }
50a55a907eSThu Ba Nguyen     }
517452a867SVijay Khemka 
527452a867SVijay Khemka   private:
537452a867SVijay Khemka     /** @brief sdbusplus bus client connection. */
54*8e11cccbSPatrick Williams     sdbusplus::bus_t& bus;
557452a867SVijay Khemka     /** @brief complete path for sensor */
561204b433SGeorge Liu     std::string path{};
577452a867SVijay Khemka     /** @brief service name for the sensor daemon */
581204b433SGeorge Liu     std::string servName{};
5951f898e2SVijay Khemka     /** @brief signal for sensor value change */
60b57d7370SPatrick Williams     sdbusplus::bus::match_t signal;
617452a867SVijay Khemka };
62