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