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