blob: 24a6c175dadf54c25fb214b37635635c2edda275 [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
Alexander Hansena6f9a412024-07-24 12:27:42 +020013// to better see what the string is representing
14using LedName = std::string;
15
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053016namespace phosphor
17{
18namespace led
19{
George Liu1c737af2020-10-16 09:07:02 +080020using namespace phosphor::led::utils;
Vishwanatha Subbanna11ca8f92017-02-27 19:33:45 +053021
George Liu1f0b7152023-07-18 09:24:34 +080022static constexpr auto phyLedPath = "/xyz/openbmc_project/led/physical/";
23static constexpr auto phyLedIntf = "xyz.openbmc_project.Led.Physical";
George Liu87fd11c2020-11-23 16:40:14 +080024
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053025/** @class Manager
26 * @brief Manages group of LEDs and applies action on the elements of group
27 */
28class Manager
29{
Patrick Venture91ac8d32018-11-01 17:03:22 -070030 public:
31 /** @brief Only need the default Manager */
32 Manager() = delete;
33 ~Manager() = default;
34 Manager(const Manager&) = delete;
35 Manager& operator=(const Manager&) = delete;
36 Manager(Manager&&) = delete;
37 Manager& operator=(Manager&&) = delete;
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053038
Patrick Venture91ac8d32018-11-01 17:03:22 -070039 /** @brief Special comparator for finding set difference */
40 static bool ledComp(const phosphor::led::Layout::LedAction& left,
41 const phosphor::led::Layout::LedAction& right)
42 {
43 // Example :
44 // If FIRST_1 is {fan0, 1, 1} and FIRST_2 is {fan0, 2, 2},
45 // with default priority of Blink, this comparator would return
46 // false. But considering the priority, this comparator would need
47 // to return true so that we consider appropriate set and in
48 // this case its {fan0, 1, 1}
49 if (left.name == right.name)
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053050 {
Patrick Venture91ac8d32018-11-01 17:03:22 -070051 if (left.action == right.action)
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +053052 {
Patrick Venture91ac8d32018-11-01 17:03:22 -070053 return false;
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +053054 }
Patrick Venture91ac8d32018-11-01 17:03:22 -070055 else
56 {
57 return true;
58 }
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053059 }
Patrick Venture91ac8d32018-11-01 17:03:22 -070060 return left.name < right.name;
61 }
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053062
Patrick Venture91ac8d32018-11-01 17:03:22 -070063 /** @brief Comparator for finding LEDs to be DeAsserted */
64 static bool ledLess(const phosphor::led::Layout::LedAction& left,
65 const phosphor::led::Layout::LedAction& right)
66 {
67 return left.name < right.name;
68 }
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +053069
Patrick Venture91ac8d32018-11-01 17:03:22 -070070 /** @brief Comparator for helping unique_copy */
71 static bool ledEqual(const phosphor::led::Layout::LedAction& left,
72 const phosphor::led::Layout::LedAction& right)
73 {
74 return left.name == right.name;
75 }
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +053076
Patrick Venture91ac8d32018-11-01 17:03:22 -070077 /** @brief static global map constructed at compile time */
Patrick Williams158b2c12022-03-17 05:57:44 -050078 const GroupMap& ledMap;
Vishwanatha Subbannaed490732016-12-20 15:59:29 +053079
Patrick Venture91ac8d32018-11-01 17:03:22 -070080 /** @brief Refer the user supplied LED layout and sdbusplus handler
81 *
82 * @param [in] bus - sdbusplus handler
Patrick Williams158b2c12022-03-17 05:57:44 -050083 * @param [in] GroupMap - LEDs group layout
Potin Laif1ed4792023-07-13 18:45:14 +080084 * @param [in] Event - sd event handler
Patrick Venture91ac8d32018-11-01 17:03:22 -070085 */
Potin Laif1ed4792023-07-13 18:45:14 +080086 Manager(
87 sdbusplus::bus_t& bus, const GroupMap& ledLayout,
88 const sdeventplus::Event& event = sdeventplus::Event::get_default()) :
Patrick Williams543ac9f2024-08-16 15:19:59 -040089 ledMap(ledLayout), bus(bus),
90 timer(event, [this](auto&) { driveLedsHandler(); })
Patrick Venture91ac8d32018-11-01 17:03:22 -070091 {
92 // Nothing here
93 }
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053094
Alexander Hansena6f9a412024-07-24 12:27:42 +020095 /* create the resulting map from all currently asserted groups */
96 static auto getNewMap(std::set<const ActionSet*> assertedGroups)
97 -> std::map<LedName, Layout::LedAction>;
98
Patrick Venture91ac8d32018-11-01 17:03:22 -070099 /** @brief Given a group name, applies the action on the group
100 *
101 * @param[in] path - dbus path of group
102 * @param[in] assert - Could be true or false
103 * @param[in] ledsAssert - LEDs that are to be asserted new
104 * or to a different state
105 * @param[in] ledsDeAssert - LEDs that are to be Deasserted
106 *
107 * @return - Success or exception thrown
108 */
Patrick Williams158b2c12022-03-17 05:57:44 -0500109 bool setGroupState(const std::string& path, bool assert,
110 ActionSet& ledsAssert, ActionSet& ledsDeAssert);
Vishwanatha Subbannaed490732016-12-20 15:59:29 +0530111
Patrick Venture91ac8d32018-11-01 17:03:22 -0700112 /** @brief Finds the set of LEDs to operate on and executes action
113 *
114 * @param[in] ledsAssert - LEDs that are to be asserted newly
115 * or to a different state
116 * @param[in] ledsDeAssert - LEDs that are to be Deasserted
117 *
118 * @return: None
119 */
Patrick Williams158b2c12022-03-17 05:57:44 -0500120 void driveLEDs(ActionSet& ledsAssert, ActionSet& ledsDeAssert);
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +0530121
George Liu87fd11c2020-11-23 16:40:14 +0800122 /** @brief Chooses appropriate action to be triggered on physical LED
123 * and calls into function that applies the actual action.
124 *
125 * @param[in] objPath - D-Bus object path
126 * @param[in] action - Intended action to be triggered
127 * @param[in] dutyOn - Duty Cycle ON percentage
128 * @param[in] period - Time taken for one blink cycle
Potin Laif1ed4792023-07-13 18:45:14 +0800129 *
130 * @return: - 0: success, -1: LED set failed
George Liu87fd11c2020-11-23 16:40:14 +0800131 */
Potin Laif1ed4792023-07-13 18:45:14 +0800132 int drivePhysicalLED(const std::string& objPath, Layout::Action action,
133 uint8_t dutyOn, const uint16_t period);
George Liu87fd11c2020-11-23 16:40:14 +0800134
George Liub6151622020-11-23 18:16:18 +0800135 /** @brief Set lamp test callback when enabled lamp test.
136 *
137 * @param[in] callBack - Custom callback when enabled lamp test
138 */
139 void setLampTestCallBack(
Patrick Williams158b2c12022-03-17 05:57:44 -0500140 std::function<bool(ActionSet& ledsAssert, ActionSet& ledsDeAssert)>
141 callBack);
George Liub6151622020-11-23 18:16:18 +0800142
Patrick Venture91ac8d32018-11-01 17:03:22 -0700143 private:
144 /** @brief sdbusplus handler */
Patrick Williams3e073ba2022-07-22 19:26:52 -0500145 sdbusplus::bus_t& bus;
Vishwanatha Subbanna11ca8f92017-02-27 19:33:45 +0530146
Patrick Venture91ac8d32018-11-01 17:03:22 -0700147 /** Map of physical LED path to service name */
Patrick Williamsf2044032022-03-17 05:12:30 -0500148 std::unordered_map<std::string, std::string> phyLeds{};
Vishwanatha Subbannadcc3f382017-03-24 20:15:02 +0530149
George Liu1c737af2020-10-16 09:07:02 +0800150 /** DBusHandler class handles the D-Bus operations */
151 DBusHandler dBusHandler;
152
Patrick Venture91ac8d32018-11-01 17:03:22 -0700153 /** @brief Pointers to groups that are in asserted state */
Patrick Williams158b2c12022-03-17 05:57:44 -0500154 std::set<const ActionSet*> assertedGroups;
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +0530155
Alexander Hansena6f9a412024-07-24 12:27:42 +0200156 /** Map of led name to current state */
157 std::map<std::string, Layout::LedAction> ledStateMap;
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +0530158
George Liub6151622020-11-23 18:16:18 +0800159 /** @brief Custom callback when enabled lamp test */
Patrick Williams158b2c12022-03-17 05:57:44 -0500160 std::function<bool(ActionSet& ledsAssert, ActionSet& ledsDeAssert)>
George Liub6151622020-11-23 18:16:18 +0800161 lampTestCallBack;
162
Potin Laif1ed4792023-07-13 18:45:14 +0800163 /** @brief Timer used for LEDs handler callback*/
164 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> timer;
165
166 /** @brief Contains the required set of assert LEDs action */
167 ActionSet reqLedsAssert;
168
169 /** @brief Contains the required set of deassert LEDs action */
170 ActionSet reqLedsDeAssert;
171
172 /** @brief LEDs handler callback */
173 void driveLedsHandler();
174
Patrick Venture91ac8d32018-11-01 17:03:22 -0700175 /** @brief Returns action string based on enum
176 *
177 * @param[in] action - Action enum
178 *
179 * @return string equivalent of the passed in enumeration
180 */
181 static std::string getPhysicalAction(Layout::Action action);
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +0530182};
183
184} // namespace led
185} // namespace phosphor