xref: /openbmc/bmcweb/features/redfish/lib/thermal.hpp (revision d78572018fc2022091ff8b8eb5a7fef2172ba3d6)
140e9b92eSEd Tanous // SPDX-License-Identifier: Apache-2.0
240e9b92eSEd Tanous // SPDX-FileCopyrightText: Copyright OpenBMC Authors
340e9b92eSEd Tanous // SPDX-FileCopyrightText: Copyright 2018 Intel Corporation
408777fb0SLewanczyk, Dawid #pragma once
508777fb0SLewanczyk, Dawid 
63ccb3adbSEd Tanous #include "app.hpp"
7*d7857201SEd Tanous #include "async_resp.hpp"
8*d7857201SEd Tanous #include "error_messages.hpp"
9*d7857201SEd Tanous #include "http_request.hpp"
103ccb3adbSEd Tanous #include "query.hpp"
113ccb3adbSEd Tanous #include "registries/privilege_registry.hpp"
1208777fb0SLewanczyk, Dawid #include "sensors.hpp"
135b90429aSEd Tanous #include "utils/json_utils.hpp"
14c9563608SJanet Adkins #include "utils/sensor_utils.hpp"
1508777fb0SLewanczyk, Dawid 
16*d7857201SEd Tanous #include <boost/beast/http/verb.hpp>
17*d7857201SEd Tanous #include <nlohmann/json.hpp>
18*d7857201SEd Tanous 
19*d7857201SEd Tanous #include <memory>
20*d7857201SEd Tanous #include <optional>
21*d7857201SEd Tanous #include <unordered_map>
22*d7857201SEd Tanous #include <utility>
23*d7857201SEd Tanous #include <vector>
24*d7857201SEd Tanous 
251abe55efSEd Tanous namespace redfish
261abe55efSEd Tanous {
2708777fb0SLewanczyk, Dawid 
287e860f15SJohn Edward Broadbent inline void requestRoutesThermal(App& app)
291abe55efSEd Tanous {
307e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Thermal/")
31ed398213SEd Tanous         .privileges(redfish::privileges::getThermal)
327e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
3345ca1b86SEd Tanous             [&app](const crow::Request& req,
347e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
357e860f15SJohn Edward Broadbent                    const std::string& chassisName) {
363ba00073SCarson Labrado                 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
3745ca1b86SEd Tanous                 {
3845ca1b86SEd Tanous                     return;
3945ca1b86SEd Tanous                 }
4045ca1b86SEd Tanous 
412474adfaSEd Tanous                 auto sensorAsyncResp = std::make_shared<SensorsAsyncResp>(
4202da7c5aSEd Tanous                     asyncResp, chassisName, sensors::dbus::thermalPaths,
430c728b42SJanet Adkins                     sensor_utils::chassisSubNodeToString(
440c728b42SJanet Adkins                         sensor_utils::ChassisSubNode::thermalNode));
452474adfaSEd Tanous 
462474adfaSEd Tanous                 // TODO Need to get Chassis Redundancy information.
472474adfaSEd Tanous                 getChassisData(sensorAsyncResp);
487e860f15SJohn Edward Broadbent             });
498d1b46d7Szhanghch05 
507e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Thermal/")
51ed398213SEd Tanous         .privileges(redfish::privileges::patchThermal)
527e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::patch)(
5345ca1b86SEd Tanous             [&app](const crow::Request& req,
547e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
557e860f15SJohn Edward Broadbent                    const std::string& chassisName) {
563ba00073SCarson Labrado                 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
5745ca1b86SEd Tanous                 {
5845ca1b86SEd Tanous                     return;
5945ca1b86SEd Tanous                 }
6045ca1b86SEd Tanous 
610885057cSEd Tanous                 std::optional<std::vector<nlohmann::json::object_t>>
620885057cSEd Tanous                     temperatureCollections;
63bd79bce8SPatrick Williams                 std::optional<std::vector<nlohmann::json::object_t>>
64bd79bce8SPatrick Williams                     fanCollections;
65bd79bce8SPatrick Williams                 std::unordered_map<std::string,
66bd79bce8SPatrick Williams                                    std::vector<nlohmann::json::object_t>>
674bb3dc34SCarol Wang                     allCollections;
684bb3dc34SCarol Wang 
698d1b46d7Szhanghch05                 auto sensorsAsyncResp = std::make_shared<SensorsAsyncResp>(
7002da7c5aSEd Tanous                     asyncResp, chassisName, sensors::dbus::thermalPaths,
710c728b42SJanet Adkins                     sensor_utils::chassisSubNodeToString(
720c728b42SJanet Adkins                         sensor_utils::ChassisSubNode::thermalNode));
734bb3dc34SCarol Wang 
74afc474aeSMyung Bae                 if (!json_util::readJsonPatch( //
75afc474aeSMyung Bae                         req, sensorsAsyncResp->asyncResp->res, //
76afc474aeSMyung Bae                         "Fans", fanCollections, //
77afc474aeSMyung Bae                         "Temperatures", temperatureCollections //
78afc474aeSMyung Bae                         ))
794bb3dc34SCarol Wang                 {
804bb3dc34SCarol Wang                     return;
814bb3dc34SCarol Wang                 }
824bb3dc34SCarol Wang                 if (!temperatureCollections && !fanCollections)
834bb3dc34SCarol Wang                 {
848d1b46d7Szhanghch05                     messages::resourceNotFound(sensorsAsyncResp->asyncResp->res,
85bd79bce8SPatrick Williams                                                "Thermal",
86bd79bce8SPatrick Williams                                                "Temperatures / Voltages");
874bb3dc34SCarol Wang                     return;
884bb3dc34SCarol Wang                 }
894bb3dc34SCarol Wang                 if (temperatureCollections)
904bb3dc34SCarol Wang                 {
914bb3dc34SCarol Wang                     allCollections.emplace("Temperatures",
924bb3dc34SCarol Wang                                            *std::move(temperatureCollections));
934bb3dc34SCarol Wang                 }
944bb3dc34SCarol Wang                 if (fanCollections)
954bb3dc34SCarol Wang                 {
964bb3dc34SCarol Wang                     allCollections.emplace("Fans", *std::move(fanCollections));
974bb3dc34SCarol Wang                 }
9880ac4024SBruce Lee                 setSensorsOverride(sensorsAsyncResp, allCollections);
997e860f15SJohn Edward Broadbent             });
100413961deSRichard Marian Thomaiyar }
10108777fb0SLewanczyk, Dawid 
10208777fb0SLewanczyk, Dawid } // namespace redfish
103