George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 3 | #include "manager.hpp" |
| 4 | |
George Liu | e9fb5c6 | 2021-07-01 14:05:32 +0800 | [diff] [blame] | 5 | #include <phosphor-logging/lg2.hpp> |
William A. Kennington III | 151122a | 2018-05-15 12:00:32 -0700 | [diff] [blame] | 6 | #include <sdbusplus/exception.hpp> |
Vishwanatha Subbanna | 4fa9248 | 2017-03-10 14:39:20 +0530 | [diff] [blame] | 7 | #include <xyz/openbmc_project/Led/Physical/server.hpp> |
George Liu | a6c18f8 | 2020-06-22 10:50:04 +0800 | [diff] [blame] | 8 | |
| 9 | #include <algorithm> |
| 10 | #include <iostream> |
| 11 | #include <string> |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 12 | namespace phosphor |
| 13 | { |
| 14 | namespace led |
| 15 | { |
| 16 | |
| 17 | // Assert -or- De-assert |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 18 | bool Manager::setGroupState(const std::string& path, bool assert, |
Patrick Williams | 158b2c1 | 2022-03-17 05:57:44 -0500 | [diff] [blame] | 19 | ActionSet& ledsAssert, ActionSet& ledsDeAssert) |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 20 | { |
| 21 | if (assert) |
| 22 | { |
| 23 | assertedGroups.insert(&ledMap.at(path)); |
| 24 | } |
| 25 | else |
| 26 | { |
George Liu | 7f53a03 | 2021-05-04 11:18:21 +0800 | [diff] [blame] | 27 | if (assertedGroups.contains(&ledMap.at(path))) |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 28 | { |
| 29 | assertedGroups.erase(&ledMap.at(path)); |
| 30 | } |
| 31 | } |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 32 | |
Patrick Williams | 158b2c1 | 2022-03-17 05:57:44 -0500 | [diff] [blame] | 33 | // This will contain the union of what's already in the asserted ActionSet |
| 34 | ActionSet desiredState{}; |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 35 | for (const auto& grp : assertedGroups) |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 36 | { |
| 37 | desiredState.insert(grp->cbegin(), grp->cend()); |
| 38 | } |
| 39 | |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 40 | // Find difference between Combined and Desired to identify |
| 41 | // which LEDs are getting altered |
Patrick Williams | 158b2c1 | 2022-03-17 05:57:44 -0500 | [diff] [blame] | 42 | ActionSet transient{}; |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 43 | std::set_difference(combinedState.begin(), combinedState.end(), |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 44 | desiredState.begin(), desiredState.end(), |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 45 | std::inserter(transient, transient.begin()), ledComp); |
| 46 | if (transient.size()) |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 47 | { |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 48 | // Find common LEDs between transient and Desired to know if some LEDs |
| 49 | // are changing state and not really getting DeAsserted |
Patrick Williams | 158b2c1 | 2022-03-17 05:57:44 -0500 | [diff] [blame] | 50 | ActionSet ledsTransient{}; |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 51 | std::set_intersection( |
| 52 | transient.begin(), transient.end(), desiredState.begin(), |
| 53 | desiredState.end(), |
| 54 | std::inserter(ledsTransient, ledsTransient.begin()), ledLess); |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 55 | |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 56 | // Find difference between above 2 to identify those LEDs which are |
| 57 | // really getting DeAsserted |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 58 | std::set_difference(transient.begin(), transient.end(), |
| 59 | ledsTransient.begin(), ledsTransient.end(), |
Vishwanatha Subbanna | 447d0c8 | 2016-12-19 14:12:42 +0530 | [diff] [blame] | 60 | std::inserter(ledsDeAssert, ledsDeAssert.begin()), |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 61 | ledLess); |
Vishwanatha Subbanna | 447d0c8 | 2016-12-19 14:12:42 +0530 | [diff] [blame] | 62 | |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 63 | // Remove the elements from Current that are being DeAsserted. |
Alexander Hansen | fe476e1 | 2024-07-23 15:45:22 +0200 | [diff] [blame] | 64 | // Power off LEDs that are to be really DeAsserted |
| 65 | for (auto& it : ledsDeAssert) |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 66 | { |
Alexander Hansen | fe476e1 | 2024-07-23 15:45:22 +0200 | [diff] [blame] | 67 | // Update LEDs in "physically asserted" set by removing those |
| 68 | // LEDs which are De-Asserted |
| 69 | auto found = currentState.find(it); |
| 70 | if (found != currentState.end()) |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 71 | { |
Alexander Hansen | fe476e1 | 2024-07-23 15:45:22 +0200 | [diff] [blame] | 72 | currentState.erase(found); |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 73 | } |
| 74 | } |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 75 | } |
| 76 | |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 77 | // Now LEDs that are to be Asserted. These could either be fresh asserts |
| 78 | // -or- change between [On]<-->[Blink] |
Patrick Williams | 158b2c1 | 2022-03-17 05:57:44 -0500 | [diff] [blame] | 79 | ActionSet temp{}; |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 80 | std::unique_copy(desiredState.begin(), desiredState.end(), |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 81 | std::inserter(temp, temp.begin()), ledEqual); |
| 82 | if (temp.size()) |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 83 | { |
| 84 | // Find difference between [desired to be Asserted] and those LEDs |
| 85 | // that are physically asserted currently. |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 86 | std::set_difference( |
| 87 | temp.begin(), temp.end(), currentState.begin(), currentState.end(), |
| 88 | std::inserter(ledsAssert, ledsAssert.begin()), ledComp); |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 89 | } |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 90 | |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 91 | // Update the current actual and desired(the virtual actual) |
| 92 | currentState = std::move(temp); |
| 93 | combinedState = std::move(desiredState); |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 94 | |
| 95 | // If we survive, then set the state accordingly. |
| 96 | return assert; |
| 97 | } |
| 98 | |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 99 | void Manager::setLampTestCallBack( |
Patrick Williams | 158b2c1 | 2022-03-17 05:57:44 -0500 | [diff] [blame] | 100 | std::function<bool(ActionSet& ledsAssert, ActionSet& ledsDeAssert)> |
| 101 | callBack) |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 102 | { |
| 103 | lampTestCallBack = callBack; |
| 104 | } |
| 105 | |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 106 | /** @brief Run through the map and apply action on the LEDs */ |
Patrick Williams | 158b2c1 | 2022-03-17 05:57:44 -0500 | [diff] [blame] | 107 | void Manager::driveLEDs(ActionSet& ledsAssert, ActionSet& ledsDeAssert) |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 108 | { |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 109 | #ifdef USE_LAMP_TEST |
| 110 | // Use the lampTestCallBack method and trigger the callback method in the |
| 111 | // lamp test(processLEDUpdates), in this way, all lamp test operations |
| 112 | // are performed in the lamp test class. |
| 113 | if (lampTestCallBack(ledsAssert, ledsDeAssert)) |
| 114 | { |
| 115 | return; |
| 116 | } |
| 117 | #endif |
Potin Lai | f1ed479 | 2023-07-13 18:45:14 +0800 | [diff] [blame] | 118 | ActionSet newReqChangedLeds; |
| 119 | std::vector<std::pair<ActionSet&, ActionSet&>> actionsVec = { |
| 120 | {reqLedsAssert, ledsAssert}, {reqLedsDeAssert, ledsDeAssert}}; |
| 121 | |
| 122 | timer.setEnabled(false); |
| 123 | std::set_union(ledsAssert.begin(), ledsAssert.end(), ledsDeAssert.begin(), |
| 124 | ledsDeAssert.end(), |
| 125 | std::inserter(newReqChangedLeds, newReqChangedLeds.begin()), |
| 126 | ledLess); |
| 127 | |
| 128 | // prepare reqLedsAssert & reqLedsDeAssert |
| 129 | for (auto pair : actionsVec) |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 130 | { |
Potin Lai | f1ed479 | 2023-07-13 18:45:14 +0800 | [diff] [blame] | 131 | ActionSet tmpSet; |
| 132 | |
| 133 | // Discard current required LED actions, if these LEDs have new actions |
| 134 | // in newReqChangedLeds. |
| 135 | std::set_difference(pair.first.begin(), pair.first.end(), |
| 136 | newReqChangedLeds.begin(), newReqChangedLeds.end(), |
| 137 | std::inserter(tmpSet, tmpSet.begin()), ledLess); |
| 138 | |
| 139 | // Union the remaining LED actions with new LED actions. |
| 140 | pair.first.clear(); |
| 141 | std::set_union(tmpSet.begin(), tmpSet.end(), pair.second.begin(), |
| 142 | pair.second.end(), |
| 143 | std::inserter(pair.first, pair.first.begin()), ledLess); |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 144 | } |
| 145 | |
Potin Lai | f1ed479 | 2023-07-13 18:45:14 +0800 | [diff] [blame] | 146 | driveLedsHandler(); |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 147 | return; |
| 148 | } |
| 149 | |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 150 | // Calls into driving physical LED post choosing the action |
Potin Lai | f1ed479 | 2023-07-13 18:45:14 +0800 | [diff] [blame] | 151 | int Manager::drivePhysicalLED(const std::string& objPath, Layout::Action action, |
| 152 | uint8_t dutyOn, const uint16_t period) |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 153 | { |
George Liu | 1c737af | 2020-10-16 09:07:02 +0800 | [diff] [blame] | 154 | try |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 155 | { |
George Liu | 1c737af | 2020-10-16 09:07:02 +0800 | [diff] [blame] | 156 | // If Blink, set its property |
| 157 | if (action == Layout::Action::Blink) |
| 158 | { |
| 159 | PropertyValue dutyOnValue{dutyOn}; |
| 160 | PropertyValue periodValue{period}; |
| 161 | |
George Liu | 1f0b715 | 2023-07-18 09:24:34 +0800 | [diff] [blame] | 162 | dBusHandler.setProperty(objPath, phyLedIntf, "DutyOn", dutyOnValue); |
| 163 | dBusHandler.setProperty(objPath, phyLedIntf, "Period", periodValue); |
George Liu | 1c737af | 2020-10-16 09:07:02 +0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | PropertyValue actionValue{getPhysicalAction(action)}; |
George Liu | 1f0b715 | 2023-07-18 09:24:34 +0800 | [diff] [blame] | 167 | dBusHandler.setProperty(objPath, phyLedIntf, "State", actionValue); |
George Liu | 1c737af | 2020-10-16 09:07:02 +0800 | [diff] [blame] | 168 | } |
George Liu | 829c0b3 | 2023-07-18 10:00:47 +0800 | [diff] [blame] | 169 | catch (const sdbusplus::exception_t& e) |
George Liu | 1c737af | 2020-10-16 09:07:02 +0800 | [diff] [blame] | 170 | { |
George Liu | e9fb5c6 | 2021-07-01 14:05:32 +0800 | [diff] [blame] | 171 | lg2::error( |
| 172 | "Error setting property for physical LED, ERROR = {ERROR}, OBJECT_PATH = {PATH}", |
| 173 | "ERROR", e, "PATH", objPath); |
Potin Lai | f1ed479 | 2023-07-13 18:45:14 +0800 | [diff] [blame] | 174 | return -1; |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 175 | } |
Vishwanatha Subbanna | dcc3f38 | 2017-03-24 20:15:02 +0530 | [diff] [blame] | 176 | |
Potin Lai | f1ed479 | 2023-07-13 18:45:14 +0800 | [diff] [blame] | 177 | return 0; |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | /** @brief Returns action string based on enum */ |
Vishwanatha Subbanna | 4fa9248 | 2017-03-10 14:39:20 +0530 | [diff] [blame] | 181 | std::string Manager::getPhysicalAction(Layout::Action action) |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 182 | { |
Vishwanatha Subbanna | 4fa9248 | 2017-03-10 14:39:20 +0530 | [diff] [blame] | 183 | namespace server = sdbusplus::xyz::openbmc_project::Led::server; |
| 184 | |
| 185 | // TODO: openbmc/phosphor-led-manager#5 |
| 186 | // Somehow need to use the generated Action enum than giving one |
| 187 | // in ledlayout. |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 188 | if (action == Layout::Action::On) |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 189 | { |
Vishwanatha Subbanna | 4fa9248 | 2017-03-10 14:39:20 +0530 | [diff] [blame] | 190 | return server::convertForMessage(server::Physical::Action::On); |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 191 | } |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 192 | else if (action == Layout::Action::Blink) |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 193 | { |
Vishwanatha Subbanna | 4fa9248 | 2017-03-10 14:39:20 +0530 | [diff] [blame] | 194 | return server::convertForMessage(server::Physical::Action::Blink); |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 195 | } |
| 196 | else |
| 197 | { |
Vishwanatha Subbanna | 4fa9248 | 2017-03-10 14:39:20 +0530 | [diff] [blame] | 198 | return server::convertForMessage(server::Physical::Action::Off); |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
Potin Lai | f1ed479 | 2023-07-13 18:45:14 +0800 | [diff] [blame] | 202 | void Manager::driveLedsHandler(void) |
| 203 | { |
| 204 | ActionSet failedLedsAssert; |
| 205 | ActionSet failedLedsDeAssert; |
| 206 | |
| 207 | // This order of LED operation is important. |
Alexander Hansen | fe476e1 | 2024-07-23 15:45:22 +0200 | [diff] [blame] | 208 | for (const auto& it : reqLedsDeAssert) |
Potin Lai | f1ed479 | 2023-07-13 18:45:14 +0800 | [diff] [blame] | 209 | { |
Alexander Hansen | fe476e1 | 2024-07-23 15:45:22 +0200 | [diff] [blame] | 210 | std::string objPath = std::string(phyLedPath) + it.name; |
| 211 | lg2::debug("De-Asserting LED, NAME = {NAME}, ACTION = {ACTION}", "NAME", |
| 212 | it.name, "ACTION", it.action); |
| 213 | if (drivePhysicalLED(objPath, Layout::Action::Off, it.dutyOn, |
| 214 | it.period)) |
Potin Lai | f1ed479 | 2023-07-13 18:45:14 +0800 | [diff] [blame] | 215 | { |
Alexander Hansen | fe476e1 | 2024-07-23 15:45:22 +0200 | [diff] [blame] | 216 | failedLedsDeAssert.insert(it); |
Potin Lai | f1ed479 | 2023-07-13 18:45:14 +0800 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
Alexander Hansen | fe476e1 | 2024-07-23 15:45:22 +0200 | [diff] [blame] | 220 | for (const auto& it : reqLedsAssert) |
Potin Lai | f1ed479 | 2023-07-13 18:45:14 +0800 | [diff] [blame] | 221 | { |
Alexander Hansen | fe476e1 | 2024-07-23 15:45:22 +0200 | [diff] [blame] | 222 | std::string objPath = std::string(phyLedPath) + it.name; |
| 223 | lg2::debug("Asserting LED, NAME = {NAME}, ACTION = {ACTION}", "NAME", |
| 224 | it.name, "ACTION", it.action); |
| 225 | if (drivePhysicalLED(objPath, it.action, it.dutyOn, it.period)) |
Potin Lai | f1ed479 | 2023-07-13 18:45:14 +0800 | [diff] [blame] | 226 | { |
Alexander Hansen | fe476e1 | 2024-07-23 15:45:22 +0200 | [diff] [blame] | 227 | failedLedsAssert.insert(it); |
Potin Lai | f1ed479 | 2023-07-13 18:45:14 +0800 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | |
| 231 | reqLedsAssert = failedLedsAssert; |
| 232 | reqLedsDeAssert = failedLedsDeAssert; |
| 233 | |
| 234 | if (reqLedsDeAssert.empty() && reqLedsAssert.empty()) |
| 235 | { |
| 236 | timer.setEnabled(false); |
| 237 | } |
| 238 | else |
| 239 | { |
| 240 | timer.restartOnce(std::chrono::seconds(1)); |
| 241 | } |
| 242 | |
| 243 | return; |
| 244 | } |
| 245 | |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 246 | } // namespace led |
| 247 | } // namespace phosphor |