blob: f4b85c720c8fe4079a8d8fab6e0e4ba4cf6a64eb [file] [log] [blame]
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +05301#include <iostream>
2#include <string>
3#include <algorithm>
4#include "manager.hpp"
5#include "led-gen.hpp"
6namespace phosphor
7{
8namespace led
9{
10
11// Assert -or- De-assert
12bool 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 */
35void 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 Subbanna447d0c82016-12-19 14:12:42 +053044 // Has the LEDs that are either to be turned off -or- want a new assertion
45 group transient {};
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053046 std::set_difference(currentState.begin(), currentState.end(),
47 desiredState.begin(), desiredState.end(),
Vishwanatha Subbanna447d0c82016-12-19 14:12:42 +053048 std::inserter(transient, transient.begin()));
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053049
Vishwanatha Subbanna447d0c82016-12-19 14:12:42 +053050 if(transient.size())
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053051 {
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 Subbanna4c8c72b2016-11-29 23:02:06 +053059 // 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 Subbanna447d0c82016-12-19 14:12:42 +053063 group ledsUpdate {};
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053064 std::set_intersection(desiredState.begin(), desiredState.end(),
Vishwanatha Subbanna447d0c82016-12-19 14:12:42 +053065 transient.begin(), transient.end(),
66 std::inserter(ledsUpdate, ledsUpdate.begin()),
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053067 ledComp);
68
Vishwanatha Subbanna447d0c82016-12-19 14:12:42 +053069 if (ledsUpdate.size())
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053070 {
71 std::cout << "Asserting LEDs again" << std::endl;
Vishwanatha Subbanna447d0c82016-12-19 14:12:42 +053072 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 Subbanna4c8c72b2016-11-29 23:02:06 +053090 {
91 std::cout << "\t{" << it.name << "::" << it.action << "}"
92 << std::endl;
93 }
94 }
95 }
96
97 // Turn on these
Vishwanatha Subbanna447d0c82016-12-19 14:12:42 +053098 group ledsAssert {};
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053099 std::set_difference(desiredState.begin(), desiredState.end(),
100 currentState.begin(), currentState.end(),
Vishwanatha Subbanna447d0c82016-12-19 14:12:42 +0530101 std::inserter(ledsAssert, ledsAssert.begin()));
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +0530102
Vishwanatha Subbanna447d0c82016-12-19 14:12:42 +0530103 if(ledsAssert.size())
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +0530104 {
105 std::cout << "Asserting LEDs" << std::endl;
Vishwanatha Subbanna447d0c82016-12-19 14:12:42 +0530106 for (const auto& it: ledsAssert)
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +0530107 {
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