xref: /openbmc/bmcweb/features/redfish/lib/sensors.hpp (revision 02da7c5a626a747ce93aead08ee050678a16c79b)
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 
47*02da7c5aSEd Tanous // clang-format off
48a0ec28b6SAdrian Ambrożewicz namespace dbus
49a0ec28b6SAdrian Ambrożewicz {
50*02da7c5aSEd Tanous auto powerPaths = std::to_array<std::string_view>({
51*02da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/voltage",
52*02da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/power"
53*02da7c5aSEd Tanous });
54c2bf7f99SWludzik, Jozef 
55*02da7c5aSEd Tanous auto sensorPaths = std::to_array<std::string_view>({
56*02da7c5aSEd 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
68*02da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/utilization"
69*02da7c5aSEd Tanous });
70*02da7c5aSEd Tanous 
71*02da7c5aSEd Tanous auto thermalPaths = std::to_array<std::string_view>({
72*02da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/fan_tach",
73a0ec28b6SAdrian Ambrożewicz     "/xyz/openbmc_project/sensors/temperature",
74*02da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/fan_pwm"
75*02da7c5aSEd Tanous });
76*02da7c5aSEd Tanous 
77c2bf7f99SWludzik, Jozef } // namespace dbus
78*02da7c5aSEd Tanous // clang-format on
79*02da7c5aSEd Tanous 
80*02da7c5aSEd Tanous using sensorPair = std::pair<std::string_view, std::span<std::string_view>>;
81*02da7c5aSEd Tanous static constexpr std::array<sensorPair, 3> paths = {
82*02da7c5aSEd Tanous     {{node::power, std::span<std::string_view>(dbus::powerPaths)},
83*02da7c5aSEd Tanous      {node::sensors, std::span<std::string_view>(dbus::sensorPaths)},
84*02da7c5aSEd 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,
195*02da7c5aSEd Tanous                      std::span<std::string_view> typesIn,
196*02da7c5aSEd 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,
205*02da7c5aSEd Tanous                      std::span<std::string_view> typesIn,
206*02da7c5aSEd 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,
217*02da7c5aSEd 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;
282*02da7c5aSEd 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 
466*02da7c5aSEd 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                                 }
12668bd25ccdSJames Feist                                 std::vector<nlohmann::json> 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                                     {
12898bd25ccdSJames Feist                                         redfishCollection.push_back(
12908bd25ccdSJames Feist                                             {{"@odata.id",
12918bd25ccdSJames Feist                                               (*schemaItem)["@odata.id"]}});
12928bd25ccdSJames Feist                                     }
12938bd25ccdSJames Feist                                     else
12948bd25ccdSJames Feist                                     {
12958bd25ccdSJames Feist                                         BMCWEB_LOG_ERROR
12968bd25ccdSJames Feist                                             << "failed to find fan in schema";
12978bd25ccdSJames Feist                                         messages::internalError(
12988d1b46d7Szhanghch05                                             sensorsAsyncResp->asyncResp->res);
12998bd25ccdSJames Feist                                         return;
13008bd25ccdSJames Feist                                     }
13018bd25ccdSJames Feist                                 }
13028bd25ccdSJames Feist 
13033e9e72ebSKuiying Wang                                 size_t minNumNeeded =
130426f6976fSEd Tanous                                     collection->empty()
130526f6976fSEd Tanous                                         ? 0
130626f6976fSEd Tanous                                         : collection->size() - *allowedFailures;
1307271584abSEd Tanous                                 nlohmann::json& jResp =
13088d1b46d7Szhanghch05                                     sensorsAsyncResp->asyncResp->res
13098bd25ccdSJames Feist                                         .jsonValue["Redundancy"];
1310271584abSEd Tanous                                 jResp.push_back(
13118bd25ccdSJames Feist                                     {{"@odata.id",
1312717794d5SAppaRao Puli                                       "/redfish/v1/Chassis/" +
13138bd25ccdSJames Feist                                           sensorsAsyncResp->chassisId + "/" +
13148bd25ccdSJames Feist                                           sensorsAsyncResp->chassisSubNode +
13158bd25ccdSJames Feist                                           "#/Redundancy/" +
1316271584abSEd Tanous                                           std::to_string(jResp.size())},
13178bd25ccdSJames Feist                                      {"@odata.type",
13188bd25ccdSJames Feist                                       "#Redundancy.v1_3_2.Redundancy"},
13193e9e72ebSKuiying Wang                                      {"MinNumNeeded", minNumNeeded},
13208bd25ccdSJames Feist                                      {"MemberId", name},
13218bd25ccdSJames Feist                                      {"Mode", "N+m"},
13228bd25ccdSJames Feist                                      {"Name", name},
13238bd25ccdSJames Feist                                      {"RedundancySet", redfishCollection},
13248bd25ccdSJames Feist                                      {"Status",
13258bd25ccdSJames Feist                                       {{"Health", health},
13268bd25ccdSJames Feist                                        {"State", "Enabled"}}}});
13278bd25ccdSJames Feist                             },
13288bd25ccdSJames Feist                             owner, path, "org.freedesktop.DBus.Properties",
13298bd25ccdSJames Feist                             "GetAll",
13308bd25ccdSJames Feist                             "xyz.openbmc_project.Control.FanRedundancy");
13311e1e598dSJonathan Doman                     });
13328bd25ccdSJames Feist             }
13338bd25ccdSJames Feist         },
13348bd25ccdSJames Feist         "xyz.openbmc_project.ObjectMapper",
13358bd25ccdSJames Feist         "/xyz/openbmc_project/object_mapper",
13368bd25ccdSJames Feist         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
13378bd25ccdSJames Feist         "/xyz/openbmc_project/control", 2,
13388bd25ccdSJames Feist         std::array<const char*, 1>{
13398bd25ccdSJames Feist             "xyz.openbmc_project.Control.FanRedundancy"});
13408bd25ccdSJames Feist }
13418bd25ccdSJames Feist 
1342b5a76932SEd Tanous inline void
134381ce609eSEd Tanous     sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
134449c53ac9SJohnathan Mantey {
13458d1b46d7Szhanghch05     nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue;
134649c53ac9SJohnathan Mantey     std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
134781ce609eSEd Tanous     if (sensorsAsyncResp->chassisSubNode == sensors::node::power)
134849c53ac9SJohnathan Mantey     {
134949c53ac9SJohnathan Mantey         sensorHeaders = {"Voltages", "PowerSupplies"};
135049c53ac9SJohnathan Mantey     }
135149c53ac9SJohnathan Mantey     for (const std::string& sensorGroup : sensorHeaders)
135249c53ac9SJohnathan Mantey     {
135349c53ac9SJohnathan Mantey         nlohmann::json::iterator entry = response.find(sensorGroup);
135449c53ac9SJohnathan Mantey         if (entry != response.end())
135549c53ac9SJohnathan Mantey         {
135649c53ac9SJohnathan Mantey             std::sort(entry->begin(), entry->end(),
135749c53ac9SJohnathan Mantey                       [](nlohmann::json& c1, nlohmann::json& c2) {
135849c53ac9SJohnathan Mantey                           return c1["Name"] < c2["Name"];
135949c53ac9SJohnathan Mantey                       });
136049c53ac9SJohnathan Mantey 
136149c53ac9SJohnathan Mantey             // add the index counts to the end of each entry
136249c53ac9SJohnathan Mantey             size_t count = 0;
136349c53ac9SJohnathan Mantey             for (nlohmann::json& sensorJson : *entry)
136449c53ac9SJohnathan Mantey             {
136549c53ac9SJohnathan Mantey                 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
136649c53ac9SJohnathan Mantey                 if (odata == sensorJson.end())
136749c53ac9SJohnathan Mantey                 {
136849c53ac9SJohnathan Mantey                     continue;
136949c53ac9SJohnathan Mantey                 }
137049c53ac9SJohnathan Mantey                 std::string* value = odata->get_ptr<std::string*>();
137149c53ac9SJohnathan Mantey                 if (value != nullptr)
137249c53ac9SJohnathan Mantey                 {
137349c53ac9SJohnathan Mantey                     *value += std::to_string(count);
137449c53ac9SJohnathan Mantey                     count++;
137581ce609eSEd Tanous                     sensorsAsyncResp->updateUri(sensorJson["Name"], *value);
137649c53ac9SJohnathan Mantey                 }
137749c53ac9SJohnathan Mantey             }
137849c53ac9SJohnathan Mantey         }
137949c53ac9SJohnathan Mantey     }
138049c53ac9SJohnathan Mantey }
138149c53ac9SJohnathan Mantey 
138208777fb0SLewanczyk, Dawid /**
1383adc4f0dbSShawn McCarney  * @brief Finds the inventory item with the specified object path.
1384adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1385adc4f0dbSShawn McCarney  * @param invItemObjPath D-Bus object path of inventory item.
1386adc4f0dbSShawn McCarney  * @return Inventory item within vector, or nullptr if no match found.
13878fb49dd6SShawn McCarney  */
138823a21a1cSEd Tanous inline InventoryItem* findInventoryItem(
1389b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1390adc4f0dbSShawn McCarney     const std::string& invItemObjPath)
13918fb49dd6SShawn McCarney {
1392adc4f0dbSShawn McCarney     for (InventoryItem& inventoryItem : *inventoryItems)
13938fb49dd6SShawn McCarney     {
1394adc4f0dbSShawn McCarney         if (inventoryItem.objectPath == invItemObjPath)
13958fb49dd6SShawn McCarney         {
1396adc4f0dbSShawn McCarney             return &inventoryItem;
13978fb49dd6SShawn McCarney         }
13988fb49dd6SShawn McCarney     }
13998fb49dd6SShawn McCarney     return nullptr;
14008fb49dd6SShawn McCarney }
14018fb49dd6SShawn McCarney 
14028fb49dd6SShawn McCarney /**
1403adc4f0dbSShawn McCarney  * @brief Finds the inventory item associated with the specified sensor.
1404adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1405adc4f0dbSShawn McCarney  * @param sensorObjPath D-Bus object path of sensor.
1406adc4f0dbSShawn McCarney  * @return Inventory item within vector, or nullptr if no match found.
14078fb49dd6SShawn McCarney  */
140823a21a1cSEd Tanous inline InventoryItem* findInventoryItemForSensor(
1409b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1410adc4f0dbSShawn McCarney     const std::string& sensorObjPath)
1411adc4f0dbSShawn McCarney {
1412adc4f0dbSShawn McCarney     for (InventoryItem& inventoryItem : *inventoryItems)
1413adc4f0dbSShawn McCarney     {
1414adc4f0dbSShawn McCarney         if (inventoryItem.sensors.count(sensorObjPath) > 0)
1415adc4f0dbSShawn McCarney         {
1416adc4f0dbSShawn McCarney             return &inventoryItem;
1417adc4f0dbSShawn McCarney         }
1418adc4f0dbSShawn McCarney     }
1419adc4f0dbSShawn McCarney     return nullptr;
1420adc4f0dbSShawn McCarney }
1421adc4f0dbSShawn McCarney 
1422adc4f0dbSShawn McCarney /**
1423d500549bSAnthony Wilson  * @brief Finds the inventory item associated with the specified led path.
1424d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1425d500549bSAnthony Wilson  * @param ledObjPath D-Bus object path of led.
1426d500549bSAnthony Wilson  * @return Inventory item within vector, or nullptr if no match found.
1427d500549bSAnthony Wilson  */
1428d500549bSAnthony Wilson inline InventoryItem*
1429d500549bSAnthony Wilson     findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems,
1430d500549bSAnthony Wilson                             const std::string& ledObjPath)
1431d500549bSAnthony Wilson {
1432d500549bSAnthony Wilson     for (InventoryItem& inventoryItem : inventoryItems)
1433d500549bSAnthony Wilson     {
1434d500549bSAnthony Wilson         if (inventoryItem.ledObjectPath == ledObjPath)
1435d500549bSAnthony Wilson         {
1436d500549bSAnthony Wilson             return &inventoryItem;
1437d500549bSAnthony Wilson         }
1438d500549bSAnthony Wilson     }
1439d500549bSAnthony Wilson     return nullptr;
1440d500549bSAnthony Wilson }
1441d500549bSAnthony Wilson 
1442d500549bSAnthony Wilson /**
1443adc4f0dbSShawn McCarney  * @brief Adds inventory item and associated sensor to specified vector.
1444adc4f0dbSShawn McCarney  *
1445adc4f0dbSShawn McCarney  * Adds a new InventoryItem to the vector if necessary.  Searches for an
1446adc4f0dbSShawn McCarney  * existing InventoryItem with the specified object path.  If not found, one is
1447adc4f0dbSShawn McCarney  * added to the vector.
1448adc4f0dbSShawn McCarney  *
1449adc4f0dbSShawn McCarney  * Next, the specified sensor is added to the set of sensors associated with the
1450adc4f0dbSShawn McCarney  * InventoryItem.
1451adc4f0dbSShawn McCarney  *
1452adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1453adc4f0dbSShawn McCarney  * @param invItemObjPath D-Bus object path of inventory item.
1454adc4f0dbSShawn McCarney  * @param sensorObjPath D-Bus object path of sensor
1455adc4f0dbSShawn McCarney  */
1456b5a76932SEd Tanous inline void addInventoryItem(
1457b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1458b5a76932SEd Tanous     const std::string& invItemObjPath, const std::string& sensorObjPath)
1459adc4f0dbSShawn McCarney {
1460adc4f0dbSShawn McCarney     // Look for inventory item in vector
1461adc4f0dbSShawn McCarney     InventoryItem* inventoryItem =
1462adc4f0dbSShawn McCarney         findInventoryItem(inventoryItems, invItemObjPath);
1463adc4f0dbSShawn McCarney 
1464adc4f0dbSShawn McCarney     // If inventory item doesn't exist in vector, add it
1465adc4f0dbSShawn McCarney     if (inventoryItem == nullptr)
1466adc4f0dbSShawn McCarney     {
1467adc4f0dbSShawn McCarney         inventoryItems->emplace_back(invItemObjPath);
1468adc4f0dbSShawn McCarney         inventoryItem = &(inventoryItems->back());
1469adc4f0dbSShawn McCarney     }
1470adc4f0dbSShawn McCarney 
1471adc4f0dbSShawn McCarney     // Add sensor to set of sensors associated with inventory item
1472adc4f0dbSShawn McCarney     inventoryItem->sensors.emplace(sensorObjPath);
1473adc4f0dbSShawn McCarney }
1474adc4f0dbSShawn McCarney 
1475adc4f0dbSShawn McCarney /**
1476adc4f0dbSShawn McCarney  * @brief Stores D-Bus data in the specified inventory item.
1477adc4f0dbSShawn McCarney  *
1478adc4f0dbSShawn McCarney  * Finds D-Bus data in the specified map of interfaces.  Stores the data in the
1479adc4f0dbSShawn McCarney  * specified InventoryItem.
1480adc4f0dbSShawn McCarney  *
1481adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
1482adc4f0dbSShawn McCarney  * response.
1483adc4f0dbSShawn McCarney  *
1484adc4f0dbSShawn McCarney  * @param inventoryItem Inventory item where data will be stored.
1485adc4f0dbSShawn McCarney  * @param interfacesDict Map containing D-Bus interfaces and their properties
1486adc4f0dbSShawn McCarney  * for the specified inventory item.
1487adc4f0dbSShawn McCarney  */
148823a21a1cSEd Tanous inline void storeInventoryItemData(
1489adc4f0dbSShawn McCarney     InventoryItem& inventoryItem,
1490711ac7a9SEd Tanous     const dbus::utility::DBusInteracesMap& interfacesDict)
14918fb49dd6SShawn McCarney {
1492adc4f0dbSShawn McCarney     // Get properties from Inventory.Item interface
1493711ac7a9SEd Tanous 
14949eb808c1SEd Tanous     for (const auto& [interface, values] : interfacesDict)
14958fb49dd6SShawn McCarney     {
1496711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Item")
14978fb49dd6SShawn McCarney         {
14989eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
1499711ac7a9SEd Tanous             {
1500711ac7a9SEd Tanous                 if (name == "Present")
1501711ac7a9SEd Tanous                 {
1502711ac7a9SEd Tanous                     const bool* value = std::get_if<bool>(&dbusValue);
1503adc4f0dbSShawn McCarney                     if (value != nullptr)
15048fb49dd6SShawn McCarney                     {
1505adc4f0dbSShawn McCarney                         inventoryItem.isPresent = *value;
15068fb49dd6SShawn McCarney                     }
15078fb49dd6SShawn McCarney                 }
15088fb49dd6SShawn McCarney             }
1509711ac7a9SEd Tanous         }
1510adc4f0dbSShawn McCarney         // Check if Inventory.Item.PowerSupply interface is present
1511711ac7a9SEd Tanous 
1512711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Item.PowerSupply")
15138fb49dd6SShawn McCarney         {
1514adc4f0dbSShawn McCarney             inventoryItem.isPowerSupply = true;
15158fb49dd6SShawn McCarney         }
1516adc4f0dbSShawn McCarney 
1517adc4f0dbSShawn McCarney         // Get properties from Inventory.Decorator.Asset interface
1518711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
1519adc4f0dbSShawn McCarney         {
15209eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
1521711ac7a9SEd Tanous             {
1522711ac7a9SEd Tanous                 if (name == "Manufacturer")
1523adc4f0dbSShawn McCarney                 {
1524adc4f0dbSShawn McCarney                     const std::string* value =
1525711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
1526adc4f0dbSShawn McCarney                     if (value != nullptr)
1527adc4f0dbSShawn McCarney                     {
1528adc4f0dbSShawn McCarney                         inventoryItem.manufacturer = *value;
1529adc4f0dbSShawn McCarney                     }
1530adc4f0dbSShawn McCarney                 }
1531711ac7a9SEd Tanous                 if (name == "Model")
1532adc4f0dbSShawn McCarney                 {
1533adc4f0dbSShawn McCarney                     const std::string* value =
1534711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
1535adc4f0dbSShawn McCarney                     if (value != nullptr)
1536adc4f0dbSShawn McCarney                     {
1537adc4f0dbSShawn McCarney                         inventoryItem.model = *value;
1538adc4f0dbSShawn McCarney                     }
1539adc4f0dbSShawn McCarney                 }
1540711ac7a9SEd Tanous                 if (name == "SerialNumber")
1541adc4f0dbSShawn McCarney                 {
1542adc4f0dbSShawn McCarney                     const std::string* value =
1543711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
1544adc4f0dbSShawn McCarney                     if (value != nullptr)
1545adc4f0dbSShawn McCarney                     {
1546adc4f0dbSShawn McCarney                         inventoryItem.serialNumber = *value;
1547adc4f0dbSShawn McCarney                     }
1548adc4f0dbSShawn McCarney                 }
1549711ac7a9SEd Tanous                 if (name == "PartNumber")
1550711ac7a9SEd Tanous                 {
1551711ac7a9SEd Tanous                     const std::string* value =
1552711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
1553711ac7a9SEd Tanous                     if (value != nullptr)
1554711ac7a9SEd Tanous                     {
1555711ac7a9SEd Tanous                         inventoryItem.partNumber = *value;
1556711ac7a9SEd Tanous                     }
1557711ac7a9SEd Tanous                 }
1558711ac7a9SEd Tanous             }
1559adc4f0dbSShawn McCarney         }
1560adc4f0dbSShawn McCarney 
1561711ac7a9SEd Tanous         if (interface ==
1562711ac7a9SEd Tanous             "xyz.openbmc_project.State.Decorator.OperationalStatus")
1563adc4f0dbSShawn McCarney         {
15649eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
1565adc4f0dbSShawn McCarney             {
1566711ac7a9SEd Tanous                 if (name == "Functional")
1567711ac7a9SEd Tanous                 {
1568711ac7a9SEd Tanous                     const bool* value = std::get_if<bool>(&dbusValue);
1569adc4f0dbSShawn McCarney                     if (value != nullptr)
1570adc4f0dbSShawn McCarney                     {
1571adc4f0dbSShawn McCarney                         inventoryItem.isFunctional = *value;
15728fb49dd6SShawn McCarney                     }
15738fb49dd6SShawn McCarney                 }
15748fb49dd6SShawn McCarney             }
15758fb49dd6SShawn McCarney         }
1576711ac7a9SEd Tanous     }
1577711ac7a9SEd Tanous }
15788fb49dd6SShawn McCarney 
15798fb49dd6SShawn McCarney /**
1580adc4f0dbSShawn McCarney  * @brief Gets D-Bus data for inventory items associated with sensors.
15818fb49dd6SShawn McCarney  *
1582adc4f0dbSShawn McCarney  * Uses the specified connections (services) to obtain D-Bus data for inventory
1583adc4f0dbSShawn McCarney  * items associated with sensors.  Stores the resulting data in the
1584adc4f0dbSShawn McCarney  * inventoryItems vector.
15858fb49dd6SShawn McCarney  *
1586adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
1587adc4f0dbSShawn McCarney  * response.
1588adc4f0dbSShawn McCarney  *
1589adc4f0dbSShawn McCarney  * Finds the inventory item data asynchronously.  Invokes callback when data has
1590adc4f0dbSShawn McCarney  * been obtained.
1591adc4f0dbSShawn McCarney  *
1592adc4f0dbSShawn McCarney  * The callback must have the following signature:
1593adc4f0dbSShawn McCarney  *   @code
1594d500549bSAnthony Wilson  *   callback(void)
1595adc4f0dbSShawn McCarney  *   @endcode
1596adc4f0dbSShawn McCarney  *
1597adc4f0dbSShawn McCarney  * This function is called recursively, obtaining data asynchronously from one
1598adc4f0dbSShawn McCarney  * connection in each call.  This ensures the callback is not invoked until the
1599adc4f0dbSShawn McCarney  * last asynchronous function has completed.
16008fb49dd6SShawn McCarney  *
16018fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
1602adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1603adc4f0dbSShawn McCarney  * @param invConnections Connections that provide data for the inventory items.
16048fb49dd6SShawn McCarney  * @param objectMgrPaths Mappings from connection name to DBus object path that
16058fb49dd6SShawn McCarney  * implements ObjectManager.
1606adc4f0dbSShawn McCarney  * @param callback Callback to invoke when inventory data has been obtained.
1607adc4f0dbSShawn McCarney  * @param invConnectionsIndex Current index in invConnections.  Only specified
1608adc4f0dbSShawn McCarney  * in recursive calls to this function.
16098fb49dd6SShawn McCarney  */
1610adc4f0dbSShawn McCarney template <typename Callback>
1611adc4f0dbSShawn McCarney static void getInventoryItemsData(
16128fb49dd6SShawn McCarney     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1613adc4f0dbSShawn McCarney     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
16148fb49dd6SShawn McCarney     std::shared_ptr<boost::container::flat_set<std::string>> invConnections,
16158fb49dd6SShawn McCarney     std::shared_ptr<boost::container::flat_map<std::string, std::string>>
1616adc4f0dbSShawn McCarney         objectMgrPaths,
1617271584abSEd Tanous     Callback&& callback, size_t invConnectionsIndex = 0)
16188fb49dd6SShawn McCarney {
1619adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsData enter";
16208fb49dd6SShawn McCarney 
1621adc4f0dbSShawn McCarney     // If no more connections left, call callback
1622adc4f0dbSShawn McCarney     if (invConnectionsIndex >= invConnections->size())
16238fb49dd6SShawn McCarney     {
1624d500549bSAnthony Wilson         callback();
1625adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
1626adc4f0dbSShawn McCarney         return;
1627adc4f0dbSShawn McCarney     }
1628adc4f0dbSShawn McCarney 
1629adc4f0dbSShawn McCarney     // Get inventory item data from current connection
1630adc4f0dbSShawn McCarney     auto it = invConnections->nth(invConnectionsIndex);
1631adc4f0dbSShawn McCarney     if (it != invConnections->end())
1632adc4f0dbSShawn McCarney     {
1633adc4f0dbSShawn McCarney         const std::string& invConnection = *it;
1634adc4f0dbSShawn McCarney 
16358fb49dd6SShawn McCarney         // Response handler for GetManagedObjects
1636adc4f0dbSShawn McCarney         auto respHandler = [sensorsAsyncResp, inventoryItems, invConnections,
1637f94c4ecfSEd Tanous                             objectMgrPaths,
1638f94c4ecfSEd Tanous                             callback{std::forward<Callback>(callback)},
1639adc4f0dbSShawn McCarney                             invConnectionsIndex](
1640adc4f0dbSShawn McCarney                                const boost::system::error_code ec,
1641711ac7a9SEd Tanous                                dbus::utility::ManagedObjectType& resp) {
1642adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler enter";
16438fb49dd6SShawn McCarney             if (ec)
16448fb49dd6SShawn McCarney             {
16458fb49dd6SShawn McCarney                 BMCWEB_LOG_ERROR
1646adc4f0dbSShawn McCarney                     << "getInventoryItemsData respHandler DBus error " << ec;
16478d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
16488fb49dd6SShawn McCarney                 return;
16498fb49dd6SShawn McCarney             }
16508fb49dd6SShawn McCarney 
16518fb49dd6SShawn McCarney             // Loop through returned object paths
16528fb49dd6SShawn McCarney             for (const auto& objDictEntry : resp)
16538fb49dd6SShawn McCarney             {
16548fb49dd6SShawn McCarney                 const std::string& objPath =
16558fb49dd6SShawn McCarney                     static_cast<const std::string&>(objDictEntry.first);
16568fb49dd6SShawn McCarney 
1657adc4f0dbSShawn McCarney                 // If this object path is one of the specified inventory items
1658adc4f0dbSShawn McCarney                 InventoryItem* inventoryItem =
1659adc4f0dbSShawn McCarney                     findInventoryItem(inventoryItems, objPath);
1660adc4f0dbSShawn McCarney                 if (inventoryItem != nullptr)
16618fb49dd6SShawn McCarney                 {
1662adc4f0dbSShawn McCarney                     // Store inventory data in InventoryItem
1663adc4f0dbSShawn McCarney                     storeInventoryItemData(*inventoryItem, objDictEntry.second);
16648fb49dd6SShawn McCarney                 }
16658fb49dd6SShawn McCarney             }
16668fb49dd6SShawn McCarney 
1667adc4f0dbSShawn McCarney             // Recurse to get inventory item data from next connection
1668adc4f0dbSShawn McCarney             getInventoryItemsData(sensorsAsyncResp, inventoryItems,
1669adc4f0dbSShawn McCarney                                   invConnections, objectMgrPaths,
1670adc4f0dbSShawn McCarney                                   std::move(callback), invConnectionsIndex + 1);
1671adc4f0dbSShawn McCarney 
1672adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler exit";
16738fb49dd6SShawn McCarney         };
16748fb49dd6SShawn McCarney 
16758fb49dd6SShawn McCarney         // Find DBus object path that implements ObjectManager for the current
16768fb49dd6SShawn McCarney         // connection.  If no mapping found, default to "/".
16778fb49dd6SShawn McCarney         auto iter = objectMgrPaths->find(invConnection);
16788fb49dd6SShawn McCarney         const std::string& objectMgrPath =
16798fb49dd6SShawn McCarney             (iter != objectMgrPaths->end()) ? iter->second : "/";
16808fb49dd6SShawn McCarney         BMCWEB_LOG_DEBUG << "ObjectManager path for " << invConnection << " is "
16818fb49dd6SShawn McCarney                          << objectMgrPath;
16828fb49dd6SShawn McCarney 
16838fb49dd6SShawn McCarney         // Get all object paths and their interfaces for current connection
16848fb49dd6SShawn McCarney         crow::connections::systemBus->async_method_call(
16858fb49dd6SShawn McCarney             std::move(respHandler), invConnection, objectMgrPath,
16868fb49dd6SShawn McCarney             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
16878fb49dd6SShawn McCarney     }
16888fb49dd6SShawn McCarney 
1689adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
16908fb49dd6SShawn McCarney }
16918fb49dd6SShawn McCarney 
16928fb49dd6SShawn McCarney /**
1693adc4f0dbSShawn McCarney  * @brief Gets connections that provide D-Bus data for inventory items.
16948fb49dd6SShawn McCarney  *
1695adc4f0dbSShawn McCarney  * Gets the D-Bus connections (services) that provide data for the inventory
1696adc4f0dbSShawn McCarney  * items that are associated with sensors.
16978fb49dd6SShawn McCarney  *
16988fb49dd6SShawn McCarney  * Finds the connections asynchronously.  Invokes callback when information has
16998fb49dd6SShawn McCarney  * been obtained.
17008fb49dd6SShawn McCarney  *
17018fb49dd6SShawn McCarney  * The callback must have the following signature:
17028fb49dd6SShawn McCarney  *   @code
17038fb49dd6SShawn McCarney  *   callback(std::shared_ptr<boost::container::flat_set<std::string>>
17048fb49dd6SShawn McCarney  *            invConnections)
17058fb49dd6SShawn McCarney  *   @endcode
17068fb49dd6SShawn McCarney  *
17078fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
1708adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
17098fb49dd6SShawn McCarney  * @param callback Callback to invoke when connections have been obtained.
17108fb49dd6SShawn McCarney  */
17118fb49dd6SShawn McCarney template <typename Callback>
17128fb49dd6SShawn McCarney static void getInventoryItemsConnections(
1713b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1714b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
17158fb49dd6SShawn McCarney     Callback&& callback)
17168fb49dd6SShawn McCarney {
17178fb49dd6SShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsConnections enter";
17188fb49dd6SShawn McCarney 
17198fb49dd6SShawn McCarney     const std::string path = "/xyz/openbmc_project/inventory";
1720adc4f0dbSShawn McCarney     const std::array<std::string, 4> interfaces = {
17218fb49dd6SShawn McCarney         "xyz.openbmc_project.Inventory.Item",
1722adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Item.PowerSupply",
1723adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Decorator.Asset",
17248fb49dd6SShawn McCarney         "xyz.openbmc_project.State.Decorator.OperationalStatus"};
17258fb49dd6SShawn McCarney 
17268fb49dd6SShawn McCarney     // Response handler for parsing output from GetSubTree
1727f94c4ecfSEd Tanous     auto respHandler = [callback{std::forward<Callback>(callback)},
1728b9d36b47SEd Tanous                         sensorsAsyncResp, inventoryItems](
1729b9d36b47SEd Tanous                            const boost::system::error_code ec,
1730b9d36b47SEd Tanous                            const dbus::utility::MapperGetSubTreeResponse&
1731b9d36b47SEd Tanous                                subtree) {
17328fb49dd6SShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler enter";
17338fb49dd6SShawn McCarney         if (ec)
17348fb49dd6SShawn McCarney         {
17358d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
17368fb49dd6SShawn McCarney             BMCWEB_LOG_ERROR
17378fb49dd6SShawn McCarney                 << "getInventoryItemsConnections respHandler DBus error " << ec;
17388fb49dd6SShawn McCarney             return;
17398fb49dd6SShawn McCarney         }
17408fb49dd6SShawn McCarney 
17418fb49dd6SShawn McCarney         // Make unique list of connections for desired inventory items
17428fb49dd6SShawn McCarney         std::shared_ptr<boost::container::flat_set<std::string>>
17438fb49dd6SShawn McCarney             invConnections =
17448fb49dd6SShawn McCarney                 std::make_shared<boost::container::flat_set<std::string>>();
17458fb49dd6SShawn McCarney         invConnections->reserve(8);
17468fb49dd6SShawn McCarney 
17478fb49dd6SShawn McCarney         // Loop through objects from GetSubTree
17488fb49dd6SShawn McCarney         for (const std::pair<
17498fb49dd6SShawn McCarney                  std::string,
17508fb49dd6SShawn McCarney                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
17518fb49dd6SShawn McCarney                  object : subtree)
17528fb49dd6SShawn McCarney         {
1753adc4f0dbSShawn McCarney             // Check if object path is one of the specified inventory items
17548fb49dd6SShawn McCarney             const std::string& objPath = object.first;
1755adc4f0dbSShawn McCarney             if (findInventoryItem(inventoryItems, objPath) != nullptr)
17568fb49dd6SShawn McCarney             {
17578fb49dd6SShawn McCarney                 // Store all connections to inventory item
17588fb49dd6SShawn McCarney                 for (const std::pair<std::string, std::vector<std::string>>&
17598fb49dd6SShawn McCarney                          objData : object.second)
17608fb49dd6SShawn McCarney                 {
17618fb49dd6SShawn McCarney                     const std::string& invConnection = objData.first;
17628fb49dd6SShawn McCarney                     invConnections->insert(invConnection);
17638fb49dd6SShawn McCarney                 }
17648fb49dd6SShawn McCarney             }
17658fb49dd6SShawn McCarney         }
1766d500549bSAnthony Wilson 
17678fb49dd6SShawn McCarney         callback(invConnections);
17688fb49dd6SShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler exit";
17698fb49dd6SShawn McCarney     };
17708fb49dd6SShawn McCarney 
17718fb49dd6SShawn McCarney     // Make call to ObjectMapper to find all inventory items
17728fb49dd6SShawn McCarney     crow::connections::systemBus->async_method_call(
17738fb49dd6SShawn McCarney         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
17748fb49dd6SShawn McCarney         "/xyz/openbmc_project/object_mapper",
17758fb49dd6SShawn McCarney         "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
17768fb49dd6SShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsConnections exit";
17778fb49dd6SShawn McCarney }
17788fb49dd6SShawn McCarney 
17798fb49dd6SShawn McCarney /**
1780adc4f0dbSShawn McCarney  * @brief Gets associations from sensors to inventory items.
17818fb49dd6SShawn McCarney  *
17828fb49dd6SShawn McCarney  * Looks for ObjectMapper associations from the specified sensors to related
1783d500549bSAnthony Wilson  * inventory items. Then finds the associations from those inventory items to
1784d500549bSAnthony Wilson  * their LEDs, if any.
17858fb49dd6SShawn McCarney  *
17868fb49dd6SShawn McCarney  * Finds the inventory items asynchronously.  Invokes callback when information
17878fb49dd6SShawn McCarney  * has been obtained.
17888fb49dd6SShawn McCarney  *
17898fb49dd6SShawn McCarney  * The callback must have the following signature:
17908fb49dd6SShawn McCarney  *   @code
1791adc4f0dbSShawn McCarney  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
17928fb49dd6SShawn McCarney  *   @endcode
17938fb49dd6SShawn McCarney  *
17948fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
17958fb49dd6SShawn McCarney  * @param sensorNames All sensors within the current chassis.
17968fb49dd6SShawn McCarney  * @param objectMgrPaths Mappings from connection name to DBus object path that
17978fb49dd6SShawn McCarney  * implements ObjectManager.
17988fb49dd6SShawn McCarney  * @param callback Callback to invoke when inventory items have been obtained.
17998fb49dd6SShawn McCarney  */
18008fb49dd6SShawn McCarney template <typename Callback>
1801adc4f0dbSShawn McCarney static void getInventoryItemAssociations(
1802b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1803b5a76932SEd Tanous     const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames,
1804b5a76932SEd Tanous     const std::shared_ptr<boost::container::flat_map<std::string, std::string>>&
18058fb49dd6SShawn McCarney         objectMgrPaths,
18068fb49dd6SShawn McCarney     Callback&& callback)
18078fb49dd6SShawn McCarney {
1808adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemAssociations enter";
18098fb49dd6SShawn McCarney 
18108fb49dd6SShawn McCarney     // Response handler for GetManagedObjects
1811f94c4ecfSEd Tanous     auto respHandler = [callback{std::forward<Callback>(callback)},
1812f94c4ecfSEd Tanous                         sensorsAsyncResp,
18138fb49dd6SShawn McCarney                         sensorNames](const boost::system::error_code ec,
18148fb49dd6SShawn McCarney                                      dbus::utility::ManagedObjectType& resp) {
1815adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler enter";
18168fb49dd6SShawn McCarney         if (ec)
18178fb49dd6SShawn McCarney         {
1818adc4f0dbSShawn McCarney             BMCWEB_LOG_ERROR
1819adc4f0dbSShawn McCarney                 << "getInventoryItemAssociations respHandler DBus error " << ec;
18208d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
18218fb49dd6SShawn McCarney             return;
18228fb49dd6SShawn McCarney         }
18238fb49dd6SShawn McCarney 
1824adc4f0dbSShawn McCarney         // Create vector to hold list of inventory items
1825adc4f0dbSShawn McCarney         std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1826adc4f0dbSShawn McCarney             std::make_shared<std::vector<InventoryItem>>();
1827adc4f0dbSShawn McCarney 
18288fb49dd6SShawn McCarney         // Loop through returned object paths
18298fb49dd6SShawn McCarney         std::string sensorAssocPath;
18308fb49dd6SShawn McCarney         sensorAssocPath.reserve(128); // avoid memory allocations
18318fb49dd6SShawn McCarney         for (const auto& objDictEntry : resp)
18328fb49dd6SShawn McCarney         {
18338fb49dd6SShawn McCarney             const std::string& objPath =
18348fb49dd6SShawn McCarney                 static_cast<const std::string&>(objDictEntry.first);
18358fb49dd6SShawn McCarney 
18368fb49dd6SShawn McCarney             // If path is inventory association for one of the specified sensors
18378fb49dd6SShawn McCarney             for (const std::string& sensorName : *sensorNames)
18388fb49dd6SShawn McCarney             {
18398fb49dd6SShawn McCarney                 sensorAssocPath = sensorName;
18408fb49dd6SShawn McCarney                 sensorAssocPath += "/inventory";
18418fb49dd6SShawn McCarney                 if (objPath == sensorAssocPath)
18428fb49dd6SShawn McCarney                 {
18438fb49dd6SShawn McCarney                     // Get Association interface for object path
1844711ac7a9SEd Tanous                     for (const auto& [interface, values] : objDictEntry.second)
18458fb49dd6SShawn McCarney                     {
1846711ac7a9SEd Tanous                         if (interface == "xyz.openbmc_project.Association")
1847711ac7a9SEd Tanous                         {
1848711ac7a9SEd Tanous                             for (const auto& [valueName, value] : values)
1849711ac7a9SEd Tanous                             {
1850711ac7a9SEd Tanous                                 if (valueName == "endpoints")
18518fb49dd6SShawn McCarney                                 {
18528fb49dd6SShawn McCarney                                     const std::vector<std::string>* endpoints =
18538fb49dd6SShawn McCarney                                         std::get_if<std::vector<std::string>>(
1854711ac7a9SEd Tanous                                             &value);
1855711ac7a9SEd Tanous                                     if ((endpoints != nullptr) &&
1856711ac7a9SEd Tanous                                         !endpoints->empty())
18578fb49dd6SShawn McCarney                                     {
1858adc4f0dbSShawn McCarney                                         // Add inventory item to vector
1859adc4f0dbSShawn McCarney                                         const std::string& invItemPath =
1860adc4f0dbSShawn McCarney                                             endpoints->front();
1861711ac7a9SEd Tanous                                         addInventoryItem(inventoryItems,
1862711ac7a9SEd Tanous                                                          invItemPath,
1863adc4f0dbSShawn McCarney                                                          sensorName);
18648fb49dd6SShawn McCarney                                     }
18658fb49dd6SShawn McCarney                                 }
18668fb49dd6SShawn McCarney                             }
1867711ac7a9SEd Tanous                         }
1868711ac7a9SEd Tanous                     }
18698fb49dd6SShawn McCarney                     break;
18708fb49dd6SShawn McCarney                 }
18718fb49dd6SShawn McCarney             }
18728fb49dd6SShawn McCarney         }
18738fb49dd6SShawn McCarney 
1874d500549bSAnthony Wilson         // Now loop through the returned object paths again, this time to
1875d500549bSAnthony Wilson         // find the leds associated with the inventory items we just found
1876d500549bSAnthony Wilson         std::string inventoryAssocPath;
1877d500549bSAnthony Wilson         inventoryAssocPath.reserve(128); // avoid memory allocations
1878d500549bSAnthony Wilson         for (const auto& objDictEntry : resp)
1879d500549bSAnthony Wilson         {
1880d500549bSAnthony Wilson             const std::string& objPath =
1881d500549bSAnthony Wilson                 static_cast<const std::string&>(objDictEntry.first);
1882d500549bSAnthony Wilson 
1883d500549bSAnthony Wilson             for (InventoryItem& inventoryItem : *inventoryItems)
1884d500549bSAnthony Wilson             {
1885d500549bSAnthony Wilson                 inventoryAssocPath = inventoryItem.objectPath;
1886d500549bSAnthony Wilson                 inventoryAssocPath += "/leds";
1887d500549bSAnthony Wilson                 if (objPath == inventoryAssocPath)
1888d500549bSAnthony Wilson                 {
1889711ac7a9SEd Tanous                     for (const auto& [interface, values] : objDictEntry.second)
1890d500549bSAnthony Wilson                     {
1891711ac7a9SEd Tanous                         if (interface == "xyz.openbmc_project.Association")
1892711ac7a9SEd Tanous                         {
1893711ac7a9SEd Tanous                             for (const auto& [valueName, value] : values)
1894711ac7a9SEd Tanous                             {
1895711ac7a9SEd Tanous                                 if (valueName == "endpoints")
1896d500549bSAnthony Wilson                                 {
1897d500549bSAnthony Wilson                                     const std::vector<std::string>* endpoints =
1898d500549bSAnthony Wilson                                         std::get_if<std::vector<std::string>>(
1899711ac7a9SEd Tanous                                             &value);
1900711ac7a9SEd Tanous                                     if ((endpoints != nullptr) &&
1901711ac7a9SEd Tanous                                         !endpoints->empty())
1902d500549bSAnthony Wilson                                     {
1903711ac7a9SEd Tanous                                         // Add inventory item to vector
1904d500549bSAnthony Wilson                                         // Store LED path in inventory item
1905711ac7a9SEd Tanous                                         const std::string& ledPath =
1906711ac7a9SEd Tanous                                             endpoints->front();
1907d500549bSAnthony Wilson                                         inventoryItem.ledObjectPath = ledPath;
1908d500549bSAnthony Wilson                                     }
1909d500549bSAnthony Wilson                                 }
1910d500549bSAnthony Wilson                             }
1911711ac7a9SEd Tanous                         }
1912711ac7a9SEd Tanous                     }
1913711ac7a9SEd Tanous 
1914d500549bSAnthony Wilson                     break;
1915d500549bSAnthony Wilson                 }
1916d500549bSAnthony Wilson             }
1917d500549bSAnthony Wilson         }
1918adc4f0dbSShawn McCarney         callback(inventoryItems);
1919adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler exit";
19208fb49dd6SShawn McCarney     };
19218fb49dd6SShawn McCarney 
19228fb49dd6SShawn McCarney     // Find DBus object path that implements ObjectManager for ObjectMapper
19238fb49dd6SShawn McCarney     std::string connection = "xyz.openbmc_project.ObjectMapper";
19248fb49dd6SShawn McCarney     auto iter = objectMgrPaths->find(connection);
19258fb49dd6SShawn McCarney     const std::string& objectMgrPath =
19268fb49dd6SShawn McCarney         (iter != objectMgrPaths->end()) ? iter->second : "/";
19278fb49dd6SShawn McCarney     BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
19288fb49dd6SShawn McCarney                      << objectMgrPath;
19298fb49dd6SShawn McCarney 
19308fb49dd6SShawn McCarney     // Call GetManagedObjects on the ObjectMapper to get all associations
19318fb49dd6SShawn McCarney     crow::connections::systemBus->async_method_call(
19328fb49dd6SShawn McCarney         std::move(respHandler), connection, objectMgrPath,
19338fb49dd6SShawn McCarney         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
19348fb49dd6SShawn McCarney 
1935adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemAssociations exit";
19368fb49dd6SShawn McCarney }
19378fb49dd6SShawn McCarney 
19388fb49dd6SShawn McCarney /**
1939d500549bSAnthony Wilson  * @brief Gets D-Bus data for inventory item leds associated with sensors.
1940d500549bSAnthony Wilson  *
1941d500549bSAnthony Wilson  * Uses the specified connections (services) to obtain D-Bus data for inventory
1942d500549bSAnthony Wilson  * item leds associated with sensors.  Stores the resulting data in the
1943d500549bSAnthony Wilson  * inventoryItems vector.
1944d500549bSAnthony Wilson  *
1945d500549bSAnthony Wilson  * This data is later used to provide sensor property values in the JSON
1946d500549bSAnthony Wilson  * response.
1947d500549bSAnthony Wilson  *
1948d500549bSAnthony Wilson  * Finds the inventory item led data asynchronously.  Invokes callback when data
1949d500549bSAnthony Wilson  * has been obtained.
1950d500549bSAnthony Wilson  *
1951d500549bSAnthony Wilson  * The callback must have the following signature:
1952d500549bSAnthony Wilson  *   @code
195342cbe538SGunnar Mills  *   callback()
1954d500549bSAnthony Wilson  *   @endcode
1955d500549bSAnthony Wilson  *
1956d500549bSAnthony Wilson  * This function is called recursively, obtaining data asynchronously from one
1957d500549bSAnthony Wilson  * connection in each call.  This ensures the callback is not invoked until the
1958d500549bSAnthony Wilson  * last asynchronous function has completed.
1959d500549bSAnthony Wilson  *
1960d500549bSAnthony Wilson  * @param sensorsAsyncResp Pointer to object holding response data.
1961d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1962d500549bSAnthony Wilson  * @param ledConnections Connections that provide data for the inventory leds.
1963d500549bSAnthony Wilson  * @param callback Callback to invoke when inventory data has been obtained.
1964d500549bSAnthony Wilson  * @param ledConnectionsIndex Current index in ledConnections.  Only specified
1965d500549bSAnthony Wilson  * in recursive calls to this function.
1966d500549bSAnthony Wilson  */
1967d500549bSAnthony Wilson template <typename Callback>
1968d500549bSAnthony Wilson void getInventoryLedData(
1969d500549bSAnthony Wilson     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1970d500549bSAnthony Wilson     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1971d500549bSAnthony Wilson     std::shared_ptr<boost::container::flat_map<std::string, std::string>>
1972d500549bSAnthony Wilson         ledConnections,
1973d500549bSAnthony Wilson     Callback&& callback, size_t ledConnectionsIndex = 0)
1974d500549bSAnthony Wilson {
1975d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLedData enter";
1976d500549bSAnthony Wilson 
1977d500549bSAnthony Wilson     // If no more connections left, call callback
1978d500549bSAnthony Wilson     if (ledConnectionsIndex >= ledConnections->size())
1979d500549bSAnthony Wilson     {
198042cbe538SGunnar Mills         callback();
1981d500549bSAnthony Wilson         BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1982d500549bSAnthony Wilson         return;
1983d500549bSAnthony Wilson     }
1984d500549bSAnthony Wilson 
1985d500549bSAnthony Wilson     // Get inventory item data from current connection
1986d500549bSAnthony Wilson     auto it = ledConnections->nth(ledConnectionsIndex);
1987d500549bSAnthony Wilson     if (it != ledConnections->end())
1988d500549bSAnthony Wilson     {
1989d500549bSAnthony Wilson         const std::string& ledPath = (*it).first;
1990d500549bSAnthony Wilson         const std::string& ledConnection = (*it).second;
1991d500549bSAnthony Wilson         // Response handler for Get State property
19921e1e598dSJonathan Doman         auto respHandler =
19931e1e598dSJonathan Doman             [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
1994f94c4ecfSEd Tanous              callback{std::forward<Callback>(callback)}, ledConnectionsIndex](
19951e1e598dSJonathan Doman                 const boost::system::error_code ec, const std::string& state) {
1996d500549bSAnthony Wilson                 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler enter";
1997d500549bSAnthony Wilson                 if (ec)
1998d500549bSAnthony Wilson                 {
1999d500549bSAnthony Wilson                     BMCWEB_LOG_ERROR
2000d500549bSAnthony Wilson                         << "getInventoryLedData respHandler DBus error " << ec;
20018d1b46d7Szhanghch05                     messages::internalError(sensorsAsyncResp->asyncResp->res);
2002d500549bSAnthony Wilson                     return;
2003d500549bSAnthony Wilson                 }
2004d500549bSAnthony Wilson 
20051e1e598dSJonathan Doman                 BMCWEB_LOG_DEBUG << "Led state: " << state;
2006d500549bSAnthony Wilson                 // Find inventory item with this LED object path
2007d500549bSAnthony Wilson                 InventoryItem* inventoryItem =
2008d500549bSAnthony Wilson                     findInventoryItemForLed(*inventoryItems, ledPath);
2009d500549bSAnthony Wilson                 if (inventoryItem != nullptr)
2010d500549bSAnthony Wilson                 {
2011d500549bSAnthony Wilson                     // Store LED state in InventoryItem
20121e1e598dSJonathan Doman                     if (boost::ends_with(state, "On"))
2013d500549bSAnthony Wilson                     {
2014d500549bSAnthony Wilson                         inventoryItem->ledState = LedState::ON;
2015d500549bSAnthony Wilson                     }
20161e1e598dSJonathan Doman                     else if (boost::ends_with(state, "Blink"))
2017d500549bSAnthony Wilson                     {
2018d500549bSAnthony Wilson                         inventoryItem->ledState = LedState::BLINK;
2019d500549bSAnthony Wilson                     }
20201e1e598dSJonathan Doman                     else if (boost::ends_with(state, "Off"))
2021d500549bSAnthony Wilson                     {
2022d500549bSAnthony Wilson                         inventoryItem->ledState = LedState::OFF;
2023d500549bSAnthony Wilson                     }
2024d500549bSAnthony Wilson                     else
2025d500549bSAnthony Wilson                     {
2026d500549bSAnthony Wilson                         inventoryItem->ledState = LedState::UNKNOWN;
2027d500549bSAnthony Wilson                     }
2028d500549bSAnthony Wilson                 }
2029d500549bSAnthony Wilson 
2030d500549bSAnthony Wilson                 // Recurse to get LED data from next connection
2031d500549bSAnthony Wilson                 getInventoryLedData(sensorsAsyncResp, inventoryItems,
2032d500549bSAnthony Wilson                                     ledConnections, std::move(callback),
2033d500549bSAnthony Wilson                                     ledConnectionsIndex + 1);
2034d500549bSAnthony Wilson 
2035d500549bSAnthony Wilson                 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler exit";
2036d500549bSAnthony Wilson             };
2037d500549bSAnthony Wilson 
2038d500549bSAnthony Wilson         // Get the State property for the current LED
20391e1e598dSJonathan Doman         sdbusplus::asio::getProperty<std::string>(
20401e1e598dSJonathan Doman             *crow::connections::systemBus, ledConnection, ledPath,
20411e1e598dSJonathan Doman             "xyz.openbmc_project.Led.Physical", "State",
20421e1e598dSJonathan Doman             std::move(respHandler));
2043d500549bSAnthony Wilson     }
2044d500549bSAnthony Wilson 
2045d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
2046d500549bSAnthony Wilson }
2047d500549bSAnthony Wilson 
2048d500549bSAnthony Wilson /**
2049d500549bSAnthony Wilson  * @brief Gets LED data for LEDs associated with given inventory items.
2050d500549bSAnthony Wilson  *
2051d500549bSAnthony Wilson  * Gets the D-Bus connections (services) that provide LED data for the LEDs
2052d500549bSAnthony Wilson  * associated with the specified inventory items.  Then gets the LED data from
2053d500549bSAnthony Wilson  * each connection and stores it in the inventory item.
2054d500549bSAnthony Wilson  *
2055d500549bSAnthony Wilson  * This data is later used to provide sensor property values in the JSON
2056d500549bSAnthony Wilson  * response.
2057d500549bSAnthony Wilson  *
2058d500549bSAnthony Wilson  * Finds the LED data asynchronously.  Invokes callback when information has
2059d500549bSAnthony Wilson  * been obtained.
2060d500549bSAnthony Wilson  *
2061d500549bSAnthony Wilson  * The callback must have the following signature:
2062d500549bSAnthony Wilson  *   @code
206342cbe538SGunnar Mills  *   callback()
2064d500549bSAnthony Wilson  *   @endcode
2065d500549bSAnthony Wilson  *
2066d500549bSAnthony Wilson  * @param sensorsAsyncResp Pointer to object holding response data.
2067d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
2068d500549bSAnthony Wilson  * @param callback Callback to invoke when inventory items have been obtained.
2069d500549bSAnthony Wilson  */
2070d500549bSAnthony Wilson template <typename Callback>
2071d500549bSAnthony Wilson void getInventoryLeds(
2072d500549bSAnthony Wilson     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2073d500549bSAnthony Wilson     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2074d500549bSAnthony Wilson     Callback&& callback)
2075d500549bSAnthony Wilson {
2076d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLeds enter";
2077d500549bSAnthony Wilson 
2078d500549bSAnthony Wilson     const std::string path = "/xyz/openbmc_project";
2079d500549bSAnthony Wilson     const std::array<std::string, 1> interfaces = {
2080d500549bSAnthony Wilson         "xyz.openbmc_project.Led.Physical"};
2081d500549bSAnthony Wilson 
2082d500549bSAnthony Wilson     // Response handler for parsing output from GetSubTree
2083f94c4ecfSEd Tanous     auto respHandler = [callback{std::forward<Callback>(callback)},
2084b9d36b47SEd Tanous                         sensorsAsyncResp, inventoryItems](
2085b9d36b47SEd Tanous                            const boost::system::error_code ec,
2086b9d36b47SEd Tanous                            const dbus::utility::MapperGetSubTreeResponse&
2087b9d36b47SEd Tanous                                subtree) {
2088d500549bSAnthony Wilson         BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler enter";
2089d500549bSAnthony Wilson         if (ec)
2090d500549bSAnthony Wilson         {
20918d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
2092d500549bSAnthony Wilson             BMCWEB_LOG_ERROR << "getInventoryLeds respHandler DBus error "
2093d500549bSAnthony Wilson                              << ec;
2094d500549bSAnthony Wilson             return;
2095d500549bSAnthony Wilson         }
2096d500549bSAnthony Wilson 
2097d500549bSAnthony Wilson         // Build map of LED object paths to connections
2098d500549bSAnthony Wilson         std::shared_ptr<boost::container::flat_map<std::string, std::string>>
2099d500549bSAnthony Wilson             ledConnections = std::make_shared<
2100d500549bSAnthony Wilson                 boost::container::flat_map<std::string, std::string>>();
2101d500549bSAnthony Wilson 
2102d500549bSAnthony Wilson         // Loop through objects from GetSubTree
2103d500549bSAnthony Wilson         for (const std::pair<
2104d500549bSAnthony Wilson                  std::string,
2105d500549bSAnthony Wilson                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
2106d500549bSAnthony Wilson                  object : subtree)
2107d500549bSAnthony Wilson         {
2108d500549bSAnthony Wilson             // Check if object path is LED for one of the specified inventory
2109d500549bSAnthony Wilson             // items
2110d500549bSAnthony Wilson             const std::string& ledPath = object.first;
2111d500549bSAnthony Wilson             if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr)
2112d500549bSAnthony Wilson             {
2113d500549bSAnthony Wilson                 // Add mapping from ledPath to connection
2114d500549bSAnthony Wilson                 const std::string& connection = object.second.begin()->first;
2115d500549bSAnthony Wilson                 (*ledConnections)[ledPath] = connection;
2116d500549bSAnthony Wilson                 BMCWEB_LOG_DEBUG << "Added mapping " << ledPath << " -> "
2117d500549bSAnthony Wilson                                  << connection;
2118d500549bSAnthony Wilson             }
2119d500549bSAnthony Wilson         }
2120d500549bSAnthony Wilson 
2121d500549bSAnthony Wilson         getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections,
2122d500549bSAnthony Wilson                             std::move(callback));
2123d500549bSAnthony Wilson         BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler exit";
2124d500549bSAnthony Wilson     };
2125d500549bSAnthony Wilson     // Make call to ObjectMapper to find all inventory items
2126d500549bSAnthony Wilson     crow::connections::systemBus->async_method_call(
2127d500549bSAnthony Wilson         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
2128d500549bSAnthony Wilson         "/xyz/openbmc_project/object_mapper",
2129d500549bSAnthony Wilson         "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
2130d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLeds exit";
2131d500549bSAnthony Wilson }
2132d500549bSAnthony Wilson 
2133d500549bSAnthony Wilson /**
213442cbe538SGunnar Mills  * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
213542cbe538SGunnar Mills  *
213642cbe538SGunnar Mills  * Uses the specified connections (services) (currently assumes just one) to
213742cbe538SGunnar Mills  * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
213842cbe538SGunnar Mills  * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
213942cbe538SGunnar Mills  *
214042cbe538SGunnar Mills  * This data is later used to provide sensor property values in the JSON
214142cbe538SGunnar Mills  * response.
214242cbe538SGunnar Mills  *
214342cbe538SGunnar Mills  * Finds the Power Supply Attributes data asynchronously.  Invokes callback
214442cbe538SGunnar Mills  * when data has been obtained.
214542cbe538SGunnar Mills  *
214642cbe538SGunnar Mills  * The callback must have the following signature:
214742cbe538SGunnar Mills  *   @code
214842cbe538SGunnar Mills  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
214942cbe538SGunnar Mills  *   @endcode
215042cbe538SGunnar Mills  *
215142cbe538SGunnar Mills  * @param sensorsAsyncResp Pointer to object holding response data.
215242cbe538SGunnar Mills  * @param inventoryItems D-Bus inventory items associated with sensors.
215342cbe538SGunnar Mills  * @param psAttributesConnections Connections that provide data for the Power
215442cbe538SGunnar Mills  *        Supply Attributes
215542cbe538SGunnar Mills  * @param callback Callback to invoke when data has been obtained.
215642cbe538SGunnar Mills  */
215742cbe538SGunnar Mills template <typename Callback>
215842cbe538SGunnar Mills void getPowerSupplyAttributesData(
2159b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
216042cbe538SGunnar Mills     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
216142cbe538SGunnar Mills     const boost::container::flat_map<std::string, std::string>&
216242cbe538SGunnar Mills         psAttributesConnections,
216342cbe538SGunnar Mills     Callback&& callback)
216442cbe538SGunnar Mills {
216542cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData enter";
216642cbe538SGunnar Mills 
216742cbe538SGunnar Mills     if (psAttributesConnections.empty())
216842cbe538SGunnar Mills     {
216942cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "Can't find PowerSupplyAttributes, no connections!";
217042cbe538SGunnar Mills         callback(inventoryItems);
217142cbe538SGunnar Mills         return;
217242cbe538SGunnar Mills     }
217342cbe538SGunnar Mills 
217442cbe538SGunnar Mills     // Assuming just one connection (service) for now
217542cbe538SGunnar Mills     auto it = psAttributesConnections.nth(0);
217642cbe538SGunnar Mills 
217742cbe538SGunnar Mills     const std::string& psAttributesPath = (*it).first;
217842cbe538SGunnar Mills     const std::string& psAttributesConnection = (*it).second;
217942cbe538SGunnar Mills 
218042cbe538SGunnar Mills     // Response handler for Get DeratingFactor property
218142cbe538SGunnar Mills     auto respHandler = [sensorsAsyncResp, inventoryItems,
2182f94c4ecfSEd Tanous                         callback{std::forward<Callback>(callback)}](
218342cbe538SGunnar Mills                            const boost::system::error_code ec,
21841e1e598dSJonathan Doman                            const uint32_t value) {
218542cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler enter";
218642cbe538SGunnar Mills         if (ec)
218742cbe538SGunnar Mills         {
218842cbe538SGunnar Mills             BMCWEB_LOG_ERROR
218942cbe538SGunnar Mills                 << "getPowerSupplyAttributesData respHandler DBus error " << ec;
21908d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
219142cbe538SGunnar Mills             return;
219242cbe538SGunnar Mills         }
219342cbe538SGunnar Mills 
21941e1e598dSJonathan Doman         BMCWEB_LOG_DEBUG << "PS EfficiencyPercent value: " << value;
219542cbe538SGunnar Mills         // Store value in Power Supply Inventory Items
219642cbe538SGunnar Mills         for (InventoryItem& inventoryItem : *inventoryItems)
219742cbe538SGunnar Mills         {
219855f79e6fSEd Tanous             if (inventoryItem.isPowerSupply)
219942cbe538SGunnar Mills             {
220042cbe538SGunnar Mills                 inventoryItem.powerSupplyEfficiencyPercent =
22011e1e598dSJonathan Doman                     static_cast<int>(value);
220242cbe538SGunnar Mills             }
220342cbe538SGunnar Mills         }
220442cbe538SGunnar Mills 
220542cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler exit";
220642cbe538SGunnar Mills         callback(inventoryItems);
220742cbe538SGunnar Mills     };
220842cbe538SGunnar Mills 
220942cbe538SGunnar Mills     // Get the DeratingFactor property for the PowerSupplyAttributes
221042cbe538SGunnar Mills     // Currently only property on the interface/only one we care about
22111e1e598dSJonathan Doman     sdbusplus::asio::getProperty<uint32_t>(
22121e1e598dSJonathan Doman         *crow::connections::systemBus, psAttributesConnection, psAttributesPath,
22131e1e598dSJonathan Doman         "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor",
22141e1e598dSJonathan Doman         std::move(respHandler));
221542cbe538SGunnar Mills 
221642cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData exit";
221742cbe538SGunnar Mills }
221842cbe538SGunnar Mills 
221942cbe538SGunnar Mills /**
222042cbe538SGunnar Mills  * @brief Gets the Power Supply Attributes such as EfficiencyPercent
222142cbe538SGunnar Mills  *
222242cbe538SGunnar Mills  * Gets the D-Bus connection (service) that provides Power Supply Attributes
222342cbe538SGunnar Mills  * data. Then gets the Power Supply Attributes data from the connection
222442cbe538SGunnar Mills  * (currently just assumes 1 connection) and stores the data in the inventory
222542cbe538SGunnar Mills  * item.
222642cbe538SGunnar Mills  *
222742cbe538SGunnar Mills  * This data is later used to provide sensor property values in the JSON
222842cbe538SGunnar Mills  * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
222942cbe538SGunnar Mills  *
223042cbe538SGunnar Mills  * Finds the Power Supply Attributes data asynchronously. Invokes callback
223142cbe538SGunnar Mills  * when information has been obtained.
223242cbe538SGunnar Mills  *
223342cbe538SGunnar Mills  * The callback must have the following signature:
223442cbe538SGunnar Mills  *   @code
223542cbe538SGunnar Mills  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
223642cbe538SGunnar Mills  *   @endcode
223742cbe538SGunnar Mills  *
223842cbe538SGunnar Mills  * @param sensorsAsyncResp Pointer to object holding response data.
223942cbe538SGunnar Mills  * @param inventoryItems D-Bus inventory items associated with sensors.
224042cbe538SGunnar Mills  * @param callback Callback to invoke when data has been obtained.
224142cbe538SGunnar Mills  */
224242cbe538SGunnar Mills template <typename Callback>
224342cbe538SGunnar Mills void getPowerSupplyAttributes(
224442cbe538SGunnar Mills     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
224542cbe538SGunnar Mills     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
224642cbe538SGunnar Mills     Callback&& callback)
224742cbe538SGunnar Mills {
224842cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes enter";
224942cbe538SGunnar Mills 
225042cbe538SGunnar Mills     // Only need the power supply attributes when the Power Schema
2251a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode != sensors::node::power)
225242cbe538SGunnar Mills     {
225342cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit since not Power";
225442cbe538SGunnar Mills         callback(inventoryItems);
225542cbe538SGunnar Mills         return;
225642cbe538SGunnar Mills     }
225742cbe538SGunnar Mills 
225842cbe538SGunnar Mills     const std::array<std::string, 1> interfaces = {
225942cbe538SGunnar Mills         "xyz.openbmc_project.Control.PowerSupplyAttributes"};
226042cbe538SGunnar Mills 
226142cbe538SGunnar Mills     // Response handler for parsing output from GetSubTree
2262b9d36b47SEd Tanous     auto respHandler =
2263b9d36b47SEd Tanous         [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
2264b9d36b47SEd Tanous          inventoryItems](
2265b9d36b47SEd Tanous             const boost::system::error_code ec,
2266b9d36b47SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) {
226742cbe538SGunnar Mills             BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler enter";
226842cbe538SGunnar Mills             if (ec)
226942cbe538SGunnar Mills             {
22708d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
227142cbe538SGunnar Mills                 BMCWEB_LOG_ERROR
227242cbe538SGunnar Mills                     << "getPowerSupplyAttributes respHandler DBus error " << ec;
227342cbe538SGunnar Mills                 return;
227442cbe538SGunnar Mills             }
227526f6976fSEd Tanous             if (subtree.empty())
227642cbe538SGunnar Mills             {
227742cbe538SGunnar Mills                 BMCWEB_LOG_DEBUG << "Can't find Power Supply Attributes!";
227842cbe538SGunnar Mills                 callback(inventoryItems);
227942cbe538SGunnar Mills                 return;
228042cbe538SGunnar Mills             }
228142cbe538SGunnar Mills 
228242cbe538SGunnar Mills             // Currently we only support 1 power supply attribute, use this for
228342cbe538SGunnar Mills             // all the power supplies. Build map of object path to connection.
228442cbe538SGunnar Mills             // Assume just 1 connection and 1 path for now.
228542cbe538SGunnar Mills             boost::container::flat_map<std::string, std::string>
228642cbe538SGunnar Mills                 psAttributesConnections;
228742cbe538SGunnar Mills 
228842cbe538SGunnar Mills             if (subtree[0].first.empty() || subtree[0].second.empty())
228942cbe538SGunnar Mills             {
229042cbe538SGunnar Mills                 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
229142cbe538SGunnar Mills                 callback(inventoryItems);
229242cbe538SGunnar Mills                 return;
229342cbe538SGunnar Mills             }
229442cbe538SGunnar Mills 
229542cbe538SGunnar Mills             const std::string& psAttributesPath = subtree[0].first;
229642cbe538SGunnar Mills             const std::string& connection = subtree[0].second.begin()->first;
229742cbe538SGunnar Mills 
229842cbe538SGunnar Mills             if (connection.empty())
229942cbe538SGunnar Mills             {
230042cbe538SGunnar Mills                 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
230142cbe538SGunnar Mills                 callback(inventoryItems);
230242cbe538SGunnar Mills                 return;
230342cbe538SGunnar Mills             }
230442cbe538SGunnar Mills 
230542cbe538SGunnar Mills             psAttributesConnections[psAttributesPath] = connection;
230642cbe538SGunnar Mills             BMCWEB_LOG_DEBUG << "Added mapping " << psAttributesPath << " -> "
230742cbe538SGunnar Mills                              << connection;
230842cbe538SGunnar Mills 
230942cbe538SGunnar Mills             getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
231042cbe538SGunnar Mills                                          psAttributesConnections,
231142cbe538SGunnar Mills                                          std::move(callback));
231242cbe538SGunnar Mills             BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler exit";
231342cbe538SGunnar Mills         };
231442cbe538SGunnar Mills     // Make call to ObjectMapper to find the PowerSupplyAttributes service
231542cbe538SGunnar Mills     crow::connections::systemBus->async_method_call(
231642cbe538SGunnar Mills         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
231742cbe538SGunnar Mills         "/xyz/openbmc_project/object_mapper",
231842cbe538SGunnar Mills         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
231942cbe538SGunnar Mills         "/xyz/openbmc_project", 0, interfaces);
232042cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit";
232142cbe538SGunnar Mills }
232242cbe538SGunnar Mills 
232342cbe538SGunnar Mills /**
2324adc4f0dbSShawn McCarney  * @brief Gets inventory items associated with sensors.
23258fb49dd6SShawn McCarney  *
23268fb49dd6SShawn McCarney  * Finds the inventory items that are associated with the specified sensors.
2327adc4f0dbSShawn McCarney  * Then gets D-Bus data for the inventory items, such as presence and VPD.
23288fb49dd6SShawn McCarney  *
2329adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
2330adc4f0dbSShawn McCarney  * response.
23318fb49dd6SShawn McCarney  *
2332adc4f0dbSShawn McCarney  * Finds the inventory items asynchronously.  Invokes callback when the
2333adc4f0dbSShawn McCarney  * inventory items have been obtained.
2334adc4f0dbSShawn McCarney  *
2335adc4f0dbSShawn McCarney  * The callback must have the following signature:
2336adc4f0dbSShawn McCarney  *   @code
2337adc4f0dbSShawn McCarney  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2338adc4f0dbSShawn McCarney  *   @endcode
23398fb49dd6SShawn McCarney  *
23408fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
23418fb49dd6SShawn McCarney  * @param sensorNames All sensors within the current chassis.
23428fb49dd6SShawn McCarney  * @param objectMgrPaths Mappings from connection name to DBus object path that
23438fb49dd6SShawn McCarney  * implements ObjectManager.
2344adc4f0dbSShawn McCarney  * @param callback Callback to invoke when inventory items have been obtained.
23458fb49dd6SShawn McCarney  */
2346adc4f0dbSShawn McCarney template <typename Callback>
2347adc4f0dbSShawn McCarney static void getInventoryItems(
23488fb49dd6SShawn McCarney     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
23498fb49dd6SShawn McCarney     const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
23508fb49dd6SShawn McCarney     std::shared_ptr<boost::container::flat_map<std::string, std::string>>
2351adc4f0dbSShawn McCarney         objectMgrPaths,
2352adc4f0dbSShawn McCarney     Callback&& callback)
23538fb49dd6SShawn McCarney {
2354adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItems enter";
2355adc4f0dbSShawn McCarney     auto getInventoryItemAssociationsCb =
2356f94c4ecfSEd Tanous         [sensorsAsyncResp, objectMgrPaths,
2357f94c4ecfSEd Tanous          callback{std::forward<Callback>(callback)}](
2358adc4f0dbSShawn McCarney             std::shared_ptr<std::vector<InventoryItem>> inventoryItems) {
2359adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb enter";
23608fb49dd6SShawn McCarney             auto getInventoryItemsConnectionsCb =
2361adc4f0dbSShawn McCarney                 [sensorsAsyncResp, inventoryItems, objectMgrPaths,
2362f94c4ecfSEd Tanous                  callback{std::forward<const Callback>(callback)}](
23638fb49dd6SShawn McCarney                     std::shared_ptr<boost::container::flat_set<std::string>>
23648fb49dd6SShawn McCarney                         invConnections) {
23658fb49dd6SShawn McCarney                     BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb enter";
2366d500549bSAnthony Wilson                     auto getInventoryItemsDataCb =
2367d500549bSAnthony Wilson                         [sensorsAsyncResp, inventoryItems,
2368d500549bSAnthony Wilson                          callback{std::move(callback)}]() {
2369d500549bSAnthony Wilson                             BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb enter";
237042cbe538SGunnar Mills 
237142cbe538SGunnar Mills                             auto getInventoryLedsCb = [sensorsAsyncResp,
237242cbe538SGunnar Mills                                                        inventoryItems,
237342cbe538SGunnar Mills                                                        callback{std::move(
237442cbe538SGunnar Mills                                                            callback)}]() {
237542cbe538SGunnar Mills                                 BMCWEB_LOG_DEBUG << "getInventoryLedsCb enter";
237642cbe538SGunnar Mills                                 // Find Power Supply Attributes and get the data
237742cbe538SGunnar Mills                                 getPowerSupplyAttributes(sensorsAsyncResp,
237842cbe538SGunnar Mills                                                          inventoryItems,
237942cbe538SGunnar Mills                                                          std::move(callback));
238042cbe538SGunnar Mills                                 BMCWEB_LOG_DEBUG << "getInventoryLedsCb exit";
238142cbe538SGunnar Mills                             };
238242cbe538SGunnar Mills 
2383d500549bSAnthony Wilson                             // Find led connections and get the data
2384d500549bSAnthony Wilson                             getInventoryLeds(sensorsAsyncResp, inventoryItems,
238542cbe538SGunnar Mills                                              std::move(getInventoryLedsCb));
2386d500549bSAnthony Wilson                             BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb exit";
2387d500549bSAnthony Wilson                         };
23888fb49dd6SShawn McCarney 
2389adc4f0dbSShawn McCarney                     // Get inventory item data from connections
2390adc4f0dbSShawn McCarney                     getInventoryItemsData(sensorsAsyncResp, inventoryItems,
2391adc4f0dbSShawn McCarney                                           invConnections, objectMgrPaths,
2392d500549bSAnthony Wilson                                           std::move(getInventoryItemsDataCb));
23938fb49dd6SShawn McCarney                     BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb exit";
23948fb49dd6SShawn McCarney                 };
23958fb49dd6SShawn McCarney 
2396adc4f0dbSShawn McCarney             // Get connections that provide inventory item data
23978fb49dd6SShawn McCarney             getInventoryItemsConnections(
2398adc4f0dbSShawn McCarney                 sensorsAsyncResp, inventoryItems,
23998fb49dd6SShawn McCarney                 std::move(getInventoryItemsConnectionsCb));
2400adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb exit";
24018fb49dd6SShawn McCarney         };
24028fb49dd6SShawn McCarney 
2403adc4f0dbSShawn McCarney     // Get associations from sensors to inventory items
2404adc4f0dbSShawn McCarney     getInventoryItemAssociations(sensorsAsyncResp, sensorNames, objectMgrPaths,
2405adc4f0dbSShawn McCarney                                  std::move(getInventoryItemAssociationsCb));
2406adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItems exit";
2407adc4f0dbSShawn McCarney }
2408adc4f0dbSShawn McCarney 
2409adc4f0dbSShawn McCarney /**
2410adc4f0dbSShawn McCarney  * @brief Returns JSON PowerSupply object for the specified inventory item.
2411adc4f0dbSShawn McCarney  *
2412adc4f0dbSShawn McCarney  * Searches for a JSON PowerSupply object that matches the specified inventory
2413adc4f0dbSShawn McCarney  * item.  If one is not found, a new PowerSupply object is added to the JSON
2414adc4f0dbSShawn McCarney  * array.
2415adc4f0dbSShawn McCarney  *
2416adc4f0dbSShawn McCarney  * Multiple sensors are often associated with one power supply inventory item.
2417adc4f0dbSShawn McCarney  * As a result, multiple sensor values are stored in one JSON PowerSupply
2418adc4f0dbSShawn McCarney  * object.
2419adc4f0dbSShawn McCarney  *
2420adc4f0dbSShawn McCarney  * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
2421adc4f0dbSShawn McCarney  * @param inventoryItem Inventory item for the power supply.
2422adc4f0dbSShawn McCarney  * @param chassisId Chassis that contains the power supply.
2423adc4f0dbSShawn McCarney  * @return JSON PowerSupply object for the specified inventory item.
2424adc4f0dbSShawn McCarney  */
242523a21a1cSEd Tanous inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
2426adc4f0dbSShawn McCarney                                       const InventoryItem& inventoryItem,
2427adc4f0dbSShawn McCarney                                       const std::string& chassisId)
2428adc4f0dbSShawn McCarney {
2429adc4f0dbSShawn McCarney     // Check if matching PowerSupply object already exists in JSON array
2430adc4f0dbSShawn McCarney     for (nlohmann::json& powerSupply : powerSupplyArray)
2431adc4f0dbSShawn McCarney     {
2432adc4f0dbSShawn McCarney         if (powerSupply["MemberId"] == inventoryItem.name)
2433adc4f0dbSShawn McCarney         {
2434adc4f0dbSShawn McCarney             return powerSupply;
2435adc4f0dbSShawn McCarney         }
2436adc4f0dbSShawn McCarney     }
2437adc4f0dbSShawn McCarney 
2438adc4f0dbSShawn McCarney     // Add new PowerSupply object to JSON array
2439adc4f0dbSShawn McCarney     powerSupplyArray.push_back({});
2440adc4f0dbSShawn McCarney     nlohmann::json& powerSupply = powerSupplyArray.back();
2441adc4f0dbSShawn McCarney     powerSupply["@odata.id"] =
2442adc4f0dbSShawn McCarney         "/redfish/v1/Chassis/" + chassisId + "/Power#/PowerSupplies/";
2443adc4f0dbSShawn McCarney     powerSupply["MemberId"] = inventoryItem.name;
2444adc4f0dbSShawn McCarney     powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " ");
2445adc4f0dbSShawn McCarney     powerSupply["Manufacturer"] = inventoryItem.manufacturer;
2446adc4f0dbSShawn McCarney     powerSupply["Model"] = inventoryItem.model;
2447adc4f0dbSShawn McCarney     powerSupply["PartNumber"] = inventoryItem.partNumber;
2448adc4f0dbSShawn McCarney     powerSupply["SerialNumber"] = inventoryItem.serialNumber;
2449d500549bSAnthony Wilson     setLedState(powerSupply, &inventoryItem);
2450adc4f0dbSShawn McCarney 
245142cbe538SGunnar Mills     if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
245242cbe538SGunnar Mills     {
245342cbe538SGunnar Mills         powerSupply["EfficiencyPercent"] =
245442cbe538SGunnar Mills             inventoryItem.powerSupplyEfficiencyPercent;
245542cbe538SGunnar Mills     }
245642cbe538SGunnar Mills 
245742cbe538SGunnar Mills     powerSupply["Status"]["State"] = getState(&inventoryItem);
2458adc4f0dbSShawn McCarney     const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
2459adc4f0dbSShawn McCarney     powerSupply["Status"]["Health"] = health;
2460adc4f0dbSShawn McCarney 
2461adc4f0dbSShawn McCarney     return powerSupply;
24628fb49dd6SShawn McCarney }
24638fb49dd6SShawn McCarney 
24648fb49dd6SShawn McCarney /**
2465de629b6eSShawn McCarney  * @brief Gets the values of the specified sensors.
2466de629b6eSShawn McCarney  *
2467de629b6eSShawn McCarney  * Stores the results as JSON in the SensorsAsyncResp.
2468de629b6eSShawn McCarney  *
2469de629b6eSShawn McCarney  * Gets the sensor values asynchronously.  Stores the results later when the
2470de629b6eSShawn McCarney  * information has been obtained.
2471de629b6eSShawn McCarney  *
2472adc4f0dbSShawn McCarney  * The sensorNames set contains all requested sensors for the current chassis.
2473de629b6eSShawn McCarney  *
2474de629b6eSShawn McCarney  * To minimize the number of DBus calls, the DBus method
2475de629b6eSShawn McCarney  * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
2476de629b6eSShawn McCarney  * values of all sensors provided by a connection (service).
2477de629b6eSShawn McCarney  *
2478de629b6eSShawn McCarney  * The connections set contains all the connections that provide sensor values.
2479de629b6eSShawn McCarney  *
2480de629b6eSShawn McCarney  * The objectMgrPaths map contains mappings from a connection name to the
2481de629b6eSShawn McCarney  * corresponding DBus object path that implements ObjectManager.
2482de629b6eSShawn McCarney  *
2483adc4f0dbSShawn McCarney  * The InventoryItem vector contains D-Bus inventory items associated with the
2484adc4f0dbSShawn McCarney  * sensors.  Inventory item data is needed for some Redfish sensor properties.
2485adc4f0dbSShawn McCarney  *
2486de629b6eSShawn McCarney  * @param SensorsAsyncResp Pointer to object holding response data.
2487adc4f0dbSShawn McCarney  * @param sensorNames All requested sensors within the current chassis.
2488de629b6eSShawn McCarney  * @param connections Connections that provide sensor values.
2489de629b6eSShawn McCarney  * @param objectMgrPaths Mappings from connection name to DBus object path that
2490de629b6eSShawn McCarney  * implements ObjectManager.
2491adc4f0dbSShawn McCarney  * @param inventoryItems Inventory items associated with the sensors.
2492de629b6eSShawn McCarney  */
249323a21a1cSEd Tanous inline void getSensorData(
249481ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2495b5a76932SEd Tanous     const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames,
2496de629b6eSShawn McCarney     const boost::container::flat_set<std::string>& connections,
2497b5a76932SEd Tanous     const std::shared_ptr<boost::container::flat_map<std::string, std::string>>&
2498adc4f0dbSShawn McCarney         objectMgrPaths,
2499b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems)
2500de629b6eSShawn McCarney {
2501de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getSensorData enter";
2502de629b6eSShawn McCarney     // Get managed objects from all services exposing sensors
2503de629b6eSShawn McCarney     for (const std::string& connection : connections)
2504de629b6eSShawn McCarney     {
2505de629b6eSShawn McCarney         // Response handler to process managed objects
250681ce609eSEd Tanous         auto getManagedObjectsCb = [sensorsAsyncResp, sensorNames,
2507adc4f0dbSShawn McCarney                                     inventoryItems](
2508de629b6eSShawn McCarney                                        const boost::system::error_code ec,
2509711ac7a9SEd Tanous                                        dbus::utility::ManagedObjectType& resp) {
2510de629b6eSShawn McCarney             BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter";
2511de629b6eSShawn McCarney             if (ec)
2512de629b6eSShawn McCarney             {
2513de629b6eSShawn McCarney                 BMCWEB_LOG_ERROR << "getManagedObjectsCb DBUS error: " << ec;
25148d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
2515de629b6eSShawn McCarney                 return;
2516de629b6eSShawn McCarney             }
2517de629b6eSShawn McCarney             // Go through all objects and update response with sensor data
2518de629b6eSShawn McCarney             for (const auto& objDictEntry : resp)
2519de629b6eSShawn McCarney             {
2520de629b6eSShawn McCarney                 const std::string& objPath =
2521de629b6eSShawn McCarney                     static_cast<const std::string&>(objDictEntry.first);
2522de629b6eSShawn McCarney                 BMCWEB_LOG_DEBUG << "getManagedObjectsCb parsing object "
2523de629b6eSShawn McCarney                                  << objPath;
2524de629b6eSShawn McCarney 
2525de629b6eSShawn McCarney                 std::vector<std::string> split;
2526de629b6eSShawn McCarney                 // Reserve space for
2527de629b6eSShawn McCarney                 // /xyz/openbmc_project/sensors/<name>/<subname>
2528de629b6eSShawn McCarney                 split.reserve(6);
2529de629b6eSShawn McCarney                 boost::algorithm::split(split, objPath, boost::is_any_of("/"));
2530de629b6eSShawn McCarney                 if (split.size() < 6)
2531de629b6eSShawn McCarney                 {
2532de629b6eSShawn McCarney                     BMCWEB_LOG_ERROR << "Got path that isn't long enough "
2533de629b6eSShawn McCarney                                      << objPath;
2534de629b6eSShawn McCarney                     continue;
2535de629b6eSShawn McCarney                 }
2536de629b6eSShawn McCarney                 // These indexes aren't intuitive, as boost::split puts an empty
2537de629b6eSShawn McCarney                 // string at the beginning
2538de629b6eSShawn McCarney                 const std::string& sensorType = split[4];
2539de629b6eSShawn McCarney                 const std::string& sensorName = split[5];
2540de629b6eSShawn McCarney                 BMCWEB_LOG_DEBUG << "sensorName " << sensorName
2541de629b6eSShawn McCarney                                  << " sensorType " << sensorType;
254249c53ac9SJohnathan Mantey                 if (sensorNames->find(objPath) == sensorNames->end())
2543de629b6eSShawn McCarney                 {
2544accdbb2cSAndrew Geissler                     BMCWEB_LOG_DEBUG << sensorName << " not in sensor list ";
2545de629b6eSShawn McCarney                     continue;
2546de629b6eSShawn McCarney                 }
2547de629b6eSShawn McCarney 
2548adc4f0dbSShawn McCarney                 // Find inventory item (if any) associated with sensor
2549adc4f0dbSShawn McCarney                 InventoryItem* inventoryItem =
2550adc4f0dbSShawn McCarney                     findInventoryItemForSensor(inventoryItems, objPath);
2551adc4f0dbSShawn McCarney 
255295a3ecadSAnthony Wilson                 const std::string& sensorSchema =
255381ce609eSEd Tanous                     sensorsAsyncResp->chassisSubNode;
255495a3ecadSAnthony Wilson 
255595a3ecadSAnthony Wilson                 nlohmann::json* sensorJson = nullptr;
255695a3ecadSAnthony Wilson 
2557928fefb9SNan Zhou                 if (sensorSchema == sensors::node::sensors &&
2558928fefb9SNan Zhou                     !sensorsAsyncResp->efficientExpand)
255995a3ecadSAnthony Wilson                 {
25608d1b46d7Szhanghch05                     sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
256181ce609eSEd Tanous                         "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId +
256281ce609eSEd Tanous                         "/" + sensorsAsyncResp->chassisSubNode + "/" +
256395a3ecadSAnthony Wilson                         sensorName;
25648d1b46d7Szhanghch05                     sensorJson = &(sensorsAsyncResp->asyncResp->res.jsonValue);
256595a3ecadSAnthony Wilson                 }
256695a3ecadSAnthony Wilson                 else
256795a3ecadSAnthony Wilson                 {
2568271584abSEd Tanous                     std::string fieldName;
2569928fefb9SNan Zhou                     if (sensorsAsyncResp->efficientExpand)
2570928fefb9SNan Zhou                     {
2571928fefb9SNan Zhou                         fieldName = "Members";
2572928fefb9SNan Zhou                     }
2573928fefb9SNan Zhou                     else if (sensorType == "temperature")
2574de629b6eSShawn McCarney                     {
2575de629b6eSShawn McCarney                         fieldName = "Temperatures";
2576de629b6eSShawn McCarney                     }
2577de629b6eSShawn McCarney                     else if (sensorType == "fan" || sensorType == "fan_tach" ||
2578de629b6eSShawn McCarney                              sensorType == "fan_pwm")
2579de629b6eSShawn McCarney                     {
2580de629b6eSShawn McCarney                         fieldName = "Fans";
2581de629b6eSShawn McCarney                     }
2582de629b6eSShawn McCarney                     else if (sensorType == "voltage")
2583de629b6eSShawn McCarney                     {
2584de629b6eSShawn McCarney                         fieldName = "Voltages";
2585de629b6eSShawn McCarney                     }
2586de629b6eSShawn McCarney                     else if (sensorType == "power")
2587de629b6eSShawn McCarney                     {
258855f79e6fSEd Tanous                         if (sensorName == "total_power")
2589028f7ebcSEddie James                         {
2590028f7ebcSEddie James                             fieldName = "PowerControl";
2591028f7ebcSEddie James                         }
2592adc4f0dbSShawn McCarney                         else if ((inventoryItem != nullptr) &&
2593adc4f0dbSShawn McCarney                                  (inventoryItem->isPowerSupply))
2594028f7ebcSEddie James                         {
2595de629b6eSShawn McCarney                             fieldName = "PowerSupplies";
2596de629b6eSShawn McCarney                         }
2597adc4f0dbSShawn McCarney                         else
2598adc4f0dbSShawn McCarney                         {
2599adc4f0dbSShawn McCarney                             // Other power sensors are in SensorCollection
2600adc4f0dbSShawn McCarney                             continue;
2601adc4f0dbSShawn McCarney                         }
2602028f7ebcSEddie James                     }
2603de629b6eSShawn McCarney                     else
2604de629b6eSShawn McCarney                     {
2605de629b6eSShawn McCarney                         BMCWEB_LOG_ERROR << "Unsure how to handle sensorType "
2606de629b6eSShawn McCarney                                          << sensorType;
2607de629b6eSShawn McCarney                         continue;
2608de629b6eSShawn McCarney                     }
2609de629b6eSShawn McCarney 
2610de629b6eSShawn McCarney                     nlohmann::json& tempArray =
26118d1b46d7Szhanghch05                         sensorsAsyncResp->asyncResp->res.jsonValue[fieldName];
2612adc4f0dbSShawn McCarney                     if (fieldName == "PowerControl")
261349c53ac9SJohnathan Mantey                     {
2614adc4f0dbSShawn McCarney                         if (tempArray.empty())
26157ab06f49SGunnar Mills                         {
261695a3ecadSAnthony Wilson                             // Put multiple "sensors" into a single
261795a3ecadSAnthony Wilson                             // PowerControl. Follows MemberId naming and
261895a3ecadSAnthony Wilson                             // naming in power.hpp.
26197ab06f49SGunnar Mills                             tempArray.push_back(
2620adc4f0dbSShawn McCarney                                 {{"@odata.id",
2621adc4f0dbSShawn McCarney                                   "/redfish/v1/Chassis/" +
262281ce609eSEd Tanous                                       sensorsAsyncResp->chassisId + "/" +
262381ce609eSEd Tanous                                       sensorsAsyncResp->chassisSubNode + "#/" +
2624adc4f0dbSShawn McCarney                                       fieldName + "/0"}});
2625adc4f0dbSShawn McCarney                         }
2626adc4f0dbSShawn McCarney                         sensorJson = &(tempArray.back());
2627adc4f0dbSShawn McCarney                     }
2628adc4f0dbSShawn McCarney                     else if (fieldName == "PowerSupplies")
2629adc4f0dbSShawn McCarney                     {
2630adc4f0dbSShawn McCarney                         if (inventoryItem != nullptr)
2631adc4f0dbSShawn McCarney                         {
2632adc4f0dbSShawn McCarney                             sensorJson =
2633adc4f0dbSShawn McCarney                                 &(getPowerSupply(tempArray, *inventoryItem,
263481ce609eSEd Tanous                                                  sensorsAsyncResp->chassisId));
2635adc4f0dbSShawn McCarney                         }
263649c53ac9SJohnathan Mantey                     }
2637928fefb9SNan Zhou                     else if (fieldName == "Members")
2638928fefb9SNan Zhou                     {
2639928fefb9SNan Zhou                         tempArray.push_back(
2640928fefb9SNan Zhou                             {{"@odata.id",
2641928fefb9SNan Zhou                               "/redfish/v1/Chassis/" +
2642928fefb9SNan Zhou                                   sensorsAsyncResp->chassisId + "/" +
2643928fefb9SNan Zhou                                   sensorsAsyncResp->chassisSubNode + "/" +
2644928fefb9SNan Zhou                                   sensorName}});
2645928fefb9SNan Zhou                         sensorJson = &(tempArray.back());
2646928fefb9SNan Zhou                     }
264749c53ac9SJohnathan Mantey                     else
264849c53ac9SJohnathan Mantey                     {
2649de629b6eSShawn McCarney                         tempArray.push_back(
265095a3ecadSAnthony Wilson                             {{"@odata.id",
265195a3ecadSAnthony Wilson                               "/redfish/v1/Chassis/" +
265281ce609eSEd Tanous                                   sensorsAsyncResp->chassisId + "/" +
265381ce609eSEd Tanous                                   sensorsAsyncResp->chassisSubNode + "#/" +
265495a3ecadSAnthony Wilson                                   fieldName + "/"}});
2655adc4f0dbSShawn McCarney                         sensorJson = &(tempArray.back());
265649c53ac9SJohnathan Mantey                     }
265795a3ecadSAnthony Wilson                 }
2658de629b6eSShawn McCarney 
2659adc4f0dbSShawn McCarney                 if (sensorJson != nullptr)
2660adc4f0dbSShawn McCarney                 {
2661a0ec28b6SAdrian Ambrożewicz                     objectInterfacesToJson(
266281ce609eSEd Tanous                         sensorName, sensorType, sensorsAsyncResp,
2663a0ec28b6SAdrian Ambrożewicz                         objDictEntry.second, *sensorJson, inventoryItem);
2664adc4f0dbSShawn McCarney                 }
2665de629b6eSShawn McCarney             }
266681ce609eSEd Tanous             if (sensorsAsyncResp.use_count() == 1)
266749c53ac9SJohnathan Mantey             {
266881ce609eSEd Tanous                 sortJSONResponse(sensorsAsyncResp);
2669928fefb9SNan Zhou                 if (sensorsAsyncResp->chassisSubNode ==
2670928fefb9SNan Zhou                         sensors::node::sensors &&
2671928fefb9SNan Zhou                     sensorsAsyncResp->efficientExpand)
2672928fefb9SNan Zhou                 {
2673928fefb9SNan Zhou                     sensorsAsyncResp->asyncResp->res
2674928fefb9SNan Zhou                         .jsonValue["Members@odata.count"] =
2675928fefb9SNan Zhou                         sensorsAsyncResp->asyncResp->res.jsonValue["Members"]
2676928fefb9SNan Zhou                             .size();
2677928fefb9SNan Zhou                 }
2678928fefb9SNan Zhou                 else if (sensorsAsyncResp->chassisSubNode ==
2679928fefb9SNan Zhou                          sensors::node::thermal)
26808bd25ccdSJames Feist                 {
268181ce609eSEd Tanous                     populateFanRedundancy(sensorsAsyncResp);
26828bd25ccdSJames Feist                 }
268349c53ac9SJohnathan Mantey             }
2684de629b6eSShawn McCarney             BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit";
2685de629b6eSShawn McCarney         };
2686de629b6eSShawn McCarney 
2687de629b6eSShawn McCarney         // Find DBus object path that implements ObjectManager for the current
2688de629b6eSShawn McCarney         // connection.  If no mapping found, default to "/".
26898fb49dd6SShawn McCarney         auto iter = objectMgrPaths->find(connection);
2690de629b6eSShawn McCarney         const std::string& objectMgrPath =
26918fb49dd6SShawn McCarney             (iter != objectMgrPaths->end()) ? iter->second : "/";
2692de629b6eSShawn McCarney         BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
2693de629b6eSShawn McCarney                          << objectMgrPath;
2694de629b6eSShawn McCarney 
2695de629b6eSShawn McCarney         crow::connections::systemBus->async_method_call(
2696de629b6eSShawn McCarney             getManagedObjectsCb, connection, objectMgrPath,
2697de629b6eSShawn McCarney             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
269823a21a1cSEd Tanous     }
2699de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getSensorData exit";
2700de629b6eSShawn McCarney }
2701de629b6eSShawn McCarney 
270223a21a1cSEd Tanous inline void processSensorList(
270381ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2704b5a76932SEd Tanous     const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames)
27051abe55efSEd Tanous {
270695a3ecadSAnthony Wilson     auto getConnectionCb =
270781ce609eSEd Tanous         [sensorsAsyncResp, sensorNames](
270895a3ecadSAnthony Wilson             const boost::container::flat_set<std::string>& connections) {
270955c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "getConnectionCb enter";
2710de629b6eSShawn McCarney             auto getObjectManagerPathsCb =
271181ce609eSEd Tanous                 [sensorsAsyncResp, sensorNames,
2712b5a76932SEd Tanous                  connections](const std::shared_ptr<boost::container::flat_map<
2713b5a76932SEd Tanous                                   std::string, std::string>>& objectMgrPaths) {
2714de629b6eSShawn McCarney                     BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb enter";
2715adc4f0dbSShawn McCarney                     auto getInventoryItemsCb =
271681ce609eSEd Tanous                         [sensorsAsyncResp, sensorNames, connections,
2717adc4f0dbSShawn McCarney                          objectMgrPaths](
2718f23b7296SEd Tanous                             const std::shared_ptr<std::vector<InventoryItem>>&
2719adc4f0dbSShawn McCarney                                 inventoryItems) {
2720adc4f0dbSShawn McCarney                             BMCWEB_LOG_DEBUG << "getInventoryItemsCb enter";
272149c53ac9SJohnathan Mantey                             // Get sensor data and store results in JSON
272281ce609eSEd Tanous                             getSensorData(sensorsAsyncResp, sensorNames,
2723adc4f0dbSShawn McCarney                                           connections, objectMgrPaths,
2724f23b7296SEd Tanous                                           inventoryItems);
2725adc4f0dbSShawn McCarney                             BMCWEB_LOG_DEBUG << "getInventoryItemsCb exit";
2726adc4f0dbSShawn McCarney                         };
2727adc4f0dbSShawn McCarney 
2728adc4f0dbSShawn McCarney                     // Get inventory items associated with sensors
272981ce609eSEd Tanous                     getInventoryItems(sensorsAsyncResp, sensorNames,
2730adc4f0dbSShawn McCarney                                       objectMgrPaths,
2731adc4f0dbSShawn McCarney                                       std::move(getInventoryItemsCb));
2732adc4f0dbSShawn McCarney 
2733de629b6eSShawn McCarney                     BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb exit";
273408777fb0SLewanczyk, Dawid                 };
2735de629b6eSShawn McCarney 
273649c53ac9SJohnathan Mantey             // Get mapping from connection names to the DBus object
273749c53ac9SJohnathan Mantey             // paths that implement the ObjectManager interface
273881ce609eSEd Tanous             getObjectManagerPaths(sensorsAsyncResp,
2739de629b6eSShawn McCarney                                   std::move(getObjectManagerPathsCb));
274055c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "getConnectionCb exit";
274108777fb0SLewanczyk, Dawid         };
2742de629b6eSShawn McCarney 
2743de629b6eSShawn McCarney     // Get set of connections that provide sensor values
274481ce609eSEd Tanous     getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb));
274595a3ecadSAnthony Wilson }
274695a3ecadSAnthony Wilson 
274795a3ecadSAnthony Wilson /**
274895a3ecadSAnthony Wilson  * @brief Entry point for retrieving sensors data related to requested
274995a3ecadSAnthony Wilson  *        chassis.
275095a3ecadSAnthony Wilson  * @param SensorsAsyncResp   Pointer to object holding response data
275195a3ecadSAnthony Wilson  */
2752b5a76932SEd Tanous inline void
275381ce609eSEd Tanous     getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
275495a3ecadSAnthony Wilson {
275595a3ecadSAnthony Wilson     BMCWEB_LOG_DEBUG << "getChassisData enter";
275695a3ecadSAnthony Wilson     auto getChassisCb =
275781ce609eSEd Tanous         [sensorsAsyncResp](
2758f23b7296SEd Tanous             const std::shared_ptr<boost::container::flat_set<std::string>>&
275995a3ecadSAnthony Wilson                 sensorNames) {
276095a3ecadSAnthony Wilson             BMCWEB_LOG_DEBUG << "getChassisCb enter";
276181ce609eSEd Tanous             processSensorList(sensorsAsyncResp, sensorNames);
276255c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "getChassisCb exit";
276308777fb0SLewanczyk, Dawid         };
2764928fefb9SNan Zhou     // SensorCollection doesn't contain the Redundancy property
2765928fefb9SNan Zhou     if (sensorsAsyncResp->chassisSubNode != sensors::node::sensors)
2766928fefb9SNan Zhou     {
27678d1b46d7Szhanghch05         sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] =
27688d1b46d7Szhanghch05             nlohmann::json::array();
2769928fefb9SNan Zhou     }
277026f03899SShawn McCarney     // Get set of sensors in chassis
277181ce609eSEd Tanous     getChassis(sensorsAsyncResp, std::move(getChassisCb));
277255c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "getChassisData exit";
2773271584abSEd Tanous }
277408777fb0SLewanczyk, Dawid 
2775413961deSRichard Marian Thomaiyar /**
277649c53ac9SJohnathan Mantey  * @brief Find the requested sensorName in the list of all sensors supplied by
277749c53ac9SJohnathan Mantey  * the chassis node
277849c53ac9SJohnathan Mantey  *
277949c53ac9SJohnathan Mantey  * @param sensorName   The sensor name supplied in the PATCH request
278049c53ac9SJohnathan Mantey  * @param sensorsList  The list of sensors managed by the chassis node
278149c53ac9SJohnathan Mantey  * @param sensorsModified  The list of sensors that were found as a result of
278249c53ac9SJohnathan Mantey  *                         repeated calls to this function
278349c53ac9SJohnathan Mantey  */
278423a21a1cSEd Tanous inline bool findSensorNameUsingSensorPath(
27850a86febdSRichard Marian Thomaiyar     std::string_view sensorName,
278649c53ac9SJohnathan Mantey     boost::container::flat_set<std::string>& sensorsList,
278749c53ac9SJohnathan Mantey     boost::container::flat_set<std::string>& sensorsModified)
278849c53ac9SJohnathan Mantey {
278928aa8de5SGeorge Liu     for (auto& chassisSensor : sensorsList)
279049c53ac9SJohnathan Mantey     {
279128aa8de5SGeorge Liu         sdbusplus::message::object_path path(chassisSensor);
2792b00dcc27SEd Tanous         std::string thisSensorName = path.filename();
279328aa8de5SGeorge Liu         if (thisSensorName.empty())
279449c53ac9SJohnathan Mantey         {
279549c53ac9SJohnathan Mantey             continue;
279649c53ac9SJohnathan Mantey         }
279749c53ac9SJohnathan Mantey         if (thisSensorName == sensorName)
279849c53ac9SJohnathan Mantey         {
279949c53ac9SJohnathan Mantey             sensorsModified.emplace(chassisSensor);
280049c53ac9SJohnathan Mantey             return true;
280149c53ac9SJohnathan Mantey         }
280249c53ac9SJohnathan Mantey     }
280349c53ac9SJohnathan Mantey     return false;
280449c53ac9SJohnathan Mantey }
280549c53ac9SJohnathan Mantey 
280649c53ac9SJohnathan Mantey /**
2807413961deSRichard Marian Thomaiyar  * @brief Entry point for overriding sensor values of given sensor
2808413961deSRichard Marian Thomaiyar  *
28098d1b46d7Szhanghch05  * @param sensorAsyncResp   response object
28104bb3dc34SCarol Wang  * @param allCollections   Collections extract from sensors' request patch info
2811413961deSRichard Marian Thomaiyar  * @param chassisSubNode   Chassis Node for which the query has to happen
2812413961deSRichard Marian Thomaiyar  */
281323a21a1cSEd Tanous inline void setSensorsOverride(
2814b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
28154bb3dc34SCarol Wang     std::unordered_map<std::string, std::vector<nlohmann::json>>&
2816397fd61fSjayaprakash Mutyala         allCollections)
2817413961deSRichard Marian Thomaiyar {
281870d1d0aaSjayaprakash Mutyala     BMCWEB_LOG_INFO << "setSensorsOverride for subNode"
28194bb3dc34SCarol Wang                     << sensorAsyncResp->chassisSubNode << "\n";
2820413961deSRichard Marian Thomaiyar 
2821543f4400SEd Tanous     const char* propertyValueName = nullptr;
2822f65af9e8SRichard Marian Thomaiyar     std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
2823413961deSRichard Marian Thomaiyar     std::string memberId;
2824543f4400SEd Tanous     double value = 0.0;
2825f65af9e8SRichard Marian Thomaiyar     for (auto& collectionItems : allCollections)
2826f65af9e8SRichard Marian Thomaiyar     {
2827f65af9e8SRichard Marian Thomaiyar         if (collectionItems.first == "Temperatures")
2828f65af9e8SRichard Marian Thomaiyar         {
2829f65af9e8SRichard Marian Thomaiyar             propertyValueName = "ReadingCelsius";
2830f65af9e8SRichard Marian Thomaiyar         }
2831f65af9e8SRichard Marian Thomaiyar         else if (collectionItems.first == "Fans")
2832f65af9e8SRichard Marian Thomaiyar         {
2833f65af9e8SRichard Marian Thomaiyar             propertyValueName = "Reading";
2834f65af9e8SRichard Marian Thomaiyar         }
2835f65af9e8SRichard Marian Thomaiyar         else
2836f65af9e8SRichard Marian Thomaiyar         {
2837f65af9e8SRichard Marian Thomaiyar             propertyValueName = "ReadingVolts";
2838f65af9e8SRichard Marian Thomaiyar         }
2839f65af9e8SRichard Marian Thomaiyar         for (auto& item : collectionItems.second)
2840f65af9e8SRichard Marian Thomaiyar         {
28418d1b46d7Szhanghch05             if (!json_util::readJson(item, sensorAsyncResp->asyncResp->res,
28428d1b46d7Szhanghch05                                      "MemberId", memberId, propertyValueName,
28438d1b46d7Szhanghch05                                      value))
2844413961deSRichard Marian Thomaiyar             {
2845413961deSRichard Marian Thomaiyar                 return;
2846413961deSRichard Marian Thomaiyar             }
2847f65af9e8SRichard Marian Thomaiyar             overrideMap.emplace(memberId,
2848f65af9e8SRichard Marian Thomaiyar                                 std::make_pair(value, collectionItems.first));
2849f65af9e8SRichard Marian Thomaiyar         }
2850f65af9e8SRichard Marian Thomaiyar     }
28514bb3dc34SCarol Wang 
2852b5a76932SEd Tanous     auto getChassisSensorListCb = [sensorAsyncResp, overrideMap](
2853b5a76932SEd Tanous                                       const std::shared_ptr<
285449c53ac9SJohnathan Mantey                                           boost::container::flat_set<
2855b5a76932SEd Tanous                                               std::string>>& sensorsList) {
285649c53ac9SJohnathan Mantey         // Match sensor names in the PATCH request to those managed by the
285749c53ac9SJohnathan Mantey         // chassis node
285849c53ac9SJohnathan Mantey         const std::shared_ptr<boost::container::flat_set<std::string>>
285949c53ac9SJohnathan Mantey             sensorNames =
286049c53ac9SJohnathan Mantey                 std::make_shared<boost::container::flat_set<std::string>>();
2861f65af9e8SRichard Marian Thomaiyar         for (const auto& item : overrideMap)
2862413961deSRichard Marian Thomaiyar         {
2863f65af9e8SRichard Marian Thomaiyar             const auto& sensor = item.first;
286449c53ac9SJohnathan Mantey             if (!findSensorNameUsingSensorPath(sensor, *sensorsList,
286549c53ac9SJohnathan Mantey                                                *sensorNames))
2866f65af9e8SRichard Marian Thomaiyar             {
2867f65af9e8SRichard Marian Thomaiyar                 BMCWEB_LOG_INFO << "Unable to find memberId " << item.first;
28688d1b46d7Szhanghch05                 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2869f65af9e8SRichard Marian Thomaiyar                                            item.second.second, item.first);
2870413961deSRichard Marian Thomaiyar                 return;
2871413961deSRichard Marian Thomaiyar             }
2872f65af9e8SRichard Marian Thomaiyar         }
2873413961deSRichard Marian Thomaiyar         // Get the connection to which the memberId belongs
28744f277b54SJayaprakash Mutyala         auto getObjectsWithConnectionCb = [sensorAsyncResp, overrideMap](
28754f277b54SJayaprakash Mutyala                                               const boost::container::flat_set<
28764f277b54SJayaprakash Mutyala                                                   std::string>& /*connections*/,
28774f277b54SJayaprakash Mutyala                                               const std::set<std::pair<
28784f277b54SJayaprakash Mutyala                                                   std::string, std::string>>&
2879413961deSRichard Marian Thomaiyar                                                   objectsWithConnection) {
2880f65af9e8SRichard Marian Thomaiyar             if (objectsWithConnection.size() != overrideMap.size())
2881413961deSRichard Marian Thomaiyar             {
2882413961deSRichard Marian Thomaiyar                 BMCWEB_LOG_INFO
2883f65af9e8SRichard Marian Thomaiyar                     << "Unable to find all objects with proper connection "
2884f65af9e8SRichard Marian Thomaiyar                     << objectsWithConnection.size() << " requested "
2885f65af9e8SRichard Marian Thomaiyar                     << overrideMap.size() << "\n";
28864f277b54SJayaprakash Mutyala                 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2887a0ec28b6SAdrian Ambrożewicz                                            sensorAsyncResp->chassisSubNode ==
2888a0ec28b6SAdrian Ambrożewicz                                                    sensors::node::thermal
2889413961deSRichard Marian Thomaiyar                                                ? "Temperatures"
2890413961deSRichard Marian Thomaiyar                                                : "Voltages",
2891f65af9e8SRichard Marian Thomaiyar                                            "Count");
2892f65af9e8SRichard Marian Thomaiyar                 return;
2893f65af9e8SRichard Marian Thomaiyar             }
2894f65af9e8SRichard Marian Thomaiyar             for (const auto& item : objectsWithConnection)
2895f65af9e8SRichard Marian Thomaiyar             {
289628aa8de5SGeorge Liu                 sdbusplus::message::object_path path(item.first);
289728aa8de5SGeorge Liu                 std::string sensorName = path.filename();
289828aa8de5SGeorge Liu                 if (sensorName.empty())
2899f65af9e8SRichard Marian Thomaiyar                 {
29004f277b54SJayaprakash Mutyala                     messages::internalError(sensorAsyncResp->asyncResp->res);
2901f65af9e8SRichard Marian Thomaiyar                     return;
2902f65af9e8SRichard Marian Thomaiyar                 }
2903f65af9e8SRichard Marian Thomaiyar 
2904f65af9e8SRichard Marian Thomaiyar                 const auto& iterator = overrideMap.find(sensorName);
2905f65af9e8SRichard Marian Thomaiyar                 if (iterator == overrideMap.end())
2906f65af9e8SRichard Marian Thomaiyar                 {
2907f65af9e8SRichard Marian Thomaiyar                     BMCWEB_LOG_INFO << "Unable to find sensor object"
2908f65af9e8SRichard Marian Thomaiyar                                     << item.first << "\n";
29094f277b54SJayaprakash Mutyala                     messages::internalError(sensorAsyncResp->asyncResp->res);
2910413961deSRichard Marian Thomaiyar                     return;
2911413961deSRichard Marian Thomaiyar                 }
2912413961deSRichard Marian Thomaiyar                 crow::connections::systemBus->async_method_call(
2913f65af9e8SRichard Marian Thomaiyar                     [sensorAsyncResp](const boost::system::error_code ec) {
2914413961deSRichard Marian Thomaiyar                         if (ec)
2915413961deSRichard Marian Thomaiyar                         {
29164f277b54SJayaprakash Mutyala                             if (ec.value() ==
29174f277b54SJayaprakash Mutyala                                 boost::system::errc::permission_denied)
29184f277b54SJayaprakash Mutyala                             {
29194f277b54SJayaprakash Mutyala                                 BMCWEB_LOG_WARNING
29204f277b54SJayaprakash Mutyala                                     << "Manufacturing mode is not Enabled...can't "
29214f277b54SJayaprakash Mutyala                                        "Override the sensor value. ";
29224f277b54SJayaprakash Mutyala 
29234f277b54SJayaprakash Mutyala                                 messages::insufficientPrivilege(
29248d1b46d7Szhanghch05                                     sensorAsyncResp->asyncResp->res);
2925413961deSRichard Marian Thomaiyar                                 return;
2926413961deSRichard Marian Thomaiyar                             }
29274f277b54SJayaprakash Mutyala                             BMCWEB_LOG_DEBUG
29284f277b54SJayaprakash Mutyala                                 << "setOverrideValueStatus DBUS error: " << ec;
29294f277b54SJayaprakash Mutyala                             messages::internalError(
29304f277b54SJayaprakash Mutyala                                 sensorAsyncResp->asyncResp->res);
29314f277b54SJayaprakash Mutyala                         }
2932413961deSRichard Marian Thomaiyar                     },
29334f277b54SJayaprakash Mutyala                     item.second, item.first, "org.freedesktop.DBus.Properties",
29344f277b54SJayaprakash Mutyala                     "Set", "xyz.openbmc_project.Sensor.Value", "Value",
2935168e20c1SEd Tanous                     dbus::utility::DbusVariantType(iterator->second.first));
2936f65af9e8SRichard Marian Thomaiyar             }
2937413961deSRichard Marian Thomaiyar         };
2938413961deSRichard Marian Thomaiyar         // Get object with connection for the given sensor name
2939413961deSRichard Marian Thomaiyar         getObjectsWithConnection(sensorAsyncResp, sensorNames,
2940413961deSRichard Marian Thomaiyar                                  std::move(getObjectsWithConnectionCb));
2941413961deSRichard Marian Thomaiyar     };
2942413961deSRichard Marian Thomaiyar     // get full sensor list for the given chassisId and cross verify the sensor.
2943413961deSRichard Marian Thomaiyar     getChassis(sensorAsyncResp, std::move(getChassisSensorListCb));
2944413961deSRichard Marian Thomaiyar }
2945413961deSRichard Marian Thomaiyar 
2946a0ec28b6SAdrian Ambrożewicz /**
2947a0ec28b6SAdrian Ambrożewicz  * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
2948a0ec28b6SAdrian Ambrożewicz  * path of the sensor.
2949a0ec28b6SAdrian Ambrożewicz  *
2950a0ec28b6SAdrian Ambrożewicz  * Function builds valid Redfish response for sensor query of given chassis and
2951a0ec28b6SAdrian Ambrożewicz  * node. It then builds metadata about Redfish<->D-Bus correlations and provides
2952a0ec28b6SAdrian Ambrożewicz  * it to caller in a callback.
2953a0ec28b6SAdrian Ambrożewicz  *
2954a0ec28b6SAdrian Ambrożewicz  * @param chassis   Chassis for which retrieval should be performed
2955a0ec28b6SAdrian Ambrożewicz  * @param node  Node (group) of sensors. See sensors::node for supported values
2956a0ec28b6SAdrian Ambrożewicz  * @param mapComplete   Callback to be called with retrieval result
2957a0ec28b6SAdrian Ambrożewicz  */
2958021d32cfSKrzysztof Grobelny inline void retrieveUriToDbusMap(const std::string& chassis,
2959021d32cfSKrzysztof Grobelny                                  const std::string& node,
2960a0ec28b6SAdrian Ambrożewicz                                  SensorsAsyncResp::DataCompleteCb&& mapComplete)
2961a0ec28b6SAdrian Ambrożewicz {
2962*02da7c5aSEd Tanous     decltype(sensors::paths)::const_iterator pathIt =
2963*02da7c5aSEd Tanous         std::find_if(sensors::paths.cbegin(), sensors::paths.cend(),
2964*02da7c5aSEd Tanous                      [&node](auto&& val) { return val.first == node; });
2965*02da7c5aSEd Tanous     if (pathIt == sensors::paths.cend())
2966a0ec28b6SAdrian Ambrożewicz     {
2967a0ec28b6SAdrian Ambrożewicz         BMCWEB_LOG_ERROR << "Wrong node provided : " << node;
2968a0ec28b6SAdrian Ambrożewicz         mapComplete(boost::beast::http::status::bad_request, {});
2969a0ec28b6SAdrian Ambrożewicz         return;
2970a0ec28b6SAdrian Ambrożewicz     }
2971d51e072fSKrzysztof Grobelny 
297272374eb7SNan Zhou     auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
2973a0ec28b6SAdrian Ambrożewicz     auto callback =
297472374eb7SNan Zhou         [asyncResp, mapCompleteCb{std::move(mapComplete)}](
2975a0ec28b6SAdrian Ambrożewicz             const boost::beast::http::status status,
2976a0ec28b6SAdrian Ambrożewicz             const boost::container::flat_map<std::string, std::string>&
2977a0ec28b6SAdrian Ambrożewicz                 uriToDbus) { mapCompleteCb(status, uriToDbus); };
2978a0ec28b6SAdrian Ambrożewicz 
2979a0ec28b6SAdrian Ambrożewicz     auto resp = std::make_shared<SensorsAsyncResp>(
2980d51e072fSKrzysztof Grobelny         asyncResp, chassis, pathIt->second, node, std::move(callback));
2981a0ec28b6SAdrian Ambrożewicz     getChassisData(resp);
2982a0ec28b6SAdrian Ambrożewicz }
2983a0ec28b6SAdrian Ambrożewicz 
2984bacb2162SNan Zhou namespace sensors
2985bacb2162SNan Zhou {
2986928fefb9SNan Zhou 
2987bacb2162SNan Zhou inline void getChassisCallback(
2988bacb2162SNan Zhou     const std::shared_ptr<SensorsAsyncResp>& asyncResp,
2989bacb2162SNan Zhou     const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames)
2990bacb2162SNan Zhou {
2991bacb2162SNan Zhou     BMCWEB_LOG_DEBUG << "getChassisCallback enter";
2992bacb2162SNan Zhou 
2993bacb2162SNan Zhou     nlohmann::json& entriesArray =
2994bacb2162SNan Zhou         asyncResp->asyncResp->res.jsonValue["Members"];
2995bacb2162SNan Zhou     for (auto& sensor : *sensorNames)
2996bacb2162SNan Zhou     {
2997bacb2162SNan Zhou         BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor;
2998bacb2162SNan Zhou 
2999bacb2162SNan Zhou         sdbusplus::message::object_path path(sensor);
3000bacb2162SNan Zhou         std::string sensorName = path.filename();
3001bacb2162SNan Zhou         if (sensorName.empty())
3002bacb2162SNan Zhou         {
3003bacb2162SNan Zhou             BMCWEB_LOG_ERROR << "Invalid sensor path: " << sensor;
3004bacb2162SNan Zhou             messages::internalError(asyncResp->asyncResp->res);
3005bacb2162SNan Zhou             return;
3006bacb2162SNan Zhou         }
3007bacb2162SNan Zhou         entriesArray.push_back(
3008bacb2162SNan Zhou             {{"@odata.id", "/redfish/v1/Chassis/" + asyncResp->chassisId + "/" +
3009bacb2162SNan Zhou                                asyncResp->chassisSubNode + "/" + sensorName}});
3010bacb2162SNan Zhou     }
3011bacb2162SNan Zhou 
3012bacb2162SNan Zhou     asyncResp->asyncResp->res.jsonValue["Members@odata.count"] =
3013bacb2162SNan Zhou         entriesArray.size();
3014bacb2162SNan Zhou     BMCWEB_LOG_DEBUG << "getChassisCallback exit";
3015bacb2162SNan Zhou }
3016bacb2162SNan Zhou } // namespace sensors
3017bacb2162SNan Zhou 
30187e860f15SJohn Edward Broadbent inline void requestRoutesSensorCollection(App& app)
301995a3ecadSAnthony Wilson {
30207e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/")
3021ed398213SEd Tanous         .privileges(redfish::privileges::getSensorCollection)
3022928fefb9SNan Zhou         .methods(
3023928fefb9SNan Zhou             boost::beast::http::verb::get)([&app](
3024928fefb9SNan Zhou                                                const crow::Request& req,
3025928fefb9SNan Zhou                                                const std::shared_ptr<
3026928fefb9SNan Zhou                                                    bmcweb::AsyncResp>& aResp,
30277e860f15SJohn Edward Broadbent                                                const std::string& chassisId) {
3028928fefb9SNan Zhou             query_param::QueryCapabilities capabilities = {
3029928fefb9SNan Zhou                 .canDelegateExpandLevel = 1,
3030928fefb9SNan Zhou             };
3031928fefb9SNan Zhou             query_param::Query delegatedQuery;
3032928fefb9SNan Zhou             if (!redfish::setUpRedfishRouteWithDelegation(
3033928fefb9SNan Zhou                     app, req, aResp->res, delegatedQuery, capabilities))
303445ca1b86SEd Tanous             {
303545ca1b86SEd Tanous                 return;
303645ca1b86SEd Tanous             }
303745ca1b86SEd Tanous 
3038928fefb9SNan Zhou             if (delegatedQuery.expandType != query_param::ExpandType::None)
3039928fefb9SNan Zhou             {
3040928fefb9SNan Zhou                 // we perform efficient expand.
3041928fefb9SNan Zhou                 auto asyncResp = std::make_shared<SensorsAsyncResp>(
3042*02da7c5aSEd Tanous                     aResp, chassisId, sensors::dbus::sensorPaths,
3043928fefb9SNan Zhou                     sensors::node::sensors,
3044928fefb9SNan Zhou                     /*efficientExpand=*/true);
3045928fefb9SNan Zhou                 getChassisData(asyncResp);
30468d1b46d7Szhanghch05 
3047928fefb9SNan Zhou                 BMCWEB_LOG_DEBUG
3048928fefb9SNan Zhou                     << "SensorCollection doGet exit via efficient expand handler";
3049928fefb9SNan Zhou                 return;
3050928fefb9SNan Zhou             };
3051928fefb9SNan Zhou 
3052928fefb9SNan Zhou             // if there's no efficient expand available, we use the default
3053928fefb9SNan Zhou             // Query Parameters route
3054928fefb9SNan Zhou             auto asyncResp = std::make_shared<SensorsAsyncResp>(
3055*02da7c5aSEd Tanous                 aResp, chassisId, sensors::dbus::sensorPaths,
3056a0ec28b6SAdrian Ambrożewicz                 sensors::node::sensors);
3057928fefb9SNan Zhou 
3058928fefb9SNan Zhou             // We get all sensors as hyperlinkes in the chassis (this
3059928fefb9SNan Zhou             // implies we reply on the default query parameters handler)
3060928fefb9SNan Zhou             getChassis(asyncResp,
3061bacb2162SNan Zhou                        std::bind_front(sensors::getChassisCallback, asyncResp));
306295a3ecadSAnthony Wilson             BMCWEB_LOG_DEBUG << "SensorCollection doGet exit";
30637e860f15SJohn Edward Broadbent         });
306495a3ecadSAnthony Wilson }
306595a3ecadSAnthony Wilson 
30667e860f15SJohn Edward Broadbent inline void requestRoutesSensor(App& app)
306795a3ecadSAnthony Wilson {
30687e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/")
3069ed398213SEd Tanous         .privileges(redfish::privileges::getSensor)
30707e860f15SJohn Edward Broadbent         .methods(
307145ca1b86SEd Tanous             boost::beast::http::verb::get)([&app](
307245ca1b86SEd Tanous                                                const crow::Request& req,
30737e860f15SJohn Edward Broadbent                                                const std::shared_ptr<
30747e860f15SJohn Edward Broadbent                                                    bmcweb::AsyncResp>& aResp,
30757e860f15SJohn Edward Broadbent                                                const std::string& chassisId,
30767e860f15SJohn Edward Broadbent                                                const std::string& sensorName) {
307745ca1b86SEd Tanous             if (!redfish::setUpRedfishRoute(app, req, aResp->res))
307845ca1b86SEd Tanous             {
307945ca1b86SEd Tanous                 return;
308045ca1b86SEd Tanous             }
308195a3ecadSAnthony Wilson             BMCWEB_LOG_DEBUG << "Sensor doGet enter";
308295a3ecadSAnthony Wilson             std::shared_ptr<SensorsAsyncResp> asyncResp =
3083*02da7c5aSEd Tanous                 std::make_shared<SensorsAsyncResp>(
3084*02da7c5aSEd Tanous                     aResp, chassisId, std::span<std::string_view>(),
3085a0ec28b6SAdrian Ambrożewicz                     sensors::node::sensors);
308695a3ecadSAnthony Wilson 
308795a3ecadSAnthony Wilson             const std::array<const char*, 1> interfaces = {
308895a3ecadSAnthony Wilson                 "xyz.openbmc_project.Sensor.Value"};
308995a3ecadSAnthony Wilson 
309095a3ecadSAnthony Wilson             // Get a list of all of the sensors that implement Sensor.Value
309195a3ecadSAnthony Wilson             // and get the path and service name associated with the sensor
309295a3ecadSAnthony Wilson             crow::connections::systemBus->async_method_call(
3093b9d36b47SEd Tanous                 [asyncResp, sensorName](
3094b9d36b47SEd Tanous                     const boost::system::error_code ec,
3095b9d36b47SEd Tanous                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
309695a3ecadSAnthony Wilson                     BMCWEB_LOG_DEBUG << "respHandler1 enter";
309795a3ecadSAnthony Wilson                     if (ec)
309895a3ecadSAnthony Wilson                     {
30998d1b46d7Szhanghch05                         messages::internalError(asyncResp->asyncResp->res);
31007e860f15SJohn Edward Broadbent                         BMCWEB_LOG_ERROR
31017e860f15SJohn Edward Broadbent                             << "Sensor getSensorPaths resp_handler: "
310295a3ecadSAnthony Wilson                             << "Dbus error " << ec;
310395a3ecadSAnthony Wilson                         return;
310495a3ecadSAnthony Wilson                     }
310595a3ecadSAnthony Wilson 
3106b9d36b47SEd Tanous                     dbus::utility::MapperGetSubTreeResponse::const_iterator it =
3107b9d36b47SEd Tanous                         std::find_if(
310895a3ecadSAnthony Wilson                             subtree.begin(), subtree.end(),
310995a3ecadSAnthony Wilson                             [sensorName](
3110b9d36b47SEd Tanous                                 const std::pair<std::string,
31117e860f15SJohn Edward Broadbent                                                 std::vector<std::pair<
3112b9d36b47SEd Tanous                                                     std::string,
3113b9d36b47SEd Tanous                                                     std::vector<std::string>>>>&
311495a3ecadSAnthony Wilson                                     object) {
3115b9d36b47SEd Tanous                                 sdbusplus::message::object_path path(
3116b9d36b47SEd Tanous                                     object.first);
311728aa8de5SGeorge Liu                                 std::string name = path.filename();
311828aa8de5SGeorge Liu                                 if (name.empty())
311995a3ecadSAnthony Wilson                                 {
312095a3ecadSAnthony Wilson                                     BMCWEB_LOG_ERROR << "Invalid sensor path: "
312128aa8de5SGeorge Liu                                                      << object.first;
312295a3ecadSAnthony Wilson                                     return false;
312395a3ecadSAnthony Wilson                                 }
312495a3ecadSAnthony Wilson 
312595a3ecadSAnthony Wilson                                 return name == sensorName;
312695a3ecadSAnthony Wilson                             });
312795a3ecadSAnthony Wilson 
312895a3ecadSAnthony Wilson                     if (it == subtree.end())
312995a3ecadSAnthony Wilson                     {
313095a3ecadSAnthony Wilson                         BMCWEB_LOG_ERROR << "Could not find path for sensor: "
313195a3ecadSAnthony Wilson                                          << sensorName;
31328d1b46d7Szhanghch05                         messages::resourceNotFound(asyncResp->asyncResp->res,
31338d1b46d7Szhanghch05                                                    "Sensor", sensorName);
313495a3ecadSAnthony Wilson                         return;
313595a3ecadSAnthony Wilson                     }
313695a3ecadSAnthony Wilson                     std::string_view sensorPath = (*it).first;
313795a3ecadSAnthony Wilson                     BMCWEB_LOG_DEBUG << "Found sensor path for sensor '"
313895a3ecadSAnthony Wilson                                      << sensorName << "': " << sensorPath;
313995a3ecadSAnthony Wilson 
31407e860f15SJohn Edward Broadbent                     const std::shared_ptr<
31417e860f15SJohn Edward Broadbent                         boost::container::flat_set<std::string>>
314295a3ecadSAnthony Wilson                         sensorList = std::make_shared<
314395a3ecadSAnthony Wilson                             boost::container::flat_set<std::string>>();
314495a3ecadSAnthony Wilson 
314595a3ecadSAnthony Wilson                     sensorList->emplace(sensorPath);
314695a3ecadSAnthony Wilson                     processSensorList(asyncResp, sensorList);
314795a3ecadSAnthony Wilson                     BMCWEB_LOG_DEBUG << "respHandler1 exit";
314895a3ecadSAnthony Wilson                 },
314995a3ecadSAnthony Wilson                 "xyz.openbmc_project.ObjectMapper",
315095a3ecadSAnthony Wilson                 "/xyz/openbmc_project/object_mapper",
315195a3ecadSAnthony Wilson                 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
315295a3ecadSAnthony Wilson                 "/xyz/openbmc_project/sensors", 2, interfaces);
31537e860f15SJohn Edward Broadbent         });
315495a3ecadSAnthony Wilson }
315595a3ecadSAnthony Wilson 
315608777fb0SLewanczyk, Dawid } // namespace redfish
3157