xref: /openbmc/phosphor-hwmon/gpio_handle.cpp (revision 5906173a)
1 #include "gpio_handle.hpp"
2 
3 #include <cstdlib>
4 #include <gpioplus/chip.hpp>
5 #include <gpioplus/handle.hpp>
6 #include <memory>
7 #include <phosphor-logging/log.hpp>
8 #include <string>
9 
10 namespace gpio
11 {
12 
13 using namespace phosphor::logging;
14 
15 std::unique_ptr<gpioplus::HandleInterface>
16     BuildGpioHandle(const std::string& gpiochip, const std::string& line)
17 {
18     char *gpioEnd, *lineEnd;
19     unsigned long chipId = std::strtoul(gpiochip.c_str(), &gpioEnd, 10);
20     unsigned long lineOffset = std::strtoul(line.c_str(), &lineEnd, 10);
21 
22     if (!gpioEnd || gpioEnd != &gpiochip.c_str()[gpiochip.length()])
23     {
24         log<level::ERR>("Unable to handle giochip entry",
25                         entry("GPIOCHIP=%s", gpiochip.c_str()));
26         return nullptr;
27     }
28 
29     if (!lineEnd || lineEnd != &line.c_str()[line.length()])
30     {
31         log<level::ERR>("Unable to handle line entry",
32                         entry("LINE=%s", line.c_str()));
33         return nullptr;
34     }
35 
36     try
37     {
38         gpioplus::Chip chip(chipId);
39         gpioplus::HandleFlags flags(chip.getLineInfo(lineOffset).flags);
40         flags.output = true;
41         std::vector<gpioplus::Handle::Line> lines = {
42             {static_cast<uint32_t>(lineOffset), 0}};
43 
44         return std::make_unique<gpioplus::Handle>(chip, lines, flags,
45                                                   "phosphor-hwmon");
46     }
47     catch (const std::exception& e)
48     {
49         log<level::ERR>("Unable to set up GPIO handle",
50                         entry("ERROR=%s", e.what()));
51         return nullptr;
52     }
53 }
54 
55 } // namespace gpio
56