blob: 934c7c25e72787cb33a2f5ae946ca676feb7f45f [file] [log] [blame]
Brad Bishop26b815f2017-01-04 13:32:47 -05001#pragma once
Patrick Williams3667cf32015-10-20 22:39:11 -05002
Matt Spinler3b8e36e2017-07-28 10:44:45 -05003#include <exception>
Patrick Williams3667cf32015-10-20 22:39:11 -05004#include <fstream>
5#include <string>
6
Patrick Venture1e6324f2017-06-01 14:07:05 -07007namespace sysfs {
8
Brad Bishopa9b5f052017-01-17 14:50:08 -05009inline std::string make_sysfs_path(const std::string& path,
10 const std::string& type,
11 const std::string& id,
12 const std::string& entry)
Patrick Williams3667cf32015-10-20 22:39:11 -050013{
14 using namespace std::literals;
15
16 return path + "/"s + type + id + "_"s + entry;
17}
18
Brad Bishopf4bf63a2017-08-28 15:39:19 -040019/** @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 */
33std::string findPhandleMatch(
34 const std::string& iochanneldir,
35 const std::string& phandledir);
Brad Bishop613a5b32017-01-05 20:58:13 -050036
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 */
47std::string findHwmon(const std::string& ofNode);
48
Brad Bishop431d26a2017-08-25 09:47:58 -040049/** @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 */
58std::string findCalloutPath(const std::string& instancePath);
59
Brad Bishop8b574a72017-08-25 16:17:19 -040060namespace 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 */
72class 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
Patrick Venture1e6324f2017-06-01 14:07:05 -0700135}
136
Brad Bishop03476f12016-12-19 13:09:12 -0500137// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4