blob: ff182042b7de6172c7e7af0f702025bbc76a874c [file] [log] [blame]
Patrick Venture75e56c62018-04-20 18:10:15 -07001#pragma once
2
3#include <chrono>
4#include <string>
5
Patrick Venture043d3232018-08-31 10:10:53 -07006namespace hwmonio
7{
Patrick Venture75e56c62018-04-20 18:10:15 -07008
9static constexpr auto retries = 10;
10static constexpr auto delay = std::chrono::milliseconds{100};
11
Patrick Venturecaaebd12019-06-21 14:56:07 -070012/** @class FileSystemInterface
13 * @brief Abstract base class allowing testing of HwmonIO.
14 *
15 * This is used to provide testing of behaviors within HwmonIO.
16 */
17class FileSystemInterface
18{
19 public:
20 virtual ~FileSystemInterface() = default;
21 virtual int64_t read(const std::string& path) const = 0;
22 virtual void write(const std::string& path, uint32_t value) const = 0;
23};
24
25class FileSystem : public FileSystemInterface
26{
27 public:
28 int64_t read(const std::string& path) const override;
29 void write(const std::string& path, uint32_t value) const override;
30};
31
32extern FileSystem fileSystemImpl;
33
Patrick Venture50552372018-06-07 10:53:56 -070034/** @class HwmonIOInterface
35 * @brief Abstract base class defining a HwmonIOInterface.
36 *
37 * This is initially to provide easier testing through injection,
38 * however, could in theory support non-sysfs handling of hwmon IO.
39 */
40class HwmonIOInterface
41{
Patrick Venture043d3232018-08-31 10:10:53 -070042 public:
43 virtual ~HwmonIOInterface() = default;
Patrick Venture50552372018-06-07 10:53:56 -070044
Patrick Venture043d3232018-08-31 10:10:53 -070045 virtual int64_t read(const std::string& type, const std::string& id,
46 const std::string& sensor, size_t retries,
47 std::chrono::milliseconds delay) const = 0;
Patrick Venture50552372018-06-07 10:53:56 -070048
Patrick Venture043d3232018-08-31 10:10:53 -070049 virtual void write(uint32_t val, const std::string& type,
50 const std::string& id, const std::string& sensor,
51 size_t retries,
52 std::chrono::milliseconds delay) const = 0;
Patrick Venture50552372018-06-07 10:53:56 -070053
Patrick Venture043d3232018-08-31 10:10:53 -070054 virtual std::string path() const = 0;
Patrick Venture50552372018-06-07 10:53:56 -070055};
56
Patrick Venture75e56c62018-04-20 18:10:15 -070057/** @class HwmonIO
58 * @brief Convenience wrappers for HWMON sysfs attribute IO.
59 *
60 * Unburden the rest of the application from having to check
61 * ENOENT after every hwmon attribute io operation. Hwmon
62 * device drivers can be unbound at any time; the program
63 * cannot always be terminated externally before we try to
64 * do an io.
65 */
Patrick Venture50552372018-06-07 10:53:56 -070066class HwmonIO : public HwmonIOInterface
Patrick Venture75e56c62018-04-20 18:10:15 -070067{
Patrick Venture043d3232018-08-31 10:10:53 -070068 public:
69 HwmonIO() = delete;
70 HwmonIO(const HwmonIO&) = default;
71 HwmonIO(HwmonIO&&) = default;
72 HwmonIO& operator=(const HwmonIO&) = default;
73 HwmonIO& operator=(HwmonIO&&) = default;
74 ~HwmonIO() = default;
Patrick Venture75e56c62018-04-20 18:10:15 -070075
Patrick Venture043d3232018-08-31 10:10:53 -070076 /** @brief Constructor
77 *
78 * @param[in] path - hwmon instance root - eg:
79 * /sys/class/hwmon/hwmon<N>
80 */
Patrick Venturecaaebd12019-06-21 14:56:07 -070081 explicit HwmonIO(const std::string& path,
82 const FileSystemInterface* intf = &fileSystemImpl);
Patrick Venture75e56c62018-04-20 18:10:15 -070083
Patrick Venture043d3232018-08-31 10:10:53 -070084 /** @brief Perform formatted hwmon sysfs read.
85 *
86 * Propagates any exceptions other than ENOENT.
87 * ENOENT will result in a call to exit(0) in case
88 * the underlying hwmon driver is unbound and
89 * the program is inadvertently left running.
90 *
91 * For possibly transient errors will retry up to
92 * the specified number of times.
93 *
94 * @param[in] type - The hwmon type (ex. temp).
95 * @param[in] id - The hwmon id (ex. 1).
96 * @param[in] sensor - The hwmon sensor (ex. input).
97 * @param[in] retries - The number of times to retry.
98 * @param[in] delay - The time to sleep between retry attempts.
99 *
100 * @return val - The read value.
101 */
102 int64_t read(const std::string& type, const std::string& id,
103 const std::string& sensor, size_t retries,
104 std::chrono::milliseconds delay) const override;
Patrick Venture75e56c62018-04-20 18:10:15 -0700105
Patrick Venture043d3232018-08-31 10:10:53 -0700106 /** @brief Perform formatted hwmon sysfs write.
107 *
108 * Propagates any exceptions other than ENOENT.
109 * ENOENT will result in a call to exit(0) in case
110 * the underlying hwmon driver is unbound and
111 * the program is inadvertently left running.
112 *
113 * For possibly transient errors will retry up to
114 * the specified number of times.
115 *
116 * @param[in] val - The value to be written.
117 * @param[in] type - The hwmon type (ex. fan).
118 * @param[in] id - The hwmon id (ex. 1).
119 * @param[in] retries - The number of times to retry.
120 * @param[in] delay - The time to sleep between retry attempts.
121 */
122 void write(uint32_t val, const std::string& type, const std::string& id,
123 const std::string& sensor, size_t retries,
124 std::chrono::milliseconds delay) const override;
Patrick Venture75e56c62018-04-20 18:10:15 -0700125
Patrick Venture043d3232018-08-31 10:10:53 -0700126 /** @brief Hwmon instance path access.
127 *
128 * @return path - The hwmon instance path.
129 */
130 std::string path() const override;
Patrick Venture75e56c62018-04-20 18:10:15 -0700131
Patrick Venture043d3232018-08-31 10:10:53 -0700132 private:
Patrick Venture4d9506c2018-12-19 14:19:34 -0800133 std::string _p;
Patrick Venturecaaebd12019-06-21 14:56:07 -0700134 const FileSystemInterface* _intf;
Patrick Venture75e56c62018-04-20 18:10:15 -0700135};
Patrick Venture16805a62019-06-21 14:19:33 -0700136
Patrick Venture75e56c62018-04-20 18:10:15 -0700137} // namespace hwmonio
138
139// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4