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> |
| 14 | using ServerObject = typename sdbusplus::server::object::object<T...>; |
| 15 | |
| 16 | using ValueInterface = sdbusplus::xyz::openbmc_project::Sensor::server::Value; |
| 17 | using ValueObject = ServerObject<ValueInterface>; |
| 18 | |
James Feist | 8154e32 | 2020-06-22 17:12:18 -0700 | [diff] [blame] | 19 | class ValueHelper : public ValueInterface |
| 20 | { |
| 21 | |
| 22 | public: |
| 23 | auto operator()() const |
| 24 | { |
| 25 | return value(); |
| 26 | } |
| 27 | }; |
| 28 | |
| 29 | constexpr bool usingDouble = |
| 30 | std::is_same_v<std::result_of_t<ValueHelper()>, double>; |
| 31 | using ValueType = std::conditional_t<usingDouble, double, int64_t>; |
| 32 | |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 33 | /* |
| 34 | * HostSensor object is a Sensor derivative that also implements a ValueObject, |
| 35 | * which comes from the dbus as an object that implements Sensor.Value. |
| 36 | */ |
| 37 | class HostSensor : public Sensor, public ValueObject |
| 38 | { |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 39 | public: |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 40 | static std::unique_ptr<Sensor> createTemp(const std::string& name, |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 41 | int64_t timeout, |
| 42 | sdbusplus::bus::bus& bus, |
| 43 | const char* objPath, bool defer); |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 44 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 45 | HostSensor(const std::string& name, int64_t timeout, |
| 46 | sdbusplus::bus::bus& bus, const char* objPath, bool defer) : |
| 47 | Sensor(name, timeout), |
| 48 | ValueObject(bus, objPath, defer) |
Patrick Venture | a83a3ec | 2020-08-04 09:52:05 -0700 | [diff] [blame] | 49 | {} |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 50 | |
James Feist | 8154e32 | 2020-06-22 17:12:18 -0700 | [diff] [blame] | 51 | ValueType value(ValueType value) override; |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 52 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 53 | ReadReturn read(void) override; |
| 54 | void write(double 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 | private: |
| 57 | /* |
| 58 | * _lock will be used to make sure _updated & _value are updated |
| 59 | * together. |
| 60 | */ |
| 61 | std::mutex _lock; |
| 62 | std::chrono::high_resolution_clock::time_point _updated; |
| 63 | double _value = 0; |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 64 | }; |