xref: /openbmc/bmcweb/redfish-core/lib/led.hpp (revision cdf25ffb)
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 "app.hpp"
19 #include "async_resp.hpp"
20 #include "dbus_utility.hpp"
21 #include "generated/enums/chassis.hpp"
22 #include "redfish_util.hpp"
23 
24 #include <sdbusplus/asio/property.hpp>
25 
26 namespace redfish
27 {
28 /**
29  * @brief Retrieves identify led group properties over dbus
30  *
31  * @param[in] asyncResp     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>& asyncResp)
38 {
39     BMCWEB_LOG_DEBUG("Get led groups");
40     sdbusplus::asio::getProperty<bool>(
41         *crow::connections::systemBus, "xyz.openbmc_project.LED.GroupManager",
42         "/xyz/openbmc_project/led/groups/enclosure_identify_blink",
43         "xyz.openbmc_project.Led.Group", "Asserted",
44         [asyncResp](const boost::system::error_code& ec, const bool blinking) {
45         // Some systems may not have enclosure_identify_blink object so
46         // proceed to get enclosure_identify state.
47         if (ec == boost::system::errc::invalid_argument)
48         {
49             BMCWEB_LOG_DEBUG(
50                 "Get identity blinking LED failed, mismatch in property type");
51             messages::internalError(asyncResp->res);
52             return;
53         }
54 
55         // Blinking ON, no need to check enclosure_identify assert.
56         if (!ec && blinking)
57         {
58             asyncResp->res.jsonValue["IndicatorLED"] =
59                 chassis::IndicatorLED::Blinking;
60             return;
61         }
62 
63         sdbusplus::asio::getProperty<bool>(
64             *crow::connections::systemBus,
65             "xyz.openbmc_project.LED.GroupManager",
66             "/xyz/openbmc_project/led/groups/enclosure_identify",
67             "xyz.openbmc_project.Led.Group", "Asserted",
68             [asyncResp](const boost::system::error_code& ec2,
69                         const bool ledOn) {
70             if (ec2 == boost::system::errc::invalid_argument)
71             {
72                 BMCWEB_LOG_DEBUG(
73                     "Get enclosure identity led failed, mismatch in property type");
74                 messages::internalError(asyncResp->res);
75                 return;
76             }
77 
78             if (ec2)
79             {
80                 return;
81             }
82 
83             if (ledOn)
84             {
85                 asyncResp->res.jsonValue["IndicatorLED"] =
86                     chassis::IndicatorLED::Lit;
87             }
88             else
89             {
90                 asyncResp->res.jsonValue["IndicatorLED"] =
91                     chassis::IndicatorLED::Off;
92             }
93         });
94     });
95 }
96 
97 /**
98  * @brief Sets identify led group properties
99  *
100  * @param[in] asyncResp     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>& asyncResp,
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(asyncResp->res, ledState,
125                                          "IndicatorLED");
126         return;
127     }
128 
129     sdbusplus::asio::setProperty(
130         *crow::connections::systemBus, "xyz.openbmc_project.LED.GroupManager",
131         "/xyz/openbmc_project/led/groups/enclosure_identify_blink",
132         "xyz.openbmc_project.Led.Group", "Asserted", ledBlinkng,
133         [asyncResp, ledOn,
134          ledBlinkng](const boost::system::error_code& ec) mutable {
135         if (ec)
136         {
137             // Some systems may not have enclosure_identify_blink object so
138             // Lets set enclosure_identify state to true if Blinking is
139             // true.
140             if (ledBlinkng)
141             {
142                 ledOn = true;
143             }
144         }
145         setDbusProperty(
146             asyncResp, "IndicatorLED", "xyz.openbmc_project.LED.GroupManager",
147             sdbusplus::message::object_path(
148                 "/xyz/openbmc_project/led/groups/enclosure_identify"),
149             "xyz.openbmc_project.Led.Group", "Asserted", ledBlinkng);
150     });
151 }
152 
153 /**
154  * @brief Retrieves identify system led group properties over dbus
155  *
156  * @param[in] asyncResp     Shared pointer for generating response message.
157  *
158  * @return None.
159  */
160 inline void getSystemLocationIndicatorActive(
161     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
162 {
163     BMCWEB_LOG_DEBUG("Get LocationIndicatorActive");
164     sdbusplus::asio::getProperty<bool>(
165         *crow::connections::systemBus, "xyz.openbmc_project.LED.GroupManager",
166         "/xyz/openbmc_project/led/groups/enclosure_identify_blink",
167         "xyz.openbmc_project.Led.Group", "Asserted",
168         [asyncResp](const boost::system::error_code& ec, const bool blinking) {
169         // Some systems may not have enclosure_identify_blink object so
170         // proceed to get enclosure_identify state.
171         if (ec == boost::system::errc::invalid_argument)
172         {
173             BMCWEB_LOG_DEBUG(
174                 "Get identity blinking LED failed, mismatch in property type");
175             messages::internalError(asyncResp->res);
176             return;
177         }
178 
179         // Blinking ON, no need to check enclosure_identify assert.
180         if (!ec && blinking)
181         {
182             asyncResp->res.jsonValue["LocationIndicatorActive"] = true;
183             return;
184         }
185 
186         sdbusplus::asio::getProperty<bool>(
187             *crow::connections::systemBus,
188             "xyz.openbmc_project.LED.GroupManager",
189             "/xyz/openbmc_project/led/groups/enclosure_identify",
190             "xyz.openbmc_project.Led.Group", "Asserted",
191             [asyncResp](const boost::system::error_code& ec2,
192                         const bool ledOn) {
193             if (ec2 == boost::system::errc::invalid_argument)
194             {
195                 BMCWEB_LOG_DEBUG(
196                     "Get enclosure identity led failed, mismatch in property type");
197                 messages::internalError(asyncResp->res);
198                 return;
199             }
200 
201             if (ec2)
202             {
203                 return;
204             }
205 
206             asyncResp->res.jsonValue["LocationIndicatorActive"] = ledOn;
207         });
208     });
209 }
210 
211 /**
212  * @brief Sets identify system led group properties
213  *
214  * @param[in] asyncResp     Shared pointer for generating response message.
215  * @param[in] ledState  LED state passed from request
216  *
217  * @return None.
218  */
219 inline void setSystemLocationIndicatorActive(
220     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, const bool ledState)
221 {
222     BMCWEB_LOG_DEBUG("Set LocationIndicatorActive");
223 
224     sdbusplus::asio::setProperty(
225         *crow::connections::systemBus, "xyz.openbmc_project.LED.GroupManager",
226         "/xyz/openbmc_project/led/groups/enclosure_identify_blink",
227         "xyz.openbmc_project.Led.Group", "Asserted", ledState,
228         [asyncResp, ledState](const boost::system::error_code& ec) {
229         if (ec)
230         {
231             // Some systems may not have enclosure_identify_blink object so
232             // lets set enclosure_identify state also if
233             // enclosure_identify_blink failed
234             setDbusProperty(
235                 asyncResp, "LocationIndicatorActive",
236                 "xyz.openbmc_project.LED.GroupManager",
237                 sdbusplus::message::object_path(
238                     "/xyz/openbmc_project/led/groups/enclosure_identify"),
239                 "xyz.openbmc_project.Led.Group", "Asserted", ledState);
240         }
241     });
242 }
243 } // namespace redfish
244