xref: /openbmc/phosphor-hwmon/sysfs.hpp (revision dd467396)
126b815faSBrad Bishop #pragma once
26292aeedSMatthew Barth 
3754d38cfSBrad 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 
179331ab78SPatrick Venture     if (entry.empty()) {
189331ab78SPatrick Venture         return path + "/"s + type + id;
199331ab78SPatrick Venture     }
209331ab78SPatrick Venture 
216292aeedSMatthew Barth     return path + "/"s + type + id + "_"s + entry;
226292aeedSMatthew Barth }
236292aeedSMatthew Barth 
24f4bf63adSBrad Bishop /** @brief Return the path to the phandle file matching value in io-channels.
25f4bf63adSBrad Bishop  *
26f4bf63adSBrad Bishop  *  This function will take two passed in paths.
27f4bf63adSBrad Bishop  *  One path is used to find the io-channels file.
28f4bf63adSBrad Bishop  *  The other path is used to find the phandle file.
29f4bf63adSBrad Bishop  *  The 4 byte phandle value is read from the phandle file(s).
30f4bf63adSBrad Bishop  *  The 4 byte phandle value and 4 byte index value is read from io-channels.
31f4bf63adSBrad Bishop  *  When a match is found, the path to the matching phandle file is returned.
32f4bf63adSBrad Bishop  *
33f4bf63adSBrad Bishop  *  @param[in] iochanneldir - Path to file for getting phandle from io-channels
34f4bf63adSBrad Bishop  *  @param[in] phandledir - Path to use for reading from phandle file
35f4bf63adSBrad Bishop  *
36f4bf63adSBrad Bishop  *  @return Path to phandle file with value matching that in io-channels
37f4bf63adSBrad Bishop  */
38f4bf63adSBrad Bishop std::string findPhandleMatch(
39f4bf63adSBrad Bishop         const std::string& iochanneldir,
40f4bf63adSBrad Bishop         const std::string& phandledir);
41613a5b37SBrad Bishop 
4231dbe062SMatt Spinler /** @brief Find hwmon instances from an open-firmware device tree path
43613a5b37SBrad Bishop  *
44613a5b37SBrad Bishop  *  Look for a matching hwmon instance given an
45613a5b37SBrad Bishop  *  open firmware device path.
46613a5b37SBrad Bishop  *
47613a5b37SBrad Bishop  *  @param[in] ofNode- The open firmware device path.
48613a5b37SBrad Bishop  *
49613a5b37SBrad Bishop  *  @returns[in] - The hwmon instance path or an empty
50613a5b37SBrad Bishop  *                 string if no match is found.
51613a5b37SBrad Bishop  */
5231dbe062SMatt Spinler std::string findHwmonFromOFPath(const std::string& ofNode);
53613a5b37SBrad Bishop 
54626df17aSMatt Spinler /** @brief Find hwmon instances from a device path
55626df17aSMatt Spinler  *
56626df17aSMatt Spinler  *  Look for a matching hwmon instance given a device path that
57626df17aSMatt Spinler  *  starts with /devices.  This path is the DEVPATH udev attribute
58626df17aSMatt Spinler  *  for the device except it has the '/hwmon/hwmonN' stripped off.
59626df17aSMatt Spinler  *
60626df17aSMatt Spinler  *  @param[in] devPath - The device path.
61626df17aSMatt Spinler  *
62626df17aSMatt Spinler  *  @return - The hwmon instance path or an empty
63626df17aSMatt Spinler  *            string if no match is found.
64626df17aSMatt Spinler  */
65626df17aSMatt Spinler std::string findHwmonFromDevPath(const std::string& devPath);
66626df17aSMatt Spinler 
67431d26a5SBrad Bishop /** @brief Return the path to use for a call out.
68431d26a5SBrad Bishop  *
69431d26a5SBrad Bishop  *  Return an empty string if a callout path cannot be
70431d26a5SBrad Bishop  *  found.
71431d26a5SBrad Bishop  *
72431d26a5SBrad Bishop  *  @param[in] instancePath - /sys/class/hwmon/hwmon<N> path.
73431d26a5SBrad Bishop  *
74431d26a5SBrad Bishop  *  @return Path to use for call out
75431d26a5SBrad Bishop  */
76431d26a5SBrad Bishop std::string findCalloutPath(const std::string& instancePath);
77431d26a5SBrad Bishop 
788b574a7eSBrad Bishop namespace hwmonio
798b574a7eSBrad Bishop {
80754d38cfSBrad Bishop static constexpr auto retries = 10;
81754d38cfSBrad Bishop static constexpr auto delay = std::chrono::milliseconds{100};
828b574a7eSBrad Bishop 
838b574a7eSBrad Bishop /** @class HwmonIO
848b574a7eSBrad Bishop  *  @brief Convenience wrappers for HWMON sysfs attribute IO.
858b574a7eSBrad Bishop  *
868b574a7eSBrad Bishop  *  Unburden the rest of the application from having to check
878b574a7eSBrad Bishop  *  ENOENT after every hwmon attribute io operation.  Hwmon
888b574a7eSBrad Bishop  *  device drivers can be unbound at any time; the program
898b574a7eSBrad Bishop  *  cannot always be terminated externally before we try to
908b574a7eSBrad Bishop  *  do an io.
918b574a7eSBrad Bishop  */
928b574a7eSBrad Bishop class HwmonIO
938b574a7eSBrad Bishop {
948b574a7eSBrad Bishop     public:
958b574a7eSBrad Bishop         HwmonIO() = delete;
968b574a7eSBrad Bishop         HwmonIO(const HwmonIO&) = default;
978b574a7eSBrad Bishop         HwmonIO(HwmonIO&&) = default;
988b574a7eSBrad Bishop         HwmonIO& operator=(const HwmonIO&) = default;
998b574a7eSBrad Bishop         HwmonIO& operator=(HwmonIO&&) = default;
1008b574a7eSBrad Bishop         ~HwmonIO() = default;
1018b574a7eSBrad Bishop 
1028b574a7eSBrad Bishop         /** @brief Constructor
1038b574a7eSBrad Bishop          *
1048b574a7eSBrad Bishop          *  @param[in] path - hwmon instance root - eg:
1058b574a7eSBrad Bishop          *      /sys/class/hwmon/hwmon<N>
1068b574a7eSBrad Bishop          */
1078b574a7eSBrad Bishop         explicit HwmonIO(const std::string& path);
1088b574a7eSBrad Bishop 
1098b574a7eSBrad Bishop         /** @brief Perform formatted hwmon sysfs read.
1108b574a7eSBrad Bishop          *
111ca64c25fSGunnar Mills          *  Propagates any exceptions other than ENOENT.
1128b574a7eSBrad Bishop          *  ENOENT will result in a call to exit(0) in case
1138b574a7eSBrad Bishop          *  the underlying hwmon driver is unbound and
1148b574a7eSBrad Bishop          *  the program is inadvertently left running.
1158b574a7eSBrad Bishop          *
116754d38cfSBrad Bishop          *  For possibly transient errors will retry up to
117754d38cfSBrad Bishop          *  the specified number of times.
118754d38cfSBrad Bishop          *
1198b574a7eSBrad Bishop          *  @param[in] type - The hwmon type (ex. temp).
1208b574a7eSBrad Bishop          *  @param[in] id - The hwmon id (ex. 1).
1218b574a7eSBrad Bishop          *  @param[in] sensor - The hwmon sensor (ex. input).
122754d38cfSBrad Bishop          *  @param[in] retries - The number of times to retry.
123754d38cfSBrad Bishop          *  @param[in] delay - The time to sleep between retry attempts.
1248b574a7eSBrad Bishop          *
1258b574a7eSBrad Bishop          *  @return val - The read value.
1268b574a7eSBrad Bishop          */
127fee106b9SMatt Spinler         int64_t read(
1288b574a7eSBrad Bishop                 const std::string& type,
1298b574a7eSBrad Bishop                 const std::string& id,
130754d38cfSBrad Bishop                 const std::string& sensor,
131754d38cfSBrad Bishop                 size_t retries,
132a23babd6SMatthew Barth                 std::chrono::milliseconds delay,
133a23babd6SMatthew Barth                 bool isOCC = false) const;
1348b574a7eSBrad Bishop 
1358b574a7eSBrad Bishop         /** @brief Perform formatted hwmon sysfs write.
1368b574a7eSBrad Bishop          *
137ca64c25fSGunnar Mills          *  Propagates any exceptions other than ENOENT.
1388b574a7eSBrad Bishop          *  ENOENT will result in a call to exit(0) in case
1398b574a7eSBrad Bishop          *  the underlying hwmon driver is unbound and
1408b574a7eSBrad Bishop          *  the program is inadvertently left running.
1418b574a7eSBrad Bishop          *
142754d38cfSBrad Bishop          *  For possibly transient errors will retry up to
143754d38cfSBrad Bishop          *  the specified number of times.
144754d38cfSBrad Bishop          *
1458b574a7eSBrad Bishop          *  @param[in] val - The value to be written.
1468b574a7eSBrad Bishop          *  @param[in] type - The hwmon type (ex. fan).
1478b574a7eSBrad Bishop          *  @param[in] id - The hwmon id (ex. 1).
148754d38cfSBrad Bishop          *  @param[in] retries - The number of times to retry.
149754d38cfSBrad Bishop          *  @param[in] delay - The time to sleep between retry attempts.
1508b574a7eSBrad Bishop          */
1518b574a7eSBrad Bishop         void write(
1528b574a7eSBrad Bishop                 uint32_t val,
1538b574a7eSBrad Bishop                 const std::string& type,
1548b574a7eSBrad Bishop                 const std::string& id,
155754d38cfSBrad Bishop                 const std::string& sensor,
156754d38cfSBrad Bishop                 size_t retries,
157754d38cfSBrad Bishop                 std::chrono::milliseconds delay) const;
158754d38cfSBrad Bishop 
1598b574a7eSBrad Bishop 
1608b574a7eSBrad Bishop         /** @brief Hwmon instance path access.
1618b574a7eSBrad Bishop          *
1628b574a7eSBrad Bishop          *  @return path - The hwmon instance path.
1638b574a7eSBrad Bishop          */
1648b574a7eSBrad Bishop         std::string path() const;
1658b574a7eSBrad Bishop 
1668b574a7eSBrad Bishop     private:
1678b574a7eSBrad Bishop         std::string p;
1688b574a7eSBrad Bishop };
1698b574a7eSBrad Bishop } // namespace hwmonio
170*dd467396SPatrick Venture } // namespace sysfs
1711e6324faSPatrick Venture 
17203476f11SBrad Bishop // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
173