blob: 36dd1029a5a6574c4b03c8cfaa71e5212e4d98f4 [file] [log] [blame]
#ifndef THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_HAL_GPIO_GPIO_H_
#define THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_HAL_GPIO_GPIO_H_
#include <gpiod.h>
#include <memory>
#include <string>
#include <utility>
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "gpio_config.pb.h"
namespace milotic_tlbmc {
namespace internal {
// Custom deleter for gpiod_line using gpiod_line_release.
struct GpioLineDeleter {
void operator()(gpiod_line* line) const {
if (line != nullptr) {
gpiod_line_release(line);
}
}
};
absl::StatusOr<int> ParseGpioRequestType(const GpioConfig& config);
} // namespace internal
class Gpio {
public:
// unique_ptr alias for gpiod_line.
using GpioLineUniquePtr =
std::unique_ptr<gpiod_line, internal::GpioLineDeleter>;
// Don't need to close the event fd in the destructor.
// It's covered in the gpiod_line_release when the unique_ptr of gpio_line
// is destructed.
~Gpio() = default;
// Gpio is a resource-managing class and should not be copied or moved.
Gpio(const Gpio&) = delete;
Gpio& operator=(const Gpio&) = delete;
Gpio(Gpio&&) = delete;
Gpio& operator=(Gpio&&) = delete;
absl::StatusOr<bool> GetValue() const;
// Set the GPIO line value. True to set the GPIO line to 1(HIGH), false to set
// the GPIO line to 0(LOW).
absl::Status SetValue(bool value);
// Get the GPIO name. Returns the line name if it is set, otherwise returns
// "<chip id>:<gpio num>".
std::string GetGpioName() const;
// Get the GPIO line event file descriptor. This is used to monitor the GPIO
// line.
int GetGpioLineEventFd() const { return event_fd_; };
static absl::StatusOr<std::unique_ptr<Gpio>> Create(const GpioConfig& config);
protected:
Gpio(const GpioConfig& config, GpioLineUniquePtr gpio_line, int event_fd)
: config_(config),
gpio_line_(std::move(gpio_line)),
event_fd_(event_fd) {}
private:
const GpioConfig config_;
// The GPIO line, managed by unique_ptr with a custom deleter.
GpioLineUniquePtr gpio_line_;
// The file descriptor of the GPIO line event. This is only used when the GPIO
// line is requested for event monitoring.
int event_fd_;
};
} // namespace milotic_tlbmc
#endif // THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_HAL_GPIO_GPIO_H_