xref: /openbmc/bmcweb/features/redfish/lib/sensors.hpp (revision 3577e44683a5ade8ad02a6418984b56f4ca2bcac)
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"
203f95a277SMyung Bae #include "utils/chassis_utils.hpp"
213ccb3adbSEd Tanous #include "utils/dbus_utils.hpp"
223ccb3adbSEd Tanous #include "utils/json_utils.hpp"
233ccb3adbSEd Tanous #include "utils/query_param.hpp"
241516c21bSJanet Adkins #include "utils/sensor_utils.hpp"
250ec8b83dSEd Tanous 
26d7857201SEd Tanous #include <asm-generic/errno.h>
27d7857201SEd Tanous 
28d7857201SEd Tanous #include <boost/beast/http/status.hpp>
29d7857201SEd Tanous #include <boost/beast/http/verb.hpp>
30e99073f5SGeorge Liu #include <boost/system/error_code.hpp>
31ef4c65b7SEd Tanous #include <boost/url/format.hpp>
32d7857201SEd Tanous #include <boost/url/url.hpp>
33d7857201SEd Tanous #include <nlohmann/json.hpp>
34d7857201SEd Tanous #include <sdbusplus/message/native_types.hpp>
3586d89ed7SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
361214b7e7SGunnar Mills 
37d7857201SEd Tanous #include <algorithm>
387a1dbc48SGeorge Liu #include <array>
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",
8344914192SZev Weiss         "/xyz/openbmc_project/sensors/liquidflow",
8444914192SZev 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     {
20482b286fbSEd Tanous         if (!metadata)
205a0ec28b6SAdrian Ambrożewicz         {
20682b286fbSEd Tanous             return;
207a0ec28b6SAdrian Ambrożewicz         }
20882b286fbSEd Tanous         const auto nameIt = sensorObject.find("Name");
209d9cca622SJames Athappilly         if (nameIt == sensorObject.end())
21082b286fbSEd Tanous         {
21182b286fbSEd Tanous             return;
21282b286fbSEd Tanous         }
21382b286fbSEd Tanous         const auto idIt = sensorObject.find("@odata.id");
214d9cca622SJames Athappilly         if (idIt == sensorObject.end())
21582b286fbSEd Tanous         {
21682b286fbSEd Tanous             return;
21782b286fbSEd Tanous         }
21882b286fbSEd Tanous         const std::string* name = nameIt->get_ptr<const std::string*>();
21982b286fbSEd Tanous         if (name == nullptr)
22082b286fbSEd Tanous         {
22182b286fbSEd Tanous             return;
22282b286fbSEd Tanous         }
22382b286fbSEd Tanous         const std::string* id = idIt->get_ptr<const std::string*>();
22482b286fbSEd Tanous         if (id == nullptr)
22582b286fbSEd Tanous         {
22682b286fbSEd Tanous             return;
22782b286fbSEd Tanous         }
22882b286fbSEd Tanous         metadata->emplace_back(SensorData{*name, *id, dbusPath});
229a0ec28b6SAdrian Ambrożewicz     }
230a0ec28b6SAdrian Ambrożewicz 
231a0ec28b6SAdrian Ambrożewicz     void updateUri(const std::string& name, const std::string& uri)
232a0ec28b6SAdrian Ambrożewicz     {
233a0ec28b6SAdrian Ambrożewicz         if (metadata)
234a0ec28b6SAdrian Ambrożewicz         {
235a0ec28b6SAdrian Ambrożewicz             for (auto& sensor : *metadata)
236a0ec28b6SAdrian Ambrożewicz             {
237a0ec28b6SAdrian Ambrożewicz                 if (sensor.name == name)
238a0ec28b6SAdrian Ambrożewicz                 {
239a0ec28b6SAdrian Ambrożewicz                     sensor.uri = uri;
240a0ec28b6SAdrian Ambrożewicz                 }
241a0ec28b6SAdrian Ambrożewicz             }
242a0ec28b6SAdrian Ambrożewicz         }
243a0ec28b6SAdrian Ambrożewicz     }
244a0ec28b6SAdrian Ambrożewicz 
2458d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp> asyncResp;
246a0ec28b6SAdrian Ambrożewicz     const std::string chassisId;
247cf9e417dSEd Tanous     const std::span<const std::string_view> types;
248a0ec28b6SAdrian Ambrożewicz     const std::string chassisSubNode;
249928fefb9SNan Zhou     const bool efficientExpand;
250a0ec28b6SAdrian Ambrożewicz 
251a0ec28b6SAdrian Ambrożewicz   private:
252a0ec28b6SAdrian Ambrożewicz     std::optional<std::vector<SensorData>> metadata;
253a0ec28b6SAdrian Ambrożewicz     DataCompleteCb dataComplete;
25408777fb0SLewanczyk, Dawid };
25508777fb0SLewanczyk, Dawid 
256c9563608SJanet Adkins using InventoryItem = sensor_utils::InventoryItem;
257adc4f0dbSShawn McCarney 
258adc4f0dbSShawn McCarney /**
259413961deSRichard Marian Thomaiyar  * @brief Get objects with connection necessary for sensors
260588c3f0dSKowalski, Kamil  * @param SensorsAsyncResp Pointer to object holding response data
26108777fb0SLewanczyk, Dawid  * @param sensorNames Sensors retrieved from chassis
26208777fb0SLewanczyk, Dawid  * @param callback Callback for processing gathered connections
26308777fb0SLewanczyk, Dawid  */
26408777fb0SLewanczyk, Dawid template <typename Callback>
265413961deSRichard Marian Thomaiyar void getObjectsWithConnection(
26681ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
267fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& sensorNames,
2681abe55efSEd Tanous     Callback&& callback)
2691abe55efSEd Tanous {
27062598e31SEd Tanous     BMCWEB_LOG_DEBUG("getObjectsWithConnection enter");
27103b5bae3SJames Feist     const std::string path = "/xyz/openbmc_project/sensors";
272e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
27308777fb0SLewanczyk, Dawid         "xyz.openbmc_project.Sensor.Value"};
27408777fb0SLewanczyk, Dawid 
275e99073f5SGeorge Liu     // Make call to ObjectMapper to find all sensors objects
276e99073f5SGeorge Liu     dbus::utility::getSubTree(
277e99073f5SGeorge Liu         path, 2, interfaces,
2788cb2c024SEd Tanous         [callback = std::forward<Callback>(callback), sensorsAsyncResp,
279e99073f5SGeorge Liu          sensorNames](const boost::system::error_code& ec,
280002d39b4SEd Tanous                       const dbus::utility::MapperGetSubTreeResponse& subtree) {
281e99073f5SGeorge Liu             // Response handler for parsing objects subtree
28262598e31SEd Tanous             BMCWEB_LOG_DEBUG("getObjectsWithConnection resp_handler enter");
2831abe55efSEd Tanous             if (ec)
2841abe55efSEd Tanous             {
2858d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
28662598e31SEd Tanous                 BMCWEB_LOG_ERROR(
28762598e31SEd Tanous                     "getObjectsWithConnection resp_handler: Dbus error {}", ec);
28808777fb0SLewanczyk, Dawid                 return;
28908777fb0SLewanczyk, Dawid             }
29008777fb0SLewanczyk, Dawid 
29162598e31SEd Tanous             BMCWEB_LOG_DEBUG("Found {} subtrees", subtree.size());
29208777fb0SLewanczyk, Dawid 
293bd79bce8SPatrick Williams             // Make unique list of connections only for requested sensor types
294bd79bce8SPatrick Williams             // and found in the chassis
295fe04d49cSNan Zhou             std::set<std::string> connections;
296413961deSRichard Marian Thomaiyar             std::set<std::pair<std::string, std::string>> objectsWithConnection;
29708777fb0SLewanczyk, Dawid 
29862598e31SEd Tanous             BMCWEB_LOG_DEBUG("sensorNames list count: {}", sensorNames->size());
29949c53ac9SJohnathan Mantey             for (const std::string& tsensor : *sensorNames)
3001abe55efSEd Tanous             {
30162598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Sensor to find: {}", tsensor);
30208777fb0SLewanczyk, Dawid             }
30308777fb0SLewanczyk, Dawid 
304bd79bce8SPatrick Williams             for (const std::pair<std::string,
305bd79bce8SPatrick Williams                                  std::vector<std::pair<
306bd79bce8SPatrick Williams                                      std::string, std::vector<std::string>>>>&
3071abe55efSEd Tanous                      object : subtree)
3081abe55efSEd Tanous             {
3093d158643SEd Tanous                 if (sensorNames->contains(object.first))
3101abe55efSEd Tanous                 {
31149c53ac9SJohnathan Mantey                     for (const std::pair<std::string, std::vector<std::string>>&
3121abe55efSEd Tanous                              objData : object.second)
3131abe55efSEd Tanous                     {
314bd79bce8SPatrick Williams                         BMCWEB_LOG_DEBUG("Adding connection: {}",
315bd79bce8SPatrick Williams                                          objData.first);
31608777fb0SLewanczyk, Dawid                         connections.insert(objData.first);
317de629b6eSShawn McCarney                         objectsWithConnection.insert(
318de629b6eSShawn McCarney                             std::make_pair(object.first, objData.first));
31908777fb0SLewanczyk, Dawid                     }
32008777fb0SLewanczyk, Dawid                 }
32108777fb0SLewanczyk, Dawid             }
32262598e31SEd Tanous             BMCWEB_LOG_DEBUG("Found {} connections", connections.size());
323413961deSRichard Marian Thomaiyar             callback(std::move(connections), std::move(objectsWithConnection));
32462598e31SEd Tanous             BMCWEB_LOG_DEBUG("getObjectsWithConnection resp_handler exit");
325e99073f5SGeorge Liu         });
32662598e31SEd Tanous     BMCWEB_LOG_DEBUG("getObjectsWithConnection exit");
327413961deSRichard Marian Thomaiyar }
328413961deSRichard Marian Thomaiyar 
329413961deSRichard Marian Thomaiyar /**
330413961deSRichard Marian Thomaiyar  * @brief Create connections necessary for sensors
331413961deSRichard Marian Thomaiyar  * @param SensorsAsyncResp Pointer to object holding response data
332413961deSRichard Marian Thomaiyar  * @param sensorNames Sensors retrieved from chassis
333413961deSRichard Marian Thomaiyar  * @param callback Callback for processing gathered connections
334413961deSRichard Marian Thomaiyar  */
335413961deSRichard Marian Thomaiyar template <typename Callback>
336daadfb2eSEd Tanous void getConnections(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
337daadfb2eSEd Tanous                     const std::shared_ptr<std::set<std::string>>& sensorNames,
338413961deSRichard Marian Thomaiyar                     Callback&& callback)
339413961deSRichard Marian Thomaiyar {
340413961deSRichard Marian Thomaiyar     auto objectsWithConnectionCb =
3418cb2c024SEd Tanous         [callback = std::forward<Callback>(callback)](
3428cb2c024SEd Tanous             const std::set<std::string>& connections,
343413961deSRichard Marian Thomaiyar             const std::set<std::pair<std::string, std::string>>&
3443174e4dfSEd Tanous             /*objectsWithConnection*/) { callback(connections); };
34581ce609eSEd Tanous     getObjectsWithConnection(sensorsAsyncResp, sensorNames,
346413961deSRichard Marian Thomaiyar                              std::move(objectsWithConnectionCb));
34708777fb0SLewanczyk, Dawid }
34808777fb0SLewanczyk, Dawid 
34908777fb0SLewanczyk, Dawid /**
35049c53ac9SJohnathan Mantey  * @brief Shrinks the list of sensors for processing
35149c53ac9SJohnathan Mantey  * @param SensorsAysncResp  The class holding the Redfish response
35249c53ac9SJohnathan Mantey  * @param allSensors  A list of all the sensors associated to the
35349c53ac9SJohnathan Mantey  * chassis element (i.e. baseboard, front panel, etc...)
35449c53ac9SJohnathan Mantey  * @param activeSensors A list that is a reduction of the incoming
35549c53ac9SJohnathan Mantey  * allSensors list.  Eliminate Thermal sensors when a Power request is
35649c53ac9SJohnathan Mantey  * made, and eliminate Power sensors when a Thermal request is made.
35749c53ac9SJohnathan Mantey  */
35823a21a1cSEd Tanous inline void reduceSensorList(
3597f1cc26dSEd Tanous     crow::Response& res, std::string_view chassisSubNode,
360cf9e417dSEd Tanous     std::span<const std::string_view> sensorTypes,
36149c53ac9SJohnathan Mantey     const std::vector<std::string>* allSensors,
362fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& activeSensors)
36349c53ac9SJohnathan Mantey {
36449c53ac9SJohnathan Mantey     if ((allSensors == nullptr) || (activeSensors == nullptr))
36549c53ac9SJohnathan Mantey     {
3667f1cc26dSEd Tanous         messages::resourceNotFound(res, chassisSubNode,
3670c728b42SJanet Adkins                                    chassisSubNode == sensors::thermalNodeStr
368a0ec28b6SAdrian Ambrożewicz                                        ? "Temperatures"
36949c53ac9SJohnathan Mantey                                        : "Voltages");
37049c53ac9SJohnathan Mantey 
37149c53ac9SJohnathan Mantey         return;
37249c53ac9SJohnathan Mantey     }
37349c53ac9SJohnathan Mantey     if (allSensors->empty())
37449c53ac9SJohnathan Mantey     {
37549c53ac9SJohnathan Mantey         // Nothing to do, the activeSensors object is also empty
37649c53ac9SJohnathan Mantey         return;
37749c53ac9SJohnathan Mantey     }
37849c53ac9SJohnathan Mantey 
3797f1cc26dSEd Tanous     for (std::string_view type : sensorTypes)
38049c53ac9SJohnathan Mantey     {
38149c53ac9SJohnathan Mantey         for (const std::string& sensor : *allSensors)
38249c53ac9SJohnathan Mantey         {
38311ba3979SEd Tanous             if (sensor.starts_with(type))
38449c53ac9SJohnathan Mantey             {
38549c53ac9SJohnathan Mantey                 activeSensors->emplace(sensor);
38649c53ac9SJohnathan Mantey             }
38749c53ac9SJohnathan Mantey         }
38849c53ac9SJohnathan Mantey     }
38949c53ac9SJohnathan Mantey }
39049c53ac9SJohnathan Mantey 
3917f1cc26dSEd Tanous /*
3927f1cc26dSEd Tanous  *Populates the top level collection for a given subnode.  Populates
3937f1cc26dSEd Tanous  *SensorCollection, Power, or Thermal schemas.
3947f1cc26dSEd Tanous  *
3957f1cc26dSEd Tanous  * */
3967f1cc26dSEd Tanous inline void populateChassisNode(nlohmann::json& jsonValue,
3977f1cc26dSEd Tanous                                 std::string_view chassisSubNode)
3987f1cc26dSEd Tanous {
3990c728b42SJanet Adkins     if (chassisSubNode == sensors::powerNodeStr)
4007f1cc26dSEd Tanous     {
4017f1cc26dSEd Tanous         jsonValue["@odata.type"] = "#Power.v1_5_2.Power";
4027f1cc26dSEd Tanous     }
4030c728b42SJanet Adkins     else if (chassisSubNode == sensors::thermalNodeStr)
4047f1cc26dSEd Tanous     {
4057f1cc26dSEd Tanous         jsonValue["@odata.type"] = "#Thermal.v1_4_0.Thermal";
4067f1cc26dSEd Tanous         jsonValue["Fans"] = nlohmann::json::array();
4077f1cc26dSEd Tanous         jsonValue["Temperatures"] = nlohmann::json::array();
4087f1cc26dSEd Tanous     }
4090c728b42SJanet Adkins     else if (chassisSubNode == sensors::sensorsNodeStr)
4107f1cc26dSEd Tanous     {
4117f1cc26dSEd Tanous         jsonValue["@odata.type"] = "#SensorCollection.SensorCollection";
4127f1cc26dSEd Tanous         jsonValue["Description"] = "Collection of Sensors for this Chassis";
4137f1cc26dSEd Tanous         jsonValue["Members"] = nlohmann::json::array();
4147f1cc26dSEd Tanous         jsonValue["Members@odata.count"] = 0;
4157f1cc26dSEd Tanous     }
4167f1cc26dSEd Tanous 
4170c728b42SJanet Adkins     if (chassisSubNode != sensors::sensorsNodeStr)
4187f1cc26dSEd Tanous     {
4197f1cc26dSEd Tanous         jsonValue["Id"] = chassisSubNode;
4207f1cc26dSEd Tanous     }
4217f1cc26dSEd Tanous     jsonValue["Name"] = chassisSubNode;
4227f1cc26dSEd Tanous }
4237f1cc26dSEd Tanous 
42449c53ac9SJohnathan Mantey /**
42508777fb0SLewanczyk, Dawid  * @brief Retrieves requested chassis sensors and redundancy data from DBus .
426588c3f0dSKowalski, Kamil  * @param SensorsAsyncResp   Pointer to object holding response data
42708777fb0SLewanczyk, Dawid  * @param callback  Callback for next step in gathered sensor processing
42808777fb0SLewanczyk, Dawid  */
42908777fb0SLewanczyk, Dawid template <typename Callback>
4307f1cc26dSEd Tanous void getChassis(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
4317f1cc26dSEd Tanous                 std::string_view chassisId, std::string_view chassisSubNode,
432cf9e417dSEd Tanous                 std::span<const std::string_view> sensorTypes,
433cf9e417dSEd Tanous                 Callback&& callback)
4341abe55efSEd Tanous {
43562598e31SEd Tanous     BMCWEB_LOG_DEBUG("getChassis enter");
4367a1dbc48SGeorge Liu 
4377a1dbc48SGeorge Liu     // Get the Chassis Collection
4387a1dbc48SGeorge Liu     dbus::utility::getSubTreePaths(
4393f95a277SMyung Bae         "/xyz/openbmc_project/inventory", 0, chassisInterfaces,
4408cb2c024SEd Tanous         [callback = std::forward<Callback>(callback), asyncResp,
4417f1cc26dSEd Tanous          chassisIdStr{std::string(chassisId)},
4424e0d8789SEd Tanous          chassisSubNode{std::string(chassisSubNode)},
4434e0d8789SEd Tanous          sensorTypes](const boost::system::error_code& ec,
4444e0d8789SEd Tanous                       const dbus::utility::MapperGetSubTreePathsResponse&
4454e0d8789SEd Tanous                           chassisPaths) mutable {
44662598e31SEd Tanous             BMCWEB_LOG_DEBUG("getChassis respHandler enter");
4471abe55efSEd Tanous             if (ec)
4481abe55efSEd Tanous             {
44962598e31SEd Tanous                 BMCWEB_LOG_ERROR("getChassis respHandler DBUS error: {}", ec);
4507f1cc26dSEd Tanous                 messages::internalError(asyncResp->res);
45108777fb0SLewanczyk, Dawid                 return;
45208777fb0SLewanczyk, Dawid             }
45349c53ac9SJohnathan Mantey             const std::string* chassisPath = nullptr;
45449c53ac9SJohnathan Mantey             for (const std::string& chassis : chassisPaths)
4551abe55efSEd Tanous             {
45628aa8de5SGeorge Liu                 sdbusplus::message::object_path path(chassis);
457f8fe53e7SEd Tanous                 std::string chassisName = path.filename();
45828aa8de5SGeorge Liu                 if (chassisName.empty())
4591abe55efSEd Tanous                 {
46062598e31SEd Tanous                     BMCWEB_LOG_ERROR("Failed to find '/' in {}", chassis);
461daf36e2eSEd Tanous                     continue;
462daf36e2eSEd Tanous                 }
4637f1cc26dSEd Tanous                 if (chassisName == chassisIdStr)
4641abe55efSEd Tanous                 {
46549c53ac9SJohnathan Mantey                     chassisPath = &chassis;
46649c53ac9SJohnathan Mantey                     break;
467daf36e2eSEd Tanous                 }
46849c53ac9SJohnathan Mantey             }
46949c53ac9SJohnathan Mantey             if (chassisPath == nullptr)
4701abe55efSEd Tanous             {
471bd79bce8SPatrick Williams                 messages::resourceNotFound(asyncResp->res, "Chassis",
472bd79bce8SPatrick Williams                                            chassisIdStr);
47349c53ac9SJohnathan Mantey                 return;
4741abe55efSEd Tanous             }
4757f1cc26dSEd Tanous             populateChassisNode(asyncResp->res.jsonValue, chassisSubNode);
47608777fb0SLewanczyk, Dawid 
477ef4c65b7SEd Tanous             asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
478ef4c65b7SEd Tanous                 "/redfish/v1/Chassis/{}/{}", chassisIdStr, chassisSubNode);
47995a3ecadSAnthony Wilson 
4808fb49dd6SShawn McCarney             // Get the list of all sensors for this Chassis element
4818fb49dd6SShawn McCarney             std::string sensorPath = *chassisPath + "/all_sensors";
4826c3e9451SGeorge Liu             dbus::utility::getAssociationEndPoints(
4834e0d8789SEd Tanous                 sensorPath, [asyncResp, chassisSubNode, sensorTypes,
4844e0d8789SEd Tanous                              callback = std::forward<Callback>(callback)](
4858b24275dSEd Tanous                                 const boost::system::error_code& ec2,
4864e0d8789SEd Tanous                                 const dbus::utility::MapperEndPoints&
4874e0d8789SEd Tanous                                     nodeSensorList) mutable {
4888b24275dSEd Tanous                     if (ec2)
48949c53ac9SJohnathan Mantey                     {
4908b24275dSEd Tanous                         if (ec2.value() != EBADR)
49149c53ac9SJohnathan Mantey                         {
4927f1cc26dSEd Tanous                             messages::internalError(asyncResp->res);
49349c53ac9SJohnathan Mantey                             return;
49449c53ac9SJohnathan Mantey                         }
49549c53ac9SJohnathan Mantey                     }
496bd79bce8SPatrick Williams                     const std::shared_ptr<std::set<std::string>>
497bd79bce8SPatrick Williams                         culledSensorList =
498fe04d49cSNan Zhou                             std::make_shared<std::set<std::string>>();
499bd79bce8SPatrick Williams                     reduceSensorList(asyncResp->res, chassisSubNode,
500bd79bce8SPatrick Williams                                      sensorTypes, &nodeSensorList,
501bd79bce8SPatrick Williams                                      culledSensorList);
502bd79bce8SPatrick Williams                     BMCWEB_LOG_DEBUG("Finishing with {}",
503bd79bce8SPatrick Williams                                      culledSensorList->size());
50449c53ac9SJohnathan Mantey                     callback(culledSensorList);
5051e1e598dSJonathan Doman                 });
5067a1dbc48SGeorge Liu         });
50762598e31SEd Tanous     BMCWEB_LOG_DEBUG("getChassis exit");
50808777fb0SLewanczyk, Dawid }
50908777fb0SLewanczyk, Dawid 
51008777fb0SLewanczyk, Dawid /**
5111d7c0054SEd Tanous  * @brief Builds a json sensor representation of a sensor.
5121d7c0054SEd Tanous  * @param sensorName  The name of the sensor to be built
5131d7c0054SEd Tanous  * @param sensorType  The type (temperature, fan_tach, etc) of the sensor to
5141d7c0054SEd Tanous  * build
5158ece0e45SEd Tanous  * @param chassisSubNode The subnode (thermal, sensor, etc) of the sensor
5161d7c0054SEd Tanous  * @param interfacesDict  A dictionary of the interfaces and properties of said
5171d7c0054SEd Tanous  * interfaces to be built from
5181d7c0054SEd Tanous  * @param sensorJson  The json object to fill
5191d7c0054SEd Tanous  * @param inventoryItem D-Bus inventory item associated with the sensor.  Will
5201d7c0054SEd Tanous  * be nullptr if no associated inventory item was found.
5211d7c0054SEd Tanous  */
5221d7c0054SEd Tanous inline void objectInterfacesToJson(
5231d7c0054SEd Tanous     const std::string& sensorName, const std::string& sensorType,
5240c728b42SJanet Adkins     const sensor_utils::ChassisSubNode chassisSubNode,
52580f79a40SMichael Shen     const dbus::utility::DBusInterfacesMap& interfacesDict,
5261d7c0054SEd Tanous     nlohmann::json& sensorJson, InventoryItem* inventoryItem)
5271d7c0054SEd Tanous {
5281d7c0054SEd Tanous     for (const auto& [interface, valuesDict] : interfacesDict)
5291d7c0054SEd Tanous     {
530c9563608SJanet Adkins         sensor_utils::objectPropertiesToJson(
531c9563608SJanet Adkins             sensorName, sensorType, chassisSubNode, valuesDict, sensorJson,
532c9563608SJanet Adkins             inventoryItem);
5331d7c0054SEd Tanous     }
53462598e31SEd Tanous     BMCWEB_LOG_DEBUG("Added sensor {}", sensorName);
5351d7c0054SEd Tanous }
5361d7c0054SEd Tanous 
537b5a76932SEd Tanous inline void populateFanRedundancy(
538b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
5398bd25ccdSJames Feist {
540e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
541e99073f5SGeorge Liu         "xyz.openbmc_project.Control.FanRedundancy"};
542e99073f5SGeorge Liu     dbus::utility::getSubTree(
543e99073f5SGeorge Liu         "/xyz/openbmc_project/control", 2, interfaces,
544b9d36b47SEd Tanous         [sensorsAsyncResp](
545e99073f5SGeorge Liu             const boost::system::error_code& ec,
546b9d36b47SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& resp) {
5478bd25ccdSJames Feist             if (ec)
5488bd25ccdSJames Feist             {
5498bd25ccdSJames Feist                 return; // don't have to have this interface
5508bd25ccdSJames Feist             }
5516c3e9451SGeorge Liu             for (const std::pair<std::string, dbus::utility::MapperServiceMap>&
552e278c18fSEd Tanous                      pathPair : resp)
5538bd25ccdSJames Feist             {
554e278c18fSEd Tanous                 const std::string& path = pathPair.first;
555bd79bce8SPatrick Williams                 const dbus::utility::MapperServiceMap& objDict =
556bd79bce8SPatrick Williams                     pathPair.second;
5578bd25ccdSJames Feist                 if (objDict.empty())
5588bd25ccdSJames Feist                 {
5598bd25ccdSJames Feist                     continue; // this should be impossible
5608bd25ccdSJames Feist                 }
5618bd25ccdSJames Feist 
5628bd25ccdSJames Feist                 const std::string& owner = objDict.begin()->first;
5636c3e9451SGeorge Liu                 dbus::utility::getAssociationEndPoints(
5646c3e9451SGeorge Liu                     path + "/chassis",
5656c3e9451SGeorge Liu                     [path, owner, sensorsAsyncResp](
5668b24275dSEd Tanous                         const boost::system::error_code& ec2,
5676c3e9451SGeorge Liu                         const dbus::utility::MapperEndPoints& endpoints) {
5688b24275dSEd Tanous                         if (ec2)
5698bd25ccdSJames Feist                         {
5708bd25ccdSJames Feist                             return; // if they don't have an association we
5718bd25ccdSJames Feist                                     // can't tell what chassis is
5728bd25ccdSJames Feist                         }
5733544d2a7SEd Tanous                         auto found = std::ranges::find_if(
574bd79bce8SPatrick Williams                             endpoints,
575bd79bce8SPatrick Williams                             [sensorsAsyncResp](const std::string& entry) {
576bd79bce8SPatrick Williams                                 return entry.find(
577bd79bce8SPatrick Williams                                            sensorsAsyncResp->chassisId) !=
5788bd25ccdSJames Feist                                        std::string::npos;
5798bd25ccdSJames Feist                             });
5808bd25ccdSJames Feist 
5811e1e598dSJonathan Doman                         if (found == endpoints.end())
5828bd25ccdSJames Feist                         {
5838bd25ccdSJames Feist                             return;
5848bd25ccdSJames Feist                         }
585deae6a78SEd Tanous                         dbus::utility::getAllProperties(
58686d89ed7SKrzysztof Grobelny                             *crow::connections::systemBus, owner, path,
58786d89ed7SKrzysztof Grobelny                             "xyz.openbmc_project.Control.FanRedundancy",
5888bd25ccdSJames Feist                             [path, sensorsAsyncResp](
5898b24275dSEd Tanous                                 const boost::system::error_code& ec3,
59086d89ed7SKrzysztof Grobelny                                 const dbus::utility::DBusPropertiesMap& ret) {
5918b24275dSEd Tanous                                 if (ec3)
5928bd25ccdSJames Feist                                 {
5938bd25ccdSJames Feist                                     return; // don't have to have this
5948bd25ccdSJames Feist                                             // interface
5958bd25ccdSJames Feist                                 }
5968bd25ccdSJames Feist 
59786d89ed7SKrzysztof Grobelny                                 const uint8_t* allowedFailures = nullptr;
598bd79bce8SPatrick Williams                                 const std::vector<std::string>* collection =
599bd79bce8SPatrick Williams                                     nullptr;
60086d89ed7SKrzysztof Grobelny                                 const std::string* status = nullptr;
60186d89ed7SKrzysztof Grobelny 
602bd79bce8SPatrick Williams                                 const bool success =
603bd79bce8SPatrick Williams                                     sdbusplus::unpackPropertiesNoThrow(
60486d89ed7SKrzysztof Grobelny                                         dbus_utils::UnpackErrorPrinter(), ret,
605bd79bce8SPatrick Williams                                         "AllowedFailures", allowedFailures,
606bd79bce8SPatrick Williams                                         "Collection", collection, "Status",
607bd79bce8SPatrick Williams                                         status);
60886d89ed7SKrzysztof Grobelny 
60986d89ed7SKrzysztof Grobelny                                 if (!success)
61086d89ed7SKrzysztof Grobelny                                 {
61186d89ed7SKrzysztof Grobelny                                     messages::internalError(
61286d89ed7SKrzysztof Grobelny                                         sensorsAsyncResp->asyncResp->res);
61386d89ed7SKrzysztof Grobelny                                     return;
61486d89ed7SKrzysztof Grobelny                                 }
61586d89ed7SKrzysztof Grobelny 
616bd79bce8SPatrick Williams                                 if (allowedFailures == nullptr ||
617bd79bce8SPatrick Williams                                     collection == nullptr || status == nullptr)
6188bd25ccdSJames Feist                                 {
619bd79bce8SPatrick Williams                                     BMCWEB_LOG_ERROR(
620bd79bce8SPatrick Williams                                         "Invalid redundancy interface");
6218bd25ccdSJames Feist                                     messages::internalError(
6228d1b46d7Szhanghch05                                         sensorsAsyncResp->asyncResp->res);
6238bd25ccdSJames Feist                                     return;
6248bd25ccdSJames Feist                                 }
6258bd25ccdSJames Feist 
626bd79bce8SPatrick Williams                                 sdbusplus::message::object_path objectPath(
627bd79bce8SPatrick Williams                                     path);
62828aa8de5SGeorge Liu                                 std::string name = objectPath.filename();
62928aa8de5SGeorge Liu                                 if (name.empty())
6308bd25ccdSJames Feist                                 {
6318bd25ccdSJames Feist                                     // this should be impossible
6328bd25ccdSJames Feist                                     messages::internalError(
6338d1b46d7Szhanghch05                                         sensorsAsyncResp->asyncResp->res);
6348bd25ccdSJames Feist                                     return;
6358bd25ccdSJames Feist                                 }
63618f8f608SEd Tanous                                 std::ranges::replace(name, '_', ' ');
6378bd25ccdSJames Feist 
6388bd25ccdSJames Feist                                 std::string health;
6398bd25ccdSJames Feist 
64011ba3979SEd Tanous                                 if (status->ends_with("Full"))
6418bd25ccdSJames Feist                                 {
6428bd25ccdSJames Feist                                     health = "OK";
6438bd25ccdSJames Feist                                 }
64411ba3979SEd Tanous                                 else if (status->ends_with("Degraded"))
6458bd25ccdSJames Feist                                 {
6468bd25ccdSJames Feist                                     health = "Warning";
6478bd25ccdSJames Feist                                 }
6488bd25ccdSJames Feist                                 else
6498bd25ccdSJames Feist                                 {
6508bd25ccdSJames Feist                                     health = "Critical";
6518bd25ccdSJames Feist                                 }
6521476687dSEd Tanous                                 nlohmann::json::array_t redfishCollection;
6538bd25ccdSJames Feist                                 const auto& fanRedfish =
654bd79bce8SPatrick Williams                                     sensorsAsyncResp->asyncResp->res
655bd79bce8SPatrick Williams                                         .jsonValue["Fans"];
6568bd25ccdSJames Feist                                 for (const std::string& item : *collection)
6578bd25ccdSJames Feist                                 {
658bd79bce8SPatrick Williams                                     sdbusplus::message::object_path itemPath(
659bd79bce8SPatrick Williams                                         item);
6608a592810SEd Tanous                                     std::string itemName = itemPath.filename();
66128aa8de5SGeorge Liu                                     if (itemName.empty())
66228aa8de5SGeorge Liu                                     {
66328aa8de5SGeorge Liu                                         continue;
66428aa8de5SGeorge Liu                                     }
6658bd25ccdSJames Feist                                     /*
6668bd25ccdSJames Feist                                     todo(ed): merge patch that fixes the names
6678bd25ccdSJames Feist                                     std::replace(itemName.begin(),
6688bd25ccdSJames Feist                                                  itemName.end(), '_', ' ');*/
6693544d2a7SEd Tanous                                     auto schemaItem = std::ranges::find_if(
670bd79bce8SPatrick Williams                                         fanRedfish,
671bd79bce8SPatrick Williams                                         [itemName](const nlohmann::json& fan) {
6723e35c761SGeorge Liu                                             return fan["Name"] == itemName;
6738bd25ccdSJames Feist                                         });
6748bd25ccdSJames Feist                                     if (schemaItem != fanRedfish.end())
6758bd25ccdSJames Feist                                     {
6768a592810SEd Tanous                                         nlohmann::json::object_t collectionId;
6778a592810SEd Tanous                                         collectionId["@odata.id"] =
6781476687dSEd Tanous                                             (*schemaItem)["@odata.id"];
6791476687dSEd Tanous                                         redfishCollection.emplace_back(
6808a592810SEd Tanous                                             std::move(collectionId));
6818bd25ccdSJames Feist                                     }
6828bd25ccdSJames Feist                                     else
6838bd25ccdSJames Feist                                     {
684bd79bce8SPatrick Williams                                         BMCWEB_LOG_ERROR(
685bd79bce8SPatrick Williams                                             "failed to find fan in schema");
6868bd25ccdSJames Feist                                         messages::internalError(
6878d1b46d7Szhanghch05                                             sensorsAsyncResp->asyncResp->res);
6888bd25ccdSJames Feist                                         return;
6898bd25ccdSJames Feist                                     }
6908bd25ccdSJames Feist                                 }
6918bd25ccdSJames Feist 
692bd79bce8SPatrick Williams                                 size_t minNumNeeded =
693bd79bce8SPatrick Williams                                     collection->empty()
69426f6976fSEd Tanous                                         ? 0
695bd79bce8SPatrick Williams                                         : collection->size() - *allowedFailures;
696bd79bce8SPatrick Williams                                 nlohmann::json& jResp =
697bd79bce8SPatrick Williams                                     sensorsAsyncResp->asyncResp->res
6988bd25ccdSJames Feist                                         .jsonValue["Redundancy"];
6991476687dSEd Tanous 
7001476687dSEd Tanous                                 nlohmann::json::object_t redundancy;
701bd79bce8SPatrick Williams                                 boost::urls::url url = boost::urls::format(
702bd79bce8SPatrick Williams                                     "/redfish/v1/Chassis/{}/{}",
703ef4c65b7SEd Tanous                                     sensorsAsyncResp->chassisId,
704eddfc437SWilly Tu                                     sensorsAsyncResp->chassisSubNode);
705bd79bce8SPatrick Williams                                 url.set_fragment(
706bd79bce8SPatrick Williams                                     ("/Redundancy"_json_pointer / jResp.size())
707eddfc437SWilly Tu                                         .to_string());
708eddfc437SWilly Tu                                 redundancy["@odata.id"] = std::move(url);
709bd79bce8SPatrick Williams                                 redundancy["@odata.type"] =
710bd79bce8SPatrick Williams                                     "#Redundancy.v1_3_2.Redundancy";
7111476687dSEd Tanous                                 redundancy["MinNumNeeded"] = minNumNeeded;
712bd79bce8SPatrick Williams                                 redundancy["Mode"] =
713bd79bce8SPatrick Williams                                     redundancy::RedundancyType::NPlusM;
7141476687dSEd Tanous                                 redundancy["Name"] = name;
7151476687dSEd Tanous                                 redundancy["RedundancySet"] = redfishCollection;
7161476687dSEd Tanous                                 redundancy["Status"]["Health"] = health;
717bd79bce8SPatrick Williams                                 redundancy["Status"]["State"] =
718bd79bce8SPatrick Williams                                     resource::State::Enabled;
7191476687dSEd Tanous 
720b2ba3072SPatrick Williams                                 jResp.emplace_back(std::move(redundancy));
72186d89ed7SKrzysztof Grobelny                             });
7221e1e598dSJonathan Doman                     });
7238bd25ccdSJames Feist             }
724e99073f5SGeorge Liu         });
7258bd25ccdSJames Feist }
7268bd25ccdSJames Feist 
727504af5a0SPatrick Williams inline void sortJSONResponse(
728504af5a0SPatrick Williams     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
72949c53ac9SJohnathan Mantey {
7308d1b46d7Szhanghch05     nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue;
73149c53ac9SJohnathan Mantey     std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
7320c728b42SJanet Adkins     if (sensorsAsyncResp->chassisSubNode == sensors::powerNodeStr)
73349c53ac9SJohnathan Mantey     {
73449c53ac9SJohnathan Mantey         sensorHeaders = {"Voltages", "PowerSupplies"};
73549c53ac9SJohnathan Mantey     }
73649c53ac9SJohnathan Mantey     for (const std::string& sensorGroup : sensorHeaders)
73749c53ac9SJohnathan Mantey     {
73849c53ac9SJohnathan Mantey         nlohmann::json::iterator entry = response.find(sensorGroup);
7394e196b9aSEd Tanous         if (entry == response.end())
74049c53ac9SJohnathan Mantey         {
7414e196b9aSEd Tanous             continue;
7424e196b9aSEd Tanous         }
7434e196b9aSEd Tanous         nlohmann::json::array_t* arr =
7444e196b9aSEd Tanous             entry->get_ptr<nlohmann::json::array_t*>();
7454e196b9aSEd Tanous         if (arr == nullptr)
7464e196b9aSEd Tanous         {
7474e196b9aSEd Tanous             continue;
7484e196b9aSEd Tanous         }
7494e196b9aSEd Tanous         json_util::sortJsonArrayByKey(*arr, "Name");
75049c53ac9SJohnathan Mantey 
75149c53ac9SJohnathan Mantey         // add the index counts to the end of each entry
75249c53ac9SJohnathan Mantey         size_t count = 0;
75349c53ac9SJohnathan Mantey         for (nlohmann::json& sensorJson : *entry)
75449c53ac9SJohnathan Mantey         {
75549c53ac9SJohnathan Mantey             nlohmann::json::iterator odata = sensorJson.find("@odata.id");
75649c53ac9SJohnathan Mantey             if (odata == sensorJson.end())
75749c53ac9SJohnathan Mantey             {
75849c53ac9SJohnathan Mantey                 continue;
75949c53ac9SJohnathan Mantey             }
76082b286fbSEd Tanous             const auto nameIt = sensorJson.find("Name");
76182b286fbSEd Tanous             if (nameIt == sensorJson.end())
76249c53ac9SJohnathan Mantey             {
76382b286fbSEd Tanous                 continue;
76482b286fbSEd Tanous             }
76582b286fbSEd Tanous             std::string* value = odata->get_ptr<std::string*>();
76682b286fbSEd Tanous             if (value == nullptr)
76782b286fbSEd Tanous             {
76882b286fbSEd Tanous                 continue;
76982b286fbSEd Tanous             }
77082b286fbSEd Tanous             const std::string* name = nameIt->get_ptr<const std::string*>();
77182b286fbSEd Tanous             if (name == nullptr)
77282b286fbSEd Tanous             {
77382b286fbSEd Tanous                 continue;
77482b286fbSEd Tanous             }
775eddfc437SWilly Tu             *value += "/" + std::to_string(count);
7763e35c761SGeorge Liu             sensorJson["MemberId"] = std::to_string(count);
77749c53ac9SJohnathan Mantey             count++;
77882b286fbSEd Tanous             sensorsAsyncResp->updateUri(*name, *value);
77949c53ac9SJohnathan Mantey         }
78049c53ac9SJohnathan Mantey     }
78149c53ac9SJohnathan Mantey }
78249c53ac9SJohnathan Mantey 
78308777fb0SLewanczyk, Dawid /**
784adc4f0dbSShawn McCarney  * @brief Finds the inventory item with the specified object path.
785adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
786adc4f0dbSShawn McCarney  * @param invItemObjPath D-Bus object path of inventory item.
787adc4f0dbSShawn McCarney  * @return Inventory item within vector, or nullptr if no match found.
7888fb49dd6SShawn McCarney  */
78923a21a1cSEd Tanous inline InventoryItem* findInventoryItem(
790b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
791adc4f0dbSShawn McCarney     const std::string& invItemObjPath)
7928fb49dd6SShawn McCarney {
793adc4f0dbSShawn McCarney     for (InventoryItem& inventoryItem : *inventoryItems)
7948fb49dd6SShawn McCarney     {
795adc4f0dbSShawn McCarney         if (inventoryItem.objectPath == invItemObjPath)
7968fb49dd6SShawn McCarney         {
797adc4f0dbSShawn McCarney             return &inventoryItem;
7988fb49dd6SShawn McCarney         }
7998fb49dd6SShawn McCarney     }
8008fb49dd6SShawn McCarney     return nullptr;
8018fb49dd6SShawn McCarney }
8028fb49dd6SShawn McCarney 
8038fb49dd6SShawn McCarney /**
804adc4f0dbSShawn McCarney  * @brief Finds the inventory item associated with the specified sensor.
805adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
806adc4f0dbSShawn McCarney  * @param sensorObjPath D-Bus object path of sensor.
807adc4f0dbSShawn McCarney  * @return Inventory item within vector, or nullptr if no match found.
8088fb49dd6SShawn McCarney  */
80923a21a1cSEd Tanous inline InventoryItem* findInventoryItemForSensor(
810b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
811adc4f0dbSShawn McCarney     const std::string& sensorObjPath)
812adc4f0dbSShawn McCarney {
813adc4f0dbSShawn McCarney     for (InventoryItem& inventoryItem : *inventoryItems)
814adc4f0dbSShawn McCarney     {
815db0d36efSEd Tanous         if (inventoryItem.sensors.contains(sensorObjPath))
816adc4f0dbSShawn McCarney         {
817adc4f0dbSShawn McCarney             return &inventoryItem;
818adc4f0dbSShawn McCarney         }
819adc4f0dbSShawn McCarney     }
820adc4f0dbSShawn McCarney     return nullptr;
821adc4f0dbSShawn McCarney }
822adc4f0dbSShawn McCarney 
823adc4f0dbSShawn McCarney /**
824d500549bSAnthony Wilson  * @brief Finds the inventory item associated with the specified led path.
825d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
826d500549bSAnthony Wilson  * @param ledObjPath D-Bus object path of led.
827d500549bSAnthony Wilson  * @return Inventory item within vector, or nullptr if no match found.
828d500549bSAnthony Wilson  */
829bd79bce8SPatrick Williams inline InventoryItem* findInventoryItemForLed(
830bd79bce8SPatrick Williams     std::vector<InventoryItem>& inventoryItems, const std::string& ledObjPath)
831d500549bSAnthony Wilson {
832d500549bSAnthony Wilson     for (InventoryItem& inventoryItem : inventoryItems)
833d500549bSAnthony Wilson     {
834d500549bSAnthony Wilson         if (inventoryItem.ledObjectPath == ledObjPath)
835d500549bSAnthony Wilson         {
836d500549bSAnthony Wilson             return &inventoryItem;
837d500549bSAnthony Wilson         }
838d500549bSAnthony Wilson     }
839d500549bSAnthony Wilson     return nullptr;
840d500549bSAnthony Wilson }
841d500549bSAnthony Wilson 
842d500549bSAnthony Wilson /**
843adc4f0dbSShawn McCarney  * @brief Adds inventory item and associated sensor to specified vector.
844adc4f0dbSShawn McCarney  *
845adc4f0dbSShawn McCarney  * Adds a new InventoryItem to the vector if necessary.  Searches for an
846adc4f0dbSShawn McCarney  * existing InventoryItem with the specified object path.  If not found, one is
847adc4f0dbSShawn McCarney  * added to the vector.
848adc4f0dbSShawn McCarney  *
849adc4f0dbSShawn McCarney  * Next, the specified sensor is added to the set of sensors associated with the
850adc4f0dbSShawn McCarney  * InventoryItem.
851adc4f0dbSShawn McCarney  *
852adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
853adc4f0dbSShawn McCarney  * @param invItemObjPath D-Bus object path of inventory item.
854adc4f0dbSShawn McCarney  * @param sensorObjPath D-Bus object path of sensor
855adc4f0dbSShawn McCarney  */
856b5a76932SEd Tanous inline void addInventoryItem(
857b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
858b5a76932SEd Tanous     const std::string& invItemObjPath, const std::string& sensorObjPath)
859adc4f0dbSShawn McCarney {
860adc4f0dbSShawn McCarney     // Look for inventory item in vector
861bd79bce8SPatrick Williams     InventoryItem* inventoryItem =
862bd79bce8SPatrick Williams         findInventoryItem(inventoryItems, invItemObjPath);
863adc4f0dbSShawn McCarney 
864adc4f0dbSShawn McCarney     // If inventory item doesn't exist in vector, add it
865adc4f0dbSShawn McCarney     if (inventoryItem == nullptr)
866adc4f0dbSShawn McCarney     {
867adc4f0dbSShawn McCarney         inventoryItems->emplace_back(invItemObjPath);
868adc4f0dbSShawn McCarney         inventoryItem = &(inventoryItems->back());
869adc4f0dbSShawn McCarney     }
870adc4f0dbSShawn McCarney 
871adc4f0dbSShawn McCarney     // Add sensor to set of sensors associated with inventory item
872adc4f0dbSShawn McCarney     inventoryItem->sensors.emplace(sensorObjPath);
873adc4f0dbSShawn McCarney }
874adc4f0dbSShawn McCarney 
875adc4f0dbSShawn McCarney /**
876adc4f0dbSShawn McCarney  * @brief Stores D-Bus data in the specified inventory item.
877adc4f0dbSShawn McCarney  *
878adc4f0dbSShawn McCarney  * Finds D-Bus data in the specified map of interfaces.  Stores the data in the
879adc4f0dbSShawn McCarney  * specified InventoryItem.
880adc4f0dbSShawn McCarney  *
881adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
882adc4f0dbSShawn McCarney  * response.
883adc4f0dbSShawn McCarney  *
884adc4f0dbSShawn McCarney  * @param inventoryItem Inventory item where data will be stored.
885adc4f0dbSShawn McCarney  * @param interfacesDict Map containing D-Bus interfaces and their properties
886adc4f0dbSShawn McCarney  * for the specified inventory item.
887adc4f0dbSShawn McCarney  */
88823a21a1cSEd Tanous inline void storeInventoryItemData(
889adc4f0dbSShawn McCarney     InventoryItem& inventoryItem,
89080f79a40SMichael Shen     const dbus::utility::DBusInterfacesMap& interfacesDict)
8918fb49dd6SShawn McCarney {
892adc4f0dbSShawn McCarney     // Get properties from Inventory.Item interface
893711ac7a9SEd Tanous 
8949eb808c1SEd Tanous     for (const auto& [interface, values] : interfacesDict)
8958fb49dd6SShawn McCarney     {
896711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Item")
8978fb49dd6SShawn McCarney         {
8989eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
899711ac7a9SEd Tanous             {
900711ac7a9SEd Tanous                 if (name == "Present")
901711ac7a9SEd Tanous                 {
902711ac7a9SEd Tanous                     const bool* value = std::get_if<bool>(&dbusValue);
903adc4f0dbSShawn McCarney                     if (value != nullptr)
9048fb49dd6SShawn McCarney                     {
905adc4f0dbSShawn McCarney                         inventoryItem.isPresent = *value;
9068fb49dd6SShawn McCarney                     }
9078fb49dd6SShawn McCarney                 }
9088fb49dd6SShawn McCarney             }
909711ac7a9SEd Tanous         }
910adc4f0dbSShawn McCarney         // Check if Inventory.Item.PowerSupply interface is present
911711ac7a9SEd Tanous 
912711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Item.PowerSupply")
9138fb49dd6SShawn McCarney         {
914adc4f0dbSShawn McCarney             inventoryItem.isPowerSupply = true;
9158fb49dd6SShawn McCarney         }
916adc4f0dbSShawn McCarney 
917adc4f0dbSShawn McCarney         // Get properties from Inventory.Decorator.Asset interface
918711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
919adc4f0dbSShawn McCarney         {
9209eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
921711ac7a9SEd Tanous             {
922711ac7a9SEd Tanous                 if (name == "Manufacturer")
923adc4f0dbSShawn McCarney                 {
924adc4f0dbSShawn McCarney                     const std::string* value =
925711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
926adc4f0dbSShawn McCarney                     if (value != nullptr)
927adc4f0dbSShawn McCarney                     {
928adc4f0dbSShawn McCarney                         inventoryItem.manufacturer = *value;
929adc4f0dbSShawn McCarney                     }
930adc4f0dbSShawn McCarney                 }
931711ac7a9SEd Tanous                 if (name == "Model")
932adc4f0dbSShawn McCarney                 {
933adc4f0dbSShawn McCarney                     const std::string* value =
934711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
935adc4f0dbSShawn McCarney                     if (value != nullptr)
936adc4f0dbSShawn McCarney                     {
937adc4f0dbSShawn McCarney                         inventoryItem.model = *value;
938adc4f0dbSShawn McCarney                     }
939adc4f0dbSShawn McCarney                 }
940711ac7a9SEd Tanous                 if (name == "SerialNumber")
941adc4f0dbSShawn McCarney                 {
942adc4f0dbSShawn McCarney                     const std::string* value =
943711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
944adc4f0dbSShawn McCarney                     if (value != nullptr)
945adc4f0dbSShawn McCarney                     {
946adc4f0dbSShawn McCarney                         inventoryItem.serialNumber = *value;
947adc4f0dbSShawn McCarney                     }
948adc4f0dbSShawn McCarney                 }
949711ac7a9SEd Tanous                 if (name == "PartNumber")
950711ac7a9SEd Tanous                 {
951711ac7a9SEd Tanous                     const std::string* value =
952711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
953711ac7a9SEd Tanous                     if (value != nullptr)
954711ac7a9SEd Tanous                     {
955711ac7a9SEd Tanous                         inventoryItem.partNumber = *value;
956711ac7a9SEd Tanous                     }
957711ac7a9SEd Tanous                 }
958711ac7a9SEd Tanous             }
959adc4f0dbSShawn McCarney         }
960adc4f0dbSShawn McCarney 
961711ac7a9SEd Tanous         if (interface ==
962711ac7a9SEd Tanous             "xyz.openbmc_project.State.Decorator.OperationalStatus")
963adc4f0dbSShawn McCarney         {
9649eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
965adc4f0dbSShawn McCarney             {
966711ac7a9SEd Tanous                 if (name == "Functional")
967711ac7a9SEd Tanous                 {
968711ac7a9SEd Tanous                     const bool* value = std::get_if<bool>(&dbusValue);
969adc4f0dbSShawn McCarney                     if (value != nullptr)
970adc4f0dbSShawn McCarney                     {
971adc4f0dbSShawn McCarney                         inventoryItem.isFunctional = *value;
9728fb49dd6SShawn McCarney                     }
9738fb49dd6SShawn McCarney                 }
9748fb49dd6SShawn McCarney             }
9758fb49dd6SShawn McCarney         }
976711ac7a9SEd Tanous     }
977711ac7a9SEd Tanous }
9788fb49dd6SShawn McCarney 
9798fb49dd6SShawn McCarney /**
980adc4f0dbSShawn McCarney  * @brief Gets D-Bus data for inventory items associated with sensors.
9818fb49dd6SShawn McCarney  *
982adc4f0dbSShawn McCarney  * Uses the specified connections (services) to obtain D-Bus data for inventory
983adc4f0dbSShawn McCarney  * items associated with sensors.  Stores the resulting data in the
984adc4f0dbSShawn McCarney  * inventoryItems vector.
9858fb49dd6SShawn McCarney  *
986adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
987adc4f0dbSShawn McCarney  * response.
988adc4f0dbSShawn McCarney  *
989adc4f0dbSShawn McCarney  * Finds the inventory item data asynchronously.  Invokes callback when data has
990adc4f0dbSShawn McCarney  * been obtained.
991adc4f0dbSShawn McCarney  *
992adc4f0dbSShawn McCarney  * The callback must have the following signature:
993adc4f0dbSShawn McCarney  *   @code
994d500549bSAnthony Wilson  *   callback(void)
995adc4f0dbSShawn McCarney  *   @endcode
996adc4f0dbSShawn McCarney  *
997adc4f0dbSShawn McCarney  * This function is called recursively, obtaining data asynchronously from one
998adc4f0dbSShawn McCarney  * connection in each call.  This ensures the callback is not invoked until the
999adc4f0dbSShawn McCarney  * last asynchronous function has completed.
10008fb49dd6SShawn McCarney  *
10018fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
1002adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1003adc4f0dbSShawn McCarney  * @param invConnections Connections that provide data for the inventory items.
10048fb49dd6SShawn McCarney  * implements ObjectManager.
1005adc4f0dbSShawn McCarney  * @param callback Callback to invoke when inventory data has been obtained.
1006adc4f0dbSShawn McCarney  * @param invConnectionsIndex Current index in invConnections.  Only specified
1007adc4f0dbSShawn McCarney  * in recursive calls to this function.
10088fb49dd6SShawn McCarney  */
1009adc4f0dbSShawn McCarney template <typename Callback>
10104ff0f1f4SEd Tanous void getInventoryItemsData(
1011daadfb2eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1012daadfb2eSEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1013daadfb2eSEd Tanous     const std::shared_ptr<std::set<std::string>>& invConnections,
1014daadfb2eSEd Tanous     Callback&& callback, size_t invConnectionsIndex = 0)
10158fb49dd6SShawn McCarney {
101662598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryItemsData enter");
10178fb49dd6SShawn McCarney 
1018adc4f0dbSShawn McCarney     // If no more connections left, call callback
1019adc4f0dbSShawn McCarney     if (invConnectionsIndex >= invConnections->size())
10208fb49dd6SShawn McCarney     {
1021d500549bSAnthony Wilson         callback();
102262598e31SEd Tanous         BMCWEB_LOG_DEBUG("getInventoryItemsData exit");
1023adc4f0dbSShawn McCarney         return;
1024adc4f0dbSShawn McCarney     }
1025adc4f0dbSShawn McCarney 
1026adc4f0dbSShawn McCarney     // Get inventory item data from current connection
1027fe04d49cSNan Zhou     auto it = invConnections->begin();
1028fe04d49cSNan Zhou     std::advance(it, invConnectionsIndex);
1029adc4f0dbSShawn McCarney     if (it != invConnections->end())
1030adc4f0dbSShawn McCarney     {
1031adc4f0dbSShawn McCarney         const std::string& invConnection = *it;
1032adc4f0dbSShawn McCarney 
10335eb468daSGeorge Liu         // Get all object paths and their interfaces for current connection
10345eb468daSGeorge Liu         sdbusplus::message::object_path path("/xyz/openbmc_project/inventory");
10355eb468daSGeorge Liu         dbus::utility::getManagedObjects(
10365eb468daSGeorge Liu             invConnection, path,
10375eb468daSGeorge Liu             [sensorsAsyncResp, inventoryItems, invConnections,
10388cb2c024SEd Tanous              callback = std::forward<Callback>(callback), invConnectionsIndex](
10395e7e2dc5SEd Tanous                 const boost::system::error_code& ec,
10404e0d8789SEd Tanous                 const dbus::utility::ManagedObjectType& resp) mutable {
104162598e31SEd Tanous                 BMCWEB_LOG_DEBUG("getInventoryItemsData respHandler enter");
10428fb49dd6SShawn McCarney                 if (ec)
10438fb49dd6SShawn McCarney                 {
104462598e31SEd Tanous                     BMCWEB_LOG_ERROR(
104562598e31SEd Tanous                         "getInventoryItemsData respHandler DBus error {}", ec);
10468d1b46d7Szhanghch05                     messages::internalError(sensorsAsyncResp->asyncResp->res);
10478fb49dd6SShawn McCarney                     return;
10488fb49dd6SShawn McCarney                 }
10498fb49dd6SShawn McCarney 
10508fb49dd6SShawn McCarney                 // Loop through returned object paths
10518fb49dd6SShawn McCarney                 for (const auto& objDictEntry : resp)
10528fb49dd6SShawn McCarney                 {
10538fb49dd6SShawn McCarney                     const std::string& objPath =
10548fb49dd6SShawn McCarney                         static_cast<const std::string&>(objDictEntry.first);
10558fb49dd6SShawn McCarney 
1056bd79bce8SPatrick Williams                     // If this object path is one of the specified inventory
1057bd79bce8SPatrick Williams                     // items
1058bd79bce8SPatrick Williams                     InventoryItem* inventoryItem =
1059bd79bce8SPatrick Williams                         findInventoryItem(inventoryItems, objPath);
1060adc4f0dbSShawn McCarney                     if (inventoryItem != nullptr)
10618fb49dd6SShawn McCarney                     {
1062adc4f0dbSShawn McCarney                         // Store inventory data in InventoryItem
1063bd79bce8SPatrick Williams                         storeInventoryItemData(*inventoryItem,
1064bd79bce8SPatrick Williams                                                objDictEntry.second);
10658fb49dd6SShawn McCarney                     }
10668fb49dd6SShawn McCarney                 }
10678fb49dd6SShawn McCarney 
1068adc4f0dbSShawn McCarney                 // Recurse to get inventory item data from next connection
1069adc4f0dbSShawn McCarney                 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
1070d0090733SEd Tanous                                       invConnections, std::move(callback),
1071d0090733SEd Tanous                                       invConnectionsIndex + 1);
1072adc4f0dbSShawn McCarney 
107362598e31SEd Tanous                 BMCWEB_LOG_DEBUG("getInventoryItemsData respHandler exit");
10745eb468daSGeorge Liu             });
10758fb49dd6SShawn McCarney     }
10768fb49dd6SShawn McCarney 
107762598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryItemsData exit");
10788fb49dd6SShawn McCarney }
10798fb49dd6SShawn McCarney 
10808fb49dd6SShawn McCarney /**
1081adc4f0dbSShawn McCarney  * @brief Gets connections that provide D-Bus data for inventory items.
10828fb49dd6SShawn McCarney  *
1083adc4f0dbSShawn McCarney  * Gets the D-Bus connections (services) that provide data for the inventory
1084adc4f0dbSShawn McCarney  * items that are associated with sensors.
10858fb49dd6SShawn McCarney  *
10868fb49dd6SShawn McCarney  * Finds the connections asynchronously.  Invokes callback when information has
10878fb49dd6SShawn McCarney  * been obtained.
10888fb49dd6SShawn McCarney  *
10898fb49dd6SShawn McCarney  * The callback must have the following signature:
10908fb49dd6SShawn McCarney  *   @code
1091fe04d49cSNan Zhou  *   callback(std::shared_ptr<std::set<std::string>> invConnections)
10928fb49dd6SShawn McCarney  *   @endcode
10938fb49dd6SShawn McCarney  *
10948fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
1095adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
10968fb49dd6SShawn McCarney  * @param callback Callback to invoke when connections have been obtained.
10978fb49dd6SShawn McCarney  */
10988fb49dd6SShawn McCarney template <typename Callback>
10994ff0f1f4SEd Tanous void getInventoryItemsConnections(
1100b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1101b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
11028fb49dd6SShawn McCarney     Callback&& callback)
11038fb49dd6SShawn McCarney {
110462598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryItemsConnections enter");
11058fb49dd6SShawn McCarney 
11068fb49dd6SShawn McCarney     const std::string path = "/xyz/openbmc_project/inventory";
1107e99073f5SGeorge Liu     constexpr std::array<std::string_view, 4> interfaces = {
11088fb49dd6SShawn McCarney         "xyz.openbmc_project.Inventory.Item",
1109adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Item.PowerSupply",
1110adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Decorator.Asset",
11118fb49dd6SShawn McCarney         "xyz.openbmc_project.State.Decorator.OperationalStatus"};
11128fb49dd6SShawn McCarney 
1113e99073f5SGeorge Liu     // Make call to ObjectMapper to find all inventory items
1114e99073f5SGeorge Liu     dbus::utility::getSubTree(
1115e99073f5SGeorge Liu         path, 0, interfaces,
11168cb2c024SEd Tanous         [callback = std::forward<Callback>(callback), sensorsAsyncResp,
1117002d39b4SEd Tanous          inventoryItems](
1118e99073f5SGeorge Liu             const boost::system::error_code& ec,
11194e0d8789SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) mutable {
1120e99073f5SGeorge Liu             // Response handler for parsing output from GetSubTree
112162598e31SEd Tanous             BMCWEB_LOG_DEBUG("getInventoryItemsConnections respHandler enter");
11228fb49dd6SShawn McCarney             if (ec)
11238fb49dd6SShawn McCarney             {
11248d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
112562598e31SEd Tanous                 BMCWEB_LOG_ERROR(
1126bd79bce8SPatrick Williams                     "getInventoryItemsConnections respHandler DBus error {}",
1127bd79bce8SPatrick Williams                     ec);
11288fb49dd6SShawn McCarney                 return;
11298fb49dd6SShawn McCarney             }
11308fb49dd6SShawn McCarney 
11318fb49dd6SShawn McCarney             // Make unique list of connections for desired inventory items
1132fe04d49cSNan Zhou             std::shared_ptr<std::set<std::string>> invConnections =
1133fe04d49cSNan Zhou                 std::make_shared<std::set<std::string>>();
11348fb49dd6SShawn McCarney 
11358fb49dd6SShawn McCarney             // Loop through objects from GetSubTree
1136bd79bce8SPatrick Williams             for (const std::pair<std::string,
1137bd79bce8SPatrick Williams                                  std::vector<std::pair<
1138bd79bce8SPatrick Williams                                      std::string, std::vector<std::string>>>>&
11398fb49dd6SShawn McCarney                      object : subtree)
11408fb49dd6SShawn McCarney             {
1141adc4f0dbSShawn McCarney                 // Check if object path is one of the specified inventory items
11428fb49dd6SShawn McCarney                 const std::string& objPath = object.first;
1143adc4f0dbSShawn McCarney                 if (findInventoryItem(inventoryItems, objPath) != nullptr)
11448fb49dd6SShawn McCarney                 {
11458fb49dd6SShawn McCarney                     // Store all connections to inventory item
11468fb49dd6SShawn McCarney                     for (const std::pair<std::string, std::vector<std::string>>&
11478fb49dd6SShawn McCarney                              objData : object.second)
11488fb49dd6SShawn McCarney                     {
11498fb49dd6SShawn McCarney                         const std::string& invConnection = objData.first;
11508fb49dd6SShawn McCarney                         invConnections->insert(invConnection);
11518fb49dd6SShawn McCarney                     }
11528fb49dd6SShawn McCarney                 }
11538fb49dd6SShawn McCarney             }
1154d500549bSAnthony Wilson 
11558fb49dd6SShawn McCarney             callback(invConnections);
115662598e31SEd Tanous             BMCWEB_LOG_DEBUG("getInventoryItemsConnections respHandler exit");
1157e99073f5SGeorge Liu         });
115862598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryItemsConnections exit");
11598fb49dd6SShawn McCarney }
11608fb49dd6SShawn McCarney 
11618fb49dd6SShawn McCarney /**
1162adc4f0dbSShawn McCarney  * @brief Gets associations from sensors to inventory items.
11638fb49dd6SShawn McCarney  *
11648fb49dd6SShawn McCarney  * Looks for ObjectMapper associations from the specified sensors to related
1165d500549bSAnthony Wilson  * inventory items. Then finds the associations from those inventory items to
1166d500549bSAnthony Wilson  * their LEDs, if any.
11678fb49dd6SShawn McCarney  *
11688fb49dd6SShawn McCarney  * Finds the inventory items asynchronously.  Invokes callback when information
11698fb49dd6SShawn McCarney  * has been obtained.
11708fb49dd6SShawn McCarney  *
11718fb49dd6SShawn McCarney  * The callback must have the following signature:
11728fb49dd6SShawn McCarney  *   @code
1173adc4f0dbSShawn McCarney  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
11748fb49dd6SShawn McCarney  *   @endcode
11758fb49dd6SShawn McCarney  *
11768fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
11778fb49dd6SShawn McCarney  * @param sensorNames All sensors within the current chassis.
11788fb49dd6SShawn McCarney  * implements ObjectManager.
11798fb49dd6SShawn McCarney  * @param callback Callback to invoke when inventory items have been obtained.
11808fb49dd6SShawn McCarney  */
11818fb49dd6SShawn McCarney template <typename Callback>
11824ff0f1f4SEd Tanous void getInventoryItemAssociations(
1183b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1184fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& sensorNames,
11858fb49dd6SShawn McCarney     Callback&& callback)
11868fb49dd6SShawn McCarney {
118762598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryItemAssociations enter");
11888fb49dd6SShawn McCarney 
11895eb468daSGeorge Liu     // Call GetManagedObjects on the ObjectMapper to get all associations
11905eb468daSGeorge Liu     sdbusplus::message::object_path path("/");
11915eb468daSGeorge Liu     dbus::utility::getManagedObjects(
11925eb468daSGeorge Liu         "xyz.openbmc_project.ObjectMapper", path,
11938cb2c024SEd Tanous         [callback = std::forward<Callback>(callback), sensorsAsyncResp,
11945e7e2dc5SEd Tanous          sensorNames](const boost::system::error_code& ec,
11954e0d8789SEd Tanous                       const dbus::utility::ManagedObjectType& resp) mutable {
119662598e31SEd Tanous             BMCWEB_LOG_DEBUG("getInventoryItemAssociations respHandler enter");
11978fb49dd6SShawn McCarney             if (ec)
11988fb49dd6SShawn McCarney             {
119962598e31SEd Tanous                 BMCWEB_LOG_ERROR(
1200bd79bce8SPatrick Williams                     "getInventoryItemAssociations respHandler DBus error {}",
1201bd79bce8SPatrick Williams                     ec);
12028d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
12038fb49dd6SShawn McCarney                 return;
12048fb49dd6SShawn McCarney             }
12058fb49dd6SShawn McCarney 
1206adc4f0dbSShawn McCarney             // Create vector to hold list of inventory items
1207adc4f0dbSShawn McCarney             std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1208adc4f0dbSShawn McCarney                 std::make_shared<std::vector<InventoryItem>>();
1209adc4f0dbSShawn McCarney 
12108fb49dd6SShawn McCarney             // Loop through returned object paths
12118fb49dd6SShawn McCarney             std::string sensorAssocPath;
12128fb49dd6SShawn McCarney             sensorAssocPath.reserve(128); // avoid memory allocations
12138fb49dd6SShawn McCarney             for (const auto& objDictEntry : resp)
12148fb49dd6SShawn McCarney             {
12158fb49dd6SShawn McCarney                 const std::string& objPath =
12168fb49dd6SShawn McCarney                     static_cast<const std::string&>(objDictEntry.first);
12178fb49dd6SShawn McCarney 
1218bd79bce8SPatrick Williams                 // If path is inventory association for one of the specified
1219bd79bce8SPatrick Williams                 // sensors
12208fb49dd6SShawn McCarney                 for (const std::string& sensorName : *sensorNames)
12218fb49dd6SShawn McCarney                 {
12228fb49dd6SShawn McCarney                     sensorAssocPath = sensorName;
12238fb49dd6SShawn McCarney                     sensorAssocPath += "/inventory";
12248fb49dd6SShawn McCarney                     if (objPath == sensorAssocPath)
12258fb49dd6SShawn McCarney                     {
12268fb49dd6SShawn McCarney                         // Get Association interface for object path
1227bd79bce8SPatrick Williams                         for (const auto& [interface, values] :
1228bd79bce8SPatrick Williams                              objDictEntry.second)
12298fb49dd6SShawn McCarney                         {
1230711ac7a9SEd Tanous                             if (interface == "xyz.openbmc_project.Association")
1231711ac7a9SEd Tanous                             {
1232711ac7a9SEd Tanous                                 for (const auto& [valueName, value] : values)
1233711ac7a9SEd Tanous                                 {
1234711ac7a9SEd Tanous                                     if (valueName == "endpoints")
12358fb49dd6SShawn McCarney                                     {
1236bd79bce8SPatrick Williams                                         const std::vector<std::string>*
1237bd79bce8SPatrick Williams                                             endpoints = std::get_if<
1238bd79bce8SPatrick Williams                                                 std::vector<std::string>>(
1239711ac7a9SEd Tanous                                                 &value);
1240711ac7a9SEd Tanous                                         if ((endpoints != nullptr) &&
1241711ac7a9SEd Tanous                                             !endpoints->empty())
12428fb49dd6SShawn McCarney                                         {
1243adc4f0dbSShawn McCarney                                             // Add inventory item to vector
1244adc4f0dbSShawn McCarney                                             const std::string& invItemPath =
1245adc4f0dbSShawn McCarney                                                 endpoints->front();
1246711ac7a9SEd Tanous                                             addInventoryItem(inventoryItems,
1247711ac7a9SEd Tanous                                                              invItemPath,
1248adc4f0dbSShawn McCarney                                                              sensorName);
12498fb49dd6SShawn McCarney                                         }
12508fb49dd6SShawn McCarney                                     }
12518fb49dd6SShawn McCarney                                 }
1252711ac7a9SEd Tanous                             }
1253711ac7a9SEd Tanous                         }
12548fb49dd6SShawn McCarney                         break;
12558fb49dd6SShawn McCarney                     }
12568fb49dd6SShawn McCarney                 }
12578fb49dd6SShawn McCarney             }
12588fb49dd6SShawn McCarney 
1259d500549bSAnthony Wilson             // Now loop through the returned object paths again, this time to
1260d500549bSAnthony Wilson             // find the leds associated with the inventory items we just found
1261d500549bSAnthony Wilson             std::string inventoryAssocPath;
1262d500549bSAnthony Wilson             inventoryAssocPath.reserve(128); // avoid memory allocations
1263d500549bSAnthony Wilson             for (const auto& objDictEntry : resp)
1264d500549bSAnthony Wilson             {
1265d500549bSAnthony Wilson                 const std::string& objPath =
1266d500549bSAnthony Wilson                     static_cast<const std::string&>(objDictEntry.first);
1267d500549bSAnthony Wilson 
1268d500549bSAnthony Wilson                 for (InventoryItem& inventoryItem : *inventoryItems)
1269d500549bSAnthony Wilson                 {
1270d500549bSAnthony Wilson                     inventoryAssocPath = inventoryItem.objectPath;
1271d500549bSAnthony Wilson                     inventoryAssocPath += "/leds";
1272d500549bSAnthony Wilson                     if (objPath == inventoryAssocPath)
1273d500549bSAnthony Wilson                     {
1274bd79bce8SPatrick Williams                         for (const auto& [interface, values] :
1275bd79bce8SPatrick Williams                              objDictEntry.second)
1276d500549bSAnthony Wilson                         {
1277711ac7a9SEd Tanous                             if (interface == "xyz.openbmc_project.Association")
1278711ac7a9SEd Tanous                             {
1279711ac7a9SEd Tanous                                 for (const auto& [valueName, value] : values)
1280711ac7a9SEd Tanous                                 {
1281711ac7a9SEd Tanous                                     if (valueName == "endpoints")
1282d500549bSAnthony Wilson                                     {
1283bd79bce8SPatrick Williams                                         const std::vector<std::string>*
1284bd79bce8SPatrick Williams                                             endpoints = std::get_if<
1285bd79bce8SPatrick Williams                                                 std::vector<std::string>>(
1286711ac7a9SEd Tanous                                                 &value);
1287711ac7a9SEd Tanous                                         if ((endpoints != nullptr) &&
1288711ac7a9SEd Tanous                                             !endpoints->empty())
1289d500549bSAnthony Wilson                                         {
1290711ac7a9SEd Tanous                                             // Add inventory item to vector
1291d500549bSAnthony Wilson                                             // Store LED path in inventory item
1292711ac7a9SEd Tanous                                             const std::string& ledPath =
1293711ac7a9SEd Tanous                                                 endpoints->front();
1294bd79bce8SPatrick Williams                                             inventoryItem.ledObjectPath =
1295bd79bce8SPatrick Williams                                                 ledPath;
1296d500549bSAnthony Wilson                                         }
1297d500549bSAnthony Wilson                                     }
1298d500549bSAnthony Wilson                                 }
1299711ac7a9SEd Tanous                             }
1300711ac7a9SEd Tanous                         }
1301711ac7a9SEd Tanous 
1302d500549bSAnthony Wilson                         break;
1303d500549bSAnthony Wilson                     }
1304d500549bSAnthony Wilson                 }
1305d500549bSAnthony Wilson             }
1306adc4f0dbSShawn McCarney             callback(inventoryItems);
130762598e31SEd Tanous             BMCWEB_LOG_DEBUG("getInventoryItemAssociations respHandler exit");
13085eb468daSGeorge Liu         });
13098fb49dd6SShawn McCarney 
131062598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryItemAssociations exit");
13118fb49dd6SShawn McCarney }
13128fb49dd6SShawn McCarney 
13138fb49dd6SShawn McCarney /**
1314d500549bSAnthony Wilson  * @brief Gets D-Bus data for inventory item leds associated with sensors.
1315d500549bSAnthony Wilson  *
1316d500549bSAnthony Wilson  * Uses the specified connections (services) to obtain D-Bus data for inventory
1317d500549bSAnthony Wilson  * item leds associated with sensors.  Stores the resulting data in the
1318d500549bSAnthony Wilson  * inventoryItems vector.
1319d500549bSAnthony Wilson  *
1320d500549bSAnthony Wilson  * This data is later used to provide sensor property values in the JSON
1321d500549bSAnthony Wilson  * response.
1322d500549bSAnthony Wilson  *
1323d500549bSAnthony Wilson  * Finds the inventory item led data asynchronously.  Invokes callback when data
1324d500549bSAnthony Wilson  * has been obtained.
1325d500549bSAnthony Wilson  *
1326d500549bSAnthony Wilson  * The callback must have the following signature:
1327d500549bSAnthony Wilson  *   @code
132842cbe538SGunnar Mills  *   callback()
1329d500549bSAnthony Wilson  *   @endcode
1330d500549bSAnthony Wilson  *
1331d500549bSAnthony Wilson  * This function is called recursively, obtaining data asynchronously from one
1332d500549bSAnthony Wilson  * connection in each call.  This ensures the callback is not invoked until the
1333d500549bSAnthony Wilson  * last asynchronous function has completed.
1334d500549bSAnthony Wilson  *
1335d500549bSAnthony Wilson  * @param sensorsAsyncResp Pointer to object holding response data.
1336d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1337d500549bSAnthony Wilson  * @param ledConnections Connections that provide data for the inventory leds.
1338d500549bSAnthony Wilson  * @param callback Callback to invoke when inventory data has been obtained.
1339d500549bSAnthony Wilson  * @param ledConnectionsIndex Current index in ledConnections.  Only specified
1340d500549bSAnthony Wilson  * in recursive calls to this function.
1341d500549bSAnthony Wilson  */
1342d500549bSAnthony Wilson template <typename Callback>
1343d500549bSAnthony Wilson void getInventoryLedData(
1344daadfb2eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1345daadfb2eSEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1346daadfb2eSEd Tanous     const std::shared_ptr<std::map<std::string, std::string>>& ledConnections,
1347d500549bSAnthony Wilson     Callback&& callback, size_t ledConnectionsIndex = 0)
1348d500549bSAnthony Wilson {
134962598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryLedData enter");
1350d500549bSAnthony Wilson 
1351d500549bSAnthony Wilson     // If no more connections left, call callback
1352d500549bSAnthony Wilson     if (ledConnectionsIndex >= ledConnections->size())
1353d500549bSAnthony Wilson     {
135442cbe538SGunnar Mills         callback();
135562598e31SEd Tanous         BMCWEB_LOG_DEBUG("getInventoryLedData exit");
1356d500549bSAnthony Wilson         return;
1357d500549bSAnthony Wilson     }
1358d500549bSAnthony Wilson 
1359d500549bSAnthony Wilson     // Get inventory item data from current connection
1360fe04d49cSNan Zhou     auto it = ledConnections->begin();
1361fe04d49cSNan Zhou     std::advance(it, ledConnectionsIndex);
1362d500549bSAnthony Wilson     if (it != ledConnections->end())
1363d500549bSAnthony Wilson     {
1364d500549bSAnthony Wilson         const std::string& ledPath = (*it).first;
1365d500549bSAnthony Wilson         const std::string& ledConnection = (*it).second;
1366d500549bSAnthony Wilson         // Response handler for Get State property
13671e1e598dSJonathan Doman         auto respHandler =
13681e1e598dSJonathan Doman             [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
13694e0d8789SEd Tanous              callback = std::forward<Callback>(callback),
13704e0d8789SEd Tanous              ledConnectionsIndex](const boost::system::error_code& ec,
13714e0d8789SEd Tanous                                   const std::string& state) mutable {
137262598e31SEd Tanous                 BMCWEB_LOG_DEBUG("getInventoryLedData respHandler enter");
1373d500549bSAnthony Wilson                 if (ec)
1374d500549bSAnthony Wilson                 {
137562598e31SEd Tanous                     BMCWEB_LOG_ERROR(
137662598e31SEd Tanous                         "getInventoryLedData respHandler DBus error {}", ec);
13778d1b46d7Szhanghch05                     messages::internalError(sensorsAsyncResp->asyncResp->res);
1378d500549bSAnthony Wilson                     return;
1379d500549bSAnthony Wilson                 }
1380d500549bSAnthony Wilson 
138162598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Led state: {}", state);
1382d500549bSAnthony Wilson                 // Find inventory item with this LED object path
1383d500549bSAnthony Wilson                 InventoryItem* inventoryItem =
1384d500549bSAnthony Wilson                     findInventoryItemForLed(*inventoryItems, ledPath);
1385d500549bSAnthony Wilson                 if (inventoryItem != nullptr)
1386d500549bSAnthony Wilson                 {
1387d500549bSAnthony Wilson                     // Store LED state in InventoryItem
138811ba3979SEd Tanous                     if (state.ends_with("On"))
1389d500549bSAnthony Wilson                     {
1390c9563608SJanet Adkins                         inventoryItem->ledState = sensor_utils::LedState::ON;
1391d500549bSAnthony Wilson                     }
139211ba3979SEd Tanous                     else if (state.ends_with("Blink"))
1393d500549bSAnthony Wilson                     {
1394c9563608SJanet Adkins                         inventoryItem->ledState = sensor_utils::LedState::BLINK;
1395d500549bSAnthony Wilson                     }
139611ba3979SEd Tanous                     else if (state.ends_with("Off"))
1397d500549bSAnthony Wilson                     {
1398c9563608SJanet Adkins                         inventoryItem->ledState = sensor_utils::LedState::OFF;
1399d500549bSAnthony Wilson                     }
1400d500549bSAnthony Wilson                     else
1401d500549bSAnthony Wilson                     {
1402c9563608SJanet Adkins                         inventoryItem->ledState =
1403c9563608SJanet Adkins                             sensor_utils::LedState::UNKNOWN;
1404d500549bSAnthony Wilson                     }
1405d500549bSAnthony Wilson                 }
1406d500549bSAnthony Wilson 
1407d500549bSAnthony Wilson                 // Recurse to get LED data from next connection
1408d500549bSAnthony Wilson                 getInventoryLedData(sensorsAsyncResp, inventoryItems,
1409d500549bSAnthony Wilson                                     ledConnections, std::move(callback),
1410d500549bSAnthony Wilson                                     ledConnectionsIndex + 1);
1411d500549bSAnthony Wilson 
141262598e31SEd Tanous                 BMCWEB_LOG_DEBUG("getInventoryLedData respHandler exit");
1413d500549bSAnthony Wilson             };
1414d500549bSAnthony Wilson 
1415d500549bSAnthony Wilson         // Get the State property for the current LED
1416deae6a78SEd Tanous         dbus::utility::getProperty<std::string>(
1417deae6a78SEd Tanous             ledConnection, ledPath, "xyz.openbmc_project.Led.Physical", "State",
14181e1e598dSJonathan Doman             std::move(respHandler));
1419d500549bSAnthony Wilson     }
1420d500549bSAnthony Wilson 
142162598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryLedData exit");
1422d500549bSAnthony Wilson }
1423d500549bSAnthony Wilson 
1424d500549bSAnthony Wilson /**
1425d500549bSAnthony Wilson  * @brief Gets LED data for LEDs associated with given inventory items.
1426d500549bSAnthony Wilson  *
1427d500549bSAnthony Wilson  * Gets the D-Bus connections (services) that provide LED data for the LEDs
1428d500549bSAnthony Wilson  * associated with the specified inventory items.  Then gets the LED data from
1429d500549bSAnthony Wilson  * each connection and stores it in the inventory item.
1430d500549bSAnthony Wilson  *
1431d500549bSAnthony Wilson  * This data is later used to provide sensor property values in the JSON
1432d500549bSAnthony Wilson  * response.
1433d500549bSAnthony Wilson  *
1434d500549bSAnthony Wilson  * Finds the LED data asynchronously.  Invokes callback when information has
1435d500549bSAnthony Wilson  * been obtained.
1436d500549bSAnthony Wilson  *
1437d500549bSAnthony Wilson  * The callback must have the following signature:
1438d500549bSAnthony Wilson  *   @code
143942cbe538SGunnar Mills  *   callback()
1440d500549bSAnthony Wilson  *   @endcode
1441d500549bSAnthony Wilson  *
1442d500549bSAnthony Wilson  * @param sensorsAsyncResp Pointer to object holding response data.
1443d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1444d500549bSAnthony Wilson  * @param callback Callback to invoke when inventory items have been obtained.
1445d500549bSAnthony Wilson  */
1446d500549bSAnthony Wilson template <typename Callback>
1447d500549bSAnthony Wilson void getInventoryLeds(
1448daadfb2eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1449daadfb2eSEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1450d500549bSAnthony Wilson     Callback&& callback)
1451d500549bSAnthony Wilson {
145262598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryLeds enter");
1453d500549bSAnthony Wilson 
1454d500549bSAnthony Wilson     const std::string path = "/xyz/openbmc_project";
1455e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
1456d500549bSAnthony Wilson         "xyz.openbmc_project.Led.Physical"};
1457d500549bSAnthony Wilson 
1458e99073f5SGeorge Liu     // Make call to ObjectMapper to find all inventory items
1459e99073f5SGeorge Liu     dbus::utility::getSubTree(
1460e99073f5SGeorge Liu         path, 0, interfaces,
14618cb2c024SEd Tanous         [callback = std::forward<Callback>(callback), sensorsAsyncResp,
1462002d39b4SEd Tanous          inventoryItems](
1463e99073f5SGeorge Liu             const boost::system::error_code& ec,
14644e0d8789SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) mutable {
1465e99073f5SGeorge Liu             // Response handler for parsing output from GetSubTree
146662598e31SEd Tanous             BMCWEB_LOG_DEBUG("getInventoryLeds respHandler enter");
1467d500549bSAnthony Wilson             if (ec)
1468d500549bSAnthony Wilson             {
14698d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
1470bd79bce8SPatrick Williams                 BMCWEB_LOG_ERROR("getInventoryLeds respHandler DBus error {}",
1471bd79bce8SPatrick Williams                                  ec);
1472d500549bSAnthony Wilson                 return;
1473d500549bSAnthony Wilson             }
1474d500549bSAnthony Wilson 
1475d500549bSAnthony Wilson             // Build map of LED object paths to connections
1476fe04d49cSNan Zhou             std::shared_ptr<std::map<std::string, std::string>> ledConnections =
1477fe04d49cSNan Zhou                 std::make_shared<std::map<std::string, std::string>>();
1478d500549bSAnthony Wilson 
1479d500549bSAnthony Wilson             // Loop through objects from GetSubTree
1480bd79bce8SPatrick Williams             for (const std::pair<std::string,
1481bd79bce8SPatrick Williams                                  std::vector<std::pair<
1482bd79bce8SPatrick Williams                                      std::string, std::vector<std::string>>>>&
1483d500549bSAnthony Wilson                      object : subtree)
1484d500549bSAnthony Wilson             {
1485bd79bce8SPatrick Williams                 // Check if object path is LED for one of the specified
1486bd79bce8SPatrick Williams                 // inventory items
1487d500549bSAnthony Wilson                 const std::string& ledPath = object.first;
1488bd79bce8SPatrick Williams                 if (findInventoryItemForLed(*inventoryItems, ledPath) !=
1489bd79bce8SPatrick Williams                     nullptr)
1490d500549bSAnthony Wilson                 {
1491d500549bSAnthony Wilson                     // Add mapping from ledPath to connection
1492bd79bce8SPatrick Williams                     const std::string& connection =
1493bd79bce8SPatrick Williams                         object.second.begin()->first;
1494d500549bSAnthony Wilson                     (*ledConnections)[ledPath] = connection;
1495bd79bce8SPatrick Williams                     BMCWEB_LOG_DEBUG("Added mapping {} -> {}", ledPath,
1496bd79bce8SPatrick Williams                                      connection);
1497d500549bSAnthony Wilson                 }
1498d500549bSAnthony Wilson             }
1499d500549bSAnthony Wilson 
1500bd79bce8SPatrick Williams             getInventoryLedData(sensorsAsyncResp, inventoryItems,
1501bd79bce8SPatrick Williams                                 ledConnections, std::move(callback));
150262598e31SEd Tanous             BMCWEB_LOG_DEBUG("getInventoryLeds respHandler exit");
1503e99073f5SGeorge Liu         });
150462598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryLeds exit");
1505d500549bSAnthony Wilson }
1506d500549bSAnthony Wilson 
1507d500549bSAnthony Wilson /**
150842cbe538SGunnar Mills  * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
150942cbe538SGunnar Mills  *
151042cbe538SGunnar Mills  * Uses the specified connections (services) (currently assumes just one) to
151142cbe538SGunnar Mills  * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
151242cbe538SGunnar Mills  * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
151342cbe538SGunnar Mills  *
151442cbe538SGunnar Mills  * This data is later used to provide sensor property values in the JSON
151542cbe538SGunnar Mills  * response.
151642cbe538SGunnar Mills  *
151742cbe538SGunnar Mills  * Finds the Power Supply Attributes data asynchronously.  Invokes callback
151842cbe538SGunnar Mills  * when data has been obtained.
151942cbe538SGunnar Mills  *
152042cbe538SGunnar Mills  * The callback must have the following signature:
152142cbe538SGunnar Mills  *   @code
152242cbe538SGunnar Mills  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
152342cbe538SGunnar Mills  *   @endcode
152442cbe538SGunnar Mills  *
152542cbe538SGunnar Mills  * @param sensorsAsyncResp Pointer to object holding response data.
152642cbe538SGunnar Mills  * @param inventoryItems D-Bus inventory items associated with sensors.
152742cbe538SGunnar Mills  * @param psAttributesConnections Connections that provide data for the Power
152842cbe538SGunnar Mills  *        Supply Attributes
152942cbe538SGunnar Mills  * @param callback Callback to invoke when data has been obtained.
153042cbe538SGunnar Mills  */
153142cbe538SGunnar Mills template <typename Callback>
153242cbe538SGunnar Mills void getPowerSupplyAttributesData(
1533b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1534daadfb2eSEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1535fe04d49cSNan Zhou     const std::map<std::string, std::string>& psAttributesConnections,
153642cbe538SGunnar Mills     Callback&& callback)
153742cbe538SGunnar Mills {
153862598e31SEd Tanous     BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData enter");
153942cbe538SGunnar Mills 
154042cbe538SGunnar Mills     if (psAttributesConnections.empty())
154142cbe538SGunnar Mills     {
154262598e31SEd Tanous         BMCWEB_LOG_DEBUG("Can't find PowerSupplyAttributes, no connections!");
154342cbe538SGunnar Mills         callback(inventoryItems);
154442cbe538SGunnar Mills         return;
154542cbe538SGunnar Mills     }
154642cbe538SGunnar Mills 
154742cbe538SGunnar Mills     // Assuming just one connection (service) for now
1548fe04d49cSNan Zhou     auto it = psAttributesConnections.begin();
154942cbe538SGunnar Mills 
155042cbe538SGunnar Mills     const std::string& psAttributesPath = (*it).first;
155142cbe538SGunnar Mills     const std::string& psAttributesConnection = (*it).second;
155242cbe538SGunnar Mills 
155342cbe538SGunnar Mills     // Response handler for Get DeratingFactor property
15545a39f77aSPatrick Williams     auto respHandler = [sensorsAsyncResp, inventoryItems,
15558cb2c024SEd Tanous                         callback = std::forward<Callback>(callback)](
15565a39f77aSPatrick Williams                            const boost::system::error_code& ec,
15574e0d8789SEd Tanous                            uint32_t value) mutable {
155862598e31SEd Tanous         BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData respHandler enter");
155942cbe538SGunnar Mills         if (ec)
156042cbe538SGunnar Mills         {
156162598e31SEd Tanous             BMCWEB_LOG_ERROR(
156262598e31SEd Tanous                 "getPowerSupplyAttributesData respHandler DBus error {}", ec);
15638d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
156442cbe538SGunnar Mills             return;
156542cbe538SGunnar Mills         }
156642cbe538SGunnar Mills 
156762598e31SEd Tanous         BMCWEB_LOG_DEBUG("PS EfficiencyPercent value: {}", value);
156842cbe538SGunnar Mills         // Store value in Power Supply Inventory Items
156942cbe538SGunnar Mills         for (InventoryItem& inventoryItem : *inventoryItems)
157042cbe538SGunnar Mills         {
157155f79e6fSEd Tanous             if (inventoryItem.isPowerSupply)
157242cbe538SGunnar Mills             {
157342cbe538SGunnar Mills                 inventoryItem.powerSupplyEfficiencyPercent =
15741e1e598dSJonathan Doman                     static_cast<int>(value);
157542cbe538SGunnar Mills             }
157642cbe538SGunnar Mills         }
157742cbe538SGunnar Mills 
157862598e31SEd Tanous         BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData respHandler exit");
157942cbe538SGunnar Mills         callback(inventoryItems);
158042cbe538SGunnar Mills     };
158142cbe538SGunnar Mills 
158242cbe538SGunnar Mills     // Get the DeratingFactor property for the PowerSupplyAttributes
158342cbe538SGunnar Mills     // Currently only property on the interface/only one we care about
1584deae6a78SEd Tanous     dbus::utility::getProperty<uint32_t>(
1585deae6a78SEd Tanous         psAttributesConnection, psAttributesPath,
15861e1e598dSJonathan Doman         "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor",
15871e1e598dSJonathan Doman         std::move(respHandler));
158842cbe538SGunnar Mills 
158962598e31SEd Tanous     BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData exit");
159042cbe538SGunnar Mills }
159142cbe538SGunnar Mills 
159242cbe538SGunnar Mills /**
159342cbe538SGunnar Mills  * @brief Gets the Power Supply Attributes such as EfficiencyPercent
159442cbe538SGunnar Mills  *
159542cbe538SGunnar Mills  * Gets the D-Bus connection (service) that provides Power Supply Attributes
159642cbe538SGunnar Mills  * data. Then gets the Power Supply Attributes data from the connection
159742cbe538SGunnar Mills  * (currently just assumes 1 connection) and stores the data in the inventory
159842cbe538SGunnar Mills  * item.
159942cbe538SGunnar Mills  *
160042cbe538SGunnar Mills  * This data is later used to provide sensor property values in the JSON
160142cbe538SGunnar Mills  * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
160242cbe538SGunnar Mills  *
160342cbe538SGunnar Mills  * Finds the Power Supply Attributes data asynchronously. Invokes callback
160442cbe538SGunnar Mills  * when information has been obtained.
160542cbe538SGunnar Mills  *
160642cbe538SGunnar Mills  * The callback must have the following signature:
160742cbe538SGunnar Mills  *   @code
160842cbe538SGunnar Mills  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
160942cbe538SGunnar Mills  *   @endcode
161042cbe538SGunnar Mills  *
161142cbe538SGunnar Mills  * @param sensorsAsyncResp Pointer to object holding response data.
161242cbe538SGunnar Mills  * @param inventoryItems D-Bus inventory items associated with sensors.
161342cbe538SGunnar Mills  * @param callback Callback to invoke when data has been obtained.
161442cbe538SGunnar Mills  */
161542cbe538SGunnar Mills template <typename Callback>
161642cbe538SGunnar Mills void getPowerSupplyAttributes(
1617daadfb2eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1618daadfb2eSEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
161942cbe538SGunnar Mills     Callback&& callback)
162042cbe538SGunnar Mills {
162162598e31SEd Tanous     BMCWEB_LOG_DEBUG("getPowerSupplyAttributes enter");
162242cbe538SGunnar Mills 
162342cbe538SGunnar Mills     // Only need the power supply attributes when the Power Schema
16240c728b42SJanet Adkins     if (sensorsAsyncResp->chassisSubNode != sensors::powerNodeStr)
162542cbe538SGunnar Mills     {
162662598e31SEd Tanous         BMCWEB_LOG_DEBUG("getPowerSupplyAttributes exit since not Power");
162742cbe538SGunnar Mills         callback(inventoryItems);
162842cbe538SGunnar Mills         return;
162942cbe538SGunnar Mills     }
163042cbe538SGunnar Mills 
1631e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
163242cbe538SGunnar Mills         "xyz.openbmc_project.Control.PowerSupplyAttributes"};
163342cbe538SGunnar Mills 
1634e99073f5SGeorge Liu     // Make call to ObjectMapper to find the PowerSupplyAttributes service
1635e99073f5SGeorge Liu     dbus::utility::getSubTree(
1636e99073f5SGeorge Liu         "/xyz/openbmc_project", 0, interfaces,
16378cb2c024SEd Tanous         [callback = std::forward<Callback>(callback), sensorsAsyncResp,
1638b9d36b47SEd Tanous          inventoryItems](
1639e99073f5SGeorge Liu             const boost::system::error_code& ec,
16404e0d8789SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) mutable {
1641e99073f5SGeorge Liu             // Response handler for parsing output from GetSubTree
164262598e31SEd Tanous             BMCWEB_LOG_DEBUG("getPowerSupplyAttributes respHandler enter");
164342cbe538SGunnar Mills             if (ec)
164442cbe538SGunnar Mills             {
16458d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
164662598e31SEd Tanous                 BMCWEB_LOG_ERROR(
164762598e31SEd Tanous                     "getPowerSupplyAttributes respHandler DBus error {}", ec);
164842cbe538SGunnar Mills                 return;
164942cbe538SGunnar Mills             }
165026f6976fSEd Tanous             if (subtree.empty())
165142cbe538SGunnar Mills             {
165262598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Can't find Power Supply Attributes!");
165342cbe538SGunnar Mills                 callback(inventoryItems);
165442cbe538SGunnar Mills                 return;
165542cbe538SGunnar Mills             }
165642cbe538SGunnar Mills 
165742cbe538SGunnar Mills             // Currently we only support 1 power supply attribute, use this for
165842cbe538SGunnar Mills             // all the power supplies. Build map of object path to connection.
165942cbe538SGunnar Mills             // Assume just 1 connection and 1 path for now.
1660fe04d49cSNan Zhou             std::map<std::string, std::string> psAttributesConnections;
166142cbe538SGunnar Mills 
166242cbe538SGunnar Mills             if (subtree[0].first.empty() || subtree[0].second.empty())
166342cbe538SGunnar Mills             {
166462598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Power Supply Attributes mapper error!");
166542cbe538SGunnar Mills                 callback(inventoryItems);
166642cbe538SGunnar Mills                 return;
166742cbe538SGunnar Mills             }
166842cbe538SGunnar Mills 
166942cbe538SGunnar Mills             const std::string& psAttributesPath = subtree[0].first;
167042cbe538SGunnar Mills             const std::string& connection = subtree[0].second.begin()->first;
167142cbe538SGunnar Mills 
167242cbe538SGunnar Mills             if (connection.empty())
167342cbe538SGunnar Mills             {
167462598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Power Supply Attributes mapper error!");
167542cbe538SGunnar Mills                 callback(inventoryItems);
167642cbe538SGunnar Mills                 return;
167742cbe538SGunnar Mills             }
167842cbe538SGunnar Mills 
167942cbe538SGunnar Mills             psAttributesConnections[psAttributesPath] = connection;
168062598e31SEd Tanous             BMCWEB_LOG_DEBUG("Added mapping {} -> {}", psAttributesPath,
168162598e31SEd Tanous                              connection);
168242cbe538SGunnar Mills 
168342cbe538SGunnar Mills             getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
168442cbe538SGunnar Mills                                          psAttributesConnections,
168542cbe538SGunnar Mills                                          std::move(callback));
168662598e31SEd Tanous             BMCWEB_LOG_DEBUG("getPowerSupplyAttributes respHandler exit");
1687e99073f5SGeorge Liu         });
168862598e31SEd Tanous     BMCWEB_LOG_DEBUG("getPowerSupplyAttributes exit");
168942cbe538SGunnar Mills }
169042cbe538SGunnar Mills 
169142cbe538SGunnar Mills /**
1692adc4f0dbSShawn McCarney  * @brief Gets inventory items associated with sensors.
16938fb49dd6SShawn McCarney  *
16948fb49dd6SShawn McCarney  * Finds the inventory items that are associated with the specified sensors.
1695adc4f0dbSShawn McCarney  * Then gets D-Bus data for the inventory items, such as presence and VPD.
16968fb49dd6SShawn McCarney  *
1697adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
1698adc4f0dbSShawn McCarney  * response.
16998fb49dd6SShawn McCarney  *
1700adc4f0dbSShawn McCarney  * Finds the inventory items asynchronously.  Invokes callback when the
1701adc4f0dbSShawn McCarney  * inventory items have been obtained.
1702adc4f0dbSShawn McCarney  *
1703adc4f0dbSShawn McCarney  * The callback must have the following signature:
1704adc4f0dbSShawn McCarney  *   @code
1705adc4f0dbSShawn McCarney  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
1706adc4f0dbSShawn McCarney  *   @endcode
17078fb49dd6SShawn McCarney  *
17088fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
17098fb49dd6SShawn McCarney  * @param sensorNames All sensors within the current chassis.
17108fb49dd6SShawn McCarney  * implements ObjectManager.
1711adc4f0dbSShawn McCarney  * @param callback Callback to invoke when inventory items have been obtained.
17128fb49dd6SShawn McCarney  */
1713adc4f0dbSShawn McCarney template <typename Callback>
1714504af5a0SPatrick Williams inline void getInventoryItems(
1715504af5a0SPatrick Williams     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1716daadfb2eSEd Tanous     const std::shared_ptr<std::set<std::string>>& sensorNames,
1717adc4f0dbSShawn McCarney     Callback&& callback)
17188fb49dd6SShawn McCarney {
171962598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryItems enter");
1720adc4f0dbSShawn McCarney     auto getInventoryItemAssociationsCb =
17218cb2c024SEd Tanous         [sensorsAsyncResp, callback = std::forward<Callback>(callback)](
1722daadfb2eSEd Tanous             const std::shared_ptr<std::vector<InventoryItem>>&
17234e0d8789SEd Tanous                 inventoryItems) mutable {
172462598e31SEd Tanous             BMCWEB_LOG_DEBUG("getInventoryItemAssociationsCb enter");
17258fb49dd6SShawn McCarney             auto getInventoryItemsConnectionsCb =
1726d0090733SEd Tanous                 [sensorsAsyncResp, inventoryItems,
17274e0d8789SEd Tanous                  callback = std::forward<Callback>(callback)](
1728daadfb2eSEd Tanous                     const std::shared_ptr<std::set<std::string>>&
17294e0d8789SEd Tanous                         invConnections) mutable {
173062598e31SEd Tanous                     BMCWEB_LOG_DEBUG("getInventoryItemsConnectionsCb enter");
1731bd79bce8SPatrick Williams                     auto getInventoryItemsDataCb =
1732bd79bce8SPatrick Williams                         [sensorsAsyncResp, inventoryItems,
17334e0d8789SEd Tanous                          callback =
17344e0d8789SEd Tanous                              std::forward<Callback>(callback)]() mutable {
173562598e31SEd Tanous                             BMCWEB_LOG_DEBUG("getInventoryItemsDataCb enter");
173642cbe538SGunnar Mills 
1737bd79bce8SPatrick Williams                             auto getInventoryLedsCb =
1738bd79bce8SPatrick Williams                                 [sensorsAsyncResp, inventoryItems,
17394e0d8789SEd Tanous                                  callback = std::forward<Callback>(
17404e0d8789SEd Tanous                                      callback)]() mutable {
1741bd79bce8SPatrick Williams                                     BMCWEB_LOG_DEBUG(
1742bd79bce8SPatrick Williams                                         "getInventoryLedsCb enter");
1743bd79bce8SPatrick Williams                                     // Find Power Supply Attributes and get the
1744bd79bce8SPatrick Williams                                     // data
1745bd79bce8SPatrick Williams                                     getPowerSupplyAttributes(
1746bd79bce8SPatrick Williams                                         sensorsAsyncResp, inventoryItems,
174742cbe538SGunnar Mills                                         std::move(callback));
174862598e31SEd Tanous                                     BMCWEB_LOG_DEBUG("getInventoryLedsCb exit");
174942cbe538SGunnar Mills                                 };
175042cbe538SGunnar Mills 
1751d500549bSAnthony Wilson                             // Find led connections and get the data
1752d500549bSAnthony Wilson                             getInventoryLeds(sensorsAsyncResp, inventoryItems,
175342cbe538SGunnar Mills                                              std::move(getInventoryLedsCb));
175462598e31SEd Tanous                             BMCWEB_LOG_DEBUG("getInventoryItemsDataCb exit");
1755d500549bSAnthony Wilson                         };
17568fb49dd6SShawn McCarney 
1757adc4f0dbSShawn McCarney                     // Get inventory item data from connections
1758adc4f0dbSShawn McCarney                     getInventoryItemsData(sensorsAsyncResp, inventoryItems,
1759d0090733SEd Tanous                                           invConnections,
1760d500549bSAnthony Wilson                                           std::move(getInventoryItemsDataCb));
176162598e31SEd Tanous                     BMCWEB_LOG_DEBUG("getInventoryItemsConnectionsCb exit");
17628fb49dd6SShawn McCarney                 };
17638fb49dd6SShawn McCarney 
1764adc4f0dbSShawn McCarney             // Get connections that provide inventory item data
1765bd79bce8SPatrick Williams             getInventoryItemsConnections(
1766bd79bce8SPatrick Williams                 sensorsAsyncResp, inventoryItems,
17678fb49dd6SShawn McCarney                 std::move(getInventoryItemsConnectionsCb));
176862598e31SEd Tanous             BMCWEB_LOG_DEBUG("getInventoryItemAssociationsCb exit");
17698fb49dd6SShawn McCarney         };
17708fb49dd6SShawn McCarney 
1771adc4f0dbSShawn McCarney     // Get associations from sensors to inventory items
1772d0090733SEd Tanous     getInventoryItemAssociations(sensorsAsyncResp, sensorNames,
1773adc4f0dbSShawn McCarney                                  std::move(getInventoryItemAssociationsCb));
177462598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryItems exit");
1775adc4f0dbSShawn McCarney }
1776adc4f0dbSShawn McCarney 
1777adc4f0dbSShawn McCarney /**
1778adc4f0dbSShawn McCarney  * @brief Returns JSON PowerSupply object for the specified inventory item.
1779adc4f0dbSShawn McCarney  *
1780adc4f0dbSShawn McCarney  * Searches for a JSON PowerSupply object that matches the specified inventory
1781adc4f0dbSShawn McCarney  * item.  If one is not found, a new PowerSupply object is added to the JSON
1782adc4f0dbSShawn McCarney  * array.
1783adc4f0dbSShawn McCarney  *
1784adc4f0dbSShawn McCarney  * Multiple sensors are often associated with one power supply inventory item.
1785adc4f0dbSShawn McCarney  * As a result, multiple sensor values are stored in one JSON PowerSupply
1786adc4f0dbSShawn McCarney  * object.
1787adc4f0dbSShawn McCarney  *
1788adc4f0dbSShawn McCarney  * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
1789adc4f0dbSShawn McCarney  * @param inventoryItem Inventory item for the power supply.
1790adc4f0dbSShawn McCarney  * @param chassisId Chassis that contains the power supply.
1791adc4f0dbSShawn McCarney  * @return JSON PowerSupply object for the specified inventory item.
1792adc4f0dbSShawn McCarney  */
179323a21a1cSEd Tanous inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
1794adc4f0dbSShawn McCarney                                       const InventoryItem& inventoryItem,
1795adc4f0dbSShawn McCarney                                       const std::string& chassisId)
1796adc4f0dbSShawn McCarney {
179718f8f608SEd Tanous     std::string nameS;
17986f4bd290SAlexander Hansen     nameS.resize(inventoryItem.name.size());
179918f8f608SEd Tanous     std::ranges::replace_copy(inventoryItem.name, nameS.begin(), '_', ' ');
1800adc4f0dbSShawn McCarney     // Check if matching PowerSupply object already exists in JSON array
1801adc4f0dbSShawn McCarney     for (nlohmann::json& powerSupply : powerSupplyArray)
1802adc4f0dbSShawn McCarney     {
180318f8f608SEd Tanous         nlohmann::json::iterator nameIt = powerSupply.find("Name");
180418f8f608SEd Tanous         if (nameIt == powerSupply.end())
180518f8f608SEd Tanous         {
180618f8f608SEd Tanous             continue;
180718f8f608SEd Tanous         }
180818f8f608SEd Tanous         const std::string* name = nameIt->get_ptr<std::string*>();
180918f8f608SEd Tanous         if (name == nullptr)
181018f8f608SEd Tanous         {
181118f8f608SEd Tanous             continue;
181218f8f608SEd Tanous         }
181318f8f608SEd Tanous         if (nameS == *name)
1814adc4f0dbSShawn McCarney         {
1815adc4f0dbSShawn McCarney             return powerSupply;
1816adc4f0dbSShawn McCarney         }
1817adc4f0dbSShawn McCarney     }
1818adc4f0dbSShawn McCarney 
1819adc4f0dbSShawn McCarney     // Add new PowerSupply object to JSON array
1820adc4f0dbSShawn McCarney     powerSupplyArray.push_back({});
1821adc4f0dbSShawn McCarney     nlohmann::json& powerSupply = powerSupplyArray.back();
1822bd79bce8SPatrick Williams     boost::urls::url url =
1823bd79bce8SPatrick Williams         boost::urls::format("/redfish/v1/Chassis/{}/Power", chassisId);
1824eddfc437SWilly Tu     url.set_fragment(("/PowerSupplies"_json_pointer).to_string());
1825eddfc437SWilly Tu     powerSupply["@odata.id"] = std::move(url);
182618f8f608SEd Tanous     std::string escaped;
18276f4bd290SAlexander Hansen     escaped.resize(inventoryItem.name.size());
182818f8f608SEd Tanous     std::ranges::replace_copy(inventoryItem.name, escaped.begin(), '_', ' ');
182918f8f608SEd Tanous     powerSupply["Name"] = std::move(escaped);
1830adc4f0dbSShawn McCarney     powerSupply["Manufacturer"] = inventoryItem.manufacturer;
1831adc4f0dbSShawn McCarney     powerSupply["Model"] = inventoryItem.model;
1832adc4f0dbSShawn McCarney     powerSupply["PartNumber"] = inventoryItem.partNumber;
1833adc4f0dbSShawn McCarney     powerSupply["SerialNumber"] = inventoryItem.serialNumber;
1834*f664fd8aSJanet Adkins     if constexpr (BMCWEB_REDFISH_ALLOW_DEPRECATED_INDICATORLED)
1835*f664fd8aSJanet Adkins     {
1836c9563608SJanet Adkins         sensor_utils::setLedState(powerSupply, &inventoryItem);
1837*f664fd8aSJanet Adkins     }
1838adc4f0dbSShawn McCarney 
183942cbe538SGunnar Mills     if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
184042cbe538SGunnar Mills     {
184142cbe538SGunnar Mills         powerSupply["EfficiencyPercent"] =
184242cbe538SGunnar Mills             inventoryItem.powerSupplyEfficiencyPercent;
184342cbe538SGunnar Mills     }
184442cbe538SGunnar Mills 
1845c9563608SJanet Adkins     powerSupply["Status"]["State"] =
1846c9563608SJanet Adkins         sensor_utils::getState(&inventoryItem, true);
1847adc4f0dbSShawn McCarney     const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
1848adc4f0dbSShawn McCarney     powerSupply["Status"]["Health"] = health;
1849adc4f0dbSShawn McCarney 
1850adc4f0dbSShawn McCarney     return powerSupply;
18518fb49dd6SShawn McCarney }
18528fb49dd6SShawn McCarney 
18538fb49dd6SShawn McCarney /**
1854de629b6eSShawn McCarney  * @brief Gets the values of the specified sensors.
1855de629b6eSShawn McCarney  *
1856de629b6eSShawn McCarney  * Stores the results as JSON in the SensorsAsyncResp.
1857de629b6eSShawn McCarney  *
1858de629b6eSShawn McCarney  * Gets the sensor values asynchronously.  Stores the results later when the
1859de629b6eSShawn McCarney  * information has been obtained.
1860de629b6eSShawn McCarney  *
1861adc4f0dbSShawn McCarney  * The sensorNames set contains all requested sensors for the current chassis.
1862de629b6eSShawn McCarney  *
1863de629b6eSShawn McCarney  * To minimize the number of DBus calls, the DBus method
1864de629b6eSShawn McCarney  * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
1865de629b6eSShawn McCarney  * values of all sensors provided by a connection (service).
1866de629b6eSShawn McCarney  *
1867de629b6eSShawn McCarney  * The connections set contains all the connections that provide sensor values.
1868de629b6eSShawn McCarney  *
1869adc4f0dbSShawn McCarney  * The InventoryItem vector contains D-Bus inventory items associated with the
1870adc4f0dbSShawn McCarney  * sensors.  Inventory item data is needed for some Redfish sensor properties.
1871adc4f0dbSShawn McCarney  *
1872de629b6eSShawn McCarney  * @param SensorsAsyncResp Pointer to object holding response data.
1873adc4f0dbSShawn McCarney  * @param sensorNames All requested sensors within the current chassis.
1874de629b6eSShawn McCarney  * @param connections Connections that provide sensor values.
1875de629b6eSShawn McCarney  * implements ObjectManager.
1876adc4f0dbSShawn McCarney  * @param inventoryItems Inventory items associated with the sensors.
1877de629b6eSShawn McCarney  */
187823a21a1cSEd Tanous inline void getSensorData(
187981ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1880fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& sensorNames,
1881fe04d49cSNan Zhou     const std::set<std::string>& connections,
1882b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems)
1883de629b6eSShawn McCarney {
188462598e31SEd Tanous     BMCWEB_LOG_DEBUG("getSensorData enter");
1885de629b6eSShawn McCarney     // Get managed objects from all services exposing sensors
1886de629b6eSShawn McCarney     for (const std::string& connection : connections)
1887de629b6eSShawn McCarney     {
18885eb468daSGeorge Liu         sdbusplus::message::object_path sensorPath(
18895eb468daSGeorge Liu             "/xyz/openbmc_project/sensors");
18905eb468daSGeorge Liu         dbus::utility::getManagedObjects(
18915eb468daSGeorge Liu             connection, sensorPath,
1892002d39b4SEd Tanous             [sensorsAsyncResp, sensorNames,
18935e7e2dc5SEd Tanous              inventoryItems](const boost::system::error_code& ec,
189402cad96eSEd Tanous                              const dbus::utility::ManagedObjectType& resp) {
189562598e31SEd Tanous                 BMCWEB_LOG_DEBUG("getManagedObjectsCb enter");
1896de629b6eSShawn McCarney                 if (ec)
1897de629b6eSShawn McCarney                 {
189862598e31SEd Tanous                     BMCWEB_LOG_ERROR("getManagedObjectsCb DBUS error: {}", ec);
18998d1b46d7Szhanghch05                     messages::internalError(sensorsAsyncResp->asyncResp->res);
1900de629b6eSShawn McCarney                     return;
1901de629b6eSShawn McCarney                 }
19020c728b42SJanet Adkins                 auto chassisSubNode = sensor_utils::chassisSubNodeFromString(
19030c728b42SJanet Adkins                     sensorsAsyncResp->chassisSubNode);
1904de629b6eSShawn McCarney                 // Go through all objects and update response with sensor data
1905de629b6eSShawn McCarney                 for (const auto& objDictEntry : resp)
1906de629b6eSShawn McCarney                 {
1907de629b6eSShawn McCarney                     const std::string& objPath =
1908de629b6eSShawn McCarney                         static_cast<const std::string&>(objDictEntry.first);
190962598e31SEd Tanous                     BMCWEB_LOG_DEBUG("getManagedObjectsCb parsing object {}",
191062598e31SEd Tanous                                      objPath);
1911de629b6eSShawn McCarney 
1912de629b6eSShawn McCarney                     std::vector<std::string> split;
1913de629b6eSShawn McCarney                     // Reserve space for
1914de629b6eSShawn McCarney                     // /xyz/openbmc_project/sensors/<name>/<subname>
1915de629b6eSShawn McCarney                     split.reserve(6);
191650ebd4afSEd Tanous                     // NOLINTNEXTLINE
191750ebd4afSEd Tanous                     bmcweb::split(split, objPath, '/');
1918de629b6eSShawn McCarney                     if (split.size() < 6)
1919de629b6eSShawn McCarney                     {
192062598e31SEd Tanous                         BMCWEB_LOG_ERROR("Got path that isn't long enough {}",
192162598e31SEd Tanous                                          objPath);
1922de629b6eSShawn McCarney                         continue;
1923de629b6eSShawn McCarney                     }
192450ebd4afSEd Tanous                     // These indexes aren't intuitive, as split puts an empty
1925de629b6eSShawn McCarney                     // string at the beginning
1926de629b6eSShawn McCarney                     const std::string& sensorType = split[4];
1927de629b6eSShawn McCarney                     const std::string& sensorName = split[5];
192862598e31SEd Tanous                     BMCWEB_LOG_DEBUG("sensorName {} sensorType {}", sensorName,
192962598e31SEd Tanous                                      sensorType);
19303d158643SEd Tanous                     if (!sensorNames->contains(objPath))
1931de629b6eSShawn McCarney                     {
193262598e31SEd Tanous                         BMCWEB_LOG_DEBUG("{} not in sensor list ", sensorName);
1933de629b6eSShawn McCarney                         continue;
1934de629b6eSShawn McCarney                     }
1935de629b6eSShawn McCarney 
1936adc4f0dbSShawn McCarney                     // Find inventory item (if any) associated with sensor
1937adc4f0dbSShawn McCarney                     InventoryItem* inventoryItem =
1938adc4f0dbSShawn McCarney                         findInventoryItemForSensor(inventoryItems, objPath);
1939adc4f0dbSShawn McCarney 
194095a3ecadSAnthony Wilson                     const std::string& sensorSchema =
194181ce609eSEd Tanous                         sensorsAsyncResp->chassisSubNode;
194295a3ecadSAnthony Wilson 
194395a3ecadSAnthony Wilson                     nlohmann::json* sensorJson = nullptr;
194495a3ecadSAnthony Wilson 
19450c728b42SJanet Adkins                     if (sensorSchema == sensors::sensorsNodeStr &&
1946928fefb9SNan Zhou                         !sensorsAsyncResp->efficientExpand)
194795a3ecadSAnthony Wilson                     {
19481516c21bSJanet Adkins                         std::string sensorId =
19491516c21bSJanet Adkins                             redfish::sensor_utils::getSensorId(sensorName,
19501516c21bSJanet Adkins                                                                sensorType);
1951c1d019a6SEd Tanous 
1952bd79bce8SPatrick Williams                         sensorsAsyncResp->asyncResp->res
1953bd79bce8SPatrick Williams                             .jsonValue["@odata.id"] = boost::urls::format(
1954bd79bce8SPatrick Williams                             "/redfish/v1/Chassis/{}/{}/{}",
1955c1d019a6SEd Tanous                             sensorsAsyncResp->chassisId,
1956bd79bce8SPatrick Williams                             sensorsAsyncResp->chassisSubNode, sensorId);
1957bd79bce8SPatrick Williams                         sensorJson =
1958bd79bce8SPatrick Williams                             &(sensorsAsyncResp->asyncResp->res.jsonValue);
195995a3ecadSAnthony Wilson                     }
196095a3ecadSAnthony Wilson                     else
196195a3ecadSAnthony Wilson                     {
1962271584abSEd Tanous                         std::string fieldName;
1963928fefb9SNan Zhou                         if (sensorsAsyncResp->efficientExpand)
1964928fefb9SNan Zhou                         {
1965928fefb9SNan Zhou                             fieldName = "Members";
1966928fefb9SNan Zhou                         }
1967928fefb9SNan Zhou                         else if (sensorType == "temperature")
1968de629b6eSShawn McCarney                         {
1969de629b6eSShawn McCarney                             fieldName = "Temperatures";
1970de629b6eSShawn McCarney                         }
1971bd79bce8SPatrick Williams                         else if (sensorType == "fan" ||
1972bd79bce8SPatrick Williams                                  sensorType == "fan_tach" ||
1973de629b6eSShawn McCarney                                  sensorType == "fan_pwm")
1974de629b6eSShawn McCarney                         {
1975de629b6eSShawn McCarney                             fieldName = "Fans";
1976de629b6eSShawn McCarney                         }
1977de629b6eSShawn McCarney                         else if (sensorType == "voltage")
1978de629b6eSShawn McCarney                         {
1979de629b6eSShawn McCarney                             fieldName = "Voltages";
1980de629b6eSShawn McCarney                         }
1981de629b6eSShawn McCarney                         else if (sensorType == "power")
1982de629b6eSShawn McCarney                         {
198355f79e6fSEd Tanous                             if (sensorName == "total_power")
1984028f7ebcSEddie James                             {
1985028f7ebcSEddie James                                 fieldName = "PowerControl";
1986028f7ebcSEddie James                             }
1987adc4f0dbSShawn McCarney                             else if ((inventoryItem != nullptr) &&
1988adc4f0dbSShawn McCarney                                      (inventoryItem->isPowerSupply))
1989028f7ebcSEddie James                             {
1990de629b6eSShawn McCarney                                 fieldName = "PowerSupplies";
1991de629b6eSShawn McCarney                             }
1992adc4f0dbSShawn McCarney                             else
1993adc4f0dbSShawn McCarney                             {
1994adc4f0dbSShawn McCarney                                 // Other power sensors are in SensorCollection
1995adc4f0dbSShawn McCarney                                 continue;
1996adc4f0dbSShawn McCarney                             }
1997028f7ebcSEddie James                         }
1998de629b6eSShawn McCarney                         else
1999de629b6eSShawn McCarney                         {
2000bd79bce8SPatrick Williams                             BMCWEB_LOG_ERROR(
2001bd79bce8SPatrick Williams                                 "Unsure how to handle sensorType {}",
200262598e31SEd Tanous                                 sensorType);
2003de629b6eSShawn McCarney                             continue;
2004de629b6eSShawn McCarney                         }
2005de629b6eSShawn McCarney 
2006de629b6eSShawn McCarney                         nlohmann::json& tempArray =
2007bd79bce8SPatrick Williams                             sensorsAsyncResp->asyncResp->res
2008bd79bce8SPatrick Williams                                 .jsonValue[fieldName];
2009adc4f0dbSShawn McCarney                         if (fieldName == "PowerControl")
201049c53ac9SJohnathan Mantey                         {
2011adc4f0dbSShawn McCarney                             if (tempArray.empty())
20127ab06f49SGunnar Mills                             {
201395a3ecadSAnthony Wilson                                 // Put multiple "sensors" into a single
201495a3ecadSAnthony Wilson                                 // PowerControl. Follows MemberId naming and
201595a3ecadSAnthony Wilson                                 // naming in power.hpp.
20161476687dSEd Tanous                                 nlohmann::json::object_t power;
2017ef4c65b7SEd Tanous                                 boost::urls::url url = boost::urls::format(
2018ef4c65b7SEd Tanous                                     "/redfish/v1/Chassis/{}/{}",
2019eddfc437SWilly Tu                                     sensorsAsyncResp->chassisId,
2020eddfc437SWilly Tu                                     sensorsAsyncResp->chassisSubNode);
2021bd79bce8SPatrick Williams                                 url.set_fragment(
2022bd79bce8SPatrick Williams                                     (""_json_pointer / fieldName / "0")
2023eddfc437SWilly Tu                                         .to_string());
2024eddfc437SWilly Tu                                 power["@odata.id"] = std::move(url);
2025b2ba3072SPatrick Williams                                 tempArray.emplace_back(std::move(power));
2026adc4f0dbSShawn McCarney                             }
2027adc4f0dbSShawn McCarney                             sensorJson = &(tempArray.back());
2028adc4f0dbSShawn McCarney                         }
2029adc4f0dbSShawn McCarney                         else if (fieldName == "PowerSupplies")
2030adc4f0dbSShawn McCarney                         {
2031adc4f0dbSShawn McCarney                             if (inventoryItem != nullptr)
2032adc4f0dbSShawn McCarney                             {
2033bd79bce8SPatrick Williams                                 sensorJson = &(getPowerSupply(
2034bd79bce8SPatrick Williams                                     tempArray, *inventoryItem,
203581ce609eSEd Tanous                                     sensorsAsyncResp->chassisId));
2036adc4f0dbSShawn McCarney                             }
203749c53ac9SJohnathan Mantey                         }
2038928fefb9SNan Zhou                         else if (fieldName == "Members")
2039928fefb9SNan Zhou                         {
20401516c21bSJanet Adkins                             std::string sensorId =
20411516c21bSJanet Adkins                                 redfish::sensor_utils::getSensorId(sensorName,
20421516c21bSJanet Adkins                                                                    sensorType);
2043677bb756SEd Tanous 
20441476687dSEd Tanous                             nlohmann::json::object_t member;
2045ef4c65b7SEd Tanous                             member["@odata.id"] = boost::urls::format(
2046ef4c65b7SEd Tanous                                 "/redfish/v1/Chassis/{}/{}/{}",
2047677bb756SEd Tanous                                 sensorsAsyncResp->chassisId,
2048677bb756SEd Tanous                                 sensorsAsyncResp->chassisSubNode, sensorId);
2049b2ba3072SPatrick Williams                             tempArray.emplace_back(std::move(member));
2050928fefb9SNan Zhou                             sensorJson = &(tempArray.back());
2051928fefb9SNan Zhou                         }
205249c53ac9SJohnathan Mantey                         else
205349c53ac9SJohnathan Mantey                         {
20541476687dSEd Tanous                             nlohmann::json::object_t member;
2055ef4c65b7SEd Tanous                             boost::urls::url url = boost::urls::format(
2056ef4c65b7SEd Tanous                                 "/redfish/v1/Chassis/{}/{}",
2057eddfc437SWilly Tu                                 sensorsAsyncResp->chassisId,
2058eddfc437SWilly Tu                                 sensorsAsyncResp->chassisSubNode);
2059eddfc437SWilly Tu                             url.set_fragment(
2060eddfc437SWilly Tu                                 (""_json_pointer / fieldName).to_string());
2061eddfc437SWilly Tu                             member["@odata.id"] = std::move(url);
2062b2ba3072SPatrick Williams                             tempArray.emplace_back(std::move(member));
2063adc4f0dbSShawn McCarney                             sensorJson = &(tempArray.back());
206449c53ac9SJohnathan Mantey                         }
206595a3ecadSAnthony Wilson                     }
2066de629b6eSShawn McCarney 
2067adc4f0dbSShawn McCarney                     if (sensorJson != nullptr)
2068adc4f0dbSShawn McCarney                     {
20690c728b42SJanet Adkins                         objectInterfacesToJson(
20700c728b42SJanet Adkins                             sensorName, sensorType, chassisSubNode,
20710c728b42SJanet Adkins                             objDictEntry.second, *sensorJson, inventoryItem);
20721d7c0054SEd Tanous 
20731d7c0054SEd Tanous                         std::string path = "/xyz/openbmc_project/sensors/";
20741d7c0054SEd Tanous                         path += sensorType;
20751d7c0054SEd Tanous                         path += "/";
20761d7c0054SEd Tanous                         path += sensorName;
2077c1d019a6SEd Tanous                         sensorsAsyncResp->addMetadata(*sensorJson, path);
2078adc4f0dbSShawn McCarney                     }
2079de629b6eSShawn McCarney                 }
208081ce609eSEd Tanous                 if (sensorsAsyncResp.use_count() == 1)
208149c53ac9SJohnathan Mantey                 {
208281ce609eSEd Tanous                     sortJSONResponse(sensorsAsyncResp);
20830c728b42SJanet Adkins                     if (chassisSubNode ==
20840c728b42SJanet Adkins                             sensor_utils::ChassisSubNode::sensorsNode &&
2085928fefb9SNan Zhou                         sensorsAsyncResp->efficientExpand)
2086928fefb9SNan Zhou                     {
2087928fefb9SNan Zhou                         sensorsAsyncResp->asyncResp->res
2088928fefb9SNan Zhou                             .jsonValue["Members@odata.count"] =
2089bd79bce8SPatrick Williams                             sensorsAsyncResp->asyncResp->res
2090bd79bce8SPatrick Williams                                 .jsonValue["Members"]
2091928fefb9SNan Zhou                                 .size();
2092928fefb9SNan Zhou                     }
20930c728b42SJanet Adkins                     else if (chassisSubNode ==
20940c728b42SJanet Adkins                              sensor_utils::ChassisSubNode::thermalNode)
20958bd25ccdSJames Feist                     {
209681ce609eSEd Tanous                         populateFanRedundancy(sensorsAsyncResp);
20978bd25ccdSJames Feist                     }
209849c53ac9SJohnathan Mantey                 }
209962598e31SEd Tanous                 BMCWEB_LOG_DEBUG("getManagedObjectsCb exit");
21005eb468daSGeorge Liu             });
210123a21a1cSEd Tanous     }
210262598e31SEd Tanous     BMCWEB_LOG_DEBUG("getSensorData exit");
2103de629b6eSShawn McCarney }
2104de629b6eSShawn McCarney 
2105504af5a0SPatrick Williams inline void processSensorList(
2106504af5a0SPatrick Williams     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2107fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& sensorNames)
21081abe55efSEd Tanous {
2109fe04d49cSNan Zhou     auto getConnectionCb = [sensorsAsyncResp, sensorNames](
2110fe04d49cSNan Zhou                                const std::set<std::string>& connections) {
211162598e31SEd Tanous         BMCWEB_LOG_DEBUG("getConnectionCb enter");
2112adc4f0dbSShawn McCarney         auto getInventoryItemsCb =
2113bd79bce8SPatrick Williams             [sensorsAsyncResp, sensorNames, connections](
2114bd79bce8SPatrick Williams                 const std::shared_ptr<std::vector<InventoryItem>>&
21154e0d8789SEd Tanous                     inventoryItems) mutable {
211662598e31SEd Tanous                 BMCWEB_LOG_DEBUG("getInventoryItemsCb enter");
211749c53ac9SJohnathan Mantey                 // Get sensor data and store results in JSON
2118002d39b4SEd Tanous                 getSensorData(sensorsAsyncResp, sensorNames, connections,
2119d0090733SEd Tanous                               inventoryItems);
212062598e31SEd Tanous                 BMCWEB_LOG_DEBUG("getInventoryItemsCb exit");
2121adc4f0dbSShawn McCarney             };
2122adc4f0dbSShawn McCarney 
2123adc4f0dbSShawn McCarney         // Get inventory items associated with sensors
2124d0090733SEd Tanous         getInventoryItems(sensorsAsyncResp, sensorNames,
2125adc4f0dbSShawn McCarney                           std::move(getInventoryItemsCb));
2126adc4f0dbSShawn McCarney 
212762598e31SEd Tanous         BMCWEB_LOG_DEBUG("getConnectionCb exit");
212808777fb0SLewanczyk, Dawid     };
2129de629b6eSShawn McCarney 
2130de629b6eSShawn McCarney     // Get set of connections that provide sensor values
213181ce609eSEd Tanous     getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb));
213295a3ecadSAnthony Wilson }
213395a3ecadSAnthony Wilson 
213495a3ecadSAnthony Wilson /**
213595a3ecadSAnthony Wilson  * @brief Entry point for retrieving sensors data related to requested
213695a3ecadSAnthony Wilson  *        chassis.
213795a3ecadSAnthony Wilson  * @param SensorsAsyncResp   Pointer to object holding response data
213895a3ecadSAnthony Wilson  */
2139504af5a0SPatrick Williams inline void getChassisData(
2140504af5a0SPatrick Williams     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
214195a3ecadSAnthony Wilson {
214262598e31SEd Tanous     BMCWEB_LOG_DEBUG("getChassisData enter");
214395a3ecadSAnthony Wilson     auto getChassisCb =
214481ce609eSEd Tanous         [sensorsAsyncResp](
2145fe04d49cSNan Zhou             const std::shared_ptr<std::set<std::string>>& sensorNames) {
214662598e31SEd Tanous             BMCWEB_LOG_DEBUG("getChassisCb enter");
214781ce609eSEd Tanous             processSensorList(sensorsAsyncResp, sensorNames);
214862598e31SEd Tanous             BMCWEB_LOG_DEBUG("getChassisCb exit");
214908777fb0SLewanczyk, Dawid         };
2150928fefb9SNan Zhou     // SensorCollection doesn't contain the Redundancy property
21510c728b42SJanet Adkins     if (sensorsAsyncResp->chassisSubNode != sensors::sensorsNodeStr)
2152928fefb9SNan Zhou     {
21538d1b46d7Szhanghch05         sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] =
21548d1b46d7Szhanghch05             nlohmann::json::array();
2155928fefb9SNan Zhou     }
215626f03899SShawn McCarney     // Get set of sensors in chassis
21577f1cc26dSEd Tanous     getChassis(sensorsAsyncResp->asyncResp, sensorsAsyncResp->chassisId,
21587f1cc26dSEd Tanous                sensorsAsyncResp->chassisSubNode, sensorsAsyncResp->types,
21597f1cc26dSEd Tanous                std::move(getChassisCb));
216062598e31SEd Tanous     BMCWEB_LOG_DEBUG("getChassisData exit");
2161271584abSEd Tanous }
216208777fb0SLewanczyk, Dawid 
2163413961deSRichard Marian Thomaiyar /**
216449c53ac9SJohnathan Mantey  * @brief Find the requested sensorName in the list of all sensors supplied by
216549c53ac9SJohnathan Mantey  * the chassis node
216649c53ac9SJohnathan Mantey  *
216749c53ac9SJohnathan Mantey  * @param sensorName   The sensor name supplied in the PATCH request
216849c53ac9SJohnathan Mantey  * @param sensorsList  The list of sensors managed by the chassis node
216949c53ac9SJohnathan Mantey  * @param sensorsModified  The list of sensors that were found as a result of
217049c53ac9SJohnathan Mantey  *                         repeated calls to this function
217149c53ac9SJohnathan Mantey  */
2172bd79bce8SPatrick Williams inline bool findSensorNameUsingSensorPath(
2173bd79bce8SPatrick Williams     std::string_view sensorName, const std::set<std::string>& sensorsList,
2174fe04d49cSNan Zhou     std::set<std::string>& sensorsModified)
217549c53ac9SJohnathan Mantey {
2176fe04d49cSNan Zhou     for (const auto& chassisSensor : sensorsList)
217749c53ac9SJohnathan Mantey     {
217828aa8de5SGeorge Liu         sdbusplus::message::object_path path(chassisSensor);
2179b00dcc27SEd Tanous         std::string thisSensorName = path.filename();
218028aa8de5SGeorge Liu         if (thisSensorName.empty())
218149c53ac9SJohnathan Mantey         {
218249c53ac9SJohnathan Mantey             continue;
218349c53ac9SJohnathan Mantey         }
218449c53ac9SJohnathan Mantey         if (thisSensorName == sensorName)
218549c53ac9SJohnathan Mantey         {
218649c53ac9SJohnathan Mantey             sensorsModified.emplace(chassisSensor);
218749c53ac9SJohnathan Mantey             return true;
218849c53ac9SJohnathan Mantey         }
218949c53ac9SJohnathan Mantey     }
219049c53ac9SJohnathan Mantey     return false;
219149c53ac9SJohnathan Mantey }
219249c53ac9SJohnathan Mantey 
219349c53ac9SJohnathan Mantey /**
2194413961deSRichard Marian Thomaiyar  * @brief Entry point for overriding sensor values of given sensor
2195413961deSRichard Marian Thomaiyar  *
21968d1b46d7Szhanghch05  * @param sensorAsyncResp   response object
21974bb3dc34SCarol Wang  * @param allCollections   Collections extract from sensors' request patch info
2198413961deSRichard Marian Thomaiyar  * @param chassisSubNode   Chassis Node for which the query has to happen
2199413961deSRichard Marian Thomaiyar  */
220023a21a1cSEd Tanous inline void setSensorsOverride(
2201b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
22020885057cSEd Tanous     std::unordered_map<std::string, std::vector<nlohmann::json::object_t>>&
2203397fd61fSjayaprakash Mutyala         allCollections)
2204413961deSRichard Marian Thomaiyar {
220562598e31SEd Tanous     BMCWEB_LOG_INFO("setSensorsOverride for subNode{}",
220662598e31SEd Tanous                     sensorAsyncResp->chassisSubNode);
2207413961deSRichard Marian Thomaiyar 
2208d02aad39SEd Tanous     std::string_view propertyValueName;
2209f65af9e8SRichard Marian Thomaiyar     std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
2210413961deSRichard Marian Thomaiyar     std::string memberId;
2211543f4400SEd Tanous     double value = 0.0;
2212f65af9e8SRichard Marian Thomaiyar     for (auto& collectionItems : allCollections)
2213f65af9e8SRichard Marian Thomaiyar     {
2214f65af9e8SRichard Marian Thomaiyar         if (collectionItems.first == "Temperatures")
2215f65af9e8SRichard Marian Thomaiyar         {
2216f65af9e8SRichard Marian Thomaiyar             propertyValueName = "ReadingCelsius";
2217f65af9e8SRichard Marian Thomaiyar         }
2218f65af9e8SRichard Marian Thomaiyar         else if (collectionItems.first == "Fans")
2219f65af9e8SRichard Marian Thomaiyar         {
2220f65af9e8SRichard Marian Thomaiyar             propertyValueName = "Reading";
2221f65af9e8SRichard Marian Thomaiyar         }
2222f65af9e8SRichard Marian Thomaiyar         else
2223f65af9e8SRichard Marian Thomaiyar         {
2224f65af9e8SRichard Marian Thomaiyar             propertyValueName = "ReadingVolts";
2225f65af9e8SRichard Marian Thomaiyar         }
2226f65af9e8SRichard Marian Thomaiyar         for (auto& item : collectionItems.second)
2227f65af9e8SRichard Marian Thomaiyar         {
2228afc474aeSMyung Bae             if (!json_util::readJsonObject(                //
2229afc474aeSMyung Bae                     item, sensorAsyncResp->asyncResp->res, //
2230afc474aeSMyung Bae                     "MemberId", memberId,                  //
2231afc474aeSMyung Bae                     propertyValueName, value               //
2232afc474aeSMyung Bae                     ))
2233413961deSRichard Marian Thomaiyar             {
2234413961deSRichard Marian Thomaiyar                 return;
2235413961deSRichard Marian Thomaiyar             }
2236f65af9e8SRichard Marian Thomaiyar             overrideMap.emplace(memberId,
2237f65af9e8SRichard Marian Thomaiyar                                 std::make_pair(value, collectionItems.first));
2238f65af9e8SRichard Marian Thomaiyar         }
2239f65af9e8SRichard Marian Thomaiyar     }
22404bb3dc34SCarol Wang 
2241bd79bce8SPatrick Williams     auto getChassisSensorListCb = [sensorAsyncResp, overrideMap,
2242bd79bce8SPatrick Williams                                    propertyValueNameStr =
2243bd79bce8SPatrick Williams                                        std::string(propertyValueName)](
2244bd79bce8SPatrick Williams                                       const std::shared_ptr<
2245bd79bce8SPatrick Williams                                           std::set<std::string>>& sensorsList) {
224649c53ac9SJohnathan Mantey         // Match sensor names in the PATCH request to those managed by the
224749c53ac9SJohnathan Mantey         // chassis node
2248fe04d49cSNan Zhou         const std::shared_ptr<std::set<std::string>> sensorNames =
2249fe04d49cSNan Zhou             std::make_shared<std::set<std::string>>();
2250f65af9e8SRichard Marian Thomaiyar         for (const auto& item : overrideMap)
2251413961deSRichard Marian Thomaiyar         {
2252f65af9e8SRichard Marian Thomaiyar             const auto& sensor = item.first;
2253c71d6125SEd Tanous             std::pair<std::string, std::string> sensorNameType =
22541516c21bSJanet Adkins                 redfish::sensor_utils::splitSensorNameAndType(sensor);
2255c71d6125SEd Tanous             if (!findSensorNameUsingSensorPath(sensorNameType.second,
2256c71d6125SEd Tanous                                                *sensorsList, *sensorNames))
2257f65af9e8SRichard Marian Thomaiyar             {
225862598e31SEd Tanous                 BMCWEB_LOG_INFO("Unable to find memberId {}", item.first);
22598d1b46d7Szhanghch05                 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2260f65af9e8SRichard Marian Thomaiyar                                            item.second.second, item.first);
2261413961deSRichard Marian Thomaiyar                 return;
2262413961deSRichard Marian Thomaiyar             }
2263f65af9e8SRichard Marian Thomaiyar         }
2264413961deSRichard Marian Thomaiyar         // Get the connection to which the memberId belongs
2265bd79bce8SPatrick Williams         auto getObjectsWithConnectionCb = [sensorAsyncResp, overrideMap,
2266bd79bce8SPatrick Williams                                            propertyValueNameStr](
2267bd79bce8SPatrick Williams                                               const std::set<
2268bd79bce8SPatrick Williams                                                   std::string>& /*connections*/,
2269bd79bce8SPatrick Williams                                               const std::set<std::pair<
2270bd79bce8SPatrick Williams                                                   std::string, std::string>>&
2271413961deSRichard Marian Thomaiyar                                                   objectsWithConnection) {
2272f65af9e8SRichard Marian Thomaiyar             if (objectsWithConnection.size() != overrideMap.size())
2273413961deSRichard Marian Thomaiyar             {
227462598e31SEd Tanous                 BMCWEB_LOG_INFO(
227562598e31SEd Tanous                     "Unable to find all objects with proper connection {} requested {}",
227662598e31SEd Tanous                     objectsWithConnection.size(), overrideMap.size());
2277bd79bce8SPatrick Williams                 messages::resourceNotFound(
2278bd79bce8SPatrick Williams                     sensorAsyncResp->asyncResp->res,
22790c728b42SJanet Adkins                     sensorAsyncResp->chassisSubNode == sensors::thermalNodeStr
2280413961deSRichard Marian Thomaiyar                         ? "Temperatures"
2281413961deSRichard Marian Thomaiyar                         : "Voltages",
2282f65af9e8SRichard Marian Thomaiyar                     "Count");
2283f65af9e8SRichard Marian Thomaiyar                 return;
2284f65af9e8SRichard Marian Thomaiyar             }
2285f65af9e8SRichard Marian Thomaiyar             for (const auto& item : objectsWithConnection)
2286f65af9e8SRichard Marian Thomaiyar             {
228728aa8de5SGeorge Liu                 sdbusplus::message::object_path path(item.first);
228828aa8de5SGeorge Liu                 std::string sensorName = path.filename();
228928aa8de5SGeorge Liu                 if (sensorName.empty())
2290f65af9e8SRichard Marian Thomaiyar                 {
22914f277b54SJayaprakash Mutyala                     messages::internalError(sensorAsyncResp->asyncResp->res);
2292f65af9e8SRichard Marian Thomaiyar                     return;
2293f65af9e8SRichard Marian Thomaiyar                 }
22941516c21bSJanet Adkins                 std::string id = redfish::sensor_utils::getSensorId(
22951516c21bSJanet Adkins                     sensorName, path.parent_path().filename());
2296f65af9e8SRichard Marian Thomaiyar 
22973f5eb755SBan Feng                 const auto& iterator = overrideMap.find(id);
2298f65af9e8SRichard Marian Thomaiyar                 if (iterator == overrideMap.end())
2299f65af9e8SRichard Marian Thomaiyar                 {
230062598e31SEd Tanous                     BMCWEB_LOG_INFO("Unable to find sensor object{}",
230162598e31SEd Tanous                                     item.first);
23024f277b54SJayaprakash Mutyala                     messages::internalError(sensorAsyncResp->asyncResp->res);
2303413961deSRichard Marian Thomaiyar                     return;
2304413961deSRichard Marian Thomaiyar                 }
2305e93abac6SGinu George                 setDbusProperty(sensorAsyncResp->asyncResp,
2306e93abac6SGinu George                                 propertyValueNameStr, item.second, item.first,
2307e93abac6SGinu George                                 "xyz.openbmc_project.Sensor.Value", "Value",
2308d02aad39SEd Tanous                                 iterator->second.first);
2309f65af9e8SRichard Marian Thomaiyar             }
2310413961deSRichard Marian Thomaiyar         };
2311413961deSRichard Marian Thomaiyar         // Get object with connection for the given sensor name
2312413961deSRichard Marian Thomaiyar         getObjectsWithConnection(sensorAsyncResp, sensorNames,
2313413961deSRichard Marian Thomaiyar                                  std::move(getObjectsWithConnectionCb));
2314413961deSRichard Marian Thomaiyar     };
2315413961deSRichard Marian Thomaiyar     // get full sensor list for the given chassisId and cross verify the sensor.
23167f1cc26dSEd Tanous     getChassis(sensorAsyncResp->asyncResp, sensorAsyncResp->chassisId,
23177f1cc26dSEd Tanous                sensorAsyncResp->chassisSubNode, sensorAsyncResp->types,
23187f1cc26dSEd Tanous                std::move(getChassisSensorListCb));
2319413961deSRichard Marian Thomaiyar }
2320413961deSRichard Marian Thomaiyar 
2321a0ec28b6SAdrian Ambrożewicz /**
2322a0ec28b6SAdrian Ambrożewicz  * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
2323a0ec28b6SAdrian Ambrożewicz  * path of the sensor.
2324a0ec28b6SAdrian Ambrożewicz  *
2325a0ec28b6SAdrian Ambrożewicz  * Function builds valid Redfish response for sensor query of given chassis and
2326a0ec28b6SAdrian Ambrożewicz  * node. It then builds metadata about Redfish<->D-Bus correlations and provides
2327a0ec28b6SAdrian Ambrożewicz  * it to caller in a callback.
2328a0ec28b6SAdrian Ambrożewicz  *
2329a0ec28b6SAdrian Ambrożewicz  * @param chassis   Chassis for which retrieval should be performed
2330c9563608SJanet Adkins  * @param node  Node (group) of sensors. See sensor_utils::node for supported
2331c9563608SJanet Adkins  * values
2332a0ec28b6SAdrian Ambrożewicz  * @param mapComplete   Callback to be called with retrieval result
2333a0ec28b6SAdrian Ambrożewicz  */
2334931edc79SEd Tanous template <typename Callback>
2335bd79bce8SPatrick Williams inline void retrieveUriToDbusMap(
2336bd79bce8SPatrick Williams     const std::string& chassis, const std::string& node, Callback&& mapComplete)
2337a0ec28b6SAdrian Ambrożewicz {
233802da7c5aSEd Tanous     decltype(sensors::paths)::const_iterator pathIt =
233902da7c5aSEd Tanous         std::find_if(sensors::paths.cbegin(), sensors::paths.cend(),
234002da7c5aSEd Tanous                      [&node](auto&& val) { return val.first == node; });
234102da7c5aSEd Tanous     if (pathIt == sensors::paths.cend())
2342a0ec28b6SAdrian Ambrożewicz     {
234362598e31SEd Tanous         BMCWEB_LOG_ERROR("Wrong node provided : {}", node);
23446804b5c8SEd Tanous         std::map<std::string, std::string> noop;
23456804b5c8SEd Tanous         mapComplete(boost::beast::http::status::bad_request, noop);
2346a0ec28b6SAdrian Ambrożewicz         return;
2347a0ec28b6SAdrian Ambrożewicz     }
2348d51e072fSKrzysztof Grobelny 
234972374eb7SNan Zhou     auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
2350bd79bce8SPatrick Williams     auto callback =
2351bd79bce8SPatrick Williams         [asyncResp, mapCompleteCb = std::forward<Callback>(mapComplete)](
2352a0ec28b6SAdrian Ambrożewicz             const boost::beast::http::status status,
2353fe04d49cSNan Zhou             const std::map<std::string, std::string>& uriToDbus) {
2354fe04d49cSNan Zhou             mapCompleteCb(status, uriToDbus);
2355fe04d49cSNan Zhou         };
2356a0ec28b6SAdrian Ambrożewicz 
2357a0ec28b6SAdrian Ambrożewicz     auto resp = std::make_shared<SensorsAsyncResp>(
2358d51e072fSKrzysztof Grobelny         asyncResp, chassis, pathIt->second, node, std::move(callback));
2359a0ec28b6SAdrian Ambrożewicz     getChassisData(resp);
2360a0ec28b6SAdrian Ambrożewicz }
2361a0ec28b6SAdrian Ambrożewicz 
2362bacb2162SNan Zhou namespace sensors
2363bacb2162SNan Zhou {
2364928fefb9SNan Zhou 
2365bacb2162SNan Zhou inline void getChassisCallback(
2366c1d019a6SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2367c1d019a6SEd Tanous     std::string_view chassisId, std::string_view chassisSubNode,
2368fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& sensorNames)
2369bacb2162SNan Zhou {
237062598e31SEd Tanous     BMCWEB_LOG_DEBUG("getChassisCallback enter ");
2371bacb2162SNan Zhou 
2372c1d019a6SEd Tanous     nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
2373c1d019a6SEd Tanous     for (const std::string& sensor : *sensorNames)
2374bacb2162SNan Zhou     {
237562598e31SEd Tanous         BMCWEB_LOG_DEBUG("Adding sensor: {}", sensor);
2376bacb2162SNan Zhou 
2377bacb2162SNan Zhou         sdbusplus::message::object_path path(sensor);
2378bacb2162SNan Zhou         std::string sensorName = path.filename();
2379bacb2162SNan Zhou         if (sensorName.empty())
2380bacb2162SNan Zhou         {
238162598e31SEd Tanous             BMCWEB_LOG_ERROR("Invalid sensor path: {}", sensor);
2382c1d019a6SEd Tanous             messages::internalError(asyncResp->res);
2383bacb2162SNan Zhou             return;
2384bacb2162SNan Zhou         }
2385c1d019a6SEd Tanous         std::string type = path.parent_path().filename();
23861516c21bSJanet Adkins         std::string id = redfish::sensor_utils::getSensorId(sensorName, type);
2387c1d019a6SEd Tanous 
23881476687dSEd Tanous         nlohmann::json::object_t member;
2389ef4c65b7SEd Tanous         member["@odata.id"] = boost::urls::format(
2390ef4c65b7SEd Tanous             "/redfish/v1/Chassis/{}/{}/{}", chassisId, chassisSubNode, id);
2391c1d019a6SEd Tanous 
2392b2ba3072SPatrick Williams         entriesArray.emplace_back(std::move(member));
2393bacb2162SNan Zhou     }
2394bacb2162SNan Zhou 
2395c1d019a6SEd Tanous     asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
239662598e31SEd Tanous     BMCWEB_LOG_DEBUG("getChassisCallback exit");
2397bacb2162SNan Zhou }
2398e6bd846dSNan Zhou 
2399ac106bf6SEd Tanous inline void handleSensorCollectionGet(
2400ac106bf6SEd Tanous     App& app, const crow::Request& req,
2401ac106bf6SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2402de167a6fSNan Zhou     const std::string& chassisId)
2403de167a6fSNan Zhou {
2404de167a6fSNan Zhou     query_param::QueryCapabilities capabilities = {
2405de167a6fSNan Zhou         .canDelegateExpandLevel = 1,
2406de167a6fSNan Zhou     };
2407de167a6fSNan Zhou     query_param::Query delegatedQuery;
2408ac106bf6SEd Tanous     if (!redfish::setUpRedfishRouteWithDelegation(app, req, asyncResp,
2409de167a6fSNan Zhou                                                   delegatedQuery, capabilities))
2410de167a6fSNan Zhou     {
2411de167a6fSNan Zhou         return;
2412de167a6fSNan Zhou     }
2413de167a6fSNan Zhou 
2414de167a6fSNan Zhou     if (delegatedQuery.expandType != query_param::ExpandType::None)
2415de167a6fSNan Zhou     {
2416de167a6fSNan Zhou         // we perform efficient expand.
2417ac106bf6SEd Tanous         auto sensorsAsyncResp = std::make_shared<SensorsAsyncResp>(
2418ac106bf6SEd Tanous             asyncResp, chassisId, sensors::dbus::sensorPaths,
24190c728b42SJanet Adkins             sensors::sensorsNodeStr,
2420de167a6fSNan Zhou             /*efficientExpand=*/true);
2421ac106bf6SEd Tanous         getChassisData(sensorsAsyncResp);
2422de167a6fSNan Zhou 
242362598e31SEd Tanous         BMCWEB_LOG_DEBUG(
242462598e31SEd Tanous             "SensorCollection doGet exit via efficient expand handler");
2425de167a6fSNan Zhou         return;
24260bad320cSEd Tanous     }
2427de167a6fSNan Zhou 
2428de167a6fSNan Zhou     // We get all sensors as hyperlinkes in the chassis (this
2429de167a6fSNan Zhou     // implies we reply on the default query parameters handler)
24300c728b42SJanet Adkins     getChassis(asyncResp, chassisId, sensors::sensorsNodeStr, dbus::sensorPaths,
2431ac106bf6SEd Tanous                std::bind_front(sensors::getChassisCallback, asyncResp,
24320c728b42SJanet Adkins                                chassisId, sensors::sensorsNodeStr));
2433c1d019a6SEd Tanous }
24347f1cc26dSEd Tanous 
2435504af5a0SPatrick Williams inline void getSensorFromDbus(
2436504af5a0SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2437c1d019a6SEd Tanous     const std::string& sensorPath,
2438c1d019a6SEd Tanous     const ::dbus::utility::MapperGetObject& mapperResponse)
2439c1d019a6SEd Tanous {
2440c1d019a6SEd Tanous     if (mapperResponse.size() != 1)
2441c1d019a6SEd Tanous     {
2442c1d019a6SEd Tanous         messages::internalError(asyncResp->res);
2443c1d019a6SEd Tanous         return;
2444c1d019a6SEd Tanous     }
2445c1d019a6SEd Tanous     const auto& valueIface = *mapperResponse.begin();
2446c1d019a6SEd Tanous     const std::string& connectionName = valueIface.first;
244762598e31SEd Tanous     BMCWEB_LOG_DEBUG("Looking up {}", connectionName);
244862598e31SEd Tanous     BMCWEB_LOG_DEBUG("Path {}", sensorPath);
2449c1343bf6SKrzysztof Grobelny 
2450deae6a78SEd Tanous     ::dbus::utility::getAllProperties(
2451c1343bf6SKrzysztof Grobelny         *crow::connections::systemBus, connectionName, sensorPath, "",
2452c1d019a6SEd Tanous         [asyncResp,
24535e7e2dc5SEd Tanous          sensorPath](const boost::system::error_code& ec,
2454c1d019a6SEd Tanous                      const ::dbus::utility::DBusPropertiesMap& valuesDict) {
2455c1d019a6SEd Tanous             if (ec)
2456c1d019a6SEd Tanous             {
2457c1d019a6SEd Tanous                 messages::internalError(asyncResp->res);
2458c1d019a6SEd Tanous                 return;
2459c1d019a6SEd Tanous             }
2460c1d019a6SEd Tanous             sdbusplus::message::object_path path(sensorPath);
2461c1d019a6SEd Tanous             std::string name = path.filename();
2462c1d019a6SEd Tanous             path = path.parent_path();
2463c1d019a6SEd Tanous             std::string type = path.filename();
2464c9563608SJanet Adkins             sensor_utils::objectPropertiesToJson(
24650c728b42SJanet Adkins                 name, type, sensor_utils::ChassisSubNode::sensorsNode,
24660c728b42SJanet Adkins                 valuesDict, asyncResp->res.jsonValue, nullptr);
2467c1343bf6SKrzysztof Grobelny         });
2468de167a6fSNan Zhou }
2469de167a6fSNan Zhou 
2470e6bd846dSNan Zhou inline void handleSensorGet(App& app, const crow::Request& req,
2471c1d019a6SEd Tanous                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2472677bb756SEd Tanous                             const std::string& chassisId,
2473c1d019a6SEd Tanous                             const std::string& sensorId)
2474e6bd846dSNan Zhou {
2475c1d019a6SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2476e6bd846dSNan Zhou     {
2477e6bd846dSNan Zhou         return;
2478e6bd846dSNan Zhou     }
2479c71d6125SEd Tanous     std::pair<std::string, std::string> nameType =
24801516c21bSJanet Adkins         redfish::sensor_utils::splitSensorNameAndType(sensorId);
2481c71d6125SEd Tanous     if (nameType.first.empty() || nameType.second.empty())
2482c1d019a6SEd Tanous     {
2483c1d019a6SEd Tanous         messages::resourceNotFound(asyncResp->res, sensorId, "Sensor");
2484c1d019a6SEd Tanous         return;
2485c1d019a6SEd Tanous     }
2486c71d6125SEd Tanous 
2487ef4c65b7SEd Tanous     asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
2488ef4c65b7SEd Tanous         "/redfish/v1/Chassis/{}/Sensors/{}", chassisId, sensorId);
2489c1d019a6SEd Tanous 
249062598e31SEd Tanous     BMCWEB_LOG_DEBUG("Sensor doGet enter");
2491e6bd846dSNan Zhou 
24922b73119cSGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
2493e6bd846dSNan Zhou         "xyz.openbmc_project.Sensor.Value"};
2494c71d6125SEd Tanous     std::string sensorPath = "/xyz/openbmc_project/sensors/" + nameType.first +
2495c71d6125SEd Tanous                              '/' + nameType.second;
2496e6bd846dSNan Zhou     // Get a list of all of the sensors that implement Sensor.Value
2497e6bd846dSNan Zhou     // and get the path and service name associated with the sensor
24982b73119cSGeorge Liu     ::dbus::utility::getDbusObject(
24992b73119cSGeorge Liu         sensorPath, interfaces,
2500aec0ec30SMyung Bae         [asyncResp, sensorId,
25012b73119cSGeorge Liu          sensorPath](const boost::system::error_code& ec,
2502c1d019a6SEd Tanous                      const ::dbus::utility::MapperGetObject& subtree) {
250362598e31SEd Tanous             BMCWEB_LOG_DEBUG("respHandler1 enter");
2504aec0ec30SMyung Bae             if (ec == boost::system::errc::io_error)
2505aec0ec30SMyung Bae             {
250662598e31SEd Tanous                 BMCWEB_LOG_WARNING("Sensor not found from getSensorPaths");
2507aec0ec30SMyung Bae                 messages::resourceNotFound(asyncResp->res, sensorId, "Sensor");
2508aec0ec30SMyung Bae                 return;
2509aec0ec30SMyung Bae             }
2510e6bd846dSNan Zhou             if (ec)
2511e6bd846dSNan Zhou             {
2512c1d019a6SEd Tanous                 messages::internalError(asyncResp->res);
251362598e31SEd Tanous                 BMCWEB_LOG_ERROR(
251462598e31SEd Tanous                     "Sensor getSensorPaths resp_handler: Dbus error {}", ec);
2515e6bd846dSNan Zhou                 return;
2516e6bd846dSNan Zhou             }
2517c1d019a6SEd Tanous             getSensorFromDbus(asyncResp, sensorPath, subtree);
251862598e31SEd Tanous             BMCWEB_LOG_DEBUG("respHandler1 exit");
25192b73119cSGeorge Liu         });
2520e6bd846dSNan Zhou }
2521e6bd846dSNan Zhou 
2522bacb2162SNan Zhou } // namespace sensors
2523bacb2162SNan Zhou 
25247e860f15SJohn Edward Broadbent inline void requestRoutesSensorCollection(App& app)
252595a3ecadSAnthony Wilson {
25267e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/")
2527ed398213SEd Tanous         .privileges(redfish::privileges::getSensorCollection)
2528002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2529de167a6fSNan Zhou             std::bind_front(sensors::handleSensorCollectionGet, std::ref(app)));
253095a3ecadSAnthony Wilson }
253195a3ecadSAnthony Wilson 
25327e860f15SJohn Edward Broadbent inline void requestRoutesSensor(App& app)
253395a3ecadSAnthony Wilson {
25347e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/")
2535ed398213SEd Tanous         .privileges(redfish::privileges::getSensor)
2536002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2537e6bd846dSNan Zhou             std::bind_front(sensors::handleSensorGet, std::ref(app)));
253895a3ecadSAnthony Wilson }
253995a3ecadSAnthony Wilson 
254008777fb0SLewanczyk, Dawid } // namespace redfish
2541