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