blob: 5c119a5cd4b80bea39ee7da51e0bbe26590d0633 [file] [log] [blame]
Vijay Khemka7452a862020-08-11 16:01:23 -07001#include "dbusUtils.hpp"
2
3#include <sdbusplus/bus.hpp>
Patrick Williamsb57d7372021-11-19 12:26:08 -06004#include <sdbusplus/bus/match.hpp>
Vijay Khemka7452a862020-08-11 16:01:23 -07005
6const char* sensorIntf = "xyz.openbmc_project.Sensor.Value";
7
Vijay Khemka51f898e2020-09-09 22:24:18 -07008int handleDbusSignal(sd_bus_message* msg, void* usrData, sd_bus_error* err);
9
Vijay Khemka7452a862020-08-11 16:01:23 -070010class 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 */
Patrick Williams8e11ccc2022-07-22 19:26:57 -050021 DbusSensor(sdbusplus::bus_t& bus, const std::string& path, void* ctx) :
Vijay Khemka51f898e2020-09-09 22:24:18 -070022 bus(bus), path(path),
23 signal(
24 bus,
25 sdbusplus::bus::match::rules::propertiesChanged(path, sensorIntf),
26 handleDbusSignal, ctx)
George Liu1204b432021-12-29 17:24:48 +080027 {}
Vijay Khemka7452a862020-08-11 16:01:23 -070028
29 /** @brief Get sensor value property from D-bus interface */
30 double getSensorValue()
31 {
Matt Spinlerf8db7222020-11-10 08:15:03 -060032 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
Thu Ba Nguyena55a9072021-04-07 09:44:33 +070041 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 }
Vijay Khemka7452a862020-08-11 16:01:23 -070050 }
51
52 private:
53 /** @brief sdbusplus bus client connection. */
Patrick Williams8e11ccc2022-07-22 19:26:57 -050054 sdbusplus::bus_t& bus;
Vijay Khemka7452a862020-08-11 16:01:23 -070055 /** @brief complete path for sensor */
George Liu1204b432021-12-29 17:24:48 +080056 std::string path{};
Vijay Khemka7452a862020-08-11 16:01:23 -070057 /** @brief service name for the sensor daemon */
George Liu1204b432021-12-29 17:24:48 +080058 std::string servName{};
Vijay Khemka51f898e2020-09-09 22:24:18 -070059 /** @brief signal for sensor value change */
Patrick Williamsb57d7372021-11-19 12:26:08 -060060 sdbusplus::bus::match_t signal;
Vijay Khemka7452a862020-08-11 16:01:23 -070061};