1 #include "occ_device.hpp" 2 3 #include "occ_manager.hpp" 4 #include "occ_status.hpp" 5 6 #include <phosphor-logging/log.hpp> 7 8 #include <filesystem> 9 #include <iostream> 10 11 namespace open_power 12 { 13 namespace occ 14 { 15 16 using namespace phosphor::logging; 17 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 } 33 34 std::string Device::getPathBack(const fs::path& path) 35 { 36 if (path.empty()) 37 { 38 return std::string(); 39 } 40 41 // Points to the last element in the path 42 auto conf = --path.end(); 43 44 if (conf->empty() && conf != path.begin()) 45 { 46 return *(--conf); 47 } 48 else 49 { 50 return *conf; 51 } 52 } 53 54 bool Device::active() const 55 { 56 return readBinary("occ_active"); 57 } 58 59 bool Device::master() const 60 { 61 return readBinary("occ_master"); 62 } 63 64 bool Device::readBinary(const std::string& fileName) const 65 { 66 int v = 0; 67 if (statusObject.occActive()) 68 { 69 auto filePath = devPath / fileName; 70 std::ifstream file(filePath, std::ios::in); 71 if (!file) 72 { 73 return false; 74 } 75 76 file >> v; 77 file.close(); 78 } 79 return v == 1; 80 } 81 82 void Device::errorCallback(int error) 83 { 84 if (error) 85 { 86 if (error != -EHOSTDOWN) 87 { 88 fs::path p = devPath; 89 if (fs::is_symlink(p)) 90 { 91 p = fs::read_symlink(p); 92 } 93 statusObject.deviceError(Error::Descriptor( 94 "org.open_power.OCC.Device.ReadFailure", error, p.c_str())); 95 } 96 else 97 { 98 statusObject.deviceError(Error::Descriptor(SAFE_ERROR_PATH)); 99 } 100 } 101 } 102 103 void Device::presenceCallback(int) 104 { 105 statusObject.deviceError(Error::Descriptor(PRESENCE_ERROR_PATH)); 106 } 107 108 #ifdef PLDM 109 void Device::timeoutCallback(int error) 110 { 111 if (error) 112 { 113 managerObject.sbeTimeout(instance); 114 } 115 } 116 #endif 117 118 void Device::throttleProcTempCallback(int error) 119 { 120 statusObject.throttleProcTemp(error); 121 // Update the processor throttle on dbus 122 statusObject.updateThrottle(error, THROTTLED_THERMAL); 123 } 124 125 void Device::throttleProcPowerCallback(int error) 126 { 127 statusObject.throttleProcPower(error); 128 // Update the processor throttle on dbus 129 statusObject.updateThrottle(error, THROTTLED_POWER); 130 } 131 132 void Device::throttleMemTempCallback(int error) 133 { 134 statusObject.throttleMemTemp(error); 135 } 136 137 fs::path Device::getFilenameByRegex(fs::path basePath, 138 const std::regex& expr) const 139 { 140 try 141 { 142 for (auto& file : fs::directory_iterator(basePath)) 143 { 144 if (std::regex_search(file.path().string(), expr)) 145 { 146 // Found match 147 return file; 148 } 149 } 150 } 151 catch (const fs::filesystem_error& e) 152 { 153 log<level::ERR>( 154 fmt::format("getFilenameByRegex: Failed to get filename: {}", 155 e.what()) 156 .c_str()); 157 } 158 159 // Return empty path 160 return fs::path{}; 161 } 162 163 } // namespace occ 164 } // namespace open_power 165