blob: 8580178819c9f47a7b2eb2cceacae966493e7f27 [file]
#ifndef THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_HAL_GPIO_GPIO_MONITOR_H_
#define THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_HAL_GPIO_GPIO_MONITOR_H_
#include <memory>
#include <string>
#include <vector>
#include "gbmc-hal/api/system/transport/gpio.h"
#include "absl/functional/any_invocable.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "boost/asio/io_context.hpp" // NOLINT: boost is commonly used in BMC codebase
#include "boost/asio/posix/stream_descriptor.hpp" // NOLINT: boost is commonly used in BMC codebase
#include "gpio_config.pb.h"
#include "tlbmc/hal/gpio/gpio.h"
namespace milotic_tlbmc {
// A single instance of GpioMonitor is created for each GPIO line.
class GpioMonitor : public platforms::gbmc::hal::Gpio {
public:
static absl::StatusOr<std::unique_ptr<GpioMonitor>> Create(
const GpioConfig& gpio_config,
std::shared_ptr<boost::asio::io_context> io_context);
~GpioMonitor() override;
// GpioMonitor is a resource-managing class and should not be copied or moved.
GpioMonitor(const GpioMonitor&) = delete;
GpioMonitor& operator=(const GpioMonitor&) = delete;
GpioMonitor(GpioMonitor&&) = delete;
GpioMonitor& operator=(GpioMonitor&&) = delete;
std::string GetName() const override { return gpio_->GetName(); }
absl::StatusOr<bool> GetValue() const override { return gpio_->GetValue(); }
absl::Status SetValue(bool value) override { return gpio_->SetValue(value); }
absl::StatusOr<Direction> GetDirection() const override {
return gpio_->GetDirection();
}
absl::StatusOr<Polarity> GetPolarity() const override {
return gpio_->GetPolarity();
}
void RegisterCallback(
absl::AnyInvocable<void(GpioEventType event)>&& callback);
protected:
void ScheduleEventHandler();
virtual void EventHandler();
private:
GpioMonitor(
std::unique_ptr<milotic_tlbmc::Gpio> gpio, bool is_one_time_request,
std::shared_ptr<boost::asio::io_context> io_context,
std::unique_ptr<boost::asio::posix::stream_descriptor> event_descriptor);
std::unique_ptr<milotic_tlbmc::Gpio> gpio_;
bool is_one_time_request_;
std::shared_ptr<boost::asio::io_context> io_context_;
std::unique_ptr<boost::asio::posix::stream_descriptor> event_descriptor_;
std::vector<absl::AnyInvocable<void(GpioEventType event)>> callbacks_;
};
} // namespace milotic_tlbmc
#endif // THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_HAL_GPIO_GPIO_MONITOR_H_