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