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 | |
| 15 | // Assert -or- De-assert |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 16 | bool Manager::setGroupState(const std::string& path, bool assert, |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 17 | group& ledsAssert, group& ledsDeAssert) |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 18 | { |
| 19 | if (assert) |
| 20 | { |
| 21 | assertedGroups.insert(&ledMap.at(path)); |
| 22 | } |
| 23 | else |
| 24 | { |
| 25 | auto search = assertedGroups.find(&ledMap.at(path)); |
| 26 | if (search != assertedGroups.end()) |
| 27 | { |
| 28 | assertedGroups.erase(&ledMap.at(path)); |
| 29 | } |
| 30 | } |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 31 | |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 32 | // 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] | 33 | group desiredState{}; |
| 34 | for (const auto& grp : assertedGroups) |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 35 | { |
| 36 | desiredState.insert(grp->cbegin(), grp->cend()); |
| 37 | } |
| 38 | |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 39 | // Find difference between Combined and Desired to identify |
| 40 | // which LEDs are getting altered |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 41 | group transient{}; |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 42 | std::set_difference(combinedState.begin(), combinedState.end(), |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 43 | desiredState.begin(), desiredState.end(), |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 44 | std::inserter(transient, transient.begin()), ledComp); |
| 45 | if (transient.size()) |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 46 | { |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 47 | // Find common LEDs between transient and Desired to know if some LEDs |
| 48 | // are changing state and not really getting DeAsserted |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 49 | group ledsTransient{}; |
| 50 | std::set_intersection( |
| 51 | transient.begin(), transient.end(), desiredState.begin(), |
| 52 | desiredState.end(), |
| 53 | std::inserter(ledsTransient, ledsTransient.begin()), ledLess); |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 54 | |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 55 | // Find difference between above 2 to identify those LEDs which are |
| 56 | // really getting DeAsserted |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 57 | std::set_difference(transient.begin(), transient.end(), |
| 58 | ledsTransient.begin(), ledsTransient.end(), |
Vishwanatha Subbanna | 447d0c8 | 2016-12-19 14:12:42 +0530 | [diff] [blame] | 59 | std::inserter(ledsDeAssert, ledsDeAssert.begin()), |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 60 | ledLess); |
Vishwanatha Subbanna | 447d0c8 | 2016-12-19 14:12:42 +0530 | [diff] [blame] | 61 | |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 62 | // Remove the elements from Current that are being DeAsserted. |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 63 | if (ledsDeAssert.size()) |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 64 | { |
| 65 | // Power off LEDs that are to be really DeAsserted |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 66 | for (auto& it : ledsDeAssert) |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 67 | { |
| 68 | // Update LEDs in "physically asserted" set by removing those |
| 69 | // LEDs which are De-Asserted |
| 70 | auto found = currentState.find(it); |
| 71 | if (found != currentState.end()) |
| 72 | { |
| 73 | currentState.erase(found); |
| 74 | } |
| 75 | } |
| 76 | } |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 77 | } |
| 78 | |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 79 | // Now LEDs that are to be Asserted. These could either be fresh asserts |
| 80 | // -or- change between [On]<-->[Blink] |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 81 | group temp{}; |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 82 | std::unique_copy(desiredState.begin(), desiredState.end(), |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 83 | std::inserter(temp, temp.begin()), ledEqual); |
| 84 | if (temp.size()) |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 85 | { |
| 86 | // Find difference between [desired to be Asserted] and those LEDs |
| 87 | // that are physically asserted currently. |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 88 | std::set_difference( |
| 89 | temp.begin(), temp.end(), currentState.begin(), currentState.end(), |
| 90 | std::inserter(ledsAssert, ledsAssert.begin()), ledComp); |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 91 | } |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 92 | |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 93 | // Update the current actual and desired(the virtual actual) |
| 94 | currentState = std::move(temp); |
| 95 | combinedState = std::move(desiredState); |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 96 | |
| 97 | // If we survive, then set the state accordingly. |
| 98 | return assert; |
| 99 | } |
| 100 | |
| 101 | /** @brief Run through the map and apply action on the LEDs */ |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 102 | void Manager::driveLEDs(group& ledsAssert, group& ledsDeAssert) |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 103 | { |
Alexander Soldatov | f3c0341 | 2018-04-12 14:40:23 +0300 | [diff] [blame] | 104 | using namespace phosphor::logging; |
Vishwanatha Subbanna | dcc3f38 | 2017-03-24 20:15:02 +0530 | [diff] [blame] | 105 | // Map of physical LED dbus paths to their Service providers |
| 106 | populateObjectMap(); |
| 107 | |
| 108 | if (phyLeds.empty()) |
| 109 | { |
| 110 | // Error message is inside the map construction logic. |
| 111 | return; |
| 112 | } |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 113 | |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 114 | // This order of LED operation is important. |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 115 | if (ledsDeAssert.size()) |
| 116 | { |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 117 | for (const auto& it : ledsDeAssert) |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 118 | { |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 119 | std::string objPath = std::string(PHY_LED_PATH) + it.name; |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 120 | log<level::DEBUG>("De-Asserting LED", |
| 121 | entry("NAME=%s", it.name.c_str())); |
tony lee | 6fd9e44 | 2019-04-23 09:09:15 +0800 | [diff] [blame] | 122 | drivePhysicalLED(objPath, Layout::Action::Off, it.dutyOn, |
| 123 | it.period); |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 127 | if (ledsAssert.size()) |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 128 | { |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 129 | for (const auto& it : ledsAssert) |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 130 | { |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 131 | std::string objPath = std::string(PHY_LED_PATH) + it.name; |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 132 | log<level::DEBUG>("Asserting LED", |
| 133 | entry("NAME=%s", it.name.c_str())); |
tony lee | 6fd9e44 | 2019-04-23 09:09:15 +0800 | [diff] [blame] | 134 | drivePhysicalLED(objPath, it.action, it.dutyOn, it.period); |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 135 | } |
| 136 | } |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 137 | return; |
| 138 | } |
| 139 | |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 140 | // Calls into driving physical LED post choosing the action |
| 141 | void Manager::drivePhysicalLED(const std::string& objPath, |
tony lee | 6fd9e44 | 2019-04-23 09:09:15 +0800 | [diff] [blame] | 142 | Layout::Action action, uint8_t dutyOn, |
| 143 | const uint16_t period) |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 144 | { |
Vishwanatha Subbanna | dcc3f38 | 2017-03-24 20:15:02 +0530 | [diff] [blame] | 145 | using namespace phosphor::logging; |
| 146 | |
| 147 | auto service = phyLeds.find(objPath); |
| 148 | if (service == phyLeds.end() || service->second.empty()) |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 149 | { |
Vishwanatha Subbanna | dcc3f38 | 2017-03-24 20:15:02 +0530 | [diff] [blame] | 150 | log<level::ERR>("No service providers for physical LED", |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 151 | entry("PATH=%s", objPath.c_str())); |
Vishwanatha Subbanna | dcc3f38 | 2017-03-24 20:15:02 +0530 | [diff] [blame] | 152 | return; |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 153 | } |
Vishwanatha Subbanna | dcc3f38 | 2017-03-24 20:15:02 +0530 | [diff] [blame] | 154 | |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 155 | // If Blink, set its property |
| 156 | if (action == Layout::Action::Blink) |
| 157 | { |
| 158 | drivePhysicalLED(service->second, objPath, "DutyOn", dutyOn); |
tony lee | 6fd9e44 | 2019-04-23 09:09:15 +0800 | [diff] [blame] | 159 | drivePhysicalLED(service->second, objPath, "Period", period); |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 160 | } |
| 161 | drivePhysicalLED(service->second, objPath, "State", |
| 162 | getPhysicalAction(action)); |
| 163 | return; |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | /** @brief Returns action string based on enum */ |
Vishwanatha Subbanna | 4fa9248 | 2017-03-10 14:39:20 +0530 | [diff] [blame] | 167 | std::string Manager::getPhysicalAction(Layout::Action action) |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 168 | { |
Vishwanatha Subbanna | 4fa9248 | 2017-03-10 14:39:20 +0530 | [diff] [blame] | 169 | namespace server = sdbusplus::xyz::openbmc_project::Led::server; |
| 170 | |
| 171 | // TODO: openbmc/phosphor-led-manager#5 |
| 172 | // Somehow need to use the generated Action enum than giving one |
| 173 | // in ledlayout. |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 174 | if (action == Layout::Action::On) |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 175 | { |
Vishwanatha Subbanna | 4fa9248 | 2017-03-10 14:39:20 +0530 | [diff] [blame] | 176 | return server::convertForMessage(server::Physical::Action::On); |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 177 | } |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 178 | else if (action == Layout::Action::Blink) |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 179 | { |
Vishwanatha Subbanna | 4fa9248 | 2017-03-10 14:39:20 +0530 | [diff] [blame] | 180 | return server::convertForMessage(server::Physical::Action::Blink); |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 181 | } |
| 182 | else |
| 183 | { |
Vishwanatha Subbanna | 4fa9248 | 2017-03-10 14:39:20 +0530 | [diff] [blame] | 184 | return server::convertForMessage(server::Physical::Action::Off); |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
Vishwanatha Subbanna | dcc3f38 | 2017-03-24 20:15:02 +0530 | [diff] [blame] | 188 | /** Populates a map with physical LED paths to its service providers */ |
| 189 | void Manager::populateObjectMap() |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 190 | { |
| 191 | using namespace phosphor::logging; |
| 192 | |
| 193 | // Mapper dbus constructs |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 194 | constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper"; |
| 195 | constexpr auto MAPPER_OBJ_PATH = "/xyz/openbmc_project/object_mapper"; |
| 196 | constexpr auto MAPPER_IFACE = "xyz.openbmc_project.ObjectMapper"; |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 197 | |
Vishwanatha Subbanna | dcc3f38 | 2017-03-24 20:15:02 +0530 | [diff] [blame] | 198 | // Needed to be passed to get the SubTree level |
| 199 | auto depth = 0; |
| 200 | |
| 201 | // Clean start |
| 202 | phyLeds.clear(); |
| 203 | |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 204 | // Make a mapper call |
| 205 | auto mapperCall = bus.new_method_call(MAPPER_BUSNAME, MAPPER_OBJ_PATH, |
Vishwanatha Subbanna | dcc3f38 | 2017-03-24 20:15:02 +0530 | [diff] [blame] | 206 | MAPPER_IFACE, "GetSubTree"); |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 207 | // Cook rest of the things. |
Vishwanatha Subbanna | dcc3f38 | 2017-03-24 20:15:02 +0530 | [diff] [blame] | 208 | mapperCall.append(PHY_LED_PATH); |
| 209 | mapperCall.append(depth); |
| 210 | mapperCall.append(std::vector<std::string>({PHY_LED_IFACE})); |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 211 | |
| 212 | auto reply = bus.call(mapperCall); |
| 213 | if (reply.is_method_error()) |
| 214 | { |
| 215 | // Its okay if we do not see a corresponding physical LED. |
Vishwanatha Subbanna | dcc3f38 | 2017-03-24 20:15:02 +0530 | [diff] [blame] | 216 | log<level::INFO>("Error looking up Physical LED services", |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 217 | entry("PATH=%s", PHY_LED_PATH)); |
Vishwanatha Subbanna | dcc3f38 | 2017-03-24 20:15:02 +0530 | [diff] [blame] | 218 | return; |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | // Response by mapper in the case of success |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 222 | std::map<std::string, std::map<std::string, std::vector<std::string>>> |
| 223 | objectTree; |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 224 | |
Vishwanatha Subbanna | dcc3f38 | 2017-03-24 20:15:02 +0530 | [diff] [blame] | 225 | // This is the dict of object paths - service names - interfaces |
William A. Kennington III | 151122a | 2018-05-15 12:00:32 -0700 | [diff] [blame] | 226 | try |
| 227 | { |
| 228 | reply.read(objectTree); |
| 229 | } |
| 230 | catch (const sdbusplus::exception::SdBusError& e) |
| 231 | { |
| 232 | log<level::ERR>("Failed to parse Physical LED service lookup", |
| 233 | entry("ERROR=%s", e.what()), |
| 234 | entry("REPLY_SIG=%s", reply.get_signature())); |
| 235 | return; |
| 236 | } |
Vishwanatha Subbanna | dcc3f38 | 2017-03-24 20:15:02 +0530 | [diff] [blame] | 237 | if (objectTree.empty()) |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 238 | { |
Vishwanatha Subbanna | dcc3f38 | 2017-03-24 20:15:02 +0530 | [diff] [blame] | 239 | log<level::INFO>("Physical LED lookup did not return any services", |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 240 | entry("PATH=%s", PHY_LED_PATH)); |
Vishwanatha Subbanna | dcc3f38 | 2017-03-24 20:15:02 +0530 | [diff] [blame] | 241 | return; |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 242 | } |
| 243 | |
Vishwanatha Subbanna | dcc3f38 | 2017-03-24 20:15:02 +0530 | [diff] [blame] | 244 | // Now construct our path -> Service name map. |
| 245 | for (const auto& iter : objectTree) |
| 246 | { |
| 247 | phyLeds.emplace(iter.first, iter.second.begin()->first); |
| 248 | } |
| 249 | return; |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 250 | } |
| 251 | |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 252 | } // namespace led |
| 253 | } // namespace phosphor |