xref: /openbmc/bmcweb/features/redfish/lib/led.hpp (revision 1e1e598df6d1d9530dde6e92d8f74f8143f60e50)
11c8fba97SJames Feist /*
21c8fba97SJames Feist // Copyright (c) 2019 Intel Corporation
31c8fba97SJames Feist //
41c8fba97SJames Feist // Licensed under the Apache License, Version 2.0 (the "License");
51c8fba97SJames Feist // you may not use this file except in compliance with the License.
61c8fba97SJames Feist // You may obtain a copy of the License at
71c8fba97SJames Feist //
81c8fba97SJames Feist //      http://www.apache.org/licenses/LICENSE-2.0
91c8fba97SJames Feist //
101c8fba97SJames Feist // Unless required by applicable law or agreed to in writing, software
111c8fba97SJames Feist // distributed under the License is distributed on an "AS IS" BASIS,
121c8fba97SJames Feist // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131c8fba97SJames Feist // See the License for the specific language governing permissions and
141c8fba97SJames Feist // limitations under the License.
151c8fba97SJames Feist */
161c8fba97SJames Feist #pragma once
171c8fba97SJames Feist 
181c8fba97SJames Feist #include "async_resp.hpp"
191c8fba97SJames Feist #include "dbus_utility.hpp"
201c8fba97SJames Feist #include "redfish_util.hpp"
211c8fba97SJames Feist 
227e860f15SJohn Edward Broadbent #include <app.hpp>
23*1e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp>
247e860f15SJohn Edward Broadbent 
251c8fba97SJames Feist namespace redfish
261c8fba97SJames Feist {
271c8fba97SJames Feist /**
281c8fba97SJames Feist  * @brief Retrieves identify led group properties over dbus
291c8fba97SJames Feist  *
301c8fba97SJames Feist  * @param[in] aResp     Shared pointer for generating response message.
311c8fba97SJames Feist  *
321c8fba97SJames Feist  * @return None.
331c8fba97SJames Feist  */
349f8bfa7cSGunnar Mills // TODO (Gunnar): Remove IndicatorLED after enough time has passed
358d1b46d7Szhanghch05 inline void
368d1b46d7Szhanghch05     getIndicatorLedState(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
371c8fba97SJames Feist {
381c8fba97SJames Feist     BMCWEB_LOG_DEBUG << "Get led groups";
39*1e1e598dSJonathan Doman     sdbusplus::asio::getProperty<bool>(
40*1e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.LED.GroupManager",
41*1e1e598dSJonathan Doman         "/xyz/openbmc_project/led/groups/enclosure_identify_blink",
42*1e1e598dSJonathan Doman         "xyz.openbmc_project.Led.Group", "Asserted",
43*1e1e598dSJonathan Doman         [aResp](const boost::system::error_code ec, const bool blinking) {
441c8fba97SJames Feist             // Some systems may not have enclosure_identify_blink object so
451c8fba97SJames Feist             // proceed to get enclosure_identify state.
46*1e1e598dSJonathan Doman             if (ec == boost::system::errc::invalid_argument)
471c8fba97SJames Feist             {
481c8fba97SJames Feist                 BMCWEB_LOG_DEBUG
49*1e1e598dSJonathan Doman                     << "Get identity blinking LED failed, missmatch in property type";
501c8fba97SJames Feist                 messages::internalError(aResp->res);
511c8fba97SJames Feist                 return;
521c8fba97SJames Feist             }
531c8fba97SJames Feist 
54*1e1e598dSJonathan Doman             // Blinking ON, no need to check enclosure_identify assert.
55*1e1e598dSJonathan Doman             if (!ec && blinking)
56*1e1e598dSJonathan Doman             {
57*1e1e598dSJonathan Doman                 aResp->res.jsonValue["IndicatorLED"] = "Blinking";
58*1e1e598dSJonathan Doman                 return;
59*1e1e598dSJonathan Doman             }
60*1e1e598dSJonathan Doman 
61*1e1e598dSJonathan Doman             sdbusplus::asio::getProperty<bool>(
62*1e1e598dSJonathan Doman                 *crow::connections::systemBus,
63*1e1e598dSJonathan Doman                 "xyz.openbmc_project.LED.GroupManager",
64*1e1e598dSJonathan Doman                 "/xyz/openbmc_project/led/groups/enclosure_identify",
65*1e1e598dSJonathan Doman                 "xyz.openbmc_project.Led.Group", "Asserted",
66*1e1e598dSJonathan Doman                 [aResp](const boost::system::error_code ec2, const bool ledOn) {
67*1e1e598dSJonathan Doman                     if (ec2 == boost::system::errc::invalid_argument)
68*1e1e598dSJonathan Doman                     {
69*1e1e598dSJonathan Doman                         BMCWEB_LOG_DEBUG
70*1e1e598dSJonathan Doman                             << "Get enclosure identity led failed, missmatch in property type";
71*1e1e598dSJonathan Doman                         messages::internalError(aResp->res);
72*1e1e598dSJonathan Doman                         return;
73*1e1e598dSJonathan Doman                     }
74*1e1e598dSJonathan Doman 
75*1e1e598dSJonathan Doman                     if (ec2)
76*1e1e598dSJonathan Doman                     {
77*1e1e598dSJonathan Doman                         return;
78*1e1e598dSJonathan Doman                     }
79*1e1e598dSJonathan Doman 
80*1e1e598dSJonathan Doman                     if (ledOn)
811c8fba97SJames Feist                     {
821c8fba97SJames Feist                         aResp->res.jsonValue["IndicatorLED"] = "Lit";
831c8fba97SJames Feist                     }
841c8fba97SJames Feist                     else
851c8fba97SJames Feist                     {
861c8fba97SJames Feist                         aResp->res.jsonValue["IndicatorLED"] = "Off";
871c8fba97SJames Feist                     }
88*1e1e598dSJonathan Doman                 });
89*1e1e598dSJonathan Doman         });
901c8fba97SJames Feist }
911c8fba97SJames Feist 
921c8fba97SJames Feist /**
931c8fba97SJames Feist  * @brief Sets identify led group properties
941c8fba97SJames Feist  *
951c8fba97SJames Feist  * @param[in] aResp     Shared pointer for generating response message.
961c8fba97SJames Feist  * @param[in] ledState  LED state passed from request
971c8fba97SJames Feist  *
981c8fba97SJames Feist  * @return None.
991c8fba97SJames Feist  */
1009f8bfa7cSGunnar Mills // TODO (Gunnar): Remove IndicatorLED after enough time has passed
1018d1b46d7Szhanghch05 inline void
1028d1b46d7Szhanghch05     setIndicatorLedState(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
1031c8fba97SJames Feist                          const std::string& ledState)
1041c8fba97SJames Feist {
1051c8fba97SJames Feist     BMCWEB_LOG_DEBUG << "Set led groups";
1061c8fba97SJames Feist     bool ledOn = false;
1071c8fba97SJames Feist     bool ledBlinkng = false;
1081c8fba97SJames Feist 
1091c8fba97SJames Feist     if (ledState == "Lit")
1101c8fba97SJames Feist     {
1111c8fba97SJames Feist         ledOn = true;
1121c8fba97SJames Feist     }
1131c8fba97SJames Feist     else if (ledState == "Blinking")
1141c8fba97SJames Feist     {
1151c8fba97SJames Feist         ledBlinkng = true;
1161c8fba97SJames Feist     }
1171c8fba97SJames Feist     else if (ledState != "Off")
1181c8fba97SJames Feist     {
1191c8fba97SJames Feist         messages::propertyValueNotInList(aResp->res, ledState, "IndicatorLED");
1201c8fba97SJames Feist         return;
1211c8fba97SJames Feist     }
1221c8fba97SJames Feist 
1231c8fba97SJames Feist     crow::connections::systemBus->async_method_call(
124cb13a392SEd Tanous         [aResp, ledOn, ledBlinkng](const boost::system::error_code ec) mutable {
1251c8fba97SJames Feist             if (ec)
1261c8fba97SJames Feist             {
1271c8fba97SJames Feist                 // Some systems may not have enclosure_identify_blink object so
1281c8fba97SJames Feist                 // Lets set enclosure_identify state to true if Blinking is
1291c8fba97SJames Feist                 // true.
1301c8fba97SJames Feist                 if (ledBlinkng)
1311c8fba97SJames Feist                 {
1321c8fba97SJames Feist                     ledOn = true;
1331c8fba97SJames Feist                 }
1341c8fba97SJames Feist             }
1351c8fba97SJames Feist             crow::connections::systemBus->async_method_call(
136cb13a392SEd Tanous                 [aResp](const boost::system::error_code ec2) {
13723a21a1cSEd Tanous                     if (ec2)
1381c8fba97SJames Feist                     {
13923a21a1cSEd Tanous                         BMCWEB_LOG_DEBUG << "DBUS response error " << ec2;
1401c8fba97SJames Feist                         messages::internalError(aResp->res);
1411c8fba97SJames Feist                         return;
1421c8fba97SJames Feist                     }
1431d40ef69SJayaprakash Mutyala                     messages::success(aResp->res);
1441c8fba97SJames Feist                 },
1451c8fba97SJames Feist                 "xyz.openbmc_project.LED.GroupManager",
1461c8fba97SJames Feist                 "/xyz/openbmc_project/led/groups/enclosure_identify",
1471c8fba97SJames Feist                 "org.freedesktop.DBus.Properties", "Set",
1481c8fba97SJames Feist                 "xyz.openbmc_project.Led.Group", "Asserted",
149168e20c1SEd Tanous                 dbus::utility::DbusVariantType(ledOn));
1501c8fba97SJames Feist         },
1511c8fba97SJames Feist         "xyz.openbmc_project.LED.GroupManager",
1521c8fba97SJames Feist         "/xyz/openbmc_project/led/groups/enclosure_identify_blink",
1531c8fba97SJames Feist         "org.freedesktop.DBus.Properties", "Set",
1541c8fba97SJames Feist         "xyz.openbmc_project.Led.Group", "Asserted",
155168e20c1SEd Tanous         dbus::utility::DbusVariantType(ledBlinkng));
1561c8fba97SJames Feist }
1579f8bfa7cSGunnar Mills 
1589f8bfa7cSGunnar Mills /**
1599f8bfa7cSGunnar Mills  * @brief Retrieves identify led group properties over dbus
1609f8bfa7cSGunnar Mills  *
1619f8bfa7cSGunnar Mills  * @param[in] aResp     Shared pointer for generating response message.
1629f8bfa7cSGunnar Mills  *
1639f8bfa7cSGunnar Mills  * @return None.
1649f8bfa7cSGunnar Mills  */
1658d1b46d7Szhanghch05 inline void
1668d1b46d7Szhanghch05     getLocationIndicatorActive(const std::shared_ptr<bmcweb::AsyncResp>& aResp)
1679f8bfa7cSGunnar Mills {
1689f8bfa7cSGunnar Mills     BMCWEB_LOG_DEBUG << "Get LocationIndicatorActive";
169*1e1e598dSJonathan Doman     sdbusplus::asio::getProperty<bool>(
170*1e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.LED.GroupManager",
171*1e1e598dSJonathan Doman         "/xyz/openbmc_project/led/groups/enclosure_identify_blink",
172*1e1e598dSJonathan Doman         "xyz.openbmc_project.Led.Group", "Asserted",
173*1e1e598dSJonathan Doman         [aResp](const boost::system::error_code ec, const bool blinking) {
1749f8bfa7cSGunnar Mills             // Some systems may not have enclosure_identify_blink object so
1759f8bfa7cSGunnar Mills             // proceed to get enclosure_identify state.
176*1e1e598dSJonathan Doman             if (ec == boost::system::errc::invalid_argument)
1779f8bfa7cSGunnar Mills             {
1789f8bfa7cSGunnar Mills                 BMCWEB_LOG_DEBUG
179*1e1e598dSJonathan Doman                     << "Get identity blinking LED failed, missmatch in property type";
1809f8bfa7cSGunnar Mills                 messages::internalError(aResp->res);
1819f8bfa7cSGunnar Mills                 return;
1829f8bfa7cSGunnar Mills             }
1839f8bfa7cSGunnar Mills 
184*1e1e598dSJonathan Doman             // Blinking ON, no need to check enclosure_identify assert.
185*1e1e598dSJonathan Doman             if (!ec && blinking)
1869f8bfa7cSGunnar Mills             {
187*1e1e598dSJonathan Doman                 aResp->res.jsonValue["LocationIndicatorActive"] = true;
1889f8bfa7cSGunnar Mills                 return;
189*1e1e598dSJonathan Doman             }
190*1e1e598dSJonathan Doman 
191*1e1e598dSJonathan Doman             sdbusplus::asio::getProperty<bool>(
192*1e1e598dSJonathan Doman                 *crow::connections::systemBus,
1939f8bfa7cSGunnar Mills                 "xyz.openbmc_project.LED.GroupManager",
1949f8bfa7cSGunnar Mills                 "/xyz/openbmc_project/led/groups/enclosure_identify",
195*1e1e598dSJonathan Doman                 "xyz.openbmc_project.Led.Group", "Asserted",
196*1e1e598dSJonathan Doman                 [aResp](const boost::system::error_code ec2, const bool ledOn) {
197*1e1e598dSJonathan Doman                     if (ec2 == boost::system::errc::invalid_argument)
198*1e1e598dSJonathan Doman                     {
199*1e1e598dSJonathan Doman                         BMCWEB_LOG_DEBUG
200*1e1e598dSJonathan Doman                             << "Get enclosure identity led failed, missmatch in property type";
201*1e1e598dSJonathan Doman                         messages::internalError(aResp->res);
202*1e1e598dSJonathan Doman                         return;
203*1e1e598dSJonathan Doman                     }
204*1e1e598dSJonathan Doman 
205*1e1e598dSJonathan Doman                     if (ec2)
206*1e1e598dSJonathan Doman                     {
207*1e1e598dSJonathan Doman                         return;
208*1e1e598dSJonathan Doman                     }
209*1e1e598dSJonathan Doman 
210*1e1e598dSJonathan Doman                     aResp->res.jsonValue["LocationIndicatorActive"] = ledOn;
211*1e1e598dSJonathan Doman                 });
212*1e1e598dSJonathan Doman         });
2139f8bfa7cSGunnar Mills }
2149f8bfa7cSGunnar Mills 
2159f8bfa7cSGunnar Mills /**
2169f8bfa7cSGunnar Mills  * @brief Sets identify led group properties
2179f8bfa7cSGunnar Mills  *
2189f8bfa7cSGunnar Mills  * @param[in] aResp     Shared pointer for generating response message.
2199f8bfa7cSGunnar Mills  * @param[in] ledState  LED state passed from request
2209f8bfa7cSGunnar Mills  *
2219f8bfa7cSGunnar Mills  * @return None.
2229f8bfa7cSGunnar Mills  */
2238d1b46d7Szhanghch05 inline void
2248d1b46d7Szhanghch05     setLocationIndicatorActive(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
2259f8bfa7cSGunnar Mills                                const bool ledState)
2269f8bfa7cSGunnar Mills {
2279f8bfa7cSGunnar Mills     BMCWEB_LOG_DEBUG << "Set LocationIndicatorActive";
2289f8bfa7cSGunnar Mills 
2299f8bfa7cSGunnar Mills     crow::connections::systemBus->async_method_call(
2309f8bfa7cSGunnar Mills         [aResp, ledState](const boost::system::error_code ec) mutable {
2319f8bfa7cSGunnar Mills             if (ec)
2329f8bfa7cSGunnar Mills             {
2339f8bfa7cSGunnar Mills                 // Some systems may not have enclosure_identify_blink object so
2349f8bfa7cSGunnar Mills                 // lets set enclosure_identify state also if
2359f8bfa7cSGunnar Mills                 // enclosure_identify_blink failed
2369f8bfa7cSGunnar Mills                 crow::connections::systemBus->async_method_call(
2379f8bfa7cSGunnar Mills                     [aResp](const boost::system::error_code ec2) {
2389f8bfa7cSGunnar Mills                         if (ec2)
2399f8bfa7cSGunnar Mills                         {
2409f8bfa7cSGunnar Mills                             BMCWEB_LOG_DEBUG << "DBUS response error " << ec2;
2419f8bfa7cSGunnar Mills                             messages::internalError(aResp->res);
2429f8bfa7cSGunnar Mills                             return;
2439f8bfa7cSGunnar Mills                         }
2449f8bfa7cSGunnar Mills                     },
2459f8bfa7cSGunnar Mills                     "xyz.openbmc_project.LED.GroupManager",
2469f8bfa7cSGunnar Mills                     "/xyz/openbmc_project/led/groups/enclosure_identify",
2479f8bfa7cSGunnar Mills                     "org.freedesktop.DBus.Properties", "Set",
2489f8bfa7cSGunnar Mills                     "xyz.openbmc_project.Led.Group", "Asserted",
249168e20c1SEd Tanous                     dbus::utility::DbusVariantType(ledState));
2509f8bfa7cSGunnar Mills             }
2519f8bfa7cSGunnar Mills         },
2529f8bfa7cSGunnar Mills         "xyz.openbmc_project.LED.GroupManager",
2539f8bfa7cSGunnar Mills         "/xyz/openbmc_project/led/groups/enclosure_identify_blink",
2549f8bfa7cSGunnar Mills         "org.freedesktop.DBus.Properties", "Set",
2559f8bfa7cSGunnar Mills         "xyz.openbmc_project.Led.Group", "Asserted",
256168e20c1SEd Tanous         dbus::utility::DbusVariantType(ledState));
2579f8bfa7cSGunnar Mills }
2581c8fba97SJames Feist } // namespace redfish
259