xref: /openbmc/bmcweb/features/redfish/src/utils/dbus_utils.cpp (revision 83609eb5f07b73e14be4789719b93fe933559c59)
140e9b92eSEd Tanous // SPDX-License-Identifier: Apache-2.0
240e9b92eSEd 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 
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             {
79*83609eb5SGunnar Mills                 messages::propertyValueExternalConflict(
80*83609eb5SGunnar Mills                     asyncResp->res, redfishPropertyName, propertyValue);
8187c44966SAsmitha Karunanithi                 return;
8287c44966SAsmitha Karunanithi             }
83d02aad39SEd Tanous         }
84d02aad39SEd Tanous         BMCWEB_LOG_ERROR("D-Bus error setting Redfish Property {} ec={}",
85d02aad39SEd Tanous                          redfishPropertyName, ec);
86d02aad39SEd Tanous         messages::internalError(asyncResp->res);
87d02aad39SEd Tanous         return;
88d02aad39SEd Tanous     }
89247ae89bSEd Tanous     // Only set 204 if another error hasn't already happened.
901827b4f1SAsmitha Karunanithi     if (asyncResp->res.result() == boost::beast::http::status::ok)
911827b4f1SAsmitha Karunanithi     {
92247ae89bSEd Tanous         asyncResp->res.result(boost::beast::http::status::no_content);
931827b4f1SAsmitha Karunanithi     }
941827b4f1SAsmitha Karunanithi };
951827b4f1SAsmitha Karunanithi 
961827b4f1SAsmitha Karunanithi void afterSetPropertyAction(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
971827b4f1SAsmitha Karunanithi                             const std::string& redfishActionName,
981827b4f1SAsmitha Karunanithi                             const std::string& redfishActionParameterName,
991827b4f1SAsmitha Karunanithi                             const boost::system::error_code& ec,
1001827b4f1SAsmitha Karunanithi                             const sdbusplus::message_t& /*msg*/)
1011827b4f1SAsmitha Karunanithi {
1021827b4f1SAsmitha Karunanithi     if (ec)
1031827b4f1SAsmitha Karunanithi     {
1041827b4f1SAsmitha Karunanithi         if (ec.value() == boost::asio::error::invalid_argument)
1051827b4f1SAsmitha Karunanithi         {
1061827b4f1SAsmitha Karunanithi             BMCWEB_LOG_WARNING(
1071827b4f1SAsmitha Karunanithi                 "Resource {} is patched with invalid argument during action {}",
1081827b4f1SAsmitha Karunanithi                 redfishActionParameterName, redfishActionName);
1091827b4f1SAsmitha Karunanithi             if (redfishActionParameterName.empty())
1101827b4f1SAsmitha Karunanithi             {
1111827b4f1SAsmitha Karunanithi                 messages::operationFailed(asyncResp->res);
1121827b4f1SAsmitha Karunanithi             }
1131827b4f1SAsmitha Karunanithi             else
1141827b4f1SAsmitha Karunanithi             {
1151827b4f1SAsmitha Karunanithi                 messages::actionParameterValueError(asyncResp->res,
1161827b4f1SAsmitha Karunanithi                                                     redfishActionParameterName,
1171827b4f1SAsmitha Karunanithi                                                     redfishActionName);
1181827b4f1SAsmitha Karunanithi             }
1191827b4f1SAsmitha Karunanithi             return;
1201827b4f1SAsmitha Karunanithi         }
1211827b4f1SAsmitha Karunanithi         if (ec.value() == boost::asio::error::host_unreachable)
1221827b4f1SAsmitha Karunanithi         {
1231827b4f1SAsmitha Karunanithi             BMCWEB_LOG_WARNING(
1241827b4f1SAsmitha Karunanithi                 "Resource {} is not found while performing action {}",
1251827b4f1SAsmitha Karunanithi                 redfishActionParameterName, redfishActionName);
1261827b4f1SAsmitha Karunanithi             messages::resourceNotFound(asyncResp->res, "Actions",
1271827b4f1SAsmitha Karunanithi                                        redfishActionName);
1281827b4f1SAsmitha Karunanithi             return;
1291827b4f1SAsmitha Karunanithi         }
1301827b4f1SAsmitha Karunanithi 
1311827b4f1SAsmitha Karunanithi         BMCWEB_LOG_ERROR("D-Bus error setting Redfish Property {} ec={}",
1321827b4f1SAsmitha Karunanithi                          redfishActionParameterName, ec);
1331827b4f1SAsmitha Karunanithi         messages::internalError(asyncResp->res);
1341827b4f1SAsmitha Karunanithi         return;
1351827b4f1SAsmitha Karunanithi     }
136cdf25ffbSEd Tanous     // Only set success if another error hasn't already happened.
137d02aad39SEd Tanous     if (asyncResp->res.result() == boost::beast::http::status::ok)
138d02aad39SEd Tanous     {
139cdf25ffbSEd Tanous         messages::success(asyncResp->res);
140d02aad39SEd Tanous     }
141d02aad39SEd Tanous };
142d02aad39SEd Tanous } // namespace details
143d02aad39SEd Tanous } // namespace redfish
144