xref: /openbmc/bmcweb/redfish-core/lib/led.hpp (revision c8ccb774)
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 inline void getIndicatorLedState(std::shared_ptr<AsyncResp> aResp)
34 {
35     BMCWEB_LOG_DEBUG << "Get led groups";
36     crow::connections::systemBus->async_method_call(
37         [aResp](const boost::system::error_code ec,
38                 const std::variant<bool> asserted) {
39             // Some systems may not have enclosure_identify_blink object so
40             // proceed to get enclosure_identify state.
41             if (!ec)
42             {
43                 const bool* blinking = std::get_if<bool>(&asserted);
44                 if (!blinking)
45                 {
46                     BMCWEB_LOG_DEBUG << "Get identity blinking LED failed";
47                     messages::internalError(aResp->res);
48                     return;
49                 }
50                 // Blinking ON, no need to check enclosure_identify assert.
51                 if (*blinking)
52                 {
53                     aResp->res.jsonValue["IndicatorLED"] = "Blinking";
54                     return;
55                 }
56             }
57             crow::connections::systemBus->async_method_call(
58                 [aResp](const boost::system::error_code ec2,
59                         const std::variant<bool> asserted2) {
60                     if (!ec2)
61                     {
62                         const bool* ledOn = std::get_if<bool>(&asserted2);
63                         if (!ledOn)
64                         {
65                             BMCWEB_LOG_DEBUG
66                                 << "Get enclosure identity led failed";
67                             messages::internalError(aResp->res);
68                             return;
69                         }
70 
71                         if (*ledOn)
72                         {
73                             aResp->res.jsonValue["IndicatorLED"] = "Lit";
74                         }
75                         else
76                         {
77                             aResp->res.jsonValue["IndicatorLED"] = "Off";
78                         }
79                     }
80                     return;
81                 },
82                 "xyz.openbmc_project.LED.GroupManager",
83                 "/xyz/openbmc_project/led/groups/enclosure_identify",
84                 "org.freedesktop.DBus.Properties", "Get",
85                 "xyz.openbmc_project.Led.Group", "Asserted");
86         },
87         "xyz.openbmc_project.LED.GroupManager",
88         "/xyz/openbmc_project/led/groups/enclosure_identify_blink",
89         "org.freedesktop.DBus.Properties", "Get",
90         "xyz.openbmc_project.Led.Group", "Asserted");
91 }
92 
93 /**
94  * @brief Sets identify led group properties
95  *
96  * @param[in] aResp     Shared pointer for generating response message.
97  * @param[in] ledState  LED state passed from request
98  *
99  * @return None.
100  */
101 inline void setIndicatorLedState(std::shared_ptr<AsyncResp> aResp,
102                                  const std::string& ledState)
103 {
104     BMCWEB_LOG_DEBUG << "Set led groups";
105     bool ledOn = false;
106     bool ledBlinkng = false;
107 
108     if (ledState == "Lit")
109     {
110         ledOn = true;
111     }
112     else if (ledState == "Blinking")
113     {
114         ledBlinkng = true;
115     }
116     else if (ledState != "Off")
117     {
118         messages::propertyValueNotInList(aResp->res, ledState, "IndicatorLED");
119         return;
120     }
121 
122     crow::connections::systemBus->async_method_call(
123         [aResp, ledOn, ledBlinkng](const boost::system::error_code ec) mutable {
124             if (ec)
125             {
126                 // Some systems may not have enclosure_identify_blink object so
127                 // Lets set enclosure_identify state to true if Blinking is
128                 // true.
129                 if (ledBlinkng)
130                 {
131                     ledOn = true;
132                 }
133             }
134             crow::connections::systemBus->async_method_call(
135                 [aResp](const boost::system::error_code ec2) {
136                     if (ec2)
137                     {
138                         BMCWEB_LOG_DEBUG << "DBUS response error " << ec2;
139                         messages::internalError(aResp->res);
140                         return;
141                     }
142                 },
143                 "xyz.openbmc_project.LED.GroupManager",
144                 "/xyz/openbmc_project/led/groups/enclosure_identify",
145                 "org.freedesktop.DBus.Properties", "Set",
146                 "xyz.openbmc_project.Led.Group", "Asserted",
147                 std::variant<bool>(ledOn));
148         },
149         "xyz.openbmc_project.LED.GroupManager",
150         "/xyz/openbmc_project/led/groups/enclosure_identify_blink",
151         "org.freedesktop.DBus.Properties", "Set",
152         "xyz.openbmc_project.Led.Group", "Asserted",
153         std::variant<bool>(ledBlinkng));
154 }
155 } // namespace redfish
156