blob: e4137d3c9ee9b20126d2b4087ede957ad8b61fe3 [file] [log] [blame]
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +05301#include <iostream>
2#include <string>
3#include <algorithm>
4#include "manager.hpp"
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +05305namespace phosphor
6{
7namespace led
8{
9
10// Assert -or- De-assert
Vishwanatha Subbannaed490732016-12-20 15:59:29 +053011bool Manager::setGroupState(const std::string& path, bool assert,
12 group& ledsAssert, group& ledsDeAssert,
13 group& ledsUpdate)
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053014{
15 if (assert)
16 {
17 assertedGroups.insert(&ledMap.at(path));
18 }
19 else
20 {
21 auto search = assertedGroups.find(&ledMap.at(path));
22 if (search != assertedGroups.end())
23 {
24 assertedGroups.erase(&ledMap.at(path));
25 }
26 }
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053027
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053028 // This will contain the union of what's already in the asserted group
29 group desiredState {};
30 for(const auto& grp : assertedGroups)
31 {
32 desiredState.insert(grp->cbegin(), grp->cend());
33 }
34
Vishwanatha Subbanna447d0c82016-12-19 14:12:42 +053035 // Has the LEDs that are either to be turned off -or- want a new assertion
36 group transient {};
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053037 std::set_difference(currentState.begin(), currentState.end(),
38 desiredState.begin(), desiredState.end(),
Vishwanatha Subbanna447d0c82016-12-19 14:12:42 +053039 std::inserter(transient, transient.begin()));
Vishwanatha Subbanna447d0c82016-12-19 14:12:42 +053040 if(transient.size())
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053041 {
42 // We really do not want the Manager to know how a particular LED
43 // transitions from State-A --> State-B and all this must be handled by
44 // the physical LED controller implementation.
45 // So in this case, Manager really does not want to turn off the
46 // LEDs and then turning it back on and let the physical LED controller
47 // handle that.
48
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053049 // If we previously had a FRU in ON state , and then if there was a
50 // request to make it blink, the end state would now be blink.
51 // If we either turn off blink / fault, then we need to go back to its
52 // previous state.
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053053 std::set_intersection(desiredState.begin(), desiredState.end(),
Vishwanatha Subbanna447d0c82016-12-19 14:12:42 +053054 transient.begin(), transient.end(),
55 std::inserter(ledsUpdate, ledsUpdate.begin()),
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053056 ledComp);
57
Vishwanatha Subbanna447d0c82016-12-19 14:12:42 +053058 // These LEDs are only to be De-Asserted.
Vishwanatha Subbanna447d0c82016-12-19 14:12:42 +053059 std::set_difference(transient.begin(), transient.end(),
60 ledsUpdate.begin(), ledsUpdate.end(),
61 std::inserter(ledsDeAssert, ledsDeAssert.begin()),
62 ledComp);
63
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053064 }
65
66 // Turn on these
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053067 std::set_difference(desiredState.begin(), desiredState.end(),
68 currentState.begin(), currentState.end(),
Vishwanatha Subbanna447d0c82016-12-19 14:12:42 +053069 std::inserter(ledsAssert, ledsAssert.begin()));
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053070
Vishwanatha Subbannaed490732016-12-20 15:59:29 +053071
72 // Done.. Save the latest and greatest.
73 currentState = std::move(desiredState);
74
75 // If we survive, then set the state accordingly.
76 return assert;
77}
78
79/** @brief Run through the map and apply action on the LEDs */
80void Manager::driveLEDs(group& ledsAssert, group& ledsDeAssert,
81 group& ledsUpdate)
82{
83 // This order of LED operation is important.
84 if (ledsUpdate.size())
85 {
86 std::cout << "Updating LED states between (On <--> Blink)"
87 << std::endl;
88 for (const auto& it: ledsUpdate)
89 {
90 std::cout << "\t{" << it.name << "::" << it.action << "}"
91 << std::endl;
92 }
93 }
94
95 if (ledsDeAssert.size())
96 {
97 std::cout << "De-Asserting LEDs" << std::endl;
98 for (const auto& it: ledsDeAssert)
99 {
100 std::cout << "\t{" << it.name << "::" << it.action << "}"
101 << std::endl;
102 }
103 }
104
Vishwanatha Subbanna447d0c82016-12-19 14:12:42 +0530105 if(ledsAssert.size())
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +0530106 {
107 std::cout << "Asserting LEDs" << std::endl;
Vishwanatha Subbanna447d0c82016-12-19 14:12:42 +0530108 for (const auto& it: ledsAssert)
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +0530109 {
110 std::cout << "\t{" << it.name << "::" << it.action << "}"
Vishwanatha Subbannaed490732016-12-20 15:59:29 +0530111 << std::endl;
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +0530112 }
113 }
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +0530114 return;
115}
116
117} // namespace led
118} // namespace phosphor