blob: df8683e4f79b4b5a1cb9c880c9b55420edec5e3f [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 Caine2d0a432022-03-28 11:08:49 -05006#include <phosphor-logging/log.hpp>
7
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
Chris Caine2d0a432022-03-28 11:08:49 -050016using namespace phosphor::logging;
17
Eddie Jamesaced3092022-04-22 16:19:30 -050018void 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 Subbanna32e84e92017-06-28 19:17:28 +053033
Eddie James774f9af2019-03-19 20:58:53 +000034std::string Device::getPathBack(const fs::path& path)
35{
36 if (path.empty())
George Liubcef3b42021-09-10 12:39:02 +080037 {
Eddie James774f9af2019-03-19 20:58:53 +000038 return std::string();
George Liubcef3b42021-09-10 12:39:02 +080039 }
Eddie James774f9af2019-03-19 20:58:53 +000040
41 // Points to the last element in the path
42 auto conf = --path.end();
43
George Liubcef3b42021-09-10 12:39:02 +080044 if (conf->empty() && conf != path.begin())
Eddie James774f9af2019-03-19 20:58:53 +000045 {
46 return *(--conf);
47 }
48 else
49 {
50 return *conf;
51 }
52}
53
Eddie Jamesaced3092022-04-22 16:19:30 -050054bool Device::active() const
55{
56 return readBinary("occ_active");
57}
58
Edward A. James636577f2017-10-06 10:53:55 -050059bool Device::master() const
60{
Eddie Jamesaced3092022-04-22 16:19:30 -050061 return readBinary("occ_master");
62}
63
64bool Device::readBinary(const std::string& fileName) const
65{
Chris Cainbd551de2022-04-26 13:41:16 -050066 int v = 0;
67 if (statusObject.occActive())
Edward A. James636577f2017-10-06 10:53:55 -050068 {
Chris Cainbd551de2022-04-26 13:41:16 -050069 auto filePath = devPath / fileName;
70 std::ifstream file(filePath, std::ios::in);
71 if (!file)
72 {
73 return false;
74 }
Edward A. James636577f2017-10-06 10:53:55 -050075
Chris Cainbd551de2022-04-26 13:41:16 -050076 file >> v;
77 file.close();
78 }
Eddie Jamesaced3092022-04-22 16:19:30 -050079 return v == 1;
Edward A. James636577f2017-10-06 10:53:55 -050080}
81
Eddie Jamescbad2192021-10-07 09:39:39 -050082void Device::errorCallback(bool error)
83{
84 if (error)
85 {
86 statusObject.deviceError();
87 }
88}
89
90#ifdef PLDM
91void Device::timeoutCallback(bool error)
92{
93 if (error)
94 {
95 managerObject.sbeTimeout(instance);
96 }
97}
98#endif
99
Eddie James482e31f2017-09-14 13:17:17 -0500100void Device::throttleProcTempCallback(bool error)
101{
Gunnar Mills94df8c92018-09-14 14:50:03 -0500102 statusObject.throttleProcTemp(error);
Eddie James482e31f2017-09-14 13:17:17 -0500103}
104
105void Device::throttleProcPowerCallback(bool error)
106{
Gunnar Mills94df8c92018-09-14 14:50:03 -0500107 statusObject.throttleProcPower(error);
Eddie James482e31f2017-09-14 13:17:17 -0500108}
109
110void Device::throttleMemTempCallback(bool error)
111{
Gunnar Mills94df8c92018-09-14 14:50:03 -0500112 statusObject.throttleMemTemp(error);
Eddie James482e31f2017-09-14 13:17:17 -0500113}
114
Chris Caine2d0a432022-03-28 11:08:49 -0500115fs::path Device::getFilenameByRegex(fs::path basePath,
116 const std::regex& expr) const
117{
118 try
119 {
120 for (auto& file : fs::directory_iterator(basePath))
121 {
122 if (std::regex_search(file.path().string(), expr))
123 {
124 // Found match
125 return file;
126 }
127 }
128 }
129 catch (const fs::filesystem_error& e)
130 {
131 log<level::ERR>(
132 fmt::format("getFilenameByRegex: Failed to get filename: {}",
133 e.what())
134 .c_str());
135 }
136
137 // Return empty path
138 return fs::path{};
139}
140
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530141} // namespace occ
142} // namespace open_power