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