xref: /openbmc/bmcweb/features/redfish/src/utils/dbus_utils.cpp (revision 1827b4f1de1e42bb3e2f53f584f07bb7119a3ed2)
1d02aad39SEd Tanous #include "utils/dbus_utils.hpp"
2d02aad39SEd Tanous 
3d02aad39SEd Tanous #include "async_resp.hpp"
4d02aad39SEd Tanous 
5d02aad39SEd Tanous #include <boost/system/error_code.hpp>
6d02aad39SEd Tanous #include <nlohmann/json.hpp>
7d02aad39SEd Tanous #include <sdbusplus/message.hpp>
8d02aad39SEd Tanous 
9d02aad39SEd Tanous #include <memory>
10d02aad39SEd Tanous #include <string>
11d02aad39SEd Tanous #include <string_view>
12d02aad39SEd Tanous 
13d02aad39SEd Tanous namespace redfish
14d02aad39SEd Tanous {
15d02aad39SEd Tanous namespace details
16d02aad39SEd Tanous {
17d02aad39SEd Tanous 
18d02aad39SEd Tanous void afterSetProperty(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
19d02aad39SEd Tanous                       const std::string& redfishPropertyName,
20d02aad39SEd Tanous                       const nlohmann::json& propertyValue,
21d02aad39SEd Tanous                       const boost::system::error_code& ec,
22d02aad39SEd Tanous                       const sdbusplus::message_t& msg)
23d02aad39SEd Tanous {
24d02aad39SEd Tanous     if (ec)
25d02aad39SEd Tanous     {
26d02aad39SEd Tanous         if (ec.value() == boost::system::errc::permission_denied)
27d02aad39SEd Tanous         {
28d02aad39SEd Tanous             messages::insufficientPrivilege(asyncResp->res);
29d02aad39SEd Tanous         }
30d02aad39SEd Tanous         const sd_bus_error* dbusError = msg.get_error();
31d02aad39SEd Tanous         if (dbusError != nullptr)
32d02aad39SEd Tanous         {
33d02aad39SEd Tanous             std::string_view errorName(dbusError->name);
34d02aad39SEd Tanous 
35d02aad39SEd Tanous             if (errorName == "xyz.openbmc_project.Common.Error.InvalidArgument")
36d02aad39SEd Tanous             {
37d02aad39SEd Tanous                 BMCWEB_LOG_WARNING("DBUS response error: {}", ec);
38d02aad39SEd Tanous                 messages::propertyValueIncorrect(
39d02aad39SEd Tanous                     asyncResp->res, redfishPropertyName, propertyValue);
40d02aad39SEd Tanous                 return;
41d02aad39SEd Tanous             }
42d02aad39SEd Tanous             if (errorName ==
43d02aad39SEd Tanous                 "xyz.openbmc_project.State.Chassis.Error.BMCNotReady")
44d02aad39SEd Tanous             {
45d02aad39SEd Tanous                 BMCWEB_LOG_WARNING(
46d02aad39SEd Tanous                     "BMC not ready, operation not allowed right now");
47d02aad39SEd Tanous                 messages::serviceTemporarilyUnavailable(asyncResp->res, "10");
48d02aad39SEd Tanous                 return;
49d02aad39SEd Tanous             }
50d02aad39SEd Tanous             if (errorName == "xyz.openbmc_project.State.Host.Error.BMCNotReady")
51d02aad39SEd Tanous             {
52d02aad39SEd Tanous                 BMCWEB_LOG_WARNING(
53d02aad39SEd Tanous                     "BMC not ready, operation not allowed right now");
54d02aad39SEd Tanous                 messages::serviceTemporarilyUnavailable(asyncResp->res, "10");
55d02aad39SEd Tanous                 return;
56d02aad39SEd Tanous             }
57d02aad39SEd Tanous             if (errorName == "xyz.openbmc_project.Common.Error.NotAllowed")
58d02aad39SEd Tanous             {
59d02aad39SEd Tanous                 messages::propertyNotWritable(asyncResp->res,
60d02aad39SEd Tanous                                               redfishPropertyName);
61d02aad39SEd Tanous                 return;
62d02aad39SEd Tanous             }
63d02aad39SEd Tanous         }
64d02aad39SEd Tanous         BMCWEB_LOG_ERROR("D-Bus error setting Redfish Property {} ec={}",
65d02aad39SEd Tanous                          redfishPropertyName, ec);
66d02aad39SEd Tanous         messages::internalError(asyncResp->res);
67d02aad39SEd Tanous         return;
68d02aad39SEd Tanous     }
69*1827b4f1SAsmitha Karunanithi     // Only set 204 if another error hasn't already happened.
70*1827b4f1SAsmitha Karunanithi     if (asyncResp->res.result() == boost::beast::http::status::ok)
71*1827b4f1SAsmitha Karunanithi     {
72*1827b4f1SAsmitha Karunanithi         asyncResp->res.result(boost::beast::http::status::no_content);
73*1827b4f1SAsmitha Karunanithi     }
74*1827b4f1SAsmitha Karunanithi };
75*1827b4f1SAsmitha Karunanithi 
76*1827b4f1SAsmitha Karunanithi void afterSetPropertyAction(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
77*1827b4f1SAsmitha Karunanithi                             const std::string& redfishActionName,
78*1827b4f1SAsmitha Karunanithi                             const std::string& redfishActionParameterName,
79*1827b4f1SAsmitha Karunanithi                             const boost::system::error_code& ec,
80*1827b4f1SAsmitha Karunanithi                             const sdbusplus::message_t& /*msg*/)
81*1827b4f1SAsmitha Karunanithi {
82*1827b4f1SAsmitha Karunanithi     if (ec)
83*1827b4f1SAsmitha Karunanithi     {
84*1827b4f1SAsmitha Karunanithi         if (ec.value() == boost::asio::error::invalid_argument)
85*1827b4f1SAsmitha Karunanithi         {
86*1827b4f1SAsmitha Karunanithi             BMCWEB_LOG_WARNING(
87*1827b4f1SAsmitha Karunanithi                 "Resource {} is patched with invalid argument during action {}",
88*1827b4f1SAsmitha Karunanithi                 redfishActionParameterName, redfishActionName);
89*1827b4f1SAsmitha Karunanithi             if (redfishActionParameterName.empty())
90*1827b4f1SAsmitha Karunanithi             {
91*1827b4f1SAsmitha Karunanithi                 messages::operationFailed(asyncResp->res);
92*1827b4f1SAsmitha Karunanithi             }
93*1827b4f1SAsmitha Karunanithi             else
94*1827b4f1SAsmitha Karunanithi             {
95*1827b4f1SAsmitha Karunanithi                 messages::actionParameterValueError(asyncResp->res,
96*1827b4f1SAsmitha Karunanithi                                                     redfishActionParameterName,
97*1827b4f1SAsmitha Karunanithi                                                     redfishActionName);
98*1827b4f1SAsmitha Karunanithi             }
99*1827b4f1SAsmitha Karunanithi             return;
100*1827b4f1SAsmitha Karunanithi         }
101*1827b4f1SAsmitha Karunanithi         if (ec.value() == boost::asio::error::host_unreachable)
102*1827b4f1SAsmitha Karunanithi         {
103*1827b4f1SAsmitha Karunanithi             BMCWEB_LOG_WARNING(
104*1827b4f1SAsmitha Karunanithi                 "Resource {} is not found while performing action {}",
105*1827b4f1SAsmitha Karunanithi                 redfishActionParameterName, redfishActionName);
106*1827b4f1SAsmitha Karunanithi             messages::resourceNotFound(asyncResp->res, "Actions",
107*1827b4f1SAsmitha Karunanithi                                        redfishActionName);
108*1827b4f1SAsmitha Karunanithi             return;
109*1827b4f1SAsmitha Karunanithi         }
110*1827b4f1SAsmitha Karunanithi 
111*1827b4f1SAsmitha Karunanithi         BMCWEB_LOG_ERROR("D-Bus error setting Redfish Property {} ec={}",
112*1827b4f1SAsmitha Karunanithi                          redfishActionParameterName, ec);
113*1827b4f1SAsmitha Karunanithi         messages::internalError(asyncResp->res);
114*1827b4f1SAsmitha Karunanithi         return;
115*1827b4f1SAsmitha Karunanithi     }
116*1827b4f1SAsmitha Karunanithi     // Only set 204 if another error hasn't already happened.
117d02aad39SEd Tanous     if (asyncResp->res.result() == boost::beast::http::status::ok)
118d02aad39SEd Tanous     {
119d02aad39SEd Tanous         asyncResp->res.result(boost::beast::http::status::no_content);
120d02aad39SEd Tanous     }
121d02aad39SEd Tanous };
122d02aad39SEd Tanous } // namespace details
123d02aad39SEd Tanous } // namespace redfish
124