blob: 91f8b53e69829ee663f21c47a5da82d1952cc026 [file] [log] [blame]
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +05301#pragma once
2
Vishwanatha Subbannaed490732016-12-20 15:59:29 +05303#include "ledlayout.hpp"
George Liu1c737af2020-10-16 09:07:02 +08004#include "utils.hpp"
George Liua6c18f82020-06-22 10:50:04 +08005
6#include <map>
Patrick Venture91ac8d32018-11-01 17:03:22 -07007#include <set>
Andrew Geisslerd02c3cb2020-05-16 10:28:02 -05008#include <string>
Patrick Venture91ac8d32018-11-01 17:03:22 -07009
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053010namespace phosphor
11{
12namespace led
13{
George Liu1c737af2020-10-16 09:07:02 +080014using namespace phosphor::led::utils;
Vishwanatha Subbanna11ca8f92017-02-27 19:33:45 +053015
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053016/** @class Manager
17 * @brief Manages group of LEDs and applies action on the elements of group
18 */
19class Manager
20{
Patrick Venture91ac8d32018-11-01 17:03:22 -070021 public:
22 /** @brief Only need the default Manager */
23 Manager() = delete;
24 ~Manager() = default;
25 Manager(const Manager&) = delete;
26 Manager& operator=(const Manager&) = delete;
27 Manager(Manager&&) = delete;
28 Manager& operator=(Manager&&) = delete;
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053029
Patrick Venture91ac8d32018-11-01 17:03:22 -070030 /** @brief Special comparator for finding set difference */
31 static bool ledComp(const phosphor::led::Layout::LedAction& left,
32 const phosphor::led::Layout::LedAction& right)
33 {
34 // Example :
35 // If FIRST_1 is {fan0, 1, 1} and FIRST_2 is {fan0, 2, 2},
36 // with default priority of Blink, this comparator would return
37 // false. But considering the priority, this comparator would need
38 // to return true so that we consider appropriate set and in
39 // this case its {fan0, 1, 1}
40 if (left.name == right.name)
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053041 {
Patrick Venture91ac8d32018-11-01 17:03:22 -070042 if (left.action == right.action)
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +053043 {
Patrick Venture91ac8d32018-11-01 17:03:22 -070044 return false;
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +053045 }
Patrick Venture91ac8d32018-11-01 17:03:22 -070046 else
47 {
48 return true;
49 }
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053050 }
Patrick Venture91ac8d32018-11-01 17:03:22 -070051 return left.name < right.name;
52 }
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053053
Patrick Venture91ac8d32018-11-01 17:03:22 -070054 /** @brief Comparator for finding LEDs to be DeAsserted */
55 static bool ledLess(const phosphor::led::Layout::LedAction& left,
56 const phosphor::led::Layout::LedAction& right)
57 {
58 return left.name < right.name;
59 }
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +053060
Patrick Venture91ac8d32018-11-01 17:03:22 -070061 /** @brief Comparator for helping unique_copy */
62 static bool ledEqual(const phosphor::led::Layout::LedAction& left,
63 const phosphor::led::Layout::LedAction& right)
64 {
65 return left.name == right.name;
66 }
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +053067
Patrick Venture91ac8d32018-11-01 17:03:22 -070068 using group = std::set<phosphor::led::Layout::LedAction>;
69 using LedLayout = std::map<std::string, group>;
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053070
Patrick Venture91ac8d32018-11-01 17:03:22 -070071 /** @brief static global map constructed at compile time */
72 const LedLayout& ledMap;
Vishwanatha Subbannaed490732016-12-20 15:59:29 +053073
Patrick Venture91ac8d32018-11-01 17:03:22 -070074 /** @brief Refer the user supplied LED layout and sdbusplus handler
75 *
76 * @param [in] bus - sdbusplus handler
77 * @param [in] LedLayout - LEDs group layout
78 */
79 Manager(sdbusplus::bus::bus& bus, const LedLayout& ledLayout) :
80 ledMap(ledLayout), bus(bus)
81 {
82 // Nothing here
83 }
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053084
Patrick Venture91ac8d32018-11-01 17:03:22 -070085 /** @brief Given a group name, applies the action on the group
86 *
87 * @param[in] path - dbus path of group
88 * @param[in] assert - Could be true or false
89 * @param[in] ledsAssert - LEDs that are to be asserted new
90 * or to a different state
91 * @param[in] ledsDeAssert - LEDs that are to be Deasserted
92 *
93 * @return - Success or exception thrown
94 */
95 bool setGroupState(const std::string& path, bool assert, group& ledsAssert,
96 group& ledsDeAssert);
Vishwanatha Subbannaed490732016-12-20 15:59:29 +053097
Patrick Venture91ac8d32018-11-01 17:03:22 -070098 /** @brief Finds the set of LEDs to operate on and executes action
99 *
100 * @param[in] ledsAssert - LEDs that are to be asserted newly
101 * or to a different state
102 * @param[in] ledsDeAssert - LEDs that are to be Deasserted
103 *
104 * @return: None
105 */
106 void driveLEDs(group& ledsAssert, group& ledsDeAssert);
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +0530107
George Liu4c5f5332020-10-10 17:04:28 +0800108 /** @brief Set OperationalStatus according to the status of asserted
109 * property
110 *
111 * @param[in] path - D-Bus path of group
112 * @param[in] value - Could be true or false
113 *
114 * @return: None
115 */
116 void setOperationalStatus(const std::string& path, bool value) const;
117
Patrick Venture91ac8d32018-11-01 17:03:22 -0700118 private:
119 /** @brief sdbusplus handler */
120 sdbusplus::bus::bus& bus;
Vishwanatha Subbanna11ca8f92017-02-27 19:33:45 +0530121
Patrick Venture91ac8d32018-11-01 17:03:22 -0700122 /** Map of physical LED path to service name */
123 std::map<std::string, std::string> phyLeds{};
Vishwanatha Subbannadcc3f382017-03-24 20:15:02 +0530124
George Liu1c737af2020-10-16 09:07:02 +0800125 /** DBusHandler class handles the D-Bus operations */
126 DBusHandler dBusHandler;
127
Patrick Venture91ac8d32018-11-01 17:03:22 -0700128 /** @brief Pointers to groups that are in asserted state */
129 std::set<const group*> assertedGroups;
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +0530130
Patrick Venture91ac8d32018-11-01 17:03:22 -0700131 /** @brief Contains the highest priority actions for all
132 * asserted LEDs.
133 */
134 group currentState;
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +0530135
Patrick Venture91ac8d32018-11-01 17:03:22 -0700136 /** @brief Contains the set of all actions for asserted LEDs */
137 group combinedState;
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +0530138
Patrick Venture91ac8d32018-11-01 17:03:22 -0700139 /** @brief Returns action string based on enum
140 *
141 * @param[in] action - Action enum
142 *
143 * @return string equivalent of the passed in enumeration
144 */
145 static std::string getPhysicalAction(Layout::Action action);
Vishwanatha Subbanna11ca8f92017-02-27 19:33:45 +0530146
Patrick Venture91ac8d32018-11-01 17:03:22 -0700147 /** @brief Chooses appropriate action to be triggered on physical LED
148 * and calls into function that applies the actual action.
149 *
150 * @param[in] objPath - dbus object path
151 * @param[in] action - Intended action to be triggered
152 * @param[in] dutyOn - Duty Cycle ON percentage
tony lee6fd9e442019-04-23 09:09:15 +0800153 * @param[in] period - Time taken for one blink cycle
Patrick Venture91ac8d32018-11-01 17:03:22 -0700154 */
155 void drivePhysicalLED(const std::string& objPath, Layout::Action action,
tony lee6fd9e442019-04-23 09:09:15 +0800156 uint8_t dutyOn, const uint16_t period);
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +0530157};
158
159} // namespace led
160} // namespace phosphor