forked from openbmc/phosphor-power
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpio.hpp
117 lines (99 loc) · 2.13 KB
/
gpio.hpp
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
#pragma once
#include "file.hpp"
#include <linux/gpio.h>
#include <string>
#include <type_traits>
namespace phosphor
{
namespace gpio
{
typedef std::remove_reference<decltype(
gpiohandle_request::lineoffsets[0])>::type gpioNum_t;
typedef std::remove_reference<decltype(gpiohandle_data::values[0])>::type
gpioValue_t;
/**
* If the GPIO is an input or output
*/
enum class Direction
{
input,
output
};
/**
* The possible values - low or high
*/
enum class Value
{
low,
high
};
/**
* Represents a GPIO.
*
* Currently supports reading a GPIO.
*
* Write support may be added in the future.
*/
class GPIO
{
public:
GPIO() = delete;
GPIO(const GPIO&) = delete;
GPIO(GPIO&&) = default;
GPIO& operator=(const GPIO&) = delete;
GPIO& operator=(GPIO&&) = default;
~GPIO() = default;
/**
* Constructor
*
* @param[in] device - the GPIO device file
* @param[in] gpio - the GPIO number
* @param[in] direction - the GPIO direction
*/
GPIO(const std::string& device, gpioNum_t gpio, Direction direction) :
device(device), gpio(gpio), direction(direction)
{
}
/**
* Reads the GPIO value
*
* Requests the GPIO line if it hasn't been done already.
*
* @return Value - the GPIO value
*/
Value read();
/**
* Sets the GPIO value to low or high
*
* Requests the GPIO line if it hasn't been done already.
*
* @param[in] Value - the value to set
*/
void set(Value value);
private:
/**
* Requests a GPIO line from the GPIO device
*
* @param[in] defaultValue - The default value, required for
* output GPIOs only.
*/
void requestLine(Value defaultValue = Value::high);
/**
* The GPIO device name, like /dev/gpiochip0
*/
const std::string device;
/**
* The GPIO number
*/
const gpioNum_t gpio;
/**
* The GPIO direction
*/
const Direction direction;
/**
* File descriptor for the GPIO line
*/
power::util::FileDescriptor lineFD;
};
} // namespace gpio
} // namespace phosphor