xref: /openbmc/bmcweb/features/redfish/lib/sensors.hpp (revision c95636082b63d020e0f63545f5d87e2eb6a05c52)
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 
96cf9e417dSEd Tanous using sensorPair =
97cf9e417dSEd Tanous     std::pair<std::string_view, std::span<const std::string_view>>;
9802da7c5aSEd Tanous static constexpr std::array<sensorPair, 3> paths = {
99*c9563608SJanet Adkins     {{sensor_utils::powerNode, dbus::powerPaths},
100*c9563608SJanet Adkins      {sensor_utils::sensorsNode, dbus::sensorPaths},
101*c9563608SJanet Adkins      {sensor_utils::thermalNode, dbus::thermalPaths}}};
102c2bf7f99SWludzik, Jozef 
103a0ec28b6SAdrian Ambrożewicz } // namespace sensors
104a0ec28b6SAdrian Ambrożewicz 
10508777fb0SLewanczyk, Dawid /**
106588c3f0dSKowalski, Kamil  * SensorsAsyncResp
10708777fb0SLewanczyk, Dawid  * Gathers data needed for response processing after async calls are done
10808777fb0SLewanczyk, Dawid  */
1091abe55efSEd Tanous class SensorsAsyncResp
1101abe55efSEd Tanous {
11108777fb0SLewanczyk, Dawid   public:
112a0ec28b6SAdrian Ambrożewicz     using DataCompleteCb = std::function<void(
113a0ec28b6SAdrian Ambrożewicz         const boost::beast::http::status status,
114fe04d49cSNan Zhou         const std::map<std::string, std::string>& uriToDbus)>;
115a0ec28b6SAdrian Ambrożewicz 
116a0ec28b6SAdrian Ambrożewicz     struct SensorData
117a0ec28b6SAdrian Ambrożewicz     {
118a0ec28b6SAdrian Ambrożewicz         const std::string name;
119a0ec28b6SAdrian Ambrożewicz         std::string uri;
120a0ec28b6SAdrian Ambrożewicz         const std::string dbusPath;
121a0ec28b6SAdrian Ambrożewicz     };
122a0ec28b6SAdrian Ambrożewicz 
1238a592810SEd Tanous     SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
1248d1b46d7Szhanghch05                      const std::string& chassisIdIn,
125cf9e417dSEd Tanous                      std::span<const std::string_view> typesIn,
12602da7c5aSEd Tanous                      std::string_view subNode) :
127bd79bce8SPatrick Williams         asyncResp(asyncRespIn), chassisId(chassisIdIn), types(typesIn),
128bd79bce8SPatrick Williams         chassisSubNode(subNode), efficientExpand(false)
1291214b7e7SGunnar Mills     {}
13008777fb0SLewanczyk, Dawid 
131a0ec28b6SAdrian Ambrożewicz     // Store extra data about sensor mapping and return it in callback
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,
136a0ec28b6SAdrian Ambrożewicz                      DataCompleteCb&& creationComplete) :
137bd79bce8SPatrick Williams         asyncResp(asyncRespIn), chassisId(chassisIdIn), types(typesIn),
138bd79bce8SPatrick Williams         chassisSubNode(subNode), efficientExpand(false),
139bd79bce8SPatrick Williams         metadata{std::vector<SensorData>()},
140a0ec28b6SAdrian Ambrożewicz         dataComplete{std::move(creationComplete)}
141a0ec28b6SAdrian Ambrożewicz     {}
142a0ec28b6SAdrian Ambrożewicz 
143928fefb9SNan Zhou     // sensor collections expand
1448a592810SEd Tanous     SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
145928fefb9SNan Zhou                      const std::string& chassisIdIn,
146cf9e417dSEd Tanous                      std::span<const std::string_view> typesIn,
1478a592810SEd Tanous                      const std::string_view& subNode, bool efficientExpandIn) :
148bd79bce8SPatrick Williams         asyncResp(asyncRespIn), chassisId(chassisIdIn), types(typesIn),
149bd79bce8SPatrick Williams         chassisSubNode(subNode), efficientExpand(efficientExpandIn)
150928fefb9SNan Zhou     {}
151928fefb9SNan Zhou 
1521abe55efSEd Tanous     ~SensorsAsyncResp()
1531abe55efSEd Tanous     {
1548d1b46d7Szhanghch05         if (asyncResp->res.result() ==
1558d1b46d7Szhanghch05             boost::beast::http::status::internal_server_error)
1561abe55efSEd Tanous         {
1571abe55efSEd Tanous             // Reset the json object to clear out any data that made it in
1581abe55efSEd Tanous             // before the error happened todo(ed) handle error condition with
1591abe55efSEd Tanous             // proper code
1608d1b46d7Szhanghch05             asyncResp->res.jsonValue = nlohmann::json::object();
16108777fb0SLewanczyk, Dawid         }
162a0ec28b6SAdrian Ambrożewicz 
163a0ec28b6SAdrian Ambrożewicz         if (dataComplete && metadata)
164a0ec28b6SAdrian Ambrożewicz         {
165fe04d49cSNan Zhou             std::map<std::string, std::string> map;
1668d1b46d7Szhanghch05             if (asyncResp->res.result() == boost::beast::http::status::ok)
167a0ec28b6SAdrian Ambrożewicz             {
168a0ec28b6SAdrian Ambrożewicz                 for (auto& sensor : *metadata)
169a0ec28b6SAdrian Ambrożewicz                 {
170c1d019a6SEd Tanous                     map.emplace(sensor.uri, sensor.dbusPath);
171a0ec28b6SAdrian Ambrożewicz                 }
172a0ec28b6SAdrian Ambrożewicz             }
1738d1b46d7Szhanghch05             dataComplete(asyncResp->res.result(), map);
174a0ec28b6SAdrian Ambrożewicz         }
17508777fb0SLewanczyk, Dawid     }
176588c3f0dSKowalski, Kamil 
177ecd6a3a2SEd Tanous     SensorsAsyncResp(const SensorsAsyncResp&) = delete;
178ecd6a3a2SEd Tanous     SensorsAsyncResp(SensorsAsyncResp&&) = delete;
179ecd6a3a2SEd Tanous     SensorsAsyncResp& operator=(const SensorsAsyncResp&) = delete;
180ecd6a3a2SEd Tanous     SensorsAsyncResp& operator=(SensorsAsyncResp&&) = delete;
181ecd6a3a2SEd Tanous 
182a0ec28b6SAdrian Ambrożewicz     void addMetadata(const nlohmann::json& sensorObject,
183c1d019a6SEd Tanous                      const std::string& dbusPath)
184a0ec28b6SAdrian Ambrożewicz     {
185a0ec28b6SAdrian Ambrożewicz         if (metadata)
186a0ec28b6SAdrian Ambrożewicz         {
187c1d019a6SEd Tanous             metadata->emplace_back(SensorData{
188c1d019a6SEd Tanous                 sensorObject["Name"], sensorObject["@odata.id"], dbusPath});
189a0ec28b6SAdrian Ambrożewicz         }
190a0ec28b6SAdrian Ambrożewicz     }
191a0ec28b6SAdrian Ambrożewicz 
192a0ec28b6SAdrian Ambrożewicz     void updateUri(const std::string& name, const std::string& uri)
193a0ec28b6SAdrian Ambrożewicz     {
194a0ec28b6SAdrian Ambrożewicz         if (metadata)
195a0ec28b6SAdrian Ambrożewicz         {
196a0ec28b6SAdrian Ambrożewicz             for (auto& sensor : *metadata)
197a0ec28b6SAdrian Ambrożewicz             {
198a0ec28b6SAdrian Ambrożewicz                 if (sensor.name == name)
199a0ec28b6SAdrian Ambrożewicz                 {
200a0ec28b6SAdrian Ambrożewicz                     sensor.uri = uri;
201a0ec28b6SAdrian Ambrożewicz                 }
202a0ec28b6SAdrian Ambrożewicz             }
203a0ec28b6SAdrian Ambrożewicz         }
204a0ec28b6SAdrian Ambrożewicz     }
205a0ec28b6SAdrian Ambrożewicz 
2068d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp> asyncResp;
207a0ec28b6SAdrian Ambrożewicz     const std::string chassisId;
208cf9e417dSEd Tanous     const std::span<const std::string_view> types;
209a0ec28b6SAdrian Ambrożewicz     const std::string chassisSubNode;
210928fefb9SNan Zhou     const bool efficientExpand;
211a0ec28b6SAdrian Ambrożewicz 
212a0ec28b6SAdrian Ambrożewicz   private:
213a0ec28b6SAdrian Ambrożewicz     std::optional<std::vector<SensorData>> metadata;
214a0ec28b6SAdrian Ambrożewicz     DataCompleteCb dataComplete;
21508777fb0SLewanczyk, Dawid };
21608777fb0SLewanczyk, Dawid 
217*c9563608SJanet Adkins using InventoryItem = sensor_utils::InventoryItem;
218adc4f0dbSShawn McCarney 
219adc4f0dbSShawn McCarney /**
220413961deSRichard Marian Thomaiyar  * @brief Get objects with connection necessary for sensors
221588c3f0dSKowalski, Kamil  * @param SensorsAsyncResp Pointer to object holding response data
22208777fb0SLewanczyk, Dawid  * @param sensorNames Sensors retrieved from chassis
22308777fb0SLewanczyk, Dawid  * @param callback Callback for processing gathered connections
22408777fb0SLewanczyk, Dawid  */
22508777fb0SLewanczyk, Dawid template <typename Callback>
226413961deSRichard Marian Thomaiyar void getObjectsWithConnection(
22781ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
228fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& sensorNames,
2291abe55efSEd Tanous     Callback&& callback)
2301abe55efSEd Tanous {
23162598e31SEd Tanous     BMCWEB_LOG_DEBUG("getObjectsWithConnection enter");
23203b5bae3SJames Feist     const std::string path = "/xyz/openbmc_project/sensors";
233e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
23408777fb0SLewanczyk, Dawid         "xyz.openbmc_project.Sensor.Value"};
23508777fb0SLewanczyk, Dawid 
236e99073f5SGeorge Liu     // Make call to ObjectMapper to find all sensors objects
237e99073f5SGeorge Liu     dbus::utility::getSubTree(
238e99073f5SGeorge Liu         path, 2, interfaces,
2398cb2c024SEd Tanous         [callback = std::forward<Callback>(callback), sensorsAsyncResp,
240e99073f5SGeorge Liu          sensorNames](const boost::system::error_code& ec,
241002d39b4SEd Tanous                       const dbus::utility::MapperGetSubTreeResponse& subtree) {
242e99073f5SGeorge Liu             // Response handler for parsing objects subtree
24362598e31SEd Tanous             BMCWEB_LOG_DEBUG("getObjectsWithConnection resp_handler enter");
2441abe55efSEd Tanous             if (ec)
2451abe55efSEd Tanous             {
2468d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
24762598e31SEd Tanous                 BMCWEB_LOG_ERROR(
24862598e31SEd Tanous                     "getObjectsWithConnection resp_handler: Dbus error {}", ec);
24908777fb0SLewanczyk, Dawid                 return;
25008777fb0SLewanczyk, Dawid             }
25108777fb0SLewanczyk, Dawid 
25262598e31SEd Tanous             BMCWEB_LOG_DEBUG("Found {} subtrees", subtree.size());
25308777fb0SLewanczyk, Dawid 
254bd79bce8SPatrick Williams             // Make unique list of connections only for requested sensor types
255bd79bce8SPatrick Williams             // and found in the chassis
256fe04d49cSNan Zhou             std::set<std::string> connections;
257413961deSRichard Marian Thomaiyar             std::set<std::pair<std::string, std::string>> objectsWithConnection;
25808777fb0SLewanczyk, Dawid 
25962598e31SEd Tanous             BMCWEB_LOG_DEBUG("sensorNames list count: {}", sensorNames->size());
26049c53ac9SJohnathan Mantey             for (const std::string& tsensor : *sensorNames)
2611abe55efSEd Tanous             {
26262598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Sensor to find: {}", tsensor);
26308777fb0SLewanczyk, Dawid             }
26408777fb0SLewanczyk, Dawid 
265bd79bce8SPatrick Williams             for (const std::pair<std::string,
266bd79bce8SPatrick Williams                                  std::vector<std::pair<
267bd79bce8SPatrick Williams                                      std::string, std::vector<std::string>>>>&
2681abe55efSEd Tanous                      object : subtree)
2691abe55efSEd Tanous             {
27049c53ac9SJohnathan Mantey                 if (sensorNames->find(object.first) != sensorNames->end())
2711abe55efSEd Tanous                 {
27249c53ac9SJohnathan Mantey                     for (const std::pair<std::string, std::vector<std::string>>&
2731abe55efSEd Tanous                              objData : object.second)
2741abe55efSEd Tanous                     {
275bd79bce8SPatrick Williams                         BMCWEB_LOG_DEBUG("Adding connection: {}",
276bd79bce8SPatrick Williams                                          objData.first);
27708777fb0SLewanczyk, Dawid                         connections.insert(objData.first);
278de629b6eSShawn McCarney                         objectsWithConnection.insert(
279de629b6eSShawn McCarney                             std::make_pair(object.first, objData.first));
28008777fb0SLewanczyk, Dawid                     }
28108777fb0SLewanczyk, Dawid                 }
28208777fb0SLewanczyk, Dawid             }
28362598e31SEd Tanous             BMCWEB_LOG_DEBUG("Found {} connections", connections.size());
284413961deSRichard Marian Thomaiyar             callback(std::move(connections), std::move(objectsWithConnection));
28562598e31SEd Tanous             BMCWEB_LOG_DEBUG("getObjectsWithConnection resp_handler exit");
286e99073f5SGeorge Liu         });
28762598e31SEd Tanous     BMCWEB_LOG_DEBUG("getObjectsWithConnection exit");
288413961deSRichard Marian Thomaiyar }
289413961deSRichard Marian Thomaiyar 
290413961deSRichard Marian Thomaiyar /**
291413961deSRichard Marian Thomaiyar  * @brief Create connections necessary for sensors
292413961deSRichard Marian Thomaiyar  * @param SensorsAsyncResp Pointer to object holding response data
293413961deSRichard Marian Thomaiyar  * @param sensorNames Sensors retrieved from chassis
294413961deSRichard Marian Thomaiyar  * @param callback Callback for processing gathered connections
295413961deSRichard Marian Thomaiyar  */
296413961deSRichard Marian Thomaiyar template <typename Callback>
297fe04d49cSNan Zhou void getConnections(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
298fe04d49cSNan Zhou                     const std::shared_ptr<std::set<std::string>> sensorNames,
299413961deSRichard Marian Thomaiyar                     Callback&& callback)
300413961deSRichard Marian Thomaiyar {
301413961deSRichard Marian Thomaiyar     auto objectsWithConnectionCb =
3028cb2c024SEd Tanous         [callback = std::forward<Callback>(callback)](
3038cb2c024SEd Tanous             const std::set<std::string>& connections,
304413961deSRichard Marian Thomaiyar             const std::set<std::pair<std::string, std::string>>&
3053174e4dfSEd Tanous             /*objectsWithConnection*/) { callback(connections); };
30681ce609eSEd Tanous     getObjectsWithConnection(sensorsAsyncResp, sensorNames,
307413961deSRichard Marian Thomaiyar                              std::move(objectsWithConnectionCb));
30808777fb0SLewanczyk, Dawid }
30908777fb0SLewanczyk, Dawid 
31008777fb0SLewanczyk, Dawid /**
31149c53ac9SJohnathan Mantey  * @brief Shrinks the list of sensors for processing
31249c53ac9SJohnathan Mantey  * @param SensorsAysncResp  The class holding the Redfish response
31349c53ac9SJohnathan Mantey  * @param allSensors  A list of all the sensors associated to the
31449c53ac9SJohnathan Mantey  * chassis element (i.e. baseboard, front panel, etc...)
31549c53ac9SJohnathan Mantey  * @param activeSensors A list that is a reduction of the incoming
31649c53ac9SJohnathan Mantey  * allSensors list.  Eliminate Thermal sensors when a Power request is
31749c53ac9SJohnathan Mantey  * made, and eliminate Power sensors when a Thermal request is made.
31849c53ac9SJohnathan Mantey  */
31923a21a1cSEd Tanous inline void reduceSensorList(
3207f1cc26dSEd Tanous     crow::Response& res, std::string_view chassisSubNode,
321cf9e417dSEd Tanous     std::span<const std::string_view> sensorTypes,
32249c53ac9SJohnathan Mantey     const std::vector<std::string>* allSensors,
323fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& activeSensors)
32449c53ac9SJohnathan Mantey {
32549c53ac9SJohnathan Mantey     if ((allSensors == nullptr) || (activeSensors == nullptr))
32649c53ac9SJohnathan Mantey     {
3277f1cc26dSEd Tanous         messages::resourceNotFound(res, chassisSubNode,
328*c9563608SJanet Adkins                                    chassisSubNode == sensor_utils::thermalNode
329a0ec28b6SAdrian Ambrożewicz                                        ? "Temperatures"
33049c53ac9SJohnathan Mantey                                        : "Voltages");
33149c53ac9SJohnathan Mantey 
33249c53ac9SJohnathan Mantey         return;
33349c53ac9SJohnathan Mantey     }
33449c53ac9SJohnathan Mantey     if (allSensors->empty())
33549c53ac9SJohnathan Mantey     {
33649c53ac9SJohnathan Mantey         // Nothing to do, the activeSensors object is also empty
33749c53ac9SJohnathan Mantey         return;
33849c53ac9SJohnathan Mantey     }
33949c53ac9SJohnathan Mantey 
3407f1cc26dSEd Tanous     for (std::string_view type : sensorTypes)
34149c53ac9SJohnathan Mantey     {
34249c53ac9SJohnathan Mantey         for (const std::string& sensor : *allSensors)
34349c53ac9SJohnathan Mantey         {
34411ba3979SEd Tanous             if (sensor.starts_with(type))
34549c53ac9SJohnathan Mantey             {
34649c53ac9SJohnathan Mantey                 activeSensors->emplace(sensor);
34749c53ac9SJohnathan Mantey             }
34849c53ac9SJohnathan Mantey         }
34949c53ac9SJohnathan Mantey     }
35049c53ac9SJohnathan Mantey }
35149c53ac9SJohnathan Mantey 
3527f1cc26dSEd Tanous /*
3537f1cc26dSEd Tanous  *Populates the top level collection for a given subnode.  Populates
3547f1cc26dSEd Tanous  *SensorCollection, Power, or Thermal schemas.
3557f1cc26dSEd Tanous  *
3567f1cc26dSEd Tanous  * */
3577f1cc26dSEd Tanous inline void populateChassisNode(nlohmann::json& jsonValue,
3587f1cc26dSEd Tanous                                 std::string_view chassisSubNode)
3597f1cc26dSEd Tanous {
360*c9563608SJanet Adkins     if (chassisSubNode == sensor_utils::powerNode)
3617f1cc26dSEd Tanous     {
3627f1cc26dSEd Tanous         jsonValue["@odata.type"] = "#Power.v1_5_2.Power";
3637f1cc26dSEd Tanous     }
364*c9563608SJanet Adkins     else if (chassisSubNode == sensor_utils::thermalNode)
3657f1cc26dSEd Tanous     {
3667f1cc26dSEd Tanous         jsonValue["@odata.type"] = "#Thermal.v1_4_0.Thermal";
3677f1cc26dSEd Tanous         jsonValue["Fans"] = nlohmann::json::array();
3687f1cc26dSEd Tanous         jsonValue["Temperatures"] = nlohmann::json::array();
3697f1cc26dSEd Tanous     }
370*c9563608SJanet Adkins     else if (chassisSubNode == sensor_utils::sensorsNode)
3717f1cc26dSEd Tanous     {
3727f1cc26dSEd Tanous         jsonValue["@odata.type"] = "#SensorCollection.SensorCollection";
3737f1cc26dSEd Tanous         jsonValue["Description"] = "Collection of Sensors for this Chassis";
3747f1cc26dSEd Tanous         jsonValue["Members"] = nlohmann::json::array();
3757f1cc26dSEd Tanous         jsonValue["Members@odata.count"] = 0;
3767f1cc26dSEd Tanous     }
3777f1cc26dSEd Tanous 
378*c9563608SJanet Adkins     if (chassisSubNode != sensor_utils::sensorsNode)
3797f1cc26dSEd Tanous     {
3807f1cc26dSEd Tanous         jsonValue["Id"] = chassisSubNode;
3817f1cc26dSEd Tanous     }
3827f1cc26dSEd Tanous     jsonValue["Name"] = chassisSubNode;
3837f1cc26dSEd Tanous }
3847f1cc26dSEd Tanous 
38549c53ac9SJohnathan Mantey /**
38608777fb0SLewanczyk, Dawid  * @brief Retrieves requested chassis sensors and redundancy data from DBus .
387588c3f0dSKowalski, Kamil  * @param SensorsAsyncResp   Pointer to object holding response data
38808777fb0SLewanczyk, Dawid  * @param callback  Callback for next step in gathered sensor processing
38908777fb0SLewanczyk, Dawid  */
39008777fb0SLewanczyk, Dawid template <typename Callback>
3917f1cc26dSEd Tanous void getChassis(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3927f1cc26dSEd Tanous                 std::string_view chassisId, std::string_view chassisSubNode,
393cf9e417dSEd Tanous                 std::span<const std::string_view> sensorTypes,
394cf9e417dSEd Tanous                 Callback&& callback)
3951abe55efSEd Tanous {
39662598e31SEd Tanous     BMCWEB_LOG_DEBUG("getChassis enter");
3977a1dbc48SGeorge Liu     constexpr std::array<std::string_view, 2> interfaces = {
39849c53ac9SJohnathan Mantey         "xyz.openbmc_project.Inventory.Item.Board",
399adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Item.Chassis"};
4007a1dbc48SGeorge Liu 
4017a1dbc48SGeorge Liu     // Get the Chassis Collection
4027a1dbc48SGeorge Liu     dbus::utility::getSubTreePaths(
4037a1dbc48SGeorge Liu         "/xyz/openbmc_project/inventory", 0, interfaces,
4048cb2c024SEd Tanous         [callback = std::forward<Callback>(callback), asyncResp,
4057f1cc26dSEd Tanous          chassisIdStr{std::string(chassisId)},
4067f1cc26dSEd Tanous          chassisSubNode{std::string(chassisSubNode)}, sensorTypes](
4077a1dbc48SGeorge Liu             const boost::system::error_code& ec,
408002d39b4SEd Tanous             const dbus::utility::MapperGetSubTreePathsResponse& chassisPaths) {
40962598e31SEd Tanous             BMCWEB_LOG_DEBUG("getChassis respHandler enter");
4101abe55efSEd Tanous             if (ec)
4111abe55efSEd Tanous             {
41262598e31SEd Tanous                 BMCWEB_LOG_ERROR("getChassis respHandler DBUS error: {}", ec);
4137f1cc26dSEd Tanous                 messages::internalError(asyncResp->res);
41408777fb0SLewanczyk, Dawid                 return;
41508777fb0SLewanczyk, Dawid             }
41649c53ac9SJohnathan Mantey             const std::string* chassisPath = nullptr;
41749c53ac9SJohnathan Mantey             for (const std::string& chassis : chassisPaths)
4181abe55efSEd Tanous             {
41928aa8de5SGeorge Liu                 sdbusplus::message::object_path path(chassis);
420f8fe53e7SEd Tanous                 std::string chassisName = path.filename();
42128aa8de5SGeorge Liu                 if (chassisName.empty())
4221abe55efSEd Tanous                 {
42362598e31SEd Tanous                     BMCWEB_LOG_ERROR("Failed to find '/' in {}", chassis);
424daf36e2eSEd Tanous                     continue;
425daf36e2eSEd Tanous                 }
4267f1cc26dSEd Tanous                 if (chassisName == chassisIdStr)
4271abe55efSEd Tanous                 {
42849c53ac9SJohnathan Mantey                     chassisPath = &chassis;
42949c53ac9SJohnathan Mantey                     break;
430daf36e2eSEd Tanous                 }
43149c53ac9SJohnathan Mantey             }
43249c53ac9SJohnathan Mantey             if (chassisPath == nullptr)
4331abe55efSEd Tanous             {
434bd79bce8SPatrick Williams                 messages::resourceNotFound(asyncResp->res, "Chassis",
435bd79bce8SPatrick Williams                                            chassisIdStr);
43649c53ac9SJohnathan Mantey                 return;
4371abe55efSEd Tanous             }
4387f1cc26dSEd Tanous             populateChassisNode(asyncResp->res.jsonValue, chassisSubNode);
43908777fb0SLewanczyk, Dawid 
440ef4c65b7SEd Tanous             asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
441ef4c65b7SEd Tanous                 "/redfish/v1/Chassis/{}/{}", chassisIdStr, chassisSubNode);
44295a3ecadSAnthony Wilson 
4438fb49dd6SShawn McCarney             // Get the list of all sensors for this Chassis element
4448fb49dd6SShawn McCarney             std::string sensorPath = *chassisPath + "/all_sensors";
4456c3e9451SGeorge Liu             dbus::utility::getAssociationEndPoints(
4466c3e9451SGeorge Liu                 sensorPath,
4477f1cc26dSEd Tanous                 [asyncResp, chassisSubNode, sensorTypes,
4488cb2c024SEd Tanous                  callback = std::forward<const Callback>(callback)](
4498b24275dSEd Tanous                     const boost::system::error_code& ec2,
4506c3e9451SGeorge Liu                     const dbus::utility::MapperEndPoints& nodeSensorList) {
4518b24275dSEd Tanous                     if (ec2)
45249c53ac9SJohnathan Mantey                     {
4538b24275dSEd Tanous                         if (ec2.value() != EBADR)
45449c53ac9SJohnathan Mantey                         {
4557f1cc26dSEd Tanous                             messages::internalError(asyncResp->res);
45649c53ac9SJohnathan Mantey                             return;
45749c53ac9SJohnathan Mantey                         }
45849c53ac9SJohnathan Mantey                     }
459bd79bce8SPatrick Williams                     const std::shared_ptr<std::set<std::string>>
460bd79bce8SPatrick Williams                         culledSensorList =
461fe04d49cSNan Zhou                             std::make_shared<std::set<std::string>>();
462bd79bce8SPatrick Williams                     reduceSensorList(asyncResp->res, chassisSubNode,
463bd79bce8SPatrick Williams                                      sensorTypes, &nodeSensorList,
464bd79bce8SPatrick Williams                                      culledSensorList);
465bd79bce8SPatrick Williams                     BMCWEB_LOG_DEBUG("Finishing with {}",
466bd79bce8SPatrick Williams                                      culledSensorList->size());
46749c53ac9SJohnathan Mantey                     callback(culledSensorList);
4681e1e598dSJonathan Doman                 });
4697a1dbc48SGeorge Liu         });
47062598e31SEd Tanous     BMCWEB_LOG_DEBUG("getChassis exit");
47108777fb0SLewanczyk, Dawid }
47208777fb0SLewanczyk, Dawid 
47308777fb0SLewanczyk, Dawid /**
4741d7c0054SEd Tanous  * @brief Builds a json sensor representation of a sensor.
4751d7c0054SEd Tanous  * @param sensorName  The name of the sensor to be built
4761d7c0054SEd Tanous  * @param sensorType  The type (temperature, fan_tach, etc) of the sensor to
4771d7c0054SEd Tanous  * build
4788ece0e45SEd Tanous  * @param chassisSubNode The subnode (thermal, sensor, etc) of the sensor
4791d7c0054SEd Tanous  * @param interfacesDict  A dictionary of the interfaces and properties of said
4801d7c0054SEd Tanous  * interfaces to be built from
4811d7c0054SEd Tanous  * @param sensorJson  The json object to fill
4821d7c0054SEd Tanous  * @param inventoryItem D-Bus inventory item associated with the sensor.  Will
4831d7c0054SEd Tanous  * be nullptr if no associated inventory item was found.
4841d7c0054SEd Tanous  */
4851d7c0054SEd Tanous inline void objectInterfacesToJson(
4861d7c0054SEd Tanous     const std::string& sensorName, const std::string& sensorType,
4871d7c0054SEd Tanous     const std::string& chassisSubNode,
48880f79a40SMichael Shen     const dbus::utility::DBusInterfacesMap& interfacesDict,
4891d7c0054SEd Tanous     nlohmann::json& sensorJson, InventoryItem* inventoryItem)
4901d7c0054SEd Tanous {
4911d7c0054SEd Tanous     for (const auto& [interface, valuesDict] : interfacesDict)
4921d7c0054SEd Tanous     {
493*c9563608SJanet Adkins         sensor_utils::objectPropertiesToJson(
494*c9563608SJanet Adkins             sensorName, sensorType, chassisSubNode, valuesDict, sensorJson,
495*c9563608SJanet Adkins             inventoryItem);
4961d7c0054SEd Tanous     }
49762598e31SEd Tanous     BMCWEB_LOG_DEBUG("Added sensor {}", sensorName);
4981d7c0054SEd Tanous }
4991d7c0054SEd Tanous 
500b5a76932SEd Tanous inline void populateFanRedundancy(
501b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
5028bd25ccdSJames Feist {
503e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
504e99073f5SGeorge Liu         "xyz.openbmc_project.Control.FanRedundancy"};
505e99073f5SGeorge Liu     dbus::utility::getSubTree(
506e99073f5SGeorge Liu         "/xyz/openbmc_project/control", 2, interfaces,
507b9d36b47SEd Tanous         [sensorsAsyncResp](
508e99073f5SGeorge Liu             const boost::system::error_code& ec,
509b9d36b47SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& resp) {
5108bd25ccdSJames Feist             if (ec)
5118bd25ccdSJames Feist             {
5128bd25ccdSJames Feist                 return; // don't have to have this interface
5138bd25ccdSJames Feist             }
5146c3e9451SGeorge Liu             for (const std::pair<std::string, dbus::utility::MapperServiceMap>&
515e278c18fSEd Tanous                      pathPair : resp)
5168bd25ccdSJames Feist             {
517e278c18fSEd Tanous                 const std::string& path = pathPair.first;
518bd79bce8SPatrick Williams                 const dbus::utility::MapperServiceMap& objDict =
519bd79bce8SPatrick Williams                     pathPair.second;
5208bd25ccdSJames Feist                 if (objDict.empty())
5218bd25ccdSJames Feist                 {
5228bd25ccdSJames Feist                     continue; // this should be impossible
5238bd25ccdSJames Feist                 }
5248bd25ccdSJames Feist 
5258bd25ccdSJames Feist                 const std::string& owner = objDict.begin()->first;
5266c3e9451SGeorge Liu                 dbus::utility::getAssociationEndPoints(
5276c3e9451SGeorge Liu                     path + "/chassis",
5286c3e9451SGeorge Liu                     [path, owner, sensorsAsyncResp](
5298b24275dSEd Tanous                         const boost::system::error_code& ec2,
5306c3e9451SGeorge Liu                         const dbus::utility::MapperEndPoints& endpoints) {
5318b24275dSEd Tanous                         if (ec2)
5328bd25ccdSJames Feist                         {
5338bd25ccdSJames Feist                             return; // if they don't have an association we
5348bd25ccdSJames Feist                                     // can't tell what chassis is
5358bd25ccdSJames Feist                         }
5363544d2a7SEd Tanous                         auto found = std::ranges::find_if(
537bd79bce8SPatrick Williams                             endpoints,
538bd79bce8SPatrick Williams                             [sensorsAsyncResp](const std::string& entry) {
539bd79bce8SPatrick Williams                                 return entry.find(
540bd79bce8SPatrick Williams                                            sensorsAsyncResp->chassisId) !=
5418bd25ccdSJames Feist                                        std::string::npos;
5428bd25ccdSJames Feist                             });
5438bd25ccdSJames Feist 
5441e1e598dSJonathan Doman                         if (found == endpoints.end())
5458bd25ccdSJames Feist                         {
5468bd25ccdSJames Feist                             return;
5478bd25ccdSJames Feist                         }
54886d89ed7SKrzysztof Grobelny                         sdbusplus::asio::getAllProperties(
54986d89ed7SKrzysztof Grobelny                             *crow::connections::systemBus, owner, path,
55086d89ed7SKrzysztof Grobelny                             "xyz.openbmc_project.Control.FanRedundancy",
5518bd25ccdSJames Feist                             [path, sensorsAsyncResp](
5528b24275dSEd Tanous                                 const boost::system::error_code& ec3,
55386d89ed7SKrzysztof Grobelny                                 const dbus::utility::DBusPropertiesMap& ret) {
5548b24275dSEd Tanous                                 if (ec3)
5558bd25ccdSJames Feist                                 {
5568bd25ccdSJames Feist                                     return; // don't have to have this
5578bd25ccdSJames Feist                                             // interface
5588bd25ccdSJames Feist                                 }
5598bd25ccdSJames Feist 
56086d89ed7SKrzysztof Grobelny                                 const uint8_t* allowedFailures = nullptr;
561bd79bce8SPatrick Williams                                 const std::vector<std::string>* collection =
562bd79bce8SPatrick Williams                                     nullptr;
56386d89ed7SKrzysztof Grobelny                                 const std::string* status = nullptr;
56486d89ed7SKrzysztof Grobelny 
565bd79bce8SPatrick Williams                                 const bool success =
566bd79bce8SPatrick Williams                                     sdbusplus::unpackPropertiesNoThrow(
56786d89ed7SKrzysztof Grobelny                                         dbus_utils::UnpackErrorPrinter(), ret,
568bd79bce8SPatrick Williams                                         "AllowedFailures", allowedFailures,
569bd79bce8SPatrick Williams                                         "Collection", collection, "Status",
570bd79bce8SPatrick Williams                                         status);
57186d89ed7SKrzysztof Grobelny 
57286d89ed7SKrzysztof Grobelny                                 if (!success)
57386d89ed7SKrzysztof Grobelny                                 {
57486d89ed7SKrzysztof Grobelny                                     messages::internalError(
57586d89ed7SKrzysztof Grobelny                                         sensorsAsyncResp->asyncResp->res);
57686d89ed7SKrzysztof Grobelny                                     return;
57786d89ed7SKrzysztof Grobelny                                 }
57886d89ed7SKrzysztof Grobelny 
579bd79bce8SPatrick Williams                                 if (allowedFailures == nullptr ||
580bd79bce8SPatrick Williams                                     collection == nullptr || status == nullptr)
5818bd25ccdSJames Feist                                 {
582bd79bce8SPatrick Williams                                     BMCWEB_LOG_ERROR(
583bd79bce8SPatrick Williams                                         "Invalid redundancy interface");
5848bd25ccdSJames Feist                                     messages::internalError(
5858d1b46d7Szhanghch05                                         sensorsAsyncResp->asyncResp->res);
5868bd25ccdSJames Feist                                     return;
5878bd25ccdSJames Feist                                 }
5888bd25ccdSJames Feist 
589bd79bce8SPatrick Williams                                 sdbusplus::message::object_path objectPath(
590bd79bce8SPatrick Williams                                     path);
59128aa8de5SGeorge Liu                                 std::string name = objectPath.filename();
59228aa8de5SGeorge Liu                                 if (name.empty())
5938bd25ccdSJames Feist                                 {
5948bd25ccdSJames Feist                                     // this should be impossible
5958bd25ccdSJames Feist                                     messages::internalError(
5968d1b46d7Szhanghch05                                         sensorsAsyncResp->asyncResp->res);
5978bd25ccdSJames Feist                                     return;
5988bd25ccdSJames Feist                                 }
59918f8f608SEd Tanous                                 std::ranges::replace(name, '_', ' ');
6008bd25ccdSJames Feist 
6018bd25ccdSJames Feist                                 std::string health;
6028bd25ccdSJames Feist 
60311ba3979SEd Tanous                                 if (status->ends_with("Full"))
6048bd25ccdSJames Feist                                 {
6058bd25ccdSJames Feist                                     health = "OK";
6068bd25ccdSJames Feist                                 }
60711ba3979SEd Tanous                                 else if (status->ends_with("Degraded"))
6088bd25ccdSJames Feist                                 {
6098bd25ccdSJames Feist                                     health = "Warning";
6108bd25ccdSJames Feist                                 }
6118bd25ccdSJames Feist                                 else
6128bd25ccdSJames Feist                                 {
6138bd25ccdSJames Feist                                     health = "Critical";
6148bd25ccdSJames Feist                                 }
6151476687dSEd Tanous                                 nlohmann::json::array_t redfishCollection;
6168bd25ccdSJames Feist                                 const auto& fanRedfish =
617bd79bce8SPatrick Williams                                     sensorsAsyncResp->asyncResp->res
618bd79bce8SPatrick Williams                                         .jsonValue["Fans"];
6198bd25ccdSJames Feist                                 for (const std::string& item : *collection)
6208bd25ccdSJames Feist                                 {
621bd79bce8SPatrick Williams                                     sdbusplus::message::object_path itemPath(
622bd79bce8SPatrick Williams                                         item);
6238a592810SEd Tanous                                     std::string itemName = itemPath.filename();
62428aa8de5SGeorge Liu                                     if (itemName.empty())
62528aa8de5SGeorge Liu                                     {
62628aa8de5SGeorge Liu                                         continue;
62728aa8de5SGeorge Liu                                     }
6288bd25ccdSJames Feist                                     /*
6298bd25ccdSJames Feist                                     todo(ed): merge patch that fixes the names
6308bd25ccdSJames Feist                                     std::replace(itemName.begin(),
6318bd25ccdSJames Feist                                                  itemName.end(), '_', ' ');*/
6323544d2a7SEd Tanous                                     auto schemaItem = std::ranges::find_if(
633bd79bce8SPatrick Williams                                         fanRedfish,
634bd79bce8SPatrick Williams                                         [itemName](const nlohmann::json& fan) {
6353e35c761SGeorge Liu                                             return fan["Name"] == itemName;
6368bd25ccdSJames Feist                                         });
6378bd25ccdSJames Feist                                     if (schemaItem != fanRedfish.end())
6388bd25ccdSJames Feist                                     {
6398a592810SEd Tanous                                         nlohmann::json::object_t collectionId;
6408a592810SEd Tanous                                         collectionId["@odata.id"] =
6411476687dSEd Tanous                                             (*schemaItem)["@odata.id"];
6421476687dSEd Tanous                                         redfishCollection.emplace_back(
6438a592810SEd Tanous                                             std::move(collectionId));
6448bd25ccdSJames Feist                                     }
6458bd25ccdSJames Feist                                     else
6468bd25ccdSJames Feist                                     {
647bd79bce8SPatrick Williams                                         BMCWEB_LOG_ERROR(
648bd79bce8SPatrick Williams                                             "failed to find fan in schema");
6498bd25ccdSJames Feist                                         messages::internalError(
6508d1b46d7Szhanghch05                                             sensorsAsyncResp->asyncResp->res);
6518bd25ccdSJames Feist                                         return;
6528bd25ccdSJames Feist                                     }
6538bd25ccdSJames Feist                                 }
6548bd25ccdSJames Feist 
655bd79bce8SPatrick Williams                                 size_t minNumNeeded =
656bd79bce8SPatrick Williams                                     collection->empty()
65726f6976fSEd Tanous                                         ? 0
658bd79bce8SPatrick Williams                                         : collection->size() - *allowedFailures;
659bd79bce8SPatrick Williams                                 nlohmann::json& jResp =
660bd79bce8SPatrick Williams                                     sensorsAsyncResp->asyncResp->res
6618bd25ccdSJames Feist                                         .jsonValue["Redundancy"];
6621476687dSEd Tanous 
6631476687dSEd Tanous                                 nlohmann::json::object_t redundancy;
664bd79bce8SPatrick Williams                                 boost::urls::url url = boost::urls::format(
665bd79bce8SPatrick Williams                                     "/redfish/v1/Chassis/{}/{}",
666ef4c65b7SEd Tanous                                     sensorsAsyncResp->chassisId,
667eddfc437SWilly Tu                                     sensorsAsyncResp->chassisSubNode);
668bd79bce8SPatrick Williams                                 url.set_fragment(
669bd79bce8SPatrick Williams                                     ("/Redundancy"_json_pointer / jResp.size())
670eddfc437SWilly Tu                                         .to_string());
671eddfc437SWilly Tu                                 redundancy["@odata.id"] = std::move(url);
672bd79bce8SPatrick Williams                                 redundancy["@odata.type"] =
673bd79bce8SPatrick Williams                                     "#Redundancy.v1_3_2.Redundancy";
6741476687dSEd Tanous                                 redundancy["MinNumNeeded"] = minNumNeeded;
675bd79bce8SPatrick Williams                                 redundancy["Mode"] =
676bd79bce8SPatrick Williams                                     redundancy::RedundancyType::NPlusM;
6771476687dSEd Tanous                                 redundancy["Name"] = name;
6781476687dSEd Tanous                                 redundancy["RedundancySet"] = redfishCollection;
6791476687dSEd Tanous                                 redundancy["Status"]["Health"] = health;
680bd79bce8SPatrick Williams                                 redundancy["Status"]["State"] =
681bd79bce8SPatrick Williams                                     resource::State::Enabled;
6821476687dSEd Tanous 
683b2ba3072SPatrick Williams                                 jResp.emplace_back(std::move(redundancy));
68486d89ed7SKrzysztof Grobelny                             });
6851e1e598dSJonathan Doman                     });
6868bd25ccdSJames Feist             }
687e99073f5SGeorge Liu         });
6888bd25ccdSJames Feist }
6898bd25ccdSJames Feist 
690b5a76932SEd Tanous inline void
69181ce609eSEd Tanous     sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
69249c53ac9SJohnathan Mantey {
6938d1b46d7Szhanghch05     nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue;
69449c53ac9SJohnathan Mantey     std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
695*c9563608SJanet Adkins     if (sensorsAsyncResp->chassisSubNode == sensor_utils::powerNode)
69649c53ac9SJohnathan Mantey     {
69749c53ac9SJohnathan Mantey         sensorHeaders = {"Voltages", "PowerSupplies"};
69849c53ac9SJohnathan Mantey     }
69949c53ac9SJohnathan Mantey     for (const std::string& sensorGroup : sensorHeaders)
70049c53ac9SJohnathan Mantey     {
70149c53ac9SJohnathan Mantey         nlohmann::json::iterator entry = response.find(sensorGroup);
70249c53ac9SJohnathan Mantey         if (entry != response.end())
70349c53ac9SJohnathan Mantey         {
70449c53ac9SJohnathan Mantey             std::sort(entry->begin(), entry->end(),
70502cad96eSEd Tanous                       [](const nlohmann::json& c1, const nlohmann::json& c2) {
70649c53ac9SJohnathan Mantey                           return c1["Name"] < c2["Name"];
70749c53ac9SJohnathan Mantey                       });
70849c53ac9SJohnathan Mantey 
70949c53ac9SJohnathan Mantey             // add the index counts to the end of each entry
71049c53ac9SJohnathan Mantey             size_t count = 0;
71149c53ac9SJohnathan Mantey             for (nlohmann::json& sensorJson : *entry)
71249c53ac9SJohnathan Mantey             {
71349c53ac9SJohnathan Mantey                 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
71449c53ac9SJohnathan Mantey                 if (odata == sensorJson.end())
71549c53ac9SJohnathan Mantey                 {
71649c53ac9SJohnathan Mantey                     continue;
71749c53ac9SJohnathan Mantey                 }
71849c53ac9SJohnathan Mantey                 std::string* value = odata->get_ptr<std::string*>();
71949c53ac9SJohnathan Mantey                 if (value != nullptr)
72049c53ac9SJohnathan Mantey                 {
721eddfc437SWilly Tu                     *value += "/" + std::to_string(count);
7223e35c761SGeorge Liu                     sensorJson["MemberId"] = std::to_string(count);
72349c53ac9SJohnathan Mantey                     count++;
72481ce609eSEd Tanous                     sensorsAsyncResp->updateUri(sensorJson["Name"], *value);
72549c53ac9SJohnathan Mantey                 }
72649c53ac9SJohnathan Mantey             }
72749c53ac9SJohnathan Mantey         }
72849c53ac9SJohnathan Mantey     }
72949c53ac9SJohnathan Mantey }
73049c53ac9SJohnathan Mantey 
73108777fb0SLewanczyk, Dawid /**
732adc4f0dbSShawn McCarney  * @brief Finds the inventory item with the specified object path.
733adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
734adc4f0dbSShawn McCarney  * @param invItemObjPath D-Bus object path of inventory item.
735adc4f0dbSShawn McCarney  * @return Inventory item within vector, or nullptr if no match found.
7368fb49dd6SShawn McCarney  */
73723a21a1cSEd Tanous inline InventoryItem* findInventoryItem(
738b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
739adc4f0dbSShawn McCarney     const std::string& invItemObjPath)
7408fb49dd6SShawn McCarney {
741adc4f0dbSShawn McCarney     for (InventoryItem& inventoryItem : *inventoryItems)
7428fb49dd6SShawn McCarney     {
743adc4f0dbSShawn McCarney         if (inventoryItem.objectPath == invItemObjPath)
7448fb49dd6SShawn McCarney         {
745adc4f0dbSShawn McCarney             return &inventoryItem;
7468fb49dd6SShawn McCarney         }
7478fb49dd6SShawn McCarney     }
7488fb49dd6SShawn McCarney     return nullptr;
7498fb49dd6SShawn McCarney }
7508fb49dd6SShawn McCarney 
7518fb49dd6SShawn McCarney /**
752adc4f0dbSShawn McCarney  * @brief Finds the inventory item associated with the specified sensor.
753adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
754adc4f0dbSShawn McCarney  * @param sensorObjPath D-Bus object path of sensor.
755adc4f0dbSShawn McCarney  * @return Inventory item within vector, or nullptr if no match found.
7568fb49dd6SShawn McCarney  */
75723a21a1cSEd Tanous inline InventoryItem* findInventoryItemForSensor(
758b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
759adc4f0dbSShawn McCarney     const std::string& sensorObjPath)
760adc4f0dbSShawn McCarney {
761adc4f0dbSShawn McCarney     for (InventoryItem& inventoryItem : *inventoryItems)
762adc4f0dbSShawn McCarney     {
763db0d36efSEd Tanous         if (inventoryItem.sensors.contains(sensorObjPath))
764adc4f0dbSShawn McCarney         {
765adc4f0dbSShawn McCarney             return &inventoryItem;
766adc4f0dbSShawn McCarney         }
767adc4f0dbSShawn McCarney     }
768adc4f0dbSShawn McCarney     return nullptr;
769adc4f0dbSShawn McCarney }
770adc4f0dbSShawn McCarney 
771adc4f0dbSShawn McCarney /**
772d500549bSAnthony Wilson  * @brief Finds the inventory item associated with the specified led path.
773d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
774d500549bSAnthony Wilson  * @param ledObjPath D-Bus object path of led.
775d500549bSAnthony Wilson  * @return Inventory item within vector, or nullptr if no match found.
776d500549bSAnthony Wilson  */
777bd79bce8SPatrick Williams inline InventoryItem* findInventoryItemForLed(
778bd79bce8SPatrick Williams     std::vector<InventoryItem>& inventoryItems, const std::string& ledObjPath)
779d500549bSAnthony Wilson {
780d500549bSAnthony Wilson     for (InventoryItem& inventoryItem : inventoryItems)
781d500549bSAnthony Wilson     {
782d500549bSAnthony Wilson         if (inventoryItem.ledObjectPath == ledObjPath)
783d500549bSAnthony Wilson         {
784d500549bSAnthony Wilson             return &inventoryItem;
785d500549bSAnthony Wilson         }
786d500549bSAnthony Wilson     }
787d500549bSAnthony Wilson     return nullptr;
788d500549bSAnthony Wilson }
789d500549bSAnthony Wilson 
790d500549bSAnthony Wilson /**
791adc4f0dbSShawn McCarney  * @brief Adds inventory item and associated sensor to specified vector.
792adc4f0dbSShawn McCarney  *
793adc4f0dbSShawn McCarney  * Adds a new InventoryItem to the vector if necessary.  Searches for an
794adc4f0dbSShawn McCarney  * existing InventoryItem with the specified object path.  If not found, one is
795adc4f0dbSShawn McCarney  * added to the vector.
796adc4f0dbSShawn McCarney  *
797adc4f0dbSShawn McCarney  * Next, the specified sensor is added to the set of sensors associated with the
798adc4f0dbSShawn McCarney  * InventoryItem.
799adc4f0dbSShawn McCarney  *
800adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
801adc4f0dbSShawn McCarney  * @param invItemObjPath D-Bus object path of inventory item.
802adc4f0dbSShawn McCarney  * @param sensorObjPath D-Bus object path of sensor
803adc4f0dbSShawn McCarney  */
804b5a76932SEd Tanous inline void addInventoryItem(
805b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
806b5a76932SEd Tanous     const std::string& invItemObjPath, const std::string& sensorObjPath)
807adc4f0dbSShawn McCarney {
808adc4f0dbSShawn McCarney     // Look for inventory item in vector
809bd79bce8SPatrick Williams     InventoryItem* inventoryItem =
810bd79bce8SPatrick Williams         findInventoryItem(inventoryItems, invItemObjPath);
811adc4f0dbSShawn McCarney 
812adc4f0dbSShawn McCarney     // If inventory item doesn't exist in vector, add it
813adc4f0dbSShawn McCarney     if (inventoryItem == nullptr)
814adc4f0dbSShawn McCarney     {
815adc4f0dbSShawn McCarney         inventoryItems->emplace_back(invItemObjPath);
816adc4f0dbSShawn McCarney         inventoryItem = &(inventoryItems->back());
817adc4f0dbSShawn McCarney     }
818adc4f0dbSShawn McCarney 
819adc4f0dbSShawn McCarney     // Add sensor to set of sensors associated with inventory item
820adc4f0dbSShawn McCarney     inventoryItem->sensors.emplace(sensorObjPath);
821adc4f0dbSShawn McCarney }
822adc4f0dbSShawn McCarney 
823adc4f0dbSShawn McCarney /**
824adc4f0dbSShawn McCarney  * @brief Stores D-Bus data in the specified inventory item.
825adc4f0dbSShawn McCarney  *
826adc4f0dbSShawn McCarney  * Finds D-Bus data in the specified map of interfaces.  Stores the data in the
827adc4f0dbSShawn McCarney  * specified InventoryItem.
828adc4f0dbSShawn McCarney  *
829adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
830adc4f0dbSShawn McCarney  * response.
831adc4f0dbSShawn McCarney  *
832adc4f0dbSShawn McCarney  * @param inventoryItem Inventory item where data will be stored.
833adc4f0dbSShawn McCarney  * @param interfacesDict Map containing D-Bus interfaces and their properties
834adc4f0dbSShawn McCarney  * for the specified inventory item.
835adc4f0dbSShawn McCarney  */
83623a21a1cSEd Tanous inline void storeInventoryItemData(
837adc4f0dbSShawn McCarney     InventoryItem& inventoryItem,
83880f79a40SMichael Shen     const dbus::utility::DBusInterfacesMap& interfacesDict)
8398fb49dd6SShawn McCarney {
840adc4f0dbSShawn McCarney     // Get properties from Inventory.Item interface
841711ac7a9SEd Tanous 
8429eb808c1SEd Tanous     for (const auto& [interface, values] : interfacesDict)
8438fb49dd6SShawn McCarney     {
844711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Item")
8458fb49dd6SShawn McCarney         {
8469eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
847711ac7a9SEd Tanous             {
848711ac7a9SEd Tanous                 if (name == "Present")
849711ac7a9SEd Tanous                 {
850711ac7a9SEd Tanous                     const bool* value = std::get_if<bool>(&dbusValue);
851adc4f0dbSShawn McCarney                     if (value != nullptr)
8528fb49dd6SShawn McCarney                     {
853adc4f0dbSShawn McCarney                         inventoryItem.isPresent = *value;
8548fb49dd6SShawn McCarney                     }
8558fb49dd6SShawn McCarney                 }
8568fb49dd6SShawn McCarney             }
857711ac7a9SEd Tanous         }
858adc4f0dbSShawn McCarney         // Check if Inventory.Item.PowerSupply interface is present
859711ac7a9SEd Tanous 
860711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Item.PowerSupply")
8618fb49dd6SShawn McCarney         {
862adc4f0dbSShawn McCarney             inventoryItem.isPowerSupply = true;
8638fb49dd6SShawn McCarney         }
864adc4f0dbSShawn McCarney 
865adc4f0dbSShawn McCarney         // Get properties from Inventory.Decorator.Asset interface
866711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
867adc4f0dbSShawn McCarney         {
8689eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
869711ac7a9SEd Tanous             {
870711ac7a9SEd Tanous                 if (name == "Manufacturer")
871adc4f0dbSShawn McCarney                 {
872adc4f0dbSShawn McCarney                     const std::string* value =
873711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
874adc4f0dbSShawn McCarney                     if (value != nullptr)
875adc4f0dbSShawn McCarney                     {
876adc4f0dbSShawn McCarney                         inventoryItem.manufacturer = *value;
877adc4f0dbSShawn McCarney                     }
878adc4f0dbSShawn McCarney                 }
879711ac7a9SEd Tanous                 if (name == "Model")
880adc4f0dbSShawn McCarney                 {
881adc4f0dbSShawn McCarney                     const std::string* value =
882711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
883adc4f0dbSShawn McCarney                     if (value != nullptr)
884adc4f0dbSShawn McCarney                     {
885adc4f0dbSShawn McCarney                         inventoryItem.model = *value;
886adc4f0dbSShawn McCarney                     }
887adc4f0dbSShawn McCarney                 }
888711ac7a9SEd Tanous                 if (name == "SerialNumber")
889adc4f0dbSShawn McCarney                 {
890adc4f0dbSShawn McCarney                     const std::string* value =
891711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
892adc4f0dbSShawn McCarney                     if (value != nullptr)
893adc4f0dbSShawn McCarney                     {
894adc4f0dbSShawn McCarney                         inventoryItem.serialNumber = *value;
895adc4f0dbSShawn McCarney                     }
896adc4f0dbSShawn McCarney                 }
897711ac7a9SEd Tanous                 if (name == "PartNumber")
898711ac7a9SEd Tanous                 {
899711ac7a9SEd Tanous                     const std::string* value =
900711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
901711ac7a9SEd Tanous                     if (value != nullptr)
902711ac7a9SEd Tanous                     {
903711ac7a9SEd Tanous                         inventoryItem.partNumber = *value;
904711ac7a9SEd Tanous                     }
905711ac7a9SEd Tanous                 }
906711ac7a9SEd Tanous             }
907adc4f0dbSShawn McCarney         }
908adc4f0dbSShawn McCarney 
909711ac7a9SEd Tanous         if (interface ==
910711ac7a9SEd Tanous             "xyz.openbmc_project.State.Decorator.OperationalStatus")
911adc4f0dbSShawn McCarney         {
9129eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
913adc4f0dbSShawn McCarney             {
914711ac7a9SEd Tanous                 if (name == "Functional")
915711ac7a9SEd Tanous                 {
916711ac7a9SEd Tanous                     const bool* value = std::get_if<bool>(&dbusValue);
917adc4f0dbSShawn McCarney                     if (value != nullptr)
918adc4f0dbSShawn McCarney                     {
919adc4f0dbSShawn McCarney                         inventoryItem.isFunctional = *value;
9208fb49dd6SShawn McCarney                     }
9218fb49dd6SShawn McCarney                 }
9228fb49dd6SShawn McCarney             }
9238fb49dd6SShawn McCarney         }
924711ac7a9SEd Tanous     }
925711ac7a9SEd Tanous }
9268fb49dd6SShawn McCarney 
9278fb49dd6SShawn McCarney /**
928adc4f0dbSShawn McCarney  * @brief Gets D-Bus data for inventory items associated with sensors.
9298fb49dd6SShawn McCarney  *
930adc4f0dbSShawn McCarney  * Uses the specified connections (services) to obtain D-Bus data for inventory
931adc4f0dbSShawn McCarney  * items associated with sensors.  Stores the resulting data in the
932adc4f0dbSShawn McCarney  * inventoryItems vector.
9338fb49dd6SShawn McCarney  *
934adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
935adc4f0dbSShawn McCarney  * response.
936adc4f0dbSShawn McCarney  *
937adc4f0dbSShawn McCarney  * Finds the inventory item data asynchronously.  Invokes callback when data has
938adc4f0dbSShawn McCarney  * been obtained.
939adc4f0dbSShawn McCarney  *
940adc4f0dbSShawn McCarney  * The callback must have the following signature:
941adc4f0dbSShawn McCarney  *   @code
942d500549bSAnthony Wilson  *   callback(void)
943adc4f0dbSShawn McCarney  *   @endcode
944adc4f0dbSShawn McCarney  *
945adc4f0dbSShawn McCarney  * This function is called recursively, obtaining data asynchronously from one
946adc4f0dbSShawn McCarney  * connection in each call.  This ensures the callback is not invoked until the
947adc4f0dbSShawn McCarney  * last asynchronous function has completed.
9488fb49dd6SShawn McCarney  *
9498fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
950adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
951adc4f0dbSShawn McCarney  * @param invConnections Connections that provide data for the inventory items.
9528fb49dd6SShawn McCarney  * implements ObjectManager.
953adc4f0dbSShawn McCarney  * @param callback Callback to invoke when inventory data has been obtained.
954adc4f0dbSShawn McCarney  * @param invConnectionsIndex Current index in invConnections.  Only specified
955adc4f0dbSShawn McCarney  * in recursive calls to this function.
9568fb49dd6SShawn McCarney  */
957adc4f0dbSShawn McCarney template <typename Callback>
958adc4f0dbSShawn McCarney static void getInventoryItemsData(
9598fb49dd6SShawn McCarney     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
960adc4f0dbSShawn McCarney     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
961d0090733SEd Tanous     std::shared_ptr<std::set<std::string>> invConnections, Callback&& callback,
962d0090733SEd Tanous     size_t invConnectionsIndex = 0)
9638fb49dd6SShawn McCarney {
96462598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryItemsData enter");
9658fb49dd6SShawn McCarney 
966adc4f0dbSShawn McCarney     // If no more connections left, call callback
967adc4f0dbSShawn McCarney     if (invConnectionsIndex >= invConnections->size())
9688fb49dd6SShawn McCarney     {
969d500549bSAnthony Wilson         callback();
97062598e31SEd Tanous         BMCWEB_LOG_DEBUG("getInventoryItemsData exit");
971adc4f0dbSShawn McCarney         return;
972adc4f0dbSShawn McCarney     }
973adc4f0dbSShawn McCarney 
974adc4f0dbSShawn McCarney     // Get inventory item data from current connection
975fe04d49cSNan Zhou     auto it = invConnections->begin();
976fe04d49cSNan Zhou     std::advance(it, invConnectionsIndex);
977adc4f0dbSShawn McCarney     if (it != invConnections->end())
978adc4f0dbSShawn McCarney     {
979adc4f0dbSShawn McCarney         const std::string& invConnection = *it;
980adc4f0dbSShawn McCarney 
9815eb468daSGeorge Liu         // Get all object paths and their interfaces for current connection
9825eb468daSGeorge Liu         sdbusplus::message::object_path path("/xyz/openbmc_project/inventory");
9835eb468daSGeorge Liu         dbus::utility::getManagedObjects(
9845eb468daSGeorge Liu             invConnection, path,
9855eb468daSGeorge Liu             [sensorsAsyncResp, inventoryItems, invConnections,
9868cb2c024SEd Tanous              callback = std::forward<Callback>(callback), invConnectionsIndex](
9875e7e2dc5SEd Tanous                 const boost::system::error_code& ec,
98802cad96eSEd Tanous                 const dbus::utility::ManagedObjectType& resp) {
98962598e31SEd Tanous                 BMCWEB_LOG_DEBUG("getInventoryItemsData respHandler enter");
9908fb49dd6SShawn McCarney                 if (ec)
9918fb49dd6SShawn McCarney                 {
99262598e31SEd Tanous                     BMCWEB_LOG_ERROR(
99362598e31SEd Tanous                         "getInventoryItemsData respHandler DBus error {}", ec);
9948d1b46d7Szhanghch05                     messages::internalError(sensorsAsyncResp->asyncResp->res);
9958fb49dd6SShawn McCarney                     return;
9968fb49dd6SShawn McCarney                 }
9978fb49dd6SShawn McCarney 
9988fb49dd6SShawn McCarney                 // Loop through returned object paths
9998fb49dd6SShawn McCarney                 for (const auto& objDictEntry : resp)
10008fb49dd6SShawn McCarney                 {
10018fb49dd6SShawn McCarney                     const std::string& objPath =
10028fb49dd6SShawn McCarney                         static_cast<const std::string&>(objDictEntry.first);
10038fb49dd6SShawn McCarney 
1004bd79bce8SPatrick Williams                     // If this object path is one of the specified inventory
1005bd79bce8SPatrick Williams                     // items
1006bd79bce8SPatrick Williams                     InventoryItem* inventoryItem =
1007bd79bce8SPatrick Williams                         findInventoryItem(inventoryItems, objPath);
1008adc4f0dbSShawn McCarney                     if (inventoryItem != nullptr)
10098fb49dd6SShawn McCarney                     {
1010adc4f0dbSShawn McCarney                         // Store inventory data in InventoryItem
1011bd79bce8SPatrick Williams                         storeInventoryItemData(*inventoryItem,
1012bd79bce8SPatrick Williams                                                objDictEntry.second);
10138fb49dd6SShawn McCarney                     }
10148fb49dd6SShawn McCarney                 }
10158fb49dd6SShawn McCarney 
1016adc4f0dbSShawn McCarney                 // Recurse to get inventory item data from next connection
1017adc4f0dbSShawn McCarney                 getInventoryItemsData(sensorsAsyncResp, inventoryItems,
1018d0090733SEd Tanous                                       invConnections, std::move(callback),
1019d0090733SEd Tanous                                       invConnectionsIndex + 1);
1020adc4f0dbSShawn McCarney 
102162598e31SEd Tanous                 BMCWEB_LOG_DEBUG("getInventoryItemsData respHandler exit");
10225eb468daSGeorge Liu             });
10238fb49dd6SShawn McCarney     }
10248fb49dd6SShawn McCarney 
102562598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryItemsData exit");
10268fb49dd6SShawn McCarney }
10278fb49dd6SShawn McCarney 
10288fb49dd6SShawn McCarney /**
1029adc4f0dbSShawn McCarney  * @brief Gets connections that provide D-Bus data for inventory items.
10308fb49dd6SShawn McCarney  *
1031adc4f0dbSShawn McCarney  * Gets the D-Bus connections (services) that provide data for the inventory
1032adc4f0dbSShawn McCarney  * items that are associated with sensors.
10338fb49dd6SShawn McCarney  *
10348fb49dd6SShawn McCarney  * Finds the connections asynchronously.  Invokes callback when information has
10358fb49dd6SShawn McCarney  * been obtained.
10368fb49dd6SShawn McCarney  *
10378fb49dd6SShawn McCarney  * The callback must have the following signature:
10388fb49dd6SShawn McCarney  *   @code
1039fe04d49cSNan Zhou  *   callback(std::shared_ptr<std::set<std::string>> invConnections)
10408fb49dd6SShawn McCarney  *   @endcode
10418fb49dd6SShawn McCarney  *
10428fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
1043adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
10448fb49dd6SShawn McCarney  * @param callback Callback to invoke when connections have been obtained.
10458fb49dd6SShawn McCarney  */
10468fb49dd6SShawn McCarney template <typename Callback>
10478fb49dd6SShawn McCarney static void getInventoryItemsConnections(
1048b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1049b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
10508fb49dd6SShawn McCarney     Callback&& callback)
10518fb49dd6SShawn McCarney {
105262598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryItemsConnections enter");
10538fb49dd6SShawn McCarney 
10548fb49dd6SShawn McCarney     const std::string path = "/xyz/openbmc_project/inventory";
1055e99073f5SGeorge Liu     constexpr std::array<std::string_view, 4> interfaces = {
10568fb49dd6SShawn McCarney         "xyz.openbmc_project.Inventory.Item",
1057adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Item.PowerSupply",
1058adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Decorator.Asset",
10598fb49dd6SShawn McCarney         "xyz.openbmc_project.State.Decorator.OperationalStatus"};
10608fb49dd6SShawn McCarney 
1061e99073f5SGeorge Liu     // Make call to ObjectMapper to find all inventory items
1062e99073f5SGeorge Liu     dbus::utility::getSubTree(
1063e99073f5SGeorge Liu         path, 0, interfaces,
10648cb2c024SEd Tanous         [callback = std::forward<Callback>(callback), sensorsAsyncResp,
1065002d39b4SEd Tanous          inventoryItems](
1066e99073f5SGeorge Liu             const boost::system::error_code& ec,
1067002d39b4SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) {
1068e99073f5SGeorge Liu             // Response handler for parsing output from GetSubTree
106962598e31SEd Tanous             BMCWEB_LOG_DEBUG("getInventoryItemsConnections respHandler enter");
10708fb49dd6SShawn McCarney             if (ec)
10718fb49dd6SShawn McCarney             {
10728d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
107362598e31SEd Tanous                 BMCWEB_LOG_ERROR(
1074bd79bce8SPatrick Williams                     "getInventoryItemsConnections respHandler DBus error {}",
1075bd79bce8SPatrick Williams                     ec);
10768fb49dd6SShawn McCarney                 return;
10778fb49dd6SShawn McCarney             }
10788fb49dd6SShawn McCarney 
10798fb49dd6SShawn McCarney             // Make unique list of connections for desired inventory items
1080fe04d49cSNan Zhou             std::shared_ptr<std::set<std::string>> invConnections =
1081fe04d49cSNan Zhou                 std::make_shared<std::set<std::string>>();
10828fb49dd6SShawn McCarney 
10838fb49dd6SShawn McCarney             // Loop through objects from GetSubTree
1084bd79bce8SPatrick Williams             for (const std::pair<std::string,
1085bd79bce8SPatrick Williams                                  std::vector<std::pair<
1086bd79bce8SPatrick Williams                                      std::string, std::vector<std::string>>>>&
10878fb49dd6SShawn McCarney                      object : subtree)
10888fb49dd6SShawn McCarney             {
1089adc4f0dbSShawn McCarney                 // Check if object path is one of the specified inventory items
10908fb49dd6SShawn McCarney                 const std::string& objPath = object.first;
1091adc4f0dbSShawn McCarney                 if (findInventoryItem(inventoryItems, objPath) != nullptr)
10928fb49dd6SShawn McCarney                 {
10938fb49dd6SShawn McCarney                     // Store all connections to inventory item
10948fb49dd6SShawn McCarney                     for (const std::pair<std::string, std::vector<std::string>>&
10958fb49dd6SShawn McCarney                              objData : object.second)
10968fb49dd6SShawn McCarney                     {
10978fb49dd6SShawn McCarney                         const std::string& invConnection = objData.first;
10988fb49dd6SShawn McCarney                         invConnections->insert(invConnection);
10998fb49dd6SShawn McCarney                     }
11008fb49dd6SShawn McCarney                 }
11018fb49dd6SShawn McCarney             }
1102d500549bSAnthony Wilson 
11038fb49dd6SShawn McCarney             callback(invConnections);
110462598e31SEd Tanous             BMCWEB_LOG_DEBUG("getInventoryItemsConnections respHandler exit");
1105e99073f5SGeorge Liu         });
110662598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryItemsConnections exit");
11078fb49dd6SShawn McCarney }
11088fb49dd6SShawn McCarney 
11098fb49dd6SShawn McCarney /**
1110adc4f0dbSShawn McCarney  * @brief Gets associations from sensors to inventory items.
11118fb49dd6SShawn McCarney  *
11128fb49dd6SShawn McCarney  * Looks for ObjectMapper associations from the specified sensors to related
1113d500549bSAnthony Wilson  * inventory items. Then finds the associations from those inventory items to
1114d500549bSAnthony Wilson  * their LEDs, if any.
11158fb49dd6SShawn McCarney  *
11168fb49dd6SShawn McCarney  * Finds the inventory items asynchronously.  Invokes callback when information
11178fb49dd6SShawn McCarney  * has been obtained.
11188fb49dd6SShawn McCarney  *
11198fb49dd6SShawn McCarney  * The callback must have the following signature:
11208fb49dd6SShawn McCarney  *   @code
1121adc4f0dbSShawn McCarney  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
11228fb49dd6SShawn McCarney  *   @endcode
11238fb49dd6SShawn McCarney  *
11248fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
11258fb49dd6SShawn McCarney  * @param sensorNames All sensors within the current chassis.
11268fb49dd6SShawn McCarney  * implements ObjectManager.
11278fb49dd6SShawn McCarney  * @param callback Callback to invoke when inventory items have been obtained.
11288fb49dd6SShawn McCarney  */
11298fb49dd6SShawn McCarney template <typename Callback>
1130adc4f0dbSShawn McCarney static void getInventoryItemAssociations(
1131b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1132fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& sensorNames,
11338fb49dd6SShawn McCarney     Callback&& callback)
11348fb49dd6SShawn McCarney {
113562598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryItemAssociations enter");
11368fb49dd6SShawn McCarney 
11375eb468daSGeorge Liu     // Call GetManagedObjects on the ObjectMapper to get all associations
11385eb468daSGeorge Liu     sdbusplus::message::object_path path("/");
11395eb468daSGeorge Liu     dbus::utility::getManagedObjects(
11405eb468daSGeorge Liu         "xyz.openbmc_project.ObjectMapper", path,
11418cb2c024SEd Tanous         [callback = std::forward<Callback>(callback), sensorsAsyncResp,
11425e7e2dc5SEd Tanous          sensorNames](const boost::system::error_code& ec,
114302cad96eSEd Tanous                       const dbus::utility::ManagedObjectType& resp) {
114462598e31SEd Tanous             BMCWEB_LOG_DEBUG("getInventoryItemAssociations respHandler enter");
11458fb49dd6SShawn McCarney             if (ec)
11468fb49dd6SShawn McCarney             {
114762598e31SEd Tanous                 BMCWEB_LOG_ERROR(
1148bd79bce8SPatrick Williams                     "getInventoryItemAssociations respHandler DBus error {}",
1149bd79bce8SPatrick Williams                     ec);
11508d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
11518fb49dd6SShawn McCarney                 return;
11528fb49dd6SShawn McCarney             }
11538fb49dd6SShawn McCarney 
1154adc4f0dbSShawn McCarney             // Create vector to hold list of inventory items
1155adc4f0dbSShawn McCarney             std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1156adc4f0dbSShawn McCarney                 std::make_shared<std::vector<InventoryItem>>();
1157adc4f0dbSShawn McCarney 
11588fb49dd6SShawn McCarney             // Loop through returned object paths
11598fb49dd6SShawn McCarney             std::string sensorAssocPath;
11608fb49dd6SShawn McCarney             sensorAssocPath.reserve(128); // avoid memory allocations
11618fb49dd6SShawn McCarney             for (const auto& objDictEntry : resp)
11628fb49dd6SShawn McCarney             {
11638fb49dd6SShawn McCarney                 const std::string& objPath =
11648fb49dd6SShawn McCarney                     static_cast<const std::string&>(objDictEntry.first);
11658fb49dd6SShawn McCarney 
1166bd79bce8SPatrick Williams                 // If path is inventory association for one of the specified
1167bd79bce8SPatrick Williams                 // sensors
11688fb49dd6SShawn McCarney                 for (const std::string& sensorName : *sensorNames)
11698fb49dd6SShawn McCarney                 {
11708fb49dd6SShawn McCarney                     sensorAssocPath = sensorName;
11718fb49dd6SShawn McCarney                     sensorAssocPath += "/inventory";
11728fb49dd6SShawn McCarney                     if (objPath == sensorAssocPath)
11738fb49dd6SShawn McCarney                     {
11748fb49dd6SShawn McCarney                         // Get Association interface for object path
1175bd79bce8SPatrick Williams                         for (const auto& [interface, values] :
1176bd79bce8SPatrick Williams                              objDictEntry.second)
11778fb49dd6SShawn McCarney                         {
1178711ac7a9SEd Tanous                             if (interface == "xyz.openbmc_project.Association")
1179711ac7a9SEd Tanous                             {
1180711ac7a9SEd Tanous                                 for (const auto& [valueName, value] : values)
1181711ac7a9SEd Tanous                                 {
1182711ac7a9SEd Tanous                                     if (valueName == "endpoints")
11838fb49dd6SShawn McCarney                                     {
1184bd79bce8SPatrick Williams                                         const std::vector<std::string>*
1185bd79bce8SPatrick Williams                                             endpoints = std::get_if<
1186bd79bce8SPatrick Williams                                                 std::vector<std::string>>(
1187711ac7a9SEd Tanous                                                 &value);
1188711ac7a9SEd Tanous                                         if ((endpoints != nullptr) &&
1189711ac7a9SEd Tanous                                             !endpoints->empty())
11908fb49dd6SShawn McCarney                                         {
1191adc4f0dbSShawn McCarney                                             // Add inventory item to vector
1192adc4f0dbSShawn McCarney                                             const std::string& invItemPath =
1193adc4f0dbSShawn McCarney                                                 endpoints->front();
1194711ac7a9SEd Tanous                                             addInventoryItem(inventoryItems,
1195711ac7a9SEd Tanous                                                              invItemPath,
1196adc4f0dbSShawn McCarney                                                              sensorName);
11978fb49dd6SShawn McCarney                                         }
11988fb49dd6SShawn McCarney                                     }
11998fb49dd6SShawn McCarney                                 }
1200711ac7a9SEd Tanous                             }
1201711ac7a9SEd Tanous                         }
12028fb49dd6SShawn McCarney                         break;
12038fb49dd6SShawn McCarney                     }
12048fb49dd6SShawn McCarney                 }
12058fb49dd6SShawn McCarney             }
12068fb49dd6SShawn McCarney 
1207d500549bSAnthony Wilson             // Now loop through the returned object paths again, this time to
1208d500549bSAnthony Wilson             // find the leds associated with the inventory items we just found
1209d500549bSAnthony Wilson             std::string inventoryAssocPath;
1210d500549bSAnthony Wilson             inventoryAssocPath.reserve(128); // avoid memory allocations
1211d500549bSAnthony Wilson             for (const auto& objDictEntry : resp)
1212d500549bSAnthony Wilson             {
1213d500549bSAnthony Wilson                 const std::string& objPath =
1214d500549bSAnthony Wilson                     static_cast<const std::string&>(objDictEntry.first);
1215d500549bSAnthony Wilson 
1216d500549bSAnthony Wilson                 for (InventoryItem& inventoryItem : *inventoryItems)
1217d500549bSAnthony Wilson                 {
1218d500549bSAnthony Wilson                     inventoryAssocPath = inventoryItem.objectPath;
1219d500549bSAnthony Wilson                     inventoryAssocPath += "/leds";
1220d500549bSAnthony Wilson                     if (objPath == inventoryAssocPath)
1221d500549bSAnthony Wilson                     {
1222bd79bce8SPatrick Williams                         for (const auto& [interface, values] :
1223bd79bce8SPatrick Williams                              objDictEntry.second)
1224d500549bSAnthony Wilson                         {
1225711ac7a9SEd Tanous                             if (interface == "xyz.openbmc_project.Association")
1226711ac7a9SEd Tanous                             {
1227711ac7a9SEd Tanous                                 for (const auto& [valueName, value] : values)
1228711ac7a9SEd Tanous                                 {
1229711ac7a9SEd Tanous                                     if (valueName == "endpoints")
1230d500549bSAnthony Wilson                                     {
1231bd79bce8SPatrick Williams                                         const std::vector<std::string>*
1232bd79bce8SPatrick Williams                                             endpoints = std::get_if<
1233bd79bce8SPatrick Williams                                                 std::vector<std::string>>(
1234711ac7a9SEd Tanous                                                 &value);
1235711ac7a9SEd Tanous                                         if ((endpoints != nullptr) &&
1236711ac7a9SEd Tanous                                             !endpoints->empty())
1237d500549bSAnthony Wilson                                         {
1238711ac7a9SEd Tanous                                             // Add inventory item to vector
1239d500549bSAnthony Wilson                                             // Store LED path in inventory item
1240711ac7a9SEd Tanous                                             const std::string& ledPath =
1241711ac7a9SEd Tanous                                                 endpoints->front();
1242bd79bce8SPatrick Williams                                             inventoryItem.ledObjectPath =
1243bd79bce8SPatrick Williams                                                 ledPath;
1244d500549bSAnthony Wilson                                         }
1245d500549bSAnthony Wilson                                     }
1246d500549bSAnthony Wilson                                 }
1247711ac7a9SEd Tanous                             }
1248711ac7a9SEd Tanous                         }
1249711ac7a9SEd Tanous 
1250d500549bSAnthony Wilson                         break;
1251d500549bSAnthony Wilson                     }
1252d500549bSAnthony Wilson                 }
1253d500549bSAnthony Wilson             }
1254adc4f0dbSShawn McCarney             callback(inventoryItems);
125562598e31SEd Tanous             BMCWEB_LOG_DEBUG("getInventoryItemAssociations respHandler exit");
12565eb468daSGeorge Liu         });
12578fb49dd6SShawn McCarney 
125862598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryItemAssociations exit");
12598fb49dd6SShawn McCarney }
12608fb49dd6SShawn McCarney 
12618fb49dd6SShawn McCarney /**
1262d500549bSAnthony Wilson  * @brief Gets D-Bus data for inventory item leds associated with sensors.
1263d500549bSAnthony Wilson  *
1264d500549bSAnthony Wilson  * Uses the specified connections (services) to obtain D-Bus data for inventory
1265d500549bSAnthony Wilson  * item leds associated with sensors.  Stores the resulting data in the
1266d500549bSAnthony Wilson  * inventoryItems vector.
1267d500549bSAnthony Wilson  *
1268d500549bSAnthony Wilson  * This data is later used to provide sensor property values in the JSON
1269d500549bSAnthony Wilson  * response.
1270d500549bSAnthony Wilson  *
1271d500549bSAnthony Wilson  * Finds the inventory item led data asynchronously.  Invokes callback when data
1272d500549bSAnthony Wilson  * has been obtained.
1273d500549bSAnthony Wilson  *
1274d500549bSAnthony Wilson  * The callback must have the following signature:
1275d500549bSAnthony Wilson  *   @code
127642cbe538SGunnar Mills  *   callback()
1277d500549bSAnthony Wilson  *   @endcode
1278d500549bSAnthony Wilson  *
1279d500549bSAnthony Wilson  * This function is called recursively, obtaining data asynchronously from one
1280d500549bSAnthony Wilson  * connection in each call.  This ensures the callback is not invoked until the
1281d500549bSAnthony Wilson  * last asynchronous function has completed.
1282d500549bSAnthony Wilson  *
1283d500549bSAnthony Wilson  * @param sensorsAsyncResp Pointer to object holding response data.
1284d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1285d500549bSAnthony Wilson  * @param ledConnections Connections that provide data for the inventory leds.
1286d500549bSAnthony Wilson  * @param callback Callback to invoke when inventory data has been obtained.
1287d500549bSAnthony Wilson  * @param ledConnectionsIndex Current index in ledConnections.  Only specified
1288d500549bSAnthony Wilson  * in recursive calls to this function.
1289d500549bSAnthony Wilson  */
1290d500549bSAnthony Wilson template <typename Callback>
1291d500549bSAnthony Wilson void getInventoryLedData(
1292d500549bSAnthony Wilson     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1293d500549bSAnthony Wilson     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1294fe04d49cSNan Zhou     std::shared_ptr<std::map<std::string, std::string>> ledConnections,
1295d500549bSAnthony Wilson     Callback&& callback, size_t ledConnectionsIndex = 0)
1296d500549bSAnthony Wilson {
129762598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryLedData enter");
1298d500549bSAnthony Wilson 
1299d500549bSAnthony Wilson     // If no more connections left, call callback
1300d500549bSAnthony Wilson     if (ledConnectionsIndex >= ledConnections->size())
1301d500549bSAnthony Wilson     {
130242cbe538SGunnar Mills         callback();
130362598e31SEd Tanous         BMCWEB_LOG_DEBUG("getInventoryLedData exit");
1304d500549bSAnthony Wilson         return;
1305d500549bSAnthony Wilson     }
1306d500549bSAnthony Wilson 
1307d500549bSAnthony Wilson     // Get inventory item data from current connection
1308fe04d49cSNan Zhou     auto it = ledConnections->begin();
1309fe04d49cSNan Zhou     std::advance(it, ledConnectionsIndex);
1310d500549bSAnthony Wilson     if (it != ledConnections->end())
1311d500549bSAnthony Wilson     {
1312d500549bSAnthony Wilson         const std::string& ledPath = (*it).first;
1313d500549bSAnthony Wilson         const std::string& ledConnection = (*it).second;
1314d500549bSAnthony Wilson         // Response handler for Get State property
13151e1e598dSJonathan Doman         auto respHandler =
13161e1e598dSJonathan Doman             [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
13178cb2c024SEd Tanous              callback = std::forward<Callback>(callback), ledConnectionsIndex](
13185e7e2dc5SEd Tanous                 const boost::system::error_code& ec, const std::string& state) {
131962598e31SEd Tanous                 BMCWEB_LOG_DEBUG("getInventoryLedData respHandler enter");
1320d500549bSAnthony Wilson                 if (ec)
1321d500549bSAnthony Wilson                 {
132262598e31SEd Tanous                     BMCWEB_LOG_ERROR(
132362598e31SEd Tanous                         "getInventoryLedData respHandler DBus error {}", ec);
13248d1b46d7Szhanghch05                     messages::internalError(sensorsAsyncResp->asyncResp->res);
1325d500549bSAnthony Wilson                     return;
1326d500549bSAnthony Wilson                 }
1327d500549bSAnthony Wilson 
132862598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Led state: {}", state);
1329d500549bSAnthony Wilson                 // Find inventory item with this LED object path
1330d500549bSAnthony Wilson                 InventoryItem* inventoryItem =
1331d500549bSAnthony Wilson                     findInventoryItemForLed(*inventoryItems, ledPath);
1332d500549bSAnthony Wilson                 if (inventoryItem != nullptr)
1333d500549bSAnthony Wilson                 {
1334d500549bSAnthony Wilson                     // Store LED state in InventoryItem
133511ba3979SEd Tanous                     if (state.ends_with("On"))
1336d500549bSAnthony Wilson                     {
1337*c9563608SJanet Adkins                         inventoryItem->ledState = sensor_utils::LedState::ON;
1338d500549bSAnthony Wilson                     }
133911ba3979SEd Tanous                     else if (state.ends_with("Blink"))
1340d500549bSAnthony Wilson                     {
1341*c9563608SJanet Adkins                         inventoryItem->ledState = sensor_utils::LedState::BLINK;
1342d500549bSAnthony Wilson                     }
134311ba3979SEd Tanous                     else if (state.ends_with("Off"))
1344d500549bSAnthony Wilson                     {
1345*c9563608SJanet Adkins                         inventoryItem->ledState = sensor_utils::LedState::OFF;
1346d500549bSAnthony Wilson                     }
1347d500549bSAnthony Wilson                     else
1348d500549bSAnthony Wilson                     {
1349*c9563608SJanet Adkins                         inventoryItem->ledState =
1350*c9563608SJanet Adkins                             sensor_utils::LedState::UNKNOWN;
1351d500549bSAnthony Wilson                     }
1352d500549bSAnthony Wilson                 }
1353d500549bSAnthony Wilson 
1354d500549bSAnthony Wilson                 // Recurse to get LED data from next connection
1355d500549bSAnthony Wilson                 getInventoryLedData(sensorsAsyncResp, inventoryItems,
1356d500549bSAnthony Wilson                                     ledConnections, std::move(callback),
1357d500549bSAnthony Wilson                                     ledConnectionsIndex + 1);
1358d500549bSAnthony Wilson 
135962598e31SEd Tanous                 BMCWEB_LOG_DEBUG("getInventoryLedData respHandler exit");
1360d500549bSAnthony Wilson             };
1361d500549bSAnthony Wilson 
1362d500549bSAnthony Wilson         // Get the State property for the current LED
13631e1e598dSJonathan Doman         sdbusplus::asio::getProperty<std::string>(
13641e1e598dSJonathan Doman             *crow::connections::systemBus, ledConnection, ledPath,
13651e1e598dSJonathan Doman             "xyz.openbmc_project.Led.Physical", "State",
13661e1e598dSJonathan Doman             std::move(respHandler));
1367d500549bSAnthony Wilson     }
1368d500549bSAnthony Wilson 
136962598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryLedData exit");
1370d500549bSAnthony Wilson }
1371d500549bSAnthony Wilson 
1372d500549bSAnthony Wilson /**
1373d500549bSAnthony Wilson  * @brief Gets LED data for LEDs associated with given inventory items.
1374d500549bSAnthony Wilson  *
1375d500549bSAnthony Wilson  * Gets the D-Bus connections (services) that provide LED data for the LEDs
1376d500549bSAnthony Wilson  * associated with the specified inventory items.  Then gets the LED data from
1377d500549bSAnthony Wilson  * each connection and stores it in the inventory item.
1378d500549bSAnthony Wilson  *
1379d500549bSAnthony Wilson  * This data is later used to provide sensor property values in the JSON
1380d500549bSAnthony Wilson  * response.
1381d500549bSAnthony Wilson  *
1382d500549bSAnthony Wilson  * Finds the LED data asynchronously.  Invokes callback when information has
1383d500549bSAnthony Wilson  * been obtained.
1384d500549bSAnthony Wilson  *
1385d500549bSAnthony Wilson  * The callback must have the following signature:
1386d500549bSAnthony Wilson  *   @code
138742cbe538SGunnar Mills  *   callback()
1388d500549bSAnthony Wilson  *   @endcode
1389d500549bSAnthony Wilson  *
1390d500549bSAnthony Wilson  * @param sensorsAsyncResp Pointer to object holding response data.
1391d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1392d500549bSAnthony Wilson  * @param callback Callback to invoke when inventory items have been obtained.
1393d500549bSAnthony Wilson  */
1394d500549bSAnthony Wilson template <typename Callback>
1395d500549bSAnthony Wilson void getInventoryLeds(
1396d500549bSAnthony Wilson     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1397d500549bSAnthony Wilson     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1398d500549bSAnthony Wilson     Callback&& callback)
1399d500549bSAnthony Wilson {
140062598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryLeds enter");
1401d500549bSAnthony Wilson 
1402d500549bSAnthony Wilson     const std::string path = "/xyz/openbmc_project";
1403e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
1404d500549bSAnthony Wilson         "xyz.openbmc_project.Led.Physical"};
1405d500549bSAnthony Wilson 
1406e99073f5SGeorge Liu     // Make call to ObjectMapper to find all inventory items
1407e99073f5SGeorge Liu     dbus::utility::getSubTree(
1408e99073f5SGeorge Liu         path, 0, interfaces,
14098cb2c024SEd Tanous         [callback = std::forward<Callback>(callback), sensorsAsyncResp,
1410002d39b4SEd Tanous          inventoryItems](
1411e99073f5SGeorge Liu             const boost::system::error_code& ec,
1412002d39b4SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) {
1413e99073f5SGeorge Liu             // Response handler for parsing output from GetSubTree
141462598e31SEd Tanous             BMCWEB_LOG_DEBUG("getInventoryLeds respHandler enter");
1415d500549bSAnthony Wilson             if (ec)
1416d500549bSAnthony Wilson             {
14178d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
1418bd79bce8SPatrick Williams                 BMCWEB_LOG_ERROR("getInventoryLeds respHandler DBus error {}",
1419bd79bce8SPatrick Williams                                  ec);
1420d500549bSAnthony Wilson                 return;
1421d500549bSAnthony Wilson             }
1422d500549bSAnthony Wilson 
1423d500549bSAnthony Wilson             // Build map of LED object paths to connections
1424fe04d49cSNan Zhou             std::shared_ptr<std::map<std::string, std::string>> ledConnections =
1425fe04d49cSNan Zhou                 std::make_shared<std::map<std::string, std::string>>();
1426d500549bSAnthony Wilson 
1427d500549bSAnthony Wilson             // Loop through objects from GetSubTree
1428bd79bce8SPatrick Williams             for (const std::pair<std::string,
1429bd79bce8SPatrick Williams                                  std::vector<std::pair<
1430bd79bce8SPatrick Williams                                      std::string, std::vector<std::string>>>>&
1431d500549bSAnthony Wilson                      object : subtree)
1432d500549bSAnthony Wilson             {
1433bd79bce8SPatrick Williams                 // Check if object path is LED for one of the specified
1434bd79bce8SPatrick Williams                 // inventory items
1435d500549bSAnthony Wilson                 const std::string& ledPath = object.first;
1436bd79bce8SPatrick Williams                 if (findInventoryItemForLed(*inventoryItems, ledPath) !=
1437bd79bce8SPatrick Williams                     nullptr)
1438d500549bSAnthony Wilson                 {
1439d500549bSAnthony Wilson                     // Add mapping from ledPath to connection
1440bd79bce8SPatrick Williams                     const std::string& connection =
1441bd79bce8SPatrick Williams                         object.second.begin()->first;
1442d500549bSAnthony Wilson                     (*ledConnections)[ledPath] = connection;
1443bd79bce8SPatrick Williams                     BMCWEB_LOG_DEBUG("Added mapping {} -> {}", ledPath,
1444bd79bce8SPatrick Williams                                      connection);
1445d500549bSAnthony Wilson                 }
1446d500549bSAnthony Wilson             }
1447d500549bSAnthony Wilson 
1448bd79bce8SPatrick Williams             getInventoryLedData(sensorsAsyncResp, inventoryItems,
1449bd79bce8SPatrick Williams                                 ledConnections, std::move(callback));
145062598e31SEd Tanous             BMCWEB_LOG_DEBUG("getInventoryLeds respHandler exit");
1451e99073f5SGeorge Liu         });
145262598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryLeds exit");
1453d500549bSAnthony Wilson }
1454d500549bSAnthony Wilson 
1455d500549bSAnthony Wilson /**
145642cbe538SGunnar Mills  * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
145742cbe538SGunnar Mills  *
145842cbe538SGunnar Mills  * Uses the specified connections (services) (currently assumes just one) to
145942cbe538SGunnar Mills  * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
146042cbe538SGunnar Mills  * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
146142cbe538SGunnar Mills  *
146242cbe538SGunnar Mills  * This data is later used to provide sensor property values in the JSON
146342cbe538SGunnar Mills  * response.
146442cbe538SGunnar Mills  *
146542cbe538SGunnar Mills  * Finds the Power Supply Attributes data asynchronously.  Invokes callback
146642cbe538SGunnar Mills  * when data has been obtained.
146742cbe538SGunnar Mills  *
146842cbe538SGunnar Mills  * The callback must have the following signature:
146942cbe538SGunnar Mills  *   @code
147042cbe538SGunnar Mills  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
147142cbe538SGunnar Mills  *   @endcode
147242cbe538SGunnar Mills  *
147342cbe538SGunnar Mills  * @param sensorsAsyncResp Pointer to object holding response data.
147442cbe538SGunnar Mills  * @param inventoryItems D-Bus inventory items associated with sensors.
147542cbe538SGunnar Mills  * @param psAttributesConnections Connections that provide data for the Power
147642cbe538SGunnar Mills  *        Supply Attributes
147742cbe538SGunnar Mills  * @param callback Callback to invoke when data has been obtained.
147842cbe538SGunnar Mills  */
147942cbe538SGunnar Mills template <typename Callback>
148042cbe538SGunnar Mills void getPowerSupplyAttributesData(
1481b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
148242cbe538SGunnar Mills     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1483fe04d49cSNan Zhou     const std::map<std::string, std::string>& psAttributesConnections,
148442cbe538SGunnar Mills     Callback&& callback)
148542cbe538SGunnar Mills {
148662598e31SEd Tanous     BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData enter");
148742cbe538SGunnar Mills 
148842cbe538SGunnar Mills     if (psAttributesConnections.empty())
148942cbe538SGunnar Mills     {
149062598e31SEd Tanous         BMCWEB_LOG_DEBUG("Can't find PowerSupplyAttributes, no connections!");
149142cbe538SGunnar Mills         callback(inventoryItems);
149242cbe538SGunnar Mills         return;
149342cbe538SGunnar Mills     }
149442cbe538SGunnar Mills 
149542cbe538SGunnar Mills     // Assuming just one connection (service) for now
1496fe04d49cSNan Zhou     auto it = psAttributesConnections.begin();
149742cbe538SGunnar Mills 
149842cbe538SGunnar Mills     const std::string& psAttributesPath = (*it).first;
149942cbe538SGunnar Mills     const std::string& psAttributesConnection = (*it).second;
150042cbe538SGunnar Mills 
150142cbe538SGunnar Mills     // Response handler for Get DeratingFactor property
15025a39f77aSPatrick Williams     auto respHandler = [sensorsAsyncResp, inventoryItems,
15038cb2c024SEd Tanous                         callback = std::forward<Callback>(callback)](
15045a39f77aSPatrick Williams                            const boost::system::error_code& ec,
15055a39f77aSPatrick Williams                            const uint32_t value) {
150662598e31SEd Tanous         BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData respHandler enter");
150742cbe538SGunnar Mills         if (ec)
150842cbe538SGunnar Mills         {
150962598e31SEd Tanous             BMCWEB_LOG_ERROR(
151062598e31SEd Tanous                 "getPowerSupplyAttributesData respHandler DBus error {}", ec);
15118d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
151242cbe538SGunnar Mills             return;
151342cbe538SGunnar Mills         }
151442cbe538SGunnar Mills 
151562598e31SEd Tanous         BMCWEB_LOG_DEBUG("PS EfficiencyPercent value: {}", value);
151642cbe538SGunnar Mills         // Store value in Power Supply Inventory Items
151742cbe538SGunnar Mills         for (InventoryItem& inventoryItem : *inventoryItems)
151842cbe538SGunnar Mills         {
151955f79e6fSEd Tanous             if (inventoryItem.isPowerSupply)
152042cbe538SGunnar Mills             {
152142cbe538SGunnar Mills                 inventoryItem.powerSupplyEfficiencyPercent =
15221e1e598dSJonathan Doman                     static_cast<int>(value);
152342cbe538SGunnar Mills             }
152442cbe538SGunnar Mills         }
152542cbe538SGunnar Mills 
152662598e31SEd Tanous         BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData respHandler exit");
152742cbe538SGunnar Mills         callback(inventoryItems);
152842cbe538SGunnar Mills     };
152942cbe538SGunnar Mills 
153042cbe538SGunnar Mills     // Get the DeratingFactor property for the PowerSupplyAttributes
153142cbe538SGunnar Mills     // Currently only property on the interface/only one we care about
15321e1e598dSJonathan Doman     sdbusplus::asio::getProperty<uint32_t>(
15331e1e598dSJonathan Doman         *crow::connections::systemBus, psAttributesConnection, psAttributesPath,
15341e1e598dSJonathan Doman         "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor",
15351e1e598dSJonathan Doman         std::move(respHandler));
153642cbe538SGunnar Mills 
153762598e31SEd Tanous     BMCWEB_LOG_DEBUG("getPowerSupplyAttributesData exit");
153842cbe538SGunnar Mills }
153942cbe538SGunnar Mills 
154042cbe538SGunnar Mills /**
154142cbe538SGunnar Mills  * @brief Gets the Power Supply Attributes such as EfficiencyPercent
154242cbe538SGunnar Mills  *
154342cbe538SGunnar Mills  * Gets the D-Bus connection (service) that provides Power Supply Attributes
154442cbe538SGunnar Mills  * data. Then gets the Power Supply Attributes data from the connection
154542cbe538SGunnar Mills  * (currently just assumes 1 connection) and stores the data in the inventory
154642cbe538SGunnar Mills  * item.
154742cbe538SGunnar Mills  *
154842cbe538SGunnar Mills  * This data is later used to provide sensor property values in the JSON
154942cbe538SGunnar Mills  * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
155042cbe538SGunnar Mills  *
155142cbe538SGunnar Mills  * Finds the Power Supply Attributes data asynchronously. Invokes callback
155242cbe538SGunnar Mills  * when information has been obtained.
155342cbe538SGunnar Mills  *
155442cbe538SGunnar Mills  * The callback must have the following signature:
155542cbe538SGunnar Mills  *   @code
155642cbe538SGunnar Mills  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
155742cbe538SGunnar Mills  *   @endcode
155842cbe538SGunnar Mills  *
155942cbe538SGunnar Mills  * @param sensorsAsyncResp Pointer to object holding response data.
156042cbe538SGunnar Mills  * @param inventoryItems D-Bus inventory items associated with sensors.
156142cbe538SGunnar Mills  * @param callback Callback to invoke when data has been obtained.
156242cbe538SGunnar Mills  */
156342cbe538SGunnar Mills template <typename Callback>
156442cbe538SGunnar Mills void getPowerSupplyAttributes(
156542cbe538SGunnar Mills     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
156642cbe538SGunnar Mills     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
156742cbe538SGunnar Mills     Callback&& callback)
156842cbe538SGunnar Mills {
156962598e31SEd Tanous     BMCWEB_LOG_DEBUG("getPowerSupplyAttributes enter");
157042cbe538SGunnar Mills 
157142cbe538SGunnar Mills     // Only need the power supply attributes when the Power Schema
1572*c9563608SJanet Adkins     if (sensorsAsyncResp->chassisSubNode != sensor_utils::powerNode)
157342cbe538SGunnar Mills     {
157462598e31SEd Tanous         BMCWEB_LOG_DEBUG("getPowerSupplyAttributes exit since not Power");
157542cbe538SGunnar Mills         callback(inventoryItems);
157642cbe538SGunnar Mills         return;
157742cbe538SGunnar Mills     }
157842cbe538SGunnar Mills 
1579e99073f5SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
158042cbe538SGunnar Mills         "xyz.openbmc_project.Control.PowerSupplyAttributes"};
158142cbe538SGunnar Mills 
1582e99073f5SGeorge Liu     // Make call to ObjectMapper to find the PowerSupplyAttributes service
1583e99073f5SGeorge Liu     dbus::utility::getSubTree(
1584e99073f5SGeorge Liu         "/xyz/openbmc_project", 0, interfaces,
15858cb2c024SEd Tanous         [callback = std::forward<Callback>(callback), sensorsAsyncResp,
1586b9d36b47SEd Tanous          inventoryItems](
1587e99073f5SGeorge Liu             const boost::system::error_code& ec,
1588b9d36b47SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) {
1589e99073f5SGeorge Liu             // Response handler for parsing output from GetSubTree
159062598e31SEd Tanous             BMCWEB_LOG_DEBUG("getPowerSupplyAttributes respHandler enter");
159142cbe538SGunnar Mills             if (ec)
159242cbe538SGunnar Mills             {
15938d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
159462598e31SEd Tanous                 BMCWEB_LOG_ERROR(
159562598e31SEd Tanous                     "getPowerSupplyAttributes respHandler DBus error {}", ec);
159642cbe538SGunnar Mills                 return;
159742cbe538SGunnar Mills             }
159826f6976fSEd Tanous             if (subtree.empty())
159942cbe538SGunnar Mills             {
160062598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Can't find Power Supply Attributes!");
160142cbe538SGunnar Mills                 callback(inventoryItems);
160242cbe538SGunnar Mills                 return;
160342cbe538SGunnar Mills             }
160442cbe538SGunnar Mills 
160542cbe538SGunnar Mills             // Currently we only support 1 power supply attribute, use this for
160642cbe538SGunnar Mills             // all the power supplies. Build map of object path to connection.
160742cbe538SGunnar Mills             // Assume just 1 connection and 1 path for now.
1608fe04d49cSNan Zhou             std::map<std::string, std::string> psAttributesConnections;
160942cbe538SGunnar Mills 
161042cbe538SGunnar Mills             if (subtree[0].first.empty() || subtree[0].second.empty())
161142cbe538SGunnar Mills             {
161262598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Power Supply Attributes mapper error!");
161342cbe538SGunnar Mills                 callback(inventoryItems);
161442cbe538SGunnar Mills                 return;
161542cbe538SGunnar Mills             }
161642cbe538SGunnar Mills 
161742cbe538SGunnar Mills             const std::string& psAttributesPath = subtree[0].first;
161842cbe538SGunnar Mills             const std::string& connection = subtree[0].second.begin()->first;
161942cbe538SGunnar Mills 
162042cbe538SGunnar Mills             if (connection.empty())
162142cbe538SGunnar Mills             {
162262598e31SEd Tanous                 BMCWEB_LOG_DEBUG("Power Supply Attributes mapper error!");
162342cbe538SGunnar Mills                 callback(inventoryItems);
162442cbe538SGunnar Mills                 return;
162542cbe538SGunnar Mills             }
162642cbe538SGunnar Mills 
162742cbe538SGunnar Mills             psAttributesConnections[psAttributesPath] = connection;
162862598e31SEd Tanous             BMCWEB_LOG_DEBUG("Added mapping {} -> {}", psAttributesPath,
162962598e31SEd Tanous                              connection);
163042cbe538SGunnar Mills 
163142cbe538SGunnar Mills             getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
163242cbe538SGunnar Mills                                          psAttributesConnections,
163342cbe538SGunnar Mills                                          std::move(callback));
163462598e31SEd Tanous             BMCWEB_LOG_DEBUG("getPowerSupplyAttributes respHandler exit");
1635e99073f5SGeorge Liu         });
163662598e31SEd Tanous     BMCWEB_LOG_DEBUG("getPowerSupplyAttributes exit");
163742cbe538SGunnar Mills }
163842cbe538SGunnar Mills 
163942cbe538SGunnar Mills /**
1640adc4f0dbSShawn McCarney  * @brief Gets inventory items associated with sensors.
16418fb49dd6SShawn McCarney  *
16428fb49dd6SShawn McCarney  * Finds the inventory items that are associated with the specified sensors.
1643adc4f0dbSShawn McCarney  * Then gets D-Bus data for the inventory items, such as presence and VPD.
16448fb49dd6SShawn McCarney  *
1645adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
1646adc4f0dbSShawn McCarney  * response.
16478fb49dd6SShawn McCarney  *
1648adc4f0dbSShawn McCarney  * Finds the inventory items asynchronously.  Invokes callback when the
1649adc4f0dbSShawn McCarney  * inventory items have been obtained.
1650adc4f0dbSShawn McCarney  *
1651adc4f0dbSShawn McCarney  * The callback must have the following signature:
1652adc4f0dbSShawn McCarney  *   @code
1653adc4f0dbSShawn McCarney  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
1654adc4f0dbSShawn McCarney  *   @endcode
16558fb49dd6SShawn McCarney  *
16568fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
16578fb49dd6SShawn McCarney  * @param sensorNames All sensors within the current chassis.
16588fb49dd6SShawn McCarney  * implements ObjectManager.
1659adc4f0dbSShawn McCarney  * @param callback Callback to invoke when inventory items have been obtained.
16608fb49dd6SShawn McCarney  */
1661adc4f0dbSShawn McCarney template <typename Callback>
1662d0090733SEd Tanous static void
1663d0090733SEd Tanous     getInventoryItems(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1664fe04d49cSNan Zhou                       const std::shared_ptr<std::set<std::string>> sensorNames,
1665adc4f0dbSShawn McCarney                       Callback&& callback)
16668fb49dd6SShawn McCarney {
166762598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryItems enter");
1668adc4f0dbSShawn McCarney     auto getInventoryItemAssociationsCb =
16698cb2c024SEd Tanous         [sensorsAsyncResp, callback = std::forward<Callback>(callback)](
1670adc4f0dbSShawn McCarney             std::shared_ptr<std::vector<InventoryItem>> inventoryItems) {
167162598e31SEd Tanous             BMCWEB_LOG_DEBUG("getInventoryItemAssociationsCb enter");
16728fb49dd6SShawn McCarney             auto getInventoryItemsConnectionsCb =
1673d0090733SEd Tanous                 [sensorsAsyncResp, inventoryItems,
16748cb2c024SEd Tanous                  callback = std::forward<const Callback>(callback)](
1675fe04d49cSNan Zhou                     std::shared_ptr<std::set<std::string>> invConnections) {
167662598e31SEd Tanous                     BMCWEB_LOG_DEBUG("getInventoryItemsConnectionsCb enter");
1677bd79bce8SPatrick Williams                     auto getInventoryItemsDataCb =
1678bd79bce8SPatrick Williams                         [sensorsAsyncResp, inventoryItems,
1679d500549bSAnthony Wilson                          callback{std::move(callback)}]() {
168062598e31SEd Tanous                             BMCWEB_LOG_DEBUG("getInventoryItemsDataCb enter");
168142cbe538SGunnar Mills 
1682bd79bce8SPatrick Williams                             auto getInventoryLedsCb =
1683bd79bce8SPatrick Williams                                 [sensorsAsyncResp, inventoryItems,
1684002d39b4SEd Tanous                                  callback{std::move(callback)}]() {
1685bd79bce8SPatrick Williams                                     BMCWEB_LOG_DEBUG(
1686bd79bce8SPatrick Williams                                         "getInventoryLedsCb enter");
1687bd79bce8SPatrick Williams                                     // Find Power Supply Attributes and get the
1688bd79bce8SPatrick Williams                                     // data
1689bd79bce8SPatrick Williams                                     getPowerSupplyAttributes(
1690bd79bce8SPatrick Williams                                         sensorsAsyncResp, inventoryItems,
169142cbe538SGunnar Mills                                         std::move(callback));
169262598e31SEd Tanous                                     BMCWEB_LOG_DEBUG("getInventoryLedsCb exit");
169342cbe538SGunnar Mills                                 };
169442cbe538SGunnar Mills 
1695d500549bSAnthony Wilson                             // Find led connections and get the data
1696d500549bSAnthony Wilson                             getInventoryLeds(sensorsAsyncResp, inventoryItems,
169742cbe538SGunnar Mills                                              std::move(getInventoryLedsCb));
169862598e31SEd Tanous                             BMCWEB_LOG_DEBUG("getInventoryItemsDataCb exit");
1699d500549bSAnthony Wilson                         };
17008fb49dd6SShawn McCarney 
1701adc4f0dbSShawn McCarney                     // Get inventory item data from connections
1702adc4f0dbSShawn McCarney                     getInventoryItemsData(sensorsAsyncResp, inventoryItems,
1703d0090733SEd Tanous                                           invConnections,
1704d500549bSAnthony Wilson                                           std::move(getInventoryItemsDataCb));
170562598e31SEd Tanous                     BMCWEB_LOG_DEBUG("getInventoryItemsConnectionsCb exit");
17068fb49dd6SShawn McCarney                 };
17078fb49dd6SShawn McCarney 
1708adc4f0dbSShawn McCarney             // Get connections that provide inventory item data
1709bd79bce8SPatrick Williams             getInventoryItemsConnections(
1710bd79bce8SPatrick Williams                 sensorsAsyncResp, inventoryItems,
17118fb49dd6SShawn McCarney                 std::move(getInventoryItemsConnectionsCb));
171262598e31SEd Tanous             BMCWEB_LOG_DEBUG("getInventoryItemAssociationsCb exit");
17138fb49dd6SShawn McCarney         };
17148fb49dd6SShawn McCarney 
1715adc4f0dbSShawn McCarney     // Get associations from sensors to inventory items
1716d0090733SEd Tanous     getInventoryItemAssociations(sensorsAsyncResp, sensorNames,
1717adc4f0dbSShawn McCarney                                  std::move(getInventoryItemAssociationsCb));
171862598e31SEd Tanous     BMCWEB_LOG_DEBUG("getInventoryItems exit");
1719adc4f0dbSShawn McCarney }
1720adc4f0dbSShawn McCarney 
1721adc4f0dbSShawn McCarney /**
1722adc4f0dbSShawn McCarney  * @brief Returns JSON PowerSupply object for the specified inventory item.
1723adc4f0dbSShawn McCarney  *
1724adc4f0dbSShawn McCarney  * Searches for a JSON PowerSupply object that matches the specified inventory
1725adc4f0dbSShawn McCarney  * item.  If one is not found, a new PowerSupply object is added to the JSON
1726adc4f0dbSShawn McCarney  * array.
1727adc4f0dbSShawn McCarney  *
1728adc4f0dbSShawn McCarney  * Multiple sensors are often associated with one power supply inventory item.
1729adc4f0dbSShawn McCarney  * As a result, multiple sensor values are stored in one JSON PowerSupply
1730adc4f0dbSShawn McCarney  * object.
1731adc4f0dbSShawn McCarney  *
1732adc4f0dbSShawn McCarney  * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
1733adc4f0dbSShawn McCarney  * @param inventoryItem Inventory item for the power supply.
1734adc4f0dbSShawn McCarney  * @param chassisId Chassis that contains the power supply.
1735adc4f0dbSShawn McCarney  * @return JSON PowerSupply object for the specified inventory item.
1736adc4f0dbSShawn McCarney  */
173723a21a1cSEd Tanous inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
1738adc4f0dbSShawn McCarney                                       const InventoryItem& inventoryItem,
1739adc4f0dbSShawn McCarney                                       const std::string& chassisId)
1740adc4f0dbSShawn McCarney {
174118f8f608SEd Tanous     std::string nameS;
17426f4bd290SAlexander Hansen     nameS.resize(inventoryItem.name.size());
174318f8f608SEd Tanous     std::ranges::replace_copy(inventoryItem.name, nameS.begin(), '_', ' ');
1744adc4f0dbSShawn McCarney     // Check if matching PowerSupply object already exists in JSON array
1745adc4f0dbSShawn McCarney     for (nlohmann::json& powerSupply : powerSupplyArray)
1746adc4f0dbSShawn McCarney     {
174718f8f608SEd Tanous         nlohmann::json::iterator nameIt = powerSupply.find("Name");
174818f8f608SEd Tanous         if (nameIt == powerSupply.end())
174918f8f608SEd Tanous         {
175018f8f608SEd Tanous             continue;
175118f8f608SEd Tanous         }
175218f8f608SEd Tanous         const std::string* name = nameIt->get_ptr<std::string*>();
175318f8f608SEd Tanous         if (name == nullptr)
175418f8f608SEd Tanous         {
175518f8f608SEd Tanous             continue;
175618f8f608SEd Tanous         }
175718f8f608SEd Tanous         if (nameS == *name)
1758adc4f0dbSShawn McCarney         {
1759adc4f0dbSShawn McCarney             return powerSupply;
1760adc4f0dbSShawn McCarney         }
1761adc4f0dbSShawn McCarney     }
1762adc4f0dbSShawn McCarney 
1763adc4f0dbSShawn McCarney     // Add new PowerSupply object to JSON array
1764adc4f0dbSShawn McCarney     powerSupplyArray.push_back({});
1765adc4f0dbSShawn McCarney     nlohmann::json& powerSupply = powerSupplyArray.back();
1766bd79bce8SPatrick Williams     boost::urls::url url =
1767bd79bce8SPatrick Williams         boost::urls::format("/redfish/v1/Chassis/{}/Power", chassisId);
1768eddfc437SWilly Tu     url.set_fragment(("/PowerSupplies"_json_pointer).to_string());
1769eddfc437SWilly Tu     powerSupply["@odata.id"] = std::move(url);
177018f8f608SEd Tanous     std::string escaped;
17716f4bd290SAlexander Hansen     escaped.resize(inventoryItem.name.size());
177218f8f608SEd Tanous     std::ranges::replace_copy(inventoryItem.name, escaped.begin(), '_', ' ');
177318f8f608SEd Tanous     powerSupply["Name"] = std::move(escaped);
1774adc4f0dbSShawn McCarney     powerSupply["Manufacturer"] = inventoryItem.manufacturer;
1775adc4f0dbSShawn McCarney     powerSupply["Model"] = inventoryItem.model;
1776adc4f0dbSShawn McCarney     powerSupply["PartNumber"] = inventoryItem.partNumber;
1777adc4f0dbSShawn McCarney     powerSupply["SerialNumber"] = inventoryItem.serialNumber;
1778*c9563608SJanet Adkins     sensor_utils::setLedState(powerSupply, &inventoryItem);
1779adc4f0dbSShawn McCarney 
178042cbe538SGunnar Mills     if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
178142cbe538SGunnar Mills     {
178242cbe538SGunnar Mills         powerSupply["EfficiencyPercent"] =
178342cbe538SGunnar Mills             inventoryItem.powerSupplyEfficiencyPercent;
178442cbe538SGunnar Mills     }
178542cbe538SGunnar Mills 
1786*c9563608SJanet Adkins     powerSupply["Status"]["State"] =
1787*c9563608SJanet Adkins         sensor_utils::getState(&inventoryItem, true);
1788adc4f0dbSShawn McCarney     const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
1789adc4f0dbSShawn McCarney     powerSupply["Status"]["Health"] = health;
1790adc4f0dbSShawn McCarney 
1791adc4f0dbSShawn McCarney     return powerSupply;
17928fb49dd6SShawn McCarney }
17938fb49dd6SShawn McCarney 
17948fb49dd6SShawn McCarney /**
1795de629b6eSShawn McCarney  * @brief Gets the values of the specified sensors.
1796de629b6eSShawn McCarney  *
1797de629b6eSShawn McCarney  * Stores the results as JSON in the SensorsAsyncResp.
1798de629b6eSShawn McCarney  *
1799de629b6eSShawn McCarney  * Gets the sensor values asynchronously.  Stores the results later when the
1800de629b6eSShawn McCarney  * information has been obtained.
1801de629b6eSShawn McCarney  *
1802adc4f0dbSShawn McCarney  * The sensorNames set contains all requested sensors for the current chassis.
1803de629b6eSShawn McCarney  *
1804de629b6eSShawn McCarney  * To minimize the number of DBus calls, the DBus method
1805de629b6eSShawn McCarney  * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
1806de629b6eSShawn McCarney  * values of all sensors provided by a connection (service).
1807de629b6eSShawn McCarney  *
1808de629b6eSShawn McCarney  * The connections set contains all the connections that provide sensor values.
1809de629b6eSShawn McCarney  *
1810adc4f0dbSShawn McCarney  * The InventoryItem vector contains D-Bus inventory items associated with the
1811adc4f0dbSShawn McCarney  * sensors.  Inventory item data is needed for some Redfish sensor properties.
1812adc4f0dbSShawn McCarney  *
1813de629b6eSShawn McCarney  * @param SensorsAsyncResp Pointer to object holding response data.
1814adc4f0dbSShawn McCarney  * @param sensorNames All requested sensors within the current chassis.
1815de629b6eSShawn McCarney  * @param connections Connections that provide sensor values.
1816de629b6eSShawn McCarney  * implements ObjectManager.
1817adc4f0dbSShawn McCarney  * @param inventoryItems Inventory items associated with the sensors.
1818de629b6eSShawn McCarney  */
181923a21a1cSEd Tanous inline void getSensorData(
182081ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1821fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& sensorNames,
1822fe04d49cSNan Zhou     const std::set<std::string>& connections,
1823b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems)
1824de629b6eSShawn McCarney {
182562598e31SEd Tanous     BMCWEB_LOG_DEBUG("getSensorData enter");
1826de629b6eSShawn McCarney     // Get managed objects from all services exposing sensors
1827de629b6eSShawn McCarney     for (const std::string& connection : connections)
1828de629b6eSShawn McCarney     {
18295eb468daSGeorge Liu         sdbusplus::message::object_path sensorPath(
18305eb468daSGeorge Liu             "/xyz/openbmc_project/sensors");
18315eb468daSGeorge Liu         dbus::utility::getManagedObjects(
18325eb468daSGeorge Liu             connection, sensorPath,
1833002d39b4SEd Tanous             [sensorsAsyncResp, sensorNames,
18345e7e2dc5SEd Tanous              inventoryItems](const boost::system::error_code& ec,
183502cad96eSEd Tanous                              const dbus::utility::ManagedObjectType& resp) {
183662598e31SEd Tanous                 BMCWEB_LOG_DEBUG("getManagedObjectsCb enter");
1837de629b6eSShawn McCarney                 if (ec)
1838de629b6eSShawn McCarney                 {
183962598e31SEd Tanous                     BMCWEB_LOG_ERROR("getManagedObjectsCb DBUS error: {}", ec);
18408d1b46d7Szhanghch05                     messages::internalError(sensorsAsyncResp->asyncResp->res);
1841de629b6eSShawn McCarney                     return;
1842de629b6eSShawn McCarney                 }
1843de629b6eSShawn McCarney                 // Go through all objects and update response with sensor data
1844de629b6eSShawn McCarney                 for (const auto& objDictEntry : resp)
1845de629b6eSShawn McCarney                 {
1846de629b6eSShawn McCarney                     const std::string& objPath =
1847de629b6eSShawn McCarney                         static_cast<const std::string&>(objDictEntry.first);
184862598e31SEd Tanous                     BMCWEB_LOG_DEBUG("getManagedObjectsCb parsing object {}",
184962598e31SEd Tanous                                      objPath);
1850de629b6eSShawn McCarney 
1851de629b6eSShawn McCarney                     std::vector<std::string> split;
1852de629b6eSShawn McCarney                     // Reserve space for
1853de629b6eSShawn McCarney                     // /xyz/openbmc_project/sensors/<name>/<subname>
1854de629b6eSShawn McCarney                     split.reserve(6);
185550ebd4afSEd Tanous                     // NOLINTNEXTLINE
185650ebd4afSEd Tanous                     bmcweb::split(split, objPath, '/');
1857de629b6eSShawn McCarney                     if (split.size() < 6)
1858de629b6eSShawn McCarney                     {
185962598e31SEd Tanous                         BMCWEB_LOG_ERROR("Got path that isn't long enough {}",
186062598e31SEd Tanous                                          objPath);
1861de629b6eSShawn McCarney                         continue;
1862de629b6eSShawn McCarney                     }
186350ebd4afSEd Tanous                     // These indexes aren't intuitive, as split puts an empty
1864de629b6eSShawn McCarney                     // string at the beginning
1865de629b6eSShawn McCarney                     const std::string& sensorType = split[4];
1866de629b6eSShawn McCarney                     const std::string& sensorName = split[5];
186762598e31SEd Tanous                     BMCWEB_LOG_DEBUG("sensorName {} sensorType {}", sensorName,
186862598e31SEd Tanous                                      sensorType);
186949c53ac9SJohnathan Mantey                     if (sensorNames->find(objPath) == sensorNames->end())
1870de629b6eSShawn McCarney                     {
187162598e31SEd Tanous                         BMCWEB_LOG_DEBUG("{} not in sensor list ", sensorName);
1872de629b6eSShawn McCarney                         continue;
1873de629b6eSShawn McCarney                     }
1874de629b6eSShawn McCarney 
1875adc4f0dbSShawn McCarney                     // Find inventory item (if any) associated with sensor
1876adc4f0dbSShawn McCarney                     InventoryItem* inventoryItem =
1877adc4f0dbSShawn McCarney                         findInventoryItemForSensor(inventoryItems, objPath);
1878adc4f0dbSShawn McCarney 
187995a3ecadSAnthony Wilson                     const std::string& sensorSchema =
188081ce609eSEd Tanous                         sensorsAsyncResp->chassisSubNode;
188195a3ecadSAnthony Wilson 
188295a3ecadSAnthony Wilson                     nlohmann::json* sensorJson = nullptr;
188395a3ecadSAnthony Wilson 
1884*c9563608SJanet Adkins                     if (sensorSchema == sensor_utils::sensorsNode &&
1885928fefb9SNan Zhou                         !sensorsAsyncResp->efficientExpand)
188695a3ecadSAnthony Wilson                     {
18871516c21bSJanet Adkins                         std::string sensorId =
18881516c21bSJanet Adkins                             redfish::sensor_utils::getSensorId(sensorName,
18891516c21bSJanet Adkins                                                                sensorType);
1890c1d019a6SEd Tanous 
1891bd79bce8SPatrick Williams                         sensorsAsyncResp->asyncResp->res
1892bd79bce8SPatrick Williams                             .jsonValue["@odata.id"] = boost::urls::format(
1893bd79bce8SPatrick Williams                             "/redfish/v1/Chassis/{}/{}/{}",
1894c1d019a6SEd Tanous                             sensorsAsyncResp->chassisId,
1895bd79bce8SPatrick Williams                             sensorsAsyncResp->chassisSubNode, sensorId);
1896bd79bce8SPatrick Williams                         sensorJson =
1897bd79bce8SPatrick Williams                             &(sensorsAsyncResp->asyncResp->res.jsonValue);
189895a3ecadSAnthony Wilson                     }
189995a3ecadSAnthony Wilson                     else
190095a3ecadSAnthony Wilson                     {
1901271584abSEd Tanous                         std::string fieldName;
1902928fefb9SNan Zhou                         if (sensorsAsyncResp->efficientExpand)
1903928fefb9SNan Zhou                         {
1904928fefb9SNan Zhou                             fieldName = "Members";
1905928fefb9SNan Zhou                         }
1906928fefb9SNan Zhou                         else if (sensorType == "temperature")
1907de629b6eSShawn McCarney                         {
1908de629b6eSShawn McCarney                             fieldName = "Temperatures";
1909de629b6eSShawn McCarney                         }
1910bd79bce8SPatrick Williams                         else if (sensorType == "fan" ||
1911bd79bce8SPatrick Williams                                  sensorType == "fan_tach" ||
1912de629b6eSShawn McCarney                                  sensorType == "fan_pwm")
1913de629b6eSShawn McCarney                         {
1914de629b6eSShawn McCarney                             fieldName = "Fans";
1915de629b6eSShawn McCarney                         }
1916de629b6eSShawn McCarney                         else if (sensorType == "voltage")
1917de629b6eSShawn McCarney                         {
1918de629b6eSShawn McCarney                             fieldName = "Voltages";
1919de629b6eSShawn McCarney                         }
1920de629b6eSShawn McCarney                         else if (sensorType == "power")
1921de629b6eSShawn McCarney                         {
192255f79e6fSEd Tanous                             if (sensorName == "total_power")
1923028f7ebcSEddie James                             {
1924028f7ebcSEddie James                                 fieldName = "PowerControl";
1925028f7ebcSEddie James                             }
1926adc4f0dbSShawn McCarney                             else if ((inventoryItem != nullptr) &&
1927adc4f0dbSShawn McCarney                                      (inventoryItem->isPowerSupply))
1928028f7ebcSEddie James                             {
1929de629b6eSShawn McCarney                                 fieldName = "PowerSupplies";
1930de629b6eSShawn McCarney                             }
1931adc4f0dbSShawn McCarney                             else
1932adc4f0dbSShawn McCarney                             {
1933adc4f0dbSShawn McCarney                                 // Other power sensors are in SensorCollection
1934adc4f0dbSShawn McCarney                                 continue;
1935adc4f0dbSShawn McCarney                             }
1936028f7ebcSEddie James                         }
1937de629b6eSShawn McCarney                         else
1938de629b6eSShawn McCarney                         {
1939bd79bce8SPatrick Williams                             BMCWEB_LOG_ERROR(
1940bd79bce8SPatrick Williams                                 "Unsure how to handle sensorType {}",
194162598e31SEd Tanous                                 sensorType);
1942de629b6eSShawn McCarney                             continue;
1943de629b6eSShawn McCarney                         }
1944de629b6eSShawn McCarney 
1945de629b6eSShawn McCarney                         nlohmann::json& tempArray =
1946bd79bce8SPatrick Williams                             sensorsAsyncResp->asyncResp->res
1947bd79bce8SPatrick Williams                                 .jsonValue[fieldName];
1948adc4f0dbSShawn McCarney                         if (fieldName == "PowerControl")
194949c53ac9SJohnathan Mantey                         {
1950adc4f0dbSShawn McCarney                             if (tempArray.empty())
19517ab06f49SGunnar Mills                             {
195295a3ecadSAnthony Wilson                                 // Put multiple "sensors" into a single
195395a3ecadSAnthony Wilson                                 // PowerControl. Follows MemberId naming and
195495a3ecadSAnthony Wilson                                 // naming in power.hpp.
19551476687dSEd Tanous                                 nlohmann::json::object_t power;
1956ef4c65b7SEd Tanous                                 boost::urls::url url = boost::urls::format(
1957ef4c65b7SEd Tanous                                     "/redfish/v1/Chassis/{}/{}",
1958eddfc437SWilly Tu                                     sensorsAsyncResp->chassisId,
1959eddfc437SWilly Tu                                     sensorsAsyncResp->chassisSubNode);
1960bd79bce8SPatrick Williams                                 url.set_fragment(
1961bd79bce8SPatrick Williams                                     (""_json_pointer / fieldName / "0")
1962eddfc437SWilly Tu                                         .to_string());
1963eddfc437SWilly Tu                                 power["@odata.id"] = std::move(url);
1964b2ba3072SPatrick Williams                                 tempArray.emplace_back(std::move(power));
1965adc4f0dbSShawn McCarney                             }
1966adc4f0dbSShawn McCarney                             sensorJson = &(tempArray.back());
1967adc4f0dbSShawn McCarney                         }
1968adc4f0dbSShawn McCarney                         else if (fieldName == "PowerSupplies")
1969adc4f0dbSShawn McCarney                         {
1970adc4f0dbSShawn McCarney                             if (inventoryItem != nullptr)
1971adc4f0dbSShawn McCarney                             {
1972bd79bce8SPatrick Williams                                 sensorJson = &(getPowerSupply(
1973bd79bce8SPatrick Williams                                     tempArray, *inventoryItem,
197481ce609eSEd Tanous                                     sensorsAsyncResp->chassisId));
1975adc4f0dbSShawn McCarney                             }
197649c53ac9SJohnathan Mantey                         }
1977928fefb9SNan Zhou                         else if (fieldName == "Members")
1978928fefb9SNan Zhou                         {
19791516c21bSJanet Adkins                             std::string sensorId =
19801516c21bSJanet Adkins                                 redfish::sensor_utils::getSensorId(sensorName,
19811516c21bSJanet Adkins                                                                    sensorType);
1982677bb756SEd Tanous 
19831476687dSEd Tanous                             nlohmann::json::object_t member;
1984ef4c65b7SEd Tanous                             member["@odata.id"] = boost::urls::format(
1985ef4c65b7SEd Tanous                                 "/redfish/v1/Chassis/{}/{}/{}",
1986677bb756SEd Tanous                                 sensorsAsyncResp->chassisId,
1987677bb756SEd Tanous                                 sensorsAsyncResp->chassisSubNode, sensorId);
1988b2ba3072SPatrick Williams                             tempArray.emplace_back(std::move(member));
1989928fefb9SNan Zhou                             sensorJson = &(tempArray.back());
1990928fefb9SNan Zhou                         }
199149c53ac9SJohnathan Mantey                         else
199249c53ac9SJohnathan Mantey                         {
19931476687dSEd Tanous                             nlohmann::json::object_t member;
1994ef4c65b7SEd Tanous                             boost::urls::url url = boost::urls::format(
1995ef4c65b7SEd Tanous                                 "/redfish/v1/Chassis/{}/{}",
1996eddfc437SWilly Tu                                 sensorsAsyncResp->chassisId,
1997eddfc437SWilly Tu                                 sensorsAsyncResp->chassisSubNode);
1998eddfc437SWilly Tu                             url.set_fragment(
1999eddfc437SWilly Tu                                 (""_json_pointer / fieldName).to_string());
2000eddfc437SWilly Tu                             member["@odata.id"] = std::move(url);
2001b2ba3072SPatrick Williams                             tempArray.emplace_back(std::move(member));
2002adc4f0dbSShawn McCarney                             sensorJson = &(tempArray.back());
200349c53ac9SJohnathan Mantey                         }
200495a3ecadSAnthony Wilson                     }
2005de629b6eSShawn McCarney 
2006adc4f0dbSShawn McCarney                     if (sensorJson != nullptr)
2007adc4f0dbSShawn McCarney                     {
20081d7c0054SEd Tanous                         objectInterfacesToJson(sensorName, sensorType,
20091d7c0054SEd Tanous                                                sensorsAsyncResp->chassisSubNode,
20101d7c0054SEd Tanous                                                objDictEntry.second, *sensorJson,
20111d7c0054SEd Tanous                                                inventoryItem);
20121d7c0054SEd Tanous 
20131d7c0054SEd Tanous                         std::string path = "/xyz/openbmc_project/sensors/";
20141d7c0054SEd Tanous                         path += sensorType;
20151d7c0054SEd Tanous                         path += "/";
20161d7c0054SEd Tanous                         path += sensorName;
2017c1d019a6SEd Tanous                         sensorsAsyncResp->addMetadata(*sensorJson, path);
2018adc4f0dbSShawn McCarney                     }
2019de629b6eSShawn McCarney                 }
202081ce609eSEd Tanous                 if (sensorsAsyncResp.use_count() == 1)
202149c53ac9SJohnathan Mantey                 {
202281ce609eSEd Tanous                     sortJSONResponse(sensorsAsyncResp);
2023928fefb9SNan Zhou                     if (sensorsAsyncResp->chassisSubNode ==
2024*c9563608SJanet Adkins                             sensor_utils::sensorsNode &&
2025928fefb9SNan Zhou                         sensorsAsyncResp->efficientExpand)
2026928fefb9SNan Zhou                     {
2027928fefb9SNan Zhou                         sensorsAsyncResp->asyncResp->res
2028928fefb9SNan Zhou                             .jsonValue["Members@odata.count"] =
2029bd79bce8SPatrick Williams                             sensorsAsyncResp->asyncResp->res
2030bd79bce8SPatrick Williams                                 .jsonValue["Members"]
2031928fefb9SNan Zhou                                 .size();
2032928fefb9SNan Zhou                     }
2033928fefb9SNan Zhou                     else if (sensorsAsyncResp->chassisSubNode ==
2034*c9563608SJanet Adkins                              sensor_utils::thermalNode)
20358bd25ccdSJames Feist                     {
203681ce609eSEd Tanous                         populateFanRedundancy(sensorsAsyncResp);
20378bd25ccdSJames Feist                     }
203849c53ac9SJohnathan Mantey                 }
203962598e31SEd Tanous                 BMCWEB_LOG_DEBUG("getManagedObjectsCb exit");
20405eb468daSGeorge Liu             });
204123a21a1cSEd Tanous     }
204262598e31SEd Tanous     BMCWEB_LOG_DEBUG("getSensorData exit");
2043de629b6eSShawn McCarney }
2044de629b6eSShawn McCarney 
2045fe04d49cSNan Zhou inline void
2046fe04d49cSNan Zhou     processSensorList(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2047fe04d49cSNan Zhou                       const std::shared_ptr<std::set<std::string>>& sensorNames)
20481abe55efSEd Tanous {
2049fe04d49cSNan Zhou     auto getConnectionCb = [sensorsAsyncResp, sensorNames](
2050fe04d49cSNan Zhou                                const std::set<std::string>& connections) {
205162598e31SEd Tanous         BMCWEB_LOG_DEBUG("getConnectionCb enter");
2052adc4f0dbSShawn McCarney         auto getInventoryItemsCb =
2053bd79bce8SPatrick Williams             [sensorsAsyncResp, sensorNames, connections](
2054bd79bce8SPatrick Williams                 const std::shared_ptr<std::vector<InventoryItem>>&
2055adc4f0dbSShawn McCarney                     inventoryItems) {
205662598e31SEd Tanous                 BMCWEB_LOG_DEBUG("getInventoryItemsCb enter");
205749c53ac9SJohnathan Mantey                 // Get sensor data and store results in JSON
2058002d39b4SEd Tanous                 getSensorData(sensorsAsyncResp, sensorNames, connections,
2059d0090733SEd Tanous                               inventoryItems);
206062598e31SEd Tanous                 BMCWEB_LOG_DEBUG("getInventoryItemsCb exit");
2061adc4f0dbSShawn McCarney             };
2062adc4f0dbSShawn McCarney 
2063adc4f0dbSShawn McCarney         // Get inventory items associated with sensors
2064d0090733SEd Tanous         getInventoryItems(sensorsAsyncResp, sensorNames,
2065adc4f0dbSShawn McCarney                           std::move(getInventoryItemsCb));
2066adc4f0dbSShawn McCarney 
206762598e31SEd Tanous         BMCWEB_LOG_DEBUG("getConnectionCb exit");
206808777fb0SLewanczyk, Dawid     };
2069de629b6eSShawn McCarney 
2070de629b6eSShawn McCarney     // Get set of connections that provide sensor values
207181ce609eSEd Tanous     getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb));
207295a3ecadSAnthony Wilson }
207395a3ecadSAnthony Wilson 
207495a3ecadSAnthony Wilson /**
207595a3ecadSAnthony Wilson  * @brief Entry point for retrieving sensors data related to requested
207695a3ecadSAnthony Wilson  *        chassis.
207795a3ecadSAnthony Wilson  * @param SensorsAsyncResp   Pointer to object holding response data
207895a3ecadSAnthony Wilson  */
2079b5a76932SEd Tanous inline void
208081ce609eSEd Tanous     getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
208195a3ecadSAnthony Wilson {
208262598e31SEd Tanous     BMCWEB_LOG_DEBUG("getChassisData enter");
208395a3ecadSAnthony Wilson     auto getChassisCb =
208481ce609eSEd Tanous         [sensorsAsyncResp](
2085fe04d49cSNan Zhou             const std::shared_ptr<std::set<std::string>>& sensorNames) {
208662598e31SEd Tanous             BMCWEB_LOG_DEBUG("getChassisCb enter");
208781ce609eSEd Tanous             processSensorList(sensorsAsyncResp, sensorNames);
208862598e31SEd Tanous             BMCWEB_LOG_DEBUG("getChassisCb exit");
208908777fb0SLewanczyk, Dawid         };
2090928fefb9SNan Zhou     // SensorCollection doesn't contain the Redundancy property
2091*c9563608SJanet Adkins     if (sensorsAsyncResp->chassisSubNode != sensor_utils::sensorsNode)
2092928fefb9SNan Zhou     {
20938d1b46d7Szhanghch05         sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] =
20948d1b46d7Szhanghch05             nlohmann::json::array();
2095928fefb9SNan Zhou     }
209626f03899SShawn McCarney     // Get set of sensors in chassis
20977f1cc26dSEd Tanous     getChassis(sensorsAsyncResp->asyncResp, sensorsAsyncResp->chassisId,
20987f1cc26dSEd Tanous                sensorsAsyncResp->chassisSubNode, sensorsAsyncResp->types,
20997f1cc26dSEd Tanous                std::move(getChassisCb));
210062598e31SEd Tanous     BMCWEB_LOG_DEBUG("getChassisData exit");
2101271584abSEd Tanous }
210208777fb0SLewanczyk, Dawid 
2103413961deSRichard Marian Thomaiyar /**
210449c53ac9SJohnathan Mantey  * @brief Find the requested sensorName in the list of all sensors supplied by
210549c53ac9SJohnathan Mantey  * the chassis node
210649c53ac9SJohnathan Mantey  *
210749c53ac9SJohnathan Mantey  * @param sensorName   The sensor name supplied in the PATCH request
210849c53ac9SJohnathan Mantey  * @param sensorsList  The list of sensors managed by the chassis node
210949c53ac9SJohnathan Mantey  * @param sensorsModified  The list of sensors that were found as a result of
211049c53ac9SJohnathan Mantey  *                         repeated calls to this function
211149c53ac9SJohnathan Mantey  */
2112bd79bce8SPatrick Williams inline bool findSensorNameUsingSensorPath(
2113bd79bce8SPatrick Williams     std::string_view sensorName, const std::set<std::string>& sensorsList,
2114fe04d49cSNan Zhou     std::set<std::string>& sensorsModified)
211549c53ac9SJohnathan Mantey {
2116fe04d49cSNan Zhou     for (const auto& chassisSensor : sensorsList)
211749c53ac9SJohnathan Mantey     {
211828aa8de5SGeorge Liu         sdbusplus::message::object_path path(chassisSensor);
2119b00dcc27SEd Tanous         std::string thisSensorName = path.filename();
212028aa8de5SGeorge Liu         if (thisSensorName.empty())
212149c53ac9SJohnathan Mantey         {
212249c53ac9SJohnathan Mantey             continue;
212349c53ac9SJohnathan Mantey         }
212449c53ac9SJohnathan Mantey         if (thisSensorName == sensorName)
212549c53ac9SJohnathan Mantey         {
212649c53ac9SJohnathan Mantey             sensorsModified.emplace(chassisSensor);
212749c53ac9SJohnathan Mantey             return true;
212849c53ac9SJohnathan Mantey         }
212949c53ac9SJohnathan Mantey     }
213049c53ac9SJohnathan Mantey     return false;
213149c53ac9SJohnathan Mantey }
213249c53ac9SJohnathan Mantey 
213349c53ac9SJohnathan Mantey /**
2134413961deSRichard Marian Thomaiyar  * @brief Entry point for overriding sensor values of given sensor
2135413961deSRichard Marian Thomaiyar  *
21368d1b46d7Szhanghch05  * @param sensorAsyncResp   response object
21374bb3dc34SCarol Wang  * @param allCollections   Collections extract from sensors' request patch info
2138413961deSRichard Marian Thomaiyar  * @param chassisSubNode   Chassis Node for which the query has to happen
2139413961deSRichard Marian Thomaiyar  */
214023a21a1cSEd Tanous inline void setSensorsOverride(
2141b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
21420885057cSEd Tanous     std::unordered_map<std::string, std::vector<nlohmann::json::object_t>>&
2143397fd61fSjayaprakash Mutyala         allCollections)
2144413961deSRichard Marian Thomaiyar {
214562598e31SEd Tanous     BMCWEB_LOG_INFO("setSensorsOverride for subNode{}",
214662598e31SEd Tanous                     sensorAsyncResp->chassisSubNode);
2147413961deSRichard Marian Thomaiyar 
2148d02aad39SEd Tanous     std::string_view propertyValueName;
2149f65af9e8SRichard Marian Thomaiyar     std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
2150413961deSRichard Marian Thomaiyar     std::string memberId;
2151543f4400SEd Tanous     double value = 0.0;
2152f65af9e8SRichard Marian Thomaiyar     for (auto& collectionItems : allCollections)
2153f65af9e8SRichard Marian Thomaiyar     {
2154f65af9e8SRichard Marian Thomaiyar         if (collectionItems.first == "Temperatures")
2155f65af9e8SRichard Marian Thomaiyar         {
2156f65af9e8SRichard Marian Thomaiyar             propertyValueName = "ReadingCelsius";
2157f65af9e8SRichard Marian Thomaiyar         }
2158f65af9e8SRichard Marian Thomaiyar         else if (collectionItems.first == "Fans")
2159f65af9e8SRichard Marian Thomaiyar         {
2160f65af9e8SRichard Marian Thomaiyar             propertyValueName = "Reading";
2161f65af9e8SRichard Marian Thomaiyar         }
2162f65af9e8SRichard Marian Thomaiyar         else
2163f65af9e8SRichard Marian Thomaiyar         {
2164f65af9e8SRichard Marian Thomaiyar             propertyValueName = "ReadingVolts";
2165f65af9e8SRichard Marian Thomaiyar         }
2166f65af9e8SRichard Marian Thomaiyar         for (auto& item : collectionItems.second)
2167f65af9e8SRichard Marian Thomaiyar         {
21680885057cSEd Tanous             if (!json_util::readJsonObject(
21690885057cSEd Tanous                     item, sensorAsyncResp->asyncResp->res, "MemberId", memberId,
21700885057cSEd Tanous                     propertyValueName, value))
2171413961deSRichard Marian Thomaiyar             {
2172413961deSRichard Marian Thomaiyar                 return;
2173413961deSRichard Marian Thomaiyar             }
2174f65af9e8SRichard Marian Thomaiyar             overrideMap.emplace(memberId,
2175f65af9e8SRichard Marian Thomaiyar                                 std::make_pair(value, collectionItems.first));
2176f65af9e8SRichard Marian Thomaiyar         }
2177f65af9e8SRichard Marian Thomaiyar     }
21784bb3dc34SCarol Wang 
2179bd79bce8SPatrick Williams     auto getChassisSensorListCb = [sensorAsyncResp, overrideMap,
2180bd79bce8SPatrick Williams                                    propertyValueNameStr =
2181bd79bce8SPatrick Williams                                        std::string(propertyValueName)](
2182bd79bce8SPatrick Williams                                       const std::shared_ptr<
2183bd79bce8SPatrick Williams                                           std::set<std::string>>& sensorsList) {
218449c53ac9SJohnathan Mantey         // Match sensor names in the PATCH request to those managed by the
218549c53ac9SJohnathan Mantey         // chassis node
2186fe04d49cSNan Zhou         const std::shared_ptr<std::set<std::string>> sensorNames =
2187fe04d49cSNan Zhou             std::make_shared<std::set<std::string>>();
2188f65af9e8SRichard Marian Thomaiyar         for (const auto& item : overrideMap)
2189413961deSRichard Marian Thomaiyar         {
2190f65af9e8SRichard Marian Thomaiyar             const auto& sensor = item.first;
2191c71d6125SEd Tanous             std::pair<std::string, std::string> sensorNameType =
21921516c21bSJanet Adkins                 redfish::sensor_utils::splitSensorNameAndType(sensor);
2193c71d6125SEd Tanous             if (!findSensorNameUsingSensorPath(sensorNameType.second,
2194c71d6125SEd Tanous                                                *sensorsList, *sensorNames))
2195f65af9e8SRichard Marian Thomaiyar             {
219662598e31SEd Tanous                 BMCWEB_LOG_INFO("Unable to find memberId {}", item.first);
21978d1b46d7Szhanghch05                 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2198f65af9e8SRichard Marian Thomaiyar                                            item.second.second, item.first);
2199413961deSRichard Marian Thomaiyar                 return;
2200413961deSRichard Marian Thomaiyar             }
2201f65af9e8SRichard Marian Thomaiyar         }
2202413961deSRichard Marian Thomaiyar         // Get the connection to which the memberId belongs
2203bd79bce8SPatrick Williams         auto getObjectsWithConnectionCb = [sensorAsyncResp, overrideMap,
2204bd79bce8SPatrick Williams                                            propertyValueNameStr](
2205bd79bce8SPatrick Williams                                               const std::set<
2206bd79bce8SPatrick Williams                                                   std::string>& /*connections*/,
2207bd79bce8SPatrick Williams                                               const std::set<std::pair<
2208bd79bce8SPatrick Williams                                                   std::string, std::string>>&
2209413961deSRichard Marian Thomaiyar                                                   objectsWithConnection) {
2210f65af9e8SRichard Marian Thomaiyar             if (objectsWithConnection.size() != overrideMap.size())
2211413961deSRichard Marian Thomaiyar             {
221262598e31SEd Tanous                 BMCWEB_LOG_INFO(
221362598e31SEd Tanous                     "Unable to find all objects with proper connection {} requested {}",
221462598e31SEd Tanous                     objectsWithConnection.size(), overrideMap.size());
2215bd79bce8SPatrick Williams                 messages::resourceNotFound(
2216bd79bce8SPatrick Williams                     sensorAsyncResp->asyncResp->res,
2217*c9563608SJanet Adkins                     sensorAsyncResp->chassisSubNode == sensor_utils::thermalNode
2218413961deSRichard Marian Thomaiyar                         ? "Temperatures"
2219413961deSRichard Marian Thomaiyar                         : "Voltages",
2220f65af9e8SRichard Marian Thomaiyar                     "Count");
2221f65af9e8SRichard Marian Thomaiyar                 return;
2222f65af9e8SRichard Marian Thomaiyar             }
2223f65af9e8SRichard Marian Thomaiyar             for (const auto& item : objectsWithConnection)
2224f65af9e8SRichard Marian Thomaiyar             {
222528aa8de5SGeorge Liu                 sdbusplus::message::object_path path(item.first);
222628aa8de5SGeorge Liu                 std::string sensorName = path.filename();
222728aa8de5SGeorge Liu                 if (sensorName.empty())
2228f65af9e8SRichard Marian Thomaiyar                 {
22294f277b54SJayaprakash Mutyala                     messages::internalError(sensorAsyncResp->asyncResp->res);
2230f65af9e8SRichard Marian Thomaiyar                     return;
2231f65af9e8SRichard Marian Thomaiyar                 }
22321516c21bSJanet Adkins                 std::string id = redfish::sensor_utils::getSensorId(
22331516c21bSJanet Adkins                     sensorName, path.parent_path().filename());
2234f65af9e8SRichard Marian Thomaiyar 
22353f5eb755SBan Feng                 const auto& iterator = overrideMap.find(id);
2236f65af9e8SRichard Marian Thomaiyar                 if (iterator == overrideMap.end())
2237f65af9e8SRichard Marian Thomaiyar                 {
223862598e31SEd Tanous                     BMCWEB_LOG_INFO("Unable to find sensor object{}",
223962598e31SEd Tanous                                     item.first);
22404f277b54SJayaprakash Mutyala                     messages::internalError(sensorAsyncResp->asyncResp->res);
2241413961deSRichard Marian Thomaiyar                     return;
2242413961deSRichard Marian Thomaiyar                 }
2243e93abac6SGinu George                 setDbusProperty(sensorAsyncResp->asyncResp,
2244e93abac6SGinu George                                 propertyValueNameStr, item.second, item.first,
2245e93abac6SGinu George                                 "xyz.openbmc_project.Sensor.Value", "Value",
2246d02aad39SEd Tanous                                 iterator->second.first);
2247f65af9e8SRichard Marian Thomaiyar             }
2248413961deSRichard Marian Thomaiyar         };
2249413961deSRichard Marian Thomaiyar         // Get object with connection for the given sensor name
2250413961deSRichard Marian Thomaiyar         getObjectsWithConnection(sensorAsyncResp, sensorNames,
2251413961deSRichard Marian Thomaiyar                                  std::move(getObjectsWithConnectionCb));
2252413961deSRichard Marian Thomaiyar     };
2253413961deSRichard Marian Thomaiyar     // get full sensor list for the given chassisId and cross verify the sensor.
22547f1cc26dSEd Tanous     getChassis(sensorAsyncResp->asyncResp, sensorAsyncResp->chassisId,
22557f1cc26dSEd Tanous                sensorAsyncResp->chassisSubNode, sensorAsyncResp->types,
22567f1cc26dSEd Tanous                std::move(getChassisSensorListCb));
2257413961deSRichard Marian Thomaiyar }
2258413961deSRichard Marian Thomaiyar 
2259a0ec28b6SAdrian Ambrożewicz /**
2260a0ec28b6SAdrian Ambrożewicz  * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
2261a0ec28b6SAdrian Ambrożewicz  * path of the sensor.
2262a0ec28b6SAdrian Ambrożewicz  *
2263a0ec28b6SAdrian Ambrożewicz  * Function builds valid Redfish response for sensor query of given chassis and
2264a0ec28b6SAdrian Ambrożewicz  * node. It then builds metadata about Redfish<->D-Bus correlations and provides
2265a0ec28b6SAdrian Ambrożewicz  * it to caller in a callback.
2266a0ec28b6SAdrian Ambrożewicz  *
2267a0ec28b6SAdrian Ambrożewicz  * @param chassis   Chassis for which retrieval should be performed
2268*c9563608SJanet Adkins  * @param node  Node (group) of sensors. See sensor_utils::node for supported
2269*c9563608SJanet Adkins  * values
2270a0ec28b6SAdrian Ambrożewicz  * @param mapComplete   Callback to be called with retrieval result
2271a0ec28b6SAdrian Ambrożewicz  */
2272931edc79SEd Tanous template <typename Callback>
2273bd79bce8SPatrick Williams inline void retrieveUriToDbusMap(
2274bd79bce8SPatrick Williams     const std::string& chassis, const std::string& node, Callback&& mapComplete)
2275a0ec28b6SAdrian Ambrożewicz {
227602da7c5aSEd Tanous     decltype(sensors::paths)::const_iterator pathIt =
227702da7c5aSEd Tanous         std::find_if(sensors::paths.cbegin(), sensors::paths.cend(),
227802da7c5aSEd Tanous                      [&node](auto&& val) { return val.first == node; });
227902da7c5aSEd Tanous     if (pathIt == sensors::paths.cend())
2280a0ec28b6SAdrian Ambrożewicz     {
228162598e31SEd Tanous         BMCWEB_LOG_ERROR("Wrong node provided : {}", node);
22826804b5c8SEd Tanous         std::map<std::string, std::string> noop;
22836804b5c8SEd Tanous         mapComplete(boost::beast::http::status::bad_request, noop);
2284a0ec28b6SAdrian Ambrożewicz         return;
2285a0ec28b6SAdrian Ambrożewicz     }
2286d51e072fSKrzysztof Grobelny 
228772374eb7SNan Zhou     auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
2288bd79bce8SPatrick Williams     auto callback =
2289bd79bce8SPatrick Williams         [asyncResp, mapCompleteCb = std::forward<Callback>(mapComplete)](
2290a0ec28b6SAdrian Ambrożewicz             const boost::beast::http::status status,
2291fe04d49cSNan Zhou             const std::map<std::string, std::string>& uriToDbus) {
2292fe04d49cSNan Zhou             mapCompleteCb(status, uriToDbus);
2293fe04d49cSNan Zhou         };
2294a0ec28b6SAdrian Ambrożewicz 
2295a0ec28b6SAdrian Ambrożewicz     auto resp = std::make_shared<SensorsAsyncResp>(
2296d51e072fSKrzysztof Grobelny         asyncResp, chassis, pathIt->second, node, std::move(callback));
2297a0ec28b6SAdrian Ambrożewicz     getChassisData(resp);
2298a0ec28b6SAdrian Ambrożewicz }
2299a0ec28b6SAdrian Ambrożewicz 
2300bacb2162SNan Zhou namespace sensors
2301bacb2162SNan Zhou {
2302928fefb9SNan Zhou 
2303bacb2162SNan Zhou inline void getChassisCallback(
2304c1d019a6SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2305c1d019a6SEd Tanous     std::string_view chassisId, std::string_view chassisSubNode,
2306fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& sensorNames)
2307bacb2162SNan Zhou {
230862598e31SEd Tanous     BMCWEB_LOG_DEBUG("getChassisCallback enter ");
2309bacb2162SNan Zhou 
2310c1d019a6SEd Tanous     nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
2311c1d019a6SEd Tanous     for (const std::string& sensor : *sensorNames)
2312bacb2162SNan Zhou     {
231362598e31SEd Tanous         BMCWEB_LOG_DEBUG("Adding sensor: {}", sensor);
2314bacb2162SNan Zhou 
2315bacb2162SNan Zhou         sdbusplus::message::object_path path(sensor);
2316bacb2162SNan Zhou         std::string sensorName = path.filename();
2317bacb2162SNan Zhou         if (sensorName.empty())
2318bacb2162SNan Zhou         {
231962598e31SEd Tanous             BMCWEB_LOG_ERROR("Invalid sensor path: {}", sensor);
2320c1d019a6SEd Tanous             messages::internalError(asyncResp->res);
2321bacb2162SNan Zhou             return;
2322bacb2162SNan Zhou         }
2323c1d019a6SEd Tanous         std::string type = path.parent_path().filename();
23241516c21bSJanet Adkins         std::string id = redfish::sensor_utils::getSensorId(sensorName, type);
2325c1d019a6SEd Tanous 
23261476687dSEd Tanous         nlohmann::json::object_t member;
2327ef4c65b7SEd Tanous         member["@odata.id"] = boost::urls::format(
2328ef4c65b7SEd Tanous             "/redfish/v1/Chassis/{}/{}/{}", chassisId, chassisSubNode, id);
2329c1d019a6SEd Tanous 
2330b2ba3072SPatrick Williams         entriesArray.emplace_back(std::move(member));
2331bacb2162SNan Zhou     }
2332bacb2162SNan Zhou 
2333c1d019a6SEd Tanous     asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
233462598e31SEd Tanous     BMCWEB_LOG_DEBUG("getChassisCallback exit");
2335bacb2162SNan Zhou }
2336e6bd846dSNan Zhou 
2337ac106bf6SEd Tanous inline void handleSensorCollectionGet(
2338ac106bf6SEd Tanous     App& app, const crow::Request& req,
2339ac106bf6SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2340de167a6fSNan Zhou     const std::string& chassisId)
2341de167a6fSNan Zhou {
2342de167a6fSNan Zhou     query_param::QueryCapabilities capabilities = {
2343de167a6fSNan Zhou         .canDelegateExpandLevel = 1,
2344de167a6fSNan Zhou     };
2345de167a6fSNan Zhou     query_param::Query delegatedQuery;
2346ac106bf6SEd Tanous     if (!redfish::setUpRedfishRouteWithDelegation(app, req, asyncResp,
2347de167a6fSNan Zhou                                                   delegatedQuery, capabilities))
2348de167a6fSNan Zhou     {
2349de167a6fSNan Zhou         return;
2350de167a6fSNan Zhou     }
2351de167a6fSNan Zhou 
2352de167a6fSNan Zhou     if (delegatedQuery.expandType != query_param::ExpandType::None)
2353de167a6fSNan Zhou     {
2354de167a6fSNan Zhou         // we perform efficient expand.
2355ac106bf6SEd Tanous         auto sensorsAsyncResp = std::make_shared<SensorsAsyncResp>(
2356ac106bf6SEd Tanous             asyncResp, chassisId, sensors::dbus::sensorPaths,
2357*c9563608SJanet Adkins             sensor_utils::sensorsNode,
2358de167a6fSNan Zhou             /*efficientExpand=*/true);
2359ac106bf6SEd Tanous         getChassisData(sensorsAsyncResp);
2360de167a6fSNan Zhou 
236162598e31SEd Tanous         BMCWEB_LOG_DEBUG(
236262598e31SEd Tanous             "SensorCollection doGet exit via efficient expand handler");
2363de167a6fSNan Zhou         return;
23640bad320cSEd Tanous     }
2365de167a6fSNan Zhou 
2366de167a6fSNan Zhou     // We get all sensors as hyperlinkes in the chassis (this
2367de167a6fSNan Zhou     // implies we reply on the default query parameters handler)
2368*c9563608SJanet Adkins     getChassis(asyncResp, chassisId, sensor_utils::sensorsNode,
2369*c9563608SJanet Adkins                dbus::sensorPaths,
2370ac106bf6SEd Tanous                std::bind_front(sensors::getChassisCallback, asyncResp,
2371*c9563608SJanet Adkins                                chassisId, sensor_utils::sensorsNode));
2372c1d019a6SEd Tanous }
23737f1cc26dSEd Tanous 
2374c1d019a6SEd Tanous inline void
2375c1d019a6SEd Tanous     getSensorFromDbus(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2376c1d019a6SEd Tanous                       const std::string& sensorPath,
2377c1d019a6SEd Tanous                       const ::dbus::utility::MapperGetObject& mapperResponse)
2378c1d019a6SEd Tanous {
2379c1d019a6SEd Tanous     if (mapperResponse.size() != 1)
2380c1d019a6SEd Tanous     {
2381c1d019a6SEd Tanous         messages::internalError(asyncResp->res);
2382c1d019a6SEd Tanous         return;
2383c1d019a6SEd Tanous     }
2384c1d019a6SEd Tanous     const auto& valueIface = *mapperResponse.begin();
2385c1d019a6SEd Tanous     const std::string& connectionName = valueIface.first;
238662598e31SEd Tanous     BMCWEB_LOG_DEBUG("Looking up {}", connectionName);
238762598e31SEd Tanous     BMCWEB_LOG_DEBUG("Path {}", sensorPath);
2388c1343bf6SKrzysztof Grobelny 
2389c1343bf6SKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
2390c1343bf6SKrzysztof Grobelny         *crow::connections::systemBus, connectionName, sensorPath, "",
2391c1d019a6SEd Tanous         [asyncResp,
23925e7e2dc5SEd Tanous          sensorPath](const boost::system::error_code& ec,
2393c1d019a6SEd Tanous                      const ::dbus::utility::DBusPropertiesMap& valuesDict) {
2394c1d019a6SEd Tanous             if (ec)
2395c1d019a6SEd Tanous             {
2396c1d019a6SEd Tanous                 messages::internalError(asyncResp->res);
2397c1d019a6SEd Tanous                 return;
2398c1d019a6SEd Tanous             }
2399c1d019a6SEd Tanous             sdbusplus::message::object_path path(sensorPath);
2400c1d019a6SEd Tanous             std::string name = path.filename();
2401c1d019a6SEd Tanous             path = path.parent_path();
2402c1d019a6SEd Tanous             std::string type = path.filename();
2403*c9563608SJanet Adkins             sensor_utils::objectPropertiesToJson(
2404*c9563608SJanet Adkins                 name, type, sensor_utils::sensorsNode, valuesDict,
2405*c9563608SJanet Adkins                 asyncResp->res.jsonValue, nullptr);
2406c1343bf6SKrzysztof Grobelny         });
2407de167a6fSNan Zhou }
2408de167a6fSNan Zhou 
2409e6bd846dSNan Zhou inline void handleSensorGet(App& app, const crow::Request& req,
2410c1d019a6SEd Tanous                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2411677bb756SEd Tanous                             const std::string& chassisId,
2412c1d019a6SEd Tanous                             const std::string& sensorId)
2413e6bd846dSNan Zhou {
2414c1d019a6SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2415e6bd846dSNan Zhou     {
2416e6bd846dSNan Zhou         return;
2417e6bd846dSNan Zhou     }
2418c71d6125SEd Tanous     std::pair<std::string, std::string> nameType =
24191516c21bSJanet Adkins         redfish::sensor_utils::splitSensorNameAndType(sensorId);
2420c71d6125SEd Tanous     if (nameType.first.empty() || nameType.second.empty())
2421c1d019a6SEd Tanous     {
2422c1d019a6SEd Tanous         messages::resourceNotFound(asyncResp->res, sensorId, "Sensor");
2423c1d019a6SEd Tanous         return;
2424c1d019a6SEd Tanous     }
2425c71d6125SEd Tanous 
2426ef4c65b7SEd Tanous     asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
2427ef4c65b7SEd Tanous         "/redfish/v1/Chassis/{}/Sensors/{}", chassisId, sensorId);
2428c1d019a6SEd Tanous 
242962598e31SEd Tanous     BMCWEB_LOG_DEBUG("Sensor doGet enter");
2430e6bd846dSNan Zhou 
24312b73119cSGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
2432e6bd846dSNan Zhou         "xyz.openbmc_project.Sensor.Value"};
2433c71d6125SEd Tanous     std::string sensorPath = "/xyz/openbmc_project/sensors/" + nameType.first +
2434c71d6125SEd Tanous                              '/' + nameType.second;
2435e6bd846dSNan Zhou     // Get a list of all of the sensors that implement Sensor.Value
2436e6bd846dSNan Zhou     // and get the path and service name associated with the sensor
24372b73119cSGeorge Liu     ::dbus::utility::getDbusObject(
24382b73119cSGeorge Liu         sensorPath, interfaces,
2439aec0ec30SMyung Bae         [asyncResp, sensorId,
24402b73119cSGeorge Liu          sensorPath](const boost::system::error_code& ec,
2441c1d019a6SEd Tanous                      const ::dbus::utility::MapperGetObject& subtree) {
244262598e31SEd Tanous             BMCWEB_LOG_DEBUG("respHandler1 enter");
2443aec0ec30SMyung Bae             if (ec == boost::system::errc::io_error)
2444aec0ec30SMyung Bae             {
244562598e31SEd Tanous                 BMCWEB_LOG_WARNING("Sensor not found from getSensorPaths");
2446aec0ec30SMyung Bae                 messages::resourceNotFound(asyncResp->res, sensorId, "Sensor");
2447aec0ec30SMyung Bae                 return;
2448aec0ec30SMyung Bae             }
2449e6bd846dSNan Zhou             if (ec)
2450e6bd846dSNan Zhou             {
2451c1d019a6SEd Tanous                 messages::internalError(asyncResp->res);
245262598e31SEd Tanous                 BMCWEB_LOG_ERROR(
245362598e31SEd Tanous                     "Sensor getSensorPaths resp_handler: Dbus error {}", ec);
2454e6bd846dSNan Zhou                 return;
2455e6bd846dSNan Zhou             }
2456c1d019a6SEd Tanous             getSensorFromDbus(asyncResp, sensorPath, subtree);
245762598e31SEd Tanous             BMCWEB_LOG_DEBUG("respHandler1 exit");
24582b73119cSGeorge Liu         });
2459e6bd846dSNan Zhou }
2460e6bd846dSNan Zhou 
2461bacb2162SNan Zhou } // namespace sensors
2462bacb2162SNan Zhou 
24637e860f15SJohn Edward Broadbent inline void requestRoutesSensorCollection(App& app)
246495a3ecadSAnthony Wilson {
24657e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/")
2466ed398213SEd Tanous         .privileges(redfish::privileges::getSensorCollection)
2467002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2468de167a6fSNan Zhou             std::bind_front(sensors::handleSensorCollectionGet, std::ref(app)));
246995a3ecadSAnthony Wilson }
247095a3ecadSAnthony Wilson 
24717e860f15SJohn Edward Broadbent inline void requestRoutesSensor(App& app)
247295a3ecadSAnthony Wilson {
24737e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/")
2474ed398213SEd Tanous         .privileges(redfish::privileges::getSensor)
2475002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2476e6bd846dSNan Zhou             std::bind_front(sensors::handleSensorGet, std::ref(app)));
247795a3ecadSAnthony Wilson }
247895a3ecadSAnthony Wilson 
247908777fb0SLewanczyk, Dawid } // namespace redfish
2480