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 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 */ 48 std::string findHwmon(const std::string& ofNode); 49 50 /** @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). 59 * @param[in] throwDeviceBusy - will throw a DeviceBusyException 60 * on an EAGAIN errno instead of an error log exception. 61 * 62 * @returns - The read value. 63 */ 64 int readSysfsWithCallout(const std::string& root, 65 const std::string& instance, 66 const std::string& type, 67 const std::string& id, 68 const std::string& sensor, 69 bool throwDeviceBusy = true); 70 71 /** @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 */ 84 uint64_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 91 } 92 93 // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 94