xref: /openbmc/bmcweb/features/redfish/lib/thermal.hpp (revision 432a890cfca335e565b770b1604ed4e547c5a732)
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 "sensors.hpp"
19 
20 #include <app.hpp>
21 
22 namespace redfish
23 {
24 
25 inline void requestRoutesThermal(App& app)
26 {
27     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Thermal/")
28         .privileges({{"Login"}})
29         .methods(boost::beast::http::verb::get)(
30             [](const crow::Request&,
31                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
32                const std::string& chassisName) {
33                 auto thermalPaths =
34                     sensors::dbus::paths.find(sensors::node::thermal);
35                 if (thermalPaths == sensors::dbus::paths.end())
36                 {
37                     messages::internalError(asyncResp->res);
38                     return;
39                 }
40 
41                 auto sensorAsyncResp = std::make_shared<SensorsAsyncResp>(
42                     asyncResp, chassisName, thermalPaths->second,
43                     sensors::node::thermal);
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({{"ConfigureManager"}})
51         .methods(boost::beast::http::verb::patch)(
52             [](const crow::Request& req,
53                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
54                const std::string& chassisName) {
55                 auto thermalPaths =
56                     sensors::dbus::paths.find(sensors::node::thermal);
57                 if (thermalPaths == sensors::dbus::paths.end())
58                 {
59                     messages::internalError(asyncResp->res);
60                     return;
61                 }
62 
63                 std::optional<std::vector<nlohmann::json>>
64                     temperatureCollections;
65                 std::optional<std::vector<nlohmann::json>> fanCollections;
66                 std::unordered_map<std::string, std::vector<nlohmann::json>>
67                     allCollections;
68 
69                 auto sensorsAsyncResp = std::make_shared<SensorsAsyncResp>(
70                     asyncResp, chassisName, thermalPaths->second,
71                     sensors::node::thermal);
72 
73                 if (!json_util::readJson(req, sensorsAsyncResp->asyncResp->res,
74                                          "Temperatures", temperatureCollections,
75                                          "Fans", fanCollections))
76                 {
77                     return;
78                 }
79                 if (!temperatureCollections && !fanCollections)
80                 {
81                     messages::resourceNotFound(sensorsAsyncResp->asyncResp->res,
82                                                "Thermal",
83                                                "Temperatures / Voltages");
84                     return;
85                 }
86                 if (temperatureCollections)
87                 {
88                     allCollections.emplace("Temperatures",
89                                            *std::move(temperatureCollections));
90                 }
91                 if (fanCollections)
92                 {
93                     allCollections.emplace("Fans", *std::move(fanCollections));
94                 }
95                 setSensorsOverride(sensorsAsyncResp, allCollections);
96             });
97 }
98 
99 } // namespace redfish
100