blob: 98eeab754e8597dd05f03aa63f29a58d710dce95 [file] [log] [blame]
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +05301#include "occ_device.hpp"
Gunnar Mills94df8c92018-09-14 14:50:03 -05002
Eddie Jamescbad2192021-10-07 09:39:39 -05003#include "occ_manager.hpp"
Eddie James482e31f2017-09-14 13:17:17 -05004#include "occ_status.hpp"
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +05305
Chris Cain37abe9b2024-10-31 17:20:31 -05006#include <phosphor-logging/lg2.hpp>
Chris Caine2d0a432022-03-28 11:08:49 -05007
8#include <filesystem>
Gunnar Mills94df8c92018-09-14 14:50:03 -05009#include <iostream>
10
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +053011namespace open_power
12{
13namespace occ
14{
15
Eddie Jamesaced3092022-04-22 16:19:30 -050016void 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 Cain37abe9b2024-10-31 17:20:31 -050026 lg2::error("Failed to set {DEVICE} active: {ERROR}", "DEVICE", devPath,
27 "ERROR", e.what());
Eddie Jamesaced3092022-04-22 16:19:30 -050028 }
29}
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +053030
Eddie James774f9af2019-03-19 20:58:53 +000031std::string Device::getPathBack(const fs::path& path)
32{
33 if (path.empty())
George Liubcef3b42021-09-10 12:39:02 +080034 {
Eddie James774f9af2019-03-19 20:58:53 +000035 return std::string();
George Liubcef3b42021-09-10 12:39:02 +080036 }
Eddie James774f9af2019-03-19 20:58:53 +000037
38 // Points to the last element in the path
39 auto conf = --path.end();
40
George Liubcef3b42021-09-10 12:39:02 +080041 if (conf->empty() && conf != path.begin())
Eddie James774f9af2019-03-19 20:58:53 +000042 {
43 return *(--conf);
44 }
45 else
46 {
47 return *conf;
48 }
49}
50
Eddie Jamesaced3092022-04-22 16:19:30 -050051bool Device::active() const
52{
53 return readBinary("occ_active");
54}
55
Edward A. James636577f2017-10-06 10:53:55 -050056bool Device::master() const
57{
Eddie Jamesaced3092022-04-22 16:19:30 -050058 return readBinary("occ_master");
59}
60
61bool Device::readBinary(const std::string& fileName) const
62{
Chris Cainbd551de2022-04-26 13:41:16 -050063 int v = 0;
64 if (statusObject.occActive())
Edward A. James636577f2017-10-06 10:53:55 -050065 {
Chris Cainbd551de2022-04-26 13:41:16 -050066 auto filePath = devPath / fileName;
67 std::ifstream file(filePath, std::ios::in);
68 if (!file)
69 {
70 return false;
71 }
Edward A. James636577f2017-10-06 10:53:55 -050072
Chris Cainbd551de2022-04-26 13:41:16 -050073 file >> v;
74 file.close();
75 }
Eddie Jamesaced3092022-04-22 16:19:30 -050076 return v == 1;
Edward A. James636577f2017-10-06 10:53:55 -050077}
78
Eddie James9789e712022-05-25 15:43:40 -050079void Device::errorCallback(int error)
Eddie Jamescbad2192021-10-07 09:39:39 -050080{
81 if (error)
82 {
Eddie James9789e712022-05-25 15:43:40 -050083 if (error != -EHOSTDOWN)
84 {
85 fs::path p = devPath;
86 if (fs::is_symlink(p))
87 {
88 p = fs::read_symlink(p);
89 }
Matt Spinlerfec4b0b2024-01-04 15:10:37 -060090 statusObject.deviceError(
91 Error::Descriptor("org.open_power.OCC.Device.Error.ReadFailure",
92 error, p.c_str()));
Eddie James9789e712022-05-25 15:43:40 -050093 }
94 else
95 {
96 statusObject.deviceError(Error::Descriptor(SAFE_ERROR_PATH));
97 }
Eddie Jamescbad2192021-10-07 09:39:39 -050098 }
99}
100
Eddie James9789e712022-05-25 15:43:40 -0500101void Device::presenceCallback(int)
102{
103 statusObject.deviceError(Error::Descriptor(PRESENCE_ERROR_PATH));
104}
105
Eddie Jamescbad2192021-10-07 09:39:39 -0500106#ifdef PLDM
Eddie James9789e712022-05-25 15:43:40 -0500107void Device::timeoutCallback(int error)
Eddie Jamescbad2192021-10-07 09:39:39 -0500108{
109 if (error)
110 {
111 managerObject.sbeTimeout(instance);
112 }
113}
114#endif
115
Eddie James9789e712022-05-25 15:43:40 -0500116void Device::throttleProcTempCallback(int error)
Eddie James482e31f2017-09-14 13:17:17 -0500117{
Gunnar Mills94df8c92018-09-14 14:50:03 -0500118 statusObject.throttleProcTemp(error);
Chris Cainc86d80f2023-05-04 15:49:18 -0500119 // Update the processor throttle on dbus
120 statusObject.updateThrottle(error, THROTTLED_THERMAL);
Eddie James482e31f2017-09-14 13:17:17 -0500121}
122
Eddie James9789e712022-05-25 15:43:40 -0500123void Device::throttleProcPowerCallback(int error)
Eddie James482e31f2017-09-14 13:17:17 -0500124{
Gunnar Mills94df8c92018-09-14 14:50:03 -0500125 statusObject.throttleProcPower(error);
Chris Cainc86d80f2023-05-04 15:49:18 -0500126 // Update the processor throttle on dbus
127 statusObject.updateThrottle(error, THROTTLED_POWER);
Eddie James482e31f2017-09-14 13:17:17 -0500128}
129
Eddie James9789e712022-05-25 15:43:40 -0500130void Device::throttleMemTempCallback(int error)
Eddie James482e31f2017-09-14 13:17:17 -0500131{
Gunnar Mills94df8c92018-09-14 14:50:03 -0500132 statusObject.throttleMemTemp(error);
Eddie James482e31f2017-09-14 13:17:17 -0500133}
134
Chris Caine2d0a432022-03-28 11:08:49 -0500135fs::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 Cain37abe9b2024-10-31 17:20:31 -0500151 lg2::error("getFilenameByRegex: Failed to get filename: {ERROR}",
152 "ERROR", e.what());
Chris Caine2d0a432022-03-28 11:08:49 -0500153 }
154
155 // Return empty path
156 return fs::path{};
157}
158
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530159} // namespace occ
160} // namespace open_power