blob: fb25a6f29638623dac00f1f7d4459b3f995b1a16 [file] [log] [blame]
Tomáš Peckaefd88432020-06-17 15:31:39 +02001/*
2 * Copyright (C) 2020 CESNET, https://photonics.cesnet.cz/
3 *
4 * Written by Tomáš Pecka <tomas.pecka@fit.cvut.cz>
5 *
6*/
7
8#include <fstream>
9#include "LedSysfsDriver.h"
Tomáš Pecka9cd349f2021-04-20 21:45:42 +020010#include "utils/io.h"
Tomáš Peckaefd88432020-06-17 15:31:39 +020011#include "utils/log.h"
12
Tomáš Pecka261c8862020-11-05 11:23:08 +010013namespace velia::health {
Tomáš Peckaefd88432020-06-17 15:31:39 +020014
15LedSysfsDriver::LedSysfsDriver(const std::filesystem::path& directory)
Tomáš Pecka7c6b1d32020-11-25 14:17:21 +010016 : m_log(spdlog::get("health"))
Tomáš Peckaefd88432020-06-17 15:31:39 +020017 , m_brightnessFile(directory / "brightness")
18{
19 // check that brightness file exists
20 if (!std::filesystem::exists(m_brightnessFile)) {
21 throw std::invalid_argument("Sysfs dir must contain 'brightness' file.");
22 }
23
Tomáš Pecka9cd349f2021-04-20 21:45:42 +020024 const auto maxBrightnessFile = directory / "max_brightness";
25 if (!std::filesystem::exists(maxBrightnessFile)) {
26 throw std::invalid_argument("Sysfs dir must contain 'max_brightness' file.");
27 }
28 m_maxBrightness = utils::readFileInt64(maxBrightnessFile);
29
Tomáš Peckaefd88432020-06-17 15:31:39 +020030 m_log->trace("Initialized LED {}", std::string(directory));
31}
32
33/** @brief Set the brightness of the LED to @p brightness.
34 * Caller is responsible for providing correct brightness value. No checks for valid value are performed.
35 */
36void LedSysfsDriver::set(uint32_t brightness)
37{
38 std::ofstream ofs(m_brightnessFile);
39 if (!(ofs << brightness)) {
40 throw std::invalid_argument("Write to '" + std::string(m_brightnessFile) + "' failed.");
41 }
42}
Tomáš Pecka9cd349f2021-04-20 21:45:42 +020043uint32_t LedSysfsDriver::maxBrightness() const
44{
45 return m_maxBrightness;
46}
Tomáš Pecka261c8862020-11-05 11:23:08 +010047}