xref: /openbmc/bmcweb/redfish-core/lib/led.hpp (revision d78572018fc2022091ff8b8eb5a7fef2172ba3d6)
1 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: Copyright OpenBMC Authors
3 // SPDX-FileCopyrightText: Copyright 2019 Intel Corporation
4 #pragma once
5 
6 #include "async_resp.hpp"
7 #include "dbus_singleton.hpp"
8 #include "dbus_utility.hpp"
9 #include "error_messages.hpp"
10 #include "generated/enums/chassis.hpp"
11 #include "logging.hpp"
12 #include "utils/dbus_utils.hpp"
13 
14 #include <sdbusplus/asio/property.hpp>
15 #include <sdbusplus/message/native_types.hpp>
16 
17 #include <memory>
18 
19 namespace redfish
20 {
21 /**
22  * @brief Retrieves identify led group properties over dbus
23  *
24  * @param[in] asyncResp     Shared pointer for generating response message.
25  *
26  * @return None.
27  */
28 // TODO (Gunnar): Remove IndicatorLED after enough time has passed
29 inline void
getIndicatorLedState(const std::shared_ptr<bmcweb::AsyncResp> & asyncResp)30     getIndicatorLedState(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
31 {
32     BMCWEB_LOG_DEBUG("Get led groups");
33     dbus::utility::getProperty<bool>(
34         "xyz.openbmc_project.LED.GroupManager",
35         "/xyz/openbmc_project/led/groups/enclosure_identify_blink",
36         "xyz.openbmc_project.Led.Group", "Asserted",
37         [asyncResp](const boost::system::error_code& ec, const bool blinking) {
38             // Some systems may not have enclosure_identify_blink object so
39             // proceed to get enclosure_identify state.
40             if (ec == boost::system::errc::invalid_argument)
41             {
42                 BMCWEB_LOG_DEBUG(
43                     "Get identity blinking LED failed, mismatch in property type");
44                 messages::internalError(asyncResp->res);
45                 return;
46             }
47 
48             // Blinking ON, no need to check enclosure_identify assert.
49             if (!ec && blinking)
50             {
51                 asyncResp->res.jsonValue["IndicatorLED"] =
52                     chassis::IndicatorLED::Blinking;
53                 return;
54             }
55 
56             dbus::utility::getProperty<bool>(
57                 "xyz.openbmc_project.LED.GroupManager",
58                 "/xyz/openbmc_project/led/groups/enclosure_identify",
59                 "xyz.openbmc_project.Led.Group", "Asserted",
60                 [asyncResp](const boost::system::error_code& ec2,
61                             const bool ledOn) {
62                     if (ec2 == boost::system::errc::invalid_argument)
63                     {
64                         BMCWEB_LOG_DEBUG(
65                             "Get enclosure identity led failed, mismatch in property type");
66                         messages::internalError(asyncResp->res);
67                         return;
68                     }
69 
70                     if (ec2)
71                     {
72                         return;
73                     }
74 
75                     if (ledOn)
76                     {
77                         asyncResp->res.jsonValue["IndicatorLED"] =
78                             chassis::IndicatorLED::Lit;
79                     }
80                     else
81                     {
82                         asyncResp->res.jsonValue["IndicatorLED"] =
83                             chassis::IndicatorLED::Off;
84                     }
85                 });
86         });
87 }
88 
89 /**
90  * @brief Sets identify led group properties
91  *
92  * @param[in] asyncResp     Shared pointer for generating response message.
93  * @param[in] ledState  LED state passed from request
94  *
95  * @return None.
96  */
97 // TODO (Gunnar): Remove IndicatorLED after enough time has passed
98 inline void
setIndicatorLedState(const std::shared_ptr<bmcweb::AsyncResp> & asyncResp,const std::string & ledState)99     setIndicatorLedState(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
100                          const std::string& ledState)
101 {
102     BMCWEB_LOG_DEBUG("Set led groups");
103     bool ledOn = false;
104     bool ledBlinkng = false;
105 
106     if (ledState == "Lit")
107     {
108         ledOn = true;
109     }
110     else if (ledState == "Blinking")
111     {
112         ledBlinkng = true;
113     }
114     else if (ledState != "Off")
115     {
116         messages::propertyValueNotInList(asyncResp->res, ledState,
117                                          "IndicatorLED");
118         return;
119     }
120 
121     sdbusplus::asio::setProperty(
122         *crow::connections::systemBus, "xyz.openbmc_project.LED.GroupManager",
123         "/xyz/openbmc_project/led/groups/enclosure_identify_blink",
124         "xyz.openbmc_project.Led.Group", "Asserted", ledBlinkng,
125         [asyncResp, ledOn,
126          ledBlinkng](const boost::system::error_code& ec) mutable {
127             if (ec)
128             {
129                 // Some systems may not have enclosure_identify_blink object so
130                 // Lets set enclosure_identify state to true if Blinking is
131                 // true.
132                 if (ledBlinkng)
133                 {
134                     ledOn = true;
135                 }
136             }
137             setDbusProperty(
138                 asyncResp, "IndicatorLED",
139                 "xyz.openbmc_project.LED.GroupManager",
140                 sdbusplus::message::object_path(
141                     "/xyz/openbmc_project/led/groups/enclosure_identify"),
142                 "xyz.openbmc_project.Led.Group", "Asserted", ledBlinkng);
143         });
144 }
145 
146 /**
147  * @brief Retrieves identify system led group properties over dbus
148  *
149  * @param[in] asyncResp     Shared pointer for generating response message.
150  *
151  * @return None.
152  */
getSystemLocationIndicatorActive(const std::shared_ptr<bmcweb::AsyncResp> & asyncResp)153 inline void getSystemLocationIndicatorActive(
154     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
155 {
156     BMCWEB_LOG_DEBUG("Get LocationIndicatorActive");
157     dbus::utility::getProperty<bool>(
158         "xyz.openbmc_project.LED.GroupManager",
159         "/xyz/openbmc_project/led/groups/enclosure_identify_blink",
160         "xyz.openbmc_project.Led.Group", "Asserted",
161         [asyncResp](const boost::system::error_code& ec, const bool blinking) {
162             // Some systems may not have enclosure_identify_blink object so
163             // proceed to get enclosure_identify state.
164             if (ec == boost::system::errc::invalid_argument)
165             {
166                 BMCWEB_LOG_DEBUG(
167                     "Get identity blinking LED failed, mismatch in property type");
168                 messages::internalError(asyncResp->res);
169                 return;
170             }
171 
172             // Blinking ON, no need to check enclosure_identify assert.
173             if (!ec && blinking)
174             {
175                 asyncResp->res.jsonValue["LocationIndicatorActive"] = true;
176                 return;
177             }
178 
179             dbus::utility::getProperty<bool>(
180                 "xyz.openbmc_project.LED.GroupManager",
181                 "/xyz/openbmc_project/led/groups/enclosure_identify",
182                 "xyz.openbmc_project.Led.Group", "Asserted",
183                 [asyncResp](const boost::system::error_code& ec2,
184                             const bool ledOn) {
185                     if (ec2 == boost::system::errc::invalid_argument)
186                     {
187                         BMCWEB_LOG_DEBUG(
188                             "Get enclosure identity led failed, mismatch in property type");
189                         messages::internalError(asyncResp->res);
190                         return;
191                     }
192 
193                     if (ec2)
194                     {
195                         return;
196                     }
197 
198                     asyncResp->res.jsonValue["LocationIndicatorActive"] = ledOn;
199                 });
200         });
201 }
202 
203 /**
204  * @brief Sets identify system led group properties
205  *
206  * @param[in] asyncResp     Shared pointer for generating response message.
207  * @param[in] ledState  LED state passed from request
208  *
209  * @return None.
210  */
setSystemLocationIndicatorActive(const std::shared_ptr<bmcweb::AsyncResp> & asyncResp,const bool ledState)211 inline void setSystemLocationIndicatorActive(
212     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, const bool ledState)
213 {
214     BMCWEB_LOG_DEBUG("Set LocationIndicatorActive");
215 
216     sdbusplus::asio::setProperty(
217         *crow::connections::systemBus, "xyz.openbmc_project.LED.GroupManager",
218         "/xyz/openbmc_project/led/groups/enclosure_identify_blink",
219         "xyz.openbmc_project.Led.Group", "Asserted", ledState,
220         [asyncResp, ledState](const boost::system::error_code& ec) {
221             if (ec)
222             {
223                 // Some systems may not have enclosure_identify_blink object so
224                 // lets set enclosure_identify state also if
225                 // enclosure_identify_blink failed
226                 setDbusProperty(
227                     asyncResp, "LocationIndicatorActive",
228                     "xyz.openbmc_project.LED.GroupManager",
229                     sdbusplus::message::object_path(
230                         "/xyz/openbmc_project/led/groups/enclosure_identify"),
231                     "xyz.openbmc_project.Led.Group", "Asserted", ledState);
232             }
233         });
234 }
235 } // namespace redfish
236