summaryrefslogtreecommitdiffstats
path: root/gpio_handle.cpp
blob: d99a031d5de1552ffbe37028b621fdb26324f936 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "gpio_handle.hpp"

#include <cstdlib>
#include <gpioplus/chip.hpp>
#include <gpioplus/handle.hpp>
#include <memory>
#include <phosphor-logging/log.hpp>
#include <string>

namespace gpio
{

using namespace phosphor::logging;

std::unique_ptr<gpioplus::HandleInterface>
    BuildGpioHandle(const std::string& gpiochip, const std::string& line)
{
    char *gpioEnd, *lineEnd;
    unsigned long chipId = std::strtoul(gpiochip.c_str(), &gpioEnd, 10);
    unsigned long lineOffset = std::strtoul(line.c_str(), &lineEnd, 10);

    if (!gpioEnd || gpioEnd != &gpiochip.c_str()[gpiochip.length()])
    {
        log<level::ERR>("Unable to handle giochip entry",
                        entry("GPIOCHIP=%s", gpiochip.c_str()));
        return nullptr;
    }

    if (!lineEnd || lineEnd != &line.c_str()[line.length()])
    {
        log<level::ERR>("Unable to handle line entry",
                        entry("LINE=%s", line.c_str()));
        return nullptr;
    }

    try
    {
        gpioplus::Chip chip(chipId);
        gpioplus::HandleFlags flags(chip.getLineInfo(lineOffset).flags);
        flags.output = true;
        std::vector<gpioplus::Handle::Line> lines = {
            {static_cast<uint32_t>(lineOffset), 0}};

        return std::make_unique<gpioplus::Handle>(chip, lines, flags,
                                                  "phosphor-hwmon");
    }
    catch (const std::exception& e)
    {
        log<level::ERR>("Unable to set up GPIO handle",
                        entry("ERROR=%s", e.what()));
        return nullptr;
    }
}

} // namespace gpio
OpenPOWER on IntegriCloud