xref: /openbmc/bmcweb/features/redfish/lib/sensors.hpp (revision 1476687deb1697d865b20458a0097c9ab5fd44e2)
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
352f94c4ecfSEd Tanous     auto respHandler = [callback{std::forward<Callback>(callback)},
353b9d36b47SEd Tanous                         sensorsAsyncResp, sensorNames](
354b9d36b47SEd Tanous                            const boost::system::error_code ec,
355b9d36b47SEd Tanous                            const dbus::utility::MapperGetSubTreeResponse&
356b9d36b47SEd Tanous                                subtree) {
357413961deSRichard Marian Thomaiyar         BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler enter";
3581abe55efSEd Tanous         if (ec)
3591abe55efSEd Tanous         {
3608d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
361413961deSRichard Marian Thomaiyar             BMCWEB_LOG_ERROR
362413961deSRichard Marian Thomaiyar                 << "getObjectsWithConnection resp_handler: Dbus error " << ec;
36308777fb0SLewanczyk, Dawid             return;
36408777fb0SLewanczyk, Dawid         }
36508777fb0SLewanczyk, Dawid 
36655c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "Found " << subtree.size() << " subtrees";
36708777fb0SLewanczyk, Dawid 
36808777fb0SLewanczyk, Dawid         // Make unique list of connections only for requested sensor types and
36908777fb0SLewanczyk, Dawid         // found in the chassis
37008777fb0SLewanczyk, Dawid         boost::container::flat_set<std::string> connections;
371413961deSRichard Marian Thomaiyar         std::set<std::pair<std::string, std::string>> objectsWithConnection;
3721abe55efSEd Tanous         // Intrinsic to avoid malloc.  Most systems will have < 8 sensor
3731abe55efSEd Tanous         // producers
37408777fb0SLewanczyk, Dawid         connections.reserve(8);
37508777fb0SLewanczyk, Dawid 
37649c53ac9SJohnathan Mantey         BMCWEB_LOG_DEBUG << "sensorNames list count: " << sensorNames->size();
37749c53ac9SJohnathan Mantey         for (const std::string& tsensor : *sensorNames)
3781abe55efSEd Tanous         {
37955c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "Sensor to find: " << tsensor;
38008777fb0SLewanczyk, Dawid         }
38108777fb0SLewanczyk, Dawid 
38208777fb0SLewanczyk, Dawid         for (const std::pair<
38308777fb0SLewanczyk, Dawid                  std::string,
38408777fb0SLewanczyk, Dawid                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
3851abe55efSEd Tanous                  object : subtree)
3861abe55efSEd Tanous         {
38749c53ac9SJohnathan Mantey             if (sensorNames->find(object.first) != sensorNames->end())
3881abe55efSEd Tanous             {
38949c53ac9SJohnathan Mantey                 for (const std::pair<std::string, std::vector<std::string>>&
3901abe55efSEd Tanous                          objData : object.second)
3911abe55efSEd Tanous                 {
39249c53ac9SJohnathan Mantey                     BMCWEB_LOG_DEBUG << "Adding connection: " << objData.first;
39308777fb0SLewanczyk, Dawid                     connections.insert(objData.first);
394de629b6eSShawn McCarney                     objectsWithConnection.insert(
395de629b6eSShawn McCarney                         std::make_pair(object.first, objData.first));
39608777fb0SLewanczyk, Dawid                 }
39708777fb0SLewanczyk, Dawid             }
39808777fb0SLewanczyk, Dawid         }
39955c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "Found " << connections.size() << " connections";
400413961deSRichard Marian Thomaiyar         callback(std::move(connections), std::move(objectsWithConnection));
401413961deSRichard Marian Thomaiyar         BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler exit";
40208777fb0SLewanczyk, Dawid     };
40308777fb0SLewanczyk, Dawid     // Make call to ObjectMapper to find all sensors objects
40455c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
40555c7b7a2SEd Tanous         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
4061abe55efSEd Tanous         "/xyz/openbmc_project/object_mapper",
4071abe55efSEd Tanous         "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 2, interfaces);
408413961deSRichard Marian Thomaiyar     BMCWEB_LOG_DEBUG << "getObjectsWithConnection exit";
409413961deSRichard Marian Thomaiyar }
410413961deSRichard Marian Thomaiyar 
411413961deSRichard Marian Thomaiyar /**
412413961deSRichard Marian Thomaiyar  * @brief Create connections necessary for sensors
413413961deSRichard Marian Thomaiyar  * @param SensorsAsyncResp Pointer to object holding response data
414413961deSRichard Marian Thomaiyar  * @param sensorNames Sensors retrieved from chassis
415413961deSRichard Marian Thomaiyar  * @param callback Callback for processing gathered connections
416413961deSRichard Marian Thomaiyar  */
417413961deSRichard Marian Thomaiyar template <typename Callback>
41849c53ac9SJohnathan Mantey void getConnections(
41981ce609eSEd Tanous     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
42049c53ac9SJohnathan Mantey     const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
421413961deSRichard Marian Thomaiyar     Callback&& callback)
422413961deSRichard Marian Thomaiyar {
423413961deSRichard Marian Thomaiyar     auto objectsWithConnectionCb =
424413961deSRichard Marian Thomaiyar         [callback](const boost::container::flat_set<std::string>& connections,
425413961deSRichard Marian Thomaiyar                    const std::set<std::pair<std::string, std::string>>&
4263174e4dfSEd Tanous                    /*objectsWithConnection*/) { callback(connections); };
42781ce609eSEd Tanous     getObjectsWithConnection(sensorsAsyncResp, sensorNames,
428413961deSRichard Marian Thomaiyar                              std::move(objectsWithConnectionCb));
42908777fb0SLewanczyk, Dawid }
43008777fb0SLewanczyk, Dawid 
43108777fb0SLewanczyk, Dawid /**
43249c53ac9SJohnathan Mantey  * @brief Shrinks the list of sensors for processing
43349c53ac9SJohnathan Mantey  * @param SensorsAysncResp  The class holding the Redfish response
43449c53ac9SJohnathan Mantey  * @param allSensors  A list of all the sensors associated to the
43549c53ac9SJohnathan Mantey  * chassis element (i.e. baseboard, front panel, etc...)
43649c53ac9SJohnathan Mantey  * @param activeSensors A list that is a reduction of the incoming
43749c53ac9SJohnathan Mantey  * allSensors list.  Eliminate Thermal sensors when a Power request is
43849c53ac9SJohnathan Mantey  * made, and eliminate Power sensors when a Thermal request is made.
43949c53ac9SJohnathan Mantey  */
44023a21a1cSEd Tanous inline void reduceSensorList(
44181ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
44249c53ac9SJohnathan Mantey     const std::vector<std::string>* allSensors,
443b5a76932SEd Tanous     const std::shared_ptr<boost::container::flat_set<std::string>>&
444b5a76932SEd Tanous         activeSensors)
44549c53ac9SJohnathan Mantey {
44681ce609eSEd Tanous     if (sensorsAsyncResp == nullptr)
44749c53ac9SJohnathan Mantey     {
44849c53ac9SJohnathan Mantey         return;
44949c53ac9SJohnathan Mantey     }
45049c53ac9SJohnathan Mantey     if ((allSensors == nullptr) || (activeSensors == nullptr))
45149c53ac9SJohnathan Mantey     {
45249c53ac9SJohnathan Mantey         messages::resourceNotFound(
4538d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res, sensorsAsyncResp->chassisSubNode,
45481ce609eSEd Tanous             sensorsAsyncResp->chassisSubNode == sensors::node::thermal
455a0ec28b6SAdrian Ambrożewicz                 ? "Temperatures"
45649c53ac9SJohnathan Mantey                 : "Voltages");
45749c53ac9SJohnathan Mantey 
45849c53ac9SJohnathan Mantey         return;
45949c53ac9SJohnathan Mantey     }
46049c53ac9SJohnathan Mantey     if (allSensors->empty())
46149c53ac9SJohnathan Mantey     {
46249c53ac9SJohnathan Mantey         // Nothing to do, the activeSensors object is also empty
46349c53ac9SJohnathan Mantey         return;
46449c53ac9SJohnathan Mantey     }
46549c53ac9SJohnathan Mantey 
46602da7c5aSEd Tanous     for (std::string_view type : sensorsAsyncResp->types)
46749c53ac9SJohnathan Mantey     {
46849c53ac9SJohnathan Mantey         for (const std::string& sensor : *allSensors)
46949c53ac9SJohnathan Mantey         {
47049c53ac9SJohnathan Mantey             if (boost::starts_with(sensor, type))
47149c53ac9SJohnathan Mantey             {
47249c53ac9SJohnathan Mantey                 activeSensors->emplace(sensor);
47349c53ac9SJohnathan Mantey             }
47449c53ac9SJohnathan Mantey         }
47549c53ac9SJohnathan Mantey     }
47649c53ac9SJohnathan Mantey }
47749c53ac9SJohnathan Mantey 
47849c53ac9SJohnathan Mantey /**
4794bb3dc34SCarol Wang  * @brief Retrieves valid chassis path
4804bb3dc34SCarol Wang  * @param asyncResp   Pointer to object holding response data
4814bb3dc34SCarol Wang  * @param callback  Callback for next step to get valid chassis path
4824bb3dc34SCarol Wang  */
4834bb3dc34SCarol Wang template <typename Callback>
484b5a76932SEd Tanous void getValidChassisPath(const std::shared_ptr<SensorsAsyncResp>& asyncResp,
4854bb3dc34SCarol Wang                          Callback&& callback)
4864bb3dc34SCarol Wang {
4874bb3dc34SCarol Wang     BMCWEB_LOG_DEBUG << "checkChassisId enter";
4884bb3dc34SCarol Wang     const std::array<const char*, 2> interfaces = {
4894bb3dc34SCarol Wang         "xyz.openbmc_project.Inventory.Item.Board",
4904bb3dc34SCarol Wang         "xyz.openbmc_project.Inventory.Item.Chassis"};
4914bb3dc34SCarol Wang 
492b9d36b47SEd Tanous     auto respHandler = [callback{std::forward<Callback>(callback)}, asyncResp](
493b9d36b47SEd Tanous                            const boost::system::error_code ec,
494b9d36b47SEd Tanous                            const dbus::utility::MapperGetSubTreePathsResponse&
495b9d36b47SEd Tanous                                chassisPaths) mutable {
4964bb3dc34SCarol Wang         BMCWEB_LOG_DEBUG << "getValidChassisPath respHandler enter";
4974bb3dc34SCarol Wang         if (ec)
4984bb3dc34SCarol Wang         {
499b9d36b47SEd Tanous             BMCWEB_LOG_ERROR << "getValidChassisPath respHandler DBUS error: "
500b9d36b47SEd Tanous                              << ec;
5018d1b46d7Szhanghch05             messages::internalError(asyncResp->asyncResp->res);
5024bb3dc34SCarol Wang             return;
5034bb3dc34SCarol Wang         }
5044bb3dc34SCarol Wang 
5054bb3dc34SCarol Wang         std::optional<std::string> chassisPath;
5064bb3dc34SCarol Wang         std::string chassisName;
5074bb3dc34SCarol Wang         for (const std::string& chassis : chassisPaths)
5084bb3dc34SCarol Wang         {
50928aa8de5SGeorge Liu             sdbusplus::message::object_path path(chassis);
51028aa8de5SGeorge Liu             chassisName = path.filename();
51128aa8de5SGeorge Liu             if (chassisName.empty())
5124bb3dc34SCarol Wang             {
5134bb3dc34SCarol Wang                 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
5144bb3dc34SCarol Wang                 continue;
5154bb3dc34SCarol Wang             }
5164bb3dc34SCarol Wang             if (chassisName == asyncResp->chassisId)
5174bb3dc34SCarol Wang             {
5184bb3dc34SCarol Wang                 chassisPath = chassis;
5194bb3dc34SCarol Wang                 break;
5204bb3dc34SCarol Wang             }
5214bb3dc34SCarol Wang         }
5224bb3dc34SCarol Wang         callback(chassisPath);
5234bb3dc34SCarol Wang     };
5244bb3dc34SCarol Wang 
5254bb3dc34SCarol Wang     // Get the Chassis Collection
5264bb3dc34SCarol Wang     crow::connections::systemBus->async_method_call(
5274bb3dc34SCarol Wang         respHandler, "xyz.openbmc_project.ObjectMapper",
5284bb3dc34SCarol Wang         "/xyz/openbmc_project/object_mapper",
5294bb3dc34SCarol Wang         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
5304bb3dc34SCarol Wang         "/xyz/openbmc_project/inventory", 0, interfaces);
5314bb3dc34SCarol Wang     BMCWEB_LOG_DEBUG << "checkChassisId exit";
5324bb3dc34SCarol Wang }
5334bb3dc34SCarol Wang 
5344bb3dc34SCarol Wang /**
53508777fb0SLewanczyk, Dawid  * @brief Retrieves requested chassis sensors and redundancy data from DBus .
536588c3f0dSKowalski, Kamil  * @param SensorsAsyncResp   Pointer to object holding response data
53708777fb0SLewanczyk, Dawid  * @param callback  Callback for next step in gathered sensor processing
53808777fb0SLewanczyk, Dawid  */
53908777fb0SLewanczyk, Dawid template <typename Callback>
540b5a76932SEd Tanous void getChassis(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
5411abe55efSEd Tanous                 Callback&& callback)
5421abe55efSEd Tanous {
54355c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "getChassis enter";
544adc4f0dbSShawn McCarney     const std::array<const char*, 2> interfaces = {
54549c53ac9SJohnathan Mantey         "xyz.openbmc_project.Inventory.Item.Board",
546adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Item.Chassis"};
547f94c4ecfSEd Tanous     auto respHandler = [callback{std::forward<Callback>(callback)},
548f94c4ecfSEd Tanous                         sensorsAsyncResp](
54949c53ac9SJohnathan Mantey                            const boost::system::error_code ec,
550b9d36b47SEd Tanous                            const dbus::utility::MapperGetSubTreePathsResponse&
551b9d36b47SEd Tanous                                chassisPaths) {
55255c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "getChassis respHandler enter";
5531abe55efSEd Tanous         if (ec)
5541abe55efSEd Tanous         {
55555c7b7a2SEd Tanous             BMCWEB_LOG_ERROR << "getChassis respHandler DBUS error: " << ec;
5568d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
55708777fb0SLewanczyk, Dawid             return;
55808777fb0SLewanczyk, Dawid         }
55908777fb0SLewanczyk, Dawid 
56049c53ac9SJohnathan Mantey         const std::string* chassisPath = nullptr;
56149c53ac9SJohnathan Mantey         std::string chassisName;
56249c53ac9SJohnathan Mantey         for (const std::string& chassis : chassisPaths)
5631abe55efSEd Tanous         {
56428aa8de5SGeorge Liu             sdbusplus::message::object_path path(chassis);
56528aa8de5SGeorge Liu             chassisName = path.filename();
56628aa8de5SGeorge Liu             if (chassisName.empty())
5671abe55efSEd Tanous             {
56849c53ac9SJohnathan Mantey                 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
569daf36e2eSEd Tanous                 continue;
570daf36e2eSEd Tanous             }
57149c53ac9SJohnathan Mantey             if (chassisName == sensorsAsyncResp->chassisId)
5721abe55efSEd Tanous             {
57349c53ac9SJohnathan Mantey                 chassisPath = &chassis;
57449c53ac9SJohnathan Mantey                 break;
575daf36e2eSEd Tanous             }
57649c53ac9SJohnathan Mantey         }
57749c53ac9SJohnathan Mantey         if (chassisPath == nullptr)
5781abe55efSEd Tanous         {
5798d1b46d7Szhanghch05             messages::resourceNotFound(sensorsAsyncResp->asyncResp->res,
5808d1b46d7Szhanghch05                                        "Chassis", sensorsAsyncResp->chassisId);
58149c53ac9SJohnathan Mantey             return;
5821abe55efSEd Tanous         }
58308777fb0SLewanczyk, Dawid 
58449c53ac9SJohnathan Mantey         const std::string& chassisSubNode = sensorsAsyncResp->chassisSubNode;
585a0ec28b6SAdrian Ambrożewicz         if (chassisSubNode == sensors::node::power)
58649c53ac9SJohnathan Mantey         {
5878d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] =
58849c53ac9SJohnathan Mantey                 "#Power.v1_5_2.Power";
58949c53ac9SJohnathan Mantey         }
590a0ec28b6SAdrian Ambrożewicz         else if (chassisSubNode == sensors::node::thermal)
59149c53ac9SJohnathan Mantey         {
5928d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] =
59349c53ac9SJohnathan Mantey                 "#Thermal.v1_4_0.Thermal";
5948d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["Fans"] =
5958d1b46d7Szhanghch05                 nlohmann::json::array();
5968d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["Temperatures"] =
5974f9a2130SJennifer Lee                 nlohmann::json::array();
59849c53ac9SJohnathan Mantey         }
599a0ec28b6SAdrian Ambrożewicz         else if (chassisSubNode == sensors::node::sensors)
60095a3ecadSAnthony Wilson         {
6018d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] =
60295a3ecadSAnthony Wilson                 "#SensorCollection.SensorCollection";
6038d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["Description"] =
60495a3ecadSAnthony Wilson                 "Collection of Sensors for this Chassis";
6058d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["Members"] =
60695a3ecadSAnthony Wilson                 nlohmann::json::array();
6078d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["Members@odata.count"] =
6088d1b46d7Szhanghch05                 0;
60995a3ecadSAnthony Wilson         }
61095a3ecadSAnthony Wilson 
611a0ec28b6SAdrian Ambrożewicz         if (chassisSubNode != sensors::node::sensors)
61295a3ecadSAnthony Wilson         {
6138d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["Id"] = chassisSubNode;
61495a3ecadSAnthony Wilson         }
61595a3ecadSAnthony Wilson 
6168d1b46d7Szhanghch05         sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
61749c53ac9SJohnathan Mantey             "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId + "/" +
61849c53ac9SJohnathan Mantey             chassisSubNode;
6198d1b46d7Szhanghch05         sensorsAsyncResp->asyncResp->res.jsonValue["Name"] = chassisSubNode;
6208fb49dd6SShawn McCarney         // Get the list of all sensors for this Chassis element
6218fb49dd6SShawn McCarney         std::string sensorPath = *chassisPath + "/all_sensors";
6221e1e598dSJonathan Doman         sdbusplus::asio::getProperty<std::vector<std::string>>(
6231e1e598dSJonathan Doman             *crow::connections::systemBus, "xyz.openbmc_project.ObjectMapper",
6241e1e598dSJonathan Doman             sensorPath, "xyz.openbmc_project.Association", "endpoints",
625f94c4ecfSEd Tanous             [sensorsAsyncResp,
626f94c4ecfSEd Tanous              callback{std::forward<const Callback>(callback)}](
627271584abSEd Tanous                 const boost::system::error_code& e,
6281e1e598dSJonathan Doman                 const std::vector<std::string>& nodeSensorList) {
629271584abSEd Tanous                 if (e)
63049c53ac9SJohnathan Mantey                 {
631271584abSEd Tanous                     if (e.value() != EBADR)
63249c53ac9SJohnathan Mantey                     {
6338d1b46d7Szhanghch05                         messages::internalError(
6348d1b46d7Szhanghch05                             sensorsAsyncResp->asyncResp->res);
63549c53ac9SJohnathan Mantey                         return;
63649c53ac9SJohnathan Mantey                     }
63749c53ac9SJohnathan Mantey                 }
63849c53ac9SJohnathan Mantey                 const std::shared_ptr<boost::container::flat_set<std::string>>
63949c53ac9SJohnathan Mantey                     culledSensorList = std::make_shared<
64049c53ac9SJohnathan Mantey                         boost::container::flat_set<std::string>>();
6411e1e598dSJonathan Doman                 reduceSensorList(sensorsAsyncResp, &nodeSensorList,
64249c53ac9SJohnathan Mantey                                  culledSensorList);
64349c53ac9SJohnathan Mantey                 callback(culledSensorList);
6441e1e598dSJonathan Doman             });
64549c53ac9SJohnathan Mantey     };
64649c53ac9SJohnathan Mantey 
64749c53ac9SJohnathan Mantey     // Get the Chassis Collection
64849c53ac9SJohnathan Mantey     crow::connections::systemBus->async_method_call(
64949c53ac9SJohnathan Mantey         respHandler, "xyz.openbmc_project.ObjectMapper",
65049c53ac9SJohnathan Mantey         "/xyz/openbmc_project/object_mapper",
65149c53ac9SJohnathan Mantey         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
652271584abSEd Tanous         "/xyz/openbmc_project/inventory", 0, interfaces);
65355c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "getChassis exit";
65408777fb0SLewanczyk, Dawid }
65508777fb0SLewanczyk, Dawid 
65608777fb0SLewanczyk, Dawid /**
657de629b6eSShawn McCarney  * @brief Finds all DBus object paths that implement ObjectManager.
658de629b6eSShawn McCarney  *
659de629b6eSShawn McCarney  * Creates a mapping from the associated connection name to the object path.
660de629b6eSShawn McCarney  *
661de629b6eSShawn McCarney  * Finds the object paths asynchronously.  Invokes callback when information has
662de629b6eSShawn McCarney  * been obtained.
663de629b6eSShawn McCarney  *
664de629b6eSShawn McCarney  * The callback must have the following signature:
665de629b6eSShawn McCarney  *   @code
6668fb49dd6SShawn McCarney  *   callback(std::shared_ptr<boost::container::flat_map<std::string,
6678fb49dd6SShawn McCarney  *                std::string>> objectMgrPaths)
668de629b6eSShawn McCarney  *   @endcode
669de629b6eSShawn McCarney  *
67049c53ac9SJohnathan Mantey  * @param sensorsAsyncResp Pointer to object holding response data.
671de629b6eSShawn McCarney  * @param callback Callback to invoke when object paths obtained.
672de629b6eSShawn McCarney  */
673de629b6eSShawn McCarney template <typename Callback>
674b5a76932SEd Tanous void getObjectManagerPaths(
67581ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
676de629b6eSShawn McCarney     Callback&& callback)
677de629b6eSShawn McCarney {
678de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getObjectManagerPaths enter";
679de629b6eSShawn McCarney     const std::array<std::string, 1> interfaces = {
680de629b6eSShawn McCarney         "org.freedesktop.DBus.ObjectManager"};
681de629b6eSShawn McCarney 
682de629b6eSShawn McCarney     // Response handler for GetSubTree DBus method
683f94c4ecfSEd Tanous     auto respHandler = [callback{std::forward<Callback>(callback)},
684b9d36b47SEd Tanous                         sensorsAsyncResp](
685b9d36b47SEd Tanous                            const boost::system::error_code ec,
686b9d36b47SEd Tanous                            const dbus::utility::MapperGetSubTreeResponse&
687b9d36b47SEd Tanous                                subtree) {
688de629b6eSShawn McCarney         BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler enter";
689de629b6eSShawn McCarney         if (ec)
690de629b6eSShawn McCarney         {
6918d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
692de629b6eSShawn McCarney             BMCWEB_LOG_ERROR << "getObjectManagerPaths respHandler: DBus error "
693de629b6eSShawn McCarney                              << ec;
694de629b6eSShawn McCarney             return;
695de629b6eSShawn McCarney         }
696de629b6eSShawn McCarney 
697de629b6eSShawn McCarney         // Loop over returned object paths
6988fb49dd6SShawn McCarney         std::shared_ptr<boost::container::flat_map<std::string, std::string>>
6998fb49dd6SShawn McCarney             objectMgrPaths = std::make_shared<
7008fb49dd6SShawn McCarney                 boost::container::flat_map<std::string, std::string>>();
701de629b6eSShawn McCarney         for (const std::pair<
702de629b6eSShawn McCarney                  std::string,
703de629b6eSShawn McCarney                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
704de629b6eSShawn McCarney                  object : subtree)
705de629b6eSShawn McCarney         {
706de629b6eSShawn McCarney             // Loop over connections for current object path
707de629b6eSShawn McCarney             const std::string& objectPath = object.first;
708de629b6eSShawn McCarney             for (const std::pair<std::string, std::vector<std::string>>&
709de629b6eSShawn McCarney                      objData : object.second)
710de629b6eSShawn McCarney             {
711de629b6eSShawn McCarney                 // Add mapping from connection to object path
712de629b6eSShawn McCarney                 const std::string& connection = objData.first;
7138fb49dd6SShawn McCarney                 (*objectMgrPaths)[connection] = objectPath;
714de629b6eSShawn McCarney                 BMCWEB_LOG_DEBUG << "Added mapping " << connection << " -> "
715de629b6eSShawn McCarney                                  << objectPath;
716de629b6eSShawn McCarney             }
717de629b6eSShawn McCarney         }
7188fb49dd6SShawn McCarney         callback(objectMgrPaths);
719de629b6eSShawn McCarney         BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler exit";
720de629b6eSShawn McCarney     };
721de629b6eSShawn McCarney 
722de629b6eSShawn McCarney     // Query mapper for all DBus object paths that implement ObjectManager
723de629b6eSShawn McCarney     crow::connections::systemBus->async_method_call(
724de629b6eSShawn McCarney         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
725de629b6eSShawn McCarney         "/xyz/openbmc_project/object_mapper",
726271584abSEd Tanous         "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0, interfaces);
727de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getObjectManagerPaths exit";
728de629b6eSShawn McCarney }
729de629b6eSShawn McCarney 
730de629b6eSShawn McCarney /**
731adc4f0dbSShawn McCarney  * @brief Returns the Redfish State value for the specified inventory item.
732adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with a sensor.
733adc4f0dbSShawn McCarney  * @return State value for inventory item.
73434dd179eSJames Feist  */
73523a21a1cSEd Tanous inline std::string getState(const InventoryItem* inventoryItem)
736adc4f0dbSShawn McCarney {
737adc4f0dbSShawn McCarney     if ((inventoryItem != nullptr) && !(inventoryItem->isPresent))
738adc4f0dbSShawn McCarney     {
739adc4f0dbSShawn McCarney         return "Absent";
740adc4f0dbSShawn McCarney     }
74134dd179eSJames Feist 
742adc4f0dbSShawn McCarney     return "Enabled";
743adc4f0dbSShawn McCarney }
744adc4f0dbSShawn McCarney 
745adc4f0dbSShawn McCarney /**
746adc4f0dbSShawn McCarney  * @brief Returns the Redfish Health value for the specified sensor.
747adc4f0dbSShawn McCarney  * @param sensorJson Sensor JSON object.
748adc4f0dbSShawn McCarney  * @param interfacesDict Map of all sensor interfaces.
749adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with the sensor.  Will
750adc4f0dbSShawn McCarney  * be nullptr if no associated inventory item was found.
751adc4f0dbSShawn McCarney  * @return Health value for sensor.
752adc4f0dbSShawn McCarney  */
753711ac7a9SEd Tanous inline std::string
754711ac7a9SEd Tanous     getHealth(nlohmann::json& sensorJson,
755711ac7a9SEd Tanous               const dbus::utility::DBusInteracesMap& interfacesDict,
756adc4f0dbSShawn McCarney               const InventoryItem* inventoryItem)
75734dd179eSJames Feist {
758adc4f0dbSShawn McCarney     // Get current health value (if any) in the sensor JSON object.  Some JSON
759adc4f0dbSShawn McCarney     // objects contain multiple sensors (such as PowerSupplies).  We want to set
760adc4f0dbSShawn McCarney     // the overall health to be the most severe of any of the sensors.
761adc4f0dbSShawn McCarney     std::string currentHealth;
762adc4f0dbSShawn McCarney     auto statusIt = sensorJson.find("Status");
763adc4f0dbSShawn McCarney     if (statusIt != sensorJson.end())
764adc4f0dbSShawn McCarney     {
765adc4f0dbSShawn McCarney         auto healthIt = statusIt->find("Health");
766adc4f0dbSShawn McCarney         if (healthIt != statusIt->end())
767adc4f0dbSShawn McCarney         {
768adc4f0dbSShawn McCarney             std::string* health = healthIt->get_ptr<std::string*>();
769adc4f0dbSShawn McCarney             if (health != nullptr)
770adc4f0dbSShawn McCarney             {
771adc4f0dbSShawn McCarney                 currentHealth = *health;
772adc4f0dbSShawn McCarney             }
773adc4f0dbSShawn McCarney         }
774adc4f0dbSShawn McCarney     }
775adc4f0dbSShawn McCarney 
776adc4f0dbSShawn McCarney     // If current health in JSON object is already Critical, return that.  This
777adc4f0dbSShawn McCarney     // should override the sensor health, which might be less severe.
778adc4f0dbSShawn McCarney     if (currentHealth == "Critical")
779adc4f0dbSShawn McCarney     {
780adc4f0dbSShawn McCarney         return "Critical";
781adc4f0dbSShawn McCarney     }
782adc4f0dbSShawn McCarney 
783adc4f0dbSShawn McCarney     // Check if sensor has critical threshold alarm
784711ac7a9SEd Tanous 
7859eb808c1SEd Tanous     for (const auto& [interface, values] : interfacesDict)
78634dd179eSJames Feist     {
787711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Sensor.Threshold.Critical")
78834dd179eSJames Feist         {
7899eb808c1SEd Tanous             for (const auto& [valueName, value] : values)
790711ac7a9SEd Tanous             {
791711ac7a9SEd Tanous                 if (valueName == "CriticalAlarmHigh" ||
792711ac7a9SEd Tanous                     valueName == "CriticalAlarmLow")
793711ac7a9SEd Tanous                 {
794711ac7a9SEd Tanous                     const bool* asserted = std::get_if<bool>(&value);
79534dd179eSJames Feist                     if (asserted == nullptr)
79634dd179eSJames Feist                     {
79734dd179eSJames Feist                         BMCWEB_LOG_ERROR << "Illegal sensor threshold";
79834dd179eSJames Feist                     }
79934dd179eSJames Feist                     else if (*asserted)
80034dd179eSJames Feist                     {
80134dd179eSJames Feist                         return "Critical";
80234dd179eSJames Feist                     }
80334dd179eSJames Feist                 }
80434dd179eSJames Feist             }
80534dd179eSJames Feist         }
80634dd179eSJames Feist     }
80734dd179eSJames Feist 
808adc4f0dbSShawn McCarney     // Check if associated inventory item is not functional
809adc4f0dbSShawn McCarney     if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional))
810adc4f0dbSShawn McCarney     {
811adc4f0dbSShawn McCarney         return "Critical";
812adc4f0dbSShawn McCarney     }
813adc4f0dbSShawn McCarney 
814adc4f0dbSShawn McCarney     // If current health in JSON object is already Warning, return that.  This
815adc4f0dbSShawn McCarney     // should override the sensor status, which might be less severe.
816adc4f0dbSShawn McCarney     if (currentHealth == "Warning")
817adc4f0dbSShawn McCarney     {
818adc4f0dbSShawn McCarney         return "Warning";
819adc4f0dbSShawn McCarney     }
820adc4f0dbSShawn McCarney 
821adc4f0dbSShawn McCarney     // Check if sensor has warning threshold alarm
8229eb808c1SEd Tanous     for (const auto& [interface, values] : interfacesDict)
82334dd179eSJames Feist     {
824711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Sensor.Threshold.Warning")
82534dd179eSJames Feist         {
8269eb808c1SEd Tanous             for (const auto& [valueName, value] : values)
827711ac7a9SEd Tanous             {
828711ac7a9SEd Tanous                 if (valueName == "WarningAlarmHigh" ||
829711ac7a9SEd Tanous                     valueName == "WarningAlarmLow")
830711ac7a9SEd Tanous                 {
831711ac7a9SEd Tanous                     const bool* asserted = std::get_if<bool>(&value);
83234dd179eSJames Feist                     if (asserted == nullptr)
83334dd179eSJames Feist                     {
83434dd179eSJames Feist                         BMCWEB_LOG_ERROR << "Illegal sensor threshold";
83534dd179eSJames Feist                     }
83634dd179eSJames Feist                     else if (*asserted)
83734dd179eSJames Feist                     {
838ebe4d91eSEd Tanous                         return "Warning";
83934dd179eSJames Feist                     }
84034dd179eSJames Feist                 }
84134dd179eSJames Feist             }
84234dd179eSJames Feist         }
84334dd179eSJames Feist     }
844adc4f0dbSShawn McCarney 
84534dd179eSJames Feist     return "OK";
84634dd179eSJames Feist }
84734dd179eSJames Feist 
84823a21a1cSEd Tanous inline void setLedState(nlohmann::json& sensorJson,
849d500549bSAnthony Wilson                         const InventoryItem* inventoryItem)
850d500549bSAnthony Wilson {
851d500549bSAnthony Wilson     if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty())
852d500549bSAnthony Wilson     {
853d500549bSAnthony Wilson         switch (inventoryItem->ledState)
854d500549bSAnthony Wilson         {
855d500549bSAnthony Wilson             case LedState::OFF:
856d500549bSAnthony Wilson                 sensorJson["IndicatorLED"] = "Off";
857d500549bSAnthony Wilson                 break;
858d500549bSAnthony Wilson             case LedState::ON:
859d500549bSAnthony Wilson                 sensorJson["IndicatorLED"] = "Lit";
860d500549bSAnthony Wilson                 break;
861d500549bSAnthony Wilson             case LedState::BLINK:
862d500549bSAnthony Wilson                 sensorJson["IndicatorLED"] = "Blinking";
863d500549bSAnthony Wilson                 break;
86423a21a1cSEd Tanous             case LedState::UNKNOWN:
865d500549bSAnthony Wilson                 break;
866d500549bSAnthony Wilson         }
867d500549bSAnthony Wilson     }
868d500549bSAnthony Wilson }
869d500549bSAnthony Wilson 
87034dd179eSJames Feist /**
87108777fb0SLewanczyk, Dawid  * @brief Builds a json sensor representation of a sensor.
87208777fb0SLewanczyk, Dawid  * @param sensorName  The name of the sensor to be built
873274fad5aSGunnar Mills  * @param sensorType  The type (temperature, fan_tach, etc) of the sensor to
87408777fb0SLewanczyk, Dawid  * build
875a0ec28b6SAdrian Ambrożewicz  * @param sensorsAsyncResp  Sensor metadata
87608777fb0SLewanczyk, Dawid  * @param interfacesDict  A dictionary of the interfaces and properties of said
87708777fb0SLewanczyk, Dawid  * interfaces to be built from
87808777fb0SLewanczyk, Dawid  * @param sensor_json  The json object to fill
879adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with the sensor.  Will
880adc4f0dbSShawn McCarney  * be nullptr if no associated inventory item was found.
88108777fb0SLewanczyk, Dawid  */
88223a21a1cSEd Tanous inline void objectInterfacesToJson(
88308777fb0SLewanczyk, Dawid     const std::string& sensorName, const std::string& sensorType,
884b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
885711ac7a9SEd Tanous     const dbus::utility::DBusInteracesMap& interfacesDict,
88681ce609eSEd Tanous     nlohmann::json& sensorJson, InventoryItem* inventoryItem)
8871abe55efSEd Tanous {
88808777fb0SLewanczyk, Dawid     // Assume values exist as is (10^0 == 1) if no scale exists
88908777fb0SLewanczyk, Dawid     int64_t scaleMultiplier = 0;
8909eb808c1SEd Tanous     for (const auto& [interface, values] : interfacesDict)
8911abe55efSEd Tanous     {
892711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Sensor.Value")
893711ac7a9SEd Tanous         {
8949eb808c1SEd Tanous             for (const auto& [valueName, value] : values)
895711ac7a9SEd Tanous             {
896711ac7a9SEd Tanous                 if (valueName == "Scale")
897711ac7a9SEd Tanous                 {
898711ac7a9SEd Tanous                     const int64_t* int64Value = std::get_if<int64_t>(&value);
8991abe55efSEd Tanous                     if (int64Value != nullptr)
9001abe55efSEd Tanous                     {
90108777fb0SLewanczyk, Dawid                         scaleMultiplier = *int64Value;
90208777fb0SLewanczyk, Dawid                     }
90308777fb0SLewanczyk, Dawid                 }
904711ac7a9SEd Tanous             }
905711ac7a9SEd Tanous         }
906711ac7a9SEd Tanous     }
90708777fb0SLewanczyk, Dawid 
908a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
909adc4f0dbSShawn McCarney     {
91095a3ecadSAnthony Wilson         // For sensors in SensorCollection we set Id instead of MemberId,
91195a3ecadSAnthony Wilson         // including power sensors.
91281ce609eSEd Tanous         sensorJson["Id"] = sensorName;
91381ce609eSEd Tanous         sensorJson["Name"] = boost::replace_all_copy(sensorName, "_", " ");
91495a3ecadSAnthony Wilson     }
91595a3ecadSAnthony Wilson     else if (sensorType != "power")
91695a3ecadSAnthony Wilson     {
91795a3ecadSAnthony Wilson         // Set MemberId and Name for non-power sensors.  For PowerSupplies and
91895a3ecadSAnthony Wilson         // PowerControl, those properties have more general values because
91995a3ecadSAnthony Wilson         // multiple sensors can be stored in the same JSON object.
92081ce609eSEd Tanous         sensorJson["MemberId"] = sensorName;
92181ce609eSEd Tanous         sensorJson["Name"] = boost::replace_all_copy(sensorName, "_", " ");
922adc4f0dbSShawn McCarney     }
923e742b6ccSEd Tanous 
92481ce609eSEd Tanous     sensorJson["Status"]["State"] = getState(inventoryItem);
92581ce609eSEd Tanous     sensorJson["Status"]["Health"] =
92681ce609eSEd Tanous         getHealth(sensorJson, interfacesDict, inventoryItem);
92708777fb0SLewanczyk, Dawid 
92808777fb0SLewanczyk, Dawid     // Parameter to set to override the type we get from dbus, and force it to
92908777fb0SLewanczyk, Dawid     // int, regardless of what is available.  This is used for schemas like fan,
93008777fb0SLewanczyk, Dawid     // that require integers, not floats.
93108777fb0SLewanczyk, Dawid     bool forceToInt = false;
93208777fb0SLewanczyk, Dawid 
9333929aca1SAnthony Wilson     nlohmann::json::json_pointer unit("/Reading");
934a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
93595a3ecadSAnthony Wilson     {
93681ce609eSEd Tanous         sensorJson["@odata.type"] = "#Sensor.v1_0_0.Sensor";
937c2bf7f99SWludzik, Jozef 
938c2bf7f99SWludzik, Jozef         const std::string& readingType = sensors::toReadingType(sensorType);
939c2bf7f99SWludzik, Jozef         if (readingType.empty())
94095a3ecadSAnthony Wilson         {
941c2bf7f99SWludzik, Jozef             BMCWEB_LOG_ERROR << "Redfish cannot map reading type for "
942c2bf7f99SWludzik, Jozef                              << sensorType;
94395a3ecadSAnthony Wilson         }
944c2bf7f99SWludzik, Jozef         else
94595a3ecadSAnthony Wilson         {
946c2bf7f99SWludzik, Jozef             sensorJson["ReadingType"] = readingType;
94795a3ecadSAnthony Wilson         }
948c2bf7f99SWludzik, Jozef 
949c2bf7f99SWludzik, Jozef         const std::string& readingUnits = sensors::toReadingUnits(sensorType);
950c2bf7f99SWludzik, Jozef         if (readingUnits.empty())
951f8ede15eSAdrian Ambrożewicz         {
952c2bf7f99SWludzik, Jozef             BMCWEB_LOG_ERROR << "Redfish cannot map reading unit for "
953c2bf7f99SWludzik, Jozef                              << sensorType;
954c2bf7f99SWludzik, Jozef         }
955c2bf7f99SWludzik, Jozef         else
956c2bf7f99SWludzik, Jozef         {
957c2bf7f99SWludzik, Jozef             sensorJson["ReadingUnits"] = readingUnits;
958f8ede15eSAdrian Ambrożewicz         }
95995a3ecadSAnthony Wilson     }
96095a3ecadSAnthony Wilson     else if (sensorType == "temperature")
9611abe55efSEd Tanous     {
9623929aca1SAnthony Wilson         unit = "/ReadingCelsius"_json_pointer;
96381ce609eSEd Tanous         sensorJson["@odata.type"] = "#Thermal.v1_3_0.Temperature";
96408777fb0SLewanczyk, Dawid         // TODO(ed) Documentation says that path should be type fan_tach,
96508777fb0SLewanczyk, Dawid         // implementation seems to implement fan
9661abe55efSEd Tanous     }
9671abe55efSEd Tanous     else if (sensorType == "fan" || sensorType == "fan_tach")
9681abe55efSEd Tanous     {
9693929aca1SAnthony Wilson         unit = "/Reading"_json_pointer;
97081ce609eSEd Tanous         sensorJson["ReadingUnits"] = "RPM";
97181ce609eSEd Tanous         sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
97281ce609eSEd Tanous         setLedState(sensorJson, inventoryItem);
97308777fb0SLewanczyk, Dawid         forceToInt = true;
9741abe55efSEd Tanous     }
9756f6d0d32SEd Tanous     else if (sensorType == "fan_pwm")
9766f6d0d32SEd Tanous     {
9773929aca1SAnthony Wilson         unit = "/Reading"_json_pointer;
97881ce609eSEd Tanous         sensorJson["ReadingUnits"] = "Percent";
97981ce609eSEd Tanous         sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
98081ce609eSEd Tanous         setLedState(sensorJson, inventoryItem);
9816f6d0d32SEd Tanous         forceToInt = true;
9826f6d0d32SEd Tanous     }
9831abe55efSEd Tanous     else if (sensorType == "voltage")
9841abe55efSEd Tanous     {
9853929aca1SAnthony Wilson         unit = "/ReadingVolts"_json_pointer;
98681ce609eSEd Tanous         sensorJson["@odata.type"] = "#Power.v1_0_0.Voltage";
9871abe55efSEd Tanous     }
9882474adfaSEd Tanous     else if (sensorType == "power")
9892474adfaSEd Tanous     {
99049c53ac9SJohnathan Mantey         std::string sensorNameLower =
99149c53ac9SJohnathan Mantey             boost::algorithm::to_lower_copy(sensorName);
99249c53ac9SJohnathan Mantey 
99355f79e6fSEd Tanous         if (sensorName == "total_power")
994028f7ebcSEddie James         {
99581ce609eSEd Tanous             sensorJson["@odata.type"] = "#Power.v1_0_0.PowerControl";
9967ab06f49SGunnar Mills             // Put multiple "sensors" into a single PowerControl, so have
9977ab06f49SGunnar Mills             // generic names for MemberId and Name. Follows Redfish mockup.
99881ce609eSEd Tanous             sensorJson["MemberId"] = "0";
99981ce609eSEd Tanous             sensorJson["Name"] = "Chassis Power Control";
10003929aca1SAnthony Wilson             unit = "/PowerConsumedWatts"_json_pointer;
1001028f7ebcSEddie James         }
1002028f7ebcSEddie James         else if (sensorNameLower.find("input") != std::string::npos)
100349c53ac9SJohnathan Mantey         {
10043929aca1SAnthony Wilson             unit = "/PowerInputWatts"_json_pointer;
100549c53ac9SJohnathan Mantey         }
100649c53ac9SJohnathan Mantey         else
100749c53ac9SJohnathan Mantey         {
10083929aca1SAnthony Wilson             unit = "/PowerOutputWatts"_json_pointer;
100949c53ac9SJohnathan Mantey         }
10102474adfaSEd Tanous     }
10111abe55efSEd Tanous     else
10121abe55efSEd Tanous     {
101355c7b7a2SEd Tanous         BMCWEB_LOG_ERROR << "Redfish cannot map object type for " << sensorName;
101408777fb0SLewanczyk, Dawid         return;
101508777fb0SLewanczyk, Dawid     }
101608777fb0SLewanczyk, Dawid     // Map of dbus interface name, dbus property name and redfish property_name
10173929aca1SAnthony Wilson     std::vector<
10183929aca1SAnthony Wilson         std::tuple<const char*, const char*, nlohmann::json::json_pointer>>
10193929aca1SAnthony Wilson         properties;
102008777fb0SLewanczyk, Dawid     properties.reserve(7);
102108777fb0SLewanczyk, Dawid 
102208777fb0SLewanczyk, Dawid     properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit);
1023de629b6eSShawn McCarney 
1024a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
10253929aca1SAnthony Wilson     {
10263929aca1SAnthony Wilson         properties.emplace_back(
10273929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh",
10283929aca1SAnthony Wilson             "/Thresholds/UpperCaution/Reading"_json_pointer);
10293929aca1SAnthony Wilson         properties.emplace_back(
10303929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow",
10313929aca1SAnthony Wilson             "/Thresholds/LowerCaution/Reading"_json_pointer);
10323929aca1SAnthony Wilson         properties.emplace_back(
10333929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh",
10343929aca1SAnthony Wilson             "/Thresholds/UpperCritical/Reading"_json_pointer);
10353929aca1SAnthony Wilson         properties.emplace_back(
10363929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow",
10373929aca1SAnthony Wilson             "/Thresholds/LowerCritical/Reading"_json_pointer);
10383929aca1SAnthony Wilson     }
10393929aca1SAnthony Wilson     else if (sensorType != "power")
1040de629b6eSShawn McCarney     {
104108777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
10423929aca1SAnthony Wilson                                 "WarningHigh",
10433929aca1SAnthony Wilson                                 "/UpperThresholdNonCritical"_json_pointer);
104408777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
10453929aca1SAnthony Wilson                                 "WarningLow",
10463929aca1SAnthony Wilson                                 "/LowerThresholdNonCritical"_json_pointer);
104708777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
10483929aca1SAnthony Wilson                                 "CriticalHigh",
10493929aca1SAnthony Wilson                                 "/UpperThresholdCritical"_json_pointer);
105008777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
10513929aca1SAnthony Wilson                                 "CriticalLow",
10523929aca1SAnthony Wilson                                 "/LowerThresholdCritical"_json_pointer);
1053de629b6eSShawn McCarney     }
105408777fb0SLewanczyk, Dawid 
10552474adfaSEd Tanous     // TODO Need to get UpperThresholdFatal and LowerThresholdFatal
10562474adfaSEd Tanous 
1057a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
105895a3ecadSAnthony Wilson     {
105995a3ecadSAnthony Wilson         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
10603929aca1SAnthony Wilson                                 "/ReadingRangeMin"_json_pointer);
106195a3ecadSAnthony Wilson         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
10623929aca1SAnthony Wilson                                 "/ReadingRangeMax"_json_pointer);
106395a3ecadSAnthony Wilson     }
106495a3ecadSAnthony Wilson     else if (sensorType == "temperature")
10651abe55efSEd Tanous     {
106608777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
10673929aca1SAnthony Wilson                                 "/MinReadingRangeTemp"_json_pointer);
106808777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
10693929aca1SAnthony Wilson                                 "/MaxReadingRangeTemp"_json_pointer);
10701abe55efSEd Tanous     }
1071adc4f0dbSShawn McCarney     else if (sensorType != "power")
10721abe55efSEd Tanous     {
107308777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
10743929aca1SAnthony Wilson                                 "/MinReadingRange"_json_pointer);
107508777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
10763929aca1SAnthony Wilson                                 "/MaxReadingRange"_json_pointer);
107708777fb0SLewanczyk, Dawid     }
107808777fb0SLewanczyk, Dawid 
10793929aca1SAnthony Wilson     for (const std::tuple<const char*, const char*,
10803929aca1SAnthony Wilson                           nlohmann::json::json_pointer>& p : properties)
10811abe55efSEd Tanous     {
108255f79e6fSEd Tanous         for (const auto& [interface, values] : interfacesDict)
10831abe55efSEd Tanous         {
1084711ac7a9SEd Tanous             if (interface != std::get<0>(p))
10851abe55efSEd Tanous             {
1086711ac7a9SEd Tanous                 continue;
1087711ac7a9SEd Tanous             }
108855f79e6fSEd Tanous             for (const auto& [valueName, valueVariant] : values)
1089711ac7a9SEd Tanous             {
1090711ac7a9SEd Tanous                 if (valueName != std::get<1>(p))
1091711ac7a9SEd Tanous                 {
1092711ac7a9SEd Tanous                     continue;
1093711ac7a9SEd Tanous                 }
10943929aca1SAnthony Wilson 
10953929aca1SAnthony Wilson                 // The property we want to set may be nested json, so use
10963929aca1SAnthony Wilson                 // a json_pointer for easy indexing into the json structure.
10973929aca1SAnthony Wilson                 const nlohmann::json::json_pointer& key = std::get<2>(p);
10983929aca1SAnthony Wilson 
109908777fb0SLewanczyk, Dawid                 // Attempt to pull the int64 directly
1100abf2add6SEd Tanous                 const int64_t* int64Value = std::get_if<int64_t>(&valueVariant);
110108777fb0SLewanczyk, Dawid 
1102abf2add6SEd Tanous                 const double* doubleValue = std::get_if<double>(&valueVariant);
1103028f7ebcSEddie James                 const uint32_t* uValue = std::get_if<uint32_t>(&valueVariant);
11046f6d0d32SEd Tanous                 double temp = 0.0;
11056f6d0d32SEd Tanous                 if (int64Value != nullptr)
11061abe55efSEd Tanous                 {
1107271584abSEd Tanous                     temp = static_cast<double>(*int64Value);
11086f6d0d32SEd Tanous                 }
11096f6d0d32SEd Tanous                 else if (doubleValue != nullptr)
11101abe55efSEd Tanous                 {
11116f6d0d32SEd Tanous                     temp = *doubleValue;
11121abe55efSEd Tanous                 }
1113028f7ebcSEddie James                 else if (uValue != nullptr)
1114028f7ebcSEddie James                 {
1115028f7ebcSEddie James                     temp = *uValue;
1116028f7ebcSEddie James                 }
11171abe55efSEd Tanous                 else
11181abe55efSEd Tanous                 {
11196f6d0d32SEd Tanous                     BMCWEB_LOG_ERROR
11206f6d0d32SEd Tanous                         << "Got value interface that wasn't int or double";
11216f6d0d32SEd Tanous                     continue;
112208777fb0SLewanczyk, Dawid                 }
11236f6d0d32SEd Tanous                 temp = temp * std::pow(10, scaleMultiplier);
11246f6d0d32SEd Tanous                 if (forceToInt)
11256f6d0d32SEd Tanous                 {
112681ce609eSEd Tanous                     sensorJson[key] = static_cast<int64_t>(temp);
11276f6d0d32SEd Tanous                 }
11286f6d0d32SEd Tanous                 else
11296f6d0d32SEd Tanous                 {
113081ce609eSEd Tanous                     sensorJson[key] = temp;
113108777fb0SLewanczyk, Dawid                 }
113208777fb0SLewanczyk, Dawid             }
113308777fb0SLewanczyk, Dawid         }
113408777fb0SLewanczyk, Dawid     }
1135a0ec28b6SAdrian Ambrożewicz 
113681ce609eSEd Tanous     sensorsAsyncResp->addMetadata(sensorJson, unit.to_string(),
1137a0ec28b6SAdrian Ambrożewicz                                   "/xyz/openbmc_project/sensors/" + sensorType +
1138a0ec28b6SAdrian Ambrożewicz                                       "/" + sensorName);
1139a0ec28b6SAdrian Ambrożewicz 
114055c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "Added sensor " << sensorName;
114108777fb0SLewanczyk, Dawid }
114208777fb0SLewanczyk, Dawid 
1143b5a76932SEd Tanous inline void populateFanRedundancy(
1144b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
11458bd25ccdSJames Feist {
11468bd25ccdSJames Feist     crow::connections::systemBus->async_method_call(
1147b9d36b47SEd Tanous         [sensorsAsyncResp](
1148b9d36b47SEd Tanous             const boost::system::error_code ec,
1149b9d36b47SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& resp) {
11508bd25ccdSJames Feist             if (ec)
11518bd25ccdSJames Feist             {
11528bd25ccdSJames Feist                 return; // don't have to have this interface
11538bd25ccdSJames Feist             }
1154e278c18fSEd Tanous             for (const std::pair<std::string,
1155e278c18fSEd Tanous                                  std::vector<std::pair<
1156e278c18fSEd Tanous                                      std::string, std::vector<std::string>>>>&
1157e278c18fSEd Tanous                      pathPair : resp)
11588bd25ccdSJames Feist             {
1159e278c18fSEd Tanous                 const std::string& path = pathPair.first;
1160e278c18fSEd Tanous                 const std::vector<
1161e278c18fSEd Tanous                     std::pair<std::string, std::vector<std::string>>>& objDict =
1162e278c18fSEd Tanous                     pathPair.second;
11638bd25ccdSJames Feist                 if (objDict.empty())
11648bd25ccdSJames Feist                 {
11658bd25ccdSJames Feist                     continue; // this should be impossible
11668bd25ccdSJames Feist                 }
11678bd25ccdSJames Feist 
11688bd25ccdSJames Feist                 const std::string& owner = objDict.begin()->first;
11691e1e598dSJonathan Doman                 sdbusplus::asio::getProperty<std::vector<std::string>>(
11701e1e598dSJonathan Doman                     *crow::connections::systemBus,
11711e1e598dSJonathan Doman                     "xyz.openbmc_project.ObjectMapper", path + "/chassis",
11721e1e598dSJonathan Doman                     "xyz.openbmc_project.Association", "endpoints",
11731e1e598dSJonathan Doman                     [path, owner, sensorsAsyncResp](
11741e1e598dSJonathan Doman                         const boost::system::error_code e,
11751e1e598dSJonathan Doman                         const std::vector<std::string>& endpoints) {
1176271584abSEd Tanous                         if (e)
11778bd25ccdSJames Feist                         {
11788bd25ccdSJames Feist                             return; // if they don't have an association we
11798bd25ccdSJames Feist                                     // can't tell what chassis is
11808bd25ccdSJames Feist                         }
11818bd25ccdSJames Feist                         auto found = std::find_if(
11821e1e598dSJonathan Doman                             endpoints.begin(), endpoints.end(),
11838bd25ccdSJames Feist                             [sensorsAsyncResp](const std::string& entry) {
11848bd25ccdSJames Feist                                 return entry.find(
11858bd25ccdSJames Feist                                            sensorsAsyncResp->chassisId) !=
11868bd25ccdSJames Feist                                        std::string::npos;
11878bd25ccdSJames Feist                             });
11888bd25ccdSJames Feist 
11891e1e598dSJonathan Doman                         if (found == endpoints.end())
11908bd25ccdSJames Feist                         {
11918bd25ccdSJames Feist                             return;
11928bd25ccdSJames Feist                         }
11938bd25ccdSJames Feist                         crow::connections::systemBus->async_method_call(
11948bd25ccdSJames Feist                             [path, sensorsAsyncResp](
1195271584abSEd Tanous                                 const boost::system::error_code& err,
11968bd25ccdSJames Feist                                 const boost::container::flat_map<
11978bd25ccdSJames Feist                                     std::string,
1198168e20c1SEd Tanous                                     dbus::utility::DbusVariantType>& ret) {
1199271584abSEd Tanous                                 if (err)
12008bd25ccdSJames Feist                                 {
12018bd25ccdSJames Feist                                     return; // don't have to have this
12028bd25ccdSJames Feist                                             // interface
12038bd25ccdSJames Feist                                 }
12048bd25ccdSJames Feist                                 auto findFailures = ret.find("AllowedFailures");
12058bd25ccdSJames Feist                                 auto findCollection = ret.find("Collection");
12068bd25ccdSJames Feist                                 auto findStatus = ret.find("Status");
12078bd25ccdSJames Feist 
12088bd25ccdSJames Feist                                 if (findFailures == ret.end() ||
12098bd25ccdSJames Feist                                     findCollection == ret.end() ||
12108bd25ccdSJames Feist                                     findStatus == ret.end())
12118bd25ccdSJames Feist                                 {
12128bd25ccdSJames Feist                                     BMCWEB_LOG_ERROR
12138bd25ccdSJames Feist                                         << "Invalid redundancy interface";
12148bd25ccdSJames Feist                                     messages::internalError(
12158d1b46d7Szhanghch05                                         sensorsAsyncResp->asyncResp->res);
12168bd25ccdSJames Feist                                     return;
12178bd25ccdSJames Feist                                 }
12188bd25ccdSJames Feist 
12199eb808c1SEd Tanous                                 const uint8_t* allowedFailures =
12209eb808c1SEd Tanous                                     std::get_if<uint8_t>(
12218bd25ccdSJames Feist                                         &(findFailures->second));
12229eb808c1SEd Tanous                                 const std::vector<std::string>* collection =
12238bd25ccdSJames Feist                                     std::get_if<std::vector<std::string>>(
12248bd25ccdSJames Feist                                         &(findCollection->second));
12259eb808c1SEd Tanous                                 const std::string* status =
12269eb808c1SEd Tanous                                     std::get_if<std::string>(
12278bd25ccdSJames Feist                                         &(findStatus->second));
12288bd25ccdSJames Feist 
12298bd25ccdSJames Feist                                 if (allowedFailures == nullptr ||
12308bd25ccdSJames Feist                                     collection == nullptr || status == nullptr)
12318bd25ccdSJames Feist                                 {
12328bd25ccdSJames Feist 
12338bd25ccdSJames Feist                                     BMCWEB_LOG_ERROR
12340fda0f12SGeorge Liu                                         << "Invalid redundancy interface types";
12358bd25ccdSJames Feist                                     messages::internalError(
12368d1b46d7Szhanghch05                                         sensorsAsyncResp->asyncResp->res);
12378bd25ccdSJames Feist                                     return;
12388bd25ccdSJames Feist                                 }
123928aa8de5SGeorge Liu                                 sdbusplus::message::object_path objectPath(
124028aa8de5SGeorge Liu                                     path);
124128aa8de5SGeorge Liu                                 std::string name = objectPath.filename();
124228aa8de5SGeorge Liu                                 if (name.empty())
12438bd25ccdSJames Feist                                 {
12448bd25ccdSJames Feist                                     // this should be impossible
12458bd25ccdSJames Feist                                     messages::internalError(
12468d1b46d7Szhanghch05                                         sensorsAsyncResp->asyncResp->res);
12478bd25ccdSJames Feist                                     return;
12488bd25ccdSJames Feist                                 }
12498bd25ccdSJames Feist                                 std::replace(name.begin(), name.end(), '_',
12508bd25ccdSJames Feist                                              ' ');
12518bd25ccdSJames Feist 
12528bd25ccdSJames Feist                                 std::string health;
12538bd25ccdSJames Feist 
12548bd25ccdSJames Feist                                 if (boost::ends_with(*status, "Full"))
12558bd25ccdSJames Feist                                 {
12568bd25ccdSJames Feist                                     health = "OK";
12578bd25ccdSJames Feist                                 }
12588bd25ccdSJames Feist                                 else if (boost::ends_with(*status, "Degraded"))
12598bd25ccdSJames Feist                                 {
12608bd25ccdSJames Feist                                     health = "Warning";
12618bd25ccdSJames Feist                                 }
12628bd25ccdSJames Feist                                 else
12638bd25ccdSJames Feist                                 {
12648bd25ccdSJames Feist                                     health = "Critical";
12658bd25ccdSJames Feist                                 }
1266*1476687dSEd Tanous                                 nlohmann::json::array_t redfishCollection;
12678bd25ccdSJames Feist                                 const auto& fanRedfish =
12688d1b46d7Szhanghch05                                     sensorsAsyncResp->asyncResp->res
12698d1b46d7Szhanghch05                                         .jsonValue["Fans"];
12708bd25ccdSJames Feist                                 for (const std::string& item : *collection)
12718bd25ccdSJames Feist                                 {
127228aa8de5SGeorge Liu                                     sdbusplus::message::object_path path(item);
127328aa8de5SGeorge Liu                                     std::string itemName = path.filename();
127428aa8de5SGeorge Liu                                     if (itemName.empty())
127528aa8de5SGeorge Liu                                     {
127628aa8de5SGeorge Liu                                         continue;
127728aa8de5SGeorge Liu                                     }
12788bd25ccdSJames Feist                                     /*
12798bd25ccdSJames Feist                                     todo(ed): merge patch that fixes the names
12808bd25ccdSJames Feist                                     std::replace(itemName.begin(),
12818bd25ccdSJames Feist                                                  itemName.end(), '_', ' ');*/
12828bd25ccdSJames Feist                                     auto schemaItem = std::find_if(
12838bd25ccdSJames Feist                                         fanRedfish.begin(), fanRedfish.end(),
12848bd25ccdSJames Feist                                         [itemName](const nlohmann::json& fan) {
12858bd25ccdSJames Feist                                             return fan["MemberId"] == itemName;
12868bd25ccdSJames Feist                                         });
12878bd25ccdSJames Feist                                     if (schemaItem != fanRedfish.end())
12888bd25ccdSJames Feist                                     {
1289*1476687dSEd Tanous                                         nlohmann::json::object_t collection;
1290*1476687dSEd Tanous                                         collection["@odata.id"] =
1291*1476687dSEd Tanous                                             (*schemaItem)["@odata.id"];
1292*1476687dSEd Tanous                                         redfishCollection.emplace_back(
1293*1476687dSEd Tanous                                             std::move(collection));
12948bd25ccdSJames Feist                                     }
12958bd25ccdSJames Feist                                     else
12968bd25ccdSJames Feist                                     {
12978bd25ccdSJames Feist                                         BMCWEB_LOG_ERROR
12988bd25ccdSJames Feist                                             << "failed to find fan in schema";
12998bd25ccdSJames Feist                                         messages::internalError(
13008d1b46d7Szhanghch05                                             sensorsAsyncResp->asyncResp->res);
13018bd25ccdSJames Feist                                         return;
13028bd25ccdSJames Feist                                     }
13038bd25ccdSJames Feist                                 }
13048bd25ccdSJames Feist 
13053e9e72ebSKuiying Wang                                 size_t minNumNeeded =
130626f6976fSEd Tanous                                     collection->empty()
130726f6976fSEd Tanous                                         ? 0
130826f6976fSEd Tanous                                         : collection->size() - *allowedFailures;
1309271584abSEd Tanous                                 nlohmann::json& jResp =
13108d1b46d7Szhanghch05                                     sensorsAsyncResp->asyncResp->res
13118bd25ccdSJames Feist                                         .jsonValue["Redundancy"];
1312*1476687dSEd Tanous 
1313*1476687dSEd Tanous                                 nlohmann::json::object_t redundancy;
1314*1476687dSEd Tanous                                 redundancy["@odata.id"] =
1315717794d5SAppaRao Puli                                     "/redfish/v1/Chassis/" +
13168bd25ccdSJames Feist                                     sensorsAsyncResp->chassisId + "/" +
13178bd25ccdSJames Feist                                     sensorsAsyncResp->chassisSubNode +
13188bd25ccdSJames Feist                                     "#/Redundancy/" +
1319*1476687dSEd Tanous                                     std::to_string(jResp.size());
1320*1476687dSEd Tanous                                 redundancy["@odata.type"] =
1321*1476687dSEd Tanous                                     "#Redundancy.v1_3_2.Redundancy";
1322*1476687dSEd Tanous                                 redundancy["MinNumNeeded"] = minNumNeeded;
1323*1476687dSEd Tanous                                 redundancy["MemberId"] = name;
1324*1476687dSEd Tanous                                 redundancy["Mode"] = "N+m";
1325*1476687dSEd Tanous                                 redundancy["Name"] = name;
1326*1476687dSEd Tanous                                 redundancy["RedundancySet"] = redfishCollection;
1327*1476687dSEd Tanous                                 redundancy["Status"]["Health"] = health;
1328*1476687dSEd Tanous                                 redundancy["Status"]["State"] = "Enabled";
1329*1476687dSEd Tanous 
1330*1476687dSEd Tanous                                 jResp.push_back(std::move(redundancy));
13318bd25ccdSJames Feist                             },
13328bd25ccdSJames Feist                             owner, path, "org.freedesktop.DBus.Properties",
13338bd25ccdSJames Feist                             "GetAll",
13348bd25ccdSJames Feist                             "xyz.openbmc_project.Control.FanRedundancy");
13351e1e598dSJonathan Doman                     });
13368bd25ccdSJames Feist             }
13378bd25ccdSJames Feist         },
13388bd25ccdSJames Feist         "xyz.openbmc_project.ObjectMapper",
13398bd25ccdSJames Feist         "/xyz/openbmc_project/object_mapper",
13408bd25ccdSJames Feist         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
13418bd25ccdSJames Feist         "/xyz/openbmc_project/control", 2,
13428bd25ccdSJames Feist         std::array<const char*, 1>{
13438bd25ccdSJames Feist             "xyz.openbmc_project.Control.FanRedundancy"});
13448bd25ccdSJames Feist }
13458bd25ccdSJames Feist 
1346b5a76932SEd Tanous inline void
134781ce609eSEd Tanous     sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
134849c53ac9SJohnathan Mantey {
13498d1b46d7Szhanghch05     nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue;
135049c53ac9SJohnathan Mantey     std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
135181ce609eSEd Tanous     if (sensorsAsyncResp->chassisSubNode == sensors::node::power)
135249c53ac9SJohnathan Mantey     {
135349c53ac9SJohnathan Mantey         sensorHeaders = {"Voltages", "PowerSupplies"};
135449c53ac9SJohnathan Mantey     }
135549c53ac9SJohnathan Mantey     for (const std::string& sensorGroup : sensorHeaders)
135649c53ac9SJohnathan Mantey     {
135749c53ac9SJohnathan Mantey         nlohmann::json::iterator entry = response.find(sensorGroup);
135849c53ac9SJohnathan Mantey         if (entry != response.end())
135949c53ac9SJohnathan Mantey         {
136049c53ac9SJohnathan Mantey             std::sort(entry->begin(), entry->end(),
136149c53ac9SJohnathan Mantey                       [](nlohmann::json& c1, nlohmann::json& c2) {
136249c53ac9SJohnathan Mantey                           return c1["Name"] < c2["Name"];
136349c53ac9SJohnathan Mantey                       });
136449c53ac9SJohnathan Mantey 
136549c53ac9SJohnathan Mantey             // add the index counts to the end of each entry
136649c53ac9SJohnathan Mantey             size_t count = 0;
136749c53ac9SJohnathan Mantey             for (nlohmann::json& sensorJson : *entry)
136849c53ac9SJohnathan Mantey             {
136949c53ac9SJohnathan Mantey                 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
137049c53ac9SJohnathan Mantey                 if (odata == sensorJson.end())
137149c53ac9SJohnathan Mantey                 {
137249c53ac9SJohnathan Mantey                     continue;
137349c53ac9SJohnathan Mantey                 }
137449c53ac9SJohnathan Mantey                 std::string* value = odata->get_ptr<std::string*>();
137549c53ac9SJohnathan Mantey                 if (value != nullptr)
137649c53ac9SJohnathan Mantey                 {
137749c53ac9SJohnathan Mantey                     *value += std::to_string(count);
137849c53ac9SJohnathan Mantey                     count++;
137981ce609eSEd Tanous                     sensorsAsyncResp->updateUri(sensorJson["Name"], *value);
138049c53ac9SJohnathan Mantey                 }
138149c53ac9SJohnathan Mantey             }
138249c53ac9SJohnathan Mantey         }
138349c53ac9SJohnathan Mantey     }
138449c53ac9SJohnathan Mantey }
138549c53ac9SJohnathan Mantey 
138608777fb0SLewanczyk, Dawid /**
1387adc4f0dbSShawn McCarney  * @brief Finds the inventory item with the specified object path.
1388adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1389adc4f0dbSShawn McCarney  * @param invItemObjPath D-Bus object path of inventory item.
1390adc4f0dbSShawn McCarney  * @return Inventory item within vector, or nullptr if no match found.
13918fb49dd6SShawn McCarney  */
139223a21a1cSEd Tanous inline InventoryItem* findInventoryItem(
1393b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1394adc4f0dbSShawn McCarney     const std::string& invItemObjPath)
13958fb49dd6SShawn McCarney {
1396adc4f0dbSShawn McCarney     for (InventoryItem& inventoryItem : *inventoryItems)
13978fb49dd6SShawn McCarney     {
1398adc4f0dbSShawn McCarney         if (inventoryItem.objectPath == invItemObjPath)
13998fb49dd6SShawn McCarney         {
1400adc4f0dbSShawn McCarney             return &inventoryItem;
14018fb49dd6SShawn McCarney         }
14028fb49dd6SShawn McCarney     }
14038fb49dd6SShawn McCarney     return nullptr;
14048fb49dd6SShawn McCarney }
14058fb49dd6SShawn McCarney 
14068fb49dd6SShawn McCarney /**
1407adc4f0dbSShawn McCarney  * @brief Finds the inventory item associated with the specified sensor.
1408adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1409adc4f0dbSShawn McCarney  * @param sensorObjPath D-Bus object path of sensor.
1410adc4f0dbSShawn McCarney  * @return Inventory item within vector, or nullptr if no match found.
14118fb49dd6SShawn McCarney  */
141223a21a1cSEd Tanous inline InventoryItem* findInventoryItemForSensor(
1413b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1414adc4f0dbSShawn McCarney     const std::string& sensorObjPath)
1415adc4f0dbSShawn McCarney {
1416adc4f0dbSShawn McCarney     for (InventoryItem& inventoryItem : *inventoryItems)
1417adc4f0dbSShawn McCarney     {
1418adc4f0dbSShawn McCarney         if (inventoryItem.sensors.count(sensorObjPath) > 0)
1419adc4f0dbSShawn McCarney         {
1420adc4f0dbSShawn McCarney             return &inventoryItem;
1421adc4f0dbSShawn McCarney         }
1422adc4f0dbSShawn McCarney     }
1423adc4f0dbSShawn McCarney     return nullptr;
1424adc4f0dbSShawn McCarney }
1425adc4f0dbSShawn McCarney 
1426adc4f0dbSShawn McCarney /**
1427d500549bSAnthony Wilson  * @brief Finds the inventory item associated with the specified led path.
1428d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1429d500549bSAnthony Wilson  * @param ledObjPath D-Bus object path of led.
1430d500549bSAnthony Wilson  * @return Inventory item within vector, or nullptr if no match found.
1431d500549bSAnthony Wilson  */
1432d500549bSAnthony Wilson inline InventoryItem*
1433d500549bSAnthony Wilson     findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems,
1434d500549bSAnthony Wilson                             const std::string& ledObjPath)
1435d500549bSAnthony Wilson {
1436d500549bSAnthony Wilson     for (InventoryItem& inventoryItem : inventoryItems)
1437d500549bSAnthony Wilson     {
1438d500549bSAnthony Wilson         if (inventoryItem.ledObjectPath == ledObjPath)
1439d500549bSAnthony Wilson         {
1440d500549bSAnthony Wilson             return &inventoryItem;
1441d500549bSAnthony Wilson         }
1442d500549bSAnthony Wilson     }
1443d500549bSAnthony Wilson     return nullptr;
1444d500549bSAnthony Wilson }
1445d500549bSAnthony Wilson 
1446d500549bSAnthony Wilson /**
1447adc4f0dbSShawn McCarney  * @brief Adds inventory item and associated sensor to specified vector.
1448adc4f0dbSShawn McCarney  *
1449adc4f0dbSShawn McCarney  * Adds a new InventoryItem to the vector if necessary.  Searches for an
1450adc4f0dbSShawn McCarney  * existing InventoryItem with the specified object path.  If not found, one is
1451adc4f0dbSShawn McCarney  * added to the vector.
1452adc4f0dbSShawn McCarney  *
1453adc4f0dbSShawn McCarney  * Next, the specified sensor is added to the set of sensors associated with the
1454adc4f0dbSShawn McCarney  * InventoryItem.
1455adc4f0dbSShawn McCarney  *
1456adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1457adc4f0dbSShawn McCarney  * @param invItemObjPath D-Bus object path of inventory item.
1458adc4f0dbSShawn McCarney  * @param sensorObjPath D-Bus object path of sensor
1459adc4f0dbSShawn McCarney  */
1460b5a76932SEd Tanous inline void addInventoryItem(
1461b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1462b5a76932SEd Tanous     const std::string& invItemObjPath, const std::string& sensorObjPath)
1463adc4f0dbSShawn McCarney {
1464adc4f0dbSShawn McCarney     // Look for inventory item in vector
1465adc4f0dbSShawn McCarney     InventoryItem* inventoryItem =
1466adc4f0dbSShawn McCarney         findInventoryItem(inventoryItems, invItemObjPath);
1467adc4f0dbSShawn McCarney 
1468adc4f0dbSShawn McCarney     // If inventory item doesn't exist in vector, add it
1469adc4f0dbSShawn McCarney     if (inventoryItem == nullptr)
1470adc4f0dbSShawn McCarney     {
1471adc4f0dbSShawn McCarney         inventoryItems->emplace_back(invItemObjPath);
1472adc4f0dbSShawn McCarney         inventoryItem = &(inventoryItems->back());
1473adc4f0dbSShawn McCarney     }
1474adc4f0dbSShawn McCarney 
1475adc4f0dbSShawn McCarney     // Add sensor to set of sensors associated with inventory item
1476adc4f0dbSShawn McCarney     inventoryItem->sensors.emplace(sensorObjPath);
1477adc4f0dbSShawn McCarney }
1478adc4f0dbSShawn McCarney 
1479adc4f0dbSShawn McCarney /**
1480adc4f0dbSShawn McCarney  * @brief Stores D-Bus data in the specified inventory item.
1481adc4f0dbSShawn McCarney  *
1482adc4f0dbSShawn McCarney  * Finds D-Bus data in the specified map of interfaces.  Stores the data in the
1483adc4f0dbSShawn McCarney  * specified InventoryItem.
1484adc4f0dbSShawn McCarney  *
1485adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
1486adc4f0dbSShawn McCarney  * response.
1487adc4f0dbSShawn McCarney  *
1488adc4f0dbSShawn McCarney  * @param inventoryItem Inventory item where data will be stored.
1489adc4f0dbSShawn McCarney  * @param interfacesDict Map containing D-Bus interfaces and their properties
1490adc4f0dbSShawn McCarney  * for the specified inventory item.
1491adc4f0dbSShawn McCarney  */
149223a21a1cSEd Tanous inline void storeInventoryItemData(
1493adc4f0dbSShawn McCarney     InventoryItem& inventoryItem,
1494711ac7a9SEd Tanous     const dbus::utility::DBusInteracesMap& interfacesDict)
14958fb49dd6SShawn McCarney {
1496adc4f0dbSShawn McCarney     // Get properties from Inventory.Item interface
1497711ac7a9SEd Tanous 
14989eb808c1SEd Tanous     for (const auto& [interface, values] : interfacesDict)
14998fb49dd6SShawn McCarney     {
1500711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Item")
15018fb49dd6SShawn McCarney         {
15029eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
1503711ac7a9SEd Tanous             {
1504711ac7a9SEd Tanous                 if (name == "Present")
1505711ac7a9SEd Tanous                 {
1506711ac7a9SEd Tanous                     const bool* value = std::get_if<bool>(&dbusValue);
1507adc4f0dbSShawn McCarney                     if (value != nullptr)
15088fb49dd6SShawn McCarney                     {
1509adc4f0dbSShawn McCarney                         inventoryItem.isPresent = *value;
15108fb49dd6SShawn McCarney                     }
15118fb49dd6SShawn McCarney                 }
15128fb49dd6SShawn McCarney             }
1513711ac7a9SEd Tanous         }
1514adc4f0dbSShawn McCarney         // Check if Inventory.Item.PowerSupply interface is present
1515711ac7a9SEd Tanous 
1516711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Item.PowerSupply")
15178fb49dd6SShawn McCarney         {
1518adc4f0dbSShawn McCarney             inventoryItem.isPowerSupply = true;
15198fb49dd6SShawn McCarney         }
1520adc4f0dbSShawn McCarney 
1521adc4f0dbSShawn McCarney         // Get properties from Inventory.Decorator.Asset interface
1522711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
1523adc4f0dbSShawn McCarney         {
15249eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
1525711ac7a9SEd Tanous             {
1526711ac7a9SEd Tanous                 if (name == "Manufacturer")
1527adc4f0dbSShawn McCarney                 {
1528adc4f0dbSShawn McCarney                     const std::string* value =
1529711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
1530adc4f0dbSShawn McCarney                     if (value != nullptr)
1531adc4f0dbSShawn McCarney                     {
1532adc4f0dbSShawn McCarney                         inventoryItem.manufacturer = *value;
1533adc4f0dbSShawn McCarney                     }
1534adc4f0dbSShawn McCarney                 }
1535711ac7a9SEd Tanous                 if (name == "Model")
1536adc4f0dbSShawn McCarney                 {
1537adc4f0dbSShawn McCarney                     const std::string* value =
1538711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
1539adc4f0dbSShawn McCarney                     if (value != nullptr)
1540adc4f0dbSShawn McCarney                     {
1541adc4f0dbSShawn McCarney                         inventoryItem.model = *value;
1542adc4f0dbSShawn McCarney                     }
1543adc4f0dbSShawn McCarney                 }
1544711ac7a9SEd Tanous                 if (name == "SerialNumber")
1545adc4f0dbSShawn McCarney                 {
1546adc4f0dbSShawn McCarney                     const std::string* value =
1547711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
1548adc4f0dbSShawn McCarney                     if (value != nullptr)
1549adc4f0dbSShawn McCarney                     {
1550adc4f0dbSShawn McCarney                         inventoryItem.serialNumber = *value;
1551adc4f0dbSShawn McCarney                     }
1552adc4f0dbSShawn McCarney                 }
1553711ac7a9SEd Tanous                 if (name == "PartNumber")
1554711ac7a9SEd Tanous                 {
1555711ac7a9SEd Tanous                     const std::string* value =
1556711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
1557711ac7a9SEd Tanous                     if (value != nullptr)
1558711ac7a9SEd Tanous                     {
1559711ac7a9SEd Tanous                         inventoryItem.partNumber = *value;
1560711ac7a9SEd Tanous                     }
1561711ac7a9SEd Tanous                 }
1562711ac7a9SEd Tanous             }
1563adc4f0dbSShawn McCarney         }
1564adc4f0dbSShawn McCarney 
1565711ac7a9SEd Tanous         if (interface ==
1566711ac7a9SEd Tanous             "xyz.openbmc_project.State.Decorator.OperationalStatus")
1567adc4f0dbSShawn McCarney         {
15689eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
1569adc4f0dbSShawn McCarney             {
1570711ac7a9SEd Tanous                 if (name == "Functional")
1571711ac7a9SEd Tanous                 {
1572711ac7a9SEd Tanous                     const bool* value = std::get_if<bool>(&dbusValue);
1573adc4f0dbSShawn McCarney                     if (value != nullptr)
1574adc4f0dbSShawn McCarney                     {
1575adc4f0dbSShawn McCarney                         inventoryItem.isFunctional = *value;
15768fb49dd6SShawn McCarney                     }
15778fb49dd6SShawn McCarney                 }
15788fb49dd6SShawn McCarney             }
15798fb49dd6SShawn McCarney         }
1580711ac7a9SEd Tanous     }
1581711ac7a9SEd Tanous }
15828fb49dd6SShawn McCarney 
15838fb49dd6SShawn McCarney /**
1584adc4f0dbSShawn McCarney  * @brief Gets D-Bus data for inventory items associated with sensors.
15858fb49dd6SShawn McCarney  *
1586adc4f0dbSShawn McCarney  * Uses the specified connections (services) to obtain D-Bus data for inventory
1587adc4f0dbSShawn McCarney  * items associated with sensors.  Stores the resulting data in the
1588adc4f0dbSShawn McCarney  * inventoryItems vector.
15898fb49dd6SShawn McCarney  *
1590adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
1591adc4f0dbSShawn McCarney  * response.
1592adc4f0dbSShawn McCarney  *
1593adc4f0dbSShawn McCarney  * Finds the inventory item data asynchronously.  Invokes callback when data has
1594adc4f0dbSShawn McCarney  * been obtained.
1595adc4f0dbSShawn McCarney  *
1596adc4f0dbSShawn McCarney  * The callback must have the following signature:
1597adc4f0dbSShawn McCarney  *   @code
1598d500549bSAnthony Wilson  *   callback(void)
1599adc4f0dbSShawn McCarney  *   @endcode
1600adc4f0dbSShawn McCarney  *
1601adc4f0dbSShawn McCarney  * This function is called recursively, obtaining data asynchronously from one
1602adc4f0dbSShawn McCarney  * connection in each call.  This ensures the callback is not invoked until the
1603adc4f0dbSShawn McCarney  * last asynchronous function has completed.
16048fb49dd6SShawn McCarney  *
16058fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
1606adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1607adc4f0dbSShawn McCarney  * @param invConnections Connections that provide data for the inventory items.
16088fb49dd6SShawn McCarney  * @param objectMgrPaths Mappings from connection name to DBus object path that
16098fb49dd6SShawn McCarney  * implements ObjectManager.
1610adc4f0dbSShawn McCarney  * @param callback Callback to invoke when inventory data has been obtained.
1611adc4f0dbSShawn McCarney  * @param invConnectionsIndex Current index in invConnections.  Only specified
1612adc4f0dbSShawn McCarney  * in recursive calls to this function.
16138fb49dd6SShawn McCarney  */
1614adc4f0dbSShawn McCarney template <typename Callback>
1615adc4f0dbSShawn McCarney static void getInventoryItemsData(
16168fb49dd6SShawn McCarney     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1617adc4f0dbSShawn McCarney     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
16188fb49dd6SShawn McCarney     std::shared_ptr<boost::container::flat_set<std::string>> invConnections,
16198fb49dd6SShawn McCarney     std::shared_ptr<boost::container::flat_map<std::string, std::string>>
1620adc4f0dbSShawn McCarney         objectMgrPaths,
1621271584abSEd Tanous     Callback&& callback, size_t invConnectionsIndex = 0)
16228fb49dd6SShawn McCarney {
1623adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsData enter";
16248fb49dd6SShawn McCarney 
1625adc4f0dbSShawn McCarney     // If no more connections left, call callback
1626adc4f0dbSShawn McCarney     if (invConnectionsIndex >= invConnections->size())
16278fb49dd6SShawn McCarney     {
1628d500549bSAnthony Wilson         callback();
1629adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
1630adc4f0dbSShawn McCarney         return;
1631adc4f0dbSShawn McCarney     }
1632adc4f0dbSShawn McCarney 
1633adc4f0dbSShawn McCarney     // Get inventory item data from current connection
1634adc4f0dbSShawn McCarney     auto it = invConnections->nth(invConnectionsIndex);
1635adc4f0dbSShawn McCarney     if (it != invConnections->end())
1636adc4f0dbSShawn McCarney     {
1637adc4f0dbSShawn McCarney         const std::string& invConnection = *it;
1638adc4f0dbSShawn McCarney 
16398fb49dd6SShawn McCarney         // Response handler for GetManagedObjects
1640adc4f0dbSShawn McCarney         auto respHandler = [sensorsAsyncResp, inventoryItems, invConnections,
1641f94c4ecfSEd Tanous                             objectMgrPaths,
1642f94c4ecfSEd Tanous                             callback{std::forward<Callback>(callback)},
1643adc4f0dbSShawn McCarney                             invConnectionsIndex](
1644adc4f0dbSShawn McCarney                                const boost::system::error_code ec,
1645711ac7a9SEd Tanous                                dbus::utility::ManagedObjectType& resp) {
1646adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler enter";
16478fb49dd6SShawn McCarney             if (ec)
16488fb49dd6SShawn McCarney             {
16498fb49dd6SShawn McCarney                 BMCWEB_LOG_ERROR
1650adc4f0dbSShawn McCarney                     << "getInventoryItemsData respHandler DBus error " << ec;
16518d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
16528fb49dd6SShawn McCarney                 return;
16538fb49dd6SShawn McCarney             }
16548fb49dd6SShawn McCarney 
16558fb49dd6SShawn McCarney             // Loop through returned object paths
16568fb49dd6SShawn McCarney             for (const auto& objDictEntry : resp)
16578fb49dd6SShawn McCarney             {
16588fb49dd6SShawn McCarney                 const std::string& objPath =
16598fb49dd6SShawn McCarney                     static_cast<const std::string&>(objDictEntry.first);
16608fb49dd6SShawn McCarney 
1661adc4f0dbSShawn McCarney                 // If this object path is one of the specified inventory items
1662adc4f0dbSShawn McCarney                 InventoryItem* inventoryItem =
1663adc4f0dbSShawn McCarney                     findInventoryItem(inventoryItems, objPath);
1664adc4f0dbSShawn McCarney                 if (inventoryItem != nullptr)
16658fb49dd6SShawn McCarney                 {
1666adc4f0dbSShawn McCarney                     // Store inventory data in InventoryItem
1667adc4f0dbSShawn McCarney                     storeInventoryItemData(*inventoryItem, objDictEntry.second);
16688fb49dd6SShawn McCarney                 }
16698fb49dd6SShawn McCarney             }
16708fb49dd6SShawn McCarney 
1671adc4f0dbSShawn McCarney             // Recurse to get inventory item data from next connection
1672adc4f0dbSShawn McCarney             getInventoryItemsData(sensorsAsyncResp, inventoryItems,
1673adc4f0dbSShawn McCarney                                   invConnections, objectMgrPaths,
1674adc4f0dbSShawn McCarney                                   std::move(callback), invConnectionsIndex + 1);
1675adc4f0dbSShawn McCarney 
1676adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler exit";
16778fb49dd6SShawn McCarney         };
16788fb49dd6SShawn McCarney 
16798fb49dd6SShawn McCarney         // Find DBus object path that implements ObjectManager for the current
16808fb49dd6SShawn McCarney         // connection.  If no mapping found, default to "/".
16818fb49dd6SShawn McCarney         auto iter = objectMgrPaths->find(invConnection);
16828fb49dd6SShawn McCarney         const std::string& objectMgrPath =
16838fb49dd6SShawn McCarney             (iter != objectMgrPaths->end()) ? iter->second : "/";
16848fb49dd6SShawn McCarney         BMCWEB_LOG_DEBUG << "ObjectManager path for " << invConnection << " is "
16858fb49dd6SShawn McCarney                          << objectMgrPath;
16868fb49dd6SShawn McCarney 
16878fb49dd6SShawn McCarney         // Get all object paths and their interfaces for current connection
16888fb49dd6SShawn McCarney         crow::connections::systemBus->async_method_call(
16898fb49dd6SShawn McCarney             std::move(respHandler), invConnection, objectMgrPath,
16908fb49dd6SShawn McCarney             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
16918fb49dd6SShawn McCarney     }
16928fb49dd6SShawn McCarney 
1693adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
16948fb49dd6SShawn McCarney }
16958fb49dd6SShawn McCarney 
16968fb49dd6SShawn McCarney /**
1697adc4f0dbSShawn McCarney  * @brief Gets connections that provide D-Bus data for inventory items.
16988fb49dd6SShawn McCarney  *
1699adc4f0dbSShawn McCarney  * Gets the D-Bus connections (services) that provide data for the inventory
1700adc4f0dbSShawn McCarney  * items that are associated with sensors.
17018fb49dd6SShawn McCarney  *
17028fb49dd6SShawn McCarney  * Finds the connections asynchronously.  Invokes callback when information has
17038fb49dd6SShawn McCarney  * been obtained.
17048fb49dd6SShawn McCarney  *
17058fb49dd6SShawn McCarney  * The callback must have the following signature:
17068fb49dd6SShawn McCarney  *   @code
17078fb49dd6SShawn McCarney  *   callback(std::shared_ptr<boost::container::flat_set<std::string>>
17088fb49dd6SShawn McCarney  *            invConnections)
17098fb49dd6SShawn McCarney  *   @endcode
17108fb49dd6SShawn McCarney  *
17118fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
1712adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
17138fb49dd6SShawn McCarney  * @param callback Callback to invoke when connections have been obtained.
17148fb49dd6SShawn McCarney  */
17158fb49dd6SShawn McCarney template <typename Callback>
17168fb49dd6SShawn McCarney static void getInventoryItemsConnections(
1717b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1718b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
17198fb49dd6SShawn McCarney     Callback&& callback)
17208fb49dd6SShawn McCarney {
17218fb49dd6SShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsConnections enter";
17228fb49dd6SShawn McCarney 
17238fb49dd6SShawn McCarney     const std::string path = "/xyz/openbmc_project/inventory";
1724adc4f0dbSShawn McCarney     const std::array<std::string, 4> interfaces = {
17258fb49dd6SShawn McCarney         "xyz.openbmc_project.Inventory.Item",
1726adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Item.PowerSupply",
1727adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Decorator.Asset",
17288fb49dd6SShawn McCarney         "xyz.openbmc_project.State.Decorator.OperationalStatus"};
17298fb49dd6SShawn McCarney 
17308fb49dd6SShawn McCarney     // Response handler for parsing output from GetSubTree
1731f94c4ecfSEd Tanous     auto respHandler = [callback{std::forward<Callback>(callback)},
1732b9d36b47SEd Tanous                         sensorsAsyncResp, inventoryItems](
1733b9d36b47SEd Tanous                            const boost::system::error_code ec,
1734b9d36b47SEd Tanous                            const dbus::utility::MapperGetSubTreeResponse&
1735b9d36b47SEd Tanous                                subtree) {
17368fb49dd6SShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler enter";
17378fb49dd6SShawn McCarney         if (ec)
17388fb49dd6SShawn McCarney         {
17398d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
17408fb49dd6SShawn McCarney             BMCWEB_LOG_ERROR
17418fb49dd6SShawn McCarney                 << "getInventoryItemsConnections respHandler DBus error " << ec;
17428fb49dd6SShawn McCarney             return;
17438fb49dd6SShawn McCarney         }
17448fb49dd6SShawn McCarney 
17458fb49dd6SShawn McCarney         // Make unique list of connections for desired inventory items
17468fb49dd6SShawn McCarney         std::shared_ptr<boost::container::flat_set<std::string>>
17478fb49dd6SShawn McCarney             invConnections =
17488fb49dd6SShawn McCarney                 std::make_shared<boost::container::flat_set<std::string>>();
17498fb49dd6SShawn McCarney         invConnections->reserve(8);
17508fb49dd6SShawn McCarney 
17518fb49dd6SShawn McCarney         // Loop through objects from GetSubTree
17528fb49dd6SShawn McCarney         for (const std::pair<
17538fb49dd6SShawn McCarney                  std::string,
17548fb49dd6SShawn McCarney                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
17558fb49dd6SShawn McCarney                  object : subtree)
17568fb49dd6SShawn McCarney         {
1757adc4f0dbSShawn McCarney             // Check if object path is one of the specified inventory items
17588fb49dd6SShawn McCarney             const std::string& objPath = object.first;
1759adc4f0dbSShawn McCarney             if (findInventoryItem(inventoryItems, objPath) != nullptr)
17608fb49dd6SShawn McCarney             {
17618fb49dd6SShawn McCarney                 // Store all connections to inventory item
17628fb49dd6SShawn McCarney                 for (const std::pair<std::string, std::vector<std::string>>&
17638fb49dd6SShawn McCarney                          objData : object.second)
17648fb49dd6SShawn McCarney                 {
17658fb49dd6SShawn McCarney                     const std::string& invConnection = objData.first;
17668fb49dd6SShawn McCarney                     invConnections->insert(invConnection);
17678fb49dd6SShawn McCarney                 }
17688fb49dd6SShawn McCarney             }
17698fb49dd6SShawn McCarney         }
1770d500549bSAnthony Wilson 
17718fb49dd6SShawn McCarney         callback(invConnections);
17728fb49dd6SShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler exit";
17738fb49dd6SShawn McCarney     };
17748fb49dd6SShawn McCarney 
17758fb49dd6SShawn McCarney     // Make call to ObjectMapper to find all inventory items
17768fb49dd6SShawn McCarney     crow::connections::systemBus->async_method_call(
17778fb49dd6SShawn McCarney         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
17788fb49dd6SShawn McCarney         "/xyz/openbmc_project/object_mapper",
17798fb49dd6SShawn McCarney         "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
17808fb49dd6SShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsConnections exit";
17818fb49dd6SShawn McCarney }
17828fb49dd6SShawn McCarney 
17838fb49dd6SShawn McCarney /**
1784adc4f0dbSShawn McCarney  * @brief Gets associations from sensors to inventory items.
17858fb49dd6SShawn McCarney  *
17868fb49dd6SShawn McCarney  * Looks for ObjectMapper associations from the specified sensors to related
1787d500549bSAnthony Wilson  * inventory items. Then finds the associations from those inventory items to
1788d500549bSAnthony Wilson  * their LEDs, if any.
17898fb49dd6SShawn McCarney  *
17908fb49dd6SShawn McCarney  * Finds the inventory items asynchronously.  Invokes callback when information
17918fb49dd6SShawn McCarney  * has been obtained.
17928fb49dd6SShawn McCarney  *
17938fb49dd6SShawn McCarney  * The callback must have the following signature:
17948fb49dd6SShawn McCarney  *   @code
1795adc4f0dbSShawn McCarney  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
17968fb49dd6SShawn McCarney  *   @endcode
17978fb49dd6SShawn McCarney  *
17988fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
17998fb49dd6SShawn McCarney  * @param sensorNames All sensors within the current chassis.
18008fb49dd6SShawn McCarney  * @param objectMgrPaths Mappings from connection name to DBus object path that
18018fb49dd6SShawn McCarney  * implements ObjectManager.
18028fb49dd6SShawn McCarney  * @param callback Callback to invoke when inventory items have been obtained.
18038fb49dd6SShawn McCarney  */
18048fb49dd6SShawn McCarney template <typename Callback>
1805adc4f0dbSShawn McCarney static void getInventoryItemAssociations(
1806b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1807b5a76932SEd Tanous     const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames,
1808b5a76932SEd Tanous     const std::shared_ptr<boost::container::flat_map<std::string, std::string>>&
18098fb49dd6SShawn McCarney         objectMgrPaths,
18108fb49dd6SShawn McCarney     Callback&& callback)
18118fb49dd6SShawn McCarney {
1812adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemAssociations enter";
18138fb49dd6SShawn McCarney 
18148fb49dd6SShawn McCarney     // Response handler for GetManagedObjects
1815f94c4ecfSEd Tanous     auto respHandler = [callback{std::forward<Callback>(callback)},
1816f94c4ecfSEd Tanous                         sensorsAsyncResp,
18178fb49dd6SShawn McCarney                         sensorNames](const boost::system::error_code ec,
18188fb49dd6SShawn McCarney                                      dbus::utility::ManagedObjectType& resp) {
1819adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler enter";
18208fb49dd6SShawn McCarney         if (ec)
18218fb49dd6SShawn McCarney         {
1822adc4f0dbSShawn McCarney             BMCWEB_LOG_ERROR
1823adc4f0dbSShawn McCarney                 << "getInventoryItemAssociations respHandler DBus error " << ec;
18248d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
18258fb49dd6SShawn McCarney             return;
18268fb49dd6SShawn McCarney         }
18278fb49dd6SShawn McCarney 
1828adc4f0dbSShawn McCarney         // Create vector to hold list of inventory items
1829adc4f0dbSShawn McCarney         std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1830adc4f0dbSShawn McCarney             std::make_shared<std::vector<InventoryItem>>();
1831adc4f0dbSShawn McCarney 
18328fb49dd6SShawn McCarney         // Loop through returned object paths
18338fb49dd6SShawn McCarney         std::string sensorAssocPath;
18348fb49dd6SShawn McCarney         sensorAssocPath.reserve(128); // avoid memory allocations
18358fb49dd6SShawn McCarney         for (const auto& objDictEntry : resp)
18368fb49dd6SShawn McCarney         {
18378fb49dd6SShawn McCarney             const std::string& objPath =
18388fb49dd6SShawn McCarney                 static_cast<const std::string&>(objDictEntry.first);
18398fb49dd6SShawn McCarney 
18408fb49dd6SShawn McCarney             // If path is inventory association for one of the specified sensors
18418fb49dd6SShawn McCarney             for (const std::string& sensorName : *sensorNames)
18428fb49dd6SShawn McCarney             {
18438fb49dd6SShawn McCarney                 sensorAssocPath = sensorName;
18448fb49dd6SShawn McCarney                 sensorAssocPath += "/inventory";
18458fb49dd6SShawn McCarney                 if (objPath == sensorAssocPath)
18468fb49dd6SShawn McCarney                 {
18478fb49dd6SShawn McCarney                     // Get Association interface for object path
1848711ac7a9SEd Tanous                     for (const auto& [interface, values] : objDictEntry.second)
18498fb49dd6SShawn McCarney                     {
1850711ac7a9SEd Tanous                         if (interface == "xyz.openbmc_project.Association")
1851711ac7a9SEd Tanous                         {
1852711ac7a9SEd Tanous                             for (const auto& [valueName, value] : values)
1853711ac7a9SEd Tanous                             {
1854711ac7a9SEd Tanous                                 if (valueName == "endpoints")
18558fb49dd6SShawn McCarney                                 {
18568fb49dd6SShawn McCarney                                     const std::vector<std::string>* endpoints =
18578fb49dd6SShawn McCarney                                         std::get_if<std::vector<std::string>>(
1858711ac7a9SEd Tanous                                             &value);
1859711ac7a9SEd Tanous                                     if ((endpoints != nullptr) &&
1860711ac7a9SEd Tanous                                         !endpoints->empty())
18618fb49dd6SShawn McCarney                                     {
1862adc4f0dbSShawn McCarney                                         // Add inventory item to vector
1863adc4f0dbSShawn McCarney                                         const std::string& invItemPath =
1864adc4f0dbSShawn McCarney                                             endpoints->front();
1865711ac7a9SEd Tanous                                         addInventoryItem(inventoryItems,
1866711ac7a9SEd Tanous                                                          invItemPath,
1867adc4f0dbSShawn McCarney                                                          sensorName);
18688fb49dd6SShawn McCarney                                     }
18698fb49dd6SShawn McCarney                                 }
18708fb49dd6SShawn McCarney                             }
1871711ac7a9SEd Tanous                         }
1872711ac7a9SEd Tanous                     }
18738fb49dd6SShawn McCarney                     break;
18748fb49dd6SShawn McCarney                 }
18758fb49dd6SShawn McCarney             }
18768fb49dd6SShawn McCarney         }
18778fb49dd6SShawn McCarney 
1878d500549bSAnthony Wilson         // Now loop through the returned object paths again, this time to
1879d500549bSAnthony Wilson         // find the leds associated with the inventory items we just found
1880d500549bSAnthony Wilson         std::string inventoryAssocPath;
1881d500549bSAnthony Wilson         inventoryAssocPath.reserve(128); // avoid memory allocations
1882d500549bSAnthony Wilson         for (const auto& objDictEntry : resp)
1883d500549bSAnthony Wilson         {
1884d500549bSAnthony Wilson             const std::string& objPath =
1885d500549bSAnthony Wilson                 static_cast<const std::string&>(objDictEntry.first);
1886d500549bSAnthony Wilson 
1887d500549bSAnthony Wilson             for (InventoryItem& inventoryItem : *inventoryItems)
1888d500549bSAnthony Wilson             {
1889d500549bSAnthony Wilson                 inventoryAssocPath = inventoryItem.objectPath;
1890d500549bSAnthony Wilson                 inventoryAssocPath += "/leds";
1891d500549bSAnthony Wilson                 if (objPath == inventoryAssocPath)
1892d500549bSAnthony Wilson                 {
1893711ac7a9SEd Tanous                     for (const auto& [interface, values] : objDictEntry.second)
1894d500549bSAnthony Wilson                     {
1895711ac7a9SEd Tanous                         if (interface == "xyz.openbmc_project.Association")
1896711ac7a9SEd Tanous                         {
1897711ac7a9SEd Tanous                             for (const auto& [valueName, value] : values)
1898711ac7a9SEd Tanous                             {
1899711ac7a9SEd Tanous                                 if (valueName == "endpoints")
1900d500549bSAnthony Wilson                                 {
1901d500549bSAnthony Wilson                                     const std::vector<std::string>* endpoints =
1902d500549bSAnthony Wilson                                         std::get_if<std::vector<std::string>>(
1903711ac7a9SEd Tanous                                             &value);
1904711ac7a9SEd Tanous                                     if ((endpoints != nullptr) &&
1905711ac7a9SEd Tanous                                         !endpoints->empty())
1906d500549bSAnthony Wilson                                     {
1907711ac7a9SEd Tanous                                         // Add inventory item to vector
1908d500549bSAnthony Wilson                                         // Store LED path in inventory item
1909711ac7a9SEd Tanous                                         const std::string& ledPath =
1910711ac7a9SEd Tanous                                             endpoints->front();
1911d500549bSAnthony Wilson                                         inventoryItem.ledObjectPath = ledPath;
1912d500549bSAnthony Wilson                                     }
1913d500549bSAnthony Wilson                                 }
1914d500549bSAnthony Wilson                             }
1915711ac7a9SEd Tanous                         }
1916711ac7a9SEd Tanous                     }
1917711ac7a9SEd Tanous 
1918d500549bSAnthony Wilson                     break;
1919d500549bSAnthony Wilson                 }
1920d500549bSAnthony Wilson             }
1921d500549bSAnthony Wilson         }
1922adc4f0dbSShawn McCarney         callback(inventoryItems);
1923adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler exit";
19248fb49dd6SShawn McCarney     };
19258fb49dd6SShawn McCarney 
19268fb49dd6SShawn McCarney     // Find DBus object path that implements ObjectManager for ObjectMapper
19278fb49dd6SShawn McCarney     std::string connection = "xyz.openbmc_project.ObjectMapper";
19288fb49dd6SShawn McCarney     auto iter = objectMgrPaths->find(connection);
19298fb49dd6SShawn McCarney     const std::string& objectMgrPath =
19308fb49dd6SShawn McCarney         (iter != objectMgrPaths->end()) ? iter->second : "/";
19318fb49dd6SShawn McCarney     BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
19328fb49dd6SShawn McCarney                      << objectMgrPath;
19338fb49dd6SShawn McCarney 
19348fb49dd6SShawn McCarney     // Call GetManagedObjects on the ObjectMapper to get all associations
19358fb49dd6SShawn McCarney     crow::connections::systemBus->async_method_call(
19368fb49dd6SShawn McCarney         std::move(respHandler), connection, objectMgrPath,
19378fb49dd6SShawn McCarney         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
19388fb49dd6SShawn McCarney 
1939adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemAssociations exit";
19408fb49dd6SShawn McCarney }
19418fb49dd6SShawn McCarney 
19428fb49dd6SShawn McCarney /**
1943d500549bSAnthony Wilson  * @brief Gets D-Bus data for inventory item leds associated with sensors.
1944d500549bSAnthony Wilson  *
1945d500549bSAnthony Wilson  * Uses the specified connections (services) to obtain D-Bus data for inventory
1946d500549bSAnthony Wilson  * item leds associated with sensors.  Stores the resulting data in the
1947d500549bSAnthony Wilson  * inventoryItems vector.
1948d500549bSAnthony Wilson  *
1949d500549bSAnthony Wilson  * This data is later used to provide sensor property values in the JSON
1950d500549bSAnthony Wilson  * response.
1951d500549bSAnthony Wilson  *
1952d500549bSAnthony Wilson  * Finds the inventory item led data asynchronously.  Invokes callback when data
1953d500549bSAnthony Wilson  * has been obtained.
1954d500549bSAnthony Wilson  *
1955d500549bSAnthony Wilson  * The callback must have the following signature:
1956d500549bSAnthony Wilson  *   @code
195742cbe538SGunnar Mills  *   callback()
1958d500549bSAnthony Wilson  *   @endcode
1959d500549bSAnthony Wilson  *
1960d500549bSAnthony Wilson  * This function is called recursively, obtaining data asynchronously from one
1961d500549bSAnthony Wilson  * connection in each call.  This ensures the callback is not invoked until the
1962d500549bSAnthony Wilson  * last asynchronous function has completed.
1963d500549bSAnthony Wilson  *
1964d500549bSAnthony Wilson  * @param sensorsAsyncResp Pointer to object holding response data.
1965d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1966d500549bSAnthony Wilson  * @param ledConnections Connections that provide data for the inventory leds.
1967d500549bSAnthony Wilson  * @param callback Callback to invoke when inventory data has been obtained.
1968d500549bSAnthony Wilson  * @param ledConnectionsIndex Current index in ledConnections.  Only specified
1969d500549bSAnthony Wilson  * in recursive calls to this function.
1970d500549bSAnthony Wilson  */
1971d500549bSAnthony Wilson template <typename Callback>
1972d500549bSAnthony Wilson void getInventoryLedData(
1973d500549bSAnthony Wilson     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1974d500549bSAnthony Wilson     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1975d500549bSAnthony Wilson     std::shared_ptr<boost::container::flat_map<std::string, std::string>>
1976d500549bSAnthony Wilson         ledConnections,
1977d500549bSAnthony Wilson     Callback&& callback, size_t ledConnectionsIndex = 0)
1978d500549bSAnthony Wilson {
1979d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLedData enter";
1980d500549bSAnthony Wilson 
1981d500549bSAnthony Wilson     // If no more connections left, call callback
1982d500549bSAnthony Wilson     if (ledConnectionsIndex >= ledConnections->size())
1983d500549bSAnthony Wilson     {
198442cbe538SGunnar Mills         callback();
1985d500549bSAnthony Wilson         BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1986d500549bSAnthony Wilson         return;
1987d500549bSAnthony Wilson     }
1988d500549bSAnthony Wilson 
1989d500549bSAnthony Wilson     // Get inventory item data from current connection
1990d500549bSAnthony Wilson     auto it = ledConnections->nth(ledConnectionsIndex);
1991d500549bSAnthony Wilson     if (it != ledConnections->end())
1992d500549bSAnthony Wilson     {
1993d500549bSAnthony Wilson         const std::string& ledPath = (*it).first;
1994d500549bSAnthony Wilson         const std::string& ledConnection = (*it).second;
1995d500549bSAnthony Wilson         // Response handler for Get State property
19961e1e598dSJonathan Doman         auto respHandler =
19971e1e598dSJonathan Doman             [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
1998f94c4ecfSEd Tanous              callback{std::forward<Callback>(callback)}, ledConnectionsIndex](
19991e1e598dSJonathan Doman                 const boost::system::error_code ec, const std::string& state) {
2000d500549bSAnthony Wilson                 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler enter";
2001d500549bSAnthony Wilson                 if (ec)
2002d500549bSAnthony Wilson                 {
2003d500549bSAnthony Wilson                     BMCWEB_LOG_ERROR
2004d500549bSAnthony Wilson                         << "getInventoryLedData respHandler DBus error " << ec;
20058d1b46d7Szhanghch05                     messages::internalError(sensorsAsyncResp->asyncResp->res);
2006d500549bSAnthony Wilson                     return;
2007d500549bSAnthony Wilson                 }
2008d500549bSAnthony Wilson 
20091e1e598dSJonathan Doman                 BMCWEB_LOG_DEBUG << "Led state: " << state;
2010d500549bSAnthony Wilson                 // Find inventory item with this LED object path
2011d500549bSAnthony Wilson                 InventoryItem* inventoryItem =
2012d500549bSAnthony Wilson                     findInventoryItemForLed(*inventoryItems, ledPath);
2013d500549bSAnthony Wilson                 if (inventoryItem != nullptr)
2014d500549bSAnthony Wilson                 {
2015d500549bSAnthony Wilson                     // Store LED state in InventoryItem
20161e1e598dSJonathan Doman                     if (boost::ends_with(state, "On"))
2017d500549bSAnthony Wilson                     {
2018d500549bSAnthony Wilson                         inventoryItem->ledState = LedState::ON;
2019d500549bSAnthony Wilson                     }
20201e1e598dSJonathan Doman                     else if (boost::ends_with(state, "Blink"))
2021d500549bSAnthony Wilson                     {
2022d500549bSAnthony Wilson                         inventoryItem->ledState = LedState::BLINK;
2023d500549bSAnthony Wilson                     }
20241e1e598dSJonathan Doman                     else if (boost::ends_with(state, "Off"))
2025d500549bSAnthony Wilson                     {
2026d500549bSAnthony Wilson                         inventoryItem->ledState = LedState::OFF;
2027d500549bSAnthony Wilson                     }
2028d500549bSAnthony Wilson                     else
2029d500549bSAnthony Wilson                     {
2030d500549bSAnthony Wilson                         inventoryItem->ledState = LedState::UNKNOWN;
2031d500549bSAnthony Wilson                     }
2032d500549bSAnthony Wilson                 }
2033d500549bSAnthony Wilson 
2034d500549bSAnthony Wilson                 // Recurse to get LED data from next connection
2035d500549bSAnthony Wilson                 getInventoryLedData(sensorsAsyncResp, inventoryItems,
2036d500549bSAnthony Wilson                                     ledConnections, std::move(callback),
2037d500549bSAnthony Wilson                                     ledConnectionsIndex + 1);
2038d500549bSAnthony Wilson 
2039d500549bSAnthony Wilson                 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler exit";
2040d500549bSAnthony Wilson             };
2041d500549bSAnthony Wilson 
2042d500549bSAnthony Wilson         // Get the State property for the current LED
20431e1e598dSJonathan Doman         sdbusplus::asio::getProperty<std::string>(
20441e1e598dSJonathan Doman             *crow::connections::systemBus, ledConnection, ledPath,
20451e1e598dSJonathan Doman             "xyz.openbmc_project.Led.Physical", "State",
20461e1e598dSJonathan Doman             std::move(respHandler));
2047d500549bSAnthony Wilson     }
2048d500549bSAnthony Wilson 
2049d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
2050d500549bSAnthony Wilson }
2051d500549bSAnthony Wilson 
2052d500549bSAnthony Wilson /**
2053d500549bSAnthony Wilson  * @brief Gets LED data for LEDs associated with given inventory items.
2054d500549bSAnthony Wilson  *
2055d500549bSAnthony Wilson  * Gets the D-Bus connections (services) that provide LED data for the LEDs
2056d500549bSAnthony Wilson  * associated with the specified inventory items.  Then gets the LED data from
2057d500549bSAnthony Wilson  * each connection and stores it in the inventory item.
2058d500549bSAnthony Wilson  *
2059d500549bSAnthony Wilson  * This data is later used to provide sensor property values in the JSON
2060d500549bSAnthony Wilson  * response.
2061d500549bSAnthony Wilson  *
2062d500549bSAnthony Wilson  * Finds the LED data asynchronously.  Invokes callback when information has
2063d500549bSAnthony Wilson  * been obtained.
2064d500549bSAnthony Wilson  *
2065d500549bSAnthony Wilson  * The callback must have the following signature:
2066d500549bSAnthony Wilson  *   @code
206742cbe538SGunnar Mills  *   callback()
2068d500549bSAnthony Wilson  *   @endcode
2069d500549bSAnthony Wilson  *
2070d500549bSAnthony Wilson  * @param sensorsAsyncResp Pointer to object holding response data.
2071d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
2072d500549bSAnthony Wilson  * @param callback Callback to invoke when inventory items have been obtained.
2073d500549bSAnthony Wilson  */
2074d500549bSAnthony Wilson template <typename Callback>
2075d500549bSAnthony Wilson void getInventoryLeds(
2076d500549bSAnthony Wilson     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2077d500549bSAnthony Wilson     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2078d500549bSAnthony Wilson     Callback&& callback)
2079d500549bSAnthony Wilson {
2080d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLeds enter";
2081d500549bSAnthony Wilson 
2082d500549bSAnthony Wilson     const std::string path = "/xyz/openbmc_project";
2083d500549bSAnthony Wilson     const std::array<std::string, 1> interfaces = {
2084d500549bSAnthony Wilson         "xyz.openbmc_project.Led.Physical"};
2085d500549bSAnthony Wilson 
2086d500549bSAnthony Wilson     // Response handler for parsing output from GetSubTree
2087f94c4ecfSEd Tanous     auto respHandler = [callback{std::forward<Callback>(callback)},
2088b9d36b47SEd Tanous                         sensorsAsyncResp, inventoryItems](
2089b9d36b47SEd Tanous                            const boost::system::error_code ec,
2090b9d36b47SEd Tanous                            const dbus::utility::MapperGetSubTreeResponse&
2091b9d36b47SEd Tanous                                subtree) {
2092d500549bSAnthony Wilson         BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler enter";
2093d500549bSAnthony Wilson         if (ec)
2094d500549bSAnthony Wilson         {
20958d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
2096d500549bSAnthony Wilson             BMCWEB_LOG_ERROR << "getInventoryLeds respHandler DBus error "
2097d500549bSAnthony Wilson                              << ec;
2098d500549bSAnthony Wilson             return;
2099d500549bSAnthony Wilson         }
2100d500549bSAnthony Wilson 
2101d500549bSAnthony Wilson         // Build map of LED object paths to connections
2102d500549bSAnthony Wilson         std::shared_ptr<boost::container::flat_map<std::string, std::string>>
2103d500549bSAnthony Wilson             ledConnections = std::make_shared<
2104d500549bSAnthony Wilson                 boost::container::flat_map<std::string, std::string>>();
2105d500549bSAnthony Wilson 
2106d500549bSAnthony Wilson         // Loop through objects from GetSubTree
2107d500549bSAnthony Wilson         for (const std::pair<
2108d500549bSAnthony Wilson                  std::string,
2109d500549bSAnthony Wilson                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
2110d500549bSAnthony Wilson                  object : subtree)
2111d500549bSAnthony Wilson         {
2112d500549bSAnthony Wilson             // Check if object path is LED for one of the specified inventory
2113d500549bSAnthony Wilson             // items
2114d500549bSAnthony Wilson             const std::string& ledPath = object.first;
2115d500549bSAnthony Wilson             if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr)
2116d500549bSAnthony Wilson             {
2117d500549bSAnthony Wilson                 // Add mapping from ledPath to connection
2118d500549bSAnthony Wilson                 const std::string& connection = object.second.begin()->first;
2119d500549bSAnthony Wilson                 (*ledConnections)[ledPath] = connection;
2120d500549bSAnthony Wilson                 BMCWEB_LOG_DEBUG << "Added mapping " << ledPath << " -> "
2121d500549bSAnthony Wilson                                  << connection;
2122d500549bSAnthony Wilson             }
2123d500549bSAnthony Wilson         }
2124d500549bSAnthony Wilson 
2125d500549bSAnthony Wilson         getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections,
2126d500549bSAnthony Wilson                             std::move(callback));
2127d500549bSAnthony Wilson         BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler exit";
2128d500549bSAnthony Wilson     };
2129d500549bSAnthony Wilson     // Make call to ObjectMapper to find all inventory items
2130d500549bSAnthony Wilson     crow::connections::systemBus->async_method_call(
2131d500549bSAnthony Wilson         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
2132d500549bSAnthony Wilson         "/xyz/openbmc_project/object_mapper",
2133d500549bSAnthony Wilson         "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
2134d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLeds exit";
2135d500549bSAnthony Wilson }
2136d500549bSAnthony Wilson 
2137d500549bSAnthony Wilson /**
213842cbe538SGunnar Mills  * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
213942cbe538SGunnar Mills  *
214042cbe538SGunnar Mills  * Uses the specified connections (services) (currently assumes just one) to
214142cbe538SGunnar Mills  * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
214242cbe538SGunnar Mills  * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
214342cbe538SGunnar Mills  *
214442cbe538SGunnar Mills  * This data is later used to provide sensor property values in the JSON
214542cbe538SGunnar Mills  * response.
214642cbe538SGunnar Mills  *
214742cbe538SGunnar Mills  * Finds the Power Supply Attributes data asynchronously.  Invokes callback
214842cbe538SGunnar Mills  * when data has been obtained.
214942cbe538SGunnar Mills  *
215042cbe538SGunnar Mills  * The callback must have the following signature:
215142cbe538SGunnar Mills  *   @code
215242cbe538SGunnar Mills  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
215342cbe538SGunnar Mills  *   @endcode
215442cbe538SGunnar Mills  *
215542cbe538SGunnar Mills  * @param sensorsAsyncResp Pointer to object holding response data.
215642cbe538SGunnar Mills  * @param inventoryItems D-Bus inventory items associated with sensors.
215742cbe538SGunnar Mills  * @param psAttributesConnections Connections that provide data for the Power
215842cbe538SGunnar Mills  *        Supply Attributes
215942cbe538SGunnar Mills  * @param callback Callback to invoke when data has been obtained.
216042cbe538SGunnar Mills  */
216142cbe538SGunnar Mills template <typename Callback>
216242cbe538SGunnar Mills void getPowerSupplyAttributesData(
2163b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
216442cbe538SGunnar Mills     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
216542cbe538SGunnar Mills     const boost::container::flat_map<std::string, std::string>&
216642cbe538SGunnar Mills         psAttributesConnections,
216742cbe538SGunnar Mills     Callback&& callback)
216842cbe538SGunnar Mills {
216942cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData enter";
217042cbe538SGunnar Mills 
217142cbe538SGunnar Mills     if (psAttributesConnections.empty())
217242cbe538SGunnar Mills     {
217342cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "Can't find PowerSupplyAttributes, no connections!";
217442cbe538SGunnar Mills         callback(inventoryItems);
217542cbe538SGunnar Mills         return;
217642cbe538SGunnar Mills     }
217742cbe538SGunnar Mills 
217842cbe538SGunnar Mills     // Assuming just one connection (service) for now
217942cbe538SGunnar Mills     auto it = psAttributesConnections.nth(0);
218042cbe538SGunnar Mills 
218142cbe538SGunnar Mills     const std::string& psAttributesPath = (*it).first;
218242cbe538SGunnar Mills     const std::string& psAttributesConnection = (*it).second;
218342cbe538SGunnar Mills 
218442cbe538SGunnar Mills     // Response handler for Get DeratingFactor property
218542cbe538SGunnar Mills     auto respHandler = [sensorsAsyncResp, inventoryItems,
2186f94c4ecfSEd Tanous                         callback{std::forward<Callback>(callback)}](
218742cbe538SGunnar Mills                            const boost::system::error_code ec,
21881e1e598dSJonathan Doman                            const uint32_t value) {
218942cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler enter";
219042cbe538SGunnar Mills         if (ec)
219142cbe538SGunnar Mills         {
219242cbe538SGunnar Mills             BMCWEB_LOG_ERROR
219342cbe538SGunnar Mills                 << "getPowerSupplyAttributesData respHandler DBus error " << ec;
21948d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
219542cbe538SGunnar Mills             return;
219642cbe538SGunnar Mills         }
219742cbe538SGunnar Mills 
21981e1e598dSJonathan Doman         BMCWEB_LOG_DEBUG << "PS EfficiencyPercent value: " << value;
219942cbe538SGunnar Mills         // Store value in Power Supply Inventory Items
220042cbe538SGunnar Mills         for (InventoryItem& inventoryItem : *inventoryItems)
220142cbe538SGunnar Mills         {
220255f79e6fSEd Tanous             if (inventoryItem.isPowerSupply)
220342cbe538SGunnar Mills             {
220442cbe538SGunnar Mills                 inventoryItem.powerSupplyEfficiencyPercent =
22051e1e598dSJonathan Doman                     static_cast<int>(value);
220642cbe538SGunnar Mills             }
220742cbe538SGunnar Mills         }
220842cbe538SGunnar Mills 
220942cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler exit";
221042cbe538SGunnar Mills         callback(inventoryItems);
221142cbe538SGunnar Mills     };
221242cbe538SGunnar Mills 
221342cbe538SGunnar Mills     // Get the DeratingFactor property for the PowerSupplyAttributes
221442cbe538SGunnar Mills     // Currently only property on the interface/only one we care about
22151e1e598dSJonathan Doman     sdbusplus::asio::getProperty<uint32_t>(
22161e1e598dSJonathan Doman         *crow::connections::systemBus, psAttributesConnection, psAttributesPath,
22171e1e598dSJonathan Doman         "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor",
22181e1e598dSJonathan Doman         std::move(respHandler));
221942cbe538SGunnar Mills 
222042cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData exit";
222142cbe538SGunnar Mills }
222242cbe538SGunnar Mills 
222342cbe538SGunnar Mills /**
222442cbe538SGunnar Mills  * @brief Gets the Power Supply Attributes such as EfficiencyPercent
222542cbe538SGunnar Mills  *
222642cbe538SGunnar Mills  * Gets the D-Bus connection (service) that provides Power Supply Attributes
222742cbe538SGunnar Mills  * data. Then gets the Power Supply Attributes data from the connection
222842cbe538SGunnar Mills  * (currently just assumes 1 connection) and stores the data in the inventory
222942cbe538SGunnar Mills  * item.
223042cbe538SGunnar Mills  *
223142cbe538SGunnar Mills  * This data is later used to provide sensor property values in the JSON
223242cbe538SGunnar Mills  * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
223342cbe538SGunnar Mills  *
223442cbe538SGunnar Mills  * Finds the Power Supply Attributes data asynchronously. Invokes callback
223542cbe538SGunnar Mills  * when information has been obtained.
223642cbe538SGunnar Mills  *
223742cbe538SGunnar Mills  * The callback must have the following signature:
223842cbe538SGunnar Mills  *   @code
223942cbe538SGunnar Mills  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
224042cbe538SGunnar Mills  *   @endcode
224142cbe538SGunnar Mills  *
224242cbe538SGunnar Mills  * @param sensorsAsyncResp Pointer to object holding response data.
224342cbe538SGunnar Mills  * @param inventoryItems D-Bus inventory items associated with sensors.
224442cbe538SGunnar Mills  * @param callback Callback to invoke when data has been obtained.
224542cbe538SGunnar Mills  */
224642cbe538SGunnar Mills template <typename Callback>
224742cbe538SGunnar Mills void getPowerSupplyAttributes(
224842cbe538SGunnar Mills     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
224942cbe538SGunnar Mills     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
225042cbe538SGunnar Mills     Callback&& callback)
225142cbe538SGunnar Mills {
225242cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes enter";
225342cbe538SGunnar Mills 
225442cbe538SGunnar Mills     // Only need the power supply attributes when the Power Schema
2255a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode != sensors::node::power)
225642cbe538SGunnar Mills     {
225742cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit since not Power";
225842cbe538SGunnar Mills         callback(inventoryItems);
225942cbe538SGunnar Mills         return;
226042cbe538SGunnar Mills     }
226142cbe538SGunnar Mills 
226242cbe538SGunnar Mills     const std::array<std::string, 1> interfaces = {
226342cbe538SGunnar Mills         "xyz.openbmc_project.Control.PowerSupplyAttributes"};
226442cbe538SGunnar Mills 
226542cbe538SGunnar Mills     // Response handler for parsing output from GetSubTree
2266b9d36b47SEd Tanous     auto respHandler =
2267b9d36b47SEd Tanous         [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
2268b9d36b47SEd Tanous          inventoryItems](
2269b9d36b47SEd Tanous             const boost::system::error_code ec,
2270b9d36b47SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) {
227142cbe538SGunnar Mills             BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler enter";
227242cbe538SGunnar Mills             if (ec)
227342cbe538SGunnar Mills             {
22748d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
227542cbe538SGunnar Mills                 BMCWEB_LOG_ERROR
227642cbe538SGunnar Mills                     << "getPowerSupplyAttributes respHandler DBus error " << ec;
227742cbe538SGunnar Mills                 return;
227842cbe538SGunnar Mills             }
227926f6976fSEd Tanous             if (subtree.empty())
228042cbe538SGunnar Mills             {
228142cbe538SGunnar Mills                 BMCWEB_LOG_DEBUG << "Can't find Power Supply Attributes!";
228242cbe538SGunnar Mills                 callback(inventoryItems);
228342cbe538SGunnar Mills                 return;
228442cbe538SGunnar Mills             }
228542cbe538SGunnar Mills 
228642cbe538SGunnar Mills             // Currently we only support 1 power supply attribute, use this for
228742cbe538SGunnar Mills             // all the power supplies. Build map of object path to connection.
228842cbe538SGunnar Mills             // Assume just 1 connection and 1 path for now.
228942cbe538SGunnar Mills             boost::container::flat_map<std::string, std::string>
229042cbe538SGunnar Mills                 psAttributesConnections;
229142cbe538SGunnar Mills 
229242cbe538SGunnar Mills             if (subtree[0].first.empty() || subtree[0].second.empty())
229342cbe538SGunnar Mills             {
229442cbe538SGunnar Mills                 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
229542cbe538SGunnar Mills                 callback(inventoryItems);
229642cbe538SGunnar Mills                 return;
229742cbe538SGunnar Mills             }
229842cbe538SGunnar Mills 
229942cbe538SGunnar Mills             const std::string& psAttributesPath = subtree[0].first;
230042cbe538SGunnar Mills             const std::string& connection = subtree[0].second.begin()->first;
230142cbe538SGunnar Mills 
230242cbe538SGunnar Mills             if (connection.empty())
230342cbe538SGunnar Mills             {
230442cbe538SGunnar Mills                 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
230542cbe538SGunnar Mills                 callback(inventoryItems);
230642cbe538SGunnar Mills                 return;
230742cbe538SGunnar Mills             }
230842cbe538SGunnar Mills 
230942cbe538SGunnar Mills             psAttributesConnections[psAttributesPath] = connection;
231042cbe538SGunnar Mills             BMCWEB_LOG_DEBUG << "Added mapping " << psAttributesPath << " -> "
231142cbe538SGunnar Mills                              << connection;
231242cbe538SGunnar Mills 
231342cbe538SGunnar Mills             getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
231442cbe538SGunnar Mills                                          psAttributesConnections,
231542cbe538SGunnar Mills                                          std::move(callback));
231642cbe538SGunnar Mills             BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler exit";
231742cbe538SGunnar Mills         };
231842cbe538SGunnar Mills     // Make call to ObjectMapper to find the PowerSupplyAttributes service
231942cbe538SGunnar Mills     crow::connections::systemBus->async_method_call(
232042cbe538SGunnar Mills         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
232142cbe538SGunnar Mills         "/xyz/openbmc_project/object_mapper",
232242cbe538SGunnar Mills         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
232342cbe538SGunnar Mills         "/xyz/openbmc_project", 0, interfaces);
232442cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit";
232542cbe538SGunnar Mills }
232642cbe538SGunnar Mills 
232742cbe538SGunnar Mills /**
2328adc4f0dbSShawn McCarney  * @brief Gets inventory items associated with sensors.
23298fb49dd6SShawn McCarney  *
23308fb49dd6SShawn McCarney  * Finds the inventory items that are associated with the specified sensors.
2331adc4f0dbSShawn McCarney  * Then gets D-Bus data for the inventory items, such as presence and VPD.
23328fb49dd6SShawn McCarney  *
2333adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
2334adc4f0dbSShawn McCarney  * response.
23358fb49dd6SShawn McCarney  *
2336adc4f0dbSShawn McCarney  * Finds the inventory items asynchronously.  Invokes callback when the
2337adc4f0dbSShawn McCarney  * inventory items have been obtained.
2338adc4f0dbSShawn McCarney  *
2339adc4f0dbSShawn McCarney  * The callback must have the following signature:
2340adc4f0dbSShawn McCarney  *   @code
2341adc4f0dbSShawn McCarney  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2342adc4f0dbSShawn McCarney  *   @endcode
23438fb49dd6SShawn McCarney  *
23448fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
23458fb49dd6SShawn McCarney  * @param sensorNames All sensors within the current chassis.
23468fb49dd6SShawn McCarney  * @param objectMgrPaths Mappings from connection name to DBus object path that
23478fb49dd6SShawn McCarney  * implements ObjectManager.
2348adc4f0dbSShawn McCarney  * @param callback Callback to invoke when inventory items have been obtained.
23498fb49dd6SShawn McCarney  */
2350adc4f0dbSShawn McCarney template <typename Callback>
2351adc4f0dbSShawn McCarney static void getInventoryItems(
23528fb49dd6SShawn McCarney     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
23538fb49dd6SShawn McCarney     const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
23548fb49dd6SShawn McCarney     std::shared_ptr<boost::container::flat_map<std::string, std::string>>
2355adc4f0dbSShawn McCarney         objectMgrPaths,
2356adc4f0dbSShawn McCarney     Callback&& callback)
23578fb49dd6SShawn McCarney {
2358adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItems enter";
2359adc4f0dbSShawn McCarney     auto getInventoryItemAssociationsCb =
2360f94c4ecfSEd Tanous         [sensorsAsyncResp, objectMgrPaths,
2361f94c4ecfSEd Tanous          callback{std::forward<Callback>(callback)}](
2362adc4f0dbSShawn McCarney             std::shared_ptr<std::vector<InventoryItem>> inventoryItems) {
2363adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb enter";
23648fb49dd6SShawn McCarney             auto getInventoryItemsConnectionsCb =
2365adc4f0dbSShawn McCarney                 [sensorsAsyncResp, inventoryItems, objectMgrPaths,
2366f94c4ecfSEd Tanous                  callback{std::forward<const Callback>(callback)}](
23678fb49dd6SShawn McCarney                     std::shared_ptr<boost::container::flat_set<std::string>>
23688fb49dd6SShawn McCarney                         invConnections) {
23698fb49dd6SShawn McCarney                     BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb enter";
2370d500549bSAnthony Wilson                     auto getInventoryItemsDataCb =
2371d500549bSAnthony Wilson                         [sensorsAsyncResp, inventoryItems,
2372d500549bSAnthony Wilson                          callback{std::move(callback)}]() {
2373d500549bSAnthony Wilson                             BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb enter";
237442cbe538SGunnar Mills 
237542cbe538SGunnar Mills                             auto getInventoryLedsCb = [sensorsAsyncResp,
237642cbe538SGunnar Mills                                                        inventoryItems,
237742cbe538SGunnar Mills                                                        callback{std::move(
237842cbe538SGunnar Mills                                                            callback)}]() {
237942cbe538SGunnar Mills                                 BMCWEB_LOG_DEBUG << "getInventoryLedsCb enter";
238042cbe538SGunnar Mills                                 // Find Power Supply Attributes and get the data
238142cbe538SGunnar Mills                                 getPowerSupplyAttributes(sensorsAsyncResp,
238242cbe538SGunnar Mills                                                          inventoryItems,
238342cbe538SGunnar Mills                                                          std::move(callback));
238442cbe538SGunnar Mills                                 BMCWEB_LOG_DEBUG << "getInventoryLedsCb exit";
238542cbe538SGunnar Mills                             };
238642cbe538SGunnar Mills 
2387d500549bSAnthony Wilson                             // Find led connections and get the data
2388d500549bSAnthony Wilson                             getInventoryLeds(sensorsAsyncResp, inventoryItems,
238942cbe538SGunnar Mills                                              std::move(getInventoryLedsCb));
2390d500549bSAnthony Wilson                             BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb exit";
2391d500549bSAnthony Wilson                         };
23928fb49dd6SShawn McCarney 
2393adc4f0dbSShawn McCarney                     // Get inventory item data from connections
2394adc4f0dbSShawn McCarney                     getInventoryItemsData(sensorsAsyncResp, inventoryItems,
2395adc4f0dbSShawn McCarney                                           invConnections, objectMgrPaths,
2396d500549bSAnthony Wilson                                           std::move(getInventoryItemsDataCb));
23978fb49dd6SShawn McCarney                     BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb exit";
23988fb49dd6SShawn McCarney                 };
23998fb49dd6SShawn McCarney 
2400adc4f0dbSShawn McCarney             // Get connections that provide inventory item data
24018fb49dd6SShawn McCarney             getInventoryItemsConnections(
2402adc4f0dbSShawn McCarney                 sensorsAsyncResp, inventoryItems,
24038fb49dd6SShawn McCarney                 std::move(getInventoryItemsConnectionsCb));
2404adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb exit";
24058fb49dd6SShawn McCarney         };
24068fb49dd6SShawn McCarney 
2407adc4f0dbSShawn McCarney     // Get associations from sensors to inventory items
2408adc4f0dbSShawn McCarney     getInventoryItemAssociations(sensorsAsyncResp, sensorNames, objectMgrPaths,
2409adc4f0dbSShawn McCarney                                  std::move(getInventoryItemAssociationsCb));
2410adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItems exit";
2411adc4f0dbSShawn McCarney }
2412adc4f0dbSShawn McCarney 
2413adc4f0dbSShawn McCarney /**
2414adc4f0dbSShawn McCarney  * @brief Returns JSON PowerSupply object for the specified inventory item.
2415adc4f0dbSShawn McCarney  *
2416adc4f0dbSShawn McCarney  * Searches for a JSON PowerSupply object that matches the specified inventory
2417adc4f0dbSShawn McCarney  * item.  If one is not found, a new PowerSupply object is added to the JSON
2418adc4f0dbSShawn McCarney  * array.
2419adc4f0dbSShawn McCarney  *
2420adc4f0dbSShawn McCarney  * Multiple sensors are often associated with one power supply inventory item.
2421adc4f0dbSShawn McCarney  * As a result, multiple sensor values are stored in one JSON PowerSupply
2422adc4f0dbSShawn McCarney  * object.
2423adc4f0dbSShawn McCarney  *
2424adc4f0dbSShawn McCarney  * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
2425adc4f0dbSShawn McCarney  * @param inventoryItem Inventory item for the power supply.
2426adc4f0dbSShawn McCarney  * @param chassisId Chassis that contains the power supply.
2427adc4f0dbSShawn McCarney  * @return JSON PowerSupply object for the specified inventory item.
2428adc4f0dbSShawn McCarney  */
242923a21a1cSEd Tanous inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
2430adc4f0dbSShawn McCarney                                       const InventoryItem& inventoryItem,
2431adc4f0dbSShawn McCarney                                       const std::string& chassisId)
2432adc4f0dbSShawn McCarney {
2433adc4f0dbSShawn McCarney     // Check if matching PowerSupply object already exists in JSON array
2434adc4f0dbSShawn McCarney     for (nlohmann::json& powerSupply : powerSupplyArray)
2435adc4f0dbSShawn McCarney     {
2436adc4f0dbSShawn McCarney         if (powerSupply["MemberId"] == inventoryItem.name)
2437adc4f0dbSShawn McCarney         {
2438adc4f0dbSShawn McCarney             return powerSupply;
2439adc4f0dbSShawn McCarney         }
2440adc4f0dbSShawn McCarney     }
2441adc4f0dbSShawn McCarney 
2442adc4f0dbSShawn McCarney     // Add new PowerSupply object to JSON array
2443adc4f0dbSShawn McCarney     powerSupplyArray.push_back({});
2444adc4f0dbSShawn McCarney     nlohmann::json& powerSupply = powerSupplyArray.back();
2445adc4f0dbSShawn McCarney     powerSupply["@odata.id"] =
2446adc4f0dbSShawn McCarney         "/redfish/v1/Chassis/" + chassisId + "/Power#/PowerSupplies/";
2447adc4f0dbSShawn McCarney     powerSupply["MemberId"] = inventoryItem.name;
2448adc4f0dbSShawn McCarney     powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " ");
2449adc4f0dbSShawn McCarney     powerSupply["Manufacturer"] = inventoryItem.manufacturer;
2450adc4f0dbSShawn McCarney     powerSupply["Model"] = inventoryItem.model;
2451adc4f0dbSShawn McCarney     powerSupply["PartNumber"] = inventoryItem.partNumber;
2452adc4f0dbSShawn McCarney     powerSupply["SerialNumber"] = inventoryItem.serialNumber;
2453d500549bSAnthony Wilson     setLedState(powerSupply, &inventoryItem);
2454adc4f0dbSShawn McCarney 
245542cbe538SGunnar Mills     if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
245642cbe538SGunnar Mills     {
245742cbe538SGunnar Mills         powerSupply["EfficiencyPercent"] =
245842cbe538SGunnar Mills             inventoryItem.powerSupplyEfficiencyPercent;
245942cbe538SGunnar Mills     }
246042cbe538SGunnar Mills 
246142cbe538SGunnar Mills     powerSupply["Status"]["State"] = getState(&inventoryItem);
2462adc4f0dbSShawn McCarney     const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
2463adc4f0dbSShawn McCarney     powerSupply["Status"]["Health"] = health;
2464adc4f0dbSShawn McCarney 
2465adc4f0dbSShawn McCarney     return powerSupply;
24668fb49dd6SShawn McCarney }
24678fb49dd6SShawn McCarney 
24688fb49dd6SShawn McCarney /**
2469de629b6eSShawn McCarney  * @brief Gets the values of the specified sensors.
2470de629b6eSShawn McCarney  *
2471de629b6eSShawn McCarney  * Stores the results as JSON in the SensorsAsyncResp.
2472de629b6eSShawn McCarney  *
2473de629b6eSShawn McCarney  * Gets the sensor values asynchronously.  Stores the results later when the
2474de629b6eSShawn McCarney  * information has been obtained.
2475de629b6eSShawn McCarney  *
2476adc4f0dbSShawn McCarney  * The sensorNames set contains all requested sensors for the current chassis.
2477de629b6eSShawn McCarney  *
2478de629b6eSShawn McCarney  * To minimize the number of DBus calls, the DBus method
2479de629b6eSShawn McCarney  * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
2480de629b6eSShawn McCarney  * values of all sensors provided by a connection (service).
2481de629b6eSShawn McCarney  *
2482de629b6eSShawn McCarney  * The connections set contains all the connections that provide sensor values.
2483de629b6eSShawn McCarney  *
2484de629b6eSShawn McCarney  * The objectMgrPaths map contains mappings from a connection name to the
2485de629b6eSShawn McCarney  * corresponding DBus object path that implements ObjectManager.
2486de629b6eSShawn McCarney  *
2487adc4f0dbSShawn McCarney  * The InventoryItem vector contains D-Bus inventory items associated with the
2488adc4f0dbSShawn McCarney  * sensors.  Inventory item data is needed for some Redfish sensor properties.
2489adc4f0dbSShawn McCarney  *
2490de629b6eSShawn McCarney  * @param SensorsAsyncResp Pointer to object holding response data.
2491adc4f0dbSShawn McCarney  * @param sensorNames All requested sensors within the current chassis.
2492de629b6eSShawn McCarney  * @param connections Connections that provide sensor values.
2493de629b6eSShawn McCarney  * @param objectMgrPaths Mappings from connection name to DBus object path that
2494de629b6eSShawn McCarney  * implements ObjectManager.
2495adc4f0dbSShawn McCarney  * @param inventoryItems Inventory items associated with the sensors.
2496de629b6eSShawn McCarney  */
249723a21a1cSEd Tanous inline void getSensorData(
249881ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2499b5a76932SEd Tanous     const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames,
2500de629b6eSShawn McCarney     const boost::container::flat_set<std::string>& connections,
2501b5a76932SEd Tanous     const std::shared_ptr<boost::container::flat_map<std::string, std::string>>&
2502adc4f0dbSShawn McCarney         objectMgrPaths,
2503b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems)
2504de629b6eSShawn McCarney {
2505de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getSensorData enter";
2506de629b6eSShawn McCarney     // Get managed objects from all services exposing sensors
2507de629b6eSShawn McCarney     for (const std::string& connection : connections)
2508de629b6eSShawn McCarney     {
2509de629b6eSShawn McCarney         // Response handler to process managed objects
251081ce609eSEd Tanous         auto getManagedObjectsCb = [sensorsAsyncResp, sensorNames,
2511adc4f0dbSShawn McCarney                                     inventoryItems](
2512de629b6eSShawn McCarney                                        const boost::system::error_code ec,
2513711ac7a9SEd Tanous                                        dbus::utility::ManagedObjectType& resp) {
2514de629b6eSShawn McCarney             BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter";
2515de629b6eSShawn McCarney             if (ec)
2516de629b6eSShawn McCarney             {
2517de629b6eSShawn McCarney                 BMCWEB_LOG_ERROR << "getManagedObjectsCb DBUS error: " << ec;
25188d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
2519de629b6eSShawn McCarney                 return;
2520de629b6eSShawn McCarney             }
2521de629b6eSShawn McCarney             // Go through all objects and update response with sensor data
2522de629b6eSShawn McCarney             for (const auto& objDictEntry : resp)
2523de629b6eSShawn McCarney             {
2524de629b6eSShawn McCarney                 const std::string& objPath =
2525de629b6eSShawn McCarney                     static_cast<const std::string&>(objDictEntry.first);
2526de629b6eSShawn McCarney                 BMCWEB_LOG_DEBUG << "getManagedObjectsCb parsing object "
2527de629b6eSShawn McCarney                                  << objPath;
2528de629b6eSShawn McCarney 
2529de629b6eSShawn McCarney                 std::vector<std::string> split;
2530de629b6eSShawn McCarney                 // Reserve space for
2531de629b6eSShawn McCarney                 // /xyz/openbmc_project/sensors/<name>/<subname>
2532de629b6eSShawn McCarney                 split.reserve(6);
2533de629b6eSShawn McCarney                 boost::algorithm::split(split, objPath, boost::is_any_of("/"));
2534de629b6eSShawn McCarney                 if (split.size() < 6)
2535de629b6eSShawn McCarney                 {
2536de629b6eSShawn McCarney                     BMCWEB_LOG_ERROR << "Got path that isn't long enough "
2537de629b6eSShawn McCarney                                      << objPath;
2538de629b6eSShawn McCarney                     continue;
2539de629b6eSShawn McCarney                 }
2540de629b6eSShawn McCarney                 // These indexes aren't intuitive, as boost::split puts an empty
2541de629b6eSShawn McCarney                 // string at the beginning
2542de629b6eSShawn McCarney                 const std::string& sensorType = split[4];
2543de629b6eSShawn McCarney                 const std::string& sensorName = split[5];
2544de629b6eSShawn McCarney                 BMCWEB_LOG_DEBUG << "sensorName " << sensorName
2545de629b6eSShawn McCarney                                  << " sensorType " << sensorType;
254649c53ac9SJohnathan Mantey                 if (sensorNames->find(objPath) == sensorNames->end())
2547de629b6eSShawn McCarney                 {
2548accdbb2cSAndrew Geissler                     BMCWEB_LOG_DEBUG << sensorName << " not in sensor list ";
2549de629b6eSShawn McCarney                     continue;
2550de629b6eSShawn McCarney                 }
2551de629b6eSShawn McCarney 
2552adc4f0dbSShawn McCarney                 // Find inventory item (if any) associated with sensor
2553adc4f0dbSShawn McCarney                 InventoryItem* inventoryItem =
2554adc4f0dbSShawn McCarney                     findInventoryItemForSensor(inventoryItems, objPath);
2555adc4f0dbSShawn McCarney 
255695a3ecadSAnthony Wilson                 const std::string& sensorSchema =
255781ce609eSEd Tanous                     sensorsAsyncResp->chassisSubNode;
255895a3ecadSAnthony Wilson 
255995a3ecadSAnthony Wilson                 nlohmann::json* sensorJson = nullptr;
256095a3ecadSAnthony Wilson 
2561928fefb9SNan Zhou                 if (sensorSchema == sensors::node::sensors &&
2562928fefb9SNan Zhou                     !sensorsAsyncResp->efficientExpand)
256395a3ecadSAnthony Wilson                 {
25648d1b46d7Szhanghch05                     sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
256581ce609eSEd Tanous                         "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId +
256681ce609eSEd Tanous                         "/" + sensorsAsyncResp->chassisSubNode + "/" +
256795a3ecadSAnthony Wilson                         sensorName;
25688d1b46d7Szhanghch05                     sensorJson = &(sensorsAsyncResp->asyncResp->res.jsonValue);
256995a3ecadSAnthony Wilson                 }
257095a3ecadSAnthony Wilson                 else
257195a3ecadSAnthony Wilson                 {
2572271584abSEd Tanous                     std::string fieldName;
2573928fefb9SNan Zhou                     if (sensorsAsyncResp->efficientExpand)
2574928fefb9SNan Zhou                     {
2575928fefb9SNan Zhou                         fieldName = "Members";
2576928fefb9SNan Zhou                     }
2577928fefb9SNan Zhou                     else if (sensorType == "temperature")
2578de629b6eSShawn McCarney                     {
2579de629b6eSShawn McCarney                         fieldName = "Temperatures";
2580de629b6eSShawn McCarney                     }
2581de629b6eSShawn McCarney                     else if (sensorType == "fan" || sensorType == "fan_tach" ||
2582de629b6eSShawn McCarney                              sensorType == "fan_pwm")
2583de629b6eSShawn McCarney                     {
2584de629b6eSShawn McCarney                         fieldName = "Fans";
2585de629b6eSShawn McCarney                     }
2586de629b6eSShawn McCarney                     else if (sensorType == "voltage")
2587de629b6eSShawn McCarney                     {
2588de629b6eSShawn McCarney                         fieldName = "Voltages";
2589de629b6eSShawn McCarney                     }
2590de629b6eSShawn McCarney                     else if (sensorType == "power")
2591de629b6eSShawn McCarney                     {
259255f79e6fSEd Tanous                         if (sensorName == "total_power")
2593028f7ebcSEddie James                         {
2594028f7ebcSEddie James                             fieldName = "PowerControl";
2595028f7ebcSEddie James                         }
2596adc4f0dbSShawn McCarney                         else if ((inventoryItem != nullptr) &&
2597adc4f0dbSShawn McCarney                                  (inventoryItem->isPowerSupply))
2598028f7ebcSEddie James                         {
2599de629b6eSShawn McCarney                             fieldName = "PowerSupplies";
2600de629b6eSShawn McCarney                         }
2601adc4f0dbSShawn McCarney                         else
2602adc4f0dbSShawn McCarney                         {
2603adc4f0dbSShawn McCarney                             // Other power sensors are in SensorCollection
2604adc4f0dbSShawn McCarney                             continue;
2605adc4f0dbSShawn McCarney                         }
2606028f7ebcSEddie James                     }
2607de629b6eSShawn McCarney                     else
2608de629b6eSShawn McCarney                     {
2609de629b6eSShawn McCarney                         BMCWEB_LOG_ERROR << "Unsure how to handle sensorType "
2610de629b6eSShawn McCarney                                          << sensorType;
2611de629b6eSShawn McCarney                         continue;
2612de629b6eSShawn McCarney                     }
2613de629b6eSShawn McCarney 
2614de629b6eSShawn McCarney                     nlohmann::json& tempArray =
26158d1b46d7Szhanghch05                         sensorsAsyncResp->asyncResp->res.jsonValue[fieldName];
2616adc4f0dbSShawn McCarney                     if (fieldName == "PowerControl")
261749c53ac9SJohnathan Mantey                     {
2618adc4f0dbSShawn McCarney                         if (tempArray.empty())
26197ab06f49SGunnar Mills                         {
262095a3ecadSAnthony Wilson                             // Put multiple "sensors" into a single
262195a3ecadSAnthony Wilson                             // PowerControl. Follows MemberId naming and
262295a3ecadSAnthony Wilson                             // naming in power.hpp.
2623*1476687dSEd Tanous                             nlohmann::json::object_t power;
2624*1476687dSEd Tanous                             power["@odata.id"] =
2625adc4f0dbSShawn McCarney                                 "/redfish/v1/Chassis/" +
262681ce609eSEd Tanous                                 sensorsAsyncResp->chassisId + "/" +
262781ce609eSEd Tanous                                 sensorsAsyncResp->chassisSubNode + "#/" +
2628*1476687dSEd Tanous                                 fieldName + "/0";
2629*1476687dSEd Tanous                             tempArray.push_back(std::move(power));
2630adc4f0dbSShawn McCarney                         }
2631adc4f0dbSShawn McCarney                         sensorJson = &(tempArray.back());
2632adc4f0dbSShawn McCarney                     }
2633adc4f0dbSShawn McCarney                     else if (fieldName == "PowerSupplies")
2634adc4f0dbSShawn McCarney                     {
2635adc4f0dbSShawn McCarney                         if (inventoryItem != nullptr)
2636adc4f0dbSShawn McCarney                         {
2637adc4f0dbSShawn McCarney                             sensorJson =
2638adc4f0dbSShawn McCarney                                 &(getPowerSupply(tempArray, *inventoryItem,
263981ce609eSEd Tanous                                                  sensorsAsyncResp->chassisId));
2640adc4f0dbSShawn McCarney                         }
264149c53ac9SJohnathan Mantey                     }
2642928fefb9SNan Zhou                     else if (fieldName == "Members")
2643928fefb9SNan Zhou                     {
2644*1476687dSEd Tanous                         nlohmann::json::object_t member;
2645*1476687dSEd Tanous                         member["@odata.id"] =
2646928fefb9SNan Zhou                             "/redfish/v1/Chassis/" +
2647928fefb9SNan Zhou                             sensorsAsyncResp->chassisId + "/" +
2648*1476687dSEd Tanous                             sensorsAsyncResp->chassisSubNode + "/" + sensorName;
2649*1476687dSEd Tanous                         tempArray.push_back(std::move(member));
2650928fefb9SNan Zhou                         sensorJson = &(tempArray.back());
2651928fefb9SNan Zhou                     }
265249c53ac9SJohnathan Mantey                     else
265349c53ac9SJohnathan Mantey                     {
2654*1476687dSEd Tanous                         nlohmann::json::object_t member;
2655*1476687dSEd Tanous                         member["@odata.id"] = "/redfish/v1/Chassis/" +
2656*1476687dSEd Tanous                                               sensorsAsyncResp->chassisId +
2657*1476687dSEd Tanous                                               "/" +
2658*1476687dSEd Tanous                                               sensorsAsyncResp->chassisSubNode +
2659*1476687dSEd Tanous                                               "#/" + fieldName + "/";
2660*1476687dSEd Tanous                         tempArray.push_back(std::move(member));
2661adc4f0dbSShawn McCarney                         sensorJson = &(tempArray.back());
266249c53ac9SJohnathan Mantey                     }
266395a3ecadSAnthony Wilson                 }
2664de629b6eSShawn McCarney 
2665adc4f0dbSShawn McCarney                 if (sensorJson != nullptr)
2666adc4f0dbSShawn McCarney                 {
2667a0ec28b6SAdrian Ambrożewicz                     objectInterfacesToJson(
266881ce609eSEd Tanous                         sensorName, sensorType, sensorsAsyncResp,
2669a0ec28b6SAdrian Ambrożewicz                         objDictEntry.second, *sensorJson, inventoryItem);
2670adc4f0dbSShawn McCarney                 }
2671de629b6eSShawn McCarney             }
267281ce609eSEd Tanous             if (sensorsAsyncResp.use_count() == 1)
267349c53ac9SJohnathan Mantey             {
267481ce609eSEd Tanous                 sortJSONResponse(sensorsAsyncResp);
2675928fefb9SNan Zhou                 if (sensorsAsyncResp->chassisSubNode ==
2676928fefb9SNan Zhou                         sensors::node::sensors &&
2677928fefb9SNan Zhou                     sensorsAsyncResp->efficientExpand)
2678928fefb9SNan Zhou                 {
2679928fefb9SNan Zhou                     sensorsAsyncResp->asyncResp->res
2680928fefb9SNan Zhou                         .jsonValue["Members@odata.count"] =
2681928fefb9SNan Zhou                         sensorsAsyncResp->asyncResp->res.jsonValue["Members"]
2682928fefb9SNan Zhou                             .size();
2683928fefb9SNan Zhou                 }
2684928fefb9SNan Zhou                 else if (sensorsAsyncResp->chassisSubNode ==
2685928fefb9SNan Zhou                          sensors::node::thermal)
26868bd25ccdSJames Feist                 {
268781ce609eSEd Tanous                     populateFanRedundancy(sensorsAsyncResp);
26888bd25ccdSJames Feist                 }
268949c53ac9SJohnathan Mantey             }
2690de629b6eSShawn McCarney             BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit";
2691de629b6eSShawn McCarney         };
2692de629b6eSShawn McCarney 
2693de629b6eSShawn McCarney         // Find DBus object path that implements ObjectManager for the current
2694de629b6eSShawn McCarney         // connection.  If no mapping found, default to "/".
26958fb49dd6SShawn McCarney         auto iter = objectMgrPaths->find(connection);
2696de629b6eSShawn McCarney         const std::string& objectMgrPath =
26978fb49dd6SShawn McCarney             (iter != objectMgrPaths->end()) ? iter->second : "/";
2698de629b6eSShawn McCarney         BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
2699de629b6eSShawn McCarney                          << objectMgrPath;
2700de629b6eSShawn McCarney 
2701de629b6eSShawn McCarney         crow::connections::systemBus->async_method_call(
2702de629b6eSShawn McCarney             getManagedObjectsCb, connection, objectMgrPath,
2703de629b6eSShawn McCarney             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
270423a21a1cSEd Tanous     }
2705de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getSensorData exit";
2706de629b6eSShawn McCarney }
2707de629b6eSShawn McCarney 
270823a21a1cSEd Tanous inline void processSensorList(
270981ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2710b5a76932SEd Tanous     const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames)
27111abe55efSEd Tanous {
271295a3ecadSAnthony Wilson     auto getConnectionCb =
271381ce609eSEd Tanous         [sensorsAsyncResp, sensorNames](
271495a3ecadSAnthony Wilson             const boost::container::flat_set<std::string>& connections) {
271555c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "getConnectionCb enter";
2716de629b6eSShawn McCarney             auto getObjectManagerPathsCb =
271781ce609eSEd Tanous                 [sensorsAsyncResp, sensorNames,
2718b5a76932SEd Tanous                  connections](const std::shared_ptr<boost::container::flat_map<
2719b5a76932SEd Tanous                                   std::string, std::string>>& objectMgrPaths) {
2720de629b6eSShawn McCarney                     BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb enter";
2721adc4f0dbSShawn McCarney                     auto getInventoryItemsCb =
272281ce609eSEd Tanous                         [sensorsAsyncResp, sensorNames, connections,
2723adc4f0dbSShawn McCarney                          objectMgrPaths](
2724f23b7296SEd Tanous                             const std::shared_ptr<std::vector<InventoryItem>>&
2725adc4f0dbSShawn McCarney                                 inventoryItems) {
2726adc4f0dbSShawn McCarney                             BMCWEB_LOG_DEBUG << "getInventoryItemsCb enter";
272749c53ac9SJohnathan Mantey                             // Get sensor data and store results in JSON
272881ce609eSEd Tanous                             getSensorData(sensorsAsyncResp, sensorNames,
2729adc4f0dbSShawn McCarney                                           connections, objectMgrPaths,
2730f23b7296SEd Tanous                                           inventoryItems);
2731adc4f0dbSShawn McCarney                             BMCWEB_LOG_DEBUG << "getInventoryItemsCb exit";
2732adc4f0dbSShawn McCarney                         };
2733adc4f0dbSShawn McCarney 
2734adc4f0dbSShawn McCarney                     // Get inventory items associated with sensors
273581ce609eSEd Tanous                     getInventoryItems(sensorsAsyncResp, sensorNames,
2736adc4f0dbSShawn McCarney                                       objectMgrPaths,
2737adc4f0dbSShawn McCarney                                       std::move(getInventoryItemsCb));
2738adc4f0dbSShawn McCarney 
2739de629b6eSShawn McCarney                     BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb exit";
274008777fb0SLewanczyk, Dawid                 };
2741de629b6eSShawn McCarney 
274249c53ac9SJohnathan Mantey             // Get mapping from connection names to the DBus object
274349c53ac9SJohnathan Mantey             // paths that implement the ObjectManager interface
274481ce609eSEd Tanous             getObjectManagerPaths(sensorsAsyncResp,
2745de629b6eSShawn McCarney                                   std::move(getObjectManagerPathsCb));
274655c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "getConnectionCb exit";
274708777fb0SLewanczyk, Dawid         };
2748de629b6eSShawn McCarney 
2749de629b6eSShawn McCarney     // Get set of connections that provide sensor values
275081ce609eSEd Tanous     getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb));
275195a3ecadSAnthony Wilson }
275295a3ecadSAnthony Wilson 
275395a3ecadSAnthony Wilson /**
275495a3ecadSAnthony Wilson  * @brief Entry point for retrieving sensors data related to requested
275595a3ecadSAnthony Wilson  *        chassis.
275695a3ecadSAnthony Wilson  * @param SensorsAsyncResp   Pointer to object holding response data
275795a3ecadSAnthony Wilson  */
2758b5a76932SEd Tanous inline void
275981ce609eSEd Tanous     getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
276095a3ecadSAnthony Wilson {
276195a3ecadSAnthony Wilson     BMCWEB_LOG_DEBUG << "getChassisData enter";
276295a3ecadSAnthony Wilson     auto getChassisCb =
276381ce609eSEd Tanous         [sensorsAsyncResp](
2764f23b7296SEd Tanous             const std::shared_ptr<boost::container::flat_set<std::string>>&
276595a3ecadSAnthony Wilson                 sensorNames) {
276695a3ecadSAnthony Wilson             BMCWEB_LOG_DEBUG << "getChassisCb enter";
276781ce609eSEd Tanous             processSensorList(sensorsAsyncResp, sensorNames);
276855c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "getChassisCb exit";
276908777fb0SLewanczyk, Dawid         };
2770928fefb9SNan Zhou     // SensorCollection doesn't contain the Redundancy property
2771928fefb9SNan Zhou     if (sensorsAsyncResp->chassisSubNode != sensors::node::sensors)
2772928fefb9SNan Zhou     {
27738d1b46d7Szhanghch05         sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] =
27748d1b46d7Szhanghch05             nlohmann::json::array();
2775928fefb9SNan Zhou     }
277626f03899SShawn McCarney     // Get set of sensors in chassis
277781ce609eSEd Tanous     getChassis(sensorsAsyncResp, std::move(getChassisCb));
277855c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "getChassisData exit";
2779271584abSEd Tanous }
278008777fb0SLewanczyk, Dawid 
2781413961deSRichard Marian Thomaiyar /**
278249c53ac9SJohnathan Mantey  * @brief Find the requested sensorName in the list of all sensors supplied by
278349c53ac9SJohnathan Mantey  * the chassis node
278449c53ac9SJohnathan Mantey  *
278549c53ac9SJohnathan Mantey  * @param sensorName   The sensor name supplied in the PATCH request
278649c53ac9SJohnathan Mantey  * @param sensorsList  The list of sensors managed by the chassis node
278749c53ac9SJohnathan Mantey  * @param sensorsModified  The list of sensors that were found as a result of
278849c53ac9SJohnathan Mantey  *                         repeated calls to this function
278949c53ac9SJohnathan Mantey  */
279023a21a1cSEd Tanous inline bool findSensorNameUsingSensorPath(
27910a86febdSRichard Marian Thomaiyar     std::string_view sensorName,
279249c53ac9SJohnathan Mantey     boost::container::flat_set<std::string>& sensorsList,
279349c53ac9SJohnathan Mantey     boost::container::flat_set<std::string>& sensorsModified)
279449c53ac9SJohnathan Mantey {
279528aa8de5SGeorge Liu     for (auto& chassisSensor : sensorsList)
279649c53ac9SJohnathan Mantey     {
279728aa8de5SGeorge Liu         sdbusplus::message::object_path path(chassisSensor);
2798b00dcc27SEd Tanous         std::string thisSensorName = path.filename();
279928aa8de5SGeorge Liu         if (thisSensorName.empty())
280049c53ac9SJohnathan Mantey         {
280149c53ac9SJohnathan Mantey             continue;
280249c53ac9SJohnathan Mantey         }
280349c53ac9SJohnathan Mantey         if (thisSensorName == sensorName)
280449c53ac9SJohnathan Mantey         {
280549c53ac9SJohnathan Mantey             sensorsModified.emplace(chassisSensor);
280649c53ac9SJohnathan Mantey             return true;
280749c53ac9SJohnathan Mantey         }
280849c53ac9SJohnathan Mantey     }
280949c53ac9SJohnathan Mantey     return false;
281049c53ac9SJohnathan Mantey }
281149c53ac9SJohnathan Mantey 
281249c53ac9SJohnathan Mantey /**
2813413961deSRichard Marian Thomaiyar  * @brief Entry point for overriding sensor values of given sensor
2814413961deSRichard Marian Thomaiyar  *
28158d1b46d7Szhanghch05  * @param sensorAsyncResp   response object
28164bb3dc34SCarol Wang  * @param allCollections   Collections extract from sensors' request patch info
2817413961deSRichard Marian Thomaiyar  * @param chassisSubNode   Chassis Node for which the query has to happen
2818413961deSRichard Marian Thomaiyar  */
281923a21a1cSEd Tanous inline void setSensorsOverride(
2820b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
28214bb3dc34SCarol Wang     std::unordered_map<std::string, std::vector<nlohmann::json>>&
2822397fd61fSjayaprakash Mutyala         allCollections)
2823413961deSRichard Marian Thomaiyar {
282470d1d0aaSjayaprakash Mutyala     BMCWEB_LOG_INFO << "setSensorsOverride for subNode"
28254bb3dc34SCarol Wang                     << sensorAsyncResp->chassisSubNode << "\n";
2826413961deSRichard Marian Thomaiyar 
2827543f4400SEd Tanous     const char* propertyValueName = nullptr;
2828f65af9e8SRichard Marian Thomaiyar     std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
2829413961deSRichard Marian Thomaiyar     std::string memberId;
2830543f4400SEd Tanous     double value = 0.0;
2831f65af9e8SRichard Marian Thomaiyar     for (auto& collectionItems : allCollections)
2832f65af9e8SRichard Marian Thomaiyar     {
2833f65af9e8SRichard Marian Thomaiyar         if (collectionItems.first == "Temperatures")
2834f65af9e8SRichard Marian Thomaiyar         {
2835f65af9e8SRichard Marian Thomaiyar             propertyValueName = "ReadingCelsius";
2836f65af9e8SRichard Marian Thomaiyar         }
2837f65af9e8SRichard Marian Thomaiyar         else if (collectionItems.first == "Fans")
2838f65af9e8SRichard Marian Thomaiyar         {
2839f65af9e8SRichard Marian Thomaiyar             propertyValueName = "Reading";
2840f65af9e8SRichard Marian Thomaiyar         }
2841f65af9e8SRichard Marian Thomaiyar         else
2842f65af9e8SRichard Marian Thomaiyar         {
2843f65af9e8SRichard Marian Thomaiyar             propertyValueName = "ReadingVolts";
2844f65af9e8SRichard Marian Thomaiyar         }
2845f65af9e8SRichard Marian Thomaiyar         for (auto& item : collectionItems.second)
2846f65af9e8SRichard Marian Thomaiyar         {
28478d1b46d7Szhanghch05             if (!json_util::readJson(item, sensorAsyncResp->asyncResp->res,
28488d1b46d7Szhanghch05                                      "MemberId", memberId, propertyValueName,
28498d1b46d7Szhanghch05                                      value))
2850413961deSRichard Marian Thomaiyar             {
2851413961deSRichard Marian Thomaiyar                 return;
2852413961deSRichard Marian Thomaiyar             }
2853f65af9e8SRichard Marian Thomaiyar             overrideMap.emplace(memberId,
2854f65af9e8SRichard Marian Thomaiyar                                 std::make_pair(value, collectionItems.first));
2855f65af9e8SRichard Marian Thomaiyar         }
2856f65af9e8SRichard Marian Thomaiyar     }
28574bb3dc34SCarol Wang 
2858b5a76932SEd Tanous     auto getChassisSensorListCb = [sensorAsyncResp, overrideMap](
2859b5a76932SEd Tanous                                       const std::shared_ptr<
286049c53ac9SJohnathan Mantey                                           boost::container::flat_set<
2861b5a76932SEd Tanous                                               std::string>>& sensorsList) {
286249c53ac9SJohnathan Mantey         // Match sensor names in the PATCH request to those managed by the
286349c53ac9SJohnathan Mantey         // chassis node
286449c53ac9SJohnathan Mantey         const std::shared_ptr<boost::container::flat_set<std::string>>
286549c53ac9SJohnathan Mantey             sensorNames =
286649c53ac9SJohnathan Mantey                 std::make_shared<boost::container::flat_set<std::string>>();
2867f65af9e8SRichard Marian Thomaiyar         for (const auto& item : overrideMap)
2868413961deSRichard Marian Thomaiyar         {
2869f65af9e8SRichard Marian Thomaiyar             const auto& sensor = item.first;
287049c53ac9SJohnathan Mantey             if (!findSensorNameUsingSensorPath(sensor, *sensorsList,
287149c53ac9SJohnathan Mantey                                                *sensorNames))
2872f65af9e8SRichard Marian Thomaiyar             {
2873f65af9e8SRichard Marian Thomaiyar                 BMCWEB_LOG_INFO << "Unable to find memberId " << item.first;
28748d1b46d7Szhanghch05                 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2875f65af9e8SRichard Marian Thomaiyar                                            item.second.second, item.first);
2876413961deSRichard Marian Thomaiyar                 return;
2877413961deSRichard Marian Thomaiyar             }
2878f65af9e8SRichard Marian Thomaiyar         }
2879413961deSRichard Marian Thomaiyar         // Get the connection to which the memberId belongs
28804f277b54SJayaprakash Mutyala         auto getObjectsWithConnectionCb = [sensorAsyncResp, overrideMap](
28814f277b54SJayaprakash Mutyala                                               const boost::container::flat_set<
28824f277b54SJayaprakash Mutyala                                                   std::string>& /*connections*/,
28834f277b54SJayaprakash Mutyala                                               const std::set<std::pair<
28844f277b54SJayaprakash Mutyala                                                   std::string, std::string>>&
2885413961deSRichard Marian Thomaiyar                                                   objectsWithConnection) {
2886f65af9e8SRichard Marian Thomaiyar             if (objectsWithConnection.size() != overrideMap.size())
2887413961deSRichard Marian Thomaiyar             {
2888413961deSRichard Marian Thomaiyar                 BMCWEB_LOG_INFO
2889f65af9e8SRichard Marian Thomaiyar                     << "Unable to find all objects with proper connection "
2890f65af9e8SRichard Marian Thomaiyar                     << objectsWithConnection.size() << " requested "
2891f65af9e8SRichard Marian Thomaiyar                     << overrideMap.size() << "\n";
28924f277b54SJayaprakash Mutyala                 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2893a0ec28b6SAdrian Ambrożewicz                                            sensorAsyncResp->chassisSubNode ==
2894a0ec28b6SAdrian Ambrożewicz                                                    sensors::node::thermal
2895413961deSRichard Marian Thomaiyar                                                ? "Temperatures"
2896413961deSRichard Marian Thomaiyar                                                : "Voltages",
2897f65af9e8SRichard Marian Thomaiyar                                            "Count");
2898f65af9e8SRichard Marian Thomaiyar                 return;
2899f65af9e8SRichard Marian Thomaiyar             }
2900f65af9e8SRichard Marian Thomaiyar             for (const auto& item : objectsWithConnection)
2901f65af9e8SRichard Marian Thomaiyar             {
290228aa8de5SGeorge Liu                 sdbusplus::message::object_path path(item.first);
290328aa8de5SGeorge Liu                 std::string sensorName = path.filename();
290428aa8de5SGeorge Liu                 if (sensorName.empty())
2905f65af9e8SRichard Marian Thomaiyar                 {
29064f277b54SJayaprakash Mutyala                     messages::internalError(sensorAsyncResp->asyncResp->res);
2907f65af9e8SRichard Marian Thomaiyar                     return;
2908f65af9e8SRichard Marian Thomaiyar                 }
2909f65af9e8SRichard Marian Thomaiyar 
2910f65af9e8SRichard Marian Thomaiyar                 const auto& iterator = overrideMap.find(sensorName);
2911f65af9e8SRichard Marian Thomaiyar                 if (iterator == overrideMap.end())
2912f65af9e8SRichard Marian Thomaiyar                 {
2913f65af9e8SRichard Marian Thomaiyar                     BMCWEB_LOG_INFO << "Unable to find sensor object"
2914f65af9e8SRichard Marian Thomaiyar                                     << item.first << "\n";
29154f277b54SJayaprakash Mutyala                     messages::internalError(sensorAsyncResp->asyncResp->res);
2916413961deSRichard Marian Thomaiyar                     return;
2917413961deSRichard Marian Thomaiyar                 }
2918413961deSRichard Marian Thomaiyar                 crow::connections::systemBus->async_method_call(
2919f65af9e8SRichard Marian Thomaiyar                     [sensorAsyncResp](const boost::system::error_code ec) {
2920413961deSRichard Marian Thomaiyar                         if (ec)
2921413961deSRichard Marian Thomaiyar                         {
29224f277b54SJayaprakash Mutyala                             if (ec.value() ==
29234f277b54SJayaprakash Mutyala                                 boost::system::errc::permission_denied)
29244f277b54SJayaprakash Mutyala                             {
29254f277b54SJayaprakash Mutyala                                 BMCWEB_LOG_WARNING
29264f277b54SJayaprakash Mutyala                                     << "Manufacturing mode is not Enabled...can't "
29274f277b54SJayaprakash Mutyala                                        "Override the sensor value. ";
29284f277b54SJayaprakash Mutyala 
29294f277b54SJayaprakash Mutyala                                 messages::insufficientPrivilege(
29308d1b46d7Szhanghch05                                     sensorAsyncResp->asyncResp->res);
2931413961deSRichard Marian Thomaiyar                                 return;
2932413961deSRichard Marian Thomaiyar                             }
29334f277b54SJayaprakash Mutyala                             BMCWEB_LOG_DEBUG
29344f277b54SJayaprakash Mutyala                                 << "setOverrideValueStatus DBUS error: " << ec;
29354f277b54SJayaprakash Mutyala                             messages::internalError(
29364f277b54SJayaprakash Mutyala                                 sensorAsyncResp->asyncResp->res);
29374f277b54SJayaprakash Mutyala                         }
2938413961deSRichard Marian Thomaiyar                     },
29394f277b54SJayaprakash Mutyala                     item.second, item.first, "org.freedesktop.DBus.Properties",
29404f277b54SJayaprakash Mutyala                     "Set", "xyz.openbmc_project.Sensor.Value", "Value",
2941168e20c1SEd Tanous                     dbus::utility::DbusVariantType(iterator->second.first));
2942f65af9e8SRichard Marian Thomaiyar             }
2943413961deSRichard Marian Thomaiyar         };
2944413961deSRichard Marian Thomaiyar         // Get object with connection for the given sensor name
2945413961deSRichard Marian Thomaiyar         getObjectsWithConnection(sensorAsyncResp, sensorNames,
2946413961deSRichard Marian Thomaiyar                                  std::move(getObjectsWithConnectionCb));
2947413961deSRichard Marian Thomaiyar     };
2948413961deSRichard Marian Thomaiyar     // get full sensor list for the given chassisId and cross verify the sensor.
2949413961deSRichard Marian Thomaiyar     getChassis(sensorAsyncResp, std::move(getChassisSensorListCb));
2950413961deSRichard Marian Thomaiyar }
2951413961deSRichard Marian Thomaiyar 
2952a0ec28b6SAdrian Ambrożewicz /**
2953a0ec28b6SAdrian Ambrożewicz  * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
2954a0ec28b6SAdrian Ambrożewicz  * path of the sensor.
2955a0ec28b6SAdrian Ambrożewicz  *
2956a0ec28b6SAdrian Ambrożewicz  * Function builds valid Redfish response for sensor query of given chassis and
2957a0ec28b6SAdrian Ambrożewicz  * node. It then builds metadata about Redfish<->D-Bus correlations and provides
2958a0ec28b6SAdrian Ambrożewicz  * it to caller in a callback.
2959a0ec28b6SAdrian Ambrożewicz  *
2960a0ec28b6SAdrian Ambrożewicz  * @param chassis   Chassis for which retrieval should be performed
2961a0ec28b6SAdrian Ambrożewicz  * @param node  Node (group) of sensors. See sensors::node for supported values
2962a0ec28b6SAdrian Ambrożewicz  * @param mapComplete   Callback to be called with retrieval result
2963a0ec28b6SAdrian Ambrożewicz  */
2964021d32cfSKrzysztof Grobelny inline void retrieveUriToDbusMap(const std::string& chassis,
2965021d32cfSKrzysztof Grobelny                                  const std::string& node,
2966a0ec28b6SAdrian Ambrożewicz                                  SensorsAsyncResp::DataCompleteCb&& mapComplete)
2967a0ec28b6SAdrian Ambrożewicz {
296802da7c5aSEd Tanous     decltype(sensors::paths)::const_iterator pathIt =
296902da7c5aSEd Tanous         std::find_if(sensors::paths.cbegin(), sensors::paths.cend(),
297002da7c5aSEd Tanous                      [&node](auto&& val) { return val.first == node; });
297102da7c5aSEd Tanous     if (pathIt == sensors::paths.cend())
2972a0ec28b6SAdrian Ambrożewicz     {
2973a0ec28b6SAdrian Ambrożewicz         BMCWEB_LOG_ERROR << "Wrong node provided : " << node;
2974a0ec28b6SAdrian Ambrożewicz         mapComplete(boost::beast::http::status::bad_request, {});
2975a0ec28b6SAdrian Ambrożewicz         return;
2976a0ec28b6SAdrian Ambrożewicz     }
2977d51e072fSKrzysztof Grobelny 
297872374eb7SNan Zhou     auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
2979a0ec28b6SAdrian Ambrożewicz     auto callback =
298072374eb7SNan Zhou         [asyncResp, mapCompleteCb{std::move(mapComplete)}](
2981a0ec28b6SAdrian Ambrożewicz             const boost::beast::http::status status,
2982a0ec28b6SAdrian Ambrożewicz             const boost::container::flat_map<std::string, std::string>&
2983a0ec28b6SAdrian Ambrożewicz                 uriToDbus) { mapCompleteCb(status, uriToDbus); };
2984a0ec28b6SAdrian Ambrożewicz 
2985a0ec28b6SAdrian Ambrożewicz     auto resp = std::make_shared<SensorsAsyncResp>(
2986d51e072fSKrzysztof Grobelny         asyncResp, chassis, pathIt->second, node, std::move(callback));
2987a0ec28b6SAdrian Ambrożewicz     getChassisData(resp);
2988a0ec28b6SAdrian Ambrożewicz }
2989a0ec28b6SAdrian Ambrożewicz 
2990bacb2162SNan Zhou namespace sensors
2991bacb2162SNan Zhou {
2992928fefb9SNan Zhou 
2993bacb2162SNan Zhou inline void getChassisCallback(
2994bacb2162SNan Zhou     const std::shared_ptr<SensorsAsyncResp>& asyncResp,
2995bacb2162SNan Zhou     const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames)
2996bacb2162SNan Zhou {
2997bacb2162SNan Zhou     BMCWEB_LOG_DEBUG << "getChassisCallback enter";
2998bacb2162SNan Zhou 
2999bacb2162SNan Zhou     nlohmann::json& entriesArray =
3000bacb2162SNan Zhou         asyncResp->asyncResp->res.jsonValue["Members"];
3001bacb2162SNan Zhou     for (auto& sensor : *sensorNames)
3002bacb2162SNan Zhou     {
3003bacb2162SNan Zhou         BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor;
3004bacb2162SNan Zhou 
3005bacb2162SNan Zhou         sdbusplus::message::object_path path(sensor);
3006bacb2162SNan Zhou         std::string sensorName = path.filename();
3007bacb2162SNan Zhou         if (sensorName.empty())
3008bacb2162SNan Zhou         {
3009bacb2162SNan Zhou             BMCWEB_LOG_ERROR << "Invalid sensor path: " << sensor;
3010bacb2162SNan Zhou             messages::internalError(asyncResp->asyncResp->res);
3011bacb2162SNan Zhou             return;
3012bacb2162SNan Zhou         }
3013*1476687dSEd Tanous         nlohmann::json::object_t member;
3014*1476687dSEd Tanous         member["@odata.id"] = "/redfish/v1/Chassis/" + asyncResp->chassisId +
3015*1476687dSEd Tanous                               "/" + asyncResp->chassisSubNode + "/" +
3016*1476687dSEd Tanous                               sensorName;
3017*1476687dSEd Tanous         entriesArray.push_back(std::move(member));
3018bacb2162SNan Zhou     }
3019bacb2162SNan Zhou 
3020bacb2162SNan Zhou     asyncResp->asyncResp->res.jsonValue["Members@odata.count"] =
3021bacb2162SNan Zhou         entriesArray.size();
3022bacb2162SNan Zhou     BMCWEB_LOG_DEBUG << "getChassisCallback exit";
3023bacb2162SNan Zhou }
3024bacb2162SNan Zhou } // namespace sensors
3025bacb2162SNan Zhou 
30267e860f15SJohn Edward Broadbent inline void requestRoutesSensorCollection(App& app)
302795a3ecadSAnthony Wilson {
30287e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/")
3029ed398213SEd Tanous         .privileges(redfish::privileges::getSensorCollection)
3030928fefb9SNan Zhou         .methods(
3031928fefb9SNan Zhou             boost::beast::http::verb::get)([&app](
3032928fefb9SNan Zhou                                                const crow::Request& req,
3033928fefb9SNan Zhou                                                const std::shared_ptr<
3034928fefb9SNan Zhou                                                    bmcweb::AsyncResp>& aResp,
30357e860f15SJohn Edward Broadbent                                                const std::string& chassisId) {
3036928fefb9SNan Zhou             query_param::QueryCapabilities capabilities = {
3037928fefb9SNan Zhou                 .canDelegateExpandLevel = 1,
3038928fefb9SNan Zhou             };
3039928fefb9SNan Zhou             query_param::Query delegatedQuery;
3040928fefb9SNan Zhou             if (!redfish::setUpRedfishRouteWithDelegation(
3041928fefb9SNan Zhou                     app, req, aResp->res, delegatedQuery, capabilities))
304245ca1b86SEd Tanous             {
304345ca1b86SEd Tanous                 return;
304445ca1b86SEd Tanous             }
304545ca1b86SEd Tanous 
3046928fefb9SNan Zhou             if (delegatedQuery.expandType != query_param::ExpandType::None)
3047928fefb9SNan Zhou             {
3048928fefb9SNan Zhou                 // we perform efficient expand.
3049928fefb9SNan Zhou                 auto asyncResp = std::make_shared<SensorsAsyncResp>(
305002da7c5aSEd Tanous                     aResp, chassisId, sensors::dbus::sensorPaths,
3051928fefb9SNan Zhou                     sensors::node::sensors,
3052928fefb9SNan Zhou                     /*efficientExpand=*/true);
3053928fefb9SNan Zhou                 getChassisData(asyncResp);
30548d1b46d7Szhanghch05 
3055928fefb9SNan Zhou                 BMCWEB_LOG_DEBUG
3056928fefb9SNan Zhou                     << "SensorCollection doGet exit via efficient expand handler";
3057928fefb9SNan Zhou                 return;
3058928fefb9SNan Zhou             };
3059928fefb9SNan Zhou 
3060928fefb9SNan Zhou             // if there's no efficient expand available, we use the default
3061928fefb9SNan Zhou             // Query Parameters route
3062928fefb9SNan Zhou             auto asyncResp = std::make_shared<SensorsAsyncResp>(
306302da7c5aSEd Tanous                 aResp, chassisId, sensors::dbus::sensorPaths,
3064a0ec28b6SAdrian Ambrożewicz                 sensors::node::sensors);
3065928fefb9SNan Zhou 
3066928fefb9SNan Zhou             // We get all sensors as hyperlinkes in the chassis (this
3067928fefb9SNan Zhou             // implies we reply on the default query parameters handler)
3068928fefb9SNan Zhou             getChassis(asyncResp,
3069bacb2162SNan Zhou                        std::bind_front(sensors::getChassisCallback, asyncResp));
307095a3ecadSAnthony Wilson             BMCWEB_LOG_DEBUG << "SensorCollection doGet exit";
30717e860f15SJohn Edward Broadbent         });
307295a3ecadSAnthony Wilson }
307395a3ecadSAnthony Wilson 
30747e860f15SJohn Edward Broadbent inline void requestRoutesSensor(App& app)
307595a3ecadSAnthony Wilson {
30767e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/")
3077ed398213SEd Tanous         .privileges(redfish::privileges::getSensor)
30787e860f15SJohn Edward Broadbent         .methods(
307945ca1b86SEd Tanous             boost::beast::http::verb::get)([&app](
308045ca1b86SEd Tanous                                                const crow::Request& req,
30817e860f15SJohn Edward Broadbent                                                const std::shared_ptr<
30827e860f15SJohn Edward Broadbent                                                    bmcweb::AsyncResp>& aResp,
30837e860f15SJohn Edward Broadbent                                                const std::string& chassisId,
30847e860f15SJohn Edward Broadbent                                                const std::string& sensorName) {
308545ca1b86SEd Tanous             if (!redfish::setUpRedfishRoute(app, req, aResp->res))
308645ca1b86SEd Tanous             {
308745ca1b86SEd Tanous                 return;
308845ca1b86SEd Tanous             }
308995a3ecadSAnthony Wilson             BMCWEB_LOG_DEBUG << "Sensor doGet enter";
309095a3ecadSAnthony Wilson             std::shared_ptr<SensorsAsyncResp> asyncResp =
309102da7c5aSEd Tanous                 std::make_shared<SensorsAsyncResp>(
309202da7c5aSEd Tanous                     aResp, chassisId, std::span<std::string_view>(),
3093a0ec28b6SAdrian Ambrożewicz                     sensors::node::sensors);
309495a3ecadSAnthony Wilson 
309595a3ecadSAnthony Wilson             const std::array<const char*, 1> interfaces = {
309695a3ecadSAnthony Wilson                 "xyz.openbmc_project.Sensor.Value"};
309795a3ecadSAnthony Wilson 
309895a3ecadSAnthony Wilson             // Get a list of all of the sensors that implement Sensor.Value
309995a3ecadSAnthony Wilson             // and get the path and service name associated with the sensor
310095a3ecadSAnthony Wilson             crow::connections::systemBus->async_method_call(
3101b9d36b47SEd Tanous                 [asyncResp, sensorName](
3102b9d36b47SEd Tanous                     const boost::system::error_code ec,
3103b9d36b47SEd Tanous                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
310495a3ecadSAnthony Wilson                     BMCWEB_LOG_DEBUG << "respHandler1 enter";
310595a3ecadSAnthony Wilson                     if (ec)
310695a3ecadSAnthony Wilson                     {
31078d1b46d7Szhanghch05                         messages::internalError(asyncResp->asyncResp->res);
31087e860f15SJohn Edward Broadbent                         BMCWEB_LOG_ERROR
31097e860f15SJohn Edward Broadbent                             << "Sensor getSensorPaths resp_handler: "
311095a3ecadSAnthony Wilson                             << "Dbus error " << ec;
311195a3ecadSAnthony Wilson                         return;
311295a3ecadSAnthony Wilson                     }
311395a3ecadSAnthony Wilson 
3114b9d36b47SEd Tanous                     dbus::utility::MapperGetSubTreeResponse::const_iterator it =
3115b9d36b47SEd Tanous                         std::find_if(
311695a3ecadSAnthony Wilson                             subtree.begin(), subtree.end(),
311795a3ecadSAnthony Wilson                             [sensorName](
3118b9d36b47SEd Tanous                                 const std::pair<std::string,
31197e860f15SJohn Edward Broadbent                                                 std::vector<std::pair<
3120b9d36b47SEd Tanous                                                     std::string,
3121b9d36b47SEd Tanous                                                     std::vector<std::string>>>>&
312295a3ecadSAnthony Wilson                                     object) {
3123b9d36b47SEd Tanous                                 sdbusplus::message::object_path path(
3124b9d36b47SEd Tanous                                     object.first);
312528aa8de5SGeorge Liu                                 std::string name = path.filename();
312628aa8de5SGeorge Liu                                 if (name.empty())
312795a3ecadSAnthony Wilson                                 {
312895a3ecadSAnthony Wilson                                     BMCWEB_LOG_ERROR << "Invalid sensor path: "
312928aa8de5SGeorge Liu                                                      << object.first;
313095a3ecadSAnthony Wilson                                     return false;
313195a3ecadSAnthony Wilson                                 }
313295a3ecadSAnthony Wilson 
313395a3ecadSAnthony Wilson                                 return name == sensorName;
313495a3ecadSAnthony Wilson                             });
313595a3ecadSAnthony Wilson 
313695a3ecadSAnthony Wilson                     if (it == subtree.end())
313795a3ecadSAnthony Wilson                     {
313895a3ecadSAnthony Wilson                         BMCWEB_LOG_ERROR << "Could not find path for sensor: "
313995a3ecadSAnthony Wilson                                          << sensorName;
31408d1b46d7Szhanghch05                         messages::resourceNotFound(asyncResp->asyncResp->res,
31418d1b46d7Szhanghch05                                                    "Sensor", sensorName);
314295a3ecadSAnthony Wilson                         return;
314395a3ecadSAnthony Wilson                     }
314495a3ecadSAnthony Wilson                     std::string_view sensorPath = (*it).first;
314595a3ecadSAnthony Wilson                     BMCWEB_LOG_DEBUG << "Found sensor path for sensor '"
314695a3ecadSAnthony Wilson                                      << sensorName << "': " << sensorPath;
314795a3ecadSAnthony Wilson 
31487e860f15SJohn Edward Broadbent                     const std::shared_ptr<
31497e860f15SJohn Edward Broadbent                         boost::container::flat_set<std::string>>
315095a3ecadSAnthony Wilson                         sensorList = std::make_shared<
315195a3ecadSAnthony Wilson                             boost::container::flat_set<std::string>>();
315295a3ecadSAnthony Wilson 
315395a3ecadSAnthony Wilson                     sensorList->emplace(sensorPath);
315495a3ecadSAnthony Wilson                     processSensorList(asyncResp, sensorList);
315595a3ecadSAnthony Wilson                     BMCWEB_LOG_DEBUG << "respHandler1 exit";
315695a3ecadSAnthony Wilson                 },
315795a3ecadSAnthony Wilson                 "xyz.openbmc_project.ObjectMapper",
315895a3ecadSAnthony Wilson                 "/xyz/openbmc_project/object_mapper",
315995a3ecadSAnthony Wilson                 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
316095a3ecadSAnthony Wilson                 "/xyz/openbmc_project/sensors", 2, interfaces);
31617e860f15SJohn Edward Broadbent         });
316295a3ecadSAnthony Wilson }
316395a3ecadSAnthony Wilson 
316408777fb0SLewanczyk, Dawid } // namespace redfish
3165