1bda97eb1SVishwanatha Subbanna #pragma once
2bda97eb1SVishwanatha Subbanna 
342e02d34SAndrew Jeffery #include "sysfs.hpp"
442e02d34SAndrew Jeffery 
5bda97eb1SVishwanatha Subbanna #include <sdbusplus/bus.hpp>
6bda97eb1SVishwanatha Subbanna #include <sdbusplus/server/object.hpp>
7e089173fSVishwanatha Subbanna #include <xyz/openbmc_project/Led/Physical/server.hpp>
842e02d34SAndrew Jeffery 
961b90636SGeorge Liu #include <fstream>
1061b90636SGeorge Liu #include <string>
1161b90636SGeorge Liu 
12*e48bf95dSJayashree Dhanapal namespace fs = std::filesystem;
13*e48bf95dSJayashree Dhanapal 
14bda97eb1SVishwanatha Subbanna namespace phosphor
15bda97eb1SVishwanatha Subbanna {
16bda97eb1SVishwanatha Subbanna namespace led
17bda97eb1SVishwanatha Subbanna {
185b1417bdSAndrew Jeffery /** @brief De-assert value */
198e85228cSAndrew Jeffery constexpr unsigned long deasserted = 0;
20bda97eb1SVishwanatha Subbanna 
2197db22fdSPatrick Williams using PhysicalIfaces = sdbusplus::server::object_t<
2297db22fdSPatrick Williams     sdbusplus::xyz::openbmc_project::Led::server::Physical>;
2397db22fdSPatrick Williams 
24bda97eb1SVishwanatha Subbanna /** @class Physical
25bda97eb1SVishwanatha Subbanna  *  @brief Responsible for applying actions on a particular physical LED
26bda97eb1SVishwanatha Subbanna  */
2797db22fdSPatrick Williams class Physical : public PhysicalIfaces
28bda97eb1SVishwanatha Subbanna {
29bda97eb1SVishwanatha Subbanna   public:
30bda97eb1SVishwanatha Subbanna     Physical() = delete;
31c060c287SAndrew Jeffery     ~Physical() override = default;
32bda97eb1SVishwanatha Subbanna     Physical(const Physical&) = delete;
33bda97eb1SVishwanatha Subbanna     Physical& operator=(const Physical&) = delete;
34e089173fSVishwanatha Subbanna     Physical(Physical&&) = delete;
35e089173fSVishwanatha Subbanna     Physical& operator=(Physical&&) = delete;
36bda97eb1SVishwanatha Subbanna 
3775b5510fSVishwanatha Subbanna     /** @brief Constructs LED object. Argument 'true' says that we hold off
3875b5510fSVishwanatha Subbanna      *   from sending the signals since we need to do some house keeping and
3975b5510fSVishwanatha Subbanna      *   only when we finish that, we are considered active and can then
4075b5510fSVishwanatha Subbanna      *   broadcast the signal.
41bda97eb1SVishwanatha Subbanna      *
42bda97eb1SVishwanatha Subbanna      * @param[in] bus       - system dbus handler
43bda97eb1SVishwanatha Subbanna      * @param[in] objPath   - The Dbus path that hosts physical LED
44bda97eb1SVishwanatha Subbanna      * @param[in] ledPath   - sysfs path where this LED is exported
4597ddb723SAlexander Soldatov      * @param[in] color     - led color name
46bda97eb1SVishwanatha Subbanna      */
47*e48bf95dSJayashree Dhanapal 
Physical(sdbusplus::bus_t & bus,const std::string & objPath,std::unique_ptr<phosphor::led::SysfsLed> led,const std::string & color="")48*e48bf95dSJayashree Dhanapal     Physical(sdbusplus::bus_t& bus, const std::string& objPath,
49*e48bf95dSJayashree Dhanapal              std::unique_ptr<phosphor::led::SysfsLed> led,
50ff3d538bSPatrick Williams              const std::string& color = "") :
5197db22fdSPatrick Williams         PhysicalIfaces(bus, objPath.c_str(),
5297db22fdSPatrick Williams                        PhysicalIfaces::action::defer_emit),
53*e48bf95dSJayashree Dhanapal         led(std::move(led))
54bda97eb1SVishwanatha Subbanna     {
5575b5510fSVishwanatha Subbanna         // Suppose this is getting launched as part of BMC reboot, then we
5675b5510fSVishwanatha Subbanna         // need to save what the micro-controller currently has.
5775b5510fSVishwanatha Subbanna         setInitialState();
5875b5510fSVishwanatha Subbanna 
5929bd56b1SManojkiran Eda         // Read led color from environment and set it in DBus.
6097ddb723SAlexander Soldatov         setLedColor(color);
6197ddb723SAlexander Soldatov 
6275b5510fSVishwanatha Subbanna         // We are now ready.
6375b5510fSVishwanatha Subbanna         emit_object_added();
64bda97eb1SVishwanatha Subbanna     }
65bda97eb1SVishwanatha Subbanna 
6675b5510fSVishwanatha Subbanna     /** @brief Overloaded State Property Setter function
6775b5510fSVishwanatha Subbanna      *
6875b5510fSVishwanatha Subbanna      *  @param[in] value   -  One of OFF / ON / BLINK
6975b5510fSVishwanatha Subbanna      *  @return            -  Success or exception thrown
7075b5510fSVishwanatha Subbanna      */
7175b5510fSVishwanatha Subbanna     Action state(Action value) override;
7275b5510fSVishwanatha Subbanna 
7329bd56b1SManojkiran Eda     /** @brief Overridden State Property Getter function
74db21bc00SVishwanatha Subbanna      *
75db21bc00SVishwanatha Subbanna      *  @return  -  One of OFF / ON / BLINK
76db21bc00SVishwanatha Subbanna      */
77db21bc00SVishwanatha Subbanna     Action state() const override;
78db21bc00SVishwanatha Subbanna 
79bda97eb1SVishwanatha Subbanna   private:
8042e02d34SAndrew Jeffery     /** @brief Associated LED implementation
81bda97eb1SVishwanatha Subbanna      */
82*e48bf95dSJayashree Dhanapal     std::unique_ptr<phosphor::led::SysfsLed> led;
8375b5510fSVishwanatha Subbanna 
845b1417bdSAndrew Jeffery     /** @brief The value that will assert the LED */
85478d0a71SAndrew Jeffery     unsigned long assert{};
865b1417bdSAndrew Jeffery 
8729bd56b1SManojkiran Eda     /** @brief reads sysfs and then setup the parameters accordingly
8875b5510fSVishwanatha Subbanna      *
8961675c32SVishwanatha Subbanna      *  @return None
9075b5510fSVishwanatha Subbanna      */
9161675c32SVishwanatha Subbanna     void setInitialState();
9261675c32SVishwanatha Subbanna 
9361675c32SVishwanatha Subbanna     /** @brief Applies the user triggered action on the LED
9461675c32SVishwanatha Subbanna      *   by writing to sysfs
9561675c32SVishwanatha Subbanna      *
9661675c32SVishwanatha Subbanna      *  @param [in] current - Current state of LED
9761675c32SVishwanatha Subbanna      *  @param [in] request - Requested state
9861675c32SVishwanatha Subbanna      *
9961675c32SVishwanatha Subbanna      *  @return None
10061675c32SVishwanatha Subbanna      */
10161675c32SVishwanatha Subbanna     void driveLED(Action current, Action request);
10261675c32SVishwanatha Subbanna 
10361675c32SVishwanatha Subbanna     /** @brief Sets the LED to either ON or OFF state
10461675c32SVishwanatha Subbanna      *
10561675c32SVishwanatha Subbanna      *  @param [in] action - Requested action. Could be OFF or ON
10661675c32SVishwanatha Subbanna      *  @return None
10761675c32SVishwanatha Subbanna      */
10861675c32SVishwanatha Subbanna     void stableStateOperation(Action action);
10961675c32SVishwanatha Subbanna 
11061675c32SVishwanatha Subbanna     /** @brief Sets the LED to BLINKING
11161675c32SVishwanatha Subbanna      *
11261675c32SVishwanatha Subbanna      *  @return None
11361675c32SVishwanatha Subbanna      */
11461675c32SVishwanatha Subbanna     void blinkOperation();
11597ddb723SAlexander Soldatov 
11697ddb723SAlexander Soldatov     /** @brief set led color property in DBus
11797ddb723SAlexander Soldatov      *
11897ddb723SAlexander Soldatov      *  @param[in] color - led color name
11997ddb723SAlexander Soldatov      */
12097ddb723SAlexander Soldatov     void setLedColor(const std::string& color);
121bda97eb1SVishwanatha Subbanna };
122bda97eb1SVishwanatha Subbanna 
123bda97eb1SVishwanatha Subbanna } // namespace led
124c41bf5b7SAndrew Jeffery } // namespace phosphor
125