blob: 6d223b829f63d58a181feeb44bf554255d871d02 [file] [log] [blame]
George Liub6151622020-11-23 18:16:18 +08001#include "config.h"
2
Patrick Venture91ac8d32018-11-01 17:03:22 -07003#include "manager.hpp"
4
George Liue9fb5c62021-07-01 14:05:32 +08005#include <phosphor-logging/lg2.hpp>
William A. Kennington III151122a2018-05-15 12:00:32 -07006#include <sdbusplus/exception.hpp>
Vishwanatha Subbanna4fa92482017-03-10 14:39:20 +05307#include <xyz/openbmc_project/Led/Physical/server.hpp>
George Liua6c18f82020-06-22 10:50:04 +08008
9#include <algorithm>
10#include <iostream>
11#include <string>
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053012namespace phosphor
13{
14namespace led
15{
16
17// Assert -or- De-assert
Vishwanatha Subbannaed490732016-12-20 15:59:29 +053018bool Manager::setGroupState(const std::string& path, bool assert,
Patrick Williams158b2c12022-03-17 05:57:44 -050019 ActionSet& ledsAssert, ActionSet& ledsDeAssert)
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053020{
21 if (assert)
22 {
23 assertedGroups.insert(&ledMap.at(path));
24 }
25 else
26 {
George Liu7f53a032021-05-04 11:18:21 +080027 if (assertedGroups.contains(&ledMap.at(path)))
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053028 {
29 assertedGroups.erase(&ledMap.at(path));
30 }
31 }
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053032
Patrick Williams158b2c12022-03-17 05:57:44 -050033 // This will contain the union of what's already in the asserted ActionSet
34 ActionSet desiredState{};
Patrick Venture91ac8d32018-11-01 17:03:22 -070035 for (const auto& grp : assertedGroups)
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053036 {
37 desiredState.insert(grp->cbegin(), grp->cend());
38 }
39
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +053040 // Find difference between Combined and Desired to identify
41 // which LEDs are getting altered
Patrick Williams158b2c12022-03-17 05:57:44 -050042 ActionSet transient{};
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +053043 std::set_difference(combinedState.begin(), combinedState.end(),
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053044 desiredState.begin(), desiredState.end(),
Patrick Venture91ac8d32018-11-01 17:03:22 -070045 std::inserter(transient, transient.begin()), ledComp);
46 if (transient.size())
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053047 {
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +053048 // Find common LEDs between transient and Desired to know if some LEDs
49 // are changing state and not really getting DeAsserted
Patrick Williams158b2c12022-03-17 05:57:44 -050050 ActionSet ledsTransient{};
Patrick Venture91ac8d32018-11-01 17:03:22 -070051 std::set_intersection(
52 transient.begin(), transient.end(), desiredState.begin(),
53 desiredState.end(),
54 std::inserter(ledsTransient, ledsTransient.begin()), ledLess);
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053055
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +053056 // Find difference between above 2 to identify those LEDs which are
57 // really getting DeAsserted
Patrick Venture91ac8d32018-11-01 17:03:22 -070058 std::set_difference(transient.begin(), transient.end(),
59 ledsTransient.begin(), ledsTransient.end(),
Vishwanatha Subbanna447d0c82016-12-19 14:12:42 +053060 std::inserter(ledsDeAssert, ledsDeAssert.begin()),
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +053061 ledLess);
Vishwanatha Subbanna447d0c82016-12-19 14:12:42 +053062
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +053063 // Remove the elements from Current that are being DeAsserted.
Patrick Venture91ac8d32018-11-01 17:03:22 -070064 if (ledsDeAssert.size())
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +053065 {
66 // Power off LEDs that are to be really DeAsserted
Patrick Venture91ac8d32018-11-01 17:03:22 -070067 for (auto& it : ledsDeAssert)
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +053068 {
69 // Update LEDs in "physically asserted" set by removing those
70 // LEDs which are De-Asserted
71 auto found = currentState.find(it);
72 if (found != currentState.end())
73 {
74 currentState.erase(found);
75 }
76 }
77 }
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053078 }
79
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +053080 // Now LEDs that are to be Asserted. These could either be fresh asserts
81 // -or- change between [On]<-->[Blink]
Patrick Williams158b2c12022-03-17 05:57:44 -050082 ActionSet temp{};
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +053083 std::unique_copy(desiredState.begin(), desiredState.end(),
Patrick Venture91ac8d32018-11-01 17:03:22 -070084 std::inserter(temp, temp.begin()), ledEqual);
85 if (temp.size())
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +053086 {
87 // Find difference between [desired to be Asserted] and those LEDs
88 // that are physically asserted currently.
Patrick Venture91ac8d32018-11-01 17:03:22 -070089 std::set_difference(
90 temp.begin(), temp.end(), currentState.begin(), currentState.end(),
91 std::inserter(ledsAssert, ledsAssert.begin()), ledComp);
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +053092 }
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053093
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +053094 // Update the current actual and desired(the virtual actual)
95 currentState = std::move(temp);
96 combinedState = std::move(desiredState);
Vishwanatha Subbannaed490732016-12-20 15:59:29 +053097
98 // If we survive, then set the state accordingly.
99 return assert;
100}
101
George Liub6151622020-11-23 18:16:18 +0800102void Manager::setLampTestCallBack(
Patrick Williams158b2c12022-03-17 05:57:44 -0500103 std::function<bool(ActionSet& ledsAssert, ActionSet& ledsDeAssert)>
104 callBack)
George Liub6151622020-11-23 18:16:18 +0800105{
106 lampTestCallBack = callBack;
107}
108
Vishwanatha Subbannaed490732016-12-20 15:59:29 +0530109/** @brief Run through the map and apply action on the LEDs */
Patrick Williams158b2c12022-03-17 05:57:44 -0500110void Manager::driveLEDs(ActionSet& ledsAssert, ActionSet& ledsDeAssert)
Vishwanatha Subbannaed490732016-12-20 15:59:29 +0530111{
George Liub6151622020-11-23 18:16:18 +0800112#ifdef USE_LAMP_TEST
113 // Use the lampTestCallBack method and trigger the callback method in the
114 // lamp test(processLEDUpdates), in this way, all lamp test operations
115 // are performed in the lamp test class.
116 if (lampTestCallBack(ledsAssert, ledsDeAssert))
117 {
118 return;
119 }
120#endif
Potin Laif1ed4792023-07-13 18:45:14 +0800121 ActionSet newReqChangedLeds;
122 std::vector<std::pair<ActionSet&, ActionSet&>> actionsVec = {
123 {reqLedsAssert, ledsAssert}, {reqLedsDeAssert, ledsDeAssert}};
124
125 timer.setEnabled(false);
126 std::set_union(ledsAssert.begin(), ledsAssert.end(), ledsDeAssert.begin(),
127 ledsDeAssert.end(),
128 std::inserter(newReqChangedLeds, newReqChangedLeds.begin()),
129 ledLess);
130
131 // prepare reqLedsAssert & reqLedsDeAssert
132 for (auto pair : actionsVec)
Vishwanatha Subbannaed490732016-12-20 15:59:29 +0530133 {
Potin Laif1ed4792023-07-13 18:45:14 +0800134 ActionSet tmpSet;
135
136 // Discard current required LED actions, if these LEDs have new actions
137 // in newReqChangedLeds.
138 std::set_difference(pair.first.begin(), pair.first.end(),
139 newReqChangedLeds.begin(), newReqChangedLeds.end(),
140 std::inserter(tmpSet, tmpSet.begin()), ledLess);
141
142 // Union the remaining LED actions with new LED actions.
143 pair.first.clear();
144 std::set_union(tmpSet.begin(), tmpSet.end(), pair.second.begin(),
145 pair.second.end(),
146 std::inserter(pair.first, pair.first.begin()), ledLess);
Vishwanatha Subbannaed490732016-12-20 15:59:29 +0530147 }
148
Potin Laif1ed4792023-07-13 18:45:14 +0800149 driveLedsHandler();
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +0530150 return;
151}
152
Vishwanatha Subbanna11ca8f92017-02-27 19:33:45 +0530153// Calls into driving physical LED post choosing the action
Potin Laif1ed4792023-07-13 18:45:14 +0800154int Manager::drivePhysicalLED(const std::string& objPath, Layout::Action action,
155 uint8_t dutyOn, const uint16_t period)
Vishwanatha Subbanna11ca8f92017-02-27 19:33:45 +0530156{
George Liu1c737af2020-10-16 09:07:02 +0800157 try
Vishwanatha Subbanna11ca8f92017-02-27 19:33:45 +0530158 {
George Liu1c737af2020-10-16 09:07:02 +0800159 // If Blink, set its property
160 if (action == Layout::Action::Blink)
161 {
162 PropertyValue dutyOnValue{dutyOn};
163 PropertyValue periodValue{period};
164
George Liu1f0b7152023-07-18 09:24:34 +0800165 dBusHandler.setProperty(objPath, phyLedIntf, "DutyOn", dutyOnValue);
166 dBusHandler.setProperty(objPath, phyLedIntf, "Period", periodValue);
George Liu1c737af2020-10-16 09:07:02 +0800167 }
168
169 PropertyValue actionValue{getPhysicalAction(action)};
George Liu1f0b7152023-07-18 09:24:34 +0800170 dBusHandler.setProperty(objPath, phyLedIntf, "State", actionValue);
George Liu1c737af2020-10-16 09:07:02 +0800171 }
George Liu829c0b32023-07-18 10:00:47 +0800172 catch (const sdbusplus::exception_t& e)
George Liu1c737af2020-10-16 09:07:02 +0800173 {
George Liue9fb5c62021-07-01 14:05:32 +0800174 lg2::error(
175 "Error setting property for physical LED, ERROR = {ERROR}, OBJECT_PATH = {PATH}",
176 "ERROR", e, "PATH", objPath);
Potin Laif1ed4792023-07-13 18:45:14 +0800177 return -1;
Vishwanatha Subbanna11ca8f92017-02-27 19:33:45 +0530178 }
Vishwanatha Subbannadcc3f382017-03-24 20:15:02 +0530179
Potin Laif1ed4792023-07-13 18:45:14 +0800180 return 0;
Vishwanatha Subbanna11ca8f92017-02-27 19:33:45 +0530181}
182
183/** @brief Returns action string based on enum */
Vishwanatha Subbanna4fa92482017-03-10 14:39:20 +0530184std::string Manager::getPhysicalAction(Layout::Action action)
Vishwanatha Subbanna11ca8f92017-02-27 19:33:45 +0530185{
Vishwanatha Subbanna4fa92482017-03-10 14:39:20 +0530186 namespace server = sdbusplus::xyz::openbmc_project::Led::server;
187
188 // TODO: openbmc/phosphor-led-manager#5
189 // Somehow need to use the generated Action enum than giving one
190 // in ledlayout.
Patrick Venture91ac8d32018-11-01 17:03:22 -0700191 if (action == Layout::Action::On)
Vishwanatha Subbanna11ca8f92017-02-27 19:33:45 +0530192 {
Vishwanatha Subbanna4fa92482017-03-10 14:39:20 +0530193 return server::convertForMessage(server::Physical::Action::On);
Vishwanatha Subbanna11ca8f92017-02-27 19:33:45 +0530194 }
Patrick Venture91ac8d32018-11-01 17:03:22 -0700195 else if (action == Layout::Action::Blink)
Vishwanatha Subbanna11ca8f92017-02-27 19:33:45 +0530196 {
Vishwanatha Subbanna4fa92482017-03-10 14:39:20 +0530197 return server::convertForMessage(server::Physical::Action::Blink);
Vishwanatha Subbanna11ca8f92017-02-27 19:33:45 +0530198 }
199 else
200 {
Vishwanatha Subbanna4fa92482017-03-10 14:39:20 +0530201 return server::convertForMessage(server::Physical::Action::Off);
Vishwanatha Subbanna11ca8f92017-02-27 19:33:45 +0530202 }
203}
204
Potin Laif1ed4792023-07-13 18:45:14 +0800205void Manager::driveLedsHandler(void)
206{
207 ActionSet failedLedsAssert;
208 ActionSet failedLedsDeAssert;
209
210 // This order of LED operation is important.
211 if (reqLedsDeAssert.size())
212 {
213 for (const auto& it : reqLedsDeAssert)
214 {
215 std::string objPath = std::string(phyLedPath) + it.name;
216 lg2::debug("De-Asserting LED, NAME = {NAME}, ACTION = {ACTION}",
217 "NAME", it.name, "ACTION", it.action);
218 if (drivePhysicalLED(objPath, Layout::Action::Off, it.dutyOn,
219 it.period))
220 {
221 failedLedsDeAssert.insert(it);
222 }
223 }
224 }
225
226 if (reqLedsAssert.size())
227 {
228 for (const auto& it : reqLedsAssert)
229 {
230 std::string objPath = std::string(phyLedPath) + it.name;
231 lg2::debug("Asserting LED, NAME = {NAME}, ACTION = {ACTION}",
232 "NAME", it.name, "ACTION", it.action);
233 if (drivePhysicalLED(objPath, it.action, it.dutyOn, it.period))
234 {
235 failedLedsAssert.insert(it);
236 }
237 }
238 }
239
240 reqLedsAssert = failedLedsAssert;
241 reqLedsDeAssert = failedLedsDeAssert;
242
243 if (reqLedsDeAssert.empty() && reqLedsAssert.empty())
244 {
245 timer.setEnabled(false);
246 }
247 else
248 {
249 timer.restartOnce(std::chrono::seconds(1));
250 }
251
252 return;
253}
254
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +0530255} // namespace led
256} // namespace phosphor