xref: /openbmc/bmcweb/redfish-core/src/utils/dbus_utils.cpp (revision 40e9b92ec19acffb46f83a6e55b18974da5d708e)
1*40e9b92eSEd Tanous // SPDX-License-Identifier: Apache-2.0
2*40e9b92eSEd Tanous // SPDX-FileCopyrightText: Copyright OpenBMC Authors
3d02aad39SEd Tanous #include "utils/dbus_utils.hpp"
4d02aad39SEd Tanous 
5d02aad39SEd Tanous #include "async_resp.hpp"
695c6307aSEd Tanous #include "boost_formatters.hpp"
75b90429aSEd Tanous #include "error_messages.hpp"
85b90429aSEd Tanous #include "logging.hpp"
9d02aad39SEd Tanous 
105b90429aSEd Tanous #include <systemd/sd-bus.h>
115b90429aSEd Tanous 
125b90429aSEd Tanous #include <boost/asio/error.hpp>
135b90429aSEd Tanous #include <boost/beast/http/status.hpp>
14d02aad39SEd Tanous #include <boost/system/error_code.hpp>
155900d4c3SPatrick Williams #include <nlohmann/json.hpp>
16d02aad39SEd Tanous #include <sdbusplus/message.hpp>
17d02aad39SEd Tanous 
18d02aad39SEd Tanous #include <memory>
19d02aad39SEd Tanous #include <string>
20d02aad39SEd Tanous #include <string_view>
21d02aad39SEd Tanous 
22d02aad39SEd Tanous namespace redfish
23d02aad39SEd Tanous {
24d02aad39SEd Tanous namespace details
25d02aad39SEd Tanous {
26d02aad39SEd Tanous 
afterSetProperty(const std::shared_ptr<bmcweb::AsyncResp> & asyncResp,const std::string & redfishPropertyName,const nlohmann::json & propertyValue,const boost::system::error_code & ec,const sdbusplus::message_t & msg)27bd79bce8SPatrick Williams void afterSetProperty(
28bd79bce8SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
29bd79bce8SPatrick Williams     const std::string& redfishPropertyName, const nlohmann::json& propertyValue,
30bd79bce8SPatrick Williams     const boost::system::error_code& ec, const sdbusplus::message_t& msg)
31d02aad39SEd Tanous {
32d02aad39SEd Tanous     if (ec)
33d02aad39SEd Tanous     {
34d02aad39SEd Tanous         if (ec.value() == boost::system::errc::permission_denied)
35d02aad39SEd Tanous         {
36d02aad39SEd Tanous             messages::insufficientPrivilege(asyncResp->res);
37d02aad39SEd Tanous         }
3887c44966SAsmitha Karunanithi         if (ec.value() == boost::asio::error::host_unreachable)
3987c44966SAsmitha Karunanithi         {
4087c44966SAsmitha Karunanithi             messages::resourceNotFound(asyncResp->res, "Set",
4187c44966SAsmitha Karunanithi                                        redfishPropertyName);
4287c44966SAsmitha Karunanithi             return;
4387c44966SAsmitha Karunanithi         }
44d02aad39SEd Tanous         const sd_bus_error* dbusError = msg.get_error();
45d02aad39SEd Tanous         if (dbusError != nullptr)
46d02aad39SEd Tanous         {
47d02aad39SEd Tanous             std::string_view errorName(dbusError->name);
48d02aad39SEd Tanous 
49d02aad39SEd Tanous             if (errorName == "xyz.openbmc_project.Common.Error.InvalidArgument")
50d02aad39SEd Tanous             {
51d02aad39SEd Tanous                 BMCWEB_LOG_WARNING("DBUS response error: {}", ec);
52d02aad39SEd Tanous                 messages::propertyValueIncorrect(
53d02aad39SEd Tanous                     asyncResp->res, redfishPropertyName, propertyValue);
54d02aad39SEd Tanous                 return;
55d02aad39SEd Tanous             }
56d02aad39SEd Tanous             if (errorName ==
57d02aad39SEd Tanous                 "xyz.openbmc_project.State.Chassis.Error.BMCNotReady")
58d02aad39SEd Tanous             {
59d02aad39SEd Tanous                 BMCWEB_LOG_WARNING(
60d02aad39SEd Tanous                     "BMC not ready, operation not allowed right now");
61d02aad39SEd Tanous                 messages::serviceTemporarilyUnavailable(asyncResp->res, "10");
62d02aad39SEd Tanous                 return;
63d02aad39SEd Tanous             }
64d02aad39SEd Tanous             if (errorName == "xyz.openbmc_project.State.Host.Error.BMCNotReady")
65d02aad39SEd Tanous             {
66d02aad39SEd Tanous                 BMCWEB_LOG_WARNING(
67d02aad39SEd Tanous                     "BMC not ready, operation not allowed right now");
68d02aad39SEd Tanous                 messages::serviceTemporarilyUnavailable(asyncResp->res, "10");
69d02aad39SEd Tanous                 return;
70d02aad39SEd Tanous             }
71d02aad39SEd Tanous             if (errorName == "xyz.openbmc_project.Common.Error.NotAllowed")
72d02aad39SEd Tanous             {
73d02aad39SEd Tanous                 messages::propertyNotWritable(asyncResp->res,
74d02aad39SEd Tanous                                               redfishPropertyName);
75d02aad39SEd Tanous                 return;
76d02aad39SEd Tanous             }
7787c44966SAsmitha Karunanithi             if (errorName == "xyz.openbmc_project.Common.Error.Unavailable")
7887c44966SAsmitha Karunanithi             {
7987c44966SAsmitha Karunanithi                 messages::resourceInStandby(asyncResp->res);
8087c44966SAsmitha Karunanithi                 return;
8187c44966SAsmitha Karunanithi             }
82d02aad39SEd Tanous         }
83d02aad39SEd Tanous         BMCWEB_LOG_ERROR("D-Bus error setting Redfish Property {} ec={}",
84d02aad39SEd Tanous                          redfishPropertyName, ec);
85d02aad39SEd Tanous         messages::internalError(asyncResp->res);
86d02aad39SEd Tanous         return;
87d02aad39SEd Tanous     }
88247ae89bSEd Tanous     // Only set 204 if another error hasn't already happened.
891827b4f1SAsmitha Karunanithi     if (asyncResp->res.result() == boost::beast::http::status::ok)
901827b4f1SAsmitha Karunanithi     {
91247ae89bSEd Tanous         asyncResp->res.result(boost::beast::http::status::no_content);
921827b4f1SAsmitha Karunanithi     }
931827b4f1SAsmitha Karunanithi };
941827b4f1SAsmitha Karunanithi 
afterSetPropertyAction(const std::shared_ptr<bmcweb::AsyncResp> & asyncResp,const std::string & redfishActionName,const std::string & redfishActionParameterName,const boost::system::error_code & ec,const sdbusplus::message_t &)951827b4f1SAsmitha Karunanithi void afterSetPropertyAction(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
961827b4f1SAsmitha Karunanithi                             const std::string& redfishActionName,
971827b4f1SAsmitha Karunanithi                             const std::string& redfishActionParameterName,
981827b4f1SAsmitha Karunanithi                             const boost::system::error_code& ec,
991827b4f1SAsmitha Karunanithi                             const sdbusplus::message_t& /*msg*/)
1001827b4f1SAsmitha Karunanithi {
1011827b4f1SAsmitha Karunanithi     if (ec)
1021827b4f1SAsmitha Karunanithi     {
1031827b4f1SAsmitha Karunanithi         if (ec.value() == boost::asio::error::invalid_argument)
1041827b4f1SAsmitha Karunanithi         {
1051827b4f1SAsmitha Karunanithi             BMCWEB_LOG_WARNING(
1061827b4f1SAsmitha Karunanithi                 "Resource {} is patched with invalid argument during action {}",
1071827b4f1SAsmitha Karunanithi                 redfishActionParameterName, redfishActionName);
1081827b4f1SAsmitha Karunanithi             if (redfishActionParameterName.empty())
1091827b4f1SAsmitha Karunanithi             {
1101827b4f1SAsmitha Karunanithi                 messages::operationFailed(asyncResp->res);
1111827b4f1SAsmitha Karunanithi             }
1121827b4f1SAsmitha Karunanithi             else
1131827b4f1SAsmitha Karunanithi             {
1141827b4f1SAsmitha Karunanithi                 messages::actionParameterValueError(asyncResp->res,
1151827b4f1SAsmitha Karunanithi                                                     redfishActionParameterName,
1161827b4f1SAsmitha Karunanithi                                                     redfishActionName);
1171827b4f1SAsmitha Karunanithi             }
1181827b4f1SAsmitha Karunanithi             return;
1191827b4f1SAsmitha Karunanithi         }
1201827b4f1SAsmitha Karunanithi         if (ec.value() == boost::asio::error::host_unreachable)
1211827b4f1SAsmitha Karunanithi         {
1221827b4f1SAsmitha Karunanithi             BMCWEB_LOG_WARNING(
1231827b4f1SAsmitha Karunanithi                 "Resource {} is not found while performing action {}",
1241827b4f1SAsmitha Karunanithi                 redfishActionParameterName, redfishActionName);
1251827b4f1SAsmitha Karunanithi             messages::resourceNotFound(asyncResp->res, "Actions",
1261827b4f1SAsmitha Karunanithi                                        redfishActionName);
1271827b4f1SAsmitha Karunanithi             return;
1281827b4f1SAsmitha Karunanithi         }
1291827b4f1SAsmitha Karunanithi 
1301827b4f1SAsmitha Karunanithi         BMCWEB_LOG_ERROR("D-Bus error setting Redfish Property {} ec={}",
1311827b4f1SAsmitha Karunanithi                          redfishActionParameterName, ec);
1321827b4f1SAsmitha Karunanithi         messages::internalError(asyncResp->res);
1331827b4f1SAsmitha Karunanithi         return;
1341827b4f1SAsmitha Karunanithi     }
135cdf25ffbSEd Tanous     // Only set success if another error hasn't already happened.
136d02aad39SEd Tanous     if (asyncResp->res.result() == boost::beast::http::status::ok)
137d02aad39SEd Tanous     {
138cdf25ffbSEd Tanous         messages::success(asyncResp->res);
139d02aad39SEd Tanous     }
140d02aad39SEd Tanous };
141d02aad39SEd Tanous } // namespace details
142d02aad39SEd Tanous } // namespace redfish
143