1 #include "host_condition.hpp"
2 
3 #include <gpiod.hpp>
4 #include <phosphor-logging/elog-errors.hpp>
5 #include <phosphor-logging/lg2.hpp>
6 
7 namespace phosphor
8 {
9 namespace condition
10 {
11 
12 PHOSPHOR_LOG2_USING;
13 
14 using namespace phosphor::logging;
15 
16 void Host::scanGpioPin()
17 {
18     /*
19      * Check the hostX-ready/-n pin is defined or not
20      */
21     if (gpiod::find_line(lineName + "-ready"))
22     {
23         lineName += "-ready";
24         isActHigh = true;
25     }
26     else if (gpiod::find_line(lineName + "-ready-n"))
27     {
28         lineName += "-ready-n";
29         isActHigh = false;
30     }
31     else
32     {
33         lineName = "";
34     }
35 }
36 
37 Host::FirmwareCondition Host::currentFirmwareCondition() const
38 {
39     auto retVal = Host::FirmwareCondition::Unknown;
40 
41     /*
42      * Check host is ready or not
43      */
44     if (!lineName.empty())
45     {
46         auto line = gpiod::find_line(lineName);
47 
48         try
49         {
50             int32_t gpioVal = 0;
51 
52             line.request({lineName, gpiod::line_request::DIRECTION_INPUT,
53                           (true == isActHigh)
54                               ? 0
55                               : gpiod::line_request::FLAG_ACTIVE_LOW});
56 
57             gpioVal = line.get_value();
58             line.release();
59 
60             retVal = (0 == gpioVal) ? Host::FirmwareCondition::Off
61                                     : Host::FirmwareCondition::Running;
62         }
63         catch (std::system_error&)
64         {
65             error("Error when read gpio value");
66         }
67     }
68 
69     return retVal;
70 }
71 } // namespace condition
72 } // namespace phosphor
73