xref: /openbmc/phosphor-hwmon/sysfs.hpp (revision 4db64422217b51b4f392521568fbcb30dc56a3e9)
1 #pragma once
2 
3 #include <fstream>
4 #include <string>
5 
6 template <typename T>
7 void read_sysfs(const std::string& path, T& val)
8 {
9     std::ifstream s(path);
10     s >> val;
11 }
12 
13 template <typename T>
14 void write_sysfs(const std::string& path, const T& val)
15 {
16     std::ofstream s(path);
17     s << val;
18 }
19 
20 inline std::string make_sysfs_path(const std::string& path,
21                                    const std::string& type,
22                                    const std::string& id,
23                                    const std::string& entry)
24 {
25     using namespace std::literals;
26 
27     return path + "/"s + type + id + "_"s + entry;
28 }
29 
30 
31 /** @brief Find hwmon instances
32  *
33  *  Look for a matching hwmon instance given an
34  *  open firmware device path.
35  *
36  *  @param[in] ofNode- The open firmware device path.
37  *
38  *  @returns[in] - The hwmon instance path or an empty
39  *                 string if no match is found.
40  */
41 std::string findHwmon(const std::string& ofNode);
42 
43 /** @brief Read an hwmon sysfs value.
44  *
45  *  Calls exit(3) with bad status on failure.
46  *
47  *  @param[in] root - The hwmon class root.
48  *  @param[in] instance - The hwmon instance (ex. hwmon1).
49  *  @param[in] type - The hwmon type (ex. temp).
50  *  @param[in] id - The hwmon id (ex. 1).
51  *  @param[in] sensor - The hwmon sensor (ex. input).
52  *
53  *  @returns - The read value.
54  */
55 int readSysfsWithCallout(const std::string& root,
56                          const std::string& instance,
57                          const std::string& type,
58                          const std::string& id,
59                          const std::string& sensor);
60 
61 // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
62