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"; |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 21 | static constexpr char availabilityIntf[] = |
| 22 | "xyz.openbmc_project.State.Decorator.Availability"; |
Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 23 | |
Patrick Williams | 8c05112 | 2023-05-10 07:50:59 -0500 | [diff] [blame] | 24 | explicit DbusHelper(sdbusplus::bus_t bus) : _bus(std::move(bus)) {} |
Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 25 | ~DbusHelper() = default; |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 26 | |
| 27 | DbusHelper(const DbusHelper&) = delete; |
| 28 | DbusHelper& operator=(const DbusHelper&) = delete; |
| 29 | |
Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 30 | DbusHelper(DbusHelper&&) = default; |
| 31 | DbusHelper& operator=(DbusHelper&&) = default; |
| 32 | |
Patrick Venture | 9b93692 | 2020-08-10 11:28:39 -0700 | [diff] [blame] | 33 | std::string getService(const std::string& intf, |
Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 34 | const std::string& path) override; |
| 35 | |
Patrick Venture | 9b93692 | 2020-08-10 11:28:39 -0700 | [diff] [blame] | 36 | void getProperties(const std::string& service, const std::string& path, |
Patrick Venture | 1df9e87 | 2020-10-08 15:35:01 -0700 | [diff] [blame] | 37 | SensorProperties* prop) override; |
Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 38 | |
Patrick Venture | 9b93692 | 2020-08-10 11:28:39 -0700 | [diff] [blame] | 39 | bool thresholdsAsserted(const std::string& service, |
Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 40 | const std::string& path) override; |
| 41 | |
| 42 | template <typename T> |
Patrick Venture | 9b93692 | 2020-08-10 11:28:39 -0700 | [diff] [blame] | 43 | void getProperty(const std::string& service, const std::string& path, |
| 44 | const std::string& interface, |
Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 45 | const std::string& propertyName, T& prop) |
| 46 | { |
| 47 | namespace log = phosphor::logging; |
| 48 | |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 49 | auto msg = _bus.new_method_call(service.c_str(), path.c_str(), |
| 50 | propertiesintf, "Get"); |
Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 51 | |
| 52 | msg.append(interface, propertyName); |
| 53 | |
| 54 | std::variant<T> result; |
| 55 | try |
| 56 | { |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 57 | auto valueResponseMsg = _bus.call(msg); |
Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 58 | valueResponseMsg.read(result); |
| 59 | } |
Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 60 | catch (const sdbusplus::exception_t& ex) |
Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 61 | { |
| 62 | log::log<log::level::ERR>("Get Property Failed", |
| 63 | log::entry("WHAT=%s", ex.what())); |
| 64 | throw; |
| 65 | } |
| 66 | |
| 67 | prop = std::get<T>(result); |
| 68 | } |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 69 | |
| 70 | private: |
Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 71 | sdbusplus::bus_t _bus; |
Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | } // namespace pid_control |