blob: 2c79a2f907d29e437a96c89be6495071ca11f4b4 [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"
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +05304
Patrick Venture91ac8d32018-11-01 17:03:22 -07005#include <map>
6#include <sdbusplus/bus.hpp>
7#include <set>
Andrew Geisslerd02c3cb2020-05-16 10:28:02 -05008#include <string>
Patrick Venture91ac8d32018-11-01 17:03:22 -07009
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053010namespace phosphor
11{
12namespace led
13{
14
Vishwanatha Subbanna11ca8f92017-02-27 19:33:45 +053015/** @brief Physical LED dbus constructs */
16constexpr auto PHY_LED_PATH = "/xyz/openbmc_project/led/physical/";
17constexpr auto PHY_LED_IFACE = "xyz.openbmc_project.Led.Physical";
18constexpr auto DBUS_PROPERTY_IFACE = "org.freedesktop.DBus.Properties";
19
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053020/** @class Manager
21 * @brief Manages group of LEDs and applies action on the elements of group
22 */
23class Manager
24{
Patrick Venture91ac8d32018-11-01 17:03:22 -070025 public:
26 /** @brief Only need the default Manager */
27 Manager() = delete;
28 ~Manager() = default;
29 Manager(const Manager&) = delete;
30 Manager& operator=(const Manager&) = delete;
31 Manager(Manager&&) = delete;
32 Manager& operator=(Manager&&) = delete;
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053033
Patrick Venture91ac8d32018-11-01 17:03:22 -070034 /** @brief Special comparator for finding set difference */
35 static bool ledComp(const phosphor::led::Layout::LedAction& left,
36 const phosphor::led::Layout::LedAction& right)
37 {
38 // Example :
39 // If FIRST_1 is {fan0, 1, 1} and FIRST_2 is {fan0, 2, 2},
40 // with default priority of Blink, this comparator would return
41 // false. But considering the priority, this comparator would need
42 // to return true so that we consider appropriate set and in
43 // this case its {fan0, 1, 1}
44 if (left.name == right.name)
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053045 {
Patrick Venture91ac8d32018-11-01 17:03:22 -070046 if (left.action == right.action)
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +053047 {
Patrick Venture91ac8d32018-11-01 17:03:22 -070048 return false;
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +053049 }
Patrick Venture91ac8d32018-11-01 17:03:22 -070050 else
51 {
52 return true;
53 }
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053054 }
Patrick Venture91ac8d32018-11-01 17:03:22 -070055 return left.name < right.name;
56 }
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053057
Patrick Venture91ac8d32018-11-01 17:03:22 -070058 /** @brief Comparator for finding LEDs to be DeAsserted */
59 static bool ledLess(const phosphor::led::Layout::LedAction& left,
60 const phosphor::led::Layout::LedAction& right)
61 {
62 return left.name < right.name;
63 }
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +053064
Patrick Venture91ac8d32018-11-01 17:03:22 -070065 /** @brief Comparator for helping unique_copy */
66 static bool ledEqual(const phosphor::led::Layout::LedAction& left,
67 const phosphor::led::Layout::LedAction& right)
68 {
69 return left.name == right.name;
70 }
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +053071
Patrick Venture91ac8d32018-11-01 17:03:22 -070072 using group = std::set<phosphor::led::Layout::LedAction>;
73 using LedLayout = std::map<std::string, group>;
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053074
Patrick Venture91ac8d32018-11-01 17:03:22 -070075 /** @brief static global map constructed at compile time */
76 const LedLayout& ledMap;
Vishwanatha Subbannaed490732016-12-20 15:59:29 +053077
Patrick Venture91ac8d32018-11-01 17:03:22 -070078 /** @brief Refer the user supplied LED layout and sdbusplus handler
79 *
80 * @param [in] bus - sdbusplus handler
81 * @param [in] LedLayout - LEDs group layout
82 */
83 Manager(sdbusplus::bus::bus& bus, const LedLayout& ledLayout) :
84 ledMap(ledLayout), bus(bus)
85 {
86 // Nothing here
87 }
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053088
Patrick Venture91ac8d32018-11-01 17:03:22 -070089 /** @brief Given a group name, applies the action on the group
90 *
91 * @param[in] path - dbus path of group
92 * @param[in] assert - Could be true or false
93 * @param[in] ledsAssert - LEDs that are to be asserted new
94 * or to a different state
95 * @param[in] ledsDeAssert - LEDs that are to be Deasserted
96 *
97 * @return - Success or exception thrown
98 */
99 bool setGroupState(const std::string& path, bool assert, group& ledsAssert,
100 group& ledsDeAssert);
Vishwanatha Subbannaed490732016-12-20 15:59:29 +0530101
Patrick Venture91ac8d32018-11-01 17:03:22 -0700102 /** @brief Finds the set of LEDs to operate on and executes action
103 *
104 * @param[in] ledsAssert - LEDs that are to be asserted newly
105 * or to a different state
106 * @param[in] ledsDeAssert - LEDs that are to be Deasserted
107 *
108 * @return: None
109 */
110 void driveLEDs(group& ledsAssert, group& ledsDeAssert);
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +0530111
Patrick Venture91ac8d32018-11-01 17:03:22 -0700112 private:
113 /** @brief sdbusplus handler */
114 sdbusplus::bus::bus& bus;
Vishwanatha Subbanna11ca8f92017-02-27 19:33:45 +0530115
Patrick Venture91ac8d32018-11-01 17:03:22 -0700116 /** Map of physical LED path to service name */
117 std::map<std::string, std::string> phyLeds{};
Vishwanatha Subbannadcc3f382017-03-24 20:15:02 +0530118
Patrick Venture91ac8d32018-11-01 17:03:22 -0700119 /** @brief Pointers to groups that are in asserted state */
120 std::set<const group*> assertedGroups;
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +0530121
Patrick Venture91ac8d32018-11-01 17:03:22 -0700122 /** @brief Contains the highest priority actions for all
123 * asserted LEDs.
124 */
125 group currentState;
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +0530126
Patrick Venture91ac8d32018-11-01 17:03:22 -0700127 /** @brief Contains the set of all actions for asserted LEDs */
128 group combinedState;
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +0530129
Patrick Venture91ac8d32018-11-01 17:03:22 -0700130 /** @brief Returns action string based on enum
131 *
132 * @param[in] action - Action enum
133 *
134 * @return string equivalent of the passed in enumeration
135 */
136 static std::string getPhysicalAction(Layout::Action action);
Vishwanatha Subbanna11ca8f92017-02-27 19:33:45 +0530137
Patrick Venture91ac8d32018-11-01 17:03:22 -0700138 /** @brief Chooses appropriate action to be triggered on physical LED
139 * and calls into function that applies the actual action.
140 *
141 * @param[in] objPath - dbus object path
142 * @param[in] action - Intended action to be triggered
143 * @param[in] dutyOn - Duty Cycle ON percentage
tony lee6fd9e442019-04-23 09:09:15 +0800144 * @param[in] period - Time taken for one blink cycle
Patrick Venture91ac8d32018-11-01 17:03:22 -0700145 */
146 void drivePhysicalLED(const std::string& objPath, Layout::Action action,
tony lee6fd9e442019-04-23 09:09:15 +0800147 uint8_t dutyOn, const uint16_t period);
Vishwanatha Subbanna11ca8f92017-02-27 19:33:45 +0530148
Patrick Venture91ac8d32018-11-01 17:03:22 -0700149 /** @brief Makes a dbus call to a passed in service name.
150 * This is now the physical LED controller
151 *
152 * @param[in] service - dbus service name
153 * @param[in] objPath - dbus object path
154 * @param[in] property - property to be written to
155 * @param[in] value - Value to write
156 */
157 template <typename T>
158 void drivePhysicalLED(const std::string& service,
159 const std::string& objPath,
160 const std::string& property, const T& value)
161 {
Patrick Williamsa41d2822020-05-13 17:57:23 -0500162 std::variant<T> data = value;
Vishwanatha Subbanna11ca8f92017-02-27 19:33:45 +0530163
Patrick Venture91ac8d32018-11-01 17:03:22 -0700164 auto method = bus.new_method_call(service.c_str(), objPath.c_str(),
165 DBUS_PROPERTY_IFACE, "Set");
166 method.append(PHY_LED_IFACE);
167 method.append(property);
168 method.append(data);
Vishwanatha Subbanna11ca8f92017-02-27 19:33:45 +0530169
Patrick Venture91ac8d32018-11-01 17:03:22 -0700170 // There will be exceptions going forward and hence don't need a
171 // response
172 bus.call_noreply(method);
173 return;
174 }
Vishwanatha Subbanna11ca8f92017-02-27 19:33:45 +0530175
Patrick Venture91ac8d32018-11-01 17:03:22 -0700176 /** @brief Populates map of Physical LED paths to service name */
177 void populateObjectMap();
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +0530178};
179
180} // namespace led
181} // namespace phosphor