xref: /openbmc/bmcweb/redfish-core/lib/thermal.hpp (revision 0c728b42)
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 
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, "Temperatures",
76                         temperatureCollections, "Fans", fanCollections))
77                 {
78                     return;
79                 }
80                 if (!temperatureCollections && !fanCollections)
81                 {
82                     messages::resourceNotFound(sensorsAsyncResp->asyncResp->res,
83                                                "Thermal",
84                                                "Temperatures / Voltages");
85                     return;
86                 }
87                 if (temperatureCollections)
88                 {
89                     allCollections.emplace("Temperatures",
90                                            *std::move(temperatureCollections));
91                 }
92                 if (fanCollections)
93                 {
94                     allCollections.emplace("Fans", *std::move(fanCollections));
95                 }
96                 setSensorsOverride(sensorsAsyncResp, allCollections);
97             });
98 }
99 
100 } // namespace redfish
101