132e84e98SVishwanatha Subbanna #include "occ_device.hpp"
294df8c90SGunnar Mills
3cbad219eSEddie James #include "occ_manager.hpp"
4482e31ffSEddie James #include "occ_status.hpp"
532e84e98SVishwanatha Subbanna
6*37abe9beSChris Cain #include <phosphor-logging/lg2.hpp>
7e2d0a43cSChris Cain
8e2d0a43cSChris Cain #include <filesystem>
994df8c90SGunnar Mills #include <iostream>
1094df8c90SGunnar Mills
1132e84e98SVishwanatha Subbanna namespace open_power
1232e84e98SVishwanatha Subbanna {
1332e84e98SVishwanatha Subbanna namespace occ
1432e84e98SVishwanatha Subbanna {
1532e84e98SVishwanatha Subbanna
setActive(bool active)16aced3098SEddie James void Device::setActive(bool active)
17aced3098SEddie James {
18aced3098SEddie James std::string data = active ? "1" : "0";
19aced3098SEddie James auto activeFile = devPath / "occ_active";
20aced3098SEddie James try
21aced3098SEddie James {
22aced3098SEddie James write(activeFile, data);
23aced3098SEddie James }
24aced3098SEddie James catch (const std::exception& e)
25aced3098SEddie James {
26*37abe9beSChris Cain lg2::error("Failed to set {DEVICE} active: {ERROR}", "DEVICE", devPath,
27*37abe9beSChris Cain "ERROR", e.what());
28aced3098SEddie James }
29aced3098SEddie James }
3032e84e98SVishwanatha Subbanna
getPathBack(const fs::path & path)31774f9af9SEddie James std::string Device::getPathBack(const fs::path& path)
32774f9af9SEddie James {
33774f9af9SEddie James if (path.empty())
34bcef3b48SGeorge Liu {
35774f9af9SEddie James return std::string();
36bcef3b48SGeorge Liu }
37774f9af9SEddie James
38774f9af9SEddie James // Points to the last element in the path
39774f9af9SEddie James auto conf = --path.end();
40774f9af9SEddie James
41bcef3b48SGeorge Liu if (conf->empty() && conf != path.begin())
42774f9af9SEddie James {
43774f9af9SEddie James return *(--conf);
44774f9af9SEddie James }
45774f9af9SEddie James else
46774f9af9SEddie James {
47774f9af9SEddie James return *conf;
48774f9af9SEddie James }
49774f9af9SEddie James }
50774f9af9SEddie James
active() const51aced3098SEddie James bool Device::active() const
52aced3098SEddie James {
53aced3098SEddie James return readBinary("occ_active");
54aced3098SEddie James }
55aced3098SEddie James
master() const56636577f4SEdward A. James bool Device::master() const
57636577f4SEdward A. James {
58aced3098SEddie James return readBinary("occ_master");
59aced3098SEddie James }
60aced3098SEddie James
readBinary(const std::string & fileName) const61aced3098SEddie James bool Device::readBinary(const std::string& fileName) const
62aced3098SEddie James {
63bd551de3SChris Cain int v = 0;
64bd551de3SChris Cain if (statusObject.occActive())
65bd551de3SChris Cain {
66aced3098SEddie James auto filePath = devPath / fileName;
67aced3098SEddie James std::ifstream file(filePath, std::ios::in);
68636577f4SEdward A. James if (!file)
69636577f4SEdward A. James {
70636577f4SEdward A. James return false;
71636577f4SEdward A. James }
72636577f4SEdward A. James
73aced3098SEddie James file >> v;
74636577f4SEdward A. James file.close();
75bd551de3SChris Cain }
76aced3098SEddie James return v == 1;
77636577f4SEdward A. James }
78636577f4SEdward A. James
errorCallback(int error)799789e71fSEddie James void Device::errorCallback(int error)
80cbad219eSEddie James {
81cbad219eSEddie James if (error)
82cbad219eSEddie James {
839789e71fSEddie James if (error != -EHOSTDOWN)
849789e71fSEddie James {
859789e71fSEddie James fs::path p = devPath;
869789e71fSEddie James if (fs::is_symlink(p))
879789e71fSEddie James {
889789e71fSEddie James p = fs::read_symlink(p);
899789e71fSEddie James }
90fec4b0b1SMatt Spinler statusObject.deviceError(
91fec4b0b1SMatt Spinler Error::Descriptor("org.open_power.OCC.Device.Error.ReadFailure",
92fec4b0b1SMatt Spinler error, p.c_str()));
939789e71fSEddie James }
949789e71fSEddie James else
959789e71fSEddie James {
969789e71fSEddie James statusObject.deviceError(Error::Descriptor(SAFE_ERROR_PATH));
979789e71fSEddie James }
98cbad219eSEddie James }
99cbad219eSEddie James }
100cbad219eSEddie James
presenceCallback(int)1019789e71fSEddie James void Device::presenceCallback(int)
1029789e71fSEddie James {
1039789e71fSEddie James statusObject.deviceError(Error::Descriptor(PRESENCE_ERROR_PATH));
1049789e71fSEddie James }
1059789e71fSEddie James
106cbad219eSEddie James #ifdef PLDM
timeoutCallback(int error)1079789e71fSEddie James void Device::timeoutCallback(int error)
108cbad219eSEddie James {
109cbad219eSEddie James if (error)
110cbad219eSEddie James {
111cbad219eSEddie James managerObject.sbeTimeout(instance);
112cbad219eSEddie James }
113cbad219eSEddie James }
114cbad219eSEddie James #endif
115cbad219eSEddie James
throttleProcTempCallback(int error)1169789e71fSEddie James void Device::throttleProcTempCallback(int error)
117482e31ffSEddie James {
118482e31ffSEddie James statusObject.throttleProcTemp(error);
119c86d80faSChris Cain // Update the processor throttle on dbus
120c86d80faSChris Cain statusObject.updateThrottle(error, THROTTLED_THERMAL);
121482e31ffSEddie James }
122482e31ffSEddie James
throttleProcPowerCallback(int error)1239789e71fSEddie James void Device::throttleProcPowerCallback(int error)
124482e31ffSEddie James {
125482e31ffSEddie James statusObject.throttleProcPower(error);
126c86d80faSChris Cain // Update the processor throttle on dbus
127c86d80faSChris Cain statusObject.updateThrottle(error, THROTTLED_POWER);
128482e31ffSEddie James }
129482e31ffSEddie James
throttleMemTempCallback(int error)1309789e71fSEddie James void Device::throttleMemTempCallback(int error)
131482e31ffSEddie James {
132482e31ffSEddie James statusObject.throttleMemTemp(error);
133482e31ffSEddie James }
134482e31ffSEddie James
getFilenameByRegex(fs::path basePath,const std::regex & expr) const135e2d0a43cSChris Cain fs::path Device::getFilenameByRegex(fs::path basePath,
136e2d0a43cSChris Cain const std::regex& expr) const
137e2d0a43cSChris Cain {
138e2d0a43cSChris Cain try
139e2d0a43cSChris Cain {
140e2d0a43cSChris Cain for (auto& file : fs::directory_iterator(basePath))
141e2d0a43cSChris Cain {
142e2d0a43cSChris Cain if (std::regex_search(file.path().string(), expr))
143e2d0a43cSChris Cain {
144e2d0a43cSChris Cain // Found match
145e2d0a43cSChris Cain return file;
146e2d0a43cSChris Cain }
147e2d0a43cSChris Cain }
148e2d0a43cSChris Cain }
149e2d0a43cSChris Cain catch (const fs::filesystem_error& e)
150e2d0a43cSChris Cain {
151*37abe9beSChris Cain lg2::error("getFilenameByRegex: Failed to get filename: {ERROR}",
152*37abe9beSChris Cain "ERROR", e.what());
153e2d0a43cSChris Cain }
154e2d0a43cSChris Cain
155e2d0a43cSChris Cain // Return empty path
156e2d0a43cSChris Cain return fs::path{};
157e2d0a43cSChris Cain }
158e2d0a43cSChris Cain
15932e84e98SVishwanatha Subbanna } // namespace occ
16032e84e98SVishwanatha Subbanna } // namespace open_power
161