blob: 0cc5cf62dc2f727f327b22ab0c5e835023f1a5ac [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 <sdbusplus/bus.hpp>
George Liua6c18f82020-06-22 10:50:04 +08006
7#include <map>
Patrick Venture91ac8d32018-11-01 17:03:22 -07008#include <set>
Andrew Geisslerd02c3cb2020-05-16 10:28:02 -05009#include <string>
Patrick Venture91ac8d32018-11-01 17:03:22 -070010
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053011namespace phosphor
12{
13namespace led
14{
15
Vishwanatha Subbanna11ca8f92017-02-27 19:33:45 +053016/** @brief Physical LED dbus constructs */
17constexpr auto PHY_LED_PATH = "/xyz/openbmc_project/led/physical/";
18constexpr auto PHY_LED_IFACE = "xyz.openbmc_project.Led.Physical";
19constexpr auto DBUS_PROPERTY_IFACE = "org.freedesktop.DBus.Properties";
20
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053021/** @class Manager
22 * @brief Manages group of LEDs and applies action on the elements of group
23 */
24class Manager
25{
Patrick Venture91ac8d32018-11-01 17:03:22 -070026 public:
27 /** @brief Only need the default Manager */
28 Manager() = delete;
29 ~Manager() = default;
30 Manager(const Manager&) = delete;
31 Manager& operator=(const Manager&) = delete;
32 Manager(Manager&&) = delete;
33 Manager& operator=(Manager&&) = delete;
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053034
Patrick Venture91ac8d32018-11-01 17:03:22 -070035 /** @brief Special comparator for finding set difference */
36 static bool ledComp(const phosphor::led::Layout::LedAction& left,
37 const phosphor::led::Layout::LedAction& right)
38 {
39 // Example :
40 // If FIRST_1 is {fan0, 1, 1} and FIRST_2 is {fan0, 2, 2},
41 // with default priority of Blink, this comparator would return
42 // false. But considering the priority, this comparator would need
43 // to return true so that we consider appropriate set and in
44 // this case its {fan0, 1, 1}
45 if (left.name == right.name)
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053046 {
Patrick Venture91ac8d32018-11-01 17:03:22 -070047 if (left.action == right.action)
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +053048 {
Patrick Venture91ac8d32018-11-01 17:03:22 -070049 return false;
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +053050 }
Patrick Venture91ac8d32018-11-01 17:03:22 -070051 else
52 {
53 return true;
54 }
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053055 }
Patrick Venture91ac8d32018-11-01 17:03:22 -070056 return left.name < right.name;
57 }
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053058
Patrick Venture91ac8d32018-11-01 17:03:22 -070059 /** @brief Comparator for finding LEDs to be DeAsserted */
60 static bool ledLess(const phosphor::led::Layout::LedAction& left,
61 const phosphor::led::Layout::LedAction& right)
62 {
63 return left.name < right.name;
64 }
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +053065
Patrick Venture91ac8d32018-11-01 17:03:22 -070066 /** @brief Comparator for helping unique_copy */
67 static bool ledEqual(const phosphor::led::Layout::LedAction& left,
68 const phosphor::led::Layout::LedAction& right)
69 {
70 return left.name == right.name;
71 }
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +053072
Patrick Venture91ac8d32018-11-01 17:03:22 -070073 using group = std::set<phosphor::led::Layout::LedAction>;
74 using LedLayout = std::map<std::string, group>;
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053075
Patrick Venture91ac8d32018-11-01 17:03:22 -070076 /** @brief static global map constructed at compile time */
77 const LedLayout& ledMap;
Vishwanatha Subbannaed490732016-12-20 15:59:29 +053078
Patrick Venture91ac8d32018-11-01 17:03:22 -070079 /** @brief Refer the user supplied LED layout and sdbusplus handler
80 *
81 * @param [in] bus - sdbusplus handler
82 * @param [in] LedLayout - LEDs group layout
83 */
84 Manager(sdbusplus::bus::bus& bus, const LedLayout& ledLayout) :
85 ledMap(ledLayout), bus(bus)
86 {
87 // Nothing here
88 }
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053089
Patrick Venture91ac8d32018-11-01 17:03:22 -070090 /** @brief Given a group name, applies the action on the group
91 *
92 * @param[in] path - dbus path of group
93 * @param[in] assert - Could be true or false
94 * @param[in] ledsAssert - LEDs that are to be asserted new
95 * or to a different state
96 * @param[in] ledsDeAssert - LEDs that are to be Deasserted
97 *
98 * @return - Success or exception thrown
99 */
100 bool setGroupState(const std::string& path, bool assert, group& ledsAssert,
101 group& ledsDeAssert);
Vishwanatha Subbannaed490732016-12-20 15:59:29 +0530102
Patrick Venture91ac8d32018-11-01 17:03:22 -0700103 /** @brief Finds the set of LEDs to operate on and executes action
104 *
105 * @param[in] ledsAssert - LEDs that are to be asserted newly
106 * or to a different state
107 * @param[in] ledsDeAssert - LEDs that are to be Deasserted
108 *
109 * @return: None
110 */
111 void driveLEDs(group& ledsAssert, group& ledsDeAssert);
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +0530112
Patrick Venture91ac8d32018-11-01 17:03:22 -0700113 private:
114 /** @brief sdbusplus handler */
115 sdbusplus::bus::bus& bus;
Vishwanatha Subbanna11ca8f92017-02-27 19:33:45 +0530116
Patrick Venture91ac8d32018-11-01 17:03:22 -0700117 /** Map of physical LED path to service name */
118 std::map<std::string, std::string> phyLeds{};
Vishwanatha Subbannadcc3f382017-03-24 20:15:02 +0530119
Patrick Venture91ac8d32018-11-01 17:03:22 -0700120 /** @brief Pointers to groups that are in asserted state */
121 std::set<const group*> assertedGroups;
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +0530122
Patrick Venture91ac8d32018-11-01 17:03:22 -0700123 /** @brief Contains the highest priority actions for all
124 * asserted LEDs.
125 */
126 group currentState;
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +0530127
Patrick Venture91ac8d32018-11-01 17:03:22 -0700128 /** @brief Contains the set of all actions for asserted LEDs */
129 group combinedState;
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +0530130
Patrick Venture91ac8d32018-11-01 17:03:22 -0700131 /** @brief Returns action string based on enum
132 *
133 * @param[in] action - Action enum
134 *
135 * @return string equivalent of the passed in enumeration
136 */
137 static std::string getPhysicalAction(Layout::Action action);
Vishwanatha Subbanna11ca8f92017-02-27 19:33:45 +0530138
Patrick Venture91ac8d32018-11-01 17:03:22 -0700139 /** @brief Chooses appropriate action to be triggered on physical LED
140 * and calls into function that applies the actual action.
141 *
142 * @param[in] objPath - dbus object path
143 * @param[in] action - Intended action to be triggered
144 * @param[in] dutyOn - Duty Cycle ON percentage
tony lee6fd9e442019-04-23 09:09:15 +0800145 * @param[in] period - Time taken for one blink cycle
Patrick Venture91ac8d32018-11-01 17:03:22 -0700146 */
147 void drivePhysicalLED(const std::string& objPath, Layout::Action action,
tony lee6fd9e442019-04-23 09:09:15 +0800148 uint8_t dutyOn, const uint16_t period);
Vishwanatha Subbanna11ca8f92017-02-27 19:33:45 +0530149
Patrick Venture91ac8d32018-11-01 17:03:22 -0700150 /** @brief Makes a dbus call to a passed in service name.
151 * This is now the physical LED controller
152 *
153 * @param[in] service - dbus service name
154 * @param[in] objPath - dbus object path
155 * @param[in] property - property to be written to
156 * @param[in] value - Value to write
157 */
158 template <typename T>
159 void drivePhysicalLED(const std::string& service,
160 const std::string& objPath,
161 const std::string& property, const T& value)
162 {
Patrick Williamsa41d2822020-05-13 17:57:23 -0500163 std::variant<T> data = value;
Vishwanatha Subbanna11ca8f92017-02-27 19:33:45 +0530164
Patrick Venture91ac8d32018-11-01 17:03:22 -0700165 auto method = bus.new_method_call(service.c_str(), objPath.c_str(),
166 DBUS_PROPERTY_IFACE, "Set");
167 method.append(PHY_LED_IFACE);
168 method.append(property);
169 method.append(data);
Vishwanatha Subbanna11ca8f92017-02-27 19:33:45 +0530170
Patrick Venture91ac8d32018-11-01 17:03:22 -0700171 // There will be exceptions going forward and hence don't need a
172 // response
173 bus.call_noreply(method);
174 return;
175 }
Vishwanatha Subbanna11ca8f92017-02-27 19:33:45 +0530176
Patrick Venture91ac8d32018-11-01 17:03:22 -0700177 /** @brief Populates map of Physical LED paths to service name */
178 void populateObjectMap();
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +0530179};
180
181} // namespace led
182} // namespace phosphor