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 | |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 5 | #include <phosphor-logging/log.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 | |
George Liu | 1c737af | 2020-10-16 09:07:02 +0800 | [diff] [blame] | 17 | using namespace phosphor::logging; |
| 18 | |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 19 | // Assert -or- De-assert |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 20 | bool Manager::setGroupState(const std::string& path, bool assert, |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 21 | group& ledsAssert, group& ledsDeAssert) |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 22 | { |
| 23 | if (assert) |
| 24 | { |
| 25 | assertedGroups.insert(&ledMap.at(path)); |
| 26 | } |
| 27 | else |
| 28 | { |
George Liu | 7f53a03 | 2021-05-04 11:18:21 +0800 | [diff] [blame] | 29 | if (assertedGroups.contains(&ledMap.at(path))) |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 30 | { |
| 31 | assertedGroups.erase(&ledMap.at(path)); |
| 32 | } |
| 33 | } |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 34 | |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 35 | // This will contain the union of what's already in the asserted group |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 36 | group desiredState{}; |
| 37 | for (const auto& grp : assertedGroups) |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 38 | { |
| 39 | desiredState.insert(grp->cbegin(), grp->cend()); |
| 40 | } |
| 41 | |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 42 | // Find difference between Combined and Desired to identify |
| 43 | // which LEDs are getting altered |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 44 | group transient{}; |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 45 | std::set_difference(combinedState.begin(), combinedState.end(), |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 46 | desiredState.begin(), desiredState.end(), |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 47 | std::inserter(transient, transient.begin()), ledComp); |
| 48 | if (transient.size()) |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 49 | { |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 50 | // Find common LEDs between transient and Desired to know if some LEDs |
| 51 | // are changing state and not really getting DeAsserted |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 52 | group ledsTransient{}; |
| 53 | std::set_intersection( |
| 54 | transient.begin(), transient.end(), desiredState.begin(), |
| 55 | desiredState.end(), |
| 56 | std::inserter(ledsTransient, ledsTransient.begin()), ledLess); |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 57 | |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 58 | // Find difference between above 2 to identify those LEDs which are |
| 59 | // really getting DeAsserted |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 60 | std::set_difference(transient.begin(), transient.end(), |
| 61 | ledsTransient.begin(), ledsTransient.end(), |
Vishwanatha Subbanna | 447d0c8 | 2016-12-19 14:12:42 +0530 | [diff] [blame] | 62 | std::inserter(ledsDeAssert, ledsDeAssert.begin()), |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 63 | ledLess); |
Vishwanatha Subbanna | 447d0c8 | 2016-12-19 14:12:42 +0530 | [diff] [blame] | 64 | |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 65 | // Remove the elements from Current that are being DeAsserted. |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 66 | if (ledsDeAssert.size()) |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 67 | { |
| 68 | // Power off LEDs that are to be really DeAsserted |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 69 | for (auto& it : ledsDeAssert) |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 70 | { |
| 71 | // Update LEDs in "physically asserted" set by removing those |
| 72 | // LEDs which are De-Asserted |
| 73 | auto found = currentState.find(it); |
| 74 | if (found != currentState.end()) |
| 75 | { |
| 76 | currentState.erase(found); |
| 77 | } |
| 78 | } |
| 79 | } |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 80 | } |
| 81 | |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 82 | // Now LEDs that are to be Asserted. These could either be fresh asserts |
| 83 | // -or- change between [On]<-->[Blink] |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 84 | group temp{}; |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 85 | std::unique_copy(desiredState.begin(), desiredState.end(), |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 86 | std::inserter(temp, temp.begin()), ledEqual); |
| 87 | if (temp.size()) |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 88 | { |
| 89 | // Find difference between [desired to be Asserted] and those LEDs |
| 90 | // that are physically asserted currently. |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 91 | std::set_difference( |
| 92 | temp.begin(), temp.end(), currentState.begin(), currentState.end(), |
| 93 | std::inserter(ledsAssert, ledsAssert.begin()), ledComp); |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 94 | } |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 95 | |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 96 | // Update the current actual and desired(the virtual actual) |
| 97 | currentState = std::move(temp); |
| 98 | combinedState = std::move(desiredState); |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 99 | |
| 100 | // If we survive, then set the state accordingly. |
| 101 | return assert; |
| 102 | } |
| 103 | |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 104 | void Manager::setLampTestCallBack( |
| 105 | std::function<bool(group& ledsAssert, group& ledsDeAssert)> callBack) |
| 106 | { |
| 107 | lampTestCallBack = callBack; |
| 108 | } |
| 109 | |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 110 | /** @brief Run through the map and apply action on the LEDs */ |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 111 | void Manager::driveLEDs(group& ledsAssert, group& ledsDeAssert) |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 112 | { |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 113 | #ifdef USE_LAMP_TEST |
| 114 | // Use the lampTestCallBack method and trigger the callback method in the |
| 115 | // lamp test(processLEDUpdates), in this way, all lamp test operations |
| 116 | // are performed in the lamp test class. |
| 117 | if (lampTestCallBack(ledsAssert, ledsDeAssert)) |
| 118 | { |
| 119 | return; |
| 120 | } |
| 121 | #endif |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 122 | // This order of LED operation is important. |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 123 | if (ledsDeAssert.size()) |
| 124 | { |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 125 | for (const auto& it : ledsDeAssert) |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 126 | { |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 127 | std::string objPath = std::string(PHY_LED_PATH) + it.name; |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 128 | log<level::DEBUG>("De-Asserting LED", |
| 129 | entry("NAME=%s", it.name.c_str())); |
tony lee | 6fd9e44 | 2019-04-23 09:09:15 +0800 | [diff] [blame] | 130 | drivePhysicalLED(objPath, Layout::Action::Off, it.dutyOn, |
| 131 | it.period); |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 135 | if (ledsAssert.size()) |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 136 | { |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 137 | for (const auto& it : ledsAssert) |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 138 | { |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 139 | std::string objPath = std::string(PHY_LED_PATH) + it.name; |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 140 | log<level::DEBUG>("Asserting LED", |
| 141 | entry("NAME=%s", it.name.c_str())); |
tony lee | 6fd9e44 | 2019-04-23 09:09:15 +0800 | [diff] [blame] | 142 | drivePhysicalLED(objPath, it.action, it.dutyOn, it.period); |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 143 | } |
| 144 | } |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 145 | return; |
| 146 | } |
| 147 | |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 148 | // Calls into driving physical LED post choosing the action |
| 149 | void Manager::drivePhysicalLED(const std::string& objPath, |
tony lee | 6fd9e44 | 2019-04-23 09:09:15 +0800 | [diff] [blame] | 150 | Layout::Action action, uint8_t dutyOn, |
| 151 | const uint16_t period) |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 152 | { |
George Liu | 1c737af | 2020-10-16 09:07:02 +0800 | [diff] [blame] | 153 | try |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 154 | { |
George Liu | 1c737af | 2020-10-16 09:07:02 +0800 | [diff] [blame] | 155 | // If Blink, set its property |
| 156 | if (action == Layout::Action::Blink) |
| 157 | { |
| 158 | PropertyValue dutyOnValue{dutyOn}; |
| 159 | PropertyValue periodValue{period}; |
| 160 | |
| 161 | dBusHandler.setProperty(objPath, PHY_LED_IFACE, "DutyOn", |
| 162 | dutyOnValue); |
| 163 | dBusHandler.setProperty(objPath, PHY_LED_IFACE, "Period", |
| 164 | periodValue); |
| 165 | } |
| 166 | |
| 167 | PropertyValue actionValue{getPhysicalAction(action)}; |
| 168 | dBusHandler.setProperty(objPath, PHY_LED_IFACE, "State", actionValue); |
| 169 | } |
| 170 | catch (const std::exception& e) |
| 171 | { |
| 172 | log<level::ERR>("Error setting property for physical LED", |
| 173 | entry("ERROR=%s", e.what()), |
| 174 | entry("OBJECT_PATH=%s", objPath.c_str())); |
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 | |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 177 | return; |
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 | |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 202 | } // namespace led |
| 203 | } // namespace phosphor |