xref: /openbmc/bmcweb/features/redfish/lib/sensors.hpp (revision f664fd8abeba11c7aa06d5119423a27b9d40d045)
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>
391214b7e7SGunnar Mills #include <cmath>
40d7857201SEd Tanous #include <cstddef>
41d7857201SEd Tanous #include <cstdint>
42d7857201SEd Tanous #include <functional>
43fe04d49cSNan Zhou #include <iterator>
44fe04d49cSNan Zhou #include <map>
45d7857201SEd Tanous #include <memory>
46d7857201SEd Tanous #include <optional>
473544d2a7SEd Tanous #include <ranges>
48fe04d49cSNan Zhou #include <set>
49d7857201SEd Tanous #include <span>
5018f8f608SEd Tanous #include <string>
517a1dbc48SGeorge Liu #include <string_view>
52d7857201SEd Tanous #include <unordered_map>
53b5a76932SEd Tanous #include <utility>
54abf2add6SEd Tanous #include <variant>
55d7857201SEd Tanous #include <vector>
5608777fb0SLewanczyk, Dawid 
571abe55efSEd Tanous namespace redfish
581abe55efSEd Tanous {
5908777fb0SLewanczyk, Dawid 
60a0ec28b6SAdrian Ambrożewicz namespace sensors
61a0ec28b6SAdrian Ambrożewicz {
62a0ec28b6SAdrian Ambrożewicz 
6302da7c5aSEd Tanous // clang-format off
64a0ec28b6SAdrian Ambrożewicz namespace dbus
65a0ec28b6SAdrian Ambrożewicz {
66cf9e417dSEd Tanous constexpr auto powerPaths = std::to_array<std::string_view>({
6702da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/voltage",
6802da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/power"
6902da7c5aSEd Tanous });
70c2bf7f99SWludzik, Jozef 
7125b54dbaSEd Tanous constexpr auto getSensorPaths(){
7225b54dbaSEd Tanous     if constexpr(BMCWEB_REDFISH_NEW_POWERSUBSYSTEM_THERMALSUBSYSTEM){
7325b54dbaSEd Tanous     return std::to_array<std::string_view>({
7402da7c5aSEd Tanous         "/xyz/openbmc_project/sensors/power",
75a0ec28b6SAdrian Ambrożewicz         "/xyz/openbmc_project/sensors/current",
767088690cSBasheer Ahmed Muddebihal         "/xyz/openbmc_project/sensors/airflow",
775deabed9SGunnar Mills         "/xyz/openbmc_project/sensors/humidity",
78e8204933SGeorge Liu         "/xyz/openbmc_project/sensors/voltage",
79e8204933SGeorge Liu         "/xyz/openbmc_project/sensors/fan_tach",
80e8204933SGeorge Liu         "/xyz/openbmc_project/sensors/temperature",
81e8204933SGeorge Liu         "/xyz/openbmc_project/sensors/fan_pwm",
82e8204933SGeorge Liu         "/xyz/openbmc_project/sensors/altitude",
83e8204933SGeorge Liu         "/xyz/openbmc_project/sensors/energy",
8444914192SZev Weiss         "/xyz/openbmc_project/sensors/liquidflow",
8544914192SZev Weiss         "/xyz/openbmc_project/sensors/pressure",
8625b54dbaSEd Tanous         "/xyz/openbmc_project/sensors/utilization"});
8725b54dbaSEd Tanous     } else {
8825b54dbaSEd Tanous       return  std::to_array<std::string_view>({"/xyz/openbmc_project/sensors/power",
8925b54dbaSEd Tanous         "/xyz/openbmc_project/sensors/current",
9025b54dbaSEd Tanous         "/xyz/openbmc_project/sensors/airflow",
9125b54dbaSEd Tanous         "/xyz/openbmc_project/sensors/humidity",
9225b54dbaSEd Tanous         "/xyz/openbmc_project/sensors/utilization"});
9325b54dbaSEd Tanous }
9425b54dbaSEd Tanous }
9525b54dbaSEd Tanous 
9625b54dbaSEd Tanous constexpr auto sensorPaths = getSensorPaths();
9702da7c5aSEd Tanous 
98cf9e417dSEd Tanous constexpr auto thermalPaths = std::to_array<std::string_view>({
9902da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/fan_tach",
100a0ec28b6SAdrian Ambrożewicz     "/xyz/openbmc_project/sensors/temperature",
10102da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/fan_pwm"
10202da7c5aSEd Tanous });
10302da7c5aSEd Tanous 
104c2bf7f99SWludzik, Jozef } // namespace dbus
10502da7c5aSEd Tanous // clang-format on
10602da7c5aSEd Tanous 
1070c728b42SJanet Adkins constexpr std::string_view powerNodeStr = sensor_utils::chassisSubNodeToString(
1080c728b42SJanet Adkins     sensor_utils::ChassisSubNode::powerNode);
1090c728b42SJanet Adkins constexpr std::string_view sensorsNodeStr =
1100c728b42SJanet Adkins     sensor_utils::chassisSubNodeToString(
1110c728b42SJanet Adkins         sensor_utils::ChassisSubNode::sensorsNode);
1120c728b42SJanet Adkins constexpr std::string_view thermalNodeStr =
1130c728b42SJanet Adkins     sensor_utils::chassisSubNodeToString(
1140c728b42SJanet Adkins         sensor_utils::ChassisSubNode::thermalNode);
1150c728b42SJanet Adkins 
116cf9e417dSEd Tanous using sensorPair =
117cf9e417dSEd Tanous     std::pair<std::string_view, std::span<const std::string_view>>;
11802da7c5aSEd Tanous static constexpr std::array<sensorPair, 3> paths = {
1190c728b42SJanet Adkins     {{sensors::powerNodeStr, dbus::powerPaths},
1200c728b42SJanet Adkins      {sensors::sensorsNodeStr, dbus::sensorPaths},
1210c728b42SJanet Adkins      {sensors::thermalNodeStr, dbus::thermalPaths}}};
122c2bf7f99SWludzik, Jozef 
123a0ec28b6SAdrian Ambrożewicz } // namespace sensors
124a0ec28b6SAdrian Ambrożewicz 
12508777fb0SLewanczyk, Dawid /**
126588c3f0dSKowalski, Kamil  * SensorsAsyncResp
12708777fb0SLewanczyk, Dawid  * Gathers data needed for response processing after async calls are done
12808777fb0SLewanczyk, Dawid  */
1291abe55efSEd Tanous class SensorsAsyncResp
1301abe55efSEd Tanous {
13108777fb0SLewanczyk, Dawid   public:
132a0ec28b6SAdrian Ambrożewicz     using DataCompleteCb = std::function<void(
133a0ec28b6SAdrian Ambrożewicz         const boost::beast::http::status status,
134fe04d49cSNan Zhou         const std::map<std::string, std::string>& uriToDbus)>;
135a0ec28b6SAdrian Ambrożewicz 
136a0ec28b6SAdrian Ambrożewicz     struct SensorData
137a0ec28b6SAdrian Ambrożewicz     {
138f836c1d8SEd Tanous         std::string name;
139a0ec28b6SAdrian Ambrożewicz         std::string uri;
140f836c1d8SEd Tanous         std::string dbusPath;
141a0ec28b6SAdrian Ambrożewicz     };
142a0ec28b6SAdrian Ambrożewicz 
1438a592810SEd Tanous     SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
1448d1b46d7Szhanghch05                      const std::string& chassisIdIn,
145cf9e417dSEd Tanous                      std::span<const std::string_view> typesIn,
14602da7c5aSEd Tanous                      std::string_view subNode) :
147bd79bce8SPatrick Williams         asyncResp(asyncRespIn), chassisId(chassisIdIn), types(typesIn),
148bd79bce8SPatrick Williams         chassisSubNode(subNode), efficientExpand(false)
1491214b7e7SGunnar Mills     {}
15008777fb0SLewanczyk, Dawid 
151a0ec28b6SAdrian Ambrożewicz     // Store extra data about sensor mapping and return it in callback
1528a592810SEd Tanous     SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
1538d1b46d7Szhanghch05                      const std::string& chassisIdIn,
154cf9e417dSEd Tanous                      std::span<const std::string_view> typesIn,
15502da7c5aSEd Tanous                      std::string_view subNode,
156a0ec28b6SAdrian Ambrożewicz                      DataCompleteCb&& creationComplete) :
157bd79bce8SPatrick Williams         asyncResp(asyncRespIn), chassisId(chassisIdIn), types(typesIn),
158bd79bce8SPatrick Williams         chassisSubNode(subNode), efficientExpand(false),
159bd79bce8SPatrick Williams         metadata{std::vector<SensorData>()},
160a0ec28b6SAdrian Ambrożewicz         dataComplete{std::move(creationComplete)}
161a0ec28b6SAdrian Ambrożewicz     {}
162a0ec28b6SAdrian Ambrożewicz 
163928fefb9SNan Zhou     // sensor collections expand
1648a592810SEd Tanous     SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
165928fefb9SNan Zhou                      const std::string& chassisIdIn,
166cf9e417dSEd Tanous                      std::span<const std::string_view> typesIn,
1678a592810SEd Tanous                      const std::string_view& subNode, bool efficientExpandIn) :
168bd79bce8SPatrick Williams         asyncResp(asyncRespIn), chassisId(chassisIdIn), types(typesIn),
169bd79bce8SPatrick Williams         chassisSubNode(subNode), efficientExpand(efficientExpandIn)
170928fefb9SNan Zhou     {}
171928fefb9SNan Zhou 
1721abe55efSEd Tanous     ~SensorsAsyncResp()
1731abe55efSEd Tanous     {
1748d1b46d7Szhanghch05         if (asyncResp->res.result() ==
1758d1b46d7Szhanghch05             boost::beast::http::status::internal_server_error)
1761abe55efSEd Tanous         {
1771abe55efSEd Tanous             // Reset the json object to clear out any data that made it in
1781abe55efSEd Tanous             // before the error happened todo(ed) handle error condition with
1791abe55efSEd Tanous             // proper code
1808d1b46d7Szhanghch05             asyncResp->res.jsonValue = nlohmann::json::object();
18108777fb0SLewanczyk, Dawid         }
182a0ec28b6SAdrian Ambrożewicz 
183a0ec28b6SAdrian Ambrożewicz         if (dataComplete && metadata)
184a0ec28b6SAdrian Ambrożewicz         {
185fe04d49cSNan Zhou             std::map<std::string, std::string> map;
1868d1b46d7Szhanghch05             if (asyncResp->res.result() == boost::beast::http::status::ok)
187a0ec28b6SAdrian Ambrożewicz             {
188a0ec28b6SAdrian Ambrożewicz                 for (auto& sensor : *metadata)
189a0ec28b6SAdrian Ambrożewicz                 {
190c1d019a6SEd Tanous                     map.emplace(sensor.uri, sensor.dbusPath);
191a0ec28b6SAdrian Ambrożewicz                 }
192a0ec28b6SAdrian Ambrożewicz             }
1938d1b46d7Szhanghch05             dataComplete(asyncResp->res.result(), map);
194a0ec28b6SAdrian Ambrożewicz         }
19508777fb0SLewanczyk, Dawid     }
196588c3f0dSKowalski, Kamil 
197ecd6a3a2SEd Tanous     SensorsAsyncResp(const SensorsAsyncResp&) = delete;
198ecd6a3a2SEd Tanous     SensorsAsyncResp(SensorsAsyncResp&&) = delete;
199ecd6a3a2SEd Tanous     SensorsAsyncResp& operator=(const SensorsAsyncResp&) = delete;
200ecd6a3a2SEd Tanous     SensorsAsyncResp& operator=(SensorsAsyncResp&&) = delete;
201ecd6a3a2SEd Tanous 
202a0ec28b6SAdrian Ambrożewicz     void addMetadata(const nlohmann::json& sensorObject,
203c1d019a6SEd Tanous                      const std::string& dbusPath)
204a0ec28b6SAdrian Ambrożewicz     {
20582b286fbSEd Tanous         if (!metadata)
206a0ec28b6SAdrian Ambrożewicz         {
20782b286fbSEd Tanous             return;
208a0ec28b6SAdrian Ambrożewicz         }
20982b286fbSEd Tanous         const auto nameIt = sensorObject.find("Name");
210d9cca622SJames Athappilly         if (nameIt == sensorObject.end())
21182b286fbSEd Tanous         {
21282b286fbSEd Tanous             return;
21382b286fbSEd Tanous         }
21482b286fbSEd Tanous         const auto idIt = sensorObject.find("@odata.id");
215d9cca622SJames Athappilly         if (idIt == sensorObject.end())
21682b286fbSEd Tanous         {
21782b286fbSEd Tanous             return;
21882b286fbSEd Tanous         }
21982b286fbSEd Tanous         const std::string* name = nameIt->get_ptr<const std::string*>();
22082b286fbSEd Tanous         if (name == nullptr)
22182b286fbSEd Tanous         {
22282b286fbSEd Tanous             return;
22382b286fbSEd Tanous         }
22482b286fbSEd Tanous         const std::string* id = idIt->get_ptr<const std::string*>();
22582b286fbSEd Tanous         if (id == nullptr)
22682b286fbSEd Tanous         {
22782b286fbSEd Tanous             return;
22882b286fbSEd Tanous         }
22982b286fbSEd Tanous         metadata->emplace_back(SensorData{*name, *id, dbusPath});
230a0ec28b6SAdrian Ambrożewicz     }
231a0ec28b6SAdrian Ambrożewicz 
232a0ec28b6SAdrian Ambrożewicz     void updateUri(const std::string& name, const std::string& uri)
233a0ec28b6SAdrian Ambrożewicz     {
234a0ec28b6SAdrian Ambrożewicz         if (metadata)
235a0ec28b6SAdrian Ambrożewicz         {
236a0ec28b6SAdrian Ambrożewicz             for (auto& sensor : *metadata)
237a0ec28b6SAdrian Ambrożewicz             {
238a0ec28b6SAdrian Ambrożewicz                 if (sensor.name == name)
239a0ec28b6SAdrian Ambrożewicz                 {
240a0ec28b6SAdrian Ambrożewicz                     sensor.uri = uri;
241a0ec28b6SAdrian Ambrożewicz                 }
242a0ec28b6SAdrian Ambrożewicz             }
243a0ec28b6SAdrian Ambrożewicz         }
244a0ec28b6SAdrian Ambrożewicz     }
245a0ec28b6SAdrian Ambrożewicz 
2468d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp> asyncResp;
247a0ec28b6SAdrian Ambrożewicz     const std::string chassisId;
248cf9e417dSEd Tanous     const std::span<const std::string_view> types;
249a0ec28b6SAdrian Ambrożewicz     const std::string chassisSubNode;
250928fefb9SNan Zhou     const bool efficientExpand;
251a0ec28b6SAdrian Ambrożewicz 
252a0ec28b6SAdrian Ambrożewicz   private:
253a0ec28b6SAdrian Ambrożewicz     std::optional<std::vector<SensorData>> metadata;
254a0ec28b6SAdrian Ambrożewicz     DataCompleteCb dataComplete;
25508777fb0SLewanczyk, Dawid };
25608777fb0SLewanczyk, Dawid 
257c9563608SJanet Adkins using InventoryItem = sensor_utils::InventoryItem;
258adc4f0dbSShawn McCarney 
259adc4f0dbSShawn McCarney /**
260413961deSRichard Marian Thomaiyar  * @brief Get objects with connection necessary for sensors
261588c3f0dSKowalski, Kamil  * @param SensorsAsyncResp Pointer to object holding response data
26208777fb0SLewanczyk, Dawid  * @param sensorNames Sensors retrieved from chassis
26308777fb0SLewanczyk, Dawid  * @param callback Callback for processing gathered connections
26408777fb0SLewanczyk, Dawid  */
26508777fb0SLewanczyk, Dawid template <typename Callback>
266413961deSRichard Marian Thomaiyar void getObjectsWithConnection(
26781ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
268fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& sensorNames,
2691abe55efSEd Tanous     Callback&& callback)
2701abe55efSEd Tanous {
27162598e31SEd Tanous     BMCWEB_LOG_DEBUG("getObjectsWithConnection enter");
27203b5bae3SJames Feist     const std::string path = "/xyz/openbmc_project/sensors";
273e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
27408777fb0SLewanczyk, Dawid         "xyz.openbmc_project.Sensor.Value"};
27508777fb0SLewanczyk, Dawid 
276e99073f5SGeorge Liu     // Make call to ObjectMapper to find all sensors objects
277e99073f5SGeorge Liu     dbus::utility::getSubTree(
278e99073f5SGeorge Liu         path, 2, interfaces,
2798cb2c024SEd Tanous         [callback = std::forward<Callback>(callback), sensorsAsyncResp,
280e99073f5SGeorge Liu          sensorNames](const boost::system::error_code& ec,
281002d39b4SEd Tanous                       const dbus::utility::MapperGetSubTreeResponse& subtree) {
282e99073f5SGeorge Liu             // Response handler for parsing objects subtree
28362598e31SEd Tanous             BMCWEB_LOG_DEBUG("getObjectsWithConnection resp_handler enter");
2841abe55efSEd Tanous             if (ec)
2851abe55efSEd Tanous             {
2868d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
28762598e31SEd Tanous                 BMCWEB_LOG_ERROR(
28862598e31SEd Tanous                     "getObjectsWithConnection resp_handler: Dbus error {}", ec);
28908777fb0SLewanczyk, Dawid                 return;
29008777fb0SLewanczyk, Dawid             }
29108777fb0SLewanczyk, Dawid 
29262598e31SEd Tanous             BMCWEB_LOG_DEBUG("Found {} subtrees", subtree.size());
29308777fb0SLewanczyk, Dawid 
294bd79bce8SPatrick Williams             // Make unique list of connections only for requested sensor types
295bd79bce8SPatrick Williams             // and found in the chassis
296fe04d49cSNan Zhou             std::set<std::string> connections;
297413961deSRichard Marian Thomaiyar             std::set<std::pair<std::string, std::string>> objectsWithConnection;
29808777fb0SLewanczyk, Dawid 
29962598e31SEd Tanous             BMCWEB_LOG_DEBUG("sensorNames list count: {}", sensorNames->size());
30049c53ac9SJohnathan Mantey             for (const std::string& tsensor : *sensorNames)
3011abe55efSEd Tanous             {
30262598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Sensor to find: {}", tsensor);
30308777fb0SLewanczyk, Dawid             }
30408777fb0SLewanczyk, Dawid 
305bd79bce8SPatrick Williams             for (const std::pair<std::string,
306bd79bce8SPatrick Williams                                  std::vector<std::pair<
307bd79bce8SPatrick Williams                                      std::string, std::vector<std::string>>>>&
3081abe55efSEd Tanous                      object : subtree)
3091abe55efSEd Tanous             {
3103d158643SEd Tanous                 if (sensorNames->contains(object.first))
3111abe55efSEd Tanous                 {
31249c53ac9SJohnathan Mantey                     for (const std::pair<std::string, std::vector<std::string>>&
3131abe55efSEd Tanous                              objData : object.second)
3141abe55efSEd Tanous                     {
315bd79bce8SPatrick Williams                         BMCWEB_LOG_DEBUG("Adding connection: {}",
316bd79bce8SPatrick Williams                                          objData.first);
31708777fb0SLewanczyk, Dawid                         connections.insert(objData.first);
318de629b6eSShawn McCarney                         objectsWithConnection.insert(
319de629b6eSShawn McCarney                             std::make_pair(object.first, objData.first));
32008777fb0SLewanczyk, Dawid                     }
32108777fb0SLewanczyk, Dawid                 }
32208777fb0SLewanczyk, Dawid             }
32362598e31SEd Tanous             BMCWEB_LOG_DEBUG("Found {} connections", connections.size());
324413961deSRichard Marian Thomaiyar             callback(std::move(connections), std::move(objectsWithConnection));
32562598e31SEd Tanous             BMCWEB_LOG_DEBUG("getObjectsWithConnection resp_handler exit");
326e99073f5SGeorge Liu         });
32762598e31SEd Tanous     BMCWEB_LOG_DEBUG("getObjectsWithConnection exit");
328413961deSRichard Marian Thomaiyar }
329413961deSRichard Marian Thomaiyar 
330413961deSRichard Marian Thomaiyar /**
331413961deSRichard Marian Thomaiyar  * @brief Create connections necessary for sensors
332413961deSRichard Marian Thomaiyar  * @param SensorsAsyncResp Pointer to object holding response data
333413961deSRichard Marian Thomaiyar  * @param sensorNames Sensors retrieved from chassis
334413961deSRichard Marian Thomaiyar  * @param callback Callback for processing gathered connections
335413961deSRichard Marian Thomaiyar  */
336413961deSRichard Marian Thomaiyar template <typename Callback>
337daadfb2eSEd Tanous void getConnections(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
338daadfb2eSEd Tanous                     const std::shared_ptr<std::set<std::string>>& sensorNames,
339413961deSRichard Marian Thomaiyar                     Callback&& callback)
340413961deSRichard Marian Thomaiyar {
341413961deSRichard Marian Thomaiyar     auto objectsWithConnectionCb =
3428cb2c024SEd Tanous         [callback = std::forward<Callback>(callback)](
3438cb2c024SEd Tanous             const std::set<std::string>& connections,
344413961deSRichard Marian Thomaiyar             const std::set<std::pair<std::string, std::string>>&
3453174e4dfSEd Tanous             /*objectsWithConnection*/) { callback(connections); };
34681ce609eSEd Tanous     getObjectsWithConnection(sensorsAsyncResp, sensorNames,
347413961deSRichard Marian Thomaiyar                              std::move(objectsWithConnectionCb));
34808777fb0SLewanczyk, Dawid }
34908777fb0SLewanczyk, Dawid 
35008777fb0SLewanczyk, Dawid /**
35149c53ac9SJohnathan Mantey  * @brief Shrinks the list of sensors for processing
35249c53ac9SJohnathan Mantey  * @param SensorsAysncResp  The class holding the Redfish response
35349c53ac9SJohnathan Mantey  * @param allSensors  A list of all the sensors associated to the
35449c53ac9SJohnathan Mantey  * chassis element (i.e. baseboard, front panel, etc...)
35549c53ac9SJohnathan Mantey  * @param activeSensors A list that is a reduction of the incoming
35649c53ac9SJohnathan Mantey  * allSensors list.  Eliminate Thermal sensors when a Power request is
35749c53ac9SJohnathan Mantey  * made, and eliminate Power sensors when a Thermal request is made.
35849c53ac9SJohnathan Mantey  */
35923a21a1cSEd Tanous inline void reduceSensorList(
3607f1cc26dSEd Tanous     crow::Response& res, std::string_view chassisSubNode,
361cf9e417dSEd Tanous     std::span<const std::string_view> sensorTypes,
36249c53ac9SJohnathan Mantey     const std::vector<std::string>* allSensors,
363fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& activeSensors)
36449c53ac9SJohnathan Mantey {
36549c53ac9SJohnathan Mantey     if ((allSensors == nullptr) || (activeSensors == nullptr))
36649c53ac9SJohnathan Mantey     {
3677f1cc26dSEd Tanous         messages::resourceNotFound(res, chassisSubNode,
3680c728b42SJanet Adkins                                    chassisSubNode == sensors::thermalNodeStr
369a0ec28b6SAdrian Ambrożewicz                                        ? "Temperatures"
37049c53ac9SJohnathan Mantey                                        : "Voltages");
37149c53ac9SJohnathan Mantey 
37249c53ac9SJohnathan Mantey         return;
37349c53ac9SJohnathan Mantey     }
37449c53ac9SJohnathan Mantey     if (allSensors->empty())
37549c53ac9SJohnathan Mantey     {
37649c53ac9SJohnathan Mantey         // Nothing to do, the activeSensors object is also empty
37749c53ac9SJohnathan Mantey         return;
37849c53ac9SJohnathan Mantey     }
37949c53ac9SJohnathan Mantey 
3807f1cc26dSEd Tanous     for (std::string_view type : sensorTypes)
38149c53ac9SJohnathan Mantey     {
38249c53ac9SJohnathan Mantey         for (const std::string& sensor : *allSensors)
38349c53ac9SJohnathan Mantey         {
38411ba3979SEd Tanous             if (sensor.starts_with(type))
38549c53ac9SJohnathan Mantey             {
38649c53ac9SJohnathan Mantey                 activeSensors->emplace(sensor);
38749c53ac9SJohnathan Mantey             }
38849c53ac9SJohnathan Mantey         }
38949c53ac9SJohnathan Mantey     }
39049c53ac9SJohnathan Mantey }
39149c53ac9SJohnathan Mantey 
3927f1cc26dSEd Tanous /*
3937f1cc26dSEd Tanous  *Populates the top level collection for a given subnode.  Populates
3947f1cc26dSEd Tanous  *SensorCollection, Power, or Thermal schemas.
3957f1cc26dSEd Tanous  *
3967f1cc26dSEd Tanous  * */
3977f1cc26dSEd Tanous inline void populateChassisNode(nlohmann::json& jsonValue,
3987f1cc26dSEd Tanous                                 std::string_view chassisSubNode)
3997f1cc26dSEd Tanous {
4000c728b42SJanet Adkins     if (chassisSubNode == sensors::powerNodeStr)
4017f1cc26dSEd Tanous     {
4027f1cc26dSEd Tanous         jsonValue["@odata.type"] = "#Power.v1_5_2.Power";
4037f1cc26dSEd Tanous     }
4040c728b42SJanet Adkins     else if (chassisSubNode == sensors::thermalNodeStr)
4057f1cc26dSEd Tanous     {
4067f1cc26dSEd Tanous         jsonValue["@odata.type"] = "#Thermal.v1_4_0.Thermal";
4077f1cc26dSEd Tanous         jsonValue["Fans"] = nlohmann::json::array();
4087f1cc26dSEd Tanous         jsonValue["Temperatures"] = nlohmann::json::array();
4097f1cc26dSEd Tanous     }
4100c728b42SJanet Adkins     else if (chassisSubNode == sensors::sensorsNodeStr)
4117f1cc26dSEd Tanous     {
4127f1cc26dSEd Tanous         jsonValue["@odata.type"] = "#SensorCollection.SensorCollection";
4137f1cc26dSEd Tanous         jsonValue["Description"] = "Collection of Sensors for this Chassis";
4147f1cc26dSEd Tanous         jsonValue["Members"] = nlohmann::json::array();
4157f1cc26dSEd Tanous         jsonValue["Members@odata.count"] = 0;
4167f1cc26dSEd Tanous     }
4177f1cc26dSEd Tanous 
4180c728b42SJanet Adkins     if (chassisSubNode != sensors::sensorsNodeStr)
4197f1cc26dSEd Tanous     {
4207f1cc26dSEd Tanous         jsonValue["Id"] = chassisSubNode;
4217f1cc26dSEd Tanous     }
4227f1cc26dSEd Tanous     jsonValue["Name"] = chassisSubNode;
4237f1cc26dSEd Tanous }
4247f1cc26dSEd Tanous 
42549c53ac9SJohnathan Mantey /**
42608777fb0SLewanczyk, Dawid  * @brief Retrieves requested chassis sensors and redundancy data from DBus .
427588c3f0dSKowalski, Kamil  * @param SensorsAsyncResp   Pointer to object holding response data
42808777fb0SLewanczyk, Dawid  * @param callback  Callback for next step in gathered sensor processing
42908777fb0SLewanczyk, Dawid  */
43008777fb0SLewanczyk, Dawid template <typename Callback>
4317f1cc26dSEd Tanous void getChassis(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
4327f1cc26dSEd Tanous                 std::string_view chassisId, std::string_view chassisSubNode,
433cf9e417dSEd Tanous                 std::span<const std::string_view> sensorTypes,
434cf9e417dSEd Tanous                 Callback&& callback)
4351abe55efSEd Tanous {
43662598e31SEd Tanous     BMCWEB_LOG_DEBUG("getChassis enter");
4377a1dbc48SGeorge Liu 
4387a1dbc48SGeorge Liu     // Get the Chassis Collection
4397a1dbc48SGeorge Liu     dbus::utility::getSubTreePaths(
4403f95a277SMyung Bae         "/xyz/openbmc_project/inventory", 0, chassisInterfaces,
4418cb2c024SEd Tanous         [callback = std::forward<Callback>(callback), asyncResp,
4427f1cc26dSEd Tanous          chassisIdStr{std::string(chassisId)},
4434e0d8789SEd Tanous          chassisSubNode{std::string(chassisSubNode)},
4444e0d8789SEd Tanous          sensorTypes](const boost::system::error_code& ec,
4454e0d8789SEd Tanous                       const dbus::utility::MapperGetSubTreePathsResponse&
4464e0d8789SEd Tanous                           chassisPaths) mutable {
44762598e31SEd Tanous             BMCWEB_LOG_DEBUG("getChassis respHandler enter");
4481abe55efSEd Tanous             if (ec)
4491abe55efSEd Tanous             {
45062598e31SEd Tanous                 BMCWEB_LOG_ERROR("getChassis respHandler DBUS error: {}", ec);
4517f1cc26dSEd Tanous                 messages::internalError(asyncResp->res);
45208777fb0SLewanczyk, Dawid                 return;
45308777fb0SLewanczyk, Dawid             }
45449c53ac9SJohnathan Mantey             const std::string* chassisPath = nullptr;
45549c53ac9SJohnathan Mantey             for (const std::string& chassis : chassisPaths)
4561abe55efSEd Tanous             {
45728aa8de5SGeorge Liu                 sdbusplus::message::object_path path(chassis);
458f8fe53e7SEd Tanous                 std::string chassisName = path.filename();
45928aa8de5SGeorge Liu                 if (chassisName.empty())
4601abe55efSEd Tanous                 {
46162598e31SEd Tanous                     BMCWEB_LOG_ERROR("Failed to find '/' in {}", chassis);
462daf36e2eSEd Tanous                     continue;
463daf36e2eSEd Tanous                 }
4647f1cc26dSEd Tanous                 if (chassisName == chassisIdStr)
4651abe55efSEd Tanous                 {
46649c53ac9SJohnathan Mantey                     chassisPath = &chassis;
46749c53ac9SJohnathan Mantey                     break;
468daf36e2eSEd Tanous                 }
46949c53ac9SJohnathan Mantey             }
47049c53ac9SJohnathan Mantey             if (chassisPath == nullptr)
4711abe55efSEd Tanous             {
472bd79bce8SPatrick Williams                 messages::resourceNotFound(asyncResp->res, "Chassis",
473bd79bce8SPatrick Williams                                            chassisIdStr);
47449c53ac9SJohnathan Mantey                 return;
4751abe55efSEd Tanous             }
4767f1cc26dSEd Tanous             populateChassisNode(asyncResp->res.jsonValue, chassisSubNode);
47708777fb0SLewanczyk, Dawid 
478ef4c65b7SEd Tanous             asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
479ef4c65b7SEd Tanous                 "/redfish/v1/Chassis/{}/{}", chassisIdStr, chassisSubNode);
48095a3ecadSAnthony Wilson 
4818fb49dd6SShawn McCarney             // Get the list of all sensors for this Chassis element
4828fb49dd6SShawn McCarney             std::string sensorPath = *chassisPath + "/all_sensors";
4836c3e9451SGeorge Liu             dbus::utility::getAssociationEndPoints(
4844e0d8789SEd Tanous                 sensorPath, [asyncResp, chassisSubNode, sensorTypes,
4854e0d8789SEd Tanous                              callback = std::forward<Callback>(callback)](
4868b24275dSEd Tanous                                 const boost::system::error_code& ec2,
4874e0d8789SEd Tanous                                 const dbus::utility::MapperEndPoints&
4884e0d8789SEd Tanous                                     nodeSensorList) mutable {
4898b24275dSEd Tanous                     if (ec2)
49049c53ac9SJohnathan Mantey                     {
4918b24275dSEd Tanous                         if (ec2.value() != EBADR)
49249c53ac9SJohnathan Mantey                         {
4937f1cc26dSEd Tanous                             messages::internalError(asyncResp->res);
49449c53ac9SJohnathan Mantey                             return;
49549c53ac9SJohnathan Mantey                         }
49649c53ac9SJohnathan Mantey                     }
497bd79bce8SPatrick Williams                     const std::shared_ptr<std::set<std::string>>
498bd79bce8SPatrick Williams                         culledSensorList =
499fe04d49cSNan Zhou                             std::make_shared<std::set<std::string>>();
500bd79bce8SPatrick Williams                     reduceSensorList(asyncResp->res, chassisSubNode,
501bd79bce8SPatrick Williams                                      sensorTypes, &nodeSensorList,
502bd79bce8SPatrick Williams                                      culledSensorList);
503bd79bce8SPatrick Williams                     BMCWEB_LOG_DEBUG("Finishing with {}",
504bd79bce8SPatrick Williams                                      culledSensorList->size());
50549c53ac9SJohnathan Mantey                     callback(culledSensorList);
5061e1e598dSJonathan Doman                 });
5077a1dbc48SGeorge Liu         });
50862598e31SEd Tanous     BMCWEB_LOG_DEBUG("getChassis exit");
50908777fb0SLewanczyk, Dawid }
51008777fb0SLewanczyk, Dawid 
51108777fb0SLewanczyk, Dawid /**
5121d7c0054SEd Tanous  * @brief Builds a json sensor representation of a sensor.
5131d7c0054SEd Tanous  * @param sensorName  The name of the sensor to be built
5141d7c0054SEd Tanous  * @param sensorType  The type (temperature, fan_tach, etc) of the sensor to
5151d7c0054SEd Tanous  * build
5168ece0e45SEd Tanous  * @param chassisSubNode The subnode (thermal, sensor, etc) of the sensor
5171d7c0054SEd Tanous  * @param interfacesDict  A dictionary of the interfaces and properties of said
5181d7c0054SEd Tanous  * interfaces to be built from
5191d7c0054SEd Tanous  * @param sensorJson  The json object to fill
5201d7c0054SEd Tanous  * @param inventoryItem D-Bus inventory item associated with the sensor.  Will
5211d7c0054SEd Tanous  * be nullptr if no associated inventory item was found.
5221d7c0054SEd Tanous  */
5231d7c0054SEd Tanous inline void objectInterfacesToJson(
5241d7c0054SEd Tanous     const std::string& sensorName, const std::string& sensorType,
5250c728b42SJanet Adkins     const sensor_utils::ChassisSubNode chassisSubNode,
52680f79a40SMichael Shen     const dbus::utility::DBusInterfacesMap& interfacesDict,
5271d7c0054SEd Tanous     nlohmann::json& sensorJson, InventoryItem* inventoryItem)
5281d7c0054SEd Tanous {
5291d7c0054SEd Tanous     for (const auto& [interface, valuesDict] : interfacesDict)
5301d7c0054SEd Tanous     {
531c9563608SJanet Adkins         sensor_utils::objectPropertiesToJson(
532c9563608SJanet Adkins             sensorName, sensorType, chassisSubNode, valuesDict, sensorJson,
533c9563608SJanet Adkins             inventoryItem);
5341d7c0054SEd Tanous     }
53562598e31SEd Tanous     BMCWEB_LOG_DEBUG("Added sensor {}", sensorName);
5361d7c0054SEd Tanous }
5371d7c0054SEd Tanous 
538b5a76932SEd Tanous inline void populateFanRedundancy(
539b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
5408bd25ccdSJames Feist {
541e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
542e99073f5SGeorge Liu         "xyz.openbmc_project.Control.FanRedundancy"};
543e99073f5SGeorge Liu     dbus::utility::getSubTree(
544e99073f5SGeorge Liu         "/xyz/openbmc_project/control", 2, interfaces,
545b9d36b47SEd Tanous         [sensorsAsyncResp](
546e99073f5SGeorge Liu             const boost::system::error_code& ec,
547b9d36b47SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& resp) {
5488bd25ccdSJames Feist             if (ec)
5498bd25ccdSJames Feist             {
5508bd25ccdSJames Feist                 return; // don't have to have this interface
5518bd25ccdSJames Feist             }
5526c3e9451SGeorge Liu             for (const std::pair<std::string, dbus::utility::MapperServiceMap>&
553e278c18fSEd Tanous                      pathPair : resp)
5548bd25ccdSJames Feist             {
555e278c18fSEd Tanous                 const std::string& path = pathPair.first;
556bd79bce8SPatrick Williams                 const dbus::utility::MapperServiceMap& objDict =
557bd79bce8SPatrick Williams                     pathPair.second;
5588bd25ccdSJames Feist                 if (objDict.empty())
5598bd25ccdSJames Feist                 {
5608bd25ccdSJames Feist                     continue; // this should be impossible
5618bd25ccdSJames Feist                 }
5628bd25ccdSJames Feist 
5638bd25ccdSJames Feist                 const std::string& owner = objDict.begin()->first;
5646c3e9451SGeorge Liu                 dbus::utility::getAssociationEndPoints(
5656c3e9451SGeorge Liu                     path + "/chassis",
5666c3e9451SGeorge Liu                     [path, owner, sensorsAsyncResp](
5678b24275dSEd Tanous                         const boost::system::error_code& ec2,
5686c3e9451SGeorge Liu                         const dbus::utility::MapperEndPoints& endpoints) {
5698b24275dSEd Tanous                         if (ec2)
5708bd25ccdSJames Feist                         {
5718bd25ccdSJames Feist                             return; // if they don't have an association we
5728bd25ccdSJames Feist                                     // can't tell what chassis is
5738bd25ccdSJames Feist                         }
5743544d2a7SEd Tanous                         auto found = std::ranges::find_if(
575bd79bce8SPatrick Williams                             endpoints,
576bd79bce8SPatrick Williams                             [sensorsAsyncResp](const std::string& entry) {
577bd79bce8SPatrick Williams                                 return entry.find(
578bd79bce8SPatrick Williams                                            sensorsAsyncResp->chassisId) !=
5798bd25ccdSJames Feist                                        std::string::npos;
5808bd25ccdSJames Feist                             });
5818bd25ccdSJames Feist 
5821e1e598dSJonathan Doman                         if (found == endpoints.end())
5838bd25ccdSJames Feist                         {
5848bd25ccdSJames Feist                             return;
5858bd25ccdSJames Feist                         }
586deae6a78SEd Tanous                         dbus::utility::getAllProperties(
58786d89ed7SKrzysztof Grobelny                             *crow::connections::systemBus, owner, path,
58886d89ed7SKrzysztof Grobelny                             "xyz.openbmc_project.Control.FanRedundancy",
5898bd25ccdSJames Feist                             [path, sensorsAsyncResp](
5908b24275dSEd Tanous                                 const boost::system::error_code& ec3,
59186d89ed7SKrzysztof Grobelny                                 const dbus::utility::DBusPropertiesMap& ret) {
5928b24275dSEd Tanous                                 if (ec3)
5938bd25ccdSJames Feist                                 {
5948bd25ccdSJames Feist                                     return; // don't have to have this
5958bd25ccdSJames Feist                                             // interface
5968bd25ccdSJames Feist                                 }
5978bd25ccdSJames Feist 
59886d89ed7SKrzysztof Grobelny                                 const uint8_t* allowedFailures = nullptr;
599bd79bce8SPatrick Williams                                 const std::vector<std::string>* collection =
600bd79bce8SPatrick Williams                                     nullptr;
60186d89ed7SKrzysztof Grobelny                                 const std::string* status = nullptr;
60286d89ed7SKrzysztof Grobelny 
603bd79bce8SPatrick Williams                                 const bool success =
604bd79bce8SPatrick Williams                                     sdbusplus::unpackPropertiesNoThrow(
60586d89ed7SKrzysztof Grobelny                                         dbus_utils::UnpackErrorPrinter(), ret,
606bd79bce8SPatrick Williams                                         "AllowedFailures", allowedFailures,
607bd79bce8SPatrick Williams                                         "Collection", collection, "Status",
608bd79bce8SPatrick Williams                                         status);
60986d89ed7SKrzysztof Grobelny 
61086d89ed7SKrzysztof Grobelny                                 if (!success)
61186d89ed7SKrzysztof Grobelny                                 {
61286d89ed7SKrzysztof Grobelny                                     messages::internalError(
61386d89ed7SKrzysztof Grobelny                                         sensorsAsyncResp->asyncResp->res);
61486d89ed7SKrzysztof Grobelny                                     return;
61586d89ed7SKrzysztof Grobelny                                 }
61686d89ed7SKrzysztof Grobelny 
617bd79bce8SPatrick Williams                                 if (allowedFailures == nullptr ||
618bd79bce8SPatrick Williams                                     collection == nullptr || status == nullptr)
6198bd25ccdSJames Feist                                 {
620bd79bce8SPatrick Williams                                     BMCWEB_LOG_ERROR(
621bd79bce8SPatrick Williams                                         "Invalid redundancy interface");
6228bd25ccdSJames Feist                                     messages::internalError(
6238d1b46d7Szhanghch05                                         sensorsAsyncResp->asyncResp->res);
6248bd25ccdSJames Feist                                     return;
6258bd25ccdSJames Feist                                 }
6268bd25ccdSJames Feist 
627bd79bce8SPatrick Williams                                 sdbusplus::message::object_path objectPath(
628bd79bce8SPatrick Williams                                     path);
62928aa8de5SGeorge Liu                                 std::string name = objectPath.filename();
63028aa8de5SGeorge Liu                                 if (name.empty())
6318bd25ccdSJames Feist                                 {
6328bd25ccdSJames Feist                                     // this should be impossible
6338bd25ccdSJames Feist                                     messages::internalError(
6348d1b46d7Szhanghch05                                         sensorsAsyncResp->asyncResp->res);
6358bd25ccdSJames Feist                                     return;
6368bd25ccdSJames Feist                                 }
63718f8f608SEd Tanous                                 std::ranges::replace(name, '_', ' ');
6388bd25ccdSJames Feist 
6398bd25ccdSJames Feist                                 std::string health;
6408bd25ccdSJames Feist 
64111ba3979SEd Tanous                                 if (status->ends_with("Full"))
6428bd25ccdSJames Feist                                 {
6438bd25ccdSJames Feist                                     health = "OK";
6448bd25ccdSJames Feist                                 }
64511ba3979SEd Tanous                                 else if (status->ends_with("Degraded"))
6468bd25ccdSJames Feist                                 {
6478bd25ccdSJames Feist                                     health = "Warning";
6488bd25ccdSJames Feist                                 }
6498bd25ccdSJames Feist                                 else
6508bd25ccdSJames Feist                                 {
6518bd25ccdSJames Feist                                     health = "Critical";
6528bd25ccdSJames Feist                                 }
6531476687dSEd Tanous                                 nlohmann::json::array_t redfishCollection;
6548bd25ccdSJames Feist                                 const auto& fanRedfish =
655bd79bce8SPatrick Williams                                     sensorsAsyncResp->asyncResp->res
656bd79bce8SPatrick Williams                                         .jsonValue["Fans"];
6578bd25ccdSJames Feist                                 for (const std::string& item : *collection)
6588bd25ccdSJames Feist                                 {
659bd79bce8SPatrick Williams                                     sdbusplus::message::object_path itemPath(
660bd79bce8SPatrick Williams                                         item);
6618a592810SEd Tanous                                     std::string itemName = itemPath.filename();
66228aa8de5SGeorge Liu                                     if (itemName.empty())
66328aa8de5SGeorge Liu                                     {
66428aa8de5SGeorge Liu                                         continue;
66528aa8de5SGeorge Liu                                     }
6668bd25ccdSJames Feist                                     /*
6678bd25ccdSJames Feist                                     todo(ed): merge patch that fixes the names
6688bd25ccdSJames Feist                                     std::replace(itemName.begin(),
6698bd25ccdSJames Feist                                                  itemName.end(), '_', ' ');*/
6703544d2a7SEd Tanous                                     auto schemaItem = std::ranges::find_if(
671bd79bce8SPatrick Williams                                         fanRedfish,
672bd79bce8SPatrick Williams                                         [itemName](const nlohmann::json& fan) {
6733e35c761SGeorge Liu                                             return fan["Name"] == itemName;
6748bd25ccdSJames Feist                                         });
6758bd25ccdSJames Feist                                     if (schemaItem != fanRedfish.end())
6768bd25ccdSJames Feist                                     {
6778a592810SEd Tanous                                         nlohmann::json::object_t collectionId;
6788a592810SEd Tanous                                         collectionId["@odata.id"] =
6791476687dSEd Tanous                                             (*schemaItem)["@odata.id"];
6801476687dSEd Tanous                                         redfishCollection.emplace_back(
6818a592810SEd Tanous                                             std::move(collectionId));
6828bd25ccdSJames Feist                                     }
6838bd25ccdSJames Feist                                     else
6848bd25ccdSJames Feist                                     {
685bd79bce8SPatrick Williams                                         BMCWEB_LOG_ERROR(
686bd79bce8SPatrick Williams                                             "failed to find fan in schema");
6878bd25ccdSJames Feist                                         messages::internalError(
6888d1b46d7Szhanghch05                                             sensorsAsyncResp->asyncResp->res);
6898bd25ccdSJames Feist                                         return;
6908bd25ccdSJames Feist                                     }
6918bd25ccdSJames Feist                                 }
6928bd25ccdSJames Feist 
693bd79bce8SPatrick Williams                                 size_t minNumNeeded =
694bd79bce8SPatrick Williams                                     collection->empty()
69526f6976fSEd Tanous                                         ? 0
696bd79bce8SPatrick Williams                                         : collection->size() - *allowedFailures;
697bd79bce8SPatrick Williams                                 nlohmann::json& jResp =
698bd79bce8SPatrick Williams                                     sensorsAsyncResp->asyncResp->res
6998bd25ccdSJames Feist                                         .jsonValue["Redundancy"];
7001476687dSEd Tanous 
7011476687dSEd Tanous                                 nlohmann::json::object_t redundancy;
702bd79bce8SPatrick Williams                                 boost::urls::url url = boost::urls::format(
703bd79bce8SPatrick Williams                                     "/redfish/v1/Chassis/{}/{}",
704ef4c65b7SEd Tanous                                     sensorsAsyncResp->chassisId,
705eddfc437SWilly Tu                                     sensorsAsyncResp->chassisSubNode);
706bd79bce8SPatrick Williams                                 url.set_fragment(
707bd79bce8SPatrick Williams                                     ("/Redundancy"_json_pointer / jResp.size())
708eddfc437SWilly Tu                                         .to_string());
709eddfc437SWilly Tu                                 redundancy["@odata.id"] = std::move(url);
710bd79bce8SPatrick Williams                                 redundancy["@odata.type"] =
711bd79bce8SPatrick Williams                                     "#Redundancy.v1_3_2.Redundancy";
7121476687dSEd Tanous                                 redundancy["MinNumNeeded"] = minNumNeeded;
713bd79bce8SPatrick Williams                                 redundancy["Mode"] =
714bd79bce8SPatrick Williams                                     redundancy::RedundancyType::NPlusM;
7151476687dSEd Tanous                                 redundancy["Name"] = name;
7161476687dSEd Tanous                                 redundancy["RedundancySet"] = redfishCollection;
7171476687dSEd Tanous                                 redundancy["Status"]["Health"] = health;
718bd79bce8SPatrick Williams                                 redundancy["Status"]["State"] =
719bd79bce8SPatrick Williams                                     resource::State::Enabled;
7201476687dSEd Tanous 
721b2ba3072SPatrick Williams                                 jResp.emplace_back(std::move(redundancy));
72286d89ed7SKrzysztof Grobelny                             });
7231e1e598dSJonathan Doman                     });
7248bd25ccdSJames Feist             }
725e99073f5SGeorge Liu         });
7268bd25ccdSJames Feist }
7278bd25ccdSJames Feist 
728504af5a0SPatrick Williams inline void sortJSONResponse(
729504af5a0SPatrick Williams     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
73049c53ac9SJohnathan Mantey {
7318d1b46d7Szhanghch05     nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue;
73249c53ac9SJohnathan Mantey     std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
7330c728b42SJanet Adkins     if (sensorsAsyncResp->chassisSubNode == sensors::powerNodeStr)
73449c53ac9SJohnathan Mantey     {
73549c53ac9SJohnathan Mantey         sensorHeaders = {"Voltages", "PowerSupplies"};
73649c53ac9SJohnathan Mantey     }
73749c53ac9SJohnathan Mantey     for (const std::string& sensorGroup : sensorHeaders)
73849c53ac9SJohnathan Mantey     {
73949c53ac9SJohnathan Mantey         nlohmann::json::iterator entry = response.find(sensorGroup);
7404e196b9aSEd Tanous         if (entry == response.end())
74149c53ac9SJohnathan Mantey         {
7424e196b9aSEd Tanous             continue;
7434e196b9aSEd Tanous         }
7444e196b9aSEd Tanous         nlohmann::json::array_t* arr =
7454e196b9aSEd Tanous             entry->get_ptr<nlohmann::json::array_t*>();
7464e196b9aSEd Tanous         if (arr == nullptr)
7474e196b9aSEd Tanous         {
7484e196b9aSEd Tanous             continue;
7494e196b9aSEd Tanous         }
7504e196b9aSEd Tanous         json_util::sortJsonArrayByKey(*arr, "Name");
75149c53ac9SJohnathan Mantey 
75249c53ac9SJohnathan Mantey         // add the index counts to the end of each entry
75349c53ac9SJohnathan Mantey         size_t count = 0;
75449c53ac9SJohnathan Mantey         for (nlohmann::json& sensorJson : *entry)
75549c53ac9SJohnathan Mantey         {
75649c53ac9SJohnathan Mantey             nlohmann::json::iterator odata = sensorJson.find("@odata.id");
75749c53ac9SJohnathan Mantey             if (odata == sensorJson.end())
75849c53ac9SJohnathan Mantey             {
75949c53ac9SJohnathan Mantey                 continue;
76049c53ac9SJohnathan Mantey             }
76182b286fbSEd Tanous             const auto nameIt = sensorJson.find("Name");
76282b286fbSEd Tanous             if (nameIt == sensorJson.end())
76349c53ac9SJohnathan Mantey             {
76482b286fbSEd Tanous                 continue;
76582b286fbSEd Tanous             }
76682b286fbSEd Tanous             std::string* value = odata->get_ptr<std::string*>();
76782b286fbSEd Tanous             if (value == nullptr)
76882b286fbSEd Tanous             {
76982b286fbSEd Tanous                 continue;
77082b286fbSEd Tanous             }
77182b286fbSEd Tanous             const std::string* name = nameIt->get_ptr<const std::string*>();
77282b286fbSEd Tanous             if (name == nullptr)
77382b286fbSEd Tanous             {
77482b286fbSEd Tanous                 continue;
77582b286fbSEd Tanous             }
776eddfc437SWilly Tu             *value += "/" + std::to_string(count);
7773e35c761SGeorge Liu             sensorJson["MemberId"] = std::to_string(count);
77849c53ac9SJohnathan Mantey             count++;
77982b286fbSEd Tanous             sensorsAsyncResp->updateUri(*name, *value);
78049c53ac9SJohnathan Mantey         }
78149c53ac9SJohnathan Mantey     }
78249c53ac9SJohnathan Mantey }
78349c53ac9SJohnathan Mantey 
78408777fb0SLewanczyk, Dawid /**
785adc4f0dbSShawn McCarney  * @brief Finds the inventory item with the specified object path.
786adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
787adc4f0dbSShawn McCarney  * @param invItemObjPath D-Bus object path of inventory item.
788adc4f0dbSShawn McCarney  * @return Inventory item within vector, or nullptr if no match found.
7898fb49dd6SShawn McCarney  */
79023a21a1cSEd Tanous inline InventoryItem* findInventoryItem(
791b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
792adc4f0dbSShawn McCarney     const std::string& invItemObjPath)
7938fb49dd6SShawn McCarney {
794adc4f0dbSShawn McCarney     for (InventoryItem& inventoryItem : *inventoryItems)
7958fb49dd6SShawn McCarney     {
796adc4f0dbSShawn McCarney         if (inventoryItem.objectPath == invItemObjPath)
7978fb49dd6SShawn McCarney         {
798adc4f0dbSShawn McCarney             return &inventoryItem;
7998fb49dd6SShawn McCarney         }
8008fb49dd6SShawn McCarney     }
8018fb49dd6SShawn McCarney     return nullptr;
8028fb49dd6SShawn McCarney }
8038fb49dd6SShawn McCarney 
8048fb49dd6SShawn McCarney /**
805adc4f0dbSShawn McCarney  * @brief Finds the inventory item associated with the specified sensor.
806adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
807adc4f0dbSShawn McCarney  * @param sensorObjPath D-Bus object path of sensor.
808adc4f0dbSShawn McCarney  * @return Inventory item within vector, or nullptr if no match found.
8098fb49dd6SShawn McCarney  */
81023a21a1cSEd Tanous inline InventoryItem* findInventoryItemForSensor(
811b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
812adc4f0dbSShawn McCarney     const std::string& sensorObjPath)
813adc4f0dbSShawn McCarney {
814adc4f0dbSShawn McCarney     for (InventoryItem& inventoryItem : *inventoryItems)
815adc4f0dbSShawn McCarney     {
816db0d36efSEd Tanous         if (inventoryItem.sensors.contains(sensorObjPath))
817adc4f0dbSShawn McCarney         {
818adc4f0dbSShawn McCarney             return &inventoryItem;
819adc4f0dbSShawn McCarney         }
820adc4f0dbSShawn McCarney     }
821adc4f0dbSShawn McCarney     return nullptr;
822adc4f0dbSShawn McCarney }
823adc4f0dbSShawn McCarney 
824adc4f0dbSShawn McCarney /**
825d500549bSAnthony Wilson  * @brief Finds the inventory item associated with the specified led path.
826d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
827d500549bSAnthony Wilson  * @param ledObjPath D-Bus object path of led.
828d500549bSAnthony Wilson  * @return Inventory item within vector, or nullptr if no match found.
829d500549bSAnthony Wilson  */
830bd79bce8SPatrick Williams inline InventoryItem* findInventoryItemForLed(
831bd79bce8SPatrick Williams     std::vector<InventoryItem>& inventoryItems, const std::string& ledObjPath)
832d500549bSAnthony Wilson {
833d500549bSAnthony Wilson     for (InventoryItem& inventoryItem : inventoryItems)
834d500549bSAnthony Wilson     {
835d500549bSAnthony Wilson         if (inventoryItem.ledObjectPath == ledObjPath)
836d500549bSAnthony Wilson         {
837d500549bSAnthony Wilson             return &inventoryItem;
838d500549bSAnthony Wilson         }
839d500549bSAnthony Wilson     }
840d500549bSAnthony Wilson     return nullptr;
841d500549bSAnthony Wilson }
842d500549bSAnthony Wilson 
843d500549bSAnthony Wilson /**
844adc4f0dbSShawn McCarney  * @brief Adds inventory item and associated sensor to specified vector.
845adc4f0dbSShawn McCarney  *
846adc4f0dbSShawn McCarney  * Adds a new InventoryItem to the vector if necessary.  Searches for an
847adc4f0dbSShawn McCarney  * existing InventoryItem with the specified object path.  If not found, one is
848adc4f0dbSShawn McCarney  * added to the vector.
849adc4f0dbSShawn McCarney  *
850adc4f0dbSShawn McCarney  * Next, the specified sensor is added to the set of sensors associated with the
851adc4f0dbSShawn McCarney  * InventoryItem.
852adc4f0dbSShawn McCarney  *
853adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
854adc4f0dbSShawn McCarney  * @param invItemObjPath D-Bus object path of inventory item.
855adc4f0dbSShawn McCarney  * @param sensorObjPath D-Bus object path of sensor
856adc4f0dbSShawn McCarney  */
857b5a76932SEd Tanous inline void addInventoryItem(
858b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
859b5a76932SEd Tanous     const std::string& invItemObjPath, const std::string& sensorObjPath)
860adc4f0dbSShawn McCarney {
861adc4f0dbSShawn McCarney     // Look for inventory item in vector
862bd79bce8SPatrick Williams     InventoryItem* inventoryItem =
863bd79bce8SPatrick Williams         findInventoryItem(inventoryItems, invItemObjPath);
864adc4f0dbSShawn McCarney 
865adc4f0dbSShawn McCarney     // If inventory item doesn't exist in vector, add it
866adc4f0dbSShawn McCarney     if (inventoryItem == nullptr)
867adc4f0dbSShawn McCarney     {
868adc4f0dbSShawn McCarney         inventoryItems->emplace_back(invItemObjPath);
869adc4f0dbSShawn McCarney         inventoryItem = &(inventoryItems->back());
870adc4f0dbSShawn McCarney     }
871adc4f0dbSShawn McCarney 
872adc4f0dbSShawn McCarney     // Add sensor to set of sensors associated with inventory item
873adc4f0dbSShawn McCarney     inventoryItem->sensors.emplace(sensorObjPath);
874adc4f0dbSShawn McCarney }
875adc4f0dbSShawn McCarney 
876adc4f0dbSShawn McCarney /**
877adc4f0dbSShawn McCarney  * @brief Stores D-Bus data in the specified inventory item.
878adc4f0dbSShawn McCarney  *
879adc4f0dbSShawn McCarney  * Finds D-Bus data in the specified map of interfaces.  Stores the data in the
880adc4f0dbSShawn McCarney  * specified InventoryItem.
881adc4f0dbSShawn McCarney  *
882adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
883adc4f0dbSShawn McCarney  * response.
884adc4f0dbSShawn McCarney  *
885adc4f0dbSShawn McCarney  * @param inventoryItem Inventory item where data will be stored.
886adc4f0dbSShawn McCarney  * @param interfacesDict Map containing D-Bus interfaces and their properties
887adc4f0dbSShawn McCarney  * for the specified inventory item.
888adc4f0dbSShawn McCarney  */
88923a21a1cSEd Tanous inline void storeInventoryItemData(
890adc4f0dbSShawn McCarney     InventoryItem& inventoryItem,
89180f79a40SMichael Shen     const dbus::utility::DBusInterfacesMap& interfacesDict)
8928fb49dd6SShawn McCarney {
893adc4f0dbSShawn McCarney     // Get properties from Inventory.Item interface
894711ac7a9SEd Tanous 
8959eb808c1SEd Tanous     for (const auto& [interface, values] : interfacesDict)
8968fb49dd6SShawn McCarney     {
897711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Item")
8988fb49dd6SShawn McCarney         {
8999eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
900711ac7a9SEd Tanous             {
901711ac7a9SEd Tanous                 if (name == "Present")
902711ac7a9SEd Tanous                 {
903711ac7a9SEd Tanous                     const bool* value = std::get_if<bool>(&dbusValue);
904adc4f0dbSShawn McCarney                     if (value != nullptr)
9058fb49dd6SShawn McCarney                     {
906adc4f0dbSShawn McCarney                         inventoryItem.isPresent = *value;
9078fb49dd6SShawn McCarney                     }
9088fb49dd6SShawn McCarney                 }
9098fb49dd6SShawn McCarney             }
910711ac7a9SEd Tanous         }
911adc4f0dbSShawn McCarney         // Check if Inventory.Item.PowerSupply interface is present
912711ac7a9SEd Tanous 
913711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Item.PowerSupply")
9148fb49dd6SShawn McCarney         {
915adc4f0dbSShawn McCarney             inventoryItem.isPowerSupply = true;
9168fb49dd6SShawn McCarney         }
917adc4f0dbSShawn McCarney 
918adc4f0dbSShawn McCarney         // Get properties from Inventory.Decorator.Asset interface
919711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
920adc4f0dbSShawn McCarney         {
9219eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
922711ac7a9SEd Tanous             {
923711ac7a9SEd Tanous                 if (name == "Manufacturer")
924adc4f0dbSShawn McCarney                 {
925adc4f0dbSShawn McCarney                     const std::string* value =
926711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
927adc4f0dbSShawn McCarney                     if (value != nullptr)
928adc4f0dbSShawn McCarney                     {
929adc4f0dbSShawn McCarney                         inventoryItem.manufacturer = *value;
930adc4f0dbSShawn McCarney                     }
931adc4f0dbSShawn McCarney                 }
932711ac7a9SEd Tanous                 if (name == "Model")
933adc4f0dbSShawn McCarney                 {
934adc4f0dbSShawn McCarney                     const std::string* value =
935711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
936adc4f0dbSShawn McCarney                     if (value != nullptr)
937adc4f0dbSShawn McCarney                     {
938adc4f0dbSShawn McCarney                         inventoryItem.model = *value;
939adc4f0dbSShawn McCarney                     }
940adc4f0dbSShawn McCarney                 }
941711ac7a9SEd Tanous                 if (name == "SerialNumber")
942adc4f0dbSShawn McCarney                 {
943adc4f0dbSShawn McCarney                     const std::string* value =
944711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
945adc4f0dbSShawn McCarney                     if (value != nullptr)
946adc4f0dbSShawn McCarney                     {
947adc4f0dbSShawn McCarney                         inventoryItem.serialNumber = *value;
948adc4f0dbSShawn McCarney                     }
949adc4f0dbSShawn McCarney                 }
950711ac7a9SEd Tanous                 if (name == "PartNumber")
951711ac7a9SEd Tanous                 {
952711ac7a9SEd Tanous                     const std::string* value =
953711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
954711ac7a9SEd Tanous                     if (value != nullptr)
955711ac7a9SEd Tanous                     {
956711ac7a9SEd Tanous                         inventoryItem.partNumber = *value;
957711ac7a9SEd Tanous                     }
958711ac7a9SEd Tanous                 }
959711ac7a9SEd Tanous             }
960adc4f0dbSShawn McCarney         }
961adc4f0dbSShawn McCarney 
962711ac7a9SEd Tanous         if (interface ==
963711ac7a9SEd Tanous             "xyz.openbmc_project.State.Decorator.OperationalStatus")
964adc4f0dbSShawn McCarney         {
9659eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
966adc4f0dbSShawn McCarney             {
967711ac7a9SEd Tanous                 if (name == "Functional")
968711ac7a9SEd Tanous                 {
969711ac7a9SEd Tanous                     const bool* value = std::get_if<bool>(&dbusValue);
970adc4f0dbSShawn McCarney                     if (value != nullptr)
971adc4f0dbSShawn McCarney                     {
972adc4f0dbSShawn McCarney                         inventoryItem.isFunctional = *value;
9738fb49dd6SShawn McCarney                     }
9748fb49dd6SShawn McCarney                 }
9758fb49dd6SShawn McCarney             }
9768fb49dd6SShawn McCarney         }
977711ac7a9SEd Tanous     }
978711ac7a9SEd Tanous }
9798fb49dd6SShawn McCarney 
9808fb49dd6SShawn McCarney /**
981adc4f0dbSShawn McCarney  * @brief Gets D-Bus data for inventory items associated with sensors.
9828fb49dd6SShawn McCarney  *
983adc4f0dbSShawn McCarney  * Uses the specified connections (services) to obtain D-Bus data for inventory
984adc4f0dbSShawn McCarney  * items associated with sensors.  Stores the resulting data in the
985adc4f0dbSShawn McCarney  * inventoryItems vector.
9868fb49dd6SShawn McCarney  *
987adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
988adc4f0dbSShawn McCarney  * response.
989adc4f0dbSShawn McCarney  *
990adc4f0dbSShawn McCarney  * Finds the inventory item data asynchronously.  Invokes callback when data has
991adc4f0dbSShawn McCarney  * been obtained.
992adc4f0dbSShawn McCarney  *
993adc4f0dbSShawn McCarney  * The callback must have the following signature:
994adc4f0dbSShawn McCarney  *   @code
995d500549bSAnthony Wilson  *   callback(void)
996adc4f0dbSShawn McCarney  *   @endcode
997adc4f0dbSShawn McCarney  *
998adc4f0dbSShawn McCarney  * This function is called recursively, obtaining data asynchronously from one
999adc4f0dbSShawn McCarney  * connection in each call.  This ensures the callback is not invoked until the
1000adc4f0dbSShawn McCarney  * last asynchronous function has completed.
10018fb49dd6SShawn McCarney  *
10028fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
1003adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1004adc4f0dbSShawn McCarney  * @param invConnections Connections that provide data for the inventory items.
10058fb49dd6SShawn McCarney  * implements ObjectManager.
1006adc4f0dbSShawn McCarney  * @param callback Callback to invoke when inventory data has been obtained.
1007adc4f0dbSShawn McCarney  * @param invConnectionsIndex Current index in invConnections.  Only specified
1008adc4f0dbSShawn McCarney  * in recursive calls to this function.
10098fb49dd6SShawn McCarney  */
1010adc4f0dbSShawn McCarney template <typename Callback>
10114ff0f1f4SEd Tanous void getInventoryItemsData(
1012daadfb2eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1013daadfb2eSEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1014daadfb2eSEd Tanous     const std::shared_ptr<std::set<std::string>>& invConnections,
1015daadfb2eSEd Tanous     Callback&& callback, size_t invConnectionsIndex = 0)
10168fb49dd6SShawn McCarney {
101762598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryItemsData enter");
10188fb49dd6SShawn McCarney 
1019adc4f0dbSShawn McCarney     // If no more connections left, call callback
1020adc4f0dbSShawn McCarney     if (invConnectionsIndex >= invConnections->size())
10218fb49dd6SShawn McCarney     {
1022d500549bSAnthony Wilson         callback();
102362598e31SEd Tanous         BMCWEB_LOG_DEBUG("getInventoryItemsData exit");
1024adc4f0dbSShawn McCarney         return;
1025adc4f0dbSShawn McCarney     }
1026adc4f0dbSShawn McCarney 
1027adc4f0dbSShawn McCarney     // Get inventory item data from current connection
1028fe04d49cSNan Zhou     auto it = invConnections->begin();
1029fe04d49cSNan Zhou     std::advance(it, invConnectionsIndex);
1030adc4f0dbSShawn McCarney     if (it != invConnections->end())
1031adc4f0dbSShawn McCarney     {
1032adc4f0dbSShawn McCarney         const std::string& invConnection = *it;
1033adc4f0dbSShawn McCarney 
10345eb468daSGeorge Liu         // Get all object paths and their interfaces for current connection
10355eb468daSGeorge Liu         sdbusplus::message::object_path path("/xyz/openbmc_project/inventory");
10365eb468daSGeorge Liu         dbus::utility::getManagedObjects(
10375eb468daSGeorge Liu             invConnection, path,
10385eb468daSGeorge Liu             [sensorsAsyncResp, inventoryItems, invConnections,
10398cb2c024SEd Tanous              callback = std::forward<Callback>(callback), invConnectionsIndex](
10405e7e2dc5SEd Tanous                 const boost::system::error_code& ec,
10414e0d8789SEd Tanous                 const dbus::utility::ManagedObjectType& resp) mutable {
104262598e31SEd Tanous                 BMCWEB_LOG_DEBUG("getInventoryItemsData respHandler enter");
10438fb49dd6SShawn McCarney                 if (ec)
10448fb49dd6SShawn McCarney                 {
104562598e31SEd Tanous                     BMCWEB_LOG_ERROR(
104662598e31SEd Tanous                         "getInventoryItemsData respHandler DBus error {}", ec);
10478d1b46d7Szhanghch05                     messages::internalError(sensorsAsyncResp->asyncResp->res);
10488fb49dd6SShawn McCarney                     return;
10498fb49dd6SShawn McCarney                 }
10508fb49dd6SShawn McCarney 
10518fb49dd6SShawn McCarney                 // Loop through returned object paths
10528fb49dd6SShawn McCarney                 for (const auto& objDictEntry : resp)
10538fb49dd6SShawn McCarney                 {
10548fb49dd6SShawn McCarney                     const std::string& objPath =
10558fb49dd6SShawn McCarney                         static_cast<const std::string&>(objDictEntry.first);
10568fb49dd6SShawn McCarney 
1057bd79bce8SPatrick Williams                     // If this object path is one of the specified inventory
1058bd79bce8SPatrick Williams                     // items
1059bd79bce8SPatrick Williams                     InventoryItem* inventoryItem =
1060bd79bce8SPatrick Williams                         findInventoryItem(inventoryItems, objPath);
1061adc4f0dbSShawn McCarney                     if (inventoryItem != nullptr)
10628fb49dd6SShawn McCarney                     {
1063adc4f0dbSShawn McCarney                         // Store inventory data in InventoryItem
1064bd79bce8SPatrick Williams                         storeInventoryItemData(*inventoryItem,
1065bd79bce8SPatrick Williams                                                objDictEntry.second);
10668fb49dd6SShawn McCarney                     }
10678fb49dd6SShawn McCarney                 }
10688fb49dd6SShawn McCarney 
1069adc4f0dbSShawn McCarney                 // Recurse to get inventory item data from next connection
1070adc4f0dbSShawn McCarney                 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
1071d0090733SEd Tanous                                       invConnections, std::move(callback),
1072d0090733SEd Tanous                                       invConnectionsIndex + 1);
1073adc4f0dbSShawn McCarney 
107462598e31SEd Tanous                 BMCWEB_LOG_DEBUG("getInventoryItemsData respHandler exit");
10755eb468daSGeorge Liu             });
10768fb49dd6SShawn McCarney     }
10778fb49dd6SShawn McCarney 
107862598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryItemsData exit");
10798fb49dd6SShawn McCarney }
10808fb49dd6SShawn McCarney 
10818fb49dd6SShawn McCarney /**
1082adc4f0dbSShawn McCarney  * @brief Gets connections that provide D-Bus data for inventory items.
10838fb49dd6SShawn McCarney  *
1084adc4f0dbSShawn McCarney  * Gets the D-Bus connections (services) that provide data for the inventory
1085adc4f0dbSShawn McCarney  * items that are associated with sensors.
10868fb49dd6SShawn McCarney  *
10878fb49dd6SShawn McCarney  * Finds the connections asynchronously.  Invokes callback when information has
10888fb49dd6SShawn McCarney  * been obtained.
10898fb49dd6SShawn McCarney  *
10908fb49dd6SShawn McCarney  * The callback must have the following signature:
10918fb49dd6SShawn McCarney  *   @code
1092fe04d49cSNan Zhou  *   callback(std::shared_ptr<std::set<std::string>> invConnections)
10938fb49dd6SShawn McCarney  *   @endcode
10948fb49dd6SShawn McCarney  *
10958fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
1096adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
10978fb49dd6SShawn McCarney  * @param callback Callback to invoke when connections have been obtained.
10988fb49dd6SShawn McCarney  */
10998fb49dd6SShawn McCarney template <typename Callback>
11004ff0f1f4SEd Tanous void getInventoryItemsConnections(
1101b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1102b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
11038fb49dd6SShawn McCarney     Callback&& callback)
11048fb49dd6SShawn McCarney {
110562598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryItemsConnections enter");
11068fb49dd6SShawn McCarney 
11078fb49dd6SShawn McCarney     const std::string path = "/xyz/openbmc_project/inventory";
1108e99073f5SGeorge Liu     constexpr std::array<std::string_view, 4> interfaces = {
11098fb49dd6SShawn McCarney         "xyz.openbmc_project.Inventory.Item",
1110adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Item.PowerSupply",
1111adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Decorator.Asset",
11128fb49dd6SShawn McCarney         "xyz.openbmc_project.State.Decorator.OperationalStatus"};
11138fb49dd6SShawn McCarney 
1114e99073f5SGeorge Liu     // Make call to ObjectMapper to find all inventory items
1115e99073f5SGeorge Liu     dbus::utility::getSubTree(
1116e99073f5SGeorge Liu         path, 0, interfaces,
11178cb2c024SEd Tanous         [callback = std::forward<Callback>(callback), sensorsAsyncResp,
1118002d39b4SEd Tanous          inventoryItems](
1119e99073f5SGeorge Liu             const boost::system::error_code& ec,
11204e0d8789SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) mutable {
1121e99073f5SGeorge Liu             // Response handler for parsing output from GetSubTree
112262598e31SEd Tanous             BMCWEB_LOG_DEBUG("getInventoryItemsConnections respHandler enter");
11238fb49dd6SShawn McCarney             if (ec)
11248fb49dd6SShawn McCarney             {
11258d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
112662598e31SEd Tanous                 BMCWEB_LOG_ERROR(
1127bd79bce8SPatrick Williams                     "getInventoryItemsConnections respHandler DBus error {}",
1128bd79bce8SPatrick Williams                     ec);
11298fb49dd6SShawn McCarney                 return;
11308fb49dd6SShawn McCarney             }
11318fb49dd6SShawn McCarney 
11328fb49dd6SShawn McCarney             // Make unique list of connections for desired inventory items
1133fe04d49cSNan Zhou             std::shared_ptr<std::set<std::string>> invConnections =
1134fe04d49cSNan Zhou                 std::make_shared<std::set<std::string>>();
11358fb49dd6SShawn McCarney 
11368fb49dd6SShawn McCarney             // Loop through objects from GetSubTree
1137bd79bce8SPatrick Williams             for (const std::pair<std::string,
1138bd79bce8SPatrick Williams                                  std::vector<std::pair<
1139bd79bce8SPatrick Williams                                      std::string, std::vector<std::string>>>>&
11408fb49dd6SShawn McCarney                      object : subtree)
11418fb49dd6SShawn McCarney             {
1142adc4f0dbSShawn McCarney                 // Check if object path is one of the specified inventory items
11438fb49dd6SShawn McCarney                 const std::string& objPath = object.first;
1144adc4f0dbSShawn McCarney                 if (findInventoryItem(inventoryItems, objPath) != nullptr)
11458fb49dd6SShawn McCarney                 {
11468fb49dd6SShawn McCarney                     // Store all connections to inventory item
11478fb49dd6SShawn McCarney                     for (const std::pair<std::string, std::vector<std::string>>&
11488fb49dd6SShawn McCarney                              objData : object.second)
11498fb49dd6SShawn McCarney                     {
11508fb49dd6SShawn McCarney                         const std::string& invConnection = objData.first;
11518fb49dd6SShawn McCarney                         invConnections->insert(invConnection);
11528fb49dd6SShawn McCarney                     }
11538fb49dd6SShawn McCarney                 }
11548fb49dd6SShawn McCarney             }
1155d500549bSAnthony Wilson 
11568fb49dd6SShawn McCarney             callback(invConnections);
115762598e31SEd Tanous             BMCWEB_LOG_DEBUG("getInventoryItemsConnections respHandler exit");
1158e99073f5SGeorge Liu         });
115962598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryItemsConnections exit");
11608fb49dd6SShawn McCarney }
11618fb49dd6SShawn McCarney 
11628fb49dd6SShawn McCarney /**
1163adc4f0dbSShawn McCarney  * @brief Gets associations from sensors to inventory items.
11648fb49dd6SShawn McCarney  *
11658fb49dd6SShawn McCarney  * Looks for ObjectMapper associations from the specified sensors to related
1166d500549bSAnthony Wilson  * inventory items. Then finds the associations from those inventory items to
1167d500549bSAnthony Wilson  * their LEDs, if any.
11688fb49dd6SShawn McCarney  *
11698fb49dd6SShawn McCarney  * Finds the inventory items asynchronously.  Invokes callback when information
11708fb49dd6SShawn McCarney  * has been obtained.
11718fb49dd6SShawn McCarney  *
11728fb49dd6SShawn McCarney  * The callback must have the following signature:
11738fb49dd6SShawn McCarney  *   @code
1174adc4f0dbSShawn McCarney  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
11758fb49dd6SShawn McCarney  *   @endcode
11768fb49dd6SShawn McCarney  *
11778fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
11788fb49dd6SShawn McCarney  * @param sensorNames All sensors within the current chassis.
11798fb49dd6SShawn McCarney  * implements ObjectManager.
11808fb49dd6SShawn McCarney  * @param callback Callback to invoke when inventory items have been obtained.
11818fb49dd6SShawn McCarney  */
11828fb49dd6SShawn McCarney template <typename Callback>
11834ff0f1f4SEd Tanous void getInventoryItemAssociations(
1184b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1185fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& sensorNames,
11868fb49dd6SShawn McCarney     Callback&& callback)
11878fb49dd6SShawn McCarney {
118862598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryItemAssociations enter");
11898fb49dd6SShawn McCarney 
11905eb468daSGeorge Liu     // Call GetManagedObjects on the ObjectMapper to get all associations
11915eb468daSGeorge Liu     sdbusplus::message::object_path path("/");
11925eb468daSGeorge Liu     dbus::utility::getManagedObjects(
11935eb468daSGeorge Liu         "xyz.openbmc_project.ObjectMapper", path,
11948cb2c024SEd Tanous         [callback = std::forward<Callback>(callback), sensorsAsyncResp,
11955e7e2dc5SEd Tanous          sensorNames](const boost::system::error_code& ec,
11964e0d8789SEd Tanous                       const dbus::utility::ManagedObjectType& resp) mutable {
119762598e31SEd Tanous             BMCWEB_LOG_DEBUG("getInventoryItemAssociations respHandler enter");
11988fb49dd6SShawn McCarney             if (ec)
11998fb49dd6SShawn McCarney             {
120062598e31SEd Tanous                 BMCWEB_LOG_ERROR(
1201bd79bce8SPatrick Williams                     "getInventoryItemAssociations respHandler DBus error {}",
1202bd79bce8SPatrick Williams                     ec);
12038d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
12048fb49dd6SShawn McCarney                 return;
12058fb49dd6SShawn McCarney             }
12068fb49dd6SShawn McCarney 
1207adc4f0dbSShawn McCarney             // Create vector to hold list of inventory items
1208adc4f0dbSShawn McCarney             std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1209adc4f0dbSShawn McCarney                 std::make_shared<std::vector<InventoryItem>>();
1210adc4f0dbSShawn McCarney 
12118fb49dd6SShawn McCarney             // Loop through returned object paths
12128fb49dd6SShawn McCarney             std::string sensorAssocPath;
12138fb49dd6SShawn McCarney             sensorAssocPath.reserve(128); // avoid memory allocations
12148fb49dd6SShawn McCarney             for (const auto& objDictEntry : resp)
12158fb49dd6SShawn McCarney             {
12168fb49dd6SShawn McCarney                 const std::string& objPath =
12178fb49dd6SShawn McCarney                     static_cast<const std::string&>(objDictEntry.first);
12188fb49dd6SShawn McCarney 
1219bd79bce8SPatrick Williams                 // If path is inventory association for one of the specified
1220bd79bce8SPatrick Williams                 // sensors
12218fb49dd6SShawn McCarney                 for (const std::string& sensorName : *sensorNames)
12228fb49dd6SShawn McCarney                 {
12238fb49dd6SShawn McCarney                     sensorAssocPath = sensorName;
12248fb49dd6SShawn McCarney                     sensorAssocPath += "/inventory";
12258fb49dd6SShawn McCarney                     if (objPath == sensorAssocPath)
12268fb49dd6SShawn McCarney                     {
12278fb49dd6SShawn McCarney                         // Get Association interface for object path
1228bd79bce8SPatrick Williams                         for (const auto& [interface, values] :
1229bd79bce8SPatrick Williams                              objDictEntry.second)
12308fb49dd6SShawn McCarney                         {
1231711ac7a9SEd Tanous                             if (interface == "xyz.openbmc_project.Association")
1232711ac7a9SEd Tanous                             {
1233711ac7a9SEd Tanous                                 for (const auto& [valueName, value] : values)
1234711ac7a9SEd Tanous                                 {
1235711ac7a9SEd Tanous                                     if (valueName == "endpoints")
12368fb49dd6SShawn McCarney                                     {
1237bd79bce8SPatrick Williams                                         const std::vector<std::string>*
1238bd79bce8SPatrick Williams                                             endpoints = std::get_if<
1239bd79bce8SPatrick Williams                                                 std::vector<std::string>>(
1240711ac7a9SEd Tanous                                                 &value);
1241711ac7a9SEd Tanous                                         if ((endpoints != nullptr) &&
1242711ac7a9SEd Tanous                                             !endpoints->empty())
12438fb49dd6SShawn McCarney                                         {
1244adc4f0dbSShawn McCarney                                             // Add inventory item to vector
1245adc4f0dbSShawn McCarney                                             const std::string& invItemPath =
1246adc4f0dbSShawn McCarney                                                 endpoints->front();
1247711ac7a9SEd Tanous                                             addInventoryItem(inventoryItems,
1248711ac7a9SEd Tanous                                                              invItemPath,
1249adc4f0dbSShawn McCarney                                                              sensorName);
12508fb49dd6SShawn McCarney                                         }
12518fb49dd6SShawn McCarney                                     }
12528fb49dd6SShawn McCarney                                 }
1253711ac7a9SEd Tanous                             }
1254711ac7a9SEd Tanous                         }
12558fb49dd6SShawn McCarney                         break;
12568fb49dd6SShawn McCarney                     }
12578fb49dd6SShawn McCarney                 }
12588fb49dd6SShawn McCarney             }
12598fb49dd6SShawn McCarney 
1260d500549bSAnthony Wilson             // Now loop through the returned object paths again, this time to
1261d500549bSAnthony Wilson             // find the leds associated with the inventory items we just found
1262d500549bSAnthony Wilson             std::string inventoryAssocPath;
1263d500549bSAnthony Wilson             inventoryAssocPath.reserve(128); // avoid memory allocations
1264d500549bSAnthony Wilson             for (const auto& objDictEntry : resp)
1265d500549bSAnthony Wilson             {
1266d500549bSAnthony Wilson                 const std::string& objPath =
1267d500549bSAnthony Wilson                     static_cast<const std::string&>(objDictEntry.first);
1268d500549bSAnthony Wilson 
1269d500549bSAnthony Wilson                 for (InventoryItem& inventoryItem : *inventoryItems)
1270d500549bSAnthony Wilson                 {
1271d500549bSAnthony Wilson                     inventoryAssocPath = inventoryItem.objectPath;
1272d500549bSAnthony Wilson                     inventoryAssocPath += "/leds";
1273d500549bSAnthony Wilson                     if (objPath == inventoryAssocPath)
1274d500549bSAnthony Wilson                     {
1275bd79bce8SPatrick Williams                         for (const auto& [interface, values] :
1276bd79bce8SPatrick Williams                              objDictEntry.second)
1277d500549bSAnthony Wilson                         {
1278711ac7a9SEd Tanous                             if (interface == "xyz.openbmc_project.Association")
1279711ac7a9SEd Tanous                             {
1280711ac7a9SEd Tanous                                 for (const auto& [valueName, value] : values)
1281711ac7a9SEd Tanous                                 {
1282711ac7a9SEd Tanous                                     if (valueName == "endpoints")
1283d500549bSAnthony Wilson                                     {
1284bd79bce8SPatrick Williams                                         const std::vector<std::string>*
1285bd79bce8SPatrick Williams                                             endpoints = std::get_if<
1286bd79bce8SPatrick Williams                                                 std::vector<std::string>>(
1287711ac7a9SEd Tanous                                                 &value);
1288711ac7a9SEd Tanous                                         if ((endpoints != nullptr) &&
1289711ac7a9SEd Tanous                                             !endpoints->empty())
1290d500549bSAnthony Wilson                                         {
1291711ac7a9SEd Tanous                                             // Add inventory item to vector
1292d500549bSAnthony Wilson                                             // Store LED path in inventory item
1293711ac7a9SEd Tanous                                             const std::string& ledPath =
1294711ac7a9SEd Tanous                                                 endpoints->front();
1295bd79bce8SPatrick Williams                                             inventoryItem.ledObjectPath =
1296bd79bce8SPatrick Williams                                                 ledPath;
1297d500549bSAnthony Wilson                                         }
1298d500549bSAnthony Wilson                                     }
1299d500549bSAnthony Wilson                                 }
1300711ac7a9SEd Tanous                             }
1301711ac7a9SEd Tanous                         }
1302711ac7a9SEd Tanous 
1303d500549bSAnthony Wilson                         break;
1304d500549bSAnthony Wilson                     }
1305d500549bSAnthony Wilson                 }
1306d500549bSAnthony Wilson             }
1307adc4f0dbSShawn McCarney             callback(inventoryItems);
130862598e31SEd Tanous             BMCWEB_LOG_DEBUG("getInventoryItemAssociations respHandler exit");
13095eb468daSGeorge Liu         });
13108fb49dd6SShawn McCarney 
131162598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryItemAssociations exit");
13128fb49dd6SShawn McCarney }
13138fb49dd6SShawn McCarney 
13148fb49dd6SShawn McCarney /**
1315d500549bSAnthony Wilson  * @brief Gets D-Bus data for inventory item leds associated with sensors.
1316d500549bSAnthony Wilson  *
1317d500549bSAnthony Wilson  * Uses the specified connections (services) to obtain D-Bus data for inventory
1318d500549bSAnthony Wilson  * item leds associated with sensors.  Stores the resulting data in the
1319d500549bSAnthony Wilson  * inventoryItems vector.
1320d500549bSAnthony Wilson  *
1321d500549bSAnthony Wilson  * This data is later used to provide sensor property values in the JSON
1322d500549bSAnthony Wilson  * response.
1323d500549bSAnthony Wilson  *
1324d500549bSAnthony Wilson  * Finds the inventory item led data asynchronously.  Invokes callback when data
1325d500549bSAnthony Wilson  * has been obtained.
1326d500549bSAnthony Wilson  *
1327d500549bSAnthony Wilson  * The callback must have the following signature:
1328d500549bSAnthony Wilson  *   @code
132942cbe538SGunnar Mills  *   callback()
1330d500549bSAnthony Wilson  *   @endcode
1331d500549bSAnthony Wilson  *
1332d500549bSAnthony Wilson  * This function is called recursively, obtaining data asynchronously from one
1333d500549bSAnthony Wilson  * connection in each call.  This ensures the callback is not invoked until the
1334d500549bSAnthony Wilson  * last asynchronous function has completed.
1335d500549bSAnthony Wilson  *
1336d500549bSAnthony Wilson  * @param sensorsAsyncResp Pointer to object holding response data.
1337d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1338d500549bSAnthony Wilson  * @param ledConnections Connections that provide data for the inventory leds.
1339d500549bSAnthony Wilson  * @param callback Callback to invoke when inventory data has been obtained.
1340d500549bSAnthony Wilson  * @param ledConnectionsIndex Current index in ledConnections.  Only specified
1341d500549bSAnthony Wilson  * in recursive calls to this function.
1342d500549bSAnthony Wilson  */
1343d500549bSAnthony Wilson template <typename Callback>
1344d500549bSAnthony Wilson void getInventoryLedData(
1345daadfb2eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1346daadfb2eSEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1347daadfb2eSEd Tanous     const std::shared_ptr<std::map<std::string, std::string>>& ledConnections,
1348d500549bSAnthony Wilson     Callback&& callback, size_t ledConnectionsIndex = 0)
1349d500549bSAnthony Wilson {
135062598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryLedData enter");
1351d500549bSAnthony Wilson 
1352d500549bSAnthony Wilson     // If no more connections left, call callback
1353d500549bSAnthony Wilson     if (ledConnectionsIndex >= ledConnections->size())
1354d500549bSAnthony Wilson     {
135542cbe538SGunnar Mills         callback();
135662598e31SEd Tanous         BMCWEB_LOG_DEBUG("getInventoryLedData exit");
1357d500549bSAnthony Wilson         return;
1358d500549bSAnthony Wilson     }
1359d500549bSAnthony Wilson 
1360d500549bSAnthony Wilson     // Get inventory item data from current connection
1361fe04d49cSNan Zhou     auto it = ledConnections->begin();
1362fe04d49cSNan Zhou     std::advance(it, ledConnectionsIndex);
1363d500549bSAnthony Wilson     if (it != ledConnections->end())
1364d500549bSAnthony Wilson     {
1365d500549bSAnthony Wilson         const std::string& ledPath = (*it).first;
1366d500549bSAnthony Wilson         const std::string& ledConnection = (*it).second;
1367d500549bSAnthony Wilson         // Response handler for Get State property
13681e1e598dSJonathan Doman         auto respHandler =
13691e1e598dSJonathan Doman             [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
13704e0d8789SEd Tanous              callback = std::forward<Callback>(callback),
13714e0d8789SEd Tanous              ledConnectionsIndex](const boost::system::error_code& ec,
13724e0d8789SEd Tanous                                   const std::string& state) mutable {
137362598e31SEd Tanous                 BMCWEB_LOG_DEBUG("getInventoryLedData respHandler enter");
1374d500549bSAnthony Wilson                 if (ec)
1375d500549bSAnthony Wilson                 {
137662598e31SEd Tanous                     BMCWEB_LOG_ERROR(
137762598e31SEd Tanous                         "getInventoryLedData respHandler DBus error {}", ec);
13788d1b46d7Szhanghch05                     messages::internalError(sensorsAsyncResp->asyncResp->res);
1379d500549bSAnthony Wilson                     return;
1380d500549bSAnthony Wilson                 }
1381d500549bSAnthony Wilson 
138262598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Led state: {}", state);
1383d500549bSAnthony Wilson                 // Find inventory item with this LED object path
1384d500549bSAnthony Wilson                 InventoryItem* inventoryItem =
1385d500549bSAnthony Wilson                     findInventoryItemForLed(*inventoryItems, ledPath);
1386d500549bSAnthony Wilson                 if (inventoryItem != nullptr)
1387d500549bSAnthony Wilson                 {
1388d500549bSAnthony Wilson                     // Store LED state in InventoryItem
138911ba3979SEd Tanous                     if (state.ends_with("On"))
1390d500549bSAnthony Wilson                     {
1391c9563608SJanet Adkins                         inventoryItem->ledState = sensor_utils::LedState::ON;
1392d500549bSAnthony Wilson                     }
139311ba3979SEd Tanous                     else if (state.ends_with("Blink"))
1394d500549bSAnthony Wilson                     {
1395c9563608SJanet Adkins                         inventoryItem->ledState = sensor_utils::LedState::BLINK;
1396d500549bSAnthony Wilson                     }
139711ba3979SEd Tanous                     else if (state.ends_with("Off"))
1398d500549bSAnthony Wilson                     {
1399c9563608SJanet Adkins                         inventoryItem->ledState = sensor_utils::LedState::OFF;
1400d500549bSAnthony Wilson                     }
1401d500549bSAnthony Wilson                     else
1402d500549bSAnthony Wilson                     {
1403c9563608SJanet Adkins                         inventoryItem->ledState =
1404c9563608SJanet Adkins                             sensor_utils::LedState::UNKNOWN;
1405d500549bSAnthony Wilson                     }
1406d500549bSAnthony Wilson                 }
1407d500549bSAnthony Wilson 
1408d500549bSAnthony Wilson                 // Recurse to get LED data from next connection
1409d500549bSAnthony Wilson                 getInventoryLedData(sensorsAsyncResp, inventoryItems,
1410d500549bSAnthony Wilson                                     ledConnections, std::move(callback),
1411d500549bSAnthony Wilson                                     ledConnectionsIndex + 1);
1412d500549bSAnthony Wilson 
141362598e31SEd Tanous                 BMCWEB_LOG_DEBUG("getInventoryLedData respHandler exit");
1414d500549bSAnthony Wilson             };
1415d500549bSAnthony Wilson 
1416d500549bSAnthony Wilson         // Get the State property for the current LED
1417deae6a78SEd Tanous         dbus::utility::getProperty<std::string>(
1418deae6a78SEd Tanous             ledConnection, ledPath, "xyz.openbmc_project.Led.Physical", "State",
14191e1e598dSJonathan Doman             std::move(respHandler));
1420d500549bSAnthony Wilson     }
1421d500549bSAnthony Wilson 
142262598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryLedData exit");
1423d500549bSAnthony Wilson }
1424d500549bSAnthony Wilson 
1425d500549bSAnthony Wilson /**
1426d500549bSAnthony Wilson  * @brief Gets LED data for LEDs associated with given inventory items.
1427d500549bSAnthony Wilson  *
1428d500549bSAnthony Wilson  * Gets the D-Bus connections (services) that provide LED data for the LEDs
1429d500549bSAnthony Wilson  * associated with the specified inventory items.  Then gets the LED data from
1430d500549bSAnthony Wilson  * each connection and stores it in the inventory item.
1431d500549bSAnthony Wilson  *
1432d500549bSAnthony Wilson  * This data is later used to provide sensor property values in the JSON
1433d500549bSAnthony Wilson  * response.
1434d500549bSAnthony Wilson  *
1435d500549bSAnthony Wilson  * Finds the LED data asynchronously.  Invokes callback when information has
1436d500549bSAnthony Wilson  * been obtained.
1437d500549bSAnthony Wilson  *
1438d500549bSAnthony Wilson  * The callback must have the following signature:
1439d500549bSAnthony Wilson  *   @code
144042cbe538SGunnar Mills  *   callback()
1441d500549bSAnthony Wilson  *   @endcode
1442d500549bSAnthony Wilson  *
1443d500549bSAnthony Wilson  * @param sensorsAsyncResp Pointer to object holding response data.
1444d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1445d500549bSAnthony Wilson  * @param callback Callback to invoke when inventory items have been obtained.
1446d500549bSAnthony Wilson  */
1447d500549bSAnthony Wilson template <typename Callback>
1448d500549bSAnthony Wilson void getInventoryLeds(
1449daadfb2eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1450daadfb2eSEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1451d500549bSAnthony Wilson     Callback&& callback)
1452d500549bSAnthony Wilson {
145362598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryLeds enter");
1454d500549bSAnthony Wilson 
1455d500549bSAnthony Wilson     const std::string path = "/xyz/openbmc_project";
1456e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
1457d500549bSAnthony Wilson         "xyz.openbmc_project.Led.Physical"};
1458d500549bSAnthony Wilson 
1459e99073f5SGeorge Liu     // Make call to ObjectMapper to find all inventory items
1460e99073f5SGeorge Liu     dbus::utility::getSubTree(
1461e99073f5SGeorge Liu         path, 0, interfaces,
14628cb2c024SEd Tanous         [callback = std::forward<Callback>(callback), sensorsAsyncResp,
1463002d39b4SEd Tanous          inventoryItems](
1464e99073f5SGeorge Liu             const boost::system::error_code& ec,
14654e0d8789SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) mutable {
1466e99073f5SGeorge Liu             // Response handler for parsing output from GetSubTree
146762598e31SEd Tanous             BMCWEB_LOG_DEBUG("getInventoryLeds respHandler enter");
1468d500549bSAnthony Wilson             if (ec)
1469d500549bSAnthony Wilson             {
14708d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
1471bd79bce8SPatrick Williams                 BMCWEB_LOG_ERROR("getInventoryLeds respHandler DBus error {}",
1472bd79bce8SPatrick Williams                                  ec);
1473d500549bSAnthony Wilson                 return;
1474d500549bSAnthony Wilson             }
1475d500549bSAnthony Wilson 
1476d500549bSAnthony Wilson             // Build map of LED object paths to connections
1477fe04d49cSNan Zhou             std::shared_ptr<std::map<std::string, std::string>> ledConnections =
1478fe04d49cSNan Zhou                 std::make_shared<std::map<std::string, std::string>>();
1479d500549bSAnthony Wilson 
1480d500549bSAnthony Wilson             // Loop through objects from GetSubTree
1481bd79bce8SPatrick Williams             for (const std::pair<std::string,
1482bd79bce8SPatrick Williams                                  std::vector<std::pair<
1483bd79bce8SPatrick Williams                                      std::string, std::vector<std::string>>>>&
1484d500549bSAnthony Wilson                      object : subtree)
1485d500549bSAnthony Wilson             {
1486bd79bce8SPatrick Williams                 // Check if object path is LED for one of the specified
1487bd79bce8SPatrick Williams                 // inventory items
1488d500549bSAnthony Wilson                 const std::string& ledPath = object.first;
1489bd79bce8SPatrick Williams                 if (findInventoryItemForLed(*inventoryItems, ledPath) !=
1490bd79bce8SPatrick Williams                     nullptr)
1491d500549bSAnthony Wilson                 {
1492d500549bSAnthony Wilson                     // Add mapping from ledPath to connection
1493bd79bce8SPatrick Williams                     const std::string& connection =
1494bd79bce8SPatrick Williams                         object.second.begin()->first;
1495d500549bSAnthony Wilson                     (*ledConnections)[ledPath] = connection;
1496bd79bce8SPatrick Williams                     BMCWEB_LOG_DEBUG("Added mapping {} -> {}", ledPath,
1497bd79bce8SPatrick Williams                                      connection);
1498d500549bSAnthony Wilson                 }
1499d500549bSAnthony Wilson             }
1500d500549bSAnthony Wilson 
1501bd79bce8SPatrick Williams             getInventoryLedData(sensorsAsyncResp, inventoryItems,
1502bd79bce8SPatrick Williams                                 ledConnections, std::move(callback));
150362598e31SEd Tanous             BMCWEB_LOG_DEBUG("getInventoryLeds respHandler exit");
1504e99073f5SGeorge Liu         });
150562598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryLeds exit");
1506d500549bSAnthony Wilson }
1507d500549bSAnthony Wilson 
1508d500549bSAnthony Wilson /**
150942cbe538SGunnar Mills  * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
151042cbe538SGunnar Mills  *
151142cbe538SGunnar Mills  * Uses the specified connections (services) (currently assumes just one) to
151242cbe538SGunnar Mills  * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
151342cbe538SGunnar Mills  * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
151442cbe538SGunnar Mills  *
151542cbe538SGunnar Mills  * This data is later used to provide sensor property values in the JSON
151642cbe538SGunnar Mills  * response.
151742cbe538SGunnar Mills  *
151842cbe538SGunnar Mills  * Finds the Power Supply Attributes data asynchronously.  Invokes callback
151942cbe538SGunnar Mills  * when data has been obtained.
152042cbe538SGunnar Mills  *
152142cbe538SGunnar Mills  * The callback must have the following signature:
152242cbe538SGunnar Mills  *   @code
152342cbe538SGunnar Mills  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
152442cbe538SGunnar Mills  *   @endcode
152542cbe538SGunnar Mills  *
152642cbe538SGunnar Mills  * @param sensorsAsyncResp Pointer to object holding response data.
152742cbe538SGunnar Mills  * @param inventoryItems D-Bus inventory items associated with sensors.
152842cbe538SGunnar Mills  * @param psAttributesConnections Connections that provide data for the Power
152942cbe538SGunnar Mills  *        Supply Attributes
153042cbe538SGunnar Mills  * @param callback Callback to invoke when data has been obtained.
153142cbe538SGunnar Mills  */
153242cbe538SGunnar Mills template <typename Callback>
153342cbe538SGunnar Mills void getPowerSupplyAttributesData(
1534b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1535daadfb2eSEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1536fe04d49cSNan Zhou     const std::map<std::string, std::string>& psAttributesConnections,
153742cbe538SGunnar Mills     Callback&& callback)
153842cbe538SGunnar Mills {
153962598e31SEd Tanous     BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData enter");
154042cbe538SGunnar Mills 
154142cbe538SGunnar Mills     if (psAttributesConnections.empty())
154242cbe538SGunnar Mills     {
154362598e31SEd Tanous         BMCWEB_LOG_DEBUG("Can't find PowerSupplyAttributes, no connections!");
154442cbe538SGunnar Mills         callback(inventoryItems);
154542cbe538SGunnar Mills         return;
154642cbe538SGunnar Mills     }
154742cbe538SGunnar Mills 
154842cbe538SGunnar Mills     // Assuming just one connection (service) for now
1549fe04d49cSNan Zhou     auto it = psAttributesConnections.begin();
155042cbe538SGunnar Mills 
155142cbe538SGunnar Mills     const std::string& psAttributesPath = (*it).first;
155242cbe538SGunnar Mills     const std::string& psAttributesConnection = (*it).second;
155342cbe538SGunnar Mills 
155442cbe538SGunnar Mills     // Response handler for Get DeratingFactor property
15555a39f77aSPatrick Williams     auto respHandler = [sensorsAsyncResp, inventoryItems,
15568cb2c024SEd Tanous                         callback = std::forward<Callback>(callback)](
15575a39f77aSPatrick Williams                            const boost::system::error_code& ec,
15584e0d8789SEd Tanous                            uint32_t value) mutable {
155962598e31SEd Tanous         BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData respHandler enter");
156042cbe538SGunnar Mills         if (ec)
156142cbe538SGunnar Mills         {
156262598e31SEd Tanous             BMCWEB_LOG_ERROR(
156362598e31SEd Tanous                 "getPowerSupplyAttributesData respHandler DBus error {}", ec);
15648d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
156542cbe538SGunnar Mills             return;
156642cbe538SGunnar Mills         }
156742cbe538SGunnar Mills 
156862598e31SEd Tanous         BMCWEB_LOG_DEBUG("PS EfficiencyPercent value: {}", value);
156942cbe538SGunnar Mills         // Store value in Power Supply Inventory Items
157042cbe538SGunnar Mills         for (InventoryItem& inventoryItem : *inventoryItems)
157142cbe538SGunnar Mills         {
157255f79e6fSEd Tanous             if (inventoryItem.isPowerSupply)
157342cbe538SGunnar Mills             {
157442cbe538SGunnar Mills                 inventoryItem.powerSupplyEfficiencyPercent =
15751e1e598dSJonathan Doman                     static_cast<int>(value);
157642cbe538SGunnar Mills             }
157742cbe538SGunnar Mills         }
157842cbe538SGunnar Mills 
157962598e31SEd Tanous         BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData respHandler exit");
158042cbe538SGunnar Mills         callback(inventoryItems);
158142cbe538SGunnar Mills     };
158242cbe538SGunnar Mills 
158342cbe538SGunnar Mills     // Get the DeratingFactor property for the PowerSupplyAttributes
158442cbe538SGunnar Mills     // Currently only property on the interface/only one we care about
1585deae6a78SEd Tanous     dbus::utility::getProperty<uint32_t>(
1586deae6a78SEd Tanous         psAttributesConnection, psAttributesPath,
15871e1e598dSJonathan Doman         "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor",
15881e1e598dSJonathan Doman         std::move(respHandler));
158942cbe538SGunnar Mills 
159062598e31SEd Tanous     BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData exit");
159142cbe538SGunnar Mills }
159242cbe538SGunnar Mills 
159342cbe538SGunnar Mills /**
159442cbe538SGunnar Mills  * @brief Gets the Power Supply Attributes such as EfficiencyPercent
159542cbe538SGunnar Mills  *
159642cbe538SGunnar Mills  * Gets the D-Bus connection (service) that provides Power Supply Attributes
159742cbe538SGunnar Mills  * data. Then gets the Power Supply Attributes data from the connection
159842cbe538SGunnar Mills  * (currently just assumes 1 connection) and stores the data in the inventory
159942cbe538SGunnar Mills  * item.
160042cbe538SGunnar Mills  *
160142cbe538SGunnar Mills  * This data is later used to provide sensor property values in the JSON
160242cbe538SGunnar Mills  * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
160342cbe538SGunnar Mills  *
160442cbe538SGunnar Mills  * Finds the Power Supply Attributes data asynchronously. Invokes callback
160542cbe538SGunnar Mills  * when information has been obtained.
160642cbe538SGunnar Mills  *
160742cbe538SGunnar Mills  * The callback must have the following signature:
160842cbe538SGunnar Mills  *   @code
160942cbe538SGunnar Mills  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
161042cbe538SGunnar Mills  *   @endcode
161142cbe538SGunnar Mills  *
161242cbe538SGunnar Mills  * @param sensorsAsyncResp Pointer to object holding response data.
161342cbe538SGunnar Mills  * @param inventoryItems D-Bus inventory items associated with sensors.
161442cbe538SGunnar Mills  * @param callback Callback to invoke when data has been obtained.
161542cbe538SGunnar Mills  */
161642cbe538SGunnar Mills template <typename Callback>
161742cbe538SGunnar Mills void getPowerSupplyAttributes(
1618daadfb2eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1619daadfb2eSEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
162042cbe538SGunnar Mills     Callback&& callback)
162142cbe538SGunnar Mills {
162262598e31SEd Tanous     BMCWEB_LOG_DEBUG("getPowerSupplyAttributes enter");
162342cbe538SGunnar Mills 
162442cbe538SGunnar Mills     // Only need the power supply attributes when the Power Schema
16250c728b42SJanet Adkins     if (sensorsAsyncResp->chassisSubNode != sensors::powerNodeStr)
162642cbe538SGunnar Mills     {
162762598e31SEd Tanous         BMCWEB_LOG_DEBUG("getPowerSupplyAttributes exit since not Power");
162842cbe538SGunnar Mills         callback(inventoryItems);
162942cbe538SGunnar Mills         return;
163042cbe538SGunnar Mills     }
163142cbe538SGunnar Mills 
1632e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
163342cbe538SGunnar Mills         "xyz.openbmc_project.Control.PowerSupplyAttributes"};
163442cbe538SGunnar Mills 
1635e99073f5SGeorge Liu     // Make call to ObjectMapper to find the PowerSupplyAttributes service
1636e99073f5SGeorge Liu     dbus::utility::getSubTree(
1637e99073f5SGeorge Liu         "/xyz/openbmc_project", 0, interfaces,
16388cb2c024SEd Tanous         [callback = std::forward<Callback>(callback), sensorsAsyncResp,
1639b9d36b47SEd Tanous          inventoryItems](
1640e99073f5SGeorge Liu             const boost::system::error_code& ec,
16414e0d8789SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) mutable {
1642e99073f5SGeorge Liu             // Response handler for parsing output from GetSubTree
164362598e31SEd Tanous             BMCWEB_LOG_DEBUG("getPowerSupplyAttributes respHandler enter");
164442cbe538SGunnar Mills             if (ec)
164542cbe538SGunnar Mills             {
16468d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
164762598e31SEd Tanous                 BMCWEB_LOG_ERROR(
164862598e31SEd Tanous                     "getPowerSupplyAttributes respHandler DBus error {}", ec);
164942cbe538SGunnar Mills                 return;
165042cbe538SGunnar Mills             }
165126f6976fSEd Tanous             if (subtree.empty())
165242cbe538SGunnar Mills             {
165362598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Can't find Power Supply Attributes!");
165442cbe538SGunnar Mills                 callback(inventoryItems);
165542cbe538SGunnar Mills                 return;
165642cbe538SGunnar Mills             }
165742cbe538SGunnar Mills 
165842cbe538SGunnar Mills             // Currently we only support 1 power supply attribute, use this for
165942cbe538SGunnar Mills             // all the power supplies. Build map of object path to connection.
166042cbe538SGunnar Mills             // Assume just 1 connection and 1 path for now.
1661fe04d49cSNan Zhou             std::map<std::string, std::string> psAttributesConnections;
166242cbe538SGunnar Mills 
166342cbe538SGunnar Mills             if (subtree[0].first.empty() || subtree[0].second.empty())
166442cbe538SGunnar Mills             {
166562598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Power Supply Attributes mapper error!");
166642cbe538SGunnar Mills                 callback(inventoryItems);
166742cbe538SGunnar Mills                 return;
166842cbe538SGunnar Mills             }
166942cbe538SGunnar Mills 
167042cbe538SGunnar Mills             const std::string& psAttributesPath = subtree[0].first;
167142cbe538SGunnar Mills             const std::string& connection = subtree[0].second.begin()->first;
167242cbe538SGunnar Mills 
167342cbe538SGunnar Mills             if (connection.empty())
167442cbe538SGunnar Mills             {
167562598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Power Supply Attributes mapper error!");
167642cbe538SGunnar Mills                 callback(inventoryItems);
167742cbe538SGunnar Mills                 return;
167842cbe538SGunnar Mills             }
167942cbe538SGunnar Mills 
168042cbe538SGunnar Mills             psAttributesConnections[psAttributesPath] = connection;
168162598e31SEd Tanous             BMCWEB_LOG_DEBUG("Added mapping {} -> {}", psAttributesPath,
168262598e31SEd Tanous                              connection);
168342cbe538SGunnar Mills 
168442cbe538SGunnar Mills             getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
168542cbe538SGunnar Mills                                          psAttributesConnections,
168642cbe538SGunnar Mills                                          std::move(callback));
168762598e31SEd Tanous             BMCWEB_LOG_DEBUG("getPowerSupplyAttributes respHandler exit");
1688e99073f5SGeorge Liu         });
168962598e31SEd Tanous     BMCWEB_LOG_DEBUG("getPowerSupplyAttributes exit");
169042cbe538SGunnar Mills }
169142cbe538SGunnar Mills 
169242cbe538SGunnar Mills /**
1693adc4f0dbSShawn McCarney  * @brief Gets inventory items associated with sensors.
16948fb49dd6SShawn McCarney  *
16958fb49dd6SShawn McCarney  * Finds the inventory items that are associated with the specified sensors.
1696adc4f0dbSShawn McCarney  * Then gets D-Bus data for the inventory items, such as presence and VPD.
16978fb49dd6SShawn McCarney  *
1698adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
1699adc4f0dbSShawn McCarney  * response.
17008fb49dd6SShawn McCarney  *
1701adc4f0dbSShawn McCarney  * Finds the inventory items asynchronously.  Invokes callback when the
1702adc4f0dbSShawn McCarney  * inventory items have been obtained.
1703adc4f0dbSShawn McCarney  *
1704adc4f0dbSShawn McCarney  * The callback must have the following signature:
1705adc4f0dbSShawn McCarney  *   @code
1706adc4f0dbSShawn McCarney  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
1707adc4f0dbSShawn McCarney  *   @endcode
17088fb49dd6SShawn McCarney  *
17098fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
17108fb49dd6SShawn McCarney  * @param sensorNames All sensors within the current chassis.
17118fb49dd6SShawn McCarney  * implements ObjectManager.
1712adc4f0dbSShawn McCarney  * @param callback Callback to invoke when inventory items have been obtained.
17138fb49dd6SShawn McCarney  */
1714adc4f0dbSShawn McCarney template <typename Callback>
1715504af5a0SPatrick Williams inline void getInventoryItems(
1716504af5a0SPatrick Williams     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1717daadfb2eSEd Tanous     const std::shared_ptr<std::set<std::string>>& sensorNames,
1718adc4f0dbSShawn McCarney     Callback&& callback)
17198fb49dd6SShawn McCarney {
172062598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryItems enter");
1721adc4f0dbSShawn McCarney     auto getInventoryItemAssociationsCb =
17228cb2c024SEd Tanous         [sensorsAsyncResp, callback = std::forward<Callback>(callback)](
1723daadfb2eSEd Tanous             const std::shared_ptr<std::vector<InventoryItem>>&
17244e0d8789SEd Tanous                 inventoryItems) mutable {
172562598e31SEd Tanous             BMCWEB_LOG_DEBUG("getInventoryItemAssociationsCb enter");
17268fb49dd6SShawn McCarney             auto getInventoryItemsConnectionsCb =
1727d0090733SEd Tanous                 [sensorsAsyncResp, inventoryItems,
17284e0d8789SEd Tanous                  callback = std::forward<Callback>(callback)](
1729daadfb2eSEd Tanous                     const std::shared_ptr<std::set<std::string>>&
17304e0d8789SEd Tanous                         invConnections) mutable {
173162598e31SEd Tanous                     BMCWEB_LOG_DEBUG("getInventoryItemsConnectionsCb enter");
1732bd79bce8SPatrick Williams                     auto getInventoryItemsDataCb =
1733bd79bce8SPatrick Williams                         [sensorsAsyncResp, inventoryItems,
17344e0d8789SEd Tanous                          callback =
17354e0d8789SEd Tanous                              std::forward<Callback>(callback)]() mutable {
173662598e31SEd Tanous                             BMCWEB_LOG_DEBUG("getInventoryItemsDataCb enter");
173742cbe538SGunnar Mills 
1738bd79bce8SPatrick Williams                             auto getInventoryLedsCb =
1739bd79bce8SPatrick Williams                                 [sensorsAsyncResp, inventoryItems,
17404e0d8789SEd Tanous                                  callback = std::forward<Callback>(
17414e0d8789SEd Tanous                                      callback)]() mutable {
1742bd79bce8SPatrick Williams                                     BMCWEB_LOG_DEBUG(
1743bd79bce8SPatrick Williams                                         "getInventoryLedsCb enter");
1744bd79bce8SPatrick Williams                                     // Find Power Supply Attributes and get the
1745bd79bce8SPatrick Williams                                     // data
1746bd79bce8SPatrick Williams                                     getPowerSupplyAttributes(
1747bd79bce8SPatrick Williams                                         sensorsAsyncResp, inventoryItems,
174842cbe538SGunnar Mills                                         std::move(callback));
174962598e31SEd Tanous                                     BMCWEB_LOG_DEBUG("getInventoryLedsCb exit");
175042cbe538SGunnar Mills                                 };
175142cbe538SGunnar Mills 
1752d500549bSAnthony Wilson                             // Find led connections and get the data
1753d500549bSAnthony Wilson                             getInventoryLeds(sensorsAsyncResp, inventoryItems,
175442cbe538SGunnar Mills                                              std::move(getInventoryLedsCb));
175562598e31SEd Tanous                             BMCWEB_LOG_DEBUG("getInventoryItemsDataCb exit");
1756d500549bSAnthony Wilson                         };
17578fb49dd6SShawn McCarney 
1758adc4f0dbSShawn McCarney                     // Get inventory item data from connections
1759adc4f0dbSShawn McCarney                     getInventoryItemsData(sensorsAsyncResp, inventoryItems,
1760d0090733SEd Tanous                                           invConnections,
1761d500549bSAnthony Wilson                                           std::move(getInventoryItemsDataCb));
176262598e31SEd Tanous                     BMCWEB_LOG_DEBUG("getInventoryItemsConnectionsCb exit");
17638fb49dd6SShawn McCarney                 };
17648fb49dd6SShawn McCarney 
1765adc4f0dbSShawn McCarney             // Get connections that provide inventory item data
1766bd79bce8SPatrick Williams             getInventoryItemsConnections(
1767bd79bce8SPatrick Williams                 sensorsAsyncResp, inventoryItems,
17688fb49dd6SShawn McCarney                 std::move(getInventoryItemsConnectionsCb));
176962598e31SEd Tanous             BMCWEB_LOG_DEBUG("getInventoryItemAssociationsCb exit");
17708fb49dd6SShawn McCarney         };
17718fb49dd6SShawn McCarney 
1772adc4f0dbSShawn McCarney     // Get associations from sensors to inventory items
1773d0090733SEd Tanous     getInventoryItemAssociations(sensorsAsyncResp, sensorNames,
1774adc4f0dbSShawn McCarney                                  std::move(getInventoryItemAssociationsCb));
177562598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryItems exit");
1776adc4f0dbSShawn McCarney }
1777adc4f0dbSShawn McCarney 
1778adc4f0dbSShawn McCarney /**
1779adc4f0dbSShawn McCarney  * @brief Returns JSON PowerSupply object for the specified inventory item.
1780adc4f0dbSShawn McCarney  *
1781adc4f0dbSShawn McCarney  * Searches for a JSON PowerSupply object that matches the specified inventory
1782adc4f0dbSShawn McCarney  * item.  If one is not found, a new PowerSupply object is added to the JSON
1783adc4f0dbSShawn McCarney  * array.
1784adc4f0dbSShawn McCarney  *
1785adc4f0dbSShawn McCarney  * Multiple sensors are often associated with one power supply inventory item.
1786adc4f0dbSShawn McCarney  * As a result, multiple sensor values are stored in one JSON PowerSupply
1787adc4f0dbSShawn McCarney  * object.
1788adc4f0dbSShawn McCarney  *
1789adc4f0dbSShawn McCarney  * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
1790adc4f0dbSShawn McCarney  * @param inventoryItem Inventory item for the power supply.
1791adc4f0dbSShawn McCarney  * @param chassisId Chassis that contains the power supply.
1792adc4f0dbSShawn McCarney  * @return JSON PowerSupply object for the specified inventory item.
1793adc4f0dbSShawn McCarney  */
179423a21a1cSEd Tanous inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
1795adc4f0dbSShawn McCarney                                       const InventoryItem& inventoryItem,
1796adc4f0dbSShawn McCarney                                       const std::string& chassisId)
1797adc4f0dbSShawn McCarney {
179818f8f608SEd Tanous     std::string nameS;
17996f4bd290SAlexander Hansen     nameS.resize(inventoryItem.name.size());
180018f8f608SEd Tanous     std::ranges::replace_copy(inventoryItem.name, nameS.begin(), '_', ' ');
1801adc4f0dbSShawn McCarney     // Check if matching PowerSupply object already exists in JSON array
1802adc4f0dbSShawn McCarney     for (nlohmann::json& powerSupply : powerSupplyArray)
1803adc4f0dbSShawn McCarney     {
180418f8f608SEd Tanous         nlohmann::json::iterator nameIt = powerSupply.find("Name");
180518f8f608SEd Tanous         if (nameIt == powerSupply.end())
180618f8f608SEd Tanous         {
180718f8f608SEd Tanous             continue;
180818f8f608SEd Tanous         }
180918f8f608SEd Tanous         const std::string* name = nameIt->get_ptr<std::string*>();
181018f8f608SEd Tanous         if (name == nullptr)
181118f8f608SEd Tanous         {
181218f8f608SEd Tanous             continue;
181318f8f608SEd Tanous         }
181418f8f608SEd Tanous         if (nameS == *name)
1815adc4f0dbSShawn McCarney         {
1816adc4f0dbSShawn McCarney             return powerSupply;
1817adc4f0dbSShawn McCarney         }
1818adc4f0dbSShawn McCarney     }
1819adc4f0dbSShawn McCarney 
1820adc4f0dbSShawn McCarney     // Add new PowerSupply object to JSON array
1821adc4f0dbSShawn McCarney     powerSupplyArray.push_back({});
1822adc4f0dbSShawn McCarney     nlohmann::json& powerSupply = powerSupplyArray.back();
1823bd79bce8SPatrick Williams     boost::urls::url url =
1824bd79bce8SPatrick Williams         boost::urls::format("/redfish/v1/Chassis/{}/Power", chassisId);
1825eddfc437SWilly Tu     url.set_fragment(("/PowerSupplies"_json_pointer).to_string());
1826eddfc437SWilly Tu     powerSupply["@odata.id"] = std::move(url);
182718f8f608SEd Tanous     std::string escaped;
18286f4bd290SAlexander Hansen     escaped.resize(inventoryItem.name.size());
182918f8f608SEd Tanous     std::ranges::replace_copy(inventoryItem.name, escaped.begin(), '_', ' ');
183018f8f608SEd Tanous     powerSupply["Name"] = std::move(escaped);
1831adc4f0dbSShawn McCarney     powerSupply["Manufacturer"] = inventoryItem.manufacturer;
1832adc4f0dbSShawn McCarney     powerSupply["Model"] = inventoryItem.model;
1833adc4f0dbSShawn McCarney     powerSupply["PartNumber"] = inventoryItem.partNumber;
1834adc4f0dbSShawn McCarney     powerSupply["SerialNumber"] = inventoryItem.serialNumber;
1835*f664fd8aSJanet Adkins     if constexpr (BMCWEB_REDFISH_ALLOW_DEPRECATED_INDICATORLED)
1836*f664fd8aSJanet Adkins     {
1837c9563608SJanet Adkins         sensor_utils::setLedState(powerSupply, &inventoryItem);
1838*f664fd8aSJanet Adkins     }
1839adc4f0dbSShawn McCarney 
184042cbe538SGunnar Mills     if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
184142cbe538SGunnar Mills     {
184242cbe538SGunnar Mills         powerSupply["EfficiencyPercent"] =
184342cbe538SGunnar Mills             inventoryItem.powerSupplyEfficiencyPercent;
184442cbe538SGunnar Mills     }
184542cbe538SGunnar Mills 
1846c9563608SJanet Adkins     powerSupply["Status"]["State"] =
1847c9563608SJanet Adkins         sensor_utils::getState(&inventoryItem, true);
1848adc4f0dbSShawn McCarney     const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
1849adc4f0dbSShawn McCarney     powerSupply["Status"]["Health"] = health;
1850adc4f0dbSShawn McCarney 
1851adc4f0dbSShawn McCarney     return powerSupply;
18528fb49dd6SShawn McCarney }
18538fb49dd6SShawn McCarney 
18548fb49dd6SShawn McCarney /**
1855de629b6eSShawn McCarney  * @brief Gets the values of the specified sensors.
1856de629b6eSShawn McCarney  *
1857de629b6eSShawn McCarney  * Stores the results as JSON in the SensorsAsyncResp.
1858de629b6eSShawn McCarney  *
1859de629b6eSShawn McCarney  * Gets the sensor values asynchronously.  Stores the results later when the
1860de629b6eSShawn McCarney  * information has been obtained.
1861de629b6eSShawn McCarney  *
1862adc4f0dbSShawn McCarney  * The sensorNames set contains all requested sensors for the current chassis.
1863de629b6eSShawn McCarney  *
1864de629b6eSShawn McCarney  * To minimize the number of DBus calls, the DBus method
1865de629b6eSShawn McCarney  * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
1866de629b6eSShawn McCarney  * values of all sensors provided by a connection (service).
1867de629b6eSShawn McCarney  *
1868de629b6eSShawn McCarney  * The connections set contains all the connections that provide sensor values.
1869de629b6eSShawn McCarney  *
1870adc4f0dbSShawn McCarney  * The InventoryItem vector contains D-Bus inventory items associated with the
1871adc4f0dbSShawn McCarney  * sensors.  Inventory item data is needed for some Redfish sensor properties.
1872adc4f0dbSShawn McCarney  *
1873de629b6eSShawn McCarney  * @param SensorsAsyncResp Pointer to object holding response data.
1874adc4f0dbSShawn McCarney  * @param sensorNames All requested sensors within the current chassis.
1875de629b6eSShawn McCarney  * @param connections Connections that provide sensor values.
1876de629b6eSShawn McCarney  * implements ObjectManager.
1877adc4f0dbSShawn McCarney  * @param inventoryItems Inventory items associated with the sensors.
1878de629b6eSShawn McCarney  */
187923a21a1cSEd Tanous inline void getSensorData(
188081ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1881fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& sensorNames,
1882fe04d49cSNan Zhou     const std::set<std::string>& connections,
1883b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems)
1884de629b6eSShawn McCarney {
188562598e31SEd Tanous     BMCWEB_LOG_DEBUG("getSensorData enter");
1886de629b6eSShawn McCarney     // Get managed objects from all services exposing sensors
1887de629b6eSShawn McCarney     for (const std::string& connection : connections)
1888de629b6eSShawn McCarney     {
18895eb468daSGeorge Liu         sdbusplus::message::object_path sensorPath(
18905eb468daSGeorge Liu             "/xyz/openbmc_project/sensors");
18915eb468daSGeorge Liu         dbus::utility::getManagedObjects(
18925eb468daSGeorge Liu             connection, sensorPath,
1893002d39b4SEd Tanous             [sensorsAsyncResp, sensorNames,
18945e7e2dc5SEd Tanous              inventoryItems](const boost::system::error_code& ec,
189502cad96eSEd Tanous                              const dbus::utility::ManagedObjectType& resp) {
189662598e31SEd Tanous                 BMCWEB_LOG_DEBUG("getManagedObjectsCb enter");
1897de629b6eSShawn McCarney                 if (ec)
1898de629b6eSShawn McCarney                 {
189962598e31SEd Tanous                     BMCWEB_LOG_ERROR("getManagedObjectsCb DBUS error: {}", ec);
19008d1b46d7Szhanghch05                     messages::internalError(sensorsAsyncResp->asyncResp->res);
1901de629b6eSShawn McCarney                     return;
1902de629b6eSShawn McCarney                 }
19030c728b42SJanet Adkins                 auto chassisSubNode = sensor_utils::chassisSubNodeFromString(
19040c728b42SJanet Adkins                     sensorsAsyncResp->chassisSubNode);
1905de629b6eSShawn McCarney                 // Go through all objects and update response with sensor data
1906de629b6eSShawn McCarney                 for (const auto& objDictEntry : resp)
1907de629b6eSShawn McCarney                 {
1908de629b6eSShawn McCarney                     const std::string& objPath =
1909de629b6eSShawn McCarney                         static_cast<const std::string&>(objDictEntry.first);
191062598e31SEd Tanous                     BMCWEB_LOG_DEBUG("getManagedObjectsCb parsing object {}",
191162598e31SEd Tanous                                      objPath);
1912de629b6eSShawn McCarney 
1913de629b6eSShawn McCarney                     std::vector<std::string> split;
1914de629b6eSShawn McCarney                     // Reserve space for
1915de629b6eSShawn McCarney                     // /xyz/openbmc_project/sensors/<name>/<subname>
1916de629b6eSShawn McCarney                     split.reserve(6);
191750ebd4afSEd Tanous                     // NOLINTNEXTLINE
191850ebd4afSEd Tanous                     bmcweb::split(split, objPath, '/');
1919de629b6eSShawn McCarney                     if (split.size() < 6)
1920de629b6eSShawn McCarney                     {
192162598e31SEd Tanous                         BMCWEB_LOG_ERROR("Got path that isn't long enough {}",
192262598e31SEd Tanous                                          objPath);
1923de629b6eSShawn McCarney                         continue;
1924de629b6eSShawn McCarney                     }
192550ebd4afSEd Tanous                     // These indexes aren't intuitive, as split puts an empty
1926de629b6eSShawn McCarney                     // string at the beginning
1927de629b6eSShawn McCarney                     const std::string& sensorType = split[4];
1928de629b6eSShawn McCarney                     const std::string& sensorName = split[5];
192962598e31SEd Tanous                     BMCWEB_LOG_DEBUG("sensorName {} sensorType {}", sensorName,
193062598e31SEd Tanous                                      sensorType);
19313d158643SEd Tanous                     if (!sensorNames->contains(objPath))
1932de629b6eSShawn McCarney                     {
193362598e31SEd Tanous                         BMCWEB_LOG_DEBUG("{} not in sensor list ", sensorName);
1934de629b6eSShawn McCarney                         continue;
1935de629b6eSShawn McCarney                     }
1936de629b6eSShawn McCarney 
1937adc4f0dbSShawn McCarney                     // Find inventory item (if any) associated with sensor
1938adc4f0dbSShawn McCarney                     InventoryItem* inventoryItem =
1939adc4f0dbSShawn McCarney                         findInventoryItemForSensor(inventoryItems, objPath);
1940adc4f0dbSShawn McCarney 
194195a3ecadSAnthony Wilson                     const std::string& sensorSchema =
194281ce609eSEd Tanous                         sensorsAsyncResp->chassisSubNode;
194395a3ecadSAnthony Wilson 
194495a3ecadSAnthony Wilson                     nlohmann::json* sensorJson = nullptr;
194595a3ecadSAnthony Wilson 
19460c728b42SJanet Adkins                     if (sensorSchema == sensors::sensorsNodeStr &&
1947928fefb9SNan Zhou                         !sensorsAsyncResp->efficientExpand)
194895a3ecadSAnthony Wilson                     {
19491516c21bSJanet Adkins                         std::string sensorId =
19501516c21bSJanet Adkins                             redfish::sensor_utils::getSensorId(sensorName,
19511516c21bSJanet Adkins                                                                sensorType);
1952c1d019a6SEd Tanous 
1953bd79bce8SPatrick Williams                         sensorsAsyncResp->asyncResp->res
1954bd79bce8SPatrick Williams                             .jsonValue["@odata.id"] = boost::urls::format(
1955bd79bce8SPatrick Williams                             "/redfish/v1/Chassis/{}/{}/{}",
1956c1d019a6SEd Tanous                             sensorsAsyncResp->chassisId,
1957bd79bce8SPatrick Williams                             sensorsAsyncResp->chassisSubNode, sensorId);
1958bd79bce8SPatrick Williams                         sensorJson =
1959bd79bce8SPatrick Williams                             &(sensorsAsyncResp->asyncResp->res.jsonValue);
196095a3ecadSAnthony Wilson                     }
196195a3ecadSAnthony Wilson                     else
196295a3ecadSAnthony Wilson                     {
1963271584abSEd Tanous                         std::string fieldName;
1964928fefb9SNan Zhou                         if (sensorsAsyncResp->efficientExpand)
1965928fefb9SNan Zhou                         {
1966928fefb9SNan Zhou                             fieldName = "Members";
1967928fefb9SNan Zhou                         }
1968928fefb9SNan Zhou                         else if (sensorType == "temperature")
1969de629b6eSShawn McCarney                         {
1970de629b6eSShawn McCarney                             fieldName = "Temperatures";
1971de629b6eSShawn McCarney                         }
1972bd79bce8SPatrick Williams                         else if (sensorType == "fan" ||
1973bd79bce8SPatrick Williams                                  sensorType == "fan_tach" ||
1974de629b6eSShawn McCarney                                  sensorType == "fan_pwm")
1975de629b6eSShawn McCarney                         {
1976de629b6eSShawn McCarney                             fieldName = "Fans";
1977de629b6eSShawn McCarney                         }
1978de629b6eSShawn McCarney                         else if (sensorType == "voltage")
1979de629b6eSShawn McCarney                         {
1980de629b6eSShawn McCarney                             fieldName = "Voltages";
1981de629b6eSShawn McCarney                         }
1982de629b6eSShawn McCarney                         else if (sensorType == "power")
1983de629b6eSShawn McCarney                         {
198455f79e6fSEd Tanous                             if (sensorName == "total_power")
1985028f7ebcSEddie James                             {
1986028f7ebcSEddie James                                 fieldName = "PowerControl";
1987028f7ebcSEddie James                             }
1988adc4f0dbSShawn McCarney                             else if ((inventoryItem != nullptr) &&
1989adc4f0dbSShawn McCarney                                      (inventoryItem->isPowerSupply))
1990028f7ebcSEddie James                             {
1991de629b6eSShawn McCarney                                 fieldName = "PowerSupplies";
1992de629b6eSShawn McCarney                             }
1993adc4f0dbSShawn McCarney                             else
1994adc4f0dbSShawn McCarney                             {
1995adc4f0dbSShawn McCarney                                 // Other power sensors are in SensorCollection
1996adc4f0dbSShawn McCarney                                 continue;
1997adc4f0dbSShawn McCarney                             }
1998028f7ebcSEddie James                         }
1999de629b6eSShawn McCarney                         else
2000de629b6eSShawn McCarney                         {
2001bd79bce8SPatrick Williams                             BMCWEB_LOG_ERROR(
2002bd79bce8SPatrick Williams                                 "Unsure how to handle sensorType {}",
200362598e31SEd Tanous                                 sensorType);
2004de629b6eSShawn McCarney                             continue;
2005de629b6eSShawn McCarney                         }
2006de629b6eSShawn McCarney 
2007de629b6eSShawn McCarney                         nlohmann::json& tempArray =
2008bd79bce8SPatrick Williams                             sensorsAsyncResp->asyncResp->res
2009bd79bce8SPatrick Williams                                 .jsonValue[fieldName];
2010adc4f0dbSShawn McCarney                         if (fieldName == "PowerControl")
201149c53ac9SJohnathan Mantey                         {
2012adc4f0dbSShawn McCarney                             if (tempArray.empty())
20137ab06f49SGunnar Mills                             {
201495a3ecadSAnthony Wilson                                 // Put multiple "sensors" into a single
201595a3ecadSAnthony Wilson                                 // PowerControl. Follows MemberId naming and
201695a3ecadSAnthony Wilson                                 // naming in power.hpp.
20171476687dSEd Tanous                                 nlohmann::json::object_t power;
2018ef4c65b7SEd Tanous                                 boost::urls::url url = boost::urls::format(
2019ef4c65b7SEd Tanous                                     "/redfish/v1/Chassis/{}/{}",
2020eddfc437SWilly Tu                                     sensorsAsyncResp->chassisId,
2021eddfc437SWilly Tu                                     sensorsAsyncResp->chassisSubNode);
2022bd79bce8SPatrick Williams                                 url.set_fragment(
2023bd79bce8SPatrick Williams                                     (""_json_pointer / fieldName / "0")
2024eddfc437SWilly Tu                                         .to_string());
2025eddfc437SWilly Tu                                 power["@odata.id"] = std::move(url);
2026b2ba3072SPatrick Williams                                 tempArray.emplace_back(std::move(power));
2027adc4f0dbSShawn McCarney                             }
2028adc4f0dbSShawn McCarney                             sensorJson = &(tempArray.back());
2029adc4f0dbSShawn McCarney                         }
2030adc4f0dbSShawn McCarney                         else if (fieldName == "PowerSupplies")
2031adc4f0dbSShawn McCarney                         {
2032adc4f0dbSShawn McCarney                             if (inventoryItem != nullptr)
2033adc4f0dbSShawn McCarney                             {
2034bd79bce8SPatrick Williams                                 sensorJson = &(getPowerSupply(
2035bd79bce8SPatrick Williams                                     tempArray, *inventoryItem,
203681ce609eSEd Tanous                                     sensorsAsyncResp->chassisId));
2037adc4f0dbSShawn McCarney                             }
203849c53ac9SJohnathan Mantey                         }
2039928fefb9SNan Zhou                         else if (fieldName == "Members")
2040928fefb9SNan Zhou                         {
20411516c21bSJanet Adkins                             std::string sensorId =
20421516c21bSJanet Adkins                                 redfish::sensor_utils::getSensorId(sensorName,
20431516c21bSJanet Adkins                                                                    sensorType);
2044677bb756SEd Tanous 
20451476687dSEd Tanous                             nlohmann::json::object_t member;
2046ef4c65b7SEd Tanous                             member["@odata.id"] = boost::urls::format(
2047ef4c65b7SEd Tanous                                 "/redfish/v1/Chassis/{}/{}/{}",
2048677bb756SEd Tanous                                 sensorsAsyncResp->chassisId,
2049677bb756SEd Tanous                                 sensorsAsyncResp->chassisSubNode, sensorId);
2050b2ba3072SPatrick Williams                             tempArray.emplace_back(std::move(member));
2051928fefb9SNan Zhou                             sensorJson = &(tempArray.back());
2052928fefb9SNan Zhou                         }
205349c53ac9SJohnathan Mantey                         else
205449c53ac9SJohnathan Mantey                         {
20551476687dSEd Tanous                             nlohmann::json::object_t member;
2056ef4c65b7SEd Tanous                             boost::urls::url url = boost::urls::format(
2057ef4c65b7SEd Tanous                                 "/redfish/v1/Chassis/{}/{}",
2058eddfc437SWilly Tu                                 sensorsAsyncResp->chassisId,
2059eddfc437SWilly Tu                                 sensorsAsyncResp->chassisSubNode);
2060eddfc437SWilly Tu                             url.set_fragment(
2061eddfc437SWilly Tu                                 (""_json_pointer / fieldName).to_string());
2062eddfc437SWilly Tu                             member["@odata.id"] = std::move(url);
2063b2ba3072SPatrick Williams                             tempArray.emplace_back(std::move(member));
2064adc4f0dbSShawn McCarney                             sensorJson = &(tempArray.back());
206549c53ac9SJohnathan Mantey                         }
206695a3ecadSAnthony Wilson                     }
2067de629b6eSShawn McCarney 
2068adc4f0dbSShawn McCarney                     if (sensorJson != nullptr)
2069adc4f0dbSShawn McCarney                     {
20700c728b42SJanet Adkins                         objectInterfacesToJson(
20710c728b42SJanet Adkins                             sensorName, sensorType, chassisSubNode,
20720c728b42SJanet Adkins                             objDictEntry.second, *sensorJson, inventoryItem);
20731d7c0054SEd Tanous 
20741d7c0054SEd Tanous                         std::string path = "/xyz/openbmc_project/sensors/";
20751d7c0054SEd Tanous                         path += sensorType;
20761d7c0054SEd Tanous                         path += "/";
20771d7c0054SEd Tanous                         path += sensorName;
2078c1d019a6SEd Tanous                         sensorsAsyncResp->addMetadata(*sensorJson, path);
2079adc4f0dbSShawn McCarney                     }
2080de629b6eSShawn McCarney                 }
208181ce609eSEd Tanous                 if (sensorsAsyncResp.use_count() == 1)
208249c53ac9SJohnathan Mantey                 {
208381ce609eSEd Tanous                     sortJSONResponse(sensorsAsyncResp);
20840c728b42SJanet Adkins                     if (chassisSubNode ==
20850c728b42SJanet Adkins                             sensor_utils::ChassisSubNode::sensorsNode &&
2086928fefb9SNan Zhou                         sensorsAsyncResp->efficientExpand)
2087928fefb9SNan Zhou                     {
2088928fefb9SNan Zhou                         sensorsAsyncResp->asyncResp->res
2089928fefb9SNan Zhou                             .jsonValue["Members@odata.count"] =
2090bd79bce8SPatrick Williams                             sensorsAsyncResp->asyncResp->res
2091bd79bce8SPatrick Williams                                 .jsonValue["Members"]
2092928fefb9SNan Zhou                                 .size();
2093928fefb9SNan Zhou                     }
20940c728b42SJanet Adkins                     else if (chassisSubNode ==
20950c728b42SJanet Adkins                              sensor_utils::ChassisSubNode::thermalNode)
20968bd25ccdSJames Feist                     {
209781ce609eSEd Tanous                         populateFanRedundancy(sensorsAsyncResp);
20988bd25ccdSJames Feist                     }
209949c53ac9SJohnathan Mantey                 }
210062598e31SEd Tanous                 BMCWEB_LOG_DEBUG("getManagedObjectsCb exit");
21015eb468daSGeorge Liu             });
210223a21a1cSEd Tanous     }
210362598e31SEd Tanous     BMCWEB_LOG_DEBUG("getSensorData exit");
2104de629b6eSShawn McCarney }
2105de629b6eSShawn McCarney 
2106504af5a0SPatrick Williams inline void processSensorList(
2107504af5a0SPatrick Williams     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2108fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& sensorNames)
21091abe55efSEd Tanous {
2110fe04d49cSNan Zhou     auto getConnectionCb = [sensorsAsyncResp, sensorNames](
2111fe04d49cSNan Zhou                                const std::set<std::string>& connections) {
211262598e31SEd Tanous         BMCWEB_LOG_DEBUG("getConnectionCb enter");
2113adc4f0dbSShawn McCarney         auto getInventoryItemsCb =
2114bd79bce8SPatrick Williams             [sensorsAsyncResp, sensorNames, connections](
2115bd79bce8SPatrick Williams                 const std::shared_ptr<std::vector<InventoryItem>>&
21164e0d8789SEd Tanous                     inventoryItems) mutable {
211762598e31SEd Tanous                 BMCWEB_LOG_DEBUG("getInventoryItemsCb enter");
211849c53ac9SJohnathan Mantey                 // Get sensor data and store results in JSON
2119002d39b4SEd Tanous                 getSensorData(sensorsAsyncResp, sensorNames, connections,
2120d0090733SEd Tanous                               inventoryItems);
212162598e31SEd Tanous                 BMCWEB_LOG_DEBUG("getInventoryItemsCb exit");
2122adc4f0dbSShawn McCarney             };
2123adc4f0dbSShawn McCarney 
2124adc4f0dbSShawn McCarney         // Get inventory items associated with sensors
2125d0090733SEd Tanous         getInventoryItems(sensorsAsyncResp, sensorNames,
2126adc4f0dbSShawn McCarney                           std::move(getInventoryItemsCb));
2127adc4f0dbSShawn McCarney 
212862598e31SEd Tanous         BMCWEB_LOG_DEBUG("getConnectionCb exit");
212908777fb0SLewanczyk, Dawid     };
2130de629b6eSShawn McCarney 
2131de629b6eSShawn McCarney     // Get set of connections that provide sensor values
213281ce609eSEd Tanous     getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb));
213395a3ecadSAnthony Wilson }
213495a3ecadSAnthony Wilson 
213595a3ecadSAnthony Wilson /**
213695a3ecadSAnthony Wilson  * @brief Entry point for retrieving sensors data related to requested
213795a3ecadSAnthony Wilson  *        chassis.
213895a3ecadSAnthony Wilson  * @param SensorsAsyncResp   Pointer to object holding response data
213995a3ecadSAnthony Wilson  */
2140504af5a0SPatrick Williams inline void getChassisData(
2141504af5a0SPatrick Williams     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
214295a3ecadSAnthony Wilson {
214362598e31SEd Tanous     BMCWEB_LOG_DEBUG("getChassisData enter");
214495a3ecadSAnthony Wilson     auto getChassisCb =
214581ce609eSEd Tanous         [sensorsAsyncResp](
2146fe04d49cSNan Zhou             const std::shared_ptr<std::set<std::string>>& sensorNames) {
214762598e31SEd Tanous             BMCWEB_LOG_DEBUG("getChassisCb enter");
214881ce609eSEd Tanous             processSensorList(sensorsAsyncResp, sensorNames);
214962598e31SEd Tanous             BMCWEB_LOG_DEBUG("getChassisCb exit");
215008777fb0SLewanczyk, Dawid         };
2151928fefb9SNan Zhou     // SensorCollection doesn't contain the Redundancy property
21520c728b42SJanet Adkins     if (sensorsAsyncResp->chassisSubNode != sensors::sensorsNodeStr)
2153928fefb9SNan Zhou     {
21548d1b46d7Szhanghch05         sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] =
21558d1b46d7Szhanghch05             nlohmann::json::array();
2156928fefb9SNan Zhou     }
215726f03899SShawn McCarney     // Get set of sensors in chassis
21587f1cc26dSEd Tanous     getChassis(sensorsAsyncResp->asyncResp, sensorsAsyncResp->chassisId,
21597f1cc26dSEd Tanous                sensorsAsyncResp->chassisSubNode, sensorsAsyncResp->types,
21607f1cc26dSEd Tanous                std::move(getChassisCb));
216162598e31SEd Tanous     BMCWEB_LOG_DEBUG("getChassisData exit");
2162271584abSEd Tanous }
216308777fb0SLewanczyk, Dawid 
2164413961deSRichard Marian Thomaiyar /**
216549c53ac9SJohnathan Mantey  * @brief Find the requested sensorName in the list of all sensors supplied by
216649c53ac9SJohnathan Mantey  * the chassis node
216749c53ac9SJohnathan Mantey  *
216849c53ac9SJohnathan Mantey  * @param sensorName   The sensor name supplied in the PATCH request
216949c53ac9SJohnathan Mantey  * @param sensorsList  The list of sensors managed by the chassis node
217049c53ac9SJohnathan Mantey  * @param sensorsModified  The list of sensors that were found as a result of
217149c53ac9SJohnathan Mantey  *                         repeated calls to this function
217249c53ac9SJohnathan Mantey  */
2173bd79bce8SPatrick Williams inline bool findSensorNameUsingSensorPath(
2174bd79bce8SPatrick Williams     std::string_view sensorName, const std::set<std::string>& sensorsList,
2175fe04d49cSNan Zhou     std::set<std::string>& sensorsModified)
217649c53ac9SJohnathan Mantey {
2177fe04d49cSNan Zhou     for (const auto& chassisSensor : sensorsList)
217849c53ac9SJohnathan Mantey     {
217928aa8de5SGeorge Liu         sdbusplus::message::object_path path(chassisSensor);
2180b00dcc27SEd Tanous         std::string thisSensorName = path.filename();
218128aa8de5SGeorge Liu         if (thisSensorName.empty())
218249c53ac9SJohnathan Mantey         {
218349c53ac9SJohnathan Mantey             continue;
218449c53ac9SJohnathan Mantey         }
218549c53ac9SJohnathan Mantey         if (thisSensorName == sensorName)
218649c53ac9SJohnathan Mantey         {
218749c53ac9SJohnathan Mantey             sensorsModified.emplace(chassisSensor);
218849c53ac9SJohnathan Mantey             return true;
218949c53ac9SJohnathan Mantey         }
219049c53ac9SJohnathan Mantey     }
219149c53ac9SJohnathan Mantey     return false;
219249c53ac9SJohnathan Mantey }
219349c53ac9SJohnathan Mantey 
219449c53ac9SJohnathan Mantey /**
2195413961deSRichard Marian Thomaiyar  * @brief Entry point for overriding sensor values of given sensor
2196413961deSRichard Marian Thomaiyar  *
21978d1b46d7Szhanghch05  * @param sensorAsyncResp   response object
21984bb3dc34SCarol Wang  * @param allCollections   Collections extract from sensors' request patch info
2199413961deSRichard Marian Thomaiyar  * @param chassisSubNode   Chassis Node for which the query has to happen
2200413961deSRichard Marian Thomaiyar  */
220123a21a1cSEd Tanous inline void setSensorsOverride(
2202b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
22030885057cSEd Tanous     std::unordered_map<std::string, std::vector<nlohmann::json::object_t>>&
2204397fd61fSjayaprakash Mutyala         allCollections)
2205413961deSRichard Marian Thomaiyar {
220662598e31SEd Tanous     BMCWEB_LOG_INFO("setSensorsOverride for subNode{}",
220762598e31SEd Tanous                     sensorAsyncResp->chassisSubNode);
2208413961deSRichard Marian Thomaiyar 
2209d02aad39SEd Tanous     std::string_view propertyValueName;
2210f65af9e8SRichard Marian Thomaiyar     std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
2211413961deSRichard Marian Thomaiyar     std::string memberId;
2212543f4400SEd Tanous     double value = 0.0;
2213f65af9e8SRichard Marian Thomaiyar     for (auto& collectionItems : allCollections)
2214f65af9e8SRichard Marian Thomaiyar     {
2215f65af9e8SRichard Marian Thomaiyar         if (collectionItems.first == "Temperatures")
2216f65af9e8SRichard Marian Thomaiyar         {
2217f65af9e8SRichard Marian Thomaiyar             propertyValueName = "ReadingCelsius";
2218f65af9e8SRichard Marian Thomaiyar         }
2219f65af9e8SRichard Marian Thomaiyar         else if (collectionItems.first == "Fans")
2220f65af9e8SRichard Marian Thomaiyar         {
2221f65af9e8SRichard Marian Thomaiyar             propertyValueName = "Reading";
2222f65af9e8SRichard Marian Thomaiyar         }
2223f65af9e8SRichard Marian Thomaiyar         else
2224f65af9e8SRichard Marian Thomaiyar         {
2225f65af9e8SRichard Marian Thomaiyar             propertyValueName = "ReadingVolts";
2226f65af9e8SRichard Marian Thomaiyar         }
2227f65af9e8SRichard Marian Thomaiyar         for (auto& item : collectionItems.second)
2228f65af9e8SRichard Marian Thomaiyar         {
2229afc474aeSMyung Bae             if (!json_util::readJsonObject(                //
2230afc474aeSMyung Bae                     item, sensorAsyncResp->asyncResp->res, //
2231afc474aeSMyung Bae                     "MemberId", memberId,                  //
2232afc474aeSMyung Bae                     propertyValueName, value               //
2233afc474aeSMyung Bae                     ))
2234413961deSRichard Marian Thomaiyar             {
2235413961deSRichard Marian Thomaiyar                 return;
2236413961deSRichard Marian Thomaiyar             }
2237f65af9e8SRichard Marian Thomaiyar             overrideMap.emplace(memberId,
2238f65af9e8SRichard Marian Thomaiyar                                 std::make_pair(value, collectionItems.first));
2239f65af9e8SRichard Marian Thomaiyar         }
2240f65af9e8SRichard Marian Thomaiyar     }
22414bb3dc34SCarol Wang 
2242bd79bce8SPatrick Williams     auto getChassisSensorListCb = [sensorAsyncResp, overrideMap,
2243bd79bce8SPatrick Williams                                    propertyValueNameStr =
2244bd79bce8SPatrick Williams                                        std::string(propertyValueName)](
2245bd79bce8SPatrick Williams                                       const std::shared_ptr<
2246bd79bce8SPatrick Williams                                           std::set<std::string>>& sensorsList) {
224749c53ac9SJohnathan Mantey         // Match sensor names in the PATCH request to those managed by the
224849c53ac9SJohnathan Mantey         // chassis node
2249fe04d49cSNan Zhou         const std::shared_ptr<std::set<std::string>> sensorNames =
2250fe04d49cSNan Zhou             std::make_shared<std::set<std::string>>();
2251f65af9e8SRichard Marian Thomaiyar         for (const auto& item : overrideMap)
2252413961deSRichard Marian Thomaiyar         {
2253f65af9e8SRichard Marian Thomaiyar             const auto& sensor = item.first;
2254c71d6125SEd Tanous             std::pair<std::string, std::string> sensorNameType =
22551516c21bSJanet Adkins                 redfish::sensor_utils::splitSensorNameAndType(sensor);
2256c71d6125SEd Tanous             if (!findSensorNameUsingSensorPath(sensorNameType.second,
2257c71d6125SEd Tanous                                                *sensorsList, *sensorNames))
2258f65af9e8SRichard Marian Thomaiyar             {
225962598e31SEd Tanous                 BMCWEB_LOG_INFO("Unable to find memberId {}", item.first);
22608d1b46d7Szhanghch05                 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2261f65af9e8SRichard Marian Thomaiyar                                            item.second.second, item.first);
2262413961deSRichard Marian Thomaiyar                 return;
2263413961deSRichard Marian Thomaiyar             }
2264f65af9e8SRichard Marian Thomaiyar         }
2265413961deSRichard Marian Thomaiyar         // Get the connection to which the memberId belongs
2266bd79bce8SPatrick Williams         auto getObjectsWithConnectionCb = [sensorAsyncResp, overrideMap,
2267bd79bce8SPatrick Williams                                            propertyValueNameStr](
2268bd79bce8SPatrick Williams                                               const std::set<
2269bd79bce8SPatrick Williams                                                   std::string>& /*connections*/,
2270bd79bce8SPatrick Williams                                               const std::set<std::pair<
2271bd79bce8SPatrick Williams                                                   std::string, std::string>>&
2272413961deSRichard Marian Thomaiyar                                                   objectsWithConnection) {
2273f65af9e8SRichard Marian Thomaiyar             if (objectsWithConnection.size() != overrideMap.size())
2274413961deSRichard Marian Thomaiyar             {
227562598e31SEd Tanous                 BMCWEB_LOG_INFO(
227662598e31SEd Tanous                     "Unable to find all objects with proper connection {} requested {}",
227762598e31SEd Tanous                     objectsWithConnection.size(), overrideMap.size());
2278bd79bce8SPatrick Williams                 messages::resourceNotFound(
2279bd79bce8SPatrick Williams                     sensorAsyncResp->asyncResp->res,
22800c728b42SJanet Adkins                     sensorAsyncResp->chassisSubNode == sensors::thermalNodeStr
2281413961deSRichard Marian Thomaiyar                         ? "Temperatures"
2282413961deSRichard Marian Thomaiyar                         : "Voltages",
2283f65af9e8SRichard Marian Thomaiyar                     "Count");
2284f65af9e8SRichard Marian Thomaiyar                 return;
2285f65af9e8SRichard Marian Thomaiyar             }
2286f65af9e8SRichard Marian Thomaiyar             for (const auto& item : objectsWithConnection)
2287f65af9e8SRichard Marian Thomaiyar             {
228828aa8de5SGeorge Liu                 sdbusplus::message::object_path path(item.first);
228928aa8de5SGeorge Liu                 std::string sensorName = path.filename();
229028aa8de5SGeorge Liu                 if (sensorName.empty())
2291f65af9e8SRichard Marian Thomaiyar                 {
22924f277b54SJayaprakash Mutyala                     messages::internalError(sensorAsyncResp->asyncResp->res);
2293f65af9e8SRichard Marian Thomaiyar                     return;
2294f65af9e8SRichard Marian Thomaiyar                 }
22951516c21bSJanet Adkins                 std::string id = redfish::sensor_utils::getSensorId(
22961516c21bSJanet Adkins                     sensorName, path.parent_path().filename());
2297f65af9e8SRichard Marian Thomaiyar 
22983f5eb755SBan Feng                 const auto& iterator = overrideMap.find(id);
2299f65af9e8SRichard Marian Thomaiyar                 if (iterator == overrideMap.end())
2300f65af9e8SRichard Marian Thomaiyar                 {
230162598e31SEd Tanous                     BMCWEB_LOG_INFO("Unable to find sensor object{}",
230262598e31SEd Tanous                                     item.first);
23034f277b54SJayaprakash Mutyala                     messages::internalError(sensorAsyncResp->asyncResp->res);
2304413961deSRichard Marian Thomaiyar                     return;
2305413961deSRichard Marian Thomaiyar                 }
2306e93abac6SGinu George                 setDbusProperty(sensorAsyncResp->asyncResp,
2307e93abac6SGinu George                                 propertyValueNameStr, item.second, item.first,
2308e93abac6SGinu George                                 "xyz.openbmc_project.Sensor.Value", "Value",
2309d02aad39SEd Tanous                                 iterator->second.first);
2310f65af9e8SRichard Marian Thomaiyar             }
2311413961deSRichard Marian Thomaiyar         };
2312413961deSRichard Marian Thomaiyar         // Get object with connection for the given sensor name
2313413961deSRichard Marian Thomaiyar         getObjectsWithConnection(sensorAsyncResp, sensorNames,
2314413961deSRichard Marian Thomaiyar                                  std::move(getObjectsWithConnectionCb));
2315413961deSRichard Marian Thomaiyar     };
2316413961deSRichard Marian Thomaiyar     // get full sensor list for the given chassisId and cross verify the sensor.
23177f1cc26dSEd Tanous     getChassis(sensorAsyncResp->asyncResp, sensorAsyncResp->chassisId,
23187f1cc26dSEd Tanous                sensorAsyncResp->chassisSubNode, sensorAsyncResp->types,
23197f1cc26dSEd Tanous                std::move(getChassisSensorListCb));
2320413961deSRichard Marian Thomaiyar }
2321413961deSRichard Marian Thomaiyar 
2322a0ec28b6SAdrian Ambrożewicz /**
2323a0ec28b6SAdrian Ambrożewicz  * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
2324a0ec28b6SAdrian Ambrożewicz  * path of the sensor.
2325a0ec28b6SAdrian Ambrożewicz  *
2326a0ec28b6SAdrian Ambrożewicz  * Function builds valid Redfish response for sensor query of given chassis and
2327a0ec28b6SAdrian Ambrożewicz  * node. It then builds metadata about Redfish<->D-Bus correlations and provides
2328a0ec28b6SAdrian Ambrożewicz  * it to caller in a callback.
2329a0ec28b6SAdrian Ambrożewicz  *
2330a0ec28b6SAdrian Ambrożewicz  * @param chassis   Chassis for which retrieval should be performed
2331c9563608SJanet Adkins  * @param node  Node (group) of sensors. See sensor_utils::node for supported
2332c9563608SJanet Adkins  * values
2333a0ec28b6SAdrian Ambrożewicz  * @param mapComplete   Callback to be called with retrieval result
2334a0ec28b6SAdrian Ambrożewicz  */
2335931edc79SEd Tanous template <typename Callback>
2336bd79bce8SPatrick Williams inline void retrieveUriToDbusMap(
2337bd79bce8SPatrick Williams     const std::string& chassis, const std::string& node, Callback&& mapComplete)
2338a0ec28b6SAdrian Ambrożewicz {
233902da7c5aSEd Tanous     decltype(sensors::paths)::const_iterator pathIt =
234002da7c5aSEd Tanous         std::find_if(sensors::paths.cbegin(), sensors::paths.cend(),
234102da7c5aSEd Tanous                      [&node](auto&& val) { return val.first == node; });
234202da7c5aSEd Tanous     if (pathIt == sensors::paths.cend())
2343a0ec28b6SAdrian Ambrożewicz     {
234462598e31SEd Tanous         BMCWEB_LOG_ERROR("Wrong node provided : {}", node);
23456804b5c8SEd Tanous         std::map<std::string, std::string> noop;
23466804b5c8SEd Tanous         mapComplete(boost::beast::http::status::bad_request, noop);
2347a0ec28b6SAdrian Ambrożewicz         return;
2348a0ec28b6SAdrian Ambrożewicz     }
2349d51e072fSKrzysztof Grobelny 
235072374eb7SNan Zhou     auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
2351bd79bce8SPatrick Williams     auto callback =
2352bd79bce8SPatrick Williams         [asyncResp, mapCompleteCb = std::forward<Callback>(mapComplete)](
2353a0ec28b6SAdrian Ambrożewicz             const boost::beast::http::status status,
2354fe04d49cSNan Zhou             const std::map<std::string, std::string>& uriToDbus) {
2355fe04d49cSNan Zhou             mapCompleteCb(status, uriToDbus);
2356fe04d49cSNan Zhou         };
2357a0ec28b6SAdrian Ambrożewicz 
2358a0ec28b6SAdrian Ambrożewicz     auto resp = std::make_shared<SensorsAsyncResp>(
2359d51e072fSKrzysztof Grobelny         asyncResp, chassis, pathIt->second, node, std::move(callback));
2360a0ec28b6SAdrian Ambrożewicz     getChassisData(resp);
2361a0ec28b6SAdrian Ambrożewicz }
2362a0ec28b6SAdrian Ambrożewicz 
2363bacb2162SNan Zhou namespace sensors
2364bacb2162SNan Zhou {
2365928fefb9SNan Zhou 
2366bacb2162SNan Zhou inline void getChassisCallback(
2367c1d019a6SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2368c1d019a6SEd Tanous     std::string_view chassisId, std::string_view chassisSubNode,
2369fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& sensorNames)
2370bacb2162SNan Zhou {
237162598e31SEd Tanous     BMCWEB_LOG_DEBUG("getChassisCallback enter ");
2372bacb2162SNan Zhou 
2373c1d019a6SEd Tanous     nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
2374c1d019a6SEd Tanous     for (const std::string& sensor : *sensorNames)
2375bacb2162SNan Zhou     {
237662598e31SEd Tanous         BMCWEB_LOG_DEBUG("Adding sensor: {}", sensor);
2377bacb2162SNan Zhou 
2378bacb2162SNan Zhou         sdbusplus::message::object_path path(sensor);
2379bacb2162SNan Zhou         std::string sensorName = path.filename();
2380bacb2162SNan Zhou         if (sensorName.empty())
2381bacb2162SNan Zhou         {
238262598e31SEd Tanous             BMCWEB_LOG_ERROR("Invalid sensor path: {}", sensor);
2383c1d019a6SEd Tanous             messages::internalError(asyncResp->res);
2384bacb2162SNan Zhou             return;
2385bacb2162SNan Zhou         }
2386c1d019a6SEd Tanous         std::string type = path.parent_path().filename();
23871516c21bSJanet Adkins         std::string id = redfish::sensor_utils::getSensorId(sensorName, type);
2388c1d019a6SEd Tanous 
23891476687dSEd Tanous         nlohmann::json::object_t member;
2390ef4c65b7SEd Tanous         member["@odata.id"] = boost::urls::format(
2391ef4c65b7SEd Tanous             "/redfish/v1/Chassis/{}/{}/{}", chassisId, chassisSubNode, id);
2392c1d019a6SEd Tanous 
2393b2ba3072SPatrick Williams         entriesArray.emplace_back(std::move(member));
2394bacb2162SNan Zhou     }
2395bacb2162SNan Zhou 
2396c1d019a6SEd Tanous     asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
239762598e31SEd Tanous     BMCWEB_LOG_DEBUG("getChassisCallback exit");
2398bacb2162SNan Zhou }
2399e6bd846dSNan Zhou 
2400ac106bf6SEd Tanous inline void handleSensorCollectionGet(
2401ac106bf6SEd Tanous     App& app, const crow::Request& req,
2402ac106bf6SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2403de167a6fSNan Zhou     const std::string& chassisId)
2404de167a6fSNan Zhou {
2405de167a6fSNan Zhou     query_param::QueryCapabilities capabilities = {
2406de167a6fSNan Zhou         .canDelegateExpandLevel = 1,
2407de167a6fSNan Zhou     };
2408de167a6fSNan Zhou     query_param::Query delegatedQuery;
2409ac106bf6SEd Tanous     if (!redfish::setUpRedfishRouteWithDelegation(app, req, asyncResp,
2410de167a6fSNan Zhou                                                   delegatedQuery, capabilities))
2411de167a6fSNan Zhou     {
2412de167a6fSNan Zhou         return;
2413de167a6fSNan Zhou     }
2414de167a6fSNan Zhou 
2415de167a6fSNan Zhou     if (delegatedQuery.expandType != query_param::ExpandType::None)
2416de167a6fSNan Zhou     {
2417de167a6fSNan Zhou         // we perform efficient expand.
2418ac106bf6SEd Tanous         auto sensorsAsyncResp = std::make_shared<SensorsAsyncResp>(
2419ac106bf6SEd Tanous             asyncResp, chassisId, sensors::dbus::sensorPaths,
24200c728b42SJanet Adkins             sensors::sensorsNodeStr,
2421de167a6fSNan Zhou             /*efficientExpand=*/true);
2422ac106bf6SEd Tanous         getChassisData(sensorsAsyncResp);
2423de167a6fSNan Zhou 
242462598e31SEd Tanous         BMCWEB_LOG_DEBUG(
242562598e31SEd Tanous             "SensorCollection doGet exit via efficient expand handler");
2426de167a6fSNan Zhou         return;
24270bad320cSEd Tanous     }
2428de167a6fSNan Zhou 
2429de167a6fSNan Zhou     // We get all sensors as hyperlinkes in the chassis (this
2430de167a6fSNan Zhou     // implies we reply on the default query parameters handler)
24310c728b42SJanet Adkins     getChassis(asyncResp, chassisId, sensors::sensorsNodeStr, dbus::sensorPaths,
2432ac106bf6SEd Tanous                std::bind_front(sensors::getChassisCallback, asyncResp,
24330c728b42SJanet Adkins                                chassisId, sensors::sensorsNodeStr));
2434c1d019a6SEd Tanous }
24357f1cc26dSEd Tanous 
2436504af5a0SPatrick Williams inline void getSensorFromDbus(
2437504af5a0SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2438c1d019a6SEd Tanous     const std::string& sensorPath,
2439c1d019a6SEd Tanous     const ::dbus::utility::MapperGetObject& mapperResponse)
2440c1d019a6SEd Tanous {
2441c1d019a6SEd Tanous     if (mapperResponse.size() != 1)
2442c1d019a6SEd Tanous     {
2443c1d019a6SEd Tanous         messages::internalError(asyncResp->res);
2444c1d019a6SEd Tanous         return;
2445c1d019a6SEd Tanous     }
2446c1d019a6SEd Tanous     const auto& valueIface = *mapperResponse.begin();
2447c1d019a6SEd Tanous     const std::string& connectionName = valueIface.first;
244862598e31SEd Tanous     BMCWEB_LOG_DEBUG("Looking up {}", connectionName);
244962598e31SEd Tanous     BMCWEB_LOG_DEBUG("Path {}", sensorPath);
2450c1343bf6SKrzysztof Grobelny 
2451deae6a78SEd Tanous     ::dbus::utility::getAllProperties(
2452c1343bf6SKrzysztof Grobelny         *crow::connections::systemBus, connectionName, sensorPath, "",
2453c1d019a6SEd Tanous         [asyncResp,
24545e7e2dc5SEd Tanous          sensorPath](const boost::system::error_code& ec,
2455c1d019a6SEd Tanous                      const ::dbus::utility::DBusPropertiesMap& valuesDict) {
2456c1d019a6SEd Tanous             if (ec)
2457c1d019a6SEd Tanous             {
2458c1d019a6SEd Tanous                 messages::internalError(asyncResp->res);
2459c1d019a6SEd Tanous                 return;
2460c1d019a6SEd Tanous             }
2461c1d019a6SEd Tanous             sdbusplus::message::object_path path(sensorPath);
2462c1d019a6SEd Tanous             std::string name = path.filename();
2463c1d019a6SEd Tanous             path = path.parent_path();
2464c1d019a6SEd Tanous             std::string type = path.filename();
2465c9563608SJanet Adkins             sensor_utils::objectPropertiesToJson(
24660c728b42SJanet Adkins                 name, type, sensor_utils::ChassisSubNode::sensorsNode,
24670c728b42SJanet Adkins                 valuesDict, asyncResp->res.jsonValue, nullptr);
2468c1343bf6SKrzysztof Grobelny         });
2469de167a6fSNan Zhou }
2470de167a6fSNan Zhou 
2471e6bd846dSNan Zhou inline void handleSensorGet(App& app, const crow::Request& req,
2472c1d019a6SEd Tanous                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2473677bb756SEd Tanous                             const std::string& chassisId,
2474c1d019a6SEd Tanous                             const std::string& sensorId)
2475e6bd846dSNan Zhou {
2476c1d019a6SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2477e6bd846dSNan Zhou     {
2478e6bd846dSNan Zhou         return;
2479e6bd846dSNan Zhou     }
2480c71d6125SEd Tanous     std::pair<std::string, std::string> nameType =
24811516c21bSJanet Adkins         redfish::sensor_utils::splitSensorNameAndType(sensorId);
2482c71d6125SEd Tanous     if (nameType.first.empty() || nameType.second.empty())
2483c1d019a6SEd Tanous     {
2484c1d019a6SEd Tanous         messages::resourceNotFound(asyncResp->res, sensorId, "Sensor");
2485c1d019a6SEd Tanous         return;
2486c1d019a6SEd Tanous     }
2487c71d6125SEd Tanous 
2488ef4c65b7SEd Tanous     asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
2489ef4c65b7SEd Tanous         "/redfish/v1/Chassis/{}/Sensors/{}", chassisId, sensorId);
2490c1d019a6SEd Tanous 
249162598e31SEd Tanous     BMCWEB_LOG_DEBUG("Sensor doGet enter");
2492e6bd846dSNan Zhou 
24932b73119cSGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
2494e6bd846dSNan Zhou         "xyz.openbmc_project.Sensor.Value"};
2495c71d6125SEd Tanous     std::string sensorPath = "/xyz/openbmc_project/sensors/" + nameType.first +
2496c71d6125SEd Tanous                              '/' + nameType.second;
2497e6bd846dSNan Zhou     // Get a list of all of the sensors that implement Sensor.Value
2498e6bd846dSNan Zhou     // and get the path and service name associated with the sensor
24992b73119cSGeorge Liu     ::dbus::utility::getDbusObject(
25002b73119cSGeorge Liu         sensorPath, interfaces,
2501aec0ec30SMyung Bae         [asyncResp, sensorId,
25022b73119cSGeorge Liu          sensorPath](const boost::system::error_code& ec,
2503c1d019a6SEd Tanous                      const ::dbus::utility::MapperGetObject& subtree) {
250462598e31SEd Tanous             BMCWEB_LOG_DEBUG("respHandler1 enter");
2505aec0ec30SMyung Bae             if (ec == boost::system::errc::io_error)
2506aec0ec30SMyung Bae             {
250762598e31SEd Tanous                 BMCWEB_LOG_WARNING("Sensor not found from getSensorPaths");
2508aec0ec30SMyung Bae                 messages::resourceNotFound(asyncResp->res, sensorId, "Sensor");
2509aec0ec30SMyung Bae                 return;
2510aec0ec30SMyung Bae             }
2511e6bd846dSNan Zhou             if (ec)
2512e6bd846dSNan Zhou             {
2513c1d019a6SEd Tanous                 messages::internalError(asyncResp->res);
251462598e31SEd Tanous                 BMCWEB_LOG_ERROR(
251562598e31SEd Tanous                     "Sensor getSensorPaths resp_handler: Dbus error {}", ec);
2516e6bd846dSNan Zhou                 return;
2517e6bd846dSNan Zhou             }
2518c1d019a6SEd Tanous             getSensorFromDbus(asyncResp, sensorPath, subtree);
251962598e31SEd Tanous             BMCWEB_LOG_DEBUG("respHandler1 exit");
25202b73119cSGeorge Liu         });
2521e6bd846dSNan Zhou }
2522e6bd846dSNan Zhou 
2523bacb2162SNan Zhou } // namespace sensors
2524bacb2162SNan Zhou 
25257e860f15SJohn Edward Broadbent inline void requestRoutesSensorCollection(App& app)
252695a3ecadSAnthony Wilson {
25277e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/")
2528ed398213SEd Tanous         .privileges(redfish::privileges::getSensorCollection)
2529002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2530de167a6fSNan Zhou             std::bind_front(sensors::handleSensorCollectionGet, std::ref(app)));
253195a3ecadSAnthony Wilson }
253295a3ecadSAnthony Wilson 
25337e860f15SJohn Edward Broadbent inline void requestRoutesSensor(App& app)
253495a3ecadSAnthony Wilson {
25357e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/")
2536ed398213SEd Tanous         .privileges(redfish::privileges::getSensor)
2537002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2538e6bd846dSNan Zhou             std::bind_front(sensors::handleSensorGet, std::ref(app)));
253995a3ecadSAnthony Wilson }
254095a3ecadSAnthony Wilson 
254108777fb0SLewanczyk, Dawid } // namespace redfish
2542