xref: /openbmc/bmcweb/features/redfish/src/utils/dbus_utils.cpp (revision 247ae89bc26024d335f1317566bebeee52c75089)
1d02aad39SEd Tanous #include "utils/dbus_utils.hpp"
2d02aad39SEd Tanous 
3d02aad39SEd Tanous #include "async_resp.hpp"
495c6307aSEd Tanous #include "boost_formatters.hpp"
55b90429aSEd Tanous #include "error_messages.hpp"
65b90429aSEd Tanous #include "logging.hpp"
7d02aad39SEd Tanous 
85b90429aSEd Tanous #include <systemd/sd-bus.h>
95b90429aSEd Tanous 
105b90429aSEd Tanous #include <boost/asio/error.hpp>
115b90429aSEd Tanous #include <boost/beast/http/status.hpp>
12d02aad39SEd Tanous #include <boost/system/error_code.hpp>
13d02aad39SEd Tanous #include <sdbusplus/message.hpp>
14d02aad39SEd Tanous 
15d02aad39SEd Tanous #include <memory>
16d02aad39SEd Tanous #include <string>
17d02aad39SEd Tanous #include <string_view>
18d02aad39SEd Tanous 
19d02aad39SEd Tanous namespace redfish
20d02aad39SEd Tanous {
21d02aad39SEd Tanous namespace details
22d02aad39SEd Tanous {
23d02aad39SEd Tanous 
24bd79bce8SPatrick Williams void afterSetProperty(
25bd79bce8SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
26bd79bce8SPatrick Williams     const std::string& redfishPropertyName, const nlohmann::json& propertyValue,
27bd79bce8SPatrick Williams     const boost::system::error_code& ec, const sdbusplus::message_t& msg)
28d02aad39SEd Tanous {
29d02aad39SEd Tanous     if (ec)
30d02aad39SEd Tanous     {
31d02aad39SEd Tanous         if (ec.value() == boost::system::errc::permission_denied)
32d02aad39SEd Tanous         {
33d02aad39SEd Tanous             messages::insufficientPrivilege(asyncResp->res);
34d02aad39SEd Tanous         }
3587c44966SAsmitha Karunanithi         if (ec.value() == boost::asio::error::host_unreachable)
3687c44966SAsmitha Karunanithi         {
3787c44966SAsmitha Karunanithi             messages::resourceNotFound(asyncResp->res, "Set",
3887c44966SAsmitha Karunanithi                                        redfishPropertyName);
3987c44966SAsmitha Karunanithi             return;
4087c44966SAsmitha Karunanithi         }
41d02aad39SEd Tanous         const sd_bus_error* dbusError = msg.get_error();
42d02aad39SEd Tanous         if (dbusError != nullptr)
43d02aad39SEd Tanous         {
44d02aad39SEd Tanous             std::string_view errorName(dbusError->name);
45d02aad39SEd Tanous 
46d02aad39SEd Tanous             if (errorName == "xyz.openbmc_project.Common.Error.InvalidArgument")
47d02aad39SEd Tanous             {
48d02aad39SEd Tanous                 BMCWEB_LOG_WARNING("DBUS response error: {}", ec);
49d02aad39SEd Tanous                 messages::propertyValueIncorrect(
50d02aad39SEd Tanous                     asyncResp->res, redfishPropertyName, propertyValue);
51d02aad39SEd Tanous                 return;
52d02aad39SEd Tanous             }
53d02aad39SEd Tanous             if (errorName ==
54d02aad39SEd Tanous                 "xyz.openbmc_project.State.Chassis.Error.BMCNotReady")
55d02aad39SEd Tanous             {
56d02aad39SEd Tanous                 BMCWEB_LOG_WARNING(
57d02aad39SEd Tanous                     "BMC not ready, operation not allowed right now");
58d02aad39SEd Tanous                 messages::serviceTemporarilyUnavailable(asyncResp->res, "10");
59d02aad39SEd Tanous                 return;
60d02aad39SEd Tanous             }
61d02aad39SEd Tanous             if (errorName == "xyz.openbmc_project.State.Host.Error.BMCNotReady")
62d02aad39SEd Tanous             {
63d02aad39SEd Tanous                 BMCWEB_LOG_WARNING(
64d02aad39SEd Tanous                     "BMC not ready, operation not allowed right now");
65d02aad39SEd Tanous                 messages::serviceTemporarilyUnavailable(asyncResp->res, "10");
66d02aad39SEd Tanous                 return;
67d02aad39SEd Tanous             }
68d02aad39SEd Tanous             if (errorName == "xyz.openbmc_project.Common.Error.NotAllowed")
69d02aad39SEd Tanous             {
70d02aad39SEd Tanous                 messages::propertyNotWritable(asyncResp->res,
71d02aad39SEd Tanous                                               redfishPropertyName);
72d02aad39SEd Tanous                 return;
73d02aad39SEd Tanous             }
7487c44966SAsmitha Karunanithi             if (errorName == "xyz.openbmc_project.Common.Error.Unavailable")
7587c44966SAsmitha Karunanithi             {
7687c44966SAsmitha Karunanithi                 messages::resourceInStandby(asyncResp->res);
7787c44966SAsmitha Karunanithi                 return;
7887c44966SAsmitha Karunanithi             }
79d02aad39SEd Tanous         }
80d02aad39SEd Tanous         BMCWEB_LOG_ERROR("D-Bus error setting Redfish Property {} ec={}",
81d02aad39SEd Tanous                          redfishPropertyName, ec);
82d02aad39SEd Tanous         messages::internalError(asyncResp->res);
83d02aad39SEd Tanous         return;
84d02aad39SEd Tanous     }
85*247ae89bSEd Tanous     // Only set 204 if another error hasn't already happened.
861827b4f1SAsmitha Karunanithi     if (asyncResp->res.result() == boost::beast::http::status::ok)
871827b4f1SAsmitha Karunanithi     {
88*247ae89bSEd Tanous         asyncResp->res.result(boost::beast::http::status::no_content);
891827b4f1SAsmitha Karunanithi     }
901827b4f1SAsmitha Karunanithi };
911827b4f1SAsmitha Karunanithi 
921827b4f1SAsmitha Karunanithi void afterSetPropertyAction(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
931827b4f1SAsmitha Karunanithi                             const std::string& redfishActionName,
941827b4f1SAsmitha Karunanithi                             const std::string& redfishActionParameterName,
951827b4f1SAsmitha Karunanithi                             const boost::system::error_code& ec,
961827b4f1SAsmitha Karunanithi                             const sdbusplus::message_t& /*msg*/)
971827b4f1SAsmitha Karunanithi {
981827b4f1SAsmitha Karunanithi     if (ec)
991827b4f1SAsmitha Karunanithi     {
1001827b4f1SAsmitha Karunanithi         if (ec.value() == boost::asio::error::invalid_argument)
1011827b4f1SAsmitha Karunanithi         {
1021827b4f1SAsmitha Karunanithi             BMCWEB_LOG_WARNING(
1031827b4f1SAsmitha Karunanithi                 "Resource {} is patched with invalid argument during action {}",
1041827b4f1SAsmitha Karunanithi                 redfishActionParameterName, redfishActionName);
1051827b4f1SAsmitha Karunanithi             if (redfishActionParameterName.empty())
1061827b4f1SAsmitha Karunanithi             {
1071827b4f1SAsmitha Karunanithi                 messages::operationFailed(asyncResp->res);
1081827b4f1SAsmitha Karunanithi             }
1091827b4f1SAsmitha Karunanithi             else
1101827b4f1SAsmitha Karunanithi             {
1111827b4f1SAsmitha Karunanithi                 messages::actionParameterValueError(asyncResp->res,
1121827b4f1SAsmitha Karunanithi                                                     redfishActionParameterName,
1131827b4f1SAsmitha Karunanithi                                                     redfishActionName);
1141827b4f1SAsmitha Karunanithi             }
1151827b4f1SAsmitha Karunanithi             return;
1161827b4f1SAsmitha Karunanithi         }
1171827b4f1SAsmitha Karunanithi         if (ec.value() == boost::asio::error::host_unreachable)
1181827b4f1SAsmitha Karunanithi         {
1191827b4f1SAsmitha Karunanithi             BMCWEB_LOG_WARNING(
1201827b4f1SAsmitha Karunanithi                 "Resource {} is not found while performing action {}",
1211827b4f1SAsmitha Karunanithi                 redfishActionParameterName, redfishActionName);
1221827b4f1SAsmitha Karunanithi             messages::resourceNotFound(asyncResp->res, "Actions",
1231827b4f1SAsmitha Karunanithi                                        redfishActionName);
1241827b4f1SAsmitha Karunanithi             return;
1251827b4f1SAsmitha Karunanithi         }
1261827b4f1SAsmitha Karunanithi 
1271827b4f1SAsmitha Karunanithi         BMCWEB_LOG_ERROR("D-Bus error setting Redfish Property {} ec={}",
1281827b4f1SAsmitha Karunanithi                          redfishActionParameterName, ec);
1291827b4f1SAsmitha Karunanithi         messages::internalError(asyncResp->res);
1301827b4f1SAsmitha Karunanithi         return;
1311827b4f1SAsmitha Karunanithi     }
132cdf25ffbSEd Tanous     // Only set success if another error hasn't already happened.
133d02aad39SEd Tanous     if (asyncResp->res.result() == boost::beast::http::status::ok)
134d02aad39SEd Tanous     {
135cdf25ffbSEd Tanous         messages::success(asyncResp->res);
136d02aad39SEd Tanous     }
137d02aad39SEd Tanous };
138d02aad39SEd Tanous } // namespace details
139d02aad39SEd Tanous } // namespace redfish
140