xref: /openbmc/phosphor-hwmon/sysfs.hpp (revision f4bf63adc63acd8f73a1af9409ef5fb377fd03ba)
1 #pragma once
2 
3 #include <exception>
4 #include <fstream>
5 #include <string>
6 
7 namespace sysfs {
8 
9 /**
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  */
17 class 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 
27 inline std::string make_sysfs_path(const std::string& path,
28                                    const std::string& type,
29                                    const std::string& id,
30                                    const std::string& entry)
31 {
32     using namespace std::literals;
33 
34     return path + "/"s + type + id + "_"s + entry;
35 }
36 
37 /** @brief Return the path to the phandle file matching value in io-channels.
38  *
39  *  This function will take two passed in paths.
40  *  One path is used to find the io-channels file.
41  *  The other path is used to find the phandle file.
42  *  The 4 byte phandle value is read from the phandle file(s).
43  *  The 4 byte phandle value and 4 byte index value is read from io-channels.
44  *  When a match is found, the path to the matching phandle file is returned.
45  *
46  *  @param[in] iochanneldir - Path to file for getting phandle from io-channels
47  *  @param[in] phandledir - Path to use for reading from phandle file
48  *
49  *  @return Path to phandle file with value matching that in io-channels
50  */
51 std::string findPhandleMatch(
52         const std::string& iochanneldir,
53         const std::string& phandledir);
54 
55 /** @brief Find hwmon instances
56  *
57  *  Look for a matching hwmon instance given an
58  *  open firmware device path.
59  *
60  *  @param[in] ofNode- The open firmware device path.
61  *
62  *  @returns[in] - The hwmon instance path or an empty
63  *                 string if no match is found.
64  */
65 std::string findHwmon(const std::string& ofNode);
66 
67 /** @brief Read an hwmon sysfs value.
68  *
69  *  Calls exit(3) with bad status on failure.
70  *
71  *  @param[in] root - The hwmon class root.
72  *  @param[in] instance - The hwmon instance (ex. hwmon1).
73  *  @param[in] type - The hwmon type (ex. temp).
74  *  @param[in] id - The hwmon id (ex. 1).
75  *  @param[in] sensor - The hwmon sensor (ex. input).
76  *  @param[in] throwDeviceBusy - will throw a DeviceBusyException
77  *             on an EAGAIN errno instead of an error log exception.
78  *
79  *  @returns - The read value.
80  */
81 int readSysfsWithCallout(const std::string& root,
82                          const std::string& instance,
83                          const std::string& type,
84                          const std::string& id,
85                          const std::string& sensor,
86                          bool throwDeviceBusy = true);
87 
88  /** @brief Write a hwmon sysfs value
89   *
90   *  Calls exit(3) with bad status on failure
91   *
92   *  @param[in] value - The value to be written
93   *  @param[in] root - The hwmon class root.
94   *  @param[in] instance - The hwmon instance (ex. hwmon1).
95   *  @param[in] type - The hwmon type (ex. fan).
96   *  @param[in] id - The hwmon id (ex. 1).
97   *  @param[in] sensor - The hwmon sensor (ex. target).
98   *
99   *  @returns - The value written
100   */
101 uint64_t writeSysfsWithCallout(const uint64_t& value,
102                                const std::string& root,
103                                const std::string& instance,
104                                const std::string& type,
105                                const std::string& id,
106                                const std::string& sensor);
107 
108 }
109 
110 // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
111