blob: 1f5f0a5e8224a212f0b7aac1e29af1311c536a3e [file] [log] [blame]
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +05301#pragma once
2
Alexander Hansen7ba70c82024-07-23 13:46:25 +02003#include "grouplayout.hpp"
Vishwanatha Subbannaed490732016-12-20 15:59:29 +05304#include "ledlayout.hpp"
George Liu1c737af2020-10-16 09:07:02 +08005#include "utils.hpp"
George Liua6c18f82020-06-22 10:50:04 +08006
Potin Laif1ed4792023-07-13 18:45:14 +08007#include <sdeventplus/event.hpp>
8#include <sdeventplus/utility/timer.hpp>
9
Patrick Venture91ac8d32018-11-01 17:03:22 -070010#include <set>
Andrew Geisslerd02c3cb2020-05-16 10:28:02 -050011#include <string>
Patrick Williamsf2044032022-03-17 05:12:30 -050012#include <unordered_map>
Patrick Venture91ac8d32018-11-01 17:03:22 -070013
Alexander Hansena6f9a412024-07-24 12:27:42 +020014// to better see what the string is representing
15using LedName = std::string;
16
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053017namespace phosphor
18{
19namespace led
20{
George Liu1c737af2020-10-16 09:07:02 +080021using namespace phosphor::led::utils;
Vishwanatha Subbanna11ca8f92017-02-27 19:33:45 +053022
George Liu1f0b7152023-07-18 09:24:34 +080023static constexpr auto phyLedPath = "/xyz/openbmc_project/led/physical/";
24static constexpr auto phyLedIntf = "xyz.openbmc_project.Led.Physical";
George Liu87fd11c2020-11-23 16:40:14 +080025
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053026/** @class Manager
27 * @brief Manages group of LEDs and applies action on the elements of group
28 */
29class Manager
30{
Patrick Venture91ac8d32018-11-01 17:03:22 -070031 public:
32 /** @brief Only need the default Manager */
33 Manager() = delete;
34 ~Manager() = default;
35 Manager(const Manager&) = delete;
36 Manager& operator=(const Manager&) = delete;
37 Manager(Manager&&) = delete;
38 Manager& operator=(Manager&&) = delete;
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053039
Patrick Venture91ac8d32018-11-01 17:03:22 -070040 /** @brief Special comparator for finding set difference */
41 static bool ledComp(const phosphor::led::Layout::LedAction& left,
42 const phosphor::led::Layout::LedAction& right)
43 {
44 // Example :
45 // If FIRST_1 is {fan0, 1, 1} and FIRST_2 is {fan0, 2, 2},
46 // with default priority of Blink, this comparator would return
47 // false. But considering the priority, this comparator would need
48 // to return true so that we consider appropriate set and in
49 // this case its {fan0, 1, 1}
50 if (left.name == right.name)
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053051 {
George Liu349d22e2024-08-23 09:09:56 +080052 return left.action != right.action;
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 /** @brief static global map constructed at compile time */
Patrick Williams158b2c12022-03-17 05:57:44 -050072 const GroupMap& 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
Patrick Williams158b2c12022-03-17 05:57:44 -050077 * @param [in] GroupMap - LEDs group layout
Potin Laif1ed4792023-07-13 18:45:14 +080078 * @param [in] Event - sd event handler
Patrick Venture91ac8d32018-11-01 17:03:22 -070079 */
Potin Laif1ed4792023-07-13 18:45:14 +080080 Manager(
81 sdbusplus::bus_t& bus, const GroupMap& ledLayout,
82 const sdeventplus::Event& event = sdeventplus::Event::get_default()) :
Patrick Williams543ac9f2024-08-16 15:19:59 -040083 ledMap(ledLayout), bus(bus),
84 timer(event, [this](auto&) { driveLedsHandler(); })
Patrick Venture91ac8d32018-11-01 17:03:22 -070085 {
86 // Nothing here
87 }
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053088
Alexander Hansena6f9a412024-07-24 12:27:42 +020089 /* create the resulting map from all currently asserted groups */
Alexander Hansen7ba70c82024-07-23 13:46:25 +020090 static auto getNewMap(std::set<const Layout::GroupLayout*> assertedGroups)
Alexander Hansena6f9a412024-07-24 12:27:42 +020091 -> std::map<LedName, Layout::LedAction>;
92
Patrick Venture91ac8d32018-11-01 17:03:22 -070093 /** @brief Given a group name, applies the action on the group
94 *
95 * @param[in] path - dbus path of group
96 * @param[in] assert - Could be true or false
97 * @param[in] ledsAssert - LEDs that are to be asserted new
98 * or to a different state
99 * @param[in] ledsDeAssert - LEDs that are to be Deasserted
100 *
101 * @return - Success or exception thrown
102 */
Patrick Williams158b2c12022-03-17 05:57:44 -0500103 bool setGroupState(const std::string& path, bool assert,
104 ActionSet& ledsAssert, ActionSet& ledsDeAssert);
Vishwanatha Subbannaed490732016-12-20 15:59:29 +0530105
Patrick Venture91ac8d32018-11-01 17:03:22 -0700106 /** @brief Finds the set of LEDs to operate on and executes action
107 *
108 * @param[in] ledsAssert - LEDs that are to be asserted newly
109 * or to a different state
110 * @param[in] ledsDeAssert - LEDs that are to be Deasserted
111 *
112 * @return: None
113 */
Patrick Williams158b2c12022-03-17 05:57:44 -0500114 void driveLEDs(ActionSet& ledsAssert, ActionSet& ledsDeAssert);
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +0530115
George Liu87fd11c2020-11-23 16:40:14 +0800116 /** @brief Chooses appropriate action to be triggered on physical LED
117 * and calls into function that applies the actual action.
118 *
119 * @param[in] objPath - D-Bus object path
120 * @param[in] action - Intended action to be triggered
121 * @param[in] dutyOn - Duty Cycle ON percentage
122 * @param[in] period - Time taken for one blink cycle
Potin Laif1ed4792023-07-13 18:45:14 +0800123 *
124 * @return: - 0: success, -1: LED set failed
George Liu87fd11c2020-11-23 16:40:14 +0800125 */
George Liuf0592552024-08-23 09:46:17 +0800126 static int drivePhysicalLED(const std::string& objPath,
127 Layout::Action action, uint8_t dutyOn,
George Liu80f51bb2024-08-22 20:17:36 +0800128 uint16_t period);
George Liu87fd11c2020-11-23 16:40:14 +0800129
George Liub6151622020-11-23 18:16:18 +0800130 /** @brief Set lamp test callback when enabled lamp test.
131 *
132 * @param[in] callBack - Custom callback when enabled lamp test
133 */
134 void setLampTestCallBack(
Patrick Williams158b2c12022-03-17 05:57:44 -0500135 std::function<bool(ActionSet& ledsAssert, ActionSet& ledsDeAssert)>
136 callBack);
George Liub6151622020-11-23 18:16:18 +0800137
Patrick Venture91ac8d32018-11-01 17:03:22 -0700138 private:
139 /** @brief sdbusplus handler */
Patrick Williams3e073ba2022-07-22 19:26:52 -0500140 sdbusplus::bus_t& bus;
Vishwanatha Subbanna11ca8f92017-02-27 19:33:45 +0530141
Patrick Venture91ac8d32018-11-01 17:03:22 -0700142 /** Map of physical LED path to service name */
George Liu391bec52024-08-22 20:10:55 +0800143 std::unordered_map<std::string, std::string> phyLeds;
Vishwanatha Subbannadcc3f382017-03-24 20:15:02 +0530144
Patrick Venture91ac8d32018-11-01 17:03:22 -0700145 /** @brief Pointers to groups that are in asserted state */
Alexander Hansen7ba70c82024-07-23 13:46:25 +0200146 std::set<const Layout::GroupLayout*> assertedGroups;
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +0530147
Alexander Hansena6f9a412024-07-24 12:27:42 +0200148 /** Map of led name to current state */
149 std::map<std::string, Layout::LedAction> ledStateMap;
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +0530150
George Liub6151622020-11-23 18:16:18 +0800151 /** @brief Custom callback when enabled lamp test */
Patrick Williams158b2c12022-03-17 05:57:44 -0500152 std::function<bool(ActionSet& ledsAssert, ActionSet& ledsDeAssert)>
George Liub6151622020-11-23 18:16:18 +0800153 lampTestCallBack;
154
Potin Laif1ed4792023-07-13 18:45:14 +0800155 /** @brief Timer used for LEDs handler callback*/
156 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> timer;
157
158 /** @brief Contains the required set of assert LEDs action */
159 ActionSet reqLedsAssert;
160
161 /** @brief Contains the required set of deassert LEDs action */
162 ActionSet reqLedsDeAssert;
163
164 /** @brief LEDs handler callback */
165 void driveLedsHandler();
166
Patrick Venture91ac8d32018-11-01 17:03:22 -0700167 /** @brief Returns action string based on enum
168 *
169 * @param[in] action - Action enum
170 *
171 * @return string equivalent of the passed in enumeration
172 */
173 static std::string getPhysicalAction(Layout::Action action);
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +0530174};
175
176} // namespace led
177} // namespace phosphor