xref: /openbmc/phosphor-hwmon/sysfs.hpp (revision f0c3fc13382f7fbea8882d0b223a0c16c0fbe700)
1 #pragma once
2 
3 #include <exception>
4 #include <fstream>
5 #include <string>
6 
7 namespace sysfs {
8 
9 inline std::string make_sysfs_path(const std::string& path,
10                                    const std::string& type,
11                                    const std::string& id,
12                                    const std::string& entry)
13 {
14     using namespace std::literals;
15 
16     return path + "/"s + type + id + "_"s + entry;
17 }
18 
19 /** @brief Return the path to the phandle file matching value in io-channels.
20  *
21  *  This function will take two passed in paths.
22  *  One path is used to find the io-channels file.
23  *  The other path is used to find the phandle file.
24  *  The 4 byte phandle value is read from the phandle file(s).
25  *  The 4 byte phandle value and 4 byte index value is read from io-channels.
26  *  When a match is found, the path to the matching phandle file is returned.
27  *
28  *  @param[in] iochanneldir - Path to file for getting phandle from io-channels
29  *  @param[in] phandledir - Path to use for reading from phandle file
30  *
31  *  @return Path to phandle file with value matching that in io-channels
32  */
33 std::string findPhandleMatch(
34         const std::string& iochanneldir,
35         const std::string& phandledir);
36 
37 /** @brief Find hwmon instances
38  *
39  *  Look for a matching hwmon instance given an
40  *  open firmware device path.
41  *
42  *  @param[in] ofNode- The open firmware device path.
43  *
44  *  @returns[in] - The hwmon instance path or an empty
45  *                 string if no match is found.
46  */
47 std::string findHwmon(const std::string& ofNode);
48 
49 /** @brief Return the path to use for a call out.
50  *
51  *  Return an empty string if a callout path cannot be
52  *  found.
53  *
54  *  @param[in] instancePath - /sys/class/hwmon/hwmon<N> path.
55  *
56  *  @return Path to use for call out
57  */
58 std::string findCalloutPath(const std::string& instancePath);
59 
60 namespace hwmonio
61 {
62 
63 /** @class HwmonIO
64  *  @brief Convenience wrappers for HWMON sysfs attribute IO.
65  *
66  *  Unburden the rest of the application from having to check
67  *  ENOENT after every hwmon attribute io operation.  Hwmon
68  *  device drivers can be unbound at any time; the program
69  *  cannot always be terminated externally before we try to
70  *  do an io.
71  */
72 class HwmonIO
73 {
74     public:
75         HwmonIO() = delete;
76         HwmonIO(const HwmonIO&) = default;
77         HwmonIO(HwmonIO&&) = default;
78         HwmonIO& operator=(const HwmonIO&) = default;
79         HwmonIO& operator=(HwmonIO&&) = default;
80         ~HwmonIO() = default;
81 
82         /** @brief Constructor
83          *
84          *  @param[in] path - hwmon instance root - eg:
85          *      /sys/class/hwmon/hwmon<N>
86          */
87         explicit HwmonIO(const std::string& path);
88 
89         /** @brief Perform formatted hwmon sysfs read.
90          *
91          *  Propogates any exceptions other than ENOENT.
92          *  ENOENT will result in a call to exit(0) in case
93          *  the underlying hwmon driver is unbound and
94          *  the program is inadvertently left running.
95          *
96          *  @param[in] type - The hwmon type (ex. temp).
97          *  @param[in] id - The hwmon id (ex. 1).
98          *  @param[in] sensor - The hwmon sensor (ex. input).
99          *
100          *  @return val - The read value.
101          */
102         uint32_t read(
103                 const std::string& type,
104                 const std::string& id,
105                 const std::string& sensor) const;
106 
107         /** @brief Perform formatted hwmon sysfs write.
108          *
109          *  Propogates any exceptions other than ENOENT.
110          *  ENOENT will result in a call to exit(0) in case
111          *  the underlying hwmon driver is unbound and
112          *  the program is inadvertently left running.
113          *
114          *  @param[in] val - The value to be written.
115          *  @param[in] type - The hwmon type (ex. fan).
116          *  @param[in] id - The hwmon id (ex. 1).
117          *  @param[in] sensor - The hwmon sensor (ex. target).
118          */
119         void write(
120                 uint32_t val,
121                 const std::string& type,
122                 const std::string& id,
123                 const std::string& sensor) const;
124 
125         /** @brief Hwmon instance path access.
126          *
127          *  @return path - The hwmon instance path.
128          */
129         std::string path() const;
130 
131     private:
132         std::string p;
133 };
134 } // namespace hwmonio
135 }
136 
137 // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
138