blob: 5942ce756f1b050073137e5ab6784417ee84a790 [file] [log] [blame]
Brandon Wyman3f1242f2020-01-28 13:11:25 -06001#include "util.hpp"
2
B. J. Wyman681b2a32021-04-20 22:31:22 +00003#include <gpiod.hpp>
4
Brandon Wyman3f1242f2020-01-28 13:11:25 -06005namespace phosphor::power::psu
6{
7
8const UtilBase& getUtils()
9{
10 static Util util;
11 return util;
12}
13
B. J. Wyman681b2a32021-04-20 22:31:22 +000014GPIOReader::GPIOReader(const std::string& namedGpio)
15{
16 try
17 {
18 line = gpiod::find_line(namedGpio);
19 }
20 catch (std::exception& e)
21 {
22 phosphor::logging::log<phosphor::logging::level::ERR>(
23 fmt::format("Failed to find line: {}", e.what()).c_str());
24 throw;
25 }
26}
27
28std::unique_ptr<GPIOInterface>
29 GPIOReader::createGPIO(const std::string& namedGpio)
30{
31 return std::make_unique<GPIOReader>(namedGpio);
32}
33
B. J. Wymand8b8cb12021-07-15 22:03:34 +000034std::string GPIOReader::getName() const
35{
36 return line.name();
37}
38
B. J. Wyman681b2a32021-04-20 22:31:22 +000039int GPIOReader::read()
40{
41 using namespace phosphor::logging;
42
43 int value = -1;
44
45 if (!line)
46 {
47 log<level::ERR>("Failed line");
48 throw std::runtime_error{std::string{"Failed to find line"}};
49 }
50
51 try
52 {
53 line.request({__FUNCTION__, gpiod::line_request::DIRECTION_INPUT,
54 gpiod::line_request::FLAG_ACTIVE_LOW});
55 try
56 {
57 value = line.get_value();
58 }
59 catch (std::exception& e)
60 {
61 log<level::ERR>(
62 fmt::format("Failed to get_value of GPIO line: {}", e.what())
63 .c_str());
64 line.release();
65 throw;
66 }
67
B. J. Wyman681b2a32021-04-20 22:31:22 +000068 line.release();
69 }
70 catch (std::exception& e)
71 {
72 log<level::ERR>("Failed to request GPIO line",
73 entry("MSG=%s", e.what()));
74 throw;
75 }
76
77 return value;
78}
79
80std::unique_ptr<GPIOInterface> createGPIO(const std::string& namedGpio)
81{
82 return GPIOReader::createGPIO(namedGpio);
83}
84
Brandon Wyman3f1242f2020-01-28 13:11:25 -060085} // namespace phosphor::power::psu