xref: /openbmc/bmcweb/features/redfish/lib/sensors.hpp (revision 4491419217d426791003facc749f600692d9b2a6)
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 
6d7857201SEd Tanous #include "bmcweb_config.h"
7d7857201SEd Tanous 
83ccb3adbSEd Tanous #include "app.hpp"
9d7857201SEd Tanous #include "async_resp.hpp"
103ccb3adbSEd Tanous #include "dbus_singleton.hpp"
117a1dbc48SGeorge Liu #include "dbus_utility.hpp"
12d7857201SEd Tanous #include "error_messages.hpp"
13539d8c6bSEd Tanous #include "generated/enums/redundancy.hpp"
14aaf08ac7SMatt Simmering #include "generated/enums/resource.hpp"
15d7857201SEd Tanous #include "http_request.hpp"
16d7857201SEd Tanous #include "logging.hpp"
173ccb3adbSEd Tanous #include "query.hpp"
183ccb3adbSEd Tanous #include "registries/privilege_registry.hpp"
1950ebd4afSEd Tanous #include "str_utility.hpp"
203ccb3adbSEd Tanous #include "utils/dbus_utils.hpp"
213ccb3adbSEd Tanous #include "utils/json_utils.hpp"
223ccb3adbSEd Tanous #include "utils/query_param.hpp"
231516c21bSJanet Adkins #include "utils/sensor_utils.hpp"
240ec8b83dSEd Tanous 
25d7857201SEd Tanous #include <asm-generic/errno.h>
26d7857201SEd Tanous 
27d7857201SEd Tanous #include <boost/beast/http/status.hpp>
28d7857201SEd Tanous #include <boost/beast/http/verb.hpp>
29e99073f5SGeorge Liu #include <boost/system/error_code.hpp>
30ef4c65b7SEd Tanous #include <boost/url/format.hpp>
31d7857201SEd Tanous #include <boost/url/url.hpp>
32d7857201SEd Tanous #include <nlohmann/json.hpp>
33d7857201SEd Tanous #include <sdbusplus/message/native_types.hpp>
3486d89ed7SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
351214b7e7SGunnar Mills 
36d7857201SEd Tanous #include <algorithm>
377a1dbc48SGeorge Liu #include <array>
381214b7e7SGunnar Mills #include <cmath>
39d7857201SEd Tanous #include <cstddef>
40d7857201SEd Tanous #include <cstdint>
41d7857201SEd Tanous #include <functional>
42fe04d49cSNan Zhou #include <iterator>
43fe04d49cSNan Zhou #include <map>
44d7857201SEd Tanous #include <memory>
45d7857201SEd Tanous #include <optional>
463544d2a7SEd Tanous #include <ranges>
47fe04d49cSNan Zhou #include <set>
48d7857201SEd Tanous #include <span>
4918f8f608SEd Tanous #include <string>
507a1dbc48SGeorge Liu #include <string_view>
51d7857201SEd Tanous #include <unordered_map>
52b5a76932SEd Tanous #include <utility>
53abf2add6SEd Tanous #include <variant>
54d7857201SEd Tanous #include <vector>
5508777fb0SLewanczyk, Dawid 
561abe55efSEd Tanous namespace redfish
571abe55efSEd Tanous {
5808777fb0SLewanczyk, Dawid 
59a0ec28b6SAdrian Ambrożewicz namespace sensors
60a0ec28b6SAdrian Ambrożewicz {
61a0ec28b6SAdrian Ambrożewicz 
6202da7c5aSEd Tanous // clang-format off
63a0ec28b6SAdrian Ambrożewicz namespace dbus
64a0ec28b6SAdrian Ambrożewicz {
65cf9e417dSEd Tanous constexpr auto powerPaths = std::to_array<std::string_view>({
6602da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/voltage",
6702da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/power"
6802da7c5aSEd Tanous });
69c2bf7f99SWludzik, Jozef 
7025b54dbaSEd Tanous constexpr auto getSensorPaths(){
7125b54dbaSEd Tanous     if constexpr(BMCWEB_REDFISH_NEW_POWERSUBSYSTEM_THERMALSUBSYSTEM){
7225b54dbaSEd Tanous     return std::to_array<std::string_view>({
7302da7c5aSEd Tanous         "/xyz/openbmc_project/sensors/power",
74a0ec28b6SAdrian Ambrożewicz         "/xyz/openbmc_project/sensors/current",
757088690cSBasheer Ahmed Muddebihal         "/xyz/openbmc_project/sensors/airflow",
765deabed9SGunnar Mills         "/xyz/openbmc_project/sensors/humidity",
77e8204933SGeorge Liu         "/xyz/openbmc_project/sensors/voltage",
78e8204933SGeorge Liu         "/xyz/openbmc_project/sensors/fan_tach",
79e8204933SGeorge Liu         "/xyz/openbmc_project/sensors/temperature",
80e8204933SGeorge Liu         "/xyz/openbmc_project/sensors/fan_pwm",
81e8204933SGeorge Liu         "/xyz/openbmc_project/sensors/altitude",
82e8204933SGeorge Liu         "/xyz/openbmc_project/sensors/energy",
83*44914192SZev Weiss         "/xyz/openbmc_project/sensors/liquidflow",
84*44914192SZev Weiss         "/xyz/openbmc_project/sensors/pressure",
8525b54dbaSEd Tanous         "/xyz/openbmc_project/sensors/utilization"});
8625b54dbaSEd Tanous     } else {
8725b54dbaSEd Tanous       return  std::to_array<std::string_view>({"/xyz/openbmc_project/sensors/power",
8825b54dbaSEd Tanous         "/xyz/openbmc_project/sensors/current",
8925b54dbaSEd Tanous         "/xyz/openbmc_project/sensors/airflow",
9025b54dbaSEd Tanous         "/xyz/openbmc_project/sensors/humidity",
9125b54dbaSEd Tanous         "/xyz/openbmc_project/sensors/utilization"});
9225b54dbaSEd Tanous }
9325b54dbaSEd Tanous }
9425b54dbaSEd Tanous 
9525b54dbaSEd Tanous constexpr auto sensorPaths = getSensorPaths();
9602da7c5aSEd Tanous 
97cf9e417dSEd Tanous constexpr auto thermalPaths = std::to_array<std::string_view>({
9802da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/fan_tach",
99a0ec28b6SAdrian Ambrożewicz     "/xyz/openbmc_project/sensors/temperature",
10002da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/fan_pwm"
10102da7c5aSEd Tanous });
10202da7c5aSEd Tanous 
103c2bf7f99SWludzik, Jozef } // namespace dbus
10402da7c5aSEd Tanous // clang-format on
10502da7c5aSEd Tanous 
1060c728b42SJanet Adkins constexpr std::string_view powerNodeStr = sensor_utils::chassisSubNodeToString(
1070c728b42SJanet Adkins     sensor_utils::ChassisSubNode::powerNode);
1080c728b42SJanet Adkins constexpr std::string_view sensorsNodeStr =
1090c728b42SJanet Adkins     sensor_utils::chassisSubNodeToString(
1100c728b42SJanet Adkins         sensor_utils::ChassisSubNode::sensorsNode);
1110c728b42SJanet Adkins constexpr std::string_view thermalNodeStr =
1120c728b42SJanet Adkins     sensor_utils::chassisSubNodeToString(
1130c728b42SJanet Adkins         sensor_utils::ChassisSubNode::thermalNode);
1140c728b42SJanet Adkins 
115cf9e417dSEd Tanous using sensorPair =
116cf9e417dSEd Tanous     std::pair<std::string_view, std::span<const std::string_view>>;
11702da7c5aSEd Tanous static constexpr std::array<sensorPair, 3> paths = {
1180c728b42SJanet Adkins     {{sensors::powerNodeStr, dbus::powerPaths},
1190c728b42SJanet Adkins      {sensors::sensorsNodeStr, dbus::sensorPaths},
1200c728b42SJanet Adkins      {sensors::thermalNodeStr, dbus::thermalPaths}}};
121c2bf7f99SWludzik, Jozef 
122a0ec28b6SAdrian Ambrożewicz } // namespace sensors
123a0ec28b6SAdrian Ambrożewicz 
12408777fb0SLewanczyk, Dawid /**
125588c3f0dSKowalski, Kamil  * SensorsAsyncResp
12608777fb0SLewanczyk, Dawid  * Gathers data needed for response processing after async calls are done
12708777fb0SLewanczyk, Dawid  */
1281abe55efSEd Tanous class SensorsAsyncResp
1291abe55efSEd Tanous {
13008777fb0SLewanczyk, Dawid   public:
131a0ec28b6SAdrian Ambrożewicz     using DataCompleteCb = std::function<void(
132a0ec28b6SAdrian Ambrożewicz         const boost::beast::http::status status,
133fe04d49cSNan Zhou         const std::map<std::string, std::string>& uriToDbus)>;
134a0ec28b6SAdrian Ambrożewicz 
135a0ec28b6SAdrian Ambrożewicz     struct SensorData
136a0ec28b6SAdrian Ambrożewicz     {
137f836c1d8SEd Tanous         std::string name;
138a0ec28b6SAdrian Ambrożewicz         std::string uri;
139f836c1d8SEd Tanous         std::string dbusPath;
140a0ec28b6SAdrian Ambrożewicz     };
141a0ec28b6SAdrian Ambrożewicz 
1428a592810SEd Tanous     SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
1438d1b46d7Szhanghch05                      const std::string& chassisIdIn,
144cf9e417dSEd Tanous                      std::span<const std::string_view> typesIn,
14502da7c5aSEd Tanous                      std::string_view subNode) :
146bd79bce8SPatrick Williams         asyncResp(asyncRespIn), chassisId(chassisIdIn), types(typesIn),
147bd79bce8SPatrick Williams         chassisSubNode(subNode), efficientExpand(false)
1481214b7e7SGunnar Mills     {}
14908777fb0SLewanczyk, Dawid 
150a0ec28b6SAdrian Ambrożewicz     // Store extra data about sensor mapping and return it in callback
1518a592810SEd Tanous     SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
1528d1b46d7Szhanghch05                      const std::string& chassisIdIn,
153cf9e417dSEd Tanous                      std::span<const std::string_view> typesIn,
15402da7c5aSEd Tanous                      std::string_view subNode,
155a0ec28b6SAdrian Ambrożewicz                      DataCompleteCb&& creationComplete) :
156bd79bce8SPatrick Williams         asyncResp(asyncRespIn), chassisId(chassisIdIn), types(typesIn),
157bd79bce8SPatrick Williams         chassisSubNode(subNode), efficientExpand(false),
158bd79bce8SPatrick Williams         metadata{std::vector<SensorData>()},
159a0ec28b6SAdrian Ambrożewicz         dataComplete{std::move(creationComplete)}
160a0ec28b6SAdrian Ambrożewicz     {}
161a0ec28b6SAdrian Ambrożewicz 
162928fefb9SNan Zhou     // sensor collections expand
1638a592810SEd Tanous     SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
164928fefb9SNan Zhou                      const std::string& chassisIdIn,
165cf9e417dSEd Tanous                      std::span<const std::string_view> typesIn,
1668a592810SEd Tanous                      const std::string_view& subNode, bool efficientExpandIn) :
167bd79bce8SPatrick Williams         asyncResp(asyncRespIn), chassisId(chassisIdIn), types(typesIn),
168bd79bce8SPatrick Williams         chassisSubNode(subNode), efficientExpand(efficientExpandIn)
169928fefb9SNan Zhou     {}
170928fefb9SNan Zhou 
1711abe55efSEd Tanous     ~SensorsAsyncResp()
1721abe55efSEd Tanous     {
1738d1b46d7Szhanghch05         if (asyncResp->res.result() ==
1748d1b46d7Szhanghch05             boost::beast::http::status::internal_server_error)
1751abe55efSEd Tanous         {
1761abe55efSEd Tanous             // Reset the json object to clear out any data that made it in
1771abe55efSEd Tanous             // before the error happened todo(ed) handle error condition with
1781abe55efSEd Tanous             // proper code
1798d1b46d7Szhanghch05             asyncResp->res.jsonValue = nlohmann::json::object();
18008777fb0SLewanczyk, Dawid         }
181a0ec28b6SAdrian Ambrożewicz 
182a0ec28b6SAdrian Ambrożewicz         if (dataComplete && metadata)
183a0ec28b6SAdrian Ambrożewicz         {
184fe04d49cSNan Zhou             std::map<std::string, std::string> map;
1858d1b46d7Szhanghch05             if (asyncResp->res.result() == boost::beast::http::status::ok)
186a0ec28b6SAdrian Ambrożewicz             {
187a0ec28b6SAdrian Ambrożewicz                 for (auto& sensor : *metadata)
188a0ec28b6SAdrian Ambrożewicz                 {
189c1d019a6SEd Tanous                     map.emplace(sensor.uri, sensor.dbusPath);
190a0ec28b6SAdrian Ambrożewicz                 }
191a0ec28b6SAdrian Ambrożewicz             }
1928d1b46d7Szhanghch05             dataComplete(asyncResp->res.result(), map);
193a0ec28b6SAdrian Ambrożewicz         }
19408777fb0SLewanczyk, Dawid     }
195588c3f0dSKowalski, Kamil 
196ecd6a3a2SEd Tanous     SensorsAsyncResp(const SensorsAsyncResp&) = delete;
197ecd6a3a2SEd Tanous     SensorsAsyncResp(SensorsAsyncResp&&) = delete;
198ecd6a3a2SEd Tanous     SensorsAsyncResp& operator=(const SensorsAsyncResp&) = delete;
199ecd6a3a2SEd Tanous     SensorsAsyncResp& operator=(SensorsAsyncResp&&) = delete;
200ecd6a3a2SEd Tanous 
201a0ec28b6SAdrian Ambrożewicz     void addMetadata(const nlohmann::json& sensorObject,
202c1d019a6SEd Tanous                      const std::string& dbusPath)
203a0ec28b6SAdrian Ambrożewicz     {
204a0ec28b6SAdrian Ambrożewicz         if (metadata)
205a0ec28b6SAdrian Ambrożewicz         {
206c1d019a6SEd Tanous             metadata->emplace_back(SensorData{
207c1d019a6SEd Tanous                 sensorObject["Name"], sensorObject["@odata.id"], dbusPath});
208a0ec28b6SAdrian Ambrożewicz         }
209a0ec28b6SAdrian Ambrożewicz     }
210a0ec28b6SAdrian Ambrożewicz 
211a0ec28b6SAdrian Ambrożewicz     void updateUri(const std::string& name, const std::string& uri)
212a0ec28b6SAdrian Ambrożewicz     {
213a0ec28b6SAdrian Ambrożewicz         if (metadata)
214a0ec28b6SAdrian Ambrożewicz         {
215a0ec28b6SAdrian Ambrożewicz             for (auto& sensor : *metadata)
216a0ec28b6SAdrian Ambrożewicz             {
217a0ec28b6SAdrian Ambrożewicz                 if (sensor.name == name)
218a0ec28b6SAdrian Ambrożewicz                 {
219a0ec28b6SAdrian Ambrożewicz                     sensor.uri = uri;
220a0ec28b6SAdrian Ambrożewicz                 }
221a0ec28b6SAdrian Ambrożewicz             }
222a0ec28b6SAdrian Ambrożewicz         }
223a0ec28b6SAdrian Ambrożewicz     }
224a0ec28b6SAdrian Ambrożewicz 
2258d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp> asyncResp;
226a0ec28b6SAdrian Ambrożewicz     const std::string chassisId;
227cf9e417dSEd Tanous     const std::span<const std::string_view> types;
228a0ec28b6SAdrian Ambrożewicz     const std::string chassisSubNode;
229928fefb9SNan Zhou     const bool efficientExpand;
230a0ec28b6SAdrian Ambrożewicz 
231a0ec28b6SAdrian Ambrożewicz   private:
232a0ec28b6SAdrian Ambrożewicz     std::optional<std::vector<SensorData>> metadata;
233a0ec28b6SAdrian Ambrożewicz     DataCompleteCb dataComplete;
23408777fb0SLewanczyk, Dawid };
23508777fb0SLewanczyk, Dawid 
236c9563608SJanet Adkins using InventoryItem = sensor_utils::InventoryItem;
237adc4f0dbSShawn McCarney 
238adc4f0dbSShawn McCarney /**
239413961deSRichard Marian Thomaiyar  * @brief Get objects with connection necessary for sensors
240588c3f0dSKowalski, Kamil  * @param SensorsAsyncResp Pointer to object holding response data
24108777fb0SLewanczyk, Dawid  * @param sensorNames Sensors retrieved from chassis
24208777fb0SLewanczyk, Dawid  * @param callback Callback for processing gathered connections
24308777fb0SLewanczyk, Dawid  */
24408777fb0SLewanczyk, Dawid template <typename Callback>
245413961deSRichard Marian Thomaiyar void getObjectsWithConnection(
24681ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
247fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& sensorNames,
2481abe55efSEd Tanous     Callback&& callback)
2491abe55efSEd Tanous {
25062598e31SEd Tanous     BMCWEB_LOG_DEBUG("getObjectsWithConnection enter");
25103b5bae3SJames Feist     const std::string path = "/xyz/openbmc_project/sensors";
252e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
25308777fb0SLewanczyk, Dawid         "xyz.openbmc_project.Sensor.Value"};
25408777fb0SLewanczyk, Dawid 
255e99073f5SGeorge Liu     // Make call to ObjectMapper to find all sensors objects
256e99073f5SGeorge Liu     dbus::utility::getSubTree(
257e99073f5SGeorge Liu         path, 2, interfaces,
2588cb2c024SEd Tanous         [callback = std::forward<Callback>(callback), sensorsAsyncResp,
259e99073f5SGeorge Liu          sensorNames](const boost::system::error_code& ec,
260002d39b4SEd Tanous                       const dbus::utility::MapperGetSubTreeResponse& subtree) {
261e99073f5SGeorge Liu             // Response handler for parsing objects subtree
26262598e31SEd Tanous             BMCWEB_LOG_DEBUG("getObjectsWithConnection resp_handler enter");
2631abe55efSEd Tanous             if (ec)
2641abe55efSEd Tanous             {
2658d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
26662598e31SEd Tanous                 BMCWEB_LOG_ERROR(
26762598e31SEd Tanous                     "getObjectsWithConnection resp_handler: Dbus error {}", ec);
26808777fb0SLewanczyk, Dawid                 return;
26908777fb0SLewanczyk, Dawid             }
27008777fb0SLewanczyk, Dawid 
27162598e31SEd Tanous             BMCWEB_LOG_DEBUG("Found {} subtrees", subtree.size());
27208777fb0SLewanczyk, Dawid 
273bd79bce8SPatrick Williams             // Make unique list of connections only for requested sensor types
274bd79bce8SPatrick Williams             // and found in the chassis
275fe04d49cSNan Zhou             std::set<std::string> connections;
276413961deSRichard Marian Thomaiyar             std::set<std::pair<std::string, std::string>> objectsWithConnection;
27708777fb0SLewanczyk, Dawid 
27862598e31SEd Tanous             BMCWEB_LOG_DEBUG("sensorNames list count: {}", sensorNames->size());
27949c53ac9SJohnathan Mantey             for (const std::string& tsensor : *sensorNames)
2801abe55efSEd Tanous             {
28162598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Sensor to find: {}", tsensor);
28208777fb0SLewanczyk, Dawid             }
28308777fb0SLewanczyk, Dawid 
284bd79bce8SPatrick Williams             for (const std::pair<std::string,
285bd79bce8SPatrick Williams                                  std::vector<std::pair<
286bd79bce8SPatrick Williams                                      std::string, std::vector<std::string>>>>&
2871abe55efSEd Tanous                      object : subtree)
2881abe55efSEd Tanous             {
28949c53ac9SJohnathan Mantey                 if (sensorNames->find(object.first) != sensorNames->end())
2901abe55efSEd Tanous                 {
29149c53ac9SJohnathan Mantey                     for (const std::pair<std::string, std::vector<std::string>>&
2921abe55efSEd Tanous                              objData : object.second)
2931abe55efSEd Tanous                     {
294bd79bce8SPatrick Williams                         BMCWEB_LOG_DEBUG("Adding connection: {}",
295bd79bce8SPatrick Williams                                          objData.first);
29608777fb0SLewanczyk, Dawid                         connections.insert(objData.first);
297de629b6eSShawn McCarney                         objectsWithConnection.insert(
298de629b6eSShawn McCarney                             std::make_pair(object.first, objData.first));
29908777fb0SLewanczyk, Dawid                     }
30008777fb0SLewanczyk, Dawid                 }
30108777fb0SLewanczyk, Dawid             }
30262598e31SEd Tanous             BMCWEB_LOG_DEBUG("Found {} connections", connections.size());
303413961deSRichard Marian Thomaiyar             callback(std::move(connections), std::move(objectsWithConnection));
30462598e31SEd Tanous             BMCWEB_LOG_DEBUG("getObjectsWithConnection resp_handler exit");
305e99073f5SGeorge Liu         });
30662598e31SEd Tanous     BMCWEB_LOG_DEBUG("getObjectsWithConnection exit");
307413961deSRichard Marian Thomaiyar }
308413961deSRichard Marian Thomaiyar 
309413961deSRichard Marian Thomaiyar /**
310413961deSRichard Marian Thomaiyar  * @brief Create connections necessary for sensors
311413961deSRichard Marian Thomaiyar  * @param SensorsAsyncResp Pointer to object holding response data
312413961deSRichard Marian Thomaiyar  * @param sensorNames Sensors retrieved from chassis
313413961deSRichard Marian Thomaiyar  * @param callback Callback for processing gathered connections
314413961deSRichard Marian Thomaiyar  */
315413961deSRichard Marian Thomaiyar template <typename Callback>
316daadfb2eSEd Tanous void getConnections(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
317daadfb2eSEd Tanous                     const std::shared_ptr<std::set<std::string>>& sensorNames,
318413961deSRichard Marian Thomaiyar                     Callback&& callback)
319413961deSRichard Marian Thomaiyar {
320413961deSRichard Marian Thomaiyar     auto objectsWithConnectionCb =
3218cb2c024SEd Tanous         [callback = std::forward<Callback>(callback)](
3228cb2c024SEd Tanous             const std::set<std::string>& connections,
323413961deSRichard Marian Thomaiyar             const std::set<std::pair<std::string, std::string>>&
3243174e4dfSEd Tanous             /*objectsWithConnection*/) { callback(connections); };
32581ce609eSEd Tanous     getObjectsWithConnection(sensorsAsyncResp, sensorNames,
326413961deSRichard Marian Thomaiyar                              std::move(objectsWithConnectionCb));
32708777fb0SLewanczyk, Dawid }
32808777fb0SLewanczyk, Dawid 
32908777fb0SLewanczyk, Dawid /**
33049c53ac9SJohnathan Mantey  * @brief Shrinks the list of sensors for processing
33149c53ac9SJohnathan Mantey  * @param SensorsAysncResp  The class holding the Redfish response
33249c53ac9SJohnathan Mantey  * @param allSensors  A list of all the sensors associated to the
33349c53ac9SJohnathan Mantey  * chassis element (i.e. baseboard, front panel, etc...)
33449c53ac9SJohnathan Mantey  * @param activeSensors A list that is a reduction of the incoming
33549c53ac9SJohnathan Mantey  * allSensors list.  Eliminate Thermal sensors when a Power request is
33649c53ac9SJohnathan Mantey  * made, and eliminate Power sensors when a Thermal request is made.
33749c53ac9SJohnathan Mantey  */
33823a21a1cSEd Tanous inline void reduceSensorList(
3397f1cc26dSEd Tanous     crow::Response& res, std::string_view chassisSubNode,
340cf9e417dSEd Tanous     std::span<const std::string_view> sensorTypes,
34149c53ac9SJohnathan Mantey     const std::vector<std::string>* allSensors,
342fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& activeSensors)
34349c53ac9SJohnathan Mantey {
34449c53ac9SJohnathan Mantey     if ((allSensors == nullptr) || (activeSensors == nullptr))
34549c53ac9SJohnathan Mantey     {
3467f1cc26dSEd Tanous         messages::resourceNotFound(res, chassisSubNode,
3470c728b42SJanet Adkins                                    chassisSubNode == sensors::thermalNodeStr
348a0ec28b6SAdrian Ambrożewicz                                        ? "Temperatures"
34949c53ac9SJohnathan Mantey                                        : "Voltages");
35049c53ac9SJohnathan Mantey 
35149c53ac9SJohnathan Mantey         return;
35249c53ac9SJohnathan Mantey     }
35349c53ac9SJohnathan Mantey     if (allSensors->empty())
35449c53ac9SJohnathan Mantey     {
35549c53ac9SJohnathan Mantey         // Nothing to do, the activeSensors object is also empty
35649c53ac9SJohnathan Mantey         return;
35749c53ac9SJohnathan Mantey     }
35849c53ac9SJohnathan Mantey 
3597f1cc26dSEd Tanous     for (std::string_view type : sensorTypes)
36049c53ac9SJohnathan Mantey     {
36149c53ac9SJohnathan Mantey         for (const std::string& sensor : *allSensors)
36249c53ac9SJohnathan Mantey         {
36311ba3979SEd Tanous             if (sensor.starts_with(type))
36449c53ac9SJohnathan Mantey             {
36549c53ac9SJohnathan Mantey                 activeSensors->emplace(sensor);
36649c53ac9SJohnathan Mantey             }
36749c53ac9SJohnathan Mantey         }
36849c53ac9SJohnathan Mantey     }
36949c53ac9SJohnathan Mantey }
37049c53ac9SJohnathan Mantey 
3717f1cc26dSEd Tanous /*
3727f1cc26dSEd Tanous  *Populates the top level collection for a given subnode.  Populates
3737f1cc26dSEd Tanous  *SensorCollection, Power, or Thermal schemas.
3747f1cc26dSEd Tanous  *
3757f1cc26dSEd Tanous  * */
3767f1cc26dSEd Tanous inline void populateChassisNode(nlohmann::json& jsonValue,
3777f1cc26dSEd Tanous                                 std::string_view chassisSubNode)
3787f1cc26dSEd Tanous {
3790c728b42SJanet Adkins     if (chassisSubNode == sensors::powerNodeStr)
3807f1cc26dSEd Tanous     {
3817f1cc26dSEd Tanous         jsonValue["@odata.type"] = "#Power.v1_5_2.Power";
3827f1cc26dSEd Tanous     }
3830c728b42SJanet Adkins     else if (chassisSubNode == sensors::thermalNodeStr)
3847f1cc26dSEd Tanous     {
3857f1cc26dSEd Tanous         jsonValue["@odata.type"] = "#Thermal.v1_4_0.Thermal";
3867f1cc26dSEd Tanous         jsonValue["Fans"] = nlohmann::json::array();
3877f1cc26dSEd Tanous         jsonValue["Temperatures"] = nlohmann::json::array();
3887f1cc26dSEd Tanous     }
3890c728b42SJanet Adkins     else if (chassisSubNode == sensors::sensorsNodeStr)
3907f1cc26dSEd Tanous     {
3917f1cc26dSEd Tanous         jsonValue["@odata.type"] = "#SensorCollection.SensorCollection";
3927f1cc26dSEd Tanous         jsonValue["Description"] = "Collection of Sensors for this Chassis";
3937f1cc26dSEd Tanous         jsonValue["Members"] = nlohmann::json::array();
3947f1cc26dSEd Tanous         jsonValue["Members@odata.count"] = 0;
3957f1cc26dSEd Tanous     }
3967f1cc26dSEd Tanous 
3970c728b42SJanet Adkins     if (chassisSubNode != sensors::sensorsNodeStr)
3987f1cc26dSEd Tanous     {
3997f1cc26dSEd Tanous         jsonValue["Id"] = chassisSubNode;
4007f1cc26dSEd Tanous     }
4017f1cc26dSEd Tanous     jsonValue["Name"] = chassisSubNode;
4027f1cc26dSEd Tanous }
4037f1cc26dSEd Tanous 
40449c53ac9SJohnathan Mantey /**
40508777fb0SLewanczyk, Dawid  * @brief Retrieves requested chassis sensors and redundancy data from DBus .
406588c3f0dSKowalski, Kamil  * @param SensorsAsyncResp   Pointer to object holding response data
40708777fb0SLewanczyk, Dawid  * @param callback  Callback for next step in gathered sensor processing
40808777fb0SLewanczyk, Dawid  */
40908777fb0SLewanczyk, Dawid template <typename Callback>
4107f1cc26dSEd Tanous void getChassis(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
4117f1cc26dSEd Tanous                 std::string_view chassisId, std::string_view chassisSubNode,
412cf9e417dSEd Tanous                 std::span<const std::string_view> sensorTypes,
413cf9e417dSEd Tanous                 Callback&& callback)
4141abe55efSEd Tanous {
41562598e31SEd Tanous     BMCWEB_LOG_DEBUG("getChassis enter");
4167a1dbc48SGeorge Liu     constexpr std::array<std::string_view, 2> interfaces = {
41749c53ac9SJohnathan Mantey         "xyz.openbmc_project.Inventory.Item.Board",
418adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Item.Chassis"};
4197a1dbc48SGeorge Liu 
4207a1dbc48SGeorge Liu     // Get the Chassis Collection
4217a1dbc48SGeorge Liu     dbus::utility::getSubTreePaths(
4227a1dbc48SGeorge Liu         "/xyz/openbmc_project/inventory", 0, interfaces,
4238cb2c024SEd Tanous         [callback = std::forward<Callback>(callback), asyncResp,
4247f1cc26dSEd Tanous          chassisIdStr{std::string(chassisId)},
4254e0d8789SEd Tanous          chassisSubNode{std::string(chassisSubNode)},
4264e0d8789SEd Tanous          sensorTypes](const boost::system::error_code& ec,
4274e0d8789SEd Tanous                       const dbus::utility::MapperGetSubTreePathsResponse&
4284e0d8789SEd Tanous                           chassisPaths) mutable {
42962598e31SEd Tanous             BMCWEB_LOG_DEBUG("getChassis respHandler enter");
4301abe55efSEd Tanous             if (ec)
4311abe55efSEd Tanous             {
43262598e31SEd Tanous                 BMCWEB_LOG_ERROR("getChassis respHandler DBUS error: {}", ec);
4337f1cc26dSEd Tanous                 messages::internalError(asyncResp->res);
43408777fb0SLewanczyk, Dawid                 return;
43508777fb0SLewanczyk, Dawid             }
43649c53ac9SJohnathan Mantey             const std::string* chassisPath = nullptr;
43749c53ac9SJohnathan Mantey             for (const std::string& chassis : chassisPaths)
4381abe55efSEd Tanous             {
43928aa8de5SGeorge Liu                 sdbusplus::message::object_path path(chassis);
440f8fe53e7SEd Tanous                 std::string chassisName = path.filename();
44128aa8de5SGeorge Liu                 if (chassisName.empty())
4421abe55efSEd Tanous                 {
44362598e31SEd Tanous                     BMCWEB_LOG_ERROR("Failed to find '/' in {}", chassis);
444daf36e2eSEd Tanous                     continue;
445daf36e2eSEd Tanous                 }
4467f1cc26dSEd Tanous                 if (chassisName == chassisIdStr)
4471abe55efSEd Tanous                 {
44849c53ac9SJohnathan Mantey                     chassisPath = &chassis;
44949c53ac9SJohnathan Mantey                     break;
450daf36e2eSEd Tanous                 }
45149c53ac9SJohnathan Mantey             }
45249c53ac9SJohnathan Mantey             if (chassisPath == nullptr)
4531abe55efSEd Tanous             {
454bd79bce8SPatrick Williams                 messages::resourceNotFound(asyncResp->res, "Chassis",
455bd79bce8SPatrick Williams                                            chassisIdStr);
45649c53ac9SJohnathan Mantey                 return;
4571abe55efSEd Tanous             }
4587f1cc26dSEd Tanous             populateChassisNode(asyncResp->res.jsonValue, chassisSubNode);
45908777fb0SLewanczyk, Dawid 
460ef4c65b7SEd Tanous             asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
461ef4c65b7SEd Tanous                 "/redfish/v1/Chassis/{}/{}", chassisIdStr, chassisSubNode);
46295a3ecadSAnthony Wilson 
4638fb49dd6SShawn McCarney             // Get the list of all sensors for this Chassis element
4648fb49dd6SShawn McCarney             std::string sensorPath = *chassisPath + "/all_sensors";
4656c3e9451SGeorge Liu             dbus::utility::getAssociationEndPoints(
4664e0d8789SEd Tanous                 sensorPath, [asyncResp, chassisSubNode, sensorTypes,
4674e0d8789SEd Tanous                              callback = std::forward<Callback>(callback)](
4688b24275dSEd Tanous                                 const boost::system::error_code& ec2,
4694e0d8789SEd Tanous                                 const dbus::utility::MapperEndPoints&
4704e0d8789SEd Tanous                                     nodeSensorList) mutable {
4718b24275dSEd Tanous                     if (ec2)
47249c53ac9SJohnathan Mantey                     {
4738b24275dSEd Tanous                         if (ec2.value() != EBADR)
47449c53ac9SJohnathan Mantey                         {
4757f1cc26dSEd Tanous                             messages::internalError(asyncResp->res);
47649c53ac9SJohnathan Mantey                             return;
47749c53ac9SJohnathan Mantey                         }
47849c53ac9SJohnathan Mantey                     }
479bd79bce8SPatrick Williams                     const std::shared_ptr<std::set<std::string>>
480bd79bce8SPatrick Williams                         culledSensorList =
481fe04d49cSNan Zhou                             std::make_shared<std::set<std::string>>();
482bd79bce8SPatrick Williams                     reduceSensorList(asyncResp->res, chassisSubNode,
483bd79bce8SPatrick Williams                                      sensorTypes, &nodeSensorList,
484bd79bce8SPatrick Williams                                      culledSensorList);
485bd79bce8SPatrick Williams                     BMCWEB_LOG_DEBUG("Finishing with {}",
486bd79bce8SPatrick Williams                                      culledSensorList->size());
48749c53ac9SJohnathan Mantey                     callback(culledSensorList);
4881e1e598dSJonathan Doman                 });
4897a1dbc48SGeorge Liu         });
49062598e31SEd Tanous     BMCWEB_LOG_DEBUG("getChassis exit");
49108777fb0SLewanczyk, Dawid }
49208777fb0SLewanczyk, Dawid 
49308777fb0SLewanczyk, Dawid /**
4941d7c0054SEd Tanous  * @brief Builds a json sensor representation of a sensor.
4951d7c0054SEd Tanous  * @param sensorName  The name of the sensor to be built
4961d7c0054SEd Tanous  * @param sensorType  The type (temperature, fan_tach, etc) of the sensor to
4971d7c0054SEd Tanous  * build
4988ece0e45SEd Tanous  * @param chassisSubNode The subnode (thermal, sensor, etc) of the sensor
4991d7c0054SEd Tanous  * @param interfacesDict  A dictionary of the interfaces and properties of said
5001d7c0054SEd Tanous  * interfaces to be built from
5011d7c0054SEd Tanous  * @param sensorJson  The json object to fill
5021d7c0054SEd Tanous  * @param inventoryItem D-Bus inventory item associated with the sensor.  Will
5031d7c0054SEd Tanous  * be nullptr if no associated inventory item was found.
5041d7c0054SEd Tanous  */
5051d7c0054SEd Tanous inline void objectInterfacesToJson(
5061d7c0054SEd Tanous     const std::string& sensorName, const std::string& sensorType,
5070c728b42SJanet Adkins     const sensor_utils::ChassisSubNode chassisSubNode,
50880f79a40SMichael Shen     const dbus::utility::DBusInterfacesMap& interfacesDict,
5091d7c0054SEd Tanous     nlohmann::json& sensorJson, InventoryItem* inventoryItem)
5101d7c0054SEd Tanous {
5111d7c0054SEd Tanous     for (const auto& [interface, valuesDict] : interfacesDict)
5121d7c0054SEd Tanous     {
513c9563608SJanet Adkins         sensor_utils::objectPropertiesToJson(
514c9563608SJanet Adkins             sensorName, sensorType, chassisSubNode, valuesDict, sensorJson,
515c9563608SJanet Adkins             inventoryItem);
5161d7c0054SEd Tanous     }
51762598e31SEd Tanous     BMCWEB_LOG_DEBUG("Added sensor {}", sensorName);
5181d7c0054SEd Tanous }
5191d7c0054SEd Tanous 
520b5a76932SEd Tanous inline void populateFanRedundancy(
521b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
5228bd25ccdSJames Feist {
523e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
524e99073f5SGeorge Liu         "xyz.openbmc_project.Control.FanRedundancy"};
525e99073f5SGeorge Liu     dbus::utility::getSubTree(
526e99073f5SGeorge Liu         "/xyz/openbmc_project/control", 2, interfaces,
527b9d36b47SEd Tanous         [sensorsAsyncResp](
528e99073f5SGeorge Liu             const boost::system::error_code& ec,
529b9d36b47SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& resp) {
5308bd25ccdSJames Feist             if (ec)
5318bd25ccdSJames Feist             {
5328bd25ccdSJames Feist                 return; // don't have to have this interface
5338bd25ccdSJames Feist             }
5346c3e9451SGeorge Liu             for (const std::pair<std::string, dbus::utility::MapperServiceMap>&
535e278c18fSEd Tanous                      pathPair : resp)
5368bd25ccdSJames Feist             {
537e278c18fSEd Tanous                 const std::string& path = pathPair.first;
538bd79bce8SPatrick Williams                 const dbus::utility::MapperServiceMap& objDict =
539bd79bce8SPatrick Williams                     pathPair.second;
5408bd25ccdSJames Feist                 if (objDict.empty())
5418bd25ccdSJames Feist                 {
5428bd25ccdSJames Feist                     continue; // this should be impossible
5438bd25ccdSJames Feist                 }
5448bd25ccdSJames Feist 
5458bd25ccdSJames Feist                 const std::string& owner = objDict.begin()->first;
5466c3e9451SGeorge Liu                 dbus::utility::getAssociationEndPoints(
5476c3e9451SGeorge Liu                     path + "/chassis",
5486c3e9451SGeorge Liu                     [path, owner, sensorsAsyncResp](
5498b24275dSEd Tanous                         const boost::system::error_code& ec2,
5506c3e9451SGeorge Liu                         const dbus::utility::MapperEndPoints& endpoints) {
5518b24275dSEd Tanous                         if (ec2)
5528bd25ccdSJames Feist                         {
5538bd25ccdSJames Feist                             return; // if they don't have an association we
5548bd25ccdSJames Feist                                     // can't tell what chassis is
5558bd25ccdSJames Feist                         }
5563544d2a7SEd Tanous                         auto found = std::ranges::find_if(
557bd79bce8SPatrick Williams                             endpoints,
558bd79bce8SPatrick Williams                             [sensorsAsyncResp](const std::string& entry) {
559bd79bce8SPatrick Williams                                 return entry.find(
560bd79bce8SPatrick Williams                                            sensorsAsyncResp->chassisId) !=
5618bd25ccdSJames Feist                                        std::string::npos;
5628bd25ccdSJames Feist                             });
5638bd25ccdSJames Feist 
5641e1e598dSJonathan Doman                         if (found == endpoints.end())
5658bd25ccdSJames Feist                         {
5668bd25ccdSJames Feist                             return;
5678bd25ccdSJames Feist                         }
568deae6a78SEd Tanous                         dbus::utility::getAllProperties(
56986d89ed7SKrzysztof Grobelny                             *crow::connections::systemBus, owner, path,
57086d89ed7SKrzysztof Grobelny                             "xyz.openbmc_project.Control.FanRedundancy",
5718bd25ccdSJames Feist                             [path, sensorsAsyncResp](
5728b24275dSEd Tanous                                 const boost::system::error_code& ec3,
57386d89ed7SKrzysztof Grobelny                                 const dbus::utility::DBusPropertiesMap& ret) {
5748b24275dSEd Tanous                                 if (ec3)
5758bd25ccdSJames Feist                                 {
5768bd25ccdSJames Feist                                     return; // don't have to have this
5778bd25ccdSJames Feist                                             // interface
5788bd25ccdSJames Feist                                 }
5798bd25ccdSJames Feist 
58086d89ed7SKrzysztof Grobelny                                 const uint8_t* allowedFailures = nullptr;
581bd79bce8SPatrick Williams                                 const std::vector<std::string>* collection =
582bd79bce8SPatrick Williams                                     nullptr;
58386d89ed7SKrzysztof Grobelny                                 const std::string* status = nullptr;
58486d89ed7SKrzysztof Grobelny 
585bd79bce8SPatrick Williams                                 const bool success =
586bd79bce8SPatrick Williams                                     sdbusplus::unpackPropertiesNoThrow(
58786d89ed7SKrzysztof Grobelny                                         dbus_utils::UnpackErrorPrinter(), ret,
588bd79bce8SPatrick Williams                                         "AllowedFailures", allowedFailures,
589bd79bce8SPatrick Williams                                         "Collection", collection, "Status",
590bd79bce8SPatrick Williams                                         status);
59186d89ed7SKrzysztof Grobelny 
59286d89ed7SKrzysztof Grobelny                                 if (!success)
59386d89ed7SKrzysztof Grobelny                                 {
59486d89ed7SKrzysztof Grobelny                                     messages::internalError(
59586d89ed7SKrzysztof Grobelny                                         sensorsAsyncResp->asyncResp->res);
59686d89ed7SKrzysztof Grobelny                                     return;
59786d89ed7SKrzysztof Grobelny                                 }
59886d89ed7SKrzysztof Grobelny 
599bd79bce8SPatrick Williams                                 if (allowedFailures == nullptr ||
600bd79bce8SPatrick Williams                                     collection == nullptr || status == nullptr)
6018bd25ccdSJames Feist                                 {
602bd79bce8SPatrick Williams                                     BMCWEB_LOG_ERROR(
603bd79bce8SPatrick Williams                                         "Invalid redundancy interface");
6048bd25ccdSJames Feist                                     messages::internalError(
6058d1b46d7Szhanghch05                                         sensorsAsyncResp->asyncResp->res);
6068bd25ccdSJames Feist                                     return;
6078bd25ccdSJames Feist                                 }
6088bd25ccdSJames Feist 
609bd79bce8SPatrick Williams                                 sdbusplus::message::object_path objectPath(
610bd79bce8SPatrick Williams                                     path);
61128aa8de5SGeorge Liu                                 std::string name = objectPath.filename();
61228aa8de5SGeorge Liu                                 if (name.empty())
6138bd25ccdSJames Feist                                 {
6148bd25ccdSJames Feist                                     // this should be impossible
6158bd25ccdSJames Feist                                     messages::internalError(
6168d1b46d7Szhanghch05                                         sensorsAsyncResp->asyncResp->res);
6178bd25ccdSJames Feist                                     return;
6188bd25ccdSJames Feist                                 }
61918f8f608SEd Tanous                                 std::ranges::replace(name, '_', ' ');
6208bd25ccdSJames Feist 
6218bd25ccdSJames Feist                                 std::string health;
6228bd25ccdSJames Feist 
62311ba3979SEd Tanous                                 if (status->ends_with("Full"))
6248bd25ccdSJames Feist                                 {
6258bd25ccdSJames Feist                                     health = "OK";
6268bd25ccdSJames Feist                                 }
62711ba3979SEd Tanous                                 else if (status->ends_with("Degraded"))
6288bd25ccdSJames Feist                                 {
6298bd25ccdSJames Feist                                     health = "Warning";
6308bd25ccdSJames Feist                                 }
6318bd25ccdSJames Feist                                 else
6328bd25ccdSJames Feist                                 {
6338bd25ccdSJames Feist                                     health = "Critical";
6348bd25ccdSJames Feist                                 }
6351476687dSEd Tanous                                 nlohmann::json::array_t redfishCollection;
6368bd25ccdSJames Feist                                 const auto& fanRedfish =
637bd79bce8SPatrick Williams                                     sensorsAsyncResp->asyncResp->res
638bd79bce8SPatrick Williams                                         .jsonValue["Fans"];
6398bd25ccdSJames Feist                                 for (const std::string& item : *collection)
6408bd25ccdSJames Feist                                 {
641bd79bce8SPatrick Williams                                     sdbusplus::message::object_path itemPath(
642bd79bce8SPatrick Williams                                         item);
6438a592810SEd Tanous                                     std::string itemName = itemPath.filename();
64428aa8de5SGeorge Liu                                     if (itemName.empty())
64528aa8de5SGeorge Liu                                     {
64628aa8de5SGeorge Liu                                         continue;
64728aa8de5SGeorge Liu                                     }
6488bd25ccdSJames Feist                                     /*
6498bd25ccdSJames Feist                                     todo(ed): merge patch that fixes the names
6508bd25ccdSJames Feist                                     std::replace(itemName.begin(),
6518bd25ccdSJames Feist                                                  itemName.end(), '_', ' ');*/
6523544d2a7SEd Tanous                                     auto schemaItem = std::ranges::find_if(
653bd79bce8SPatrick Williams                                         fanRedfish,
654bd79bce8SPatrick Williams                                         [itemName](const nlohmann::json& fan) {
6553e35c761SGeorge Liu                                             return fan["Name"] == itemName;
6568bd25ccdSJames Feist                                         });
6578bd25ccdSJames Feist                                     if (schemaItem != fanRedfish.end())
6588bd25ccdSJames Feist                                     {
6598a592810SEd Tanous                                         nlohmann::json::object_t collectionId;
6608a592810SEd Tanous                                         collectionId["@odata.id"] =
6611476687dSEd Tanous                                             (*schemaItem)["@odata.id"];
6621476687dSEd Tanous                                         redfishCollection.emplace_back(
6638a592810SEd Tanous                                             std::move(collectionId));
6648bd25ccdSJames Feist                                     }
6658bd25ccdSJames Feist                                     else
6668bd25ccdSJames Feist                                     {
667bd79bce8SPatrick Williams                                         BMCWEB_LOG_ERROR(
668bd79bce8SPatrick Williams                                             "failed to find fan in schema");
6698bd25ccdSJames Feist                                         messages::internalError(
6708d1b46d7Szhanghch05                                             sensorsAsyncResp->asyncResp->res);
6718bd25ccdSJames Feist                                         return;
6728bd25ccdSJames Feist                                     }
6738bd25ccdSJames Feist                                 }
6748bd25ccdSJames Feist 
675bd79bce8SPatrick Williams                                 size_t minNumNeeded =
676bd79bce8SPatrick Williams                                     collection->empty()
67726f6976fSEd Tanous                                         ? 0
678bd79bce8SPatrick Williams                                         : collection->size() - *allowedFailures;
679bd79bce8SPatrick Williams                                 nlohmann::json& jResp =
680bd79bce8SPatrick Williams                                     sensorsAsyncResp->asyncResp->res
6818bd25ccdSJames Feist                                         .jsonValue["Redundancy"];
6821476687dSEd Tanous 
6831476687dSEd Tanous                                 nlohmann::json::object_t redundancy;
684bd79bce8SPatrick Williams                                 boost::urls::url url = boost::urls::format(
685bd79bce8SPatrick Williams                                     "/redfish/v1/Chassis/{}/{}",
686ef4c65b7SEd Tanous                                     sensorsAsyncResp->chassisId,
687eddfc437SWilly Tu                                     sensorsAsyncResp->chassisSubNode);
688bd79bce8SPatrick Williams                                 url.set_fragment(
689bd79bce8SPatrick Williams                                     ("/Redundancy"_json_pointer / jResp.size())
690eddfc437SWilly Tu                                         .to_string());
691eddfc437SWilly Tu                                 redundancy["@odata.id"] = std::move(url);
692bd79bce8SPatrick Williams                                 redundancy["@odata.type"] =
693bd79bce8SPatrick Williams                                     "#Redundancy.v1_3_2.Redundancy";
6941476687dSEd Tanous                                 redundancy["MinNumNeeded"] = minNumNeeded;
695bd79bce8SPatrick Williams                                 redundancy["Mode"] =
696bd79bce8SPatrick Williams                                     redundancy::RedundancyType::NPlusM;
6971476687dSEd Tanous                                 redundancy["Name"] = name;
6981476687dSEd Tanous                                 redundancy["RedundancySet"] = redfishCollection;
6991476687dSEd Tanous                                 redundancy["Status"]["Health"] = health;
700bd79bce8SPatrick Williams                                 redundancy["Status"]["State"] =
701bd79bce8SPatrick Williams                                     resource::State::Enabled;
7021476687dSEd Tanous 
703b2ba3072SPatrick Williams                                 jResp.emplace_back(std::move(redundancy));
70486d89ed7SKrzysztof Grobelny                             });
7051e1e598dSJonathan Doman                     });
7068bd25ccdSJames Feist             }
707e99073f5SGeorge Liu         });
7088bd25ccdSJames Feist }
7098bd25ccdSJames Feist 
710504af5a0SPatrick Williams inline void sortJSONResponse(
711504af5a0SPatrick Williams     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
71249c53ac9SJohnathan Mantey {
7138d1b46d7Szhanghch05     nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue;
71449c53ac9SJohnathan Mantey     std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
7150c728b42SJanet Adkins     if (sensorsAsyncResp->chassisSubNode == sensors::powerNodeStr)
71649c53ac9SJohnathan Mantey     {
71749c53ac9SJohnathan Mantey         sensorHeaders = {"Voltages", "PowerSupplies"};
71849c53ac9SJohnathan Mantey     }
71949c53ac9SJohnathan Mantey     for (const std::string& sensorGroup : sensorHeaders)
72049c53ac9SJohnathan Mantey     {
72149c53ac9SJohnathan Mantey         nlohmann::json::iterator entry = response.find(sensorGroup);
7224e196b9aSEd Tanous         if (entry == response.end())
72349c53ac9SJohnathan Mantey         {
7244e196b9aSEd Tanous             continue;
7254e196b9aSEd Tanous         }
7264e196b9aSEd Tanous         nlohmann::json::array_t* arr =
7274e196b9aSEd Tanous             entry->get_ptr<nlohmann::json::array_t*>();
7284e196b9aSEd Tanous         if (arr == nullptr)
7294e196b9aSEd Tanous         {
7304e196b9aSEd Tanous             continue;
7314e196b9aSEd Tanous         }
7324e196b9aSEd Tanous         json_util::sortJsonArrayByKey(*arr, "Name");
73349c53ac9SJohnathan Mantey 
73449c53ac9SJohnathan Mantey         // add the index counts to the end of each entry
73549c53ac9SJohnathan Mantey         size_t count = 0;
73649c53ac9SJohnathan Mantey         for (nlohmann::json& sensorJson : *entry)
73749c53ac9SJohnathan Mantey         {
73849c53ac9SJohnathan Mantey             nlohmann::json::iterator odata = sensorJson.find("@odata.id");
73949c53ac9SJohnathan Mantey             if (odata == sensorJson.end())
74049c53ac9SJohnathan Mantey             {
74149c53ac9SJohnathan Mantey                 continue;
74249c53ac9SJohnathan Mantey             }
74349c53ac9SJohnathan Mantey             std::string* value = odata->get_ptr<std::string*>();
74449c53ac9SJohnathan Mantey             if (value != nullptr)
74549c53ac9SJohnathan Mantey             {
746eddfc437SWilly Tu                 *value += "/" + std::to_string(count);
7473e35c761SGeorge Liu                 sensorJson["MemberId"] = std::to_string(count);
74849c53ac9SJohnathan Mantey                 count++;
74981ce609eSEd Tanous                 sensorsAsyncResp->updateUri(sensorJson["Name"], *value);
75049c53ac9SJohnathan Mantey             }
75149c53ac9SJohnathan Mantey         }
75249c53ac9SJohnathan Mantey     }
75349c53ac9SJohnathan Mantey }
75449c53ac9SJohnathan Mantey 
75508777fb0SLewanczyk, Dawid /**
756adc4f0dbSShawn McCarney  * @brief Finds the inventory item with the specified object path.
757adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
758adc4f0dbSShawn McCarney  * @param invItemObjPath D-Bus object path of inventory item.
759adc4f0dbSShawn McCarney  * @return Inventory item within vector, or nullptr if no match found.
7608fb49dd6SShawn McCarney  */
76123a21a1cSEd Tanous inline InventoryItem* findInventoryItem(
762b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
763adc4f0dbSShawn McCarney     const std::string& invItemObjPath)
7648fb49dd6SShawn McCarney {
765adc4f0dbSShawn McCarney     for (InventoryItem& inventoryItem : *inventoryItems)
7668fb49dd6SShawn McCarney     {
767adc4f0dbSShawn McCarney         if (inventoryItem.objectPath == invItemObjPath)
7688fb49dd6SShawn McCarney         {
769adc4f0dbSShawn McCarney             return &inventoryItem;
7708fb49dd6SShawn McCarney         }
7718fb49dd6SShawn McCarney     }
7728fb49dd6SShawn McCarney     return nullptr;
7738fb49dd6SShawn McCarney }
7748fb49dd6SShawn McCarney 
7758fb49dd6SShawn McCarney /**
776adc4f0dbSShawn McCarney  * @brief Finds the inventory item associated with the specified sensor.
777adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
778adc4f0dbSShawn McCarney  * @param sensorObjPath D-Bus object path of sensor.
779adc4f0dbSShawn McCarney  * @return Inventory item within vector, or nullptr if no match found.
7808fb49dd6SShawn McCarney  */
78123a21a1cSEd Tanous inline InventoryItem* findInventoryItemForSensor(
782b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
783adc4f0dbSShawn McCarney     const std::string& sensorObjPath)
784adc4f0dbSShawn McCarney {
785adc4f0dbSShawn McCarney     for (InventoryItem& inventoryItem : *inventoryItems)
786adc4f0dbSShawn McCarney     {
787db0d36efSEd Tanous         if (inventoryItem.sensors.contains(sensorObjPath))
788adc4f0dbSShawn McCarney         {
789adc4f0dbSShawn McCarney             return &inventoryItem;
790adc4f0dbSShawn McCarney         }
791adc4f0dbSShawn McCarney     }
792adc4f0dbSShawn McCarney     return nullptr;
793adc4f0dbSShawn McCarney }
794adc4f0dbSShawn McCarney 
795adc4f0dbSShawn McCarney /**
796d500549bSAnthony Wilson  * @brief Finds the inventory item associated with the specified led path.
797d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
798d500549bSAnthony Wilson  * @param ledObjPath D-Bus object path of led.
799d500549bSAnthony Wilson  * @return Inventory item within vector, or nullptr if no match found.
800d500549bSAnthony Wilson  */
801bd79bce8SPatrick Williams inline InventoryItem* findInventoryItemForLed(
802bd79bce8SPatrick Williams     std::vector<InventoryItem>& inventoryItems, const std::string& ledObjPath)
803d500549bSAnthony Wilson {
804d500549bSAnthony Wilson     for (InventoryItem& inventoryItem : inventoryItems)
805d500549bSAnthony Wilson     {
806d500549bSAnthony Wilson         if (inventoryItem.ledObjectPath == ledObjPath)
807d500549bSAnthony Wilson         {
808d500549bSAnthony Wilson             return &inventoryItem;
809d500549bSAnthony Wilson         }
810d500549bSAnthony Wilson     }
811d500549bSAnthony Wilson     return nullptr;
812d500549bSAnthony Wilson }
813d500549bSAnthony Wilson 
814d500549bSAnthony Wilson /**
815adc4f0dbSShawn McCarney  * @brief Adds inventory item and associated sensor to specified vector.
816adc4f0dbSShawn McCarney  *
817adc4f0dbSShawn McCarney  * Adds a new InventoryItem to the vector if necessary.  Searches for an
818adc4f0dbSShawn McCarney  * existing InventoryItem with the specified object path.  If not found, one is
819adc4f0dbSShawn McCarney  * added to the vector.
820adc4f0dbSShawn McCarney  *
821adc4f0dbSShawn McCarney  * Next, the specified sensor is added to the set of sensors associated with the
822adc4f0dbSShawn McCarney  * InventoryItem.
823adc4f0dbSShawn McCarney  *
824adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
825adc4f0dbSShawn McCarney  * @param invItemObjPath D-Bus object path of inventory item.
826adc4f0dbSShawn McCarney  * @param sensorObjPath D-Bus object path of sensor
827adc4f0dbSShawn McCarney  */
828b5a76932SEd Tanous inline void addInventoryItem(
829b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
830b5a76932SEd Tanous     const std::string& invItemObjPath, const std::string& sensorObjPath)
831adc4f0dbSShawn McCarney {
832adc4f0dbSShawn McCarney     // Look for inventory item in vector
833bd79bce8SPatrick Williams     InventoryItem* inventoryItem =
834bd79bce8SPatrick Williams         findInventoryItem(inventoryItems, invItemObjPath);
835adc4f0dbSShawn McCarney 
836adc4f0dbSShawn McCarney     // If inventory item doesn't exist in vector, add it
837adc4f0dbSShawn McCarney     if (inventoryItem == nullptr)
838adc4f0dbSShawn McCarney     {
839adc4f0dbSShawn McCarney         inventoryItems->emplace_back(invItemObjPath);
840adc4f0dbSShawn McCarney         inventoryItem = &(inventoryItems->back());
841adc4f0dbSShawn McCarney     }
842adc4f0dbSShawn McCarney 
843adc4f0dbSShawn McCarney     // Add sensor to set of sensors associated with inventory item
844adc4f0dbSShawn McCarney     inventoryItem->sensors.emplace(sensorObjPath);
845adc4f0dbSShawn McCarney }
846adc4f0dbSShawn McCarney 
847adc4f0dbSShawn McCarney /**
848adc4f0dbSShawn McCarney  * @brief Stores D-Bus data in the specified inventory item.
849adc4f0dbSShawn McCarney  *
850adc4f0dbSShawn McCarney  * Finds D-Bus data in the specified map of interfaces.  Stores the data in the
851adc4f0dbSShawn McCarney  * specified InventoryItem.
852adc4f0dbSShawn McCarney  *
853adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
854adc4f0dbSShawn McCarney  * response.
855adc4f0dbSShawn McCarney  *
856adc4f0dbSShawn McCarney  * @param inventoryItem Inventory item where data will be stored.
857adc4f0dbSShawn McCarney  * @param interfacesDict Map containing D-Bus interfaces and their properties
858adc4f0dbSShawn McCarney  * for the specified inventory item.
859adc4f0dbSShawn McCarney  */
86023a21a1cSEd Tanous inline void storeInventoryItemData(
861adc4f0dbSShawn McCarney     InventoryItem& inventoryItem,
86280f79a40SMichael Shen     const dbus::utility::DBusInterfacesMap& interfacesDict)
8638fb49dd6SShawn McCarney {
864adc4f0dbSShawn McCarney     // Get properties from Inventory.Item interface
865711ac7a9SEd Tanous 
8669eb808c1SEd Tanous     for (const auto& [interface, values] : interfacesDict)
8678fb49dd6SShawn McCarney     {
868711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Item")
8698fb49dd6SShawn McCarney         {
8709eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
871711ac7a9SEd Tanous             {
872711ac7a9SEd Tanous                 if (name == "Present")
873711ac7a9SEd Tanous                 {
874711ac7a9SEd Tanous                     const bool* value = std::get_if<bool>(&dbusValue);
875adc4f0dbSShawn McCarney                     if (value != nullptr)
8768fb49dd6SShawn McCarney                     {
877adc4f0dbSShawn McCarney                         inventoryItem.isPresent = *value;
8788fb49dd6SShawn McCarney                     }
8798fb49dd6SShawn McCarney                 }
8808fb49dd6SShawn McCarney             }
881711ac7a9SEd Tanous         }
882adc4f0dbSShawn McCarney         // Check if Inventory.Item.PowerSupply interface is present
883711ac7a9SEd Tanous 
884711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Item.PowerSupply")
8858fb49dd6SShawn McCarney         {
886adc4f0dbSShawn McCarney             inventoryItem.isPowerSupply = true;
8878fb49dd6SShawn McCarney         }
888adc4f0dbSShawn McCarney 
889adc4f0dbSShawn McCarney         // Get properties from Inventory.Decorator.Asset interface
890711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
891adc4f0dbSShawn McCarney         {
8929eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
893711ac7a9SEd Tanous             {
894711ac7a9SEd Tanous                 if (name == "Manufacturer")
895adc4f0dbSShawn McCarney                 {
896adc4f0dbSShawn McCarney                     const std::string* value =
897711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
898adc4f0dbSShawn McCarney                     if (value != nullptr)
899adc4f0dbSShawn McCarney                     {
900adc4f0dbSShawn McCarney                         inventoryItem.manufacturer = *value;
901adc4f0dbSShawn McCarney                     }
902adc4f0dbSShawn McCarney                 }
903711ac7a9SEd Tanous                 if (name == "Model")
904adc4f0dbSShawn McCarney                 {
905adc4f0dbSShawn McCarney                     const std::string* value =
906711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
907adc4f0dbSShawn McCarney                     if (value != nullptr)
908adc4f0dbSShawn McCarney                     {
909adc4f0dbSShawn McCarney                         inventoryItem.model = *value;
910adc4f0dbSShawn McCarney                     }
911adc4f0dbSShawn McCarney                 }
912711ac7a9SEd Tanous                 if (name == "SerialNumber")
913adc4f0dbSShawn McCarney                 {
914adc4f0dbSShawn McCarney                     const std::string* value =
915711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
916adc4f0dbSShawn McCarney                     if (value != nullptr)
917adc4f0dbSShawn McCarney                     {
918adc4f0dbSShawn McCarney                         inventoryItem.serialNumber = *value;
919adc4f0dbSShawn McCarney                     }
920adc4f0dbSShawn McCarney                 }
921711ac7a9SEd Tanous                 if (name == "PartNumber")
922711ac7a9SEd Tanous                 {
923711ac7a9SEd Tanous                     const std::string* value =
924711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
925711ac7a9SEd Tanous                     if (value != nullptr)
926711ac7a9SEd Tanous                     {
927711ac7a9SEd Tanous                         inventoryItem.partNumber = *value;
928711ac7a9SEd Tanous                     }
929711ac7a9SEd Tanous                 }
930711ac7a9SEd Tanous             }
931adc4f0dbSShawn McCarney         }
932adc4f0dbSShawn McCarney 
933711ac7a9SEd Tanous         if (interface ==
934711ac7a9SEd Tanous             "xyz.openbmc_project.State.Decorator.OperationalStatus")
935adc4f0dbSShawn McCarney         {
9369eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
937adc4f0dbSShawn McCarney             {
938711ac7a9SEd Tanous                 if (name == "Functional")
939711ac7a9SEd Tanous                 {
940711ac7a9SEd Tanous                     const bool* value = std::get_if<bool>(&dbusValue);
941adc4f0dbSShawn McCarney                     if (value != nullptr)
942adc4f0dbSShawn McCarney                     {
943adc4f0dbSShawn McCarney                         inventoryItem.isFunctional = *value;
9448fb49dd6SShawn McCarney                     }
9458fb49dd6SShawn McCarney                 }
9468fb49dd6SShawn McCarney             }
9478fb49dd6SShawn McCarney         }
948711ac7a9SEd Tanous     }
949711ac7a9SEd Tanous }
9508fb49dd6SShawn McCarney 
9518fb49dd6SShawn McCarney /**
952adc4f0dbSShawn McCarney  * @brief Gets D-Bus data for inventory items associated with sensors.
9538fb49dd6SShawn McCarney  *
954adc4f0dbSShawn McCarney  * Uses the specified connections (services) to obtain D-Bus data for inventory
955adc4f0dbSShawn McCarney  * items associated with sensors.  Stores the resulting data in the
956adc4f0dbSShawn McCarney  * inventoryItems vector.
9578fb49dd6SShawn McCarney  *
958adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
959adc4f0dbSShawn McCarney  * response.
960adc4f0dbSShawn McCarney  *
961adc4f0dbSShawn McCarney  * Finds the inventory item data asynchronously.  Invokes callback when data has
962adc4f0dbSShawn McCarney  * been obtained.
963adc4f0dbSShawn McCarney  *
964adc4f0dbSShawn McCarney  * The callback must have the following signature:
965adc4f0dbSShawn McCarney  *   @code
966d500549bSAnthony Wilson  *   callback(void)
967adc4f0dbSShawn McCarney  *   @endcode
968adc4f0dbSShawn McCarney  *
969adc4f0dbSShawn McCarney  * This function is called recursively, obtaining data asynchronously from one
970adc4f0dbSShawn McCarney  * connection in each call.  This ensures the callback is not invoked until the
971adc4f0dbSShawn McCarney  * last asynchronous function has completed.
9728fb49dd6SShawn McCarney  *
9738fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
974adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
975adc4f0dbSShawn McCarney  * @param invConnections Connections that provide data for the inventory items.
9768fb49dd6SShawn McCarney  * implements ObjectManager.
977adc4f0dbSShawn McCarney  * @param callback Callback to invoke when inventory data has been obtained.
978adc4f0dbSShawn McCarney  * @param invConnectionsIndex Current index in invConnections.  Only specified
979adc4f0dbSShawn McCarney  * in recursive calls to this function.
9808fb49dd6SShawn McCarney  */
981adc4f0dbSShawn McCarney template <typename Callback>
9824ff0f1f4SEd Tanous void getInventoryItemsData(
983daadfb2eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
984daadfb2eSEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
985daadfb2eSEd Tanous     const std::shared_ptr<std::set<std::string>>& invConnections,
986daadfb2eSEd Tanous     Callback&& callback, size_t invConnectionsIndex = 0)
9878fb49dd6SShawn McCarney {
98862598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryItemsData enter");
9898fb49dd6SShawn McCarney 
990adc4f0dbSShawn McCarney     // If no more connections left, call callback
991adc4f0dbSShawn McCarney     if (invConnectionsIndex >= invConnections->size())
9928fb49dd6SShawn McCarney     {
993d500549bSAnthony Wilson         callback();
99462598e31SEd Tanous         BMCWEB_LOG_DEBUG("getInventoryItemsData exit");
995adc4f0dbSShawn McCarney         return;
996adc4f0dbSShawn McCarney     }
997adc4f0dbSShawn McCarney 
998adc4f0dbSShawn McCarney     // Get inventory item data from current connection
999fe04d49cSNan Zhou     auto it = invConnections->begin();
1000fe04d49cSNan Zhou     std::advance(it, invConnectionsIndex);
1001adc4f0dbSShawn McCarney     if (it != invConnections->end())
1002adc4f0dbSShawn McCarney     {
1003adc4f0dbSShawn McCarney         const std::string& invConnection = *it;
1004adc4f0dbSShawn McCarney 
10055eb468daSGeorge Liu         // Get all object paths and their interfaces for current connection
10065eb468daSGeorge Liu         sdbusplus::message::object_path path("/xyz/openbmc_project/inventory");
10075eb468daSGeorge Liu         dbus::utility::getManagedObjects(
10085eb468daSGeorge Liu             invConnection, path,
10095eb468daSGeorge Liu             [sensorsAsyncResp, inventoryItems, invConnections,
10108cb2c024SEd Tanous              callback = std::forward<Callback>(callback), invConnectionsIndex](
10115e7e2dc5SEd Tanous                 const boost::system::error_code& ec,
10124e0d8789SEd Tanous                 const dbus::utility::ManagedObjectType& resp) mutable {
101362598e31SEd Tanous                 BMCWEB_LOG_DEBUG("getInventoryItemsData respHandler enter");
10148fb49dd6SShawn McCarney                 if (ec)
10158fb49dd6SShawn McCarney                 {
101662598e31SEd Tanous                     BMCWEB_LOG_ERROR(
101762598e31SEd Tanous                         "getInventoryItemsData respHandler DBus error {}", ec);
10188d1b46d7Szhanghch05                     messages::internalError(sensorsAsyncResp->asyncResp->res);
10198fb49dd6SShawn McCarney                     return;
10208fb49dd6SShawn McCarney                 }
10218fb49dd6SShawn McCarney 
10228fb49dd6SShawn McCarney                 // Loop through returned object paths
10238fb49dd6SShawn McCarney                 for (const auto& objDictEntry : resp)
10248fb49dd6SShawn McCarney                 {
10258fb49dd6SShawn McCarney                     const std::string& objPath =
10268fb49dd6SShawn McCarney                         static_cast<const std::string&>(objDictEntry.first);
10278fb49dd6SShawn McCarney 
1028bd79bce8SPatrick Williams                     // If this object path is one of the specified inventory
1029bd79bce8SPatrick Williams                     // items
1030bd79bce8SPatrick Williams                     InventoryItem* inventoryItem =
1031bd79bce8SPatrick Williams                         findInventoryItem(inventoryItems, objPath);
1032adc4f0dbSShawn McCarney                     if (inventoryItem != nullptr)
10338fb49dd6SShawn McCarney                     {
1034adc4f0dbSShawn McCarney                         // Store inventory data in InventoryItem
1035bd79bce8SPatrick Williams                         storeInventoryItemData(*inventoryItem,
1036bd79bce8SPatrick Williams                                                objDictEntry.second);
10378fb49dd6SShawn McCarney                     }
10388fb49dd6SShawn McCarney                 }
10398fb49dd6SShawn McCarney 
1040adc4f0dbSShawn McCarney                 // Recurse to get inventory item data from next connection
1041adc4f0dbSShawn McCarney                 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
1042d0090733SEd Tanous                                       invConnections, std::move(callback),
1043d0090733SEd Tanous                                       invConnectionsIndex + 1);
1044adc4f0dbSShawn McCarney 
104562598e31SEd Tanous                 BMCWEB_LOG_DEBUG("getInventoryItemsData respHandler exit");
10465eb468daSGeorge Liu             });
10478fb49dd6SShawn McCarney     }
10488fb49dd6SShawn McCarney 
104962598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryItemsData exit");
10508fb49dd6SShawn McCarney }
10518fb49dd6SShawn McCarney 
10528fb49dd6SShawn McCarney /**
1053adc4f0dbSShawn McCarney  * @brief Gets connections that provide D-Bus data for inventory items.
10548fb49dd6SShawn McCarney  *
1055adc4f0dbSShawn McCarney  * Gets the D-Bus connections (services) that provide data for the inventory
1056adc4f0dbSShawn McCarney  * items that are associated with sensors.
10578fb49dd6SShawn McCarney  *
10588fb49dd6SShawn McCarney  * Finds the connections asynchronously.  Invokes callback when information has
10598fb49dd6SShawn McCarney  * been obtained.
10608fb49dd6SShawn McCarney  *
10618fb49dd6SShawn McCarney  * The callback must have the following signature:
10628fb49dd6SShawn McCarney  *   @code
1063fe04d49cSNan Zhou  *   callback(std::shared_ptr<std::set<std::string>> invConnections)
10648fb49dd6SShawn McCarney  *   @endcode
10658fb49dd6SShawn McCarney  *
10668fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
1067adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
10688fb49dd6SShawn McCarney  * @param callback Callback to invoke when connections have been obtained.
10698fb49dd6SShawn McCarney  */
10708fb49dd6SShawn McCarney template <typename Callback>
10714ff0f1f4SEd Tanous void getInventoryItemsConnections(
1072b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1073b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
10748fb49dd6SShawn McCarney     Callback&& callback)
10758fb49dd6SShawn McCarney {
107662598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryItemsConnections enter");
10778fb49dd6SShawn McCarney 
10788fb49dd6SShawn McCarney     const std::string path = "/xyz/openbmc_project/inventory";
1079e99073f5SGeorge Liu     constexpr std::array<std::string_view, 4> interfaces = {
10808fb49dd6SShawn McCarney         "xyz.openbmc_project.Inventory.Item",
1081adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Item.PowerSupply",
1082adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Decorator.Asset",
10838fb49dd6SShawn McCarney         "xyz.openbmc_project.State.Decorator.OperationalStatus"};
10848fb49dd6SShawn McCarney 
1085e99073f5SGeorge Liu     // Make call to ObjectMapper to find all inventory items
1086e99073f5SGeorge Liu     dbus::utility::getSubTree(
1087e99073f5SGeorge Liu         path, 0, interfaces,
10888cb2c024SEd Tanous         [callback = std::forward<Callback>(callback), sensorsAsyncResp,
1089002d39b4SEd Tanous          inventoryItems](
1090e99073f5SGeorge Liu             const boost::system::error_code& ec,
10914e0d8789SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) mutable {
1092e99073f5SGeorge Liu             // Response handler for parsing output from GetSubTree
109362598e31SEd Tanous             BMCWEB_LOG_DEBUG("getInventoryItemsConnections respHandler enter");
10948fb49dd6SShawn McCarney             if (ec)
10958fb49dd6SShawn McCarney             {
10968d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
109762598e31SEd Tanous                 BMCWEB_LOG_ERROR(
1098bd79bce8SPatrick Williams                     "getInventoryItemsConnections respHandler DBus error {}",
1099bd79bce8SPatrick Williams                     ec);
11008fb49dd6SShawn McCarney                 return;
11018fb49dd6SShawn McCarney             }
11028fb49dd6SShawn McCarney 
11038fb49dd6SShawn McCarney             // Make unique list of connections for desired inventory items
1104fe04d49cSNan Zhou             std::shared_ptr<std::set<std::string>> invConnections =
1105fe04d49cSNan Zhou                 std::make_shared<std::set<std::string>>();
11068fb49dd6SShawn McCarney 
11078fb49dd6SShawn McCarney             // Loop through objects from GetSubTree
1108bd79bce8SPatrick Williams             for (const std::pair<std::string,
1109bd79bce8SPatrick Williams                                  std::vector<std::pair<
1110bd79bce8SPatrick Williams                                      std::string, std::vector<std::string>>>>&
11118fb49dd6SShawn McCarney                      object : subtree)
11128fb49dd6SShawn McCarney             {
1113adc4f0dbSShawn McCarney                 // Check if object path is one of the specified inventory items
11148fb49dd6SShawn McCarney                 const std::string& objPath = object.first;
1115adc4f0dbSShawn McCarney                 if (findInventoryItem(inventoryItems, objPath) != nullptr)
11168fb49dd6SShawn McCarney                 {
11178fb49dd6SShawn McCarney                     // Store all connections to inventory item
11188fb49dd6SShawn McCarney                     for (const std::pair<std::string, std::vector<std::string>>&
11198fb49dd6SShawn McCarney                              objData : object.second)
11208fb49dd6SShawn McCarney                     {
11218fb49dd6SShawn McCarney                         const std::string& invConnection = objData.first;
11228fb49dd6SShawn McCarney                         invConnections->insert(invConnection);
11238fb49dd6SShawn McCarney                     }
11248fb49dd6SShawn McCarney                 }
11258fb49dd6SShawn McCarney             }
1126d500549bSAnthony Wilson 
11278fb49dd6SShawn McCarney             callback(invConnections);
112862598e31SEd Tanous             BMCWEB_LOG_DEBUG("getInventoryItemsConnections respHandler exit");
1129e99073f5SGeorge Liu         });
113062598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryItemsConnections exit");
11318fb49dd6SShawn McCarney }
11328fb49dd6SShawn McCarney 
11338fb49dd6SShawn McCarney /**
1134adc4f0dbSShawn McCarney  * @brief Gets associations from sensors to inventory items.
11358fb49dd6SShawn McCarney  *
11368fb49dd6SShawn McCarney  * Looks for ObjectMapper associations from the specified sensors to related
1137d500549bSAnthony Wilson  * inventory items. Then finds the associations from those inventory items to
1138d500549bSAnthony Wilson  * their LEDs, if any.
11398fb49dd6SShawn McCarney  *
11408fb49dd6SShawn McCarney  * Finds the inventory items asynchronously.  Invokes callback when information
11418fb49dd6SShawn McCarney  * has been obtained.
11428fb49dd6SShawn McCarney  *
11438fb49dd6SShawn McCarney  * The callback must have the following signature:
11448fb49dd6SShawn McCarney  *   @code
1145adc4f0dbSShawn McCarney  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
11468fb49dd6SShawn McCarney  *   @endcode
11478fb49dd6SShawn McCarney  *
11488fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
11498fb49dd6SShawn McCarney  * @param sensorNames All sensors within the current chassis.
11508fb49dd6SShawn McCarney  * implements ObjectManager.
11518fb49dd6SShawn McCarney  * @param callback Callback to invoke when inventory items have been obtained.
11528fb49dd6SShawn McCarney  */
11538fb49dd6SShawn McCarney template <typename Callback>
11544ff0f1f4SEd Tanous void getInventoryItemAssociations(
1155b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1156fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& sensorNames,
11578fb49dd6SShawn McCarney     Callback&& callback)
11588fb49dd6SShawn McCarney {
115962598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryItemAssociations enter");
11608fb49dd6SShawn McCarney 
11615eb468daSGeorge Liu     // Call GetManagedObjects on the ObjectMapper to get all associations
11625eb468daSGeorge Liu     sdbusplus::message::object_path path("/");
11635eb468daSGeorge Liu     dbus::utility::getManagedObjects(
11645eb468daSGeorge Liu         "xyz.openbmc_project.ObjectMapper", path,
11658cb2c024SEd Tanous         [callback = std::forward<Callback>(callback), sensorsAsyncResp,
11665e7e2dc5SEd Tanous          sensorNames](const boost::system::error_code& ec,
11674e0d8789SEd Tanous                       const dbus::utility::ManagedObjectType& resp) mutable {
116862598e31SEd Tanous             BMCWEB_LOG_DEBUG("getInventoryItemAssociations respHandler enter");
11698fb49dd6SShawn McCarney             if (ec)
11708fb49dd6SShawn McCarney             {
117162598e31SEd Tanous                 BMCWEB_LOG_ERROR(
1172bd79bce8SPatrick Williams                     "getInventoryItemAssociations respHandler DBus error {}",
1173bd79bce8SPatrick Williams                     ec);
11748d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
11758fb49dd6SShawn McCarney                 return;
11768fb49dd6SShawn McCarney             }
11778fb49dd6SShawn McCarney 
1178adc4f0dbSShawn McCarney             // Create vector to hold list of inventory items
1179adc4f0dbSShawn McCarney             std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1180adc4f0dbSShawn McCarney                 std::make_shared<std::vector<InventoryItem>>();
1181adc4f0dbSShawn McCarney 
11828fb49dd6SShawn McCarney             // Loop through returned object paths
11838fb49dd6SShawn McCarney             std::string sensorAssocPath;
11848fb49dd6SShawn McCarney             sensorAssocPath.reserve(128); // avoid memory allocations
11858fb49dd6SShawn McCarney             for (const auto& objDictEntry : resp)
11868fb49dd6SShawn McCarney             {
11878fb49dd6SShawn McCarney                 const std::string& objPath =
11888fb49dd6SShawn McCarney                     static_cast<const std::string&>(objDictEntry.first);
11898fb49dd6SShawn McCarney 
1190bd79bce8SPatrick Williams                 // If path is inventory association for one of the specified
1191bd79bce8SPatrick Williams                 // sensors
11928fb49dd6SShawn McCarney                 for (const std::string& sensorName : *sensorNames)
11938fb49dd6SShawn McCarney                 {
11948fb49dd6SShawn McCarney                     sensorAssocPath = sensorName;
11958fb49dd6SShawn McCarney                     sensorAssocPath += "/inventory";
11968fb49dd6SShawn McCarney                     if (objPath == sensorAssocPath)
11978fb49dd6SShawn McCarney                     {
11988fb49dd6SShawn McCarney                         // Get Association interface for object path
1199bd79bce8SPatrick Williams                         for (const auto& [interface, values] :
1200bd79bce8SPatrick Williams                              objDictEntry.second)
12018fb49dd6SShawn McCarney                         {
1202711ac7a9SEd Tanous                             if (interface == "xyz.openbmc_project.Association")
1203711ac7a9SEd Tanous                             {
1204711ac7a9SEd Tanous                                 for (const auto& [valueName, value] : values)
1205711ac7a9SEd Tanous                                 {
1206711ac7a9SEd Tanous                                     if (valueName == "endpoints")
12078fb49dd6SShawn McCarney                                     {
1208bd79bce8SPatrick Williams                                         const std::vector<std::string>*
1209bd79bce8SPatrick Williams                                             endpoints = std::get_if<
1210bd79bce8SPatrick Williams                                                 std::vector<std::string>>(
1211711ac7a9SEd Tanous                                                 &value);
1212711ac7a9SEd Tanous                                         if ((endpoints != nullptr) &&
1213711ac7a9SEd Tanous                                             !endpoints->empty())
12148fb49dd6SShawn McCarney                                         {
1215adc4f0dbSShawn McCarney                                             // Add inventory item to vector
1216adc4f0dbSShawn McCarney                                             const std::string& invItemPath =
1217adc4f0dbSShawn McCarney                                                 endpoints->front();
1218711ac7a9SEd Tanous                                             addInventoryItem(inventoryItems,
1219711ac7a9SEd Tanous                                                              invItemPath,
1220adc4f0dbSShawn McCarney                                                              sensorName);
12218fb49dd6SShawn McCarney                                         }
12228fb49dd6SShawn McCarney                                     }
12238fb49dd6SShawn McCarney                                 }
1224711ac7a9SEd Tanous                             }
1225711ac7a9SEd Tanous                         }
12268fb49dd6SShawn McCarney                         break;
12278fb49dd6SShawn McCarney                     }
12288fb49dd6SShawn McCarney                 }
12298fb49dd6SShawn McCarney             }
12308fb49dd6SShawn McCarney 
1231d500549bSAnthony Wilson             // Now loop through the returned object paths again, this time to
1232d500549bSAnthony Wilson             // find the leds associated with the inventory items we just found
1233d500549bSAnthony Wilson             std::string inventoryAssocPath;
1234d500549bSAnthony Wilson             inventoryAssocPath.reserve(128); // avoid memory allocations
1235d500549bSAnthony Wilson             for (const auto& objDictEntry : resp)
1236d500549bSAnthony Wilson             {
1237d500549bSAnthony Wilson                 const std::string& objPath =
1238d500549bSAnthony Wilson                     static_cast<const std::string&>(objDictEntry.first);
1239d500549bSAnthony Wilson 
1240d500549bSAnthony Wilson                 for (InventoryItem& inventoryItem : *inventoryItems)
1241d500549bSAnthony Wilson                 {
1242d500549bSAnthony Wilson                     inventoryAssocPath = inventoryItem.objectPath;
1243d500549bSAnthony Wilson                     inventoryAssocPath += "/leds";
1244d500549bSAnthony Wilson                     if (objPath == inventoryAssocPath)
1245d500549bSAnthony Wilson                     {
1246bd79bce8SPatrick Williams                         for (const auto& [interface, values] :
1247bd79bce8SPatrick Williams                              objDictEntry.second)
1248d500549bSAnthony Wilson                         {
1249711ac7a9SEd Tanous                             if (interface == "xyz.openbmc_project.Association")
1250711ac7a9SEd Tanous                             {
1251711ac7a9SEd Tanous                                 for (const auto& [valueName, value] : values)
1252711ac7a9SEd Tanous                                 {
1253711ac7a9SEd Tanous                                     if (valueName == "endpoints")
1254d500549bSAnthony Wilson                                     {
1255bd79bce8SPatrick Williams                                         const std::vector<std::string>*
1256bd79bce8SPatrick Williams                                             endpoints = std::get_if<
1257bd79bce8SPatrick Williams                                                 std::vector<std::string>>(
1258711ac7a9SEd Tanous                                                 &value);
1259711ac7a9SEd Tanous                                         if ((endpoints != nullptr) &&
1260711ac7a9SEd Tanous                                             !endpoints->empty())
1261d500549bSAnthony Wilson                                         {
1262711ac7a9SEd Tanous                                             // Add inventory item to vector
1263d500549bSAnthony Wilson                                             // Store LED path in inventory item
1264711ac7a9SEd Tanous                                             const std::string& ledPath =
1265711ac7a9SEd Tanous                                                 endpoints->front();
1266bd79bce8SPatrick Williams                                             inventoryItem.ledObjectPath =
1267bd79bce8SPatrick Williams                                                 ledPath;
1268d500549bSAnthony Wilson                                         }
1269d500549bSAnthony Wilson                                     }
1270d500549bSAnthony Wilson                                 }
1271711ac7a9SEd Tanous                             }
1272711ac7a9SEd Tanous                         }
1273711ac7a9SEd Tanous 
1274d500549bSAnthony Wilson                         break;
1275d500549bSAnthony Wilson                     }
1276d500549bSAnthony Wilson                 }
1277d500549bSAnthony Wilson             }
1278adc4f0dbSShawn McCarney             callback(inventoryItems);
127962598e31SEd Tanous             BMCWEB_LOG_DEBUG("getInventoryItemAssociations respHandler exit");
12805eb468daSGeorge Liu         });
12818fb49dd6SShawn McCarney 
128262598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryItemAssociations exit");
12838fb49dd6SShawn McCarney }
12848fb49dd6SShawn McCarney 
12858fb49dd6SShawn McCarney /**
1286d500549bSAnthony Wilson  * @brief Gets D-Bus data for inventory item leds associated with sensors.
1287d500549bSAnthony Wilson  *
1288d500549bSAnthony Wilson  * Uses the specified connections (services) to obtain D-Bus data for inventory
1289d500549bSAnthony Wilson  * item leds associated with sensors.  Stores the resulting data in the
1290d500549bSAnthony Wilson  * inventoryItems vector.
1291d500549bSAnthony Wilson  *
1292d500549bSAnthony Wilson  * This data is later used to provide sensor property values in the JSON
1293d500549bSAnthony Wilson  * response.
1294d500549bSAnthony Wilson  *
1295d500549bSAnthony Wilson  * Finds the inventory item led data asynchronously.  Invokes callback when data
1296d500549bSAnthony Wilson  * has been obtained.
1297d500549bSAnthony Wilson  *
1298d500549bSAnthony Wilson  * The callback must have the following signature:
1299d500549bSAnthony Wilson  *   @code
130042cbe538SGunnar Mills  *   callback()
1301d500549bSAnthony Wilson  *   @endcode
1302d500549bSAnthony Wilson  *
1303d500549bSAnthony Wilson  * This function is called recursively, obtaining data asynchronously from one
1304d500549bSAnthony Wilson  * connection in each call.  This ensures the callback is not invoked until the
1305d500549bSAnthony Wilson  * last asynchronous function has completed.
1306d500549bSAnthony Wilson  *
1307d500549bSAnthony Wilson  * @param sensorsAsyncResp Pointer to object holding response data.
1308d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1309d500549bSAnthony Wilson  * @param ledConnections Connections that provide data for the inventory leds.
1310d500549bSAnthony Wilson  * @param callback Callback to invoke when inventory data has been obtained.
1311d500549bSAnthony Wilson  * @param ledConnectionsIndex Current index in ledConnections.  Only specified
1312d500549bSAnthony Wilson  * in recursive calls to this function.
1313d500549bSAnthony Wilson  */
1314d500549bSAnthony Wilson template <typename Callback>
1315d500549bSAnthony Wilson void getInventoryLedData(
1316daadfb2eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1317daadfb2eSEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1318daadfb2eSEd Tanous     const std::shared_ptr<std::map<std::string, std::string>>& ledConnections,
1319d500549bSAnthony Wilson     Callback&& callback, size_t ledConnectionsIndex = 0)
1320d500549bSAnthony Wilson {
132162598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryLedData enter");
1322d500549bSAnthony Wilson 
1323d500549bSAnthony Wilson     // If no more connections left, call callback
1324d500549bSAnthony Wilson     if (ledConnectionsIndex >= ledConnections->size())
1325d500549bSAnthony Wilson     {
132642cbe538SGunnar Mills         callback();
132762598e31SEd Tanous         BMCWEB_LOG_DEBUG("getInventoryLedData exit");
1328d500549bSAnthony Wilson         return;
1329d500549bSAnthony Wilson     }
1330d500549bSAnthony Wilson 
1331d500549bSAnthony Wilson     // Get inventory item data from current connection
1332fe04d49cSNan Zhou     auto it = ledConnections->begin();
1333fe04d49cSNan Zhou     std::advance(it, ledConnectionsIndex);
1334d500549bSAnthony Wilson     if (it != ledConnections->end())
1335d500549bSAnthony Wilson     {
1336d500549bSAnthony Wilson         const std::string& ledPath = (*it).first;
1337d500549bSAnthony Wilson         const std::string& ledConnection = (*it).second;
1338d500549bSAnthony Wilson         // Response handler for Get State property
13391e1e598dSJonathan Doman         auto respHandler =
13401e1e598dSJonathan Doman             [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
13414e0d8789SEd Tanous              callback = std::forward<Callback>(callback),
13424e0d8789SEd Tanous              ledConnectionsIndex](const boost::system::error_code& ec,
13434e0d8789SEd Tanous                                   const std::string& state) mutable {
134462598e31SEd Tanous                 BMCWEB_LOG_DEBUG("getInventoryLedData respHandler enter");
1345d500549bSAnthony Wilson                 if (ec)
1346d500549bSAnthony Wilson                 {
134762598e31SEd Tanous                     BMCWEB_LOG_ERROR(
134862598e31SEd Tanous                         "getInventoryLedData respHandler DBus error {}", ec);
13498d1b46d7Szhanghch05                     messages::internalError(sensorsAsyncResp->asyncResp->res);
1350d500549bSAnthony Wilson                     return;
1351d500549bSAnthony Wilson                 }
1352d500549bSAnthony Wilson 
135362598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Led state: {}", state);
1354d500549bSAnthony Wilson                 // Find inventory item with this LED object path
1355d500549bSAnthony Wilson                 InventoryItem* inventoryItem =
1356d500549bSAnthony Wilson                     findInventoryItemForLed(*inventoryItems, ledPath);
1357d500549bSAnthony Wilson                 if (inventoryItem != nullptr)
1358d500549bSAnthony Wilson                 {
1359d500549bSAnthony Wilson                     // Store LED state in InventoryItem
136011ba3979SEd Tanous                     if (state.ends_with("On"))
1361d500549bSAnthony Wilson                     {
1362c9563608SJanet Adkins                         inventoryItem->ledState = sensor_utils::LedState::ON;
1363d500549bSAnthony Wilson                     }
136411ba3979SEd Tanous                     else if (state.ends_with("Blink"))
1365d500549bSAnthony Wilson                     {
1366c9563608SJanet Adkins                         inventoryItem->ledState = sensor_utils::LedState::BLINK;
1367d500549bSAnthony Wilson                     }
136811ba3979SEd Tanous                     else if (state.ends_with("Off"))
1369d500549bSAnthony Wilson                     {
1370c9563608SJanet Adkins                         inventoryItem->ledState = sensor_utils::LedState::OFF;
1371d500549bSAnthony Wilson                     }
1372d500549bSAnthony Wilson                     else
1373d500549bSAnthony Wilson                     {
1374c9563608SJanet Adkins                         inventoryItem->ledState =
1375c9563608SJanet Adkins                             sensor_utils::LedState::UNKNOWN;
1376d500549bSAnthony Wilson                     }
1377d500549bSAnthony Wilson                 }
1378d500549bSAnthony Wilson 
1379d500549bSAnthony Wilson                 // Recurse to get LED data from next connection
1380d500549bSAnthony Wilson                 getInventoryLedData(sensorsAsyncResp, inventoryItems,
1381d500549bSAnthony Wilson                                     ledConnections, std::move(callback),
1382d500549bSAnthony Wilson                                     ledConnectionsIndex + 1);
1383d500549bSAnthony Wilson 
138462598e31SEd Tanous                 BMCWEB_LOG_DEBUG("getInventoryLedData respHandler exit");
1385d500549bSAnthony Wilson             };
1386d500549bSAnthony Wilson 
1387d500549bSAnthony Wilson         // Get the State property for the current LED
1388deae6a78SEd Tanous         dbus::utility::getProperty<std::string>(
1389deae6a78SEd Tanous             ledConnection, ledPath, "xyz.openbmc_project.Led.Physical", "State",
13901e1e598dSJonathan Doman             std::move(respHandler));
1391d500549bSAnthony Wilson     }
1392d500549bSAnthony Wilson 
139362598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryLedData exit");
1394d500549bSAnthony Wilson }
1395d500549bSAnthony Wilson 
1396d500549bSAnthony Wilson /**
1397d500549bSAnthony Wilson  * @brief Gets LED data for LEDs associated with given inventory items.
1398d500549bSAnthony Wilson  *
1399d500549bSAnthony Wilson  * Gets the D-Bus connections (services) that provide LED data for the LEDs
1400d500549bSAnthony Wilson  * associated with the specified inventory items.  Then gets the LED data from
1401d500549bSAnthony Wilson  * each connection and stores it in the inventory item.
1402d500549bSAnthony Wilson  *
1403d500549bSAnthony Wilson  * This data is later used to provide sensor property values in the JSON
1404d500549bSAnthony Wilson  * response.
1405d500549bSAnthony Wilson  *
1406d500549bSAnthony Wilson  * Finds the LED data asynchronously.  Invokes callback when information has
1407d500549bSAnthony Wilson  * been obtained.
1408d500549bSAnthony Wilson  *
1409d500549bSAnthony Wilson  * The callback must have the following signature:
1410d500549bSAnthony Wilson  *   @code
141142cbe538SGunnar Mills  *   callback()
1412d500549bSAnthony Wilson  *   @endcode
1413d500549bSAnthony Wilson  *
1414d500549bSAnthony Wilson  * @param sensorsAsyncResp Pointer to object holding response data.
1415d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1416d500549bSAnthony Wilson  * @param callback Callback to invoke when inventory items have been obtained.
1417d500549bSAnthony Wilson  */
1418d500549bSAnthony Wilson template <typename Callback>
1419d500549bSAnthony Wilson void getInventoryLeds(
1420daadfb2eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1421daadfb2eSEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1422d500549bSAnthony Wilson     Callback&& callback)
1423d500549bSAnthony Wilson {
142462598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryLeds enter");
1425d500549bSAnthony Wilson 
1426d500549bSAnthony Wilson     const std::string path = "/xyz/openbmc_project";
1427e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
1428d500549bSAnthony Wilson         "xyz.openbmc_project.Led.Physical"};
1429d500549bSAnthony Wilson 
1430e99073f5SGeorge Liu     // Make call to ObjectMapper to find all inventory items
1431e99073f5SGeorge Liu     dbus::utility::getSubTree(
1432e99073f5SGeorge Liu         path, 0, interfaces,
14338cb2c024SEd Tanous         [callback = std::forward<Callback>(callback), sensorsAsyncResp,
1434002d39b4SEd Tanous          inventoryItems](
1435e99073f5SGeorge Liu             const boost::system::error_code& ec,
14364e0d8789SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) mutable {
1437e99073f5SGeorge Liu             // Response handler for parsing output from GetSubTree
143862598e31SEd Tanous             BMCWEB_LOG_DEBUG("getInventoryLeds respHandler enter");
1439d500549bSAnthony Wilson             if (ec)
1440d500549bSAnthony Wilson             {
14418d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
1442bd79bce8SPatrick Williams                 BMCWEB_LOG_ERROR("getInventoryLeds respHandler DBus error {}",
1443bd79bce8SPatrick Williams                                  ec);
1444d500549bSAnthony Wilson                 return;
1445d500549bSAnthony Wilson             }
1446d500549bSAnthony Wilson 
1447d500549bSAnthony Wilson             // Build map of LED object paths to connections
1448fe04d49cSNan Zhou             std::shared_ptr<std::map<std::string, std::string>> ledConnections =
1449fe04d49cSNan Zhou                 std::make_shared<std::map<std::string, std::string>>();
1450d500549bSAnthony Wilson 
1451d500549bSAnthony Wilson             // Loop through objects from GetSubTree
1452bd79bce8SPatrick Williams             for (const std::pair<std::string,
1453bd79bce8SPatrick Williams                                  std::vector<std::pair<
1454bd79bce8SPatrick Williams                                      std::string, std::vector<std::string>>>>&
1455d500549bSAnthony Wilson                      object : subtree)
1456d500549bSAnthony Wilson             {
1457bd79bce8SPatrick Williams                 // Check if object path is LED for one of the specified
1458bd79bce8SPatrick Williams                 // inventory items
1459d500549bSAnthony Wilson                 const std::string& ledPath = object.first;
1460bd79bce8SPatrick Williams                 if (findInventoryItemForLed(*inventoryItems, ledPath) !=
1461bd79bce8SPatrick Williams                     nullptr)
1462d500549bSAnthony Wilson                 {
1463d500549bSAnthony Wilson                     // Add mapping from ledPath to connection
1464bd79bce8SPatrick Williams                     const std::string& connection =
1465bd79bce8SPatrick Williams                         object.second.begin()->first;
1466d500549bSAnthony Wilson                     (*ledConnections)[ledPath] = connection;
1467bd79bce8SPatrick Williams                     BMCWEB_LOG_DEBUG("Added mapping {} -> {}", ledPath,
1468bd79bce8SPatrick Williams                                      connection);
1469d500549bSAnthony Wilson                 }
1470d500549bSAnthony Wilson             }
1471d500549bSAnthony Wilson 
1472bd79bce8SPatrick Williams             getInventoryLedData(sensorsAsyncResp, inventoryItems,
1473bd79bce8SPatrick Williams                                 ledConnections, std::move(callback));
147462598e31SEd Tanous             BMCWEB_LOG_DEBUG("getInventoryLeds respHandler exit");
1475e99073f5SGeorge Liu         });
147662598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryLeds exit");
1477d500549bSAnthony Wilson }
1478d500549bSAnthony Wilson 
1479d500549bSAnthony Wilson /**
148042cbe538SGunnar Mills  * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
148142cbe538SGunnar Mills  *
148242cbe538SGunnar Mills  * Uses the specified connections (services) (currently assumes just one) to
148342cbe538SGunnar Mills  * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
148442cbe538SGunnar Mills  * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
148542cbe538SGunnar Mills  *
148642cbe538SGunnar Mills  * This data is later used to provide sensor property values in the JSON
148742cbe538SGunnar Mills  * response.
148842cbe538SGunnar Mills  *
148942cbe538SGunnar Mills  * Finds the Power Supply Attributes data asynchronously.  Invokes callback
149042cbe538SGunnar Mills  * when data has been obtained.
149142cbe538SGunnar Mills  *
149242cbe538SGunnar Mills  * The callback must have the following signature:
149342cbe538SGunnar Mills  *   @code
149442cbe538SGunnar Mills  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
149542cbe538SGunnar Mills  *   @endcode
149642cbe538SGunnar Mills  *
149742cbe538SGunnar Mills  * @param sensorsAsyncResp Pointer to object holding response data.
149842cbe538SGunnar Mills  * @param inventoryItems D-Bus inventory items associated with sensors.
149942cbe538SGunnar Mills  * @param psAttributesConnections Connections that provide data for the Power
150042cbe538SGunnar Mills  *        Supply Attributes
150142cbe538SGunnar Mills  * @param callback Callback to invoke when data has been obtained.
150242cbe538SGunnar Mills  */
150342cbe538SGunnar Mills template <typename Callback>
150442cbe538SGunnar Mills void getPowerSupplyAttributesData(
1505b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1506daadfb2eSEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1507fe04d49cSNan Zhou     const std::map<std::string, std::string>& psAttributesConnections,
150842cbe538SGunnar Mills     Callback&& callback)
150942cbe538SGunnar Mills {
151062598e31SEd Tanous     BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData enter");
151142cbe538SGunnar Mills 
151242cbe538SGunnar Mills     if (psAttributesConnections.empty())
151342cbe538SGunnar Mills     {
151462598e31SEd Tanous         BMCWEB_LOG_DEBUG("Can't find PowerSupplyAttributes, no connections!");
151542cbe538SGunnar Mills         callback(inventoryItems);
151642cbe538SGunnar Mills         return;
151742cbe538SGunnar Mills     }
151842cbe538SGunnar Mills 
151942cbe538SGunnar Mills     // Assuming just one connection (service) for now
1520fe04d49cSNan Zhou     auto it = psAttributesConnections.begin();
152142cbe538SGunnar Mills 
152242cbe538SGunnar Mills     const std::string& psAttributesPath = (*it).first;
152342cbe538SGunnar Mills     const std::string& psAttributesConnection = (*it).second;
152442cbe538SGunnar Mills 
152542cbe538SGunnar Mills     // Response handler for Get DeratingFactor property
15265a39f77aSPatrick Williams     auto respHandler = [sensorsAsyncResp, inventoryItems,
15278cb2c024SEd Tanous                         callback = std::forward<Callback>(callback)](
15285a39f77aSPatrick Williams                            const boost::system::error_code& ec,
15294e0d8789SEd Tanous                            uint32_t value) mutable {
153062598e31SEd Tanous         BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData respHandler enter");
153142cbe538SGunnar Mills         if (ec)
153242cbe538SGunnar Mills         {
153362598e31SEd Tanous             BMCWEB_LOG_ERROR(
153462598e31SEd Tanous                 "getPowerSupplyAttributesData respHandler DBus error {}", ec);
15358d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
153642cbe538SGunnar Mills             return;
153742cbe538SGunnar Mills         }
153842cbe538SGunnar Mills 
153962598e31SEd Tanous         BMCWEB_LOG_DEBUG("PS EfficiencyPercent value: {}", value);
154042cbe538SGunnar Mills         // Store value in Power Supply Inventory Items
154142cbe538SGunnar Mills         for (InventoryItem& inventoryItem : *inventoryItems)
154242cbe538SGunnar Mills         {
154355f79e6fSEd Tanous             if (inventoryItem.isPowerSupply)
154442cbe538SGunnar Mills             {
154542cbe538SGunnar Mills                 inventoryItem.powerSupplyEfficiencyPercent =
15461e1e598dSJonathan Doman                     static_cast<int>(value);
154742cbe538SGunnar Mills             }
154842cbe538SGunnar Mills         }
154942cbe538SGunnar Mills 
155062598e31SEd Tanous         BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData respHandler exit");
155142cbe538SGunnar Mills         callback(inventoryItems);
155242cbe538SGunnar Mills     };
155342cbe538SGunnar Mills 
155442cbe538SGunnar Mills     // Get the DeratingFactor property for the PowerSupplyAttributes
155542cbe538SGunnar Mills     // Currently only property on the interface/only one we care about
1556deae6a78SEd Tanous     dbus::utility::getProperty<uint32_t>(
1557deae6a78SEd Tanous         psAttributesConnection, psAttributesPath,
15581e1e598dSJonathan Doman         "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor",
15591e1e598dSJonathan Doman         std::move(respHandler));
156042cbe538SGunnar Mills 
156162598e31SEd Tanous     BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData exit");
156242cbe538SGunnar Mills }
156342cbe538SGunnar Mills 
156442cbe538SGunnar Mills /**
156542cbe538SGunnar Mills  * @brief Gets the Power Supply Attributes such as EfficiencyPercent
156642cbe538SGunnar Mills  *
156742cbe538SGunnar Mills  * Gets the D-Bus connection (service) that provides Power Supply Attributes
156842cbe538SGunnar Mills  * data. Then gets the Power Supply Attributes data from the connection
156942cbe538SGunnar Mills  * (currently just assumes 1 connection) and stores the data in the inventory
157042cbe538SGunnar Mills  * item.
157142cbe538SGunnar Mills  *
157242cbe538SGunnar Mills  * This data is later used to provide sensor property values in the JSON
157342cbe538SGunnar Mills  * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
157442cbe538SGunnar Mills  *
157542cbe538SGunnar Mills  * Finds the Power Supply Attributes data asynchronously. Invokes callback
157642cbe538SGunnar Mills  * when information has been obtained.
157742cbe538SGunnar Mills  *
157842cbe538SGunnar Mills  * The callback must have the following signature:
157942cbe538SGunnar Mills  *   @code
158042cbe538SGunnar Mills  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
158142cbe538SGunnar Mills  *   @endcode
158242cbe538SGunnar Mills  *
158342cbe538SGunnar Mills  * @param sensorsAsyncResp Pointer to object holding response data.
158442cbe538SGunnar Mills  * @param inventoryItems D-Bus inventory items associated with sensors.
158542cbe538SGunnar Mills  * @param callback Callback to invoke when data has been obtained.
158642cbe538SGunnar Mills  */
158742cbe538SGunnar Mills template <typename Callback>
158842cbe538SGunnar Mills void getPowerSupplyAttributes(
1589daadfb2eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1590daadfb2eSEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
159142cbe538SGunnar Mills     Callback&& callback)
159242cbe538SGunnar Mills {
159362598e31SEd Tanous     BMCWEB_LOG_DEBUG("getPowerSupplyAttributes enter");
159442cbe538SGunnar Mills 
159542cbe538SGunnar Mills     // Only need the power supply attributes when the Power Schema
15960c728b42SJanet Adkins     if (sensorsAsyncResp->chassisSubNode != sensors::powerNodeStr)
159742cbe538SGunnar Mills     {
159862598e31SEd Tanous         BMCWEB_LOG_DEBUG("getPowerSupplyAttributes exit since not Power");
159942cbe538SGunnar Mills         callback(inventoryItems);
160042cbe538SGunnar Mills         return;
160142cbe538SGunnar Mills     }
160242cbe538SGunnar Mills 
1603e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
160442cbe538SGunnar Mills         "xyz.openbmc_project.Control.PowerSupplyAttributes"};
160542cbe538SGunnar Mills 
1606e99073f5SGeorge Liu     // Make call to ObjectMapper to find the PowerSupplyAttributes service
1607e99073f5SGeorge Liu     dbus::utility::getSubTree(
1608e99073f5SGeorge Liu         "/xyz/openbmc_project", 0, interfaces,
16098cb2c024SEd Tanous         [callback = std::forward<Callback>(callback), sensorsAsyncResp,
1610b9d36b47SEd Tanous          inventoryItems](
1611e99073f5SGeorge Liu             const boost::system::error_code& ec,
16124e0d8789SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) mutable {
1613e99073f5SGeorge Liu             // Response handler for parsing output from GetSubTree
161462598e31SEd Tanous             BMCWEB_LOG_DEBUG("getPowerSupplyAttributes respHandler enter");
161542cbe538SGunnar Mills             if (ec)
161642cbe538SGunnar Mills             {
16178d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
161862598e31SEd Tanous                 BMCWEB_LOG_ERROR(
161962598e31SEd Tanous                     "getPowerSupplyAttributes respHandler DBus error {}", ec);
162042cbe538SGunnar Mills                 return;
162142cbe538SGunnar Mills             }
162226f6976fSEd Tanous             if (subtree.empty())
162342cbe538SGunnar Mills             {
162462598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Can't find Power Supply Attributes!");
162542cbe538SGunnar Mills                 callback(inventoryItems);
162642cbe538SGunnar Mills                 return;
162742cbe538SGunnar Mills             }
162842cbe538SGunnar Mills 
162942cbe538SGunnar Mills             // Currently we only support 1 power supply attribute, use this for
163042cbe538SGunnar Mills             // all the power supplies. Build map of object path to connection.
163142cbe538SGunnar Mills             // Assume just 1 connection and 1 path for now.
1632fe04d49cSNan Zhou             std::map<std::string, std::string> psAttributesConnections;
163342cbe538SGunnar Mills 
163442cbe538SGunnar Mills             if (subtree[0].first.empty() || subtree[0].second.empty())
163542cbe538SGunnar Mills             {
163662598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Power Supply Attributes mapper error!");
163742cbe538SGunnar Mills                 callback(inventoryItems);
163842cbe538SGunnar Mills                 return;
163942cbe538SGunnar Mills             }
164042cbe538SGunnar Mills 
164142cbe538SGunnar Mills             const std::string& psAttributesPath = subtree[0].first;
164242cbe538SGunnar Mills             const std::string& connection = subtree[0].second.begin()->first;
164342cbe538SGunnar Mills 
164442cbe538SGunnar Mills             if (connection.empty())
164542cbe538SGunnar Mills             {
164662598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Power Supply Attributes mapper error!");
164742cbe538SGunnar Mills                 callback(inventoryItems);
164842cbe538SGunnar Mills                 return;
164942cbe538SGunnar Mills             }
165042cbe538SGunnar Mills 
165142cbe538SGunnar Mills             psAttributesConnections[psAttributesPath] = connection;
165262598e31SEd Tanous             BMCWEB_LOG_DEBUG("Added mapping {} -> {}", psAttributesPath,
165362598e31SEd Tanous                              connection);
165442cbe538SGunnar Mills 
165542cbe538SGunnar Mills             getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
165642cbe538SGunnar Mills                                          psAttributesConnections,
165742cbe538SGunnar Mills                                          std::move(callback));
165862598e31SEd Tanous             BMCWEB_LOG_DEBUG("getPowerSupplyAttributes respHandler exit");
1659e99073f5SGeorge Liu         });
166062598e31SEd Tanous     BMCWEB_LOG_DEBUG("getPowerSupplyAttributes exit");
166142cbe538SGunnar Mills }
166242cbe538SGunnar Mills 
166342cbe538SGunnar Mills /**
1664adc4f0dbSShawn McCarney  * @brief Gets inventory items associated with sensors.
16658fb49dd6SShawn McCarney  *
16668fb49dd6SShawn McCarney  * Finds the inventory items that are associated with the specified sensors.
1667adc4f0dbSShawn McCarney  * Then gets D-Bus data for the inventory items, such as presence and VPD.
16688fb49dd6SShawn McCarney  *
1669adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
1670adc4f0dbSShawn McCarney  * response.
16718fb49dd6SShawn McCarney  *
1672adc4f0dbSShawn McCarney  * Finds the inventory items asynchronously.  Invokes callback when the
1673adc4f0dbSShawn McCarney  * inventory items have been obtained.
1674adc4f0dbSShawn McCarney  *
1675adc4f0dbSShawn McCarney  * The callback must have the following signature:
1676adc4f0dbSShawn McCarney  *   @code
1677adc4f0dbSShawn McCarney  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
1678adc4f0dbSShawn McCarney  *   @endcode
16798fb49dd6SShawn McCarney  *
16808fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
16818fb49dd6SShawn McCarney  * @param sensorNames All sensors within the current chassis.
16828fb49dd6SShawn McCarney  * implements ObjectManager.
1683adc4f0dbSShawn McCarney  * @param callback Callback to invoke when inventory items have been obtained.
16848fb49dd6SShawn McCarney  */
1685adc4f0dbSShawn McCarney template <typename Callback>
1686504af5a0SPatrick Williams inline void getInventoryItems(
1687504af5a0SPatrick Williams     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1688daadfb2eSEd Tanous     const std::shared_ptr<std::set<std::string>>& sensorNames,
1689adc4f0dbSShawn McCarney     Callback&& callback)
16908fb49dd6SShawn McCarney {
169162598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryItems enter");
1692adc4f0dbSShawn McCarney     auto getInventoryItemAssociationsCb =
16938cb2c024SEd Tanous         [sensorsAsyncResp, callback = std::forward<Callback>(callback)](
1694daadfb2eSEd Tanous             const std::shared_ptr<std::vector<InventoryItem>>&
16954e0d8789SEd Tanous                 inventoryItems) mutable {
169662598e31SEd Tanous             BMCWEB_LOG_DEBUG("getInventoryItemAssociationsCb enter");
16978fb49dd6SShawn McCarney             auto getInventoryItemsConnectionsCb =
1698d0090733SEd Tanous                 [sensorsAsyncResp, inventoryItems,
16994e0d8789SEd Tanous                  callback = std::forward<Callback>(callback)](
1700daadfb2eSEd Tanous                     const std::shared_ptr<std::set<std::string>>&
17014e0d8789SEd Tanous                         invConnections) mutable {
170262598e31SEd Tanous                     BMCWEB_LOG_DEBUG("getInventoryItemsConnectionsCb enter");
1703bd79bce8SPatrick Williams                     auto getInventoryItemsDataCb =
1704bd79bce8SPatrick Williams                         [sensorsAsyncResp, inventoryItems,
17054e0d8789SEd Tanous                          callback =
17064e0d8789SEd Tanous                              std::forward<Callback>(callback)]() mutable {
170762598e31SEd Tanous                             BMCWEB_LOG_DEBUG("getInventoryItemsDataCb enter");
170842cbe538SGunnar Mills 
1709bd79bce8SPatrick Williams                             auto getInventoryLedsCb =
1710bd79bce8SPatrick Williams                                 [sensorsAsyncResp, inventoryItems,
17114e0d8789SEd Tanous                                  callback = std::forward<Callback>(
17124e0d8789SEd Tanous                                      callback)]() mutable {
1713bd79bce8SPatrick Williams                                     BMCWEB_LOG_DEBUG(
1714bd79bce8SPatrick Williams                                         "getInventoryLedsCb enter");
1715bd79bce8SPatrick Williams                                     // Find Power Supply Attributes and get the
1716bd79bce8SPatrick Williams                                     // data
1717bd79bce8SPatrick Williams                                     getPowerSupplyAttributes(
1718bd79bce8SPatrick Williams                                         sensorsAsyncResp, inventoryItems,
171942cbe538SGunnar Mills                                         std::move(callback));
172062598e31SEd Tanous                                     BMCWEB_LOG_DEBUG("getInventoryLedsCb exit");
172142cbe538SGunnar Mills                                 };
172242cbe538SGunnar Mills 
1723d500549bSAnthony Wilson                             // Find led connections and get the data
1724d500549bSAnthony Wilson                             getInventoryLeds(sensorsAsyncResp, inventoryItems,
172542cbe538SGunnar Mills                                              std::move(getInventoryLedsCb));
172662598e31SEd Tanous                             BMCWEB_LOG_DEBUG("getInventoryItemsDataCb exit");
1727d500549bSAnthony Wilson                         };
17288fb49dd6SShawn McCarney 
1729adc4f0dbSShawn McCarney                     // Get inventory item data from connections
1730adc4f0dbSShawn McCarney                     getInventoryItemsData(sensorsAsyncResp, inventoryItems,
1731d0090733SEd Tanous                                           invConnections,
1732d500549bSAnthony Wilson                                           std::move(getInventoryItemsDataCb));
173362598e31SEd Tanous                     BMCWEB_LOG_DEBUG("getInventoryItemsConnectionsCb exit");
17348fb49dd6SShawn McCarney                 };
17358fb49dd6SShawn McCarney 
1736adc4f0dbSShawn McCarney             // Get connections that provide inventory item data
1737bd79bce8SPatrick Williams             getInventoryItemsConnections(
1738bd79bce8SPatrick Williams                 sensorsAsyncResp, inventoryItems,
17398fb49dd6SShawn McCarney                 std::move(getInventoryItemsConnectionsCb));
174062598e31SEd Tanous             BMCWEB_LOG_DEBUG("getInventoryItemAssociationsCb exit");
17418fb49dd6SShawn McCarney         };
17428fb49dd6SShawn McCarney 
1743adc4f0dbSShawn McCarney     // Get associations from sensors to inventory items
1744d0090733SEd Tanous     getInventoryItemAssociations(sensorsAsyncResp, sensorNames,
1745adc4f0dbSShawn McCarney                                  std::move(getInventoryItemAssociationsCb));
174662598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryItems exit");
1747adc4f0dbSShawn McCarney }
1748adc4f0dbSShawn McCarney 
1749adc4f0dbSShawn McCarney /**
1750adc4f0dbSShawn McCarney  * @brief Returns JSON PowerSupply object for the specified inventory item.
1751adc4f0dbSShawn McCarney  *
1752adc4f0dbSShawn McCarney  * Searches for a JSON PowerSupply object that matches the specified inventory
1753adc4f0dbSShawn McCarney  * item.  If one is not found, a new PowerSupply object is added to the JSON
1754adc4f0dbSShawn McCarney  * array.
1755adc4f0dbSShawn McCarney  *
1756adc4f0dbSShawn McCarney  * Multiple sensors are often associated with one power supply inventory item.
1757adc4f0dbSShawn McCarney  * As a result, multiple sensor values are stored in one JSON PowerSupply
1758adc4f0dbSShawn McCarney  * object.
1759adc4f0dbSShawn McCarney  *
1760adc4f0dbSShawn McCarney  * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
1761adc4f0dbSShawn McCarney  * @param inventoryItem Inventory item for the power supply.
1762adc4f0dbSShawn McCarney  * @param chassisId Chassis that contains the power supply.
1763adc4f0dbSShawn McCarney  * @return JSON PowerSupply object for the specified inventory item.
1764adc4f0dbSShawn McCarney  */
176523a21a1cSEd Tanous inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
1766adc4f0dbSShawn McCarney                                       const InventoryItem& inventoryItem,
1767adc4f0dbSShawn McCarney                                       const std::string& chassisId)
1768adc4f0dbSShawn McCarney {
176918f8f608SEd Tanous     std::string nameS;
17706f4bd290SAlexander Hansen     nameS.resize(inventoryItem.name.size());
177118f8f608SEd Tanous     std::ranges::replace_copy(inventoryItem.name, nameS.begin(), '_', ' ');
1772adc4f0dbSShawn McCarney     // Check if matching PowerSupply object already exists in JSON array
1773adc4f0dbSShawn McCarney     for (nlohmann::json& powerSupply : powerSupplyArray)
1774adc4f0dbSShawn McCarney     {
177518f8f608SEd Tanous         nlohmann::json::iterator nameIt = powerSupply.find("Name");
177618f8f608SEd Tanous         if (nameIt == powerSupply.end())
177718f8f608SEd Tanous         {
177818f8f608SEd Tanous             continue;
177918f8f608SEd Tanous         }
178018f8f608SEd Tanous         const std::string* name = nameIt->get_ptr<std::string*>();
178118f8f608SEd Tanous         if (name == nullptr)
178218f8f608SEd Tanous         {
178318f8f608SEd Tanous             continue;
178418f8f608SEd Tanous         }
178518f8f608SEd Tanous         if (nameS == *name)
1786adc4f0dbSShawn McCarney         {
1787adc4f0dbSShawn McCarney             return powerSupply;
1788adc4f0dbSShawn McCarney         }
1789adc4f0dbSShawn McCarney     }
1790adc4f0dbSShawn McCarney 
1791adc4f0dbSShawn McCarney     // Add new PowerSupply object to JSON array
1792adc4f0dbSShawn McCarney     powerSupplyArray.push_back({});
1793adc4f0dbSShawn McCarney     nlohmann::json& powerSupply = powerSupplyArray.back();
1794bd79bce8SPatrick Williams     boost::urls::url url =
1795bd79bce8SPatrick Williams         boost::urls::format("/redfish/v1/Chassis/{}/Power", chassisId);
1796eddfc437SWilly Tu     url.set_fragment(("/PowerSupplies"_json_pointer).to_string());
1797eddfc437SWilly Tu     powerSupply["@odata.id"] = std::move(url);
179818f8f608SEd Tanous     std::string escaped;
17996f4bd290SAlexander Hansen     escaped.resize(inventoryItem.name.size());
180018f8f608SEd Tanous     std::ranges::replace_copy(inventoryItem.name, escaped.begin(), '_', ' ');
180118f8f608SEd Tanous     powerSupply["Name"] = std::move(escaped);
1802adc4f0dbSShawn McCarney     powerSupply["Manufacturer"] = inventoryItem.manufacturer;
1803adc4f0dbSShawn McCarney     powerSupply["Model"] = inventoryItem.model;
1804adc4f0dbSShawn McCarney     powerSupply["PartNumber"] = inventoryItem.partNumber;
1805adc4f0dbSShawn McCarney     powerSupply["SerialNumber"] = inventoryItem.serialNumber;
1806c9563608SJanet Adkins     sensor_utils::setLedState(powerSupply, &inventoryItem);
1807adc4f0dbSShawn McCarney 
180842cbe538SGunnar Mills     if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
180942cbe538SGunnar Mills     {
181042cbe538SGunnar Mills         powerSupply["EfficiencyPercent"] =
181142cbe538SGunnar Mills             inventoryItem.powerSupplyEfficiencyPercent;
181242cbe538SGunnar Mills     }
181342cbe538SGunnar Mills 
1814c9563608SJanet Adkins     powerSupply["Status"]["State"] =
1815c9563608SJanet Adkins         sensor_utils::getState(&inventoryItem, true);
1816adc4f0dbSShawn McCarney     const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
1817adc4f0dbSShawn McCarney     powerSupply["Status"]["Health"] = health;
1818adc4f0dbSShawn McCarney 
1819adc4f0dbSShawn McCarney     return powerSupply;
18208fb49dd6SShawn McCarney }
18218fb49dd6SShawn McCarney 
18228fb49dd6SShawn McCarney /**
1823de629b6eSShawn McCarney  * @brief Gets the values of the specified sensors.
1824de629b6eSShawn McCarney  *
1825de629b6eSShawn McCarney  * Stores the results as JSON in the SensorsAsyncResp.
1826de629b6eSShawn McCarney  *
1827de629b6eSShawn McCarney  * Gets the sensor values asynchronously.  Stores the results later when the
1828de629b6eSShawn McCarney  * information has been obtained.
1829de629b6eSShawn McCarney  *
1830adc4f0dbSShawn McCarney  * The sensorNames set contains all requested sensors for the current chassis.
1831de629b6eSShawn McCarney  *
1832de629b6eSShawn McCarney  * To minimize the number of DBus calls, the DBus method
1833de629b6eSShawn McCarney  * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
1834de629b6eSShawn McCarney  * values of all sensors provided by a connection (service).
1835de629b6eSShawn McCarney  *
1836de629b6eSShawn McCarney  * The connections set contains all the connections that provide sensor values.
1837de629b6eSShawn McCarney  *
1838adc4f0dbSShawn McCarney  * The InventoryItem vector contains D-Bus inventory items associated with the
1839adc4f0dbSShawn McCarney  * sensors.  Inventory item data is needed for some Redfish sensor properties.
1840adc4f0dbSShawn McCarney  *
1841de629b6eSShawn McCarney  * @param SensorsAsyncResp Pointer to object holding response data.
1842adc4f0dbSShawn McCarney  * @param sensorNames All requested sensors within the current chassis.
1843de629b6eSShawn McCarney  * @param connections Connections that provide sensor values.
1844de629b6eSShawn McCarney  * implements ObjectManager.
1845adc4f0dbSShawn McCarney  * @param inventoryItems Inventory items associated with the sensors.
1846de629b6eSShawn McCarney  */
184723a21a1cSEd Tanous inline void getSensorData(
184881ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1849fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& sensorNames,
1850fe04d49cSNan Zhou     const std::set<std::string>& connections,
1851b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems)
1852de629b6eSShawn McCarney {
185362598e31SEd Tanous     BMCWEB_LOG_DEBUG("getSensorData enter");
1854de629b6eSShawn McCarney     // Get managed objects from all services exposing sensors
1855de629b6eSShawn McCarney     for (const std::string& connection : connections)
1856de629b6eSShawn McCarney     {
18575eb468daSGeorge Liu         sdbusplus::message::object_path sensorPath(
18585eb468daSGeorge Liu             "/xyz/openbmc_project/sensors");
18595eb468daSGeorge Liu         dbus::utility::getManagedObjects(
18605eb468daSGeorge Liu             connection, sensorPath,
1861002d39b4SEd Tanous             [sensorsAsyncResp, sensorNames,
18625e7e2dc5SEd Tanous              inventoryItems](const boost::system::error_code& ec,
186302cad96eSEd Tanous                              const dbus::utility::ManagedObjectType& resp) {
186462598e31SEd Tanous                 BMCWEB_LOG_DEBUG("getManagedObjectsCb enter");
1865de629b6eSShawn McCarney                 if (ec)
1866de629b6eSShawn McCarney                 {
186762598e31SEd Tanous                     BMCWEB_LOG_ERROR("getManagedObjectsCb DBUS error: {}", ec);
18688d1b46d7Szhanghch05                     messages::internalError(sensorsAsyncResp->asyncResp->res);
1869de629b6eSShawn McCarney                     return;
1870de629b6eSShawn McCarney                 }
18710c728b42SJanet Adkins                 auto chassisSubNode = sensor_utils::chassisSubNodeFromString(
18720c728b42SJanet Adkins                     sensorsAsyncResp->chassisSubNode);
1873de629b6eSShawn McCarney                 // Go through all objects and update response with sensor data
1874de629b6eSShawn McCarney                 for (const auto& objDictEntry : resp)
1875de629b6eSShawn McCarney                 {
1876de629b6eSShawn McCarney                     const std::string& objPath =
1877de629b6eSShawn McCarney                         static_cast<const std::string&>(objDictEntry.first);
187862598e31SEd Tanous                     BMCWEB_LOG_DEBUG("getManagedObjectsCb parsing object {}",
187962598e31SEd Tanous                                      objPath);
1880de629b6eSShawn McCarney 
1881de629b6eSShawn McCarney                     std::vector<std::string> split;
1882de629b6eSShawn McCarney                     // Reserve space for
1883de629b6eSShawn McCarney                     // /xyz/openbmc_project/sensors/<name>/<subname>
1884de629b6eSShawn McCarney                     split.reserve(6);
188550ebd4afSEd Tanous                     // NOLINTNEXTLINE
188650ebd4afSEd Tanous                     bmcweb::split(split, objPath, '/');
1887de629b6eSShawn McCarney                     if (split.size() < 6)
1888de629b6eSShawn McCarney                     {
188962598e31SEd Tanous                         BMCWEB_LOG_ERROR("Got path that isn't long enough {}",
189062598e31SEd Tanous                                          objPath);
1891de629b6eSShawn McCarney                         continue;
1892de629b6eSShawn McCarney                     }
189350ebd4afSEd Tanous                     // These indexes aren't intuitive, as split puts an empty
1894de629b6eSShawn McCarney                     // string at the beginning
1895de629b6eSShawn McCarney                     const std::string& sensorType = split[4];
1896de629b6eSShawn McCarney                     const std::string& sensorName = split[5];
189762598e31SEd Tanous                     BMCWEB_LOG_DEBUG("sensorName {} sensorType {}", sensorName,
189862598e31SEd Tanous                                      sensorType);
189949c53ac9SJohnathan Mantey                     if (sensorNames->find(objPath) == sensorNames->end())
1900de629b6eSShawn McCarney                     {
190162598e31SEd Tanous                         BMCWEB_LOG_DEBUG("{} not in sensor list ", sensorName);
1902de629b6eSShawn McCarney                         continue;
1903de629b6eSShawn McCarney                     }
1904de629b6eSShawn McCarney 
1905adc4f0dbSShawn McCarney                     // Find inventory item (if any) associated with sensor
1906adc4f0dbSShawn McCarney                     InventoryItem* inventoryItem =
1907adc4f0dbSShawn McCarney                         findInventoryItemForSensor(inventoryItems, objPath);
1908adc4f0dbSShawn McCarney 
190995a3ecadSAnthony Wilson                     const std::string& sensorSchema =
191081ce609eSEd Tanous                         sensorsAsyncResp->chassisSubNode;
191195a3ecadSAnthony Wilson 
191295a3ecadSAnthony Wilson                     nlohmann::json* sensorJson = nullptr;
191395a3ecadSAnthony Wilson 
19140c728b42SJanet Adkins                     if (sensorSchema == sensors::sensorsNodeStr &&
1915928fefb9SNan Zhou                         !sensorsAsyncResp->efficientExpand)
191695a3ecadSAnthony Wilson                     {
19171516c21bSJanet Adkins                         std::string sensorId =
19181516c21bSJanet Adkins                             redfish::sensor_utils::getSensorId(sensorName,
19191516c21bSJanet Adkins                                                                sensorType);
1920c1d019a6SEd Tanous 
1921bd79bce8SPatrick Williams                         sensorsAsyncResp->asyncResp->res
1922bd79bce8SPatrick Williams                             .jsonValue["@odata.id"] = boost::urls::format(
1923bd79bce8SPatrick Williams                             "/redfish/v1/Chassis/{}/{}/{}",
1924c1d019a6SEd Tanous                             sensorsAsyncResp->chassisId,
1925bd79bce8SPatrick Williams                             sensorsAsyncResp->chassisSubNode, sensorId);
1926bd79bce8SPatrick Williams                         sensorJson =
1927bd79bce8SPatrick Williams                             &(sensorsAsyncResp->asyncResp->res.jsonValue);
192895a3ecadSAnthony Wilson                     }
192995a3ecadSAnthony Wilson                     else
193095a3ecadSAnthony Wilson                     {
1931271584abSEd Tanous                         std::string fieldName;
1932928fefb9SNan Zhou                         if (sensorsAsyncResp->efficientExpand)
1933928fefb9SNan Zhou                         {
1934928fefb9SNan Zhou                             fieldName = "Members";
1935928fefb9SNan Zhou                         }
1936928fefb9SNan Zhou                         else if (sensorType == "temperature")
1937de629b6eSShawn McCarney                         {
1938de629b6eSShawn McCarney                             fieldName = "Temperatures";
1939de629b6eSShawn McCarney                         }
1940bd79bce8SPatrick Williams                         else if (sensorType == "fan" ||
1941bd79bce8SPatrick Williams                                  sensorType == "fan_tach" ||
1942de629b6eSShawn McCarney                                  sensorType == "fan_pwm")
1943de629b6eSShawn McCarney                         {
1944de629b6eSShawn McCarney                             fieldName = "Fans";
1945de629b6eSShawn McCarney                         }
1946de629b6eSShawn McCarney                         else if (sensorType == "voltage")
1947de629b6eSShawn McCarney                         {
1948de629b6eSShawn McCarney                             fieldName = "Voltages";
1949de629b6eSShawn McCarney                         }
1950de629b6eSShawn McCarney                         else if (sensorType == "power")
1951de629b6eSShawn McCarney                         {
195255f79e6fSEd Tanous                             if (sensorName == "total_power")
1953028f7ebcSEddie James                             {
1954028f7ebcSEddie James                                 fieldName = "PowerControl";
1955028f7ebcSEddie James                             }
1956adc4f0dbSShawn McCarney                             else if ((inventoryItem != nullptr) &&
1957adc4f0dbSShawn McCarney                                      (inventoryItem->isPowerSupply))
1958028f7ebcSEddie James                             {
1959de629b6eSShawn McCarney                                 fieldName = "PowerSupplies";
1960de629b6eSShawn McCarney                             }
1961adc4f0dbSShawn McCarney                             else
1962adc4f0dbSShawn McCarney                             {
1963adc4f0dbSShawn McCarney                                 // Other power sensors are in SensorCollection
1964adc4f0dbSShawn McCarney                                 continue;
1965adc4f0dbSShawn McCarney                             }
1966028f7ebcSEddie James                         }
1967de629b6eSShawn McCarney                         else
1968de629b6eSShawn McCarney                         {
1969bd79bce8SPatrick Williams                             BMCWEB_LOG_ERROR(
1970bd79bce8SPatrick Williams                                 "Unsure how to handle sensorType {}",
197162598e31SEd Tanous                                 sensorType);
1972de629b6eSShawn McCarney                             continue;
1973de629b6eSShawn McCarney                         }
1974de629b6eSShawn McCarney 
1975de629b6eSShawn McCarney                         nlohmann::json& tempArray =
1976bd79bce8SPatrick Williams                             sensorsAsyncResp->asyncResp->res
1977bd79bce8SPatrick Williams                                 .jsonValue[fieldName];
1978adc4f0dbSShawn McCarney                         if (fieldName == "PowerControl")
197949c53ac9SJohnathan Mantey                         {
1980adc4f0dbSShawn McCarney                             if (tempArray.empty())
19817ab06f49SGunnar Mills                             {
198295a3ecadSAnthony Wilson                                 // Put multiple "sensors" into a single
198395a3ecadSAnthony Wilson                                 // PowerControl. Follows MemberId naming and
198495a3ecadSAnthony Wilson                                 // naming in power.hpp.
19851476687dSEd Tanous                                 nlohmann::json::object_t power;
1986ef4c65b7SEd Tanous                                 boost::urls::url url = boost::urls::format(
1987ef4c65b7SEd Tanous                                     "/redfish/v1/Chassis/{}/{}",
1988eddfc437SWilly Tu                                     sensorsAsyncResp->chassisId,
1989eddfc437SWilly Tu                                     sensorsAsyncResp->chassisSubNode);
1990bd79bce8SPatrick Williams                                 url.set_fragment(
1991bd79bce8SPatrick Williams                                     (""_json_pointer / fieldName / "0")
1992eddfc437SWilly Tu                                         .to_string());
1993eddfc437SWilly Tu                                 power["@odata.id"] = std::move(url);
1994b2ba3072SPatrick Williams                                 tempArray.emplace_back(std::move(power));
1995adc4f0dbSShawn McCarney                             }
1996adc4f0dbSShawn McCarney                             sensorJson = &(tempArray.back());
1997adc4f0dbSShawn McCarney                         }
1998adc4f0dbSShawn McCarney                         else if (fieldName == "PowerSupplies")
1999adc4f0dbSShawn McCarney                         {
2000adc4f0dbSShawn McCarney                             if (inventoryItem != nullptr)
2001adc4f0dbSShawn McCarney                             {
2002bd79bce8SPatrick Williams                                 sensorJson = &(getPowerSupply(
2003bd79bce8SPatrick Williams                                     tempArray, *inventoryItem,
200481ce609eSEd Tanous                                     sensorsAsyncResp->chassisId));
2005adc4f0dbSShawn McCarney                             }
200649c53ac9SJohnathan Mantey                         }
2007928fefb9SNan Zhou                         else if (fieldName == "Members")
2008928fefb9SNan Zhou                         {
20091516c21bSJanet Adkins                             std::string sensorId =
20101516c21bSJanet Adkins                                 redfish::sensor_utils::getSensorId(sensorName,
20111516c21bSJanet Adkins                                                                    sensorType);
2012677bb756SEd Tanous 
20131476687dSEd Tanous                             nlohmann::json::object_t member;
2014ef4c65b7SEd Tanous                             member["@odata.id"] = boost::urls::format(
2015ef4c65b7SEd Tanous                                 "/redfish/v1/Chassis/{}/{}/{}",
2016677bb756SEd Tanous                                 sensorsAsyncResp->chassisId,
2017677bb756SEd Tanous                                 sensorsAsyncResp->chassisSubNode, sensorId);
2018b2ba3072SPatrick Williams                             tempArray.emplace_back(std::move(member));
2019928fefb9SNan Zhou                             sensorJson = &(tempArray.back());
2020928fefb9SNan Zhou                         }
202149c53ac9SJohnathan Mantey                         else
202249c53ac9SJohnathan Mantey                         {
20231476687dSEd Tanous                             nlohmann::json::object_t member;
2024ef4c65b7SEd Tanous                             boost::urls::url url = boost::urls::format(
2025ef4c65b7SEd Tanous                                 "/redfish/v1/Chassis/{}/{}",
2026eddfc437SWilly Tu                                 sensorsAsyncResp->chassisId,
2027eddfc437SWilly Tu                                 sensorsAsyncResp->chassisSubNode);
2028eddfc437SWilly Tu                             url.set_fragment(
2029eddfc437SWilly Tu                                 (""_json_pointer / fieldName).to_string());
2030eddfc437SWilly Tu                             member["@odata.id"] = std::move(url);
2031b2ba3072SPatrick Williams                             tempArray.emplace_back(std::move(member));
2032adc4f0dbSShawn McCarney                             sensorJson = &(tempArray.back());
203349c53ac9SJohnathan Mantey                         }
203495a3ecadSAnthony Wilson                     }
2035de629b6eSShawn McCarney 
2036adc4f0dbSShawn McCarney                     if (sensorJson != nullptr)
2037adc4f0dbSShawn McCarney                     {
20380c728b42SJanet Adkins                         objectInterfacesToJson(
20390c728b42SJanet Adkins                             sensorName, sensorType, chassisSubNode,
20400c728b42SJanet Adkins                             objDictEntry.second, *sensorJson, inventoryItem);
20411d7c0054SEd Tanous 
20421d7c0054SEd Tanous                         std::string path = "/xyz/openbmc_project/sensors/";
20431d7c0054SEd Tanous                         path += sensorType;
20441d7c0054SEd Tanous                         path += "/";
20451d7c0054SEd Tanous                         path += sensorName;
2046c1d019a6SEd Tanous                         sensorsAsyncResp->addMetadata(*sensorJson, path);
2047adc4f0dbSShawn McCarney                     }
2048de629b6eSShawn McCarney                 }
204981ce609eSEd Tanous                 if (sensorsAsyncResp.use_count() == 1)
205049c53ac9SJohnathan Mantey                 {
205181ce609eSEd Tanous                     sortJSONResponse(sensorsAsyncResp);
20520c728b42SJanet Adkins                     if (chassisSubNode ==
20530c728b42SJanet Adkins                             sensor_utils::ChassisSubNode::sensorsNode &&
2054928fefb9SNan Zhou                         sensorsAsyncResp->efficientExpand)
2055928fefb9SNan Zhou                     {
2056928fefb9SNan Zhou                         sensorsAsyncResp->asyncResp->res
2057928fefb9SNan Zhou                             .jsonValue["Members@odata.count"] =
2058bd79bce8SPatrick Williams                             sensorsAsyncResp->asyncResp->res
2059bd79bce8SPatrick Williams                                 .jsonValue["Members"]
2060928fefb9SNan Zhou                                 .size();
2061928fefb9SNan Zhou                     }
20620c728b42SJanet Adkins                     else if (chassisSubNode ==
20630c728b42SJanet Adkins                              sensor_utils::ChassisSubNode::thermalNode)
20648bd25ccdSJames Feist                     {
206581ce609eSEd Tanous                         populateFanRedundancy(sensorsAsyncResp);
20668bd25ccdSJames Feist                     }
206749c53ac9SJohnathan Mantey                 }
206862598e31SEd Tanous                 BMCWEB_LOG_DEBUG("getManagedObjectsCb exit");
20695eb468daSGeorge Liu             });
207023a21a1cSEd Tanous     }
207162598e31SEd Tanous     BMCWEB_LOG_DEBUG("getSensorData exit");
2072de629b6eSShawn McCarney }
2073de629b6eSShawn McCarney 
2074504af5a0SPatrick Williams inline void processSensorList(
2075504af5a0SPatrick Williams     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2076fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& sensorNames)
20771abe55efSEd Tanous {
2078fe04d49cSNan Zhou     auto getConnectionCb = [sensorsAsyncResp, sensorNames](
2079fe04d49cSNan Zhou                                const std::set<std::string>& connections) {
208062598e31SEd Tanous         BMCWEB_LOG_DEBUG("getConnectionCb enter");
2081adc4f0dbSShawn McCarney         auto getInventoryItemsCb =
2082bd79bce8SPatrick Williams             [sensorsAsyncResp, sensorNames, connections](
2083bd79bce8SPatrick Williams                 const std::shared_ptr<std::vector<InventoryItem>>&
20844e0d8789SEd Tanous                     inventoryItems) mutable {
208562598e31SEd Tanous                 BMCWEB_LOG_DEBUG("getInventoryItemsCb enter");
208649c53ac9SJohnathan Mantey                 // Get sensor data and store results in JSON
2087002d39b4SEd Tanous                 getSensorData(sensorsAsyncResp, sensorNames, connections,
2088d0090733SEd Tanous                               inventoryItems);
208962598e31SEd Tanous                 BMCWEB_LOG_DEBUG("getInventoryItemsCb exit");
2090adc4f0dbSShawn McCarney             };
2091adc4f0dbSShawn McCarney 
2092adc4f0dbSShawn McCarney         // Get inventory items associated with sensors
2093d0090733SEd Tanous         getInventoryItems(sensorsAsyncResp, sensorNames,
2094adc4f0dbSShawn McCarney                           std::move(getInventoryItemsCb));
2095adc4f0dbSShawn McCarney 
209662598e31SEd Tanous         BMCWEB_LOG_DEBUG("getConnectionCb exit");
209708777fb0SLewanczyk, Dawid     };
2098de629b6eSShawn McCarney 
2099de629b6eSShawn McCarney     // Get set of connections that provide sensor values
210081ce609eSEd Tanous     getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb));
210195a3ecadSAnthony Wilson }
210295a3ecadSAnthony Wilson 
210395a3ecadSAnthony Wilson /**
210495a3ecadSAnthony Wilson  * @brief Entry point for retrieving sensors data related to requested
210595a3ecadSAnthony Wilson  *        chassis.
210695a3ecadSAnthony Wilson  * @param SensorsAsyncResp   Pointer to object holding response data
210795a3ecadSAnthony Wilson  */
2108504af5a0SPatrick Williams inline void getChassisData(
2109504af5a0SPatrick Williams     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
211095a3ecadSAnthony Wilson {
211162598e31SEd Tanous     BMCWEB_LOG_DEBUG("getChassisData enter");
211295a3ecadSAnthony Wilson     auto getChassisCb =
211381ce609eSEd Tanous         [sensorsAsyncResp](
2114fe04d49cSNan Zhou             const std::shared_ptr<std::set<std::string>>& sensorNames) {
211562598e31SEd Tanous             BMCWEB_LOG_DEBUG("getChassisCb enter");
211681ce609eSEd Tanous             processSensorList(sensorsAsyncResp, sensorNames);
211762598e31SEd Tanous             BMCWEB_LOG_DEBUG("getChassisCb exit");
211808777fb0SLewanczyk, Dawid         };
2119928fefb9SNan Zhou     // SensorCollection doesn't contain the Redundancy property
21200c728b42SJanet Adkins     if (sensorsAsyncResp->chassisSubNode != sensors::sensorsNodeStr)
2121928fefb9SNan Zhou     {
21228d1b46d7Szhanghch05         sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] =
21238d1b46d7Szhanghch05             nlohmann::json::array();
2124928fefb9SNan Zhou     }
212526f03899SShawn McCarney     // Get set of sensors in chassis
21267f1cc26dSEd Tanous     getChassis(sensorsAsyncResp->asyncResp, sensorsAsyncResp->chassisId,
21277f1cc26dSEd Tanous                sensorsAsyncResp->chassisSubNode, sensorsAsyncResp->types,
21287f1cc26dSEd Tanous                std::move(getChassisCb));
212962598e31SEd Tanous     BMCWEB_LOG_DEBUG("getChassisData exit");
2130271584abSEd Tanous }
213108777fb0SLewanczyk, Dawid 
2132413961deSRichard Marian Thomaiyar /**
213349c53ac9SJohnathan Mantey  * @brief Find the requested sensorName in the list of all sensors supplied by
213449c53ac9SJohnathan Mantey  * the chassis node
213549c53ac9SJohnathan Mantey  *
213649c53ac9SJohnathan Mantey  * @param sensorName   The sensor name supplied in the PATCH request
213749c53ac9SJohnathan Mantey  * @param sensorsList  The list of sensors managed by the chassis node
213849c53ac9SJohnathan Mantey  * @param sensorsModified  The list of sensors that were found as a result of
213949c53ac9SJohnathan Mantey  *                         repeated calls to this function
214049c53ac9SJohnathan Mantey  */
2141bd79bce8SPatrick Williams inline bool findSensorNameUsingSensorPath(
2142bd79bce8SPatrick Williams     std::string_view sensorName, const std::set<std::string>& sensorsList,
2143fe04d49cSNan Zhou     std::set<std::string>& sensorsModified)
214449c53ac9SJohnathan Mantey {
2145fe04d49cSNan Zhou     for (const auto& chassisSensor : sensorsList)
214649c53ac9SJohnathan Mantey     {
214728aa8de5SGeorge Liu         sdbusplus::message::object_path path(chassisSensor);
2148b00dcc27SEd Tanous         std::string thisSensorName = path.filename();
214928aa8de5SGeorge Liu         if (thisSensorName.empty())
215049c53ac9SJohnathan Mantey         {
215149c53ac9SJohnathan Mantey             continue;
215249c53ac9SJohnathan Mantey         }
215349c53ac9SJohnathan Mantey         if (thisSensorName == sensorName)
215449c53ac9SJohnathan Mantey         {
215549c53ac9SJohnathan Mantey             sensorsModified.emplace(chassisSensor);
215649c53ac9SJohnathan Mantey             return true;
215749c53ac9SJohnathan Mantey         }
215849c53ac9SJohnathan Mantey     }
215949c53ac9SJohnathan Mantey     return false;
216049c53ac9SJohnathan Mantey }
216149c53ac9SJohnathan Mantey 
216249c53ac9SJohnathan Mantey /**
2163413961deSRichard Marian Thomaiyar  * @brief Entry point for overriding sensor values of given sensor
2164413961deSRichard Marian Thomaiyar  *
21658d1b46d7Szhanghch05  * @param sensorAsyncResp   response object
21664bb3dc34SCarol Wang  * @param allCollections   Collections extract from sensors' request patch info
2167413961deSRichard Marian Thomaiyar  * @param chassisSubNode   Chassis Node for which the query has to happen
2168413961deSRichard Marian Thomaiyar  */
216923a21a1cSEd Tanous inline void setSensorsOverride(
2170b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
21710885057cSEd Tanous     std::unordered_map<std::string, std::vector<nlohmann::json::object_t>>&
2172397fd61fSjayaprakash Mutyala         allCollections)
2173413961deSRichard Marian Thomaiyar {
217462598e31SEd Tanous     BMCWEB_LOG_INFO("setSensorsOverride for subNode{}",
217562598e31SEd Tanous                     sensorAsyncResp->chassisSubNode);
2176413961deSRichard Marian Thomaiyar 
2177d02aad39SEd Tanous     std::string_view propertyValueName;
2178f65af9e8SRichard Marian Thomaiyar     std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
2179413961deSRichard Marian Thomaiyar     std::string memberId;
2180543f4400SEd Tanous     double value = 0.0;
2181f65af9e8SRichard Marian Thomaiyar     for (auto& collectionItems : allCollections)
2182f65af9e8SRichard Marian Thomaiyar     {
2183f65af9e8SRichard Marian Thomaiyar         if (collectionItems.first == "Temperatures")
2184f65af9e8SRichard Marian Thomaiyar         {
2185f65af9e8SRichard Marian Thomaiyar             propertyValueName = "ReadingCelsius";
2186f65af9e8SRichard Marian Thomaiyar         }
2187f65af9e8SRichard Marian Thomaiyar         else if (collectionItems.first == "Fans")
2188f65af9e8SRichard Marian Thomaiyar         {
2189f65af9e8SRichard Marian Thomaiyar             propertyValueName = "Reading";
2190f65af9e8SRichard Marian Thomaiyar         }
2191f65af9e8SRichard Marian Thomaiyar         else
2192f65af9e8SRichard Marian Thomaiyar         {
2193f65af9e8SRichard Marian Thomaiyar             propertyValueName = "ReadingVolts";
2194f65af9e8SRichard Marian Thomaiyar         }
2195f65af9e8SRichard Marian Thomaiyar         for (auto& item : collectionItems.second)
2196f65af9e8SRichard Marian Thomaiyar         {
2197afc474aeSMyung Bae             if (!json_util::readJsonObject(                //
2198afc474aeSMyung Bae                     item, sensorAsyncResp->asyncResp->res, //
2199afc474aeSMyung Bae                     "MemberId", memberId,                  //
2200afc474aeSMyung Bae                     propertyValueName, value               //
2201afc474aeSMyung Bae                     ))
2202413961deSRichard Marian Thomaiyar             {
2203413961deSRichard Marian Thomaiyar                 return;
2204413961deSRichard Marian Thomaiyar             }
2205f65af9e8SRichard Marian Thomaiyar             overrideMap.emplace(memberId,
2206f65af9e8SRichard Marian Thomaiyar                                 std::make_pair(value, collectionItems.first));
2207f65af9e8SRichard Marian Thomaiyar         }
2208f65af9e8SRichard Marian Thomaiyar     }
22094bb3dc34SCarol Wang 
2210bd79bce8SPatrick Williams     auto getChassisSensorListCb = [sensorAsyncResp, overrideMap,
2211bd79bce8SPatrick Williams                                    propertyValueNameStr =
2212bd79bce8SPatrick Williams                                        std::string(propertyValueName)](
2213bd79bce8SPatrick Williams                                       const std::shared_ptr<
2214bd79bce8SPatrick Williams                                           std::set<std::string>>& sensorsList) {
221549c53ac9SJohnathan Mantey         // Match sensor names in the PATCH request to those managed by the
221649c53ac9SJohnathan Mantey         // chassis node
2217fe04d49cSNan Zhou         const std::shared_ptr<std::set<std::string>> sensorNames =
2218fe04d49cSNan Zhou             std::make_shared<std::set<std::string>>();
2219f65af9e8SRichard Marian Thomaiyar         for (const auto& item : overrideMap)
2220413961deSRichard Marian Thomaiyar         {
2221f65af9e8SRichard Marian Thomaiyar             const auto& sensor = item.first;
2222c71d6125SEd Tanous             std::pair<std::string, std::string> sensorNameType =
22231516c21bSJanet Adkins                 redfish::sensor_utils::splitSensorNameAndType(sensor);
2224c71d6125SEd Tanous             if (!findSensorNameUsingSensorPath(sensorNameType.second,
2225c71d6125SEd Tanous                                                *sensorsList, *sensorNames))
2226f65af9e8SRichard Marian Thomaiyar             {
222762598e31SEd Tanous                 BMCWEB_LOG_INFO("Unable to find memberId {}", item.first);
22288d1b46d7Szhanghch05                 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2229f65af9e8SRichard Marian Thomaiyar                                            item.second.second, item.first);
2230413961deSRichard Marian Thomaiyar                 return;
2231413961deSRichard Marian Thomaiyar             }
2232f65af9e8SRichard Marian Thomaiyar         }
2233413961deSRichard Marian Thomaiyar         // Get the connection to which the memberId belongs
2234bd79bce8SPatrick Williams         auto getObjectsWithConnectionCb = [sensorAsyncResp, overrideMap,
2235bd79bce8SPatrick Williams                                            propertyValueNameStr](
2236bd79bce8SPatrick Williams                                               const std::set<
2237bd79bce8SPatrick Williams                                                   std::string>& /*connections*/,
2238bd79bce8SPatrick Williams                                               const std::set<std::pair<
2239bd79bce8SPatrick Williams                                                   std::string, std::string>>&
2240413961deSRichard Marian Thomaiyar                                                   objectsWithConnection) {
2241f65af9e8SRichard Marian Thomaiyar             if (objectsWithConnection.size() != overrideMap.size())
2242413961deSRichard Marian Thomaiyar             {
224362598e31SEd Tanous                 BMCWEB_LOG_INFO(
224462598e31SEd Tanous                     "Unable to find all objects with proper connection {} requested {}",
224562598e31SEd Tanous                     objectsWithConnection.size(), overrideMap.size());
2246bd79bce8SPatrick Williams                 messages::resourceNotFound(
2247bd79bce8SPatrick Williams                     sensorAsyncResp->asyncResp->res,
22480c728b42SJanet Adkins                     sensorAsyncResp->chassisSubNode == sensors::thermalNodeStr
2249413961deSRichard Marian Thomaiyar                         ? "Temperatures"
2250413961deSRichard Marian Thomaiyar                         : "Voltages",
2251f65af9e8SRichard Marian Thomaiyar                     "Count");
2252f65af9e8SRichard Marian Thomaiyar                 return;
2253f65af9e8SRichard Marian Thomaiyar             }
2254f65af9e8SRichard Marian Thomaiyar             for (const auto& item : objectsWithConnection)
2255f65af9e8SRichard Marian Thomaiyar             {
225628aa8de5SGeorge Liu                 sdbusplus::message::object_path path(item.first);
225728aa8de5SGeorge Liu                 std::string sensorName = path.filename();
225828aa8de5SGeorge Liu                 if (sensorName.empty())
2259f65af9e8SRichard Marian Thomaiyar                 {
22604f277b54SJayaprakash Mutyala                     messages::internalError(sensorAsyncResp->asyncResp->res);
2261f65af9e8SRichard Marian Thomaiyar                     return;
2262f65af9e8SRichard Marian Thomaiyar                 }
22631516c21bSJanet Adkins                 std::string id = redfish::sensor_utils::getSensorId(
22641516c21bSJanet Adkins                     sensorName, path.parent_path().filename());
2265f65af9e8SRichard Marian Thomaiyar 
22663f5eb755SBan Feng                 const auto& iterator = overrideMap.find(id);
2267f65af9e8SRichard Marian Thomaiyar                 if (iterator == overrideMap.end())
2268f65af9e8SRichard Marian Thomaiyar                 {
226962598e31SEd Tanous                     BMCWEB_LOG_INFO("Unable to find sensor object{}",
227062598e31SEd Tanous                                     item.first);
22714f277b54SJayaprakash Mutyala                     messages::internalError(sensorAsyncResp->asyncResp->res);
2272413961deSRichard Marian Thomaiyar                     return;
2273413961deSRichard Marian Thomaiyar                 }
2274e93abac6SGinu George                 setDbusProperty(sensorAsyncResp->asyncResp,
2275e93abac6SGinu George                                 propertyValueNameStr, item.second, item.first,
2276e93abac6SGinu George                                 "xyz.openbmc_project.Sensor.Value", "Value",
2277d02aad39SEd Tanous                                 iterator->second.first);
2278f65af9e8SRichard Marian Thomaiyar             }
2279413961deSRichard Marian Thomaiyar         };
2280413961deSRichard Marian Thomaiyar         // Get object with connection for the given sensor name
2281413961deSRichard Marian Thomaiyar         getObjectsWithConnection(sensorAsyncResp, sensorNames,
2282413961deSRichard Marian Thomaiyar                                  std::move(getObjectsWithConnectionCb));
2283413961deSRichard Marian Thomaiyar     };
2284413961deSRichard Marian Thomaiyar     // get full sensor list for the given chassisId and cross verify the sensor.
22857f1cc26dSEd Tanous     getChassis(sensorAsyncResp->asyncResp, sensorAsyncResp->chassisId,
22867f1cc26dSEd Tanous                sensorAsyncResp->chassisSubNode, sensorAsyncResp->types,
22877f1cc26dSEd Tanous                std::move(getChassisSensorListCb));
2288413961deSRichard Marian Thomaiyar }
2289413961deSRichard Marian Thomaiyar 
2290a0ec28b6SAdrian Ambrożewicz /**
2291a0ec28b6SAdrian Ambrożewicz  * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
2292a0ec28b6SAdrian Ambrożewicz  * path of the sensor.
2293a0ec28b6SAdrian Ambrożewicz  *
2294a0ec28b6SAdrian Ambrożewicz  * Function builds valid Redfish response for sensor query of given chassis and
2295a0ec28b6SAdrian Ambrożewicz  * node. It then builds metadata about Redfish<->D-Bus correlations and provides
2296a0ec28b6SAdrian Ambrożewicz  * it to caller in a callback.
2297a0ec28b6SAdrian Ambrożewicz  *
2298a0ec28b6SAdrian Ambrożewicz  * @param chassis   Chassis for which retrieval should be performed
2299c9563608SJanet Adkins  * @param node  Node (group) of sensors. See sensor_utils::node for supported
2300c9563608SJanet Adkins  * values
2301a0ec28b6SAdrian Ambrożewicz  * @param mapComplete   Callback to be called with retrieval result
2302a0ec28b6SAdrian Ambrożewicz  */
2303931edc79SEd Tanous template <typename Callback>
2304bd79bce8SPatrick Williams inline void retrieveUriToDbusMap(
2305bd79bce8SPatrick Williams     const std::string& chassis, const std::string& node, Callback&& mapComplete)
2306a0ec28b6SAdrian Ambrożewicz {
230702da7c5aSEd Tanous     decltype(sensors::paths)::const_iterator pathIt =
230802da7c5aSEd Tanous         std::find_if(sensors::paths.cbegin(), sensors::paths.cend(),
230902da7c5aSEd Tanous                      [&node](auto&& val) { return val.first == node; });
231002da7c5aSEd Tanous     if (pathIt == sensors::paths.cend())
2311a0ec28b6SAdrian Ambrożewicz     {
231262598e31SEd Tanous         BMCWEB_LOG_ERROR("Wrong node provided : {}", node);
23136804b5c8SEd Tanous         std::map<std::string, std::string> noop;
23146804b5c8SEd Tanous         mapComplete(boost::beast::http::status::bad_request, noop);
2315a0ec28b6SAdrian Ambrożewicz         return;
2316a0ec28b6SAdrian Ambrożewicz     }
2317d51e072fSKrzysztof Grobelny 
231872374eb7SNan Zhou     auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
2319bd79bce8SPatrick Williams     auto callback =
2320bd79bce8SPatrick Williams         [asyncResp, mapCompleteCb = std::forward<Callback>(mapComplete)](
2321a0ec28b6SAdrian Ambrożewicz             const boost::beast::http::status status,
2322fe04d49cSNan Zhou             const std::map<std::string, std::string>& uriToDbus) {
2323fe04d49cSNan Zhou             mapCompleteCb(status, uriToDbus);
2324fe04d49cSNan Zhou         };
2325a0ec28b6SAdrian Ambrożewicz 
2326a0ec28b6SAdrian Ambrożewicz     auto resp = std::make_shared<SensorsAsyncResp>(
2327d51e072fSKrzysztof Grobelny         asyncResp, chassis, pathIt->second, node, std::move(callback));
2328a0ec28b6SAdrian Ambrożewicz     getChassisData(resp);
2329a0ec28b6SAdrian Ambrożewicz }
2330a0ec28b6SAdrian Ambrożewicz 
2331bacb2162SNan Zhou namespace sensors
2332bacb2162SNan Zhou {
2333928fefb9SNan Zhou 
2334bacb2162SNan Zhou inline void getChassisCallback(
2335c1d019a6SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2336c1d019a6SEd Tanous     std::string_view chassisId, std::string_view chassisSubNode,
2337fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& sensorNames)
2338bacb2162SNan Zhou {
233962598e31SEd Tanous     BMCWEB_LOG_DEBUG("getChassisCallback enter ");
2340bacb2162SNan Zhou 
2341c1d019a6SEd Tanous     nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
2342c1d019a6SEd Tanous     for (const std::string& sensor : *sensorNames)
2343bacb2162SNan Zhou     {
234462598e31SEd Tanous         BMCWEB_LOG_DEBUG("Adding sensor: {}", sensor);
2345bacb2162SNan Zhou 
2346bacb2162SNan Zhou         sdbusplus::message::object_path path(sensor);
2347bacb2162SNan Zhou         std::string sensorName = path.filename();
2348bacb2162SNan Zhou         if (sensorName.empty())
2349bacb2162SNan Zhou         {
235062598e31SEd Tanous             BMCWEB_LOG_ERROR("Invalid sensor path: {}", sensor);
2351c1d019a6SEd Tanous             messages::internalError(asyncResp->res);
2352bacb2162SNan Zhou             return;
2353bacb2162SNan Zhou         }
2354c1d019a6SEd Tanous         std::string type = path.parent_path().filename();
23551516c21bSJanet Adkins         std::string id = redfish::sensor_utils::getSensorId(sensorName, type);
2356c1d019a6SEd Tanous 
23571476687dSEd Tanous         nlohmann::json::object_t member;
2358ef4c65b7SEd Tanous         member["@odata.id"] = boost::urls::format(
2359ef4c65b7SEd Tanous             "/redfish/v1/Chassis/{}/{}/{}", chassisId, chassisSubNode, id);
2360c1d019a6SEd Tanous 
2361b2ba3072SPatrick Williams         entriesArray.emplace_back(std::move(member));
2362bacb2162SNan Zhou     }
2363bacb2162SNan Zhou 
2364c1d019a6SEd Tanous     asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
236562598e31SEd Tanous     BMCWEB_LOG_DEBUG("getChassisCallback exit");
2366bacb2162SNan Zhou }
2367e6bd846dSNan Zhou 
2368ac106bf6SEd Tanous inline void handleSensorCollectionGet(
2369ac106bf6SEd Tanous     App& app, const crow::Request& req,
2370ac106bf6SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2371de167a6fSNan Zhou     const std::string& chassisId)
2372de167a6fSNan Zhou {
2373de167a6fSNan Zhou     query_param::QueryCapabilities capabilities = {
2374de167a6fSNan Zhou         .canDelegateExpandLevel = 1,
2375de167a6fSNan Zhou     };
2376de167a6fSNan Zhou     query_param::Query delegatedQuery;
2377ac106bf6SEd Tanous     if (!redfish::setUpRedfishRouteWithDelegation(app, req, asyncResp,
2378de167a6fSNan Zhou                                                   delegatedQuery, capabilities))
2379de167a6fSNan Zhou     {
2380de167a6fSNan Zhou         return;
2381de167a6fSNan Zhou     }
2382de167a6fSNan Zhou 
2383de167a6fSNan Zhou     if (delegatedQuery.expandType != query_param::ExpandType::None)
2384de167a6fSNan Zhou     {
2385de167a6fSNan Zhou         // we perform efficient expand.
2386ac106bf6SEd Tanous         auto sensorsAsyncResp = std::make_shared<SensorsAsyncResp>(
2387ac106bf6SEd Tanous             asyncResp, chassisId, sensors::dbus::sensorPaths,
23880c728b42SJanet Adkins             sensors::sensorsNodeStr,
2389de167a6fSNan Zhou             /*efficientExpand=*/true);
2390ac106bf6SEd Tanous         getChassisData(sensorsAsyncResp);
2391de167a6fSNan Zhou 
239262598e31SEd Tanous         BMCWEB_LOG_DEBUG(
239362598e31SEd Tanous             "SensorCollection doGet exit via efficient expand handler");
2394de167a6fSNan Zhou         return;
23950bad320cSEd Tanous     }
2396de167a6fSNan Zhou 
2397de167a6fSNan Zhou     // We get all sensors as hyperlinkes in the chassis (this
2398de167a6fSNan Zhou     // implies we reply on the default query parameters handler)
23990c728b42SJanet Adkins     getChassis(asyncResp, chassisId, sensors::sensorsNodeStr, dbus::sensorPaths,
2400ac106bf6SEd Tanous                std::bind_front(sensors::getChassisCallback, asyncResp,
24010c728b42SJanet Adkins                                chassisId, sensors::sensorsNodeStr));
2402c1d019a6SEd Tanous }
24037f1cc26dSEd Tanous 
2404504af5a0SPatrick Williams inline void getSensorFromDbus(
2405504af5a0SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2406c1d019a6SEd Tanous     const std::string& sensorPath,
2407c1d019a6SEd Tanous     const ::dbus::utility::MapperGetObject& mapperResponse)
2408c1d019a6SEd Tanous {
2409c1d019a6SEd Tanous     if (mapperResponse.size() != 1)
2410c1d019a6SEd Tanous     {
2411c1d019a6SEd Tanous         messages::internalError(asyncResp->res);
2412c1d019a6SEd Tanous         return;
2413c1d019a6SEd Tanous     }
2414c1d019a6SEd Tanous     const auto& valueIface = *mapperResponse.begin();
2415c1d019a6SEd Tanous     const std::string& connectionName = valueIface.first;
241662598e31SEd Tanous     BMCWEB_LOG_DEBUG("Looking up {}", connectionName);
241762598e31SEd Tanous     BMCWEB_LOG_DEBUG("Path {}", sensorPath);
2418c1343bf6SKrzysztof Grobelny 
2419deae6a78SEd Tanous     ::dbus::utility::getAllProperties(
2420c1343bf6SKrzysztof Grobelny         *crow::connections::systemBus, connectionName, sensorPath, "",
2421c1d019a6SEd Tanous         [asyncResp,
24225e7e2dc5SEd Tanous          sensorPath](const boost::system::error_code& ec,
2423c1d019a6SEd Tanous                      const ::dbus::utility::DBusPropertiesMap& valuesDict) {
2424c1d019a6SEd Tanous             if (ec)
2425c1d019a6SEd Tanous             {
2426c1d019a6SEd Tanous                 messages::internalError(asyncResp->res);
2427c1d019a6SEd Tanous                 return;
2428c1d019a6SEd Tanous             }
2429c1d019a6SEd Tanous             sdbusplus::message::object_path path(sensorPath);
2430c1d019a6SEd Tanous             std::string name = path.filename();
2431c1d019a6SEd Tanous             path = path.parent_path();
2432c1d019a6SEd Tanous             std::string type = path.filename();
2433c9563608SJanet Adkins             sensor_utils::objectPropertiesToJson(
24340c728b42SJanet Adkins                 name, type, sensor_utils::ChassisSubNode::sensorsNode,
24350c728b42SJanet Adkins                 valuesDict, asyncResp->res.jsonValue, nullptr);
2436c1343bf6SKrzysztof Grobelny         });
2437de167a6fSNan Zhou }
2438de167a6fSNan Zhou 
2439e6bd846dSNan Zhou inline void handleSensorGet(App& app, const crow::Request& req,
2440c1d019a6SEd Tanous                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2441677bb756SEd Tanous                             const std::string& chassisId,
2442c1d019a6SEd Tanous                             const std::string& sensorId)
2443e6bd846dSNan Zhou {
2444c1d019a6SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2445e6bd846dSNan Zhou     {
2446e6bd846dSNan Zhou         return;
2447e6bd846dSNan Zhou     }
2448c71d6125SEd Tanous     std::pair<std::string, std::string> nameType =
24491516c21bSJanet Adkins         redfish::sensor_utils::splitSensorNameAndType(sensorId);
2450c71d6125SEd Tanous     if (nameType.first.empty() || nameType.second.empty())
2451c1d019a6SEd Tanous     {
2452c1d019a6SEd Tanous         messages::resourceNotFound(asyncResp->res, sensorId, "Sensor");
2453c1d019a6SEd Tanous         return;
2454c1d019a6SEd Tanous     }
2455c71d6125SEd Tanous 
2456ef4c65b7SEd Tanous     asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
2457ef4c65b7SEd Tanous         "/redfish/v1/Chassis/{}/Sensors/{}", chassisId, sensorId);
2458c1d019a6SEd Tanous 
245962598e31SEd Tanous     BMCWEB_LOG_DEBUG("Sensor doGet enter");
2460e6bd846dSNan Zhou 
24612b73119cSGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
2462e6bd846dSNan Zhou         "xyz.openbmc_project.Sensor.Value"};
2463c71d6125SEd Tanous     std::string sensorPath = "/xyz/openbmc_project/sensors/" + nameType.first +
2464c71d6125SEd Tanous                              '/' + nameType.second;
2465e6bd846dSNan Zhou     // Get a list of all of the sensors that implement Sensor.Value
2466e6bd846dSNan Zhou     // and get the path and service name associated with the sensor
24672b73119cSGeorge Liu     ::dbus::utility::getDbusObject(
24682b73119cSGeorge Liu         sensorPath, interfaces,
2469aec0ec30SMyung Bae         [asyncResp, sensorId,
24702b73119cSGeorge Liu          sensorPath](const boost::system::error_code& ec,
2471c1d019a6SEd Tanous                      const ::dbus::utility::MapperGetObject& subtree) {
247262598e31SEd Tanous             BMCWEB_LOG_DEBUG("respHandler1 enter");
2473aec0ec30SMyung Bae             if (ec == boost::system::errc::io_error)
2474aec0ec30SMyung Bae             {
247562598e31SEd Tanous                 BMCWEB_LOG_WARNING("Sensor not found from getSensorPaths");
2476aec0ec30SMyung Bae                 messages::resourceNotFound(asyncResp->res, sensorId, "Sensor");
2477aec0ec30SMyung Bae                 return;
2478aec0ec30SMyung Bae             }
2479e6bd846dSNan Zhou             if (ec)
2480e6bd846dSNan Zhou             {
2481c1d019a6SEd Tanous                 messages::internalError(asyncResp->res);
248262598e31SEd Tanous                 BMCWEB_LOG_ERROR(
248362598e31SEd Tanous                     "Sensor getSensorPaths resp_handler: Dbus error {}", ec);
2484e6bd846dSNan Zhou                 return;
2485e6bd846dSNan Zhou             }
2486c1d019a6SEd Tanous             getSensorFromDbus(asyncResp, sensorPath, subtree);
248762598e31SEd Tanous             BMCWEB_LOG_DEBUG("respHandler1 exit");
24882b73119cSGeorge Liu         });
2489e6bd846dSNan Zhou }
2490e6bd846dSNan Zhou 
2491bacb2162SNan Zhou } // namespace sensors
2492bacb2162SNan Zhou 
24937e860f15SJohn Edward Broadbent inline void requestRoutesSensorCollection(App& app)
249495a3ecadSAnthony Wilson {
24957e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/")
2496ed398213SEd Tanous         .privileges(redfish::privileges::getSensorCollection)
2497002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2498de167a6fSNan Zhou             std::bind_front(sensors::handleSensorCollectionGet, std::ref(app)));
249995a3ecadSAnthony Wilson }
250095a3ecadSAnthony Wilson 
25017e860f15SJohn Edward Broadbent inline void requestRoutesSensor(App& app)
250295a3ecadSAnthony Wilson {
25037e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/")
2504ed398213SEd Tanous         .privileges(redfish::privileges::getSensor)
2505002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2506e6bd846dSNan Zhou             std::bind_front(sensors::handleSensorGet, std::ref(app)));
250795a3ecadSAnthony Wilson }
250895a3ecadSAnthony Wilson 
250908777fb0SLewanczyk, Dawid } // namespace redfish
2510