xref: /openbmc/bmcweb/redfish-core/lib/led.hpp (revision d8ef9915)
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                     messages::success(aResp->res);
145                 },
146                 "xyz.openbmc_project.LED.GroupManager",
147                 "/xyz/openbmc_project/led/groups/enclosure_identify",
148                 "org.freedesktop.DBus.Properties", "Set",
149                 "xyz.openbmc_project.Led.Group", "Asserted",
150                 std::variant<bool>(ledOn));
151         },
152         "xyz.openbmc_project.LED.GroupManager",
153         "/xyz/openbmc_project/led/groups/enclosure_identify_blink",
154         "org.freedesktop.DBus.Properties", "Set",
155         "xyz.openbmc_project.Led.Group", "Asserted",
156         std::variant<bool>(ledBlinkng));
157 }
158 
159 /**
160  * @brief Retrieves identify led group properties over dbus
161  *
162  * @param[in] aResp     Shared pointer for generating response message.
163  *
164  * @return None.
165  */
166 inline void getLocationIndicatorActive(const std::shared_ptr<AsyncResp>& aResp)
167 {
168     BMCWEB_LOG_DEBUG << "Get LocationIndicatorActive";
169     crow::connections::systemBus->async_method_call(
170         [aResp](const boost::system::error_code ec,
171                 const std::variant<bool> asserted) {
172             // Some systems may not have enclosure_identify_blink object so
173             // proceed to get enclosure_identify state.
174             if (!ec)
175             {
176                 const bool* blinking = std::get_if<bool>(&asserted);
177                 if (!blinking)
178                 {
179                     BMCWEB_LOG_DEBUG << "Get identity blinking LED failed";
180                     messages::internalError(aResp->res);
181                     return;
182                 }
183                 // Blinking ON, no need to check enclosure_identify assert.
184                 if (*blinking)
185                 {
186                     aResp->res.jsonValue["LocationIndicatorActive"] = true;
187                     return;
188                 }
189             }
190             crow::connections::systemBus->async_method_call(
191                 [aResp](const boost::system::error_code ec2,
192                         const std::variant<bool> asserted2) {
193                     if (!ec2)
194                     {
195                         const bool* ledOn = std::get_if<bool>(&asserted2);
196                         if (!ledOn)
197                         {
198                             BMCWEB_LOG_DEBUG
199                                 << "Get enclosure identity led failed";
200                             messages::internalError(aResp->res);
201                             return;
202                         }
203 
204                         if (*ledOn)
205                         {
206                             aResp->res.jsonValue["LocationIndicatorActive"] =
207                                 true;
208                         }
209                         else
210                         {
211                             aResp->res.jsonValue["LocationIndicatorActive"] =
212                                 false;
213                         }
214                     }
215                     return;
216                 },
217                 "xyz.openbmc_project.LED.GroupManager",
218                 "/xyz/openbmc_project/led/groups/enclosure_identify",
219                 "org.freedesktop.DBus.Properties", "Get",
220                 "xyz.openbmc_project.Led.Group", "Asserted");
221         },
222         "xyz.openbmc_project.LED.GroupManager",
223         "/xyz/openbmc_project/led/groups/enclosure_identify_blink",
224         "org.freedesktop.DBus.Properties", "Get",
225         "xyz.openbmc_project.Led.Group", "Asserted");
226 }
227 
228 /**
229  * @brief Sets identify led group properties
230  *
231  * @param[in] aResp     Shared pointer for generating response message.
232  * @param[in] ledState  LED state passed from request
233  *
234  * @return None.
235  */
236 inline void setLocationIndicatorActive(const std::shared_ptr<AsyncResp>& aResp,
237                                        const bool ledState)
238 {
239     BMCWEB_LOG_DEBUG << "Set LocationIndicatorActive";
240 
241     crow::connections::systemBus->async_method_call(
242         [aResp, ledState](const boost::system::error_code ec) mutable {
243             if (ec)
244             {
245                 // Some systems may not have enclosure_identify_blink object so
246                 // lets set enclosure_identify state also if
247                 // enclosure_identify_blink failed
248                 crow::connections::systemBus->async_method_call(
249                     [aResp](const boost::system::error_code ec2) {
250                         if (ec2)
251                         {
252                             BMCWEB_LOG_DEBUG << "DBUS response error " << ec2;
253                             messages::internalError(aResp->res);
254                             return;
255                         }
256                     },
257                     "xyz.openbmc_project.LED.GroupManager",
258                     "/xyz/openbmc_project/led/groups/enclosure_identify",
259                     "org.freedesktop.DBus.Properties", "Set",
260                     "xyz.openbmc_project.Led.Group", "Asserted",
261                     std::variant<bool>(ledState));
262             }
263         },
264         "xyz.openbmc_project.LED.GroupManager",
265         "/xyz/openbmc_project/led/groups/enclosure_identify_blink",
266         "org.freedesktop.DBus.Properties", "Set",
267         "xyz.openbmc_project.Led.Group", "Asserted",
268         std::variant<bool>(ledState));
269 }
270 } // namespace redfish
271