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 | |
Alexander Hansen | a6f9a41 | 2024-07-24 12:27:42 +0200 | [diff] [blame] | 17 | // apply the led action to the map |
| 18 | static void applyGroupAction(std::map<LedName, Layout::LedAction>& newState, |
| 19 | Layout::LedAction action) |
| 20 | { |
| 21 | if (!newState.contains(action.name)) |
| 22 | { |
| 23 | newState[action.name] = action; |
| 24 | return; |
| 25 | } |
| 26 | |
| 27 | auto currentAction = newState[action.name]; |
| 28 | |
| 29 | if (currentAction.action == action.priority) |
| 30 | { |
| 31 | // if the current action is already the priority action, |
| 32 | // we cannot override it |
| 33 | return; |
| 34 | } |
| 35 | |
| 36 | newState[action.name] = action; |
| 37 | } |
| 38 | |
| 39 | // create the resulting new map from all currently asserted groups |
Alexander Hansen | 7ba70c8 | 2024-07-23 13:46:25 +0200 | [diff] [blame] | 40 | static auto getNewMapWithGroupPriorities( |
| 41 | std::set<const Layout::GroupLayout*, Layout::CompareGroupLayout> sorted) |
Alexander Hansen | a6f9a41 | 2024-07-24 12:27:42 +0200 | [diff] [blame] | 42 | -> std::map<LedName, Layout::LedAction> |
| 43 | { |
| 44 | std::map<LedName, Layout::LedAction> newState; |
| 45 | |
| 46 | // update the new map with the desired state |
Alexander Hansen | 7ba70c8 | 2024-07-23 13:46:25 +0200 | [diff] [blame] | 47 | for (const auto it : sorted) |
Alexander Hansen | a6f9a41 | 2024-07-24 12:27:42 +0200 | [diff] [blame] | 48 | { |
| 49 | // apply all led actions of that group to the map |
Alexander Hansen | 7ba70c8 | 2024-07-23 13:46:25 +0200 | [diff] [blame] | 50 | for (Layout::LedAction action : it->actionSet) |
| 51 | { |
| 52 | newState[action.name] = action; |
| 53 | } |
| 54 | } |
| 55 | return newState; |
| 56 | } |
| 57 | |
| 58 | static std::map<LedName, Layout::LedAction> getNewMapWithLEDPriorities( |
| 59 | std::set<const Layout::GroupLayout*> assertedGroups) |
| 60 | { |
| 61 | std::map<LedName, Layout::LedAction> newState; |
| 62 | // update the new map with the desired state |
| 63 | for (const Layout::GroupLayout* it : assertedGroups) |
| 64 | { |
| 65 | // apply all led actions of that group to the map |
| 66 | for (Layout::LedAction action : it->actionSet) |
Alexander Hansen | a6f9a41 | 2024-07-24 12:27:42 +0200 | [diff] [blame] | 67 | { |
| 68 | applyGroupAction(newState, action); |
| 69 | } |
| 70 | } |
| 71 | return newState; |
| 72 | } |
| 73 | |
Alexander Hansen | 7ba70c8 | 2024-07-23 13:46:25 +0200 | [diff] [blame] | 74 | // create the resulting new map from all currently asserted groups |
| 75 | std::map<LedName, Layout::LedAction> |
| 76 | Manager::getNewMap(std::set<const Layout::GroupLayout*> assertedGroups) |
| 77 | { |
| 78 | std::map<LedName, Layout::LedAction> newState; |
| 79 | |
| 80 | std::set<const Layout::GroupLayout*, Layout::CompareGroupLayout> sorted; |
| 81 | |
| 82 | bool groupPriorities = false; |
| 83 | |
| 84 | for (const Layout::GroupLayout* it : assertedGroups) |
| 85 | { |
| 86 | sorted.insert(it); |
| 87 | |
| 88 | if (it->priority != 0) |
| 89 | { |
| 90 | groupPriorities = true; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | if (groupPriorities) |
| 95 | { |
| 96 | newState = getNewMapWithGroupPriorities(sorted); |
| 97 | } |
| 98 | else |
| 99 | { |
| 100 | newState = getNewMapWithLEDPriorities(assertedGroups); |
| 101 | } |
| 102 | |
| 103 | return newState; |
| 104 | } |
| 105 | |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 106 | // Assert -or- De-assert |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 107 | bool Manager::setGroupState(const std::string& path, bool assert, |
Patrick Williams | 158b2c1 | 2022-03-17 05:57:44 -0500 | [diff] [blame] | 108 | ActionSet& ledsAssert, ActionSet& ledsDeAssert) |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 109 | { |
| 110 | if (assert) |
| 111 | { |
| 112 | assertedGroups.insert(&ledMap.at(path)); |
| 113 | } |
| 114 | else |
| 115 | { |
George Liu | 7f53a03 | 2021-05-04 11:18:21 +0800 | [diff] [blame] | 116 | if (assertedGroups.contains(&ledMap.at(path))) |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 117 | { |
| 118 | assertedGroups.erase(&ledMap.at(path)); |
| 119 | } |
| 120 | } |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 121 | |
Alexander Hansen | a6f9a41 | 2024-07-24 12:27:42 +0200 | [diff] [blame] | 122 | // create the new map from the asserted groups |
| 123 | auto newState = getNewMap(assertedGroups); |
| 124 | |
| 125 | // the ledsAssert are those that are in the new map and change state |
| 126 | // + those in the new map and not in the old map |
| 127 | for (const auto& [name, action] : newState) |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 128 | { |
Alexander Hansen | a6f9a41 | 2024-07-24 12:27:42 +0200 | [diff] [blame] | 129 | if (ledStateMap.contains(name)) |
| 130 | { |
| 131 | // check if the led action has changed |
| 132 | auto& currentAction = ledStateMap[name]; |
| 133 | |
| 134 | if (currentAction.action == action.action) |
| 135 | continue; |
| 136 | } |
| 137 | |
| 138 | ledsAssert.insert(action); |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 139 | } |
| 140 | |
Alexander Hansen | a6f9a41 | 2024-07-24 12:27:42 +0200 | [diff] [blame] | 141 | // the ledsDeAssert are those in the old map but not in the new map |
| 142 | for (const auto& [name, action] : ledStateMap) |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 143 | { |
Alexander Hansen | a6f9a41 | 2024-07-24 12:27:42 +0200 | [diff] [blame] | 144 | if (!newState.contains(name)) |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 145 | { |
Alexander Hansen | a6f9a41 | 2024-07-24 12:27:42 +0200 | [diff] [blame] | 146 | ledsDeAssert.insert(action); |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 147 | } |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 148 | } |
| 149 | |
Alexander Hansen | a6f9a41 | 2024-07-24 12:27:42 +0200 | [diff] [blame] | 150 | ledStateMap = newState; |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 151 | |
| 152 | // If we survive, then set the state accordingly. |
| 153 | return assert; |
| 154 | } |
| 155 | |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 156 | void Manager::setLampTestCallBack( |
Patrick Williams | 158b2c1 | 2022-03-17 05:57:44 -0500 | [diff] [blame] | 157 | std::function<bool(ActionSet& ledsAssert, ActionSet& ledsDeAssert)> |
| 158 | callBack) |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 159 | { |
| 160 | lampTestCallBack = callBack; |
| 161 | } |
| 162 | |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 163 | /** @brief Run through the map and apply action on the LEDs */ |
Patrick Williams | 158b2c1 | 2022-03-17 05:57:44 -0500 | [diff] [blame] | 164 | void Manager::driveLEDs(ActionSet& ledsAssert, ActionSet& ledsDeAssert) |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 165 | { |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 166 | #ifdef USE_LAMP_TEST |
| 167 | // Use the lampTestCallBack method and trigger the callback method in the |
| 168 | // lamp test(processLEDUpdates), in this way, all lamp test operations |
| 169 | // are performed in the lamp test class. |
| 170 | if (lampTestCallBack(ledsAssert, ledsDeAssert)) |
| 171 | { |
| 172 | return; |
| 173 | } |
| 174 | #endif |
Potin Lai | f1ed479 | 2023-07-13 18:45:14 +0800 | [diff] [blame] | 175 | ActionSet newReqChangedLeds; |
| 176 | std::vector<std::pair<ActionSet&, ActionSet&>> actionsVec = { |
| 177 | {reqLedsAssert, ledsAssert}, {reqLedsDeAssert, ledsDeAssert}}; |
| 178 | |
| 179 | timer.setEnabled(false); |
| 180 | std::set_union(ledsAssert.begin(), ledsAssert.end(), ledsDeAssert.begin(), |
| 181 | ledsDeAssert.end(), |
| 182 | std::inserter(newReqChangedLeds, newReqChangedLeds.begin()), |
| 183 | ledLess); |
| 184 | |
| 185 | // prepare reqLedsAssert & reqLedsDeAssert |
| 186 | for (auto pair : actionsVec) |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 187 | { |
Potin Lai | f1ed479 | 2023-07-13 18:45:14 +0800 | [diff] [blame] | 188 | ActionSet tmpSet; |
| 189 | |
| 190 | // Discard current required LED actions, if these LEDs have new actions |
| 191 | // in newReqChangedLeds. |
| 192 | std::set_difference(pair.first.begin(), pair.first.end(), |
| 193 | newReqChangedLeds.begin(), newReqChangedLeds.end(), |
| 194 | std::inserter(tmpSet, tmpSet.begin()), ledLess); |
| 195 | |
| 196 | // Union the remaining LED actions with new LED actions. |
| 197 | pair.first.clear(); |
| 198 | std::set_union(tmpSet.begin(), tmpSet.end(), pair.second.begin(), |
| 199 | pair.second.end(), |
| 200 | std::inserter(pair.first, pair.first.begin()), ledLess); |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 201 | } |
| 202 | |
Potin Lai | f1ed479 | 2023-07-13 18:45:14 +0800 | [diff] [blame] | 203 | driveLedsHandler(); |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 204 | return; |
| 205 | } |
| 206 | |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 207 | // Calls into driving physical LED post choosing the action |
Potin Lai | f1ed479 | 2023-07-13 18:45:14 +0800 | [diff] [blame] | 208 | int Manager::drivePhysicalLED(const std::string& objPath, Layout::Action action, |
| 209 | uint8_t dutyOn, const uint16_t period) |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 210 | { |
George Liu | 1c737af | 2020-10-16 09:07:02 +0800 | [diff] [blame] | 211 | try |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 212 | { |
George Liu | 1c737af | 2020-10-16 09:07:02 +0800 | [diff] [blame] | 213 | // If Blink, set its property |
| 214 | if (action == Layout::Action::Blink) |
| 215 | { |
| 216 | PropertyValue dutyOnValue{dutyOn}; |
| 217 | PropertyValue periodValue{period}; |
| 218 | |
George Liu | 1f0b715 | 2023-07-18 09:24:34 +0800 | [diff] [blame] | 219 | dBusHandler.setProperty(objPath, phyLedIntf, "DutyOn", dutyOnValue); |
| 220 | dBusHandler.setProperty(objPath, phyLedIntf, "Period", periodValue); |
George Liu | 1c737af | 2020-10-16 09:07:02 +0800 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | PropertyValue actionValue{getPhysicalAction(action)}; |
George Liu | 1f0b715 | 2023-07-18 09:24:34 +0800 | [diff] [blame] | 224 | dBusHandler.setProperty(objPath, phyLedIntf, "State", actionValue); |
George Liu | 1c737af | 2020-10-16 09:07:02 +0800 | [diff] [blame] | 225 | } |
George Liu | 829c0b3 | 2023-07-18 10:00:47 +0800 | [diff] [blame] | 226 | catch (const sdbusplus::exception_t& e) |
George Liu | 1c737af | 2020-10-16 09:07:02 +0800 | [diff] [blame] | 227 | { |
George Liu | e9fb5c6 | 2021-07-01 14:05:32 +0800 | [diff] [blame] | 228 | lg2::error( |
| 229 | "Error setting property for physical LED, ERROR = {ERROR}, OBJECT_PATH = {PATH}", |
| 230 | "ERROR", e, "PATH", objPath); |
Potin Lai | f1ed479 | 2023-07-13 18:45:14 +0800 | [diff] [blame] | 231 | return -1; |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 232 | } |
Vishwanatha Subbanna | dcc3f38 | 2017-03-24 20:15:02 +0530 | [diff] [blame] | 233 | |
Potin Lai | f1ed479 | 2023-07-13 18:45:14 +0800 | [diff] [blame] | 234 | return 0; |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | /** @brief Returns action string based on enum */ |
Vishwanatha Subbanna | 4fa9248 | 2017-03-10 14:39:20 +0530 | [diff] [blame] | 238 | std::string Manager::getPhysicalAction(Layout::Action action) |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 239 | { |
Vishwanatha Subbanna | 4fa9248 | 2017-03-10 14:39:20 +0530 | [diff] [blame] | 240 | namespace server = sdbusplus::xyz::openbmc_project::Led::server; |
| 241 | |
| 242 | // TODO: openbmc/phosphor-led-manager#5 |
| 243 | // Somehow need to use the generated Action enum than giving one |
| 244 | // in ledlayout. |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 245 | if (action == Layout::Action::On) |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 246 | { |
Vishwanatha Subbanna | 4fa9248 | 2017-03-10 14:39:20 +0530 | [diff] [blame] | 247 | return server::convertForMessage(server::Physical::Action::On); |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 248 | } |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 249 | else if (action == Layout::Action::Blink) |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 250 | { |
Vishwanatha Subbanna | 4fa9248 | 2017-03-10 14:39:20 +0530 | [diff] [blame] | 251 | return server::convertForMessage(server::Physical::Action::Blink); |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 252 | } |
| 253 | else |
| 254 | { |
Vishwanatha Subbanna | 4fa9248 | 2017-03-10 14:39:20 +0530 | [diff] [blame] | 255 | return server::convertForMessage(server::Physical::Action::Off); |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | |
Potin Lai | f1ed479 | 2023-07-13 18:45:14 +0800 | [diff] [blame] | 259 | void Manager::driveLedsHandler(void) |
| 260 | { |
| 261 | ActionSet failedLedsAssert; |
| 262 | ActionSet failedLedsDeAssert; |
| 263 | |
| 264 | // This order of LED operation is important. |
Alexander Hansen | fe476e1 | 2024-07-23 15:45:22 +0200 | [diff] [blame] | 265 | for (const auto& it : reqLedsDeAssert) |
Potin Lai | f1ed479 | 2023-07-13 18:45:14 +0800 | [diff] [blame] | 266 | { |
Alexander Hansen | fe476e1 | 2024-07-23 15:45:22 +0200 | [diff] [blame] | 267 | std::string objPath = std::string(phyLedPath) + it.name; |
| 268 | lg2::debug("De-Asserting LED, NAME = {NAME}, ACTION = {ACTION}", "NAME", |
| 269 | it.name, "ACTION", it.action); |
| 270 | if (drivePhysicalLED(objPath, Layout::Action::Off, it.dutyOn, |
| 271 | it.period)) |
Potin Lai | f1ed479 | 2023-07-13 18:45:14 +0800 | [diff] [blame] | 272 | { |
Alexander Hansen | fe476e1 | 2024-07-23 15:45:22 +0200 | [diff] [blame] | 273 | failedLedsDeAssert.insert(it); |
Potin Lai | f1ed479 | 2023-07-13 18:45:14 +0800 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | |
Alexander Hansen | fe476e1 | 2024-07-23 15:45:22 +0200 | [diff] [blame] | 277 | for (const auto& it : reqLedsAssert) |
Potin Lai | f1ed479 | 2023-07-13 18:45:14 +0800 | [diff] [blame] | 278 | { |
Alexander Hansen | fe476e1 | 2024-07-23 15:45:22 +0200 | [diff] [blame] | 279 | std::string objPath = std::string(phyLedPath) + it.name; |
| 280 | lg2::debug("Asserting LED, NAME = {NAME}, ACTION = {ACTION}", "NAME", |
| 281 | it.name, "ACTION", it.action); |
| 282 | if (drivePhysicalLED(objPath, it.action, it.dutyOn, it.period)) |
Potin Lai | f1ed479 | 2023-07-13 18:45:14 +0800 | [diff] [blame] | 283 | { |
Alexander Hansen | fe476e1 | 2024-07-23 15:45:22 +0200 | [diff] [blame] | 284 | failedLedsAssert.insert(it); |
Potin Lai | f1ed479 | 2023-07-13 18:45:14 +0800 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | |
| 288 | reqLedsAssert = failedLedsAssert; |
| 289 | reqLedsDeAssert = failedLedsDeAssert; |
| 290 | |
| 291 | if (reqLedsDeAssert.empty() && reqLedsAssert.empty()) |
| 292 | { |
| 293 | timer.setEnabled(false); |
| 294 | } |
| 295 | else |
| 296 | { |
| 297 | timer.restartOnce(std::chrono::seconds(1)); |
| 298 | } |
| 299 | |
| 300 | return; |
| 301 | } |
| 302 | |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 303 | } // namespace led |
| 304 | } // namespace phosphor |