Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 3 | #include "sensor.hpp" |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 4 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 5 | #include <sdbusplus/bus.hpp> |
| 6 | #include <sdbusplus/server.hpp> |
Patrick Venture | 1248b15 | 2018-10-30 19:09:54 -0700 | [diff] [blame] | 7 | #include <xyz/openbmc_project/Sensor/Value/server.hpp> |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 8 | |
Patrick Venture | a83a3ec | 2020-08-04 09:52:05 -0700 | [diff] [blame] | 9 | #include <memory> |
| 10 | #include <mutex> |
| 11 | #include <type_traits> |
| 12 | |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 13 | template <typename... T> |
Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 14 | using ServerObject = typename sdbusplus::server::object_t<T...>; |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 15 | |
| 16 | using ValueInterface = sdbusplus::xyz::openbmc_project::Sensor::server::Value; |
| 17 | using ValueObject = ServerObject<ValueInterface>; |
| 18 | |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 19 | namespace pid_control |
| 20 | { |
| 21 | |
James Feist | 8154e32 | 2020-06-22 17:12:18 -0700 | [diff] [blame] | 22 | class ValueHelper : public ValueInterface |
| 23 | { |
James Feist | 8154e32 | 2020-06-22 17:12:18 -0700 | [diff] [blame] | 24 | public: |
| 25 | auto operator()() const |
| 26 | { |
| 27 | return value(); |
| 28 | } |
| 29 | }; |
| 30 | |
| 31 | constexpr bool usingDouble = |
| 32 | std::is_same_v<std::result_of_t<ValueHelper()>, double>; |
| 33 | using ValueType = std::conditional_t<usingDouble, double, int64_t>; |
| 34 | |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 35 | /* |
| 36 | * HostSensor object is a Sensor derivative that also implements a ValueObject, |
| 37 | * which comes from the dbus as an object that implements Sensor.Value. |
| 38 | */ |
| 39 | class HostSensor : public Sensor, public ValueObject |
| 40 | { |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 41 | public: |
Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 42 | static std::unique_ptr<Sensor> |
| 43 | createTemp(const std::string& name, int64_t timeout, |
| 44 | sdbusplus::bus_t& bus, const char* objPath, bool defer); |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 45 | |
Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 46 | HostSensor(const std::string& name, int64_t timeout, sdbusplus::bus_t& bus, |
| 47 | const char* objPath, bool defer) : |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 48 | Sensor(name, timeout), |
Patrick Williams | d8c5a45 | 2022-04-07 16:08:56 -0500 | [diff] [blame] | 49 | ValueObject(bus, objPath, |
| 50 | defer ? ValueObject::action::defer_emit |
| 51 | : ValueObject::action::emit_object_added) |
Patrick Venture | a83a3ec | 2020-08-04 09:52:05 -0700 | [diff] [blame] | 52 | {} |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 53 | |
James Feist | 8154e32 | 2020-06-22 17:12:18 -0700 | [diff] [blame] | 54 | ValueType value(ValueType value) override; |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 55 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 56 | ReadReturn read(void) override; |
| 57 | void write(double value) override; |
Harvey Wu | 17971ef | 2021-02-17 14:36:38 +0800 | [diff] [blame] | 58 | bool getFailed(void) override; |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 59 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 60 | private: |
| 61 | /* |
| 62 | * _lock will be used to make sure _updated & _value are updated |
| 63 | * together. |
| 64 | */ |
| 65 | std::mutex _lock; |
| 66 | std::chrono::high_resolution_clock::time_point _updated; |
| 67 | double _value = 0; |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 68 | }; |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 69 | |
| 70 | } // namespace pid_control |