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