blob: fb25a6f29638623dac00f1f7d4459b3f995b1a16 [file] [log] [blame]
/*
* Copyright (C) 2020 CESNET, https://photonics.cesnet.cz/
*
* Written by Tomáš Pecka <tomas.pecka@fit.cvut.cz>
*
*/
#include <fstream>
#include "LedSysfsDriver.h"
#include "utils/io.h"
#include "utils/log.h"
namespace velia::health {
LedSysfsDriver::LedSysfsDriver(const std::filesystem::path& directory)
: m_log(spdlog::get("health"))
, m_brightnessFile(directory / "brightness")
{
// check that brightness file exists
if (!std::filesystem::exists(m_brightnessFile)) {
throw std::invalid_argument("Sysfs dir must contain 'brightness' file.");
}
const auto maxBrightnessFile = directory / "max_brightness";
if (!std::filesystem::exists(maxBrightnessFile)) {
throw std::invalid_argument("Sysfs dir must contain 'max_brightness' file.");
}
m_maxBrightness = utils::readFileInt64(maxBrightnessFile);
m_log->trace("Initialized LED {}", std::string(directory));
}
/** @brief Set the brightness of the LED to @p brightness.
* Caller is responsible for providing correct brightness value. No checks for valid value are performed.
*/
void LedSysfsDriver::set(uint32_t brightness)
{
std::ofstream ofs(m_brightnessFile);
if (!(ofs << brightness)) {
throw std::invalid_argument("Write to '" + std::string(m_brightnessFile) + "' failed.");
}
}
uint32_t LedSysfsDriver::maxBrightness() const
{
return m_maxBrightness;
}
}