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