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