blob: a4f6115200f4581abdc32ef6492a6af8c2015e1e [file] [log] [blame]
Patrick Ventureb28f4322018-09-14 10:19:14 -07001#include "gpio_handle.hpp"
2
Patrick Ventureb28f4322018-09-14 10:19:14 -07003#include <gpioplus/chip.hpp>
4#include <gpioplus/handle.hpp>
Patrick Ventureb28f4322018-09-14 10:19:14 -07005#include <phosphor-logging/log.hpp>
Patrick Williamse8771fd2023-05-10 07:51:06 -05006
7#include <cstdlib>
8#include <memory>
Patrick Ventureb28f4322018-09-14 10:19:14 -07009#include <string>
10
11namespace gpio
12{
13
14using namespace phosphor::logging;
15
Patrick Venturee3e0f972018-12-19 08:58:21 -080016std::unique_ptr<gpioplus::HandleInterface>
17 BuildGpioHandle(const std::string& gpiochip, const std::string& line)
Patrick Ventureb28f4322018-09-14 10:19:14 -070018{
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