xref: /openbmc/phosphor-hwmon/hwmonio.hpp (revision 0b3050582046e509d517360c413a6f90ca7d9b3a)
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) const;
61 
62         /** @brief Perform formatted hwmon sysfs write.
63          *
64          *  Propagates any exceptions other than ENOENT.
65          *  ENOENT will result in a call to exit(0) in case
66          *  the underlying hwmon driver is unbound and
67          *  the program is inadvertently left running.
68          *
69          *  For possibly transient errors will retry up to
70          *  the specified number of times.
71          *
72          *  @param[in] val - The value to be written.
73          *  @param[in] type - The hwmon type (ex. fan).
74          *  @param[in] id - The hwmon id (ex. 1).
75          *  @param[in] retries - The number of times to retry.
76          *  @param[in] delay - The time to sleep between retry attempts.
77          */
78         void write(
79                 uint32_t val,
80                 const std::string& type,
81                 const std::string& id,
82                 const std::string& sensor,
83                 size_t retries,
84                 std::chrono::milliseconds delay) const;
85 
86 
87         /** @brief Hwmon instance path access.
88          *
89          *  @return path - The hwmon instance path.
90          */
91         std::string path() const;
92 
93     private:
94         std::string p;
95 };
96 } // namespace hwmonio
97 
98 // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
99