1 #pragma once
2 
3 #include "sysfs.hpp"
4 
5 #include <sdbusplus/bus.hpp>
6 #include <sdbusplus/server/object.hpp>
7 #include <xyz/openbmc_project/Led/Physical/server.hpp>
8 
9 #include <fstream>
10 #include <string>
11 
12 namespace phosphor
13 {
14 namespace led
15 {
16 /** @brief De-assert value */
17 constexpr unsigned long DEASSERT = 0;
18 
19 namespace
20 {
21 using PhysicalIfaces = sdbusplus::server::object_t<
22     sdbusplus::xyz::openbmc_project::Led::server::Physical>;
23 }
24 
25 /** @class Physical
26  *  @brief Responsible for applying actions on a particular physical LED
27  */
28 class Physical : public PhysicalIfaces
29 {
30   public:
31     Physical() = delete;
32     ~Physical() = default;
33     Physical(const Physical&) = delete;
34     Physical& operator=(const Physical&) = delete;
35     Physical(Physical&&) = delete;
36     Physical& operator=(Physical&&) = delete;
37 
38     /** @brief Constructs LED object. Argument 'true' says that we hold off
39      *   from sending the signals since we need to do some house keeping and
40      *   only when we finish that, we are considered active and can then
41      *   broadcast the signal.
42      *
43      * @param[in] bus       - system dbus handler
44      * @param[in] objPath   - The Dbus path that hosts physical LED
45      * @param[in] ledPath   - sysfs path where this LED is exported
46      * @param[in] color     - led color name
47      */
48     Physical(sdbusplus::bus_t& bus, const std::string& objPath, SysfsLed& led,
49              const std::string& color = "") :
50         PhysicalIfaces(bus, objPath.c_str(),
51                        PhysicalIfaces::action::defer_emit),
52         led(led)
53     {
54         // Suppose this is getting launched as part of BMC reboot, then we
55         // need to save what the micro-controller currently has.
56         setInitialState();
57 
58         // Read led color from enviroment and set it in DBus.
59         setLedColor(color);
60 
61         // We are now ready.
62         emit_object_added();
63     }
64 
65     /** @brief Overloaded State Property Setter function
66      *
67      *  @param[in] value   -  One of OFF / ON / BLINK
68      *  @return            -  Success or exception thrown
69      */
70     Action state(Action value) override;
71 
72     /** @brief Overriden State Property Getter function
73      *
74      *  @return  -  One of OFF / ON / BLINK
75      */
76     Action state() const override;
77 
78   private:
79     /** @brief Associated LED implementation
80      */
81     SysfsLed& led;
82 
83     /** @brief The value that will assert the LED */
84     unsigned long assert;
85 
86     /** @brief reads sysfs and then setsup the parameteres accordingly
87      *
88      *  @return None
89      */
90     void setInitialState();
91 
92     /** @brief Applies the user triggered action on the LED
93      *   by writing to sysfs
94      *
95      *  @param [in] current - Current state of LED
96      *  @param [in] request - Requested state
97      *
98      *  @return None
99      */
100     void driveLED(Action current, Action request);
101 
102     /** @brief Sets the LED to either ON or OFF state
103      *
104      *  @param [in] action - Requested action. Could be OFF or ON
105      *  @return None
106      */
107     void stableStateOperation(Action action);
108 
109     /** @brief Sets the LED to BLINKING
110      *
111      *  @return None
112      */
113     void blinkOperation();
114 
115     /** @brief set led color property in DBus
116      *
117      *  @param[in] color - led color name
118      */
119     void setLedColor(const std::string& color);
120 };
121 
122 } // namespace led
123 } // namespace phosphor
124