xref: /openbmc/bmcweb/features/redfish/lib/sensors.hpp (revision f836c1d81313fad026197b36d32313480ada3a21)
108777fb0SLewanczyk, Dawid /*
208777fb0SLewanczyk, Dawid // Copyright (c) 2018 Intel Corporation
308777fb0SLewanczyk, Dawid //
408777fb0SLewanczyk, Dawid // Licensed under the Apache License, Version 2.0 (the "License");
508777fb0SLewanczyk, Dawid // you may not use this file except in compliance with the License.
608777fb0SLewanczyk, Dawid // You may obtain a copy of the License at
708777fb0SLewanczyk, Dawid //
808777fb0SLewanczyk, Dawid //      http://www.apache.org/licenses/LICENSE-2.0
908777fb0SLewanczyk, Dawid //
1008777fb0SLewanczyk, Dawid // Unless required by applicable law or agreed to in writing, software
1108777fb0SLewanczyk, Dawid // distributed under the License is distributed on an "AS IS" BASIS,
1208777fb0SLewanczyk, Dawid // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1308777fb0SLewanczyk, Dawid // See the License for the specific language governing permissions and
1408777fb0SLewanczyk, Dawid // limitations under the License.
1508777fb0SLewanczyk, Dawid */
1608777fb0SLewanczyk, Dawid #pragma once
1708777fb0SLewanczyk, Dawid 
183ccb3adbSEd Tanous #include "app.hpp"
193ccb3adbSEd Tanous #include "dbus_singleton.hpp"
207a1dbc48SGeorge Liu #include "dbus_utility.hpp"
21539d8c6bSEd Tanous #include "generated/enums/redundancy.hpp"
22aaf08ac7SMatt Simmering #include "generated/enums/resource.hpp"
233ccb3adbSEd Tanous #include "query.hpp"
243ccb3adbSEd Tanous #include "registries/privilege_registry.hpp"
2550ebd4afSEd Tanous #include "str_utility.hpp"
263ccb3adbSEd Tanous #include "utils/dbus_utils.hpp"
273ccb3adbSEd Tanous #include "utils/json_utils.hpp"
283ccb3adbSEd Tanous #include "utils/query_param.hpp"
291516c21bSJanet Adkins #include "utils/sensor_utils.hpp"
300ec8b83dSEd Tanous 
31e99073f5SGeorge Liu #include <boost/system/error_code.hpp>
32ef4c65b7SEd Tanous #include <boost/url/format.hpp>
331e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp>
3486d89ed7SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
351214b7e7SGunnar Mills 
367a1dbc48SGeorge Liu #include <array>
371214b7e7SGunnar Mills #include <cmath>
38fe04d49cSNan Zhou #include <iterator>
39283860f5SEd Tanous #include <limits>
40fe04d49cSNan Zhou #include <map>
413544d2a7SEd Tanous #include <ranges>
42fe04d49cSNan Zhou #include <set>
4318f8f608SEd Tanous #include <string>
447a1dbc48SGeorge Liu #include <string_view>
45b5a76932SEd Tanous #include <utility>
46abf2add6SEd Tanous #include <variant>
4708777fb0SLewanczyk, Dawid 
481abe55efSEd Tanous namespace redfish
491abe55efSEd Tanous {
5008777fb0SLewanczyk, Dawid 
51a0ec28b6SAdrian Ambrożewicz namespace sensors
52a0ec28b6SAdrian Ambrożewicz {
53a0ec28b6SAdrian Ambrożewicz 
5402da7c5aSEd Tanous // clang-format off
55a0ec28b6SAdrian Ambrożewicz namespace dbus
56a0ec28b6SAdrian Ambrożewicz {
57cf9e417dSEd Tanous constexpr auto powerPaths = std::to_array<std::string_view>({
5802da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/voltage",
5902da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/power"
6002da7c5aSEd Tanous });
61c2bf7f99SWludzik, Jozef 
6225b54dbaSEd Tanous constexpr auto getSensorPaths(){
6325b54dbaSEd Tanous     if constexpr(BMCWEB_REDFISH_NEW_POWERSUBSYSTEM_THERMALSUBSYSTEM){
6425b54dbaSEd Tanous     return std::to_array<std::string_view>({
6502da7c5aSEd Tanous         "/xyz/openbmc_project/sensors/power",
66a0ec28b6SAdrian Ambrożewicz         "/xyz/openbmc_project/sensors/current",
677088690cSBasheer Ahmed Muddebihal         "/xyz/openbmc_project/sensors/airflow",
685deabed9SGunnar Mills         "/xyz/openbmc_project/sensors/humidity",
69e8204933SGeorge Liu         "/xyz/openbmc_project/sensors/voltage",
70e8204933SGeorge Liu         "/xyz/openbmc_project/sensors/fan_tach",
71e8204933SGeorge Liu         "/xyz/openbmc_project/sensors/temperature",
72e8204933SGeorge Liu         "/xyz/openbmc_project/sensors/fan_pwm",
73e8204933SGeorge Liu         "/xyz/openbmc_project/sensors/altitude",
74e8204933SGeorge Liu         "/xyz/openbmc_project/sensors/energy",
7525b54dbaSEd Tanous         "/xyz/openbmc_project/sensors/utilization"});
7625b54dbaSEd Tanous     } else {
7725b54dbaSEd Tanous       return  std::to_array<std::string_view>({"/xyz/openbmc_project/sensors/power",
7825b54dbaSEd Tanous         "/xyz/openbmc_project/sensors/current",
7925b54dbaSEd Tanous         "/xyz/openbmc_project/sensors/airflow",
8025b54dbaSEd Tanous         "/xyz/openbmc_project/sensors/humidity",
8125b54dbaSEd Tanous         "/xyz/openbmc_project/sensors/utilization"});
8225b54dbaSEd Tanous }
8325b54dbaSEd Tanous }
8425b54dbaSEd Tanous 
8525b54dbaSEd Tanous constexpr auto sensorPaths = getSensorPaths();
8602da7c5aSEd Tanous 
87cf9e417dSEd Tanous constexpr auto thermalPaths = std::to_array<std::string_view>({
8802da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/fan_tach",
89a0ec28b6SAdrian Ambrożewicz     "/xyz/openbmc_project/sensors/temperature",
9002da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/fan_pwm"
9102da7c5aSEd Tanous });
9202da7c5aSEd Tanous 
93c2bf7f99SWludzik, Jozef } // namespace dbus
9402da7c5aSEd Tanous // clang-format on
9502da7c5aSEd Tanous 
960c728b42SJanet Adkins constexpr std::string_view powerNodeStr = sensor_utils::chassisSubNodeToString(
970c728b42SJanet Adkins     sensor_utils::ChassisSubNode::powerNode);
980c728b42SJanet Adkins constexpr std::string_view sensorsNodeStr =
990c728b42SJanet Adkins     sensor_utils::chassisSubNodeToString(
1000c728b42SJanet Adkins         sensor_utils::ChassisSubNode::sensorsNode);
1010c728b42SJanet Adkins constexpr std::string_view thermalNodeStr =
1020c728b42SJanet Adkins     sensor_utils::chassisSubNodeToString(
1030c728b42SJanet Adkins         sensor_utils::ChassisSubNode::thermalNode);
1040c728b42SJanet Adkins 
105cf9e417dSEd Tanous using sensorPair =
106cf9e417dSEd Tanous     std::pair<std::string_view, std::span<const std::string_view>>;
10702da7c5aSEd Tanous static constexpr std::array<sensorPair, 3> paths = {
1080c728b42SJanet Adkins     {{sensors::powerNodeStr, dbus::powerPaths},
1090c728b42SJanet Adkins      {sensors::sensorsNodeStr, dbus::sensorPaths},
1100c728b42SJanet Adkins      {sensors::thermalNodeStr, dbus::thermalPaths}}};
111c2bf7f99SWludzik, Jozef 
112a0ec28b6SAdrian Ambrożewicz } // namespace sensors
113a0ec28b6SAdrian Ambrożewicz 
11408777fb0SLewanczyk, Dawid /**
115588c3f0dSKowalski, Kamil  * SensorsAsyncResp
11608777fb0SLewanczyk, Dawid  * Gathers data needed for response processing after async calls are done
11708777fb0SLewanczyk, Dawid  */
1181abe55efSEd Tanous class SensorsAsyncResp
1191abe55efSEd Tanous {
12008777fb0SLewanczyk, Dawid   public:
121a0ec28b6SAdrian Ambrożewicz     using DataCompleteCb = std::function<void(
122a0ec28b6SAdrian Ambrożewicz         const boost::beast::http::status status,
123fe04d49cSNan Zhou         const std::map<std::string, std::string>& uriToDbus)>;
124a0ec28b6SAdrian Ambrożewicz 
125a0ec28b6SAdrian Ambrożewicz     struct SensorData
126a0ec28b6SAdrian Ambrożewicz     {
127*f836c1d8SEd Tanous         std::string name;
128a0ec28b6SAdrian Ambrożewicz         std::string uri;
129*f836c1d8SEd Tanous         std::string dbusPath;
130a0ec28b6SAdrian Ambrożewicz     };
131a0ec28b6SAdrian Ambrożewicz 
1328a592810SEd Tanous     SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
1338d1b46d7Szhanghch05                      const std::string& chassisIdIn,
134cf9e417dSEd Tanous                      std::span<const std::string_view> typesIn,
13502da7c5aSEd Tanous                      std::string_view subNode) :
136bd79bce8SPatrick Williams         asyncResp(asyncRespIn), chassisId(chassisIdIn), types(typesIn),
137bd79bce8SPatrick Williams         chassisSubNode(subNode), efficientExpand(false)
1381214b7e7SGunnar Mills     {}
13908777fb0SLewanczyk, Dawid 
140a0ec28b6SAdrian Ambrożewicz     // Store extra data about sensor mapping and return it in callback
1418a592810SEd Tanous     SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
1428d1b46d7Szhanghch05                      const std::string& chassisIdIn,
143cf9e417dSEd Tanous                      std::span<const std::string_view> typesIn,
14402da7c5aSEd Tanous                      std::string_view subNode,
145a0ec28b6SAdrian Ambrożewicz                      DataCompleteCb&& creationComplete) :
146bd79bce8SPatrick Williams         asyncResp(asyncRespIn), chassisId(chassisIdIn), types(typesIn),
147bd79bce8SPatrick Williams         chassisSubNode(subNode), efficientExpand(false),
148bd79bce8SPatrick Williams         metadata{std::vector<SensorData>()},
149a0ec28b6SAdrian Ambrożewicz         dataComplete{std::move(creationComplete)}
150a0ec28b6SAdrian Ambrożewicz     {}
151a0ec28b6SAdrian Ambrożewicz 
152928fefb9SNan Zhou     // sensor collections expand
1538a592810SEd Tanous     SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
154928fefb9SNan Zhou                      const std::string& chassisIdIn,
155cf9e417dSEd Tanous                      std::span<const std::string_view> typesIn,
1568a592810SEd Tanous                      const std::string_view& subNode, bool efficientExpandIn) :
157bd79bce8SPatrick Williams         asyncResp(asyncRespIn), chassisId(chassisIdIn), types(typesIn),
158bd79bce8SPatrick Williams         chassisSubNode(subNode), efficientExpand(efficientExpandIn)
159928fefb9SNan Zhou     {}
160928fefb9SNan Zhou 
1611abe55efSEd Tanous     ~SensorsAsyncResp()
1621abe55efSEd Tanous     {
1638d1b46d7Szhanghch05         if (asyncResp->res.result() ==
1648d1b46d7Szhanghch05             boost::beast::http::status::internal_server_error)
1651abe55efSEd Tanous         {
1661abe55efSEd Tanous             // Reset the json object to clear out any data that made it in
1671abe55efSEd Tanous             // before the error happened todo(ed) handle error condition with
1681abe55efSEd Tanous             // proper code
1698d1b46d7Szhanghch05             asyncResp->res.jsonValue = nlohmann::json::object();
17008777fb0SLewanczyk, Dawid         }
171a0ec28b6SAdrian Ambrożewicz 
172a0ec28b6SAdrian Ambrożewicz         if (dataComplete && metadata)
173a0ec28b6SAdrian Ambrożewicz         {
174fe04d49cSNan Zhou             std::map<std::string, std::string> map;
1758d1b46d7Szhanghch05             if (asyncResp->res.result() == boost::beast::http::status::ok)
176a0ec28b6SAdrian Ambrożewicz             {
177a0ec28b6SAdrian Ambrożewicz                 for (auto& sensor : *metadata)
178a0ec28b6SAdrian Ambrożewicz                 {
179c1d019a6SEd Tanous                     map.emplace(sensor.uri, sensor.dbusPath);
180a0ec28b6SAdrian Ambrożewicz                 }
181a0ec28b6SAdrian Ambrożewicz             }
1828d1b46d7Szhanghch05             dataComplete(asyncResp->res.result(), map);
183a0ec28b6SAdrian Ambrożewicz         }
18408777fb0SLewanczyk, Dawid     }
185588c3f0dSKowalski, Kamil 
186ecd6a3a2SEd Tanous     SensorsAsyncResp(const SensorsAsyncResp&) = delete;
187ecd6a3a2SEd Tanous     SensorsAsyncResp(SensorsAsyncResp&&) = delete;
188ecd6a3a2SEd Tanous     SensorsAsyncResp& operator=(const SensorsAsyncResp&) = delete;
189ecd6a3a2SEd Tanous     SensorsAsyncResp& operator=(SensorsAsyncResp&&) = delete;
190ecd6a3a2SEd Tanous 
191a0ec28b6SAdrian Ambrożewicz     void addMetadata(const nlohmann::json& sensorObject,
192c1d019a6SEd Tanous                      const std::string& dbusPath)
193a0ec28b6SAdrian Ambrożewicz     {
194a0ec28b6SAdrian Ambrożewicz         if (metadata)
195a0ec28b6SAdrian Ambrożewicz         {
196c1d019a6SEd Tanous             metadata->emplace_back(SensorData{
197c1d019a6SEd Tanous                 sensorObject["Name"], sensorObject["@odata.id"], dbusPath});
198a0ec28b6SAdrian Ambrożewicz         }
199a0ec28b6SAdrian Ambrożewicz     }
200a0ec28b6SAdrian Ambrożewicz 
201a0ec28b6SAdrian Ambrożewicz     void updateUri(const std::string& name, const std::string& uri)
202a0ec28b6SAdrian Ambrożewicz     {
203a0ec28b6SAdrian Ambrożewicz         if (metadata)
204a0ec28b6SAdrian Ambrożewicz         {
205a0ec28b6SAdrian Ambrożewicz             for (auto& sensor : *metadata)
206a0ec28b6SAdrian Ambrożewicz             {
207a0ec28b6SAdrian Ambrożewicz                 if (sensor.name == name)
208a0ec28b6SAdrian Ambrożewicz                 {
209a0ec28b6SAdrian Ambrożewicz                     sensor.uri = uri;
210a0ec28b6SAdrian Ambrożewicz                 }
211a0ec28b6SAdrian Ambrożewicz             }
212a0ec28b6SAdrian Ambrożewicz         }
213a0ec28b6SAdrian Ambrożewicz     }
214a0ec28b6SAdrian Ambrożewicz 
2158d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp> asyncResp;
216a0ec28b6SAdrian Ambrożewicz     const std::string chassisId;
217cf9e417dSEd Tanous     const std::span<const std::string_view> types;
218a0ec28b6SAdrian Ambrożewicz     const std::string chassisSubNode;
219928fefb9SNan Zhou     const bool efficientExpand;
220a0ec28b6SAdrian Ambrożewicz 
221a0ec28b6SAdrian Ambrożewicz   private:
222a0ec28b6SAdrian Ambrożewicz     std::optional<std::vector<SensorData>> metadata;
223a0ec28b6SAdrian Ambrożewicz     DataCompleteCb dataComplete;
22408777fb0SLewanczyk, Dawid };
22508777fb0SLewanczyk, Dawid 
226c9563608SJanet Adkins using InventoryItem = sensor_utils::InventoryItem;
227adc4f0dbSShawn McCarney 
228adc4f0dbSShawn McCarney /**
229413961deSRichard Marian Thomaiyar  * @brief Get objects with connection necessary for sensors
230588c3f0dSKowalski, Kamil  * @param SensorsAsyncResp Pointer to object holding response data
23108777fb0SLewanczyk, Dawid  * @param sensorNames Sensors retrieved from chassis
23208777fb0SLewanczyk, Dawid  * @param callback Callback for processing gathered connections
23308777fb0SLewanczyk, Dawid  */
23408777fb0SLewanczyk, Dawid template <typename Callback>
235413961deSRichard Marian Thomaiyar void getObjectsWithConnection(
23681ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
237fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& sensorNames,
2381abe55efSEd Tanous     Callback&& callback)
2391abe55efSEd Tanous {
24062598e31SEd Tanous     BMCWEB_LOG_DEBUG("getObjectsWithConnection enter");
24103b5bae3SJames Feist     const std::string path = "/xyz/openbmc_project/sensors";
242e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
24308777fb0SLewanczyk, Dawid         "xyz.openbmc_project.Sensor.Value"};
24408777fb0SLewanczyk, Dawid 
245e99073f5SGeorge Liu     // Make call to ObjectMapper to find all sensors objects
246e99073f5SGeorge Liu     dbus::utility::getSubTree(
247e99073f5SGeorge Liu         path, 2, interfaces,
2488cb2c024SEd Tanous         [callback = std::forward<Callback>(callback), sensorsAsyncResp,
249e99073f5SGeorge Liu          sensorNames](const boost::system::error_code& ec,
250002d39b4SEd Tanous                       const dbus::utility::MapperGetSubTreeResponse& subtree) {
251e99073f5SGeorge Liu             // Response handler for parsing objects subtree
25262598e31SEd Tanous             BMCWEB_LOG_DEBUG("getObjectsWithConnection resp_handler enter");
2531abe55efSEd Tanous             if (ec)
2541abe55efSEd Tanous             {
2558d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
25662598e31SEd Tanous                 BMCWEB_LOG_ERROR(
25762598e31SEd Tanous                     "getObjectsWithConnection resp_handler: Dbus error {}", ec);
25808777fb0SLewanczyk, Dawid                 return;
25908777fb0SLewanczyk, Dawid             }
26008777fb0SLewanczyk, Dawid 
26162598e31SEd Tanous             BMCWEB_LOG_DEBUG("Found {} subtrees", subtree.size());
26208777fb0SLewanczyk, Dawid 
263bd79bce8SPatrick Williams             // Make unique list of connections only for requested sensor types
264bd79bce8SPatrick Williams             // and found in the chassis
265fe04d49cSNan Zhou             std::set<std::string> connections;
266413961deSRichard Marian Thomaiyar             std::set<std::pair<std::string, std::string>> objectsWithConnection;
26708777fb0SLewanczyk, Dawid 
26862598e31SEd Tanous             BMCWEB_LOG_DEBUG("sensorNames list count: {}", sensorNames->size());
26949c53ac9SJohnathan Mantey             for (const std::string& tsensor : *sensorNames)
2701abe55efSEd Tanous             {
27162598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Sensor to find: {}", tsensor);
27208777fb0SLewanczyk, Dawid             }
27308777fb0SLewanczyk, Dawid 
274bd79bce8SPatrick Williams             for (const std::pair<std::string,
275bd79bce8SPatrick Williams                                  std::vector<std::pair<
276bd79bce8SPatrick Williams                                      std::string, std::vector<std::string>>>>&
2771abe55efSEd Tanous                      object : subtree)
2781abe55efSEd Tanous             {
27949c53ac9SJohnathan Mantey                 if (sensorNames->find(object.first) != sensorNames->end())
2801abe55efSEd Tanous                 {
28149c53ac9SJohnathan Mantey                     for (const std::pair<std::string, std::vector<std::string>>&
2821abe55efSEd Tanous                              objData : object.second)
2831abe55efSEd Tanous                     {
284bd79bce8SPatrick Williams                         BMCWEB_LOG_DEBUG("Adding connection: {}",
285bd79bce8SPatrick Williams                                          objData.first);
28608777fb0SLewanczyk, Dawid                         connections.insert(objData.first);
287de629b6eSShawn McCarney                         objectsWithConnection.insert(
288de629b6eSShawn McCarney                             std::make_pair(object.first, objData.first));
28908777fb0SLewanczyk, Dawid                     }
29008777fb0SLewanczyk, Dawid                 }
29108777fb0SLewanczyk, Dawid             }
29262598e31SEd Tanous             BMCWEB_LOG_DEBUG("Found {} connections", connections.size());
293413961deSRichard Marian Thomaiyar             callback(std::move(connections), std::move(objectsWithConnection));
29462598e31SEd Tanous             BMCWEB_LOG_DEBUG("getObjectsWithConnection resp_handler exit");
295e99073f5SGeorge Liu         });
29662598e31SEd Tanous     BMCWEB_LOG_DEBUG("getObjectsWithConnection exit");
297413961deSRichard Marian Thomaiyar }
298413961deSRichard Marian Thomaiyar 
299413961deSRichard Marian Thomaiyar /**
300413961deSRichard Marian Thomaiyar  * @brief Create connections necessary for sensors
301413961deSRichard Marian Thomaiyar  * @param SensorsAsyncResp Pointer to object holding response data
302413961deSRichard Marian Thomaiyar  * @param sensorNames Sensors retrieved from chassis
303413961deSRichard Marian Thomaiyar  * @param callback Callback for processing gathered connections
304413961deSRichard Marian Thomaiyar  */
305413961deSRichard Marian Thomaiyar template <typename Callback>
306fe04d49cSNan Zhou void getConnections(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
307fe04d49cSNan Zhou                     const std::shared_ptr<std::set<std::string>> sensorNames,
308413961deSRichard Marian Thomaiyar                     Callback&& callback)
309413961deSRichard Marian Thomaiyar {
310413961deSRichard Marian Thomaiyar     auto objectsWithConnectionCb =
3118cb2c024SEd Tanous         [callback = std::forward<Callback>(callback)](
3128cb2c024SEd Tanous             const std::set<std::string>& connections,
313413961deSRichard Marian Thomaiyar             const std::set<std::pair<std::string, std::string>>&
3143174e4dfSEd Tanous             /*objectsWithConnection*/) { callback(connections); };
31581ce609eSEd Tanous     getObjectsWithConnection(sensorsAsyncResp, sensorNames,
316413961deSRichard Marian Thomaiyar                              std::move(objectsWithConnectionCb));
31708777fb0SLewanczyk, Dawid }
31808777fb0SLewanczyk, Dawid 
31908777fb0SLewanczyk, Dawid /**
32049c53ac9SJohnathan Mantey  * @brief Shrinks the list of sensors for processing
32149c53ac9SJohnathan Mantey  * @param SensorsAysncResp  The class holding the Redfish response
32249c53ac9SJohnathan Mantey  * @param allSensors  A list of all the sensors associated to the
32349c53ac9SJohnathan Mantey  * chassis element (i.e. baseboard, front panel, etc...)
32449c53ac9SJohnathan Mantey  * @param activeSensors A list that is a reduction of the incoming
32549c53ac9SJohnathan Mantey  * allSensors list.  Eliminate Thermal sensors when a Power request is
32649c53ac9SJohnathan Mantey  * made, and eliminate Power sensors when a Thermal request is made.
32749c53ac9SJohnathan Mantey  */
32823a21a1cSEd Tanous inline void reduceSensorList(
3297f1cc26dSEd Tanous     crow::Response& res, std::string_view chassisSubNode,
330cf9e417dSEd Tanous     std::span<const std::string_view> sensorTypes,
33149c53ac9SJohnathan Mantey     const std::vector<std::string>* allSensors,
332fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& activeSensors)
33349c53ac9SJohnathan Mantey {
33449c53ac9SJohnathan Mantey     if ((allSensors == nullptr) || (activeSensors == nullptr))
33549c53ac9SJohnathan Mantey     {
3367f1cc26dSEd Tanous         messages::resourceNotFound(res, chassisSubNode,
3370c728b42SJanet Adkins                                    chassisSubNode == sensors::thermalNodeStr
338a0ec28b6SAdrian Ambrożewicz                                        ? "Temperatures"
33949c53ac9SJohnathan Mantey                                        : "Voltages");
34049c53ac9SJohnathan Mantey 
34149c53ac9SJohnathan Mantey         return;
34249c53ac9SJohnathan Mantey     }
34349c53ac9SJohnathan Mantey     if (allSensors->empty())
34449c53ac9SJohnathan Mantey     {
34549c53ac9SJohnathan Mantey         // Nothing to do, the activeSensors object is also empty
34649c53ac9SJohnathan Mantey         return;
34749c53ac9SJohnathan Mantey     }
34849c53ac9SJohnathan Mantey 
3497f1cc26dSEd Tanous     for (std::string_view type : sensorTypes)
35049c53ac9SJohnathan Mantey     {
35149c53ac9SJohnathan Mantey         for (const std::string& sensor : *allSensors)
35249c53ac9SJohnathan Mantey         {
35311ba3979SEd Tanous             if (sensor.starts_with(type))
35449c53ac9SJohnathan Mantey             {
35549c53ac9SJohnathan Mantey                 activeSensors->emplace(sensor);
35649c53ac9SJohnathan Mantey             }
35749c53ac9SJohnathan Mantey         }
35849c53ac9SJohnathan Mantey     }
35949c53ac9SJohnathan Mantey }
36049c53ac9SJohnathan Mantey 
3617f1cc26dSEd Tanous /*
3627f1cc26dSEd Tanous  *Populates the top level collection for a given subnode.  Populates
3637f1cc26dSEd Tanous  *SensorCollection, Power, or Thermal schemas.
3647f1cc26dSEd Tanous  *
3657f1cc26dSEd Tanous  * */
3667f1cc26dSEd Tanous inline void populateChassisNode(nlohmann::json& jsonValue,
3677f1cc26dSEd Tanous                                 std::string_view chassisSubNode)
3687f1cc26dSEd Tanous {
3690c728b42SJanet Adkins     if (chassisSubNode == sensors::powerNodeStr)
3707f1cc26dSEd Tanous     {
3717f1cc26dSEd Tanous         jsonValue["@odata.type"] = "#Power.v1_5_2.Power";
3727f1cc26dSEd Tanous     }
3730c728b42SJanet Adkins     else if (chassisSubNode == sensors::thermalNodeStr)
3747f1cc26dSEd Tanous     {
3757f1cc26dSEd Tanous         jsonValue["@odata.type"] = "#Thermal.v1_4_0.Thermal";
3767f1cc26dSEd Tanous         jsonValue["Fans"] = nlohmann::json::array();
3777f1cc26dSEd Tanous         jsonValue["Temperatures"] = nlohmann::json::array();
3787f1cc26dSEd Tanous     }
3790c728b42SJanet Adkins     else if (chassisSubNode == sensors::sensorsNodeStr)
3807f1cc26dSEd Tanous     {
3817f1cc26dSEd Tanous         jsonValue["@odata.type"] = "#SensorCollection.SensorCollection";
3827f1cc26dSEd Tanous         jsonValue["Description"] = "Collection of Sensors for this Chassis";
3837f1cc26dSEd Tanous         jsonValue["Members"] = nlohmann::json::array();
3847f1cc26dSEd Tanous         jsonValue["Members@odata.count"] = 0;
3857f1cc26dSEd Tanous     }
3867f1cc26dSEd Tanous 
3870c728b42SJanet Adkins     if (chassisSubNode != sensors::sensorsNodeStr)
3887f1cc26dSEd Tanous     {
3897f1cc26dSEd Tanous         jsonValue["Id"] = chassisSubNode;
3907f1cc26dSEd Tanous     }
3917f1cc26dSEd Tanous     jsonValue["Name"] = chassisSubNode;
3927f1cc26dSEd Tanous }
3937f1cc26dSEd Tanous 
39449c53ac9SJohnathan Mantey /**
39508777fb0SLewanczyk, Dawid  * @brief Retrieves requested chassis sensors and redundancy data from DBus .
396588c3f0dSKowalski, Kamil  * @param SensorsAsyncResp   Pointer to object holding response data
39708777fb0SLewanczyk, Dawid  * @param callback  Callback for next step in gathered sensor processing
39808777fb0SLewanczyk, Dawid  */
39908777fb0SLewanczyk, Dawid template <typename Callback>
4007f1cc26dSEd Tanous void getChassis(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
4017f1cc26dSEd Tanous                 std::string_view chassisId, std::string_view chassisSubNode,
402cf9e417dSEd Tanous                 std::span<const std::string_view> sensorTypes,
403cf9e417dSEd Tanous                 Callback&& callback)
4041abe55efSEd Tanous {
40562598e31SEd Tanous     BMCWEB_LOG_DEBUG("getChassis enter");
4067a1dbc48SGeorge Liu     constexpr std::array<std::string_view, 2> interfaces = {
40749c53ac9SJohnathan Mantey         "xyz.openbmc_project.Inventory.Item.Board",
408adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Item.Chassis"};
4097a1dbc48SGeorge Liu 
4107a1dbc48SGeorge Liu     // Get the Chassis Collection
4117a1dbc48SGeorge Liu     dbus::utility::getSubTreePaths(
4127a1dbc48SGeorge Liu         "/xyz/openbmc_project/inventory", 0, interfaces,
4138cb2c024SEd Tanous         [callback = std::forward<Callback>(callback), asyncResp,
4147f1cc26dSEd Tanous          chassisIdStr{std::string(chassisId)},
4154e0d8789SEd Tanous          chassisSubNode{std::string(chassisSubNode)},
4164e0d8789SEd Tanous          sensorTypes](const boost::system::error_code& ec,
4174e0d8789SEd Tanous                       const dbus::utility::MapperGetSubTreePathsResponse&
4184e0d8789SEd Tanous                           chassisPaths) mutable {
41962598e31SEd Tanous             BMCWEB_LOG_DEBUG("getChassis respHandler enter");
4201abe55efSEd Tanous             if (ec)
4211abe55efSEd Tanous             {
42262598e31SEd Tanous                 BMCWEB_LOG_ERROR("getChassis respHandler DBUS error: {}", ec);
4237f1cc26dSEd Tanous                 messages::internalError(asyncResp->res);
42408777fb0SLewanczyk, Dawid                 return;
42508777fb0SLewanczyk, Dawid             }
42649c53ac9SJohnathan Mantey             const std::string* chassisPath = nullptr;
42749c53ac9SJohnathan Mantey             for (const std::string& chassis : chassisPaths)
4281abe55efSEd Tanous             {
42928aa8de5SGeorge Liu                 sdbusplus::message::object_path path(chassis);
430f8fe53e7SEd Tanous                 std::string chassisName = path.filename();
43128aa8de5SGeorge Liu                 if (chassisName.empty())
4321abe55efSEd Tanous                 {
43362598e31SEd Tanous                     BMCWEB_LOG_ERROR("Failed to find '/' in {}", chassis);
434daf36e2eSEd Tanous                     continue;
435daf36e2eSEd Tanous                 }
4367f1cc26dSEd Tanous                 if (chassisName == chassisIdStr)
4371abe55efSEd Tanous                 {
43849c53ac9SJohnathan Mantey                     chassisPath = &chassis;
43949c53ac9SJohnathan Mantey                     break;
440daf36e2eSEd Tanous                 }
44149c53ac9SJohnathan Mantey             }
44249c53ac9SJohnathan Mantey             if (chassisPath == nullptr)
4431abe55efSEd Tanous             {
444bd79bce8SPatrick Williams                 messages::resourceNotFound(asyncResp->res, "Chassis",
445bd79bce8SPatrick Williams                                            chassisIdStr);
44649c53ac9SJohnathan Mantey                 return;
4471abe55efSEd Tanous             }
4487f1cc26dSEd Tanous             populateChassisNode(asyncResp->res.jsonValue, chassisSubNode);
44908777fb0SLewanczyk, Dawid 
450ef4c65b7SEd Tanous             asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
451ef4c65b7SEd Tanous                 "/redfish/v1/Chassis/{}/{}", chassisIdStr, chassisSubNode);
45295a3ecadSAnthony Wilson 
4538fb49dd6SShawn McCarney             // Get the list of all sensors for this Chassis element
4548fb49dd6SShawn McCarney             std::string sensorPath = *chassisPath + "/all_sensors";
4556c3e9451SGeorge Liu             dbus::utility::getAssociationEndPoints(
4564e0d8789SEd Tanous                 sensorPath, [asyncResp, chassisSubNode, sensorTypes,
4574e0d8789SEd Tanous                              callback = std::forward<Callback>(callback)](
4588b24275dSEd Tanous                                 const boost::system::error_code& ec2,
4594e0d8789SEd Tanous                                 const dbus::utility::MapperEndPoints&
4604e0d8789SEd Tanous                                     nodeSensorList) mutable {
4618b24275dSEd Tanous                     if (ec2)
46249c53ac9SJohnathan Mantey                     {
4638b24275dSEd Tanous                         if (ec2.value() != EBADR)
46449c53ac9SJohnathan Mantey                         {
4657f1cc26dSEd Tanous                             messages::internalError(asyncResp->res);
46649c53ac9SJohnathan Mantey                             return;
46749c53ac9SJohnathan Mantey                         }
46849c53ac9SJohnathan Mantey                     }
469bd79bce8SPatrick Williams                     const std::shared_ptr<std::set<std::string>>
470bd79bce8SPatrick Williams                         culledSensorList =
471fe04d49cSNan Zhou                             std::make_shared<std::set<std::string>>();
472bd79bce8SPatrick Williams                     reduceSensorList(asyncResp->res, chassisSubNode,
473bd79bce8SPatrick Williams                                      sensorTypes, &nodeSensorList,
474bd79bce8SPatrick Williams                                      culledSensorList);
475bd79bce8SPatrick Williams                     BMCWEB_LOG_DEBUG("Finishing with {}",
476bd79bce8SPatrick Williams                                      culledSensorList->size());
47749c53ac9SJohnathan Mantey                     callback(culledSensorList);
4781e1e598dSJonathan Doman                 });
4797a1dbc48SGeorge Liu         });
48062598e31SEd Tanous     BMCWEB_LOG_DEBUG("getChassis exit");
48108777fb0SLewanczyk, Dawid }
48208777fb0SLewanczyk, Dawid 
48308777fb0SLewanczyk, Dawid /**
4841d7c0054SEd Tanous  * @brief Builds a json sensor representation of a sensor.
4851d7c0054SEd Tanous  * @param sensorName  The name of the sensor to be built
4861d7c0054SEd Tanous  * @param sensorType  The type (temperature, fan_tach, etc) of the sensor to
4871d7c0054SEd Tanous  * build
4888ece0e45SEd Tanous  * @param chassisSubNode The subnode (thermal, sensor, etc) of the sensor
4891d7c0054SEd Tanous  * @param interfacesDict  A dictionary of the interfaces and properties of said
4901d7c0054SEd Tanous  * interfaces to be built from
4911d7c0054SEd Tanous  * @param sensorJson  The json object to fill
4921d7c0054SEd Tanous  * @param inventoryItem D-Bus inventory item associated with the sensor.  Will
4931d7c0054SEd Tanous  * be nullptr if no associated inventory item was found.
4941d7c0054SEd Tanous  */
4951d7c0054SEd Tanous inline void objectInterfacesToJson(
4961d7c0054SEd Tanous     const std::string& sensorName, const std::string& sensorType,
4970c728b42SJanet Adkins     const sensor_utils::ChassisSubNode chassisSubNode,
49880f79a40SMichael Shen     const dbus::utility::DBusInterfacesMap& interfacesDict,
4991d7c0054SEd Tanous     nlohmann::json& sensorJson, InventoryItem* inventoryItem)
5001d7c0054SEd Tanous {
5011d7c0054SEd Tanous     for (const auto& [interface, valuesDict] : interfacesDict)
5021d7c0054SEd Tanous     {
503c9563608SJanet Adkins         sensor_utils::objectPropertiesToJson(
504c9563608SJanet Adkins             sensorName, sensorType, chassisSubNode, valuesDict, sensorJson,
505c9563608SJanet Adkins             inventoryItem);
5061d7c0054SEd Tanous     }
50762598e31SEd Tanous     BMCWEB_LOG_DEBUG("Added sensor {}", sensorName);
5081d7c0054SEd Tanous }
5091d7c0054SEd Tanous 
510b5a76932SEd Tanous inline void populateFanRedundancy(
511b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
5128bd25ccdSJames Feist {
513e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
514e99073f5SGeorge Liu         "xyz.openbmc_project.Control.FanRedundancy"};
515e99073f5SGeorge Liu     dbus::utility::getSubTree(
516e99073f5SGeorge Liu         "/xyz/openbmc_project/control", 2, interfaces,
517b9d36b47SEd Tanous         [sensorsAsyncResp](
518e99073f5SGeorge Liu             const boost::system::error_code& ec,
519b9d36b47SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& resp) {
5208bd25ccdSJames Feist             if (ec)
5218bd25ccdSJames Feist             {
5228bd25ccdSJames Feist                 return; // don't have to have this interface
5238bd25ccdSJames Feist             }
5246c3e9451SGeorge Liu             for (const std::pair<std::string, dbus::utility::MapperServiceMap>&
525e278c18fSEd Tanous                      pathPair : resp)
5268bd25ccdSJames Feist             {
527e278c18fSEd Tanous                 const std::string& path = pathPair.first;
528bd79bce8SPatrick Williams                 const dbus::utility::MapperServiceMap& objDict =
529bd79bce8SPatrick Williams                     pathPair.second;
5308bd25ccdSJames Feist                 if (objDict.empty())
5318bd25ccdSJames Feist                 {
5328bd25ccdSJames Feist                     continue; // this should be impossible
5338bd25ccdSJames Feist                 }
5348bd25ccdSJames Feist 
5358bd25ccdSJames Feist                 const std::string& owner = objDict.begin()->first;
5366c3e9451SGeorge Liu                 dbus::utility::getAssociationEndPoints(
5376c3e9451SGeorge Liu                     path + "/chassis",
5386c3e9451SGeorge Liu                     [path, owner, sensorsAsyncResp](
5398b24275dSEd Tanous                         const boost::system::error_code& ec2,
5406c3e9451SGeorge Liu                         const dbus::utility::MapperEndPoints& endpoints) {
5418b24275dSEd Tanous                         if (ec2)
5428bd25ccdSJames Feist                         {
5438bd25ccdSJames Feist                             return; // if they don't have an association we
5448bd25ccdSJames Feist                                     // can't tell what chassis is
5458bd25ccdSJames Feist                         }
5463544d2a7SEd Tanous                         auto found = std::ranges::find_if(
547bd79bce8SPatrick Williams                             endpoints,
548bd79bce8SPatrick Williams                             [sensorsAsyncResp](const std::string& entry) {
549bd79bce8SPatrick Williams                                 return entry.find(
550bd79bce8SPatrick Williams                                            sensorsAsyncResp->chassisId) !=
5518bd25ccdSJames Feist                                        std::string::npos;
5528bd25ccdSJames Feist                             });
5538bd25ccdSJames Feist 
5541e1e598dSJonathan Doman                         if (found == endpoints.end())
5558bd25ccdSJames Feist                         {
5568bd25ccdSJames Feist                             return;
5578bd25ccdSJames Feist                         }
55886d89ed7SKrzysztof Grobelny                         sdbusplus::asio::getAllProperties(
55986d89ed7SKrzysztof Grobelny                             *crow::connections::systemBus, owner, path,
56086d89ed7SKrzysztof Grobelny                             "xyz.openbmc_project.Control.FanRedundancy",
5618bd25ccdSJames Feist                             [path, sensorsAsyncResp](
5628b24275dSEd Tanous                                 const boost::system::error_code& ec3,
56386d89ed7SKrzysztof Grobelny                                 const dbus::utility::DBusPropertiesMap& ret) {
5648b24275dSEd Tanous                                 if (ec3)
5658bd25ccdSJames Feist                                 {
5668bd25ccdSJames Feist                                     return; // don't have to have this
5678bd25ccdSJames Feist                                             // interface
5688bd25ccdSJames Feist                                 }
5698bd25ccdSJames Feist 
57086d89ed7SKrzysztof Grobelny                                 const uint8_t* allowedFailures = nullptr;
571bd79bce8SPatrick Williams                                 const std::vector<std::string>* collection =
572bd79bce8SPatrick Williams                                     nullptr;
57386d89ed7SKrzysztof Grobelny                                 const std::string* status = nullptr;
57486d89ed7SKrzysztof Grobelny 
575bd79bce8SPatrick Williams                                 const bool success =
576bd79bce8SPatrick Williams                                     sdbusplus::unpackPropertiesNoThrow(
57786d89ed7SKrzysztof Grobelny                                         dbus_utils::UnpackErrorPrinter(), ret,
578bd79bce8SPatrick Williams                                         "AllowedFailures", allowedFailures,
579bd79bce8SPatrick Williams                                         "Collection", collection, "Status",
580bd79bce8SPatrick Williams                                         status);
58186d89ed7SKrzysztof Grobelny 
58286d89ed7SKrzysztof Grobelny                                 if (!success)
58386d89ed7SKrzysztof Grobelny                                 {
58486d89ed7SKrzysztof Grobelny                                     messages::internalError(
58586d89ed7SKrzysztof Grobelny                                         sensorsAsyncResp->asyncResp->res);
58686d89ed7SKrzysztof Grobelny                                     return;
58786d89ed7SKrzysztof Grobelny                                 }
58886d89ed7SKrzysztof Grobelny 
589bd79bce8SPatrick Williams                                 if (allowedFailures == nullptr ||
590bd79bce8SPatrick Williams                                     collection == nullptr || status == nullptr)
5918bd25ccdSJames Feist                                 {
592bd79bce8SPatrick Williams                                     BMCWEB_LOG_ERROR(
593bd79bce8SPatrick Williams                                         "Invalid redundancy interface");
5948bd25ccdSJames Feist                                     messages::internalError(
5958d1b46d7Szhanghch05                                         sensorsAsyncResp->asyncResp->res);
5968bd25ccdSJames Feist                                     return;
5978bd25ccdSJames Feist                                 }
5988bd25ccdSJames Feist 
599bd79bce8SPatrick Williams                                 sdbusplus::message::object_path objectPath(
600bd79bce8SPatrick Williams                                     path);
60128aa8de5SGeorge Liu                                 std::string name = objectPath.filename();
60228aa8de5SGeorge Liu                                 if (name.empty())
6038bd25ccdSJames Feist                                 {
6048bd25ccdSJames Feist                                     // this should be impossible
6058bd25ccdSJames Feist                                     messages::internalError(
6068d1b46d7Szhanghch05                                         sensorsAsyncResp->asyncResp->res);
6078bd25ccdSJames Feist                                     return;
6088bd25ccdSJames Feist                                 }
60918f8f608SEd Tanous                                 std::ranges::replace(name, '_', ' ');
6108bd25ccdSJames Feist 
6118bd25ccdSJames Feist                                 std::string health;
6128bd25ccdSJames Feist 
61311ba3979SEd Tanous                                 if (status->ends_with("Full"))
6148bd25ccdSJames Feist                                 {
6158bd25ccdSJames Feist                                     health = "OK";
6168bd25ccdSJames Feist                                 }
61711ba3979SEd Tanous                                 else if (status->ends_with("Degraded"))
6188bd25ccdSJames Feist                                 {
6198bd25ccdSJames Feist                                     health = "Warning";
6208bd25ccdSJames Feist                                 }
6218bd25ccdSJames Feist                                 else
6228bd25ccdSJames Feist                                 {
6238bd25ccdSJames Feist                                     health = "Critical";
6248bd25ccdSJames Feist                                 }
6251476687dSEd Tanous                                 nlohmann::json::array_t redfishCollection;
6268bd25ccdSJames Feist                                 const auto& fanRedfish =
627bd79bce8SPatrick Williams                                     sensorsAsyncResp->asyncResp->res
628bd79bce8SPatrick Williams                                         .jsonValue["Fans"];
6298bd25ccdSJames Feist                                 for (const std::string& item : *collection)
6308bd25ccdSJames Feist                                 {
631bd79bce8SPatrick Williams                                     sdbusplus::message::object_path itemPath(
632bd79bce8SPatrick Williams                                         item);
6338a592810SEd Tanous                                     std::string itemName = itemPath.filename();
63428aa8de5SGeorge Liu                                     if (itemName.empty())
63528aa8de5SGeorge Liu                                     {
63628aa8de5SGeorge Liu                                         continue;
63728aa8de5SGeorge Liu                                     }
6388bd25ccdSJames Feist                                     /*
6398bd25ccdSJames Feist                                     todo(ed): merge patch that fixes the names
6408bd25ccdSJames Feist                                     std::replace(itemName.begin(),
6418bd25ccdSJames Feist                                                  itemName.end(), '_', ' ');*/
6423544d2a7SEd Tanous                                     auto schemaItem = std::ranges::find_if(
643bd79bce8SPatrick Williams                                         fanRedfish,
644bd79bce8SPatrick Williams                                         [itemName](const nlohmann::json& fan) {
6453e35c761SGeorge Liu                                             return fan["Name"] == itemName;
6468bd25ccdSJames Feist                                         });
6478bd25ccdSJames Feist                                     if (schemaItem != fanRedfish.end())
6488bd25ccdSJames Feist                                     {
6498a592810SEd Tanous                                         nlohmann::json::object_t collectionId;
6508a592810SEd Tanous                                         collectionId["@odata.id"] =
6511476687dSEd Tanous                                             (*schemaItem)["@odata.id"];
6521476687dSEd Tanous                                         redfishCollection.emplace_back(
6538a592810SEd Tanous                                             std::move(collectionId));
6548bd25ccdSJames Feist                                     }
6558bd25ccdSJames Feist                                     else
6568bd25ccdSJames Feist                                     {
657bd79bce8SPatrick Williams                                         BMCWEB_LOG_ERROR(
658bd79bce8SPatrick Williams                                             "failed to find fan in schema");
6598bd25ccdSJames Feist                                         messages::internalError(
6608d1b46d7Szhanghch05                                             sensorsAsyncResp->asyncResp->res);
6618bd25ccdSJames Feist                                         return;
6628bd25ccdSJames Feist                                     }
6638bd25ccdSJames Feist                                 }
6648bd25ccdSJames Feist 
665bd79bce8SPatrick Williams                                 size_t minNumNeeded =
666bd79bce8SPatrick Williams                                     collection->empty()
66726f6976fSEd Tanous                                         ? 0
668bd79bce8SPatrick Williams                                         : collection->size() - *allowedFailures;
669bd79bce8SPatrick Williams                                 nlohmann::json& jResp =
670bd79bce8SPatrick Williams                                     sensorsAsyncResp->asyncResp->res
6718bd25ccdSJames Feist                                         .jsonValue["Redundancy"];
6721476687dSEd Tanous 
6731476687dSEd Tanous                                 nlohmann::json::object_t redundancy;
674bd79bce8SPatrick Williams                                 boost::urls::url url = boost::urls::format(
675bd79bce8SPatrick Williams                                     "/redfish/v1/Chassis/{}/{}",
676ef4c65b7SEd Tanous                                     sensorsAsyncResp->chassisId,
677eddfc437SWilly Tu                                     sensorsAsyncResp->chassisSubNode);
678bd79bce8SPatrick Williams                                 url.set_fragment(
679bd79bce8SPatrick Williams                                     ("/Redundancy"_json_pointer / jResp.size())
680eddfc437SWilly Tu                                         .to_string());
681eddfc437SWilly Tu                                 redundancy["@odata.id"] = std::move(url);
682bd79bce8SPatrick Williams                                 redundancy["@odata.type"] =
683bd79bce8SPatrick Williams                                     "#Redundancy.v1_3_2.Redundancy";
6841476687dSEd Tanous                                 redundancy["MinNumNeeded"] = minNumNeeded;
685bd79bce8SPatrick Williams                                 redundancy["Mode"] =
686bd79bce8SPatrick Williams                                     redundancy::RedundancyType::NPlusM;
6871476687dSEd Tanous                                 redundancy["Name"] = name;
6881476687dSEd Tanous                                 redundancy["RedundancySet"] = redfishCollection;
6891476687dSEd Tanous                                 redundancy["Status"]["Health"] = health;
690bd79bce8SPatrick Williams                                 redundancy["Status"]["State"] =
691bd79bce8SPatrick Williams                                     resource::State::Enabled;
6921476687dSEd Tanous 
693b2ba3072SPatrick Williams                                 jResp.emplace_back(std::move(redundancy));
69486d89ed7SKrzysztof Grobelny                             });
6951e1e598dSJonathan Doman                     });
6968bd25ccdSJames Feist             }
697e99073f5SGeorge Liu         });
6988bd25ccdSJames Feist }
6998bd25ccdSJames Feist 
700b5a76932SEd Tanous inline void
70181ce609eSEd Tanous     sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
70249c53ac9SJohnathan Mantey {
7038d1b46d7Szhanghch05     nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue;
70449c53ac9SJohnathan Mantey     std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
7050c728b42SJanet Adkins     if (sensorsAsyncResp->chassisSubNode == sensors::powerNodeStr)
70649c53ac9SJohnathan Mantey     {
70749c53ac9SJohnathan Mantey         sensorHeaders = {"Voltages", "PowerSupplies"};
70849c53ac9SJohnathan Mantey     }
70949c53ac9SJohnathan Mantey     for (const std::string& sensorGroup : sensorHeaders)
71049c53ac9SJohnathan Mantey     {
71149c53ac9SJohnathan Mantey         nlohmann::json::iterator entry = response.find(sensorGroup);
71249c53ac9SJohnathan Mantey         if (entry != response.end())
71349c53ac9SJohnathan Mantey         {
71449c53ac9SJohnathan Mantey             std::sort(entry->begin(), entry->end(),
71502cad96eSEd Tanous                       [](const nlohmann::json& c1, const nlohmann::json& c2) {
71649c53ac9SJohnathan Mantey                           return c1["Name"] < c2["Name"];
71749c53ac9SJohnathan Mantey                       });
71849c53ac9SJohnathan Mantey 
71949c53ac9SJohnathan Mantey             // add the index counts to the end of each entry
72049c53ac9SJohnathan Mantey             size_t count = 0;
72149c53ac9SJohnathan Mantey             for (nlohmann::json& sensorJson : *entry)
72249c53ac9SJohnathan Mantey             {
72349c53ac9SJohnathan Mantey                 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
72449c53ac9SJohnathan Mantey                 if (odata == sensorJson.end())
72549c53ac9SJohnathan Mantey                 {
72649c53ac9SJohnathan Mantey                     continue;
72749c53ac9SJohnathan Mantey                 }
72849c53ac9SJohnathan Mantey                 std::string* value = odata->get_ptr<std::string*>();
72949c53ac9SJohnathan Mantey                 if (value != nullptr)
73049c53ac9SJohnathan Mantey                 {
731eddfc437SWilly Tu                     *value += "/" + std::to_string(count);
7323e35c761SGeorge Liu                     sensorJson["MemberId"] = std::to_string(count);
73349c53ac9SJohnathan Mantey                     count++;
73481ce609eSEd Tanous                     sensorsAsyncResp->updateUri(sensorJson["Name"], *value);
73549c53ac9SJohnathan Mantey                 }
73649c53ac9SJohnathan Mantey             }
73749c53ac9SJohnathan Mantey         }
73849c53ac9SJohnathan Mantey     }
73949c53ac9SJohnathan Mantey }
74049c53ac9SJohnathan Mantey 
74108777fb0SLewanczyk, Dawid /**
742adc4f0dbSShawn McCarney  * @brief Finds the inventory item with the specified object path.
743adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
744adc4f0dbSShawn McCarney  * @param invItemObjPath D-Bus object path of inventory item.
745adc4f0dbSShawn McCarney  * @return Inventory item within vector, or nullptr if no match found.
7468fb49dd6SShawn McCarney  */
74723a21a1cSEd Tanous inline InventoryItem* findInventoryItem(
748b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
749adc4f0dbSShawn McCarney     const std::string& invItemObjPath)
7508fb49dd6SShawn McCarney {
751adc4f0dbSShawn McCarney     for (InventoryItem& inventoryItem : *inventoryItems)
7528fb49dd6SShawn McCarney     {
753adc4f0dbSShawn McCarney         if (inventoryItem.objectPath == invItemObjPath)
7548fb49dd6SShawn McCarney         {
755adc4f0dbSShawn McCarney             return &inventoryItem;
7568fb49dd6SShawn McCarney         }
7578fb49dd6SShawn McCarney     }
7588fb49dd6SShawn McCarney     return nullptr;
7598fb49dd6SShawn McCarney }
7608fb49dd6SShawn McCarney 
7618fb49dd6SShawn McCarney /**
762adc4f0dbSShawn McCarney  * @brief Finds the inventory item associated with the specified sensor.
763adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
764adc4f0dbSShawn McCarney  * @param sensorObjPath D-Bus object path of sensor.
765adc4f0dbSShawn McCarney  * @return Inventory item within vector, or nullptr if no match found.
7668fb49dd6SShawn McCarney  */
76723a21a1cSEd Tanous inline InventoryItem* findInventoryItemForSensor(
768b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
769adc4f0dbSShawn McCarney     const std::string& sensorObjPath)
770adc4f0dbSShawn McCarney {
771adc4f0dbSShawn McCarney     for (InventoryItem& inventoryItem : *inventoryItems)
772adc4f0dbSShawn McCarney     {
773db0d36efSEd Tanous         if (inventoryItem.sensors.contains(sensorObjPath))
774adc4f0dbSShawn McCarney         {
775adc4f0dbSShawn McCarney             return &inventoryItem;
776adc4f0dbSShawn McCarney         }
777adc4f0dbSShawn McCarney     }
778adc4f0dbSShawn McCarney     return nullptr;
779adc4f0dbSShawn McCarney }
780adc4f0dbSShawn McCarney 
781adc4f0dbSShawn McCarney /**
782d500549bSAnthony Wilson  * @brief Finds the inventory item associated with the specified led path.
783d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
784d500549bSAnthony Wilson  * @param ledObjPath D-Bus object path of led.
785d500549bSAnthony Wilson  * @return Inventory item within vector, or nullptr if no match found.
786d500549bSAnthony Wilson  */
787bd79bce8SPatrick Williams inline InventoryItem* findInventoryItemForLed(
788bd79bce8SPatrick Williams     std::vector<InventoryItem>& inventoryItems, const std::string& ledObjPath)
789d500549bSAnthony Wilson {
790d500549bSAnthony Wilson     for (InventoryItem& inventoryItem : inventoryItems)
791d500549bSAnthony Wilson     {
792d500549bSAnthony Wilson         if (inventoryItem.ledObjectPath == ledObjPath)
793d500549bSAnthony Wilson         {
794d500549bSAnthony Wilson             return &inventoryItem;
795d500549bSAnthony Wilson         }
796d500549bSAnthony Wilson     }
797d500549bSAnthony Wilson     return nullptr;
798d500549bSAnthony Wilson }
799d500549bSAnthony Wilson 
800d500549bSAnthony Wilson /**
801adc4f0dbSShawn McCarney  * @brief Adds inventory item and associated sensor to specified vector.
802adc4f0dbSShawn McCarney  *
803adc4f0dbSShawn McCarney  * Adds a new InventoryItem to the vector if necessary.  Searches for an
804adc4f0dbSShawn McCarney  * existing InventoryItem with the specified object path.  If not found, one is
805adc4f0dbSShawn McCarney  * added to the vector.
806adc4f0dbSShawn McCarney  *
807adc4f0dbSShawn McCarney  * Next, the specified sensor is added to the set of sensors associated with the
808adc4f0dbSShawn McCarney  * InventoryItem.
809adc4f0dbSShawn McCarney  *
810adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
811adc4f0dbSShawn McCarney  * @param invItemObjPath D-Bus object path of inventory item.
812adc4f0dbSShawn McCarney  * @param sensorObjPath D-Bus object path of sensor
813adc4f0dbSShawn McCarney  */
814b5a76932SEd Tanous inline void addInventoryItem(
815b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
816b5a76932SEd Tanous     const std::string& invItemObjPath, const std::string& sensorObjPath)
817adc4f0dbSShawn McCarney {
818adc4f0dbSShawn McCarney     // Look for inventory item in vector
819bd79bce8SPatrick Williams     InventoryItem* inventoryItem =
820bd79bce8SPatrick Williams         findInventoryItem(inventoryItems, invItemObjPath);
821adc4f0dbSShawn McCarney 
822adc4f0dbSShawn McCarney     // If inventory item doesn't exist in vector, add it
823adc4f0dbSShawn McCarney     if (inventoryItem == nullptr)
824adc4f0dbSShawn McCarney     {
825adc4f0dbSShawn McCarney         inventoryItems->emplace_back(invItemObjPath);
826adc4f0dbSShawn McCarney         inventoryItem = &(inventoryItems->back());
827adc4f0dbSShawn McCarney     }
828adc4f0dbSShawn McCarney 
829adc4f0dbSShawn McCarney     // Add sensor to set of sensors associated with inventory item
830adc4f0dbSShawn McCarney     inventoryItem->sensors.emplace(sensorObjPath);
831adc4f0dbSShawn McCarney }
832adc4f0dbSShawn McCarney 
833adc4f0dbSShawn McCarney /**
834adc4f0dbSShawn McCarney  * @brief Stores D-Bus data in the specified inventory item.
835adc4f0dbSShawn McCarney  *
836adc4f0dbSShawn McCarney  * Finds D-Bus data in the specified map of interfaces.  Stores the data in the
837adc4f0dbSShawn McCarney  * specified InventoryItem.
838adc4f0dbSShawn McCarney  *
839adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
840adc4f0dbSShawn McCarney  * response.
841adc4f0dbSShawn McCarney  *
842adc4f0dbSShawn McCarney  * @param inventoryItem Inventory item where data will be stored.
843adc4f0dbSShawn McCarney  * @param interfacesDict Map containing D-Bus interfaces and their properties
844adc4f0dbSShawn McCarney  * for the specified inventory item.
845adc4f0dbSShawn McCarney  */
84623a21a1cSEd Tanous inline void storeInventoryItemData(
847adc4f0dbSShawn McCarney     InventoryItem& inventoryItem,
84880f79a40SMichael Shen     const dbus::utility::DBusInterfacesMap& interfacesDict)
8498fb49dd6SShawn McCarney {
850adc4f0dbSShawn McCarney     // Get properties from Inventory.Item interface
851711ac7a9SEd Tanous 
8529eb808c1SEd Tanous     for (const auto& [interface, values] : interfacesDict)
8538fb49dd6SShawn McCarney     {
854711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Item")
8558fb49dd6SShawn McCarney         {
8569eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
857711ac7a9SEd Tanous             {
858711ac7a9SEd Tanous                 if (name == "Present")
859711ac7a9SEd Tanous                 {
860711ac7a9SEd Tanous                     const bool* value = std::get_if<bool>(&dbusValue);
861adc4f0dbSShawn McCarney                     if (value != nullptr)
8628fb49dd6SShawn McCarney                     {
863adc4f0dbSShawn McCarney                         inventoryItem.isPresent = *value;
8648fb49dd6SShawn McCarney                     }
8658fb49dd6SShawn McCarney                 }
8668fb49dd6SShawn McCarney             }
867711ac7a9SEd Tanous         }
868adc4f0dbSShawn McCarney         // Check if Inventory.Item.PowerSupply interface is present
869711ac7a9SEd Tanous 
870711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Item.PowerSupply")
8718fb49dd6SShawn McCarney         {
872adc4f0dbSShawn McCarney             inventoryItem.isPowerSupply = true;
8738fb49dd6SShawn McCarney         }
874adc4f0dbSShawn McCarney 
875adc4f0dbSShawn McCarney         // Get properties from Inventory.Decorator.Asset interface
876711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
877adc4f0dbSShawn McCarney         {
8789eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
879711ac7a9SEd Tanous             {
880711ac7a9SEd Tanous                 if (name == "Manufacturer")
881adc4f0dbSShawn McCarney                 {
882adc4f0dbSShawn McCarney                     const std::string* value =
883711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
884adc4f0dbSShawn McCarney                     if (value != nullptr)
885adc4f0dbSShawn McCarney                     {
886adc4f0dbSShawn McCarney                         inventoryItem.manufacturer = *value;
887adc4f0dbSShawn McCarney                     }
888adc4f0dbSShawn McCarney                 }
889711ac7a9SEd Tanous                 if (name == "Model")
890adc4f0dbSShawn McCarney                 {
891adc4f0dbSShawn McCarney                     const std::string* value =
892711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
893adc4f0dbSShawn McCarney                     if (value != nullptr)
894adc4f0dbSShawn McCarney                     {
895adc4f0dbSShawn McCarney                         inventoryItem.model = *value;
896adc4f0dbSShawn McCarney                     }
897adc4f0dbSShawn McCarney                 }
898711ac7a9SEd Tanous                 if (name == "SerialNumber")
899adc4f0dbSShawn McCarney                 {
900adc4f0dbSShawn McCarney                     const std::string* value =
901711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
902adc4f0dbSShawn McCarney                     if (value != nullptr)
903adc4f0dbSShawn McCarney                     {
904adc4f0dbSShawn McCarney                         inventoryItem.serialNumber = *value;
905adc4f0dbSShawn McCarney                     }
906adc4f0dbSShawn McCarney                 }
907711ac7a9SEd Tanous                 if (name == "PartNumber")
908711ac7a9SEd Tanous                 {
909711ac7a9SEd Tanous                     const std::string* value =
910711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
911711ac7a9SEd Tanous                     if (value != nullptr)
912711ac7a9SEd Tanous                     {
913711ac7a9SEd Tanous                         inventoryItem.partNumber = *value;
914711ac7a9SEd Tanous                     }
915711ac7a9SEd Tanous                 }
916711ac7a9SEd Tanous             }
917adc4f0dbSShawn McCarney         }
918adc4f0dbSShawn McCarney 
919711ac7a9SEd Tanous         if (interface ==
920711ac7a9SEd Tanous             "xyz.openbmc_project.State.Decorator.OperationalStatus")
921adc4f0dbSShawn McCarney         {
9229eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
923adc4f0dbSShawn McCarney             {
924711ac7a9SEd Tanous                 if (name == "Functional")
925711ac7a9SEd Tanous                 {
926711ac7a9SEd Tanous                     const bool* value = std::get_if<bool>(&dbusValue);
927adc4f0dbSShawn McCarney                     if (value != nullptr)
928adc4f0dbSShawn McCarney                     {
929adc4f0dbSShawn McCarney                         inventoryItem.isFunctional = *value;
9308fb49dd6SShawn McCarney                     }
9318fb49dd6SShawn McCarney                 }
9328fb49dd6SShawn McCarney             }
9338fb49dd6SShawn McCarney         }
934711ac7a9SEd Tanous     }
935711ac7a9SEd Tanous }
9368fb49dd6SShawn McCarney 
9378fb49dd6SShawn McCarney /**
938adc4f0dbSShawn McCarney  * @brief Gets D-Bus data for inventory items associated with sensors.
9398fb49dd6SShawn McCarney  *
940adc4f0dbSShawn McCarney  * Uses the specified connections (services) to obtain D-Bus data for inventory
941adc4f0dbSShawn McCarney  * items associated with sensors.  Stores the resulting data in the
942adc4f0dbSShawn McCarney  * inventoryItems vector.
9438fb49dd6SShawn McCarney  *
944adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
945adc4f0dbSShawn McCarney  * response.
946adc4f0dbSShawn McCarney  *
947adc4f0dbSShawn McCarney  * Finds the inventory item data asynchronously.  Invokes callback when data has
948adc4f0dbSShawn McCarney  * been obtained.
949adc4f0dbSShawn McCarney  *
950adc4f0dbSShawn McCarney  * The callback must have the following signature:
951adc4f0dbSShawn McCarney  *   @code
952d500549bSAnthony Wilson  *   callback(void)
953adc4f0dbSShawn McCarney  *   @endcode
954adc4f0dbSShawn McCarney  *
955adc4f0dbSShawn McCarney  * This function is called recursively, obtaining data asynchronously from one
956adc4f0dbSShawn McCarney  * connection in each call.  This ensures the callback is not invoked until the
957adc4f0dbSShawn McCarney  * last asynchronous function has completed.
9588fb49dd6SShawn McCarney  *
9598fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
960adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
961adc4f0dbSShawn McCarney  * @param invConnections Connections that provide data for the inventory items.
9628fb49dd6SShawn McCarney  * implements ObjectManager.
963adc4f0dbSShawn McCarney  * @param callback Callback to invoke when inventory data has been obtained.
964adc4f0dbSShawn McCarney  * @param invConnectionsIndex Current index in invConnections.  Only specified
965adc4f0dbSShawn McCarney  * in recursive calls to this function.
9668fb49dd6SShawn McCarney  */
967adc4f0dbSShawn McCarney template <typename Callback>
968adc4f0dbSShawn McCarney static void getInventoryItemsData(
9698fb49dd6SShawn McCarney     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
970adc4f0dbSShawn McCarney     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
971d0090733SEd Tanous     std::shared_ptr<std::set<std::string>> invConnections, Callback&& callback,
972d0090733SEd Tanous     size_t invConnectionsIndex = 0)
9738fb49dd6SShawn McCarney {
97462598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryItemsData enter");
9758fb49dd6SShawn McCarney 
976adc4f0dbSShawn McCarney     // If no more connections left, call callback
977adc4f0dbSShawn McCarney     if (invConnectionsIndex >= invConnections->size())
9788fb49dd6SShawn McCarney     {
979d500549bSAnthony Wilson         callback();
98062598e31SEd Tanous         BMCWEB_LOG_DEBUG("getInventoryItemsData exit");
981adc4f0dbSShawn McCarney         return;
982adc4f0dbSShawn McCarney     }
983adc4f0dbSShawn McCarney 
984adc4f0dbSShawn McCarney     // Get inventory item data from current connection
985fe04d49cSNan Zhou     auto it = invConnections->begin();
986fe04d49cSNan Zhou     std::advance(it, invConnectionsIndex);
987adc4f0dbSShawn McCarney     if (it != invConnections->end())
988adc4f0dbSShawn McCarney     {
989adc4f0dbSShawn McCarney         const std::string& invConnection = *it;
990adc4f0dbSShawn McCarney 
9915eb468daSGeorge Liu         // Get all object paths and their interfaces for current connection
9925eb468daSGeorge Liu         sdbusplus::message::object_path path("/xyz/openbmc_project/inventory");
9935eb468daSGeorge Liu         dbus::utility::getManagedObjects(
9945eb468daSGeorge Liu             invConnection, path,
9955eb468daSGeorge Liu             [sensorsAsyncResp, inventoryItems, invConnections,
9968cb2c024SEd Tanous              callback = std::forward<Callback>(callback), invConnectionsIndex](
9975e7e2dc5SEd Tanous                 const boost::system::error_code& ec,
9984e0d8789SEd Tanous                 const dbus::utility::ManagedObjectType& resp) mutable {
99962598e31SEd Tanous                 BMCWEB_LOG_DEBUG("getInventoryItemsData respHandler enter");
10008fb49dd6SShawn McCarney                 if (ec)
10018fb49dd6SShawn McCarney                 {
100262598e31SEd Tanous                     BMCWEB_LOG_ERROR(
100362598e31SEd Tanous                         "getInventoryItemsData respHandler DBus error {}", ec);
10048d1b46d7Szhanghch05                     messages::internalError(sensorsAsyncResp->asyncResp->res);
10058fb49dd6SShawn McCarney                     return;
10068fb49dd6SShawn McCarney                 }
10078fb49dd6SShawn McCarney 
10088fb49dd6SShawn McCarney                 // Loop through returned object paths
10098fb49dd6SShawn McCarney                 for (const auto& objDictEntry : resp)
10108fb49dd6SShawn McCarney                 {
10118fb49dd6SShawn McCarney                     const std::string& objPath =
10128fb49dd6SShawn McCarney                         static_cast<const std::string&>(objDictEntry.first);
10138fb49dd6SShawn McCarney 
1014bd79bce8SPatrick Williams                     // If this object path is one of the specified inventory
1015bd79bce8SPatrick Williams                     // items
1016bd79bce8SPatrick Williams                     InventoryItem* inventoryItem =
1017bd79bce8SPatrick Williams                         findInventoryItem(inventoryItems, objPath);
1018adc4f0dbSShawn McCarney                     if (inventoryItem != nullptr)
10198fb49dd6SShawn McCarney                     {
1020adc4f0dbSShawn McCarney                         // Store inventory data in InventoryItem
1021bd79bce8SPatrick Williams                         storeInventoryItemData(*inventoryItem,
1022bd79bce8SPatrick Williams                                                objDictEntry.second);
10238fb49dd6SShawn McCarney                     }
10248fb49dd6SShawn McCarney                 }
10258fb49dd6SShawn McCarney 
1026adc4f0dbSShawn McCarney                 // Recurse to get inventory item data from next connection
1027adc4f0dbSShawn McCarney                 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
1028d0090733SEd Tanous                                       invConnections, std::move(callback),
1029d0090733SEd Tanous                                       invConnectionsIndex + 1);
1030adc4f0dbSShawn McCarney 
103162598e31SEd Tanous                 BMCWEB_LOG_DEBUG("getInventoryItemsData respHandler exit");
10325eb468daSGeorge Liu             });
10338fb49dd6SShawn McCarney     }
10348fb49dd6SShawn McCarney 
103562598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryItemsData exit");
10368fb49dd6SShawn McCarney }
10378fb49dd6SShawn McCarney 
10388fb49dd6SShawn McCarney /**
1039adc4f0dbSShawn McCarney  * @brief Gets connections that provide D-Bus data for inventory items.
10408fb49dd6SShawn McCarney  *
1041adc4f0dbSShawn McCarney  * Gets the D-Bus connections (services) that provide data for the inventory
1042adc4f0dbSShawn McCarney  * items that are associated with sensors.
10438fb49dd6SShawn McCarney  *
10448fb49dd6SShawn McCarney  * Finds the connections asynchronously.  Invokes callback when information has
10458fb49dd6SShawn McCarney  * been obtained.
10468fb49dd6SShawn McCarney  *
10478fb49dd6SShawn McCarney  * The callback must have the following signature:
10488fb49dd6SShawn McCarney  *   @code
1049fe04d49cSNan Zhou  *   callback(std::shared_ptr<std::set<std::string>> invConnections)
10508fb49dd6SShawn McCarney  *   @endcode
10518fb49dd6SShawn McCarney  *
10528fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
1053adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
10548fb49dd6SShawn McCarney  * @param callback Callback to invoke when connections have been obtained.
10558fb49dd6SShawn McCarney  */
10568fb49dd6SShawn McCarney template <typename Callback>
10578fb49dd6SShawn McCarney static void getInventoryItemsConnections(
1058b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1059b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
10608fb49dd6SShawn McCarney     Callback&& callback)
10618fb49dd6SShawn McCarney {
106262598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryItemsConnections enter");
10638fb49dd6SShawn McCarney 
10648fb49dd6SShawn McCarney     const std::string path = "/xyz/openbmc_project/inventory";
1065e99073f5SGeorge Liu     constexpr std::array<std::string_view, 4> interfaces = {
10668fb49dd6SShawn McCarney         "xyz.openbmc_project.Inventory.Item",
1067adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Item.PowerSupply",
1068adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Decorator.Asset",
10698fb49dd6SShawn McCarney         "xyz.openbmc_project.State.Decorator.OperationalStatus"};
10708fb49dd6SShawn McCarney 
1071e99073f5SGeorge Liu     // Make call to ObjectMapper to find all inventory items
1072e99073f5SGeorge Liu     dbus::utility::getSubTree(
1073e99073f5SGeorge Liu         path, 0, interfaces,
10748cb2c024SEd Tanous         [callback = std::forward<Callback>(callback), sensorsAsyncResp,
1075002d39b4SEd Tanous          inventoryItems](
1076e99073f5SGeorge Liu             const boost::system::error_code& ec,
10774e0d8789SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) mutable {
1078e99073f5SGeorge Liu             // Response handler for parsing output from GetSubTree
107962598e31SEd Tanous             BMCWEB_LOG_DEBUG("getInventoryItemsConnections respHandler enter");
10808fb49dd6SShawn McCarney             if (ec)
10818fb49dd6SShawn McCarney             {
10828d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
108362598e31SEd Tanous                 BMCWEB_LOG_ERROR(
1084bd79bce8SPatrick Williams                     "getInventoryItemsConnections respHandler DBus error {}",
1085bd79bce8SPatrick Williams                     ec);
10868fb49dd6SShawn McCarney                 return;
10878fb49dd6SShawn McCarney             }
10888fb49dd6SShawn McCarney 
10898fb49dd6SShawn McCarney             // Make unique list of connections for desired inventory items
1090fe04d49cSNan Zhou             std::shared_ptr<std::set<std::string>> invConnections =
1091fe04d49cSNan Zhou                 std::make_shared<std::set<std::string>>();
10928fb49dd6SShawn McCarney 
10938fb49dd6SShawn McCarney             // Loop through objects from GetSubTree
1094bd79bce8SPatrick Williams             for (const std::pair<std::string,
1095bd79bce8SPatrick Williams                                  std::vector<std::pair<
1096bd79bce8SPatrick Williams                                      std::string, std::vector<std::string>>>>&
10978fb49dd6SShawn McCarney                      object : subtree)
10988fb49dd6SShawn McCarney             {
1099adc4f0dbSShawn McCarney                 // Check if object path is one of the specified inventory items
11008fb49dd6SShawn McCarney                 const std::string& objPath = object.first;
1101adc4f0dbSShawn McCarney                 if (findInventoryItem(inventoryItems, objPath) != nullptr)
11028fb49dd6SShawn McCarney                 {
11038fb49dd6SShawn McCarney                     // Store all connections to inventory item
11048fb49dd6SShawn McCarney                     for (const std::pair<std::string, std::vector<std::string>>&
11058fb49dd6SShawn McCarney                              objData : object.second)
11068fb49dd6SShawn McCarney                     {
11078fb49dd6SShawn McCarney                         const std::string& invConnection = objData.first;
11088fb49dd6SShawn McCarney                         invConnections->insert(invConnection);
11098fb49dd6SShawn McCarney                     }
11108fb49dd6SShawn McCarney                 }
11118fb49dd6SShawn McCarney             }
1112d500549bSAnthony Wilson 
11138fb49dd6SShawn McCarney             callback(invConnections);
111462598e31SEd Tanous             BMCWEB_LOG_DEBUG("getInventoryItemsConnections respHandler exit");
1115e99073f5SGeorge Liu         });
111662598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryItemsConnections exit");
11178fb49dd6SShawn McCarney }
11188fb49dd6SShawn McCarney 
11198fb49dd6SShawn McCarney /**
1120adc4f0dbSShawn McCarney  * @brief Gets associations from sensors to inventory items.
11218fb49dd6SShawn McCarney  *
11228fb49dd6SShawn McCarney  * Looks for ObjectMapper associations from the specified sensors to related
1123d500549bSAnthony Wilson  * inventory items. Then finds the associations from those inventory items to
1124d500549bSAnthony Wilson  * their LEDs, if any.
11258fb49dd6SShawn McCarney  *
11268fb49dd6SShawn McCarney  * Finds the inventory items asynchronously.  Invokes callback when information
11278fb49dd6SShawn McCarney  * has been obtained.
11288fb49dd6SShawn McCarney  *
11298fb49dd6SShawn McCarney  * The callback must have the following signature:
11308fb49dd6SShawn McCarney  *   @code
1131adc4f0dbSShawn McCarney  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
11328fb49dd6SShawn McCarney  *   @endcode
11338fb49dd6SShawn McCarney  *
11348fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
11358fb49dd6SShawn McCarney  * @param sensorNames All sensors within the current chassis.
11368fb49dd6SShawn McCarney  * implements ObjectManager.
11378fb49dd6SShawn McCarney  * @param callback Callback to invoke when inventory items have been obtained.
11388fb49dd6SShawn McCarney  */
11398fb49dd6SShawn McCarney template <typename Callback>
1140adc4f0dbSShawn McCarney static void getInventoryItemAssociations(
1141b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1142fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& sensorNames,
11438fb49dd6SShawn McCarney     Callback&& callback)
11448fb49dd6SShawn McCarney {
114562598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryItemAssociations enter");
11468fb49dd6SShawn McCarney 
11475eb468daSGeorge Liu     // Call GetManagedObjects on the ObjectMapper to get all associations
11485eb468daSGeorge Liu     sdbusplus::message::object_path path("/");
11495eb468daSGeorge Liu     dbus::utility::getManagedObjects(
11505eb468daSGeorge Liu         "xyz.openbmc_project.ObjectMapper", path,
11518cb2c024SEd Tanous         [callback = std::forward<Callback>(callback), sensorsAsyncResp,
11525e7e2dc5SEd Tanous          sensorNames](const boost::system::error_code& ec,
11534e0d8789SEd Tanous                       const dbus::utility::ManagedObjectType& resp) mutable {
115462598e31SEd Tanous             BMCWEB_LOG_DEBUG("getInventoryItemAssociations respHandler enter");
11558fb49dd6SShawn McCarney             if (ec)
11568fb49dd6SShawn McCarney             {
115762598e31SEd Tanous                 BMCWEB_LOG_ERROR(
1158bd79bce8SPatrick Williams                     "getInventoryItemAssociations respHandler DBus error {}",
1159bd79bce8SPatrick Williams                     ec);
11608d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
11618fb49dd6SShawn McCarney                 return;
11628fb49dd6SShawn McCarney             }
11638fb49dd6SShawn McCarney 
1164adc4f0dbSShawn McCarney             // Create vector to hold list of inventory items
1165adc4f0dbSShawn McCarney             std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1166adc4f0dbSShawn McCarney                 std::make_shared<std::vector<InventoryItem>>();
1167adc4f0dbSShawn McCarney 
11688fb49dd6SShawn McCarney             // Loop through returned object paths
11698fb49dd6SShawn McCarney             std::string sensorAssocPath;
11708fb49dd6SShawn McCarney             sensorAssocPath.reserve(128); // avoid memory allocations
11718fb49dd6SShawn McCarney             for (const auto& objDictEntry : resp)
11728fb49dd6SShawn McCarney             {
11738fb49dd6SShawn McCarney                 const std::string& objPath =
11748fb49dd6SShawn McCarney                     static_cast<const std::string&>(objDictEntry.first);
11758fb49dd6SShawn McCarney 
1176bd79bce8SPatrick Williams                 // If path is inventory association for one of the specified
1177bd79bce8SPatrick Williams                 // sensors
11788fb49dd6SShawn McCarney                 for (const std::string& sensorName : *sensorNames)
11798fb49dd6SShawn McCarney                 {
11808fb49dd6SShawn McCarney                     sensorAssocPath = sensorName;
11818fb49dd6SShawn McCarney                     sensorAssocPath += "/inventory";
11828fb49dd6SShawn McCarney                     if (objPath == sensorAssocPath)
11838fb49dd6SShawn McCarney                     {
11848fb49dd6SShawn McCarney                         // Get Association interface for object path
1185bd79bce8SPatrick Williams                         for (const auto& [interface, values] :
1186bd79bce8SPatrick Williams                              objDictEntry.second)
11878fb49dd6SShawn McCarney                         {
1188711ac7a9SEd Tanous                             if (interface == "xyz.openbmc_project.Association")
1189711ac7a9SEd Tanous                             {
1190711ac7a9SEd Tanous                                 for (const auto& [valueName, value] : values)
1191711ac7a9SEd Tanous                                 {
1192711ac7a9SEd Tanous                                     if (valueName == "endpoints")
11938fb49dd6SShawn McCarney                                     {
1194bd79bce8SPatrick Williams                                         const std::vector<std::string>*
1195bd79bce8SPatrick Williams                                             endpoints = std::get_if<
1196bd79bce8SPatrick Williams                                                 std::vector<std::string>>(
1197711ac7a9SEd Tanous                                                 &value);
1198711ac7a9SEd Tanous                                         if ((endpoints != nullptr) &&
1199711ac7a9SEd Tanous                                             !endpoints->empty())
12008fb49dd6SShawn McCarney                                         {
1201adc4f0dbSShawn McCarney                                             // Add inventory item to vector
1202adc4f0dbSShawn McCarney                                             const std::string& invItemPath =
1203adc4f0dbSShawn McCarney                                                 endpoints->front();
1204711ac7a9SEd Tanous                                             addInventoryItem(inventoryItems,
1205711ac7a9SEd Tanous                                                              invItemPath,
1206adc4f0dbSShawn McCarney                                                              sensorName);
12078fb49dd6SShawn McCarney                                         }
12088fb49dd6SShawn McCarney                                     }
12098fb49dd6SShawn McCarney                                 }
1210711ac7a9SEd Tanous                             }
1211711ac7a9SEd Tanous                         }
12128fb49dd6SShawn McCarney                         break;
12138fb49dd6SShawn McCarney                     }
12148fb49dd6SShawn McCarney                 }
12158fb49dd6SShawn McCarney             }
12168fb49dd6SShawn McCarney 
1217d500549bSAnthony Wilson             // Now loop through the returned object paths again, this time to
1218d500549bSAnthony Wilson             // find the leds associated with the inventory items we just found
1219d500549bSAnthony Wilson             std::string inventoryAssocPath;
1220d500549bSAnthony Wilson             inventoryAssocPath.reserve(128); // avoid memory allocations
1221d500549bSAnthony Wilson             for (const auto& objDictEntry : resp)
1222d500549bSAnthony Wilson             {
1223d500549bSAnthony Wilson                 const std::string& objPath =
1224d500549bSAnthony Wilson                     static_cast<const std::string&>(objDictEntry.first);
1225d500549bSAnthony Wilson 
1226d500549bSAnthony Wilson                 for (InventoryItem& inventoryItem : *inventoryItems)
1227d500549bSAnthony Wilson                 {
1228d500549bSAnthony Wilson                     inventoryAssocPath = inventoryItem.objectPath;
1229d500549bSAnthony Wilson                     inventoryAssocPath += "/leds";
1230d500549bSAnthony Wilson                     if (objPath == inventoryAssocPath)
1231d500549bSAnthony Wilson                     {
1232bd79bce8SPatrick Williams                         for (const auto& [interface, values] :
1233bd79bce8SPatrick Williams                              objDictEntry.second)
1234d500549bSAnthony Wilson                         {
1235711ac7a9SEd Tanous                             if (interface == "xyz.openbmc_project.Association")
1236711ac7a9SEd Tanous                             {
1237711ac7a9SEd Tanous                                 for (const auto& [valueName, value] : values)
1238711ac7a9SEd Tanous                                 {
1239711ac7a9SEd Tanous                                     if (valueName == "endpoints")
1240d500549bSAnthony Wilson                                     {
1241bd79bce8SPatrick Williams                                         const std::vector<std::string>*
1242bd79bce8SPatrick Williams                                             endpoints = std::get_if<
1243bd79bce8SPatrick Williams                                                 std::vector<std::string>>(
1244711ac7a9SEd Tanous                                                 &value);
1245711ac7a9SEd Tanous                                         if ((endpoints != nullptr) &&
1246711ac7a9SEd Tanous                                             !endpoints->empty())
1247d500549bSAnthony Wilson                                         {
1248711ac7a9SEd Tanous                                             // Add inventory item to vector
1249d500549bSAnthony Wilson                                             // Store LED path in inventory item
1250711ac7a9SEd Tanous                                             const std::string& ledPath =
1251711ac7a9SEd Tanous                                                 endpoints->front();
1252bd79bce8SPatrick Williams                                             inventoryItem.ledObjectPath =
1253bd79bce8SPatrick Williams                                                 ledPath;
1254d500549bSAnthony Wilson                                         }
1255d500549bSAnthony Wilson                                     }
1256d500549bSAnthony Wilson                                 }
1257711ac7a9SEd Tanous                             }
1258711ac7a9SEd Tanous                         }
1259711ac7a9SEd Tanous 
1260d500549bSAnthony Wilson                         break;
1261d500549bSAnthony Wilson                     }
1262d500549bSAnthony Wilson                 }
1263d500549bSAnthony Wilson             }
1264adc4f0dbSShawn McCarney             callback(inventoryItems);
126562598e31SEd Tanous             BMCWEB_LOG_DEBUG("getInventoryItemAssociations respHandler exit");
12665eb468daSGeorge Liu         });
12678fb49dd6SShawn McCarney 
126862598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryItemAssociations exit");
12698fb49dd6SShawn McCarney }
12708fb49dd6SShawn McCarney 
12718fb49dd6SShawn McCarney /**
1272d500549bSAnthony Wilson  * @brief Gets D-Bus data for inventory item leds associated with sensors.
1273d500549bSAnthony Wilson  *
1274d500549bSAnthony Wilson  * Uses the specified connections (services) to obtain D-Bus data for inventory
1275d500549bSAnthony Wilson  * item leds associated with sensors.  Stores the resulting data in the
1276d500549bSAnthony Wilson  * inventoryItems vector.
1277d500549bSAnthony Wilson  *
1278d500549bSAnthony Wilson  * This data is later used to provide sensor property values in the JSON
1279d500549bSAnthony Wilson  * response.
1280d500549bSAnthony Wilson  *
1281d500549bSAnthony Wilson  * Finds the inventory item led data asynchronously.  Invokes callback when data
1282d500549bSAnthony Wilson  * has been obtained.
1283d500549bSAnthony Wilson  *
1284d500549bSAnthony Wilson  * The callback must have the following signature:
1285d500549bSAnthony Wilson  *   @code
128642cbe538SGunnar Mills  *   callback()
1287d500549bSAnthony Wilson  *   @endcode
1288d500549bSAnthony Wilson  *
1289d500549bSAnthony Wilson  * This function is called recursively, obtaining data asynchronously from one
1290d500549bSAnthony Wilson  * connection in each call.  This ensures the callback is not invoked until the
1291d500549bSAnthony Wilson  * last asynchronous function has completed.
1292d500549bSAnthony Wilson  *
1293d500549bSAnthony Wilson  * @param sensorsAsyncResp Pointer to object holding response data.
1294d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1295d500549bSAnthony Wilson  * @param ledConnections Connections that provide data for the inventory leds.
1296d500549bSAnthony Wilson  * @param callback Callback to invoke when inventory data has been obtained.
1297d500549bSAnthony Wilson  * @param ledConnectionsIndex Current index in ledConnections.  Only specified
1298d500549bSAnthony Wilson  * in recursive calls to this function.
1299d500549bSAnthony Wilson  */
1300d500549bSAnthony Wilson template <typename Callback>
1301d500549bSAnthony Wilson void getInventoryLedData(
1302d500549bSAnthony Wilson     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1303d500549bSAnthony Wilson     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1304fe04d49cSNan Zhou     std::shared_ptr<std::map<std::string, std::string>> ledConnections,
1305d500549bSAnthony Wilson     Callback&& callback, size_t ledConnectionsIndex = 0)
1306d500549bSAnthony Wilson {
130762598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryLedData enter");
1308d500549bSAnthony Wilson 
1309d500549bSAnthony Wilson     // If no more connections left, call callback
1310d500549bSAnthony Wilson     if (ledConnectionsIndex >= ledConnections->size())
1311d500549bSAnthony Wilson     {
131242cbe538SGunnar Mills         callback();
131362598e31SEd Tanous         BMCWEB_LOG_DEBUG("getInventoryLedData exit");
1314d500549bSAnthony Wilson         return;
1315d500549bSAnthony Wilson     }
1316d500549bSAnthony Wilson 
1317d500549bSAnthony Wilson     // Get inventory item data from current connection
1318fe04d49cSNan Zhou     auto it = ledConnections->begin();
1319fe04d49cSNan Zhou     std::advance(it, ledConnectionsIndex);
1320d500549bSAnthony Wilson     if (it != ledConnections->end())
1321d500549bSAnthony Wilson     {
1322d500549bSAnthony Wilson         const std::string& ledPath = (*it).first;
1323d500549bSAnthony Wilson         const std::string& ledConnection = (*it).second;
1324d500549bSAnthony Wilson         // Response handler for Get State property
13251e1e598dSJonathan Doman         auto respHandler =
13261e1e598dSJonathan Doman             [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
13274e0d8789SEd Tanous              callback = std::forward<Callback>(callback),
13284e0d8789SEd Tanous              ledConnectionsIndex](const boost::system::error_code& ec,
13294e0d8789SEd Tanous                                   const std::string& state) mutable {
133062598e31SEd Tanous                 BMCWEB_LOG_DEBUG("getInventoryLedData respHandler enter");
1331d500549bSAnthony Wilson                 if (ec)
1332d500549bSAnthony Wilson                 {
133362598e31SEd Tanous                     BMCWEB_LOG_ERROR(
133462598e31SEd Tanous                         "getInventoryLedData respHandler DBus error {}", ec);
13358d1b46d7Szhanghch05                     messages::internalError(sensorsAsyncResp->asyncResp->res);
1336d500549bSAnthony Wilson                     return;
1337d500549bSAnthony Wilson                 }
1338d500549bSAnthony Wilson 
133962598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Led state: {}", state);
1340d500549bSAnthony Wilson                 // Find inventory item with this LED object path
1341d500549bSAnthony Wilson                 InventoryItem* inventoryItem =
1342d500549bSAnthony Wilson                     findInventoryItemForLed(*inventoryItems, ledPath);
1343d500549bSAnthony Wilson                 if (inventoryItem != nullptr)
1344d500549bSAnthony Wilson                 {
1345d500549bSAnthony Wilson                     // Store LED state in InventoryItem
134611ba3979SEd Tanous                     if (state.ends_with("On"))
1347d500549bSAnthony Wilson                     {
1348c9563608SJanet Adkins                         inventoryItem->ledState = sensor_utils::LedState::ON;
1349d500549bSAnthony Wilson                     }
135011ba3979SEd Tanous                     else if (state.ends_with("Blink"))
1351d500549bSAnthony Wilson                     {
1352c9563608SJanet Adkins                         inventoryItem->ledState = sensor_utils::LedState::BLINK;
1353d500549bSAnthony Wilson                     }
135411ba3979SEd Tanous                     else if (state.ends_with("Off"))
1355d500549bSAnthony Wilson                     {
1356c9563608SJanet Adkins                         inventoryItem->ledState = sensor_utils::LedState::OFF;
1357d500549bSAnthony Wilson                     }
1358d500549bSAnthony Wilson                     else
1359d500549bSAnthony Wilson                     {
1360c9563608SJanet Adkins                         inventoryItem->ledState =
1361c9563608SJanet Adkins                             sensor_utils::LedState::UNKNOWN;
1362d500549bSAnthony Wilson                     }
1363d500549bSAnthony Wilson                 }
1364d500549bSAnthony Wilson 
1365d500549bSAnthony Wilson                 // Recurse to get LED data from next connection
1366d500549bSAnthony Wilson                 getInventoryLedData(sensorsAsyncResp, inventoryItems,
1367d500549bSAnthony Wilson                                     ledConnections, std::move(callback),
1368d500549bSAnthony Wilson                                     ledConnectionsIndex + 1);
1369d500549bSAnthony Wilson 
137062598e31SEd Tanous                 BMCWEB_LOG_DEBUG("getInventoryLedData respHandler exit");
1371d500549bSAnthony Wilson             };
1372d500549bSAnthony Wilson 
1373d500549bSAnthony Wilson         // Get the State property for the current LED
13741e1e598dSJonathan Doman         sdbusplus::asio::getProperty<std::string>(
13751e1e598dSJonathan Doman             *crow::connections::systemBus, ledConnection, ledPath,
13761e1e598dSJonathan Doman             "xyz.openbmc_project.Led.Physical", "State",
13771e1e598dSJonathan Doman             std::move(respHandler));
1378d500549bSAnthony Wilson     }
1379d500549bSAnthony Wilson 
138062598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryLedData exit");
1381d500549bSAnthony Wilson }
1382d500549bSAnthony Wilson 
1383d500549bSAnthony Wilson /**
1384d500549bSAnthony Wilson  * @brief Gets LED data for LEDs associated with given inventory items.
1385d500549bSAnthony Wilson  *
1386d500549bSAnthony Wilson  * Gets the D-Bus connections (services) that provide LED data for the LEDs
1387d500549bSAnthony Wilson  * associated with the specified inventory items.  Then gets the LED data from
1388d500549bSAnthony Wilson  * each connection and stores it in the inventory item.
1389d500549bSAnthony Wilson  *
1390d500549bSAnthony Wilson  * This data is later used to provide sensor property values in the JSON
1391d500549bSAnthony Wilson  * response.
1392d500549bSAnthony Wilson  *
1393d500549bSAnthony Wilson  * Finds the LED data asynchronously.  Invokes callback when information has
1394d500549bSAnthony Wilson  * been obtained.
1395d500549bSAnthony Wilson  *
1396d500549bSAnthony Wilson  * The callback must have the following signature:
1397d500549bSAnthony Wilson  *   @code
139842cbe538SGunnar Mills  *   callback()
1399d500549bSAnthony Wilson  *   @endcode
1400d500549bSAnthony Wilson  *
1401d500549bSAnthony Wilson  * @param sensorsAsyncResp Pointer to object holding response data.
1402d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1403d500549bSAnthony Wilson  * @param callback Callback to invoke when inventory items have been obtained.
1404d500549bSAnthony Wilson  */
1405d500549bSAnthony Wilson template <typename Callback>
1406d500549bSAnthony Wilson void getInventoryLeds(
1407d500549bSAnthony Wilson     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1408d500549bSAnthony Wilson     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1409d500549bSAnthony Wilson     Callback&& callback)
1410d500549bSAnthony Wilson {
141162598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryLeds enter");
1412d500549bSAnthony Wilson 
1413d500549bSAnthony Wilson     const std::string path = "/xyz/openbmc_project";
1414e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
1415d500549bSAnthony Wilson         "xyz.openbmc_project.Led.Physical"};
1416d500549bSAnthony Wilson 
1417e99073f5SGeorge Liu     // Make call to ObjectMapper to find all inventory items
1418e99073f5SGeorge Liu     dbus::utility::getSubTree(
1419e99073f5SGeorge Liu         path, 0, interfaces,
14208cb2c024SEd Tanous         [callback = std::forward<Callback>(callback), sensorsAsyncResp,
1421002d39b4SEd Tanous          inventoryItems](
1422e99073f5SGeorge Liu             const boost::system::error_code& ec,
14234e0d8789SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) mutable {
1424e99073f5SGeorge Liu             // Response handler for parsing output from GetSubTree
142562598e31SEd Tanous             BMCWEB_LOG_DEBUG("getInventoryLeds respHandler enter");
1426d500549bSAnthony Wilson             if (ec)
1427d500549bSAnthony Wilson             {
14288d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
1429bd79bce8SPatrick Williams                 BMCWEB_LOG_ERROR("getInventoryLeds respHandler DBus error {}",
1430bd79bce8SPatrick Williams                                  ec);
1431d500549bSAnthony Wilson                 return;
1432d500549bSAnthony Wilson             }
1433d500549bSAnthony Wilson 
1434d500549bSAnthony Wilson             // Build map of LED object paths to connections
1435fe04d49cSNan Zhou             std::shared_ptr<std::map<std::string, std::string>> ledConnections =
1436fe04d49cSNan Zhou                 std::make_shared<std::map<std::string, std::string>>();
1437d500549bSAnthony Wilson 
1438d500549bSAnthony Wilson             // Loop through objects from GetSubTree
1439bd79bce8SPatrick Williams             for (const std::pair<std::string,
1440bd79bce8SPatrick Williams                                  std::vector<std::pair<
1441bd79bce8SPatrick Williams                                      std::string, std::vector<std::string>>>>&
1442d500549bSAnthony Wilson                      object : subtree)
1443d500549bSAnthony Wilson             {
1444bd79bce8SPatrick Williams                 // Check if object path is LED for one of the specified
1445bd79bce8SPatrick Williams                 // inventory items
1446d500549bSAnthony Wilson                 const std::string& ledPath = object.first;
1447bd79bce8SPatrick Williams                 if (findInventoryItemForLed(*inventoryItems, ledPath) !=
1448bd79bce8SPatrick Williams                     nullptr)
1449d500549bSAnthony Wilson                 {
1450d500549bSAnthony Wilson                     // Add mapping from ledPath to connection
1451bd79bce8SPatrick Williams                     const std::string& connection =
1452bd79bce8SPatrick Williams                         object.second.begin()->first;
1453d500549bSAnthony Wilson                     (*ledConnections)[ledPath] = connection;
1454bd79bce8SPatrick Williams                     BMCWEB_LOG_DEBUG("Added mapping {} -> {}", ledPath,
1455bd79bce8SPatrick Williams                                      connection);
1456d500549bSAnthony Wilson                 }
1457d500549bSAnthony Wilson             }
1458d500549bSAnthony Wilson 
1459bd79bce8SPatrick Williams             getInventoryLedData(sensorsAsyncResp, inventoryItems,
1460bd79bce8SPatrick Williams                                 ledConnections, std::move(callback));
146162598e31SEd Tanous             BMCWEB_LOG_DEBUG("getInventoryLeds respHandler exit");
1462e99073f5SGeorge Liu         });
146362598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryLeds exit");
1464d500549bSAnthony Wilson }
1465d500549bSAnthony Wilson 
1466d500549bSAnthony Wilson /**
146742cbe538SGunnar Mills  * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
146842cbe538SGunnar Mills  *
146942cbe538SGunnar Mills  * Uses the specified connections (services) (currently assumes just one) to
147042cbe538SGunnar Mills  * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
147142cbe538SGunnar Mills  * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
147242cbe538SGunnar Mills  *
147342cbe538SGunnar Mills  * This data is later used to provide sensor property values in the JSON
147442cbe538SGunnar Mills  * response.
147542cbe538SGunnar Mills  *
147642cbe538SGunnar Mills  * Finds the Power Supply Attributes data asynchronously.  Invokes callback
147742cbe538SGunnar Mills  * when data has been obtained.
147842cbe538SGunnar Mills  *
147942cbe538SGunnar Mills  * The callback must have the following signature:
148042cbe538SGunnar Mills  *   @code
148142cbe538SGunnar Mills  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
148242cbe538SGunnar Mills  *   @endcode
148342cbe538SGunnar Mills  *
148442cbe538SGunnar Mills  * @param sensorsAsyncResp Pointer to object holding response data.
148542cbe538SGunnar Mills  * @param inventoryItems D-Bus inventory items associated with sensors.
148642cbe538SGunnar Mills  * @param psAttributesConnections Connections that provide data for the Power
148742cbe538SGunnar Mills  *        Supply Attributes
148842cbe538SGunnar Mills  * @param callback Callback to invoke when data has been obtained.
148942cbe538SGunnar Mills  */
149042cbe538SGunnar Mills template <typename Callback>
149142cbe538SGunnar Mills void getPowerSupplyAttributesData(
1492b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
149342cbe538SGunnar Mills     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1494fe04d49cSNan Zhou     const std::map<std::string, std::string>& psAttributesConnections,
149542cbe538SGunnar Mills     Callback&& callback)
149642cbe538SGunnar Mills {
149762598e31SEd Tanous     BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData enter");
149842cbe538SGunnar Mills 
149942cbe538SGunnar Mills     if (psAttributesConnections.empty())
150042cbe538SGunnar Mills     {
150162598e31SEd Tanous         BMCWEB_LOG_DEBUG("Can't find PowerSupplyAttributes, no connections!");
150242cbe538SGunnar Mills         callback(inventoryItems);
150342cbe538SGunnar Mills         return;
150442cbe538SGunnar Mills     }
150542cbe538SGunnar Mills 
150642cbe538SGunnar Mills     // Assuming just one connection (service) for now
1507fe04d49cSNan Zhou     auto it = psAttributesConnections.begin();
150842cbe538SGunnar Mills 
150942cbe538SGunnar Mills     const std::string& psAttributesPath = (*it).first;
151042cbe538SGunnar Mills     const std::string& psAttributesConnection = (*it).second;
151142cbe538SGunnar Mills 
151242cbe538SGunnar Mills     // Response handler for Get DeratingFactor property
15135a39f77aSPatrick Williams     auto respHandler = [sensorsAsyncResp, inventoryItems,
15148cb2c024SEd Tanous                         callback = std::forward<Callback>(callback)](
15155a39f77aSPatrick Williams                            const boost::system::error_code& ec,
15164e0d8789SEd Tanous                            uint32_t value) mutable {
151762598e31SEd Tanous         BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData respHandler enter");
151842cbe538SGunnar Mills         if (ec)
151942cbe538SGunnar Mills         {
152062598e31SEd Tanous             BMCWEB_LOG_ERROR(
152162598e31SEd Tanous                 "getPowerSupplyAttributesData respHandler DBus error {}", ec);
15228d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
152342cbe538SGunnar Mills             return;
152442cbe538SGunnar Mills         }
152542cbe538SGunnar Mills 
152662598e31SEd Tanous         BMCWEB_LOG_DEBUG("PS EfficiencyPercent value: {}", value);
152742cbe538SGunnar Mills         // Store value in Power Supply Inventory Items
152842cbe538SGunnar Mills         for (InventoryItem& inventoryItem : *inventoryItems)
152942cbe538SGunnar Mills         {
153055f79e6fSEd Tanous             if (inventoryItem.isPowerSupply)
153142cbe538SGunnar Mills             {
153242cbe538SGunnar Mills                 inventoryItem.powerSupplyEfficiencyPercent =
15331e1e598dSJonathan Doman                     static_cast<int>(value);
153442cbe538SGunnar Mills             }
153542cbe538SGunnar Mills         }
153642cbe538SGunnar Mills 
153762598e31SEd Tanous         BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData respHandler exit");
153842cbe538SGunnar Mills         callback(inventoryItems);
153942cbe538SGunnar Mills     };
154042cbe538SGunnar Mills 
154142cbe538SGunnar Mills     // Get the DeratingFactor property for the PowerSupplyAttributes
154242cbe538SGunnar Mills     // Currently only property on the interface/only one we care about
15431e1e598dSJonathan Doman     sdbusplus::asio::getProperty<uint32_t>(
15441e1e598dSJonathan Doman         *crow::connections::systemBus, psAttributesConnection, psAttributesPath,
15451e1e598dSJonathan Doman         "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor",
15461e1e598dSJonathan Doman         std::move(respHandler));
154742cbe538SGunnar Mills 
154862598e31SEd Tanous     BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData exit");
154942cbe538SGunnar Mills }
155042cbe538SGunnar Mills 
155142cbe538SGunnar Mills /**
155242cbe538SGunnar Mills  * @brief Gets the Power Supply Attributes such as EfficiencyPercent
155342cbe538SGunnar Mills  *
155442cbe538SGunnar Mills  * Gets the D-Bus connection (service) that provides Power Supply Attributes
155542cbe538SGunnar Mills  * data. Then gets the Power Supply Attributes data from the connection
155642cbe538SGunnar Mills  * (currently just assumes 1 connection) and stores the data in the inventory
155742cbe538SGunnar Mills  * item.
155842cbe538SGunnar Mills  *
155942cbe538SGunnar Mills  * This data is later used to provide sensor property values in the JSON
156042cbe538SGunnar Mills  * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
156142cbe538SGunnar Mills  *
156242cbe538SGunnar Mills  * Finds the Power Supply Attributes data asynchronously. Invokes callback
156342cbe538SGunnar Mills  * when information has been obtained.
156442cbe538SGunnar Mills  *
156542cbe538SGunnar Mills  * The callback must have the following signature:
156642cbe538SGunnar Mills  *   @code
156742cbe538SGunnar Mills  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
156842cbe538SGunnar Mills  *   @endcode
156942cbe538SGunnar Mills  *
157042cbe538SGunnar Mills  * @param sensorsAsyncResp Pointer to object holding response data.
157142cbe538SGunnar Mills  * @param inventoryItems D-Bus inventory items associated with sensors.
157242cbe538SGunnar Mills  * @param callback Callback to invoke when data has been obtained.
157342cbe538SGunnar Mills  */
157442cbe538SGunnar Mills template <typename Callback>
157542cbe538SGunnar Mills void getPowerSupplyAttributes(
157642cbe538SGunnar Mills     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
157742cbe538SGunnar Mills     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
157842cbe538SGunnar Mills     Callback&& callback)
157942cbe538SGunnar Mills {
158062598e31SEd Tanous     BMCWEB_LOG_DEBUG("getPowerSupplyAttributes enter");
158142cbe538SGunnar Mills 
158242cbe538SGunnar Mills     // Only need the power supply attributes when the Power Schema
15830c728b42SJanet Adkins     if (sensorsAsyncResp->chassisSubNode != sensors::powerNodeStr)
158442cbe538SGunnar Mills     {
158562598e31SEd Tanous         BMCWEB_LOG_DEBUG("getPowerSupplyAttributes exit since not Power");
158642cbe538SGunnar Mills         callback(inventoryItems);
158742cbe538SGunnar Mills         return;
158842cbe538SGunnar Mills     }
158942cbe538SGunnar Mills 
1590e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
159142cbe538SGunnar Mills         "xyz.openbmc_project.Control.PowerSupplyAttributes"};
159242cbe538SGunnar Mills 
1593e99073f5SGeorge Liu     // Make call to ObjectMapper to find the PowerSupplyAttributes service
1594e99073f5SGeorge Liu     dbus::utility::getSubTree(
1595e99073f5SGeorge Liu         "/xyz/openbmc_project", 0, interfaces,
15968cb2c024SEd Tanous         [callback = std::forward<Callback>(callback), sensorsAsyncResp,
1597b9d36b47SEd Tanous          inventoryItems](
1598e99073f5SGeorge Liu             const boost::system::error_code& ec,
15994e0d8789SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) mutable {
1600e99073f5SGeorge Liu             // Response handler for parsing output from GetSubTree
160162598e31SEd Tanous             BMCWEB_LOG_DEBUG("getPowerSupplyAttributes respHandler enter");
160242cbe538SGunnar Mills             if (ec)
160342cbe538SGunnar Mills             {
16048d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
160562598e31SEd Tanous                 BMCWEB_LOG_ERROR(
160662598e31SEd Tanous                     "getPowerSupplyAttributes respHandler DBus error {}", ec);
160742cbe538SGunnar Mills                 return;
160842cbe538SGunnar Mills             }
160926f6976fSEd Tanous             if (subtree.empty())
161042cbe538SGunnar Mills             {
161162598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Can't find Power Supply Attributes!");
161242cbe538SGunnar Mills                 callback(inventoryItems);
161342cbe538SGunnar Mills                 return;
161442cbe538SGunnar Mills             }
161542cbe538SGunnar Mills 
161642cbe538SGunnar Mills             // Currently we only support 1 power supply attribute, use this for
161742cbe538SGunnar Mills             // all the power supplies. Build map of object path to connection.
161842cbe538SGunnar Mills             // Assume just 1 connection and 1 path for now.
1619fe04d49cSNan Zhou             std::map<std::string, std::string> psAttributesConnections;
162042cbe538SGunnar Mills 
162142cbe538SGunnar Mills             if (subtree[0].first.empty() || subtree[0].second.empty())
162242cbe538SGunnar Mills             {
162362598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Power Supply Attributes mapper error!");
162442cbe538SGunnar Mills                 callback(inventoryItems);
162542cbe538SGunnar Mills                 return;
162642cbe538SGunnar Mills             }
162742cbe538SGunnar Mills 
162842cbe538SGunnar Mills             const std::string& psAttributesPath = subtree[0].first;
162942cbe538SGunnar Mills             const std::string& connection = subtree[0].second.begin()->first;
163042cbe538SGunnar Mills 
163142cbe538SGunnar Mills             if (connection.empty())
163242cbe538SGunnar Mills             {
163362598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Power Supply Attributes mapper error!");
163442cbe538SGunnar Mills                 callback(inventoryItems);
163542cbe538SGunnar Mills                 return;
163642cbe538SGunnar Mills             }
163742cbe538SGunnar Mills 
163842cbe538SGunnar Mills             psAttributesConnections[psAttributesPath] = connection;
163962598e31SEd Tanous             BMCWEB_LOG_DEBUG("Added mapping {} -> {}", psAttributesPath,
164062598e31SEd Tanous                              connection);
164142cbe538SGunnar Mills 
164242cbe538SGunnar Mills             getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
164342cbe538SGunnar Mills                                          psAttributesConnections,
164442cbe538SGunnar Mills                                          std::move(callback));
164562598e31SEd Tanous             BMCWEB_LOG_DEBUG("getPowerSupplyAttributes respHandler exit");
1646e99073f5SGeorge Liu         });
164762598e31SEd Tanous     BMCWEB_LOG_DEBUG("getPowerSupplyAttributes exit");
164842cbe538SGunnar Mills }
164942cbe538SGunnar Mills 
165042cbe538SGunnar Mills /**
1651adc4f0dbSShawn McCarney  * @brief Gets inventory items associated with sensors.
16528fb49dd6SShawn McCarney  *
16538fb49dd6SShawn McCarney  * Finds the inventory items that are associated with the specified sensors.
1654adc4f0dbSShawn McCarney  * Then gets D-Bus data for the inventory items, such as presence and VPD.
16558fb49dd6SShawn McCarney  *
1656adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
1657adc4f0dbSShawn McCarney  * response.
16588fb49dd6SShawn McCarney  *
1659adc4f0dbSShawn McCarney  * Finds the inventory items asynchronously.  Invokes callback when the
1660adc4f0dbSShawn McCarney  * inventory items have been obtained.
1661adc4f0dbSShawn McCarney  *
1662adc4f0dbSShawn McCarney  * The callback must have the following signature:
1663adc4f0dbSShawn McCarney  *   @code
1664adc4f0dbSShawn McCarney  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
1665adc4f0dbSShawn McCarney  *   @endcode
16668fb49dd6SShawn McCarney  *
16678fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
16688fb49dd6SShawn McCarney  * @param sensorNames All sensors within the current chassis.
16698fb49dd6SShawn McCarney  * implements ObjectManager.
1670adc4f0dbSShawn McCarney  * @param callback Callback to invoke when inventory items have been obtained.
16718fb49dd6SShawn McCarney  */
1672adc4f0dbSShawn McCarney template <typename Callback>
1673d0090733SEd Tanous static void
1674d0090733SEd Tanous     getInventoryItems(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1675fe04d49cSNan Zhou                       const std::shared_ptr<std::set<std::string>> sensorNames,
1676adc4f0dbSShawn McCarney                       Callback&& callback)
16778fb49dd6SShawn McCarney {
167862598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryItems enter");
1679adc4f0dbSShawn McCarney     auto getInventoryItemAssociationsCb =
16808cb2c024SEd Tanous         [sensorsAsyncResp, callback = std::forward<Callback>(callback)](
16814e0d8789SEd Tanous             std::shared_ptr<std::vector<InventoryItem>>
16824e0d8789SEd Tanous                 inventoryItems) mutable {
168362598e31SEd Tanous             BMCWEB_LOG_DEBUG("getInventoryItemAssociationsCb enter");
16848fb49dd6SShawn McCarney             auto getInventoryItemsConnectionsCb =
1685d0090733SEd Tanous                 [sensorsAsyncResp, inventoryItems,
16864e0d8789SEd Tanous                  callback = std::forward<Callback>(callback)](
16874e0d8789SEd Tanous                     std::shared_ptr<std::set<std::string>>
16884e0d8789SEd Tanous                         invConnections) mutable {
168962598e31SEd Tanous                     BMCWEB_LOG_DEBUG("getInventoryItemsConnectionsCb enter");
1690bd79bce8SPatrick Williams                     auto getInventoryItemsDataCb =
1691bd79bce8SPatrick Williams                         [sensorsAsyncResp, inventoryItems,
16924e0d8789SEd Tanous                          callback =
16934e0d8789SEd Tanous                              std::forward<Callback>(callback)]() mutable {
169462598e31SEd Tanous                             BMCWEB_LOG_DEBUG("getInventoryItemsDataCb enter");
169542cbe538SGunnar Mills 
1696bd79bce8SPatrick Williams                             auto getInventoryLedsCb =
1697bd79bce8SPatrick Williams                                 [sensorsAsyncResp, inventoryItems,
16984e0d8789SEd Tanous                                  callback = std::forward<Callback>(
16994e0d8789SEd Tanous                                      callback)]() mutable {
1700bd79bce8SPatrick Williams                                     BMCWEB_LOG_DEBUG(
1701bd79bce8SPatrick Williams                                         "getInventoryLedsCb enter");
1702bd79bce8SPatrick Williams                                     // Find Power Supply Attributes and get the
1703bd79bce8SPatrick Williams                                     // data
1704bd79bce8SPatrick Williams                                     getPowerSupplyAttributes(
1705bd79bce8SPatrick Williams                                         sensorsAsyncResp, inventoryItems,
170642cbe538SGunnar Mills                                         std::move(callback));
170762598e31SEd Tanous                                     BMCWEB_LOG_DEBUG("getInventoryLedsCb exit");
170842cbe538SGunnar Mills                                 };
170942cbe538SGunnar Mills 
1710d500549bSAnthony Wilson                             // Find led connections and get the data
1711d500549bSAnthony Wilson                             getInventoryLeds(sensorsAsyncResp, inventoryItems,
171242cbe538SGunnar Mills                                              std::move(getInventoryLedsCb));
171362598e31SEd Tanous                             BMCWEB_LOG_DEBUG("getInventoryItemsDataCb exit");
1714d500549bSAnthony Wilson                         };
17158fb49dd6SShawn McCarney 
1716adc4f0dbSShawn McCarney                     // Get inventory item data from connections
1717adc4f0dbSShawn McCarney                     getInventoryItemsData(sensorsAsyncResp, inventoryItems,
1718d0090733SEd Tanous                                           invConnections,
1719d500549bSAnthony Wilson                                           std::move(getInventoryItemsDataCb));
172062598e31SEd Tanous                     BMCWEB_LOG_DEBUG("getInventoryItemsConnectionsCb exit");
17218fb49dd6SShawn McCarney                 };
17228fb49dd6SShawn McCarney 
1723adc4f0dbSShawn McCarney             // Get connections that provide inventory item data
1724bd79bce8SPatrick Williams             getInventoryItemsConnections(
1725bd79bce8SPatrick Williams                 sensorsAsyncResp, inventoryItems,
17268fb49dd6SShawn McCarney                 std::move(getInventoryItemsConnectionsCb));
172762598e31SEd Tanous             BMCWEB_LOG_DEBUG("getInventoryItemAssociationsCb exit");
17288fb49dd6SShawn McCarney         };
17298fb49dd6SShawn McCarney 
1730adc4f0dbSShawn McCarney     // Get associations from sensors to inventory items
1731d0090733SEd Tanous     getInventoryItemAssociations(sensorsAsyncResp, sensorNames,
1732adc4f0dbSShawn McCarney                                  std::move(getInventoryItemAssociationsCb));
173362598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryItems exit");
1734adc4f0dbSShawn McCarney }
1735adc4f0dbSShawn McCarney 
1736adc4f0dbSShawn McCarney /**
1737adc4f0dbSShawn McCarney  * @brief Returns JSON PowerSupply object for the specified inventory item.
1738adc4f0dbSShawn McCarney  *
1739adc4f0dbSShawn McCarney  * Searches for a JSON PowerSupply object that matches the specified inventory
1740adc4f0dbSShawn McCarney  * item.  If one is not found, a new PowerSupply object is added to the JSON
1741adc4f0dbSShawn McCarney  * array.
1742adc4f0dbSShawn McCarney  *
1743adc4f0dbSShawn McCarney  * Multiple sensors are often associated with one power supply inventory item.
1744adc4f0dbSShawn McCarney  * As a result, multiple sensor values are stored in one JSON PowerSupply
1745adc4f0dbSShawn McCarney  * object.
1746adc4f0dbSShawn McCarney  *
1747adc4f0dbSShawn McCarney  * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
1748adc4f0dbSShawn McCarney  * @param inventoryItem Inventory item for the power supply.
1749adc4f0dbSShawn McCarney  * @param chassisId Chassis that contains the power supply.
1750adc4f0dbSShawn McCarney  * @return JSON PowerSupply object for the specified inventory item.
1751adc4f0dbSShawn McCarney  */
175223a21a1cSEd Tanous inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
1753adc4f0dbSShawn McCarney                                       const InventoryItem& inventoryItem,
1754adc4f0dbSShawn McCarney                                       const std::string& chassisId)
1755adc4f0dbSShawn McCarney {
175618f8f608SEd Tanous     std::string nameS;
17576f4bd290SAlexander Hansen     nameS.resize(inventoryItem.name.size());
175818f8f608SEd Tanous     std::ranges::replace_copy(inventoryItem.name, nameS.begin(), '_', ' ');
1759adc4f0dbSShawn McCarney     // Check if matching PowerSupply object already exists in JSON array
1760adc4f0dbSShawn McCarney     for (nlohmann::json& powerSupply : powerSupplyArray)
1761adc4f0dbSShawn McCarney     {
176218f8f608SEd Tanous         nlohmann::json::iterator nameIt = powerSupply.find("Name");
176318f8f608SEd Tanous         if (nameIt == powerSupply.end())
176418f8f608SEd Tanous         {
176518f8f608SEd Tanous             continue;
176618f8f608SEd Tanous         }
176718f8f608SEd Tanous         const std::string* name = nameIt->get_ptr<std::string*>();
176818f8f608SEd Tanous         if (name == nullptr)
176918f8f608SEd Tanous         {
177018f8f608SEd Tanous             continue;
177118f8f608SEd Tanous         }
177218f8f608SEd Tanous         if (nameS == *name)
1773adc4f0dbSShawn McCarney         {
1774adc4f0dbSShawn McCarney             return powerSupply;
1775adc4f0dbSShawn McCarney         }
1776adc4f0dbSShawn McCarney     }
1777adc4f0dbSShawn McCarney 
1778adc4f0dbSShawn McCarney     // Add new PowerSupply object to JSON array
1779adc4f0dbSShawn McCarney     powerSupplyArray.push_back({});
1780adc4f0dbSShawn McCarney     nlohmann::json& powerSupply = powerSupplyArray.back();
1781bd79bce8SPatrick Williams     boost::urls::url url =
1782bd79bce8SPatrick Williams         boost::urls::format("/redfish/v1/Chassis/{}/Power", chassisId);
1783eddfc437SWilly Tu     url.set_fragment(("/PowerSupplies"_json_pointer).to_string());
1784eddfc437SWilly Tu     powerSupply["@odata.id"] = std::move(url);
178518f8f608SEd Tanous     std::string escaped;
17866f4bd290SAlexander Hansen     escaped.resize(inventoryItem.name.size());
178718f8f608SEd Tanous     std::ranges::replace_copy(inventoryItem.name, escaped.begin(), '_', ' ');
178818f8f608SEd Tanous     powerSupply["Name"] = std::move(escaped);
1789adc4f0dbSShawn McCarney     powerSupply["Manufacturer"] = inventoryItem.manufacturer;
1790adc4f0dbSShawn McCarney     powerSupply["Model"] = inventoryItem.model;
1791adc4f0dbSShawn McCarney     powerSupply["PartNumber"] = inventoryItem.partNumber;
1792adc4f0dbSShawn McCarney     powerSupply["SerialNumber"] = inventoryItem.serialNumber;
1793c9563608SJanet Adkins     sensor_utils::setLedState(powerSupply, &inventoryItem);
1794adc4f0dbSShawn McCarney 
179542cbe538SGunnar Mills     if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
179642cbe538SGunnar Mills     {
179742cbe538SGunnar Mills         powerSupply["EfficiencyPercent"] =
179842cbe538SGunnar Mills             inventoryItem.powerSupplyEfficiencyPercent;
179942cbe538SGunnar Mills     }
180042cbe538SGunnar Mills 
1801c9563608SJanet Adkins     powerSupply["Status"]["State"] =
1802c9563608SJanet Adkins         sensor_utils::getState(&inventoryItem, true);
1803adc4f0dbSShawn McCarney     const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
1804adc4f0dbSShawn McCarney     powerSupply["Status"]["Health"] = health;
1805adc4f0dbSShawn McCarney 
1806adc4f0dbSShawn McCarney     return powerSupply;
18078fb49dd6SShawn McCarney }
18088fb49dd6SShawn McCarney 
18098fb49dd6SShawn McCarney /**
1810de629b6eSShawn McCarney  * @brief Gets the values of the specified sensors.
1811de629b6eSShawn McCarney  *
1812de629b6eSShawn McCarney  * Stores the results as JSON in the SensorsAsyncResp.
1813de629b6eSShawn McCarney  *
1814de629b6eSShawn McCarney  * Gets the sensor values asynchronously.  Stores the results later when the
1815de629b6eSShawn McCarney  * information has been obtained.
1816de629b6eSShawn McCarney  *
1817adc4f0dbSShawn McCarney  * The sensorNames set contains all requested sensors for the current chassis.
1818de629b6eSShawn McCarney  *
1819de629b6eSShawn McCarney  * To minimize the number of DBus calls, the DBus method
1820de629b6eSShawn McCarney  * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
1821de629b6eSShawn McCarney  * values of all sensors provided by a connection (service).
1822de629b6eSShawn McCarney  *
1823de629b6eSShawn McCarney  * The connections set contains all the connections that provide sensor values.
1824de629b6eSShawn McCarney  *
1825adc4f0dbSShawn McCarney  * The InventoryItem vector contains D-Bus inventory items associated with the
1826adc4f0dbSShawn McCarney  * sensors.  Inventory item data is needed for some Redfish sensor properties.
1827adc4f0dbSShawn McCarney  *
1828de629b6eSShawn McCarney  * @param SensorsAsyncResp Pointer to object holding response data.
1829adc4f0dbSShawn McCarney  * @param sensorNames All requested sensors within the current chassis.
1830de629b6eSShawn McCarney  * @param connections Connections that provide sensor values.
1831de629b6eSShawn McCarney  * implements ObjectManager.
1832adc4f0dbSShawn McCarney  * @param inventoryItems Inventory items associated with the sensors.
1833de629b6eSShawn McCarney  */
183423a21a1cSEd Tanous inline void getSensorData(
183581ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1836fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& sensorNames,
1837fe04d49cSNan Zhou     const std::set<std::string>& connections,
1838b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems)
1839de629b6eSShawn McCarney {
184062598e31SEd Tanous     BMCWEB_LOG_DEBUG("getSensorData enter");
1841de629b6eSShawn McCarney     // Get managed objects from all services exposing sensors
1842de629b6eSShawn McCarney     for (const std::string& connection : connections)
1843de629b6eSShawn McCarney     {
18445eb468daSGeorge Liu         sdbusplus::message::object_path sensorPath(
18455eb468daSGeorge Liu             "/xyz/openbmc_project/sensors");
18465eb468daSGeorge Liu         dbus::utility::getManagedObjects(
18475eb468daSGeorge Liu             connection, sensorPath,
1848002d39b4SEd Tanous             [sensorsAsyncResp, sensorNames,
18495e7e2dc5SEd Tanous              inventoryItems](const boost::system::error_code& ec,
185002cad96eSEd Tanous                              const dbus::utility::ManagedObjectType& resp) {
185162598e31SEd Tanous                 BMCWEB_LOG_DEBUG("getManagedObjectsCb enter");
1852de629b6eSShawn McCarney                 if (ec)
1853de629b6eSShawn McCarney                 {
185462598e31SEd Tanous                     BMCWEB_LOG_ERROR("getManagedObjectsCb DBUS error: {}", ec);
18558d1b46d7Szhanghch05                     messages::internalError(sensorsAsyncResp->asyncResp->res);
1856de629b6eSShawn McCarney                     return;
1857de629b6eSShawn McCarney                 }
18580c728b42SJanet Adkins                 auto chassisSubNode = sensor_utils::chassisSubNodeFromString(
18590c728b42SJanet Adkins                     sensorsAsyncResp->chassisSubNode);
1860de629b6eSShawn McCarney                 // Go through all objects and update response with sensor data
1861de629b6eSShawn McCarney                 for (const auto& objDictEntry : resp)
1862de629b6eSShawn McCarney                 {
1863de629b6eSShawn McCarney                     const std::string& objPath =
1864de629b6eSShawn McCarney                         static_cast<const std::string&>(objDictEntry.first);
186562598e31SEd Tanous                     BMCWEB_LOG_DEBUG("getManagedObjectsCb parsing object {}",
186662598e31SEd Tanous                                      objPath);
1867de629b6eSShawn McCarney 
1868de629b6eSShawn McCarney                     std::vector<std::string> split;
1869de629b6eSShawn McCarney                     // Reserve space for
1870de629b6eSShawn McCarney                     // /xyz/openbmc_project/sensors/<name>/<subname>
1871de629b6eSShawn McCarney                     split.reserve(6);
187250ebd4afSEd Tanous                     // NOLINTNEXTLINE
187350ebd4afSEd Tanous                     bmcweb::split(split, objPath, '/');
1874de629b6eSShawn McCarney                     if (split.size() < 6)
1875de629b6eSShawn McCarney                     {
187662598e31SEd Tanous                         BMCWEB_LOG_ERROR("Got path that isn't long enough {}",
187762598e31SEd Tanous                                          objPath);
1878de629b6eSShawn McCarney                         continue;
1879de629b6eSShawn McCarney                     }
188050ebd4afSEd Tanous                     // These indexes aren't intuitive, as split puts an empty
1881de629b6eSShawn McCarney                     // string at the beginning
1882de629b6eSShawn McCarney                     const std::string& sensorType = split[4];
1883de629b6eSShawn McCarney                     const std::string& sensorName = split[5];
188462598e31SEd Tanous                     BMCWEB_LOG_DEBUG("sensorName {} sensorType {}", sensorName,
188562598e31SEd Tanous                                      sensorType);
188649c53ac9SJohnathan Mantey                     if (sensorNames->find(objPath) == sensorNames->end())
1887de629b6eSShawn McCarney                     {
188862598e31SEd Tanous                         BMCWEB_LOG_DEBUG("{} not in sensor list ", sensorName);
1889de629b6eSShawn McCarney                         continue;
1890de629b6eSShawn McCarney                     }
1891de629b6eSShawn McCarney 
1892adc4f0dbSShawn McCarney                     // Find inventory item (if any) associated with sensor
1893adc4f0dbSShawn McCarney                     InventoryItem* inventoryItem =
1894adc4f0dbSShawn McCarney                         findInventoryItemForSensor(inventoryItems, objPath);
1895adc4f0dbSShawn McCarney 
189695a3ecadSAnthony Wilson                     const std::string& sensorSchema =
189781ce609eSEd Tanous                         sensorsAsyncResp->chassisSubNode;
189895a3ecadSAnthony Wilson 
189995a3ecadSAnthony Wilson                     nlohmann::json* sensorJson = nullptr;
190095a3ecadSAnthony Wilson 
19010c728b42SJanet Adkins                     if (sensorSchema == sensors::sensorsNodeStr &&
1902928fefb9SNan Zhou                         !sensorsAsyncResp->efficientExpand)
190395a3ecadSAnthony Wilson                     {
19041516c21bSJanet Adkins                         std::string sensorId =
19051516c21bSJanet Adkins                             redfish::sensor_utils::getSensorId(sensorName,
19061516c21bSJanet Adkins                                                                sensorType);
1907c1d019a6SEd Tanous 
1908bd79bce8SPatrick Williams                         sensorsAsyncResp->asyncResp->res
1909bd79bce8SPatrick Williams                             .jsonValue["@odata.id"] = boost::urls::format(
1910bd79bce8SPatrick Williams                             "/redfish/v1/Chassis/{}/{}/{}",
1911c1d019a6SEd Tanous                             sensorsAsyncResp->chassisId,
1912bd79bce8SPatrick Williams                             sensorsAsyncResp->chassisSubNode, sensorId);
1913bd79bce8SPatrick Williams                         sensorJson =
1914bd79bce8SPatrick Williams                             &(sensorsAsyncResp->asyncResp->res.jsonValue);
191595a3ecadSAnthony Wilson                     }
191695a3ecadSAnthony Wilson                     else
191795a3ecadSAnthony Wilson                     {
1918271584abSEd Tanous                         std::string fieldName;
1919928fefb9SNan Zhou                         if (sensorsAsyncResp->efficientExpand)
1920928fefb9SNan Zhou                         {
1921928fefb9SNan Zhou                             fieldName = "Members";
1922928fefb9SNan Zhou                         }
1923928fefb9SNan Zhou                         else if (sensorType == "temperature")
1924de629b6eSShawn McCarney                         {
1925de629b6eSShawn McCarney                             fieldName = "Temperatures";
1926de629b6eSShawn McCarney                         }
1927bd79bce8SPatrick Williams                         else if (sensorType == "fan" ||
1928bd79bce8SPatrick Williams                                  sensorType == "fan_tach" ||
1929de629b6eSShawn McCarney                                  sensorType == "fan_pwm")
1930de629b6eSShawn McCarney                         {
1931de629b6eSShawn McCarney                             fieldName = "Fans";
1932de629b6eSShawn McCarney                         }
1933de629b6eSShawn McCarney                         else if (sensorType == "voltage")
1934de629b6eSShawn McCarney                         {
1935de629b6eSShawn McCarney                             fieldName = "Voltages";
1936de629b6eSShawn McCarney                         }
1937de629b6eSShawn McCarney                         else if (sensorType == "power")
1938de629b6eSShawn McCarney                         {
193955f79e6fSEd Tanous                             if (sensorName == "total_power")
1940028f7ebcSEddie James                             {
1941028f7ebcSEddie James                                 fieldName = "PowerControl";
1942028f7ebcSEddie James                             }
1943adc4f0dbSShawn McCarney                             else if ((inventoryItem != nullptr) &&
1944adc4f0dbSShawn McCarney                                      (inventoryItem->isPowerSupply))
1945028f7ebcSEddie James                             {
1946de629b6eSShawn McCarney                                 fieldName = "PowerSupplies";
1947de629b6eSShawn McCarney                             }
1948adc4f0dbSShawn McCarney                             else
1949adc4f0dbSShawn McCarney                             {
1950adc4f0dbSShawn McCarney                                 // Other power sensors are in SensorCollection
1951adc4f0dbSShawn McCarney                                 continue;
1952adc4f0dbSShawn McCarney                             }
1953028f7ebcSEddie James                         }
1954de629b6eSShawn McCarney                         else
1955de629b6eSShawn McCarney                         {
1956bd79bce8SPatrick Williams                             BMCWEB_LOG_ERROR(
1957bd79bce8SPatrick Williams                                 "Unsure how to handle sensorType {}",
195862598e31SEd Tanous                                 sensorType);
1959de629b6eSShawn McCarney                             continue;
1960de629b6eSShawn McCarney                         }
1961de629b6eSShawn McCarney 
1962de629b6eSShawn McCarney                         nlohmann::json& tempArray =
1963bd79bce8SPatrick Williams                             sensorsAsyncResp->asyncResp->res
1964bd79bce8SPatrick Williams                                 .jsonValue[fieldName];
1965adc4f0dbSShawn McCarney                         if (fieldName == "PowerControl")
196649c53ac9SJohnathan Mantey                         {
1967adc4f0dbSShawn McCarney                             if (tempArray.empty())
19687ab06f49SGunnar Mills                             {
196995a3ecadSAnthony Wilson                                 // Put multiple "sensors" into a single
197095a3ecadSAnthony Wilson                                 // PowerControl. Follows MemberId naming and
197195a3ecadSAnthony Wilson                                 // naming in power.hpp.
19721476687dSEd Tanous                                 nlohmann::json::object_t power;
1973ef4c65b7SEd Tanous                                 boost::urls::url url = boost::urls::format(
1974ef4c65b7SEd Tanous                                     "/redfish/v1/Chassis/{}/{}",
1975eddfc437SWilly Tu                                     sensorsAsyncResp->chassisId,
1976eddfc437SWilly Tu                                     sensorsAsyncResp->chassisSubNode);
1977bd79bce8SPatrick Williams                                 url.set_fragment(
1978bd79bce8SPatrick Williams                                     (""_json_pointer / fieldName / "0")
1979eddfc437SWilly Tu                                         .to_string());
1980eddfc437SWilly Tu                                 power["@odata.id"] = std::move(url);
1981b2ba3072SPatrick Williams                                 tempArray.emplace_back(std::move(power));
1982adc4f0dbSShawn McCarney                             }
1983adc4f0dbSShawn McCarney                             sensorJson = &(tempArray.back());
1984adc4f0dbSShawn McCarney                         }
1985adc4f0dbSShawn McCarney                         else if (fieldName == "PowerSupplies")
1986adc4f0dbSShawn McCarney                         {
1987adc4f0dbSShawn McCarney                             if (inventoryItem != nullptr)
1988adc4f0dbSShawn McCarney                             {
1989bd79bce8SPatrick Williams                                 sensorJson = &(getPowerSupply(
1990bd79bce8SPatrick Williams                                     tempArray, *inventoryItem,
199181ce609eSEd Tanous                                     sensorsAsyncResp->chassisId));
1992adc4f0dbSShawn McCarney                             }
199349c53ac9SJohnathan Mantey                         }
1994928fefb9SNan Zhou                         else if (fieldName == "Members")
1995928fefb9SNan Zhou                         {
19961516c21bSJanet Adkins                             std::string sensorId =
19971516c21bSJanet Adkins                                 redfish::sensor_utils::getSensorId(sensorName,
19981516c21bSJanet Adkins                                                                    sensorType);
1999677bb756SEd Tanous 
20001476687dSEd Tanous                             nlohmann::json::object_t member;
2001ef4c65b7SEd Tanous                             member["@odata.id"] = boost::urls::format(
2002ef4c65b7SEd Tanous                                 "/redfish/v1/Chassis/{}/{}/{}",
2003677bb756SEd Tanous                                 sensorsAsyncResp->chassisId,
2004677bb756SEd Tanous                                 sensorsAsyncResp->chassisSubNode, sensorId);
2005b2ba3072SPatrick Williams                             tempArray.emplace_back(std::move(member));
2006928fefb9SNan Zhou                             sensorJson = &(tempArray.back());
2007928fefb9SNan Zhou                         }
200849c53ac9SJohnathan Mantey                         else
200949c53ac9SJohnathan Mantey                         {
20101476687dSEd Tanous                             nlohmann::json::object_t member;
2011ef4c65b7SEd Tanous                             boost::urls::url url = boost::urls::format(
2012ef4c65b7SEd Tanous                                 "/redfish/v1/Chassis/{}/{}",
2013eddfc437SWilly Tu                                 sensorsAsyncResp->chassisId,
2014eddfc437SWilly Tu                                 sensorsAsyncResp->chassisSubNode);
2015eddfc437SWilly Tu                             url.set_fragment(
2016eddfc437SWilly Tu                                 (""_json_pointer / fieldName).to_string());
2017eddfc437SWilly Tu                             member["@odata.id"] = std::move(url);
2018b2ba3072SPatrick Williams                             tempArray.emplace_back(std::move(member));
2019adc4f0dbSShawn McCarney                             sensorJson = &(tempArray.back());
202049c53ac9SJohnathan Mantey                         }
202195a3ecadSAnthony Wilson                     }
2022de629b6eSShawn McCarney 
2023adc4f0dbSShawn McCarney                     if (sensorJson != nullptr)
2024adc4f0dbSShawn McCarney                     {
20250c728b42SJanet Adkins                         objectInterfacesToJson(
20260c728b42SJanet Adkins                             sensorName, sensorType, chassisSubNode,
20270c728b42SJanet Adkins                             objDictEntry.second, *sensorJson, inventoryItem);
20281d7c0054SEd Tanous 
20291d7c0054SEd Tanous                         std::string path = "/xyz/openbmc_project/sensors/";
20301d7c0054SEd Tanous                         path += sensorType;
20311d7c0054SEd Tanous                         path += "/";
20321d7c0054SEd Tanous                         path += sensorName;
2033c1d019a6SEd Tanous                         sensorsAsyncResp->addMetadata(*sensorJson, path);
2034adc4f0dbSShawn McCarney                     }
2035de629b6eSShawn McCarney                 }
203681ce609eSEd Tanous                 if (sensorsAsyncResp.use_count() == 1)
203749c53ac9SJohnathan Mantey                 {
203881ce609eSEd Tanous                     sortJSONResponse(sensorsAsyncResp);
20390c728b42SJanet Adkins                     if (chassisSubNode ==
20400c728b42SJanet Adkins                             sensor_utils::ChassisSubNode::sensorsNode &&
2041928fefb9SNan Zhou                         sensorsAsyncResp->efficientExpand)
2042928fefb9SNan Zhou                     {
2043928fefb9SNan Zhou                         sensorsAsyncResp->asyncResp->res
2044928fefb9SNan Zhou                             .jsonValue["Members@odata.count"] =
2045bd79bce8SPatrick Williams                             sensorsAsyncResp->asyncResp->res
2046bd79bce8SPatrick Williams                                 .jsonValue["Members"]
2047928fefb9SNan Zhou                                 .size();
2048928fefb9SNan Zhou                     }
20490c728b42SJanet Adkins                     else if (chassisSubNode ==
20500c728b42SJanet Adkins                              sensor_utils::ChassisSubNode::thermalNode)
20518bd25ccdSJames Feist                     {
205281ce609eSEd Tanous                         populateFanRedundancy(sensorsAsyncResp);
20538bd25ccdSJames Feist                     }
205449c53ac9SJohnathan Mantey                 }
205562598e31SEd Tanous                 BMCWEB_LOG_DEBUG("getManagedObjectsCb exit");
20565eb468daSGeorge Liu             });
205723a21a1cSEd Tanous     }
205862598e31SEd Tanous     BMCWEB_LOG_DEBUG("getSensorData exit");
2059de629b6eSShawn McCarney }
2060de629b6eSShawn McCarney 
2061fe04d49cSNan Zhou inline void
2062fe04d49cSNan Zhou     processSensorList(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2063fe04d49cSNan Zhou                       const std::shared_ptr<std::set<std::string>>& sensorNames)
20641abe55efSEd Tanous {
2065fe04d49cSNan Zhou     auto getConnectionCb = [sensorsAsyncResp, sensorNames](
2066fe04d49cSNan Zhou                                const std::set<std::string>& connections) {
206762598e31SEd Tanous         BMCWEB_LOG_DEBUG("getConnectionCb enter");
2068adc4f0dbSShawn McCarney         auto getInventoryItemsCb =
2069bd79bce8SPatrick Williams             [sensorsAsyncResp, sensorNames, connections](
2070bd79bce8SPatrick Williams                 const std::shared_ptr<std::vector<InventoryItem>>&
20714e0d8789SEd Tanous                     inventoryItems) mutable {
207262598e31SEd Tanous                 BMCWEB_LOG_DEBUG("getInventoryItemsCb enter");
207349c53ac9SJohnathan Mantey                 // Get sensor data and store results in JSON
2074002d39b4SEd Tanous                 getSensorData(sensorsAsyncResp, sensorNames, connections,
2075d0090733SEd Tanous                               inventoryItems);
207662598e31SEd Tanous                 BMCWEB_LOG_DEBUG("getInventoryItemsCb exit");
2077adc4f0dbSShawn McCarney             };
2078adc4f0dbSShawn McCarney 
2079adc4f0dbSShawn McCarney         // Get inventory items associated with sensors
2080d0090733SEd Tanous         getInventoryItems(sensorsAsyncResp, sensorNames,
2081adc4f0dbSShawn McCarney                           std::move(getInventoryItemsCb));
2082adc4f0dbSShawn McCarney 
208362598e31SEd Tanous         BMCWEB_LOG_DEBUG("getConnectionCb exit");
208408777fb0SLewanczyk, Dawid     };
2085de629b6eSShawn McCarney 
2086de629b6eSShawn McCarney     // Get set of connections that provide sensor values
208781ce609eSEd Tanous     getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb));
208895a3ecadSAnthony Wilson }
208995a3ecadSAnthony Wilson 
209095a3ecadSAnthony Wilson /**
209195a3ecadSAnthony Wilson  * @brief Entry point for retrieving sensors data related to requested
209295a3ecadSAnthony Wilson  *        chassis.
209395a3ecadSAnthony Wilson  * @param SensorsAsyncResp   Pointer to object holding response data
209495a3ecadSAnthony Wilson  */
2095b5a76932SEd Tanous inline void
209681ce609eSEd Tanous     getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
209795a3ecadSAnthony Wilson {
209862598e31SEd Tanous     BMCWEB_LOG_DEBUG("getChassisData enter");
209995a3ecadSAnthony Wilson     auto getChassisCb =
210081ce609eSEd Tanous         [sensorsAsyncResp](
2101fe04d49cSNan Zhou             const std::shared_ptr<std::set<std::string>>& sensorNames) {
210262598e31SEd Tanous             BMCWEB_LOG_DEBUG("getChassisCb enter");
210381ce609eSEd Tanous             processSensorList(sensorsAsyncResp, sensorNames);
210462598e31SEd Tanous             BMCWEB_LOG_DEBUG("getChassisCb exit");
210508777fb0SLewanczyk, Dawid         };
2106928fefb9SNan Zhou     // SensorCollection doesn't contain the Redundancy property
21070c728b42SJanet Adkins     if (sensorsAsyncResp->chassisSubNode != sensors::sensorsNodeStr)
2108928fefb9SNan Zhou     {
21098d1b46d7Szhanghch05         sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] =
21108d1b46d7Szhanghch05             nlohmann::json::array();
2111928fefb9SNan Zhou     }
211226f03899SShawn McCarney     // Get set of sensors in chassis
21137f1cc26dSEd Tanous     getChassis(sensorsAsyncResp->asyncResp, sensorsAsyncResp->chassisId,
21147f1cc26dSEd Tanous                sensorsAsyncResp->chassisSubNode, sensorsAsyncResp->types,
21157f1cc26dSEd Tanous                std::move(getChassisCb));
211662598e31SEd Tanous     BMCWEB_LOG_DEBUG("getChassisData exit");
2117271584abSEd Tanous }
211808777fb0SLewanczyk, Dawid 
2119413961deSRichard Marian Thomaiyar /**
212049c53ac9SJohnathan Mantey  * @brief Find the requested sensorName in the list of all sensors supplied by
212149c53ac9SJohnathan Mantey  * the chassis node
212249c53ac9SJohnathan Mantey  *
212349c53ac9SJohnathan Mantey  * @param sensorName   The sensor name supplied in the PATCH request
212449c53ac9SJohnathan Mantey  * @param sensorsList  The list of sensors managed by the chassis node
212549c53ac9SJohnathan Mantey  * @param sensorsModified  The list of sensors that were found as a result of
212649c53ac9SJohnathan Mantey  *                         repeated calls to this function
212749c53ac9SJohnathan Mantey  */
2128bd79bce8SPatrick Williams inline bool findSensorNameUsingSensorPath(
2129bd79bce8SPatrick Williams     std::string_view sensorName, const std::set<std::string>& sensorsList,
2130fe04d49cSNan Zhou     std::set<std::string>& sensorsModified)
213149c53ac9SJohnathan Mantey {
2132fe04d49cSNan Zhou     for (const auto& chassisSensor : sensorsList)
213349c53ac9SJohnathan Mantey     {
213428aa8de5SGeorge Liu         sdbusplus::message::object_path path(chassisSensor);
2135b00dcc27SEd Tanous         std::string thisSensorName = path.filename();
213628aa8de5SGeorge Liu         if (thisSensorName.empty())
213749c53ac9SJohnathan Mantey         {
213849c53ac9SJohnathan Mantey             continue;
213949c53ac9SJohnathan Mantey         }
214049c53ac9SJohnathan Mantey         if (thisSensorName == sensorName)
214149c53ac9SJohnathan Mantey         {
214249c53ac9SJohnathan Mantey             sensorsModified.emplace(chassisSensor);
214349c53ac9SJohnathan Mantey             return true;
214449c53ac9SJohnathan Mantey         }
214549c53ac9SJohnathan Mantey     }
214649c53ac9SJohnathan Mantey     return false;
214749c53ac9SJohnathan Mantey }
214849c53ac9SJohnathan Mantey 
214949c53ac9SJohnathan Mantey /**
2150413961deSRichard Marian Thomaiyar  * @brief Entry point for overriding sensor values of given sensor
2151413961deSRichard Marian Thomaiyar  *
21528d1b46d7Szhanghch05  * @param sensorAsyncResp   response object
21534bb3dc34SCarol Wang  * @param allCollections   Collections extract from sensors' request patch info
2154413961deSRichard Marian Thomaiyar  * @param chassisSubNode   Chassis Node for which the query has to happen
2155413961deSRichard Marian Thomaiyar  */
215623a21a1cSEd Tanous inline void setSensorsOverride(
2157b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
21580885057cSEd Tanous     std::unordered_map<std::string, std::vector<nlohmann::json::object_t>>&
2159397fd61fSjayaprakash Mutyala         allCollections)
2160413961deSRichard Marian Thomaiyar {
216162598e31SEd Tanous     BMCWEB_LOG_INFO("setSensorsOverride for subNode{}",
216262598e31SEd Tanous                     sensorAsyncResp->chassisSubNode);
2163413961deSRichard Marian Thomaiyar 
2164d02aad39SEd Tanous     std::string_view propertyValueName;
2165f65af9e8SRichard Marian Thomaiyar     std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
2166413961deSRichard Marian Thomaiyar     std::string memberId;
2167543f4400SEd Tanous     double value = 0.0;
2168f65af9e8SRichard Marian Thomaiyar     for (auto& collectionItems : allCollections)
2169f65af9e8SRichard Marian Thomaiyar     {
2170f65af9e8SRichard Marian Thomaiyar         if (collectionItems.first == "Temperatures")
2171f65af9e8SRichard Marian Thomaiyar         {
2172f65af9e8SRichard Marian Thomaiyar             propertyValueName = "ReadingCelsius";
2173f65af9e8SRichard Marian Thomaiyar         }
2174f65af9e8SRichard Marian Thomaiyar         else if (collectionItems.first == "Fans")
2175f65af9e8SRichard Marian Thomaiyar         {
2176f65af9e8SRichard Marian Thomaiyar             propertyValueName = "Reading";
2177f65af9e8SRichard Marian Thomaiyar         }
2178f65af9e8SRichard Marian Thomaiyar         else
2179f65af9e8SRichard Marian Thomaiyar         {
2180f65af9e8SRichard Marian Thomaiyar             propertyValueName = "ReadingVolts";
2181f65af9e8SRichard Marian Thomaiyar         }
2182f65af9e8SRichard Marian Thomaiyar         for (auto& item : collectionItems.second)
2183f65af9e8SRichard Marian Thomaiyar         {
21840885057cSEd Tanous             if (!json_util::readJsonObject(
21850885057cSEd Tanous                     item, sensorAsyncResp->asyncResp->res, "MemberId", memberId,
21860885057cSEd Tanous                     propertyValueName, value))
2187413961deSRichard Marian Thomaiyar             {
2188413961deSRichard Marian Thomaiyar                 return;
2189413961deSRichard Marian Thomaiyar             }
2190f65af9e8SRichard Marian Thomaiyar             overrideMap.emplace(memberId,
2191f65af9e8SRichard Marian Thomaiyar                                 std::make_pair(value, collectionItems.first));
2192f65af9e8SRichard Marian Thomaiyar         }
2193f65af9e8SRichard Marian Thomaiyar     }
21944bb3dc34SCarol Wang 
2195bd79bce8SPatrick Williams     auto getChassisSensorListCb = [sensorAsyncResp, overrideMap,
2196bd79bce8SPatrick Williams                                    propertyValueNameStr =
2197bd79bce8SPatrick Williams                                        std::string(propertyValueName)](
2198bd79bce8SPatrick Williams                                       const std::shared_ptr<
2199bd79bce8SPatrick Williams                                           std::set<std::string>>& sensorsList) {
220049c53ac9SJohnathan Mantey         // Match sensor names in the PATCH request to those managed by the
220149c53ac9SJohnathan Mantey         // chassis node
2202fe04d49cSNan Zhou         const std::shared_ptr<std::set<std::string>> sensorNames =
2203fe04d49cSNan Zhou             std::make_shared<std::set<std::string>>();
2204f65af9e8SRichard Marian Thomaiyar         for (const auto& item : overrideMap)
2205413961deSRichard Marian Thomaiyar         {
2206f65af9e8SRichard Marian Thomaiyar             const auto& sensor = item.first;
2207c71d6125SEd Tanous             std::pair<std::string, std::string> sensorNameType =
22081516c21bSJanet Adkins                 redfish::sensor_utils::splitSensorNameAndType(sensor);
2209c71d6125SEd Tanous             if (!findSensorNameUsingSensorPath(sensorNameType.second,
2210c71d6125SEd Tanous                                                *sensorsList, *sensorNames))
2211f65af9e8SRichard Marian Thomaiyar             {
221262598e31SEd Tanous                 BMCWEB_LOG_INFO("Unable to find memberId {}", item.first);
22138d1b46d7Szhanghch05                 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2214f65af9e8SRichard Marian Thomaiyar                                            item.second.second, item.first);
2215413961deSRichard Marian Thomaiyar                 return;
2216413961deSRichard Marian Thomaiyar             }
2217f65af9e8SRichard Marian Thomaiyar         }
2218413961deSRichard Marian Thomaiyar         // Get the connection to which the memberId belongs
2219bd79bce8SPatrick Williams         auto getObjectsWithConnectionCb = [sensorAsyncResp, overrideMap,
2220bd79bce8SPatrick Williams                                            propertyValueNameStr](
2221bd79bce8SPatrick Williams                                               const std::set<
2222bd79bce8SPatrick Williams                                                   std::string>& /*connections*/,
2223bd79bce8SPatrick Williams                                               const std::set<std::pair<
2224bd79bce8SPatrick Williams                                                   std::string, std::string>>&
2225413961deSRichard Marian Thomaiyar                                                   objectsWithConnection) {
2226f65af9e8SRichard Marian Thomaiyar             if (objectsWithConnection.size() != overrideMap.size())
2227413961deSRichard Marian Thomaiyar             {
222862598e31SEd Tanous                 BMCWEB_LOG_INFO(
222962598e31SEd Tanous                     "Unable to find all objects with proper connection {} requested {}",
223062598e31SEd Tanous                     objectsWithConnection.size(), overrideMap.size());
2231bd79bce8SPatrick Williams                 messages::resourceNotFound(
2232bd79bce8SPatrick Williams                     sensorAsyncResp->asyncResp->res,
22330c728b42SJanet Adkins                     sensorAsyncResp->chassisSubNode == sensors::thermalNodeStr
2234413961deSRichard Marian Thomaiyar                         ? "Temperatures"
2235413961deSRichard Marian Thomaiyar                         : "Voltages",
2236f65af9e8SRichard Marian Thomaiyar                     "Count");
2237f65af9e8SRichard Marian Thomaiyar                 return;
2238f65af9e8SRichard Marian Thomaiyar             }
2239f65af9e8SRichard Marian Thomaiyar             for (const auto& item : objectsWithConnection)
2240f65af9e8SRichard Marian Thomaiyar             {
224128aa8de5SGeorge Liu                 sdbusplus::message::object_path path(item.first);
224228aa8de5SGeorge Liu                 std::string sensorName = path.filename();
224328aa8de5SGeorge Liu                 if (sensorName.empty())
2244f65af9e8SRichard Marian Thomaiyar                 {
22454f277b54SJayaprakash Mutyala                     messages::internalError(sensorAsyncResp->asyncResp->res);
2246f65af9e8SRichard Marian Thomaiyar                     return;
2247f65af9e8SRichard Marian Thomaiyar                 }
22481516c21bSJanet Adkins                 std::string id = redfish::sensor_utils::getSensorId(
22491516c21bSJanet Adkins                     sensorName, path.parent_path().filename());
2250f65af9e8SRichard Marian Thomaiyar 
22513f5eb755SBan Feng                 const auto& iterator = overrideMap.find(id);
2252f65af9e8SRichard Marian Thomaiyar                 if (iterator == overrideMap.end())
2253f65af9e8SRichard Marian Thomaiyar                 {
225462598e31SEd Tanous                     BMCWEB_LOG_INFO("Unable to find sensor object{}",
225562598e31SEd Tanous                                     item.first);
22564f277b54SJayaprakash Mutyala                     messages::internalError(sensorAsyncResp->asyncResp->res);
2257413961deSRichard Marian Thomaiyar                     return;
2258413961deSRichard Marian Thomaiyar                 }
2259e93abac6SGinu George                 setDbusProperty(sensorAsyncResp->asyncResp,
2260e93abac6SGinu George                                 propertyValueNameStr, item.second, item.first,
2261e93abac6SGinu George                                 "xyz.openbmc_project.Sensor.Value", "Value",
2262d02aad39SEd Tanous                                 iterator->second.first);
2263f65af9e8SRichard Marian Thomaiyar             }
2264413961deSRichard Marian Thomaiyar         };
2265413961deSRichard Marian Thomaiyar         // Get object with connection for the given sensor name
2266413961deSRichard Marian Thomaiyar         getObjectsWithConnection(sensorAsyncResp, sensorNames,
2267413961deSRichard Marian Thomaiyar                                  std::move(getObjectsWithConnectionCb));
2268413961deSRichard Marian Thomaiyar     };
2269413961deSRichard Marian Thomaiyar     // get full sensor list for the given chassisId and cross verify the sensor.
22707f1cc26dSEd Tanous     getChassis(sensorAsyncResp->asyncResp, sensorAsyncResp->chassisId,
22717f1cc26dSEd Tanous                sensorAsyncResp->chassisSubNode, sensorAsyncResp->types,
22727f1cc26dSEd Tanous                std::move(getChassisSensorListCb));
2273413961deSRichard Marian Thomaiyar }
2274413961deSRichard Marian Thomaiyar 
2275a0ec28b6SAdrian Ambrożewicz /**
2276a0ec28b6SAdrian Ambrożewicz  * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
2277a0ec28b6SAdrian Ambrożewicz  * path of the sensor.
2278a0ec28b6SAdrian Ambrożewicz  *
2279a0ec28b6SAdrian Ambrożewicz  * Function builds valid Redfish response for sensor query of given chassis and
2280a0ec28b6SAdrian Ambrożewicz  * node. It then builds metadata about Redfish<->D-Bus correlations and provides
2281a0ec28b6SAdrian Ambrożewicz  * it to caller in a callback.
2282a0ec28b6SAdrian Ambrożewicz  *
2283a0ec28b6SAdrian Ambrożewicz  * @param chassis   Chassis for which retrieval should be performed
2284c9563608SJanet Adkins  * @param node  Node (group) of sensors. See sensor_utils::node for supported
2285c9563608SJanet Adkins  * values
2286a0ec28b6SAdrian Ambrożewicz  * @param mapComplete   Callback to be called with retrieval result
2287a0ec28b6SAdrian Ambrożewicz  */
2288931edc79SEd Tanous template <typename Callback>
2289bd79bce8SPatrick Williams inline void retrieveUriToDbusMap(
2290bd79bce8SPatrick Williams     const std::string& chassis, const std::string& node, Callback&& mapComplete)
2291a0ec28b6SAdrian Ambrożewicz {
229202da7c5aSEd Tanous     decltype(sensors::paths)::const_iterator pathIt =
229302da7c5aSEd Tanous         std::find_if(sensors::paths.cbegin(), sensors::paths.cend(),
229402da7c5aSEd Tanous                      [&node](auto&& val) { return val.first == node; });
229502da7c5aSEd Tanous     if (pathIt == sensors::paths.cend())
2296a0ec28b6SAdrian Ambrożewicz     {
229762598e31SEd Tanous         BMCWEB_LOG_ERROR("Wrong node provided : {}", node);
22986804b5c8SEd Tanous         std::map<std::string, std::string> noop;
22996804b5c8SEd Tanous         mapComplete(boost::beast::http::status::bad_request, noop);
2300a0ec28b6SAdrian Ambrożewicz         return;
2301a0ec28b6SAdrian Ambrożewicz     }
2302d51e072fSKrzysztof Grobelny 
230372374eb7SNan Zhou     auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
2304bd79bce8SPatrick Williams     auto callback =
2305bd79bce8SPatrick Williams         [asyncResp, mapCompleteCb = std::forward<Callback>(mapComplete)](
2306a0ec28b6SAdrian Ambrożewicz             const boost::beast::http::status status,
2307fe04d49cSNan Zhou             const std::map<std::string, std::string>& uriToDbus) {
2308fe04d49cSNan Zhou             mapCompleteCb(status, uriToDbus);
2309fe04d49cSNan Zhou         };
2310a0ec28b6SAdrian Ambrożewicz 
2311a0ec28b6SAdrian Ambrożewicz     auto resp = std::make_shared<SensorsAsyncResp>(
2312d51e072fSKrzysztof Grobelny         asyncResp, chassis, pathIt->second, node, std::move(callback));
2313a0ec28b6SAdrian Ambrożewicz     getChassisData(resp);
2314a0ec28b6SAdrian Ambrożewicz }
2315a0ec28b6SAdrian Ambrożewicz 
2316bacb2162SNan Zhou namespace sensors
2317bacb2162SNan Zhou {
2318928fefb9SNan Zhou 
2319bacb2162SNan Zhou inline void getChassisCallback(
2320c1d019a6SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2321c1d019a6SEd Tanous     std::string_view chassisId, std::string_view chassisSubNode,
2322fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& sensorNames)
2323bacb2162SNan Zhou {
232462598e31SEd Tanous     BMCWEB_LOG_DEBUG("getChassisCallback enter ");
2325bacb2162SNan Zhou 
2326c1d019a6SEd Tanous     nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
2327c1d019a6SEd Tanous     for (const std::string& sensor : *sensorNames)
2328bacb2162SNan Zhou     {
232962598e31SEd Tanous         BMCWEB_LOG_DEBUG("Adding sensor: {}", sensor);
2330bacb2162SNan Zhou 
2331bacb2162SNan Zhou         sdbusplus::message::object_path path(sensor);
2332bacb2162SNan Zhou         std::string sensorName = path.filename();
2333bacb2162SNan Zhou         if (sensorName.empty())
2334bacb2162SNan Zhou         {
233562598e31SEd Tanous             BMCWEB_LOG_ERROR("Invalid sensor path: {}", sensor);
2336c1d019a6SEd Tanous             messages::internalError(asyncResp->res);
2337bacb2162SNan Zhou             return;
2338bacb2162SNan Zhou         }
2339c1d019a6SEd Tanous         std::string type = path.parent_path().filename();
23401516c21bSJanet Adkins         std::string id = redfish::sensor_utils::getSensorId(sensorName, type);
2341c1d019a6SEd Tanous 
23421476687dSEd Tanous         nlohmann::json::object_t member;
2343ef4c65b7SEd Tanous         member["@odata.id"] = boost::urls::format(
2344ef4c65b7SEd Tanous             "/redfish/v1/Chassis/{}/{}/{}", chassisId, chassisSubNode, id);
2345c1d019a6SEd Tanous 
2346b2ba3072SPatrick Williams         entriesArray.emplace_back(std::move(member));
2347bacb2162SNan Zhou     }
2348bacb2162SNan Zhou 
2349c1d019a6SEd Tanous     asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
235062598e31SEd Tanous     BMCWEB_LOG_DEBUG("getChassisCallback exit");
2351bacb2162SNan Zhou }
2352e6bd846dSNan Zhou 
2353ac106bf6SEd Tanous inline void handleSensorCollectionGet(
2354ac106bf6SEd Tanous     App& app, const crow::Request& req,
2355ac106bf6SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2356de167a6fSNan Zhou     const std::string& chassisId)
2357de167a6fSNan Zhou {
2358de167a6fSNan Zhou     query_param::QueryCapabilities capabilities = {
2359de167a6fSNan Zhou         .canDelegateExpandLevel = 1,
2360de167a6fSNan Zhou     };
2361de167a6fSNan Zhou     query_param::Query delegatedQuery;
2362ac106bf6SEd Tanous     if (!redfish::setUpRedfishRouteWithDelegation(app, req, asyncResp,
2363de167a6fSNan Zhou                                                   delegatedQuery, capabilities))
2364de167a6fSNan Zhou     {
2365de167a6fSNan Zhou         return;
2366de167a6fSNan Zhou     }
2367de167a6fSNan Zhou 
2368de167a6fSNan Zhou     if (delegatedQuery.expandType != query_param::ExpandType::None)
2369de167a6fSNan Zhou     {
2370de167a6fSNan Zhou         // we perform efficient expand.
2371ac106bf6SEd Tanous         auto sensorsAsyncResp = std::make_shared<SensorsAsyncResp>(
2372ac106bf6SEd Tanous             asyncResp, chassisId, sensors::dbus::sensorPaths,
23730c728b42SJanet Adkins             sensors::sensorsNodeStr,
2374de167a6fSNan Zhou             /*efficientExpand=*/true);
2375ac106bf6SEd Tanous         getChassisData(sensorsAsyncResp);
2376de167a6fSNan Zhou 
237762598e31SEd Tanous         BMCWEB_LOG_DEBUG(
237862598e31SEd Tanous             "SensorCollection doGet exit via efficient expand handler");
2379de167a6fSNan Zhou         return;
23800bad320cSEd Tanous     }
2381de167a6fSNan Zhou 
2382de167a6fSNan Zhou     // We get all sensors as hyperlinkes in the chassis (this
2383de167a6fSNan Zhou     // implies we reply on the default query parameters handler)
23840c728b42SJanet Adkins     getChassis(asyncResp, chassisId, sensors::sensorsNodeStr, dbus::sensorPaths,
2385ac106bf6SEd Tanous                std::bind_front(sensors::getChassisCallback, asyncResp,
23860c728b42SJanet Adkins                                chassisId, sensors::sensorsNodeStr));
2387c1d019a6SEd Tanous }
23887f1cc26dSEd Tanous 
2389c1d019a6SEd Tanous inline void
2390c1d019a6SEd Tanous     getSensorFromDbus(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2391c1d019a6SEd Tanous                       const std::string& sensorPath,
2392c1d019a6SEd Tanous                       const ::dbus::utility::MapperGetObject& mapperResponse)
2393c1d019a6SEd Tanous {
2394c1d019a6SEd Tanous     if (mapperResponse.size() != 1)
2395c1d019a6SEd Tanous     {
2396c1d019a6SEd Tanous         messages::internalError(asyncResp->res);
2397c1d019a6SEd Tanous         return;
2398c1d019a6SEd Tanous     }
2399c1d019a6SEd Tanous     const auto& valueIface = *mapperResponse.begin();
2400c1d019a6SEd Tanous     const std::string& connectionName = valueIface.first;
240162598e31SEd Tanous     BMCWEB_LOG_DEBUG("Looking up {}", connectionName);
240262598e31SEd Tanous     BMCWEB_LOG_DEBUG("Path {}", sensorPath);
2403c1343bf6SKrzysztof Grobelny 
2404c1343bf6SKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
2405c1343bf6SKrzysztof Grobelny         *crow::connections::systemBus, connectionName, sensorPath, "",
2406c1d019a6SEd Tanous         [asyncResp,
24075e7e2dc5SEd Tanous          sensorPath](const boost::system::error_code& ec,
2408c1d019a6SEd Tanous                      const ::dbus::utility::DBusPropertiesMap& valuesDict) {
2409c1d019a6SEd Tanous             if (ec)
2410c1d019a6SEd Tanous             {
2411c1d019a6SEd Tanous                 messages::internalError(asyncResp->res);
2412c1d019a6SEd Tanous                 return;
2413c1d019a6SEd Tanous             }
2414c1d019a6SEd Tanous             sdbusplus::message::object_path path(sensorPath);
2415c1d019a6SEd Tanous             std::string name = path.filename();
2416c1d019a6SEd Tanous             path = path.parent_path();
2417c1d019a6SEd Tanous             std::string type = path.filename();
2418c9563608SJanet Adkins             sensor_utils::objectPropertiesToJson(
24190c728b42SJanet Adkins                 name, type, sensor_utils::ChassisSubNode::sensorsNode,
24200c728b42SJanet Adkins                 valuesDict, asyncResp->res.jsonValue, nullptr);
2421c1343bf6SKrzysztof Grobelny         });
2422de167a6fSNan Zhou }
2423de167a6fSNan Zhou 
2424e6bd846dSNan Zhou inline void handleSensorGet(App& app, const crow::Request& req,
2425c1d019a6SEd Tanous                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2426677bb756SEd Tanous                             const std::string& chassisId,
2427c1d019a6SEd Tanous                             const std::string& sensorId)
2428e6bd846dSNan Zhou {
2429c1d019a6SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2430e6bd846dSNan Zhou     {
2431e6bd846dSNan Zhou         return;
2432e6bd846dSNan Zhou     }
2433c71d6125SEd Tanous     std::pair<std::string, std::string> nameType =
24341516c21bSJanet Adkins         redfish::sensor_utils::splitSensorNameAndType(sensorId);
2435c71d6125SEd Tanous     if (nameType.first.empty() || nameType.second.empty())
2436c1d019a6SEd Tanous     {
2437c1d019a6SEd Tanous         messages::resourceNotFound(asyncResp->res, sensorId, "Sensor");
2438c1d019a6SEd Tanous         return;
2439c1d019a6SEd Tanous     }
2440c71d6125SEd Tanous 
2441ef4c65b7SEd Tanous     asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
2442ef4c65b7SEd Tanous         "/redfish/v1/Chassis/{}/Sensors/{}", chassisId, sensorId);
2443c1d019a6SEd Tanous 
244462598e31SEd Tanous     BMCWEB_LOG_DEBUG("Sensor doGet enter");
2445e6bd846dSNan Zhou 
24462b73119cSGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
2447e6bd846dSNan Zhou         "xyz.openbmc_project.Sensor.Value"};
2448c71d6125SEd Tanous     std::string sensorPath = "/xyz/openbmc_project/sensors/" + nameType.first +
2449c71d6125SEd Tanous                              '/' + nameType.second;
2450e6bd846dSNan Zhou     // Get a list of all of the sensors that implement Sensor.Value
2451e6bd846dSNan Zhou     // and get the path and service name associated with the sensor
24522b73119cSGeorge Liu     ::dbus::utility::getDbusObject(
24532b73119cSGeorge Liu         sensorPath, interfaces,
2454aec0ec30SMyung Bae         [asyncResp, sensorId,
24552b73119cSGeorge Liu          sensorPath](const boost::system::error_code& ec,
2456c1d019a6SEd Tanous                      const ::dbus::utility::MapperGetObject& subtree) {
245762598e31SEd Tanous             BMCWEB_LOG_DEBUG("respHandler1 enter");
2458aec0ec30SMyung Bae             if (ec == boost::system::errc::io_error)
2459aec0ec30SMyung Bae             {
246062598e31SEd Tanous                 BMCWEB_LOG_WARNING("Sensor not found from getSensorPaths");
2461aec0ec30SMyung Bae                 messages::resourceNotFound(asyncResp->res, sensorId, "Sensor");
2462aec0ec30SMyung Bae                 return;
2463aec0ec30SMyung Bae             }
2464e6bd846dSNan Zhou             if (ec)
2465e6bd846dSNan Zhou             {
2466c1d019a6SEd Tanous                 messages::internalError(asyncResp->res);
246762598e31SEd Tanous                 BMCWEB_LOG_ERROR(
246862598e31SEd Tanous                     "Sensor getSensorPaths resp_handler: Dbus error {}", ec);
2469e6bd846dSNan Zhou                 return;
2470e6bd846dSNan Zhou             }
2471c1d019a6SEd Tanous             getSensorFromDbus(asyncResp, sensorPath, subtree);
247262598e31SEd Tanous             BMCWEB_LOG_DEBUG("respHandler1 exit");
24732b73119cSGeorge Liu         });
2474e6bd846dSNan Zhou }
2475e6bd846dSNan Zhou 
2476bacb2162SNan Zhou } // namespace sensors
2477bacb2162SNan Zhou 
24787e860f15SJohn Edward Broadbent inline void requestRoutesSensorCollection(App& app)
247995a3ecadSAnthony Wilson {
24807e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/")
2481ed398213SEd Tanous         .privileges(redfish::privileges::getSensorCollection)
2482002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2483de167a6fSNan Zhou             std::bind_front(sensors::handleSensorCollectionGet, std::ref(app)));
248495a3ecadSAnthony Wilson }
248595a3ecadSAnthony Wilson 
24867e860f15SJohn Edward Broadbent inline void requestRoutesSensor(App& app)
248795a3ecadSAnthony Wilson {
24887e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/")
2489ed398213SEd Tanous         .privileges(redfish::privileges::getSensor)
2490002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2491e6bd846dSNan Zhou             std::bind_front(sensors::handleSensorGet, std::ref(app)));
249295a3ecadSAnthony Wilson }
249395a3ecadSAnthony Wilson 
249408777fb0SLewanczyk, Dawid } // namespace redfish
2495