Vishwanatha Subbanna | 32e84e9 | 2017-06-28 19:17:28 +0530 | [diff] [blame] | 1 | #include "occ_device.hpp" |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 2 | |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 3 | #include "occ_manager.hpp" |
Eddie James | 482e31f | 2017-09-14 13:17:17 -0500 | [diff] [blame] | 4 | #include "occ_status.hpp" |
Vishwanatha Subbanna | 32e84e9 | 2017-06-28 19:17:28 +0530 | [diff] [blame] | 5 | |
Chris Cain | 37abe9b | 2024-10-31 17:20:31 -0500 | [diff] [blame] | 6 | #include <phosphor-logging/lg2.hpp> |
Chris Cain | e2d0a43 | 2022-03-28 11:08:49 -0500 | [diff] [blame] | 7 | |
| 8 | #include <filesystem> |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 9 | #include <iostream> |
| 10 | |
Vishwanatha Subbanna | 32e84e9 | 2017-06-28 19:17:28 +0530 | [diff] [blame] | 11 | namespace open_power |
| 12 | { |
| 13 | namespace occ |
| 14 | { |
| 15 | |
Eddie James | aced309 | 2022-04-22 16:19:30 -0500 | [diff] [blame] | 16 | void Device::setActive(bool active) |
| 17 | { |
| 18 | std::string data = active ? "1" : "0"; |
| 19 | auto activeFile = devPath / "occ_active"; |
| 20 | try |
| 21 | { |
| 22 | write(activeFile, data); |
| 23 | } |
| 24 | catch (const std::exception& e) |
| 25 | { |
Chris Cain | 37abe9b | 2024-10-31 17:20:31 -0500 | [diff] [blame] | 26 | lg2::error("Failed to set {DEVICE} active: {ERROR}", "DEVICE", devPath, |
| 27 | "ERROR", e.what()); |
Eddie James | aced309 | 2022-04-22 16:19:30 -0500 | [diff] [blame] | 28 | } |
| 29 | } |
Vishwanatha Subbanna | 32e84e9 | 2017-06-28 19:17:28 +0530 | [diff] [blame] | 30 | |
Eddie James | 774f9af | 2019-03-19 20:58:53 +0000 | [diff] [blame] | 31 | std::string Device::getPathBack(const fs::path& path) |
| 32 | { |
| 33 | if (path.empty()) |
George Liu | bcef3b4 | 2021-09-10 12:39:02 +0800 | [diff] [blame] | 34 | { |
Eddie James | 774f9af | 2019-03-19 20:58:53 +0000 | [diff] [blame] | 35 | return std::string(); |
George Liu | bcef3b4 | 2021-09-10 12:39:02 +0800 | [diff] [blame] | 36 | } |
Eddie James | 774f9af | 2019-03-19 20:58:53 +0000 | [diff] [blame] | 37 | |
| 38 | // Points to the last element in the path |
| 39 | auto conf = --path.end(); |
| 40 | |
George Liu | bcef3b4 | 2021-09-10 12:39:02 +0800 | [diff] [blame] | 41 | if (conf->empty() && conf != path.begin()) |
Eddie James | 774f9af | 2019-03-19 20:58:53 +0000 | [diff] [blame] | 42 | { |
| 43 | return *(--conf); |
| 44 | } |
| 45 | else |
| 46 | { |
| 47 | return *conf; |
| 48 | } |
| 49 | } |
| 50 | |
Eddie James | aced309 | 2022-04-22 16:19:30 -0500 | [diff] [blame] | 51 | bool Device::active() const |
| 52 | { |
| 53 | return readBinary("occ_active"); |
| 54 | } |
| 55 | |
Edward A. James | 636577f | 2017-10-06 10:53:55 -0500 | [diff] [blame] | 56 | bool Device::master() const |
| 57 | { |
Eddie James | aced309 | 2022-04-22 16:19:30 -0500 | [diff] [blame] | 58 | return readBinary("occ_master"); |
| 59 | } |
| 60 | |
| 61 | bool Device::readBinary(const std::string& fileName) const |
| 62 | { |
Chris Cain | bd551de | 2022-04-26 13:41:16 -0500 | [diff] [blame] | 63 | int v = 0; |
| 64 | if (statusObject.occActive()) |
Edward A. James | 636577f | 2017-10-06 10:53:55 -0500 | [diff] [blame] | 65 | { |
Chris Cain | bd551de | 2022-04-26 13:41:16 -0500 | [diff] [blame] | 66 | auto filePath = devPath / fileName; |
| 67 | std::ifstream file(filePath, std::ios::in); |
| 68 | if (!file) |
| 69 | { |
| 70 | return false; |
| 71 | } |
Edward A. James | 636577f | 2017-10-06 10:53:55 -0500 | [diff] [blame] | 72 | |
Chris Cain | bd551de | 2022-04-26 13:41:16 -0500 | [diff] [blame] | 73 | file >> v; |
| 74 | file.close(); |
| 75 | } |
Eddie James | aced309 | 2022-04-22 16:19:30 -0500 | [diff] [blame] | 76 | return v == 1; |
Edward A. James | 636577f | 2017-10-06 10:53:55 -0500 | [diff] [blame] | 77 | } |
| 78 | |
Eddie James | 9789e71 | 2022-05-25 15:43:40 -0500 | [diff] [blame] | 79 | void Device::errorCallback(int error) |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 80 | { |
| 81 | if (error) |
| 82 | { |
Eddie James | 9789e71 | 2022-05-25 15:43:40 -0500 | [diff] [blame] | 83 | if (error != -EHOSTDOWN) |
| 84 | { |
| 85 | fs::path p = devPath; |
| 86 | if (fs::is_symlink(p)) |
| 87 | { |
| 88 | p = fs::read_symlink(p); |
| 89 | } |
Matt Spinler | fec4b0b | 2024-01-04 15:10:37 -0600 | [diff] [blame] | 90 | statusObject.deviceError( |
| 91 | Error::Descriptor("org.open_power.OCC.Device.Error.ReadFailure", |
| 92 | error, p.c_str())); |
Eddie James | 9789e71 | 2022-05-25 15:43:40 -0500 | [diff] [blame] | 93 | } |
| 94 | else |
| 95 | { |
| 96 | statusObject.deviceError(Error::Descriptor(SAFE_ERROR_PATH)); |
| 97 | } |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
Eddie James | 9789e71 | 2022-05-25 15:43:40 -0500 | [diff] [blame] | 101 | void Device::presenceCallback(int) |
| 102 | { |
| 103 | statusObject.deviceError(Error::Descriptor(PRESENCE_ERROR_PATH)); |
| 104 | } |
| 105 | |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 106 | #ifdef PLDM |
Eddie James | 9789e71 | 2022-05-25 15:43:40 -0500 | [diff] [blame] | 107 | void Device::timeoutCallback(int error) |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 108 | { |
| 109 | if (error) |
| 110 | { |
| 111 | managerObject.sbeTimeout(instance); |
| 112 | } |
| 113 | } |
| 114 | #endif |
| 115 | |
Eddie James | 9789e71 | 2022-05-25 15:43:40 -0500 | [diff] [blame] | 116 | void Device::throttleProcTempCallback(int error) |
Eddie James | 482e31f | 2017-09-14 13:17:17 -0500 | [diff] [blame] | 117 | { |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 118 | statusObject.throttleProcTemp(error); |
Chris Cain | c86d80f | 2023-05-04 15:49:18 -0500 | [diff] [blame] | 119 | // Update the processor throttle on dbus |
| 120 | statusObject.updateThrottle(error, THROTTLED_THERMAL); |
Eddie James | 482e31f | 2017-09-14 13:17:17 -0500 | [diff] [blame] | 121 | } |
| 122 | |
Eddie James | 9789e71 | 2022-05-25 15:43:40 -0500 | [diff] [blame] | 123 | void Device::throttleProcPowerCallback(int error) |
Eddie James | 482e31f | 2017-09-14 13:17:17 -0500 | [diff] [blame] | 124 | { |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 125 | statusObject.throttleProcPower(error); |
Chris Cain | c86d80f | 2023-05-04 15:49:18 -0500 | [diff] [blame] | 126 | // Update the processor throttle on dbus |
| 127 | statusObject.updateThrottle(error, THROTTLED_POWER); |
Eddie James | 482e31f | 2017-09-14 13:17:17 -0500 | [diff] [blame] | 128 | } |
| 129 | |
Eddie James | 9789e71 | 2022-05-25 15:43:40 -0500 | [diff] [blame] | 130 | void Device::throttleMemTempCallback(int error) |
Eddie James | 482e31f | 2017-09-14 13:17:17 -0500 | [diff] [blame] | 131 | { |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 132 | statusObject.throttleMemTemp(error); |
Eddie James | 482e31f | 2017-09-14 13:17:17 -0500 | [diff] [blame] | 133 | } |
| 134 | |
Chris Cain | e2d0a43 | 2022-03-28 11:08:49 -0500 | [diff] [blame] | 135 | fs::path Device::getFilenameByRegex(fs::path basePath, |
| 136 | const std::regex& expr) const |
| 137 | { |
| 138 | try |
| 139 | { |
| 140 | for (auto& file : fs::directory_iterator(basePath)) |
| 141 | { |
| 142 | if (std::regex_search(file.path().string(), expr)) |
| 143 | { |
| 144 | // Found match |
| 145 | return file; |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | catch (const fs::filesystem_error& e) |
| 150 | { |
Chris Cain | 37abe9b | 2024-10-31 17:20:31 -0500 | [diff] [blame] | 151 | lg2::error("getFilenameByRegex: Failed to get filename: {ERROR}", |
| 152 | "ERROR", e.what()); |
Chris Cain | e2d0a43 | 2022-03-28 11:08:49 -0500 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | // Return empty path |
| 156 | return fs::path{}; |
| 157 | } |
| 158 | |
Vishwanatha Subbanna | 32e84e9 | 2017-06-28 19:17:28 +0530 | [diff] [blame] | 159 | } // namespace occ |
| 160 | } // namespace open_power |