blob: 4adb229e0d6b7dc944f7e7c1d6420ce43a34b279 [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 Subbanna4c8c72b2016-11-29 23:02:06 +05307namespace phosphor
8{
9namespace led
10{
11
Vishwanatha Subbanna11ca8f92017-02-27 19:33:45 +053012/** @brief Physical LED dbus constructs */
13constexpr auto PHY_LED_PATH = "/xyz/openbmc_project/led/physical/";
14constexpr auto PHY_LED_IFACE = "xyz.openbmc_project.Led.Physical";
15constexpr auto DBUS_PROPERTY_IFACE = "org.freedesktop.DBus.Properties";
16
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053017/** @class Manager
18 * @brief Manages group of LEDs and applies action on the elements of group
19 */
20class Manager
21{
22 public:
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053023 /** @brief Only need the default Manager */
Vishwanatha Subbannaed490732016-12-20 15:59:29 +053024 Manager() = delete;
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053025 ~Manager() = default;
26 Manager(const Manager&) = delete;
27 Manager& operator=(const Manager&) = delete;
28 Manager(Manager&&) = delete;
29 Manager& operator=(Manager&&) = delete;
30
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053031 /** @brief For finding intersection */
Vishwanatha Subbannaed490732016-12-20 15:59:29 +053032 static bool ledComp(const phosphor::led::Layout::LedAction& left,
33 const phosphor::led::Layout::LedAction& right)
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053034 {
35 return left.name < right.name;
36 }
37
Vishwanatha Subbannaed490732016-12-20 15:59:29 +053038 using group = std::set<phosphor::led::Layout::LedAction>;
Vishwanatha Subbanna11ca8f92017-02-27 19:33:45 +053039 using LedLayout = std::map<std::string, group>;
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053040
41 /** @brief static global map constructed at compile time */
Vishwanatha Subbanna11ca8f92017-02-27 19:33:45 +053042 const LedLayout& ledMap;
Vishwanatha Subbannaed490732016-12-20 15:59:29 +053043
Vishwanatha Subbanna11ca8f92017-02-27 19:33:45 +053044 /** @brief Refer the user supplied LED layout and sdbusplus handler
Vishwanatha Subbannaed490732016-12-20 15:59:29 +053045 *
Vishwanatha Subbanna11ca8f92017-02-27 19:33:45 +053046 * @param [in] bus - sdbusplus handler
47 * @param [in] LedLayout - LEDs group layout
Vishwanatha Subbannaed490732016-12-20 15:59:29 +053048 */
Vishwanatha Subbanna11ca8f92017-02-27 19:33:45 +053049 Manager(sdbusplus::bus::bus& bus,
50 const LedLayout& ledLayout)
51 : ledMap(ledLayout),
52 bus(bus)
Vishwanatha Subbannaed490732016-12-20 15:59:29 +053053 {
54 // Nothing here
55 }
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053056
57 /** @brief Given a group name, applies the action on the group
58 *
Vishwanatha Subbannaed490732016-12-20 15:59:29 +053059 * @param[in] path - dbus path of group
60 * @param[in] assert - Could be true or false
61 * @param[in] ledsAssert - LEDs that are to be asserted newly
62 * @param[in] ledsDeAssert - LEDs that are to be Deasserted
63 * @param[in] ledsUpdate - LEDs that need a transition between
64 * different types of asserted states.
65 *
66 * @return - Success or exception thrown
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053067 */
Vishwanatha Subbannaed490732016-12-20 15:59:29 +053068 bool setGroupState(const std::string& path, bool assert,
69 group& ledsAssert, group& ledsDeAssert,
70 group& ledsUpdate);
71
72 /** @brief Finds the set of LEDs to operate on and executes action
73 *
74 * @param[in] ledsAssert - LEDs that are to be asserted newly
75 * @param[in] ledsDeAssert - LEDs that are to be Deasserted
76 * @param[in] ledsUpdate - LEDs that need a transition between
77 * different types of asserted states.
78 *
79 * @return: None
80 */
81 void driveLEDs(group& ledsAssert, group& ledsDeAssert,
82 group& ledsUpdate);
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053083
84 private:
Vishwanatha Subbanna11ca8f92017-02-27 19:33:45 +053085 /** @brief sdbusplus handler */
86 sdbusplus::bus::bus& bus;
87
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +053088 /** @brief Pointers to groups that are in asserted state */
89 std::set<const group*> assertedGroups;
90
91 /** @brief Contains the LEDs that are in asserted state */
92 group currentState;
93
Vishwanatha Subbanna11ca8f92017-02-27 19:33:45 +053094 /** @brief Returns action string based on enum
95 *
96 * @param[in] action - Action enum
97 *
98 * @return string equivalent of the passed in enumeration
99 */
Vishwanatha Subbanna4fa92482017-03-10 14:39:20 +0530100 static std::string getPhysicalAction(Layout::Action action);
Vishwanatha Subbanna11ca8f92017-02-27 19:33:45 +0530101
102 /** @brief Chooses appropriate action to be triggered on physical LED
103 * and calls into function that applies the actual action.
104 *
105 * @param[in] objPath - dbus object path
106 * @param[in] action - Intended action to be triggered
107 * @param[in] dutyOn - Duty Cycle ON percentage
108 */
109 void drivePhysicalLED(const std::string& objPath,
110 Layout::Action action,
111 uint8_t dutyOn);
112
113 /** @brief Makes a dbus call to a passed in service name.
114 * This is now the physical LED controller
115 *
116 * @param[in] service - dbus service name
117 * @param[in] objPath - dbus object path
118 * @param[in] property - property to be written to
119 * @param[in] value - Value to write
120 */
121 template <typename T>
122 void drivePhysicalLED(const std::string& service,
123 const std::string& objPath,
124 const std::string& property,
125 const T& value)
126 {
127 sdbusplus::message::variant<T> data = value;
128
129 auto method = bus.new_method_call(service.c_str(), objPath.c_str(),
130 DBUS_PROPERTY_IFACE, "Set");
131 method.append(PHY_LED_IFACE);
132 method.append(property);
133 method.append(data);
134
135 // There will be exceptions going forward and hence don't need a
136 // response
137 bus.call_noreply(method);
138 return;
139 }
140
141 /** @brief Finds the service name given a dbus object path and interface
142 *
143 * @param[in] objPath - dbus object path
144 * @param[in] interface - dbus interface
145 *
146 * @return: Service name or none
147 */
148 std::string getServiceName(const std::string& objPath,
149 const std::string& interface) const;
Vishwanatha Subbanna4c8c72b2016-11-29 23:02:06 +0530150};
151
152} // namespace led
153} // namespace phosphor