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 | |
Vishwanatha Subbanna | 32e84e9 | 2017-06-28 19:17:28 +0530 | [diff] [blame] | 18 | fs::path Device::bindPath = fs::path(OCC_HWMON_PATH) / "bind"; |
| 19 | fs::path Device::unBindPath = fs::path(OCC_HWMON_PATH) / "unbind"; |
| 20 | |
Eddie James | 774f9af | 2019-03-19 20:58:53 +0000 | [diff] [blame] | 21 | std::string Device::getPathBack(const fs::path& path) |
| 22 | { |
| 23 | if (path.empty()) |
George Liu | bcef3b4 | 2021-09-10 12:39:02 +0800 | [diff] [blame] | 24 | { |
Eddie James | 774f9af | 2019-03-19 20:58:53 +0000 | [diff] [blame] | 25 | return std::string(); |
George Liu | bcef3b4 | 2021-09-10 12:39:02 +0800 | [diff] [blame] | 26 | } |
Eddie James | 774f9af | 2019-03-19 20:58:53 +0000 | [diff] [blame] | 27 | |
| 28 | // Points to the last element in the path |
| 29 | auto conf = --path.end(); |
| 30 | |
George Liu | bcef3b4 | 2021-09-10 12:39:02 +0800 | [diff] [blame] | 31 | if (conf->empty() && conf != path.begin()) |
Eddie James | 774f9af | 2019-03-19 20:58:53 +0000 | [diff] [blame] | 32 | { |
| 33 | return *(--conf); |
| 34 | } |
| 35 | else |
| 36 | { |
| 37 | return *conf; |
| 38 | } |
| 39 | } |
| 40 | |
Edward A. James | 636577f | 2017-10-06 10:53:55 -0500 | [diff] [blame] | 41 | bool Device::master() const |
| 42 | { |
| 43 | int master; |
Eddie James | 774f9af | 2019-03-19 20:58:53 +0000 | [diff] [blame] | 44 | auto masterFile = devPath / "occ_master"; |
Edward A. James | 636577f | 2017-10-06 10:53:55 -0500 | [diff] [blame] | 45 | std::ifstream file(masterFile, std::ios::in); |
| 46 | |
| 47 | if (!file) |
| 48 | { |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | file >> master; |
| 53 | file.close(); |
| 54 | return (master != 0); |
| 55 | } |
| 56 | |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 57 | void Device::errorCallback(bool error) |
| 58 | { |
| 59 | if (error) |
| 60 | { |
| 61 | statusObject.deviceError(); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | #ifdef PLDM |
| 66 | void Device::timeoutCallback(bool error) |
| 67 | { |
| 68 | if (error) |
| 69 | { |
| 70 | managerObject.sbeTimeout(instance); |
| 71 | } |
| 72 | } |
| 73 | #endif |
| 74 | |
Eddie James | 482e31f | 2017-09-14 13:17:17 -0500 | [diff] [blame] | 75 | void Device::throttleProcTempCallback(bool error) |
| 76 | { |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 77 | statusObject.throttleProcTemp(error); |
Eddie James | 482e31f | 2017-09-14 13:17:17 -0500 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | void Device::throttleProcPowerCallback(bool error) |
| 81 | { |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 82 | statusObject.throttleProcPower(error); |
Eddie James | 482e31f | 2017-09-14 13:17:17 -0500 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void Device::throttleMemTempCallback(bool error) |
| 86 | { |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 87 | statusObject.throttleMemTemp(error); |
Eddie James | 482e31f | 2017-09-14 13:17:17 -0500 | [diff] [blame] | 88 | } |
| 89 | |
Chris Cain | e2d0a43 | 2022-03-28 11:08:49 -0500 | [diff] [blame^] | 90 | fs::path Device::getFilenameByRegex(fs::path basePath, |
| 91 | const std::regex& expr) const |
| 92 | { |
| 93 | try |
| 94 | { |
| 95 | for (auto& file : fs::directory_iterator(basePath)) |
| 96 | { |
| 97 | if (std::regex_search(file.path().string(), expr)) |
| 98 | { |
| 99 | // Found match |
| 100 | return file; |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | catch (const fs::filesystem_error& e) |
| 105 | { |
| 106 | log<level::ERR>( |
| 107 | fmt::format("getFilenameByRegex: Failed to get filename: {}", |
| 108 | e.what()) |
| 109 | .c_str()); |
| 110 | } |
| 111 | |
| 112 | // Return empty path |
| 113 | return fs::path{}; |
| 114 | } |
| 115 | |
Vishwanatha Subbanna | 32e84e9 | 2017-06-28 19:17:28 +0530 | [diff] [blame] | 116 | } // namespace occ |
| 117 | } // namespace open_power |