xref: /openbmc/phosphor-hwmon/sysfs.hpp (revision 754d38cf)
126b815faSBrad Bishop #pragma once
26292aeedSMatthew Barth 
3*754d38cfSBrad Bishop #include <chrono>
43b8e36e2SMatt Spinler #include <exception>
56292aeedSMatthew Barth #include <fstream>
66292aeedSMatthew Barth #include <string>
76292aeedSMatthew Barth 
81e6324faSPatrick Venture namespace sysfs {
91e6324faSPatrick Venture 
10a9b5f05bSBrad Bishop inline std::string make_sysfs_path(const std::string& path,
116292aeedSMatthew Barth                                    const std::string& type,
126292aeedSMatthew Barth                                    const std::string& id,
136292aeedSMatthew Barth                                    const std::string& entry)
146292aeedSMatthew Barth {
156292aeedSMatthew Barth     using namespace std::literals;
166292aeedSMatthew Barth 
176292aeedSMatthew Barth     return path + "/"s + type + id + "_"s + entry;
186292aeedSMatthew Barth }
196292aeedSMatthew Barth 
20f4bf63adSBrad Bishop /** @brief Return the path to the phandle file matching value in io-channels.
21f4bf63adSBrad Bishop  *
22f4bf63adSBrad Bishop  *  This function will take two passed in paths.
23f4bf63adSBrad Bishop  *  One path is used to find the io-channels file.
24f4bf63adSBrad Bishop  *  The other path is used to find the phandle file.
25f4bf63adSBrad Bishop  *  The 4 byte phandle value is read from the phandle file(s).
26f4bf63adSBrad Bishop  *  The 4 byte phandle value and 4 byte index value is read from io-channels.
27f4bf63adSBrad Bishop  *  When a match is found, the path to the matching phandle file is returned.
28f4bf63adSBrad Bishop  *
29f4bf63adSBrad Bishop  *  @param[in] iochanneldir - Path to file for getting phandle from io-channels
30f4bf63adSBrad Bishop  *  @param[in] phandledir - Path to use for reading from phandle file
31f4bf63adSBrad Bishop  *
32f4bf63adSBrad Bishop  *  @return Path to phandle file with value matching that in io-channels
33f4bf63adSBrad Bishop  */
34f4bf63adSBrad Bishop std::string findPhandleMatch(
35f4bf63adSBrad Bishop         const std::string& iochanneldir,
36f4bf63adSBrad Bishop         const std::string& phandledir);
37613a5b37SBrad Bishop 
38613a5b37SBrad Bishop /** @brief Find hwmon instances
39613a5b37SBrad Bishop  *
40613a5b37SBrad Bishop  *  Look for a matching hwmon instance given an
41613a5b37SBrad Bishop  *  open firmware device path.
42613a5b37SBrad Bishop  *
43613a5b37SBrad Bishop  *  @param[in] ofNode- The open firmware device path.
44613a5b37SBrad Bishop  *
45613a5b37SBrad Bishop  *  @returns[in] - The hwmon instance path or an empty
46613a5b37SBrad Bishop  *                 string if no match is found.
47613a5b37SBrad Bishop  */
48613a5b37SBrad Bishop std::string findHwmon(const std::string& ofNode);
49613a5b37SBrad Bishop 
50431d26a5SBrad Bishop /** @brief Return the path to use for a call out.
51431d26a5SBrad Bishop  *
52431d26a5SBrad Bishop  *  Return an empty string if a callout path cannot be
53431d26a5SBrad Bishop  *  found.
54431d26a5SBrad Bishop  *
55431d26a5SBrad Bishop  *  @param[in] instancePath - /sys/class/hwmon/hwmon<N> path.
56431d26a5SBrad Bishop  *
57431d26a5SBrad Bishop  *  @return Path to use for call out
58431d26a5SBrad Bishop  */
59431d26a5SBrad Bishop std::string findCalloutPath(const std::string& instancePath);
60431d26a5SBrad Bishop 
618b574a7eSBrad Bishop namespace hwmonio
628b574a7eSBrad Bishop {
63*754d38cfSBrad Bishop static constexpr auto retries = 10;
64*754d38cfSBrad Bishop static constexpr auto delay = std::chrono::milliseconds{100};
658b574a7eSBrad Bishop 
668b574a7eSBrad Bishop /** @class HwmonIO
678b574a7eSBrad Bishop  *  @brief Convenience wrappers for HWMON sysfs attribute IO.
688b574a7eSBrad Bishop  *
698b574a7eSBrad Bishop  *  Unburden the rest of the application from having to check
708b574a7eSBrad Bishop  *  ENOENT after every hwmon attribute io operation.  Hwmon
718b574a7eSBrad Bishop  *  device drivers can be unbound at any time; the program
728b574a7eSBrad Bishop  *  cannot always be terminated externally before we try to
738b574a7eSBrad Bishop  *  do an io.
748b574a7eSBrad Bishop  */
758b574a7eSBrad Bishop class HwmonIO
768b574a7eSBrad Bishop {
778b574a7eSBrad Bishop     public:
788b574a7eSBrad Bishop         HwmonIO() = delete;
798b574a7eSBrad Bishop         HwmonIO(const HwmonIO&) = default;
808b574a7eSBrad Bishop         HwmonIO(HwmonIO&&) = default;
818b574a7eSBrad Bishop         HwmonIO& operator=(const HwmonIO&) = default;
828b574a7eSBrad Bishop         HwmonIO& operator=(HwmonIO&&) = default;
838b574a7eSBrad Bishop         ~HwmonIO() = default;
848b574a7eSBrad Bishop 
858b574a7eSBrad Bishop         /** @brief Constructor
868b574a7eSBrad Bishop          *
878b574a7eSBrad Bishop          *  @param[in] path - hwmon instance root - eg:
888b574a7eSBrad Bishop          *      /sys/class/hwmon/hwmon<N>
898b574a7eSBrad Bishop          */
908b574a7eSBrad Bishop         explicit HwmonIO(const std::string& path);
918b574a7eSBrad Bishop 
928b574a7eSBrad Bishop         /** @brief Perform formatted hwmon sysfs read.
938b574a7eSBrad Bishop          *
948b574a7eSBrad Bishop          *  Propogates any exceptions other than ENOENT.
958b574a7eSBrad Bishop          *  ENOENT will result in a call to exit(0) in case
968b574a7eSBrad Bishop          *  the underlying hwmon driver is unbound and
978b574a7eSBrad Bishop          *  the program is inadvertently left running.
988b574a7eSBrad Bishop          *
99*754d38cfSBrad Bishop          *  For possibly transient errors will retry up to
100*754d38cfSBrad Bishop          *  the specified number of times.
101*754d38cfSBrad Bishop          *
1028b574a7eSBrad Bishop          *  @param[in] type - The hwmon type (ex. temp).
1038b574a7eSBrad Bishop          *  @param[in] id - The hwmon id (ex. 1).
1048b574a7eSBrad Bishop          *  @param[in] sensor - The hwmon sensor (ex. input).
105*754d38cfSBrad Bishop          *  @param[in] retries - The number of times to retry.
106*754d38cfSBrad Bishop          *  @param[in] delay - The time to sleep between retry attempts.
1078b574a7eSBrad Bishop          *
1088b574a7eSBrad Bishop          *  @return val - The read value.
1098b574a7eSBrad Bishop          */
1108b574a7eSBrad Bishop         uint32_t read(
1118b574a7eSBrad Bishop                 const std::string& type,
1128b574a7eSBrad Bishop                 const std::string& id,
113*754d38cfSBrad Bishop                 const std::string& sensor,
114*754d38cfSBrad Bishop                 size_t retries,
115*754d38cfSBrad Bishop                 std::chrono::milliseconds delay) const;
1168b574a7eSBrad Bishop 
1178b574a7eSBrad Bishop         /** @brief Perform formatted hwmon sysfs write.
1188b574a7eSBrad Bishop          *
1198b574a7eSBrad Bishop          *  Propogates any exceptions other than ENOENT.
1208b574a7eSBrad Bishop          *  ENOENT will result in a call to exit(0) in case
1218b574a7eSBrad Bishop          *  the underlying hwmon driver is unbound and
1228b574a7eSBrad Bishop          *  the program is inadvertently left running.
1238b574a7eSBrad Bishop          *
124*754d38cfSBrad Bishop          *  For possibly transient errors will retry up to
125*754d38cfSBrad Bishop          *  the specified number of times.
126*754d38cfSBrad Bishop          *
1278b574a7eSBrad Bishop          *  @param[in] val - The value to be written.
1288b574a7eSBrad Bishop          *  @param[in] type - The hwmon type (ex. fan).
1298b574a7eSBrad Bishop          *  @param[in] id - The hwmon id (ex. 1).
130*754d38cfSBrad Bishop          *  @param[in] retries - The number of times to retry.
131*754d38cfSBrad Bishop          *  @param[in] delay - The time to sleep between retry attempts.
1328b574a7eSBrad Bishop          */
1338b574a7eSBrad Bishop         void write(
1348b574a7eSBrad Bishop                 uint32_t val,
1358b574a7eSBrad Bishop                 const std::string& type,
1368b574a7eSBrad Bishop                 const std::string& id,
137*754d38cfSBrad Bishop                 const std::string& sensor,
138*754d38cfSBrad Bishop                 size_t retries,
139*754d38cfSBrad Bishop                 std::chrono::milliseconds delay) const;
140*754d38cfSBrad Bishop 
1418b574a7eSBrad Bishop 
1428b574a7eSBrad Bishop         /** @brief Hwmon instance path access.
1438b574a7eSBrad Bishop          *
1448b574a7eSBrad Bishop          *  @return path - The hwmon instance path.
1458b574a7eSBrad Bishop          */
1468b574a7eSBrad Bishop         std::string path() const;
1478b574a7eSBrad Bishop 
1488b574a7eSBrad Bishop     private:
1498b574a7eSBrad Bishop         std::string p;
1508b574a7eSBrad Bishop };
1518b574a7eSBrad Bishop } // namespace hwmonio
1521e6324faSPatrick Venture }
1531e6324faSPatrick Venture 
15403476f11SBrad Bishop // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
155