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, |
| 14 | group& ledsAssert, group& ledsDeAssert, |
| 15 | group& ledsUpdate) |
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 | 447d0c8 | 2016-12-19 14:12:42 +0530 | [diff] [blame] | 37 | // Has the LEDs that are either to be turned off -or- want a new assertion |
| 38 | group transient {}; |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 39 | std::set_difference(currentState.begin(), currentState.end(), |
| 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 | { |
| 45 | // We really do not want the Manager to know how a particular LED |
| 46 | // transitions from State-A --> State-B and all this must be handled by |
| 47 | // the physical LED controller implementation. |
| 48 | // So in this case, Manager really does not want to turn off the |
| 49 | // LEDs and then turning it back on and let the physical LED controller |
| 50 | // handle that. |
| 51 | |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 52 | // If we previously had a FRU in ON state , and then if there was a |
| 53 | // request to make it blink, the end state would now be blink. |
| 54 | // If we either turn off blink / fault, then we need to go back to its |
| 55 | // previous state. |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 56 | std::set_intersection(desiredState.begin(), desiredState.end(), |
Vishwanatha Subbanna | 447d0c8 | 2016-12-19 14:12:42 +0530 | [diff] [blame] | 57 | transient.begin(), transient.end(), |
| 58 | std::inserter(ledsUpdate, ledsUpdate.begin()), |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 59 | ledComp); |
| 60 | |
Vishwanatha Subbanna | 447d0c8 | 2016-12-19 14:12:42 +0530 | [diff] [blame] | 61 | // These LEDs are only to be De-Asserted. |
Vishwanatha Subbanna | 447d0c8 | 2016-12-19 14:12:42 +0530 | [diff] [blame] | 62 | std::set_difference(transient.begin(), transient.end(), |
| 63 | ledsUpdate.begin(), ledsUpdate.end(), |
| 64 | std::inserter(ledsDeAssert, ledsDeAssert.begin()), |
| 65 | ledComp); |
| 66 | |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | // Turn on these |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 70 | std::set_difference(desiredState.begin(), desiredState.end(), |
| 71 | currentState.begin(), currentState.end(), |
Vishwanatha Subbanna | cd569d2 | 2017-05-03 12:46:14 +0530 | [diff] [blame] | 72 | std::inserter(ledsAssert, ledsAssert.begin()), |
| 73 | ledComp); |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 74 | |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 75 | |
| 76 | // Done.. Save the latest and greatest. |
| 77 | currentState = std::move(desiredState); |
| 78 | |
| 79 | // If we survive, then set the state accordingly. |
| 80 | return assert; |
| 81 | } |
| 82 | |
| 83 | /** @brief Run through the map and apply action on the LEDs */ |
| 84 | void Manager::driveLEDs(group& ledsAssert, group& ledsDeAssert, |
| 85 | group& ledsUpdate) |
| 86 | { |
Vishwanatha Subbanna | dcc3f38 | 2017-03-24 20:15:02 +0530 | [diff] [blame] | 87 | // Map of physical LED dbus paths to their Service providers |
| 88 | populateObjectMap(); |
| 89 | |
| 90 | if (phyLeds.empty()) |
| 91 | { |
| 92 | // Error message is inside the map construction logic. |
| 93 | return; |
| 94 | } |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 95 | |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 96 | // This order of LED operation is important. |
| 97 | if (ledsUpdate.size()) |
| 98 | { |
| 99 | std::cout << "Updating LED states between (On <--> Blink)" |
| 100 | << std::endl; |
| 101 | for (const auto& it: ledsUpdate) |
| 102 | { |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 103 | std::string objPath = std::string(PHY_LED_PATH) + it.name; |
| 104 | drivePhysicalLED(objPath, it.action, it.dutyOn); |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
| 108 | if (ledsDeAssert.size()) |
| 109 | { |
| 110 | std::cout << "De-Asserting LEDs" << std::endl; |
| 111 | for (const auto& it: ledsDeAssert) |
| 112 | { |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 113 | std::string objPath = std::string(PHY_LED_PATH) + it.name; |
| 114 | drivePhysicalLED(objPath, Layout::Action::Off, it.dutyOn); |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
Vishwanatha Subbanna | 447d0c8 | 2016-12-19 14:12:42 +0530 | [diff] [blame] | 118 | if(ledsAssert.size()) |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 119 | { |
| 120 | std::cout << "Asserting LEDs" << std::endl; |
Vishwanatha Subbanna | 447d0c8 | 2016-12-19 14:12:42 +0530 | [diff] [blame] | 121 | for (const auto& it: ledsAssert) |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 122 | { |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 123 | std::string objPath = std::string(PHY_LED_PATH) + it.name; |
| 124 | drivePhysicalLED(objPath, it.action, it.dutyOn); |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 125 | } |
| 126 | } |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 127 | return; |
| 128 | } |
| 129 | |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 130 | // Calls into driving physical LED post choosing the action |
| 131 | void Manager::drivePhysicalLED(const std::string& objPath, |
| 132 | Layout::Action action, |
| 133 | uint8_t dutyOn) |
| 134 | { |
Vishwanatha Subbanna | dcc3f38 | 2017-03-24 20:15:02 +0530 | [diff] [blame] | 135 | using namespace phosphor::logging; |
| 136 | |
| 137 | auto service = phyLeds.find(objPath); |
| 138 | if (service == phyLeds.end() || service->second.empty()) |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 139 | { |
Vishwanatha Subbanna | dcc3f38 | 2017-03-24 20:15:02 +0530 | [diff] [blame] | 140 | log<level::ERR>("No service providers for physical LED", |
| 141 | entry("PATH=%s",objPath.c_str())); |
| 142 | return; |
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 | |
| 145 | // If Blink, set its property |
| 146 | if (action == Layout::Action::Blink) |
| 147 | { |
| 148 | drivePhysicalLED(service->second, objPath, "DutyOn", dutyOn); |
| 149 | } |
| 150 | drivePhysicalLED(service->second, objPath, "State", |
| 151 | getPhysicalAction(action)); |
| 152 | return; |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | /** @brief Returns action string based on enum */ |
Vishwanatha Subbanna | 4fa9248 | 2017-03-10 14:39:20 +0530 | [diff] [blame] | 156 | std::string Manager::getPhysicalAction(Layout::Action action) |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 157 | { |
Vishwanatha Subbanna | 4fa9248 | 2017-03-10 14:39:20 +0530 | [diff] [blame] | 158 | namespace server = sdbusplus::xyz::openbmc_project::Led::server; |
| 159 | |
| 160 | // TODO: openbmc/phosphor-led-manager#5 |
| 161 | // Somehow need to use the generated Action enum than giving one |
| 162 | // in ledlayout. |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 163 | if(action == Layout::Action::On) |
| 164 | { |
Vishwanatha Subbanna | 4fa9248 | 2017-03-10 14:39:20 +0530 | [diff] [blame] | 165 | return server::convertForMessage(server::Physical::Action::On); |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 166 | } |
| 167 | else if(action == Layout::Action::Blink) |
| 168 | { |
Vishwanatha Subbanna | 4fa9248 | 2017-03-10 14:39:20 +0530 | [diff] [blame] | 169 | return server::convertForMessage(server::Physical::Action::Blink); |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 170 | } |
| 171 | else |
| 172 | { |
Vishwanatha Subbanna | 4fa9248 | 2017-03-10 14:39:20 +0530 | [diff] [blame] | 173 | return server::convertForMessage(server::Physical::Action::Off); |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
Vishwanatha Subbanna | dcc3f38 | 2017-03-24 20:15:02 +0530 | [diff] [blame] | 177 | /** Populates a map with physical LED paths to its service providers */ |
| 178 | void Manager::populateObjectMap() |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 179 | { |
| 180 | using namespace phosphor::logging; |
| 181 | |
| 182 | // Mapper dbus constructs |
| 183 | constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper"; |
Leonel Gonzalez | 76f5834 | 2017-03-16 13:47:52 -0500 | [diff] [blame] | 184 | constexpr auto MAPPER_OBJ_PATH = "/xyz/openbmc_project/object_mapper"; |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 185 | constexpr auto MAPPER_IFACE = "xyz.openbmc_project.ObjectMapper"; |
| 186 | |
Vishwanatha Subbanna | dcc3f38 | 2017-03-24 20:15:02 +0530 | [diff] [blame] | 187 | // Needed to be passed to get the SubTree level |
| 188 | auto depth = 0; |
| 189 | |
| 190 | // Clean start |
| 191 | phyLeds.clear(); |
| 192 | |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 193 | // Make a mapper call |
| 194 | auto mapperCall = bus.new_method_call(MAPPER_BUSNAME, MAPPER_OBJ_PATH, |
Vishwanatha Subbanna | dcc3f38 | 2017-03-24 20:15:02 +0530 | [diff] [blame] | 195 | MAPPER_IFACE, "GetSubTree"); |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 196 | // Cook rest of the things. |
Vishwanatha Subbanna | dcc3f38 | 2017-03-24 20:15:02 +0530 | [diff] [blame] | 197 | mapperCall.append(PHY_LED_PATH); |
| 198 | mapperCall.append(depth); |
| 199 | mapperCall.append(std::vector<std::string>({PHY_LED_IFACE})); |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 200 | |
| 201 | auto reply = bus.call(mapperCall); |
| 202 | if (reply.is_method_error()) |
| 203 | { |
| 204 | // Its okay if we do not see a corresponding physical LED. |
Vishwanatha Subbanna | dcc3f38 | 2017-03-24 20:15:02 +0530 | [diff] [blame] | 205 | log<level::INFO>("Error looking up Physical LED services", |
| 206 | entry("PATH=%s",PHY_LED_PATH)); |
| 207 | return; |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | // Response by mapper in the case of success |
Vishwanatha Subbanna | dcc3f38 | 2017-03-24 20:15:02 +0530 | [diff] [blame] | 211 | std::map<std::string, std::map<std::string, |
| 212 | std::vector<std::string>>> objectTree; |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 213 | |
Vishwanatha Subbanna | dcc3f38 | 2017-03-24 20:15:02 +0530 | [diff] [blame] | 214 | // This is the dict of object paths - service names - interfaces |
| 215 | reply.read(objectTree); |
| 216 | if (objectTree.empty()) |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 217 | { |
Vishwanatha Subbanna | dcc3f38 | 2017-03-24 20:15:02 +0530 | [diff] [blame] | 218 | log<level::INFO>("Physical LED lookup did not return any services", |
| 219 | entry("PATH=%s",PHY_LED_PATH)); |
| 220 | return; |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 221 | } |
| 222 | |
Vishwanatha Subbanna | dcc3f38 | 2017-03-24 20:15:02 +0530 | [diff] [blame] | 223 | // Now construct our path -> Service name map. |
| 224 | for (const auto& iter : objectTree) |
| 225 | { |
| 226 | phyLeds.emplace(iter.first, iter.second.begin()->first); |
| 227 | } |
| 228 | return; |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 229 | } |
| 230 | |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 231 | } // namespace led |
| 232 | } // namespace phosphor |