xref: /openbmc/bmcweb/features/redfish/src/utils/dbus_utils.cpp (revision cdf25ffb6b2d99c829094c9a4c4907aec46e3a2e)
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 
24d02aad39SEd Tanous void afterSetProperty(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
25d02aad39SEd Tanous                       const std::string& redfishPropertyName,
26d02aad39SEd Tanous                       const nlohmann::json& propertyValue,
27d02aad39SEd Tanous                       const boost::system::error_code& ec,
28d02aad39SEd Tanous                       const sdbusplus::message_t& msg)
29d02aad39SEd Tanous {
30d02aad39SEd Tanous     if (ec)
31d02aad39SEd Tanous     {
32d02aad39SEd Tanous         if (ec.value() == boost::system::errc::permission_denied)
33d02aad39SEd Tanous         {
34d02aad39SEd Tanous             messages::insufficientPrivilege(asyncResp->res);
35d02aad39SEd Tanous         }
3687c44966SAsmitha Karunanithi         if (ec.value() == boost::asio::error::host_unreachable)
3787c44966SAsmitha Karunanithi         {
3887c44966SAsmitha Karunanithi             messages::resourceNotFound(asyncResp->res, "Set",
3987c44966SAsmitha Karunanithi                                        redfishPropertyName);
4087c44966SAsmitha Karunanithi             return;
4187c44966SAsmitha Karunanithi         }
42d02aad39SEd Tanous         const sd_bus_error* dbusError = msg.get_error();
43d02aad39SEd Tanous         if (dbusError != nullptr)
44d02aad39SEd Tanous         {
45d02aad39SEd Tanous             std::string_view errorName(dbusError->name);
46d02aad39SEd Tanous 
47d02aad39SEd Tanous             if (errorName == "xyz.openbmc_project.Common.Error.InvalidArgument")
48d02aad39SEd Tanous             {
49d02aad39SEd Tanous                 BMCWEB_LOG_WARNING("DBUS response error: {}", ec);
50d02aad39SEd Tanous                 messages::propertyValueIncorrect(
51d02aad39SEd Tanous                     asyncResp->res, redfishPropertyName, propertyValue);
52d02aad39SEd Tanous                 return;
53d02aad39SEd Tanous             }
54d02aad39SEd Tanous             if (errorName ==
55d02aad39SEd Tanous                 "xyz.openbmc_project.State.Chassis.Error.BMCNotReady")
56d02aad39SEd Tanous             {
57d02aad39SEd Tanous                 BMCWEB_LOG_WARNING(
58d02aad39SEd Tanous                     "BMC not ready, operation not allowed right now");
59d02aad39SEd Tanous                 messages::serviceTemporarilyUnavailable(asyncResp->res, "10");
60d02aad39SEd Tanous                 return;
61d02aad39SEd Tanous             }
62d02aad39SEd Tanous             if (errorName == "xyz.openbmc_project.State.Host.Error.BMCNotReady")
63d02aad39SEd Tanous             {
64d02aad39SEd Tanous                 BMCWEB_LOG_WARNING(
65d02aad39SEd Tanous                     "BMC not ready, operation not allowed right now");
66d02aad39SEd Tanous                 messages::serviceTemporarilyUnavailable(asyncResp->res, "10");
67d02aad39SEd Tanous                 return;
68d02aad39SEd Tanous             }
69d02aad39SEd Tanous             if (errorName == "xyz.openbmc_project.Common.Error.NotAllowed")
70d02aad39SEd Tanous             {
71d02aad39SEd Tanous                 messages::propertyNotWritable(asyncResp->res,
72d02aad39SEd Tanous                                               redfishPropertyName);
73d02aad39SEd Tanous                 return;
74d02aad39SEd Tanous             }
7587c44966SAsmitha Karunanithi             if (errorName == "xyz.openbmc_project.Common.Error.Unavailable")
7687c44966SAsmitha Karunanithi             {
7787c44966SAsmitha Karunanithi                 messages::resourceInStandby(asyncResp->res);
7887c44966SAsmitha Karunanithi                 return;
7987c44966SAsmitha Karunanithi             }
80d02aad39SEd Tanous         }
81d02aad39SEd Tanous         BMCWEB_LOG_ERROR("D-Bus error setting Redfish Property {} ec={}",
82d02aad39SEd Tanous                          redfishPropertyName, ec);
83d02aad39SEd Tanous         messages::internalError(asyncResp->res);
84d02aad39SEd Tanous         return;
85d02aad39SEd Tanous     }
86*cdf25ffbSEd Tanous     // Only set success if another error hasn't already happened.
871827b4f1SAsmitha Karunanithi     if (asyncResp->res.result() == boost::beast::http::status::ok)
881827b4f1SAsmitha Karunanithi     {
89*cdf25ffbSEd Tanous         messages::success(asyncResp->res);
901827b4f1SAsmitha Karunanithi     }
911827b4f1SAsmitha Karunanithi };
921827b4f1SAsmitha Karunanithi 
931827b4f1SAsmitha Karunanithi void afterSetPropertyAction(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
941827b4f1SAsmitha Karunanithi                             const std::string& redfishActionName,
951827b4f1SAsmitha Karunanithi                             const std::string& redfishActionParameterName,
961827b4f1SAsmitha Karunanithi                             const boost::system::error_code& ec,
971827b4f1SAsmitha Karunanithi                             const sdbusplus::message_t& /*msg*/)
981827b4f1SAsmitha Karunanithi {
991827b4f1SAsmitha Karunanithi     if (ec)
1001827b4f1SAsmitha Karunanithi     {
1011827b4f1SAsmitha Karunanithi         if (ec.value() == boost::asio::error::invalid_argument)
1021827b4f1SAsmitha Karunanithi         {
1031827b4f1SAsmitha Karunanithi             BMCWEB_LOG_WARNING(
1041827b4f1SAsmitha Karunanithi                 "Resource {} is patched with invalid argument during action {}",
1051827b4f1SAsmitha Karunanithi                 redfishActionParameterName, redfishActionName);
1061827b4f1SAsmitha Karunanithi             if (redfishActionParameterName.empty())
1071827b4f1SAsmitha Karunanithi             {
1081827b4f1SAsmitha Karunanithi                 messages::operationFailed(asyncResp->res);
1091827b4f1SAsmitha Karunanithi             }
1101827b4f1SAsmitha Karunanithi             else
1111827b4f1SAsmitha Karunanithi             {
1121827b4f1SAsmitha Karunanithi                 messages::actionParameterValueError(asyncResp->res,
1131827b4f1SAsmitha Karunanithi                                                     redfishActionParameterName,
1141827b4f1SAsmitha Karunanithi                                                     redfishActionName);
1151827b4f1SAsmitha Karunanithi             }
1161827b4f1SAsmitha Karunanithi             return;
1171827b4f1SAsmitha Karunanithi         }
1181827b4f1SAsmitha Karunanithi         if (ec.value() == boost::asio::error::host_unreachable)
1191827b4f1SAsmitha Karunanithi         {
1201827b4f1SAsmitha Karunanithi             BMCWEB_LOG_WARNING(
1211827b4f1SAsmitha Karunanithi                 "Resource {} is not found while performing action {}",
1221827b4f1SAsmitha Karunanithi                 redfishActionParameterName, redfishActionName);
1231827b4f1SAsmitha Karunanithi             messages::resourceNotFound(asyncResp->res, "Actions",
1241827b4f1SAsmitha Karunanithi                                        redfishActionName);
1251827b4f1SAsmitha Karunanithi             return;
1261827b4f1SAsmitha Karunanithi         }
1271827b4f1SAsmitha Karunanithi 
1281827b4f1SAsmitha Karunanithi         BMCWEB_LOG_ERROR("D-Bus error setting Redfish Property {} ec={}",
1291827b4f1SAsmitha Karunanithi                          redfishActionParameterName, ec);
1301827b4f1SAsmitha Karunanithi         messages::internalError(asyncResp->res);
1311827b4f1SAsmitha Karunanithi         return;
1321827b4f1SAsmitha Karunanithi     }
133*cdf25ffbSEd Tanous     // Only set success if another error hasn't already happened.
134d02aad39SEd Tanous     if (asyncResp->res.result() == boost::beast::http::status::ok)
135d02aad39SEd Tanous     {
136*cdf25ffbSEd Tanous         messages::success(asyncResp->res);
137d02aad39SEd Tanous     }
138d02aad39SEd Tanous };
139d02aad39SEd Tanous } // namespace details
140d02aad39SEd Tanous } // namespace redfish
141