Patrick Venture | 75e56c6 | 2018-04-20 18:10:15 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <chrono> |
| 4 | #include <string> |
| 5 | |
| 6 | namespace hwmonio { |
| 7 | |
| 8 | static constexpr auto retries = 10; |
| 9 | static constexpr auto delay = std::chrono::milliseconds{100}; |
| 10 | |
Patrick Venture | 5055237 | 2018-06-07 10:53:56 -0700 | [diff] [blame] | 11 | /** @class HwmonIOInterface |
| 12 | * @brief Abstract base class defining a HwmonIOInterface. |
| 13 | * |
| 14 | * This is initially to provide easier testing through injection, |
| 15 | * however, could in theory support non-sysfs handling of hwmon IO. |
| 16 | */ |
| 17 | class HwmonIOInterface |
| 18 | { |
| 19 | public: |
| 20 | virtual ~HwmonIOInterface() = default; |
| 21 | |
| 22 | virtual int64_t read( |
| 23 | const std::string& type, |
| 24 | const std::string& id, |
| 25 | const std::string& sensor, |
| 26 | size_t retries, |
| 27 | std::chrono::milliseconds delay) const = 0; |
| 28 | |
| 29 | virtual void write( |
| 30 | uint32_t val, |
| 31 | const std::string& type, |
| 32 | const std::string& id, |
| 33 | const std::string& sensor, |
| 34 | size_t retries, |
| 35 | std::chrono::milliseconds delay) const = 0; |
| 36 | |
| 37 | virtual std::string path() const = 0; |
| 38 | }; |
| 39 | |
Patrick Venture | 75e56c6 | 2018-04-20 18:10:15 -0700 | [diff] [blame] | 40 | /** @class HwmonIO |
| 41 | * @brief Convenience wrappers for HWMON sysfs attribute IO. |
| 42 | * |
| 43 | * Unburden the rest of the application from having to check |
| 44 | * ENOENT after every hwmon attribute io operation. Hwmon |
| 45 | * device drivers can be unbound at any time; the program |
| 46 | * cannot always be terminated externally before we try to |
| 47 | * do an io. |
| 48 | */ |
Patrick Venture | 5055237 | 2018-06-07 10:53:56 -0700 | [diff] [blame] | 49 | class HwmonIO : public HwmonIOInterface |
Patrick Venture | 75e56c6 | 2018-04-20 18:10:15 -0700 | [diff] [blame] | 50 | { |
| 51 | public: |
| 52 | HwmonIO() = delete; |
| 53 | HwmonIO(const HwmonIO&) = default; |
| 54 | HwmonIO(HwmonIO&&) = default; |
| 55 | HwmonIO& operator=(const HwmonIO&) = default; |
| 56 | HwmonIO& operator=(HwmonIO&&) = default; |
| 57 | ~HwmonIO() = default; |
| 58 | |
| 59 | /** @brief Constructor |
| 60 | * |
| 61 | * @param[in] path - hwmon instance root - eg: |
| 62 | * /sys/class/hwmon/hwmon<N> |
| 63 | */ |
| 64 | explicit HwmonIO(const std::string& path); |
| 65 | |
| 66 | /** @brief Perform formatted hwmon sysfs read. |
| 67 | * |
| 68 | * Propagates any exceptions other than ENOENT. |
| 69 | * ENOENT will result in a call to exit(0) in case |
| 70 | * the underlying hwmon driver is unbound and |
| 71 | * the program is inadvertently left running. |
| 72 | * |
| 73 | * For possibly transient errors will retry up to |
| 74 | * the specified number of times. |
| 75 | * |
| 76 | * @param[in] type - The hwmon type (ex. temp). |
| 77 | * @param[in] id - The hwmon id (ex. 1). |
| 78 | * @param[in] sensor - The hwmon sensor (ex. input). |
| 79 | * @param[in] retries - The number of times to retry. |
| 80 | * @param[in] delay - The time to sleep between retry attempts. |
| 81 | * |
| 82 | * @return val - The read value. |
| 83 | */ |
| 84 | int64_t read( |
| 85 | const std::string& type, |
| 86 | const std::string& id, |
| 87 | const std::string& sensor, |
| 88 | size_t retries, |
Patrick Venture | 5055237 | 2018-06-07 10:53:56 -0700 | [diff] [blame] | 89 | std::chrono::milliseconds delay) const override; |
Patrick Venture | 75e56c6 | 2018-04-20 18:10:15 -0700 | [diff] [blame] | 90 | |
| 91 | /** @brief Perform formatted hwmon sysfs write. |
| 92 | * |
| 93 | * Propagates any exceptions other than ENOENT. |
| 94 | * ENOENT will result in a call to exit(0) in case |
| 95 | * the underlying hwmon driver is unbound and |
| 96 | * the program is inadvertently left running. |
| 97 | * |
| 98 | * For possibly transient errors will retry up to |
| 99 | * the specified number of times. |
| 100 | * |
| 101 | * @param[in] val - The value to be written. |
| 102 | * @param[in] type - The hwmon type (ex. fan). |
| 103 | * @param[in] id - The hwmon id (ex. 1). |
| 104 | * @param[in] retries - The number of times to retry. |
| 105 | * @param[in] delay - The time to sleep between retry attempts. |
| 106 | */ |
| 107 | void write( |
| 108 | uint32_t val, |
| 109 | const std::string& type, |
| 110 | const std::string& id, |
| 111 | const std::string& sensor, |
| 112 | size_t retries, |
Patrick Venture | 5055237 | 2018-06-07 10:53:56 -0700 | [diff] [blame] | 113 | std::chrono::milliseconds delay) const override; |
Patrick Venture | 75e56c6 | 2018-04-20 18:10:15 -0700 | [diff] [blame] | 114 | |
| 115 | |
| 116 | /** @brief Hwmon instance path access. |
| 117 | * |
| 118 | * @return path - The hwmon instance path. |
| 119 | */ |
Patrick Venture | 5055237 | 2018-06-07 10:53:56 -0700 | [diff] [blame] | 120 | std::string path() const override; |
Patrick Venture | 75e56c6 | 2018-04-20 18:10:15 -0700 | [diff] [blame] | 121 | |
| 122 | private: |
| 123 | std::string p; |
| 124 | }; |
| 125 | } // namespace hwmonio |
| 126 | |
| 127 | // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |