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