blob: caa7ba840fb8d960750a5794c126bcec3cf7c170 [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
Potin Laif1ed4792023-07-13 18:45:14 +08006#include <sdeventplus/event.hpp>
7#include <sdeventplus/utility/timer.hpp>
8
Patrick Venture91ac8d32018-11-01 17:03:22 -07009#include <set>
Andrew Geisslerd02c3cb2020-05-16 10:28:02 -050010#include <string>
Patrick Williamsf2044032022-03-17 05:12:30 -050011#include <unordered_map>
Patrick Venture91ac8d32018-11-01 17:03:22 -070012
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053013namespace phosphor
14{
15namespace led
16{
George Liu1c737af2020-10-16 09:07:02 +080017using namespace phosphor::led::utils;
Vishwanatha Subbanna11ca8f92017-02-27 19:33:45 +053018
George Liu1f0b7152023-07-18 09:24:34 +080019static constexpr auto phyLedPath = "/xyz/openbmc_project/led/physical/";
20static constexpr auto phyLedIntf = "xyz.openbmc_project.Led.Physical";
George Liu87fd11c2020-11-23 16:40:14 +080021
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053022/** @class Manager
23 * @brief Manages group of LEDs and applies action on the elements of group
24 */
25class Manager
26{
Patrick Venture91ac8d32018-11-01 17:03:22 -070027 public:
28 /** @brief Only need the default Manager */
29 Manager() = delete;
30 ~Manager() = default;
31 Manager(const Manager&) = delete;
32 Manager& operator=(const Manager&) = delete;
33 Manager(Manager&&) = delete;
34 Manager& operator=(Manager&&) = delete;
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053035
Patrick Venture91ac8d32018-11-01 17:03:22 -070036 /** @brief Special comparator for finding set difference */
37 static bool ledComp(const phosphor::led::Layout::LedAction& left,
38 const phosphor::led::Layout::LedAction& right)
39 {
40 // Example :
41 // If FIRST_1 is {fan0, 1, 1} and FIRST_2 is {fan0, 2, 2},
42 // with default priority of Blink, this comparator would return
43 // false. But considering the priority, this comparator would need
44 // to return true so that we consider appropriate set and in
45 // this case its {fan0, 1, 1}
46 if (left.name == right.name)
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053047 {
Patrick Venture91ac8d32018-11-01 17:03:22 -070048 if (left.action == right.action)
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +053049 {
Patrick Venture91ac8d32018-11-01 17:03:22 -070050 return false;
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +053051 }
Patrick Venture91ac8d32018-11-01 17:03:22 -070052 else
53 {
54 return true;
55 }
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053056 }
Patrick Venture91ac8d32018-11-01 17:03:22 -070057 return left.name < right.name;
58 }
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053059
Patrick Venture91ac8d32018-11-01 17:03:22 -070060 /** @brief Comparator for finding LEDs to be DeAsserted */
61 static bool ledLess(const phosphor::led::Layout::LedAction& left,
62 const phosphor::led::Layout::LedAction& right)
63 {
64 return left.name < right.name;
65 }
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +053066
Patrick Venture91ac8d32018-11-01 17:03:22 -070067 /** @brief Comparator for helping unique_copy */
68 static bool ledEqual(const phosphor::led::Layout::LedAction& left,
69 const phosphor::led::Layout::LedAction& right)
70 {
71 return left.name == right.name;
72 }
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +053073
Patrick Venture91ac8d32018-11-01 17:03:22 -070074 /** @brief static global map constructed at compile time */
Patrick Williams158b2c12022-03-17 05:57:44 -050075 const GroupMap& 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
Patrick Williams158b2c12022-03-17 05:57:44 -050080 * @param [in] GroupMap - LEDs group layout
Potin Laif1ed4792023-07-13 18:45:14 +080081 * @param [in] Event - sd event handler
Patrick Venture91ac8d32018-11-01 17:03:22 -070082 */
Potin Laif1ed4792023-07-13 18:45:14 +080083 Manager(
84 sdbusplus::bus_t& bus, const GroupMap& ledLayout,
85 const sdeventplus::Event& event = sdeventplus::Event::get_default()) :
86 ledMap(ledLayout),
87 bus(bus), timer(event, [this](auto&) { driveLedsHandler(); })
Patrick Venture91ac8d32018-11-01 17:03:22 -070088 {
89 // Nothing here
90 }
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053091
Patrick Venture91ac8d32018-11-01 17:03:22 -070092 /** @brief Given a group name, applies the action on the group
93 *
94 * @param[in] path - dbus path of group
95 * @param[in] assert - Could be true or false
96 * @param[in] ledsAssert - LEDs that are to be asserted new
97 * or to a different state
98 * @param[in] ledsDeAssert - LEDs that are to be Deasserted
99 *
100 * @return - Success or exception thrown
101 */
Patrick Williams158b2c12022-03-17 05:57:44 -0500102 bool setGroupState(const std::string& path, bool assert,
103 ActionSet& ledsAssert, ActionSet& ledsDeAssert);
Vishwanatha Subbannaed490732016-12-20 15:59:29 +0530104
Patrick Venture91ac8d32018-11-01 17:03:22 -0700105 /** @brief Finds the set of LEDs to operate on and executes action
106 *
107 * @param[in] ledsAssert - LEDs that are to be asserted newly
108 * or to a different state
109 * @param[in] ledsDeAssert - LEDs that are to be Deasserted
110 *
111 * @return: None
112 */
Patrick Williams158b2c12022-03-17 05:57:44 -0500113 void driveLEDs(ActionSet& ledsAssert, ActionSet& ledsDeAssert);
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +0530114
George Liu87fd11c2020-11-23 16:40:14 +0800115 /** @brief Chooses appropriate action to be triggered on physical LED
116 * and calls into function that applies the actual action.
117 *
118 * @param[in] objPath - D-Bus object path
119 * @param[in] action - Intended action to be triggered
120 * @param[in] dutyOn - Duty Cycle ON percentage
121 * @param[in] period - Time taken for one blink cycle
Potin Laif1ed4792023-07-13 18:45:14 +0800122 *
123 * @return: - 0: success, -1: LED set failed
George Liu87fd11c2020-11-23 16:40:14 +0800124 */
Potin Laif1ed4792023-07-13 18:45:14 +0800125 int drivePhysicalLED(const std::string& objPath, Layout::Action action,
126 uint8_t dutyOn, const uint16_t period);
George Liu87fd11c2020-11-23 16:40:14 +0800127
George Liub6151622020-11-23 18:16:18 +0800128 /** @brief Set lamp test callback when enabled lamp test.
129 *
130 * @param[in] callBack - Custom callback when enabled lamp test
131 */
132 void setLampTestCallBack(
Patrick Williams158b2c12022-03-17 05:57:44 -0500133 std::function<bool(ActionSet& ledsAssert, ActionSet& ledsDeAssert)>
134 callBack);
George Liub6151622020-11-23 18:16:18 +0800135
Patrick Venture91ac8d32018-11-01 17:03:22 -0700136 private:
137 /** @brief sdbusplus handler */
Patrick Williams3e073ba2022-07-22 19:26:52 -0500138 sdbusplus::bus_t& bus;
Vishwanatha Subbanna11ca8f92017-02-27 19:33:45 +0530139
Patrick Venture91ac8d32018-11-01 17:03:22 -0700140 /** Map of physical LED path to service name */
Patrick Williamsf2044032022-03-17 05:12:30 -0500141 std::unordered_map<std::string, std::string> phyLeds{};
Vishwanatha Subbannadcc3f382017-03-24 20:15:02 +0530142
George Liu1c737af2020-10-16 09:07:02 +0800143 /** DBusHandler class handles the D-Bus operations */
144 DBusHandler dBusHandler;
145
Patrick Venture91ac8d32018-11-01 17:03:22 -0700146 /** @brief Pointers to groups that are in asserted state */
Patrick Williams158b2c12022-03-17 05:57:44 -0500147 std::set<const ActionSet*> assertedGroups;
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +0530148
Patrick Venture91ac8d32018-11-01 17:03:22 -0700149 /** @brief Contains the highest priority actions for all
150 * asserted LEDs.
151 */
Patrick Williams158b2c12022-03-17 05:57:44 -0500152 ActionSet currentState;
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +0530153
Patrick Venture91ac8d32018-11-01 17:03:22 -0700154 /** @brief Contains the set of all actions for asserted LEDs */
Patrick Williams158b2c12022-03-17 05:57:44 -0500155 ActionSet combinedState;
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +0530156
George Liub6151622020-11-23 18:16:18 +0800157 /** @brief Custom callback when enabled lamp test */
Patrick Williams158b2c12022-03-17 05:57:44 -0500158 std::function<bool(ActionSet& ledsAssert, ActionSet& ledsDeAssert)>
George Liub6151622020-11-23 18:16:18 +0800159 lampTestCallBack;
160
Potin Laif1ed4792023-07-13 18:45:14 +0800161 /** @brief Timer used for LEDs handler callback*/
162 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> timer;
163
164 /** @brief Contains the required set of assert LEDs action */
165 ActionSet reqLedsAssert;
166
167 /** @brief Contains the required set of deassert LEDs action */
168 ActionSet reqLedsDeAssert;
169
170 /** @brief LEDs handler callback */
171 void driveLedsHandler();
172
Patrick Venture91ac8d32018-11-01 17:03:22 -0700173 /** @brief Returns action string based on enum
174 *
175 * @param[in] action - Action enum
176 *
177 * @return string equivalent of the passed in enumeration
178 */
179 static std::string getPhysicalAction(Layout::Action action);
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +0530180};
181
182} // namespace led
183} // namespace phosphor