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 fs::path Device::bindPath = fs::path(OCC_HWMON_PATH) / "bind";
19 fs::path Device::unBindPath = fs::path(OCC_HWMON_PATH) / "unbind";
20 
21 std::string Device::getPathBack(const fs::path& path)
22 {
23     if (path.empty())
24     {
25         return std::string();
26     }
27 
28     // Points to the last element in the path
29     auto conf = --path.end();
30 
31     if (conf->empty() && conf != path.begin())
32     {
33         return *(--conf);
34     }
35     else
36     {
37         return *conf;
38     }
39 }
40 
41 bool Device::master() const
42 {
43     int master;
44     auto masterFile = devPath / "occ_master";
45     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 
57 void Device::errorCallback(bool error)
58 {
59     if (error)
60     {
61         statusObject.deviceError();
62     }
63 }
64 
65 #ifdef PLDM
66 void Device::timeoutCallback(bool error)
67 {
68     if (error)
69     {
70         managerObject.sbeTimeout(instance);
71     }
72 }
73 #endif
74 
75 void Device::throttleProcTempCallback(bool error)
76 {
77     statusObject.throttleProcTemp(error);
78 }
79 
80 void Device::throttleProcPowerCallback(bool error)
81 {
82     statusObject.throttleProcPower(error);
83 }
84 
85 void Device::throttleMemTempCallback(bool error)
86 {
87     statusObject.throttleMemTemp(error);
88 }
89 
90 fs::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 
116 } // namespace occ
117 } // namespace open_power
118