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> |
Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame^] | 7 | #include <sdbusplus/exception.hpp> |
Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 8 | |
| 9 | #include <string> |
| 10 | #include <variant> |
| 11 | |
| 12 | namespace pid_control |
| 13 | { |
| 14 | |
| 15 | class DbusHelper : public DbusHelperInterface |
| 16 | { |
| 17 | public: |
| 18 | static constexpr char sensorintf[] = "xyz.openbmc_project.Sensor.Value"; |
| 19 | static constexpr char propertiesintf[] = "org.freedesktop.DBus.Properties"; |
| 20 | static constexpr char criticalThreshInf[] = |
| 21 | "xyz.openbmc_project.Sensor.Threshold.Critical"; |
Jonico Eustaquio | af97d8e | 2024-01-02 14:35:07 -0600 | [diff] [blame] | 22 | static constexpr char warningThreshInf[] = |
| 23 | "xyz.openbmc_project.Sensor.Threshold.Warning"; |
Alex.Song | 8f73ad7 | 2021-10-07 00:18:27 +0800 | [diff] [blame] | 24 | static constexpr char availabilityIntf[] = |
| 25 | "xyz.openbmc_project.State.Decorator.Availability"; |
Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 26 | |
Patrick Williams | cd1e78a | 2025-04-07 17:21:05 -0400 | [diff] [blame] | 27 | explicit DbusHelper(sdbusplus::bus_t& bus) : _bus(bus) {} |
| 28 | DbusHelper() = delete; |
Ed Tanous | d2768c5 | 2025-06-26 11:42:57 -0700 | [diff] [blame] | 29 | ~DbusHelper() override = default; |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 30 | |
| 31 | DbusHelper(const DbusHelper&) = delete; |
| 32 | DbusHelper& operator=(const DbusHelper&) = delete; |
| 33 | |
Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 34 | DbusHelper(DbusHelper&&) = default; |
Eric Yang | 841531c | 2025-05-19 11:30:54 +0800 | [diff] [blame] | 35 | DbusHelper& operator=(DbusHelper&&) = delete; |
Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 36 | |
Patrick Venture | 9b93692 | 2020-08-10 11:28:39 -0700 | [diff] [blame] | 37 | std::string getService(const std::string& intf, |
Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 38 | const std::string& path) override; |
| 39 | |
Patrick Venture | 9b93692 | 2020-08-10 11:28:39 -0700 | [diff] [blame] | 40 | void getProperties(const std::string& service, const std::string& path, |
Patrick Venture | 1df9e87 | 2020-10-08 15:35:01 -0700 | [diff] [blame] | 41 | SensorProperties* prop) override; |
Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 42 | |
Patrick Venture | 9b93692 | 2020-08-10 11:28:39 -0700 | [diff] [blame] | 43 | bool thresholdsAsserted(const std::string& service, |
Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 44 | const std::string& path) override; |
| 45 | |
| 46 | template <typename T> |
Patrick Venture | 9b93692 | 2020-08-10 11:28:39 -0700 | [diff] [blame] | 47 | void getProperty(const std::string& service, const std::string& path, |
| 48 | const std::string& interface, |
Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 49 | const std::string& propertyName, T& prop) |
| 50 | { |
| 51 | namespace log = phosphor::logging; |
| 52 | |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 53 | auto msg = _bus.new_method_call(service.c_str(), path.c_str(), |
| 54 | propertiesintf, "Get"); |
Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 55 | |
| 56 | msg.append(interface, propertyName); |
| 57 | |
| 58 | std::variant<T> result; |
| 59 | try |
| 60 | { |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 61 | auto valueResponseMsg = _bus.call(msg); |
Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 62 | valueResponseMsg.read(result); |
| 63 | } |
Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 64 | catch (const sdbusplus::exception_t& ex) |
Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 65 | { |
| 66 | log::log<log::level::ERR>("Get Property Failed", |
| 67 | log::entry("WHAT=%s", ex.what())); |
| 68 | throw; |
| 69 | } |
| 70 | |
| 71 | prop = std::get<T>(result); |
| 72 | } |
Patrick Venture | 8729eb9 | 2020-08-10 10:38:44 -0700 | [diff] [blame] | 73 | |
| 74 | private: |
Patrick Williams | cd1e78a | 2025-04-07 17:21:05 -0400 | [diff] [blame] | 75 | sdbusplus::bus_t& _bus; |
Patrick Venture | aadb30d | 2020-08-10 09:17:11 -0700 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | } // namespace pid_control |