xref: /openbmc/openpower-occ-control/occ_presence.cpp (revision 37abe9be91df2bb173c9642a2740a425904d7921)
1636577f4SEdward A. James #include "occ_presence.hpp"
294df8c90SGunnar Mills 
394df8c90SGunnar Mills #include "occ_manager.hpp"
494df8c90SGunnar Mills 
594df8c90SGunnar Mills #include <errno.h>
694df8c90SGunnar Mills #include <fcntl.h>
794df8c90SGunnar Mills #include <sys/ioctl.h>
894df8c90SGunnar Mills #include <unistd.h>
994df8c90SGunnar Mills 
1094df8c90SGunnar Mills #include <org/open_power/OCC/Device/error.hpp>
11d8aab2a9SPatrick Williams #include <phosphor-logging/elog-errors.hpp>
1294df8c90SGunnar Mills #include <phosphor-logging/elog.hpp>
13*37abe9beSChris Cain #include <phosphor-logging/lg2.hpp>
1494df8c90SGunnar Mills #include <xyz/openbmc_project/Common/error.hpp>
15636577f4SEdward A. James 
16636577f4SEdward A. James namespace open_power
17636577f4SEdward A. James {
18636577f4SEdward A. James namespace occ
19636577f4SEdward A. James {
20636577f4SEdward A. James 
21636577f4SEdward A. James // Reads the occs_present file and analyzes the data
analyzeEvent()22636577f4SEdward A. James void Presence::analyzeEvent()
23636577f4SEdward A. James {
24636577f4SEdward A. James     using namespace phosphor::logging;
25636577f4SEdward A. James     using namespace sdbusplus::org::open_power::OCC::Device::Error;
26636577f4SEdward A. James 
27636577f4SEdward A. James     // Get the number of bytes to read
28636577f4SEdward A. James     int len = -1;
29636577f4SEdward A. James     auto r = ioctl(fd, FIONREAD, &len);
30636577f4SEdward A. James     if (r < 0)
31636577f4SEdward A. James     {
32636577f4SEdward A. James         elog<ConfigFailure>(
3394df8c90SGunnar Mills             phosphor::logging::org::open_power::OCC::Device::ConfigFailure::
3494df8c90SGunnar Mills                 CALLOUT_ERRNO(errno),
3594df8c90SGunnar Mills             phosphor::logging::org::open_power::OCC::Device::ConfigFailure::
3694df8c90SGunnar Mills                 CALLOUT_DEVICE_PATH(file.c_str()));
37636577f4SEdward A. James     }
38636577f4SEdward A. James 
39636577f4SEdward A. James     auto data = readFile(len);
40636577f4SEdward A. James     if (data.empty())
41636577f4SEdward A. James     {
42636577f4SEdward A. James         return;
43636577f4SEdward A. James     }
44636577f4SEdward A. James 
45636577f4SEdward A. James     // Let stoi determine the base
46636577f4SEdward A. James     auto occsPresent = std::stoi(data, nullptr, 0);
47636577f4SEdward A. James     if (manager.getNumOCCs() != occsPresent)
48636577f4SEdward A. James     {
49*37abe9beSChris Cain         lg2::error("OCC presence mismatch - BMC: {BNUM}, OCC: {ONUM}", "BNUM",
50*37abe9beSChris Cain                    manager.getNumOCCs(), "ONUM", occsPresent);
51636577f4SEdward A. James         if (callBack)
52636577f4SEdward A. James         {
539789e71fSEddie James             callBack(occsPresent);
54636577f4SEdward A. James         }
55636577f4SEdward A. James     }
56636577f4SEdward A. James }
57636577f4SEdward A. James 
58636577f4SEdward A. James } // namespace occ
59636577f4SEdward A. James } // namespace open_power
60