1bda97eb1SVishwanatha Subbanna #pragma once 2bda97eb1SVishwanatha Subbanna 3bda97eb1SVishwanatha Subbanna #include <string> 461675c32SVishwanatha Subbanna #include <fstream> 5bda97eb1SVishwanatha Subbanna #include <sdbusplus/bus.hpp> 6bda97eb1SVishwanatha Subbanna #include <sdbusplus/server/object.hpp> 7*e089173fSVishwanatha Subbanna #include <xyz/openbmc_project/Led/Physical/server.hpp> 8bda97eb1SVishwanatha Subbanna namespace phosphor 9bda97eb1SVishwanatha Subbanna { 10bda97eb1SVishwanatha Subbanna namespace led 11bda97eb1SVishwanatha Subbanna { 1261675c32SVishwanatha Subbanna /** @brief Acts as input and output file for the current LED power state. 1361675c32SVishwanatha Subbanna * Write "0" to trigger a LED OFF operation. 1461675c32SVishwanatha Subbanna * Write "255" to trigger a LED ON operation. 1561675c32SVishwanatha Subbanna * To know the current power state of the LED, a read on this 1661675c32SVishwanatha Subbanna * would give either 0 or 255 indicating if the LED is currently 1761675c32SVishwanatha Subbanna * Off / On AT THAT moment. 1861675c32SVishwanatha Subbanna * Example: /sys/class/leds/myled/brightness 1961675c32SVishwanatha Subbanna */ 2061675c32SVishwanatha Subbanna constexpr auto BRIGHTNESS = "/brightness"; 2161675c32SVishwanatha Subbanna 2261675c32SVishwanatha Subbanna /** @brief Assert LED by writing 255 */ 2361675c32SVishwanatha Subbanna constexpr auto ASSERT = "255"; 2461675c32SVishwanatha Subbanna 2561675c32SVishwanatha Subbanna /** @brief De-assert by writing "0" */ 2661675c32SVishwanatha Subbanna constexpr auto DEASSERT = "0"; 2761675c32SVishwanatha Subbanna 2861675c32SVishwanatha Subbanna /** @brief Write "timer" to this file telling the driver that 2961675c32SVishwanatha Subbanna * the intended operation is BLINK. When the value "timer" is written to 3061675c32SVishwanatha Subbanna * the file, 2 more files get auto generated and are named "delay_on" and 3161675c32SVishwanatha Subbanna * "delay_off" 3261675c32SVishwanatha Subbanna * To move away from blinking, write "none" 3361675c32SVishwanatha Subbanna * Example: /sys/class/leds/myled/trigger 3461675c32SVishwanatha Subbanna */ 3561675c32SVishwanatha Subbanna constexpr auto BLINKCTRL = "/trigger"; 3661675c32SVishwanatha Subbanna 3761675c32SVishwanatha Subbanna /** @brief write number of milliseconds that the LED needs to be ON 3861675c32SVishwanatha Subbanna * while blinking. Default is 500 by the driver. 3961675c32SVishwanatha Subbanna * Example: /sys/class/leds/myled/delay_on 4061675c32SVishwanatha Subbanna */ 4161675c32SVishwanatha Subbanna constexpr auto DELAYON = "/delay_on"; 4261675c32SVishwanatha Subbanna 4361675c32SVishwanatha Subbanna /** @brief Write number of milliseconds that the LED needs to be OFF 4461675c32SVishwanatha Subbanna * while blinking. Default is 500 by the driver. 4561675c32SVishwanatha Subbanna * Example: /sys/class/leds/myled/delay_off 4661675c32SVishwanatha Subbanna */ 4761675c32SVishwanatha Subbanna constexpr auto DELAYOFF = "/delay_off"; 48bda97eb1SVishwanatha Subbanna 49bda97eb1SVishwanatha Subbanna /** @class Physical 50bda97eb1SVishwanatha Subbanna * @brief Responsible for applying actions on a particular physical LED 51bda97eb1SVishwanatha Subbanna */ 52bda97eb1SVishwanatha Subbanna class Physical : public sdbusplus::server::object::object< 53bda97eb1SVishwanatha Subbanna sdbusplus::xyz::openbmc_project::Led::server::Physical> 54bda97eb1SVishwanatha Subbanna { 55bda97eb1SVishwanatha Subbanna public: 56bda97eb1SVishwanatha Subbanna Physical() = delete; 57bda97eb1SVishwanatha Subbanna ~Physical() = default; 58bda97eb1SVishwanatha Subbanna Physical(const Physical&) = delete; 59bda97eb1SVishwanatha Subbanna Physical& operator=(const Physical&) = delete; 60*e089173fSVishwanatha Subbanna Physical(Physical&&) = delete; 61*e089173fSVishwanatha Subbanna Physical& operator=(Physical&&) = delete; 62bda97eb1SVishwanatha Subbanna 6375b5510fSVishwanatha Subbanna /** @brief Constructs LED object. Argument 'true' says that we hold off 6475b5510fSVishwanatha Subbanna * from sending the signals since we need to do some house keeping and 6575b5510fSVishwanatha Subbanna * only when we finish that, we are considered active and can then 6675b5510fSVishwanatha Subbanna * broadcast the signal. 67bda97eb1SVishwanatha Subbanna * 68bda97eb1SVishwanatha Subbanna * @param[in] bus - system dbus handler 69bda97eb1SVishwanatha Subbanna * @param[in] objPath - The Dbus path that hosts physical LED 70bda97eb1SVishwanatha Subbanna * @param[in] ledPath - sysfs path where this LED is exported 71bda97eb1SVishwanatha Subbanna */ 72bda97eb1SVishwanatha Subbanna Physical(sdbusplus::bus::bus& bus, 73bda97eb1SVishwanatha Subbanna const std::string& objPath, 74bda97eb1SVishwanatha Subbanna const std::string& ledPath) : 75bda97eb1SVishwanatha Subbanna 76bda97eb1SVishwanatha Subbanna sdbusplus::server::object::object< 77bda97eb1SVishwanatha Subbanna sdbusplus::xyz::openbmc_project::Led::server::Physical>( 7875b5510fSVishwanatha Subbanna bus, objPath.c_str(), true), 79bda97eb1SVishwanatha Subbanna path(ledPath) 80bda97eb1SVishwanatha Subbanna { 8175b5510fSVishwanatha Subbanna // Suppose this is getting launched as part of BMC reboot, then we 8275b5510fSVishwanatha Subbanna // need to save what the micro-controller currently has. 8375b5510fSVishwanatha Subbanna setInitialState(); 8475b5510fSVishwanatha Subbanna 8575b5510fSVishwanatha Subbanna // We are now ready. 8675b5510fSVishwanatha Subbanna emit_object_added(); 87bda97eb1SVishwanatha Subbanna } 88bda97eb1SVishwanatha Subbanna 8975b5510fSVishwanatha Subbanna /** @brief Overloaded State Property Setter function 9075b5510fSVishwanatha Subbanna * 9175b5510fSVishwanatha Subbanna * @param[in] value - One of OFF / ON / BLINK 9275b5510fSVishwanatha Subbanna * @return - Success or exception thrown 9375b5510fSVishwanatha Subbanna */ 9475b5510fSVishwanatha Subbanna Action state(Action value) override; 9575b5510fSVishwanatha Subbanna 96bda97eb1SVishwanatha Subbanna private: 97bda97eb1SVishwanatha Subbanna /** @brief File system location where this LED is exposed 98bda97eb1SVishwanatha Subbanna * Typically /sys/class/leds/<Led-Name> 99bda97eb1SVishwanatha Subbanna */ 100bda97eb1SVishwanatha Subbanna std::string path; 10175b5510fSVishwanatha Subbanna 10261675c32SVishwanatha Subbanna /** @brief Frequency range that the LED can operate on. 10361675c32SVishwanatha Subbanna * Will be removed when frequency is put into interface 10475b5510fSVishwanatha Subbanna */ 10561675c32SVishwanatha Subbanna uint32_t frequency; 10661675c32SVishwanatha Subbanna 10761675c32SVishwanatha Subbanna /** @brief Brightness described above */ 10861675c32SVishwanatha Subbanna std::string brightCtrl; 10961675c32SVishwanatha Subbanna 11061675c32SVishwanatha Subbanna /** @brief BlinkCtrl described above */ 11161675c32SVishwanatha Subbanna std::string blinkCtrl; 11261675c32SVishwanatha Subbanna 11361675c32SVishwanatha Subbanna /** @brief delay_on described above */ 11461675c32SVishwanatha Subbanna std::string delayOnCtrl; 11561675c32SVishwanatha Subbanna 11661675c32SVishwanatha Subbanna /** @brief delay_ff described above */ 11761675c32SVishwanatha Subbanna std::string delayOffCtrl; 11875b5510fSVishwanatha Subbanna 11975b5510fSVishwanatha Subbanna /** @brief reads sysfs and then setsup the parameteres accordingly 12075b5510fSVishwanatha Subbanna * 12161675c32SVishwanatha Subbanna * @return None 12275b5510fSVishwanatha Subbanna */ 12361675c32SVishwanatha Subbanna void setInitialState(); 12461675c32SVishwanatha Subbanna 12561675c32SVishwanatha Subbanna /** @brief Applies the user triggered action on the LED 12661675c32SVishwanatha Subbanna * by writing to sysfs 12761675c32SVishwanatha Subbanna * 12861675c32SVishwanatha Subbanna * @param [in] current - Current state of LED 12961675c32SVishwanatha Subbanna * @param [in] request - Requested state 13061675c32SVishwanatha Subbanna * 13161675c32SVishwanatha Subbanna * @return None 13261675c32SVishwanatha Subbanna */ 13361675c32SVishwanatha Subbanna void driveLED(Action current, Action request); 13461675c32SVishwanatha Subbanna 13561675c32SVishwanatha Subbanna /** @brief Sets the LED to either ON or OFF state 13661675c32SVishwanatha Subbanna * 13761675c32SVishwanatha Subbanna * @param [in] action - Requested action. Could be OFF or ON 13861675c32SVishwanatha Subbanna * @return None 13961675c32SVishwanatha Subbanna */ 14061675c32SVishwanatha Subbanna void stableStateOperation(Action action); 14161675c32SVishwanatha Subbanna 14261675c32SVishwanatha Subbanna /** @brief Sets the LED to BLINKING 14361675c32SVishwanatha Subbanna * 14461675c32SVishwanatha Subbanna * @return None 14561675c32SVishwanatha Subbanna */ 14661675c32SVishwanatha Subbanna void blinkOperation(); 14761675c32SVishwanatha Subbanna 14861675c32SVishwanatha Subbanna /** @brief Generic file writer. 14961675c32SVishwanatha Subbanna * There are files like "brightness", "trigger" , "delay_on" and 15061675c32SVishwanatha Subbanna * "delay_off" that will tell what the LED driver needs to do. 15161675c32SVishwanatha Subbanna * 15261675c32SVishwanatha Subbanna * @param[in] filename - Name of file to be written 15361675c32SVishwanatha Subbanna * @param[in] data - Data to be written to 15461675c32SVishwanatha Subbanna * @return - None 15561675c32SVishwanatha Subbanna */ 15661675c32SVishwanatha Subbanna template <typename T> 15761675c32SVishwanatha Subbanna auto write(const std::string& fileName, T&& data) 15861675c32SVishwanatha Subbanna { 15961675c32SVishwanatha Subbanna if(std::ifstream(fileName)) 16061675c32SVishwanatha Subbanna { 16161675c32SVishwanatha Subbanna std::ofstream file(fileName, std::ios::out); 16261675c32SVishwanatha Subbanna file << data; 16361675c32SVishwanatha Subbanna file.close(); 16461675c32SVishwanatha Subbanna } 16561675c32SVishwanatha Subbanna return; 16661675c32SVishwanatha Subbanna } 16761675c32SVishwanatha Subbanna 16861675c32SVishwanatha Subbanna /** @brief Generic file reader. 16961675c32SVishwanatha Subbanna * There are files like "brightness", "trigger" , "delay_on" and 17061675c32SVishwanatha Subbanna * "delay_off" that will tell what the LED driver needs to do. 17161675c32SVishwanatha Subbanna * 17261675c32SVishwanatha Subbanna * @param[in] filename - Name of file to be read 17361675c32SVishwanatha Subbanna * @return - File content 17461675c32SVishwanatha Subbanna */ 17561675c32SVishwanatha Subbanna template <typename T> 17661675c32SVishwanatha Subbanna T read(const std::string& fileName) 17761675c32SVishwanatha Subbanna { 17861675c32SVishwanatha Subbanna T data = T(); 17961675c32SVishwanatha Subbanna if(std::ifstream(fileName)) 18061675c32SVishwanatha Subbanna { 18161675c32SVishwanatha Subbanna std::ifstream file(fileName, std::ios::in); 18261675c32SVishwanatha Subbanna file >> data; 18361675c32SVishwanatha Subbanna file.close(); 18461675c32SVishwanatha Subbanna } 18561675c32SVishwanatha Subbanna return data; 18661675c32SVishwanatha Subbanna } 187bda97eb1SVishwanatha Subbanna }; 188bda97eb1SVishwanatha Subbanna 189bda97eb1SVishwanatha Subbanna } // namespace led 19075b5510fSVishwanatha Subbanna } 191