forked from openbmc/phosphor-hwmon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sysfs.hpp
171 lines (153 loc) · 5.53 KB
/
sysfs.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#pragma once
#include <chrono>
#include <exception>
#include <fstream>
#include <string>
namespace sysfs {
inline std::string make_sysfs_path(const std::string& path,
const std::string& type,
const std::string& id,
const std::string& entry)
{
using namespace std::literals;
if (entry.empty()) {
return path + "/"s + type + id;
}
return path + "/"s + type + id + "_"s + entry;
}
/** @brief Return the path to the phandle file matching value in io-channels.
*
* This function will take two passed in paths.
* One path is used to find the io-channels file.
* The other path is used to find the phandle file.
* The 4 byte phandle value is read from the phandle file(s).
* The 4 byte phandle value and 4 byte index value is read from io-channels.
* When a match is found, the path to the matching phandle file is returned.
*
* @param[in] iochanneldir - Path to file for getting phandle from io-channels
* @param[in] phandledir - Path to use for reading from phandle file
*
* @return Path to phandle file with value matching that in io-channels
*/
std::string findPhandleMatch(
const std::string& iochanneldir,
const std::string& phandledir);
/** @brief Find hwmon instances from an open-firmware device tree path
*
* Look for a matching hwmon instance given an
* open firmware device path.
*
* @param[in] ofNode- The open firmware device path.
*
* @returns[in] - The hwmon instance path or an empty
* string if no match is found.
*/
std::string findHwmonFromOFPath(const std::string& ofNode);
/** @brief Find hwmon instances from a device path
*
* Look for a matching hwmon instance given a device path that
* starts with /devices. This path is the DEVPATH udev attribute
* for the device except it has the '/hwmon/hwmonN' stripped off.
*
* @param[in] devPath - The device path.
*
* @return - The hwmon instance path or an empty
* string if no match is found.
*/
std::string findHwmonFromDevPath(const std::string& devPath);
/** @brief Return the path to use for a call out.
*
* Return an empty string if a callout path cannot be
* found.
*
* @param[in] instancePath - /sys/class/hwmon/hwmon<N> path.
*
* @return Path to use for call out
*/
std::string findCalloutPath(const std::string& instancePath);
namespace hwmonio
{
static constexpr auto retries = 10;
static constexpr auto delay = std::chrono::milliseconds{100};
/** @class HwmonIO
* @brief Convenience wrappers for HWMON sysfs attribute IO.
*
* Unburden the rest of the application from having to check
* ENOENT after every hwmon attribute io operation. Hwmon
* device drivers can be unbound at any time; the program
* cannot always be terminated externally before we try to
* do an io.
*/
class HwmonIO
{
public:
HwmonIO() = delete;
HwmonIO(const HwmonIO&) = default;
HwmonIO(HwmonIO&&) = default;
HwmonIO& operator=(const HwmonIO&) = default;
HwmonIO& operator=(HwmonIO&&) = default;
~HwmonIO() = default;
/** @brief Constructor
*
* @param[in] path - hwmon instance root - eg:
* /sys/class/hwmon/hwmon<N>
*/
explicit HwmonIO(const std::string& path);
/** @brief Perform formatted hwmon sysfs read.
*
* Propagates any exceptions other than ENOENT.
* ENOENT will result in a call to exit(0) in case
* the underlying hwmon driver is unbound and
* the program is inadvertently left running.
*
* For possibly transient errors will retry up to
* the specified number of times.
*
* @param[in] type - The hwmon type (ex. temp).
* @param[in] id - The hwmon id (ex. 1).
* @param[in] sensor - The hwmon sensor (ex. input).
* @param[in] retries - The number of times to retry.
* @param[in] delay - The time to sleep between retry attempts.
*
* @return val - The read value.
*/
int64_t read(
const std::string& type,
const std::string& id,
const std::string& sensor,
size_t retries,
std::chrono::milliseconds delay) const;
/** @brief Perform formatted hwmon sysfs write.
*
* Propagates any exceptions other than ENOENT.
* ENOENT will result in a call to exit(0) in case
* the underlying hwmon driver is unbound and
* the program is inadvertently left running.
*
* For possibly transient errors will retry up to
* the specified number of times.
*
* @param[in] val - The value to be written.
* @param[in] type - The hwmon type (ex. fan).
* @param[in] id - The hwmon id (ex. 1).
* @param[in] retries - The number of times to retry.
* @param[in] delay - The time to sleep between retry attempts.
*/
void write(
uint32_t val,
const std::string& type,
const std::string& id,
const std::string& sensor,
size_t retries,
std::chrono::milliseconds delay) const;
/** @brief Hwmon instance path access.
*
* @return path - The hwmon instance path.
*/
std::string path() const;
private:
std::string p;
};
} // namespace hwmonio
}
// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4