xref: /openbmc/bmcweb/features/redfish/lib/sensors.hpp (revision bacb216228c45ca715163f4c81717b1af39889ab)
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 
187e860f15SJohn Edward Broadbent #include <app.hpp>
1908777fb0SLewanczyk, Dawid #include <boost/algorithm/string/predicate.hpp>
2008777fb0SLewanczyk, Dawid #include <boost/algorithm/string/split.hpp>
2108777fb0SLewanczyk, Dawid #include <boost/container/flat_map.hpp>
2208777fb0SLewanczyk, Dawid #include <boost/range/algorithm/replace_copy_if.hpp>
231abe55efSEd Tanous #include <dbus_singleton.hpp>
24168e20c1SEd Tanous #include <dbus_utility.hpp>
2545ca1b86SEd Tanous #include <query.hpp>
26ed398213SEd Tanous #include <registries/privilege_registry.hpp>
271e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp>
28413961deSRichard Marian Thomaiyar #include <utils/json_utils.hpp>
291214b7e7SGunnar Mills 
301214b7e7SGunnar Mills #include <cmath>
31b5a76932SEd Tanous #include <utility>
32abf2add6SEd Tanous #include <variant>
3308777fb0SLewanczyk, Dawid 
341abe55efSEd Tanous namespace redfish
351abe55efSEd Tanous {
3608777fb0SLewanczyk, Dawid 
37a0ec28b6SAdrian Ambrożewicz namespace sensors
38a0ec28b6SAdrian Ambrożewicz {
39a0ec28b6SAdrian Ambrożewicz namespace node
40a0ec28b6SAdrian Ambrożewicz {
41a0ec28b6SAdrian Ambrożewicz static constexpr std::string_view power = "Power";
42a0ec28b6SAdrian Ambrożewicz static constexpr std::string_view sensors = "Sensors";
43a0ec28b6SAdrian Ambrożewicz static constexpr std::string_view thermal = "Thermal";
44a0ec28b6SAdrian Ambrożewicz } // namespace node
45a0ec28b6SAdrian Ambrożewicz 
46a0ec28b6SAdrian Ambrożewicz namespace dbus
47a0ec28b6SAdrian Ambrożewicz {
48c2bf7f99SWludzik, Jozef 
49a0ec28b6SAdrian Ambrożewicz static const boost::container::flat_map<std::string_view,
50a0ec28b6SAdrian Ambrożewicz                                         std::vector<const char*>>
51c2bf7f99SWludzik, Jozef     paths = {{node::power,
52a0ec28b6SAdrian Ambrożewicz               {"/xyz/openbmc_project/sensors/voltage",
53a0ec28b6SAdrian Ambrożewicz                "/xyz/openbmc_project/sensors/power"}},
54a0ec28b6SAdrian Ambrożewicz              {node::sensors,
55a0ec28b6SAdrian Ambrożewicz               {"/xyz/openbmc_project/sensors/power",
56a0ec28b6SAdrian Ambrożewicz                "/xyz/openbmc_project/sensors/current",
577088690cSBasheer Ahmed Muddebihal                "/xyz/openbmc_project/sensors/airflow",
58e8204933SGeorge Liu #ifdef BMCWEB_NEW_POWERSUBSYSTEM_THERMALSUBSYSTEM
59e8204933SGeorge Liu                "/xyz/openbmc_project/sensors/voltage",
60e8204933SGeorge Liu                "/xyz/openbmc_project/sensors/fan_tach",
61e8204933SGeorge Liu                "/xyz/openbmc_project/sensors/temperature",
62e8204933SGeorge Liu                "/xyz/openbmc_project/sensors/fan_pwm",
63e8204933SGeorge Liu                "/xyz/openbmc_project/sensors/altitude",
64e8204933SGeorge Liu                "/xyz/openbmc_project/sensors/energy",
65e8204933SGeorge Liu #endif
66a0ec28b6SAdrian Ambrożewicz                "/xyz/openbmc_project/sensors/utilization"}},
67a0ec28b6SAdrian Ambrożewicz              {node::thermal,
68a0ec28b6SAdrian Ambrożewicz               {"/xyz/openbmc_project/sensors/fan_tach",
69a0ec28b6SAdrian Ambrożewicz                "/xyz/openbmc_project/sensors/temperature",
70a0ec28b6SAdrian Ambrożewicz                "/xyz/openbmc_project/sensors/fan_pwm"}}};
71c2bf7f99SWludzik, Jozef } // namespace dbus
72c2bf7f99SWludzik, Jozef 
73c2bf7f99SWludzik, Jozef inline const char* toReadingType(const std::string& sensorType)
74c2bf7f99SWludzik, Jozef {
75c2bf7f99SWludzik, Jozef     if (sensorType == "voltage")
76c2bf7f99SWludzik, Jozef     {
77c2bf7f99SWludzik, Jozef         return "Voltage";
78c2bf7f99SWludzik, Jozef     }
79c2bf7f99SWludzik, Jozef     if (sensorType == "power")
80c2bf7f99SWludzik, Jozef     {
81c2bf7f99SWludzik, Jozef         return "Power";
82c2bf7f99SWludzik, Jozef     }
83c2bf7f99SWludzik, Jozef     if (sensorType == "current")
84c2bf7f99SWludzik, Jozef     {
85c2bf7f99SWludzik, Jozef         return "Current";
86c2bf7f99SWludzik, Jozef     }
87c2bf7f99SWludzik, Jozef     if (sensorType == "fan_tach")
88c2bf7f99SWludzik, Jozef     {
89c2bf7f99SWludzik, Jozef         return "Rotational";
90c2bf7f99SWludzik, Jozef     }
91c2bf7f99SWludzik, Jozef     if (sensorType == "temperature")
92c2bf7f99SWludzik, Jozef     {
93c2bf7f99SWludzik, Jozef         return "Temperature";
94c2bf7f99SWludzik, Jozef     }
95c2bf7f99SWludzik, Jozef     if (sensorType == "fan_pwm" || sensorType == "utilization")
96c2bf7f99SWludzik, Jozef     {
97c2bf7f99SWludzik, Jozef         return "Percent";
98c2bf7f99SWludzik, Jozef     }
99c2bf7f99SWludzik, Jozef     if (sensorType == "altitude")
100c2bf7f99SWludzik, Jozef     {
101c2bf7f99SWludzik, Jozef         return "Altitude";
102c2bf7f99SWludzik, Jozef     }
103c2bf7f99SWludzik, Jozef     if (sensorType == "airflow")
104c2bf7f99SWludzik, Jozef     {
105c2bf7f99SWludzik, Jozef         return "AirFlow";
106c2bf7f99SWludzik, Jozef     }
107c2bf7f99SWludzik, Jozef     if (sensorType == "energy")
108c2bf7f99SWludzik, Jozef     {
109c2bf7f99SWludzik, Jozef         return "EnergyJoules";
110c2bf7f99SWludzik, Jozef     }
111c2bf7f99SWludzik, Jozef     return "";
112c2bf7f99SWludzik, Jozef }
113c2bf7f99SWludzik, Jozef 
114c2bf7f99SWludzik, Jozef inline const char* toReadingUnits(const std::string& sensorType)
115c2bf7f99SWludzik, Jozef {
116c2bf7f99SWludzik, Jozef     if (sensorType == "voltage")
117c2bf7f99SWludzik, Jozef     {
118c2bf7f99SWludzik, Jozef         return "V";
119c2bf7f99SWludzik, Jozef     }
120c2bf7f99SWludzik, Jozef     if (sensorType == "power")
121c2bf7f99SWludzik, Jozef     {
122c2bf7f99SWludzik, Jozef         return "W";
123c2bf7f99SWludzik, Jozef     }
124c2bf7f99SWludzik, Jozef     if (sensorType == "current")
125c2bf7f99SWludzik, Jozef     {
126c2bf7f99SWludzik, Jozef         return "A";
127c2bf7f99SWludzik, Jozef     }
128c2bf7f99SWludzik, Jozef     if (sensorType == "fan_tach")
129c2bf7f99SWludzik, Jozef     {
130c2bf7f99SWludzik, Jozef         return "RPM";
131c2bf7f99SWludzik, Jozef     }
132c2bf7f99SWludzik, Jozef     if (sensorType == "temperature")
133c2bf7f99SWludzik, Jozef     {
134c2bf7f99SWludzik, Jozef         return "Cel";
135c2bf7f99SWludzik, Jozef     }
136c2bf7f99SWludzik, Jozef     if (sensorType == "fan_pwm" || sensorType == "utilization")
137c2bf7f99SWludzik, Jozef     {
138c2bf7f99SWludzik, Jozef         return "%";
139c2bf7f99SWludzik, Jozef     }
140c2bf7f99SWludzik, Jozef     if (sensorType == "altitude")
141c2bf7f99SWludzik, Jozef     {
142c2bf7f99SWludzik, Jozef         return "m";
143c2bf7f99SWludzik, Jozef     }
144c2bf7f99SWludzik, Jozef     if (sensorType == "airflow")
145c2bf7f99SWludzik, Jozef     {
146c2bf7f99SWludzik, Jozef         return "cft_i/min";
147c2bf7f99SWludzik, Jozef     }
148c2bf7f99SWludzik, Jozef     if (sensorType == "energy")
149c2bf7f99SWludzik, Jozef     {
150c2bf7f99SWludzik, Jozef         return "J";
151c2bf7f99SWludzik, Jozef     }
152c2bf7f99SWludzik, Jozef     return "";
153a0ec28b6SAdrian Ambrożewicz }
154a0ec28b6SAdrian Ambrożewicz } // namespace sensors
155a0ec28b6SAdrian Ambrożewicz 
15608777fb0SLewanczyk, Dawid /**
157588c3f0dSKowalski, Kamil  * SensorsAsyncResp
15808777fb0SLewanczyk, Dawid  * Gathers data needed for response processing after async calls are done
15908777fb0SLewanczyk, Dawid  */
1601abe55efSEd Tanous class SensorsAsyncResp
1611abe55efSEd Tanous {
16208777fb0SLewanczyk, Dawid   public:
163a0ec28b6SAdrian Ambrożewicz     using DataCompleteCb = std::function<void(
164a0ec28b6SAdrian Ambrożewicz         const boost::beast::http::status status,
165a0ec28b6SAdrian Ambrożewicz         const boost::container::flat_map<std::string, std::string>& uriToDbus)>;
166a0ec28b6SAdrian Ambrożewicz 
167a0ec28b6SAdrian Ambrożewicz     struct SensorData
168a0ec28b6SAdrian Ambrożewicz     {
169a0ec28b6SAdrian Ambrożewicz         const std::string name;
170a0ec28b6SAdrian Ambrożewicz         std::string uri;
171a0ec28b6SAdrian Ambrożewicz         const std::string valueKey;
172a0ec28b6SAdrian Ambrożewicz         const std::string dbusPath;
173a0ec28b6SAdrian Ambrożewicz     };
174a0ec28b6SAdrian Ambrożewicz 
1758d1b46d7Szhanghch05     SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1768d1b46d7Szhanghch05                      const std::string& chassisIdIn,
177b5a76932SEd Tanous                      const std::vector<const char*>& typesIn,
178a0ec28b6SAdrian Ambrożewicz                      const std::string_view& subNode) :
1798d1b46d7Szhanghch05         asyncResp(asyncResp),
180271584abSEd Tanous         chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode)
1811214b7e7SGunnar Mills     {}
18208777fb0SLewanczyk, Dawid 
183a0ec28b6SAdrian Ambrożewicz     // Store extra data about sensor mapping and return it in callback
1848d1b46d7Szhanghch05     SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1858d1b46d7Szhanghch05                      const std::string& chassisIdIn,
186b5a76932SEd Tanous                      const std::vector<const char*>& typesIn,
187a0ec28b6SAdrian Ambrożewicz                      const std::string_view& subNode,
188a0ec28b6SAdrian Ambrożewicz                      DataCompleteCb&& creationComplete) :
1898d1b46d7Szhanghch05         asyncResp(asyncResp),
190a0ec28b6SAdrian Ambrożewicz         chassisId(chassisIdIn), types(typesIn),
191a0ec28b6SAdrian Ambrożewicz         chassisSubNode(subNode), metadata{std::vector<SensorData>()},
192a0ec28b6SAdrian Ambrożewicz         dataComplete{std::move(creationComplete)}
193a0ec28b6SAdrian Ambrożewicz     {}
194a0ec28b6SAdrian Ambrożewicz 
1951abe55efSEd Tanous     ~SensorsAsyncResp()
1961abe55efSEd Tanous     {
1978d1b46d7Szhanghch05         if (asyncResp->res.result() ==
1988d1b46d7Szhanghch05             boost::beast::http::status::internal_server_error)
1991abe55efSEd Tanous         {
2001abe55efSEd Tanous             // Reset the json object to clear out any data that made it in
2011abe55efSEd Tanous             // before the error happened todo(ed) handle error condition with
2021abe55efSEd Tanous             // proper code
2038d1b46d7Szhanghch05             asyncResp->res.jsonValue = nlohmann::json::object();
20408777fb0SLewanczyk, Dawid         }
205a0ec28b6SAdrian Ambrożewicz 
206a0ec28b6SAdrian Ambrożewicz         if (dataComplete && metadata)
207a0ec28b6SAdrian Ambrożewicz         {
208a0ec28b6SAdrian Ambrożewicz             boost::container::flat_map<std::string, std::string> map;
2098d1b46d7Szhanghch05             if (asyncResp->res.result() == boost::beast::http::status::ok)
210a0ec28b6SAdrian Ambrożewicz             {
211a0ec28b6SAdrian Ambrożewicz                 for (auto& sensor : *metadata)
212a0ec28b6SAdrian Ambrożewicz                 {
213a0ec28b6SAdrian Ambrożewicz                     map.insert(std::make_pair(sensor.uri + sensor.valueKey,
214a0ec28b6SAdrian Ambrożewicz                                               sensor.dbusPath));
215a0ec28b6SAdrian Ambrożewicz                 }
216a0ec28b6SAdrian Ambrożewicz             }
2178d1b46d7Szhanghch05             dataComplete(asyncResp->res.result(), map);
218a0ec28b6SAdrian Ambrożewicz         }
21908777fb0SLewanczyk, Dawid     }
220588c3f0dSKowalski, Kamil 
221ecd6a3a2SEd Tanous     SensorsAsyncResp(const SensorsAsyncResp&) = delete;
222ecd6a3a2SEd Tanous     SensorsAsyncResp(SensorsAsyncResp&&) = delete;
223ecd6a3a2SEd Tanous     SensorsAsyncResp& operator=(const SensorsAsyncResp&) = delete;
224ecd6a3a2SEd Tanous     SensorsAsyncResp& operator=(SensorsAsyncResp&&) = delete;
225ecd6a3a2SEd Tanous 
226a0ec28b6SAdrian Ambrożewicz     void addMetadata(const nlohmann::json& sensorObject,
227a0ec28b6SAdrian Ambrożewicz                      const std::string& valueKey, const std::string& dbusPath)
228a0ec28b6SAdrian Ambrożewicz     {
229a0ec28b6SAdrian Ambrożewicz         if (metadata)
230a0ec28b6SAdrian Ambrożewicz         {
231a0ec28b6SAdrian Ambrożewicz             metadata->emplace_back(SensorData{sensorObject["Name"],
232a0ec28b6SAdrian Ambrożewicz                                               sensorObject["@odata.id"],
233a0ec28b6SAdrian Ambrożewicz                                               valueKey, dbusPath});
234a0ec28b6SAdrian Ambrożewicz         }
235a0ec28b6SAdrian Ambrożewicz     }
236a0ec28b6SAdrian Ambrożewicz 
237a0ec28b6SAdrian Ambrożewicz     void updateUri(const std::string& name, const std::string& uri)
238a0ec28b6SAdrian Ambrożewicz     {
239a0ec28b6SAdrian Ambrożewicz         if (metadata)
240a0ec28b6SAdrian Ambrożewicz         {
241a0ec28b6SAdrian Ambrożewicz             for (auto& sensor : *metadata)
242a0ec28b6SAdrian Ambrożewicz             {
243a0ec28b6SAdrian Ambrożewicz                 if (sensor.name == name)
244a0ec28b6SAdrian Ambrożewicz                 {
245a0ec28b6SAdrian Ambrożewicz                     sensor.uri = uri;
246a0ec28b6SAdrian Ambrożewicz                 }
247a0ec28b6SAdrian Ambrożewicz             }
248a0ec28b6SAdrian Ambrożewicz         }
249a0ec28b6SAdrian Ambrożewicz     }
250a0ec28b6SAdrian Ambrożewicz 
2518d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp> asyncResp;
252a0ec28b6SAdrian Ambrożewicz     const std::string chassisId;
25308777fb0SLewanczyk, Dawid     const std::vector<const char*> types;
254a0ec28b6SAdrian Ambrożewicz     const std::string chassisSubNode;
255a0ec28b6SAdrian Ambrożewicz 
256a0ec28b6SAdrian Ambrożewicz   private:
257a0ec28b6SAdrian Ambrożewicz     std::optional<std::vector<SensorData>> metadata;
258a0ec28b6SAdrian Ambrożewicz     DataCompleteCb dataComplete;
25908777fb0SLewanczyk, Dawid };
26008777fb0SLewanczyk, Dawid 
26108777fb0SLewanczyk, Dawid /**
262d500549bSAnthony Wilson  * Possible states for physical inventory leds
263d500549bSAnthony Wilson  */
264d500549bSAnthony Wilson enum class LedState
265d500549bSAnthony Wilson {
266d500549bSAnthony Wilson     OFF,
267d500549bSAnthony Wilson     ON,
268d500549bSAnthony Wilson     BLINK,
269d500549bSAnthony Wilson     UNKNOWN
270d500549bSAnthony Wilson };
271d500549bSAnthony Wilson 
272d500549bSAnthony Wilson /**
273adc4f0dbSShawn McCarney  * D-Bus inventory item associated with one or more sensors.
274adc4f0dbSShawn McCarney  */
275adc4f0dbSShawn McCarney class InventoryItem
276adc4f0dbSShawn McCarney {
277adc4f0dbSShawn McCarney   public:
278e05aec50SEd Tanous     InventoryItem(const std::string& objPath) : objectPath(objPath)
279adc4f0dbSShawn McCarney     {
280adc4f0dbSShawn McCarney         // Set inventory item name to last node of object path
28128aa8de5SGeorge Liu         sdbusplus::message::object_path path(objectPath);
28228aa8de5SGeorge Liu         name = path.filename();
28328aa8de5SGeorge Liu         if (name.empty())
284adc4f0dbSShawn McCarney         {
28528aa8de5SGeorge Liu             BMCWEB_LOG_ERROR << "Failed to find '/' in " << objectPath;
286adc4f0dbSShawn McCarney         }
287adc4f0dbSShawn McCarney     }
288adc4f0dbSShawn McCarney 
289adc4f0dbSShawn McCarney     std::string objectPath;
290adc4f0dbSShawn McCarney     std::string name;
291e05aec50SEd Tanous     bool isPresent = true;
292e05aec50SEd Tanous     bool isFunctional = true;
293e05aec50SEd Tanous     bool isPowerSupply = false;
294e05aec50SEd Tanous     int powerSupplyEfficiencyPercent = -1;
295adc4f0dbSShawn McCarney     std::string manufacturer;
296adc4f0dbSShawn McCarney     std::string model;
297adc4f0dbSShawn McCarney     std::string partNumber;
298adc4f0dbSShawn McCarney     std::string serialNumber;
299adc4f0dbSShawn McCarney     std::set<std::string> sensors;
300d500549bSAnthony Wilson     std::string ledObjectPath;
301e05aec50SEd Tanous     LedState ledState = LedState::UNKNOWN;
302adc4f0dbSShawn McCarney };
303adc4f0dbSShawn McCarney 
304adc4f0dbSShawn McCarney /**
305413961deSRichard Marian Thomaiyar  * @brief Get objects with connection necessary for sensors
306588c3f0dSKowalski, Kamil  * @param SensorsAsyncResp Pointer to object holding response data
30708777fb0SLewanczyk, Dawid  * @param sensorNames Sensors retrieved from chassis
30808777fb0SLewanczyk, Dawid  * @param callback Callback for processing gathered connections
30908777fb0SLewanczyk, Dawid  */
31008777fb0SLewanczyk, Dawid template <typename Callback>
311413961deSRichard Marian Thomaiyar void getObjectsWithConnection(
31281ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
313b5a76932SEd Tanous     const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames,
3141abe55efSEd Tanous     Callback&& callback)
3151abe55efSEd Tanous {
316413961deSRichard Marian Thomaiyar     BMCWEB_LOG_DEBUG << "getObjectsWithConnection enter";
31703b5bae3SJames Feist     const std::string path = "/xyz/openbmc_project/sensors";
31808777fb0SLewanczyk, Dawid     const std::array<std::string, 1> interfaces = {
31908777fb0SLewanczyk, Dawid         "xyz.openbmc_project.Sensor.Value"};
32008777fb0SLewanczyk, Dawid 
32108777fb0SLewanczyk, Dawid     // Response handler for parsing objects subtree
322f94c4ecfSEd Tanous     auto respHandler = [callback{std::forward<Callback>(callback)},
323b9d36b47SEd Tanous                         sensorsAsyncResp, sensorNames](
324b9d36b47SEd Tanous                            const boost::system::error_code ec,
325b9d36b47SEd Tanous                            const dbus::utility::MapperGetSubTreeResponse&
326b9d36b47SEd Tanous                                subtree) {
327413961deSRichard Marian Thomaiyar         BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler enter";
3281abe55efSEd Tanous         if (ec)
3291abe55efSEd Tanous         {
3308d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
331413961deSRichard Marian Thomaiyar             BMCWEB_LOG_ERROR
332413961deSRichard Marian Thomaiyar                 << "getObjectsWithConnection resp_handler: Dbus error " << ec;
33308777fb0SLewanczyk, Dawid             return;
33408777fb0SLewanczyk, Dawid         }
33508777fb0SLewanczyk, Dawid 
33655c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "Found " << subtree.size() << " subtrees";
33708777fb0SLewanczyk, Dawid 
33808777fb0SLewanczyk, Dawid         // Make unique list of connections only for requested sensor types and
33908777fb0SLewanczyk, Dawid         // found in the chassis
34008777fb0SLewanczyk, Dawid         boost::container::flat_set<std::string> connections;
341413961deSRichard Marian Thomaiyar         std::set<std::pair<std::string, std::string>> objectsWithConnection;
3421abe55efSEd Tanous         // Intrinsic to avoid malloc.  Most systems will have < 8 sensor
3431abe55efSEd Tanous         // producers
34408777fb0SLewanczyk, Dawid         connections.reserve(8);
34508777fb0SLewanczyk, Dawid 
34649c53ac9SJohnathan Mantey         BMCWEB_LOG_DEBUG << "sensorNames list count: " << sensorNames->size();
34749c53ac9SJohnathan Mantey         for (const std::string& tsensor : *sensorNames)
3481abe55efSEd Tanous         {
34955c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "Sensor to find: " << tsensor;
35008777fb0SLewanczyk, Dawid         }
35108777fb0SLewanczyk, Dawid 
35208777fb0SLewanczyk, Dawid         for (const std::pair<
35308777fb0SLewanczyk, Dawid                  std::string,
35408777fb0SLewanczyk, Dawid                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
3551abe55efSEd Tanous                  object : subtree)
3561abe55efSEd Tanous         {
35749c53ac9SJohnathan Mantey             if (sensorNames->find(object.first) != sensorNames->end())
3581abe55efSEd Tanous             {
35949c53ac9SJohnathan Mantey                 for (const std::pair<std::string, std::vector<std::string>>&
3601abe55efSEd Tanous                          objData : object.second)
3611abe55efSEd Tanous                 {
36249c53ac9SJohnathan Mantey                     BMCWEB_LOG_DEBUG << "Adding connection: " << objData.first;
36308777fb0SLewanczyk, Dawid                     connections.insert(objData.first);
364de629b6eSShawn McCarney                     objectsWithConnection.insert(
365de629b6eSShawn McCarney                         std::make_pair(object.first, objData.first));
36608777fb0SLewanczyk, Dawid                 }
36708777fb0SLewanczyk, Dawid             }
36808777fb0SLewanczyk, Dawid         }
36955c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "Found " << connections.size() << " connections";
370413961deSRichard Marian Thomaiyar         callback(std::move(connections), std::move(objectsWithConnection));
371413961deSRichard Marian Thomaiyar         BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler exit";
37208777fb0SLewanczyk, Dawid     };
37308777fb0SLewanczyk, Dawid     // Make call to ObjectMapper to find all sensors objects
37455c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
37555c7b7a2SEd Tanous         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
3761abe55efSEd Tanous         "/xyz/openbmc_project/object_mapper",
3771abe55efSEd Tanous         "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 2, interfaces);
378413961deSRichard Marian Thomaiyar     BMCWEB_LOG_DEBUG << "getObjectsWithConnection exit";
379413961deSRichard Marian Thomaiyar }
380413961deSRichard Marian Thomaiyar 
381413961deSRichard Marian Thomaiyar /**
382413961deSRichard Marian Thomaiyar  * @brief Create connections necessary for sensors
383413961deSRichard Marian Thomaiyar  * @param SensorsAsyncResp Pointer to object holding response data
384413961deSRichard Marian Thomaiyar  * @param sensorNames Sensors retrieved from chassis
385413961deSRichard Marian Thomaiyar  * @param callback Callback for processing gathered connections
386413961deSRichard Marian Thomaiyar  */
387413961deSRichard Marian Thomaiyar template <typename Callback>
38849c53ac9SJohnathan Mantey void getConnections(
38981ce609eSEd Tanous     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
39049c53ac9SJohnathan Mantey     const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
391413961deSRichard Marian Thomaiyar     Callback&& callback)
392413961deSRichard Marian Thomaiyar {
393413961deSRichard Marian Thomaiyar     auto objectsWithConnectionCb =
394413961deSRichard Marian Thomaiyar         [callback](const boost::container::flat_set<std::string>& connections,
395413961deSRichard Marian Thomaiyar                    const std::set<std::pair<std::string, std::string>>&
3963174e4dfSEd Tanous                    /*objectsWithConnection*/) { callback(connections); };
39781ce609eSEd Tanous     getObjectsWithConnection(sensorsAsyncResp, sensorNames,
398413961deSRichard Marian Thomaiyar                              std::move(objectsWithConnectionCb));
39908777fb0SLewanczyk, Dawid }
40008777fb0SLewanczyk, Dawid 
40108777fb0SLewanczyk, Dawid /**
40249c53ac9SJohnathan Mantey  * @brief Shrinks the list of sensors for processing
40349c53ac9SJohnathan Mantey  * @param SensorsAysncResp  The class holding the Redfish response
40449c53ac9SJohnathan Mantey  * @param allSensors  A list of all the sensors associated to the
40549c53ac9SJohnathan Mantey  * chassis element (i.e. baseboard, front panel, etc...)
40649c53ac9SJohnathan Mantey  * @param activeSensors A list that is a reduction of the incoming
40749c53ac9SJohnathan Mantey  * allSensors list.  Eliminate Thermal sensors when a Power request is
40849c53ac9SJohnathan Mantey  * made, and eliminate Power sensors when a Thermal request is made.
40949c53ac9SJohnathan Mantey  */
41023a21a1cSEd Tanous inline void reduceSensorList(
41181ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
41249c53ac9SJohnathan Mantey     const std::vector<std::string>* allSensors,
413b5a76932SEd Tanous     const std::shared_ptr<boost::container::flat_set<std::string>>&
414b5a76932SEd Tanous         activeSensors)
41549c53ac9SJohnathan Mantey {
41681ce609eSEd Tanous     if (sensorsAsyncResp == nullptr)
41749c53ac9SJohnathan Mantey     {
41849c53ac9SJohnathan Mantey         return;
41949c53ac9SJohnathan Mantey     }
42049c53ac9SJohnathan Mantey     if ((allSensors == nullptr) || (activeSensors == nullptr))
42149c53ac9SJohnathan Mantey     {
42249c53ac9SJohnathan Mantey         messages::resourceNotFound(
4238d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res, sensorsAsyncResp->chassisSubNode,
42481ce609eSEd Tanous             sensorsAsyncResp->chassisSubNode == sensors::node::thermal
425a0ec28b6SAdrian Ambrożewicz                 ? "Temperatures"
42649c53ac9SJohnathan Mantey                 : "Voltages");
42749c53ac9SJohnathan Mantey 
42849c53ac9SJohnathan Mantey         return;
42949c53ac9SJohnathan Mantey     }
43049c53ac9SJohnathan Mantey     if (allSensors->empty())
43149c53ac9SJohnathan Mantey     {
43249c53ac9SJohnathan Mantey         // Nothing to do, the activeSensors object is also empty
43349c53ac9SJohnathan Mantey         return;
43449c53ac9SJohnathan Mantey     }
43549c53ac9SJohnathan Mantey 
43681ce609eSEd Tanous     for (const char* type : sensorsAsyncResp->types)
43749c53ac9SJohnathan Mantey     {
43849c53ac9SJohnathan Mantey         for (const std::string& sensor : *allSensors)
43949c53ac9SJohnathan Mantey         {
44049c53ac9SJohnathan Mantey             if (boost::starts_with(sensor, type))
44149c53ac9SJohnathan Mantey             {
44249c53ac9SJohnathan Mantey                 activeSensors->emplace(sensor);
44349c53ac9SJohnathan Mantey             }
44449c53ac9SJohnathan Mantey         }
44549c53ac9SJohnathan Mantey     }
44649c53ac9SJohnathan Mantey }
44749c53ac9SJohnathan Mantey 
44849c53ac9SJohnathan Mantey /**
4494bb3dc34SCarol Wang  * @brief Retrieves valid chassis path
4504bb3dc34SCarol Wang  * @param asyncResp   Pointer to object holding response data
4514bb3dc34SCarol Wang  * @param callback  Callback for next step to get valid chassis path
4524bb3dc34SCarol Wang  */
4534bb3dc34SCarol Wang template <typename Callback>
454b5a76932SEd Tanous void getValidChassisPath(const std::shared_ptr<SensorsAsyncResp>& asyncResp,
4554bb3dc34SCarol Wang                          Callback&& callback)
4564bb3dc34SCarol Wang {
4574bb3dc34SCarol Wang     BMCWEB_LOG_DEBUG << "checkChassisId enter";
4584bb3dc34SCarol Wang     const std::array<const char*, 2> interfaces = {
4594bb3dc34SCarol Wang         "xyz.openbmc_project.Inventory.Item.Board",
4604bb3dc34SCarol Wang         "xyz.openbmc_project.Inventory.Item.Chassis"};
4614bb3dc34SCarol Wang 
462b9d36b47SEd Tanous     auto respHandler = [callback{std::forward<Callback>(callback)}, asyncResp](
463b9d36b47SEd Tanous                            const boost::system::error_code ec,
464b9d36b47SEd Tanous                            const dbus::utility::MapperGetSubTreePathsResponse&
465b9d36b47SEd Tanous                                chassisPaths) mutable {
4664bb3dc34SCarol Wang         BMCWEB_LOG_DEBUG << "getValidChassisPath respHandler enter";
4674bb3dc34SCarol Wang         if (ec)
4684bb3dc34SCarol Wang         {
469b9d36b47SEd Tanous             BMCWEB_LOG_ERROR << "getValidChassisPath respHandler DBUS error: "
470b9d36b47SEd Tanous                              << ec;
4718d1b46d7Szhanghch05             messages::internalError(asyncResp->asyncResp->res);
4724bb3dc34SCarol Wang             return;
4734bb3dc34SCarol Wang         }
4744bb3dc34SCarol Wang 
4754bb3dc34SCarol Wang         std::optional<std::string> chassisPath;
4764bb3dc34SCarol Wang         std::string chassisName;
4774bb3dc34SCarol Wang         for (const std::string& chassis : chassisPaths)
4784bb3dc34SCarol Wang         {
47928aa8de5SGeorge Liu             sdbusplus::message::object_path path(chassis);
48028aa8de5SGeorge Liu             chassisName = path.filename();
48128aa8de5SGeorge Liu             if (chassisName.empty())
4824bb3dc34SCarol Wang             {
4834bb3dc34SCarol Wang                 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
4844bb3dc34SCarol Wang                 continue;
4854bb3dc34SCarol Wang             }
4864bb3dc34SCarol Wang             if (chassisName == asyncResp->chassisId)
4874bb3dc34SCarol Wang             {
4884bb3dc34SCarol Wang                 chassisPath = chassis;
4894bb3dc34SCarol Wang                 break;
4904bb3dc34SCarol Wang             }
4914bb3dc34SCarol Wang         }
4924bb3dc34SCarol Wang         callback(chassisPath);
4934bb3dc34SCarol Wang     };
4944bb3dc34SCarol Wang 
4954bb3dc34SCarol Wang     // Get the Chassis Collection
4964bb3dc34SCarol Wang     crow::connections::systemBus->async_method_call(
4974bb3dc34SCarol Wang         respHandler, "xyz.openbmc_project.ObjectMapper",
4984bb3dc34SCarol Wang         "/xyz/openbmc_project/object_mapper",
4994bb3dc34SCarol Wang         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
5004bb3dc34SCarol Wang         "/xyz/openbmc_project/inventory", 0, interfaces);
5014bb3dc34SCarol Wang     BMCWEB_LOG_DEBUG << "checkChassisId exit";
5024bb3dc34SCarol Wang }
5034bb3dc34SCarol Wang 
5044bb3dc34SCarol Wang /**
50508777fb0SLewanczyk, Dawid  * @brief Retrieves requested chassis sensors and redundancy data from DBus .
506588c3f0dSKowalski, Kamil  * @param SensorsAsyncResp   Pointer to object holding response data
50708777fb0SLewanczyk, Dawid  * @param callback  Callback for next step in gathered sensor processing
50808777fb0SLewanczyk, Dawid  */
50908777fb0SLewanczyk, Dawid template <typename Callback>
510b5a76932SEd Tanous void getChassis(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
5111abe55efSEd Tanous                 Callback&& callback)
5121abe55efSEd Tanous {
51355c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "getChassis enter";
514adc4f0dbSShawn McCarney     const std::array<const char*, 2> interfaces = {
51549c53ac9SJohnathan Mantey         "xyz.openbmc_project.Inventory.Item.Board",
516adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Item.Chassis"};
517f94c4ecfSEd Tanous     auto respHandler = [callback{std::forward<Callback>(callback)},
518f94c4ecfSEd Tanous                         sensorsAsyncResp](
51949c53ac9SJohnathan Mantey                            const boost::system::error_code ec,
520b9d36b47SEd Tanous                            const dbus::utility::MapperGetSubTreePathsResponse&
521b9d36b47SEd Tanous                                chassisPaths) {
52255c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "getChassis respHandler enter";
5231abe55efSEd Tanous         if (ec)
5241abe55efSEd Tanous         {
52555c7b7a2SEd Tanous             BMCWEB_LOG_ERROR << "getChassis respHandler DBUS error: " << ec;
5268d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
52708777fb0SLewanczyk, Dawid             return;
52808777fb0SLewanczyk, Dawid         }
52908777fb0SLewanczyk, Dawid 
53049c53ac9SJohnathan Mantey         const std::string* chassisPath = nullptr;
53149c53ac9SJohnathan Mantey         std::string chassisName;
53249c53ac9SJohnathan Mantey         for (const std::string& chassis : chassisPaths)
5331abe55efSEd Tanous         {
53428aa8de5SGeorge Liu             sdbusplus::message::object_path path(chassis);
53528aa8de5SGeorge Liu             chassisName = path.filename();
53628aa8de5SGeorge Liu             if (chassisName.empty())
5371abe55efSEd Tanous             {
53849c53ac9SJohnathan Mantey                 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
539daf36e2eSEd Tanous                 continue;
540daf36e2eSEd Tanous             }
54149c53ac9SJohnathan Mantey             if (chassisName == sensorsAsyncResp->chassisId)
5421abe55efSEd Tanous             {
54349c53ac9SJohnathan Mantey                 chassisPath = &chassis;
54449c53ac9SJohnathan Mantey                 break;
545daf36e2eSEd Tanous             }
54649c53ac9SJohnathan Mantey         }
54749c53ac9SJohnathan Mantey         if (chassisPath == nullptr)
5481abe55efSEd Tanous         {
5498d1b46d7Szhanghch05             messages::resourceNotFound(sensorsAsyncResp->asyncResp->res,
5508d1b46d7Szhanghch05                                        "Chassis", sensorsAsyncResp->chassisId);
55149c53ac9SJohnathan Mantey             return;
5521abe55efSEd Tanous         }
55308777fb0SLewanczyk, Dawid 
55449c53ac9SJohnathan Mantey         const std::string& chassisSubNode = sensorsAsyncResp->chassisSubNode;
555a0ec28b6SAdrian Ambrożewicz         if (chassisSubNode == sensors::node::power)
55649c53ac9SJohnathan Mantey         {
5578d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] =
55849c53ac9SJohnathan Mantey                 "#Power.v1_5_2.Power";
55949c53ac9SJohnathan Mantey         }
560a0ec28b6SAdrian Ambrożewicz         else if (chassisSubNode == sensors::node::thermal)
56149c53ac9SJohnathan Mantey         {
5628d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] =
56349c53ac9SJohnathan Mantey                 "#Thermal.v1_4_0.Thermal";
5648d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["Fans"] =
5658d1b46d7Szhanghch05                 nlohmann::json::array();
5668d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["Temperatures"] =
5674f9a2130SJennifer Lee                 nlohmann::json::array();
56849c53ac9SJohnathan Mantey         }
569a0ec28b6SAdrian Ambrożewicz         else if (chassisSubNode == sensors::node::sensors)
57095a3ecadSAnthony Wilson         {
5718d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] =
57295a3ecadSAnthony Wilson                 "#SensorCollection.SensorCollection";
5738d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["Description"] =
57495a3ecadSAnthony Wilson                 "Collection of Sensors for this Chassis";
5758d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["Members"] =
57695a3ecadSAnthony Wilson                 nlohmann::json::array();
5778d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["Members@odata.count"] =
5788d1b46d7Szhanghch05                 0;
57995a3ecadSAnthony Wilson         }
58095a3ecadSAnthony Wilson 
581a0ec28b6SAdrian Ambrożewicz         if (chassisSubNode != sensors::node::sensors)
58295a3ecadSAnthony Wilson         {
5838d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["Id"] = chassisSubNode;
58495a3ecadSAnthony Wilson         }
58595a3ecadSAnthony Wilson 
5868d1b46d7Szhanghch05         sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
58749c53ac9SJohnathan Mantey             "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId + "/" +
58849c53ac9SJohnathan Mantey             chassisSubNode;
5898d1b46d7Szhanghch05         sensorsAsyncResp->asyncResp->res.jsonValue["Name"] = chassisSubNode;
5908fb49dd6SShawn McCarney         // Get the list of all sensors for this Chassis element
5918fb49dd6SShawn McCarney         std::string sensorPath = *chassisPath + "/all_sensors";
5921e1e598dSJonathan Doman         sdbusplus::asio::getProperty<std::vector<std::string>>(
5931e1e598dSJonathan Doman             *crow::connections::systemBus, "xyz.openbmc_project.ObjectMapper",
5941e1e598dSJonathan Doman             sensorPath, "xyz.openbmc_project.Association", "endpoints",
595f94c4ecfSEd Tanous             [sensorsAsyncResp,
596f94c4ecfSEd Tanous              callback{std::forward<const Callback>(callback)}](
597271584abSEd Tanous                 const boost::system::error_code& e,
5981e1e598dSJonathan Doman                 const std::vector<std::string>& nodeSensorList) {
599271584abSEd Tanous                 if (e)
60049c53ac9SJohnathan Mantey                 {
601271584abSEd Tanous                     if (e.value() != EBADR)
60249c53ac9SJohnathan Mantey                     {
6038d1b46d7Szhanghch05                         messages::internalError(
6048d1b46d7Szhanghch05                             sensorsAsyncResp->asyncResp->res);
60549c53ac9SJohnathan Mantey                         return;
60649c53ac9SJohnathan Mantey                     }
60749c53ac9SJohnathan Mantey                 }
60849c53ac9SJohnathan Mantey                 const std::shared_ptr<boost::container::flat_set<std::string>>
60949c53ac9SJohnathan Mantey                     culledSensorList = std::make_shared<
61049c53ac9SJohnathan Mantey                         boost::container::flat_set<std::string>>();
6111e1e598dSJonathan Doman                 reduceSensorList(sensorsAsyncResp, &nodeSensorList,
61249c53ac9SJohnathan Mantey                                  culledSensorList);
61349c53ac9SJohnathan Mantey                 callback(culledSensorList);
6141e1e598dSJonathan Doman             });
61549c53ac9SJohnathan Mantey     };
61649c53ac9SJohnathan Mantey 
61749c53ac9SJohnathan Mantey     // Get the Chassis Collection
61849c53ac9SJohnathan Mantey     crow::connections::systemBus->async_method_call(
61949c53ac9SJohnathan Mantey         respHandler, "xyz.openbmc_project.ObjectMapper",
62049c53ac9SJohnathan Mantey         "/xyz/openbmc_project/object_mapper",
62149c53ac9SJohnathan Mantey         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
622271584abSEd Tanous         "/xyz/openbmc_project/inventory", 0, interfaces);
62355c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "getChassis exit";
62408777fb0SLewanczyk, Dawid }
62508777fb0SLewanczyk, Dawid 
62608777fb0SLewanczyk, Dawid /**
627de629b6eSShawn McCarney  * @brief Finds all DBus object paths that implement ObjectManager.
628de629b6eSShawn McCarney  *
629de629b6eSShawn McCarney  * Creates a mapping from the associated connection name to the object path.
630de629b6eSShawn McCarney  *
631de629b6eSShawn McCarney  * Finds the object paths asynchronously.  Invokes callback when information has
632de629b6eSShawn McCarney  * been obtained.
633de629b6eSShawn McCarney  *
634de629b6eSShawn McCarney  * The callback must have the following signature:
635de629b6eSShawn McCarney  *   @code
6368fb49dd6SShawn McCarney  *   callback(std::shared_ptr<boost::container::flat_map<std::string,
6378fb49dd6SShawn McCarney  *                std::string>> objectMgrPaths)
638de629b6eSShawn McCarney  *   @endcode
639de629b6eSShawn McCarney  *
64049c53ac9SJohnathan Mantey  * @param sensorsAsyncResp Pointer to object holding response data.
641de629b6eSShawn McCarney  * @param callback Callback to invoke when object paths obtained.
642de629b6eSShawn McCarney  */
643de629b6eSShawn McCarney template <typename Callback>
644b5a76932SEd Tanous void getObjectManagerPaths(
64581ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
646de629b6eSShawn McCarney     Callback&& callback)
647de629b6eSShawn McCarney {
648de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getObjectManagerPaths enter";
649de629b6eSShawn McCarney     const std::array<std::string, 1> interfaces = {
650de629b6eSShawn McCarney         "org.freedesktop.DBus.ObjectManager"};
651de629b6eSShawn McCarney 
652de629b6eSShawn McCarney     // Response handler for GetSubTree DBus method
653f94c4ecfSEd Tanous     auto respHandler = [callback{std::forward<Callback>(callback)},
654b9d36b47SEd Tanous                         sensorsAsyncResp](
655b9d36b47SEd Tanous                            const boost::system::error_code ec,
656b9d36b47SEd Tanous                            const dbus::utility::MapperGetSubTreeResponse&
657b9d36b47SEd Tanous                                subtree) {
658de629b6eSShawn McCarney         BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler enter";
659de629b6eSShawn McCarney         if (ec)
660de629b6eSShawn McCarney         {
6618d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
662de629b6eSShawn McCarney             BMCWEB_LOG_ERROR << "getObjectManagerPaths respHandler: DBus error "
663de629b6eSShawn McCarney                              << ec;
664de629b6eSShawn McCarney             return;
665de629b6eSShawn McCarney         }
666de629b6eSShawn McCarney 
667de629b6eSShawn McCarney         // Loop over returned object paths
6688fb49dd6SShawn McCarney         std::shared_ptr<boost::container::flat_map<std::string, std::string>>
6698fb49dd6SShawn McCarney             objectMgrPaths = std::make_shared<
6708fb49dd6SShawn McCarney                 boost::container::flat_map<std::string, std::string>>();
671de629b6eSShawn McCarney         for (const std::pair<
672de629b6eSShawn McCarney                  std::string,
673de629b6eSShawn McCarney                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
674de629b6eSShawn McCarney                  object : subtree)
675de629b6eSShawn McCarney         {
676de629b6eSShawn McCarney             // Loop over connections for current object path
677de629b6eSShawn McCarney             const std::string& objectPath = object.first;
678de629b6eSShawn McCarney             for (const std::pair<std::string, std::vector<std::string>>&
679de629b6eSShawn McCarney                      objData : object.second)
680de629b6eSShawn McCarney             {
681de629b6eSShawn McCarney                 // Add mapping from connection to object path
682de629b6eSShawn McCarney                 const std::string& connection = objData.first;
6838fb49dd6SShawn McCarney                 (*objectMgrPaths)[connection] = objectPath;
684de629b6eSShawn McCarney                 BMCWEB_LOG_DEBUG << "Added mapping " << connection << " -> "
685de629b6eSShawn McCarney                                  << objectPath;
686de629b6eSShawn McCarney             }
687de629b6eSShawn McCarney         }
6888fb49dd6SShawn McCarney         callback(objectMgrPaths);
689de629b6eSShawn McCarney         BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler exit";
690de629b6eSShawn McCarney     };
691de629b6eSShawn McCarney 
692de629b6eSShawn McCarney     // Query mapper for all DBus object paths that implement ObjectManager
693de629b6eSShawn McCarney     crow::connections::systemBus->async_method_call(
694de629b6eSShawn McCarney         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
695de629b6eSShawn McCarney         "/xyz/openbmc_project/object_mapper",
696271584abSEd Tanous         "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0, interfaces);
697de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getObjectManagerPaths exit";
698de629b6eSShawn McCarney }
699de629b6eSShawn McCarney 
700de629b6eSShawn McCarney /**
701adc4f0dbSShawn McCarney  * @brief Returns the Redfish State value for the specified inventory item.
702adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with a sensor.
703adc4f0dbSShawn McCarney  * @return State value for inventory item.
70434dd179eSJames Feist  */
70523a21a1cSEd Tanous inline std::string getState(const InventoryItem* inventoryItem)
706adc4f0dbSShawn McCarney {
707adc4f0dbSShawn McCarney     if ((inventoryItem != nullptr) && !(inventoryItem->isPresent))
708adc4f0dbSShawn McCarney     {
709adc4f0dbSShawn McCarney         return "Absent";
710adc4f0dbSShawn McCarney     }
71134dd179eSJames Feist 
712adc4f0dbSShawn McCarney     return "Enabled";
713adc4f0dbSShawn McCarney }
714adc4f0dbSShawn McCarney 
715adc4f0dbSShawn McCarney /**
716adc4f0dbSShawn McCarney  * @brief Returns the Redfish Health value for the specified sensor.
717adc4f0dbSShawn McCarney  * @param sensorJson Sensor JSON object.
718adc4f0dbSShawn McCarney  * @param interfacesDict Map of all sensor interfaces.
719adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with the sensor.  Will
720adc4f0dbSShawn McCarney  * be nullptr if no associated inventory item was found.
721adc4f0dbSShawn McCarney  * @return Health value for sensor.
722adc4f0dbSShawn McCarney  */
723711ac7a9SEd Tanous inline std::string
724711ac7a9SEd Tanous     getHealth(nlohmann::json& sensorJson,
725711ac7a9SEd Tanous               const dbus::utility::DBusInteracesMap& interfacesDict,
726adc4f0dbSShawn McCarney               const InventoryItem* inventoryItem)
72734dd179eSJames Feist {
728adc4f0dbSShawn McCarney     // Get current health value (if any) in the sensor JSON object.  Some JSON
729adc4f0dbSShawn McCarney     // objects contain multiple sensors (such as PowerSupplies).  We want to set
730adc4f0dbSShawn McCarney     // the overall health to be the most severe of any of the sensors.
731adc4f0dbSShawn McCarney     std::string currentHealth;
732adc4f0dbSShawn McCarney     auto statusIt = sensorJson.find("Status");
733adc4f0dbSShawn McCarney     if (statusIt != sensorJson.end())
734adc4f0dbSShawn McCarney     {
735adc4f0dbSShawn McCarney         auto healthIt = statusIt->find("Health");
736adc4f0dbSShawn McCarney         if (healthIt != statusIt->end())
737adc4f0dbSShawn McCarney         {
738adc4f0dbSShawn McCarney             std::string* health = healthIt->get_ptr<std::string*>();
739adc4f0dbSShawn McCarney             if (health != nullptr)
740adc4f0dbSShawn McCarney             {
741adc4f0dbSShawn McCarney                 currentHealth = *health;
742adc4f0dbSShawn McCarney             }
743adc4f0dbSShawn McCarney         }
744adc4f0dbSShawn McCarney     }
745adc4f0dbSShawn McCarney 
746adc4f0dbSShawn McCarney     // If current health in JSON object is already Critical, return that.  This
747adc4f0dbSShawn McCarney     // should override the sensor health, which might be less severe.
748adc4f0dbSShawn McCarney     if (currentHealth == "Critical")
749adc4f0dbSShawn McCarney     {
750adc4f0dbSShawn McCarney         return "Critical";
751adc4f0dbSShawn McCarney     }
752adc4f0dbSShawn McCarney 
753adc4f0dbSShawn McCarney     // Check if sensor has critical threshold alarm
754711ac7a9SEd Tanous 
7559eb808c1SEd Tanous     for (const auto& [interface, values] : interfacesDict)
75634dd179eSJames Feist     {
757711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Sensor.Threshold.Critical")
75834dd179eSJames Feist         {
7599eb808c1SEd Tanous             for (const auto& [valueName, value] : values)
760711ac7a9SEd Tanous             {
761711ac7a9SEd Tanous                 if (valueName == "CriticalAlarmHigh" ||
762711ac7a9SEd Tanous                     valueName == "CriticalAlarmLow")
763711ac7a9SEd Tanous                 {
764711ac7a9SEd Tanous                     const bool* asserted = std::get_if<bool>(&value);
76534dd179eSJames Feist                     if (asserted == nullptr)
76634dd179eSJames Feist                     {
76734dd179eSJames Feist                         BMCWEB_LOG_ERROR << "Illegal sensor threshold";
76834dd179eSJames Feist                     }
76934dd179eSJames Feist                     else if (*asserted)
77034dd179eSJames Feist                     {
77134dd179eSJames Feist                         return "Critical";
77234dd179eSJames Feist                     }
77334dd179eSJames Feist                 }
77434dd179eSJames Feist             }
77534dd179eSJames Feist         }
77634dd179eSJames Feist     }
77734dd179eSJames Feist 
778adc4f0dbSShawn McCarney     // Check if associated inventory item is not functional
779adc4f0dbSShawn McCarney     if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional))
780adc4f0dbSShawn McCarney     {
781adc4f0dbSShawn McCarney         return "Critical";
782adc4f0dbSShawn McCarney     }
783adc4f0dbSShawn McCarney 
784adc4f0dbSShawn McCarney     // If current health in JSON object is already Warning, return that.  This
785adc4f0dbSShawn McCarney     // should override the sensor status, which might be less severe.
786adc4f0dbSShawn McCarney     if (currentHealth == "Warning")
787adc4f0dbSShawn McCarney     {
788adc4f0dbSShawn McCarney         return "Warning";
789adc4f0dbSShawn McCarney     }
790adc4f0dbSShawn McCarney 
791adc4f0dbSShawn McCarney     // Check if sensor has warning threshold alarm
7929eb808c1SEd Tanous     for (const auto& [interface, values] : interfacesDict)
79334dd179eSJames Feist     {
794711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Sensor.Threshold.Warning")
79534dd179eSJames Feist         {
7969eb808c1SEd Tanous             for (const auto& [valueName, value] : values)
797711ac7a9SEd Tanous             {
798711ac7a9SEd Tanous                 if (valueName == "WarningAlarmHigh" ||
799711ac7a9SEd Tanous                     valueName == "WarningAlarmLow")
800711ac7a9SEd Tanous                 {
801711ac7a9SEd Tanous                     const bool* asserted = std::get_if<bool>(&value);
80234dd179eSJames Feist                     if (asserted == nullptr)
80334dd179eSJames Feist                     {
80434dd179eSJames Feist                         BMCWEB_LOG_ERROR << "Illegal sensor threshold";
80534dd179eSJames Feist                     }
80634dd179eSJames Feist                     else if (*asserted)
80734dd179eSJames Feist                     {
808ebe4d91eSEd Tanous                         return "Warning";
80934dd179eSJames Feist                     }
81034dd179eSJames Feist                 }
81134dd179eSJames Feist             }
81234dd179eSJames Feist         }
81334dd179eSJames Feist     }
814adc4f0dbSShawn McCarney 
81534dd179eSJames Feist     return "OK";
81634dd179eSJames Feist }
81734dd179eSJames Feist 
81823a21a1cSEd Tanous inline void setLedState(nlohmann::json& sensorJson,
819d500549bSAnthony Wilson                         const InventoryItem* inventoryItem)
820d500549bSAnthony Wilson {
821d500549bSAnthony Wilson     if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty())
822d500549bSAnthony Wilson     {
823d500549bSAnthony Wilson         switch (inventoryItem->ledState)
824d500549bSAnthony Wilson         {
825d500549bSAnthony Wilson             case LedState::OFF:
826d500549bSAnthony Wilson                 sensorJson["IndicatorLED"] = "Off";
827d500549bSAnthony Wilson                 break;
828d500549bSAnthony Wilson             case LedState::ON:
829d500549bSAnthony Wilson                 sensorJson["IndicatorLED"] = "Lit";
830d500549bSAnthony Wilson                 break;
831d500549bSAnthony Wilson             case LedState::BLINK:
832d500549bSAnthony Wilson                 sensorJson["IndicatorLED"] = "Blinking";
833d500549bSAnthony Wilson                 break;
83423a21a1cSEd Tanous             case LedState::UNKNOWN:
835d500549bSAnthony Wilson                 break;
836d500549bSAnthony Wilson         }
837d500549bSAnthony Wilson     }
838d500549bSAnthony Wilson }
839d500549bSAnthony Wilson 
84034dd179eSJames Feist /**
84108777fb0SLewanczyk, Dawid  * @brief Builds a json sensor representation of a sensor.
84208777fb0SLewanczyk, Dawid  * @param sensorName  The name of the sensor to be built
843274fad5aSGunnar Mills  * @param sensorType  The type (temperature, fan_tach, etc) of the sensor to
84408777fb0SLewanczyk, Dawid  * build
845a0ec28b6SAdrian Ambrożewicz  * @param sensorsAsyncResp  Sensor metadata
84608777fb0SLewanczyk, Dawid  * @param interfacesDict  A dictionary of the interfaces and properties of said
84708777fb0SLewanczyk, Dawid  * interfaces to be built from
84808777fb0SLewanczyk, Dawid  * @param sensor_json  The json object to fill
849adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with the sensor.  Will
850adc4f0dbSShawn McCarney  * be nullptr if no associated inventory item was found.
85108777fb0SLewanczyk, Dawid  */
85223a21a1cSEd Tanous inline void objectInterfacesToJson(
85308777fb0SLewanczyk, Dawid     const std::string& sensorName, const std::string& sensorType,
854b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
855711ac7a9SEd Tanous     const dbus::utility::DBusInteracesMap& interfacesDict,
85681ce609eSEd Tanous     nlohmann::json& sensorJson, InventoryItem* inventoryItem)
8571abe55efSEd Tanous {
85808777fb0SLewanczyk, Dawid     // Assume values exist as is (10^0 == 1) if no scale exists
85908777fb0SLewanczyk, Dawid     int64_t scaleMultiplier = 0;
8609eb808c1SEd Tanous     for (const auto& [interface, values] : interfacesDict)
8611abe55efSEd Tanous     {
862711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Sensor.Value")
863711ac7a9SEd Tanous         {
8649eb808c1SEd Tanous             for (const auto& [valueName, value] : values)
865711ac7a9SEd Tanous             {
866711ac7a9SEd Tanous                 if (valueName == "Scale")
867711ac7a9SEd Tanous                 {
868711ac7a9SEd Tanous                     const int64_t* int64Value = std::get_if<int64_t>(&value);
8691abe55efSEd Tanous                     if (int64Value != nullptr)
8701abe55efSEd Tanous                     {
87108777fb0SLewanczyk, Dawid                         scaleMultiplier = *int64Value;
87208777fb0SLewanczyk, Dawid                     }
87308777fb0SLewanczyk, Dawid                 }
874711ac7a9SEd Tanous             }
875711ac7a9SEd Tanous         }
876711ac7a9SEd Tanous     }
87708777fb0SLewanczyk, Dawid 
878a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
879adc4f0dbSShawn McCarney     {
88095a3ecadSAnthony Wilson         // For sensors in SensorCollection we set Id instead of MemberId,
88195a3ecadSAnthony Wilson         // including power sensors.
88281ce609eSEd Tanous         sensorJson["Id"] = sensorName;
88381ce609eSEd Tanous         sensorJson["Name"] = boost::replace_all_copy(sensorName, "_", " ");
88495a3ecadSAnthony Wilson     }
88595a3ecadSAnthony Wilson     else if (sensorType != "power")
88695a3ecadSAnthony Wilson     {
88795a3ecadSAnthony Wilson         // Set MemberId and Name for non-power sensors.  For PowerSupplies and
88895a3ecadSAnthony Wilson         // PowerControl, those properties have more general values because
88995a3ecadSAnthony Wilson         // multiple sensors can be stored in the same JSON object.
89081ce609eSEd Tanous         sensorJson["MemberId"] = sensorName;
89181ce609eSEd Tanous         sensorJson["Name"] = boost::replace_all_copy(sensorName, "_", " ");
892adc4f0dbSShawn McCarney     }
893e742b6ccSEd Tanous 
89481ce609eSEd Tanous     sensorJson["Status"]["State"] = getState(inventoryItem);
89581ce609eSEd Tanous     sensorJson["Status"]["Health"] =
89681ce609eSEd Tanous         getHealth(sensorJson, interfacesDict, inventoryItem);
89708777fb0SLewanczyk, Dawid 
89808777fb0SLewanczyk, Dawid     // Parameter to set to override the type we get from dbus, and force it to
89908777fb0SLewanczyk, Dawid     // int, regardless of what is available.  This is used for schemas like fan,
90008777fb0SLewanczyk, Dawid     // that require integers, not floats.
90108777fb0SLewanczyk, Dawid     bool forceToInt = false;
90208777fb0SLewanczyk, Dawid 
9033929aca1SAnthony Wilson     nlohmann::json::json_pointer unit("/Reading");
904a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
90595a3ecadSAnthony Wilson     {
90681ce609eSEd Tanous         sensorJson["@odata.type"] = "#Sensor.v1_0_0.Sensor";
907c2bf7f99SWludzik, Jozef 
908c2bf7f99SWludzik, Jozef         const std::string& readingType = sensors::toReadingType(sensorType);
909c2bf7f99SWludzik, Jozef         if (readingType.empty())
91095a3ecadSAnthony Wilson         {
911c2bf7f99SWludzik, Jozef             BMCWEB_LOG_ERROR << "Redfish cannot map reading type for "
912c2bf7f99SWludzik, Jozef                              << sensorType;
91395a3ecadSAnthony Wilson         }
914c2bf7f99SWludzik, Jozef         else
91595a3ecadSAnthony Wilson         {
916c2bf7f99SWludzik, Jozef             sensorJson["ReadingType"] = readingType;
91795a3ecadSAnthony Wilson         }
918c2bf7f99SWludzik, Jozef 
919c2bf7f99SWludzik, Jozef         const std::string& readingUnits = sensors::toReadingUnits(sensorType);
920c2bf7f99SWludzik, Jozef         if (readingUnits.empty())
921f8ede15eSAdrian Ambrożewicz         {
922c2bf7f99SWludzik, Jozef             BMCWEB_LOG_ERROR << "Redfish cannot map reading unit for "
923c2bf7f99SWludzik, Jozef                              << sensorType;
924c2bf7f99SWludzik, Jozef         }
925c2bf7f99SWludzik, Jozef         else
926c2bf7f99SWludzik, Jozef         {
927c2bf7f99SWludzik, Jozef             sensorJson["ReadingUnits"] = readingUnits;
928f8ede15eSAdrian Ambrożewicz         }
92995a3ecadSAnthony Wilson     }
93095a3ecadSAnthony Wilson     else if (sensorType == "temperature")
9311abe55efSEd Tanous     {
9323929aca1SAnthony Wilson         unit = "/ReadingCelsius"_json_pointer;
93381ce609eSEd Tanous         sensorJson["@odata.type"] = "#Thermal.v1_3_0.Temperature";
93408777fb0SLewanczyk, Dawid         // TODO(ed) Documentation says that path should be type fan_tach,
93508777fb0SLewanczyk, Dawid         // implementation seems to implement fan
9361abe55efSEd Tanous     }
9371abe55efSEd Tanous     else if (sensorType == "fan" || sensorType == "fan_tach")
9381abe55efSEd Tanous     {
9393929aca1SAnthony Wilson         unit = "/Reading"_json_pointer;
94081ce609eSEd Tanous         sensorJson["ReadingUnits"] = "RPM";
94181ce609eSEd Tanous         sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
94281ce609eSEd Tanous         setLedState(sensorJson, inventoryItem);
94308777fb0SLewanczyk, Dawid         forceToInt = true;
9441abe55efSEd Tanous     }
9456f6d0d32SEd Tanous     else if (sensorType == "fan_pwm")
9466f6d0d32SEd Tanous     {
9473929aca1SAnthony Wilson         unit = "/Reading"_json_pointer;
94881ce609eSEd Tanous         sensorJson["ReadingUnits"] = "Percent";
94981ce609eSEd Tanous         sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
95081ce609eSEd Tanous         setLedState(sensorJson, inventoryItem);
9516f6d0d32SEd Tanous         forceToInt = true;
9526f6d0d32SEd Tanous     }
9531abe55efSEd Tanous     else if (sensorType == "voltage")
9541abe55efSEd Tanous     {
9553929aca1SAnthony Wilson         unit = "/ReadingVolts"_json_pointer;
95681ce609eSEd Tanous         sensorJson["@odata.type"] = "#Power.v1_0_0.Voltage";
9571abe55efSEd Tanous     }
9582474adfaSEd Tanous     else if (sensorType == "power")
9592474adfaSEd Tanous     {
96049c53ac9SJohnathan Mantey         std::string sensorNameLower =
96149c53ac9SJohnathan Mantey             boost::algorithm::to_lower_copy(sensorName);
96249c53ac9SJohnathan Mantey 
96355f79e6fSEd Tanous         if (sensorName == "total_power")
964028f7ebcSEddie James         {
96581ce609eSEd Tanous             sensorJson["@odata.type"] = "#Power.v1_0_0.PowerControl";
9667ab06f49SGunnar Mills             // Put multiple "sensors" into a single PowerControl, so have
9677ab06f49SGunnar Mills             // generic names for MemberId and Name. Follows Redfish mockup.
96881ce609eSEd Tanous             sensorJson["MemberId"] = "0";
96981ce609eSEd Tanous             sensorJson["Name"] = "Chassis Power Control";
9703929aca1SAnthony Wilson             unit = "/PowerConsumedWatts"_json_pointer;
971028f7ebcSEddie James         }
972028f7ebcSEddie James         else if (sensorNameLower.find("input") != std::string::npos)
97349c53ac9SJohnathan Mantey         {
9743929aca1SAnthony Wilson             unit = "/PowerInputWatts"_json_pointer;
97549c53ac9SJohnathan Mantey         }
97649c53ac9SJohnathan Mantey         else
97749c53ac9SJohnathan Mantey         {
9783929aca1SAnthony Wilson             unit = "/PowerOutputWatts"_json_pointer;
97949c53ac9SJohnathan Mantey         }
9802474adfaSEd Tanous     }
9811abe55efSEd Tanous     else
9821abe55efSEd Tanous     {
98355c7b7a2SEd Tanous         BMCWEB_LOG_ERROR << "Redfish cannot map object type for " << sensorName;
98408777fb0SLewanczyk, Dawid         return;
98508777fb0SLewanczyk, Dawid     }
98608777fb0SLewanczyk, Dawid     // Map of dbus interface name, dbus property name and redfish property_name
9873929aca1SAnthony Wilson     std::vector<
9883929aca1SAnthony Wilson         std::tuple<const char*, const char*, nlohmann::json::json_pointer>>
9893929aca1SAnthony Wilson         properties;
99008777fb0SLewanczyk, Dawid     properties.reserve(7);
99108777fb0SLewanczyk, Dawid 
99208777fb0SLewanczyk, Dawid     properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit);
993de629b6eSShawn McCarney 
994a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
9953929aca1SAnthony Wilson     {
9963929aca1SAnthony Wilson         properties.emplace_back(
9973929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh",
9983929aca1SAnthony Wilson             "/Thresholds/UpperCaution/Reading"_json_pointer);
9993929aca1SAnthony Wilson         properties.emplace_back(
10003929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow",
10013929aca1SAnthony Wilson             "/Thresholds/LowerCaution/Reading"_json_pointer);
10023929aca1SAnthony Wilson         properties.emplace_back(
10033929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh",
10043929aca1SAnthony Wilson             "/Thresholds/UpperCritical/Reading"_json_pointer);
10053929aca1SAnthony Wilson         properties.emplace_back(
10063929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow",
10073929aca1SAnthony Wilson             "/Thresholds/LowerCritical/Reading"_json_pointer);
10083929aca1SAnthony Wilson     }
10093929aca1SAnthony Wilson     else if (sensorType != "power")
1010de629b6eSShawn McCarney     {
101108777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
10123929aca1SAnthony Wilson                                 "WarningHigh",
10133929aca1SAnthony Wilson                                 "/UpperThresholdNonCritical"_json_pointer);
101408777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
10153929aca1SAnthony Wilson                                 "WarningLow",
10163929aca1SAnthony Wilson                                 "/LowerThresholdNonCritical"_json_pointer);
101708777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
10183929aca1SAnthony Wilson                                 "CriticalHigh",
10193929aca1SAnthony Wilson                                 "/UpperThresholdCritical"_json_pointer);
102008777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
10213929aca1SAnthony Wilson                                 "CriticalLow",
10223929aca1SAnthony Wilson                                 "/LowerThresholdCritical"_json_pointer);
1023de629b6eSShawn McCarney     }
102408777fb0SLewanczyk, Dawid 
10252474adfaSEd Tanous     // TODO Need to get UpperThresholdFatal and LowerThresholdFatal
10262474adfaSEd Tanous 
1027a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
102895a3ecadSAnthony Wilson     {
102995a3ecadSAnthony Wilson         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
10303929aca1SAnthony Wilson                                 "/ReadingRangeMin"_json_pointer);
103195a3ecadSAnthony Wilson         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
10323929aca1SAnthony Wilson                                 "/ReadingRangeMax"_json_pointer);
103395a3ecadSAnthony Wilson     }
103495a3ecadSAnthony Wilson     else if (sensorType == "temperature")
10351abe55efSEd Tanous     {
103608777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
10373929aca1SAnthony Wilson                                 "/MinReadingRangeTemp"_json_pointer);
103808777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
10393929aca1SAnthony Wilson                                 "/MaxReadingRangeTemp"_json_pointer);
10401abe55efSEd Tanous     }
1041adc4f0dbSShawn McCarney     else if (sensorType != "power")
10421abe55efSEd Tanous     {
104308777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
10443929aca1SAnthony Wilson                                 "/MinReadingRange"_json_pointer);
104508777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
10463929aca1SAnthony Wilson                                 "/MaxReadingRange"_json_pointer);
104708777fb0SLewanczyk, Dawid     }
104808777fb0SLewanczyk, Dawid 
10493929aca1SAnthony Wilson     for (const std::tuple<const char*, const char*,
10503929aca1SAnthony Wilson                           nlohmann::json::json_pointer>& p : properties)
10511abe55efSEd Tanous     {
105255f79e6fSEd Tanous         for (const auto& [interface, values] : interfacesDict)
10531abe55efSEd Tanous         {
1054711ac7a9SEd Tanous             if (interface != std::get<0>(p))
10551abe55efSEd Tanous             {
1056711ac7a9SEd Tanous                 continue;
1057711ac7a9SEd Tanous             }
105855f79e6fSEd Tanous             for (const auto& [valueName, valueVariant] : values)
1059711ac7a9SEd Tanous             {
1060711ac7a9SEd Tanous                 if (valueName != std::get<1>(p))
1061711ac7a9SEd Tanous                 {
1062711ac7a9SEd Tanous                     continue;
1063711ac7a9SEd Tanous                 }
10643929aca1SAnthony Wilson 
10653929aca1SAnthony Wilson                 // The property we want to set may be nested json, so use
10663929aca1SAnthony Wilson                 // a json_pointer for easy indexing into the json structure.
10673929aca1SAnthony Wilson                 const nlohmann::json::json_pointer& key = std::get<2>(p);
10683929aca1SAnthony Wilson 
106908777fb0SLewanczyk, Dawid                 // Attempt to pull the int64 directly
1070abf2add6SEd Tanous                 const int64_t* int64Value = std::get_if<int64_t>(&valueVariant);
107108777fb0SLewanczyk, Dawid 
1072abf2add6SEd Tanous                 const double* doubleValue = std::get_if<double>(&valueVariant);
1073028f7ebcSEddie James                 const uint32_t* uValue = std::get_if<uint32_t>(&valueVariant);
10746f6d0d32SEd Tanous                 double temp = 0.0;
10756f6d0d32SEd Tanous                 if (int64Value != nullptr)
10761abe55efSEd Tanous                 {
1077271584abSEd Tanous                     temp = static_cast<double>(*int64Value);
10786f6d0d32SEd Tanous                 }
10796f6d0d32SEd Tanous                 else if (doubleValue != nullptr)
10801abe55efSEd Tanous                 {
10816f6d0d32SEd Tanous                     temp = *doubleValue;
10821abe55efSEd Tanous                 }
1083028f7ebcSEddie James                 else if (uValue != nullptr)
1084028f7ebcSEddie James                 {
1085028f7ebcSEddie James                     temp = *uValue;
1086028f7ebcSEddie James                 }
10871abe55efSEd Tanous                 else
10881abe55efSEd Tanous                 {
10896f6d0d32SEd Tanous                     BMCWEB_LOG_ERROR
10906f6d0d32SEd Tanous                         << "Got value interface that wasn't int or double";
10916f6d0d32SEd Tanous                     continue;
109208777fb0SLewanczyk, Dawid                 }
10936f6d0d32SEd Tanous                 temp = temp * std::pow(10, scaleMultiplier);
10946f6d0d32SEd Tanous                 if (forceToInt)
10956f6d0d32SEd Tanous                 {
109681ce609eSEd Tanous                     sensorJson[key] = static_cast<int64_t>(temp);
10976f6d0d32SEd Tanous                 }
10986f6d0d32SEd Tanous                 else
10996f6d0d32SEd Tanous                 {
110081ce609eSEd Tanous                     sensorJson[key] = temp;
110108777fb0SLewanczyk, Dawid                 }
110208777fb0SLewanczyk, Dawid             }
110308777fb0SLewanczyk, Dawid         }
110408777fb0SLewanczyk, Dawid     }
1105a0ec28b6SAdrian Ambrożewicz 
110681ce609eSEd Tanous     sensorsAsyncResp->addMetadata(sensorJson, unit.to_string(),
1107a0ec28b6SAdrian Ambrożewicz                                   "/xyz/openbmc_project/sensors/" + sensorType +
1108a0ec28b6SAdrian Ambrożewicz                                       "/" + sensorName);
1109a0ec28b6SAdrian Ambrożewicz 
111055c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "Added sensor " << sensorName;
111108777fb0SLewanczyk, Dawid }
111208777fb0SLewanczyk, Dawid 
1113b5a76932SEd Tanous inline void populateFanRedundancy(
1114b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
11158bd25ccdSJames Feist {
11168bd25ccdSJames Feist     crow::connections::systemBus->async_method_call(
1117b9d36b47SEd Tanous         [sensorsAsyncResp](
1118b9d36b47SEd Tanous             const boost::system::error_code ec,
1119b9d36b47SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& resp) {
11208bd25ccdSJames Feist             if (ec)
11218bd25ccdSJames Feist             {
11228bd25ccdSJames Feist                 return; // don't have to have this interface
11238bd25ccdSJames Feist             }
1124e278c18fSEd Tanous             for (const std::pair<std::string,
1125e278c18fSEd Tanous                                  std::vector<std::pair<
1126e278c18fSEd Tanous                                      std::string, std::vector<std::string>>>>&
1127e278c18fSEd Tanous                      pathPair : resp)
11288bd25ccdSJames Feist             {
1129e278c18fSEd Tanous                 const std::string& path = pathPair.first;
1130e278c18fSEd Tanous                 const std::vector<
1131e278c18fSEd Tanous                     std::pair<std::string, std::vector<std::string>>>& objDict =
1132e278c18fSEd Tanous                     pathPair.second;
11338bd25ccdSJames Feist                 if (objDict.empty())
11348bd25ccdSJames Feist                 {
11358bd25ccdSJames Feist                     continue; // this should be impossible
11368bd25ccdSJames Feist                 }
11378bd25ccdSJames Feist 
11388bd25ccdSJames Feist                 const std::string& owner = objDict.begin()->first;
11391e1e598dSJonathan Doman                 sdbusplus::asio::getProperty<std::vector<std::string>>(
11401e1e598dSJonathan Doman                     *crow::connections::systemBus,
11411e1e598dSJonathan Doman                     "xyz.openbmc_project.ObjectMapper", path + "/chassis",
11421e1e598dSJonathan Doman                     "xyz.openbmc_project.Association", "endpoints",
11431e1e598dSJonathan Doman                     [path, owner, sensorsAsyncResp](
11441e1e598dSJonathan Doman                         const boost::system::error_code e,
11451e1e598dSJonathan Doman                         const std::vector<std::string>& endpoints) {
1146271584abSEd Tanous                         if (e)
11478bd25ccdSJames Feist                         {
11488bd25ccdSJames Feist                             return; // if they don't have an association we
11498bd25ccdSJames Feist                                     // can't tell what chassis is
11508bd25ccdSJames Feist                         }
11518bd25ccdSJames Feist                         auto found = std::find_if(
11521e1e598dSJonathan Doman                             endpoints.begin(), endpoints.end(),
11538bd25ccdSJames Feist                             [sensorsAsyncResp](const std::string& entry) {
11548bd25ccdSJames Feist                                 return entry.find(
11558bd25ccdSJames Feist                                            sensorsAsyncResp->chassisId) !=
11568bd25ccdSJames Feist                                        std::string::npos;
11578bd25ccdSJames Feist                             });
11588bd25ccdSJames Feist 
11591e1e598dSJonathan Doman                         if (found == endpoints.end())
11608bd25ccdSJames Feist                         {
11618bd25ccdSJames Feist                             return;
11628bd25ccdSJames Feist                         }
11638bd25ccdSJames Feist                         crow::connections::systemBus->async_method_call(
11648bd25ccdSJames Feist                             [path, sensorsAsyncResp](
1165271584abSEd Tanous                                 const boost::system::error_code& err,
11668bd25ccdSJames Feist                                 const boost::container::flat_map<
11678bd25ccdSJames Feist                                     std::string,
1168168e20c1SEd Tanous                                     dbus::utility::DbusVariantType>& ret) {
1169271584abSEd Tanous                                 if (err)
11708bd25ccdSJames Feist                                 {
11718bd25ccdSJames Feist                                     return; // don't have to have this
11728bd25ccdSJames Feist                                             // interface
11738bd25ccdSJames Feist                                 }
11748bd25ccdSJames Feist                                 auto findFailures = ret.find("AllowedFailures");
11758bd25ccdSJames Feist                                 auto findCollection = ret.find("Collection");
11768bd25ccdSJames Feist                                 auto findStatus = ret.find("Status");
11778bd25ccdSJames Feist 
11788bd25ccdSJames Feist                                 if (findFailures == ret.end() ||
11798bd25ccdSJames Feist                                     findCollection == ret.end() ||
11808bd25ccdSJames Feist                                     findStatus == ret.end())
11818bd25ccdSJames Feist                                 {
11828bd25ccdSJames Feist                                     BMCWEB_LOG_ERROR
11838bd25ccdSJames Feist                                         << "Invalid redundancy interface";
11848bd25ccdSJames Feist                                     messages::internalError(
11858d1b46d7Szhanghch05                                         sensorsAsyncResp->asyncResp->res);
11868bd25ccdSJames Feist                                     return;
11878bd25ccdSJames Feist                                 }
11888bd25ccdSJames Feist 
11899eb808c1SEd Tanous                                 const uint8_t* allowedFailures =
11909eb808c1SEd Tanous                                     std::get_if<uint8_t>(
11918bd25ccdSJames Feist                                         &(findFailures->second));
11929eb808c1SEd Tanous                                 const std::vector<std::string>* collection =
11938bd25ccdSJames Feist                                     std::get_if<std::vector<std::string>>(
11948bd25ccdSJames Feist                                         &(findCollection->second));
11959eb808c1SEd Tanous                                 const std::string* status =
11969eb808c1SEd Tanous                                     std::get_if<std::string>(
11978bd25ccdSJames Feist                                         &(findStatus->second));
11988bd25ccdSJames Feist 
11998bd25ccdSJames Feist                                 if (allowedFailures == nullptr ||
12008bd25ccdSJames Feist                                     collection == nullptr || status == nullptr)
12018bd25ccdSJames Feist                                 {
12028bd25ccdSJames Feist 
12038bd25ccdSJames Feist                                     BMCWEB_LOG_ERROR
12040fda0f12SGeorge Liu                                         << "Invalid redundancy interface types";
12058bd25ccdSJames Feist                                     messages::internalError(
12068d1b46d7Szhanghch05                                         sensorsAsyncResp->asyncResp->res);
12078bd25ccdSJames Feist                                     return;
12088bd25ccdSJames Feist                                 }
120928aa8de5SGeorge Liu                                 sdbusplus::message::object_path objectPath(
121028aa8de5SGeorge Liu                                     path);
121128aa8de5SGeorge Liu                                 std::string name = objectPath.filename();
121228aa8de5SGeorge Liu                                 if (name.empty())
12138bd25ccdSJames Feist                                 {
12148bd25ccdSJames Feist                                     // this should be impossible
12158bd25ccdSJames Feist                                     messages::internalError(
12168d1b46d7Szhanghch05                                         sensorsAsyncResp->asyncResp->res);
12178bd25ccdSJames Feist                                     return;
12188bd25ccdSJames Feist                                 }
12198bd25ccdSJames Feist                                 std::replace(name.begin(), name.end(), '_',
12208bd25ccdSJames Feist                                              ' ');
12218bd25ccdSJames Feist 
12228bd25ccdSJames Feist                                 std::string health;
12238bd25ccdSJames Feist 
12248bd25ccdSJames Feist                                 if (boost::ends_with(*status, "Full"))
12258bd25ccdSJames Feist                                 {
12268bd25ccdSJames Feist                                     health = "OK";
12278bd25ccdSJames Feist                                 }
12288bd25ccdSJames Feist                                 else if (boost::ends_with(*status, "Degraded"))
12298bd25ccdSJames Feist                                 {
12308bd25ccdSJames Feist                                     health = "Warning";
12318bd25ccdSJames Feist                                 }
12328bd25ccdSJames Feist                                 else
12338bd25ccdSJames Feist                                 {
12348bd25ccdSJames Feist                                     health = "Critical";
12358bd25ccdSJames Feist                                 }
12368bd25ccdSJames Feist                                 std::vector<nlohmann::json> redfishCollection;
12378bd25ccdSJames Feist                                 const auto& fanRedfish =
12388d1b46d7Szhanghch05                                     sensorsAsyncResp->asyncResp->res
12398d1b46d7Szhanghch05                                         .jsonValue["Fans"];
12408bd25ccdSJames Feist                                 for (const std::string& item : *collection)
12418bd25ccdSJames Feist                                 {
124228aa8de5SGeorge Liu                                     sdbusplus::message::object_path path(item);
124328aa8de5SGeorge Liu                                     std::string itemName = path.filename();
124428aa8de5SGeorge Liu                                     if (itemName.empty())
124528aa8de5SGeorge Liu                                     {
124628aa8de5SGeorge Liu                                         continue;
124728aa8de5SGeorge Liu                                     }
12488bd25ccdSJames Feist                                     /*
12498bd25ccdSJames Feist                                     todo(ed): merge patch that fixes the names
12508bd25ccdSJames Feist                                     std::replace(itemName.begin(),
12518bd25ccdSJames Feist                                                  itemName.end(), '_', ' ');*/
12528bd25ccdSJames Feist                                     auto schemaItem = std::find_if(
12538bd25ccdSJames Feist                                         fanRedfish.begin(), fanRedfish.end(),
12548bd25ccdSJames Feist                                         [itemName](const nlohmann::json& fan) {
12558bd25ccdSJames Feist                                             return fan["MemberId"] == itemName;
12568bd25ccdSJames Feist                                         });
12578bd25ccdSJames Feist                                     if (schemaItem != fanRedfish.end())
12588bd25ccdSJames Feist                                     {
12598bd25ccdSJames Feist                                         redfishCollection.push_back(
12608bd25ccdSJames Feist                                             {{"@odata.id",
12618bd25ccdSJames Feist                                               (*schemaItem)["@odata.id"]}});
12628bd25ccdSJames Feist                                     }
12638bd25ccdSJames Feist                                     else
12648bd25ccdSJames Feist                                     {
12658bd25ccdSJames Feist                                         BMCWEB_LOG_ERROR
12668bd25ccdSJames Feist                                             << "failed to find fan in schema";
12678bd25ccdSJames Feist                                         messages::internalError(
12688d1b46d7Szhanghch05                                             sensorsAsyncResp->asyncResp->res);
12698bd25ccdSJames Feist                                         return;
12708bd25ccdSJames Feist                                     }
12718bd25ccdSJames Feist                                 }
12728bd25ccdSJames Feist 
12733e9e72ebSKuiying Wang                                 size_t minNumNeeded =
127426f6976fSEd Tanous                                     collection->empty()
127526f6976fSEd Tanous                                         ? 0
127626f6976fSEd Tanous                                         : collection->size() - *allowedFailures;
1277271584abSEd Tanous                                 nlohmann::json& jResp =
12788d1b46d7Szhanghch05                                     sensorsAsyncResp->asyncResp->res
12798bd25ccdSJames Feist                                         .jsonValue["Redundancy"];
1280271584abSEd Tanous                                 jResp.push_back(
12818bd25ccdSJames Feist                                     {{"@odata.id",
1282717794d5SAppaRao Puli                                       "/redfish/v1/Chassis/" +
12838bd25ccdSJames Feist                                           sensorsAsyncResp->chassisId + "/" +
12848bd25ccdSJames Feist                                           sensorsAsyncResp->chassisSubNode +
12858bd25ccdSJames Feist                                           "#/Redundancy/" +
1286271584abSEd Tanous                                           std::to_string(jResp.size())},
12878bd25ccdSJames Feist                                      {"@odata.type",
12888bd25ccdSJames Feist                                       "#Redundancy.v1_3_2.Redundancy"},
12893e9e72ebSKuiying Wang                                      {"MinNumNeeded", minNumNeeded},
12908bd25ccdSJames Feist                                      {"MemberId", name},
12918bd25ccdSJames Feist                                      {"Mode", "N+m"},
12928bd25ccdSJames Feist                                      {"Name", name},
12938bd25ccdSJames Feist                                      {"RedundancySet", redfishCollection},
12948bd25ccdSJames Feist                                      {"Status",
12958bd25ccdSJames Feist                                       {{"Health", health},
12968bd25ccdSJames Feist                                        {"State", "Enabled"}}}});
12978bd25ccdSJames Feist                             },
12988bd25ccdSJames Feist                             owner, path, "org.freedesktop.DBus.Properties",
12998bd25ccdSJames Feist                             "GetAll",
13008bd25ccdSJames Feist                             "xyz.openbmc_project.Control.FanRedundancy");
13011e1e598dSJonathan Doman                     });
13028bd25ccdSJames Feist             }
13038bd25ccdSJames Feist         },
13048bd25ccdSJames Feist         "xyz.openbmc_project.ObjectMapper",
13058bd25ccdSJames Feist         "/xyz/openbmc_project/object_mapper",
13068bd25ccdSJames Feist         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
13078bd25ccdSJames Feist         "/xyz/openbmc_project/control", 2,
13088bd25ccdSJames Feist         std::array<const char*, 1>{
13098bd25ccdSJames Feist             "xyz.openbmc_project.Control.FanRedundancy"});
13108bd25ccdSJames Feist }
13118bd25ccdSJames Feist 
1312b5a76932SEd Tanous inline void
131381ce609eSEd Tanous     sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
131449c53ac9SJohnathan Mantey {
13158d1b46d7Szhanghch05     nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue;
131649c53ac9SJohnathan Mantey     std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
131781ce609eSEd Tanous     if (sensorsAsyncResp->chassisSubNode == sensors::node::power)
131849c53ac9SJohnathan Mantey     {
131949c53ac9SJohnathan Mantey         sensorHeaders = {"Voltages", "PowerSupplies"};
132049c53ac9SJohnathan Mantey     }
132149c53ac9SJohnathan Mantey     for (const std::string& sensorGroup : sensorHeaders)
132249c53ac9SJohnathan Mantey     {
132349c53ac9SJohnathan Mantey         nlohmann::json::iterator entry = response.find(sensorGroup);
132449c53ac9SJohnathan Mantey         if (entry != response.end())
132549c53ac9SJohnathan Mantey         {
132649c53ac9SJohnathan Mantey             std::sort(entry->begin(), entry->end(),
132749c53ac9SJohnathan Mantey                       [](nlohmann::json& c1, nlohmann::json& c2) {
132849c53ac9SJohnathan Mantey                           return c1["Name"] < c2["Name"];
132949c53ac9SJohnathan Mantey                       });
133049c53ac9SJohnathan Mantey 
133149c53ac9SJohnathan Mantey             // add the index counts to the end of each entry
133249c53ac9SJohnathan Mantey             size_t count = 0;
133349c53ac9SJohnathan Mantey             for (nlohmann::json& sensorJson : *entry)
133449c53ac9SJohnathan Mantey             {
133549c53ac9SJohnathan Mantey                 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
133649c53ac9SJohnathan Mantey                 if (odata == sensorJson.end())
133749c53ac9SJohnathan Mantey                 {
133849c53ac9SJohnathan Mantey                     continue;
133949c53ac9SJohnathan Mantey                 }
134049c53ac9SJohnathan Mantey                 std::string* value = odata->get_ptr<std::string*>();
134149c53ac9SJohnathan Mantey                 if (value != nullptr)
134249c53ac9SJohnathan Mantey                 {
134349c53ac9SJohnathan Mantey                     *value += std::to_string(count);
134449c53ac9SJohnathan Mantey                     count++;
134581ce609eSEd Tanous                     sensorsAsyncResp->updateUri(sensorJson["Name"], *value);
134649c53ac9SJohnathan Mantey                 }
134749c53ac9SJohnathan Mantey             }
134849c53ac9SJohnathan Mantey         }
134949c53ac9SJohnathan Mantey     }
135049c53ac9SJohnathan Mantey }
135149c53ac9SJohnathan Mantey 
135208777fb0SLewanczyk, Dawid /**
1353adc4f0dbSShawn McCarney  * @brief Finds the inventory item with the specified object path.
1354adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1355adc4f0dbSShawn McCarney  * @param invItemObjPath D-Bus object path of inventory item.
1356adc4f0dbSShawn McCarney  * @return Inventory item within vector, or nullptr if no match found.
13578fb49dd6SShawn McCarney  */
135823a21a1cSEd Tanous inline InventoryItem* findInventoryItem(
1359b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1360adc4f0dbSShawn McCarney     const std::string& invItemObjPath)
13618fb49dd6SShawn McCarney {
1362adc4f0dbSShawn McCarney     for (InventoryItem& inventoryItem : *inventoryItems)
13638fb49dd6SShawn McCarney     {
1364adc4f0dbSShawn McCarney         if (inventoryItem.objectPath == invItemObjPath)
13658fb49dd6SShawn McCarney         {
1366adc4f0dbSShawn McCarney             return &inventoryItem;
13678fb49dd6SShawn McCarney         }
13688fb49dd6SShawn McCarney     }
13698fb49dd6SShawn McCarney     return nullptr;
13708fb49dd6SShawn McCarney }
13718fb49dd6SShawn McCarney 
13728fb49dd6SShawn McCarney /**
1373adc4f0dbSShawn McCarney  * @brief Finds the inventory item associated with the specified sensor.
1374adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1375adc4f0dbSShawn McCarney  * @param sensorObjPath D-Bus object path of sensor.
1376adc4f0dbSShawn McCarney  * @return Inventory item within vector, or nullptr if no match found.
13778fb49dd6SShawn McCarney  */
137823a21a1cSEd Tanous inline InventoryItem* findInventoryItemForSensor(
1379b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1380adc4f0dbSShawn McCarney     const std::string& sensorObjPath)
1381adc4f0dbSShawn McCarney {
1382adc4f0dbSShawn McCarney     for (InventoryItem& inventoryItem : *inventoryItems)
1383adc4f0dbSShawn McCarney     {
1384adc4f0dbSShawn McCarney         if (inventoryItem.sensors.count(sensorObjPath) > 0)
1385adc4f0dbSShawn McCarney         {
1386adc4f0dbSShawn McCarney             return &inventoryItem;
1387adc4f0dbSShawn McCarney         }
1388adc4f0dbSShawn McCarney     }
1389adc4f0dbSShawn McCarney     return nullptr;
1390adc4f0dbSShawn McCarney }
1391adc4f0dbSShawn McCarney 
1392adc4f0dbSShawn McCarney /**
1393d500549bSAnthony Wilson  * @brief Finds the inventory item associated with the specified led path.
1394d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1395d500549bSAnthony Wilson  * @param ledObjPath D-Bus object path of led.
1396d500549bSAnthony Wilson  * @return Inventory item within vector, or nullptr if no match found.
1397d500549bSAnthony Wilson  */
1398d500549bSAnthony Wilson inline InventoryItem*
1399d500549bSAnthony Wilson     findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems,
1400d500549bSAnthony Wilson                             const std::string& ledObjPath)
1401d500549bSAnthony Wilson {
1402d500549bSAnthony Wilson     for (InventoryItem& inventoryItem : inventoryItems)
1403d500549bSAnthony Wilson     {
1404d500549bSAnthony Wilson         if (inventoryItem.ledObjectPath == ledObjPath)
1405d500549bSAnthony Wilson         {
1406d500549bSAnthony Wilson             return &inventoryItem;
1407d500549bSAnthony Wilson         }
1408d500549bSAnthony Wilson     }
1409d500549bSAnthony Wilson     return nullptr;
1410d500549bSAnthony Wilson }
1411d500549bSAnthony Wilson 
1412d500549bSAnthony Wilson /**
1413adc4f0dbSShawn McCarney  * @brief Adds inventory item and associated sensor to specified vector.
1414adc4f0dbSShawn McCarney  *
1415adc4f0dbSShawn McCarney  * Adds a new InventoryItem to the vector if necessary.  Searches for an
1416adc4f0dbSShawn McCarney  * existing InventoryItem with the specified object path.  If not found, one is
1417adc4f0dbSShawn McCarney  * added to the vector.
1418adc4f0dbSShawn McCarney  *
1419adc4f0dbSShawn McCarney  * Next, the specified sensor is added to the set of sensors associated with the
1420adc4f0dbSShawn McCarney  * InventoryItem.
1421adc4f0dbSShawn McCarney  *
1422adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1423adc4f0dbSShawn McCarney  * @param invItemObjPath D-Bus object path of inventory item.
1424adc4f0dbSShawn McCarney  * @param sensorObjPath D-Bus object path of sensor
1425adc4f0dbSShawn McCarney  */
1426b5a76932SEd Tanous inline void addInventoryItem(
1427b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1428b5a76932SEd Tanous     const std::string& invItemObjPath, const std::string& sensorObjPath)
1429adc4f0dbSShawn McCarney {
1430adc4f0dbSShawn McCarney     // Look for inventory item in vector
1431adc4f0dbSShawn McCarney     InventoryItem* inventoryItem =
1432adc4f0dbSShawn McCarney         findInventoryItem(inventoryItems, invItemObjPath);
1433adc4f0dbSShawn McCarney 
1434adc4f0dbSShawn McCarney     // If inventory item doesn't exist in vector, add it
1435adc4f0dbSShawn McCarney     if (inventoryItem == nullptr)
1436adc4f0dbSShawn McCarney     {
1437adc4f0dbSShawn McCarney         inventoryItems->emplace_back(invItemObjPath);
1438adc4f0dbSShawn McCarney         inventoryItem = &(inventoryItems->back());
1439adc4f0dbSShawn McCarney     }
1440adc4f0dbSShawn McCarney 
1441adc4f0dbSShawn McCarney     // Add sensor to set of sensors associated with inventory item
1442adc4f0dbSShawn McCarney     inventoryItem->sensors.emplace(sensorObjPath);
1443adc4f0dbSShawn McCarney }
1444adc4f0dbSShawn McCarney 
1445adc4f0dbSShawn McCarney /**
1446adc4f0dbSShawn McCarney  * @brief Stores D-Bus data in the specified inventory item.
1447adc4f0dbSShawn McCarney  *
1448adc4f0dbSShawn McCarney  * Finds D-Bus data in the specified map of interfaces.  Stores the data in the
1449adc4f0dbSShawn McCarney  * specified InventoryItem.
1450adc4f0dbSShawn McCarney  *
1451adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
1452adc4f0dbSShawn McCarney  * response.
1453adc4f0dbSShawn McCarney  *
1454adc4f0dbSShawn McCarney  * @param inventoryItem Inventory item where data will be stored.
1455adc4f0dbSShawn McCarney  * @param interfacesDict Map containing D-Bus interfaces and their properties
1456adc4f0dbSShawn McCarney  * for the specified inventory item.
1457adc4f0dbSShawn McCarney  */
145823a21a1cSEd Tanous inline void storeInventoryItemData(
1459adc4f0dbSShawn McCarney     InventoryItem& inventoryItem,
1460711ac7a9SEd Tanous     const dbus::utility::DBusInteracesMap& interfacesDict)
14618fb49dd6SShawn McCarney {
1462adc4f0dbSShawn McCarney     // Get properties from Inventory.Item interface
1463711ac7a9SEd Tanous 
14649eb808c1SEd Tanous     for (const auto& [interface, values] : interfacesDict)
14658fb49dd6SShawn McCarney     {
1466711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Item")
14678fb49dd6SShawn McCarney         {
14689eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
1469711ac7a9SEd Tanous             {
1470711ac7a9SEd Tanous                 if (name == "Present")
1471711ac7a9SEd Tanous                 {
1472711ac7a9SEd Tanous                     const bool* value = std::get_if<bool>(&dbusValue);
1473adc4f0dbSShawn McCarney                     if (value != nullptr)
14748fb49dd6SShawn McCarney                     {
1475adc4f0dbSShawn McCarney                         inventoryItem.isPresent = *value;
14768fb49dd6SShawn McCarney                     }
14778fb49dd6SShawn McCarney                 }
14788fb49dd6SShawn McCarney             }
1479711ac7a9SEd Tanous         }
1480adc4f0dbSShawn McCarney         // Check if Inventory.Item.PowerSupply interface is present
1481711ac7a9SEd Tanous 
1482711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Item.PowerSupply")
14838fb49dd6SShawn McCarney         {
1484adc4f0dbSShawn McCarney             inventoryItem.isPowerSupply = true;
14858fb49dd6SShawn McCarney         }
1486adc4f0dbSShawn McCarney 
1487adc4f0dbSShawn McCarney         // Get properties from Inventory.Decorator.Asset interface
1488711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
1489adc4f0dbSShawn McCarney         {
14909eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
1491711ac7a9SEd Tanous             {
1492711ac7a9SEd Tanous                 if (name == "Manufacturer")
1493adc4f0dbSShawn McCarney                 {
1494adc4f0dbSShawn McCarney                     const std::string* value =
1495711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
1496adc4f0dbSShawn McCarney                     if (value != nullptr)
1497adc4f0dbSShawn McCarney                     {
1498adc4f0dbSShawn McCarney                         inventoryItem.manufacturer = *value;
1499adc4f0dbSShawn McCarney                     }
1500adc4f0dbSShawn McCarney                 }
1501711ac7a9SEd Tanous                 if (name == "Model")
1502adc4f0dbSShawn McCarney                 {
1503adc4f0dbSShawn McCarney                     const std::string* value =
1504711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
1505adc4f0dbSShawn McCarney                     if (value != nullptr)
1506adc4f0dbSShawn McCarney                     {
1507adc4f0dbSShawn McCarney                         inventoryItem.model = *value;
1508adc4f0dbSShawn McCarney                     }
1509adc4f0dbSShawn McCarney                 }
1510711ac7a9SEd Tanous                 if (name == "SerialNumber")
1511adc4f0dbSShawn McCarney                 {
1512adc4f0dbSShawn McCarney                     const std::string* value =
1513711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
1514adc4f0dbSShawn McCarney                     if (value != nullptr)
1515adc4f0dbSShawn McCarney                     {
1516adc4f0dbSShawn McCarney                         inventoryItem.serialNumber = *value;
1517adc4f0dbSShawn McCarney                     }
1518adc4f0dbSShawn McCarney                 }
1519711ac7a9SEd Tanous                 if (name == "PartNumber")
1520711ac7a9SEd Tanous                 {
1521711ac7a9SEd Tanous                     const std::string* value =
1522711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
1523711ac7a9SEd Tanous                     if (value != nullptr)
1524711ac7a9SEd Tanous                     {
1525711ac7a9SEd Tanous                         inventoryItem.partNumber = *value;
1526711ac7a9SEd Tanous                     }
1527711ac7a9SEd Tanous                 }
1528711ac7a9SEd Tanous             }
1529adc4f0dbSShawn McCarney         }
1530adc4f0dbSShawn McCarney 
1531711ac7a9SEd Tanous         if (interface ==
1532711ac7a9SEd Tanous             "xyz.openbmc_project.State.Decorator.OperationalStatus")
1533adc4f0dbSShawn McCarney         {
15349eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
1535adc4f0dbSShawn McCarney             {
1536711ac7a9SEd Tanous                 if (name == "Functional")
1537711ac7a9SEd Tanous                 {
1538711ac7a9SEd Tanous                     const bool* value = std::get_if<bool>(&dbusValue);
1539adc4f0dbSShawn McCarney                     if (value != nullptr)
1540adc4f0dbSShawn McCarney                     {
1541adc4f0dbSShawn McCarney                         inventoryItem.isFunctional = *value;
15428fb49dd6SShawn McCarney                     }
15438fb49dd6SShawn McCarney                 }
15448fb49dd6SShawn McCarney             }
15458fb49dd6SShawn McCarney         }
1546711ac7a9SEd Tanous     }
1547711ac7a9SEd Tanous }
15488fb49dd6SShawn McCarney 
15498fb49dd6SShawn McCarney /**
1550adc4f0dbSShawn McCarney  * @brief Gets D-Bus data for inventory items associated with sensors.
15518fb49dd6SShawn McCarney  *
1552adc4f0dbSShawn McCarney  * Uses the specified connections (services) to obtain D-Bus data for inventory
1553adc4f0dbSShawn McCarney  * items associated with sensors.  Stores the resulting data in the
1554adc4f0dbSShawn McCarney  * inventoryItems vector.
15558fb49dd6SShawn McCarney  *
1556adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
1557adc4f0dbSShawn McCarney  * response.
1558adc4f0dbSShawn McCarney  *
1559adc4f0dbSShawn McCarney  * Finds the inventory item data asynchronously.  Invokes callback when data has
1560adc4f0dbSShawn McCarney  * been obtained.
1561adc4f0dbSShawn McCarney  *
1562adc4f0dbSShawn McCarney  * The callback must have the following signature:
1563adc4f0dbSShawn McCarney  *   @code
1564d500549bSAnthony Wilson  *   callback(void)
1565adc4f0dbSShawn McCarney  *   @endcode
1566adc4f0dbSShawn McCarney  *
1567adc4f0dbSShawn McCarney  * This function is called recursively, obtaining data asynchronously from one
1568adc4f0dbSShawn McCarney  * connection in each call.  This ensures the callback is not invoked until the
1569adc4f0dbSShawn McCarney  * last asynchronous function has completed.
15708fb49dd6SShawn McCarney  *
15718fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
1572adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1573adc4f0dbSShawn McCarney  * @param invConnections Connections that provide data for the inventory items.
15748fb49dd6SShawn McCarney  * @param objectMgrPaths Mappings from connection name to DBus object path that
15758fb49dd6SShawn McCarney  * implements ObjectManager.
1576adc4f0dbSShawn McCarney  * @param callback Callback to invoke when inventory data has been obtained.
1577adc4f0dbSShawn McCarney  * @param invConnectionsIndex Current index in invConnections.  Only specified
1578adc4f0dbSShawn McCarney  * in recursive calls to this function.
15798fb49dd6SShawn McCarney  */
1580adc4f0dbSShawn McCarney template <typename Callback>
1581adc4f0dbSShawn McCarney static void getInventoryItemsData(
15828fb49dd6SShawn McCarney     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1583adc4f0dbSShawn McCarney     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
15848fb49dd6SShawn McCarney     std::shared_ptr<boost::container::flat_set<std::string>> invConnections,
15858fb49dd6SShawn McCarney     std::shared_ptr<boost::container::flat_map<std::string, std::string>>
1586adc4f0dbSShawn McCarney         objectMgrPaths,
1587271584abSEd Tanous     Callback&& callback, size_t invConnectionsIndex = 0)
15888fb49dd6SShawn McCarney {
1589adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsData enter";
15908fb49dd6SShawn McCarney 
1591adc4f0dbSShawn McCarney     // If no more connections left, call callback
1592adc4f0dbSShawn McCarney     if (invConnectionsIndex >= invConnections->size())
15938fb49dd6SShawn McCarney     {
1594d500549bSAnthony Wilson         callback();
1595adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
1596adc4f0dbSShawn McCarney         return;
1597adc4f0dbSShawn McCarney     }
1598adc4f0dbSShawn McCarney 
1599adc4f0dbSShawn McCarney     // Get inventory item data from current connection
1600adc4f0dbSShawn McCarney     auto it = invConnections->nth(invConnectionsIndex);
1601adc4f0dbSShawn McCarney     if (it != invConnections->end())
1602adc4f0dbSShawn McCarney     {
1603adc4f0dbSShawn McCarney         const std::string& invConnection = *it;
1604adc4f0dbSShawn McCarney 
16058fb49dd6SShawn McCarney         // Response handler for GetManagedObjects
1606adc4f0dbSShawn McCarney         auto respHandler = [sensorsAsyncResp, inventoryItems, invConnections,
1607f94c4ecfSEd Tanous                             objectMgrPaths,
1608f94c4ecfSEd Tanous                             callback{std::forward<Callback>(callback)},
1609adc4f0dbSShawn McCarney                             invConnectionsIndex](
1610adc4f0dbSShawn McCarney                                const boost::system::error_code ec,
1611711ac7a9SEd Tanous                                dbus::utility::ManagedObjectType& resp) {
1612adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler enter";
16138fb49dd6SShawn McCarney             if (ec)
16148fb49dd6SShawn McCarney             {
16158fb49dd6SShawn McCarney                 BMCWEB_LOG_ERROR
1616adc4f0dbSShawn McCarney                     << "getInventoryItemsData respHandler DBus error " << ec;
16178d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
16188fb49dd6SShawn McCarney                 return;
16198fb49dd6SShawn McCarney             }
16208fb49dd6SShawn McCarney 
16218fb49dd6SShawn McCarney             // Loop through returned object paths
16228fb49dd6SShawn McCarney             for (const auto& objDictEntry : resp)
16238fb49dd6SShawn McCarney             {
16248fb49dd6SShawn McCarney                 const std::string& objPath =
16258fb49dd6SShawn McCarney                     static_cast<const std::string&>(objDictEntry.first);
16268fb49dd6SShawn McCarney 
1627adc4f0dbSShawn McCarney                 // If this object path is one of the specified inventory items
1628adc4f0dbSShawn McCarney                 InventoryItem* inventoryItem =
1629adc4f0dbSShawn McCarney                     findInventoryItem(inventoryItems, objPath);
1630adc4f0dbSShawn McCarney                 if (inventoryItem != nullptr)
16318fb49dd6SShawn McCarney                 {
1632adc4f0dbSShawn McCarney                     // Store inventory data in InventoryItem
1633adc4f0dbSShawn McCarney                     storeInventoryItemData(*inventoryItem, objDictEntry.second);
16348fb49dd6SShawn McCarney                 }
16358fb49dd6SShawn McCarney             }
16368fb49dd6SShawn McCarney 
1637adc4f0dbSShawn McCarney             // Recurse to get inventory item data from next connection
1638adc4f0dbSShawn McCarney             getInventoryItemsData(sensorsAsyncResp, inventoryItems,
1639adc4f0dbSShawn McCarney                                   invConnections, objectMgrPaths,
1640adc4f0dbSShawn McCarney                                   std::move(callback), invConnectionsIndex + 1);
1641adc4f0dbSShawn McCarney 
1642adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler exit";
16438fb49dd6SShawn McCarney         };
16448fb49dd6SShawn McCarney 
16458fb49dd6SShawn McCarney         // Find DBus object path that implements ObjectManager for the current
16468fb49dd6SShawn McCarney         // connection.  If no mapping found, default to "/".
16478fb49dd6SShawn McCarney         auto iter = objectMgrPaths->find(invConnection);
16488fb49dd6SShawn McCarney         const std::string& objectMgrPath =
16498fb49dd6SShawn McCarney             (iter != objectMgrPaths->end()) ? iter->second : "/";
16508fb49dd6SShawn McCarney         BMCWEB_LOG_DEBUG << "ObjectManager path for " << invConnection << " is "
16518fb49dd6SShawn McCarney                          << objectMgrPath;
16528fb49dd6SShawn McCarney 
16538fb49dd6SShawn McCarney         // Get all object paths and their interfaces for current connection
16548fb49dd6SShawn McCarney         crow::connections::systemBus->async_method_call(
16558fb49dd6SShawn McCarney             std::move(respHandler), invConnection, objectMgrPath,
16568fb49dd6SShawn McCarney             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
16578fb49dd6SShawn McCarney     }
16588fb49dd6SShawn McCarney 
1659adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
16608fb49dd6SShawn McCarney }
16618fb49dd6SShawn McCarney 
16628fb49dd6SShawn McCarney /**
1663adc4f0dbSShawn McCarney  * @brief Gets connections that provide D-Bus data for inventory items.
16648fb49dd6SShawn McCarney  *
1665adc4f0dbSShawn McCarney  * Gets the D-Bus connections (services) that provide data for the inventory
1666adc4f0dbSShawn McCarney  * items that are associated with sensors.
16678fb49dd6SShawn McCarney  *
16688fb49dd6SShawn McCarney  * Finds the connections asynchronously.  Invokes callback when information has
16698fb49dd6SShawn McCarney  * been obtained.
16708fb49dd6SShawn McCarney  *
16718fb49dd6SShawn McCarney  * The callback must have the following signature:
16728fb49dd6SShawn McCarney  *   @code
16738fb49dd6SShawn McCarney  *   callback(std::shared_ptr<boost::container::flat_set<std::string>>
16748fb49dd6SShawn McCarney  *            invConnections)
16758fb49dd6SShawn McCarney  *   @endcode
16768fb49dd6SShawn McCarney  *
16778fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
1678adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
16798fb49dd6SShawn McCarney  * @param callback Callback to invoke when connections have been obtained.
16808fb49dd6SShawn McCarney  */
16818fb49dd6SShawn McCarney template <typename Callback>
16828fb49dd6SShawn McCarney static void getInventoryItemsConnections(
1683b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1684b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
16858fb49dd6SShawn McCarney     Callback&& callback)
16868fb49dd6SShawn McCarney {
16878fb49dd6SShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsConnections enter";
16888fb49dd6SShawn McCarney 
16898fb49dd6SShawn McCarney     const std::string path = "/xyz/openbmc_project/inventory";
1690adc4f0dbSShawn McCarney     const std::array<std::string, 4> interfaces = {
16918fb49dd6SShawn McCarney         "xyz.openbmc_project.Inventory.Item",
1692adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Item.PowerSupply",
1693adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Decorator.Asset",
16948fb49dd6SShawn McCarney         "xyz.openbmc_project.State.Decorator.OperationalStatus"};
16958fb49dd6SShawn McCarney 
16968fb49dd6SShawn McCarney     // Response handler for parsing output from GetSubTree
1697f94c4ecfSEd Tanous     auto respHandler = [callback{std::forward<Callback>(callback)},
1698b9d36b47SEd Tanous                         sensorsAsyncResp, inventoryItems](
1699b9d36b47SEd Tanous                            const boost::system::error_code ec,
1700b9d36b47SEd Tanous                            const dbus::utility::MapperGetSubTreeResponse&
1701b9d36b47SEd Tanous                                subtree) {
17028fb49dd6SShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler enter";
17038fb49dd6SShawn McCarney         if (ec)
17048fb49dd6SShawn McCarney         {
17058d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
17068fb49dd6SShawn McCarney             BMCWEB_LOG_ERROR
17078fb49dd6SShawn McCarney                 << "getInventoryItemsConnections respHandler DBus error " << ec;
17088fb49dd6SShawn McCarney             return;
17098fb49dd6SShawn McCarney         }
17108fb49dd6SShawn McCarney 
17118fb49dd6SShawn McCarney         // Make unique list of connections for desired inventory items
17128fb49dd6SShawn McCarney         std::shared_ptr<boost::container::flat_set<std::string>>
17138fb49dd6SShawn McCarney             invConnections =
17148fb49dd6SShawn McCarney                 std::make_shared<boost::container::flat_set<std::string>>();
17158fb49dd6SShawn McCarney         invConnections->reserve(8);
17168fb49dd6SShawn McCarney 
17178fb49dd6SShawn McCarney         // Loop through objects from GetSubTree
17188fb49dd6SShawn McCarney         for (const std::pair<
17198fb49dd6SShawn McCarney                  std::string,
17208fb49dd6SShawn McCarney                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
17218fb49dd6SShawn McCarney                  object : subtree)
17228fb49dd6SShawn McCarney         {
1723adc4f0dbSShawn McCarney             // Check if object path is one of the specified inventory items
17248fb49dd6SShawn McCarney             const std::string& objPath = object.first;
1725adc4f0dbSShawn McCarney             if (findInventoryItem(inventoryItems, objPath) != nullptr)
17268fb49dd6SShawn McCarney             {
17278fb49dd6SShawn McCarney                 // Store all connections to inventory item
17288fb49dd6SShawn McCarney                 for (const std::pair<std::string, std::vector<std::string>>&
17298fb49dd6SShawn McCarney                          objData : object.second)
17308fb49dd6SShawn McCarney                 {
17318fb49dd6SShawn McCarney                     const std::string& invConnection = objData.first;
17328fb49dd6SShawn McCarney                     invConnections->insert(invConnection);
17338fb49dd6SShawn McCarney                 }
17348fb49dd6SShawn McCarney             }
17358fb49dd6SShawn McCarney         }
1736d500549bSAnthony Wilson 
17378fb49dd6SShawn McCarney         callback(invConnections);
17388fb49dd6SShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler exit";
17398fb49dd6SShawn McCarney     };
17408fb49dd6SShawn McCarney 
17418fb49dd6SShawn McCarney     // Make call to ObjectMapper to find all inventory items
17428fb49dd6SShawn McCarney     crow::connections::systemBus->async_method_call(
17438fb49dd6SShawn McCarney         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
17448fb49dd6SShawn McCarney         "/xyz/openbmc_project/object_mapper",
17458fb49dd6SShawn McCarney         "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
17468fb49dd6SShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsConnections exit";
17478fb49dd6SShawn McCarney }
17488fb49dd6SShawn McCarney 
17498fb49dd6SShawn McCarney /**
1750adc4f0dbSShawn McCarney  * @brief Gets associations from sensors to inventory items.
17518fb49dd6SShawn McCarney  *
17528fb49dd6SShawn McCarney  * Looks for ObjectMapper associations from the specified sensors to related
1753d500549bSAnthony Wilson  * inventory items. Then finds the associations from those inventory items to
1754d500549bSAnthony Wilson  * their LEDs, if any.
17558fb49dd6SShawn McCarney  *
17568fb49dd6SShawn McCarney  * Finds the inventory items asynchronously.  Invokes callback when information
17578fb49dd6SShawn McCarney  * has been obtained.
17588fb49dd6SShawn McCarney  *
17598fb49dd6SShawn McCarney  * The callback must have the following signature:
17608fb49dd6SShawn McCarney  *   @code
1761adc4f0dbSShawn McCarney  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
17628fb49dd6SShawn McCarney  *   @endcode
17638fb49dd6SShawn McCarney  *
17648fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
17658fb49dd6SShawn McCarney  * @param sensorNames All sensors within the current chassis.
17668fb49dd6SShawn McCarney  * @param objectMgrPaths Mappings from connection name to DBus object path that
17678fb49dd6SShawn McCarney  * implements ObjectManager.
17688fb49dd6SShawn McCarney  * @param callback Callback to invoke when inventory items have been obtained.
17698fb49dd6SShawn McCarney  */
17708fb49dd6SShawn McCarney template <typename Callback>
1771adc4f0dbSShawn McCarney static void getInventoryItemAssociations(
1772b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1773b5a76932SEd Tanous     const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames,
1774b5a76932SEd Tanous     const std::shared_ptr<boost::container::flat_map<std::string, std::string>>&
17758fb49dd6SShawn McCarney         objectMgrPaths,
17768fb49dd6SShawn McCarney     Callback&& callback)
17778fb49dd6SShawn McCarney {
1778adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemAssociations enter";
17798fb49dd6SShawn McCarney 
17808fb49dd6SShawn McCarney     // Response handler for GetManagedObjects
1781f94c4ecfSEd Tanous     auto respHandler = [callback{std::forward<Callback>(callback)},
1782f94c4ecfSEd Tanous                         sensorsAsyncResp,
17838fb49dd6SShawn McCarney                         sensorNames](const boost::system::error_code ec,
17848fb49dd6SShawn McCarney                                      dbus::utility::ManagedObjectType& resp) {
1785adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler enter";
17868fb49dd6SShawn McCarney         if (ec)
17878fb49dd6SShawn McCarney         {
1788adc4f0dbSShawn McCarney             BMCWEB_LOG_ERROR
1789adc4f0dbSShawn McCarney                 << "getInventoryItemAssociations respHandler DBus error " << ec;
17908d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
17918fb49dd6SShawn McCarney             return;
17928fb49dd6SShawn McCarney         }
17938fb49dd6SShawn McCarney 
1794adc4f0dbSShawn McCarney         // Create vector to hold list of inventory items
1795adc4f0dbSShawn McCarney         std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1796adc4f0dbSShawn McCarney             std::make_shared<std::vector<InventoryItem>>();
1797adc4f0dbSShawn McCarney 
17988fb49dd6SShawn McCarney         // Loop through returned object paths
17998fb49dd6SShawn McCarney         std::string sensorAssocPath;
18008fb49dd6SShawn McCarney         sensorAssocPath.reserve(128); // avoid memory allocations
18018fb49dd6SShawn McCarney         for (const auto& objDictEntry : resp)
18028fb49dd6SShawn McCarney         {
18038fb49dd6SShawn McCarney             const std::string& objPath =
18048fb49dd6SShawn McCarney                 static_cast<const std::string&>(objDictEntry.first);
18058fb49dd6SShawn McCarney 
18068fb49dd6SShawn McCarney             // If path is inventory association for one of the specified sensors
18078fb49dd6SShawn McCarney             for (const std::string& sensorName : *sensorNames)
18088fb49dd6SShawn McCarney             {
18098fb49dd6SShawn McCarney                 sensorAssocPath = sensorName;
18108fb49dd6SShawn McCarney                 sensorAssocPath += "/inventory";
18118fb49dd6SShawn McCarney                 if (objPath == sensorAssocPath)
18128fb49dd6SShawn McCarney                 {
18138fb49dd6SShawn McCarney                     // Get Association interface for object path
1814711ac7a9SEd Tanous                     for (const auto& [interface, values] : objDictEntry.second)
18158fb49dd6SShawn McCarney                     {
1816711ac7a9SEd Tanous                         if (interface == "xyz.openbmc_project.Association")
1817711ac7a9SEd Tanous                         {
1818711ac7a9SEd Tanous                             for (const auto& [valueName, value] : values)
1819711ac7a9SEd Tanous                             {
1820711ac7a9SEd Tanous                                 if (valueName == "endpoints")
18218fb49dd6SShawn McCarney                                 {
18228fb49dd6SShawn McCarney                                     const std::vector<std::string>* endpoints =
18238fb49dd6SShawn McCarney                                         std::get_if<std::vector<std::string>>(
1824711ac7a9SEd Tanous                                             &value);
1825711ac7a9SEd Tanous                                     if ((endpoints != nullptr) &&
1826711ac7a9SEd Tanous                                         !endpoints->empty())
18278fb49dd6SShawn McCarney                                     {
1828adc4f0dbSShawn McCarney                                         // Add inventory item to vector
1829adc4f0dbSShawn McCarney                                         const std::string& invItemPath =
1830adc4f0dbSShawn McCarney                                             endpoints->front();
1831711ac7a9SEd Tanous                                         addInventoryItem(inventoryItems,
1832711ac7a9SEd Tanous                                                          invItemPath,
1833adc4f0dbSShawn McCarney                                                          sensorName);
18348fb49dd6SShawn McCarney                                     }
18358fb49dd6SShawn McCarney                                 }
18368fb49dd6SShawn McCarney                             }
1837711ac7a9SEd Tanous                         }
1838711ac7a9SEd Tanous                     }
18398fb49dd6SShawn McCarney                     break;
18408fb49dd6SShawn McCarney                 }
18418fb49dd6SShawn McCarney             }
18428fb49dd6SShawn McCarney         }
18438fb49dd6SShawn McCarney 
1844d500549bSAnthony Wilson         // Now loop through the returned object paths again, this time to
1845d500549bSAnthony Wilson         // find the leds associated with the inventory items we just found
1846d500549bSAnthony Wilson         std::string inventoryAssocPath;
1847d500549bSAnthony Wilson         inventoryAssocPath.reserve(128); // avoid memory allocations
1848d500549bSAnthony Wilson         for (const auto& objDictEntry : resp)
1849d500549bSAnthony Wilson         {
1850d500549bSAnthony Wilson             const std::string& objPath =
1851d500549bSAnthony Wilson                 static_cast<const std::string&>(objDictEntry.first);
1852d500549bSAnthony Wilson 
1853d500549bSAnthony Wilson             for (InventoryItem& inventoryItem : *inventoryItems)
1854d500549bSAnthony Wilson             {
1855d500549bSAnthony Wilson                 inventoryAssocPath = inventoryItem.objectPath;
1856d500549bSAnthony Wilson                 inventoryAssocPath += "/leds";
1857d500549bSAnthony Wilson                 if (objPath == inventoryAssocPath)
1858d500549bSAnthony Wilson                 {
1859711ac7a9SEd Tanous                     for (const auto& [interface, values] : objDictEntry.second)
1860d500549bSAnthony Wilson                     {
1861711ac7a9SEd Tanous                         if (interface == "xyz.openbmc_project.Association")
1862711ac7a9SEd Tanous                         {
1863711ac7a9SEd Tanous                             for (const auto& [valueName, value] : values)
1864711ac7a9SEd Tanous                             {
1865711ac7a9SEd Tanous                                 if (valueName == "endpoints")
1866d500549bSAnthony Wilson                                 {
1867d500549bSAnthony Wilson                                     const std::vector<std::string>* endpoints =
1868d500549bSAnthony Wilson                                         std::get_if<std::vector<std::string>>(
1869711ac7a9SEd Tanous                                             &value);
1870711ac7a9SEd Tanous                                     if ((endpoints != nullptr) &&
1871711ac7a9SEd Tanous                                         !endpoints->empty())
1872d500549bSAnthony Wilson                                     {
1873711ac7a9SEd Tanous                                         // Add inventory item to vector
1874d500549bSAnthony Wilson                                         // Store LED path in inventory item
1875711ac7a9SEd Tanous                                         const std::string& ledPath =
1876711ac7a9SEd Tanous                                             endpoints->front();
1877d500549bSAnthony Wilson                                         inventoryItem.ledObjectPath = ledPath;
1878d500549bSAnthony Wilson                                     }
1879d500549bSAnthony Wilson                                 }
1880d500549bSAnthony Wilson                             }
1881711ac7a9SEd Tanous                         }
1882711ac7a9SEd Tanous                     }
1883711ac7a9SEd Tanous 
1884d500549bSAnthony Wilson                     break;
1885d500549bSAnthony Wilson                 }
1886d500549bSAnthony Wilson             }
1887d500549bSAnthony Wilson         }
1888adc4f0dbSShawn McCarney         callback(inventoryItems);
1889adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler exit";
18908fb49dd6SShawn McCarney     };
18918fb49dd6SShawn McCarney 
18928fb49dd6SShawn McCarney     // Find DBus object path that implements ObjectManager for ObjectMapper
18938fb49dd6SShawn McCarney     std::string connection = "xyz.openbmc_project.ObjectMapper";
18948fb49dd6SShawn McCarney     auto iter = objectMgrPaths->find(connection);
18958fb49dd6SShawn McCarney     const std::string& objectMgrPath =
18968fb49dd6SShawn McCarney         (iter != objectMgrPaths->end()) ? iter->second : "/";
18978fb49dd6SShawn McCarney     BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
18988fb49dd6SShawn McCarney                      << objectMgrPath;
18998fb49dd6SShawn McCarney 
19008fb49dd6SShawn McCarney     // Call GetManagedObjects on the ObjectMapper to get all associations
19018fb49dd6SShawn McCarney     crow::connections::systemBus->async_method_call(
19028fb49dd6SShawn McCarney         std::move(respHandler), connection, objectMgrPath,
19038fb49dd6SShawn McCarney         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
19048fb49dd6SShawn McCarney 
1905adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemAssociations exit";
19068fb49dd6SShawn McCarney }
19078fb49dd6SShawn McCarney 
19088fb49dd6SShawn McCarney /**
1909d500549bSAnthony Wilson  * @brief Gets D-Bus data for inventory item leds associated with sensors.
1910d500549bSAnthony Wilson  *
1911d500549bSAnthony Wilson  * Uses the specified connections (services) to obtain D-Bus data for inventory
1912d500549bSAnthony Wilson  * item leds associated with sensors.  Stores the resulting data in the
1913d500549bSAnthony Wilson  * inventoryItems vector.
1914d500549bSAnthony Wilson  *
1915d500549bSAnthony Wilson  * This data is later used to provide sensor property values in the JSON
1916d500549bSAnthony Wilson  * response.
1917d500549bSAnthony Wilson  *
1918d500549bSAnthony Wilson  * Finds the inventory item led data asynchronously.  Invokes callback when data
1919d500549bSAnthony Wilson  * has been obtained.
1920d500549bSAnthony Wilson  *
1921d500549bSAnthony Wilson  * The callback must have the following signature:
1922d500549bSAnthony Wilson  *   @code
192342cbe538SGunnar Mills  *   callback()
1924d500549bSAnthony Wilson  *   @endcode
1925d500549bSAnthony Wilson  *
1926d500549bSAnthony Wilson  * This function is called recursively, obtaining data asynchronously from one
1927d500549bSAnthony Wilson  * connection in each call.  This ensures the callback is not invoked until the
1928d500549bSAnthony Wilson  * last asynchronous function has completed.
1929d500549bSAnthony Wilson  *
1930d500549bSAnthony Wilson  * @param sensorsAsyncResp Pointer to object holding response data.
1931d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1932d500549bSAnthony Wilson  * @param ledConnections Connections that provide data for the inventory leds.
1933d500549bSAnthony Wilson  * @param callback Callback to invoke when inventory data has been obtained.
1934d500549bSAnthony Wilson  * @param ledConnectionsIndex Current index in ledConnections.  Only specified
1935d500549bSAnthony Wilson  * in recursive calls to this function.
1936d500549bSAnthony Wilson  */
1937d500549bSAnthony Wilson template <typename Callback>
1938d500549bSAnthony Wilson void getInventoryLedData(
1939d500549bSAnthony Wilson     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1940d500549bSAnthony Wilson     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1941d500549bSAnthony Wilson     std::shared_ptr<boost::container::flat_map<std::string, std::string>>
1942d500549bSAnthony Wilson         ledConnections,
1943d500549bSAnthony Wilson     Callback&& callback, size_t ledConnectionsIndex = 0)
1944d500549bSAnthony Wilson {
1945d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLedData enter";
1946d500549bSAnthony Wilson 
1947d500549bSAnthony Wilson     // If no more connections left, call callback
1948d500549bSAnthony Wilson     if (ledConnectionsIndex >= ledConnections->size())
1949d500549bSAnthony Wilson     {
195042cbe538SGunnar Mills         callback();
1951d500549bSAnthony Wilson         BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1952d500549bSAnthony Wilson         return;
1953d500549bSAnthony Wilson     }
1954d500549bSAnthony Wilson 
1955d500549bSAnthony Wilson     // Get inventory item data from current connection
1956d500549bSAnthony Wilson     auto it = ledConnections->nth(ledConnectionsIndex);
1957d500549bSAnthony Wilson     if (it != ledConnections->end())
1958d500549bSAnthony Wilson     {
1959d500549bSAnthony Wilson         const std::string& ledPath = (*it).first;
1960d500549bSAnthony Wilson         const std::string& ledConnection = (*it).second;
1961d500549bSAnthony Wilson         // Response handler for Get State property
19621e1e598dSJonathan Doman         auto respHandler =
19631e1e598dSJonathan Doman             [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
1964f94c4ecfSEd Tanous              callback{std::forward<Callback>(callback)}, ledConnectionsIndex](
19651e1e598dSJonathan Doman                 const boost::system::error_code ec, const std::string& state) {
1966d500549bSAnthony Wilson                 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler enter";
1967d500549bSAnthony Wilson                 if (ec)
1968d500549bSAnthony Wilson                 {
1969d500549bSAnthony Wilson                     BMCWEB_LOG_ERROR
1970d500549bSAnthony Wilson                         << "getInventoryLedData respHandler DBus error " << ec;
19718d1b46d7Szhanghch05                     messages::internalError(sensorsAsyncResp->asyncResp->res);
1972d500549bSAnthony Wilson                     return;
1973d500549bSAnthony Wilson                 }
1974d500549bSAnthony Wilson 
19751e1e598dSJonathan Doman                 BMCWEB_LOG_DEBUG << "Led state: " << state;
1976d500549bSAnthony Wilson                 // Find inventory item with this LED object path
1977d500549bSAnthony Wilson                 InventoryItem* inventoryItem =
1978d500549bSAnthony Wilson                     findInventoryItemForLed(*inventoryItems, ledPath);
1979d500549bSAnthony Wilson                 if (inventoryItem != nullptr)
1980d500549bSAnthony Wilson                 {
1981d500549bSAnthony Wilson                     // Store LED state in InventoryItem
19821e1e598dSJonathan Doman                     if (boost::ends_with(state, "On"))
1983d500549bSAnthony Wilson                     {
1984d500549bSAnthony Wilson                         inventoryItem->ledState = LedState::ON;
1985d500549bSAnthony Wilson                     }
19861e1e598dSJonathan Doman                     else if (boost::ends_with(state, "Blink"))
1987d500549bSAnthony Wilson                     {
1988d500549bSAnthony Wilson                         inventoryItem->ledState = LedState::BLINK;
1989d500549bSAnthony Wilson                     }
19901e1e598dSJonathan Doman                     else if (boost::ends_with(state, "Off"))
1991d500549bSAnthony Wilson                     {
1992d500549bSAnthony Wilson                         inventoryItem->ledState = LedState::OFF;
1993d500549bSAnthony Wilson                     }
1994d500549bSAnthony Wilson                     else
1995d500549bSAnthony Wilson                     {
1996d500549bSAnthony Wilson                         inventoryItem->ledState = LedState::UNKNOWN;
1997d500549bSAnthony Wilson                     }
1998d500549bSAnthony Wilson                 }
1999d500549bSAnthony Wilson 
2000d500549bSAnthony Wilson                 // Recurse to get LED data from next connection
2001d500549bSAnthony Wilson                 getInventoryLedData(sensorsAsyncResp, inventoryItems,
2002d500549bSAnthony Wilson                                     ledConnections, std::move(callback),
2003d500549bSAnthony Wilson                                     ledConnectionsIndex + 1);
2004d500549bSAnthony Wilson 
2005d500549bSAnthony Wilson                 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler exit";
2006d500549bSAnthony Wilson             };
2007d500549bSAnthony Wilson 
2008d500549bSAnthony Wilson         // Get the State property for the current LED
20091e1e598dSJonathan Doman         sdbusplus::asio::getProperty<std::string>(
20101e1e598dSJonathan Doman             *crow::connections::systemBus, ledConnection, ledPath,
20111e1e598dSJonathan Doman             "xyz.openbmc_project.Led.Physical", "State",
20121e1e598dSJonathan Doman             std::move(respHandler));
2013d500549bSAnthony Wilson     }
2014d500549bSAnthony Wilson 
2015d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
2016d500549bSAnthony Wilson }
2017d500549bSAnthony Wilson 
2018d500549bSAnthony Wilson /**
2019d500549bSAnthony Wilson  * @brief Gets LED data for LEDs associated with given inventory items.
2020d500549bSAnthony Wilson  *
2021d500549bSAnthony Wilson  * Gets the D-Bus connections (services) that provide LED data for the LEDs
2022d500549bSAnthony Wilson  * associated with the specified inventory items.  Then gets the LED data from
2023d500549bSAnthony Wilson  * each connection and stores it in the inventory item.
2024d500549bSAnthony Wilson  *
2025d500549bSAnthony Wilson  * This data is later used to provide sensor property values in the JSON
2026d500549bSAnthony Wilson  * response.
2027d500549bSAnthony Wilson  *
2028d500549bSAnthony Wilson  * Finds the LED data asynchronously.  Invokes callback when information has
2029d500549bSAnthony Wilson  * been obtained.
2030d500549bSAnthony Wilson  *
2031d500549bSAnthony Wilson  * The callback must have the following signature:
2032d500549bSAnthony Wilson  *   @code
203342cbe538SGunnar Mills  *   callback()
2034d500549bSAnthony Wilson  *   @endcode
2035d500549bSAnthony Wilson  *
2036d500549bSAnthony Wilson  * @param sensorsAsyncResp Pointer to object holding response data.
2037d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
2038d500549bSAnthony Wilson  * @param callback Callback to invoke when inventory items have been obtained.
2039d500549bSAnthony Wilson  */
2040d500549bSAnthony Wilson template <typename Callback>
2041d500549bSAnthony Wilson void getInventoryLeds(
2042d500549bSAnthony Wilson     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2043d500549bSAnthony Wilson     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2044d500549bSAnthony Wilson     Callback&& callback)
2045d500549bSAnthony Wilson {
2046d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLeds enter";
2047d500549bSAnthony Wilson 
2048d500549bSAnthony Wilson     const std::string path = "/xyz/openbmc_project";
2049d500549bSAnthony Wilson     const std::array<std::string, 1> interfaces = {
2050d500549bSAnthony Wilson         "xyz.openbmc_project.Led.Physical"};
2051d500549bSAnthony Wilson 
2052d500549bSAnthony Wilson     // Response handler for parsing output from GetSubTree
2053f94c4ecfSEd Tanous     auto respHandler = [callback{std::forward<Callback>(callback)},
2054b9d36b47SEd Tanous                         sensorsAsyncResp, inventoryItems](
2055b9d36b47SEd Tanous                            const boost::system::error_code ec,
2056b9d36b47SEd Tanous                            const dbus::utility::MapperGetSubTreeResponse&
2057b9d36b47SEd Tanous                                subtree) {
2058d500549bSAnthony Wilson         BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler enter";
2059d500549bSAnthony Wilson         if (ec)
2060d500549bSAnthony Wilson         {
20618d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
2062d500549bSAnthony Wilson             BMCWEB_LOG_ERROR << "getInventoryLeds respHandler DBus error "
2063d500549bSAnthony Wilson                              << ec;
2064d500549bSAnthony Wilson             return;
2065d500549bSAnthony Wilson         }
2066d500549bSAnthony Wilson 
2067d500549bSAnthony Wilson         // Build map of LED object paths to connections
2068d500549bSAnthony Wilson         std::shared_ptr<boost::container::flat_map<std::string, std::string>>
2069d500549bSAnthony Wilson             ledConnections = std::make_shared<
2070d500549bSAnthony Wilson                 boost::container::flat_map<std::string, std::string>>();
2071d500549bSAnthony Wilson 
2072d500549bSAnthony Wilson         // Loop through objects from GetSubTree
2073d500549bSAnthony Wilson         for (const std::pair<
2074d500549bSAnthony Wilson                  std::string,
2075d500549bSAnthony Wilson                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
2076d500549bSAnthony Wilson                  object : subtree)
2077d500549bSAnthony Wilson         {
2078d500549bSAnthony Wilson             // Check if object path is LED for one of the specified inventory
2079d500549bSAnthony Wilson             // items
2080d500549bSAnthony Wilson             const std::string& ledPath = object.first;
2081d500549bSAnthony Wilson             if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr)
2082d500549bSAnthony Wilson             {
2083d500549bSAnthony Wilson                 // Add mapping from ledPath to connection
2084d500549bSAnthony Wilson                 const std::string& connection = object.second.begin()->first;
2085d500549bSAnthony Wilson                 (*ledConnections)[ledPath] = connection;
2086d500549bSAnthony Wilson                 BMCWEB_LOG_DEBUG << "Added mapping " << ledPath << " -> "
2087d500549bSAnthony Wilson                                  << connection;
2088d500549bSAnthony Wilson             }
2089d500549bSAnthony Wilson         }
2090d500549bSAnthony Wilson 
2091d500549bSAnthony Wilson         getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections,
2092d500549bSAnthony Wilson                             std::move(callback));
2093d500549bSAnthony Wilson         BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler exit";
2094d500549bSAnthony Wilson     };
2095d500549bSAnthony Wilson     // Make call to ObjectMapper to find all inventory items
2096d500549bSAnthony Wilson     crow::connections::systemBus->async_method_call(
2097d500549bSAnthony Wilson         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
2098d500549bSAnthony Wilson         "/xyz/openbmc_project/object_mapper",
2099d500549bSAnthony Wilson         "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
2100d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLeds exit";
2101d500549bSAnthony Wilson }
2102d500549bSAnthony Wilson 
2103d500549bSAnthony Wilson /**
210442cbe538SGunnar Mills  * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
210542cbe538SGunnar Mills  *
210642cbe538SGunnar Mills  * Uses the specified connections (services) (currently assumes just one) to
210742cbe538SGunnar Mills  * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
210842cbe538SGunnar Mills  * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
210942cbe538SGunnar Mills  *
211042cbe538SGunnar Mills  * This data is later used to provide sensor property values in the JSON
211142cbe538SGunnar Mills  * response.
211242cbe538SGunnar Mills  *
211342cbe538SGunnar Mills  * Finds the Power Supply Attributes data asynchronously.  Invokes callback
211442cbe538SGunnar Mills  * when data has been obtained.
211542cbe538SGunnar Mills  *
211642cbe538SGunnar Mills  * The callback must have the following signature:
211742cbe538SGunnar Mills  *   @code
211842cbe538SGunnar Mills  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
211942cbe538SGunnar Mills  *   @endcode
212042cbe538SGunnar Mills  *
212142cbe538SGunnar Mills  * @param sensorsAsyncResp Pointer to object holding response data.
212242cbe538SGunnar Mills  * @param inventoryItems D-Bus inventory items associated with sensors.
212342cbe538SGunnar Mills  * @param psAttributesConnections Connections that provide data for the Power
212442cbe538SGunnar Mills  *        Supply Attributes
212542cbe538SGunnar Mills  * @param callback Callback to invoke when data has been obtained.
212642cbe538SGunnar Mills  */
212742cbe538SGunnar Mills template <typename Callback>
212842cbe538SGunnar Mills void getPowerSupplyAttributesData(
2129b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
213042cbe538SGunnar Mills     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
213142cbe538SGunnar Mills     const boost::container::flat_map<std::string, std::string>&
213242cbe538SGunnar Mills         psAttributesConnections,
213342cbe538SGunnar Mills     Callback&& callback)
213442cbe538SGunnar Mills {
213542cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData enter";
213642cbe538SGunnar Mills 
213742cbe538SGunnar Mills     if (psAttributesConnections.empty())
213842cbe538SGunnar Mills     {
213942cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "Can't find PowerSupplyAttributes, no connections!";
214042cbe538SGunnar Mills         callback(inventoryItems);
214142cbe538SGunnar Mills         return;
214242cbe538SGunnar Mills     }
214342cbe538SGunnar Mills 
214442cbe538SGunnar Mills     // Assuming just one connection (service) for now
214542cbe538SGunnar Mills     auto it = psAttributesConnections.nth(0);
214642cbe538SGunnar Mills 
214742cbe538SGunnar Mills     const std::string& psAttributesPath = (*it).first;
214842cbe538SGunnar Mills     const std::string& psAttributesConnection = (*it).second;
214942cbe538SGunnar Mills 
215042cbe538SGunnar Mills     // Response handler for Get DeratingFactor property
215142cbe538SGunnar Mills     auto respHandler = [sensorsAsyncResp, inventoryItems,
2152f94c4ecfSEd Tanous                         callback{std::forward<Callback>(callback)}](
215342cbe538SGunnar Mills                            const boost::system::error_code ec,
21541e1e598dSJonathan Doman                            const uint32_t value) {
215542cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler enter";
215642cbe538SGunnar Mills         if (ec)
215742cbe538SGunnar Mills         {
215842cbe538SGunnar Mills             BMCWEB_LOG_ERROR
215942cbe538SGunnar Mills                 << "getPowerSupplyAttributesData respHandler DBus error " << ec;
21608d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
216142cbe538SGunnar Mills             return;
216242cbe538SGunnar Mills         }
216342cbe538SGunnar Mills 
21641e1e598dSJonathan Doman         BMCWEB_LOG_DEBUG << "PS EfficiencyPercent value: " << value;
216542cbe538SGunnar Mills         // Store value in Power Supply Inventory Items
216642cbe538SGunnar Mills         for (InventoryItem& inventoryItem : *inventoryItems)
216742cbe538SGunnar Mills         {
216855f79e6fSEd Tanous             if (inventoryItem.isPowerSupply)
216942cbe538SGunnar Mills             {
217042cbe538SGunnar Mills                 inventoryItem.powerSupplyEfficiencyPercent =
21711e1e598dSJonathan Doman                     static_cast<int>(value);
217242cbe538SGunnar Mills             }
217342cbe538SGunnar Mills         }
217442cbe538SGunnar Mills 
217542cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler exit";
217642cbe538SGunnar Mills         callback(inventoryItems);
217742cbe538SGunnar Mills     };
217842cbe538SGunnar Mills 
217942cbe538SGunnar Mills     // Get the DeratingFactor property for the PowerSupplyAttributes
218042cbe538SGunnar Mills     // Currently only property on the interface/only one we care about
21811e1e598dSJonathan Doman     sdbusplus::asio::getProperty<uint32_t>(
21821e1e598dSJonathan Doman         *crow::connections::systemBus, psAttributesConnection, psAttributesPath,
21831e1e598dSJonathan Doman         "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor",
21841e1e598dSJonathan Doman         std::move(respHandler));
218542cbe538SGunnar Mills 
218642cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData exit";
218742cbe538SGunnar Mills }
218842cbe538SGunnar Mills 
218942cbe538SGunnar Mills /**
219042cbe538SGunnar Mills  * @brief Gets the Power Supply Attributes such as EfficiencyPercent
219142cbe538SGunnar Mills  *
219242cbe538SGunnar Mills  * Gets the D-Bus connection (service) that provides Power Supply Attributes
219342cbe538SGunnar Mills  * data. Then gets the Power Supply Attributes data from the connection
219442cbe538SGunnar Mills  * (currently just assumes 1 connection) and stores the data in the inventory
219542cbe538SGunnar Mills  * item.
219642cbe538SGunnar Mills  *
219742cbe538SGunnar Mills  * This data is later used to provide sensor property values in the JSON
219842cbe538SGunnar Mills  * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
219942cbe538SGunnar Mills  *
220042cbe538SGunnar Mills  * Finds the Power Supply Attributes data asynchronously. Invokes callback
220142cbe538SGunnar Mills  * when information has been obtained.
220242cbe538SGunnar Mills  *
220342cbe538SGunnar Mills  * The callback must have the following signature:
220442cbe538SGunnar Mills  *   @code
220542cbe538SGunnar Mills  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
220642cbe538SGunnar Mills  *   @endcode
220742cbe538SGunnar Mills  *
220842cbe538SGunnar Mills  * @param sensorsAsyncResp Pointer to object holding response data.
220942cbe538SGunnar Mills  * @param inventoryItems D-Bus inventory items associated with sensors.
221042cbe538SGunnar Mills  * @param callback Callback to invoke when data has been obtained.
221142cbe538SGunnar Mills  */
221242cbe538SGunnar Mills template <typename Callback>
221342cbe538SGunnar Mills void getPowerSupplyAttributes(
221442cbe538SGunnar Mills     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
221542cbe538SGunnar Mills     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
221642cbe538SGunnar Mills     Callback&& callback)
221742cbe538SGunnar Mills {
221842cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes enter";
221942cbe538SGunnar Mills 
222042cbe538SGunnar Mills     // Only need the power supply attributes when the Power Schema
2221a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode != sensors::node::power)
222242cbe538SGunnar Mills     {
222342cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit since not Power";
222442cbe538SGunnar Mills         callback(inventoryItems);
222542cbe538SGunnar Mills         return;
222642cbe538SGunnar Mills     }
222742cbe538SGunnar Mills 
222842cbe538SGunnar Mills     const std::array<std::string, 1> interfaces = {
222942cbe538SGunnar Mills         "xyz.openbmc_project.Control.PowerSupplyAttributes"};
223042cbe538SGunnar Mills 
223142cbe538SGunnar Mills     // Response handler for parsing output from GetSubTree
2232b9d36b47SEd Tanous     auto respHandler =
2233b9d36b47SEd Tanous         [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
2234b9d36b47SEd Tanous          inventoryItems](
2235b9d36b47SEd Tanous             const boost::system::error_code ec,
2236b9d36b47SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) {
223742cbe538SGunnar Mills             BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler enter";
223842cbe538SGunnar Mills             if (ec)
223942cbe538SGunnar Mills             {
22408d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
224142cbe538SGunnar Mills                 BMCWEB_LOG_ERROR
224242cbe538SGunnar Mills                     << "getPowerSupplyAttributes respHandler DBus error " << ec;
224342cbe538SGunnar Mills                 return;
224442cbe538SGunnar Mills             }
224526f6976fSEd Tanous             if (subtree.empty())
224642cbe538SGunnar Mills             {
224742cbe538SGunnar Mills                 BMCWEB_LOG_DEBUG << "Can't find Power Supply Attributes!";
224842cbe538SGunnar Mills                 callback(inventoryItems);
224942cbe538SGunnar Mills                 return;
225042cbe538SGunnar Mills             }
225142cbe538SGunnar Mills 
225242cbe538SGunnar Mills             // Currently we only support 1 power supply attribute, use this for
225342cbe538SGunnar Mills             // all the power supplies. Build map of object path to connection.
225442cbe538SGunnar Mills             // Assume just 1 connection and 1 path for now.
225542cbe538SGunnar Mills             boost::container::flat_map<std::string, std::string>
225642cbe538SGunnar Mills                 psAttributesConnections;
225742cbe538SGunnar Mills 
225842cbe538SGunnar Mills             if (subtree[0].first.empty() || subtree[0].second.empty())
225942cbe538SGunnar Mills             {
226042cbe538SGunnar Mills                 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
226142cbe538SGunnar Mills                 callback(inventoryItems);
226242cbe538SGunnar Mills                 return;
226342cbe538SGunnar Mills             }
226442cbe538SGunnar Mills 
226542cbe538SGunnar Mills             const std::string& psAttributesPath = subtree[0].first;
226642cbe538SGunnar Mills             const std::string& connection = subtree[0].second.begin()->first;
226742cbe538SGunnar Mills 
226842cbe538SGunnar Mills             if (connection.empty())
226942cbe538SGunnar Mills             {
227042cbe538SGunnar Mills                 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
227142cbe538SGunnar Mills                 callback(inventoryItems);
227242cbe538SGunnar Mills                 return;
227342cbe538SGunnar Mills             }
227442cbe538SGunnar Mills 
227542cbe538SGunnar Mills             psAttributesConnections[psAttributesPath] = connection;
227642cbe538SGunnar Mills             BMCWEB_LOG_DEBUG << "Added mapping " << psAttributesPath << " -> "
227742cbe538SGunnar Mills                              << connection;
227842cbe538SGunnar Mills 
227942cbe538SGunnar Mills             getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
228042cbe538SGunnar Mills                                          psAttributesConnections,
228142cbe538SGunnar Mills                                          std::move(callback));
228242cbe538SGunnar Mills             BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler exit";
228342cbe538SGunnar Mills         };
228442cbe538SGunnar Mills     // Make call to ObjectMapper to find the PowerSupplyAttributes service
228542cbe538SGunnar Mills     crow::connections::systemBus->async_method_call(
228642cbe538SGunnar Mills         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
228742cbe538SGunnar Mills         "/xyz/openbmc_project/object_mapper",
228842cbe538SGunnar Mills         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
228942cbe538SGunnar Mills         "/xyz/openbmc_project", 0, interfaces);
229042cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit";
229142cbe538SGunnar Mills }
229242cbe538SGunnar Mills 
229342cbe538SGunnar Mills /**
2294adc4f0dbSShawn McCarney  * @brief Gets inventory items associated with sensors.
22958fb49dd6SShawn McCarney  *
22968fb49dd6SShawn McCarney  * Finds the inventory items that are associated with the specified sensors.
2297adc4f0dbSShawn McCarney  * Then gets D-Bus data for the inventory items, such as presence and VPD.
22988fb49dd6SShawn McCarney  *
2299adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
2300adc4f0dbSShawn McCarney  * response.
23018fb49dd6SShawn McCarney  *
2302adc4f0dbSShawn McCarney  * Finds the inventory items asynchronously.  Invokes callback when the
2303adc4f0dbSShawn McCarney  * inventory items have been obtained.
2304adc4f0dbSShawn McCarney  *
2305adc4f0dbSShawn McCarney  * The callback must have the following signature:
2306adc4f0dbSShawn McCarney  *   @code
2307adc4f0dbSShawn McCarney  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2308adc4f0dbSShawn McCarney  *   @endcode
23098fb49dd6SShawn McCarney  *
23108fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
23118fb49dd6SShawn McCarney  * @param sensorNames All sensors within the current chassis.
23128fb49dd6SShawn McCarney  * @param objectMgrPaths Mappings from connection name to DBus object path that
23138fb49dd6SShawn McCarney  * implements ObjectManager.
2314adc4f0dbSShawn McCarney  * @param callback Callback to invoke when inventory items have been obtained.
23158fb49dd6SShawn McCarney  */
2316adc4f0dbSShawn McCarney template <typename Callback>
2317adc4f0dbSShawn McCarney static void getInventoryItems(
23188fb49dd6SShawn McCarney     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
23198fb49dd6SShawn McCarney     const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
23208fb49dd6SShawn McCarney     std::shared_ptr<boost::container::flat_map<std::string, std::string>>
2321adc4f0dbSShawn McCarney         objectMgrPaths,
2322adc4f0dbSShawn McCarney     Callback&& callback)
23238fb49dd6SShawn McCarney {
2324adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItems enter";
2325adc4f0dbSShawn McCarney     auto getInventoryItemAssociationsCb =
2326f94c4ecfSEd Tanous         [sensorsAsyncResp, objectMgrPaths,
2327f94c4ecfSEd Tanous          callback{std::forward<Callback>(callback)}](
2328adc4f0dbSShawn McCarney             std::shared_ptr<std::vector<InventoryItem>> inventoryItems) {
2329adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb enter";
23308fb49dd6SShawn McCarney             auto getInventoryItemsConnectionsCb =
2331adc4f0dbSShawn McCarney                 [sensorsAsyncResp, inventoryItems, objectMgrPaths,
2332f94c4ecfSEd Tanous                  callback{std::forward<const Callback>(callback)}](
23338fb49dd6SShawn McCarney                     std::shared_ptr<boost::container::flat_set<std::string>>
23348fb49dd6SShawn McCarney                         invConnections) {
23358fb49dd6SShawn McCarney                     BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb enter";
2336d500549bSAnthony Wilson                     auto getInventoryItemsDataCb =
2337d500549bSAnthony Wilson                         [sensorsAsyncResp, inventoryItems,
2338d500549bSAnthony Wilson                          callback{std::move(callback)}]() {
2339d500549bSAnthony Wilson                             BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb enter";
234042cbe538SGunnar Mills 
234142cbe538SGunnar Mills                             auto getInventoryLedsCb = [sensorsAsyncResp,
234242cbe538SGunnar Mills                                                        inventoryItems,
234342cbe538SGunnar Mills                                                        callback{std::move(
234442cbe538SGunnar Mills                                                            callback)}]() {
234542cbe538SGunnar Mills                                 BMCWEB_LOG_DEBUG << "getInventoryLedsCb enter";
234642cbe538SGunnar Mills                                 // Find Power Supply Attributes and get the data
234742cbe538SGunnar Mills                                 getPowerSupplyAttributes(sensorsAsyncResp,
234842cbe538SGunnar Mills                                                          inventoryItems,
234942cbe538SGunnar Mills                                                          std::move(callback));
235042cbe538SGunnar Mills                                 BMCWEB_LOG_DEBUG << "getInventoryLedsCb exit";
235142cbe538SGunnar Mills                             };
235242cbe538SGunnar Mills 
2353d500549bSAnthony Wilson                             // Find led connections and get the data
2354d500549bSAnthony Wilson                             getInventoryLeds(sensorsAsyncResp, inventoryItems,
235542cbe538SGunnar Mills                                              std::move(getInventoryLedsCb));
2356d500549bSAnthony Wilson                             BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb exit";
2357d500549bSAnthony Wilson                         };
23588fb49dd6SShawn McCarney 
2359adc4f0dbSShawn McCarney                     // Get inventory item data from connections
2360adc4f0dbSShawn McCarney                     getInventoryItemsData(sensorsAsyncResp, inventoryItems,
2361adc4f0dbSShawn McCarney                                           invConnections, objectMgrPaths,
2362d500549bSAnthony Wilson                                           std::move(getInventoryItemsDataCb));
23638fb49dd6SShawn McCarney                     BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb exit";
23648fb49dd6SShawn McCarney                 };
23658fb49dd6SShawn McCarney 
2366adc4f0dbSShawn McCarney             // Get connections that provide inventory item data
23678fb49dd6SShawn McCarney             getInventoryItemsConnections(
2368adc4f0dbSShawn McCarney                 sensorsAsyncResp, inventoryItems,
23698fb49dd6SShawn McCarney                 std::move(getInventoryItemsConnectionsCb));
2370adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb exit";
23718fb49dd6SShawn McCarney         };
23728fb49dd6SShawn McCarney 
2373adc4f0dbSShawn McCarney     // Get associations from sensors to inventory items
2374adc4f0dbSShawn McCarney     getInventoryItemAssociations(sensorsAsyncResp, sensorNames, objectMgrPaths,
2375adc4f0dbSShawn McCarney                                  std::move(getInventoryItemAssociationsCb));
2376adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItems exit";
2377adc4f0dbSShawn McCarney }
2378adc4f0dbSShawn McCarney 
2379adc4f0dbSShawn McCarney /**
2380adc4f0dbSShawn McCarney  * @brief Returns JSON PowerSupply object for the specified inventory item.
2381adc4f0dbSShawn McCarney  *
2382adc4f0dbSShawn McCarney  * Searches for a JSON PowerSupply object that matches the specified inventory
2383adc4f0dbSShawn McCarney  * item.  If one is not found, a new PowerSupply object is added to the JSON
2384adc4f0dbSShawn McCarney  * array.
2385adc4f0dbSShawn McCarney  *
2386adc4f0dbSShawn McCarney  * Multiple sensors are often associated with one power supply inventory item.
2387adc4f0dbSShawn McCarney  * As a result, multiple sensor values are stored in one JSON PowerSupply
2388adc4f0dbSShawn McCarney  * object.
2389adc4f0dbSShawn McCarney  *
2390adc4f0dbSShawn McCarney  * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
2391adc4f0dbSShawn McCarney  * @param inventoryItem Inventory item for the power supply.
2392adc4f0dbSShawn McCarney  * @param chassisId Chassis that contains the power supply.
2393adc4f0dbSShawn McCarney  * @return JSON PowerSupply object for the specified inventory item.
2394adc4f0dbSShawn McCarney  */
239523a21a1cSEd Tanous inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
2396adc4f0dbSShawn McCarney                                       const InventoryItem& inventoryItem,
2397adc4f0dbSShawn McCarney                                       const std::string& chassisId)
2398adc4f0dbSShawn McCarney {
2399adc4f0dbSShawn McCarney     // Check if matching PowerSupply object already exists in JSON array
2400adc4f0dbSShawn McCarney     for (nlohmann::json& powerSupply : powerSupplyArray)
2401adc4f0dbSShawn McCarney     {
2402adc4f0dbSShawn McCarney         if (powerSupply["MemberId"] == inventoryItem.name)
2403adc4f0dbSShawn McCarney         {
2404adc4f0dbSShawn McCarney             return powerSupply;
2405adc4f0dbSShawn McCarney         }
2406adc4f0dbSShawn McCarney     }
2407adc4f0dbSShawn McCarney 
2408adc4f0dbSShawn McCarney     // Add new PowerSupply object to JSON array
2409adc4f0dbSShawn McCarney     powerSupplyArray.push_back({});
2410adc4f0dbSShawn McCarney     nlohmann::json& powerSupply = powerSupplyArray.back();
2411adc4f0dbSShawn McCarney     powerSupply["@odata.id"] =
2412adc4f0dbSShawn McCarney         "/redfish/v1/Chassis/" + chassisId + "/Power#/PowerSupplies/";
2413adc4f0dbSShawn McCarney     powerSupply["MemberId"] = inventoryItem.name;
2414adc4f0dbSShawn McCarney     powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " ");
2415adc4f0dbSShawn McCarney     powerSupply["Manufacturer"] = inventoryItem.manufacturer;
2416adc4f0dbSShawn McCarney     powerSupply["Model"] = inventoryItem.model;
2417adc4f0dbSShawn McCarney     powerSupply["PartNumber"] = inventoryItem.partNumber;
2418adc4f0dbSShawn McCarney     powerSupply["SerialNumber"] = inventoryItem.serialNumber;
2419d500549bSAnthony Wilson     setLedState(powerSupply, &inventoryItem);
2420adc4f0dbSShawn McCarney 
242142cbe538SGunnar Mills     if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
242242cbe538SGunnar Mills     {
242342cbe538SGunnar Mills         powerSupply["EfficiencyPercent"] =
242442cbe538SGunnar Mills             inventoryItem.powerSupplyEfficiencyPercent;
242542cbe538SGunnar Mills     }
242642cbe538SGunnar Mills 
242742cbe538SGunnar Mills     powerSupply["Status"]["State"] = getState(&inventoryItem);
2428adc4f0dbSShawn McCarney     const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
2429adc4f0dbSShawn McCarney     powerSupply["Status"]["Health"] = health;
2430adc4f0dbSShawn McCarney 
2431adc4f0dbSShawn McCarney     return powerSupply;
24328fb49dd6SShawn McCarney }
24338fb49dd6SShawn McCarney 
24348fb49dd6SShawn McCarney /**
2435de629b6eSShawn McCarney  * @brief Gets the values of the specified sensors.
2436de629b6eSShawn McCarney  *
2437de629b6eSShawn McCarney  * Stores the results as JSON in the SensorsAsyncResp.
2438de629b6eSShawn McCarney  *
2439de629b6eSShawn McCarney  * Gets the sensor values asynchronously.  Stores the results later when the
2440de629b6eSShawn McCarney  * information has been obtained.
2441de629b6eSShawn McCarney  *
2442adc4f0dbSShawn McCarney  * The sensorNames set contains all requested sensors for the current chassis.
2443de629b6eSShawn McCarney  *
2444de629b6eSShawn McCarney  * To minimize the number of DBus calls, the DBus method
2445de629b6eSShawn McCarney  * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
2446de629b6eSShawn McCarney  * values of all sensors provided by a connection (service).
2447de629b6eSShawn McCarney  *
2448de629b6eSShawn McCarney  * The connections set contains all the connections that provide sensor values.
2449de629b6eSShawn McCarney  *
2450de629b6eSShawn McCarney  * The objectMgrPaths map contains mappings from a connection name to the
2451de629b6eSShawn McCarney  * corresponding DBus object path that implements ObjectManager.
2452de629b6eSShawn McCarney  *
2453adc4f0dbSShawn McCarney  * The InventoryItem vector contains D-Bus inventory items associated with the
2454adc4f0dbSShawn McCarney  * sensors.  Inventory item data is needed for some Redfish sensor properties.
2455adc4f0dbSShawn McCarney  *
2456de629b6eSShawn McCarney  * @param SensorsAsyncResp Pointer to object holding response data.
2457adc4f0dbSShawn McCarney  * @param sensorNames All requested sensors within the current chassis.
2458de629b6eSShawn McCarney  * @param connections Connections that provide sensor values.
2459de629b6eSShawn McCarney  * @param objectMgrPaths Mappings from connection name to DBus object path that
2460de629b6eSShawn McCarney  * implements ObjectManager.
2461adc4f0dbSShawn McCarney  * @param inventoryItems Inventory items associated with the sensors.
2462de629b6eSShawn McCarney  */
246323a21a1cSEd Tanous inline void getSensorData(
246481ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2465b5a76932SEd Tanous     const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames,
2466de629b6eSShawn McCarney     const boost::container::flat_set<std::string>& connections,
2467b5a76932SEd Tanous     const std::shared_ptr<boost::container::flat_map<std::string, std::string>>&
2468adc4f0dbSShawn McCarney         objectMgrPaths,
2469b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems)
2470de629b6eSShawn McCarney {
2471de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getSensorData enter";
2472de629b6eSShawn McCarney     // Get managed objects from all services exposing sensors
2473de629b6eSShawn McCarney     for (const std::string& connection : connections)
2474de629b6eSShawn McCarney     {
2475de629b6eSShawn McCarney         // Response handler to process managed objects
247681ce609eSEd Tanous         auto getManagedObjectsCb = [sensorsAsyncResp, sensorNames,
2477adc4f0dbSShawn McCarney                                     inventoryItems](
2478de629b6eSShawn McCarney                                        const boost::system::error_code ec,
2479711ac7a9SEd Tanous                                        dbus::utility::ManagedObjectType& resp) {
2480de629b6eSShawn McCarney             BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter";
2481de629b6eSShawn McCarney             if (ec)
2482de629b6eSShawn McCarney             {
2483de629b6eSShawn McCarney                 BMCWEB_LOG_ERROR << "getManagedObjectsCb DBUS error: " << ec;
24848d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
2485de629b6eSShawn McCarney                 return;
2486de629b6eSShawn McCarney             }
2487de629b6eSShawn McCarney             // Go through all objects and update response with sensor data
2488de629b6eSShawn McCarney             for (const auto& objDictEntry : resp)
2489de629b6eSShawn McCarney             {
2490de629b6eSShawn McCarney                 const std::string& objPath =
2491de629b6eSShawn McCarney                     static_cast<const std::string&>(objDictEntry.first);
2492de629b6eSShawn McCarney                 BMCWEB_LOG_DEBUG << "getManagedObjectsCb parsing object "
2493de629b6eSShawn McCarney                                  << objPath;
2494de629b6eSShawn McCarney 
2495de629b6eSShawn McCarney                 std::vector<std::string> split;
2496de629b6eSShawn McCarney                 // Reserve space for
2497de629b6eSShawn McCarney                 // /xyz/openbmc_project/sensors/<name>/<subname>
2498de629b6eSShawn McCarney                 split.reserve(6);
2499de629b6eSShawn McCarney                 boost::algorithm::split(split, objPath, boost::is_any_of("/"));
2500de629b6eSShawn McCarney                 if (split.size() < 6)
2501de629b6eSShawn McCarney                 {
2502de629b6eSShawn McCarney                     BMCWEB_LOG_ERROR << "Got path that isn't long enough "
2503de629b6eSShawn McCarney                                      << objPath;
2504de629b6eSShawn McCarney                     continue;
2505de629b6eSShawn McCarney                 }
2506de629b6eSShawn McCarney                 // These indexes aren't intuitive, as boost::split puts an empty
2507de629b6eSShawn McCarney                 // string at the beginning
2508de629b6eSShawn McCarney                 const std::string& sensorType = split[4];
2509de629b6eSShawn McCarney                 const std::string& sensorName = split[5];
2510de629b6eSShawn McCarney                 BMCWEB_LOG_DEBUG << "sensorName " << sensorName
2511de629b6eSShawn McCarney                                  << " sensorType " << sensorType;
251249c53ac9SJohnathan Mantey                 if (sensorNames->find(objPath) == sensorNames->end())
2513de629b6eSShawn McCarney                 {
2514accdbb2cSAndrew Geissler                     BMCWEB_LOG_DEBUG << sensorName << " not in sensor list ";
2515de629b6eSShawn McCarney                     continue;
2516de629b6eSShawn McCarney                 }
2517de629b6eSShawn McCarney 
2518adc4f0dbSShawn McCarney                 // Find inventory item (if any) associated with sensor
2519adc4f0dbSShawn McCarney                 InventoryItem* inventoryItem =
2520adc4f0dbSShawn McCarney                     findInventoryItemForSensor(inventoryItems, objPath);
2521adc4f0dbSShawn McCarney 
252295a3ecadSAnthony Wilson                 const std::string& sensorSchema =
252381ce609eSEd Tanous                     sensorsAsyncResp->chassisSubNode;
252495a3ecadSAnthony Wilson 
252595a3ecadSAnthony Wilson                 nlohmann::json* sensorJson = nullptr;
252695a3ecadSAnthony Wilson 
2527a0ec28b6SAdrian Ambrożewicz                 if (sensorSchema == sensors::node::sensors)
252895a3ecadSAnthony Wilson                 {
25298d1b46d7Szhanghch05                     sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
253081ce609eSEd Tanous                         "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId +
253181ce609eSEd Tanous                         "/" + sensorsAsyncResp->chassisSubNode + "/" +
253295a3ecadSAnthony Wilson                         sensorName;
25338d1b46d7Szhanghch05                     sensorJson = &(sensorsAsyncResp->asyncResp->res.jsonValue);
253495a3ecadSAnthony Wilson                 }
253595a3ecadSAnthony Wilson                 else
253695a3ecadSAnthony Wilson                 {
2537271584abSEd Tanous                     std::string fieldName;
2538de629b6eSShawn McCarney                     if (sensorType == "temperature")
2539de629b6eSShawn McCarney                     {
2540de629b6eSShawn McCarney                         fieldName = "Temperatures";
2541de629b6eSShawn McCarney                     }
2542de629b6eSShawn McCarney                     else if (sensorType == "fan" || sensorType == "fan_tach" ||
2543de629b6eSShawn McCarney                              sensorType == "fan_pwm")
2544de629b6eSShawn McCarney                     {
2545de629b6eSShawn McCarney                         fieldName = "Fans";
2546de629b6eSShawn McCarney                     }
2547de629b6eSShawn McCarney                     else if (sensorType == "voltage")
2548de629b6eSShawn McCarney                     {
2549de629b6eSShawn McCarney                         fieldName = "Voltages";
2550de629b6eSShawn McCarney                     }
2551de629b6eSShawn McCarney                     else if (sensorType == "power")
2552de629b6eSShawn McCarney                     {
255355f79e6fSEd Tanous                         if (sensorName == "total_power")
2554028f7ebcSEddie James                         {
2555028f7ebcSEddie James                             fieldName = "PowerControl";
2556028f7ebcSEddie James                         }
2557adc4f0dbSShawn McCarney                         else if ((inventoryItem != nullptr) &&
2558adc4f0dbSShawn McCarney                                  (inventoryItem->isPowerSupply))
2559028f7ebcSEddie James                         {
2560de629b6eSShawn McCarney                             fieldName = "PowerSupplies";
2561de629b6eSShawn McCarney                         }
2562adc4f0dbSShawn McCarney                         else
2563adc4f0dbSShawn McCarney                         {
2564adc4f0dbSShawn McCarney                             // Other power sensors are in SensorCollection
2565adc4f0dbSShawn McCarney                             continue;
2566adc4f0dbSShawn McCarney                         }
2567028f7ebcSEddie James                     }
2568de629b6eSShawn McCarney                     else
2569de629b6eSShawn McCarney                     {
2570de629b6eSShawn McCarney                         BMCWEB_LOG_ERROR << "Unsure how to handle sensorType "
2571de629b6eSShawn McCarney                                          << sensorType;
2572de629b6eSShawn McCarney                         continue;
2573de629b6eSShawn McCarney                     }
2574de629b6eSShawn McCarney 
2575de629b6eSShawn McCarney                     nlohmann::json& tempArray =
25768d1b46d7Szhanghch05                         sensorsAsyncResp->asyncResp->res.jsonValue[fieldName];
2577adc4f0dbSShawn McCarney                     if (fieldName == "PowerControl")
257849c53ac9SJohnathan Mantey                     {
2579adc4f0dbSShawn McCarney                         if (tempArray.empty())
25807ab06f49SGunnar Mills                         {
258195a3ecadSAnthony Wilson                             // Put multiple "sensors" into a single
258295a3ecadSAnthony Wilson                             // PowerControl. Follows MemberId naming and
258395a3ecadSAnthony Wilson                             // naming in power.hpp.
25847ab06f49SGunnar Mills                             tempArray.push_back(
2585adc4f0dbSShawn McCarney                                 {{"@odata.id",
2586adc4f0dbSShawn McCarney                                   "/redfish/v1/Chassis/" +
258781ce609eSEd Tanous                                       sensorsAsyncResp->chassisId + "/" +
258881ce609eSEd Tanous                                       sensorsAsyncResp->chassisSubNode + "#/" +
2589adc4f0dbSShawn McCarney                                       fieldName + "/0"}});
2590adc4f0dbSShawn McCarney                         }
2591adc4f0dbSShawn McCarney                         sensorJson = &(tempArray.back());
2592adc4f0dbSShawn McCarney                     }
2593adc4f0dbSShawn McCarney                     else if (fieldName == "PowerSupplies")
2594adc4f0dbSShawn McCarney                     {
2595adc4f0dbSShawn McCarney                         if (inventoryItem != nullptr)
2596adc4f0dbSShawn McCarney                         {
2597adc4f0dbSShawn McCarney                             sensorJson =
2598adc4f0dbSShawn McCarney                                 &(getPowerSupply(tempArray, *inventoryItem,
259981ce609eSEd Tanous                                                  sensorsAsyncResp->chassisId));
2600adc4f0dbSShawn McCarney                         }
260149c53ac9SJohnathan Mantey                     }
260249c53ac9SJohnathan Mantey                     else
260349c53ac9SJohnathan Mantey                     {
2604de629b6eSShawn McCarney                         tempArray.push_back(
260595a3ecadSAnthony Wilson                             {{"@odata.id",
260695a3ecadSAnthony Wilson                               "/redfish/v1/Chassis/" +
260781ce609eSEd Tanous                                   sensorsAsyncResp->chassisId + "/" +
260881ce609eSEd Tanous                                   sensorsAsyncResp->chassisSubNode + "#/" +
260995a3ecadSAnthony Wilson                                   fieldName + "/"}});
2610adc4f0dbSShawn McCarney                         sensorJson = &(tempArray.back());
261149c53ac9SJohnathan Mantey                     }
261295a3ecadSAnthony Wilson                 }
2613de629b6eSShawn McCarney 
2614adc4f0dbSShawn McCarney                 if (sensorJson != nullptr)
2615adc4f0dbSShawn McCarney                 {
2616a0ec28b6SAdrian Ambrożewicz                     objectInterfacesToJson(
261781ce609eSEd Tanous                         sensorName, sensorType, sensorsAsyncResp,
2618a0ec28b6SAdrian Ambrożewicz                         objDictEntry.second, *sensorJson, inventoryItem);
2619adc4f0dbSShawn McCarney                 }
2620de629b6eSShawn McCarney             }
262181ce609eSEd Tanous             if (sensorsAsyncResp.use_count() == 1)
262249c53ac9SJohnathan Mantey             {
262381ce609eSEd Tanous                 sortJSONResponse(sensorsAsyncResp);
262481ce609eSEd Tanous                 if (sensorsAsyncResp->chassisSubNode == sensors::node::thermal)
26258bd25ccdSJames Feist                 {
262681ce609eSEd Tanous                     populateFanRedundancy(sensorsAsyncResp);
26278bd25ccdSJames Feist                 }
262849c53ac9SJohnathan Mantey             }
2629de629b6eSShawn McCarney             BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit";
2630de629b6eSShawn McCarney         };
2631de629b6eSShawn McCarney 
2632de629b6eSShawn McCarney         // Find DBus object path that implements ObjectManager for the current
2633de629b6eSShawn McCarney         // connection.  If no mapping found, default to "/".
26348fb49dd6SShawn McCarney         auto iter = objectMgrPaths->find(connection);
2635de629b6eSShawn McCarney         const std::string& objectMgrPath =
26368fb49dd6SShawn McCarney             (iter != objectMgrPaths->end()) ? iter->second : "/";
2637de629b6eSShawn McCarney         BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
2638de629b6eSShawn McCarney                          << objectMgrPath;
2639de629b6eSShawn McCarney 
2640de629b6eSShawn McCarney         crow::connections::systemBus->async_method_call(
2641de629b6eSShawn McCarney             getManagedObjectsCb, connection, objectMgrPath,
2642de629b6eSShawn McCarney             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
264323a21a1cSEd Tanous     }
2644de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getSensorData exit";
2645de629b6eSShawn McCarney }
2646de629b6eSShawn McCarney 
264723a21a1cSEd Tanous inline void processSensorList(
264881ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2649b5a76932SEd Tanous     const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames)
26501abe55efSEd Tanous {
265195a3ecadSAnthony Wilson     auto getConnectionCb =
265281ce609eSEd Tanous         [sensorsAsyncResp, sensorNames](
265395a3ecadSAnthony Wilson             const boost::container::flat_set<std::string>& connections) {
265455c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "getConnectionCb enter";
2655de629b6eSShawn McCarney             auto getObjectManagerPathsCb =
265681ce609eSEd Tanous                 [sensorsAsyncResp, sensorNames,
2657b5a76932SEd Tanous                  connections](const std::shared_ptr<boost::container::flat_map<
2658b5a76932SEd Tanous                                   std::string, std::string>>& objectMgrPaths) {
2659de629b6eSShawn McCarney                     BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb enter";
2660adc4f0dbSShawn McCarney                     auto getInventoryItemsCb =
266181ce609eSEd Tanous                         [sensorsAsyncResp, sensorNames, connections,
2662adc4f0dbSShawn McCarney                          objectMgrPaths](
2663f23b7296SEd Tanous                             const std::shared_ptr<std::vector<InventoryItem>>&
2664adc4f0dbSShawn McCarney                                 inventoryItems) {
2665adc4f0dbSShawn McCarney                             BMCWEB_LOG_DEBUG << "getInventoryItemsCb enter";
266649c53ac9SJohnathan Mantey                             // Get sensor data and store results in JSON
266781ce609eSEd Tanous                             getSensorData(sensorsAsyncResp, sensorNames,
2668adc4f0dbSShawn McCarney                                           connections, objectMgrPaths,
2669f23b7296SEd Tanous                                           inventoryItems);
2670adc4f0dbSShawn McCarney                             BMCWEB_LOG_DEBUG << "getInventoryItemsCb exit";
2671adc4f0dbSShawn McCarney                         };
2672adc4f0dbSShawn McCarney 
2673adc4f0dbSShawn McCarney                     // Get inventory items associated with sensors
267481ce609eSEd Tanous                     getInventoryItems(sensorsAsyncResp, sensorNames,
2675adc4f0dbSShawn McCarney                                       objectMgrPaths,
2676adc4f0dbSShawn McCarney                                       std::move(getInventoryItemsCb));
2677adc4f0dbSShawn McCarney 
2678de629b6eSShawn McCarney                     BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb exit";
267908777fb0SLewanczyk, Dawid                 };
2680de629b6eSShawn McCarney 
268149c53ac9SJohnathan Mantey             // Get mapping from connection names to the DBus object
268249c53ac9SJohnathan Mantey             // paths that implement the ObjectManager interface
268381ce609eSEd Tanous             getObjectManagerPaths(sensorsAsyncResp,
2684de629b6eSShawn McCarney                                   std::move(getObjectManagerPathsCb));
268555c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "getConnectionCb exit";
268608777fb0SLewanczyk, Dawid         };
2687de629b6eSShawn McCarney 
2688de629b6eSShawn McCarney     // Get set of connections that provide sensor values
268981ce609eSEd Tanous     getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb));
269095a3ecadSAnthony Wilson }
269195a3ecadSAnthony Wilson 
269295a3ecadSAnthony Wilson /**
269395a3ecadSAnthony Wilson  * @brief Entry point for retrieving sensors data related to requested
269495a3ecadSAnthony Wilson  *        chassis.
269595a3ecadSAnthony Wilson  * @param SensorsAsyncResp   Pointer to object holding response data
269695a3ecadSAnthony Wilson  */
2697b5a76932SEd Tanous inline void
269881ce609eSEd Tanous     getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
269995a3ecadSAnthony Wilson {
270095a3ecadSAnthony Wilson     BMCWEB_LOG_DEBUG << "getChassisData enter";
270195a3ecadSAnthony Wilson     auto getChassisCb =
270281ce609eSEd Tanous         [sensorsAsyncResp](
2703f23b7296SEd Tanous             const std::shared_ptr<boost::container::flat_set<std::string>>&
270495a3ecadSAnthony Wilson                 sensorNames) {
270595a3ecadSAnthony Wilson             BMCWEB_LOG_DEBUG << "getChassisCb enter";
270681ce609eSEd Tanous             processSensorList(sensorsAsyncResp, sensorNames);
270755c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "getChassisCb exit";
270808777fb0SLewanczyk, Dawid         };
27098d1b46d7Szhanghch05     sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] =
27108d1b46d7Szhanghch05         nlohmann::json::array();
271108777fb0SLewanczyk, Dawid 
271226f03899SShawn McCarney     // Get set of sensors in chassis
271381ce609eSEd Tanous     getChassis(sensorsAsyncResp, std::move(getChassisCb));
271455c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "getChassisData exit";
2715271584abSEd Tanous }
271608777fb0SLewanczyk, Dawid 
2717413961deSRichard Marian Thomaiyar /**
271849c53ac9SJohnathan Mantey  * @brief Find the requested sensorName in the list of all sensors supplied by
271949c53ac9SJohnathan Mantey  * the chassis node
272049c53ac9SJohnathan Mantey  *
272149c53ac9SJohnathan Mantey  * @param sensorName   The sensor name supplied in the PATCH request
272249c53ac9SJohnathan Mantey  * @param sensorsList  The list of sensors managed by the chassis node
272349c53ac9SJohnathan Mantey  * @param sensorsModified  The list of sensors that were found as a result of
272449c53ac9SJohnathan Mantey  *                         repeated calls to this function
272549c53ac9SJohnathan Mantey  */
272623a21a1cSEd Tanous inline bool findSensorNameUsingSensorPath(
27270a86febdSRichard Marian Thomaiyar     std::string_view sensorName,
272849c53ac9SJohnathan Mantey     boost::container::flat_set<std::string>& sensorsList,
272949c53ac9SJohnathan Mantey     boost::container::flat_set<std::string>& sensorsModified)
273049c53ac9SJohnathan Mantey {
273128aa8de5SGeorge Liu     for (auto& chassisSensor : sensorsList)
273249c53ac9SJohnathan Mantey     {
273328aa8de5SGeorge Liu         sdbusplus::message::object_path path(chassisSensor);
2734b00dcc27SEd Tanous         std::string thisSensorName = path.filename();
273528aa8de5SGeorge Liu         if (thisSensorName.empty())
273649c53ac9SJohnathan Mantey         {
273749c53ac9SJohnathan Mantey             continue;
273849c53ac9SJohnathan Mantey         }
273949c53ac9SJohnathan Mantey         if (thisSensorName == sensorName)
274049c53ac9SJohnathan Mantey         {
274149c53ac9SJohnathan Mantey             sensorsModified.emplace(chassisSensor);
274249c53ac9SJohnathan Mantey             return true;
274349c53ac9SJohnathan Mantey         }
274449c53ac9SJohnathan Mantey     }
274549c53ac9SJohnathan Mantey     return false;
274649c53ac9SJohnathan Mantey }
274749c53ac9SJohnathan Mantey 
274849c53ac9SJohnathan Mantey /**
2749413961deSRichard Marian Thomaiyar  * @brief Entry point for overriding sensor values of given sensor
2750413961deSRichard Marian Thomaiyar  *
27518d1b46d7Szhanghch05  * @param sensorAsyncResp   response object
27524bb3dc34SCarol Wang  * @param allCollections   Collections extract from sensors' request patch info
2753413961deSRichard Marian Thomaiyar  * @param chassisSubNode   Chassis Node for which the query has to happen
2754413961deSRichard Marian Thomaiyar  */
275523a21a1cSEd Tanous inline void setSensorsOverride(
2756b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
27574bb3dc34SCarol Wang     std::unordered_map<std::string, std::vector<nlohmann::json>>&
2758397fd61fSjayaprakash Mutyala         allCollections)
2759413961deSRichard Marian Thomaiyar {
276070d1d0aaSjayaprakash Mutyala     BMCWEB_LOG_INFO << "setSensorsOverride for subNode"
27614bb3dc34SCarol Wang                     << sensorAsyncResp->chassisSubNode << "\n";
2762413961deSRichard Marian Thomaiyar 
2763543f4400SEd Tanous     const char* propertyValueName = nullptr;
2764f65af9e8SRichard Marian Thomaiyar     std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
2765413961deSRichard Marian Thomaiyar     std::string memberId;
2766543f4400SEd Tanous     double value = 0.0;
2767f65af9e8SRichard Marian Thomaiyar     for (auto& collectionItems : allCollections)
2768f65af9e8SRichard Marian Thomaiyar     {
2769f65af9e8SRichard Marian Thomaiyar         if (collectionItems.first == "Temperatures")
2770f65af9e8SRichard Marian Thomaiyar         {
2771f65af9e8SRichard Marian Thomaiyar             propertyValueName = "ReadingCelsius";
2772f65af9e8SRichard Marian Thomaiyar         }
2773f65af9e8SRichard Marian Thomaiyar         else if (collectionItems.first == "Fans")
2774f65af9e8SRichard Marian Thomaiyar         {
2775f65af9e8SRichard Marian Thomaiyar             propertyValueName = "Reading";
2776f65af9e8SRichard Marian Thomaiyar         }
2777f65af9e8SRichard Marian Thomaiyar         else
2778f65af9e8SRichard Marian Thomaiyar         {
2779f65af9e8SRichard Marian Thomaiyar             propertyValueName = "ReadingVolts";
2780f65af9e8SRichard Marian Thomaiyar         }
2781f65af9e8SRichard Marian Thomaiyar         for (auto& item : collectionItems.second)
2782f65af9e8SRichard Marian Thomaiyar         {
27838d1b46d7Szhanghch05             if (!json_util::readJson(item, sensorAsyncResp->asyncResp->res,
27848d1b46d7Szhanghch05                                      "MemberId", memberId, propertyValueName,
27858d1b46d7Szhanghch05                                      value))
2786413961deSRichard Marian Thomaiyar             {
2787413961deSRichard Marian Thomaiyar                 return;
2788413961deSRichard Marian Thomaiyar             }
2789f65af9e8SRichard Marian Thomaiyar             overrideMap.emplace(memberId,
2790f65af9e8SRichard Marian Thomaiyar                                 std::make_pair(value, collectionItems.first));
2791f65af9e8SRichard Marian Thomaiyar         }
2792f65af9e8SRichard Marian Thomaiyar     }
27934bb3dc34SCarol Wang 
2794b5a76932SEd Tanous     auto getChassisSensorListCb = [sensorAsyncResp, overrideMap](
2795b5a76932SEd Tanous                                       const std::shared_ptr<
279649c53ac9SJohnathan Mantey                                           boost::container::flat_set<
2797b5a76932SEd Tanous                                               std::string>>& sensorsList) {
279849c53ac9SJohnathan Mantey         // Match sensor names in the PATCH request to those managed by the
279949c53ac9SJohnathan Mantey         // chassis node
280049c53ac9SJohnathan Mantey         const std::shared_ptr<boost::container::flat_set<std::string>>
280149c53ac9SJohnathan Mantey             sensorNames =
280249c53ac9SJohnathan Mantey                 std::make_shared<boost::container::flat_set<std::string>>();
2803f65af9e8SRichard Marian Thomaiyar         for (const auto& item : overrideMap)
2804413961deSRichard Marian Thomaiyar         {
2805f65af9e8SRichard Marian Thomaiyar             const auto& sensor = item.first;
280649c53ac9SJohnathan Mantey             if (!findSensorNameUsingSensorPath(sensor, *sensorsList,
280749c53ac9SJohnathan Mantey                                                *sensorNames))
2808f65af9e8SRichard Marian Thomaiyar             {
2809f65af9e8SRichard Marian Thomaiyar                 BMCWEB_LOG_INFO << "Unable to find memberId " << item.first;
28108d1b46d7Szhanghch05                 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2811f65af9e8SRichard Marian Thomaiyar                                            item.second.second, item.first);
2812413961deSRichard Marian Thomaiyar                 return;
2813413961deSRichard Marian Thomaiyar             }
2814f65af9e8SRichard Marian Thomaiyar         }
2815413961deSRichard Marian Thomaiyar         // Get the connection to which the memberId belongs
28164f277b54SJayaprakash Mutyala         auto getObjectsWithConnectionCb = [sensorAsyncResp, overrideMap](
28174f277b54SJayaprakash Mutyala                                               const boost::container::flat_set<
28184f277b54SJayaprakash Mutyala                                                   std::string>& /*connections*/,
28194f277b54SJayaprakash Mutyala                                               const std::set<std::pair<
28204f277b54SJayaprakash Mutyala                                                   std::string, std::string>>&
2821413961deSRichard Marian Thomaiyar                                                   objectsWithConnection) {
2822f65af9e8SRichard Marian Thomaiyar             if (objectsWithConnection.size() != overrideMap.size())
2823413961deSRichard Marian Thomaiyar             {
2824413961deSRichard Marian Thomaiyar                 BMCWEB_LOG_INFO
2825f65af9e8SRichard Marian Thomaiyar                     << "Unable to find all objects with proper connection "
2826f65af9e8SRichard Marian Thomaiyar                     << objectsWithConnection.size() << " requested "
2827f65af9e8SRichard Marian Thomaiyar                     << overrideMap.size() << "\n";
28284f277b54SJayaprakash Mutyala                 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2829a0ec28b6SAdrian Ambrożewicz                                            sensorAsyncResp->chassisSubNode ==
2830a0ec28b6SAdrian Ambrożewicz                                                    sensors::node::thermal
2831413961deSRichard Marian Thomaiyar                                                ? "Temperatures"
2832413961deSRichard Marian Thomaiyar                                                : "Voltages",
2833f65af9e8SRichard Marian Thomaiyar                                            "Count");
2834f65af9e8SRichard Marian Thomaiyar                 return;
2835f65af9e8SRichard Marian Thomaiyar             }
2836f65af9e8SRichard Marian Thomaiyar             for (const auto& item : objectsWithConnection)
2837f65af9e8SRichard Marian Thomaiyar             {
283828aa8de5SGeorge Liu                 sdbusplus::message::object_path path(item.first);
283928aa8de5SGeorge Liu                 std::string sensorName = path.filename();
284028aa8de5SGeorge Liu                 if (sensorName.empty())
2841f65af9e8SRichard Marian Thomaiyar                 {
28424f277b54SJayaprakash Mutyala                     messages::internalError(sensorAsyncResp->asyncResp->res);
2843f65af9e8SRichard Marian Thomaiyar                     return;
2844f65af9e8SRichard Marian Thomaiyar                 }
2845f65af9e8SRichard Marian Thomaiyar 
2846f65af9e8SRichard Marian Thomaiyar                 const auto& iterator = overrideMap.find(sensorName);
2847f65af9e8SRichard Marian Thomaiyar                 if (iterator == overrideMap.end())
2848f65af9e8SRichard Marian Thomaiyar                 {
2849f65af9e8SRichard Marian Thomaiyar                     BMCWEB_LOG_INFO << "Unable to find sensor object"
2850f65af9e8SRichard Marian Thomaiyar                                     << item.first << "\n";
28514f277b54SJayaprakash Mutyala                     messages::internalError(sensorAsyncResp->asyncResp->res);
2852413961deSRichard Marian Thomaiyar                     return;
2853413961deSRichard Marian Thomaiyar                 }
2854413961deSRichard Marian Thomaiyar                 crow::connections::systemBus->async_method_call(
2855f65af9e8SRichard Marian Thomaiyar                     [sensorAsyncResp](const boost::system::error_code ec) {
2856413961deSRichard Marian Thomaiyar                         if (ec)
2857413961deSRichard Marian Thomaiyar                         {
28584f277b54SJayaprakash Mutyala                             if (ec.value() ==
28594f277b54SJayaprakash Mutyala                                 boost::system::errc::permission_denied)
28604f277b54SJayaprakash Mutyala                             {
28614f277b54SJayaprakash Mutyala                                 BMCWEB_LOG_WARNING
28624f277b54SJayaprakash Mutyala                                     << "Manufacturing mode is not Enabled...can't "
28634f277b54SJayaprakash Mutyala                                        "Override the sensor value. ";
28644f277b54SJayaprakash Mutyala 
28654f277b54SJayaprakash Mutyala                                 messages::insufficientPrivilege(
28668d1b46d7Szhanghch05                                     sensorAsyncResp->asyncResp->res);
2867413961deSRichard Marian Thomaiyar                                 return;
2868413961deSRichard Marian Thomaiyar                             }
28694f277b54SJayaprakash Mutyala                             BMCWEB_LOG_DEBUG
28704f277b54SJayaprakash Mutyala                                 << "setOverrideValueStatus DBUS error: " << ec;
28714f277b54SJayaprakash Mutyala                             messages::internalError(
28724f277b54SJayaprakash Mutyala                                 sensorAsyncResp->asyncResp->res);
28734f277b54SJayaprakash Mutyala                         }
2874413961deSRichard Marian Thomaiyar                     },
28754f277b54SJayaprakash Mutyala                     item.second, item.first, "org.freedesktop.DBus.Properties",
28764f277b54SJayaprakash Mutyala                     "Set", "xyz.openbmc_project.Sensor.Value", "Value",
2877168e20c1SEd Tanous                     dbus::utility::DbusVariantType(iterator->second.first));
2878f65af9e8SRichard Marian Thomaiyar             }
2879413961deSRichard Marian Thomaiyar         };
2880413961deSRichard Marian Thomaiyar         // Get object with connection for the given sensor name
2881413961deSRichard Marian Thomaiyar         getObjectsWithConnection(sensorAsyncResp, sensorNames,
2882413961deSRichard Marian Thomaiyar                                  std::move(getObjectsWithConnectionCb));
2883413961deSRichard Marian Thomaiyar     };
2884413961deSRichard Marian Thomaiyar     // get full sensor list for the given chassisId and cross verify the sensor.
2885413961deSRichard Marian Thomaiyar     getChassis(sensorAsyncResp, std::move(getChassisSensorListCb));
2886413961deSRichard Marian Thomaiyar }
2887413961deSRichard Marian Thomaiyar 
2888a0ec28b6SAdrian Ambrożewicz /**
2889a0ec28b6SAdrian Ambrożewicz  * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
2890a0ec28b6SAdrian Ambrożewicz  * path of the sensor.
2891a0ec28b6SAdrian Ambrożewicz  *
2892a0ec28b6SAdrian Ambrożewicz  * Function builds valid Redfish response for sensor query of given chassis and
2893a0ec28b6SAdrian Ambrożewicz  * node. It then builds metadata about Redfish<->D-Bus correlations and provides
2894a0ec28b6SAdrian Ambrożewicz  * it to caller in a callback.
2895a0ec28b6SAdrian Ambrożewicz  *
2896a0ec28b6SAdrian Ambrożewicz  * @param chassis   Chassis for which retrieval should be performed
2897a0ec28b6SAdrian Ambrożewicz  * @param node  Node (group) of sensors. See sensors::node for supported values
2898a0ec28b6SAdrian Ambrożewicz  * @param mapComplete   Callback to be called with retrieval result
2899a0ec28b6SAdrian Ambrożewicz  */
2900021d32cfSKrzysztof Grobelny inline void retrieveUriToDbusMap(const std::string& chassis,
2901021d32cfSKrzysztof Grobelny                                  const std::string& node,
2902a0ec28b6SAdrian Ambrożewicz                                  SensorsAsyncResp::DataCompleteCb&& mapComplete)
2903a0ec28b6SAdrian Ambrożewicz {
2904c2bf7f99SWludzik, Jozef     auto pathIt = sensors::dbus::paths.find(node);
2905c2bf7f99SWludzik, Jozef     if (pathIt == sensors::dbus::paths.end())
2906a0ec28b6SAdrian Ambrożewicz     {
2907a0ec28b6SAdrian Ambrożewicz         BMCWEB_LOG_ERROR << "Wrong node provided : " << node;
2908a0ec28b6SAdrian Ambrożewicz         mapComplete(boost::beast::http::status::bad_request, {});
2909a0ec28b6SAdrian Ambrożewicz         return;
2910a0ec28b6SAdrian Ambrożewicz     }
2911d51e072fSKrzysztof Grobelny 
291272374eb7SNan Zhou     auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
2913a0ec28b6SAdrian Ambrożewicz     auto callback =
291472374eb7SNan Zhou         [asyncResp, mapCompleteCb{std::move(mapComplete)}](
2915a0ec28b6SAdrian Ambrożewicz             const boost::beast::http::status status,
2916a0ec28b6SAdrian Ambrożewicz             const boost::container::flat_map<std::string, std::string>&
2917a0ec28b6SAdrian Ambrożewicz                 uriToDbus) { mapCompleteCb(status, uriToDbus); };
2918a0ec28b6SAdrian Ambrożewicz 
2919a0ec28b6SAdrian Ambrożewicz     auto resp = std::make_shared<SensorsAsyncResp>(
2920d51e072fSKrzysztof Grobelny         asyncResp, chassis, pathIt->second, node, std::move(callback));
2921a0ec28b6SAdrian Ambrożewicz     getChassisData(resp);
2922a0ec28b6SAdrian Ambrożewicz }
2923a0ec28b6SAdrian Ambrożewicz 
2924*bacb2162SNan Zhou namespace sensors
2925*bacb2162SNan Zhou {
2926*bacb2162SNan Zhou inline void getChassisCallback(
2927*bacb2162SNan Zhou     const std::shared_ptr<SensorsAsyncResp>& asyncResp,
2928*bacb2162SNan Zhou     const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames)
2929*bacb2162SNan Zhou {
2930*bacb2162SNan Zhou     BMCWEB_LOG_DEBUG << "getChassisCallback enter";
2931*bacb2162SNan Zhou 
2932*bacb2162SNan Zhou     nlohmann::json& entriesArray =
2933*bacb2162SNan Zhou         asyncResp->asyncResp->res.jsonValue["Members"];
2934*bacb2162SNan Zhou     for (auto& sensor : *sensorNames)
2935*bacb2162SNan Zhou     {
2936*bacb2162SNan Zhou         BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor;
2937*bacb2162SNan Zhou 
2938*bacb2162SNan Zhou         sdbusplus::message::object_path path(sensor);
2939*bacb2162SNan Zhou         std::string sensorName = path.filename();
2940*bacb2162SNan Zhou         if (sensorName.empty())
2941*bacb2162SNan Zhou         {
2942*bacb2162SNan Zhou             BMCWEB_LOG_ERROR << "Invalid sensor path: " << sensor;
2943*bacb2162SNan Zhou             messages::internalError(asyncResp->asyncResp->res);
2944*bacb2162SNan Zhou             return;
2945*bacb2162SNan Zhou         }
2946*bacb2162SNan Zhou         entriesArray.push_back(
2947*bacb2162SNan Zhou             {{"@odata.id", "/redfish/v1/Chassis/" + asyncResp->chassisId + "/" +
2948*bacb2162SNan Zhou                                asyncResp->chassisSubNode + "/" + sensorName}});
2949*bacb2162SNan Zhou     }
2950*bacb2162SNan Zhou 
2951*bacb2162SNan Zhou     asyncResp->asyncResp->res.jsonValue["Members@odata.count"] =
2952*bacb2162SNan Zhou         entriesArray.size();
2953*bacb2162SNan Zhou     BMCWEB_LOG_DEBUG << "getChassisCallback exit";
2954*bacb2162SNan Zhou }
2955*bacb2162SNan Zhou } // namespace sensors
2956*bacb2162SNan Zhou 
29577e860f15SJohn Edward Broadbent inline void requestRoutesSensorCollection(App& app)
295895a3ecadSAnthony Wilson {
29597e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/")
2960ed398213SEd Tanous         .privileges(redfish::privileges::getSensorCollection)
296145ca1b86SEd Tanous         .methods(boost::beast::http::verb::get)(
296245ca1b86SEd Tanous             [&app](const crow::Request& req,
296345ca1b86SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& aResp,
29647e860f15SJohn Edward Broadbent                    const std::string& chassisId) {
296545ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, aResp->res))
296645ca1b86SEd Tanous                 {
296745ca1b86SEd Tanous                     return;
296845ca1b86SEd Tanous                 }
296945ca1b86SEd Tanous 
297095a3ecadSAnthony Wilson                 BMCWEB_LOG_DEBUG << "SensorCollection doGet enter";
29718d1b46d7Szhanghch05 
297295a3ecadSAnthony Wilson                 std::shared_ptr<SensorsAsyncResp> asyncResp =
2973a0ec28b6SAdrian Ambrożewicz                     std::make_shared<SensorsAsyncResp>(
29748d1b46d7Szhanghch05                         aResp, chassisId,
29758d1b46d7Szhanghch05                         sensors::dbus::paths.at(sensors::node::sensors),
2976a0ec28b6SAdrian Ambrożewicz                         sensors::node::sensors);
297795a3ecadSAnthony Wilson                 // Get set of sensors in chassis
2978*bacb2162SNan Zhou                 getChassis(
2979*bacb2162SNan Zhou                     asyncResp,
2980*bacb2162SNan Zhou                     std::bind_front(sensors::getChassisCallback, asyncResp));
298195a3ecadSAnthony Wilson                 BMCWEB_LOG_DEBUG << "SensorCollection doGet exit";
29827e860f15SJohn Edward Broadbent             });
298395a3ecadSAnthony Wilson }
298495a3ecadSAnthony Wilson 
29857e860f15SJohn Edward Broadbent inline void requestRoutesSensor(App& app)
298695a3ecadSAnthony Wilson {
29877e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/")
2988ed398213SEd Tanous         .privileges(redfish::privileges::getSensor)
29897e860f15SJohn Edward Broadbent         .methods(
299045ca1b86SEd Tanous             boost::beast::http::verb::get)([&app](
299145ca1b86SEd Tanous                                                const crow::Request& req,
29927e860f15SJohn Edward Broadbent                                                const std::shared_ptr<
29937e860f15SJohn Edward Broadbent                                                    bmcweb::AsyncResp>& aResp,
29947e860f15SJohn Edward Broadbent                                                const std::string& chassisId,
29957e860f15SJohn Edward Broadbent                                                const std::string& sensorName) {
299645ca1b86SEd Tanous             if (!redfish::setUpRedfishRoute(app, req, aResp->res))
299745ca1b86SEd Tanous             {
299845ca1b86SEd Tanous                 return;
299945ca1b86SEd Tanous             }
300095a3ecadSAnthony Wilson             BMCWEB_LOG_DEBUG << "Sensor doGet enter";
300195a3ecadSAnthony Wilson             std::shared_ptr<SensorsAsyncResp> asyncResp =
30028d1b46d7Szhanghch05                 std::make_shared<SensorsAsyncResp>(aResp, chassisId,
3003a0ec28b6SAdrian Ambrożewicz                                                    std::vector<const char*>(),
3004a0ec28b6SAdrian Ambrożewicz                                                    sensors::node::sensors);
300595a3ecadSAnthony Wilson 
300695a3ecadSAnthony Wilson             const std::array<const char*, 1> interfaces = {
300795a3ecadSAnthony Wilson                 "xyz.openbmc_project.Sensor.Value"};
300895a3ecadSAnthony Wilson 
300995a3ecadSAnthony Wilson             // Get a list of all of the sensors that implement Sensor.Value
301095a3ecadSAnthony Wilson             // and get the path and service name associated with the sensor
301195a3ecadSAnthony Wilson             crow::connections::systemBus->async_method_call(
3012b9d36b47SEd Tanous                 [asyncResp, sensorName](
3013b9d36b47SEd Tanous                     const boost::system::error_code ec,
3014b9d36b47SEd Tanous                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
301595a3ecadSAnthony Wilson                     BMCWEB_LOG_DEBUG << "respHandler1 enter";
301695a3ecadSAnthony Wilson                     if (ec)
301795a3ecadSAnthony Wilson                     {
30188d1b46d7Szhanghch05                         messages::internalError(asyncResp->asyncResp->res);
30197e860f15SJohn Edward Broadbent                         BMCWEB_LOG_ERROR
30207e860f15SJohn Edward Broadbent                             << "Sensor getSensorPaths resp_handler: "
302195a3ecadSAnthony Wilson                             << "Dbus error " << ec;
302295a3ecadSAnthony Wilson                         return;
302395a3ecadSAnthony Wilson                     }
302495a3ecadSAnthony Wilson 
3025b9d36b47SEd Tanous                     dbus::utility::MapperGetSubTreeResponse::const_iterator it =
3026b9d36b47SEd Tanous                         std::find_if(
302795a3ecadSAnthony Wilson                             subtree.begin(), subtree.end(),
302895a3ecadSAnthony Wilson                             [sensorName](
3029b9d36b47SEd Tanous                                 const std::pair<std::string,
30307e860f15SJohn Edward Broadbent                                                 std::vector<std::pair<
3031b9d36b47SEd Tanous                                                     std::string,
3032b9d36b47SEd Tanous                                                     std::vector<std::string>>>>&
303395a3ecadSAnthony Wilson                                     object) {
3034b9d36b47SEd Tanous                                 sdbusplus::message::object_path path(
3035b9d36b47SEd Tanous                                     object.first);
303628aa8de5SGeorge Liu                                 std::string name = path.filename();
303728aa8de5SGeorge Liu                                 if (name.empty())
303895a3ecadSAnthony Wilson                                 {
303995a3ecadSAnthony Wilson                                     BMCWEB_LOG_ERROR << "Invalid sensor path: "
304028aa8de5SGeorge Liu                                                      << object.first;
304195a3ecadSAnthony Wilson                                     return false;
304295a3ecadSAnthony Wilson                                 }
304395a3ecadSAnthony Wilson 
304495a3ecadSAnthony Wilson                                 return name == sensorName;
304595a3ecadSAnthony Wilson                             });
304695a3ecadSAnthony Wilson 
304795a3ecadSAnthony Wilson                     if (it == subtree.end())
304895a3ecadSAnthony Wilson                     {
304995a3ecadSAnthony Wilson                         BMCWEB_LOG_ERROR << "Could not find path for sensor: "
305095a3ecadSAnthony Wilson                                          << sensorName;
30518d1b46d7Szhanghch05                         messages::resourceNotFound(asyncResp->asyncResp->res,
30528d1b46d7Szhanghch05                                                    "Sensor", sensorName);
305395a3ecadSAnthony Wilson                         return;
305495a3ecadSAnthony Wilson                     }
305595a3ecadSAnthony Wilson                     std::string_view sensorPath = (*it).first;
305695a3ecadSAnthony Wilson                     BMCWEB_LOG_DEBUG << "Found sensor path for sensor '"
305795a3ecadSAnthony Wilson                                      << sensorName << "': " << sensorPath;
305895a3ecadSAnthony Wilson 
30597e860f15SJohn Edward Broadbent                     const std::shared_ptr<
30607e860f15SJohn Edward Broadbent                         boost::container::flat_set<std::string>>
306195a3ecadSAnthony Wilson                         sensorList = std::make_shared<
306295a3ecadSAnthony Wilson                             boost::container::flat_set<std::string>>();
306395a3ecadSAnthony Wilson 
306495a3ecadSAnthony Wilson                     sensorList->emplace(sensorPath);
306595a3ecadSAnthony Wilson                     processSensorList(asyncResp, sensorList);
306695a3ecadSAnthony Wilson                     BMCWEB_LOG_DEBUG << "respHandler1 exit";
306795a3ecadSAnthony Wilson                 },
306895a3ecadSAnthony Wilson                 "xyz.openbmc_project.ObjectMapper",
306995a3ecadSAnthony Wilson                 "/xyz/openbmc_project/object_mapper",
307095a3ecadSAnthony Wilson                 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
307195a3ecadSAnthony Wilson                 "/xyz/openbmc_project/sensors", 2, interfaces);
30727e860f15SJohn Edward Broadbent         });
307395a3ecadSAnthony Wilson }
307495a3ecadSAnthony Wilson 
307508777fb0SLewanczyk, Dawid } // namespace redfish
3076