xref: /openbmc/bmcweb/redfish-core/lib/thermal.hpp (revision 504af5a0568171b72caf13234cc81380b261fa21)
1 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: Copyright OpenBMC Authors
3 // SPDX-FileCopyrightText: Copyright 2018 Intel Corporation
4 #pragma once
5 
6 #include "app.hpp"
7 #include "async_resp.hpp"
8 #include "error_messages.hpp"
9 #include "http_request.hpp"
10 #include "query.hpp"
11 #include "registries/privilege_registry.hpp"
12 #include "sensors.hpp"
13 #include "utils/json_utils.hpp"
14 #include "utils/sensor_utils.hpp"
15 
16 #include <boost/beast/http/verb.hpp>
17 #include <nlohmann/json.hpp>
18 
19 #include <memory>
20 #include <optional>
21 #include <unordered_map>
22 #include <utility>
23 #include <vector>
24 
25 namespace redfish
26 {
27 
requestRoutesThermal(App & app)28 inline void requestRoutesThermal(App& app)
29 {
30     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Thermal/")
31         .privileges(redfish::privileges::getThermal)
32         .methods(boost::beast::http::verb::get)(
33             [&app](const crow::Request& req,
34                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
35                    const std::string& chassisName) {
36                 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
37                 {
38                     return;
39                 }
40 
41                 auto sensorAsyncResp = std::make_shared<SensorsAsyncResp>(
42                     asyncResp, chassisName, sensors::dbus::thermalPaths,
43                     sensor_utils::chassisSubNodeToString(
44                         sensor_utils::ChassisSubNode::thermalNode));
45 
46                 // TODO Need to get Chassis Redundancy information.
47                 getChassisData(sensorAsyncResp);
48             });
49 
50     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Thermal/")
51         .privileges(redfish::privileges::patchThermal)
52         .methods(boost::beast::http::verb::patch)(
53             [&app](const crow::Request& req,
54                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
55                    const std::string& chassisName) {
56                 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
57                 {
58                     return;
59                 }
60 
61                 std::optional<std::vector<nlohmann::json::object_t>>
62                     temperatureCollections;
63                 std::optional<std::vector<nlohmann::json::object_t>>
64                     fanCollections;
65                 std::unordered_map<std::string,
66                                    std::vector<nlohmann::json::object_t>>
67                     allCollections;
68 
69                 auto sensorsAsyncResp = std::make_shared<SensorsAsyncResp>(
70                     asyncResp, chassisName, sensors::dbus::thermalPaths,
71                     sensor_utils::chassisSubNodeToString(
72                         sensor_utils::ChassisSubNode::thermalNode));
73 
74                 if (!json_util::readJsonPatch(                 //
75                         req, sensorsAsyncResp->asyncResp->res, //
76                         "Fans", fanCollections,                //
77                         "Temperatures", temperatureCollections //
78                         ))
79                 {
80                     return;
81                 }
82                 if (!temperatureCollections && !fanCollections)
83                 {
84                     messages::resourceNotFound(sensorsAsyncResp->asyncResp->res,
85                                                "Thermal",
86                                                "Temperatures / Voltages");
87                     return;
88                 }
89                 if (temperatureCollections)
90                 {
91                     allCollections.emplace("Temperatures",
92                                            *std::move(temperatureCollections));
93                 }
94                 if (fanCollections)
95                 {
96                     allCollections.emplace("Fans", *std::move(fanCollections));
97                 }
98                 setSensorsOverride(sensorsAsyncResp, allCollections);
99             });
100 }
101 
102 } // namespace redfish
103