blob: c96a4bc0d0912a22cda4b3406cf0a7f411517baa [file] [log] [blame]
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +05301#pragma once
2
3#include <map>
4#include <set>
Vishwanatha Subbanna11ca8f92017-02-27 19:33:45 +05305#include <sdbusplus/bus.hpp>
Vishwanatha Subbannaed490732016-12-20 15:59:29 +05306#include "ledlayout.hpp"
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +05307
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +05308namespace phosphor
9{
10namespace led
11{
12
Vishwanatha Subbanna11ca8f92017-02-27 19:33:45 +053013/** @brief Physical LED dbus constructs */
14constexpr auto PHY_LED_PATH = "/xyz/openbmc_project/led/physical/";
15constexpr auto PHY_LED_IFACE = "xyz.openbmc_project.Led.Physical";
16constexpr auto DBUS_PROPERTY_IFACE = "org.freedesktop.DBus.Properties";
17
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053018/** @class Manager
19 * @brief Manages group of LEDs and applies action on the elements of group
20 */
21class Manager
22{
23 public:
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053024 /** @brief Only need the default Manager */
Vishwanatha Subbannaed490732016-12-20 15:59:29 +053025 Manager() = delete;
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053026 ~Manager() = default;
27 Manager(const Manager&) = delete;
28 Manager& operator=(const Manager&) = delete;
29 Manager(Manager&&) = delete;
30 Manager& operator=(Manager&&) = delete;
31
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +053032 /** @brief Special comparator for finding set difference */
Vishwanatha Subbannaed490732016-12-20 15:59:29 +053033 static bool ledComp(const phosphor::led::Layout::LedAction& left,
34 const phosphor::led::Layout::LedAction& right)
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053035 {
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +053036 // Example :
37 // If FIRST_1 is {fan0, 1, 1} and FIRST_2 is {fan0, 2, 2},
38 // with default priority of Blink, this comparator would return
39 // false. But considering the priority, this comparator would need
40 // to return true so that we consider appropriate set and in
41 // this case its {fan0, 1, 1}
42 if (left.name == right.name)
43 {
44 if (left.action == right.action)
45 {
46 return false;
47 }
48 else
49 {
50 return true;
51 }
52 }
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053053 return left.name < right.name;
54 }
55
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +053056 /** @brief Comparator for finding LEDs to be DeAsserted */
57 static bool ledLess(const phosphor::led::Layout::LedAction& left,
58 const phosphor::led::Layout::LedAction& right)
59 {
60 return left.name < right.name;
61 }
62
63 /** @brief Comparator for helping unique_copy */
64 static bool ledEqual(const phosphor::led::Layout::LedAction& left,
65 const phosphor::led::Layout::LedAction& right)
66 {
67 return left.name == right.name;
68 }
69
Vishwanatha Subbannaed490732016-12-20 15:59:29 +053070 using group = std::set<phosphor::led::Layout::LedAction>;
Vishwanatha Subbanna11ca8f92017-02-27 19:33:45 +053071 using LedLayout = std::map<std::string, group>;
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053072
73 /** @brief static global map constructed at compile time */
Vishwanatha Subbanna11ca8f92017-02-27 19:33:45 +053074 const LedLayout& ledMap;
Vishwanatha Subbannaed490732016-12-20 15:59:29 +053075
Vishwanatha Subbanna11ca8f92017-02-27 19:33:45 +053076 /** @brief Refer the user supplied LED layout and sdbusplus handler
Vishwanatha Subbannaed490732016-12-20 15:59:29 +053077 *
Vishwanatha Subbanna11ca8f92017-02-27 19:33:45 +053078 * @param [in] bus - sdbusplus handler
79 * @param [in] LedLayout - LEDs group layout
Vishwanatha Subbannaed490732016-12-20 15:59:29 +053080 */
Vishwanatha Subbanna11ca8f92017-02-27 19:33:45 +053081 Manager(sdbusplus::bus::bus& bus,
82 const LedLayout& ledLayout)
83 : ledMap(ledLayout),
84 bus(bus)
Vishwanatha Subbannaed490732016-12-20 15:59:29 +053085 {
86 // Nothing here
87 }
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053088
89 /** @brief Given a group name, applies the action on the group
90 *
Vishwanatha Subbannaed490732016-12-20 15:59:29 +053091 * @param[in] path - dbus path of group
92 * @param[in] assert - Could be true or false
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +053093 * @param[in] ledsAssert - LEDs that are to be asserted new
94 * or to a different state
Vishwanatha Subbannaed490732016-12-20 15:59:29 +053095 * @param[in] ledsDeAssert - LEDs that are to be Deasserted
Vishwanatha Subbannaed490732016-12-20 15:59:29 +053096 *
97 * @return - Success or exception thrown
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053098 */
Vishwanatha Subbannaed490732016-12-20 15:59:29 +053099 bool setGroupState(const std::string& path, bool assert,
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +0530100 group& ledsAssert, group& ledsDeAssert);
Vishwanatha Subbannaed490732016-12-20 15:59:29 +0530101
102 /** @brief Finds the set of LEDs to operate on and executes action
103 *
104 * @param[in] ledsAssert - LEDs that are to be asserted newly
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +0530105 * or to a different state
Vishwanatha Subbannaed490732016-12-20 15:59:29 +0530106 * @param[in] ledsDeAssert - LEDs that are to be Deasserted
Vishwanatha Subbannaed490732016-12-20 15:59:29 +0530107 *
108 * @return: None
109 */
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +0530110 void driveLEDs(group& ledsAssert, group& ledsDeAssert);
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +0530111
112 private:
Vishwanatha Subbanna11ca8f92017-02-27 19:33:45 +0530113 /** @brief sdbusplus handler */
114 sdbusplus::bus::bus& bus;
115
Vishwanatha Subbannadcc3f382017-03-24 20:15:02 +0530116 /** Map of physical LED path to service name */
117 std::map<std::string, std::string> phyLeds {};
118
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +0530119 /** @brief Pointers to groups that are in asserted state */
120 std::set<const group*> assertedGroups;
121
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +0530122 /** @brief Contains the highest priority actions for all
123 * asserted LEDs.
124 */
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +0530125 group currentState;
126
Vishwanatha Subbanna4b000d82017-05-03 18:44:16 +0530127 /** @brief Contains the set of all actions for asserted LEDs */
128 group combinedState;
129
Vishwanatha Subbanna11ca8f92017-02-27 19:33:45 +0530130 /** @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 */
Vishwanatha Subbanna4fa92482017-03-10 14:39:20 +0530136 static std::string getPhysicalAction(Layout::Action action);
Vishwanatha Subbanna11ca8f92017-02-27 19:33:45 +0530137
138 /** @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
144 */
145 void drivePhysicalLED(const std::string& objPath,
146 Layout::Action action,
147 uint8_t dutyOn);
148
149 /** @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,
161 const T& value)
162 {
163 sdbusplus::message::variant<T> data = value;
164
165 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);
170
171 // There will be exceptions going forward and hence don't need a
172 // response
173 bus.call_noreply(method);
174 return;
175 }
176
Vishwanatha Subbannadcc3f382017-03-24 20:15:02 +0530177 /** @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