Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "dbushelper_interface.hpp" |
| 4 | |
| 5 | #include <phosphor-logging/log.hpp> |
| 6 | #include <sdbusplus/bus.hpp> |
| 7 | |
| 8 | #include <string> |
| 9 | #include <variant> |
| 10 | |
| 11 | namespace pid_control |
| 12 | { |
| 13 | |
| 14 | class DbusHelper : public DbusHelperInterface |
| 15 | { |
| 16 | public: |
| 17 | static constexpr char sensorintf[] = "xyz.openbmc_project.Sensor.Value"; |
| 18 | static constexpr char propertiesintf[] = "org.freedesktop.DBus.Properties"; |
| 19 | static constexpr char criticalThreshInf[] = |
| 20 | "xyz.openbmc_project.Sensor.Threshold.Critical"; |
| 21 | |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 22 | explicit DbusHelper(sdbusplus::bus::bus bus) : _bus(std::move(bus)) |
| 23 | {} |
Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 24 | ~DbusHelper() = default; |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 25 | |
| 26 | DbusHelper(const DbusHelper&) = delete; |
| 27 | DbusHelper& operator=(const DbusHelper&) = delete; |
| 28 | |
Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 29 | DbusHelper(DbusHelper&&) = default; |
| 30 | DbusHelper& operator=(DbusHelper&&) = default; |
| 31 | |
Patrick Venture | 9b93692 | 2020-08-10 11:28:39 -0700 | [diff] [blame] | 32 | std::string getService(const std::string& intf, |
Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 33 | const std::string& path) override; |
| 34 | |
Patrick Venture | 9b93692 | 2020-08-10 11:28:39 -0700 | [diff] [blame] | 35 | void getProperties(const std::string& service, const std::string& path, |
Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 36 | struct SensorProperties* prop) override; |
| 37 | |
Patrick Venture | 9b93692 | 2020-08-10 11:28:39 -0700 | [diff] [blame] | 38 | bool thresholdsAsserted(const std::string& service, |
Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 39 | const std::string& path) override; |
| 40 | |
| 41 | template <typename T> |
Patrick Venture | 9b93692 | 2020-08-10 11:28:39 -0700 | [diff] [blame] | 42 | void getProperty(const std::string& service, const std::string& path, |
| 43 | const std::string& interface, |
Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 44 | const std::string& propertyName, T& prop) |
| 45 | { |
| 46 | namespace log = phosphor::logging; |
| 47 | |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 48 | auto msg = _bus.new_method_call(service.c_str(), path.c_str(), |
| 49 | propertiesintf, "Get"); |
Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 50 | |
| 51 | msg.append(interface, propertyName); |
| 52 | |
| 53 | std::variant<T> result; |
| 54 | try |
| 55 | { |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 56 | auto valueResponseMsg = _bus.call(msg); |
Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 57 | valueResponseMsg.read(result); |
| 58 | } |
| 59 | catch (const sdbusplus::exception::SdBusError& ex) |
| 60 | { |
| 61 | log::log<log::level::ERR>("Get Property Failed", |
| 62 | log::entry("WHAT=%s", ex.what())); |
| 63 | throw; |
| 64 | } |
| 65 | |
| 66 | prop = std::get<T>(result); |
| 67 | } |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 68 | |
| 69 | private: |
| 70 | sdbusplus::bus::bus _bus; |
Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | } // namespace pid_control |