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