blob: 41a710972d28a5585925075bc526f5d7b19be821 [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
Adriana Kobylak3ca062a2021-10-20 15:27:23 +000014GPIOInterface::GPIOInterface(const std::string& namedGpio)
B. J. Wyman681b2a32021-04-20 22:31:22 +000015{
16 try
17 {
18 line = gpiod::find_line(namedGpio);
Adriana Kobylaka588eaf2021-09-20 20:05:25 +000019 if (!line)
20 {
21 throw std::runtime_error("Line does not exist: " + namedGpio);
22 }
B. J. Wyman681b2a32021-04-20 22:31:22 +000023 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -050024 catch (const std::exception& e)
B. J. Wyman681b2a32021-04-20 22:31:22 +000025 {
26 phosphor::logging::log<phosphor::logging::level::ERR>(
27 fmt::format("Failed to find line: {}", e.what()).c_str());
28 throw;
29 }
30}
31
Adriana Kobylak3ca062a2021-10-20 15:27:23 +000032std::unique_ptr<GPIOInterfaceBase>
33 GPIOInterface::createGPIO(const std::string& namedGpio)
B. J. Wyman681b2a32021-04-20 22:31:22 +000034{
Adriana Kobylak3ca062a2021-10-20 15:27:23 +000035 return std::make_unique<GPIOInterface>(namedGpio);
B. J. Wyman681b2a32021-04-20 22:31:22 +000036}
37
Adriana Kobylak3ca062a2021-10-20 15:27:23 +000038std::string GPIOInterface::getName() const
B. J. Wymand8b8cb12021-07-15 22:03:34 +000039{
40 return line.name();
41}
42
Adriana Kobylak3ca062a2021-10-20 15:27:23 +000043int GPIOInterface::read()
B. J. Wyman681b2a32021-04-20 22:31:22 +000044{
45 using namespace phosphor::logging;
46
47 int value = -1;
48
49 if (!line)
50 {
51 log<level::ERR>("Failed line");
52 throw std::runtime_error{std::string{"Failed to find line"}};
53 }
54
55 try
56 {
57 line.request({__FUNCTION__, gpiod::line_request::DIRECTION_INPUT,
58 gpiod::line_request::FLAG_ACTIVE_LOW});
59 try
60 {
61 value = line.get_value();
62 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -050063 catch (const std::exception& e)
B. J. Wyman681b2a32021-04-20 22:31:22 +000064 {
65 log<level::ERR>(
66 fmt::format("Failed to get_value of GPIO line: {}", e.what())
67 .c_str());
68 line.release();
69 throw;
70 }
71
B. J. Wyman681b2a32021-04-20 22:31:22 +000072 line.release();
73 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -050074 catch (const std::exception& e)
B. J. Wyman681b2a32021-04-20 22:31:22 +000075 {
76 log<level::ERR>("Failed to request GPIO line",
77 entry("MSG=%s", e.what()));
78 throw;
79 }
80
81 return value;
82}
83
Adriana Kobylak3ca062a2021-10-20 15:27:23 +000084std::unique_ptr<GPIOInterfaceBase> createGPIO(const std::string& namedGpio)
B. J. Wyman681b2a32021-04-20 22:31:22 +000085{
Adriana Kobylak3ca062a2021-10-20 15:27:23 +000086 return GPIOInterface::createGPIO(namedGpio);
B. J. Wyman681b2a32021-04-20 22:31:22 +000087}
88
Brandon Wyman3f1242f2020-01-28 13:11:25 -060089} // namespace phosphor::power::psu