blob: d4ac47b56fd5198a76cc33097853192c3e1b3078 [file] [log] [blame]
#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;
/**
* Represents a GPIO.
*
* Operations are setting low or high.
*
* Read support may be added in the future.
*/
class GPIO
{
public:
/**
* If the GPIO is an input or output
*/
enum class Direction
{
input,
output
};
/**
* The possible values - low or high
*/
enum class Value
{
low,
high
};
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)
{
}
/**
* Sets the GPIO value
*
* Requests the GPIO line if it hasn't been done already.
*/
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
*/
FileDescriptor lineFD;
};
} // namespace gpio
} // namespace phosphor