xref: /openbmc/bmcweb/features/redfish/lib/sensors.hpp (revision e6bd846d38bf7bb565b3f6a9aa8236543feb59f4)
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>
29928fefb9SNan Zhou #include <utils/query_param.hpp>
301214b7e7SGunnar Mills 
311214b7e7SGunnar Mills #include <cmath>
32b5a76932SEd Tanous #include <utility>
33abf2add6SEd Tanous #include <variant>
3408777fb0SLewanczyk, Dawid 
351abe55efSEd Tanous namespace redfish
361abe55efSEd Tanous {
3708777fb0SLewanczyk, Dawid 
38a0ec28b6SAdrian Ambrożewicz namespace sensors
39a0ec28b6SAdrian Ambrożewicz {
40a0ec28b6SAdrian Ambrożewicz namespace node
41a0ec28b6SAdrian Ambrożewicz {
42a0ec28b6SAdrian Ambrożewicz static constexpr std::string_view power = "Power";
43a0ec28b6SAdrian Ambrożewicz static constexpr std::string_view sensors = "Sensors";
44a0ec28b6SAdrian Ambrożewicz static constexpr std::string_view thermal = "Thermal";
45a0ec28b6SAdrian Ambrożewicz } // namespace node
46a0ec28b6SAdrian Ambrożewicz 
4702da7c5aSEd Tanous // clang-format off
48a0ec28b6SAdrian Ambrożewicz namespace dbus
49a0ec28b6SAdrian Ambrożewicz {
5002da7c5aSEd Tanous auto powerPaths = std::to_array<std::string_view>({
5102da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/voltage",
5202da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/power"
5302da7c5aSEd Tanous });
54c2bf7f99SWludzik, Jozef 
5502da7c5aSEd Tanous auto sensorPaths = std::to_array<std::string_view>({
5602da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/power",
57a0ec28b6SAdrian Ambrożewicz     "/xyz/openbmc_project/sensors/current",
587088690cSBasheer Ahmed Muddebihal     "/xyz/openbmc_project/sensors/airflow",
595deabed9SGunnar Mills                "/xyz/openbmc_project/sensors/humidity",
60e8204933SGeorge Liu #ifdef BMCWEB_NEW_POWERSUBSYSTEM_THERMALSUBSYSTEM
61e8204933SGeorge Liu     "/xyz/openbmc_project/sensors/voltage",
62e8204933SGeorge Liu     "/xyz/openbmc_project/sensors/fan_tach",
63e8204933SGeorge Liu     "/xyz/openbmc_project/sensors/temperature",
64e8204933SGeorge Liu     "/xyz/openbmc_project/sensors/fan_pwm",
65e8204933SGeorge Liu     "/xyz/openbmc_project/sensors/altitude",
66e8204933SGeorge Liu     "/xyz/openbmc_project/sensors/energy",
67e8204933SGeorge Liu #endif
6802da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/utilization"
6902da7c5aSEd Tanous });
7002da7c5aSEd Tanous 
7102da7c5aSEd Tanous auto thermalPaths = std::to_array<std::string_view>({
7202da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/fan_tach",
73a0ec28b6SAdrian Ambrożewicz     "/xyz/openbmc_project/sensors/temperature",
7402da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/fan_pwm"
7502da7c5aSEd Tanous });
7602da7c5aSEd Tanous 
77c2bf7f99SWludzik, Jozef } // namespace dbus
7802da7c5aSEd Tanous // clang-format on
7902da7c5aSEd Tanous 
8002da7c5aSEd Tanous using sensorPair = std::pair<std::string_view, std::span<std::string_view>>;
8102da7c5aSEd Tanous static constexpr std::array<sensorPair, 3> paths = {
8202da7c5aSEd Tanous     {{node::power, std::span<std::string_view>(dbus::powerPaths)},
8302da7c5aSEd Tanous      {node::sensors, std::span<std::string_view>(dbus::sensorPaths)},
8402da7c5aSEd Tanous      {node::thermal, std::span<std::string_view>(dbus::thermalPaths)}}};
85c2bf7f99SWludzik, Jozef 
86c2bf7f99SWludzik, Jozef inline const char* toReadingType(const std::string& sensorType)
87c2bf7f99SWludzik, Jozef {
88c2bf7f99SWludzik, Jozef     if (sensorType == "voltage")
89c2bf7f99SWludzik, Jozef     {
90c2bf7f99SWludzik, Jozef         return "Voltage";
91c2bf7f99SWludzik, Jozef     }
92c2bf7f99SWludzik, Jozef     if (sensorType == "power")
93c2bf7f99SWludzik, Jozef     {
94c2bf7f99SWludzik, Jozef         return "Power";
95c2bf7f99SWludzik, Jozef     }
96c2bf7f99SWludzik, Jozef     if (sensorType == "current")
97c2bf7f99SWludzik, Jozef     {
98c2bf7f99SWludzik, Jozef         return "Current";
99c2bf7f99SWludzik, Jozef     }
100c2bf7f99SWludzik, Jozef     if (sensorType == "fan_tach")
101c2bf7f99SWludzik, Jozef     {
102c2bf7f99SWludzik, Jozef         return "Rotational";
103c2bf7f99SWludzik, Jozef     }
104c2bf7f99SWludzik, Jozef     if (sensorType == "temperature")
105c2bf7f99SWludzik, Jozef     {
106c2bf7f99SWludzik, Jozef         return "Temperature";
107c2bf7f99SWludzik, Jozef     }
108c2bf7f99SWludzik, Jozef     if (sensorType == "fan_pwm" || sensorType == "utilization")
109c2bf7f99SWludzik, Jozef     {
110c2bf7f99SWludzik, Jozef         return "Percent";
111c2bf7f99SWludzik, Jozef     }
1125deabed9SGunnar Mills     if (sensorType == "humidity")
1135deabed9SGunnar Mills     {
1145deabed9SGunnar Mills         return "Humidity";
1155deabed9SGunnar Mills     }
116c2bf7f99SWludzik, Jozef     if (sensorType == "altitude")
117c2bf7f99SWludzik, Jozef     {
118c2bf7f99SWludzik, Jozef         return "Altitude";
119c2bf7f99SWludzik, Jozef     }
120c2bf7f99SWludzik, Jozef     if (sensorType == "airflow")
121c2bf7f99SWludzik, Jozef     {
122c2bf7f99SWludzik, Jozef         return "AirFlow";
123c2bf7f99SWludzik, Jozef     }
124c2bf7f99SWludzik, Jozef     if (sensorType == "energy")
125c2bf7f99SWludzik, Jozef     {
126c2bf7f99SWludzik, Jozef         return "EnergyJoules";
127c2bf7f99SWludzik, Jozef     }
128c2bf7f99SWludzik, Jozef     return "";
129c2bf7f99SWludzik, Jozef }
130c2bf7f99SWludzik, Jozef 
131c2bf7f99SWludzik, Jozef inline const char* toReadingUnits(const std::string& sensorType)
132c2bf7f99SWludzik, Jozef {
133c2bf7f99SWludzik, Jozef     if (sensorType == "voltage")
134c2bf7f99SWludzik, Jozef     {
135c2bf7f99SWludzik, Jozef         return "V";
136c2bf7f99SWludzik, Jozef     }
137c2bf7f99SWludzik, Jozef     if (sensorType == "power")
138c2bf7f99SWludzik, Jozef     {
139c2bf7f99SWludzik, Jozef         return "W";
140c2bf7f99SWludzik, Jozef     }
141c2bf7f99SWludzik, Jozef     if (sensorType == "current")
142c2bf7f99SWludzik, Jozef     {
143c2bf7f99SWludzik, Jozef         return "A";
144c2bf7f99SWludzik, Jozef     }
145c2bf7f99SWludzik, Jozef     if (sensorType == "fan_tach")
146c2bf7f99SWludzik, Jozef     {
147c2bf7f99SWludzik, Jozef         return "RPM";
148c2bf7f99SWludzik, Jozef     }
149c2bf7f99SWludzik, Jozef     if (sensorType == "temperature")
150c2bf7f99SWludzik, Jozef     {
151c2bf7f99SWludzik, Jozef         return "Cel";
152c2bf7f99SWludzik, Jozef     }
1535deabed9SGunnar Mills     if (sensorType == "fan_pwm" || sensorType == "utilization" ||
1545deabed9SGunnar Mills         sensorType == "humidity")
155c2bf7f99SWludzik, Jozef     {
156c2bf7f99SWludzik, Jozef         return "%";
157c2bf7f99SWludzik, Jozef     }
158c2bf7f99SWludzik, Jozef     if (sensorType == "altitude")
159c2bf7f99SWludzik, Jozef     {
160c2bf7f99SWludzik, Jozef         return "m";
161c2bf7f99SWludzik, Jozef     }
162c2bf7f99SWludzik, Jozef     if (sensorType == "airflow")
163c2bf7f99SWludzik, Jozef     {
164c2bf7f99SWludzik, Jozef         return "cft_i/min";
165c2bf7f99SWludzik, Jozef     }
166c2bf7f99SWludzik, Jozef     if (sensorType == "energy")
167c2bf7f99SWludzik, Jozef     {
168c2bf7f99SWludzik, Jozef         return "J";
169c2bf7f99SWludzik, Jozef     }
170c2bf7f99SWludzik, Jozef     return "";
171a0ec28b6SAdrian Ambrożewicz }
172a0ec28b6SAdrian Ambrożewicz } // namespace sensors
173a0ec28b6SAdrian Ambrożewicz 
17408777fb0SLewanczyk, Dawid /**
175588c3f0dSKowalski, Kamil  * SensorsAsyncResp
17608777fb0SLewanczyk, Dawid  * Gathers data needed for response processing after async calls are done
17708777fb0SLewanczyk, Dawid  */
1781abe55efSEd Tanous class SensorsAsyncResp
1791abe55efSEd Tanous {
18008777fb0SLewanczyk, Dawid   public:
181a0ec28b6SAdrian Ambrożewicz     using DataCompleteCb = std::function<void(
182a0ec28b6SAdrian Ambrożewicz         const boost::beast::http::status status,
183a0ec28b6SAdrian Ambrożewicz         const boost::container::flat_map<std::string, std::string>& uriToDbus)>;
184a0ec28b6SAdrian Ambrożewicz 
185a0ec28b6SAdrian Ambrożewicz     struct SensorData
186a0ec28b6SAdrian Ambrożewicz     {
187a0ec28b6SAdrian Ambrożewicz         const std::string name;
188a0ec28b6SAdrian Ambrożewicz         std::string uri;
189a0ec28b6SAdrian Ambrożewicz         const std::string valueKey;
190a0ec28b6SAdrian Ambrożewicz         const std::string dbusPath;
191a0ec28b6SAdrian Ambrożewicz     };
192a0ec28b6SAdrian Ambrożewicz 
1938d1b46d7Szhanghch05     SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1948d1b46d7Szhanghch05                      const std::string& chassisIdIn,
19502da7c5aSEd Tanous                      std::span<std::string_view> typesIn,
19602da7c5aSEd Tanous                      std::string_view subNode) :
1978d1b46d7Szhanghch05         asyncResp(asyncResp),
198928fefb9SNan Zhou         chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
199928fefb9SNan Zhou         efficientExpand(false)
2001214b7e7SGunnar Mills     {}
20108777fb0SLewanczyk, Dawid 
202a0ec28b6SAdrian Ambrożewicz     // Store extra data about sensor mapping and return it in callback
2038d1b46d7Szhanghch05     SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2048d1b46d7Szhanghch05                      const std::string& chassisIdIn,
20502da7c5aSEd Tanous                      std::span<std::string_view> typesIn,
20602da7c5aSEd Tanous                      std::string_view subNode,
207a0ec28b6SAdrian Ambrożewicz                      DataCompleteCb&& creationComplete) :
2088d1b46d7Szhanghch05         asyncResp(asyncResp),
209928fefb9SNan Zhou         chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
210928fefb9SNan Zhou         efficientExpand(false), metadata{std::vector<SensorData>()},
211a0ec28b6SAdrian Ambrożewicz         dataComplete{std::move(creationComplete)}
212a0ec28b6SAdrian Ambrożewicz     {}
213a0ec28b6SAdrian Ambrożewicz 
214928fefb9SNan Zhou     // sensor collections expand
215928fefb9SNan Zhou     SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
216928fefb9SNan Zhou                      const std::string& chassisIdIn,
21702da7c5aSEd Tanous                      const std::span<std::string_view> typesIn,
218928fefb9SNan Zhou                      const std::string_view& subNode, bool efficientExpand) :
219928fefb9SNan Zhou         asyncResp(asyncResp),
220928fefb9SNan Zhou         chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
221928fefb9SNan Zhou         efficientExpand(efficientExpand)
222928fefb9SNan Zhou     {}
223928fefb9SNan Zhou 
2241abe55efSEd Tanous     ~SensorsAsyncResp()
2251abe55efSEd Tanous     {
2268d1b46d7Szhanghch05         if (asyncResp->res.result() ==
2278d1b46d7Szhanghch05             boost::beast::http::status::internal_server_error)
2281abe55efSEd Tanous         {
2291abe55efSEd Tanous             // Reset the json object to clear out any data that made it in
2301abe55efSEd Tanous             // before the error happened todo(ed) handle error condition with
2311abe55efSEd Tanous             // proper code
2328d1b46d7Szhanghch05             asyncResp->res.jsonValue = nlohmann::json::object();
23308777fb0SLewanczyk, Dawid         }
234a0ec28b6SAdrian Ambrożewicz 
235a0ec28b6SAdrian Ambrożewicz         if (dataComplete && metadata)
236a0ec28b6SAdrian Ambrożewicz         {
237a0ec28b6SAdrian Ambrożewicz             boost::container::flat_map<std::string, std::string> map;
2388d1b46d7Szhanghch05             if (asyncResp->res.result() == boost::beast::http::status::ok)
239a0ec28b6SAdrian Ambrożewicz             {
240a0ec28b6SAdrian Ambrożewicz                 for (auto& sensor : *metadata)
241a0ec28b6SAdrian Ambrożewicz                 {
242a0ec28b6SAdrian Ambrożewicz                     map.insert(std::make_pair(sensor.uri + sensor.valueKey,
243a0ec28b6SAdrian Ambrożewicz                                               sensor.dbusPath));
244a0ec28b6SAdrian Ambrożewicz                 }
245a0ec28b6SAdrian Ambrożewicz             }
2468d1b46d7Szhanghch05             dataComplete(asyncResp->res.result(), map);
247a0ec28b6SAdrian Ambrożewicz         }
24808777fb0SLewanczyk, Dawid     }
249588c3f0dSKowalski, Kamil 
250ecd6a3a2SEd Tanous     SensorsAsyncResp(const SensorsAsyncResp&) = delete;
251ecd6a3a2SEd Tanous     SensorsAsyncResp(SensorsAsyncResp&&) = delete;
252ecd6a3a2SEd Tanous     SensorsAsyncResp& operator=(const SensorsAsyncResp&) = delete;
253ecd6a3a2SEd Tanous     SensorsAsyncResp& operator=(SensorsAsyncResp&&) = delete;
254ecd6a3a2SEd Tanous 
255a0ec28b6SAdrian Ambrożewicz     void addMetadata(const nlohmann::json& sensorObject,
256a0ec28b6SAdrian Ambrożewicz                      const std::string& valueKey, const std::string& dbusPath)
257a0ec28b6SAdrian Ambrożewicz     {
258a0ec28b6SAdrian Ambrożewicz         if (metadata)
259a0ec28b6SAdrian Ambrożewicz         {
260a0ec28b6SAdrian Ambrożewicz             metadata->emplace_back(SensorData{sensorObject["Name"],
261a0ec28b6SAdrian Ambrożewicz                                               sensorObject["@odata.id"],
262a0ec28b6SAdrian Ambrożewicz                                               valueKey, dbusPath});
263a0ec28b6SAdrian Ambrożewicz         }
264a0ec28b6SAdrian Ambrożewicz     }
265a0ec28b6SAdrian Ambrożewicz 
266a0ec28b6SAdrian Ambrożewicz     void updateUri(const std::string& name, const std::string& uri)
267a0ec28b6SAdrian Ambrożewicz     {
268a0ec28b6SAdrian Ambrożewicz         if (metadata)
269a0ec28b6SAdrian Ambrożewicz         {
270a0ec28b6SAdrian Ambrożewicz             for (auto& sensor : *metadata)
271a0ec28b6SAdrian Ambrożewicz             {
272a0ec28b6SAdrian Ambrożewicz                 if (sensor.name == name)
273a0ec28b6SAdrian Ambrożewicz                 {
274a0ec28b6SAdrian Ambrożewicz                     sensor.uri = uri;
275a0ec28b6SAdrian Ambrożewicz                 }
276a0ec28b6SAdrian Ambrożewicz             }
277a0ec28b6SAdrian Ambrożewicz         }
278a0ec28b6SAdrian Ambrożewicz     }
279a0ec28b6SAdrian Ambrożewicz 
2808d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp> asyncResp;
281a0ec28b6SAdrian Ambrożewicz     const std::string chassisId;
28202da7c5aSEd Tanous     const std::span<std::string_view> types;
283a0ec28b6SAdrian Ambrożewicz     const std::string chassisSubNode;
284928fefb9SNan Zhou     const bool efficientExpand;
285a0ec28b6SAdrian Ambrożewicz 
286a0ec28b6SAdrian Ambrożewicz   private:
287a0ec28b6SAdrian Ambrożewicz     std::optional<std::vector<SensorData>> metadata;
288a0ec28b6SAdrian Ambrożewicz     DataCompleteCb dataComplete;
28908777fb0SLewanczyk, Dawid };
29008777fb0SLewanczyk, Dawid 
29108777fb0SLewanczyk, Dawid /**
292d500549bSAnthony Wilson  * Possible states for physical inventory leds
293d500549bSAnthony Wilson  */
294d500549bSAnthony Wilson enum class LedState
295d500549bSAnthony Wilson {
296d500549bSAnthony Wilson     OFF,
297d500549bSAnthony Wilson     ON,
298d500549bSAnthony Wilson     BLINK,
299d500549bSAnthony Wilson     UNKNOWN
300d500549bSAnthony Wilson };
301d500549bSAnthony Wilson 
302d500549bSAnthony Wilson /**
303adc4f0dbSShawn McCarney  * D-Bus inventory item associated with one or more sensors.
304adc4f0dbSShawn McCarney  */
305adc4f0dbSShawn McCarney class InventoryItem
306adc4f0dbSShawn McCarney {
307adc4f0dbSShawn McCarney   public:
308e05aec50SEd Tanous     InventoryItem(const std::string& objPath) : objectPath(objPath)
309adc4f0dbSShawn McCarney     {
310adc4f0dbSShawn McCarney         // Set inventory item name to last node of object path
31128aa8de5SGeorge Liu         sdbusplus::message::object_path path(objectPath);
31228aa8de5SGeorge Liu         name = path.filename();
31328aa8de5SGeorge Liu         if (name.empty())
314adc4f0dbSShawn McCarney         {
31528aa8de5SGeorge Liu             BMCWEB_LOG_ERROR << "Failed to find '/' in " << objectPath;
316adc4f0dbSShawn McCarney         }
317adc4f0dbSShawn McCarney     }
318adc4f0dbSShawn McCarney 
319adc4f0dbSShawn McCarney     std::string objectPath;
320adc4f0dbSShawn McCarney     std::string name;
321e05aec50SEd Tanous     bool isPresent = true;
322e05aec50SEd Tanous     bool isFunctional = true;
323e05aec50SEd Tanous     bool isPowerSupply = false;
324e05aec50SEd Tanous     int powerSupplyEfficiencyPercent = -1;
325adc4f0dbSShawn McCarney     std::string manufacturer;
326adc4f0dbSShawn McCarney     std::string model;
327adc4f0dbSShawn McCarney     std::string partNumber;
328adc4f0dbSShawn McCarney     std::string serialNumber;
329adc4f0dbSShawn McCarney     std::set<std::string> sensors;
330d500549bSAnthony Wilson     std::string ledObjectPath;
331e05aec50SEd Tanous     LedState ledState = LedState::UNKNOWN;
332adc4f0dbSShawn McCarney };
333adc4f0dbSShawn McCarney 
334adc4f0dbSShawn McCarney /**
335413961deSRichard Marian Thomaiyar  * @brief Get objects with connection necessary for sensors
336588c3f0dSKowalski, Kamil  * @param SensorsAsyncResp Pointer to object holding response data
33708777fb0SLewanczyk, Dawid  * @param sensorNames Sensors retrieved from chassis
33808777fb0SLewanczyk, Dawid  * @param callback Callback for processing gathered connections
33908777fb0SLewanczyk, Dawid  */
34008777fb0SLewanczyk, Dawid template <typename Callback>
341413961deSRichard Marian Thomaiyar void getObjectsWithConnection(
34281ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
343b5a76932SEd Tanous     const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames,
3441abe55efSEd Tanous     Callback&& callback)
3451abe55efSEd Tanous {
346413961deSRichard Marian Thomaiyar     BMCWEB_LOG_DEBUG << "getObjectsWithConnection enter";
34703b5bae3SJames Feist     const std::string path = "/xyz/openbmc_project/sensors";
34808777fb0SLewanczyk, Dawid     const std::array<std::string, 1> interfaces = {
34908777fb0SLewanczyk, Dawid         "xyz.openbmc_project.Sensor.Value"};
35008777fb0SLewanczyk, Dawid 
35108777fb0SLewanczyk, Dawid     // Response handler for parsing objects subtree
352002d39b4SEd Tanous     auto respHandler =
353002d39b4SEd Tanous         [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
354002d39b4SEd Tanous          sensorNames](const boost::system::error_code ec,
355002d39b4SEd Tanous                       const dbus::utility::MapperGetSubTreeResponse& subtree) {
356413961deSRichard Marian Thomaiyar         BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler enter";
3571abe55efSEd Tanous         if (ec)
3581abe55efSEd Tanous         {
3598d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
360413961deSRichard Marian Thomaiyar             BMCWEB_LOG_ERROR
361413961deSRichard Marian Thomaiyar                 << "getObjectsWithConnection resp_handler: Dbus error " << ec;
36208777fb0SLewanczyk, Dawid             return;
36308777fb0SLewanczyk, Dawid         }
36408777fb0SLewanczyk, Dawid 
36555c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "Found " << subtree.size() << " subtrees";
36608777fb0SLewanczyk, Dawid 
36708777fb0SLewanczyk, Dawid         // Make unique list of connections only for requested sensor types and
36808777fb0SLewanczyk, Dawid         // found in the chassis
36908777fb0SLewanczyk, Dawid         boost::container::flat_set<std::string> connections;
370413961deSRichard Marian Thomaiyar         std::set<std::pair<std::string, std::string>> objectsWithConnection;
3711abe55efSEd Tanous         // Intrinsic to avoid malloc.  Most systems will have < 8 sensor
3721abe55efSEd Tanous         // producers
37308777fb0SLewanczyk, Dawid         connections.reserve(8);
37408777fb0SLewanczyk, Dawid 
37549c53ac9SJohnathan Mantey         BMCWEB_LOG_DEBUG << "sensorNames list count: " << sensorNames->size();
37649c53ac9SJohnathan Mantey         for (const std::string& tsensor : *sensorNames)
3771abe55efSEd Tanous         {
37855c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "Sensor to find: " << tsensor;
37908777fb0SLewanczyk, Dawid         }
38008777fb0SLewanczyk, Dawid 
38108777fb0SLewanczyk, Dawid         for (const std::pair<
38208777fb0SLewanczyk, Dawid                  std::string,
38308777fb0SLewanczyk, Dawid                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
3841abe55efSEd Tanous                  object : subtree)
3851abe55efSEd Tanous         {
38649c53ac9SJohnathan Mantey             if (sensorNames->find(object.first) != sensorNames->end())
3871abe55efSEd Tanous             {
38849c53ac9SJohnathan Mantey                 for (const std::pair<std::string, std::vector<std::string>>&
3891abe55efSEd Tanous                          objData : object.second)
3901abe55efSEd Tanous                 {
39149c53ac9SJohnathan Mantey                     BMCWEB_LOG_DEBUG << "Adding connection: " << objData.first;
39208777fb0SLewanczyk, Dawid                     connections.insert(objData.first);
393de629b6eSShawn McCarney                     objectsWithConnection.insert(
394de629b6eSShawn McCarney                         std::make_pair(object.first, objData.first));
39508777fb0SLewanczyk, Dawid                 }
39608777fb0SLewanczyk, Dawid             }
39708777fb0SLewanczyk, Dawid         }
39855c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "Found " << connections.size() << " connections";
399413961deSRichard Marian Thomaiyar         callback(std::move(connections), std::move(objectsWithConnection));
400413961deSRichard Marian Thomaiyar         BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler exit";
40108777fb0SLewanczyk, Dawid     };
40208777fb0SLewanczyk, Dawid     // Make call to ObjectMapper to find all sensors objects
40355c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
40455c7b7a2SEd Tanous         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
4051abe55efSEd Tanous         "/xyz/openbmc_project/object_mapper",
4061abe55efSEd Tanous         "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 2, interfaces);
407413961deSRichard Marian Thomaiyar     BMCWEB_LOG_DEBUG << "getObjectsWithConnection exit";
408413961deSRichard Marian Thomaiyar }
409413961deSRichard Marian Thomaiyar 
410413961deSRichard Marian Thomaiyar /**
411413961deSRichard Marian Thomaiyar  * @brief Create connections necessary for sensors
412413961deSRichard Marian Thomaiyar  * @param SensorsAsyncResp Pointer to object holding response data
413413961deSRichard Marian Thomaiyar  * @param sensorNames Sensors retrieved from chassis
414413961deSRichard Marian Thomaiyar  * @param callback Callback for processing gathered connections
415413961deSRichard Marian Thomaiyar  */
416413961deSRichard Marian Thomaiyar template <typename Callback>
41749c53ac9SJohnathan Mantey void getConnections(
41881ce609eSEd Tanous     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
41949c53ac9SJohnathan Mantey     const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
420413961deSRichard Marian Thomaiyar     Callback&& callback)
421413961deSRichard Marian Thomaiyar {
422413961deSRichard Marian Thomaiyar     auto objectsWithConnectionCb =
423413961deSRichard Marian Thomaiyar         [callback](const boost::container::flat_set<std::string>& connections,
424413961deSRichard Marian Thomaiyar                    const std::set<std::pair<std::string, std::string>>&
4253174e4dfSEd Tanous                    /*objectsWithConnection*/) { callback(connections); };
42681ce609eSEd Tanous     getObjectsWithConnection(sensorsAsyncResp, sensorNames,
427413961deSRichard Marian Thomaiyar                              std::move(objectsWithConnectionCb));
42808777fb0SLewanczyk, Dawid }
42908777fb0SLewanczyk, Dawid 
43008777fb0SLewanczyk, Dawid /**
43149c53ac9SJohnathan Mantey  * @brief Shrinks the list of sensors for processing
43249c53ac9SJohnathan Mantey  * @param SensorsAysncResp  The class holding the Redfish response
43349c53ac9SJohnathan Mantey  * @param allSensors  A list of all the sensors associated to the
43449c53ac9SJohnathan Mantey  * chassis element (i.e. baseboard, front panel, etc...)
43549c53ac9SJohnathan Mantey  * @param activeSensors A list that is a reduction of the incoming
43649c53ac9SJohnathan Mantey  * allSensors list.  Eliminate Thermal sensors when a Power request is
43749c53ac9SJohnathan Mantey  * made, and eliminate Power sensors when a Thermal request is made.
43849c53ac9SJohnathan Mantey  */
43923a21a1cSEd Tanous inline void reduceSensorList(
44081ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
44149c53ac9SJohnathan Mantey     const std::vector<std::string>* allSensors,
442b5a76932SEd Tanous     const std::shared_ptr<boost::container::flat_set<std::string>>&
443b5a76932SEd Tanous         activeSensors)
44449c53ac9SJohnathan Mantey {
44581ce609eSEd Tanous     if (sensorsAsyncResp == nullptr)
44649c53ac9SJohnathan Mantey     {
44749c53ac9SJohnathan Mantey         return;
44849c53ac9SJohnathan Mantey     }
44949c53ac9SJohnathan Mantey     if ((allSensors == nullptr) || (activeSensors == nullptr))
45049c53ac9SJohnathan Mantey     {
45149c53ac9SJohnathan Mantey         messages::resourceNotFound(
4528d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res, sensorsAsyncResp->chassisSubNode,
45381ce609eSEd Tanous             sensorsAsyncResp->chassisSubNode == sensors::node::thermal
454a0ec28b6SAdrian Ambrożewicz                 ? "Temperatures"
45549c53ac9SJohnathan Mantey                 : "Voltages");
45649c53ac9SJohnathan Mantey 
45749c53ac9SJohnathan Mantey         return;
45849c53ac9SJohnathan Mantey     }
45949c53ac9SJohnathan Mantey     if (allSensors->empty())
46049c53ac9SJohnathan Mantey     {
46149c53ac9SJohnathan Mantey         // Nothing to do, the activeSensors object is also empty
46249c53ac9SJohnathan Mantey         return;
46349c53ac9SJohnathan Mantey     }
46449c53ac9SJohnathan Mantey 
46502da7c5aSEd Tanous     for (std::string_view type : sensorsAsyncResp->types)
46649c53ac9SJohnathan Mantey     {
46749c53ac9SJohnathan Mantey         for (const std::string& sensor : *allSensors)
46849c53ac9SJohnathan Mantey         {
46949c53ac9SJohnathan Mantey             if (boost::starts_with(sensor, type))
47049c53ac9SJohnathan Mantey             {
47149c53ac9SJohnathan Mantey                 activeSensors->emplace(sensor);
47249c53ac9SJohnathan Mantey             }
47349c53ac9SJohnathan Mantey         }
47449c53ac9SJohnathan Mantey     }
47549c53ac9SJohnathan Mantey }
47649c53ac9SJohnathan Mantey 
47749c53ac9SJohnathan Mantey /**
4784bb3dc34SCarol Wang  * @brief Retrieves valid chassis path
4794bb3dc34SCarol Wang  * @param asyncResp   Pointer to object holding response data
4804bb3dc34SCarol Wang  * @param callback  Callback for next step to get valid chassis path
4814bb3dc34SCarol Wang  */
4824bb3dc34SCarol Wang template <typename Callback>
483b5a76932SEd Tanous void getValidChassisPath(const std::shared_ptr<SensorsAsyncResp>& asyncResp,
4844bb3dc34SCarol Wang                          Callback&& callback)
4854bb3dc34SCarol Wang {
4864bb3dc34SCarol Wang     BMCWEB_LOG_DEBUG << "checkChassisId enter";
4874bb3dc34SCarol Wang     const std::array<const char*, 2> interfaces = {
4884bb3dc34SCarol Wang         "xyz.openbmc_project.Inventory.Item.Board",
4894bb3dc34SCarol Wang         "xyz.openbmc_project.Inventory.Item.Chassis"};
4904bb3dc34SCarol Wang 
491b9d36b47SEd Tanous     auto respHandler = [callback{std::forward<Callback>(callback)}, asyncResp](
492b9d36b47SEd Tanous                            const boost::system::error_code ec,
493b9d36b47SEd Tanous                            const dbus::utility::MapperGetSubTreePathsResponse&
494b9d36b47SEd Tanous                                chassisPaths) mutable {
4954bb3dc34SCarol Wang         BMCWEB_LOG_DEBUG << "getValidChassisPath respHandler enter";
4964bb3dc34SCarol Wang         if (ec)
4974bb3dc34SCarol Wang         {
498b9d36b47SEd Tanous             BMCWEB_LOG_ERROR << "getValidChassisPath respHandler DBUS error: "
499b9d36b47SEd Tanous                              << ec;
5008d1b46d7Szhanghch05             messages::internalError(asyncResp->asyncResp->res);
5014bb3dc34SCarol Wang             return;
5024bb3dc34SCarol Wang         }
5034bb3dc34SCarol Wang 
5044bb3dc34SCarol Wang         std::optional<std::string> chassisPath;
5054bb3dc34SCarol Wang         std::string chassisName;
5064bb3dc34SCarol Wang         for (const std::string& chassis : chassisPaths)
5074bb3dc34SCarol Wang         {
50828aa8de5SGeorge Liu             sdbusplus::message::object_path path(chassis);
50928aa8de5SGeorge Liu             chassisName = path.filename();
51028aa8de5SGeorge Liu             if (chassisName.empty())
5114bb3dc34SCarol Wang             {
5124bb3dc34SCarol Wang                 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
5134bb3dc34SCarol Wang                 continue;
5144bb3dc34SCarol Wang             }
5154bb3dc34SCarol Wang             if (chassisName == asyncResp->chassisId)
5164bb3dc34SCarol Wang             {
5174bb3dc34SCarol Wang                 chassisPath = chassis;
5184bb3dc34SCarol Wang                 break;
5194bb3dc34SCarol Wang             }
5204bb3dc34SCarol Wang         }
5214bb3dc34SCarol Wang         callback(chassisPath);
5224bb3dc34SCarol Wang     };
5234bb3dc34SCarol Wang 
5244bb3dc34SCarol Wang     // Get the Chassis Collection
5254bb3dc34SCarol Wang     crow::connections::systemBus->async_method_call(
5264bb3dc34SCarol Wang         respHandler, "xyz.openbmc_project.ObjectMapper",
5274bb3dc34SCarol Wang         "/xyz/openbmc_project/object_mapper",
5284bb3dc34SCarol Wang         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
5294bb3dc34SCarol Wang         "/xyz/openbmc_project/inventory", 0, interfaces);
5304bb3dc34SCarol Wang     BMCWEB_LOG_DEBUG << "checkChassisId exit";
5314bb3dc34SCarol Wang }
5324bb3dc34SCarol Wang 
5334bb3dc34SCarol Wang /**
53408777fb0SLewanczyk, Dawid  * @brief Retrieves requested chassis sensors and redundancy data from DBus .
535588c3f0dSKowalski, Kamil  * @param SensorsAsyncResp   Pointer to object holding response data
53608777fb0SLewanczyk, Dawid  * @param callback  Callback for next step in gathered sensor processing
53708777fb0SLewanczyk, Dawid  */
53808777fb0SLewanczyk, Dawid template <typename Callback>
539b5a76932SEd Tanous void getChassis(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
5401abe55efSEd Tanous                 Callback&& callback)
5411abe55efSEd Tanous {
54255c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "getChassis enter";
543adc4f0dbSShawn McCarney     const std::array<const char*, 2> interfaces = {
54449c53ac9SJohnathan Mantey         "xyz.openbmc_project.Inventory.Item.Board",
545adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Item.Chassis"};
546002d39b4SEd Tanous     auto respHandler =
547002d39b4SEd Tanous         [callback{std::forward<Callback>(callback)}, sensorsAsyncResp](
54849c53ac9SJohnathan Mantey             const boost::system::error_code ec,
549002d39b4SEd Tanous             const dbus::utility::MapperGetSubTreePathsResponse& chassisPaths) {
55055c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "getChassis respHandler enter";
5511abe55efSEd Tanous         if (ec)
5521abe55efSEd Tanous         {
55355c7b7a2SEd Tanous             BMCWEB_LOG_ERROR << "getChassis respHandler DBUS error: " << ec;
5548d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
55508777fb0SLewanczyk, Dawid             return;
55608777fb0SLewanczyk, Dawid         }
55708777fb0SLewanczyk, Dawid 
55849c53ac9SJohnathan Mantey         const std::string* chassisPath = nullptr;
55949c53ac9SJohnathan Mantey         std::string chassisName;
56049c53ac9SJohnathan Mantey         for (const std::string& chassis : chassisPaths)
5611abe55efSEd Tanous         {
56228aa8de5SGeorge Liu             sdbusplus::message::object_path path(chassis);
56328aa8de5SGeorge Liu             chassisName = path.filename();
56428aa8de5SGeorge Liu             if (chassisName.empty())
5651abe55efSEd Tanous             {
56649c53ac9SJohnathan Mantey                 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
567daf36e2eSEd Tanous                 continue;
568daf36e2eSEd Tanous             }
56949c53ac9SJohnathan Mantey             if (chassisName == sensorsAsyncResp->chassisId)
5701abe55efSEd Tanous             {
57149c53ac9SJohnathan Mantey                 chassisPath = &chassis;
57249c53ac9SJohnathan Mantey                 break;
573daf36e2eSEd Tanous             }
57449c53ac9SJohnathan Mantey         }
57549c53ac9SJohnathan Mantey         if (chassisPath == nullptr)
5761abe55efSEd Tanous         {
5778d1b46d7Szhanghch05             messages::resourceNotFound(sensorsAsyncResp->asyncResp->res,
5788d1b46d7Szhanghch05                                        "Chassis", sensorsAsyncResp->chassisId);
57949c53ac9SJohnathan Mantey             return;
5801abe55efSEd Tanous         }
58108777fb0SLewanczyk, Dawid 
58249c53ac9SJohnathan Mantey         const std::string& chassisSubNode = sensorsAsyncResp->chassisSubNode;
583a0ec28b6SAdrian Ambrożewicz         if (chassisSubNode == sensors::node::power)
58449c53ac9SJohnathan Mantey         {
5858d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] =
58649c53ac9SJohnathan Mantey                 "#Power.v1_5_2.Power";
58749c53ac9SJohnathan Mantey         }
588a0ec28b6SAdrian Ambrożewicz         else if (chassisSubNode == sensors::node::thermal)
58949c53ac9SJohnathan Mantey         {
5908d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] =
59149c53ac9SJohnathan Mantey                 "#Thermal.v1_4_0.Thermal";
5928d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["Fans"] =
5938d1b46d7Szhanghch05                 nlohmann::json::array();
5948d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["Temperatures"] =
5954f9a2130SJennifer Lee                 nlohmann::json::array();
59649c53ac9SJohnathan Mantey         }
597a0ec28b6SAdrian Ambrożewicz         else if (chassisSubNode == sensors::node::sensors)
59895a3ecadSAnthony Wilson         {
5998d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] =
60095a3ecadSAnthony Wilson                 "#SensorCollection.SensorCollection";
6018d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["Description"] =
60295a3ecadSAnthony Wilson                 "Collection of Sensors for this Chassis";
6038d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["Members"] =
60495a3ecadSAnthony Wilson                 nlohmann::json::array();
6058d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["Members@odata.count"] =
6068d1b46d7Szhanghch05                 0;
60795a3ecadSAnthony Wilson         }
60895a3ecadSAnthony Wilson 
609a0ec28b6SAdrian Ambrożewicz         if (chassisSubNode != sensors::node::sensors)
61095a3ecadSAnthony Wilson         {
6118d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["Id"] = chassisSubNode;
61295a3ecadSAnthony Wilson         }
61395a3ecadSAnthony Wilson 
6148d1b46d7Szhanghch05         sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
61549c53ac9SJohnathan Mantey             "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId + "/" +
61649c53ac9SJohnathan Mantey             chassisSubNode;
6178d1b46d7Szhanghch05         sensorsAsyncResp->asyncResp->res.jsonValue["Name"] = chassisSubNode;
6188fb49dd6SShawn McCarney         // Get the list of all sensors for this Chassis element
6198fb49dd6SShawn McCarney         std::string sensorPath = *chassisPath + "/all_sensors";
6201e1e598dSJonathan Doman         sdbusplus::asio::getProperty<std::vector<std::string>>(
6211e1e598dSJonathan Doman             *crow::connections::systemBus, "xyz.openbmc_project.ObjectMapper",
6221e1e598dSJonathan Doman             sensorPath, "xyz.openbmc_project.Association", "endpoints",
623f94c4ecfSEd Tanous             [sensorsAsyncResp,
624f94c4ecfSEd Tanous              callback{std::forward<const Callback>(callback)}](
625271584abSEd Tanous                 const boost::system::error_code& e,
6261e1e598dSJonathan Doman                 const std::vector<std::string>& nodeSensorList) {
627271584abSEd Tanous             if (e)
62849c53ac9SJohnathan Mantey             {
629271584abSEd Tanous                 if (e.value() != EBADR)
63049c53ac9SJohnathan Mantey                 {
631002d39b4SEd Tanous                     messages::internalError(sensorsAsyncResp->asyncResp->res);
63249c53ac9SJohnathan Mantey                     return;
63349c53ac9SJohnathan Mantey                 }
63449c53ac9SJohnathan Mantey             }
63549c53ac9SJohnathan Mantey             const std::shared_ptr<boost::container::flat_set<std::string>>
636002d39b4SEd Tanous                 culledSensorList =
637002d39b4SEd Tanous                     std::make_shared<boost::container::flat_set<std::string>>();
6381e1e598dSJonathan Doman             reduceSensorList(sensorsAsyncResp, &nodeSensorList,
63949c53ac9SJohnathan Mantey                              culledSensorList);
64049c53ac9SJohnathan Mantey             callback(culledSensorList);
6411e1e598dSJonathan Doman             });
64249c53ac9SJohnathan Mantey     };
64349c53ac9SJohnathan Mantey 
64449c53ac9SJohnathan Mantey     // Get the Chassis Collection
64549c53ac9SJohnathan Mantey     crow::connections::systemBus->async_method_call(
64649c53ac9SJohnathan Mantey         respHandler, "xyz.openbmc_project.ObjectMapper",
64749c53ac9SJohnathan Mantey         "/xyz/openbmc_project/object_mapper",
64849c53ac9SJohnathan Mantey         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
649271584abSEd Tanous         "/xyz/openbmc_project/inventory", 0, interfaces);
65055c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "getChassis exit";
65108777fb0SLewanczyk, Dawid }
65208777fb0SLewanczyk, Dawid 
65308777fb0SLewanczyk, Dawid /**
654de629b6eSShawn McCarney  * @brief Finds all DBus object paths that implement ObjectManager.
655de629b6eSShawn McCarney  *
656de629b6eSShawn McCarney  * Creates a mapping from the associated connection name to the object path.
657de629b6eSShawn McCarney  *
658de629b6eSShawn McCarney  * Finds the object paths asynchronously.  Invokes callback when information has
659de629b6eSShawn McCarney  * been obtained.
660de629b6eSShawn McCarney  *
661de629b6eSShawn McCarney  * The callback must have the following signature:
662de629b6eSShawn McCarney  *   @code
6638fb49dd6SShawn McCarney  *   callback(std::shared_ptr<boost::container::flat_map<std::string,
6648fb49dd6SShawn McCarney  *                std::string>> objectMgrPaths)
665de629b6eSShawn McCarney  *   @endcode
666de629b6eSShawn McCarney  *
66749c53ac9SJohnathan Mantey  * @param sensorsAsyncResp Pointer to object holding response data.
668de629b6eSShawn McCarney  * @param callback Callback to invoke when object paths obtained.
669de629b6eSShawn McCarney  */
670de629b6eSShawn McCarney template <typename Callback>
671b5a76932SEd Tanous void getObjectManagerPaths(
67281ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
673de629b6eSShawn McCarney     Callback&& callback)
674de629b6eSShawn McCarney {
675de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getObjectManagerPaths enter";
676de629b6eSShawn McCarney     const std::array<std::string, 1> interfaces = {
677de629b6eSShawn McCarney         "org.freedesktop.DBus.ObjectManager"};
678de629b6eSShawn McCarney 
679de629b6eSShawn McCarney     // Response handler for GetSubTree DBus method
680002d39b4SEd Tanous     auto respHandler =
681002d39b4SEd Tanous         [callback{std::forward<Callback>(callback)}, sensorsAsyncResp](
682b9d36b47SEd Tanous             const boost::system::error_code ec,
683002d39b4SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) {
684de629b6eSShawn McCarney         BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler enter";
685de629b6eSShawn McCarney         if (ec)
686de629b6eSShawn McCarney         {
6878d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
688de629b6eSShawn McCarney             BMCWEB_LOG_ERROR << "getObjectManagerPaths respHandler: DBus error "
689de629b6eSShawn McCarney                              << ec;
690de629b6eSShawn McCarney             return;
691de629b6eSShawn McCarney         }
692de629b6eSShawn McCarney 
693de629b6eSShawn McCarney         // Loop over returned object paths
6948fb49dd6SShawn McCarney         std::shared_ptr<boost::container::flat_map<std::string, std::string>>
6958fb49dd6SShawn McCarney             objectMgrPaths = std::make_shared<
6968fb49dd6SShawn McCarney                 boost::container::flat_map<std::string, std::string>>();
697de629b6eSShawn McCarney         for (const std::pair<
698de629b6eSShawn McCarney                  std::string,
699de629b6eSShawn McCarney                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
700de629b6eSShawn McCarney                  object : subtree)
701de629b6eSShawn McCarney         {
702de629b6eSShawn McCarney             // Loop over connections for current object path
703de629b6eSShawn McCarney             const std::string& objectPath = object.first;
704de629b6eSShawn McCarney             for (const std::pair<std::string, std::vector<std::string>>&
705de629b6eSShawn McCarney                      objData : object.second)
706de629b6eSShawn McCarney             {
707de629b6eSShawn McCarney                 // Add mapping from connection to object path
708de629b6eSShawn McCarney                 const std::string& connection = objData.first;
7098fb49dd6SShawn McCarney                 (*objectMgrPaths)[connection] = objectPath;
710de629b6eSShawn McCarney                 BMCWEB_LOG_DEBUG << "Added mapping " << connection << " -> "
711de629b6eSShawn McCarney                                  << objectPath;
712de629b6eSShawn McCarney             }
713de629b6eSShawn McCarney         }
7148fb49dd6SShawn McCarney         callback(objectMgrPaths);
715de629b6eSShawn McCarney         BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler exit";
716de629b6eSShawn McCarney     };
717de629b6eSShawn McCarney 
718de629b6eSShawn McCarney     // Query mapper for all DBus object paths that implement ObjectManager
719de629b6eSShawn McCarney     crow::connections::systemBus->async_method_call(
720de629b6eSShawn McCarney         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
721de629b6eSShawn McCarney         "/xyz/openbmc_project/object_mapper",
722271584abSEd Tanous         "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0, interfaces);
723de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getObjectManagerPaths exit";
724de629b6eSShawn McCarney }
725de629b6eSShawn McCarney 
726de629b6eSShawn McCarney /**
727adc4f0dbSShawn McCarney  * @brief Returns the Redfish State value for the specified inventory item.
728adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with a sensor.
729adc4f0dbSShawn McCarney  * @return State value for inventory item.
73034dd179eSJames Feist  */
73123a21a1cSEd Tanous inline std::string getState(const InventoryItem* inventoryItem)
732adc4f0dbSShawn McCarney {
733adc4f0dbSShawn McCarney     if ((inventoryItem != nullptr) && !(inventoryItem->isPresent))
734adc4f0dbSShawn McCarney     {
735adc4f0dbSShawn McCarney         return "Absent";
736adc4f0dbSShawn McCarney     }
73734dd179eSJames Feist 
738adc4f0dbSShawn McCarney     return "Enabled";
739adc4f0dbSShawn McCarney }
740adc4f0dbSShawn McCarney 
741adc4f0dbSShawn McCarney /**
742adc4f0dbSShawn McCarney  * @brief Returns the Redfish Health value for the specified sensor.
743adc4f0dbSShawn McCarney  * @param sensorJson Sensor JSON object.
744adc4f0dbSShawn McCarney  * @param interfacesDict Map of all sensor interfaces.
745adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with the sensor.  Will
746adc4f0dbSShawn McCarney  * be nullptr if no associated inventory item was found.
747adc4f0dbSShawn McCarney  * @return Health value for sensor.
748adc4f0dbSShawn McCarney  */
749711ac7a9SEd Tanous inline std::string
750711ac7a9SEd Tanous     getHealth(nlohmann::json& sensorJson,
751711ac7a9SEd Tanous               const dbus::utility::DBusInteracesMap& interfacesDict,
752adc4f0dbSShawn McCarney               const InventoryItem* inventoryItem)
75334dd179eSJames Feist {
754adc4f0dbSShawn McCarney     // Get current health value (if any) in the sensor JSON object.  Some JSON
755adc4f0dbSShawn McCarney     // objects contain multiple sensors (such as PowerSupplies).  We want to set
756adc4f0dbSShawn McCarney     // the overall health to be the most severe of any of the sensors.
757adc4f0dbSShawn McCarney     std::string currentHealth;
758adc4f0dbSShawn McCarney     auto statusIt = sensorJson.find("Status");
759adc4f0dbSShawn McCarney     if (statusIt != sensorJson.end())
760adc4f0dbSShawn McCarney     {
761adc4f0dbSShawn McCarney         auto healthIt = statusIt->find("Health");
762adc4f0dbSShawn McCarney         if (healthIt != statusIt->end())
763adc4f0dbSShawn McCarney         {
764adc4f0dbSShawn McCarney             std::string* health = healthIt->get_ptr<std::string*>();
765adc4f0dbSShawn McCarney             if (health != nullptr)
766adc4f0dbSShawn McCarney             {
767adc4f0dbSShawn McCarney                 currentHealth = *health;
768adc4f0dbSShawn McCarney             }
769adc4f0dbSShawn McCarney         }
770adc4f0dbSShawn McCarney     }
771adc4f0dbSShawn McCarney 
772adc4f0dbSShawn McCarney     // If current health in JSON object is already Critical, return that.  This
773adc4f0dbSShawn McCarney     // should override the sensor health, which might be less severe.
774adc4f0dbSShawn McCarney     if (currentHealth == "Critical")
775adc4f0dbSShawn McCarney     {
776adc4f0dbSShawn McCarney         return "Critical";
777adc4f0dbSShawn McCarney     }
778adc4f0dbSShawn McCarney 
779adc4f0dbSShawn McCarney     // Check if sensor has critical threshold alarm
780711ac7a9SEd Tanous 
7819eb808c1SEd Tanous     for (const auto& [interface, values] : interfacesDict)
78234dd179eSJames Feist     {
783711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Sensor.Threshold.Critical")
78434dd179eSJames Feist         {
7859eb808c1SEd Tanous             for (const auto& [valueName, value] : values)
786711ac7a9SEd Tanous             {
787711ac7a9SEd Tanous                 if (valueName == "CriticalAlarmHigh" ||
788711ac7a9SEd Tanous                     valueName == "CriticalAlarmLow")
789711ac7a9SEd Tanous                 {
790711ac7a9SEd Tanous                     const bool* asserted = std::get_if<bool>(&value);
79134dd179eSJames Feist                     if (asserted == nullptr)
79234dd179eSJames Feist                     {
79334dd179eSJames Feist                         BMCWEB_LOG_ERROR << "Illegal sensor threshold";
79434dd179eSJames Feist                     }
79534dd179eSJames Feist                     else if (*asserted)
79634dd179eSJames Feist                     {
79734dd179eSJames Feist                         return "Critical";
79834dd179eSJames Feist                     }
79934dd179eSJames Feist                 }
80034dd179eSJames Feist             }
80134dd179eSJames Feist         }
80234dd179eSJames Feist     }
80334dd179eSJames Feist 
804adc4f0dbSShawn McCarney     // Check if associated inventory item is not functional
805adc4f0dbSShawn McCarney     if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional))
806adc4f0dbSShawn McCarney     {
807adc4f0dbSShawn McCarney         return "Critical";
808adc4f0dbSShawn McCarney     }
809adc4f0dbSShawn McCarney 
810adc4f0dbSShawn McCarney     // If current health in JSON object is already Warning, return that.  This
811adc4f0dbSShawn McCarney     // should override the sensor status, which might be less severe.
812adc4f0dbSShawn McCarney     if (currentHealth == "Warning")
813adc4f0dbSShawn McCarney     {
814adc4f0dbSShawn McCarney         return "Warning";
815adc4f0dbSShawn McCarney     }
816adc4f0dbSShawn McCarney 
817adc4f0dbSShawn McCarney     // Check if sensor has warning threshold alarm
8189eb808c1SEd Tanous     for (const auto& [interface, values] : interfacesDict)
81934dd179eSJames Feist     {
820711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Sensor.Threshold.Warning")
82134dd179eSJames Feist         {
8229eb808c1SEd Tanous             for (const auto& [valueName, value] : values)
823711ac7a9SEd Tanous             {
824711ac7a9SEd Tanous                 if (valueName == "WarningAlarmHigh" ||
825711ac7a9SEd Tanous                     valueName == "WarningAlarmLow")
826711ac7a9SEd Tanous                 {
827711ac7a9SEd Tanous                     const bool* asserted = std::get_if<bool>(&value);
82834dd179eSJames Feist                     if (asserted == nullptr)
82934dd179eSJames Feist                     {
83034dd179eSJames Feist                         BMCWEB_LOG_ERROR << "Illegal sensor threshold";
83134dd179eSJames Feist                     }
83234dd179eSJames Feist                     else if (*asserted)
83334dd179eSJames Feist                     {
834ebe4d91eSEd Tanous                         return "Warning";
83534dd179eSJames Feist                     }
83634dd179eSJames Feist                 }
83734dd179eSJames Feist             }
83834dd179eSJames Feist         }
83934dd179eSJames Feist     }
840adc4f0dbSShawn McCarney 
84134dd179eSJames Feist     return "OK";
84234dd179eSJames Feist }
84334dd179eSJames Feist 
84423a21a1cSEd Tanous inline void setLedState(nlohmann::json& sensorJson,
845d500549bSAnthony Wilson                         const InventoryItem* inventoryItem)
846d500549bSAnthony Wilson {
847d500549bSAnthony Wilson     if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty())
848d500549bSAnthony Wilson     {
849d500549bSAnthony Wilson         switch (inventoryItem->ledState)
850d500549bSAnthony Wilson         {
851d500549bSAnthony Wilson             case LedState::OFF:
852d500549bSAnthony Wilson                 sensorJson["IndicatorLED"] = "Off";
853d500549bSAnthony Wilson                 break;
854d500549bSAnthony Wilson             case LedState::ON:
855d500549bSAnthony Wilson                 sensorJson["IndicatorLED"] = "Lit";
856d500549bSAnthony Wilson                 break;
857d500549bSAnthony Wilson             case LedState::BLINK:
858d500549bSAnthony Wilson                 sensorJson["IndicatorLED"] = "Blinking";
859d500549bSAnthony Wilson                 break;
86023a21a1cSEd Tanous             case LedState::UNKNOWN:
861d500549bSAnthony Wilson                 break;
862d500549bSAnthony Wilson         }
863d500549bSAnthony Wilson     }
864d500549bSAnthony Wilson }
865d500549bSAnthony Wilson 
86634dd179eSJames Feist /**
86708777fb0SLewanczyk, Dawid  * @brief Builds a json sensor representation of a sensor.
86808777fb0SLewanczyk, Dawid  * @param sensorName  The name of the sensor to be built
869274fad5aSGunnar Mills  * @param sensorType  The type (temperature, fan_tach, etc) of the sensor to
87008777fb0SLewanczyk, Dawid  * build
871a0ec28b6SAdrian Ambrożewicz  * @param sensorsAsyncResp  Sensor metadata
87208777fb0SLewanczyk, Dawid  * @param interfacesDict  A dictionary of the interfaces and properties of said
87308777fb0SLewanczyk, Dawid  * interfaces to be built from
87408777fb0SLewanczyk, Dawid  * @param sensor_json  The json object to fill
875adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with the sensor.  Will
876adc4f0dbSShawn McCarney  * be nullptr if no associated inventory item was found.
87708777fb0SLewanczyk, Dawid  */
87823a21a1cSEd Tanous inline void objectInterfacesToJson(
87908777fb0SLewanczyk, Dawid     const std::string& sensorName, const std::string& sensorType,
880b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
881711ac7a9SEd Tanous     const dbus::utility::DBusInteracesMap& interfacesDict,
88281ce609eSEd Tanous     nlohmann::json& sensorJson, InventoryItem* inventoryItem)
8831abe55efSEd Tanous {
88408777fb0SLewanczyk, Dawid     // Assume values exist as is (10^0 == 1) if no scale exists
88508777fb0SLewanczyk, Dawid     int64_t scaleMultiplier = 0;
8869eb808c1SEd Tanous     for (const auto& [interface, values] : interfacesDict)
8871abe55efSEd Tanous     {
888711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Sensor.Value")
889711ac7a9SEd Tanous         {
8909eb808c1SEd Tanous             for (const auto& [valueName, value] : values)
891711ac7a9SEd Tanous             {
892711ac7a9SEd Tanous                 if (valueName == "Scale")
893711ac7a9SEd Tanous                 {
894711ac7a9SEd Tanous                     const int64_t* int64Value = std::get_if<int64_t>(&value);
8951abe55efSEd Tanous                     if (int64Value != nullptr)
8961abe55efSEd Tanous                     {
89708777fb0SLewanczyk, Dawid                         scaleMultiplier = *int64Value;
89808777fb0SLewanczyk, Dawid                     }
89908777fb0SLewanczyk, Dawid                 }
900711ac7a9SEd Tanous             }
901711ac7a9SEd Tanous         }
902711ac7a9SEd Tanous     }
90308777fb0SLewanczyk, Dawid 
904a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
905adc4f0dbSShawn McCarney     {
90695a3ecadSAnthony Wilson         // For sensors in SensorCollection we set Id instead of MemberId,
90795a3ecadSAnthony Wilson         // including power sensors.
90881ce609eSEd Tanous         sensorJson["Id"] = sensorName;
90981ce609eSEd Tanous         sensorJson["Name"] = boost::replace_all_copy(sensorName, "_", " ");
91095a3ecadSAnthony Wilson     }
91195a3ecadSAnthony Wilson     else if (sensorType != "power")
91295a3ecadSAnthony Wilson     {
91395a3ecadSAnthony Wilson         // Set MemberId and Name for non-power sensors.  For PowerSupplies and
91495a3ecadSAnthony Wilson         // PowerControl, those properties have more general values because
91595a3ecadSAnthony Wilson         // multiple sensors can be stored in the same JSON object.
91681ce609eSEd Tanous         sensorJson["MemberId"] = sensorName;
91781ce609eSEd Tanous         sensorJson["Name"] = boost::replace_all_copy(sensorName, "_", " ");
918adc4f0dbSShawn McCarney     }
919e742b6ccSEd Tanous 
92081ce609eSEd Tanous     sensorJson["Status"]["State"] = getState(inventoryItem);
92181ce609eSEd Tanous     sensorJson["Status"]["Health"] =
92281ce609eSEd Tanous         getHealth(sensorJson, interfacesDict, inventoryItem);
92308777fb0SLewanczyk, Dawid 
92408777fb0SLewanczyk, Dawid     // Parameter to set to override the type we get from dbus, and force it to
92508777fb0SLewanczyk, Dawid     // int, regardless of what is available.  This is used for schemas like fan,
92608777fb0SLewanczyk, Dawid     // that require integers, not floats.
92708777fb0SLewanczyk, Dawid     bool forceToInt = false;
92808777fb0SLewanczyk, Dawid 
9293929aca1SAnthony Wilson     nlohmann::json::json_pointer unit("/Reading");
930a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
93195a3ecadSAnthony Wilson     {
93281ce609eSEd Tanous         sensorJson["@odata.type"] = "#Sensor.v1_0_0.Sensor";
933c2bf7f99SWludzik, Jozef 
934c2bf7f99SWludzik, Jozef         const std::string& readingType = sensors::toReadingType(sensorType);
935c2bf7f99SWludzik, Jozef         if (readingType.empty())
93695a3ecadSAnthony Wilson         {
937c2bf7f99SWludzik, Jozef             BMCWEB_LOG_ERROR << "Redfish cannot map reading type for "
938c2bf7f99SWludzik, Jozef                              << sensorType;
93995a3ecadSAnthony Wilson         }
940c2bf7f99SWludzik, Jozef         else
94195a3ecadSAnthony Wilson         {
942c2bf7f99SWludzik, Jozef             sensorJson["ReadingType"] = readingType;
94395a3ecadSAnthony Wilson         }
944c2bf7f99SWludzik, Jozef 
945c2bf7f99SWludzik, Jozef         const std::string& readingUnits = sensors::toReadingUnits(sensorType);
946c2bf7f99SWludzik, Jozef         if (readingUnits.empty())
947f8ede15eSAdrian Ambrożewicz         {
948c2bf7f99SWludzik, Jozef             BMCWEB_LOG_ERROR << "Redfish cannot map reading unit for "
949c2bf7f99SWludzik, Jozef                              << sensorType;
950c2bf7f99SWludzik, Jozef         }
951c2bf7f99SWludzik, Jozef         else
952c2bf7f99SWludzik, Jozef         {
953c2bf7f99SWludzik, Jozef             sensorJson["ReadingUnits"] = readingUnits;
954f8ede15eSAdrian Ambrożewicz         }
95595a3ecadSAnthony Wilson     }
95695a3ecadSAnthony Wilson     else if (sensorType == "temperature")
9571abe55efSEd Tanous     {
9583929aca1SAnthony Wilson         unit = "/ReadingCelsius"_json_pointer;
95981ce609eSEd Tanous         sensorJson["@odata.type"] = "#Thermal.v1_3_0.Temperature";
96008777fb0SLewanczyk, Dawid         // TODO(ed) Documentation says that path should be type fan_tach,
96108777fb0SLewanczyk, Dawid         // implementation seems to implement fan
9621abe55efSEd Tanous     }
9631abe55efSEd Tanous     else if (sensorType == "fan" || sensorType == "fan_tach")
9641abe55efSEd Tanous     {
9653929aca1SAnthony Wilson         unit = "/Reading"_json_pointer;
96681ce609eSEd Tanous         sensorJson["ReadingUnits"] = "RPM";
96781ce609eSEd Tanous         sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
96881ce609eSEd Tanous         setLedState(sensorJson, inventoryItem);
96908777fb0SLewanczyk, Dawid         forceToInt = true;
9701abe55efSEd Tanous     }
9716f6d0d32SEd Tanous     else if (sensorType == "fan_pwm")
9726f6d0d32SEd Tanous     {
9733929aca1SAnthony Wilson         unit = "/Reading"_json_pointer;
97481ce609eSEd Tanous         sensorJson["ReadingUnits"] = "Percent";
97581ce609eSEd Tanous         sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
97681ce609eSEd Tanous         setLedState(sensorJson, inventoryItem);
9776f6d0d32SEd Tanous         forceToInt = true;
9786f6d0d32SEd Tanous     }
9791abe55efSEd Tanous     else if (sensorType == "voltage")
9801abe55efSEd Tanous     {
9813929aca1SAnthony Wilson         unit = "/ReadingVolts"_json_pointer;
98281ce609eSEd Tanous         sensorJson["@odata.type"] = "#Power.v1_0_0.Voltage";
9831abe55efSEd Tanous     }
9842474adfaSEd Tanous     else if (sensorType == "power")
9852474adfaSEd Tanous     {
98649c53ac9SJohnathan Mantey         std::string sensorNameLower =
98749c53ac9SJohnathan Mantey             boost::algorithm::to_lower_copy(sensorName);
98849c53ac9SJohnathan Mantey 
98955f79e6fSEd Tanous         if (sensorName == "total_power")
990028f7ebcSEddie James         {
99181ce609eSEd Tanous             sensorJson["@odata.type"] = "#Power.v1_0_0.PowerControl";
9927ab06f49SGunnar Mills             // Put multiple "sensors" into a single PowerControl, so have
9937ab06f49SGunnar Mills             // generic names for MemberId and Name. Follows Redfish mockup.
99481ce609eSEd Tanous             sensorJson["MemberId"] = "0";
99581ce609eSEd Tanous             sensorJson["Name"] = "Chassis Power Control";
9963929aca1SAnthony Wilson             unit = "/PowerConsumedWatts"_json_pointer;
997028f7ebcSEddie James         }
998028f7ebcSEddie James         else if (sensorNameLower.find("input") != std::string::npos)
99949c53ac9SJohnathan Mantey         {
10003929aca1SAnthony Wilson             unit = "/PowerInputWatts"_json_pointer;
100149c53ac9SJohnathan Mantey         }
100249c53ac9SJohnathan Mantey         else
100349c53ac9SJohnathan Mantey         {
10043929aca1SAnthony Wilson             unit = "/PowerOutputWatts"_json_pointer;
100549c53ac9SJohnathan Mantey         }
10062474adfaSEd Tanous     }
10071abe55efSEd Tanous     else
10081abe55efSEd Tanous     {
100955c7b7a2SEd Tanous         BMCWEB_LOG_ERROR << "Redfish cannot map object type for " << sensorName;
101008777fb0SLewanczyk, Dawid         return;
101108777fb0SLewanczyk, Dawid     }
101208777fb0SLewanczyk, Dawid     // Map of dbus interface name, dbus property name and redfish property_name
10133929aca1SAnthony Wilson     std::vector<
10143929aca1SAnthony Wilson         std::tuple<const char*, const char*, nlohmann::json::json_pointer>>
10153929aca1SAnthony Wilson         properties;
101608777fb0SLewanczyk, Dawid     properties.reserve(7);
101708777fb0SLewanczyk, Dawid 
101808777fb0SLewanczyk, Dawid     properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit);
1019de629b6eSShawn McCarney 
1020a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
10213929aca1SAnthony Wilson     {
10223929aca1SAnthony Wilson         properties.emplace_back(
10233929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh",
10243929aca1SAnthony Wilson             "/Thresholds/UpperCaution/Reading"_json_pointer);
10253929aca1SAnthony Wilson         properties.emplace_back(
10263929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow",
10273929aca1SAnthony Wilson             "/Thresholds/LowerCaution/Reading"_json_pointer);
10283929aca1SAnthony Wilson         properties.emplace_back(
10293929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh",
10303929aca1SAnthony Wilson             "/Thresholds/UpperCritical/Reading"_json_pointer);
10313929aca1SAnthony Wilson         properties.emplace_back(
10323929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow",
10333929aca1SAnthony Wilson             "/Thresholds/LowerCritical/Reading"_json_pointer);
10343929aca1SAnthony Wilson     }
10353929aca1SAnthony Wilson     else if (sensorType != "power")
1036de629b6eSShawn McCarney     {
103708777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
10383929aca1SAnthony Wilson                                 "WarningHigh",
10393929aca1SAnthony Wilson                                 "/UpperThresholdNonCritical"_json_pointer);
104008777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
10413929aca1SAnthony Wilson                                 "WarningLow",
10423929aca1SAnthony Wilson                                 "/LowerThresholdNonCritical"_json_pointer);
104308777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
10443929aca1SAnthony Wilson                                 "CriticalHigh",
10453929aca1SAnthony Wilson                                 "/UpperThresholdCritical"_json_pointer);
104608777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
10473929aca1SAnthony Wilson                                 "CriticalLow",
10483929aca1SAnthony Wilson                                 "/LowerThresholdCritical"_json_pointer);
1049de629b6eSShawn McCarney     }
105008777fb0SLewanczyk, Dawid 
10512474adfaSEd Tanous     // TODO Need to get UpperThresholdFatal and LowerThresholdFatal
10522474adfaSEd Tanous 
1053a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
105495a3ecadSAnthony Wilson     {
105595a3ecadSAnthony Wilson         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
10563929aca1SAnthony Wilson                                 "/ReadingRangeMin"_json_pointer);
105795a3ecadSAnthony Wilson         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
10583929aca1SAnthony Wilson                                 "/ReadingRangeMax"_json_pointer);
105995a3ecadSAnthony Wilson     }
106095a3ecadSAnthony Wilson     else if (sensorType == "temperature")
10611abe55efSEd Tanous     {
106208777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
10633929aca1SAnthony Wilson                                 "/MinReadingRangeTemp"_json_pointer);
106408777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
10653929aca1SAnthony Wilson                                 "/MaxReadingRangeTemp"_json_pointer);
10661abe55efSEd Tanous     }
1067adc4f0dbSShawn McCarney     else if (sensorType != "power")
10681abe55efSEd Tanous     {
106908777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
10703929aca1SAnthony Wilson                                 "/MinReadingRange"_json_pointer);
107108777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
10723929aca1SAnthony Wilson                                 "/MaxReadingRange"_json_pointer);
107308777fb0SLewanczyk, Dawid     }
107408777fb0SLewanczyk, Dawid 
10753929aca1SAnthony Wilson     for (const std::tuple<const char*, const char*,
10763929aca1SAnthony Wilson                           nlohmann::json::json_pointer>& p : properties)
10771abe55efSEd Tanous     {
107855f79e6fSEd Tanous         for (const auto& [interface, values] : interfacesDict)
10791abe55efSEd Tanous         {
1080711ac7a9SEd Tanous             if (interface != std::get<0>(p))
10811abe55efSEd Tanous             {
1082711ac7a9SEd Tanous                 continue;
1083711ac7a9SEd Tanous             }
108455f79e6fSEd Tanous             for (const auto& [valueName, valueVariant] : values)
1085711ac7a9SEd Tanous             {
1086711ac7a9SEd Tanous                 if (valueName != std::get<1>(p))
1087711ac7a9SEd Tanous                 {
1088711ac7a9SEd Tanous                     continue;
1089711ac7a9SEd Tanous                 }
10903929aca1SAnthony Wilson 
10913929aca1SAnthony Wilson                 // The property we want to set may be nested json, so use
10923929aca1SAnthony Wilson                 // a json_pointer for easy indexing into the json structure.
10933929aca1SAnthony Wilson                 const nlohmann::json::json_pointer& key = std::get<2>(p);
10943929aca1SAnthony Wilson 
109508777fb0SLewanczyk, Dawid                 // Attempt to pull the int64 directly
1096abf2add6SEd Tanous                 const int64_t* int64Value = std::get_if<int64_t>(&valueVariant);
109708777fb0SLewanczyk, Dawid 
1098abf2add6SEd Tanous                 const double* doubleValue = std::get_if<double>(&valueVariant);
1099028f7ebcSEddie James                 const uint32_t* uValue = std::get_if<uint32_t>(&valueVariant);
11006f6d0d32SEd Tanous                 double temp = 0.0;
11016f6d0d32SEd Tanous                 if (int64Value != nullptr)
11021abe55efSEd Tanous                 {
1103271584abSEd Tanous                     temp = static_cast<double>(*int64Value);
11046f6d0d32SEd Tanous                 }
11056f6d0d32SEd Tanous                 else if (doubleValue != nullptr)
11061abe55efSEd Tanous                 {
11076f6d0d32SEd Tanous                     temp = *doubleValue;
11081abe55efSEd Tanous                 }
1109028f7ebcSEddie James                 else if (uValue != nullptr)
1110028f7ebcSEddie James                 {
1111028f7ebcSEddie James                     temp = *uValue;
1112028f7ebcSEddie James                 }
11131abe55efSEd Tanous                 else
11141abe55efSEd Tanous                 {
11156f6d0d32SEd Tanous                     BMCWEB_LOG_ERROR
11166f6d0d32SEd Tanous                         << "Got value interface that wasn't int or double";
11176f6d0d32SEd Tanous                     continue;
111808777fb0SLewanczyk, Dawid                 }
11196f6d0d32SEd Tanous                 temp = temp * std::pow(10, scaleMultiplier);
11206f6d0d32SEd Tanous                 if (forceToInt)
11216f6d0d32SEd Tanous                 {
112281ce609eSEd Tanous                     sensorJson[key] = static_cast<int64_t>(temp);
11236f6d0d32SEd Tanous                 }
11246f6d0d32SEd Tanous                 else
11256f6d0d32SEd Tanous                 {
112681ce609eSEd Tanous                     sensorJson[key] = temp;
112708777fb0SLewanczyk, Dawid                 }
112808777fb0SLewanczyk, Dawid             }
112908777fb0SLewanczyk, Dawid         }
113008777fb0SLewanczyk, Dawid     }
1131a0ec28b6SAdrian Ambrożewicz 
113281ce609eSEd Tanous     sensorsAsyncResp->addMetadata(sensorJson, unit.to_string(),
1133a0ec28b6SAdrian Ambrożewicz                                   "/xyz/openbmc_project/sensors/" + sensorType +
1134a0ec28b6SAdrian Ambrożewicz                                       "/" + sensorName);
1135a0ec28b6SAdrian Ambrożewicz 
113655c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "Added sensor " << sensorName;
113708777fb0SLewanczyk, Dawid }
113808777fb0SLewanczyk, Dawid 
1139b5a76932SEd Tanous inline void populateFanRedundancy(
1140b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
11418bd25ccdSJames Feist {
11428bd25ccdSJames Feist     crow::connections::systemBus->async_method_call(
1143b9d36b47SEd Tanous         [sensorsAsyncResp](
1144b9d36b47SEd Tanous             const boost::system::error_code ec,
1145b9d36b47SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& resp) {
11468bd25ccdSJames Feist         if (ec)
11478bd25ccdSJames Feist         {
11488bd25ccdSJames Feist             return; // don't have to have this interface
11498bd25ccdSJames Feist         }
1150002d39b4SEd Tanous         for (const std::pair<
1151002d39b4SEd Tanous                  std::string,
1152002d39b4SEd Tanous                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
1153e278c18fSEd Tanous                  pathPair : resp)
11548bd25ccdSJames Feist         {
1155e278c18fSEd Tanous             const std::string& path = pathPair.first;
1156002d39b4SEd Tanous             const std::vector<std::pair<std::string, std::vector<std::string>>>&
1157002d39b4SEd Tanous                 objDict = pathPair.second;
11588bd25ccdSJames Feist             if (objDict.empty())
11598bd25ccdSJames Feist             {
11608bd25ccdSJames Feist                 continue; // this should be impossible
11618bd25ccdSJames Feist             }
11628bd25ccdSJames Feist 
11638bd25ccdSJames Feist             const std::string& owner = objDict.begin()->first;
11641e1e598dSJonathan Doman             sdbusplus::asio::getProperty<std::vector<std::string>>(
11651e1e598dSJonathan Doman                 *crow::connections::systemBus,
11661e1e598dSJonathan Doman                 "xyz.openbmc_project.ObjectMapper", path + "/chassis",
11671e1e598dSJonathan Doman                 "xyz.openbmc_project.Association", "endpoints",
1168002d39b4SEd Tanous                 [path, owner,
1169002d39b4SEd Tanous                  sensorsAsyncResp](const boost::system::error_code e,
11701e1e598dSJonathan Doman                                    const std::vector<std::string>& endpoints) {
1171271584abSEd Tanous                 if (e)
11728bd25ccdSJames Feist                 {
11738bd25ccdSJames Feist                     return; // if they don't have an association we
11748bd25ccdSJames Feist                             // can't tell what chassis is
11758bd25ccdSJames Feist                 }
1176002d39b4SEd Tanous                 auto found =
1177002d39b4SEd Tanous                     std::find_if(endpoints.begin(), endpoints.end(),
11788bd25ccdSJames Feist                                  [sensorsAsyncResp](const std::string& entry) {
1179002d39b4SEd Tanous                     return entry.find(sensorsAsyncResp->chassisId) !=
11808bd25ccdSJames Feist                            std::string::npos;
11818bd25ccdSJames Feist                     });
11828bd25ccdSJames Feist 
11831e1e598dSJonathan Doman                 if (found == endpoints.end())
11848bd25ccdSJames Feist                 {
11858bd25ccdSJames Feist                     return;
11868bd25ccdSJames Feist                 }
11878bd25ccdSJames Feist                 crow::connections::systemBus->async_method_call(
11888bd25ccdSJames Feist                     [path, sensorsAsyncResp](
1189271584abSEd Tanous                         const boost::system::error_code& err,
11908bd25ccdSJames Feist                         const boost::container::flat_map<
1191002d39b4SEd Tanous                             std::string, dbus::utility::DbusVariantType>& ret) {
1192271584abSEd Tanous                     if (err)
11938bd25ccdSJames Feist                     {
11948bd25ccdSJames Feist                         return; // don't have to have this
11958bd25ccdSJames Feist                                 // interface
11968bd25ccdSJames Feist                     }
11978bd25ccdSJames Feist                     auto findFailures = ret.find("AllowedFailures");
11988bd25ccdSJames Feist                     auto findCollection = ret.find("Collection");
11998bd25ccdSJames Feist                     auto findStatus = ret.find("Status");
12008bd25ccdSJames Feist 
12018bd25ccdSJames Feist                     if (findFailures == ret.end() ||
1202002d39b4SEd Tanous                         findCollection == ret.end() || findStatus == ret.end())
12038bd25ccdSJames Feist                     {
1204002d39b4SEd Tanous                         BMCWEB_LOG_ERROR << "Invalid redundancy interface";
12058bd25ccdSJames Feist                         messages::internalError(
12068d1b46d7Szhanghch05                             sensorsAsyncResp->asyncResp->res);
12078bd25ccdSJames Feist                         return;
12088bd25ccdSJames Feist                     }
12098bd25ccdSJames Feist 
12109eb808c1SEd Tanous                     const uint8_t* allowedFailures =
1211002d39b4SEd Tanous                         std::get_if<uint8_t>(&(findFailures->second));
12129eb808c1SEd Tanous                     const std::vector<std::string>* collection =
12138bd25ccdSJames Feist                         std::get_if<std::vector<std::string>>(
12148bd25ccdSJames Feist                             &(findCollection->second));
12159eb808c1SEd Tanous                     const std::string* status =
1216002d39b4SEd Tanous                         std::get_if<std::string>(&(findStatus->second));
12178bd25ccdSJames Feist 
1218002d39b4SEd Tanous                     if (allowedFailures == nullptr || collection == nullptr ||
1219002d39b4SEd Tanous                         status == nullptr)
12208bd25ccdSJames Feist                     {
12218bd25ccdSJames Feist 
12228bd25ccdSJames Feist                         BMCWEB_LOG_ERROR
12230fda0f12SGeorge Liu                             << "Invalid redundancy interface types";
12248bd25ccdSJames Feist                         messages::internalError(
12258d1b46d7Szhanghch05                             sensorsAsyncResp->asyncResp->res);
12268bd25ccdSJames Feist                         return;
12278bd25ccdSJames Feist                     }
1228002d39b4SEd Tanous                     sdbusplus::message::object_path objectPath(path);
122928aa8de5SGeorge Liu                     std::string name = objectPath.filename();
123028aa8de5SGeorge Liu                     if (name.empty())
12318bd25ccdSJames Feist                     {
12328bd25ccdSJames Feist                         // this should be impossible
12338bd25ccdSJames Feist                         messages::internalError(
12348d1b46d7Szhanghch05                             sensorsAsyncResp->asyncResp->res);
12358bd25ccdSJames Feist                         return;
12368bd25ccdSJames Feist                     }
1237002d39b4SEd Tanous                     std::replace(name.begin(), name.end(), '_', ' ');
12388bd25ccdSJames Feist 
12398bd25ccdSJames Feist                     std::string health;
12408bd25ccdSJames Feist 
12418bd25ccdSJames Feist                     if (boost::ends_with(*status, "Full"))
12428bd25ccdSJames Feist                     {
12438bd25ccdSJames Feist                         health = "OK";
12448bd25ccdSJames Feist                     }
12458bd25ccdSJames Feist                     else if (boost::ends_with(*status, "Degraded"))
12468bd25ccdSJames Feist                     {
12478bd25ccdSJames Feist                         health = "Warning";
12488bd25ccdSJames Feist                     }
12498bd25ccdSJames Feist                     else
12508bd25ccdSJames Feist                     {
12518bd25ccdSJames Feist                         health = "Critical";
12528bd25ccdSJames Feist                     }
12531476687dSEd Tanous                     nlohmann::json::array_t redfishCollection;
12548bd25ccdSJames Feist                     const auto& fanRedfish =
1255002d39b4SEd Tanous                         sensorsAsyncResp->asyncResp->res.jsonValue["Fans"];
12568bd25ccdSJames Feist                     for (const std::string& item : *collection)
12578bd25ccdSJames Feist                     {
125828aa8de5SGeorge Liu                         sdbusplus::message::object_path path(item);
125928aa8de5SGeorge Liu                         std::string itemName = path.filename();
126028aa8de5SGeorge Liu                         if (itemName.empty())
126128aa8de5SGeorge Liu                         {
126228aa8de5SGeorge Liu                             continue;
126328aa8de5SGeorge Liu                         }
12648bd25ccdSJames Feist                         /*
12658bd25ccdSJames Feist                         todo(ed): merge patch that fixes the names
12668bd25ccdSJames Feist                         std::replace(itemName.begin(),
12678bd25ccdSJames Feist                                      itemName.end(), '_', ' ');*/
1268002d39b4SEd Tanous                         auto schemaItem =
1269002d39b4SEd Tanous                             std::find_if(fanRedfish.begin(), fanRedfish.end(),
12708bd25ccdSJames Feist                                          [itemName](const nlohmann::json& fan) {
12718bd25ccdSJames Feist                             return fan["MemberId"] == itemName;
12728bd25ccdSJames Feist                             });
12738bd25ccdSJames Feist                         if (schemaItem != fanRedfish.end())
12748bd25ccdSJames Feist                         {
12751476687dSEd Tanous                             nlohmann::json::object_t collection;
12761476687dSEd Tanous                             collection["@odata.id"] =
12771476687dSEd Tanous                                 (*schemaItem)["@odata.id"];
12781476687dSEd Tanous                             redfishCollection.emplace_back(
12791476687dSEd Tanous                                 std::move(collection));
12808bd25ccdSJames Feist                         }
12818bd25ccdSJames Feist                         else
12828bd25ccdSJames Feist                         {
1283002d39b4SEd Tanous                             BMCWEB_LOG_ERROR << "failed to find fan in schema";
12848bd25ccdSJames Feist                             messages::internalError(
12858d1b46d7Szhanghch05                                 sensorsAsyncResp->asyncResp->res);
12868bd25ccdSJames Feist                             return;
12878bd25ccdSJames Feist                         }
12888bd25ccdSJames Feist                     }
12898bd25ccdSJames Feist 
12903e9e72ebSKuiying Wang                     size_t minNumNeeded =
129126f6976fSEd Tanous                         collection->empty()
129226f6976fSEd Tanous                             ? 0
129326f6976fSEd Tanous                             : collection->size() - *allowedFailures;
1294002d39b4SEd Tanous                     nlohmann::json& jResp = sensorsAsyncResp->asyncResp->res
12958bd25ccdSJames Feist                                                 .jsonValue["Redundancy"];
12961476687dSEd Tanous 
12971476687dSEd Tanous                     nlohmann::json::object_t redundancy;
12981476687dSEd Tanous                     redundancy["@odata.id"] =
1299002d39b4SEd Tanous                         "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId +
1300002d39b4SEd Tanous                         "/" + sensorsAsyncResp->chassisSubNode +
1301002d39b4SEd Tanous                         "#/Redundancy/" + std::to_string(jResp.size());
1302002d39b4SEd Tanous                     redundancy["@odata.type"] = "#Redundancy.v1_3_2.Redundancy";
13031476687dSEd Tanous                     redundancy["MinNumNeeded"] = minNumNeeded;
13041476687dSEd Tanous                     redundancy["MemberId"] = name;
13051476687dSEd Tanous                     redundancy["Mode"] = "N+m";
13061476687dSEd Tanous                     redundancy["Name"] = name;
13071476687dSEd Tanous                     redundancy["RedundancySet"] = redfishCollection;
13081476687dSEd Tanous                     redundancy["Status"]["Health"] = health;
13091476687dSEd Tanous                     redundancy["Status"]["State"] = "Enabled";
13101476687dSEd Tanous 
13111476687dSEd Tanous                     jResp.push_back(std::move(redundancy));
13128bd25ccdSJames Feist                     },
1313002d39b4SEd Tanous                     owner, path, "org.freedesktop.DBus.Properties", "GetAll",
13148bd25ccdSJames Feist                     "xyz.openbmc_project.Control.FanRedundancy");
13151e1e598dSJonathan Doman                 });
13168bd25ccdSJames Feist         }
13178bd25ccdSJames Feist         },
13188bd25ccdSJames Feist         "xyz.openbmc_project.ObjectMapper",
13198bd25ccdSJames Feist         "/xyz/openbmc_project/object_mapper",
13208bd25ccdSJames Feist         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
13218bd25ccdSJames Feist         "/xyz/openbmc_project/control", 2,
13228bd25ccdSJames Feist         std::array<const char*, 1>{
13238bd25ccdSJames Feist             "xyz.openbmc_project.Control.FanRedundancy"});
13248bd25ccdSJames Feist }
13258bd25ccdSJames Feist 
1326b5a76932SEd Tanous inline void
132781ce609eSEd Tanous     sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
132849c53ac9SJohnathan Mantey {
13298d1b46d7Szhanghch05     nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue;
133049c53ac9SJohnathan Mantey     std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
133181ce609eSEd Tanous     if (sensorsAsyncResp->chassisSubNode == sensors::node::power)
133249c53ac9SJohnathan Mantey     {
133349c53ac9SJohnathan Mantey         sensorHeaders = {"Voltages", "PowerSupplies"};
133449c53ac9SJohnathan Mantey     }
133549c53ac9SJohnathan Mantey     for (const std::string& sensorGroup : sensorHeaders)
133649c53ac9SJohnathan Mantey     {
133749c53ac9SJohnathan Mantey         nlohmann::json::iterator entry = response.find(sensorGroup);
133849c53ac9SJohnathan Mantey         if (entry != response.end())
133949c53ac9SJohnathan Mantey         {
134049c53ac9SJohnathan Mantey             std::sort(entry->begin(), entry->end(),
134149c53ac9SJohnathan Mantey                       [](nlohmann::json& c1, nlohmann::json& c2) {
134249c53ac9SJohnathan Mantey                 return c1["Name"] < c2["Name"];
134349c53ac9SJohnathan Mantey             });
134449c53ac9SJohnathan Mantey 
134549c53ac9SJohnathan Mantey             // add the index counts to the end of each entry
134649c53ac9SJohnathan Mantey             size_t count = 0;
134749c53ac9SJohnathan Mantey             for (nlohmann::json& sensorJson : *entry)
134849c53ac9SJohnathan Mantey             {
134949c53ac9SJohnathan Mantey                 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
135049c53ac9SJohnathan Mantey                 if (odata == sensorJson.end())
135149c53ac9SJohnathan Mantey                 {
135249c53ac9SJohnathan Mantey                     continue;
135349c53ac9SJohnathan Mantey                 }
135449c53ac9SJohnathan Mantey                 std::string* value = odata->get_ptr<std::string*>();
135549c53ac9SJohnathan Mantey                 if (value != nullptr)
135649c53ac9SJohnathan Mantey                 {
135749c53ac9SJohnathan Mantey                     *value += std::to_string(count);
135849c53ac9SJohnathan Mantey                     count++;
135981ce609eSEd Tanous                     sensorsAsyncResp->updateUri(sensorJson["Name"], *value);
136049c53ac9SJohnathan Mantey                 }
136149c53ac9SJohnathan Mantey             }
136249c53ac9SJohnathan Mantey         }
136349c53ac9SJohnathan Mantey     }
136449c53ac9SJohnathan Mantey }
136549c53ac9SJohnathan Mantey 
136608777fb0SLewanczyk, Dawid /**
1367adc4f0dbSShawn McCarney  * @brief Finds the inventory item with the specified object path.
1368adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1369adc4f0dbSShawn McCarney  * @param invItemObjPath D-Bus object path of inventory item.
1370adc4f0dbSShawn McCarney  * @return Inventory item within vector, or nullptr if no match found.
13718fb49dd6SShawn McCarney  */
137223a21a1cSEd Tanous inline InventoryItem* findInventoryItem(
1373b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1374adc4f0dbSShawn McCarney     const std::string& invItemObjPath)
13758fb49dd6SShawn McCarney {
1376adc4f0dbSShawn McCarney     for (InventoryItem& inventoryItem : *inventoryItems)
13778fb49dd6SShawn McCarney     {
1378adc4f0dbSShawn McCarney         if (inventoryItem.objectPath == invItemObjPath)
13798fb49dd6SShawn McCarney         {
1380adc4f0dbSShawn McCarney             return &inventoryItem;
13818fb49dd6SShawn McCarney         }
13828fb49dd6SShawn McCarney     }
13838fb49dd6SShawn McCarney     return nullptr;
13848fb49dd6SShawn McCarney }
13858fb49dd6SShawn McCarney 
13868fb49dd6SShawn McCarney /**
1387adc4f0dbSShawn McCarney  * @brief Finds the inventory item associated with the specified sensor.
1388adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1389adc4f0dbSShawn McCarney  * @param sensorObjPath D-Bus object path of sensor.
1390adc4f0dbSShawn McCarney  * @return Inventory item within vector, or nullptr if no match found.
13918fb49dd6SShawn McCarney  */
139223a21a1cSEd Tanous inline InventoryItem* findInventoryItemForSensor(
1393b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1394adc4f0dbSShawn McCarney     const std::string& sensorObjPath)
1395adc4f0dbSShawn McCarney {
1396adc4f0dbSShawn McCarney     for (InventoryItem& inventoryItem : *inventoryItems)
1397adc4f0dbSShawn McCarney     {
1398adc4f0dbSShawn McCarney         if (inventoryItem.sensors.count(sensorObjPath) > 0)
1399adc4f0dbSShawn McCarney         {
1400adc4f0dbSShawn McCarney             return &inventoryItem;
1401adc4f0dbSShawn McCarney         }
1402adc4f0dbSShawn McCarney     }
1403adc4f0dbSShawn McCarney     return nullptr;
1404adc4f0dbSShawn McCarney }
1405adc4f0dbSShawn McCarney 
1406adc4f0dbSShawn McCarney /**
1407d500549bSAnthony Wilson  * @brief Finds the inventory item associated with the specified led path.
1408d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1409d500549bSAnthony Wilson  * @param ledObjPath D-Bus object path of led.
1410d500549bSAnthony Wilson  * @return Inventory item within vector, or nullptr if no match found.
1411d500549bSAnthony Wilson  */
1412d500549bSAnthony Wilson inline InventoryItem*
1413d500549bSAnthony Wilson     findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems,
1414d500549bSAnthony Wilson                             const std::string& ledObjPath)
1415d500549bSAnthony Wilson {
1416d500549bSAnthony Wilson     for (InventoryItem& inventoryItem : inventoryItems)
1417d500549bSAnthony Wilson     {
1418d500549bSAnthony Wilson         if (inventoryItem.ledObjectPath == ledObjPath)
1419d500549bSAnthony Wilson         {
1420d500549bSAnthony Wilson             return &inventoryItem;
1421d500549bSAnthony Wilson         }
1422d500549bSAnthony Wilson     }
1423d500549bSAnthony Wilson     return nullptr;
1424d500549bSAnthony Wilson }
1425d500549bSAnthony Wilson 
1426d500549bSAnthony Wilson /**
1427adc4f0dbSShawn McCarney  * @brief Adds inventory item and associated sensor to specified vector.
1428adc4f0dbSShawn McCarney  *
1429adc4f0dbSShawn McCarney  * Adds a new InventoryItem to the vector if necessary.  Searches for an
1430adc4f0dbSShawn McCarney  * existing InventoryItem with the specified object path.  If not found, one is
1431adc4f0dbSShawn McCarney  * added to the vector.
1432adc4f0dbSShawn McCarney  *
1433adc4f0dbSShawn McCarney  * Next, the specified sensor is added to the set of sensors associated with the
1434adc4f0dbSShawn McCarney  * InventoryItem.
1435adc4f0dbSShawn McCarney  *
1436adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1437adc4f0dbSShawn McCarney  * @param invItemObjPath D-Bus object path of inventory item.
1438adc4f0dbSShawn McCarney  * @param sensorObjPath D-Bus object path of sensor
1439adc4f0dbSShawn McCarney  */
1440b5a76932SEd Tanous inline void addInventoryItem(
1441b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1442b5a76932SEd Tanous     const std::string& invItemObjPath, const std::string& sensorObjPath)
1443adc4f0dbSShawn McCarney {
1444adc4f0dbSShawn McCarney     // Look for inventory item in vector
1445adc4f0dbSShawn McCarney     InventoryItem* inventoryItem =
1446adc4f0dbSShawn McCarney         findInventoryItem(inventoryItems, invItemObjPath);
1447adc4f0dbSShawn McCarney 
1448adc4f0dbSShawn McCarney     // If inventory item doesn't exist in vector, add it
1449adc4f0dbSShawn McCarney     if (inventoryItem == nullptr)
1450adc4f0dbSShawn McCarney     {
1451adc4f0dbSShawn McCarney         inventoryItems->emplace_back(invItemObjPath);
1452adc4f0dbSShawn McCarney         inventoryItem = &(inventoryItems->back());
1453adc4f0dbSShawn McCarney     }
1454adc4f0dbSShawn McCarney 
1455adc4f0dbSShawn McCarney     // Add sensor to set of sensors associated with inventory item
1456adc4f0dbSShawn McCarney     inventoryItem->sensors.emplace(sensorObjPath);
1457adc4f0dbSShawn McCarney }
1458adc4f0dbSShawn McCarney 
1459adc4f0dbSShawn McCarney /**
1460adc4f0dbSShawn McCarney  * @brief Stores D-Bus data in the specified inventory item.
1461adc4f0dbSShawn McCarney  *
1462adc4f0dbSShawn McCarney  * Finds D-Bus data in the specified map of interfaces.  Stores the data in the
1463adc4f0dbSShawn McCarney  * specified InventoryItem.
1464adc4f0dbSShawn McCarney  *
1465adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
1466adc4f0dbSShawn McCarney  * response.
1467adc4f0dbSShawn McCarney  *
1468adc4f0dbSShawn McCarney  * @param inventoryItem Inventory item where data will be stored.
1469adc4f0dbSShawn McCarney  * @param interfacesDict Map containing D-Bus interfaces and their properties
1470adc4f0dbSShawn McCarney  * for the specified inventory item.
1471adc4f0dbSShawn McCarney  */
147223a21a1cSEd Tanous inline void storeInventoryItemData(
1473adc4f0dbSShawn McCarney     InventoryItem& inventoryItem,
1474711ac7a9SEd Tanous     const dbus::utility::DBusInteracesMap& interfacesDict)
14758fb49dd6SShawn McCarney {
1476adc4f0dbSShawn McCarney     // Get properties from Inventory.Item interface
1477711ac7a9SEd Tanous 
14789eb808c1SEd Tanous     for (const auto& [interface, values] : interfacesDict)
14798fb49dd6SShawn McCarney     {
1480711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Item")
14818fb49dd6SShawn McCarney         {
14829eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
1483711ac7a9SEd Tanous             {
1484711ac7a9SEd Tanous                 if (name == "Present")
1485711ac7a9SEd Tanous                 {
1486711ac7a9SEd Tanous                     const bool* value = std::get_if<bool>(&dbusValue);
1487adc4f0dbSShawn McCarney                     if (value != nullptr)
14888fb49dd6SShawn McCarney                     {
1489adc4f0dbSShawn McCarney                         inventoryItem.isPresent = *value;
14908fb49dd6SShawn McCarney                     }
14918fb49dd6SShawn McCarney                 }
14928fb49dd6SShawn McCarney             }
1493711ac7a9SEd Tanous         }
1494adc4f0dbSShawn McCarney         // Check if Inventory.Item.PowerSupply interface is present
1495711ac7a9SEd Tanous 
1496711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Item.PowerSupply")
14978fb49dd6SShawn McCarney         {
1498adc4f0dbSShawn McCarney             inventoryItem.isPowerSupply = true;
14998fb49dd6SShawn McCarney         }
1500adc4f0dbSShawn McCarney 
1501adc4f0dbSShawn McCarney         // Get properties from Inventory.Decorator.Asset interface
1502711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
1503adc4f0dbSShawn McCarney         {
15049eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
1505711ac7a9SEd Tanous             {
1506711ac7a9SEd Tanous                 if (name == "Manufacturer")
1507adc4f0dbSShawn McCarney                 {
1508adc4f0dbSShawn McCarney                     const std::string* value =
1509711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
1510adc4f0dbSShawn McCarney                     if (value != nullptr)
1511adc4f0dbSShawn McCarney                     {
1512adc4f0dbSShawn McCarney                         inventoryItem.manufacturer = *value;
1513adc4f0dbSShawn McCarney                     }
1514adc4f0dbSShawn McCarney                 }
1515711ac7a9SEd Tanous                 if (name == "Model")
1516adc4f0dbSShawn McCarney                 {
1517adc4f0dbSShawn McCarney                     const std::string* value =
1518711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
1519adc4f0dbSShawn McCarney                     if (value != nullptr)
1520adc4f0dbSShawn McCarney                     {
1521adc4f0dbSShawn McCarney                         inventoryItem.model = *value;
1522adc4f0dbSShawn McCarney                     }
1523adc4f0dbSShawn McCarney                 }
1524711ac7a9SEd Tanous                 if (name == "SerialNumber")
1525adc4f0dbSShawn McCarney                 {
1526adc4f0dbSShawn McCarney                     const std::string* value =
1527711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
1528adc4f0dbSShawn McCarney                     if (value != nullptr)
1529adc4f0dbSShawn McCarney                     {
1530adc4f0dbSShawn McCarney                         inventoryItem.serialNumber = *value;
1531adc4f0dbSShawn McCarney                     }
1532adc4f0dbSShawn McCarney                 }
1533711ac7a9SEd Tanous                 if (name == "PartNumber")
1534711ac7a9SEd Tanous                 {
1535711ac7a9SEd Tanous                     const std::string* value =
1536711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
1537711ac7a9SEd Tanous                     if (value != nullptr)
1538711ac7a9SEd Tanous                     {
1539711ac7a9SEd Tanous                         inventoryItem.partNumber = *value;
1540711ac7a9SEd Tanous                     }
1541711ac7a9SEd Tanous                 }
1542711ac7a9SEd Tanous             }
1543adc4f0dbSShawn McCarney         }
1544adc4f0dbSShawn McCarney 
1545711ac7a9SEd Tanous         if (interface ==
1546711ac7a9SEd Tanous             "xyz.openbmc_project.State.Decorator.OperationalStatus")
1547adc4f0dbSShawn McCarney         {
15489eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
1549adc4f0dbSShawn McCarney             {
1550711ac7a9SEd Tanous                 if (name == "Functional")
1551711ac7a9SEd Tanous                 {
1552711ac7a9SEd Tanous                     const bool* value = std::get_if<bool>(&dbusValue);
1553adc4f0dbSShawn McCarney                     if (value != nullptr)
1554adc4f0dbSShawn McCarney                     {
1555adc4f0dbSShawn McCarney                         inventoryItem.isFunctional = *value;
15568fb49dd6SShawn McCarney                     }
15578fb49dd6SShawn McCarney                 }
15588fb49dd6SShawn McCarney             }
15598fb49dd6SShawn McCarney         }
1560711ac7a9SEd Tanous     }
1561711ac7a9SEd Tanous }
15628fb49dd6SShawn McCarney 
15638fb49dd6SShawn McCarney /**
1564adc4f0dbSShawn McCarney  * @brief Gets D-Bus data for inventory items associated with sensors.
15658fb49dd6SShawn McCarney  *
1566adc4f0dbSShawn McCarney  * Uses the specified connections (services) to obtain D-Bus data for inventory
1567adc4f0dbSShawn McCarney  * items associated with sensors.  Stores the resulting data in the
1568adc4f0dbSShawn McCarney  * inventoryItems vector.
15698fb49dd6SShawn McCarney  *
1570adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
1571adc4f0dbSShawn McCarney  * response.
1572adc4f0dbSShawn McCarney  *
1573adc4f0dbSShawn McCarney  * Finds the inventory item data asynchronously.  Invokes callback when data has
1574adc4f0dbSShawn McCarney  * been obtained.
1575adc4f0dbSShawn McCarney  *
1576adc4f0dbSShawn McCarney  * The callback must have the following signature:
1577adc4f0dbSShawn McCarney  *   @code
1578d500549bSAnthony Wilson  *   callback(void)
1579adc4f0dbSShawn McCarney  *   @endcode
1580adc4f0dbSShawn McCarney  *
1581adc4f0dbSShawn McCarney  * This function is called recursively, obtaining data asynchronously from one
1582adc4f0dbSShawn McCarney  * connection in each call.  This ensures the callback is not invoked until the
1583adc4f0dbSShawn McCarney  * last asynchronous function has completed.
15848fb49dd6SShawn McCarney  *
15858fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
1586adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1587adc4f0dbSShawn McCarney  * @param invConnections Connections that provide data for the inventory items.
15888fb49dd6SShawn McCarney  * @param objectMgrPaths Mappings from connection name to DBus object path that
15898fb49dd6SShawn McCarney  * implements ObjectManager.
1590adc4f0dbSShawn McCarney  * @param callback Callback to invoke when inventory data has been obtained.
1591adc4f0dbSShawn McCarney  * @param invConnectionsIndex Current index in invConnections.  Only specified
1592adc4f0dbSShawn McCarney  * in recursive calls to this function.
15938fb49dd6SShawn McCarney  */
1594adc4f0dbSShawn McCarney template <typename Callback>
1595adc4f0dbSShawn McCarney static void getInventoryItemsData(
15968fb49dd6SShawn McCarney     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1597adc4f0dbSShawn McCarney     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
15988fb49dd6SShawn McCarney     std::shared_ptr<boost::container::flat_set<std::string>> invConnections,
15998fb49dd6SShawn McCarney     std::shared_ptr<boost::container::flat_map<std::string, std::string>>
1600adc4f0dbSShawn McCarney         objectMgrPaths,
1601271584abSEd Tanous     Callback&& callback, size_t invConnectionsIndex = 0)
16028fb49dd6SShawn McCarney {
1603adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsData enter";
16048fb49dd6SShawn McCarney 
1605adc4f0dbSShawn McCarney     // If no more connections left, call callback
1606adc4f0dbSShawn McCarney     if (invConnectionsIndex >= invConnections->size())
16078fb49dd6SShawn McCarney     {
1608d500549bSAnthony Wilson         callback();
1609adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
1610adc4f0dbSShawn McCarney         return;
1611adc4f0dbSShawn McCarney     }
1612adc4f0dbSShawn McCarney 
1613adc4f0dbSShawn McCarney     // Get inventory item data from current connection
1614adc4f0dbSShawn McCarney     auto it = invConnections->nth(invConnectionsIndex);
1615adc4f0dbSShawn McCarney     if (it != invConnections->end())
1616adc4f0dbSShawn McCarney     {
1617adc4f0dbSShawn McCarney         const std::string& invConnection = *it;
1618adc4f0dbSShawn McCarney 
16198fb49dd6SShawn McCarney         // Response handler for GetManagedObjects
1620002d39b4SEd Tanous         auto respHandler =
1621002d39b4SEd Tanous             [sensorsAsyncResp, inventoryItems, invConnections, objectMgrPaths,
1622f94c4ecfSEd Tanous              callback{std::forward<Callback>(callback)},
1623002d39b4SEd Tanous              invConnectionsIndex](const boost::system::error_code ec,
1624711ac7a9SEd Tanous                                   dbus::utility::ManagedObjectType& resp) {
1625adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler enter";
16268fb49dd6SShawn McCarney             if (ec)
16278fb49dd6SShawn McCarney             {
16288fb49dd6SShawn McCarney                 BMCWEB_LOG_ERROR
1629adc4f0dbSShawn McCarney                     << "getInventoryItemsData respHandler DBus error " << ec;
16308d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
16318fb49dd6SShawn McCarney                 return;
16328fb49dd6SShawn McCarney             }
16338fb49dd6SShawn McCarney 
16348fb49dd6SShawn McCarney             // Loop through returned object paths
16358fb49dd6SShawn McCarney             for (const auto& objDictEntry : resp)
16368fb49dd6SShawn McCarney             {
16378fb49dd6SShawn McCarney                 const std::string& objPath =
16388fb49dd6SShawn McCarney                     static_cast<const std::string&>(objDictEntry.first);
16398fb49dd6SShawn McCarney 
1640adc4f0dbSShawn McCarney                 // If this object path is one of the specified inventory items
1641adc4f0dbSShawn McCarney                 InventoryItem* inventoryItem =
1642adc4f0dbSShawn McCarney                     findInventoryItem(inventoryItems, objPath);
1643adc4f0dbSShawn McCarney                 if (inventoryItem != nullptr)
16448fb49dd6SShawn McCarney                 {
1645adc4f0dbSShawn McCarney                     // Store inventory data in InventoryItem
1646adc4f0dbSShawn McCarney                     storeInventoryItemData(*inventoryItem, objDictEntry.second);
16478fb49dd6SShawn McCarney                 }
16488fb49dd6SShawn McCarney             }
16498fb49dd6SShawn McCarney 
1650adc4f0dbSShawn McCarney             // Recurse to get inventory item data from next connection
1651adc4f0dbSShawn McCarney             getInventoryItemsData(sensorsAsyncResp, inventoryItems,
1652adc4f0dbSShawn McCarney                                   invConnections, objectMgrPaths,
1653adc4f0dbSShawn McCarney                                   std::move(callback), invConnectionsIndex + 1);
1654adc4f0dbSShawn McCarney 
1655adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler exit";
16568fb49dd6SShawn McCarney         };
16578fb49dd6SShawn McCarney 
16588fb49dd6SShawn McCarney         // Find DBus object path that implements ObjectManager for the current
16598fb49dd6SShawn McCarney         // connection.  If no mapping found, default to "/".
16608fb49dd6SShawn McCarney         auto iter = objectMgrPaths->find(invConnection);
16618fb49dd6SShawn McCarney         const std::string& objectMgrPath =
16628fb49dd6SShawn McCarney             (iter != objectMgrPaths->end()) ? iter->second : "/";
16638fb49dd6SShawn McCarney         BMCWEB_LOG_DEBUG << "ObjectManager path for " << invConnection << " is "
16648fb49dd6SShawn McCarney                          << objectMgrPath;
16658fb49dd6SShawn McCarney 
16668fb49dd6SShawn McCarney         // Get all object paths and their interfaces for current connection
16678fb49dd6SShawn McCarney         crow::connections::systemBus->async_method_call(
16688fb49dd6SShawn McCarney             std::move(respHandler), invConnection, objectMgrPath,
16698fb49dd6SShawn McCarney             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
16708fb49dd6SShawn McCarney     }
16718fb49dd6SShawn McCarney 
1672adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
16738fb49dd6SShawn McCarney }
16748fb49dd6SShawn McCarney 
16758fb49dd6SShawn McCarney /**
1676adc4f0dbSShawn McCarney  * @brief Gets connections that provide D-Bus data for inventory items.
16778fb49dd6SShawn McCarney  *
1678adc4f0dbSShawn McCarney  * Gets the D-Bus connections (services) that provide data for the inventory
1679adc4f0dbSShawn McCarney  * items that are associated with sensors.
16808fb49dd6SShawn McCarney  *
16818fb49dd6SShawn McCarney  * Finds the connections asynchronously.  Invokes callback when information has
16828fb49dd6SShawn McCarney  * been obtained.
16838fb49dd6SShawn McCarney  *
16848fb49dd6SShawn McCarney  * The callback must have the following signature:
16858fb49dd6SShawn McCarney  *   @code
16868fb49dd6SShawn McCarney  *   callback(std::shared_ptr<boost::container::flat_set<std::string>>
16878fb49dd6SShawn McCarney  *            invConnections)
16888fb49dd6SShawn McCarney  *   @endcode
16898fb49dd6SShawn McCarney  *
16908fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
1691adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
16928fb49dd6SShawn McCarney  * @param callback Callback to invoke when connections have been obtained.
16938fb49dd6SShawn McCarney  */
16948fb49dd6SShawn McCarney template <typename Callback>
16958fb49dd6SShawn McCarney static void getInventoryItemsConnections(
1696b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1697b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
16988fb49dd6SShawn McCarney     Callback&& callback)
16998fb49dd6SShawn McCarney {
17008fb49dd6SShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsConnections enter";
17018fb49dd6SShawn McCarney 
17028fb49dd6SShawn McCarney     const std::string path = "/xyz/openbmc_project/inventory";
1703adc4f0dbSShawn McCarney     const std::array<std::string, 4> interfaces = {
17048fb49dd6SShawn McCarney         "xyz.openbmc_project.Inventory.Item",
1705adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Item.PowerSupply",
1706adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Decorator.Asset",
17078fb49dd6SShawn McCarney         "xyz.openbmc_project.State.Decorator.OperationalStatus"};
17088fb49dd6SShawn McCarney 
17098fb49dd6SShawn McCarney     // Response handler for parsing output from GetSubTree
1710002d39b4SEd Tanous     auto respHandler =
1711002d39b4SEd Tanous         [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1712002d39b4SEd Tanous          inventoryItems](
1713b9d36b47SEd Tanous             const boost::system::error_code ec,
1714002d39b4SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) {
17158fb49dd6SShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler enter";
17168fb49dd6SShawn McCarney         if (ec)
17178fb49dd6SShawn McCarney         {
17188d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
17198fb49dd6SShawn McCarney             BMCWEB_LOG_ERROR
17208fb49dd6SShawn McCarney                 << "getInventoryItemsConnections respHandler DBus error " << ec;
17218fb49dd6SShawn McCarney             return;
17228fb49dd6SShawn McCarney         }
17238fb49dd6SShawn McCarney 
17248fb49dd6SShawn McCarney         // Make unique list of connections for desired inventory items
17258fb49dd6SShawn McCarney         std::shared_ptr<boost::container::flat_set<std::string>>
17268fb49dd6SShawn McCarney             invConnections =
17278fb49dd6SShawn McCarney                 std::make_shared<boost::container::flat_set<std::string>>();
17288fb49dd6SShawn McCarney         invConnections->reserve(8);
17298fb49dd6SShawn McCarney 
17308fb49dd6SShawn McCarney         // Loop through objects from GetSubTree
17318fb49dd6SShawn McCarney         for (const std::pair<
17328fb49dd6SShawn McCarney                  std::string,
17338fb49dd6SShawn McCarney                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
17348fb49dd6SShawn McCarney                  object : subtree)
17358fb49dd6SShawn McCarney         {
1736adc4f0dbSShawn McCarney             // Check if object path is one of the specified inventory items
17378fb49dd6SShawn McCarney             const std::string& objPath = object.first;
1738adc4f0dbSShawn McCarney             if (findInventoryItem(inventoryItems, objPath) != nullptr)
17398fb49dd6SShawn McCarney             {
17408fb49dd6SShawn McCarney                 // Store all connections to inventory item
17418fb49dd6SShawn McCarney                 for (const std::pair<std::string, std::vector<std::string>>&
17428fb49dd6SShawn McCarney                          objData : object.second)
17438fb49dd6SShawn McCarney                 {
17448fb49dd6SShawn McCarney                     const std::string& invConnection = objData.first;
17458fb49dd6SShawn McCarney                     invConnections->insert(invConnection);
17468fb49dd6SShawn McCarney                 }
17478fb49dd6SShawn McCarney             }
17488fb49dd6SShawn McCarney         }
1749d500549bSAnthony Wilson 
17508fb49dd6SShawn McCarney         callback(invConnections);
17518fb49dd6SShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler exit";
17528fb49dd6SShawn McCarney     };
17538fb49dd6SShawn McCarney 
17548fb49dd6SShawn McCarney     // Make call to ObjectMapper to find all inventory items
17558fb49dd6SShawn McCarney     crow::connections::systemBus->async_method_call(
17568fb49dd6SShawn McCarney         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
17578fb49dd6SShawn McCarney         "/xyz/openbmc_project/object_mapper",
17588fb49dd6SShawn McCarney         "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
17598fb49dd6SShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsConnections exit";
17608fb49dd6SShawn McCarney }
17618fb49dd6SShawn McCarney 
17628fb49dd6SShawn McCarney /**
1763adc4f0dbSShawn McCarney  * @brief Gets associations from sensors to inventory items.
17648fb49dd6SShawn McCarney  *
17658fb49dd6SShawn McCarney  * Looks for ObjectMapper associations from the specified sensors to related
1766d500549bSAnthony Wilson  * inventory items. Then finds the associations from those inventory items to
1767d500549bSAnthony Wilson  * their LEDs, if any.
17688fb49dd6SShawn McCarney  *
17698fb49dd6SShawn McCarney  * Finds the inventory items asynchronously.  Invokes callback when information
17708fb49dd6SShawn McCarney  * has been obtained.
17718fb49dd6SShawn McCarney  *
17728fb49dd6SShawn McCarney  * The callback must have the following signature:
17738fb49dd6SShawn McCarney  *   @code
1774adc4f0dbSShawn McCarney  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
17758fb49dd6SShawn McCarney  *   @endcode
17768fb49dd6SShawn McCarney  *
17778fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
17788fb49dd6SShawn McCarney  * @param sensorNames All sensors within the current chassis.
17798fb49dd6SShawn McCarney  * @param objectMgrPaths Mappings from connection name to DBus object path that
17808fb49dd6SShawn McCarney  * implements ObjectManager.
17818fb49dd6SShawn McCarney  * @param callback Callback to invoke when inventory items have been obtained.
17828fb49dd6SShawn McCarney  */
17838fb49dd6SShawn McCarney template <typename Callback>
1784adc4f0dbSShawn McCarney static void getInventoryItemAssociations(
1785b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1786b5a76932SEd Tanous     const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames,
1787b5a76932SEd Tanous     const std::shared_ptr<boost::container::flat_map<std::string, std::string>>&
17888fb49dd6SShawn McCarney         objectMgrPaths,
17898fb49dd6SShawn McCarney     Callback&& callback)
17908fb49dd6SShawn McCarney {
1791adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemAssociations enter";
17928fb49dd6SShawn McCarney 
17938fb49dd6SShawn McCarney     // Response handler for GetManagedObjects
1794f94c4ecfSEd Tanous     auto respHandler = [callback{std::forward<Callback>(callback)},
1795f94c4ecfSEd Tanous                         sensorsAsyncResp,
17968fb49dd6SShawn McCarney                         sensorNames](const boost::system::error_code ec,
17978fb49dd6SShawn McCarney                                      dbus::utility::ManagedObjectType& resp) {
1798adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler enter";
17998fb49dd6SShawn McCarney         if (ec)
18008fb49dd6SShawn McCarney         {
1801adc4f0dbSShawn McCarney             BMCWEB_LOG_ERROR
1802adc4f0dbSShawn McCarney                 << "getInventoryItemAssociations respHandler DBus error " << ec;
18038d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
18048fb49dd6SShawn McCarney             return;
18058fb49dd6SShawn McCarney         }
18068fb49dd6SShawn McCarney 
1807adc4f0dbSShawn McCarney         // Create vector to hold list of inventory items
1808adc4f0dbSShawn McCarney         std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1809adc4f0dbSShawn McCarney             std::make_shared<std::vector<InventoryItem>>();
1810adc4f0dbSShawn McCarney 
18118fb49dd6SShawn McCarney         // Loop through returned object paths
18128fb49dd6SShawn McCarney         std::string sensorAssocPath;
18138fb49dd6SShawn McCarney         sensorAssocPath.reserve(128); // avoid memory allocations
18148fb49dd6SShawn McCarney         for (const auto& objDictEntry : resp)
18158fb49dd6SShawn McCarney         {
18168fb49dd6SShawn McCarney             const std::string& objPath =
18178fb49dd6SShawn McCarney                 static_cast<const std::string&>(objDictEntry.first);
18188fb49dd6SShawn McCarney 
18198fb49dd6SShawn McCarney             // If path is inventory association for one of the specified sensors
18208fb49dd6SShawn McCarney             for (const std::string& sensorName : *sensorNames)
18218fb49dd6SShawn McCarney             {
18228fb49dd6SShawn McCarney                 sensorAssocPath = sensorName;
18238fb49dd6SShawn McCarney                 sensorAssocPath += "/inventory";
18248fb49dd6SShawn McCarney                 if (objPath == sensorAssocPath)
18258fb49dd6SShawn McCarney                 {
18268fb49dd6SShawn McCarney                     // Get Association interface for object path
1827711ac7a9SEd Tanous                     for (const auto& [interface, values] : objDictEntry.second)
18288fb49dd6SShawn McCarney                     {
1829711ac7a9SEd Tanous                         if (interface == "xyz.openbmc_project.Association")
1830711ac7a9SEd Tanous                         {
1831711ac7a9SEd Tanous                             for (const auto& [valueName, value] : values)
1832711ac7a9SEd Tanous                             {
1833711ac7a9SEd Tanous                                 if (valueName == "endpoints")
18348fb49dd6SShawn McCarney                                 {
18358fb49dd6SShawn McCarney                                     const std::vector<std::string>* endpoints =
18368fb49dd6SShawn McCarney                                         std::get_if<std::vector<std::string>>(
1837711ac7a9SEd Tanous                                             &value);
1838711ac7a9SEd Tanous                                     if ((endpoints != nullptr) &&
1839711ac7a9SEd Tanous                                         !endpoints->empty())
18408fb49dd6SShawn McCarney                                     {
1841adc4f0dbSShawn McCarney                                         // Add inventory item to vector
1842adc4f0dbSShawn McCarney                                         const std::string& invItemPath =
1843adc4f0dbSShawn McCarney                                             endpoints->front();
1844711ac7a9SEd Tanous                                         addInventoryItem(inventoryItems,
1845711ac7a9SEd Tanous                                                          invItemPath,
1846adc4f0dbSShawn McCarney                                                          sensorName);
18478fb49dd6SShawn McCarney                                     }
18488fb49dd6SShawn McCarney                                 }
18498fb49dd6SShawn McCarney                             }
1850711ac7a9SEd Tanous                         }
1851711ac7a9SEd Tanous                     }
18528fb49dd6SShawn McCarney                     break;
18538fb49dd6SShawn McCarney                 }
18548fb49dd6SShawn McCarney             }
18558fb49dd6SShawn McCarney         }
18568fb49dd6SShawn McCarney 
1857d500549bSAnthony Wilson         // Now loop through the returned object paths again, this time to
1858d500549bSAnthony Wilson         // find the leds associated with the inventory items we just found
1859d500549bSAnthony Wilson         std::string inventoryAssocPath;
1860d500549bSAnthony Wilson         inventoryAssocPath.reserve(128); // avoid memory allocations
1861d500549bSAnthony Wilson         for (const auto& objDictEntry : resp)
1862d500549bSAnthony Wilson         {
1863d500549bSAnthony Wilson             const std::string& objPath =
1864d500549bSAnthony Wilson                 static_cast<const std::string&>(objDictEntry.first);
1865d500549bSAnthony Wilson 
1866d500549bSAnthony Wilson             for (InventoryItem& inventoryItem : *inventoryItems)
1867d500549bSAnthony Wilson             {
1868d500549bSAnthony Wilson                 inventoryAssocPath = inventoryItem.objectPath;
1869d500549bSAnthony Wilson                 inventoryAssocPath += "/leds";
1870d500549bSAnthony Wilson                 if (objPath == inventoryAssocPath)
1871d500549bSAnthony Wilson                 {
1872711ac7a9SEd Tanous                     for (const auto& [interface, values] : objDictEntry.second)
1873d500549bSAnthony Wilson                     {
1874711ac7a9SEd Tanous                         if (interface == "xyz.openbmc_project.Association")
1875711ac7a9SEd Tanous                         {
1876711ac7a9SEd Tanous                             for (const auto& [valueName, value] : values)
1877711ac7a9SEd Tanous                             {
1878711ac7a9SEd Tanous                                 if (valueName == "endpoints")
1879d500549bSAnthony Wilson                                 {
1880d500549bSAnthony Wilson                                     const std::vector<std::string>* endpoints =
1881d500549bSAnthony Wilson                                         std::get_if<std::vector<std::string>>(
1882711ac7a9SEd Tanous                                             &value);
1883711ac7a9SEd Tanous                                     if ((endpoints != nullptr) &&
1884711ac7a9SEd Tanous                                         !endpoints->empty())
1885d500549bSAnthony Wilson                                     {
1886711ac7a9SEd Tanous                                         // Add inventory item to vector
1887d500549bSAnthony Wilson                                         // Store LED path in inventory item
1888711ac7a9SEd Tanous                                         const std::string& ledPath =
1889711ac7a9SEd Tanous                                             endpoints->front();
1890d500549bSAnthony Wilson                                         inventoryItem.ledObjectPath = ledPath;
1891d500549bSAnthony Wilson                                     }
1892d500549bSAnthony Wilson                                 }
1893d500549bSAnthony Wilson                             }
1894711ac7a9SEd Tanous                         }
1895711ac7a9SEd Tanous                     }
1896711ac7a9SEd Tanous 
1897d500549bSAnthony Wilson                     break;
1898d500549bSAnthony Wilson                 }
1899d500549bSAnthony Wilson             }
1900d500549bSAnthony Wilson         }
1901adc4f0dbSShawn McCarney         callback(inventoryItems);
1902adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler exit";
19038fb49dd6SShawn McCarney     };
19048fb49dd6SShawn McCarney 
19058fb49dd6SShawn McCarney     // Find DBus object path that implements ObjectManager for ObjectMapper
19068fb49dd6SShawn McCarney     std::string connection = "xyz.openbmc_project.ObjectMapper";
19078fb49dd6SShawn McCarney     auto iter = objectMgrPaths->find(connection);
19088fb49dd6SShawn McCarney     const std::string& objectMgrPath =
19098fb49dd6SShawn McCarney         (iter != objectMgrPaths->end()) ? iter->second : "/";
19108fb49dd6SShawn McCarney     BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
19118fb49dd6SShawn McCarney                      << objectMgrPath;
19128fb49dd6SShawn McCarney 
19138fb49dd6SShawn McCarney     // Call GetManagedObjects on the ObjectMapper to get all associations
19148fb49dd6SShawn McCarney     crow::connections::systemBus->async_method_call(
19158fb49dd6SShawn McCarney         std::move(respHandler), connection, objectMgrPath,
19168fb49dd6SShawn McCarney         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
19178fb49dd6SShawn McCarney 
1918adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemAssociations exit";
19198fb49dd6SShawn McCarney }
19208fb49dd6SShawn McCarney 
19218fb49dd6SShawn McCarney /**
1922d500549bSAnthony Wilson  * @brief Gets D-Bus data for inventory item leds associated with sensors.
1923d500549bSAnthony Wilson  *
1924d500549bSAnthony Wilson  * Uses the specified connections (services) to obtain D-Bus data for inventory
1925d500549bSAnthony Wilson  * item leds associated with sensors.  Stores the resulting data in the
1926d500549bSAnthony Wilson  * inventoryItems vector.
1927d500549bSAnthony Wilson  *
1928d500549bSAnthony Wilson  * This data is later used to provide sensor property values in the JSON
1929d500549bSAnthony Wilson  * response.
1930d500549bSAnthony Wilson  *
1931d500549bSAnthony Wilson  * Finds the inventory item led data asynchronously.  Invokes callback when data
1932d500549bSAnthony Wilson  * has been obtained.
1933d500549bSAnthony Wilson  *
1934d500549bSAnthony Wilson  * The callback must have the following signature:
1935d500549bSAnthony Wilson  *   @code
193642cbe538SGunnar Mills  *   callback()
1937d500549bSAnthony Wilson  *   @endcode
1938d500549bSAnthony Wilson  *
1939d500549bSAnthony Wilson  * This function is called recursively, obtaining data asynchronously from one
1940d500549bSAnthony Wilson  * connection in each call.  This ensures the callback is not invoked until the
1941d500549bSAnthony Wilson  * last asynchronous function has completed.
1942d500549bSAnthony Wilson  *
1943d500549bSAnthony Wilson  * @param sensorsAsyncResp Pointer to object holding response data.
1944d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1945d500549bSAnthony Wilson  * @param ledConnections Connections that provide data for the inventory leds.
1946d500549bSAnthony Wilson  * @param callback Callback to invoke when inventory data has been obtained.
1947d500549bSAnthony Wilson  * @param ledConnectionsIndex Current index in ledConnections.  Only specified
1948d500549bSAnthony Wilson  * in recursive calls to this function.
1949d500549bSAnthony Wilson  */
1950d500549bSAnthony Wilson template <typename Callback>
1951d500549bSAnthony Wilson void getInventoryLedData(
1952d500549bSAnthony Wilson     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1953d500549bSAnthony Wilson     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1954d500549bSAnthony Wilson     std::shared_ptr<boost::container::flat_map<std::string, std::string>>
1955d500549bSAnthony Wilson         ledConnections,
1956d500549bSAnthony Wilson     Callback&& callback, size_t ledConnectionsIndex = 0)
1957d500549bSAnthony Wilson {
1958d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLedData enter";
1959d500549bSAnthony Wilson 
1960d500549bSAnthony Wilson     // If no more connections left, call callback
1961d500549bSAnthony Wilson     if (ledConnectionsIndex >= ledConnections->size())
1962d500549bSAnthony Wilson     {
196342cbe538SGunnar Mills         callback();
1964d500549bSAnthony Wilson         BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1965d500549bSAnthony Wilson         return;
1966d500549bSAnthony Wilson     }
1967d500549bSAnthony Wilson 
1968d500549bSAnthony Wilson     // Get inventory item data from current connection
1969d500549bSAnthony Wilson     auto it = ledConnections->nth(ledConnectionsIndex);
1970d500549bSAnthony Wilson     if (it != ledConnections->end())
1971d500549bSAnthony Wilson     {
1972d500549bSAnthony Wilson         const std::string& ledPath = (*it).first;
1973d500549bSAnthony Wilson         const std::string& ledConnection = (*it).second;
1974d500549bSAnthony Wilson         // Response handler for Get State property
19751e1e598dSJonathan Doman         auto respHandler =
19761e1e598dSJonathan Doman             [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
1977f94c4ecfSEd Tanous              callback{std::forward<Callback>(callback)}, ledConnectionsIndex](
19781e1e598dSJonathan Doman                 const boost::system::error_code ec, const std::string& state) {
1979d500549bSAnthony Wilson             BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler enter";
1980d500549bSAnthony Wilson             if (ec)
1981d500549bSAnthony Wilson             {
1982d500549bSAnthony Wilson                 BMCWEB_LOG_ERROR
1983d500549bSAnthony Wilson                     << "getInventoryLedData respHandler DBus error " << ec;
19848d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
1985d500549bSAnthony Wilson                 return;
1986d500549bSAnthony Wilson             }
1987d500549bSAnthony Wilson 
19881e1e598dSJonathan Doman             BMCWEB_LOG_DEBUG << "Led state: " << state;
1989d500549bSAnthony Wilson             // Find inventory item with this LED object path
1990d500549bSAnthony Wilson             InventoryItem* inventoryItem =
1991d500549bSAnthony Wilson                 findInventoryItemForLed(*inventoryItems, ledPath);
1992d500549bSAnthony Wilson             if (inventoryItem != nullptr)
1993d500549bSAnthony Wilson             {
1994d500549bSAnthony Wilson                 // Store LED state in InventoryItem
19951e1e598dSJonathan Doman                 if (boost::ends_with(state, "On"))
1996d500549bSAnthony Wilson                 {
1997d500549bSAnthony Wilson                     inventoryItem->ledState = LedState::ON;
1998d500549bSAnthony Wilson                 }
19991e1e598dSJonathan Doman                 else if (boost::ends_with(state, "Blink"))
2000d500549bSAnthony Wilson                 {
2001d500549bSAnthony Wilson                     inventoryItem->ledState = LedState::BLINK;
2002d500549bSAnthony Wilson                 }
20031e1e598dSJonathan Doman                 else if (boost::ends_with(state, "Off"))
2004d500549bSAnthony Wilson                 {
2005d500549bSAnthony Wilson                     inventoryItem->ledState = LedState::OFF;
2006d500549bSAnthony Wilson                 }
2007d500549bSAnthony Wilson                 else
2008d500549bSAnthony Wilson                 {
2009d500549bSAnthony Wilson                     inventoryItem->ledState = LedState::UNKNOWN;
2010d500549bSAnthony Wilson                 }
2011d500549bSAnthony Wilson             }
2012d500549bSAnthony Wilson 
2013d500549bSAnthony Wilson             // Recurse to get LED data from next connection
2014d500549bSAnthony Wilson             getInventoryLedData(sensorsAsyncResp, inventoryItems,
2015d500549bSAnthony Wilson                                 ledConnections, std::move(callback),
2016d500549bSAnthony Wilson                                 ledConnectionsIndex + 1);
2017d500549bSAnthony Wilson 
2018d500549bSAnthony Wilson             BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler exit";
2019d500549bSAnthony Wilson         };
2020d500549bSAnthony Wilson 
2021d500549bSAnthony Wilson         // Get the State property for the current LED
20221e1e598dSJonathan Doman         sdbusplus::asio::getProperty<std::string>(
20231e1e598dSJonathan Doman             *crow::connections::systemBus, ledConnection, ledPath,
20241e1e598dSJonathan Doman             "xyz.openbmc_project.Led.Physical", "State",
20251e1e598dSJonathan Doman             std::move(respHandler));
2026d500549bSAnthony Wilson     }
2027d500549bSAnthony Wilson 
2028d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
2029d500549bSAnthony Wilson }
2030d500549bSAnthony Wilson 
2031d500549bSAnthony Wilson /**
2032d500549bSAnthony Wilson  * @brief Gets LED data for LEDs associated with given inventory items.
2033d500549bSAnthony Wilson  *
2034d500549bSAnthony Wilson  * Gets the D-Bus connections (services) that provide LED data for the LEDs
2035d500549bSAnthony Wilson  * associated with the specified inventory items.  Then gets the LED data from
2036d500549bSAnthony Wilson  * each connection and stores it in the inventory item.
2037d500549bSAnthony Wilson  *
2038d500549bSAnthony Wilson  * This data is later used to provide sensor property values in the JSON
2039d500549bSAnthony Wilson  * response.
2040d500549bSAnthony Wilson  *
2041d500549bSAnthony Wilson  * Finds the LED data asynchronously.  Invokes callback when information has
2042d500549bSAnthony Wilson  * been obtained.
2043d500549bSAnthony Wilson  *
2044d500549bSAnthony Wilson  * The callback must have the following signature:
2045d500549bSAnthony Wilson  *   @code
204642cbe538SGunnar Mills  *   callback()
2047d500549bSAnthony Wilson  *   @endcode
2048d500549bSAnthony Wilson  *
2049d500549bSAnthony Wilson  * @param sensorsAsyncResp Pointer to object holding response data.
2050d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
2051d500549bSAnthony Wilson  * @param callback Callback to invoke when inventory items have been obtained.
2052d500549bSAnthony Wilson  */
2053d500549bSAnthony Wilson template <typename Callback>
2054d500549bSAnthony Wilson void getInventoryLeds(
2055d500549bSAnthony Wilson     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2056d500549bSAnthony Wilson     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2057d500549bSAnthony Wilson     Callback&& callback)
2058d500549bSAnthony Wilson {
2059d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLeds enter";
2060d500549bSAnthony Wilson 
2061d500549bSAnthony Wilson     const std::string path = "/xyz/openbmc_project";
2062d500549bSAnthony Wilson     const std::array<std::string, 1> interfaces = {
2063d500549bSAnthony Wilson         "xyz.openbmc_project.Led.Physical"};
2064d500549bSAnthony Wilson 
2065d500549bSAnthony Wilson     // Response handler for parsing output from GetSubTree
2066002d39b4SEd Tanous     auto respHandler =
2067002d39b4SEd Tanous         [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
2068002d39b4SEd Tanous          inventoryItems](
2069b9d36b47SEd Tanous             const boost::system::error_code ec,
2070002d39b4SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) {
2071d500549bSAnthony Wilson         BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler enter";
2072d500549bSAnthony Wilson         if (ec)
2073d500549bSAnthony Wilson         {
20748d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
2075d500549bSAnthony Wilson             BMCWEB_LOG_ERROR << "getInventoryLeds respHandler DBus error "
2076d500549bSAnthony Wilson                              << ec;
2077d500549bSAnthony Wilson             return;
2078d500549bSAnthony Wilson         }
2079d500549bSAnthony Wilson 
2080d500549bSAnthony Wilson         // Build map of LED object paths to connections
2081d500549bSAnthony Wilson         std::shared_ptr<boost::container::flat_map<std::string, std::string>>
2082d500549bSAnthony Wilson             ledConnections = std::make_shared<
2083d500549bSAnthony Wilson                 boost::container::flat_map<std::string, std::string>>();
2084d500549bSAnthony Wilson 
2085d500549bSAnthony Wilson         // Loop through objects from GetSubTree
2086d500549bSAnthony Wilson         for (const std::pair<
2087d500549bSAnthony Wilson                  std::string,
2088d500549bSAnthony Wilson                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
2089d500549bSAnthony Wilson                  object : subtree)
2090d500549bSAnthony Wilson         {
2091d500549bSAnthony Wilson             // Check if object path is LED for one of the specified inventory
2092d500549bSAnthony Wilson             // items
2093d500549bSAnthony Wilson             const std::string& ledPath = object.first;
2094d500549bSAnthony Wilson             if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr)
2095d500549bSAnthony Wilson             {
2096d500549bSAnthony Wilson                 // Add mapping from ledPath to connection
2097d500549bSAnthony Wilson                 const std::string& connection = object.second.begin()->first;
2098d500549bSAnthony Wilson                 (*ledConnections)[ledPath] = connection;
2099d500549bSAnthony Wilson                 BMCWEB_LOG_DEBUG << "Added mapping " << ledPath << " -> "
2100d500549bSAnthony Wilson                                  << connection;
2101d500549bSAnthony Wilson             }
2102d500549bSAnthony Wilson         }
2103d500549bSAnthony Wilson 
2104d500549bSAnthony Wilson         getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections,
2105d500549bSAnthony Wilson                             std::move(callback));
2106d500549bSAnthony Wilson         BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler exit";
2107d500549bSAnthony Wilson     };
2108d500549bSAnthony Wilson     // Make call to ObjectMapper to find all inventory items
2109d500549bSAnthony Wilson     crow::connections::systemBus->async_method_call(
2110d500549bSAnthony Wilson         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
2111d500549bSAnthony Wilson         "/xyz/openbmc_project/object_mapper",
2112d500549bSAnthony Wilson         "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
2113d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLeds exit";
2114d500549bSAnthony Wilson }
2115d500549bSAnthony Wilson 
2116d500549bSAnthony Wilson /**
211742cbe538SGunnar Mills  * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
211842cbe538SGunnar Mills  *
211942cbe538SGunnar Mills  * Uses the specified connections (services) (currently assumes just one) to
212042cbe538SGunnar Mills  * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
212142cbe538SGunnar Mills  * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
212242cbe538SGunnar Mills  *
212342cbe538SGunnar Mills  * This data is later used to provide sensor property values in the JSON
212442cbe538SGunnar Mills  * response.
212542cbe538SGunnar Mills  *
212642cbe538SGunnar Mills  * Finds the Power Supply Attributes data asynchronously.  Invokes callback
212742cbe538SGunnar Mills  * when data has been obtained.
212842cbe538SGunnar Mills  *
212942cbe538SGunnar Mills  * The callback must have the following signature:
213042cbe538SGunnar Mills  *   @code
213142cbe538SGunnar Mills  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
213242cbe538SGunnar Mills  *   @endcode
213342cbe538SGunnar Mills  *
213442cbe538SGunnar Mills  * @param sensorsAsyncResp Pointer to object holding response data.
213542cbe538SGunnar Mills  * @param inventoryItems D-Bus inventory items associated with sensors.
213642cbe538SGunnar Mills  * @param psAttributesConnections Connections that provide data for the Power
213742cbe538SGunnar Mills  *        Supply Attributes
213842cbe538SGunnar Mills  * @param callback Callback to invoke when data has been obtained.
213942cbe538SGunnar Mills  */
214042cbe538SGunnar Mills template <typename Callback>
214142cbe538SGunnar Mills void getPowerSupplyAttributesData(
2142b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
214342cbe538SGunnar Mills     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
214442cbe538SGunnar Mills     const boost::container::flat_map<std::string, std::string>&
214542cbe538SGunnar Mills         psAttributesConnections,
214642cbe538SGunnar Mills     Callback&& callback)
214742cbe538SGunnar Mills {
214842cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData enter";
214942cbe538SGunnar Mills 
215042cbe538SGunnar Mills     if (psAttributesConnections.empty())
215142cbe538SGunnar Mills     {
215242cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "Can't find PowerSupplyAttributes, no connections!";
215342cbe538SGunnar Mills         callback(inventoryItems);
215442cbe538SGunnar Mills         return;
215542cbe538SGunnar Mills     }
215642cbe538SGunnar Mills 
215742cbe538SGunnar Mills     // Assuming just one connection (service) for now
215842cbe538SGunnar Mills     auto it = psAttributesConnections.nth(0);
215942cbe538SGunnar Mills 
216042cbe538SGunnar Mills     const std::string& psAttributesPath = (*it).first;
216142cbe538SGunnar Mills     const std::string& psAttributesConnection = (*it).second;
216242cbe538SGunnar Mills 
216342cbe538SGunnar Mills     // Response handler for Get DeratingFactor property
2164002d39b4SEd Tanous     auto respHandler =
2165002d39b4SEd Tanous         [sensorsAsyncResp, inventoryItems,
2166f94c4ecfSEd Tanous          callback{std::forward<Callback>(callback)}](
2167002d39b4SEd Tanous             const boost::system::error_code ec, const uint32_t value) {
216842cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler enter";
216942cbe538SGunnar Mills         if (ec)
217042cbe538SGunnar Mills         {
217142cbe538SGunnar Mills             BMCWEB_LOG_ERROR
217242cbe538SGunnar Mills                 << "getPowerSupplyAttributesData respHandler DBus error " << ec;
21738d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
217442cbe538SGunnar Mills             return;
217542cbe538SGunnar Mills         }
217642cbe538SGunnar Mills 
21771e1e598dSJonathan Doman         BMCWEB_LOG_DEBUG << "PS EfficiencyPercent value: " << value;
217842cbe538SGunnar Mills         // Store value in Power Supply Inventory Items
217942cbe538SGunnar Mills         for (InventoryItem& inventoryItem : *inventoryItems)
218042cbe538SGunnar Mills         {
218155f79e6fSEd Tanous             if (inventoryItem.isPowerSupply)
218242cbe538SGunnar Mills             {
218342cbe538SGunnar Mills                 inventoryItem.powerSupplyEfficiencyPercent =
21841e1e598dSJonathan Doman                     static_cast<int>(value);
218542cbe538SGunnar Mills             }
218642cbe538SGunnar Mills         }
218742cbe538SGunnar Mills 
218842cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler exit";
218942cbe538SGunnar Mills         callback(inventoryItems);
219042cbe538SGunnar Mills     };
219142cbe538SGunnar Mills 
219242cbe538SGunnar Mills     // Get the DeratingFactor property for the PowerSupplyAttributes
219342cbe538SGunnar Mills     // Currently only property on the interface/only one we care about
21941e1e598dSJonathan Doman     sdbusplus::asio::getProperty<uint32_t>(
21951e1e598dSJonathan Doman         *crow::connections::systemBus, psAttributesConnection, psAttributesPath,
21961e1e598dSJonathan Doman         "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor",
21971e1e598dSJonathan Doman         std::move(respHandler));
219842cbe538SGunnar Mills 
219942cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData exit";
220042cbe538SGunnar Mills }
220142cbe538SGunnar Mills 
220242cbe538SGunnar Mills /**
220342cbe538SGunnar Mills  * @brief Gets the Power Supply Attributes such as EfficiencyPercent
220442cbe538SGunnar Mills  *
220542cbe538SGunnar Mills  * Gets the D-Bus connection (service) that provides Power Supply Attributes
220642cbe538SGunnar Mills  * data. Then gets the Power Supply Attributes data from the connection
220742cbe538SGunnar Mills  * (currently just assumes 1 connection) and stores the data in the inventory
220842cbe538SGunnar Mills  * item.
220942cbe538SGunnar Mills  *
221042cbe538SGunnar Mills  * This data is later used to provide sensor property values in the JSON
221142cbe538SGunnar Mills  * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
221242cbe538SGunnar Mills  *
221342cbe538SGunnar Mills  * Finds the Power Supply Attributes data asynchronously. Invokes callback
221442cbe538SGunnar Mills  * when information has been obtained.
221542cbe538SGunnar Mills  *
221642cbe538SGunnar Mills  * The callback must have the following signature:
221742cbe538SGunnar Mills  *   @code
221842cbe538SGunnar Mills  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
221942cbe538SGunnar Mills  *   @endcode
222042cbe538SGunnar Mills  *
222142cbe538SGunnar Mills  * @param sensorsAsyncResp Pointer to object holding response data.
222242cbe538SGunnar Mills  * @param inventoryItems D-Bus inventory items associated with sensors.
222342cbe538SGunnar Mills  * @param callback Callback to invoke when data has been obtained.
222442cbe538SGunnar Mills  */
222542cbe538SGunnar Mills template <typename Callback>
222642cbe538SGunnar Mills void getPowerSupplyAttributes(
222742cbe538SGunnar Mills     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
222842cbe538SGunnar Mills     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
222942cbe538SGunnar Mills     Callback&& callback)
223042cbe538SGunnar Mills {
223142cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes enter";
223242cbe538SGunnar Mills 
223342cbe538SGunnar Mills     // Only need the power supply attributes when the Power Schema
2234a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode != sensors::node::power)
223542cbe538SGunnar Mills     {
223642cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit since not Power";
223742cbe538SGunnar Mills         callback(inventoryItems);
223842cbe538SGunnar Mills         return;
223942cbe538SGunnar Mills     }
224042cbe538SGunnar Mills 
224142cbe538SGunnar Mills     const std::array<std::string, 1> interfaces = {
224242cbe538SGunnar Mills         "xyz.openbmc_project.Control.PowerSupplyAttributes"};
224342cbe538SGunnar Mills 
224442cbe538SGunnar Mills     // Response handler for parsing output from GetSubTree
2245b9d36b47SEd Tanous     auto respHandler =
2246b9d36b47SEd Tanous         [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
2247b9d36b47SEd Tanous          inventoryItems](
2248b9d36b47SEd Tanous             const boost::system::error_code ec,
2249b9d36b47SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) {
225042cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler enter";
225142cbe538SGunnar Mills         if (ec)
225242cbe538SGunnar Mills         {
22538d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
225442cbe538SGunnar Mills             BMCWEB_LOG_ERROR
225542cbe538SGunnar Mills                 << "getPowerSupplyAttributes respHandler DBus error " << ec;
225642cbe538SGunnar Mills             return;
225742cbe538SGunnar Mills         }
225826f6976fSEd Tanous         if (subtree.empty())
225942cbe538SGunnar Mills         {
226042cbe538SGunnar Mills             BMCWEB_LOG_DEBUG << "Can't find Power Supply Attributes!";
226142cbe538SGunnar Mills             callback(inventoryItems);
226242cbe538SGunnar Mills             return;
226342cbe538SGunnar Mills         }
226442cbe538SGunnar Mills 
226542cbe538SGunnar Mills         // Currently we only support 1 power supply attribute, use this for
226642cbe538SGunnar Mills         // all the power supplies. Build map of object path to connection.
226742cbe538SGunnar Mills         // Assume just 1 connection and 1 path for now.
226842cbe538SGunnar Mills         boost::container::flat_map<std::string, std::string>
226942cbe538SGunnar Mills             psAttributesConnections;
227042cbe538SGunnar Mills 
227142cbe538SGunnar Mills         if (subtree[0].first.empty() || subtree[0].second.empty())
227242cbe538SGunnar Mills         {
227342cbe538SGunnar Mills             BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
227442cbe538SGunnar Mills             callback(inventoryItems);
227542cbe538SGunnar Mills             return;
227642cbe538SGunnar Mills         }
227742cbe538SGunnar Mills 
227842cbe538SGunnar Mills         const std::string& psAttributesPath = subtree[0].first;
227942cbe538SGunnar Mills         const std::string& connection = subtree[0].second.begin()->first;
228042cbe538SGunnar Mills 
228142cbe538SGunnar Mills         if (connection.empty())
228242cbe538SGunnar Mills         {
228342cbe538SGunnar Mills             BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
228442cbe538SGunnar Mills             callback(inventoryItems);
228542cbe538SGunnar Mills             return;
228642cbe538SGunnar Mills         }
228742cbe538SGunnar Mills 
228842cbe538SGunnar Mills         psAttributesConnections[psAttributesPath] = connection;
228942cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "Added mapping " << psAttributesPath << " -> "
229042cbe538SGunnar Mills                          << connection;
229142cbe538SGunnar Mills 
229242cbe538SGunnar Mills         getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
229342cbe538SGunnar Mills                                      psAttributesConnections,
229442cbe538SGunnar Mills                                      std::move(callback));
229542cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler exit";
229642cbe538SGunnar Mills     };
229742cbe538SGunnar Mills     // Make call to ObjectMapper to find the PowerSupplyAttributes service
229842cbe538SGunnar Mills     crow::connections::systemBus->async_method_call(
229942cbe538SGunnar Mills         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
230042cbe538SGunnar Mills         "/xyz/openbmc_project/object_mapper",
230142cbe538SGunnar Mills         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
230242cbe538SGunnar Mills         "/xyz/openbmc_project", 0, interfaces);
230342cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit";
230442cbe538SGunnar Mills }
230542cbe538SGunnar Mills 
230642cbe538SGunnar Mills /**
2307adc4f0dbSShawn McCarney  * @brief Gets inventory items associated with sensors.
23088fb49dd6SShawn McCarney  *
23098fb49dd6SShawn McCarney  * Finds the inventory items that are associated with the specified sensors.
2310adc4f0dbSShawn McCarney  * Then gets D-Bus data for the inventory items, such as presence and VPD.
23118fb49dd6SShawn McCarney  *
2312adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
2313adc4f0dbSShawn McCarney  * response.
23148fb49dd6SShawn McCarney  *
2315adc4f0dbSShawn McCarney  * Finds the inventory items asynchronously.  Invokes callback when the
2316adc4f0dbSShawn McCarney  * inventory items have been obtained.
2317adc4f0dbSShawn McCarney  *
2318adc4f0dbSShawn McCarney  * The callback must have the following signature:
2319adc4f0dbSShawn McCarney  *   @code
2320adc4f0dbSShawn McCarney  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2321adc4f0dbSShawn McCarney  *   @endcode
23228fb49dd6SShawn McCarney  *
23238fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
23248fb49dd6SShawn McCarney  * @param sensorNames All sensors within the current chassis.
23258fb49dd6SShawn McCarney  * @param objectMgrPaths Mappings from connection name to DBus object path that
23268fb49dd6SShawn McCarney  * implements ObjectManager.
2327adc4f0dbSShawn McCarney  * @param callback Callback to invoke when inventory items have been obtained.
23288fb49dd6SShawn McCarney  */
2329adc4f0dbSShawn McCarney template <typename Callback>
2330adc4f0dbSShawn McCarney static void getInventoryItems(
23318fb49dd6SShawn McCarney     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
23328fb49dd6SShawn McCarney     const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
23338fb49dd6SShawn McCarney     std::shared_ptr<boost::container::flat_map<std::string, std::string>>
2334adc4f0dbSShawn McCarney         objectMgrPaths,
2335adc4f0dbSShawn McCarney     Callback&& callback)
23368fb49dd6SShawn McCarney {
2337adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItems enter";
2338adc4f0dbSShawn McCarney     auto getInventoryItemAssociationsCb =
2339f94c4ecfSEd Tanous         [sensorsAsyncResp, objectMgrPaths,
2340f94c4ecfSEd Tanous          callback{std::forward<Callback>(callback)}](
2341adc4f0dbSShawn McCarney             std::shared_ptr<std::vector<InventoryItem>> inventoryItems) {
2342adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb enter";
23438fb49dd6SShawn McCarney         auto getInventoryItemsConnectionsCb =
2344adc4f0dbSShawn McCarney             [sensorsAsyncResp, inventoryItems, objectMgrPaths,
2345f94c4ecfSEd Tanous              callback{std::forward<const Callback>(callback)}](
23468fb49dd6SShawn McCarney                 std::shared_ptr<boost::container::flat_set<std::string>>
23478fb49dd6SShawn McCarney                     invConnections) {
23488fb49dd6SShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb enter";
2349002d39b4SEd Tanous             auto getInventoryItemsDataCb = [sensorsAsyncResp, inventoryItems,
2350d500549bSAnthony Wilson                                             callback{std::move(callback)}]() {
2351d500549bSAnthony Wilson                 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb enter";
235242cbe538SGunnar Mills 
2353002d39b4SEd Tanous                 auto getInventoryLedsCb = [sensorsAsyncResp, inventoryItems,
2354002d39b4SEd Tanous                                            callback{std::move(callback)}]() {
235542cbe538SGunnar Mills                     BMCWEB_LOG_DEBUG << "getInventoryLedsCb enter";
235642cbe538SGunnar Mills                     // Find Power Supply Attributes and get the data
2357002d39b4SEd Tanous                     getPowerSupplyAttributes(sensorsAsyncResp, inventoryItems,
235842cbe538SGunnar Mills                                              std::move(callback));
235942cbe538SGunnar Mills                     BMCWEB_LOG_DEBUG << "getInventoryLedsCb exit";
236042cbe538SGunnar Mills                 };
236142cbe538SGunnar Mills 
2362d500549bSAnthony Wilson                 // Find led connections and get the data
2363d500549bSAnthony Wilson                 getInventoryLeds(sensorsAsyncResp, inventoryItems,
236442cbe538SGunnar Mills                                  std::move(getInventoryLedsCb));
2365d500549bSAnthony Wilson                 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb exit";
2366d500549bSAnthony Wilson             };
23678fb49dd6SShawn McCarney 
2368adc4f0dbSShawn McCarney             // Get inventory item data from connections
2369adc4f0dbSShawn McCarney             getInventoryItemsData(sensorsAsyncResp, inventoryItems,
2370adc4f0dbSShawn McCarney                                   invConnections, objectMgrPaths,
2371d500549bSAnthony Wilson                                   std::move(getInventoryItemsDataCb));
23728fb49dd6SShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb exit";
23738fb49dd6SShawn McCarney         };
23748fb49dd6SShawn McCarney 
2375adc4f0dbSShawn McCarney         // Get connections that provide inventory item data
2376002d39b4SEd Tanous         getInventoryItemsConnections(sensorsAsyncResp, inventoryItems,
23778fb49dd6SShawn McCarney                                      std::move(getInventoryItemsConnectionsCb));
2378adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb exit";
23798fb49dd6SShawn McCarney     };
23808fb49dd6SShawn McCarney 
2381adc4f0dbSShawn McCarney     // Get associations from sensors to inventory items
2382adc4f0dbSShawn McCarney     getInventoryItemAssociations(sensorsAsyncResp, sensorNames, objectMgrPaths,
2383adc4f0dbSShawn McCarney                                  std::move(getInventoryItemAssociationsCb));
2384adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItems exit";
2385adc4f0dbSShawn McCarney }
2386adc4f0dbSShawn McCarney 
2387adc4f0dbSShawn McCarney /**
2388adc4f0dbSShawn McCarney  * @brief Returns JSON PowerSupply object for the specified inventory item.
2389adc4f0dbSShawn McCarney  *
2390adc4f0dbSShawn McCarney  * Searches for a JSON PowerSupply object that matches the specified inventory
2391adc4f0dbSShawn McCarney  * item.  If one is not found, a new PowerSupply object is added to the JSON
2392adc4f0dbSShawn McCarney  * array.
2393adc4f0dbSShawn McCarney  *
2394adc4f0dbSShawn McCarney  * Multiple sensors are often associated with one power supply inventory item.
2395adc4f0dbSShawn McCarney  * As a result, multiple sensor values are stored in one JSON PowerSupply
2396adc4f0dbSShawn McCarney  * object.
2397adc4f0dbSShawn McCarney  *
2398adc4f0dbSShawn McCarney  * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
2399adc4f0dbSShawn McCarney  * @param inventoryItem Inventory item for the power supply.
2400adc4f0dbSShawn McCarney  * @param chassisId Chassis that contains the power supply.
2401adc4f0dbSShawn McCarney  * @return JSON PowerSupply object for the specified inventory item.
2402adc4f0dbSShawn McCarney  */
240323a21a1cSEd Tanous inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
2404adc4f0dbSShawn McCarney                                       const InventoryItem& inventoryItem,
2405adc4f0dbSShawn McCarney                                       const std::string& chassisId)
2406adc4f0dbSShawn McCarney {
2407adc4f0dbSShawn McCarney     // Check if matching PowerSupply object already exists in JSON array
2408adc4f0dbSShawn McCarney     for (nlohmann::json& powerSupply : powerSupplyArray)
2409adc4f0dbSShawn McCarney     {
2410adc4f0dbSShawn McCarney         if (powerSupply["MemberId"] == inventoryItem.name)
2411adc4f0dbSShawn McCarney         {
2412adc4f0dbSShawn McCarney             return powerSupply;
2413adc4f0dbSShawn McCarney         }
2414adc4f0dbSShawn McCarney     }
2415adc4f0dbSShawn McCarney 
2416adc4f0dbSShawn McCarney     // Add new PowerSupply object to JSON array
2417adc4f0dbSShawn McCarney     powerSupplyArray.push_back({});
2418adc4f0dbSShawn McCarney     nlohmann::json& powerSupply = powerSupplyArray.back();
2419adc4f0dbSShawn McCarney     powerSupply["@odata.id"] =
2420adc4f0dbSShawn McCarney         "/redfish/v1/Chassis/" + chassisId + "/Power#/PowerSupplies/";
2421adc4f0dbSShawn McCarney     powerSupply["MemberId"] = inventoryItem.name;
2422adc4f0dbSShawn McCarney     powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " ");
2423adc4f0dbSShawn McCarney     powerSupply["Manufacturer"] = inventoryItem.manufacturer;
2424adc4f0dbSShawn McCarney     powerSupply["Model"] = inventoryItem.model;
2425adc4f0dbSShawn McCarney     powerSupply["PartNumber"] = inventoryItem.partNumber;
2426adc4f0dbSShawn McCarney     powerSupply["SerialNumber"] = inventoryItem.serialNumber;
2427d500549bSAnthony Wilson     setLedState(powerSupply, &inventoryItem);
2428adc4f0dbSShawn McCarney 
242942cbe538SGunnar Mills     if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
243042cbe538SGunnar Mills     {
243142cbe538SGunnar Mills         powerSupply["EfficiencyPercent"] =
243242cbe538SGunnar Mills             inventoryItem.powerSupplyEfficiencyPercent;
243342cbe538SGunnar Mills     }
243442cbe538SGunnar Mills 
243542cbe538SGunnar Mills     powerSupply["Status"]["State"] = getState(&inventoryItem);
2436adc4f0dbSShawn McCarney     const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
2437adc4f0dbSShawn McCarney     powerSupply["Status"]["Health"] = health;
2438adc4f0dbSShawn McCarney 
2439adc4f0dbSShawn McCarney     return powerSupply;
24408fb49dd6SShawn McCarney }
24418fb49dd6SShawn McCarney 
24428fb49dd6SShawn McCarney /**
2443de629b6eSShawn McCarney  * @brief Gets the values of the specified sensors.
2444de629b6eSShawn McCarney  *
2445de629b6eSShawn McCarney  * Stores the results as JSON in the SensorsAsyncResp.
2446de629b6eSShawn McCarney  *
2447de629b6eSShawn McCarney  * Gets the sensor values asynchronously.  Stores the results later when the
2448de629b6eSShawn McCarney  * information has been obtained.
2449de629b6eSShawn McCarney  *
2450adc4f0dbSShawn McCarney  * The sensorNames set contains all requested sensors for the current chassis.
2451de629b6eSShawn McCarney  *
2452de629b6eSShawn McCarney  * To minimize the number of DBus calls, the DBus method
2453de629b6eSShawn McCarney  * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
2454de629b6eSShawn McCarney  * values of all sensors provided by a connection (service).
2455de629b6eSShawn McCarney  *
2456de629b6eSShawn McCarney  * The connections set contains all the connections that provide sensor values.
2457de629b6eSShawn McCarney  *
2458de629b6eSShawn McCarney  * The objectMgrPaths map contains mappings from a connection name to the
2459de629b6eSShawn McCarney  * corresponding DBus object path that implements ObjectManager.
2460de629b6eSShawn McCarney  *
2461adc4f0dbSShawn McCarney  * The InventoryItem vector contains D-Bus inventory items associated with the
2462adc4f0dbSShawn McCarney  * sensors.  Inventory item data is needed for some Redfish sensor properties.
2463adc4f0dbSShawn McCarney  *
2464de629b6eSShawn McCarney  * @param SensorsAsyncResp Pointer to object holding response data.
2465adc4f0dbSShawn McCarney  * @param sensorNames All requested sensors within the current chassis.
2466de629b6eSShawn McCarney  * @param connections Connections that provide sensor values.
2467de629b6eSShawn McCarney  * @param objectMgrPaths Mappings from connection name to DBus object path that
2468de629b6eSShawn McCarney  * implements ObjectManager.
2469adc4f0dbSShawn McCarney  * @param inventoryItems Inventory items associated with the sensors.
2470de629b6eSShawn McCarney  */
247123a21a1cSEd Tanous inline void getSensorData(
247281ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2473b5a76932SEd Tanous     const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames,
2474de629b6eSShawn McCarney     const boost::container::flat_set<std::string>& connections,
2475b5a76932SEd Tanous     const std::shared_ptr<boost::container::flat_map<std::string, std::string>>&
2476adc4f0dbSShawn McCarney         objectMgrPaths,
2477b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems)
2478de629b6eSShawn McCarney {
2479de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getSensorData enter";
2480de629b6eSShawn McCarney     // Get managed objects from all services exposing sensors
2481de629b6eSShawn McCarney     for (const std::string& connection : connections)
2482de629b6eSShawn McCarney     {
2483de629b6eSShawn McCarney         // Response handler to process managed objects
2484002d39b4SEd Tanous         auto getManagedObjectsCb =
2485002d39b4SEd Tanous             [sensorsAsyncResp, sensorNames,
2486002d39b4SEd Tanous              inventoryItems](const boost::system::error_code ec,
2487711ac7a9SEd Tanous                              dbus::utility::ManagedObjectType& resp) {
2488de629b6eSShawn McCarney             BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter";
2489de629b6eSShawn McCarney             if (ec)
2490de629b6eSShawn McCarney             {
2491de629b6eSShawn McCarney                 BMCWEB_LOG_ERROR << "getManagedObjectsCb DBUS error: " << ec;
24928d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
2493de629b6eSShawn McCarney                 return;
2494de629b6eSShawn McCarney             }
2495de629b6eSShawn McCarney             // Go through all objects and update response with sensor data
2496de629b6eSShawn McCarney             for (const auto& objDictEntry : resp)
2497de629b6eSShawn McCarney             {
2498de629b6eSShawn McCarney                 const std::string& objPath =
2499de629b6eSShawn McCarney                     static_cast<const std::string&>(objDictEntry.first);
2500de629b6eSShawn McCarney                 BMCWEB_LOG_DEBUG << "getManagedObjectsCb parsing object "
2501de629b6eSShawn McCarney                                  << objPath;
2502de629b6eSShawn McCarney 
2503de629b6eSShawn McCarney                 std::vector<std::string> split;
2504de629b6eSShawn McCarney                 // Reserve space for
2505de629b6eSShawn McCarney                 // /xyz/openbmc_project/sensors/<name>/<subname>
2506de629b6eSShawn McCarney                 split.reserve(6);
2507de629b6eSShawn McCarney                 boost::algorithm::split(split, objPath, boost::is_any_of("/"));
2508de629b6eSShawn McCarney                 if (split.size() < 6)
2509de629b6eSShawn McCarney                 {
2510de629b6eSShawn McCarney                     BMCWEB_LOG_ERROR << "Got path that isn't long enough "
2511de629b6eSShawn McCarney                                      << objPath;
2512de629b6eSShawn McCarney                     continue;
2513de629b6eSShawn McCarney                 }
2514de629b6eSShawn McCarney                 // These indexes aren't intuitive, as boost::split puts an empty
2515de629b6eSShawn McCarney                 // string at the beginning
2516de629b6eSShawn McCarney                 const std::string& sensorType = split[4];
2517de629b6eSShawn McCarney                 const std::string& sensorName = split[5];
2518de629b6eSShawn McCarney                 BMCWEB_LOG_DEBUG << "sensorName " << sensorName
2519de629b6eSShawn McCarney                                  << " sensorType " << sensorType;
252049c53ac9SJohnathan Mantey                 if (sensorNames->find(objPath) == sensorNames->end())
2521de629b6eSShawn McCarney                 {
2522accdbb2cSAndrew Geissler                     BMCWEB_LOG_DEBUG << sensorName << " not in sensor list ";
2523de629b6eSShawn McCarney                     continue;
2524de629b6eSShawn McCarney                 }
2525de629b6eSShawn McCarney 
2526adc4f0dbSShawn McCarney                 // Find inventory item (if any) associated with sensor
2527adc4f0dbSShawn McCarney                 InventoryItem* inventoryItem =
2528adc4f0dbSShawn McCarney                     findInventoryItemForSensor(inventoryItems, objPath);
2529adc4f0dbSShawn McCarney 
253095a3ecadSAnthony Wilson                 const std::string& sensorSchema =
253181ce609eSEd Tanous                     sensorsAsyncResp->chassisSubNode;
253295a3ecadSAnthony Wilson 
253395a3ecadSAnthony Wilson                 nlohmann::json* sensorJson = nullptr;
253495a3ecadSAnthony Wilson 
2535928fefb9SNan Zhou                 if (sensorSchema == sensors::node::sensors &&
2536928fefb9SNan Zhou                     !sensorsAsyncResp->efficientExpand)
253795a3ecadSAnthony Wilson                 {
25388d1b46d7Szhanghch05                     sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
253981ce609eSEd Tanous                         "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId +
254081ce609eSEd Tanous                         "/" + sensorsAsyncResp->chassisSubNode + "/" +
254195a3ecadSAnthony Wilson                         sensorName;
25428d1b46d7Szhanghch05                     sensorJson = &(sensorsAsyncResp->asyncResp->res.jsonValue);
254395a3ecadSAnthony Wilson                 }
254495a3ecadSAnthony Wilson                 else
254595a3ecadSAnthony Wilson                 {
2546271584abSEd Tanous                     std::string fieldName;
2547928fefb9SNan Zhou                     if (sensorsAsyncResp->efficientExpand)
2548928fefb9SNan Zhou                     {
2549928fefb9SNan Zhou                         fieldName = "Members";
2550928fefb9SNan Zhou                     }
2551928fefb9SNan Zhou                     else if (sensorType == "temperature")
2552de629b6eSShawn McCarney                     {
2553de629b6eSShawn McCarney                         fieldName = "Temperatures";
2554de629b6eSShawn McCarney                     }
2555de629b6eSShawn McCarney                     else if (sensorType == "fan" || sensorType == "fan_tach" ||
2556de629b6eSShawn McCarney                              sensorType == "fan_pwm")
2557de629b6eSShawn McCarney                     {
2558de629b6eSShawn McCarney                         fieldName = "Fans";
2559de629b6eSShawn McCarney                     }
2560de629b6eSShawn McCarney                     else if (sensorType == "voltage")
2561de629b6eSShawn McCarney                     {
2562de629b6eSShawn McCarney                         fieldName = "Voltages";
2563de629b6eSShawn McCarney                     }
2564de629b6eSShawn McCarney                     else if (sensorType == "power")
2565de629b6eSShawn McCarney                     {
256655f79e6fSEd Tanous                         if (sensorName == "total_power")
2567028f7ebcSEddie James                         {
2568028f7ebcSEddie James                             fieldName = "PowerControl";
2569028f7ebcSEddie James                         }
2570adc4f0dbSShawn McCarney                         else if ((inventoryItem != nullptr) &&
2571adc4f0dbSShawn McCarney                                  (inventoryItem->isPowerSupply))
2572028f7ebcSEddie James                         {
2573de629b6eSShawn McCarney                             fieldName = "PowerSupplies";
2574de629b6eSShawn McCarney                         }
2575adc4f0dbSShawn McCarney                         else
2576adc4f0dbSShawn McCarney                         {
2577adc4f0dbSShawn McCarney                             // Other power sensors are in SensorCollection
2578adc4f0dbSShawn McCarney                             continue;
2579adc4f0dbSShawn McCarney                         }
2580028f7ebcSEddie James                     }
2581de629b6eSShawn McCarney                     else
2582de629b6eSShawn McCarney                     {
2583de629b6eSShawn McCarney                         BMCWEB_LOG_ERROR << "Unsure how to handle sensorType "
2584de629b6eSShawn McCarney                                          << sensorType;
2585de629b6eSShawn McCarney                         continue;
2586de629b6eSShawn McCarney                     }
2587de629b6eSShawn McCarney 
2588de629b6eSShawn McCarney                     nlohmann::json& tempArray =
25898d1b46d7Szhanghch05                         sensorsAsyncResp->asyncResp->res.jsonValue[fieldName];
2590adc4f0dbSShawn McCarney                     if (fieldName == "PowerControl")
259149c53ac9SJohnathan Mantey                     {
2592adc4f0dbSShawn McCarney                         if (tempArray.empty())
25937ab06f49SGunnar Mills                         {
259495a3ecadSAnthony Wilson                             // Put multiple "sensors" into a single
259595a3ecadSAnthony Wilson                             // PowerControl. Follows MemberId naming and
259695a3ecadSAnthony Wilson                             // naming in power.hpp.
25971476687dSEd Tanous                             nlohmann::json::object_t power;
25981476687dSEd Tanous                             power["@odata.id"] =
2599adc4f0dbSShawn McCarney                                 "/redfish/v1/Chassis/" +
260081ce609eSEd Tanous                                 sensorsAsyncResp->chassisId + "/" +
260181ce609eSEd Tanous                                 sensorsAsyncResp->chassisSubNode + "#/" +
26021476687dSEd Tanous                                 fieldName + "/0";
26031476687dSEd Tanous                             tempArray.push_back(std::move(power));
2604adc4f0dbSShawn McCarney                         }
2605adc4f0dbSShawn McCarney                         sensorJson = &(tempArray.back());
2606adc4f0dbSShawn McCarney                     }
2607adc4f0dbSShawn McCarney                     else if (fieldName == "PowerSupplies")
2608adc4f0dbSShawn McCarney                     {
2609adc4f0dbSShawn McCarney                         if (inventoryItem != nullptr)
2610adc4f0dbSShawn McCarney                         {
2611adc4f0dbSShawn McCarney                             sensorJson =
2612adc4f0dbSShawn McCarney                                 &(getPowerSupply(tempArray, *inventoryItem,
261381ce609eSEd Tanous                                                  sensorsAsyncResp->chassisId));
2614adc4f0dbSShawn McCarney                         }
261549c53ac9SJohnathan Mantey                     }
2616928fefb9SNan Zhou                     else if (fieldName == "Members")
2617928fefb9SNan Zhou                     {
26181476687dSEd Tanous                         nlohmann::json::object_t member;
26191476687dSEd Tanous                         member["@odata.id"] =
2620928fefb9SNan Zhou                             "/redfish/v1/Chassis/" +
2621928fefb9SNan Zhou                             sensorsAsyncResp->chassisId + "/" +
26221476687dSEd Tanous                             sensorsAsyncResp->chassisSubNode + "/" + sensorName;
26231476687dSEd Tanous                         tempArray.push_back(std::move(member));
2624928fefb9SNan Zhou                         sensorJson = &(tempArray.back());
2625928fefb9SNan Zhou                     }
262649c53ac9SJohnathan Mantey                     else
262749c53ac9SJohnathan Mantey                     {
26281476687dSEd Tanous                         nlohmann::json::object_t member;
26291476687dSEd Tanous                         member["@odata.id"] = "/redfish/v1/Chassis/" +
26301476687dSEd Tanous                                               sensorsAsyncResp->chassisId +
26311476687dSEd Tanous                                               "/" +
26321476687dSEd Tanous                                               sensorsAsyncResp->chassisSubNode +
26331476687dSEd Tanous                                               "#/" + fieldName + "/";
26341476687dSEd Tanous                         tempArray.push_back(std::move(member));
2635adc4f0dbSShawn McCarney                         sensorJson = &(tempArray.back());
263649c53ac9SJohnathan Mantey                     }
263795a3ecadSAnthony Wilson                 }
2638de629b6eSShawn McCarney 
2639adc4f0dbSShawn McCarney                 if (sensorJson != nullptr)
2640adc4f0dbSShawn McCarney                 {
2641a0ec28b6SAdrian Ambrożewicz                     objectInterfacesToJson(
264281ce609eSEd Tanous                         sensorName, sensorType, sensorsAsyncResp,
2643a0ec28b6SAdrian Ambrożewicz                         objDictEntry.second, *sensorJson, inventoryItem);
2644adc4f0dbSShawn McCarney                 }
2645de629b6eSShawn McCarney             }
264681ce609eSEd Tanous             if (sensorsAsyncResp.use_count() == 1)
264749c53ac9SJohnathan Mantey             {
264881ce609eSEd Tanous                 sortJSONResponse(sensorsAsyncResp);
2649928fefb9SNan Zhou                 if (sensorsAsyncResp->chassisSubNode ==
2650928fefb9SNan Zhou                         sensors::node::sensors &&
2651928fefb9SNan Zhou                     sensorsAsyncResp->efficientExpand)
2652928fefb9SNan Zhou                 {
2653928fefb9SNan Zhou                     sensorsAsyncResp->asyncResp->res
2654928fefb9SNan Zhou                         .jsonValue["Members@odata.count"] =
2655928fefb9SNan Zhou                         sensorsAsyncResp->asyncResp->res.jsonValue["Members"]
2656928fefb9SNan Zhou                             .size();
2657928fefb9SNan Zhou                 }
2658928fefb9SNan Zhou                 else if (sensorsAsyncResp->chassisSubNode ==
2659928fefb9SNan Zhou                          sensors::node::thermal)
26608bd25ccdSJames Feist                 {
266181ce609eSEd Tanous                     populateFanRedundancy(sensorsAsyncResp);
26628bd25ccdSJames Feist                 }
266349c53ac9SJohnathan Mantey             }
2664de629b6eSShawn McCarney             BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit";
2665de629b6eSShawn McCarney         };
2666de629b6eSShawn McCarney 
2667de629b6eSShawn McCarney         // Find DBus object path that implements ObjectManager for the current
2668de629b6eSShawn McCarney         // connection.  If no mapping found, default to "/".
26698fb49dd6SShawn McCarney         auto iter = objectMgrPaths->find(connection);
2670de629b6eSShawn McCarney         const std::string& objectMgrPath =
26718fb49dd6SShawn McCarney             (iter != objectMgrPaths->end()) ? iter->second : "/";
2672de629b6eSShawn McCarney         BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
2673de629b6eSShawn McCarney                          << objectMgrPath;
2674de629b6eSShawn McCarney 
2675de629b6eSShawn McCarney         crow::connections::systemBus->async_method_call(
2676de629b6eSShawn McCarney             getManagedObjectsCb, connection, objectMgrPath,
2677de629b6eSShawn McCarney             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
267823a21a1cSEd Tanous     }
2679de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getSensorData exit";
2680de629b6eSShawn McCarney }
2681de629b6eSShawn McCarney 
268223a21a1cSEd Tanous inline void processSensorList(
268381ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2684b5a76932SEd Tanous     const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames)
26851abe55efSEd Tanous {
268695a3ecadSAnthony Wilson     auto getConnectionCb =
268781ce609eSEd Tanous         [sensorsAsyncResp, sensorNames](
268895a3ecadSAnthony Wilson             const boost::container::flat_set<std::string>& connections) {
268955c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "getConnectionCb enter";
2690de629b6eSShawn McCarney         auto getObjectManagerPathsCb =
269181ce609eSEd Tanous             [sensorsAsyncResp, sensorNames,
2692b5a76932SEd Tanous              connections](const std::shared_ptr<boost::container::flat_map<
2693b5a76932SEd Tanous                               std::string, std::string>>& objectMgrPaths) {
2694de629b6eSShawn McCarney             BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb enter";
2695adc4f0dbSShawn McCarney             auto getInventoryItemsCb =
2696002d39b4SEd Tanous                 [sensorsAsyncResp, sensorNames, connections, objectMgrPaths](
2697f23b7296SEd Tanous                     const std::shared_ptr<std::vector<InventoryItem>>&
2698adc4f0dbSShawn McCarney                         inventoryItems) {
2699adc4f0dbSShawn McCarney                 BMCWEB_LOG_DEBUG << "getInventoryItemsCb enter";
270049c53ac9SJohnathan Mantey                 // Get sensor data and store results in JSON
2701002d39b4SEd Tanous                 getSensorData(sensorsAsyncResp, sensorNames, connections,
2702002d39b4SEd Tanous                               objectMgrPaths, inventoryItems);
2703adc4f0dbSShawn McCarney                 BMCWEB_LOG_DEBUG << "getInventoryItemsCb exit";
2704adc4f0dbSShawn McCarney             };
2705adc4f0dbSShawn McCarney 
2706adc4f0dbSShawn McCarney             // Get inventory items associated with sensors
2707002d39b4SEd Tanous             getInventoryItems(sensorsAsyncResp, sensorNames, objectMgrPaths,
2708adc4f0dbSShawn McCarney                               std::move(getInventoryItemsCb));
2709adc4f0dbSShawn McCarney 
2710de629b6eSShawn McCarney             BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb exit";
271108777fb0SLewanczyk, Dawid         };
2712de629b6eSShawn McCarney 
271349c53ac9SJohnathan Mantey         // Get mapping from connection names to the DBus object
271449c53ac9SJohnathan Mantey         // paths that implement the ObjectManager interface
271581ce609eSEd Tanous         getObjectManagerPaths(sensorsAsyncResp,
2716de629b6eSShawn McCarney                               std::move(getObjectManagerPathsCb));
271755c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "getConnectionCb exit";
271808777fb0SLewanczyk, Dawid     };
2719de629b6eSShawn McCarney 
2720de629b6eSShawn McCarney     // Get set of connections that provide sensor values
272181ce609eSEd Tanous     getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb));
272295a3ecadSAnthony Wilson }
272395a3ecadSAnthony Wilson 
272495a3ecadSAnthony Wilson /**
272595a3ecadSAnthony Wilson  * @brief Entry point for retrieving sensors data related to requested
272695a3ecadSAnthony Wilson  *        chassis.
272795a3ecadSAnthony Wilson  * @param SensorsAsyncResp   Pointer to object holding response data
272895a3ecadSAnthony Wilson  */
2729b5a76932SEd Tanous inline void
273081ce609eSEd Tanous     getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
273195a3ecadSAnthony Wilson {
273295a3ecadSAnthony Wilson     BMCWEB_LOG_DEBUG << "getChassisData enter";
273395a3ecadSAnthony Wilson     auto getChassisCb =
273481ce609eSEd Tanous         [sensorsAsyncResp](
2735f23b7296SEd Tanous             const std::shared_ptr<boost::container::flat_set<std::string>>&
273695a3ecadSAnthony Wilson                 sensorNames) {
273795a3ecadSAnthony Wilson         BMCWEB_LOG_DEBUG << "getChassisCb enter";
273881ce609eSEd Tanous         processSensorList(sensorsAsyncResp, sensorNames);
273955c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "getChassisCb exit";
274008777fb0SLewanczyk, Dawid     };
2741928fefb9SNan Zhou     // SensorCollection doesn't contain the Redundancy property
2742928fefb9SNan Zhou     if (sensorsAsyncResp->chassisSubNode != sensors::node::sensors)
2743928fefb9SNan Zhou     {
27448d1b46d7Szhanghch05         sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] =
27458d1b46d7Szhanghch05             nlohmann::json::array();
2746928fefb9SNan Zhou     }
274726f03899SShawn McCarney     // Get set of sensors in chassis
274881ce609eSEd Tanous     getChassis(sensorsAsyncResp, std::move(getChassisCb));
274955c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "getChassisData exit";
2750271584abSEd Tanous }
275108777fb0SLewanczyk, Dawid 
2752413961deSRichard Marian Thomaiyar /**
275349c53ac9SJohnathan Mantey  * @brief Find the requested sensorName in the list of all sensors supplied by
275449c53ac9SJohnathan Mantey  * the chassis node
275549c53ac9SJohnathan Mantey  *
275649c53ac9SJohnathan Mantey  * @param sensorName   The sensor name supplied in the PATCH request
275749c53ac9SJohnathan Mantey  * @param sensorsList  The list of sensors managed by the chassis node
275849c53ac9SJohnathan Mantey  * @param sensorsModified  The list of sensors that were found as a result of
275949c53ac9SJohnathan Mantey  *                         repeated calls to this function
276049c53ac9SJohnathan Mantey  */
276123a21a1cSEd Tanous inline bool findSensorNameUsingSensorPath(
27620a86febdSRichard Marian Thomaiyar     std::string_view sensorName,
276349c53ac9SJohnathan Mantey     boost::container::flat_set<std::string>& sensorsList,
276449c53ac9SJohnathan Mantey     boost::container::flat_set<std::string>& sensorsModified)
276549c53ac9SJohnathan Mantey {
276628aa8de5SGeorge Liu     for (auto& chassisSensor : sensorsList)
276749c53ac9SJohnathan Mantey     {
276828aa8de5SGeorge Liu         sdbusplus::message::object_path path(chassisSensor);
2769b00dcc27SEd Tanous         std::string thisSensorName = path.filename();
277028aa8de5SGeorge Liu         if (thisSensorName.empty())
277149c53ac9SJohnathan Mantey         {
277249c53ac9SJohnathan Mantey             continue;
277349c53ac9SJohnathan Mantey         }
277449c53ac9SJohnathan Mantey         if (thisSensorName == sensorName)
277549c53ac9SJohnathan Mantey         {
277649c53ac9SJohnathan Mantey             sensorsModified.emplace(chassisSensor);
277749c53ac9SJohnathan Mantey             return true;
277849c53ac9SJohnathan Mantey         }
277949c53ac9SJohnathan Mantey     }
278049c53ac9SJohnathan Mantey     return false;
278149c53ac9SJohnathan Mantey }
278249c53ac9SJohnathan Mantey 
278349c53ac9SJohnathan Mantey /**
2784413961deSRichard Marian Thomaiyar  * @brief Entry point for overriding sensor values of given sensor
2785413961deSRichard Marian Thomaiyar  *
27868d1b46d7Szhanghch05  * @param sensorAsyncResp   response object
27874bb3dc34SCarol Wang  * @param allCollections   Collections extract from sensors' request patch info
2788413961deSRichard Marian Thomaiyar  * @param chassisSubNode   Chassis Node for which the query has to happen
2789413961deSRichard Marian Thomaiyar  */
279023a21a1cSEd Tanous inline void setSensorsOverride(
2791b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
27924bb3dc34SCarol Wang     std::unordered_map<std::string, std::vector<nlohmann::json>>&
2793397fd61fSjayaprakash Mutyala         allCollections)
2794413961deSRichard Marian Thomaiyar {
279570d1d0aaSjayaprakash Mutyala     BMCWEB_LOG_INFO << "setSensorsOverride for subNode"
27964bb3dc34SCarol Wang                     << sensorAsyncResp->chassisSubNode << "\n";
2797413961deSRichard Marian Thomaiyar 
2798543f4400SEd Tanous     const char* propertyValueName = nullptr;
2799f65af9e8SRichard Marian Thomaiyar     std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
2800413961deSRichard Marian Thomaiyar     std::string memberId;
2801543f4400SEd Tanous     double value = 0.0;
2802f65af9e8SRichard Marian Thomaiyar     for (auto& collectionItems : allCollections)
2803f65af9e8SRichard Marian Thomaiyar     {
2804f65af9e8SRichard Marian Thomaiyar         if (collectionItems.first == "Temperatures")
2805f65af9e8SRichard Marian Thomaiyar         {
2806f65af9e8SRichard Marian Thomaiyar             propertyValueName = "ReadingCelsius";
2807f65af9e8SRichard Marian Thomaiyar         }
2808f65af9e8SRichard Marian Thomaiyar         else if (collectionItems.first == "Fans")
2809f65af9e8SRichard Marian Thomaiyar         {
2810f65af9e8SRichard Marian Thomaiyar             propertyValueName = "Reading";
2811f65af9e8SRichard Marian Thomaiyar         }
2812f65af9e8SRichard Marian Thomaiyar         else
2813f65af9e8SRichard Marian Thomaiyar         {
2814f65af9e8SRichard Marian Thomaiyar             propertyValueName = "ReadingVolts";
2815f65af9e8SRichard Marian Thomaiyar         }
2816f65af9e8SRichard Marian Thomaiyar         for (auto& item : collectionItems.second)
2817f65af9e8SRichard Marian Thomaiyar         {
28188d1b46d7Szhanghch05             if (!json_util::readJson(item, sensorAsyncResp->asyncResp->res,
28198d1b46d7Szhanghch05                                      "MemberId", memberId, propertyValueName,
28208d1b46d7Szhanghch05                                      value))
2821413961deSRichard Marian Thomaiyar             {
2822413961deSRichard Marian Thomaiyar                 return;
2823413961deSRichard Marian Thomaiyar             }
2824f65af9e8SRichard Marian Thomaiyar             overrideMap.emplace(memberId,
2825f65af9e8SRichard Marian Thomaiyar                                 std::make_pair(value, collectionItems.first));
2826f65af9e8SRichard Marian Thomaiyar         }
2827f65af9e8SRichard Marian Thomaiyar     }
28284bb3dc34SCarol Wang 
2829002d39b4SEd Tanous     auto getChassisSensorListCb =
2830002d39b4SEd Tanous         [sensorAsyncResp, overrideMap](
2831002d39b4SEd Tanous             const std::shared_ptr<boost::container::flat_set<std::string>>&
2832002d39b4SEd Tanous                 sensorsList) {
283349c53ac9SJohnathan Mantey         // Match sensor names in the PATCH request to those managed by the
283449c53ac9SJohnathan Mantey         // chassis node
283549c53ac9SJohnathan Mantey         const std::shared_ptr<boost::container::flat_set<std::string>>
283649c53ac9SJohnathan Mantey             sensorNames =
283749c53ac9SJohnathan Mantey                 std::make_shared<boost::container::flat_set<std::string>>();
2838f65af9e8SRichard Marian Thomaiyar         for (const auto& item : overrideMap)
2839413961deSRichard Marian Thomaiyar         {
2840f65af9e8SRichard Marian Thomaiyar             const auto& sensor = item.first;
284149c53ac9SJohnathan Mantey             if (!findSensorNameUsingSensorPath(sensor, *sensorsList,
284249c53ac9SJohnathan Mantey                                                *sensorNames))
2843f65af9e8SRichard Marian Thomaiyar             {
2844f65af9e8SRichard Marian Thomaiyar                 BMCWEB_LOG_INFO << "Unable to find memberId " << item.first;
28458d1b46d7Szhanghch05                 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2846f65af9e8SRichard Marian Thomaiyar                                            item.second.second, item.first);
2847413961deSRichard Marian Thomaiyar                 return;
2848413961deSRichard Marian Thomaiyar             }
2849f65af9e8SRichard Marian Thomaiyar         }
2850413961deSRichard Marian Thomaiyar         // Get the connection to which the memberId belongs
2851002d39b4SEd Tanous         auto getObjectsWithConnectionCb =
2852002d39b4SEd Tanous             [sensorAsyncResp, overrideMap](
2853002d39b4SEd Tanous                 const boost::container::flat_set<std::string>& /*connections*/,
2854002d39b4SEd Tanous                 const std::set<std::pair<std::string, std::string>>&
2855413961deSRichard Marian Thomaiyar                     objectsWithConnection) {
2856f65af9e8SRichard Marian Thomaiyar             if (objectsWithConnection.size() != overrideMap.size())
2857413961deSRichard Marian Thomaiyar             {
2858413961deSRichard Marian Thomaiyar                 BMCWEB_LOG_INFO
2859f65af9e8SRichard Marian Thomaiyar                     << "Unable to find all objects with proper connection "
2860f65af9e8SRichard Marian Thomaiyar                     << objectsWithConnection.size() << " requested "
2861f65af9e8SRichard Marian Thomaiyar                     << overrideMap.size() << "\n";
28624f277b54SJayaprakash Mutyala                 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2863a0ec28b6SAdrian Ambrożewicz                                            sensorAsyncResp->chassisSubNode ==
2864a0ec28b6SAdrian Ambrożewicz                                                    sensors::node::thermal
2865413961deSRichard Marian Thomaiyar                                                ? "Temperatures"
2866413961deSRichard Marian Thomaiyar                                                : "Voltages",
2867f65af9e8SRichard Marian Thomaiyar                                            "Count");
2868f65af9e8SRichard Marian Thomaiyar                 return;
2869f65af9e8SRichard Marian Thomaiyar             }
2870f65af9e8SRichard Marian Thomaiyar             for (const auto& item : objectsWithConnection)
2871f65af9e8SRichard Marian Thomaiyar             {
287228aa8de5SGeorge Liu                 sdbusplus::message::object_path path(item.first);
287328aa8de5SGeorge Liu                 std::string sensorName = path.filename();
287428aa8de5SGeorge Liu                 if (sensorName.empty())
2875f65af9e8SRichard Marian Thomaiyar                 {
28764f277b54SJayaprakash Mutyala                     messages::internalError(sensorAsyncResp->asyncResp->res);
2877f65af9e8SRichard Marian Thomaiyar                     return;
2878f65af9e8SRichard Marian Thomaiyar                 }
2879f65af9e8SRichard Marian Thomaiyar 
2880f65af9e8SRichard Marian Thomaiyar                 const auto& iterator = overrideMap.find(sensorName);
2881f65af9e8SRichard Marian Thomaiyar                 if (iterator == overrideMap.end())
2882f65af9e8SRichard Marian Thomaiyar                 {
2883f65af9e8SRichard Marian Thomaiyar                     BMCWEB_LOG_INFO << "Unable to find sensor object"
2884f65af9e8SRichard Marian Thomaiyar                                     << item.first << "\n";
28854f277b54SJayaprakash Mutyala                     messages::internalError(sensorAsyncResp->asyncResp->res);
2886413961deSRichard Marian Thomaiyar                     return;
2887413961deSRichard Marian Thomaiyar                 }
2888413961deSRichard Marian Thomaiyar                 crow::connections::systemBus->async_method_call(
2889f65af9e8SRichard Marian Thomaiyar                     [sensorAsyncResp](const boost::system::error_code ec) {
2890413961deSRichard Marian Thomaiyar                     if (ec)
2891413961deSRichard Marian Thomaiyar                     {
28924f277b54SJayaprakash Mutyala                         if (ec.value() ==
28934f277b54SJayaprakash Mutyala                             boost::system::errc::permission_denied)
28944f277b54SJayaprakash Mutyala                         {
28954f277b54SJayaprakash Mutyala                             BMCWEB_LOG_WARNING
28964f277b54SJayaprakash Mutyala                                 << "Manufacturing mode is not Enabled...can't "
28974f277b54SJayaprakash Mutyala                                    "Override the sensor value. ";
28984f277b54SJayaprakash Mutyala 
28994f277b54SJayaprakash Mutyala                             messages::insufficientPrivilege(
29008d1b46d7Szhanghch05                                 sensorAsyncResp->asyncResp->res);
2901413961deSRichard Marian Thomaiyar                             return;
2902413961deSRichard Marian Thomaiyar                         }
29034f277b54SJayaprakash Mutyala                         BMCWEB_LOG_DEBUG
29044f277b54SJayaprakash Mutyala                             << "setOverrideValueStatus DBUS error: " << ec;
29054f277b54SJayaprakash Mutyala                         messages::internalError(
29064f277b54SJayaprakash Mutyala                             sensorAsyncResp->asyncResp->res);
29074f277b54SJayaprakash Mutyala                     }
2908413961deSRichard Marian Thomaiyar                     },
29094f277b54SJayaprakash Mutyala                     item.second, item.first, "org.freedesktop.DBus.Properties",
29104f277b54SJayaprakash Mutyala                     "Set", "xyz.openbmc_project.Sensor.Value", "Value",
2911168e20c1SEd Tanous                     dbus::utility::DbusVariantType(iterator->second.first));
2912f65af9e8SRichard Marian Thomaiyar             }
2913413961deSRichard Marian Thomaiyar         };
2914413961deSRichard Marian Thomaiyar         // Get object with connection for the given sensor name
2915413961deSRichard Marian Thomaiyar         getObjectsWithConnection(sensorAsyncResp, sensorNames,
2916413961deSRichard Marian Thomaiyar                                  std::move(getObjectsWithConnectionCb));
2917413961deSRichard Marian Thomaiyar     };
2918413961deSRichard Marian Thomaiyar     // get full sensor list for the given chassisId and cross verify the sensor.
2919413961deSRichard Marian Thomaiyar     getChassis(sensorAsyncResp, std::move(getChassisSensorListCb));
2920413961deSRichard Marian Thomaiyar }
2921413961deSRichard Marian Thomaiyar 
2922a0ec28b6SAdrian Ambrożewicz /**
2923a0ec28b6SAdrian Ambrożewicz  * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
2924a0ec28b6SAdrian Ambrożewicz  * path of the sensor.
2925a0ec28b6SAdrian Ambrożewicz  *
2926a0ec28b6SAdrian Ambrożewicz  * Function builds valid Redfish response for sensor query of given chassis and
2927a0ec28b6SAdrian Ambrożewicz  * node. It then builds metadata about Redfish<->D-Bus correlations and provides
2928a0ec28b6SAdrian Ambrożewicz  * it to caller in a callback.
2929a0ec28b6SAdrian Ambrożewicz  *
2930a0ec28b6SAdrian Ambrożewicz  * @param chassis   Chassis for which retrieval should be performed
2931a0ec28b6SAdrian Ambrożewicz  * @param node  Node (group) of sensors. See sensors::node for supported values
2932a0ec28b6SAdrian Ambrożewicz  * @param mapComplete   Callback to be called with retrieval result
2933a0ec28b6SAdrian Ambrożewicz  */
2934021d32cfSKrzysztof Grobelny inline void retrieveUriToDbusMap(const std::string& chassis,
2935021d32cfSKrzysztof Grobelny                                  const std::string& node,
2936a0ec28b6SAdrian Ambrożewicz                                  SensorsAsyncResp::DataCompleteCb&& mapComplete)
2937a0ec28b6SAdrian Ambrożewicz {
293802da7c5aSEd Tanous     decltype(sensors::paths)::const_iterator pathIt =
293902da7c5aSEd Tanous         std::find_if(sensors::paths.cbegin(), sensors::paths.cend(),
294002da7c5aSEd Tanous                      [&node](auto&& val) { return val.first == node; });
294102da7c5aSEd Tanous     if (pathIt == sensors::paths.cend())
2942a0ec28b6SAdrian Ambrożewicz     {
2943a0ec28b6SAdrian Ambrożewicz         BMCWEB_LOG_ERROR << "Wrong node provided : " << node;
2944a0ec28b6SAdrian Ambrożewicz         mapComplete(boost::beast::http::status::bad_request, {});
2945a0ec28b6SAdrian Ambrożewicz         return;
2946a0ec28b6SAdrian Ambrożewicz     }
2947d51e072fSKrzysztof Grobelny 
294872374eb7SNan Zhou     auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
2949a0ec28b6SAdrian Ambrożewicz     auto callback =
295072374eb7SNan Zhou         [asyncResp, mapCompleteCb{std::move(mapComplete)}](
2951a0ec28b6SAdrian Ambrożewicz             const boost::beast::http::status status,
2952a0ec28b6SAdrian Ambrożewicz             const boost::container::flat_map<std::string, std::string>&
2953a0ec28b6SAdrian Ambrożewicz                 uriToDbus) { mapCompleteCb(status, uriToDbus); };
2954a0ec28b6SAdrian Ambrożewicz 
2955a0ec28b6SAdrian Ambrożewicz     auto resp = std::make_shared<SensorsAsyncResp>(
2956d51e072fSKrzysztof Grobelny         asyncResp, chassis, pathIt->second, node, std::move(callback));
2957a0ec28b6SAdrian Ambrożewicz     getChassisData(resp);
2958a0ec28b6SAdrian Ambrożewicz }
2959a0ec28b6SAdrian Ambrożewicz 
2960bacb2162SNan Zhou namespace sensors
2961bacb2162SNan Zhou {
2962928fefb9SNan Zhou 
2963bacb2162SNan Zhou inline void getChassisCallback(
2964bacb2162SNan Zhou     const std::shared_ptr<SensorsAsyncResp>& asyncResp,
2965bacb2162SNan Zhou     const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames)
2966bacb2162SNan Zhou {
2967bacb2162SNan Zhou     BMCWEB_LOG_DEBUG << "getChassisCallback enter";
2968bacb2162SNan Zhou 
2969bacb2162SNan Zhou     nlohmann::json& entriesArray =
2970bacb2162SNan Zhou         asyncResp->asyncResp->res.jsonValue["Members"];
2971bacb2162SNan Zhou     for (auto& sensor : *sensorNames)
2972bacb2162SNan Zhou     {
2973bacb2162SNan Zhou         BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor;
2974bacb2162SNan Zhou 
2975bacb2162SNan Zhou         sdbusplus::message::object_path path(sensor);
2976bacb2162SNan Zhou         std::string sensorName = path.filename();
2977bacb2162SNan Zhou         if (sensorName.empty())
2978bacb2162SNan Zhou         {
2979bacb2162SNan Zhou             BMCWEB_LOG_ERROR << "Invalid sensor path: " << sensor;
2980bacb2162SNan Zhou             messages::internalError(asyncResp->asyncResp->res);
2981bacb2162SNan Zhou             return;
2982bacb2162SNan Zhou         }
29831476687dSEd Tanous         nlohmann::json::object_t member;
29841476687dSEd Tanous         member["@odata.id"] = "/redfish/v1/Chassis/" + asyncResp->chassisId +
29851476687dSEd Tanous                               "/" + asyncResp->chassisSubNode + "/" +
29861476687dSEd Tanous                               sensorName;
29871476687dSEd Tanous         entriesArray.push_back(std::move(member));
2988bacb2162SNan Zhou     }
2989bacb2162SNan Zhou 
2990bacb2162SNan Zhou     asyncResp->asyncResp->res.jsonValue["Members@odata.count"] =
2991bacb2162SNan Zhou         entriesArray.size();
2992bacb2162SNan Zhou     BMCWEB_LOG_DEBUG << "getChassisCallback exit";
2993bacb2162SNan Zhou }
2994*e6bd846dSNan Zhou 
2995*e6bd846dSNan Zhou inline void handleSensorGet(App& app, const crow::Request& req,
2996*e6bd846dSNan Zhou                             const std::shared_ptr<bmcweb::AsyncResp>& aResp,
2997*e6bd846dSNan Zhou                             const std::string& chassisId,
2998*e6bd846dSNan Zhou                             const std::string& sensorName)
2999*e6bd846dSNan Zhou {
3000*e6bd846dSNan Zhou     if (!redfish::setUpRedfishRoute(app, req, aResp->res))
3001*e6bd846dSNan Zhou     {
3002*e6bd846dSNan Zhou         return;
3003*e6bd846dSNan Zhou     }
3004*e6bd846dSNan Zhou     BMCWEB_LOG_DEBUG << "Sensor doGet enter";
3005*e6bd846dSNan Zhou     std::shared_ptr<SensorsAsyncResp> asyncResp =
3006*e6bd846dSNan Zhou         std::make_shared<SensorsAsyncResp>(aResp, chassisId,
3007*e6bd846dSNan Zhou                                            std::span<std::string_view>(),
3008*e6bd846dSNan Zhou                                            sensors::node::sensors);
3009*e6bd846dSNan Zhou 
3010*e6bd846dSNan Zhou     const std::array<const char*, 1> interfaces = {
3011*e6bd846dSNan Zhou         "xyz.openbmc_project.Sensor.Value"};
3012*e6bd846dSNan Zhou 
3013*e6bd846dSNan Zhou     // Get a list of all of the sensors that implement Sensor.Value
3014*e6bd846dSNan Zhou     // and get the path and service name associated with the sensor
3015*e6bd846dSNan Zhou     crow::connections::systemBus->async_method_call(
3016*e6bd846dSNan Zhou         [asyncResp,
3017*e6bd846dSNan Zhou          sensorName](const boost::system::error_code ec,
3018*e6bd846dSNan Zhou                      const ::dbus::utility::MapperGetSubTreeResponse& subtree) {
3019*e6bd846dSNan Zhou         BMCWEB_LOG_DEBUG << "respHandler1 enter";
3020*e6bd846dSNan Zhou         if (ec)
3021*e6bd846dSNan Zhou         {
3022*e6bd846dSNan Zhou             messages::internalError(asyncResp->asyncResp->res);
3023*e6bd846dSNan Zhou             BMCWEB_LOG_ERROR << "Sensor getSensorPaths resp_handler: "
3024*e6bd846dSNan Zhou                              << "Dbus error " << ec;
3025*e6bd846dSNan Zhou             return;
3026*e6bd846dSNan Zhou         }
3027*e6bd846dSNan Zhou 
3028*e6bd846dSNan Zhou         ::dbus::utility::MapperGetSubTreeResponse::const_iterator it =
3029*e6bd846dSNan Zhou             std::find_if(
3030*e6bd846dSNan Zhou                 subtree.begin(), subtree.end(),
3031*e6bd846dSNan Zhou                 [sensorName](
3032*e6bd846dSNan Zhou                     const std::pair<
3033*e6bd846dSNan Zhou                         std::string,
3034*e6bd846dSNan Zhou                         std::vector<std::pair<
3035*e6bd846dSNan Zhou                             std::string, std::vector<std::string>>>>& object) {
3036*e6bd846dSNan Zhou             sdbusplus::message::object_path path(object.first);
3037*e6bd846dSNan Zhou             std::string name = path.filename();
3038*e6bd846dSNan Zhou             if (name.empty())
3039*e6bd846dSNan Zhou             {
3040*e6bd846dSNan Zhou                 BMCWEB_LOG_ERROR << "Invalid sensor path: " << object.first;
3041*e6bd846dSNan Zhou                 return false;
3042*e6bd846dSNan Zhou             }
3043*e6bd846dSNan Zhou 
3044*e6bd846dSNan Zhou             return name == sensorName;
3045*e6bd846dSNan Zhou                 });
3046*e6bd846dSNan Zhou 
3047*e6bd846dSNan Zhou         if (it == subtree.end())
3048*e6bd846dSNan Zhou         {
3049*e6bd846dSNan Zhou             BMCWEB_LOG_ERROR << "Could not find path for sensor: "
3050*e6bd846dSNan Zhou                              << sensorName;
3051*e6bd846dSNan Zhou             messages::resourceNotFound(asyncResp->asyncResp->res, "Sensor",
3052*e6bd846dSNan Zhou                                        sensorName);
3053*e6bd846dSNan Zhou             return;
3054*e6bd846dSNan Zhou         }
3055*e6bd846dSNan Zhou         std::string_view sensorPath = (*it).first;
3056*e6bd846dSNan Zhou         BMCWEB_LOG_DEBUG << "Found sensor path for sensor '" << sensorName
3057*e6bd846dSNan Zhou                          << "': " << sensorPath;
3058*e6bd846dSNan Zhou 
3059*e6bd846dSNan Zhou         const std::shared_ptr<boost::container::flat_set<std::string>>
3060*e6bd846dSNan Zhou             sensorList =
3061*e6bd846dSNan Zhou                 std::make_shared<boost::container::flat_set<std::string>>();
3062*e6bd846dSNan Zhou 
3063*e6bd846dSNan Zhou         sensorList->emplace(sensorPath);
3064*e6bd846dSNan Zhou         processSensorList(asyncResp, sensorList);
3065*e6bd846dSNan Zhou         BMCWEB_LOG_DEBUG << "respHandler1 exit";
3066*e6bd846dSNan Zhou         },
3067*e6bd846dSNan Zhou         "xyz.openbmc_project.ObjectMapper",
3068*e6bd846dSNan Zhou         "/xyz/openbmc_project/object_mapper",
3069*e6bd846dSNan Zhou         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
3070*e6bd846dSNan Zhou         "/xyz/openbmc_project/sensors", 2, interfaces);
3071*e6bd846dSNan Zhou }
3072*e6bd846dSNan Zhou 
3073bacb2162SNan Zhou } // namespace sensors
3074bacb2162SNan Zhou 
30757e860f15SJohn Edward Broadbent inline void requestRoutesSensorCollection(App& app)
307695a3ecadSAnthony Wilson {
30777e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/")
3078ed398213SEd Tanous         .privileges(redfish::privileges::getSensorCollection)
3079002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
3080002d39b4SEd Tanous             [&app](const crow::Request& req,
3081002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& aResp,
30827e860f15SJohn Edward Broadbent                    const std::string& chassisId) {
3083928fefb9SNan Zhou         query_param::QueryCapabilities capabilities = {
3084928fefb9SNan Zhou             .canDelegateExpandLevel = 1,
3085928fefb9SNan Zhou         };
3086928fefb9SNan Zhou         query_param::Query delegatedQuery;
3087928fefb9SNan Zhou         if (!redfish::setUpRedfishRouteWithDelegation(
3088928fefb9SNan Zhou                 app, req, aResp->res, delegatedQuery, capabilities))
308945ca1b86SEd Tanous         {
309045ca1b86SEd Tanous             return;
309145ca1b86SEd Tanous         }
309245ca1b86SEd Tanous 
3093928fefb9SNan Zhou         if (delegatedQuery.expandType != query_param::ExpandType::None)
3094928fefb9SNan Zhou         {
3095928fefb9SNan Zhou             // we perform efficient expand.
3096928fefb9SNan Zhou             auto asyncResp = std::make_shared<SensorsAsyncResp>(
309702da7c5aSEd Tanous                 aResp, chassisId, sensors::dbus::sensorPaths,
3098928fefb9SNan Zhou                 sensors::node::sensors,
3099928fefb9SNan Zhou                 /*efficientExpand=*/true);
3100928fefb9SNan Zhou             getChassisData(asyncResp);
31018d1b46d7Szhanghch05 
3102928fefb9SNan Zhou             BMCWEB_LOG_DEBUG
3103928fefb9SNan Zhou                 << "SensorCollection doGet exit via efficient expand handler";
3104928fefb9SNan Zhou             return;
3105928fefb9SNan Zhou         };
3106928fefb9SNan Zhou 
3107928fefb9SNan Zhou         // if there's no efficient expand available, we use the default
3108928fefb9SNan Zhou         // Query Parameters route
3109928fefb9SNan Zhou         auto asyncResp = std::make_shared<SensorsAsyncResp>(
311002da7c5aSEd Tanous             aResp, chassisId, sensors::dbus::sensorPaths,
3111a0ec28b6SAdrian Ambrożewicz             sensors::node::sensors);
3112928fefb9SNan Zhou 
3113928fefb9SNan Zhou         // We get all sensors as hyperlinkes in the chassis (this
3114928fefb9SNan Zhou         // implies we reply on the default query parameters handler)
3115928fefb9SNan Zhou         getChassis(asyncResp,
3116bacb2162SNan Zhou                    std::bind_front(sensors::getChassisCallback, asyncResp));
311795a3ecadSAnthony Wilson         BMCWEB_LOG_DEBUG << "SensorCollection doGet exit";
31187e860f15SJohn Edward Broadbent         });
311995a3ecadSAnthony Wilson }
312095a3ecadSAnthony Wilson 
31217e860f15SJohn Edward Broadbent inline void requestRoutesSensor(App& app)
312295a3ecadSAnthony Wilson {
31237e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/")
3124ed398213SEd Tanous         .privileges(redfish::privileges::getSensor)
3125002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
3126*e6bd846dSNan Zhou             std::bind_front(sensors::handleSensorGet, std::ref(app)));
312795a3ecadSAnthony Wilson }
312895a3ecadSAnthony Wilson 
312908777fb0SLewanczyk, Dawid } // namespace redfish
3130