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> |
Vishwanatha Subbanna | 4fa9248 | 2017-03-10 14:39:20 +0530 | [diff] [blame] | 5 | #include <xyz/openbmc_project/Led/Physical/server.hpp> |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 6 | #include "manager.hpp" |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 7 | namespace phosphor |
| 8 | { |
| 9 | namespace led |
| 10 | { |
| 11 | |
| 12 | // Assert -or- De-assert |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 13 | bool Manager::setGroupState(const std::string& path, bool assert, |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 14 | group& ledsAssert, group& ledsDeAssert) |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 15 | { |
| 16 | if (assert) |
| 17 | { |
| 18 | assertedGroups.insert(&ledMap.at(path)); |
| 19 | } |
| 20 | else |
| 21 | { |
| 22 | auto search = assertedGroups.find(&ledMap.at(path)); |
| 23 | if (search != assertedGroups.end()) |
| 24 | { |
| 25 | assertedGroups.erase(&ledMap.at(path)); |
| 26 | } |
| 27 | } |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 28 | |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 29 | // This will contain the union of what's already in the asserted group |
| 30 | group desiredState {}; |
| 31 | for(const auto& grp : assertedGroups) |
| 32 | { |
| 33 | desiredState.insert(grp->cbegin(), grp->cend()); |
| 34 | } |
| 35 | |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 36 | // Find difference between Combined and Desired to identify |
| 37 | // which LEDs are getting altered |
Vishwanatha Subbanna | 447d0c8 | 2016-12-19 14:12:42 +0530 | [diff] [blame] | 38 | group transient {}; |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 39 | std::set_difference(combinedState.begin(), combinedState.end(), |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 40 | desiredState.begin(), desiredState.end(), |
Vishwanatha Subbanna | cd569d2 | 2017-05-03 12:46:14 +0530 | [diff] [blame] | 41 | std::inserter(transient, transient.begin()), |
| 42 | ledComp); |
Vishwanatha Subbanna | 447d0c8 | 2016-12-19 14:12:42 +0530 | [diff] [blame] | 43 | if(transient.size()) |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 44 | { |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 45 | // Find common LEDs between transient and Desired to know if some LEDs |
| 46 | // are changing state and not really getting DeAsserted |
| 47 | group ledsTransient {}; |
| 48 | std::set_intersection(transient.begin(),transient.end(), |
| 49 | desiredState.begin(), desiredState.end(), |
| 50 | std::inserter(ledsTransient, ledsTransient.begin()), |
| 51 | ledLess); |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 52 | |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 53 | // Find difference between above 2 to identify those LEDs which are |
| 54 | // really getting DeAsserted |
| 55 | std::set_difference(transient.begin(),transient.end(), |
| 56 | ledsTransient.begin(),ledsTransient.end(), |
Vishwanatha Subbanna | 447d0c8 | 2016-12-19 14:12:42 +0530 | [diff] [blame] | 57 | std::inserter(ledsDeAssert, ledsDeAssert.begin()), |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 58 | ledLess); |
Vishwanatha Subbanna | 447d0c8 | 2016-12-19 14:12:42 +0530 | [diff] [blame] | 59 | |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 60 | // Remove the elements from Current that are being DeAsserted. |
| 61 | if(ledsDeAssert.size()) |
| 62 | { |
| 63 | // Power off LEDs that are to be really DeAsserted |
| 64 | for (auto& it:ledsDeAssert) |
| 65 | { |
| 66 | // Update LEDs in "physically asserted" set by removing those |
| 67 | // LEDs which are De-Asserted |
| 68 | auto found = currentState.find(it); |
| 69 | if (found != currentState.end()) |
| 70 | { |
| 71 | currentState.erase(found); |
| 72 | } |
| 73 | } |
| 74 | } |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 75 | } |
| 76 | |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 77 | // Now LEDs that are to be Asserted. These could either be fresh asserts |
| 78 | // -or- change between [On]<-->[Blink] |
| 79 | group temp {}; |
| 80 | std::unique_copy(desiredState.begin(), desiredState.end(), |
| 81 | std::inserter(temp, temp.begin()), |
| 82 | ledEqual); |
| 83 | if(temp.size()) |
| 84 | { |
| 85 | // Find difference between [desired to be Asserted] and those LEDs |
| 86 | // that are physically asserted currently. |
| 87 | std::set_difference(temp.begin(), temp.end(), |
| 88 | currentState.begin(), currentState.end(), |
| 89 | std::inserter(ledsAssert, ledsAssert.begin()), |
| 90 | ledComp); |
| 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 | { |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 117 | for (const auto& it: ledsDeAssert) |
| 118 | { |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 119 | std::string objPath = std::string(PHY_LED_PATH) + it.name; |
Alexander Soldatov | f3c0341 | 2018-04-12 14:40:23 +0300 | [diff] [blame^] | 120 | 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] | 121 | drivePhysicalLED(objPath, Layout::Action::Off, it.dutyOn); |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
Vishwanatha Subbanna | 447d0c8 | 2016-12-19 14:12:42 +0530 | [diff] [blame] | 125 | if(ledsAssert.size()) |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 126 | { |
Vishwanatha Subbanna | 447d0c8 | 2016-12-19 14:12:42 +0530 | [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; |
Alexander Soldatov | f3c0341 | 2018-04-12 14:40:23 +0300 | [diff] [blame^] | 130 | log<level::DEBUG>("Asserting LED", entry("NAME=%s", it.name.c_str())); |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 131 | drivePhysicalLED(objPath, it.action, it.dutyOn); |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 132 | } |
| 133 | } |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 134 | return; |
| 135 | } |
| 136 | |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 137 | // Calls into driving physical LED post choosing the action |
| 138 | void Manager::drivePhysicalLED(const std::string& objPath, |
| 139 | Layout::Action action, |
| 140 | uint8_t dutyOn) |
| 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", |
| 148 | entry("PATH=%s",objPath.c_str())); |
| 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 | |
| 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. |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 170 | if(action == Layout::Action::On) |
| 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 | } |
| 174 | else if(action == Layout::Action::Blink) |
| 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 |
| 190 | constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper"; |
Leonel Gonzalez | 76f5834 | 2017-03-16 13:47:52 -0500 | [diff] [blame] | 191 | constexpr auto MAPPER_OBJ_PATH = "/xyz/openbmc_project/object_mapper"; |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 192 | constexpr auto MAPPER_IFACE = "xyz.openbmc_project.ObjectMapper"; |
| 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", |
| 213 | entry("PATH=%s",PHY_LED_PATH)); |
| 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 |
Vishwanatha Subbanna | dcc3f38 | 2017-03-24 20:15:02 +0530 | [diff] [blame] | 218 | std::map<std::string, std::map<std::string, |
| 219 | std::vector<std::string>>> 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 |
| 222 | reply.read(objectTree); |
| 223 | if (objectTree.empty()) |
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 | log<level::INFO>("Physical LED lookup did not return any services", |
| 226 | entry("PATH=%s",PHY_LED_PATH)); |
| 227 | return; |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 228 | } |
| 229 | |
Vishwanatha Subbanna | dcc3f38 | 2017-03-24 20:15:02 +0530 | [diff] [blame] | 230 | // Now construct our path -> Service name map. |
| 231 | for (const auto& iter : objectTree) |
| 232 | { |
| 233 | phyLeds.emplace(iter.first, iter.second.begin()->first); |
| 234 | } |
| 235 | return; |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 236 | } |
| 237 | |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 238 | } // namespace led |
| 239 | } // namespace phosphor |