Patrick Venture | b28f432 | 2018-09-14 10:19:14 -0700 | [diff] [blame] | 1 | #include "gpio_handle.hpp" |
| 2 | |
Patrick Venture | b28f432 | 2018-09-14 10:19:14 -0700 | [diff] [blame] | 3 | #include <gpioplus/chip.hpp> |
| 4 | #include <gpioplus/handle.hpp> |
Patrick Venture | b28f432 | 2018-09-14 10:19:14 -0700 | [diff] [blame] | 5 | #include <phosphor-logging/log.hpp> |
Patrick Williams | e8771fd | 2023-05-10 07:51:06 -0500 | [diff] [blame] | 6 | |
| 7 | #include <cstdlib> |
| 8 | #include <memory> |
Patrick Venture | b28f432 | 2018-09-14 10:19:14 -0700 | [diff] [blame] | 9 | #include <string> |
| 10 | |
| 11 | namespace gpio |
| 12 | { |
| 13 | |
| 14 | using namespace phosphor::logging; |
| 15 | |
Patrick Venture | e3e0f97 | 2018-12-19 08:58:21 -0800 | [diff] [blame] | 16 | std::unique_ptr<gpioplus::HandleInterface> |
| 17 | BuildGpioHandle(const std::string& gpiochip, const std::string& line) |
Patrick Venture | b28f432 | 2018-09-14 10:19:14 -0700 | [diff] [blame] | 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 |