summaryrefslogtreecommitdiff
path: root/gpiodaemon/src/gpioutils.cpp
blob: d2d4adc6fe6fa91443eb1891c3419c3a52dd40f8 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
// Copyright (c) 2018 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
*/

#include <gpioutils.hpp>

static constexpr const char* pathExport = "/sys/class/gpio/export";
static constexpr const char* pathUnexport = "/sys/class/gpio/unexport";
static constexpr const char* prefix = "/sys/class/gpio/gpio";
static constexpr const char* postfixValue = "/value";
static constexpr const char* postfixDirection = "/direction";

Gpio::Gpio(std::string number) : gpioNumber(number)
{
    gpioExport();
}

bool Gpio::gpioExist()
{
    return std::experimental::filesystem::exists(prefix + gpioNumber);
}

void Gpio::gpioExport()
{
    if (!gpioExist())
    {
        std::fstream gpioExport;
        gpioExport.open(pathExport, std::ios::out);
        if (gpioExport.good())
        {
            gpioExport << gpioNumber;
        }
        gpioExport.close();
    }
}

void Gpio::gpioUnexport()
{
    if (gpioExist())
    {
        std::fstream gpioExport;
        gpioExport.open(pathUnexport, std::ios::out);
        if (gpioExport.good())
        {
            gpioExport << gpioNumber;
        }
        gpioExport.close();
    }
}

GpioValue Gpio::getValue()
{
    std::fstream gpioFile;
    std::string gpioValue;
    std::string gpioFilePath = prefix + gpioNumber + postfixValue;

    gpioFile.open(gpioFilePath, std::ios::in);
    if (gpioFile.good())
    {
        gpioFile >> gpioValue;
    }
    gpioFile.close();

    return gpioValue == "0" ? GpioValue::low : GpioValue::high;
}

void Gpio::setValue(const GpioValue& value)
{
    std::fstream gpioFile;
    std::string gpioFilePath = prefix + gpioNumber + postfixValue;

    setDirection("out");

    gpioFile.open(gpioFilePath, std::ios::out);
    if (gpioFile.good())
    {
        gpioFile << std::to_string((uint8_t)value);
    }
    gpioFile.close();
}

std::string Gpio::getDirection()
{
    std::fstream gpioFile;
    std::string gpioFilePath = prefix + gpioNumber + postfixDirection;
    std::string direction;

    gpioFile.open(gpioFilePath, std::ios::in);
    if (gpioFile.good())
    {
        gpioFile >> direction;
    }
    gpioFile.close();

    return direction;
}

void Gpio::setDirection(const std::string& direction)
{
    std::fstream gpioFile;
    std::string gpioFilePath = prefix + gpioNumber + postfixDirection;

    gpioFile.open(gpioFilePath, std::ios::out);
    if (gpioFile.good())
    {
        gpioFile << direction;
    }
    gpioFile.close();
}