blob: 5cadf140de3bd4e2797434d7af91b8aa47a296c8 [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
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +053018fs::path Device::bindPath = fs::path(OCC_HWMON_PATH) / "bind";
19fs::path Device::unBindPath = fs::path(OCC_HWMON_PATH) / "unbind";
20
Eddie James774f9af2019-03-19 20:58:53 +000021std::string Device::getPathBack(const fs::path& path)
22{
23 if (path.empty())
George Liubcef3b42021-09-10 12:39:02 +080024 {
Eddie James774f9af2019-03-19 20:58:53 +000025 return std::string();
George Liubcef3b42021-09-10 12:39:02 +080026 }
Eddie James774f9af2019-03-19 20:58:53 +000027
28 // Points to the last element in the path
29 auto conf = --path.end();
30
George Liubcef3b42021-09-10 12:39:02 +080031 if (conf->empty() && conf != path.begin())
Eddie James774f9af2019-03-19 20:58:53 +000032 {
33 return *(--conf);
34 }
35 else
36 {
37 return *conf;
38 }
39}
40
Edward A. James636577f2017-10-06 10:53:55 -050041bool Device::master() const
42{
43 int master;
Eddie James774f9af2019-03-19 20:58:53 +000044 auto masterFile = devPath / "occ_master";
Edward A. James636577f2017-10-06 10:53:55 -050045 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 Jamescbad2192021-10-07 09:39:39 -050057void Device::errorCallback(bool error)
58{
59 if (error)
60 {
61 statusObject.deviceError();
62 }
63}
64
65#ifdef PLDM
66void Device::timeoutCallback(bool error)
67{
68 if (error)
69 {
70 managerObject.sbeTimeout(instance);
71 }
72}
73#endif
74
Eddie James482e31f2017-09-14 13:17:17 -050075void Device::throttleProcTempCallback(bool error)
76{
Gunnar Mills94df8c92018-09-14 14:50:03 -050077 statusObject.throttleProcTemp(error);
Eddie James482e31f2017-09-14 13:17:17 -050078}
79
80void Device::throttleProcPowerCallback(bool error)
81{
Gunnar Mills94df8c92018-09-14 14:50:03 -050082 statusObject.throttleProcPower(error);
Eddie James482e31f2017-09-14 13:17:17 -050083}
84
85void Device::throttleMemTempCallback(bool error)
86{
Gunnar Mills94df8c92018-09-14 14:50:03 -050087 statusObject.throttleMemTemp(error);
Eddie James482e31f2017-09-14 13:17:17 -050088}
89
Chris Caine2d0a432022-03-28 11:08:49 -050090fs::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 Subbanna32e84e92017-06-28 19:17:28 +0530116} // namespace occ
117} // namespace open_power