xref: /openbmc/phosphor-led-manager/manager/manager.hpp (revision 349d22e358945cce4d6b1d6df808a00bc06e378c)
1953315d2SPatrick Williams #pragma once
2953315d2SPatrick Williams 
37ba70c82SAlexander Hansen #include "grouplayout.hpp"
4953315d2SPatrick Williams #include "ledlayout.hpp"
5953315d2SPatrick Williams #include "utils.hpp"
6953315d2SPatrick Williams 
7f1ed4796SPotin Lai #include <sdeventplus/event.hpp>
8f1ed4796SPotin Lai #include <sdeventplus/utility/timer.hpp>
9f1ed4796SPotin Lai 
10953315d2SPatrick Williams #include <set>
11953315d2SPatrick Williams #include <string>
12f2044037SPatrick Williams #include <unordered_map>
13953315d2SPatrick Williams 
14a6f9a41eSAlexander Hansen // to better see what the string is representing
15a6f9a41eSAlexander Hansen using LedName = std::string;
16a6f9a41eSAlexander Hansen 
17953315d2SPatrick Williams namespace phosphor
18953315d2SPatrick Williams {
19953315d2SPatrick Williams namespace led
20953315d2SPatrick Williams {
21953315d2SPatrick Williams using namespace phosphor::led::utils;
22953315d2SPatrick Williams 
231f0b715aSGeorge Liu static constexpr auto phyLedPath = "/xyz/openbmc_project/led/physical/";
241f0b715aSGeorge Liu static constexpr auto phyLedIntf = "xyz.openbmc_project.Led.Physical";
25953315d2SPatrick Williams 
26953315d2SPatrick Williams /** @class Manager
27953315d2SPatrick Williams  *  @brief Manages group of LEDs and applies action on the elements of group
28953315d2SPatrick Williams  */
29953315d2SPatrick Williams class Manager
30953315d2SPatrick Williams {
31953315d2SPatrick Williams   public:
32953315d2SPatrick Williams     /** @brief Only need the default Manager */
33953315d2SPatrick Williams     Manager() = delete;
34953315d2SPatrick Williams     ~Manager() = default;
35953315d2SPatrick Williams     Manager(const Manager&) = delete;
36953315d2SPatrick Williams     Manager& operator=(const Manager&) = delete;
37953315d2SPatrick Williams     Manager(Manager&&) = delete;
38953315d2SPatrick Williams     Manager& operator=(Manager&&) = delete;
39953315d2SPatrick Williams 
40953315d2SPatrick Williams     /** @brief Special comparator for finding set difference */
ledComp(const phosphor::led::Layout::LedAction & left,const phosphor::led::Layout::LedAction & right)41953315d2SPatrick Williams     static bool ledComp(const phosphor::led::Layout::LedAction& left,
42953315d2SPatrick Williams                         const phosphor::led::Layout::LedAction& right)
43953315d2SPatrick Williams     {
44953315d2SPatrick Williams         // Example :
45953315d2SPatrick Williams         // If FIRST_1 is {fan0, 1, 1} and FIRST_2 is {fan0, 2, 2},
46953315d2SPatrick Williams         // with default priority of Blink, this comparator would return
47953315d2SPatrick Williams         // false. But considering the priority, this comparator would need
48953315d2SPatrick Williams         // to return true so that we consider appropriate set and in
49953315d2SPatrick Williams         // this case its {fan0, 1, 1}
50953315d2SPatrick Williams         if (left.name == right.name)
51953315d2SPatrick Williams         {
52*349d22e3SGeorge Liu             return left.action != right.action;
53953315d2SPatrick Williams         }
54953315d2SPatrick Williams         return left.name < right.name;
55953315d2SPatrick Williams     }
56953315d2SPatrick Williams 
57953315d2SPatrick Williams     /** @brief Comparator for finding LEDs to be DeAsserted */
ledLess(const phosphor::led::Layout::LedAction & left,const phosphor::led::Layout::LedAction & right)58953315d2SPatrick Williams     static bool ledLess(const phosphor::led::Layout::LedAction& left,
59953315d2SPatrick Williams                         const phosphor::led::Layout::LedAction& right)
60953315d2SPatrick Williams     {
61953315d2SPatrick Williams         return left.name < right.name;
62953315d2SPatrick Williams     }
63953315d2SPatrick Williams 
64953315d2SPatrick Williams     /** @brief Comparator for helping unique_copy */
ledEqual(const phosphor::led::Layout::LedAction & left,const phosphor::led::Layout::LedAction & right)65953315d2SPatrick Williams     static bool ledEqual(const phosphor::led::Layout::LedAction& left,
66953315d2SPatrick Williams                          const phosphor::led::Layout::LedAction& right)
67953315d2SPatrick Williams     {
68953315d2SPatrick Williams         return left.name == right.name;
69953315d2SPatrick Williams     }
70953315d2SPatrick Williams 
71953315d2SPatrick Williams     /** @brief static global map constructed at compile time */
72158b2c14SPatrick Williams     const GroupMap& ledMap;
73953315d2SPatrick Williams 
74953315d2SPatrick Williams     /** @brief Refer the user supplied LED layout and sdbusplus handler
75953315d2SPatrick Williams      *
76953315d2SPatrick Williams      *  @param [in] bus       - sdbusplus handler
77158b2c14SPatrick Williams      *  @param [in] GroupMap - LEDs group layout
78f1ed4796SPotin Lai      *  @param [in] Event    - sd event handler
79953315d2SPatrick Williams      */
Manager(sdbusplus::bus_t & bus,const GroupMap & ledLayout,const sdeventplus::Event & event=sdeventplus::Event::get_default ())80f1ed4796SPotin Lai     Manager(
81f1ed4796SPotin Lai         sdbusplus::bus_t& bus, const GroupMap& ledLayout,
82f1ed4796SPotin Lai         const sdeventplus::Event& event = sdeventplus::Event::get_default()) :
83543ac9f1SPatrick Williams         ledMap(ledLayout), bus(bus),
84543ac9f1SPatrick Williams         timer(event, [this](auto&) { driveLedsHandler(); })
85953315d2SPatrick Williams     {
86953315d2SPatrick Williams         // Nothing here
87953315d2SPatrick Williams     }
88953315d2SPatrick Williams 
89a6f9a41eSAlexander Hansen     /* create the resulting map from all currently asserted groups */
907ba70c82SAlexander Hansen     static auto getNewMap(std::set<const Layout::GroupLayout*> assertedGroups)
91a6f9a41eSAlexander Hansen         -> std::map<LedName, Layout::LedAction>;
92a6f9a41eSAlexander Hansen 
93953315d2SPatrick Williams     /** @brief Given a group name, applies the action on the group
94953315d2SPatrick Williams      *
95953315d2SPatrick Williams      *  @param[in]  path          -  dbus path of group
96953315d2SPatrick Williams      *  @param[in]  assert        -  Could be true or false
97953315d2SPatrick Williams      *  @param[in]  ledsAssert    -  LEDs that are to be asserted new
98953315d2SPatrick Williams      *                               or to a different state
99953315d2SPatrick Williams      *  @param[in]  ledsDeAssert  -  LEDs that are to be Deasserted
100953315d2SPatrick Williams      *
101953315d2SPatrick Williams      *  @return                   -  Success or exception thrown
102953315d2SPatrick Williams      */
103158b2c14SPatrick Williams     bool setGroupState(const std::string& path, bool assert,
104158b2c14SPatrick Williams                        ActionSet& ledsAssert, ActionSet& ledsDeAssert);
105953315d2SPatrick Williams 
106953315d2SPatrick Williams     /** @brief Finds the set of LEDs to operate on and executes action
107953315d2SPatrick Williams      *
108953315d2SPatrick Williams      *  @param[in]  ledsAssert    -  LEDs that are to be asserted newly
109953315d2SPatrick Williams      *                               or to a different state
110953315d2SPatrick Williams      *  @param[in]  ledsDeAssert  -  LEDs that are to be Deasserted
111953315d2SPatrick Williams      *
112953315d2SPatrick Williams      *  @return: None
113953315d2SPatrick Williams      */
114158b2c14SPatrick Williams     void driveLEDs(ActionSet& ledsAssert, ActionSet& ledsDeAssert);
115953315d2SPatrick Williams 
116953315d2SPatrick Williams     /** @brief Chooses appropriate action to be triggered on physical LED
117953315d2SPatrick Williams      *  and calls into function that applies the actual action.
118953315d2SPatrick Williams      *
119953315d2SPatrick Williams      *  @param[in]  objPath   -  D-Bus object path
120953315d2SPatrick Williams      *  @param[in]  action    -  Intended action to be triggered
121953315d2SPatrick Williams      *  @param[in]  dutyOn    -  Duty Cycle ON percentage
122953315d2SPatrick Williams      *  @param[in]  period    -  Time taken for one blink cycle
123f1ed4796SPotin Lai      *
124f1ed4796SPotin Lai      *  @return:              -  0: success, -1: LED set failed
125953315d2SPatrick Williams      */
126f0592559SGeorge Liu     static int drivePhysicalLED(const std::string& objPath,
127f0592559SGeorge Liu                                 Layout::Action action, uint8_t dutyOn,
12880f51bbbSGeorge Liu                                 uint16_t period);
129953315d2SPatrick Williams 
130953315d2SPatrick Williams     /** @brief Set lamp test callback when enabled lamp test.
131953315d2SPatrick Williams      *
132953315d2SPatrick Williams      *  @param[in]  callBack   -  Custom callback when enabled lamp test
133953315d2SPatrick Williams      */
134953315d2SPatrick Williams     void setLampTestCallBack(
135158b2c14SPatrick Williams         std::function<bool(ActionSet& ledsAssert, ActionSet& ledsDeAssert)>
136158b2c14SPatrick Williams             callBack);
137953315d2SPatrick Williams 
138953315d2SPatrick Williams   private:
139953315d2SPatrick Williams     /** @brief sdbusplus handler */
1403e073ba6SPatrick Williams     sdbusplus::bus_t& bus;
141953315d2SPatrick Williams 
142953315d2SPatrick Williams     /** Map of physical LED path to service name */
143391bec5fSGeorge Liu     std::unordered_map<std::string, std::string> phyLeds;
144953315d2SPatrick Williams 
145953315d2SPatrick Williams     /** @brief Pointers to groups that are in asserted state */
1467ba70c82SAlexander Hansen     std::set<const Layout::GroupLayout*> assertedGroups;
147953315d2SPatrick Williams 
148a6f9a41eSAlexander Hansen     /** Map of led name to current state */
149a6f9a41eSAlexander Hansen     std::map<std::string, Layout::LedAction> ledStateMap;
150953315d2SPatrick Williams 
151953315d2SPatrick Williams     /** @brief Custom callback when enabled lamp test */
152158b2c14SPatrick Williams     std::function<bool(ActionSet& ledsAssert, ActionSet& ledsDeAssert)>
153953315d2SPatrick Williams         lampTestCallBack;
154953315d2SPatrick Williams 
155f1ed4796SPotin Lai     /** @brief Timer used for LEDs handler callback*/
156f1ed4796SPotin Lai     sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> timer;
157f1ed4796SPotin Lai 
158f1ed4796SPotin Lai     /** @brief Contains the required set of assert LEDs action */
159f1ed4796SPotin Lai     ActionSet reqLedsAssert;
160f1ed4796SPotin Lai 
161f1ed4796SPotin Lai     /** @brief Contains the required set of deassert LEDs action */
162f1ed4796SPotin Lai     ActionSet reqLedsDeAssert;
163f1ed4796SPotin Lai 
164f1ed4796SPotin Lai     /** @brief LEDs handler callback */
165f1ed4796SPotin Lai     void driveLedsHandler();
166f1ed4796SPotin Lai 
167953315d2SPatrick Williams     /** @brief Returns action string based on enum
168953315d2SPatrick Williams      *
169953315d2SPatrick Williams      *  @param[in]  action - Action enum
170953315d2SPatrick Williams      *
171953315d2SPatrick Williams      *  @return string equivalent of the passed in enumeration
172953315d2SPatrick Williams      */
173953315d2SPatrick Williams     static std::string getPhysicalAction(Layout::Action action);
174953315d2SPatrick Williams };
175953315d2SPatrick Williams 
176953315d2SPatrick Williams } // namespace led
177953315d2SPatrick Williams } // namespace phosphor
178