blob: d8573b62d19d1525a1412ced45544287476dabf2 [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
Patrick Venture91ac8d32018-11-01 17:03:22 -07006#include <set>
Andrew Geisslerd02c3cb2020-05-16 10:28:02 -05007#include <string>
Patrick Williamsf2044032022-03-17 05:12:30 -05008#include <unordered_map>
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 Liu1f0b7152023-07-18 09:24:34 +080016static constexpr auto phyLedPath = "/xyz/openbmc_project/led/physical/";
17static constexpr auto phyLedIntf = "xyz.openbmc_project.Led.Physical";
George Liu87fd11c2020-11-23 16:40:14 +080018
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 /** @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
Patrick Venture91ac8d32018-11-01 17:03:22 -070078 */
Patrick Williams3e073ba2022-07-22 19:26:52 -050079 Manager(sdbusplus::bus_t& bus, const GroupMap& ledLayout) :
Patrick Venture91ac8d32018-11-01 17:03:22 -070080 ledMap(ledLayout), bus(bus)
81 {
82 // Nothing here
83 }
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053084
Patrick Venture91ac8d32018-11-01 17:03:22 -070085 /** @brief Given a group name, applies the action on the group
86 *
87 * @param[in] path - dbus path of group
88 * @param[in] assert - Could be true or false
89 * @param[in] ledsAssert - LEDs that are to be asserted new
90 * or to a different state
91 * @param[in] ledsDeAssert - LEDs that are to be Deasserted
92 *
93 * @return - Success or exception thrown
94 */
Patrick Williams158b2c12022-03-17 05:57:44 -050095 bool setGroupState(const std::string& path, bool assert,
96 ActionSet& ledsAssert, ActionSet& ledsDeAssert);
Vishwanatha Subbannaed490732016-12-20 15:59:29 +053097
Patrick Venture91ac8d32018-11-01 17:03:22 -070098 /** @brief Finds the set of LEDs to operate on and executes action
99 *
100 * @param[in] ledsAssert - LEDs that are to be asserted newly
101 * or to a different state
102 * @param[in] ledsDeAssert - LEDs that are to be Deasserted
103 *
104 * @return: None
105 */
Patrick Williams158b2c12022-03-17 05:57:44 -0500106 void driveLEDs(ActionSet& ledsAssert, ActionSet& ledsDeAssert);
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +0530107
George Liu87fd11c2020-11-23 16:40:14 +0800108 /** @brief Chooses appropriate action to be triggered on physical LED
109 * and calls into function that applies the actual action.
110 *
111 * @param[in] objPath - D-Bus object path
112 * @param[in] action - Intended action to be triggered
113 * @param[in] dutyOn - Duty Cycle ON percentage
114 * @param[in] period - Time taken for one blink cycle
115 */
116 void drivePhysicalLED(const std::string& objPath, Layout::Action action,
117 uint8_t dutyOn, const uint16_t period);
118
George Liub6151622020-11-23 18:16:18 +0800119 /** @brief Set lamp test callback when enabled lamp test.
120 *
121 * @param[in] callBack - Custom callback when enabled lamp test
122 */
123 void setLampTestCallBack(
Patrick Williams158b2c12022-03-17 05:57:44 -0500124 std::function<bool(ActionSet& ledsAssert, ActionSet& ledsDeAssert)>
125 callBack);
George Liub6151622020-11-23 18:16:18 +0800126
Patrick Venture91ac8d32018-11-01 17:03:22 -0700127 private:
128 /** @brief sdbusplus handler */
Patrick Williams3e073ba2022-07-22 19:26:52 -0500129 sdbusplus::bus_t& bus;
Vishwanatha Subbanna11ca8f92017-02-27 19:33:45 +0530130
Patrick Venture91ac8d32018-11-01 17:03:22 -0700131 /** Map of physical LED path to service name */
Patrick Williamsf2044032022-03-17 05:12:30 -0500132 std::unordered_map<std::string, std::string> phyLeds{};
Vishwanatha Subbannadcc3f382017-03-24 20:15:02 +0530133
George Liu1c737af2020-10-16 09:07:02 +0800134 /** DBusHandler class handles the D-Bus operations */
135 DBusHandler dBusHandler;
136
Patrick Venture91ac8d32018-11-01 17:03:22 -0700137 /** @brief Pointers to groups that are in asserted state */
Patrick Williams158b2c12022-03-17 05:57:44 -0500138 std::set<const ActionSet*> assertedGroups;
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +0530139
Patrick Venture91ac8d32018-11-01 17:03:22 -0700140 /** @brief Contains the highest priority actions for all
141 * asserted LEDs.
142 */
Patrick Williams158b2c12022-03-17 05:57:44 -0500143 ActionSet currentState;
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +0530144
Patrick Venture91ac8d32018-11-01 17:03:22 -0700145 /** @brief Contains the set of all actions for asserted LEDs */
Patrick Williams158b2c12022-03-17 05:57:44 -0500146 ActionSet combinedState;
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +0530147
George Liub6151622020-11-23 18:16:18 +0800148 /** @brief Custom callback when enabled lamp test */
Patrick Williams158b2c12022-03-17 05:57:44 -0500149 std::function<bool(ActionSet& ledsAssert, ActionSet& ledsDeAssert)>
George Liub6151622020-11-23 18:16:18 +0800150 lampTestCallBack;
151
Patrick Venture91ac8d32018-11-01 17:03:22 -0700152 /** @brief Returns action string based on enum
153 *
154 * @param[in] action - Action enum
155 *
156 * @return string equivalent of the passed in enumeration
157 */
158 static std::string getPhysicalAction(Layout::Action action);
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +0530159};
160
161} // namespace led
162} // namespace phosphor