blob: f21146173b64cc3ad0e457da0e74a90e69f49999 [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(
Patrick Williams4e93c862025-05-14 15:44:14 -040081 sdbusplus::bus_t&, const GroupMap& ledLayout,
Potin Laif1ed4792023-07-13 18:45:14 +080082 const sdeventplus::Event& event = sdeventplus::Event::get_default()) :
Patrick Williams4e93c862025-05-14 15:44:14 -040083 ledMap(ledLayout), timer(event, [this](auto&) { driveLedsHandler(); })
Patrick Venture91ac8d32018-11-01 17:03:22 -070084 {
85 // Nothing here
86 }
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053087
Alexander Hansena6f9a412024-07-24 12:27:42 +020088 /* create the resulting map from all currently asserted groups */
Alexander Hansen7ba70c82024-07-23 13:46:25 +020089 static auto getNewMap(std::set<const Layout::GroupLayout*> assertedGroups)
Alexander Hansena6f9a412024-07-24 12:27:42 +020090 -> std::map<LedName, Layout::LedAction>;
91
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 */
George Liuf0592552024-08-23 09:46:17 +0800125 static int drivePhysicalLED(const std::string& objPath,
126 Layout::Action action, uint8_t dutyOn,
George Liu80f51bb2024-08-22 20:17:36 +0800127 uint16_t period);
George Liu87fd11c2020-11-23 16:40:14 +0800128
George Liub6151622020-11-23 18:16:18 +0800129 /** @brief Set lamp test callback when enabled lamp test.
130 *
131 * @param[in] callBack - Custom callback when enabled lamp test
132 */
133 void setLampTestCallBack(
Patrick Williams158b2c12022-03-17 05:57:44 -0500134 std::function<bool(ActionSet& ledsAssert, ActionSet& ledsDeAssert)>
135 callBack);
George Liub6151622020-11-23 18:16:18 +0800136
Patrick Venture91ac8d32018-11-01 17:03:22 -0700137 private:
Patrick Venture91ac8d32018-11-01 17:03:22 -0700138 /** Map of physical LED path to service name */
George Liu391bec52024-08-22 20:10:55 +0800139 std::unordered_map<std::string, std::string> phyLeds;
Vishwanatha Subbannadcc3f382017-03-24 20:15:02 +0530140
Patrick Venture91ac8d32018-11-01 17:03:22 -0700141 /** @brief Pointers to groups that are in asserted state */
Alexander Hansen7ba70c82024-07-23 13:46:25 +0200142 std::set<const Layout::GroupLayout*> assertedGroups;
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +0530143
Alexander Hansena6f9a412024-07-24 12:27:42 +0200144 /** Map of led name to current state */
145 std::map<std::string, Layout::LedAction> ledStateMap;
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +0530146
George Liub6151622020-11-23 18:16:18 +0800147 /** @brief Custom callback when enabled lamp test */
Patrick Williams158b2c12022-03-17 05:57:44 -0500148 std::function<bool(ActionSet& ledsAssert, ActionSet& ledsDeAssert)>
George Liub6151622020-11-23 18:16:18 +0800149 lampTestCallBack;
150
Potin Laif1ed4792023-07-13 18:45:14 +0800151 /** @brief Timer used for LEDs handler callback*/
152 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> timer;
153
154 /** @brief Contains the required set of assert LEDs action */
155 ActionSet reqLedsAssert;
156
157 /** @brief Contains the required set of deassert LEDs action */
158 ActionSet reqLedsDeAssert;
159
160 /** @brief LEDs handler callback */
161 void driveLedsHandler();
162
Patrick Venture91ac8d32018-11-01 17:03:22 -0700163 /** @brief Returns action string based on enum
164 *
165 * @param[in] action - Action enum
166 *
167 * @return string equivalent of the passed in enumeration
168 */
169 static std::string getPhysicalAction(Layout::Action action);
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +0530170};
171
172} // namespace led
173} // namespace phosphor