xref: /openbmc/phosphor-hwmon/hwmonio.hpp (revision 75e56c67a10e9f4c617f9c72a87deb695322e212)
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 
11 /** @class HwmonIO
12  *  @brief Convenience wrappers for HWMON sysfs attribute IO.
13  *
14  *  Unburden the rest of the application from having to check
15  *  ENOENT after every hwmon attribute io operation.  Hwmon
16  *  device drivers can be unbound at any time; the program
17  *  cannot always be terminated externally before we try to
18  *  do an io.
19  */
20 class HwmonIO
21 {
22     public:
23         HwmonIO() = delete;
24         HwmonIO(const HwmonIO&) = default;
25         HwmonIO(HwmonIO&&) = default;
26         HwmonIO& operator=(const HwmonIO&) = default;
27         HwmonIO& operator=(HwmonIO&&) = default;
28         ~HwmonIO() = default;
29 
30         /** @brief Constructor
31          *
32          *  @param[in] path - hwmon instance root - eg:
33          *      /sys/class/hwmon/hwmon<N>
34          */
35         explicit HwmonIO(const std::string& path);
36 
37         /** @brief Perform formatted hwmon sysfs read.
38          *
39          *  Propagates any exceptions other than ENOENT.
40          *  ENOENT will result in a call to exit(0) in case
41          *  the underlying hwmon driver is unbound and
42          *  the program is inadvertently left running.
43          *
44          *  For possibly transient errors will retry up to
45          *  the specified number of times.
46          *
47          *  @param[in] type - The hwmon type (ex. temp).
48          *  @param[in] id - The hwmon id (ex. 1).
49          *  @param[in] sensor - The hwmon sensor (ex. input).
50          *  @param[in] retries - The number of times to retry.
51          *  @param[in] delay - The time to sleep between retry attempts.
52          *
53          *  @return val - The read value.
54          */
55         int64_t read(
56                 const std::string& type,
57                 const std::string& id,
58                 const std::string& sensor,
59                 size_t retries,
60                 std::chrono::milliseconds delay,
61                 bool isOCC = false) const;
62 
63         /** @brief Perform formatted hwmon sysfs write.
64          *
65          *  Propagates any exceptions other than ENOENT.
66          *  ENOENT will result in a call to exit(0) in case
67          *  the underlying hwmon driver is unbound and
68          *  the program is inadvertently left running.
69          *
70          *  For possibly transient errors will retry up to
71          *  the specified number of times.
72          *
73          *  @param[in] val - The value to be written.
74          *  @param[in] type - The hwmon type (ex. fan).
75          *  @param[in] id - The hwmon id (ex. 1).
76          *  @param[in] retries - The number of times to retry.
77          *  @param[in] delay - The time to sleep between retry attempts.
78          */
79         void write(
80                 uint32_t val,
81                 const std::string& type,
82                 const std::string& id,
83                 const std::string& sensor,
84                 size_t retries,
85                 std::chrono::milliseconds delay) const;
86 
87 
88         /** @brief Hwmon instance path access.
89          *
90          *  @return path - The hwmon instance path.
91          */
92         std::string path() const;
93 
94     private:
95         std::string p;
96 };
97 } // namespace hwmonio
98 
99 // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
100