blob: f80bac7ffe07ef60d205e1e558a9ef7b905ce41f [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
34int GPIOReader::read()
35{
36 using namespace phosphor::logging;
37
38 int value = -1;
39
40 if (!line)
41 {
42 log<level::ERR>("Failed line");
43 throw std::runtime_error{std::string{"Failed to find line"}};
44 }
45
46 try
47 {
48 line.request({__FUNCTION__, gpiod::line_request::DIRECTION_INPUT,
49 gpiod::line_request::FLAG_ACTIVE_LOW});
50 try
51 {
52 value = line.get_value();
53 }
54 catch (std::exception& e)
55 {
56 log<level::ERR>(
57 fmt::format("Failed to get_value of GPIO line: {}", e.what())
58 .c_str());
59 line.release();
60 throw;
61 }
62
B. J. Wyman681b2a32021-04-20 22:31:22 +000063 line.release();
64 }
65 catch (std::exception& e)
66 {
67 log<level::ERR>("Failed to request GPIO line",
68 entry("MSG=%s", e.what()));
69 throw;
70 }
71
72 return value;
73}
74
75std::unique_ptr<GPIOInterface> createGPIO(const std::string& namedGpio)
76{
77 return GPIOReader::createGPIO(namedGpio);
78}
79
Brandon Wyman3f1242f2020-01-28 13:11:25 -060080} // namespace phosphor::power::psu