xref: /openbmc/bmcweb/redfish-core/lib/led.hpp (revision 40e9b92ec19acffb46f83a6e55b18974da5d708e)
1 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: Copyright OpenBMC Authors
3 // SPDX-FileCopyrightText: Copyright 2019 Intel Corporation
4 #pragma once
5 
6 #include "app.hpp"
7 #include "async_resp.hpp"
8 #include "dbus_utility.hpp"
9 #include "generated/enums/chassis.hpp"
10 #include "redfish_util.hpp"
11 
12 #include <sdbusplus/asio/property.hpp>
13 
14 namespace redfish
15 {
16 /**
17  * @brief Retrieves identify led group properties over dbus
18  *
19  * @param[in] asyncResp     Shared pointer for generating response message.
20  *
21  * @return None.
22  */
23 // TODO (Gunnar): Remove IndicatorLED after enough time has passed
24 inline void
25     getIndicatorLedState(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
26 {
27     BMCWEB_LOG_DEBUG("Get led groups");
28     dbus::utility::getProperty<bool>(
29         "xyz.openbmc_project.LED.GroupManager",
30         "/xyz/openbmc_project/led/groups/enclosure_identify_blink",
31         "xyz.openbmc_project.Led.Group", "Asserted",
32         [asyncResp](const boost::system::error_code& ec, const bool blinking) {
33             // Some systems may not have enclosure_identify_blink object so
34             // proceed to get enclosure_identify state.
35             if (ec == boost::system::errc::invalid_argument)
36             {
37                 BMCWEB_LOG_DEBUG(
38                     "Get identity blinking LED failed, mismatch in property type");
39                 messages::internalError(asyncResp->res);
40                 return;
41             }
42 
43             // Blinking ON, no need to check enclosure_identify assert.
44             if (!ec && blinking)
45             {
46                 asyncResp->res.jsonValue["IndicatorLED"] =
47                     chassis::IndicatorLED::Blinking;
48                 return;
49             }
50 
51             dbus::utility::getProperty<bool>(
52                 "xyz.openbmc_project.LED.GroupManager",
53                 "/xyz/openbmc_project/led/groups/enclosure_identify",
54                 "xyz.openbmc_project.Led.Group", "Asserted",
55                 [asyncResp](const boost::system::error_code& ec2,
56                             const bool ledOn) {
57                     if (ec2 == boost::system::errc::invalid_argument)
58                     {
59                         BMCWEB_LOG_DEBUG(
60                             "Get enclosure identity led failed, mismatch in property type");
61                         messages::internalError(asyncResp->res);
62                         return;
63                     }
64 
65                     if (ec2)
66                     {
67                         return;
68                     }
69 
70                     if (ledOn)
71                     {
72                         asyncResp->res.jsonValue["IndicatorLED"] =
73                             chassis::IndicatorLED::Lit;
74                     }
75                     else
76                     {
77                         asyncResp->res.jsonValue["IndicatorLED"] =
78                             chassis::IndicatorLED::Off;
79                     }
80                 });
81         });
82 }
83 
84 /**
85  * @brief Sets identify led group properties
86  *
87  * @param[in] asyncResp     Shared pointer for generating response message.
88  * @param[in] ledState  LED state passed from request
89  *
90  * @return None.
91  */
92 // TODO (Gunnar): Remove IndicatorLED after enough time has passed
93 inline void
94     setIndicatorLedState(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
95                          const std::string& ledState)
96 {
97     BMCWEB_LOG_DEBUG("Set led groups");
98     bool ledOn = false;
99     bool ledBlinkng = false;
100 
101     if (ledState == "Lit")
102     {
103         ledOn = true;
104     }
105     else if (ledState == "Blinking")
106     {
107         ledBlinkng = true;
108     }
109     else if (ledState != "Off")
110     {
111         messages::propertyValueNotInList(asyncResp->res, ledState,
112                                          "IndicatorLED");
113         return;
114     }
115 
116     sdbusplus::asio::setProperty(
117         *crow::connections::systemBus, "xyz.openbmc_project.LED.GroupManager",
118         "/xyz/openbmc_project/led/groups/enclosure_identify_blink",
119         "xyz.openbmc_project.Led.Group", "Asserted", ledBlinkng,
120         [asyncResp, ledOn,
121          ledBlinkng](const boost::system::error_code& ec) mutable {
122             if (ec)
123             {
124                 // Some systems may not have enclosure_identify_blink object so
125                 // Lets set enclosure_identify state to true if Blinking is
126                 // true.
127                 if (ledBlinkng)
128                 {
129                     ledOn = true;
130                 }
131             }
132             setDbusProperty(
133                 asyncResp, "IndicatorLED",
134                 "xyz.openbmc_project.LED.GroupManager",
135                 sdbusplus::message::object_path(
136                     "/xyz/openbmc_project/led/groups/enclosure_identify"),
137                 "xyz.openbmc_project.Led.Group", "Asserted", ledBlinkng);
138         });
139 }
140 
141 /**
142  * @brief Retrieves identify system led group properties over dbus
143  *
144  * @param[in] asyncResp     Shared pointer for generating response message.
145  *
146  * @return None.
147  */
148 inline void getSystemLocationIndicatorActive(
149     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
150 {
151     BMCWEB_LOG_DEBUG("Get LocationIndicatorActive");
152     dbus::utility::getProperty<bool>(
153         "xyz.openbmc_project.LED.GroupManager",
154         "/xyz/openbmc_project/led/groups/enclosure_identify_blink",
155         "xyz.openbmc_project.Led.Group", "Asserted",
156         [asyncResp](const boost::system::error_code& ec, const bool blinking) {
157             // Some systems may not have enclosure_identify_blink object so
158             // proceed to get enclosure_identify state.
159             if (ec == boost::system::errc::invalid_argument)
160             {
161                 BMCWEB_LOG_DEBUG(
162                     "Get identity blinking LED failed, mismatch in property type");
163                 messages::internalError(asyncResp->res);
164                 return;
165             }
166 
167             // Blinking ON, no need to check enclosure_identify assert.
168             if (!ec && blinking)
169             {
170                 asyncResp->res.jsonValue["LocationIndicatorActive"] = true;
171                 return;
172             }
173 
174             dbus::utility::getProperty<bool>(
175                 "xyz.openbmc_project.LED.GroupManager",
176                 "/xyz/openbmc_project/led/groups/enclosure_identify",
177                 "xyz.openbmc_project.Led.Group", "Asserted",
178                 [asyncResp](const boost::system::error_code& ec2,
179                             const bool ledOn) {
180                     if (ec2 == boost::system::errc::invalid_argument)
181                     {
182                         BMCWEB_LOG_DEBUG(
183                             "Get enclosure identity led failed, mismatch in property type");
184                         messages::internalError(asyncResp->res);
185                         return;
186                     }
187 
188                     if (ec2)
189                     {
190                         return;
191                     }
192 
193                     asyncResp->res.jsonValue["LocationIndicatorActive"] = ledOn;
194                 });
195         });
196 }
197 
198 /**
199  * @brief Sets identify system led group properties
200  *
201  * @param[in] asyncResp     Shared pointer for generating response message.
202  * @param[in] ledState  LED state passed from request
203  *
204  * @return None.
205  */
206 inline void setSystemLocationIndicatorActive(
207     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, const bool ledState)
208 {
209     BMCWEB_LOG_DEBUG("Set LocationIndicatorActive");
210 
211     sdbusplus::asio::setProperty(
212         *crow::connections::systemBus, "xyz.openbmc_project.LED.GroupManager",
213         "/xyz/openbmc_project/led/groups/enclosure_identify_blink",
214         "xyz.openbmc_project.Led.Group", "Asserted", ledState,
215         [asyncResp, ledState](const boost::system::error_code& ec) {
216             if (ec)
217             {
218                 // Some systems may not have enclosure_identify_blink object so
219                 // lets set enclosure_identify state also if
220                 // enclosure_identify_blink failed
221                 setDbusProperty(
222                     asyncResp, "LocationIndicatorActive",
223                     "xyz.openbmc_project.LED.GroupManager",
224                     sdbusplus::message::object_path(
225                         "/xyz/openbmc_project/led/groups/enclosure_identify"),
226                     "xyz.openbmc_project.Led.Group", "Asserted", ledState);
227             }
228         });
229 }
230 } // namespace redfish
231