Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 1 | #include "dbusUtils.hpp" |
| 2 | |
| 3 | #include <sdbusplus/bus.hpp> |
Patrick Williams | b57d737 | 2021-11-19 12:26:08 -0600 | [diff] [blame] | 4 | #include <sdbusplus/bus/match.hpp> |
Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 5 | |
| 6 | const char* sensorIntf = "xyz.openbmc_project.Sensor.Value"; |
| 7 | |
Vijay Khemka | 51f898e | 2020-09-09 22:24:18 -0700 | [diff] [blame] | 8 | int handleDbusSignal(sd_bus_message* msg, void* usrData, sd_bus_error* err); |
| 9 | |
Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 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 | */ |
Vijay Khemka | 51f898e | 2020-09-09 22:24:18 -0700 | [diff] [blame] | 21 | DbusSensor(sdbusplus::bus::bus& 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) |
Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 27 | { |
| 28 | servName = getService(bus, path, sensorIntf); |
| 29 | } |
| 30 | |
| 31 | /** @brief Get sensor value property from D-bus interface */ |
| 32 | double getSensorValue() |
| 33 | { |
Matt Spinler | f8db722 | 2020-11-10 08:15:03 -0600 | [diff] [blame] | 34 | if (servName.empty()) |
| 35 | { |
| 36 | servName = getService(bus, path, sensorIntf); |
| 37 | if (servName.empty()) |
| 38 | { |
| 39 | return std::numeric_limits<double>::quiet_NaN(); |
| 40 | } |
| 41 | } |
| 42 | |
Thu Ba Nguyen | a55a907 | 2021-04-07 09:44:33 +0700 | [diff] [blame] | 43 | try |
| 44 | { |
| 45 | return getDbusProperty<double>(bus, servName, path, sensorIntf, |
| 46 | "Value"); |
| 47 | } |
| 48 | catch (const std::exception& e) |
| 49 | { |
| 50 | return std::numeric_limits<double>::quiet_NaN(); |
| 51 | } |
Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | private: |
| 55 | /** @brief sdbusplus bus client connection. */ |
| 56 | sdbusplus::bus::bus& bus; |
| 57 | /** @brief complete path for sensor */ |
| 58 | std::string path; |
| 59 | /** @brief service name for the sensor daemon */ |
| 60 | std::string servName; |
Vijay Khemka | 51f898e | 2020-09-09 22:24:18 -0700 | [diff] [blame] | 61 | /** @brief signal for sensor value change */ |
Patrick Williams | b57d737 | 2021-11-19 12:26:08 -0600 | [diff] [blame] | 62 | sdbusplus::bus::match_t signal; |
Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 63 | }; |