Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 1 | #include <iostream> |
| 2 | #include <string> |
| 3 | #include <algorithm> |
| 4 | #include "manager.hpp" |
| 5 | #include "led-gen.hpp" |
| 6 | namespace phosphor |
| 7 | { |
| 8 | namespace led |
| 9 | { |
| 10 | |
| 11 | // Assert -or- De-assert |
| 12 | bool Manager::setGroupState(const std::string& path, bool assert) |
| 13 | { |
| 14 | if (assert) |
| 15 | { |
| 16 | assertedGroups.insert(&ledMap.at(path)); |
| 17 | } |
| 18 | else |
| 19 | { |
| 20 | auto search = assertedGroups.find(&ledMap.at(path)); |
| 21 | if (search != assertedGroups.end()) |
| 22 | { |
| 23 | assertedGroups.erase(&ledMap.at(path)); |
| 24 | } |
| 25 | } |
| 26 | // If something does not go right here, then there should be an sdbusplus |
| 27 | // exception thrown. |
| 28 | driveLEDs(); |
| 29 | |
| 30 | // If we survive, then set the state accordingly. |
| 31 | return assert; |
| 32 | } |
| 33 | |
| 34 | /** @brief Run through the map and apply action on the LEDs */ |
| 35 | void Manager::driveLEDs() |
| 36 | { |
| 37 | // This will contain the union of what's already in the asserted group |
| 38 | group desiredState {}; |
| 39 | for(const auto& grp : assertedGroups) |
| 40 | { |
| 41 | desiredState.insert(grp->cbegin(), grp->cend()); |
| 42 | } |
| 43 | |
Vishwanatha Subbanna | 447d0c8 | 2016-12-19 14:12:42 +0530 | [diff] [blame] | 44 | // Has the LEDs that are either to be turned off -or- want a new assertion |
| 45 | group transient {}; |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 46 | std::set_difference(currentState.begin(), currentState.end(), |
| 47 | desiredState.begin(), desiredState.end(), |
Vishwanatha Subbanna | 447d0c8 | 2016-12-19 14:12:42 +0530 | [diff] [blame] | 48 | std::inserter(transient, transient.begin())); |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 49 | |
Vishwanatha Subbanna | 447d0c8 | 2016-12-19 14:12:42 +0530 | [diff] [blame] | 50 | if(transient.size()) |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 51 | { |
| 52 | // We really do not want the Manager to know how a particular LED |
| 53 | // transitions from State-A --> State-B and all this must be handled by |
| 54 | // the physical LED controller implementation. |
| 55 | // So in this case, Manager really does not want to turn off the |
| 56 | // LEDs and then turning it back on and let the physical LED controller |
| 57 | // handle that. |
| 58 | |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 59 | // If we previously had a FRU in ON state , and then if there was a |
| 60 | // request to make it blink, the end state would now be blink. |
| 61 | // If we either turn off blink / fault, then we need to go back to its |
| 62 | // previous state. |
Vishwanatha Subbanna | 447d0c8 | 2016-12-19 14:12:42 +0530 | [diff] [blame] | 63 | group ledsUpdate {}; |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 64 | std::set_intersection(desiredState.begin(), desiredState.end(), |
Vishwanatha Subbanna | 447d0c8 | 2016-12-19 14:12:42 +0530 | [diff] [blame] | 65 | transient.begin(), transient.end(), |
| 66 | std::inserter(ledsUpdate, ledsUpdate.begin()), |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 67 | ledComp); |
| 68 | |
Vishwanatha Subbanna | 447d0c8 | 2016-12-19 14:12:42 +0530 | [diff] [blame] | 69 | if (ledsUpdate.size()) |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 70 | { |
| 71 | std::cout << "Asserting LEDs again" << std::endl; |
Vishwanatha Subbanna | 447d0c8 | 2016-12-19 14:12:42 +0530 | [diff] [blame] | 72 | for (const auto& it: ledsUpdate) |
| 73 | { |
| 74 | std::cout << "\t{" << it.name << "::" << it.action << "}" |
| 75 | << std::endl; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | // These LEDs are only to be De-Asserted. |
| 80 | group ledsDeAssert {}; |
| 81 | std::set_difference(transient.begin(), transient.end(), |
| 82 | ledsUpdate.begin(), ledsUpdate.end(), |
| 83 | std::inserter(ledsDeAssert, ledsDeAssert.begin()), |
| 84 | ledComp); |
| 85 | |
| 86 | if (ledsDeAssert.size()) |
| 87 | { |
| 88 | std::cout << "De-Asserting LEDs" << std::endl; |
| 89 | for (const auto& it: ledsDeAssert) |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 90 | { |
| 91 | std::cout << "\t{" << it.name << "::" << it.action << "}" |
| 92 | << std::endl; |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // Turn on these |
Vishwanatha Subbanna | 447d0c8 | 2016-12-19 14:12:42 +0530 | [diff] [blame] | 98 | group ledsAssert {}; |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 99 | std::set_difference(desiredState.begin(), desiredState.end(), |
| 100 | currentState.begin(), currentState.end(), |
Vishwanatha Subbanna | 447d0c8 | 2016-12-19 14:12:42 +0530 | [diff] [blame] | 101 | std::inserter(ledsAssert, ledsAssert.begin())); |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 102 | |
Vishwanatha Subbanna | 447d0c8 | 2016-12-19 14:12:42 +0530 | [diff] [blame] | 103 | if(ledsAssert.size()) |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 104 | { |
| 105 | std::cout << "Asserting LEDs" << std::endl; |
Vishwanatha Subbanna | 447d0c8 | 2016-12-19 14:12:42 +0530 | [diff] [blame] | 106 | for (const auto& it: ledsAssert) |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 107 | { |
| 108 | std::cout << "\t{" << it.name << "::" << it.action << "}" |
| 109 | << std::endl; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | // Done.. Save the latest and greatest. |
| 114 | currentState = std::move(desiredState); |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | } // namespace led |
| 119 | } // namespace phosphor |