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 17*9331ab78SPatrick Venture if (entry.empty()) { 18*9331ab78SPatrick Venture return path + "/"s + type + id; 19*9331ab78SPatrick Venture } 20*9331ab78SPatrick 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 42613a5b37SBrad Bishop /** @brief Find hwmon instances 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 */ 52613a5b37SBrad Bishop std::string findHwmon(const std::string& ofNode); 53613a5b37SBrad Bishop 54431d26a5SBrad Bishop /** @brief Return the path to use for a call out. 55431d26a5SBrad Bishop * 56431d26a5SBrad Bishop * Return an empty string if a callout path cannot be 57431d26a5SBrad Bishop * found. 58431d26a5SBrad Bishop * 59431d26a5SBrad Bishop * @param[in] instancePath - /sys/class/hwmon/hwmon<N> path. 60431d26a5SBrad Bishop * 61431d26a5SBrad Bishop * @return Path to use for call out 62431d26a5SBrad Bishop */ 63431d26a5SBrad Bishop std::string findCalloutPath(const std::string& instancePath); 64431d26a5SBrad Bishop 658b574a7eSBrad Bishop namespace hwmonio 668b574a7eSBrad Bishop { 67754d38cfSBrad Bishop static constexpr auto retries = 10; 68754d38cfSBrad Bishop static constexpr auto delay = std::chrono::milliseconds{100}; 698b574a7eSBrad Bishop 708b574a7eSBrad Bishop /** @class HwmonIO 718b574a7eSBrad Bishop * @brief Convenience wrappers for HWMON sysfs attribute IO. 728b574a7eSBrad Bishop * 738b574a7eSBrad Bishop * Unburden the rest of the application from having to check 748b574a7eSBrad Bishop * ENOENT after every hwmon attribute io operation. Hwmon 758b574a7eSBrad Bishop * device drivers can be unbound at any time; the program 768b574a7eSBrad Bishop * cannot always be terminated externally before we try to 778b574a7eSBrad Bishop * do an io. 788b574a7eSBrad Bishop */ 798b574a7eSBrad Bishop class HwmonIO 808b574a7eSBrad Bishop { 818b574a7eSBrad Bishop public: 828b574a7eSBrad Bishop HwmonIO() = delete; 838b574a7eSBrad Bishop HwmonIO(const HwmonIO&) = default; 848b574a7eSBrad Bishop HwmonIO(HwmonIO&&) = default; 858b574a7eSBrad Bishop HwmonIO& operator=(const HwmonIO&) = default; 868b574a7eSBrad Bishop HwmonIO& operator=(HwmonIO&&) = default; 878b574a7eSBrad Bishop ~HwmonIO() = default; 888b574a7eSBrad Bishop 898b574a7eSBrad Bishop /** @brief Constructor 908b574a7eSBrad Bishop * 918b574a7eSBrad Bishop * @param[in] path - hwmon instance root - eg: 928b574a7eSBrad Bishop * /sys/class/hwmon/hwmon<N> 938b574a7eSBrad Bishop */ 948b574a7eSBrad Bishop explicit HwmonIO(const std::string& path); 958b574a7eSBrad Bishop 968b574a7eSBrad Bishop /** @brief Perform formatted hwmon sysfs read. 978b574a7eSBrad Bishop * 98ca64c25fSGunnar Mills * Propagates any exceptions other than ENOENT. 998b574a7eSBrad Bishop * ENOENT will result in a call to exit(0) in case 1008b574a7eSBrad Bishop * the underlying hwmon driver is unbound and 1018b574a7eSBrad Bishop * the program is inadvertently left running. 1028b574a7eSBrad Bishop * 103754d38cfSBrad Bishop * For possibly transient errors will retry up to 104754d38cfSBrad Bishop * the specified number of times. 105754d38cfSBrad Bishop * 1068b574a7eSBrad Bishop * @param[in] type - The hwmon type (ex. temp). 1078b574a7eSBrad Bishop * @param[in] id - The hwmon id (ex. 1). 1088b574a7eSBrad Bishop * @param[in] sensor - The hwmon sensor (ex. input). 109754d38cfSBrad Bishop * @param[in] retries - The number of times to retry. 110754d38cfSBrad Bishop * @param[in] delay - The time to sleep between retry attempts. 1118b574a7eSBrad Bishop * 1128b574a7eSBrad Bishop * @return val - The read value. 1138b574a7eSBrad Bishop */ 114fee106b9SMatt Spinler int64_t read( 1158b574a7eSBrad Bishop const std::string& type, 1168b574a7eSBrad Bishop const std::string& id, 117754d38cfSBrad Bishop const std::string& sensor, 118754d38cfSBrad Bishop size_t retries, 119754d38cfSBrad Bishop std::chrono::milliseconds delay) const; 1208b574a7eSBrad Bishop 1218b574a7eSBrad Bishop /** @brief Perform formatted hwmon sysfs write. 1228b574a7eSBrad Bishop * 123ca64c25fSGunnar Mills * Propagates any exceptions other than ENOENT. 1248b574a7eSBrad Bishop * ENOENT will result in a call to exit(0) in case 1258b574a7eSBrad Bishop * the underlying hwmon driver is unbound and 1268b574a7eSBrad Bishop * the program is inadvertently left running. 1278b574a7eSBrad Bishop * 128754d38cfSBrad Bishop * For possibly transient errors will retry up to 129754d38cfSBrad Bishop * the specified number of times. 130754d38cfSBrad Bishop * 1318b574a7eSBrad Bishop * @param[in] val - The value to be written. 1328b574a7eSBrad Bishop * @param[in] type - The hwmon type (ex. fan). 1338b574a7eSBrad Bishop * @param[in] id - The hwmon id (ex. 1). 134754d38cfSBrad Bishop * @param[in] retries - The number of times to retry. 135754d38cfSBrad Bishop * @param[in] delay - The time to sleep between retry attempts. 1368b574a7eSBrad Bishop */ 1378b574a7eSBrad Bishop void write( 1388b574a7eSBrad Bishop uint32_t val, 1398b574a7eSBrad Bishop const std::string& type, 1408b574a7eSBrad Bishop const std::string& id, 141754d38cfSBrad Bishop const std::string& sensor, 142754d38cfSBrad Bishop size_t retries, 143754d38cfSBrad Bishop std::chrono::milliseconds delay) const; 144754d38cfSBrad Bishop 1458b574a7eSBrad Bishop 1468b574a7eSBrad Bishop /** @brief Hwmon instance path access. 1478b574a7eSBrad Bishop * 1488b574a7eSBrad Bishop * @return path - The hwmon instance path. 1498b574a7eSBrad Bishop */ 1508b574a7eSBrad Bishop std::string path() const; 1518b574a7eSBrad Bishop 1528b574a7eSBrad Bishop private: 1538b574a7eSBrad Bishop std::string p; 1548b574a7eSBrad Bishop }; 1558b574a7eSBrad Bishop } // namespace hwmonio 1561e6324faSPatrick Venture } 1571e6324faSPatrick Venture 15803476f11SBrad Bishop // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 159