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