Brad Bishop | 26b815f | 2017-01-04 13:32:47 -0500 | [diff] [blame] | 1 | #pragma once |
Patrick Williams | 3667cf3 | 2015-10-20 22:39:11 -0500 | [diff] [blame] | 2 | |
Matt Spinler | 3b8e36e | 2017-07-28 10:44:45 -0500 | [diff] [blame^] | 3 | #include <exception> |
Patrick Williams | 3667cf3 | 2015-10-20 22:39:11 -0500 | [diff] [blame] | 4 | #include <fstream> |
| 5 | #include <string> |
| 6 | |
Patrick Venture | 1e6324f | 2017-06-01 14:07:05 -0700 | [diff] [blame] | 7 | namespace sysfs { |
| 8 | |
Matt Spinler | 3b8e36e | 2017-07-28 10:44:45 -0500 | [diff] [blame^] | 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 | |
Brad Bishop | a9b5f05 | 2017-01-17 14:50:08 -0500 | [diff] [blame] | 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) |
Patrick Williams | 3667cf3 | 2015-10-20 22:39:11 -0500 | [diff] [blame] | 31 | { |
| 32 | using namespace std::literals; |
| 33 | |
| 34 | return path + "/"s + type + id + "_"s + entry; |
| 35 | } |
| 36 | |
Brad Bishop | 613a5b3 | 2017-01-05 20:58:13 -0500 | [diff] [blame] | 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 | |
Brad Bishop | 4db6442 | 2017-02-16 11:33:32 -0500 | [diff] [blame] | 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). |
Matt Spinler | 3b8e36e | 2017-07-28 10:44:45 -0500 | [diff] [blame^] | 59 | * @param[in] throwDeviceBusy - will throw a DeviceBusyException |
| 60 | * on an EAGAIN errno instead of an error log exception. |
Brad Bishop | 4db6442 | 2017-02-16 11:33:32 -0500 | [diff] [blame] | 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, |
Matt Spinler | 3b8e36e | 2017-07-28 10:44:45 -0500 | [diff] [blame^] | 68 | const std::string& sensor, |
| 69 | bool throwDeviceBusy = true); |
Brad Bishop | 4db6442 | 2017-02-16 11:33:32 -0500 | [diff] [blame] | 70 | |
Matthew Barth | 048ac87 | 2017-03-09 14:36:08 -0600 | [diff] [blame] | 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 | |
Patrick Venture | 1e6324f | 2017-06-01 14:07:05 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Brad Bishop | 03476f1 | 2016-12-19 13:09:12 -0500 | [diff] [blame] | 93 | // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |