blob: b4709eb85c1fb0116220037843b3fcf61624fc3d [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
Matt Spinler3b8e36e2017-07-28 10:44:45 -05009/**
10 * @class DeviceBusyException
11 *
12 * An internal exception which will be thrown when
13 * readSysfsWithCallout() hits an EAGAIN. Will never bubble
14 * up to terminate the application, nor does it need to be
15 * reported.
16 */
17class DeviceBusyException : public std::runtime_error
18{
19 public:
20
21 DeviceBusyException(const std::string& path) :
22 std::runtime_error(path + " busy")
23 {
24 }
25};
26
Brad Bishopa9b5f052017-01-17 14:50:08 -050027inline std::string make_sysfs_path(const std::string& path,
28 const std::string& type,
29 const std::string& id,
30 const std::string& entry)
Patrick Williams3667cf32015-10-20 22:39:11 -050031{
32 using namespace std::literals;
33
34 return path + "/"s + type + id + "_"s + entry;
35}
36
Brad Bishop613a5b32017-01-05 20:58:13 -050037
38/** @brief Find hwmon instances
39 *
40 * Look for a matching hwmon instance given an
41 * open firmware device path.
42 *
43 * @param[in] ofNode- The open firmware device path.
44 *
45 * @returns[in] - The hwmon instance path or an empty
46 * string if no match is found.
47 */
48std::string findHwmon(const std::string& ofNode);
49
Brad Bishop4db64422017-02-16 11:33:32 -050050/** @brief Read an hwmon sysfs value.
51 *
52 * Calls exit(3) with bad status on failure.
53 *
54 * @param[in] root - The hwmon class root.
55 * @param[in] instance - The hwmon instance (ex. hwmon1).
56 * @param[in] type - The hwmon type (ex. temp).
57 * @param[in] id - The hwmon id (ex. 1).
58 * @param[in] sensor - The hwmon sensor (ex. input).
Matt Spinler3b8e36e2017-07-28 10:44:45 -050059 * @param[in] throwDeviceBusy - will throw a DeviceBusyException
60 * on an EAGAIN errno instead of an error log exception.
Brad Bishop4db64422017-02-16 11:33:32 -050061 *
62 * @returns - The read value.
63 */
64int readSysfsWithCallout(const std::string& root,
65 const std::string& instance,
66 const std::string& type,
67 const std::string& id,
Matt Spinler3b8e36e2017-07-28 10:44:45 -050068 const std::string& sensor,
69 bool throwDeviceBusy = true);
Brad Bishop4db64422017-02-16 11:33:32 -050070
Matthew Barth048ac872017-03-09 14:36:08 -060071 /** @brief Write a hwmon sysfs value
72 *
73 * Calls exit(3) with bad status on failure
74 *
75 * @param[in] value - The value to be written
76 * @param[in] root - The hwmon class root.
77 * @param[in] instance - The hwmon instance (ex. hwmon1).
78 * @param[in] type - The hwmon type (ex. fan).
79 * @param[in] id - The hwmon id (ex. 1).
80 * @param[in] sensor - The hwmon sensor (ex. target).
81 *
82 * @returns - The value written
83 */
84uint64_t writeSysfsWithCallout(const uint64_t& value,
85 const std::string& root,
86 const std::string& instance,
87 const std::string& type,
88 const std::string& id,
89 const std::string& sensor);
90
Patrick Venture1e6324f2017-06-01 14:07:05 -070091}
92
Brad Bishop03476f12016-12-19 13:09:12 -050093// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4