xref: /openbmc/bmcweb/redfish-core/lib/led.hpp (revision 10f270b4)
1 /*
2 // Copyright (c) 2019 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16 #pragma once
17 
18 #include "async_resp.hpp"
19 #include "dbus_utility.hpp"
20 #include "redfish_util.hpp"
21 
22 #include <variant>
23 
24 namespace redfish
25 {
26 /**
27  * @brief Retrieves identify led group properties over dbus
28  *
29  * @param[in] aResp     Shared pointer for generating response message.
30  *
31  * @return None.
32  */
33 // TODO (Gunnar): Remove IndicatorLED after enough time has passed
34 inline void
35     getIndicatorLedState(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
36 {
37     BMCWEB_LOG_DEBUG << "Get led groups";
38     crow::connections::systemBus->async_method_call(
39         [aResp](const boost::system::error_code ec,
40                 const std::variant<bool> asserted) {
41             // Some systems may not have enclosure_identify_blink object so
42             // proceed to get enclosure_identify state.
43             if (!ec)
44             {
45                 const bool* blinking = std::get_if<bool>(&asserted);
46                 if (!blinking)
47                 {
48                     BMCWEB_LOG_DEBUG << "Get identity blinking LED failed";
49                     messages::internalError(aResp->res);
50                     return;
51                 }
52                 // Blinking ON, no need to check enclosure_identify assert.
53                 if (*blinking)
54                 {
55                     aResp->res.jsonValue["IndicatorLED"] = "Blinking";
56                     return;
57                 }
58             }
59             crow::connections::systemBus->async_method_call(
60                 [aResp](const boost::system::error_code ec2,
61                         const std::variant<bool> asserted2) {
62                     if (!ec2)
63                     {
64                         const bool* ledOn = std::get_if<bool>(&asserted2);
65                         if (!ledOn)
66                         {
67                             BMCWEB_LOG_DEBUG
68                                 << "Get enclosure identity led failed";
69                             messages::internalError(aResp->res);
70                             return;
71                         }
72 
73                         if (*ledOn)
74                         {
75                             aResp->res.jsonValue["IndicatorLED"] = "Lit";
76                         }
77                         else
78                         {
79                             aResp->res.jsonValue["IndicatorLED"] = "Off";
80                         }
81                     }
82                     return;
83                 },
84                 "xyz.openbmc_project.LED.GroupManager",
85                 "/xyz/openbmc_project/led/groups/enclosure_identify",
86                 "org.freedesktop.DBus.Properties", "Get",
87                 "xyz.openbmc_project.Led.Group", "Asserted");
88         },
89         "xyz.openbmc_project.LED.GroupManager",
90         "/xyz/openbmc_project/led/groups/enclosure_identify_blink",
91         "org.freedesktop.DBus.Properties", "Get",
92         "xyz.openbmc_project.Led.Group", "Asserted");
93 }
94 
95 /**
96  * @brief Sets identify led group properties
97  *
98  * @param[in] aResp     Shared pointer for generating response message.
99  * @param[in] ledState  LED state passed from request
100  *
101  * @return None.
102  */
103 // TODO (Gunnar): Remove IndicatorLED after enough time has passed
104 inline void
105     setIndicatorLedState(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
106                          const std::string& ledState)
107 {
108     BMCWEB_LOG_DEBUG << "Set led groups";
109     bool ledOn = false;
110     bool ledBlinkng = false;
111 
112     if (ledState == "Lit")
113     {
114         ledOn = true;
115     }
116     else if (ledState == "Blinking")
117     {
118         ledBlinkng = true;
119     }
120     else if (ledState != "Off")
121     {
122         messages::propertyValueNotInList(aResp->res, ledState, "IndicatorLED");
123         return;
124     }
125 
126     crow::connections::systemBus->async_method_call(
127         [aResp, ledOn, ledBlinkng](const boost::system::error_code ec) mutable {
128             if (ec)
129             {
130                 // Some systems may not have enclosure_identify_blink object so
131                 // Lets set enclosure_identify state to true if Blinking is
132                 // true.
133                 if (ledBlinkng)
134                 {
135                     ledOn = true;
136                 }
137             }
138             crow::connections::systemBus->async_method_call(
139                 [aResp](const boost::system::error_code ec2) {
140                     if (ec2)
141                     {
142                         BMCWEB_LOG_DEBUG << "DBUS response error " << ec2;
143                         messages::internalError(aResp->res);
144                         return;
145                     }
146                     messages::success(aResp->res);
147                 },
148                 "xyz.openbmc_project.LED.GroupManager",
149                 "/xyz/openbmc_project/led/groups/enclosure_identify",
150                 "org.freedesktop.DBus.Properties", "Set",
151                 "xyz.openbmc_project.Led.Group", "Asserted",
152                 std::variant<bool>(ledOn));
153         },
154         "xyz.openbmc_project.LED.GroupManager",
155         "/xyz/openbmc_project/led/groups/enclosure_identify_blink",
156         "org.freedesktop.DBus.Properties", "Set",
157         "xyz.openbmc_project.Led.Group", "Asserted",
158         std::variant<bool>(ledBlinkng));
159 }
160 
161 /**
162  * @brief Retrieves identify led group properties over dbus
163  *
164  * @param[in] aResp     Shared pointer for generating response message.
165  *
166  * @return None.
167  */
168 inline void
169     getLocationIndicatorActive(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
170 {
171     BMCWEB_LOG_DEBUG << "Get LocationIndicatorActive";
172     crow::connections::systemBus->async_method_call(
173         [aResp](const boost::system::error_code ec,
174                 const std::variant<bool> asserted) {
175             // Some systems may not have enclosure_identify_blink object so
176             // proceed to get enclosure_identify state.
177             if (!ec)
178             {
179                 const bool* blinking = std::get_if<bool>(&asserted);
180                 if (!blinking)
181                 {
182                     BMCWEB_LOG_DEBUG << "Get identity blinking LED failed";
183                     messages::internalError(aResp->res);
184                     return;
185                 }
186                 // Blinking ON, no need to check enclosure_identify assert.
187                 if (*blinking)
188                 {
189                     aResp->res.jsonValue["LocationIndicatorActive"] = true;
190                     return;
191                 }
192             }
193             crow::connections::systemBus->async_method_call(
194                 [aResp](const boost::system::error_code ec2,
195                         const std::variant<bool> asserted2) {
196                     if (!ec2)
197                     {
198                         const bool* ledOn = std::get_if<bool>(&asserted2);
199                         if (!ledOn)
200                         {
201                             BMCWEB_LOG_DEBUG
202                                 << "Get enclosure identity led failed";
203                             messages::internalError(aResp->res);
204                             return;
205                         }
206 
207                         if (*ledOn)
208                         {
209                             aResp->res.jsonValue["LocationIndicatorActive"] =
210                                 true;
211                         }
212                         else
213                         {
214                             aResp->res.jsonValue["LocationIndicatorActive"] =
215                                 false;
216                         }
217                     }
218                     return;
219                 },
220                 "xyz.openbmc_project.LED.GroupManager",
221                 "/xyz/openbmc_project/led/groups/enclosure_identify",
222                 "org.freedesktop.DBus.Properties", "Get",
223                 "xyz.openbmc_project.Led.Group", "Asserted");
224         },
225         "xyz.openbmc_project.LED.GroupManager",
226         "/xyz/openbmc_project/led/groups/enclosure_identify_blink",
227         "org.freedesktop.DBus.Properties", "Get",
228         "xyz.openbmc_project.Led.Group", "Asserted");
229 }
230 
231 /**
232  * @brief Sets identify led group properties
233  *
234  * @param[in] aResp     Shared pointer for generating response message.
235  * @param[in] ledState  LED state passed from request
236  *
237  * @return None.
238  */
239 inline void
240     setLocationIndicatorActive(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
241                                const bool ledState)
242 {
243     BMCWEB_LOG_DEBUG << "Set LocationIndicatorActive";
244 
245     crow::connections::systemBus->async_method_call(
246         [aResp, ledState](const boost::system::error_code ec) mutable {
247             if (ec)
248             {
249                 // Some systems may not have enclosure_identify_blink object so
250                 // lets set enclosure_identify state also if
251                 // enclosure_identify_blink failed
252                 crow::connections::systemBus->async_method_call(
253                     [aResp](const boost::system::error_code ec2) {
254                         if (ec2)
255                         {
256                             BMCWEB_LOG_DEBUG << "DBUS response error " << ec2;
257                             messages::internalError(aResp->res);
258                             return;
259                         }
260                     },
261                     "xyz.openbmc_project.LED.GroupManager",
262                     "/xyz/openbmc_project/led/groups/enclosure_identify",
263                     "org.freedesktop.DBus.Properties", "Set",
264                     "xyz.openbmc_project.Led.Group", "Asserted",
265                     std::variant<bool>(ledState));
266             }
267         },
268         "xyz.openbmc_project.LED.GroupManager",
269         "/xyz/openbmc_project/led/groups/enclosure_identify_blink",
270         "org.freedesktop.DBus.Properties", "Set",
271         "xyz.openbmc_project.Led.Group", "Asserted",
272         std::variant<bool>(ledState));
273 }
274 } // namespace redfish
275