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