xref: /openbmc/phosphor-hwmon/sysfs.hpp (revision 4db64422)
126b815faSBrad Bishop #pragma once
26292aeedSMatthew Barth 
36292aeedSMatthew Barth #include <fstream>
46292aeedSMatthew Barth #include <string>
56292aeedSMatthew Barth 
66292aeedSMatthew Barth template <typename T>
76292aeedSMatthew Barth void read_sysfs(const std::string& path, T& val)
86292aeedSMatthew Barth {
96292aeedSMatthew Barth     std::ifstream s(path);
106292aeedSMatthew Barth     s >> val;
116292aeedSMatthew Barth }
126292aeedSMatthew Barth 
136292aeedSMatthew Barth template <typename T>
146292aeedSMatthew Barth void write_sysfs(const std::string& path, const T& val)
156292aeedSMatthew Barth {
166292aeedSMatthew Barth     std::ofstream s(path);
176292aeedSMatthew Barth     s << val;
186292aeedSMatthew Barth }
196292aeedSMatthew Barth 
20a9b5f05bSBrad Bishop inline std::string make_sysfs_path(const std::string& path,
216292aeedSMatthew Barth                                    const std::string& type,
226292aeedSMatthew Barth                                    const std::string& id,
236292aeedSMatthew Barth                                    const std::string& entry)
246292aeedSMatthew Barth {
256292aeedSMatthew Barth     using namespace std::literals;
266292aeedSMatthew Barth 
276292aeedSMatthew Barth     return path + "/"s + type + id + "_"s + entry;
286292aeedSMatthew Barth }
296292aeedSMatthew Barth 
30613a5b37SBrad Bishop 
31613a5b37SBrad Bishop /** @brief Find hwmon instances
32613a5b37SBrad Bishop  *
33613a5b37SBrad Bishop  *  Look for a matching hwmon instance given an
34613a5b37SBrad Bishop  *  open firmware device path.
35613a5b37SBrad Bishop  *
36613a5b37SBrad Bishop  *  @param[in] ofNode- The open firmware device path.
37613a5b37SBrad Bishop  *
38613a5b37SBrad Bishop  *  @returns[in] - The hwmon instance path or an empty
39613a5b37SBrad Bishop  *                 string if no match is found.
40613a5b37SBrad Bishop  */
41613a5b37SBrad Bishop std::string findHwmon(const std::string& ofNode);
42613a5b37SBrad Bishop 
43*4db64422SBrad Bishop /** @brief Read an hwmon sysfs value.
44*4db64422SBrad Bishop  *
45*4db64422SBrad Bishop  *  Calls exit(3) with bad status on failure.
46*4db64422SBrad Bishop  *
47*4db64422SBrad Bishop  *  @param[in] root - The hwmon class root.
48*4db64422SBrad Bishop  *  @param[in] instance - The hwmon instance (ex. hwmon1).
49*4db64422SBrad Bishop  *  @param[in] type - The hwmon type (ex. temp).
50*4db64422SBrad Bishop  *  @param[in] id - The hwmon id (ex. 1).
51*4db64422SBrad Bishop  *  @param[in] sensor - The hwmon sensor (ex. input).
52*4db64422SBrad Bishop  *
53*4db64422SBrad Bishop  *  @returns - The read value.
54*4db64422SBrad Bishop  */
55*4db64422SBrad Bishop int readSysfsWithCallout(const std::string& root,
56*4db64422SBrad Bishop                          const std::string& instance,
57*4db64422SBrad Bishop                          const std::string& type,
58*4db64422SBrad Bishop                          const std::string& id,
59*4db64422SBrad Bishop                          const std::string& sensor);
60*4db64422SBrad Bishop 
6103476f11SBrad Bishop // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
62