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())); |
tony lee | 6fd9e44 | 2019-04-23 09:09:15 +0800 | [diff] [blame] | 121 | drivePhysicalLED(objPath, Layout::Action::Off, it.dutyOn, |
| 122 | it.period); |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 126 | if (ledsAssert.size()) |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 127 | { |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 128 | for (const auto& it : ledsAssert) |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 129 | { |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 130 | std::string objPath = std::string(PHY_LED_PATH) + it.name; |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 131 | log<level::DEBUG>("Asserting LED", |
| 132 | entry("NAME=%s", it.name.c_str())); |
tony lee | 6fd9e44 | 2019-04-23 09:09:15 +0800 | [diff] [blame] | 133 | drivePhysicalLED(objPath, it.action, it.dutyOn, it.period); |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 134 | } |
| 135 | } |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 136 | return; |
| 137 | } |
| 138 | |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 139 | // Calls into driving physical LED post choosing the action |
| 140 | void Manager::drivePhysicalLED(const std::string& objPath, |
tony lee | 6fd9e44 | 2019-04-23 09:09:15 +0800 | [diff] [blame] | 141 | Layout::Action action, uint8_t dutyOn, |
| 142 | const uint16_t period) |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 143 | { |
Vishwanatha Subbanna | dcc3f38 | 2017-03-24 20:15:02 +0530 | [diff] [blame] | 144 | using namespace phosphor::logging; |
| 145 | |
| 146 | auto service = phyLeds.find(objPath); |
| 147 | if (service == phyLeds.end() || service->second.empty()) |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 148 | { |
Vishwanatha Subbanna | dcc3f38 | 2017-03-24 20:15:02 +0530 | [diff] [blame] | 149 | log<level::ERR>("No service providers for physical LED", |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 150 | entry("PATH=%s", objPath.c_str())); |
Vishwanatha Subbanna | dcc3f38 | 2017-03-24 20:15:02 +0530 | [diff] [blame] | 151 | return; |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 152 | } |
Vishwanatha Subbanna | dcc3f38 | 2017-03-24 20:15:02 +0530 | [diff] [blame] | 153 | |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 154 | // If Blink, set its property |
| 155 | if (action == Layout::Action::Blink) |
| 156 | { |
| 157 | drivePhysicalLED(service->second, objPath, "DutyOn", dutyOn); |
tony lee | 6fd9e44 | 2019-04-23 09:09:15 +0800 | [diff] [blame] | 158 | drivePhysicalLED(service->second, objPath, "Period", period); |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 159 | } |
| 160 | drivePhysicalLED(service->second, objPath, "State", |
| 161 | getPhysicalAction(action)); |
| 162 | return; |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | /** @brief Returns action string based on enum */ |
Vishwanatha Subbanna | 4fa9248 | 2017-03-10 14:39:20 +0530 | [diff] [blame] | 166 | std::string Manager::getPhysicalAction(Layout::Action action) |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 167 | { |
Vishwanatha Subbanna | 4fa9248 | 2017-03-10 14:39:20 +0530 | [diff] [blame] | 168 | namespace server = sdbusplus::xyz::openbmc_project::Led::server; |
| 169 | |
| 170 | // TODO: openbmc/phosphor-led-manager#5 |
| 171 | // Somehow need to use the generated Action enum than giving one |
| 172 | // in ledlayout. |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 173 | if (action == Layout::Action::On) |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 174 | { |
Vishwanatha Subbanna | 4fa9248 | 2017-03-10 14:39:20 +0530 | [diff] [blame] | 175 | return server::convertForMessage(server::Physical::Action::On); |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 176 | } |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 177 | else if (action == Layout::Action::Blink) |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 178 | { |
Vishwanatha Subbanna | 4fa9248 | 2017-03-10 14:39:20 +0530 | [diff] [blame] | 179 | return server::convertForMessage(server::Physical::Action::Blink); |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 180 | } |
| 181 | else |
| 182 | { |
Vishwanatha Subbanna | 4fa9248 | 2017-03-10 14:39:20 +0530 | [diff] [blame] | 183 | return server::convertForMessage(server::Physical::Action::Off); |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
Vishwanatha Subbanna | dcc3f38 | 2017-03-24 20:15:02 +0530 | [diff] [blame] | 187 | /** Populates a map with physical LED paths to its service providers */ |
| 188 | void Manager::populateObjectMap() |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 189 | { |
| 190 | using namespace phosphor::logging; |
| 191 | |
| 192 | // Mapper dbus constructs |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 193 | constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper"; |
| 194 | constexpr auto MAPPER_OBJ_PATH = "/xyz/openbmc_project/object_mapper"; |
| 195 | constexpr auto MAPPER_IFACE = "xyz.openbmc_project.ObjectMapper"; |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 196 | |
Vishwanatha Subbanna | dcc3f38 | 2017-03-24 20:15:02 +0530 | [diff] [blame] | 197 | // Needed to be passed to get the SubTree level |
| 198 | auto depth = 0; |
| 199 | |
| 200 | // Clean start |
| 201 | phyLeds.clear(); |
| 202 | |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 203 | // Make a mapper call |
| 204 | auto mapperCall = bus.new_method_call(MAPPER_BUSNAME, MAPPER_OBJ_PATH, |
Vishwanatha Subbanna | dcc3f38 | 2017-03-24 20:15:02 +0530 | [diff] [blame] | 205 | MAPPER_IFACE, "GetSubTree"); |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 206 | // Cook rest of the things. |
Vishwanatha Subbanna | dcc3f38 | 2017-03-24 20:15:02 +0530 | [diff] [blame] | 207 | mapperCall.append(PHY_LED_PATH); |
| 208 | mapperCall.append(depth); |
| 209 | mapperCall.append(std::vector<std::string>({PHY_LED_IFACE})); |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 210 | |
| 211 | auto reply = bus.call(mapperCall); |
| 212 | if (reply.is_method_error()) |
| 213 | { |
| 214 | // Its okay if we do not see a corresponding physical LED. |
Vishwanatha Subbanna | dcc3f38 | 2017-03-24 20:15:02 +0530 | [diff] [blame] | 215 | log<level::INFO>("Error looking up Physical LED services", |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 216 | entry("PATH=%s", PHY_LED_PATH)); |
Vishwanatha Subbanna | dcc3f38 | 2017-03-24 20:15:02 +0530 | [diff] [blame] | 217 | return; |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | // Response by mapper in the case of success |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 221 | std::map<std::string, std::map<std::string, std::vector<std::string>>> |
| 222 | objectTree; |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 223 | |
Vishwanatha Subbanna | dcc3f38 | 2017-03-24 20:15:02 +0530 | [diff] [blame] | 224 | // This is the dict of object paths - service names - interfaces |
William A. Kennington III | 151122a | 2018-05-15 12:00:32 -0700 | [diff] [blame] | 225 | try |
| 226 | { |
| 227 | reply.read(objectTree); |
| 228 | } |
| 229 | catch (const sdbusplus::exception::SdBusError& e) |
| 230 | { |
| 231 | log<level::ERR>("Failed to parse Physical LED service lookup", |
| 232 | entry("ERROR=%s", e.what()), |
| 233 | entry("REPLY_SIG=%s", reply.get_signature())); |
| 234 | return; |
| 235 | } |
Vishwanatha Subbanna | dcc3f38 | 2017-03-24 20:15:02 +0530 | [diff] [blame] | 236 | if (objectTree.empty()) |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 237 | { |
Vishwanatha Subbanna | dcc3f38 | 2017-03-24 20:15:02 +0530 | [diff] [blame] | 238 | log<level::INFO>("Physical LED lookup did not return any services", |
Patrick Venture | 91ac8d3 | 2018-11-01 17:03:22 -0700 | [diff] [blame] | 239 | entry("PATH=%s", PHY_LED_PATH)); |
Vishwanatha Subbanna | dcc3f38 | 2017-03-24 20:15:02 +0530 | [diff] [blame] | 240 | return; |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 241 | } |
| 242 | |
Vishwanatha Subbanna | dcc3f38 | 2017-03-24 20:15:02 +0530 | [diff] [blame] | 243 | // Now construct our path -> Service name map. |
| 244 | for (const auto& iter : objectTree) |
| 245 | { |
| 246 | phyLeds.emplace(iter.first, iter.second.begin()->first); |
| 247 | } |
| 248 | return; |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 249 | } |
| 250 | |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 251 | } // namespace led |
| 252 | } // namespace phosphor |