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 | f4bf63a | 2017-08-28 15:39:19 -0400 | [diff] [blame] | 37 | /** @brief Return the path to the phandle file matching value in io-channels. |
| 38 | * |
| 39 | * This function will take two passed in paths. |
| 40 | * One path is used to find the io-channels file. |
| 41 | * The other path is used to find the phandle file. |
| 42 | * The 4 byte phandle value is read from the phandle file(s). |
| 43 | * The 4 byte phandle value and 4 byte index value is read from io-channels. |
| 44 | * When a match is found, the path to the matching phandle file is returned. |
| 45 | * |
| 46 | * @param[in] iochanneldir - Path to file for getting phandle from io-channels |
| 47 | * @param[in] phandledir - Path to use for reading from phandle file |
| 48 | * |
| 49 | * @return Path to phandle file with value matching that in io-channels |
| 50 | */ |
| 51 | std::string findPhandleMatch( |
| 52 | const std::string& iochanneldir, |
| 53 | const std::string& phandledir); |
Brad Bishop | 613a5b3 | 2017-01-05 20:58:13 -0500 | [diff] [blame] | 54 | |
| 55 | /** @brief Find hwmon instances |
| 56 | * |
| 57 | * Look for a matching hwmon instance given an |
| 58 | * open firmware device path. |
| 59 | * |
| 60 | * @param[in] ofNode- The open firmware device path. |
| 61 | * |
| 62 | * @returns[in] - The hwmon instance path or an empty |
| 63 | * string if no match is found. |
| 64 | */ |
| 65 | std::string findHwmon(const std::string& ofNode); |
| 66 | |
Brad Bishop | 431d26a | 2017-08-25 09:47:58 -0400 | [diff] [blame^] | 67 | /** @brief Return the path to use for a call out. |
| 68 | * |
| 69 | * Return an empty string if a callout path cannot be |
| 70 | * found. |
| 71 | * |
| 72 | * @param[in] instancePath - /sys/class/hwmon/hwmon<N> path. |
| 73 | * |
| 74 | * @return Path to use for call out |
| 75 | */ |
| 76 | std::string findCalloutPath(const std::string& instancePath); |
| 77 | |
Brad Bishop | 4db6442 | 2017-02-16 11:33:32 -0500 | [diff] [blame] | 78 | /** @brief Read an hwmon sysfs value. |
| 79 | * |
| 80 | * Calls exit(3) with bad status on failure. |
| 81 | * |
| 82 | * @param[in] root - The hwmon class root. |
| 83 | * @param[in] instance - The hwmon instance (ex. hwmon1). |
| 84 | * @param[in] type - The hwmon type (ex. temp). |
| 85 | * @param[in] id - The hwmon id (ex. 1). |
| 86 | * @param[in] sensor - The hwmon sensor (ex. input). |
Matt Spinler | 3b8e36e | 2017-07-28 10:44:45 -0500 | [diff] [blame] | 87 | * @param[in] throwDeviceBusy - will throw a DeviceBusyException |
| 88 | * on an EAGAIN errno instead of an error log exception. |
Brad Bishop | 4db6442 | 2017-02-16 11:33:32 -0500 | [diff] [blame] | 89 | * |
| 90 | * @returns - The read value. |
| 91 | */ |
| 92 | int readSysfsWithCallout(const std::string& root, |
| 93 | const std::string& instance, |
| 94 | const std::string& type, |
| 95 | const std::string& id, |
Matt Spinler | 3b8e36e | 2017-07-28 10:44:45 -0500 | [diff] [blame] | 96 | const std::string& sensor, |
| 97 | bool throwDeviceBusy = true); |
Brad Bishop | 4db6442 | 2017-02-16 11:33:32 -0500 | [diff] [blame] | 98 | |
Matthew Barth | 048ac87 | 2017-03-09 14:36:08 -0600 | [diff] [blame] | 99 | /** @brief Write a hwmon sysfs value |
| 100 | * |
| 101 | * Calls exit(3) with bad status on failure |
| 102 | * |
| 103 | * @param[in] value - The value to be written |
| 104 | * @param[in] root - The hwmon class root. |
| 105 | * @param[in] instance - The hwmon instance (ex. hwmon1). |
| 106 | * @param[in] type - The hwmon type (ex. fan). |
| 107 | * @param[in] id - The hwmon id (ex. 1). |
| 108 | * @param[in] sensor - The hwmon sensor (ex. target). |
| 109 | * |
| 110 | * @returns - The value written |
| 111 | */ |
| 112 | uint64_t writeSysfsWithCallout(const uint64_t& value, |
| 113 | const std::string& root, |
| 114 | const std::string& instance, |
| 115 | const std::string& type, |
| 116 | const std::string& id, |
| 117 | const std::string& sensor); |
| 118 | |
Patrick Venture | 1e6324f | 2017-06-01 14:07:05 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Brad Bishop | 03476f1 | 2016-12-19 13:09:12 -0500 | [diff] [blame] | 121 | // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |