blob: 2c2226639b1ff45148c05713e32e06cf91758730 [file]
#ifndef THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_HAL_LED_LED_GPIO_H_
#define THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_HAL_LED_LED_GPIO_H_
#include <memory>
#include <optional>
#include <string>
#include "absl/base/thread_annotations.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "tlbmc/collector/gpio_collector.h"
#include "led_config.pb.h"
#include "tlbmc/hal/led/led.h"
#include "tlbmc/scheduler/scheduler.h"
namespace milotic_tlbmc {
// LED backed by a GPIO line managed by GpioCollector.
//
// Translates high-level LedBehavior into GpioCollector calls:
// - LED_STATE_OFF: gpio_collector.SetGpio(gpio_name, false)
// - LED_STATE_ON: gpio_collector.SetGpio(gpio_name, true)
// - LED_STATE_BLINK: software blinking via TaskScheduler, toggling the GPIO
// line using gpio_collector.SetGpio(gpio_name, level).
//
// This class is thread-safe.
class GpioLed final : public Led {
public:
static absl::StatusOr<std::unique_ptr<GpioLed>> Create(
const LedConfig& config, GpioCollector& gpio_collector,
TaskScheduler* task_scheduler);
~GpioLed() override;
protected:
absl::Status SetBehaviorImpl(const LedBehavior& behavior)
ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_) override;
absl::StatusOr<LedBehavior> GetBehaviorImpl() const
ABSL_SHARED_LOCKS_REQUIRED(mutex_) override;
private:
GpioLed(const LedConfig& config, GpioCollector& gpio_collector,
TaskScheduler* task_scheduler)
: Led(config),
gpio_collector_(gpio_collector),
gpio_name_(config.output().gpio_name()),
task_scheduler_(task_scheduler) {}
private:
void StopBlinking() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
absl::Status StartBlinking(const BlinkConfig& blink_config)
ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
void BlinkPeriodicStep() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
void BlinkOneShotStep() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
GpioCollector& gpio_collector_;
const std::string gpio_name_;
TaskScheduler* const task_scheduler_;
int task_id_ ABSL_GUARDED_BY(mutex_) = -1;
bool current_level_ ABSL_GUARDED_BY(mutex_) = false;
std::optional<LedBehavior> behavior_ ABSL_GUARDED_BY(mutex_);
};
} // namespace milotic_tlbmc
#endif // THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_HAL_LED_LED_GPIO_H_