blob: d7f4405dd43568572764995f93015f7b3bcb089f [file]
#ifndef THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_HAL_SYSFS_LED_H_
#define THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_HAL_SYSFS_LED_H_
#include <cstdint>
#include <string>
#include <vector>
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
namespace milotic_tlbmc {
struct LedSysfsConfig {
std::string led_sysfs_path = "/sys/class/leds/";
};
// An abstraction for interacting with the Linux LED sysfs subsystem.
//
// The Linux LED subsystem exposes LEDs at /sys/class/leds/<led_name>/ with
// the following key attributes:
// - brightness: Current brightness level (0 to max_brightness).
// - max_brightness: Maximum brightness value supported by the LED.
// - trigger: The trigger source that controls the LED behavior
// (e.g., "none", "timer", "heartbeat", "default-on").
// - delay_on: Duration in milliseconds the LED stays on during
// blinking (only available when trigger is "timer").
// - delay_off: Duration in milliseconds the LED stays off during
// blinking (only available when trigger is "timer").
//
// The class is thread-safe. Thread safety is mostly guaranteed by the
// underlying sysfs filesystem.
class LedSysfs {
public:
// Default constructor for unit tests.
LedSysfs() : LedSysfs(LedSysfsConfig()) {}
explicit LedSysfs(const LedSysfsConfig& config) : config_(config) {}
virtual ~LedSysfs() = default;
// Returns the current brightness of the LED.
virtual absl::StatusOr<uint32_t> GetBrightness(
absl::string_view led_name) const;
// Sets the brightness of the LED to the given value.
// The value must be in the range [0, max_brightness].
virtual absl::Status SetBrightness(absl::string_view led_name,
uint32_t brightness) const;
// Returns the maximum brightness value supported by the LED.
virtual absl::StatusOr<uint32_t> GetMaxBrightness(
absl::string_view led_name) const;
// Returns the current trigger of the LED.
// The active trigger is enclosed in brackets in the sysfs trigger file,
// e.g., "none [timer] heartbeat default-on".
virtual absl::StatusOr<std::string> GetTrigger(
absl::string_view led_name) const;
// Returns the list of available triggers for the LED.
virtual absl::StatusOr<std::vector<std::string>> GetAvailableTriggers(
absl::string_view led_name) const;
// Sets the trigger of the LED to the given value.
virtual absl::Status SetTrigger(absl::string_view led_name,
absl::string_view trigger) const;
// Returns the delay_on value in milliseconds (timer trigger only).
virtual absl::StatusOr<uint32_t> GetDelayOn(absl::string_view led_name) const;
// Sets the delay_on value in milliseconds (timer trigger only).
virtual absl::Status SetDelayOn(absl::string_view led_name,
uint32_t delay_ms) const;
// Returns the delay_off value in milliseconds (timer trigger only).
virtual absl::StatusOr<uint32_t> GetDelayOff(
absl::string_view led_name) const;
// Sets the delay_off value in milliseconds (timer trigger only).
virtual absl::Status SetDelayOff(absl::string_view led_name,
uint32_t delay_ms) const;
// Returns true if the LED exists in sysfs.
virtual bool IsLedPresent(absl::string_view led_name) const;
std::string GetLedSysfsPath() const { return config_.led_sysfs_path; }
private:
const LedSysfsConfig config_;
};
} // namespace milotic_tlbmc
#endif // THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_HAL_SYSFS_LED_H_