blob: 6e6ef48eccd995003a2dbf78a67bcffa9e67e0fd [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
George Liu87fd11c2020-11-23 16:40:14 +080016static constexpr auto PHY_LED_PATH = "/xyz/openbmc_project/led/physical/";
17static constexpr auto PHY_LED_IFACE = "xyz.openbmc_project.Led.Physical";
18
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053019/** @class Manager
20 * @brief Manages group of LEDs and applies action on the elements of group
21 */
22class Manager
23{
Patrick Venture91ac8d32018-11-01 17:03:22 -070024 public:
25 /** @brief Only need the default Manager */
26 Manager() = delete;
27 ~Manager() = default;
28 Manager(const Manager&) = delete;
29 Manager& operator=(const Manager&) = delete;
30 Manager(Manager&&) = delete;
31 Manager& operator=(Manager&&) = delete;
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053032
Patrick Venture91ac8d32018-11-01 17:03:22 -070033 /** @brief Special comparator for finding set difference */
34 static bool ledComp(const phosphor::led::Layout::LedAction& left,
35 const phosphor::led::Layout::LedAction& right)
36 {
37 // Example :
38 // If FIRST_1 is {fan0, 1, 1} and FIRST_2 is {fan0, 2, 2},
39 // with default priority of Blink, this comparator would return
40 // false. But considering the priority, this comparator would need
41 // to return true so that we consider appropriate set and in
42 // this case its {fan0, 1, 1}
43 if (left.name == right.name)
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053044 {
Patrick Venture91ac8d32018-11-01 17:03:22 -070045 if (left.action == right.action)
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +053046 {
Patrick Venture91ac8d32018-11-01 17:03:22 -070047 return false;
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +053048 }
Patrick Venture91ac8d32018-11-01 17:03:22 -070049 else
50 {
51 return true;
52 }
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053053 }
Patrick Venture91ac8d32018-11-01 17:03:22 -070054 return left.name < right.name;
55 }
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053056
Patrick Venture91ac8d32018-11-01 17:03:22 -070057 /** @brief Comparator for finding LEDs to be DeAsserted */
58 static bool ledLess(const phosphor::led::Layout::LedAction& left,
59 const phosphor::led::Layout::LedAction& right)
60 {
61 return left.name < right.name;
62 }
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +053063
Patrick Venture91ac8d32018-11-01 17:03:22 -070064 /** @brief Comparator for helping unique_copy */
65 static bool ledEqual(const phosphor::led::Layout::LedAction& left,
66 const phosphor::led::Layout::LedAction& right)
67 {
68 return left.name == right.name;
69 }
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +053070
Patrick Venture91ac8d32018-11-01 17:03:22 -070071 using group = std::set<phosphor::led::Layout::LedAction>;
72 using LedLayout = std::map<std::string, group>;
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053073
Patrick Venture91ac8d32018-11-01 17:03:22 -070074 /** @brief static global map constructed at compile time */
75 const LedLayout& ledMap;
Vishwanatha Subbannaed490732016-12-20 15:59:29 +053076
Patrick Venture91ac8d32018-11-01 17:03:22 -070077 /** @brief Refer the user supplied LED layout and sdbusplus handler
78 *
79 * @param [in] bus - sdbusplus handler
80 * @param [in] LedLayout - LEDs group layout
81 */
82 Manager(sdbusplus::bus::bus& bus, const LedLayout& ledLayout) :
83 ledMap(ledLayout), bus(bus)
84 {
85 // Nothing here
86 }
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053087
Patrick Venture91ac8d32018-11-01 17:03:22 -070088 /** @brief Given a group name, applies the action on the group
89 *
90 * @param[in] path - dbus path of group
91 * @param[in] assert - Could be true or false
92 * @param[in] ledsAssert - LEDs that are to be asserted new
93 * or to a different state
94 * @param[in] ledsDeAssert - LEDs that are to be Deasserted
95 *
96 * @return - Success or exception thrown
97 */
98 bool setGroupState(const std::string& path, bool assert, group& ledsAssert,
99 group& ledsDeAssert);
Vishwanatha Subbannaed490732016-12-20 15:59:29 +0530100
Patrick Venture91ac8d32018-11-01 17:03:22 -0700101 /** @brief Finds the set of LEDs to operate on and executes action
102 *
103 * @param[in] ledsAssert - LEDs that are to be asserted newly
104 * or to a different state
105 * @param[in] ledsDeAssert - LEDs that are to be Deasserted
106 *
107 * @return: None
108 */
109 void driveLEDs(group& ledsAssert, group& ledsDeAssert);
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +0530110
George Liu4c5f5332020-10-10 17:04:28 +0800111 /** @brief Set OperationalStatus according to the status of asserted
112 * property
113 *
114 * @param[in] path - D-Bus path of group
115 * @param[in] value - Could be true or false
116 *
117 * @return: None
118 */
119 void setOperationalStatus(const std::string& path, bool value) const;
120
George Liu87fd11c2020-11-23 16:40:14 +0800121 /** @brief Chooses appropriate action to be triggered on physical LED
122 * and calls into function that applies the actual action.
123 *
124 * @param[in] objPath - D-Bus object path
125 * @param[in] action - Intended action to be triggered
126 * @param[in] dutyOn - Duty Cycle ON percentage
127 * @param[in] period - Time taken for one blink cycle
128 */
129 void drivePhysicalLED(const std::string& objPath, Layout::Action action,
130 uint8_t dutyOn, const uint16_t period);
131
George Liub6151622020-11-23 18:16:18 +0800132 /** @brief Set lamp test callback when enabled lamp test.
133 *
134 * @param[in] callBack - Custom callback when enabled lamp test
135 */
136 void setLampTestCallBack(
137 std::function<bool(group& ledsAssert, group& ledsDeAssert)> callBack);
138
Patrick Venture91ac8d32018-11-01 17:03:22 -0700139 private:
140 /** @brief sdbusplus handler */
141 sdbusplus::bus::bus& bus;
Vishwanatha Subbanna11ca8f92017-02-27 19:33:45 +0530142
Patrick Venture91ac8d32018-11-01 17:03:22 -0700143 /** Map of physical LED path to service name */
144 std::map<std::string, std::string> phyLeds{};
Vishwanatha Subbannadcc3f382017-03-24 20:15:02 +0530145
George Liu1c737af2020-10-16 09:07:02 +0800146 /** DBusHandler class handles the D-Bus operations */
147 DBusHandler dBusHandler;
148
Patrick Venture91ac8d32018-11-01 17:03:22 -0700149 /** @brief Pointers to groups that are in asserted state */
150 std::set<const group*> assertedGroups;
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +0530151
Patrick Venture91ac8d32018-11-01 17:03:22 -0700152 /** @brief Contains the highest priority actions for all
153 * asserted LEDs.
154 */
155 group currentState;
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +0530156
Patrick Venture91ac8d32018-11-01 17:03:22 -0700157 /** @brief Contains the set of all actions for asserted LEDs */
158 group combinedState;
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +0530159
George Liub6151622020-11-23 18:16:18 +0800160 /** @brief Custom callback when enabled lamp test */
161 std::function<bool(group& ledsAssert, group& ledsDeAssert)>
162 lampTestCallBack;
163
Patrick Venture91ac8d32018-11-01 17:03:22 -0700164 /** @brief Returns action string based on enum
165 *
166 * @param[in] action - Action enum
167 *
168 * @return string equivalent of the passed in enumeration
169 */
170 static std::string getPhysicalAction(Layout::Action action);
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +0530171};
172
173} // namespace led
174} // namespace phosphor