1 #include "occ_device.hpp"
2 
3 #include "occ_manager.hpp"
4 #include "occ_status.hpp"
5 
6 #include <phosphor-logging/log.hpp>
7 
8 #include <filesystem>
9 #include <iostream>
10 
11 namespace open_power
12 {
13 namespace occ
14 {
15 
16 using namespace phosphor::logging;
17 
18 void 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 }
33 
34 std::string Device::getPathBack(const fs::path& path)
35 {
36     if (path.empty())
37     {
38         return std::string();
39     }
40 
41     // Points to the last element in the path
42     auto conf = --path.end();
43 
44     if (conf->empty() && conf != path.begin())
45     {
46         return *(--conf);
47     }
48     else
49     {
50         return *conf;
51     }
52 }
53 
54 bool Device::active() const
55 {
56     return readBinary("occ_active");
57 }
58 
59 bool Device::master() const
60 {
61     return readBinary("occ_master");
62 }
63 
64 bool Device::readBinary(const std::string& fileName) const
65 {
66     int v;
67     auto filePath = devPath / fileName;
68     std::ifstream file(filePath, std::ios::in);
69 
70     if (!file)
71     {
72         return false;
73     }
74 
75     file >> v;
76     file.close();
77     return v == 1;
78 }
79 
80 void Device::errorCallback(bool error)
81 {
82     if (error)
83     {
84         statusObject.deviceError();
85     }
86 }
87 
88 #ifdef PLDM
89 void Device::timeoutCallback(bool error)
90 {
91     if (error)
92     {
93         managerObject.sbeTimeout(instance);
94     }
95 }
96 #endif
97 
98 void Device::throttleProcTempCallback(bool error)
99 {
100     statusObject.throttleProcTemp(error);
101 }
102 
103 void Device::throttleProcPowerCallback(bool error)
104 {
105     statusObject.throttleProcPower(error);
106 }
107 
108 void Device::throttleMemTempCallback(bool error)
109 {
110     statusObject.throttleMemTemp(error);
111 }
112 
113 fs::path Device::getFilenameByRegex(fs::path basePath,
114                                     const std::regex& expr) const
115 {
116     try
117     {
118         for (auto& file : fs::directory_iterator(basePath))
119         {
120             if (std::regex_search(file.path().string(), expr))
121             {
122                 // Found match
123                 return file;
124             }
125         }
126     }
127     catch (const fs::filesystem_error& e)
128     {
129         log<level::ERR>(
130             fmt::format("getFilenameByRegex: Failed to get filename: {}",
131                         e.what())
132                 .c_str());
133     }
134 
135     // Return empty path
136     return fs::path{};
137 }
138 
139 } // namespace occ
140 } // namespace open_power
141