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