xref: /openbmc/bmcweb/redfish-core/lib/thermal.hpp (revision c6178aba)
1 /*
2 // Copyright (c) 2018 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16 #pragma once
17 
18 #include "app.hpp"
19 #include "query.hpp"
20 #include "registries/privilege_registry.hpp"
21 #include "sensors.hpp"
22 #include "utils/json_utils.hpp"
23 #include "utils/sensor_utils.hpp"
24 
25 namespace redfish
26 {
27 
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::thermalNode);
44 
45                 // TODO Need to get Chassis Redundancy information.
46                 getChassisData(sensorAsyncResp);
47             });
48 
49     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Thermal/")
50         .privileges(redfish::privileges::patchThermal)
51         .methods(boost::beast::http::verb::patch)(
52             [&app](const crow::Request& req,
53                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
54                    const std::string& chassisName) {
55                 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
56                 {
57                     return;
58                 }
59 
60                 std::optional<std::vector<nlohmann::json::object_t>>
61                     temperatureCollections;
62                 std::optional<std::vector<nlohmann::json::object_t>>
63                     fanCollections;
64                 std::unordered_map<std::string,
65                                    std::vector<nlohmann::json::object_t>>
66                     allCollections;
67 
68                 auto sensorsAsyncResp = std::make_shared<SensorsAsyncResp>(
69                     asyncResp, chassisName, sensors::dbus::thermalPaths,
70                     sensor_utils::thermalNode);
71 
72                 if (!json_util::readJsonPatch(
73                         req, sensorsAsyncResp->asyncResp->res, "Temperatures",
74                         temperatureCollections, "Fans", fanCollections))
75                 {
76                     return;
77                 }
78                 if (!temperatureCollections && !fanCollections)
79                 {
80                     messages::resourceNotFound(sensorsAsyncResp->asyncResp->res,
81                                                "Thermal",
82                                                "Temperatures / Voltages");
83                     return;
84                 }
85                 if (temperatureCollections)
86                 {
87                     allCollections.emplace("Temperatures",
88                                            *std::move(temperatureCollections));
89                 }
90                 if (fanCollections)
91                 {
92                     allCollections.emplace("Fans", *std::move(fanCollections));
93                 }
94                 setSensorsOverride(sensorsAsyncResp, allCollections);
95             });
96 }
97 
98 } // namespace redfish
99