Vishwanatha Subbanna | bda97eb | 2016-11-30 12:21:25 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <string> |
Vishwanatha Subbanna | 61675c3 | 2016-11-30 15:52:15 +0530 | [diff] [blame] | 4 | #include <fstream> |
Vishwanatha Subbanna | bda97eb | 2016-11-30 12:21:25 +0530 | [diff] [blame] | 5 | #include <sdbusplus/bus.hpp> |
| 6 | #include <sdbusplus/server/object.hpp> |
Vishwanatha Subbanna | e089173 | 2017-03-10 15:27:23 +0530 | [diff] [blame] | 7 | #include <xyz/openbmc_project/Led/Physical/server.hpp> |
Vishwanatha Subbanna | bda97eb | 2016-11-30 12:21:25 +0530 | [diff] [blame] | 8 | namespace phosphor |
| 9 | { |
| 10 | namespace led |
| 11 | { |
Vishwanatha Subbanna | 61675c3 | 2016-11-30 15:52:15 +0530 | [diff] [blame] | 12 | /** @brief Acts as input and output file for the current LED power state. |
| 13 | * Write "0" to trigger a LED OFF operation. |
| 14 | * Write "255" to trigger a LED ON operation. |
| 15 | * To know the current power state of the LED, a read on this |
| 16 | * would give either 0 or 255 indicating if the LED is currently |
| 17 | * Off / On AT THAT moment. |
| 18 | * Example: /sys/class/leds/myled/brightness |
| 19 | */ |
| 20 | constexpr auto BRIGHTNESS = "/brightness"; |
| 21 | |
| 22 | /** @brief Assert LED by writing 255 */ |
| 23 | constexpr auto ASSERT = "255"; |
| 24 | |
| 25 | /** @brief De-assert by writing "0" */ |
| 26 | constexpr auto DEASSERT = "0"; |
| 27 | |
| 28 | /** @brief Write "timer" to this file telling the driver that |
| 29 | * the intended operation is BLINK. When the value "timer" is written to |
| 30 | * the file, 2 more files get auto generated and are named "delay_on" and |
| 31 | * "delay_off" |
| 32 | * To move away from blinking, write "none" |
| 33 | * Example: /sys/class/leds/myled/trigger |
| 34 | */ |
| 35 | constexpr auto BLINKCTRL = "/trigger"; |
| 36 | |
| 37 | /** @brief write number of milliseconds that the LED needs to be ON |
| 38 | * while blinking. Default is 500 by the driver. |
| 39 | * Example: /sys/class/leds/myled/delay_on |
| 40 | */ |
| 41 | constexpr auto DELAYON = "/delay_on"; |
| 42 | |
| 43 | /** @brief Write number of milliseconds that the LED needs to be OFF |
| 44 | * while blinking. Default is 500 by the driver. |
| 45 | * Example: /sys/class/leds/myled/delay_off |
| 46 | */ |
| 47 | constexpr auto DELAYOFF = "/delay_off"; |
Vishwanatha Subbanna | bda97eb | 2016-11-30 12:21:25 +0530 | [diff] [blame] | 48 | |
| 49 | /** @class Physical |
| 50 | * @brief Responsible for applying actions on a particular physical LED |
| 51 | */ |
| 52 | class Physical : public sdbusplus::server::object::object< |
| 53 | sdbusplus::xyz::openbmc_project::Led::server::Physical> |
| 54 | { |
| 55 | public: |
| 56 | Physical() = delete; |
| 57 | ~Physical() = default; |
| 58 | Physical(const Physical&) = delete; |
| 59 | Physical& operator=(const Physical&) = delete; |
Vishwanatha Subbanna | e089173 | 2017-03-10 15:27:23 +0530 | [diff] [blame] | 60 | Physical(Physical&&) = delete; |
| 61 | Physical& operator=(Physical&&) = delete; |
Vishwanatha Subbanna | bda97eb | 2016-11-30 12:21:25 +0530 | [diff] [blame] | 62 | |
Vishwanatha Subbanna | 75b5510 | 2016-11-30 14:20:53 +0530 | [diff] [blame] | 63 | /** @brief Constructs LED object. Argument 'true' says that we hold off |
| 64 | * from sending the signals since we need to do some house keeping and |
| 65 | * only when we finish that, we are considered active and can then |
| 66 | * broadcast the signal. |
Vishwanatha Subbanna | bda97eb | 2016-11-30 12:21:25 +0530 | [diff] [blame] | 67 | * |
| 68 | * @param[in] bus - system dbus handler |
| 69 | * @param[in] objPath - The Dbus path that hosts physical LED |
| 70 | * @param[in] ledPath - sysfs path where this LED is exported |
| 71 | */ |
| 72 | Physical(sdbusplus::bus::bus& bus, |
| 73 | const std::string& objPath, |
| 74 | const std::string& ledPath) : |
| 75 | |
| 76 | sdbusplus::server::object::object< |
| 77 | sdbusplus::xyz::openbmc_project::Led::server::Physical>( |
Vishwanatha Subbanna | 75b5510 | 2016-11-30 14:20:53 +0530 | [diff] [blame] | 78 | bus, objPath.c_str(), true), |
Vishwanatha Subbanna | bda97eb | 2016-11-30 12:21:25 +0530 | [diff] [blame] | 79 | path(ledPath) |
| 80 | { |
Vishwanatha Subbanna | 75b5510 | 2016-11-30 14:20:53 +0530 | [diff] [blame] | 81 | // Suppose this is getting launched as part of BMC reboot, then we |
| 82 | // need to save what the micro-controller currently has. |
| 83 | setInitialState(); |
| 84 | |
| 85 | // We are now ready. |
| 86 | emit_object_added(); |
Vishwanatha Subbanna | bda97eb | 2016-11-30 12:21:25 +0530 | [diff] [blame] | 87 | } |
| 88 | |
Vishwanatha Subbanna | 75b5510 | 2016-11-30 14:20:53 +0530 | [diff] [blame] | 89 | /** @brief Overloaded State Property Setter function |
| 90 | * |
| 91 | * @param[in] value - One of OFF / ON / BLINK |
| 92 | * @return - Success or exception thrown |
| 93 | */ |
| 94 | Action state(Action value) override; |
| 95 | |
Vishwanatha Subbanna | bda97eb | 2016-11-30 12:21:25 +0530 | [diff] [blame] | 96 | private: |
| 97 | /** @brief File system location where this LED is exposed |
| 98 | * Typically /sys/class/leds/<Led-Name> |
| 99 | */ |
| 100 | std::string path; |
Vishwanatha Subbanna | 75b5510 | 2016-11-30 14:20:53 +0530 | [diff] [blame] | 101 | |
Vishwanatha Subbanna | 61675c3 | 2016-11-30 15:52:15 +0530 | [diff] [blame] | 102 | /** @brief Frequency range that the LED can operate on. |
| 103 | * Will be removed when frequency is put into interface |
Vishwanatha Subbanna | 75b5510 | 2016-11-30 14:20:53 +0530 | [diff] [blame] | 104 | */ |
Vishwanatha Subbanna | 61675c3 | 2016-11-30 15:52:15 +0530 | [diff] [blame] | 105 | uint32_t frequency; |
| 106 | |
| 107 | /** @brief Brightness described above */ |
| 108 | std::string brightCtrl; |
| 109 | |
| 110 | /** @brief BlinkCtrl described above */ |
| 111 | std::string blinkCtrl; |
| 112 | |
| 113 | /** @brief delay_on described above */ |
| 114 | std::string delayOnCtrl; |
| 115 | |
| 116 | /** @brief delay_ff described above */ |
| 117 | std::string delayOffCtrl; |
Vishwanatha Subbanna | 75b5510 | 2016-11-30 14:20:53 +0530 | [diff] [blame] | 118 | |
| 119 | /** @brief reads sysfs and then setsup the parameteres accordingly |
| 120 | * |
Vishwanatha Subbanna | 61675c3 | 2016-11-30 15:52:15 +0530 | [diff] [blame] | 121 | * @return None |
Vishwanatha Subbanna | 75b5510 | 2016-11-30 14:20:53 +0530 | [diff] [blame] | 122 | */ |
Vishwanatha Subbanna | 61675c3 | 2016-11-30 15:52:15 +0530 | [diff] [blame] | 123 | void setInitialState(); |
| 124 | |
| 125 | /** @brief Applies the user triggered action on the LED |
| 126 | * by writing to sysfs |
| 127 | * |
| 128 | * @param [in] current - Current state of LED |
| 129 | * @param [in] request - Requested state |
| 130 | * |
| 131 | * @return None |
| 132 | */ |
| 133 | void driveLED(Action current, Action request); |
| 134 | |
| 135 | /** @brief Sets the LED to either ON or OFF state |
| 136 | * |
| 137 | * @param [in] action - Requested action. Could be OFF or ON |
| 138 | * @return None |
| 139 | */ |
| 140 | void stableStateOperation(Action action); |
| 141 | |
| 142 | /** @brief Sets the LED to BLINKING |
| 143 | * |
| 144 | * @return None |
| 145 | */ |
| 146 | void blinkOperation(); |
| 147 | |
| 148 | /** @brief Generic file writer. |
| 149 | * There are files like "brightness", "trigger" , "delay_on" and |
| 150 | * "delay_off" that will tell what the LED driver needs to do. |
| 151 | * |
| 152 | * @param[in] filename - Name of file to be written |
| 153 | * @param[in] data - Data to be written to |
| 154 | * @return - None |
| 155 | */ |
| 156 | template <typename T> |
| 157 | auto write(const std::string& fileName, T&& data) |
| 158 | { |
| 159 | if(std::ifstream(fileName)) |
| 160 | { |
| 161 | std::ofstream file(fileName, std::ios::out); |
| 162 | file << data; |
| 163 | file.close(); |
| 164 | } |
| 165 | return; |
| 166 | } |
| 167 | |
| 168 | /** @brief Generic file reader. |
| 169 | * There are files like "brightness", "trigger" , "delay_on" and |
| 170 | * "delay_off" that will tell what the LED driver needs to do. |
| 171 | * |
| 172 | * @param[in] filename - Name of file to be read |
| 173 | * @return - File content |
| 174 | */ |
| 175 | template <typename T> |
| 176 | T read(const std::string& fileName) |
| 177 | { |
| 178 | T data = T(); |
| 179 | if(std::ifstream(fileName)) |
| 180 | { |
| 181 | std::ifstream file(fileName, std::ios::in); |
| 182 | file >> data; |
| 183 | file.close(); |
| 184 | } |
| 185 | return data; |
| 186 | } |
Vishwanatha Subbanna | bda97eb | 2016-11-30 12:21:25 +0530 | [diff] [blame] | 187 | }; |
| 188 | |
| 189 | } // namespace led |
Vishwanatha Subbanna | 75b5510 | 2016-11-30 14:20:53 +0530 | [diff] [blame] | 190 | } |