xref: /openbmc/phosphor-led-sysfs/sysfs.hpp (revision 24d124f9)
1 /**
2  * Copyright © 2019 IBM Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 #include <filesystem>
19 #include <optional>
20 
21 static constexpr auto devParent = "/sys/class/leds/";
22 
23 namespace phosphor
24 {
25 namespace led
26 {
27 
28 struct LedDescr
29 {
30     // at least one of the members shall be non-empty
31     // after initialization
32 
33     std::optional<std::string> devicename;
34     std::optional<std::string> color;
35     std::optional<std::string> function;
36 };
37 
38 class SysfsLed
39 {
40   public:
SysfsLed(std::filesystem::path && root)41     explicit SysfsLed(std::filesystem::path&& root) : root(std::move(root)) {}
42     SysfsLed() = delete;
43     SysfsLed(const SysfsLed& other) = delete;
44     SysfsLed(const SysfsLed&& other) = delete;
45     SysfsLed& operator=(const SysfsLed& other) = delete;
46     SysfsLed&& operator=(const SysfsLed&& other) = delete;
47 
48     virtual ~SysfsLed() = default;
49 
50     virtual unsigned long getBrightness();
51     virtual void setBrightness(unsigned long brightness);
52     virtual unsigned long getMaxBrightness();
53     virtual std::string getTrigger();
54     virtual void setTrigger(const std::string& trigger);
55     virtual unsigned long getDelayOn();
56     virtual void setDelayOn(unsigned long ms);
57     virtual unsigned long getDelayOff();
58     virtual void setDelayOff(unsigned long ms);
59 
60     /** @brief parse LED name in sysfs
61      *  Parse sysfs LED name and sets corresponding
62      *  fields in LedDescr struct.
63      */
64     LedDescr getLedDescr();
65 
66   protected:
67     static constexpr const char* attrBrightness = "brightness";
68     static constexpr const char* attrMaxBrightness = "max_brightness";
69     static constexpr const char* attrTrigger = "trigger";
70     static constexpr const char* attrDelayOn = "delay_on";
71     static constexpr const char* attrDelayOff = "delay_off";
72 
73     std::filesystem::path root;
74 };
75 } // namespace led
76 } // namespace phosphor
77