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 | { |
| 29 | auto search = assertedGroups.find(&ledMap.at(path)); |
| 30 | if (search != assertedGroups.end()) |
| 31 | { |
| 32 | assertedGroups.erase(&ledMap.at(path)); |
| 33 | } |
| 34 | } |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 35 | |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 36 | // 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] | 37 | group desiredState{}; |
| 38 | for (const auto& grp : assertedGroups) |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 39 | { |
| 40 | desiredState.insert(grp->cbegin(), grp->cend()); |
| 41 | } |
| 42 | |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 43 | // Find difference between Combined and Desired to identify |
| 44 | // which LEDs are getting altered |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 45 | group transient{}; |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 46 | std::set_difference(combinedState.begin(), combinedState.end(), |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 47 | desiredState.begin(), desiredState.end(), |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 48 | std::inserter(transient, transient.begin()), ledComp); |
| 49 | if (transient.size()) |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 50 | { |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 51 | // Find common LEDs between transient and Desired to know if some LEDs |
| 52 | // are changing state and not really getting DeAsserted |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 53 | group ledsTransient{}; |
| 54 | std::set_intersection( |
| 55 | transient.begin(), transient.end(), desiredState.begin(), |
| 56 | desiredState.end(), |
| 57 | std::inserter(ledsTransient, ledsTransient.begin()), ledLess); |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 58 | |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 59 | // Find difference between above 2 to identify those LEDs which are |
| 60 | // really getting DeAsserted |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 61 | std::set_difference(transient.begin(), transient.end(), |
| 62 | ledsTransient.begin(), ledsTransient.end(), |
Vishwanatha Subbanna | 447d0c8 | 2016-12-19 14:12:42 +0530 | [diff] [blame] | 63 | std::inserter(ledsDeAssert, ledsDeAssert.begin()), |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 64 | ledLess); |
Vishwanatha Subbanna | 447d0c8 | 2016-12-19 14:12:42 +0530 | [diff] [blame] | 65 | |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 66 | // Remove the elements from Current that are being DeAsserted. |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 67 | if (ledsDeAssert.size()) |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 68 | { |
| 69 | // Power off LEDs that are to be really DeAsserted |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 70 | for (auto& it : ledsDeAssert) |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 71 | { |
| 72 | // Update LEDs in "physically asserted" set by removing those |
| 73 | // LEDs which are De-Asserted |
| 74 | auto found = currentState.find(it); |
| 75 | if (found != currentState.end()) |
| 76 | { |
| 77 | currentState.erase(found); |
| 78 | } |
| 79 | } |
| 80 | } |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 81 | } |
| 82 | |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 83 | // Now LEDs that are to be Asserted. These could either be fresh asserts |
| 84 | // -or- change between [On]<-->[Blink] |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 85 | group temp{}; |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 86 | std::unique_copy(desiredState.begin(), desiredState.end(), |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 87 | std::inserter(temp, temp.begin()), ledEqual); |
| 88 | if (temp.size()) |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 89 | { |
| 90 | // Find difference between [desired to be Asserted] and those LEDs |
| 91 | // that are physically asserted currently. |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 92 | std::set_difference( |
| 93 | temp.begin(), temp.end(), currentState.begin(), currentState.end(), |
| 94 | std::inserter(ledsAssert, ledsAssert.begin()), ledComp); |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 95 | } |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 96 | |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 97 | // Update the current actual and desired(the virtual actual) |
| 98 | currentState = std::move(temp); |
| 99 | combinedState = std::move(desiredState); |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 100 | |
| 101 | // If we survive, then set the state accordingly. |
| 102 | return assert; |
| 103 | } |
| 104 | |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 105 | void Manager::setLampTestCallBack( |
| 106 | std::function<bool(group& ledsAssert, group& ledsDeAssert)> callBack) |
| 107 | { |
| 108 | lampTestCallBack = callBack; |
| 109 | } |
| 110 | |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 111 | /** @brief Run through the map and apply action on the LEDs */ |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 112 | void Manager::driveLEDs(group& ledsAssert, group& ledsDeAssert) |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 113 | { |
George Liu | b615162 | 2020-11-23 18:16:18 +0800 | [diff] [blame] | 114 | #ifdef USE_LAMP_TEST |
| 115 | // Use the lampTestCallBack method and trigger the callback method in the |
| 116 | // lamp test(processLEDUpdates), in this way, all lamp test operations |
| 117 | // are performed in the lamp test class. |
| 118 | if (lampTestCallBack(ledsAssert, ledsDeAssert)) |
| 119 | { |
| 120 | return; |
| 121 | } |
| 122 | #endif |
| 123 | |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 124 | // This order of LED operation is important. |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 125 | if (ledsDeAssert.size()) |
| 126 | { |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 127 | for (const auto& it : ledsDeAssert) |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 128 | { |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 129 | std::string objPath = std::string(PHY_LED_PATH) + it.name; |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 130 | log<level::DEBUG>("De-Asserting LED", |
| 131 | entry("NAME=%s", it.name.c_str())); |
tony lee | 6fd9e44 | 2019-04-23 09:09:15 +0800 | [diff] [blame] | 132 | drivePhysicalLED(objPath, Layout::Action::Off, it.dutyOn, |
| 133 | it.period); |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 137 | if (ledsAssert.size()) |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 138 | { |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 139 | for (const auto& it : ledsAssert) |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 140 | { |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 141 | std::string objPath = std::string(PHY_LED_PATH) + it.name; |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 142 | log<level::DEBUG>("Asserting LED", |
| 143 | entry("NAME=%s", it.name.c_str())); |
tony lee | 6fd9e44 | 2019-04-23 09:09:15 +0800 | [diff] [blame] | 144 | drivePhysicalLED(objPath, it.action, it.dutyOn, it.period); |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 145 | } |
| 146 | } |
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 |
| 151 | void Manager::drivePhysicalLED(const std::string& objPath, |
tony lee | 6fd9e44 | 2019-04-23 09:09:15 +0800 | [diff] [blame] | 152 | Layout::Action action, uint8_t dutyOn, |
| 153 | const uint16_t period) |
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 | try |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 156 | { |
George Liu | 1c737af | 2020-10-16 09:07:02 +0800 | [diff] [blame] | 157 | // If Blink, set its property |
| 158 | if (action == Layout::Action::Blink) |
| 159 | { |
| 160 | PropertyValue dutyOnValue{dutyOn}; |
| 161 | PropertyValue periodValue{period}; |
| 162 | |
| 163 | dBusHandler.setProperty(objPath, PHY_LED_IFACE, "DutyOn", |
| 164 | dutyOnValue); |
| 165 | dBusHandler.setProperty(objPath, PHY_LED_IFACE, "Period", |
| 166 | periodValue); |
| 167 | } |
| 168 | |
| 169 | PropertyValue actionValue{getPhysicalAction(action)}; |
| 170 | dBusHandler.setProperty(objPath, PHY_LED_IFACE, "State", actionValue); |
| 171 | } |
| 172 | catch (const std::exception& e) |
| 173 | { |
| 174 | log<level::ERR>("Error setting property for physical LED", |
| 175 | entry("ERROR=%s", e.what()), |
| 176 | entry("OBJECT_PATH=%s", objPath.c_str())); |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 177 | } |
Vishwanatha Subbanna | dcc3f38 | 2017-03-24 20:15:02 +0530 | [diff] [blame] | 178 | |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 179 | return; |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | /** @brief Returns action string based on enum */ |
Vishwanatha Subbanna | 4fa9248 | 2017-03-10 14:39:20 +0530 | [diff] [blame] | 183 | std::string Manager::getPhysicalAction(Layout::Action action) |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 184 | { |
Vishwanatha Subbanna | 4fa9248 | 2017-03-10 14:39:20 +0530 | [diff] [blame] | 185 | namespace server = sdbusplus::xyz::openbmc_project::Led::server; |
| 186 | |
| 187 | // TODO: openbmc/phosphor-led-manager#5 |
| 188 | // Somehow need to use the generated Action enum than giving one |
| 189 | // in ledlayout. |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 190 | if (action == Layout::Action::On) |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 191 | { |
Vishwanatha Subbanna | 4fa9248 | 2017-03-10 14:39:20 +0530 | [diff] [blame] | 192 | return server::convertForMessage(server::Physical::Action::On); |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 193 | } |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 194 | else if (action == Layout::Action::Blink) |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 195 | { |
Vishwanatha Subbanna | 4fa9248 | 2017-03-10 14:39:20 +0530 | [diff] [blame] | 196 | return server::convertForMessage(server::Physical::Action::Blink); |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 197 | } |
| 198 | else |
| 199 | { |
Vishwanatha Subbanna | 4fa9248 | 2017-03-10 14:39:20 +0530 | [diff] [blame] | 200 | return server::convertForMessage(server::Physical::Action::Off); |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | |
George Liu | 4c5f533 | 2020-10-10 17:04:28 +0800 | [diff] [blame] | 204 | // Set OperationalStatus functional according to the asserted state of the group |
| 205 | void Manager::setOperationalStatus(const std::string& path, bool value) const |
| 206 | { |
| 207 | using namespace phosphor::logging; |
| 208 | |
| 209 | // Get endpoints from the rType |
| 210 | std::string fru = path + "/fru_fault"; |
| 211 | |
| 212 | // endpoint contains the vector of strings, where each string is a Inventory |
| 213 | // D-Bus object that this, associated with this LED Group D-Bus object |
| 214 | // pointed to by fru_fault |
| 215 | PropertyValue endpoint{}; |
| 216 | |
| 217 | try |
| 218 | { |
| 219 | endpoint = dBusHandler.getProperty( |
| 220 | fru, "xyz.openbmc_project.Association", "endpoints"); |
| 221 | } |
| 222 | catch (const sdbusplus::exception::SdBusError& e) |
| 223 | { |
| 224 | log<level::ERR>("Failed to get endpoints property", |
| 225 | entry("ERROR=%s", e.what()), |
| 226 | entry("PATH=%s", fru.c_str())); |
| 227 | return; |
| 228 | } |
| 229 | |
| 230 | auto& endpoints = std::get<std::vector<std::string>>(endpoint); |
| 231 | if (endpoints.empty()) |
| 232 | { |
| 233 | return; |
| 234 | } |
| 235 | |
| 236 | for (const auto& fruInstancePath : endpoints) |
| 237 | { |
| 238 | // Set OperationalStatus by fru instance path |
| 239 | try |
| 240 | { |
| 241 | PropertyValue functionalValue{value}; |
| 242 | dBusHandler.setProperty( |
| 243 | fruInstancePath, |
| 244 | "xyz.openbmc_project.State.Decorator.OperationalStatus", |
| 245 | "Functional", functionalValue); |
| 246 | } |
| 247 | catch (const sdbusplus::exception::SdBusError& e) |
| 248 | { |
| 249 | log<level::ERR>("Failed to set Functional property", |
| 250 | entry("ERROR=%s", e.what()), |
| 251 | entry("PATH=%s", fruInstancePath.c_str())); |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 256 | } // namespace led |
| 257 | } // namespace phosphor |