blob: 36fbedcdb9ef4b3c5edff4a6b42fbe666bd8e7df [file] [log] [blame]
Vishwanatha Subbannabda97eb2016-11-30 12:21:25 +05301#pragma once
2
3#include <string>
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +05304#include <fstream>
Vishwanatha Subbannabda97eb2016-11-30 12:21:25 +05305#include <sdbusplus/bus.hpp>
6#include <sdbusplus/server/object.hpp>
Vishwanatha Subbannae0891732017-03-10 15:27:23 +05307#include <xyz/openbmc_project/Led/Physical/server.hpp>
Vishwanatha Subbannabda97eb2016-11-30 12:21:25 +05308namespace phosphor
9{
10namespace led
11{
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +053012/** @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 */
20constexpr auto BRIGHTNESS = "/brightness";
21
22/** @brief Assert LED by writing 255 */
23constexpr auto ASSERT = "255";
24
25/** @brief De-assert by writing "0" */
26constexpr 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 */
35constexpr 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 */
41constexpr 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 */
47constexpr auto DELAYOFF = "/delay_off";
Vishwanatha Subbannabda97eb2016-11-30 12:21:25 +053048
49/** @class Physical
50 * @brief Responsible for applying actions on a particular physical LED
51 */
52class 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 Subbannae0891732017-03-10 15:27:23 +053060 Physical(Physical&&) = delete;
61 Physical& operator=(Physical&&) = delete;
Vishwanatha Subbannabda97eb2016-11-30 12:21:25 +053062
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053063 /** @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 Subbannabda97eb2016-11-30 12:21:25 +053067 *
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 Subbanna75b55102016-11-30 14:20:53 +053078 bus, objPath.c_str(), true),
Vishwanatha Subbannabda97eb2016-11-30 12:21:25 +053079 path(ledPath)
80 {
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053081 // 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 Subbannabda97eb2016-11-30 12:21:25 +053087 }
88
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053089 /** @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 Subbannabda97eb2016-11-30 12:21:25 +053096 private:
97 /** @brief File system location where this LED is exposed
98 * Typically /sys/class/leds/<Led-Name>
99 */
100 std::string path;
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +0530101
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +0530102 /** @brief Frequency range that the LED can operate on.
103 * Will be removed when frequency is put into interface
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +0530104 */
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +0530105 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 Subbanna75b55102016-11-30 14:20:53 +0530118
119 /** @brief reads sysfs and then setsup the parameteres accordingly
120 *
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +0530121 * @return None
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +0530122 */
Vishwanatha Subbanna61675c32016-11-30 15:52:15 +0530123 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 Subbannabda97eb2016-11-30 12:21:25 +0530187};
188
189} // namespace led
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +0530190}