blob: 8ed9852c53911f41ef3a3af7fdae4c580d09d321 [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
44 // Always Do execute Turn Off and then Turn on since we have the Blink
45 // taking priority over -on-
46 group ledsToDeAssert {};
47
48 std::set_difference(currentState.begin(), currentState.end(),
49 desiredState.begin(), desiredState.end(),
50 std::inserter(ledsToDeAssert, ledsToDeAssert.begin()));
51
52 if(ledsToDeAssert.size())
53 {
54 // We really do not want the Manager to know how a particular LED
55 // transitions from State-A --> State-B and all this must be handled by
56 // the physical LED controller implementation.
57 // So in this case, Manager really does not want to turn off the
58 // LEDs and then turning it back on and let the physical LED controller
59 // handle that.
60
61 // I am still experimenting on the algo..
62 std::cout << "De asserting the LEDs" << std::endl;
63 for (const auto& it: ledsToDeAssert)
64 {
65 std::cout << "\t{" << it.name << "::" << it.action << "}"
66 << std::endl;
67 }
68
69 // If we previously had a FRU in ON state , and then if there was a
70 // request to make it blink, the end state would now be blink.
71 // If we either turn off blink / fault, then we need to go back to its
72 // previous state.
73 group ledsToReAssert {};
74 std::set_intersection(desiredState.begin(), desiredState.end(),
75 ledsToDeAssert.begin(), ledsToDeAssert.end(),
76 std::inserter(ledsToReAssert, ledsToReAssert.begin()),
77 ledComp);
78
79 if (ledsToReAssert.size())
80 {
81 std::cout << "Asserting LEDs again" << std::endl;
82 for (const auto& it: ledsToReAssert)
83 {
84 std::cout << "\t{" << it.name << "::" << it.action << "}"
85 << std::endl;
86 }
87 }
88 }
89
90 // Turn on these
91 group ledsToAssert {};
92 std::set_difference(desiredState.begin(), desiredState.end(),
93 currentState.begin(), currentState.end(),
94 std::inserter(ledsToAssert, ledsToAssert.begin()));
95
96 if(ledsToAssert.size())
97 {
98 std::cout << "Asserting LEDs" << std::endl;
99 for (const auto& it: ledsToAssert)
100 {
101 std::cout << "\t{" << it.name << "::" << it.action << "}"
102 << std::endl;
103 }
104 }
105
106 // Done.. Save the latest and greatest.
107 currentState = std::move(desiredState);
108 return;
109}
110
111} // namespace led
112} // namespace phosphor