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