xref: /openbmc/bmcweb/features/redfish/lib/sensors.hpp (revision fe04d49cb52d8989818561c71bdbd6ba676058a5)
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/range/algorithm/replace_copy_if.hpp>
221abe55efSEd Tanous #include <dbus_singleton.hpp>
23168e20c1SEd Tanous #include <dbus_utility.hpp>
2445ca1b86SEd Tanous #include <query.hpp>
25ed398213SEd Tanous #include <registries/privilege_registry.hpp>
261e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp>
27413961deSRichard Marian Thomaiyar #include <utils/json_utils.hpp>
28928fefb9SNan Zhou #include <utils/query_param.hpp>
291214b7e7SGunnar Mills 
301214b7e7SGunnar Mills #include <cmath>
31*fe04d49cSNan Zhou #include <iterator>
32*fe04d49cSNan Zhou #include <map>
33*fe04d49cSNan Zhou #include <set>
34b5a76932SEd Tanous #include <utility>
35abf2add6SEd Tanous #include <variant>
3608777fb0SLewanczyk, Dawid 
371abe55efSEd Tanous namespace redfish
381abe55efSEd Tanous {
3908777fb0SLewanczyk, Dawid 
40a0ec28b6SAdrian Ambrożewicz namespace sensors
41a0ec28b6SAdrian Ambrożewicz {
42a0ec28b6SAdrian Ambrożewicz namespace node
43a0ec28b6SAdrian Ambrożewicz {
44a0ec28b6SAdrian Ambrożewicz static constexpr std::string_view power = "Power";
45a0ec28b6SAdrian Ambrożewicz static constexpr std::string_view sensors = "Sensors";
46a0ec28b6SAdrian Ambrożewicz static constexpr std::string_view thermal = "Thermal";
47a0ec28b6SAdrian Ambrożewicz } // namespace node
48a0ec28b6SAdrian Ambrożewicz 
4902da7c5aSEd Tanous // clang-format off
50a0ec28b6SAdrian Ambrożewicz namespace dbus
51a0ec28b6SAdrian Ambrożewicz {
524ee8e211SEd Tanous static auto powerPaths = std::to_array<std::string_view>({
5302da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/voltage",
5402da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/power"
5502da7c5aSEd Tanous });
56c2bf7f99SWludzik, Jozef 
574ee8e211SEd Tanous static auto sensorPaths = std::to_array<std::string_view>({
5802da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/power",
59a0ec28b6SAdrian Ambrożewicz     "/xyz/openbmc_project/sensors/current",
607088690cSBasheer Ahmed Muddebihal     "/xyz/openbmc_project/sensors/airflow",
615deabed9SGunnar Mills                "/xyz/openbmc_project/sensors/humidity",
62e8204933SGeorge Liu #ifdef BMCWEB_NEW_POWERSUBSYSTEM_THERMALSUBSYSTEM
63e8204933SGeorge Liu     "/xyz/openbmc_project/sensors/voltage",
64e8204933SGeorge Liu     "/xyz/openbmc_project/sensors/fan_tach",
65e8204933SGeorge Liu     "/xyz/openbmc_project/sensors/temperature",
66e8204933SGeorge Liu     "/xyz/openbmc_project/sensors/fan_pwm",
67e8204933SGeorge Liu     "/xyz/openbmc_project/sensors/altitude",
68e8204933SGeorge Liu     "/xyz/openbmc_project/sensors/energy",
69e8204933SGeorge Liu #endif
7002da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/utilization"
7102da7c5aSEd Tanous });
7202da7c5aSEd Tanous 
734ee8e211SEd Tanous static auto thermalPaths = std::to_array<std::string_view>({
7402da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/fan_tach",
75a0ec28b6SAdrian Ambrożewicz     "/xyz/openbmc_project/sensors/temperature",
7602da7c5aSEd Tanous     "/xyz/openbmc_project/sensors/fan_pwm"
7702da7c5aSEd Tanous });
7802da7c5aSEd Tanous 
79c2bf7f99SWludzik, Jozef } // namespace dbus
8002da7c5aSEd Tanous // clang-format on
8102da7c5aSEd Tanous 
8202da7c5aSEd Tanous using sensorPair = std::pair<std::string_view, std::span<std::string_view>>;
8302da7c5aSEd Tanous static constexpr std::array<sensorPair, 3> paths = {
8402da7c5aSEd Tanous     {{node::power, std::span<std::string_view>(dbus::powerPaths)},
8502da7c5aSEd Tanous      {node::sensors, std::span<std::string_view>(dbus::sensorPaths)},
8602da7c5aSEd Tanous      {node::thermal, std::span<std::string_view>(dbus::thermalPaths)}}};
87c2bf7f99SWludzik, Jozef 
88c2bf7f99SWludzik, Jozef inline const char* toReadingType(const std::string& sensorType)
89c2bf7f99SWludzik, Jozef {
90c2bf7f99SWludzik, Jozef     if (sensorType == "voltage")
91c2bf7f99SWludzik, Jozef     {
92c2bf7f99SWludzik, Jozef         return "Voltage";
93c2bf7f99SWludzik, Jozef     }
94c2bf7f99SWludzik, Jozef     if (sensorType == "power")
95c2bf7f99SWludzik, Jozef     {
96c2bf7f99SWludzik, Jozef         return "Power";
97c2bf7f99SWludzik, Jozef     }
98c2bf7f99SWludzik, Jozef     if (sensorType == "current")
99c2bf7f99SWludzik, Jozef     {
100c2bf7f99SWludzik, Jozef         return "Current";
101c2bf7f99SWludzik, Jozef     }
102c2bf7f99SWludzik, Jozef     if (sensorType == "fan_tach")
103c2bf7f99SWludzik, Jozef     {
104c2bf7f99SWludzik, Jozef         return "Rotational";
105c2bf7f99SWludzik, Jozef     }
106c2bf7f99SWludzik, Jozef     if (sensorType == "temperature")
107c2bf7f99SWludzik, Jozef     {
108c2bf7f99SWludzik, Jozef         return "Temperature";
109c2bf7f99SWludzik, Jozef     }
110c2bf7f99SWludzik, Jozef     if (sensorType == "fan_pwm" || sensorType == "utilization")
111c2bf7f99SWludzik, Jozef     {
112c2bf7f99SWludzik, Jozef         return "Percent";
113c2bf7f99SWludzik, Jozef     }
1145deabed9SGunnar Mills     if (sensorType == "humidity")
1155deabed9SGunnar Mills     {
1165deabed9SGunnar Mills         return "Humidity";
1175deabed9SGunnar Mills     }
118c2bf7f99SWludzik, Jozef     if (sensorType == "altitude")
119c2bf7f99SWludzik, Jozef     {
120c2bf7f99SWludzik, Jozef         return "Altitude";
121c2bf7f99SWludzik, Jozef     }
122c2bf7f99SWludzik, Jozef     if (sensorType == "airflow")
123c2bf7f99SWludzik, Jozef     {
124c2bf7f99SWludzik, Jozef         return "AirFlow";
125c2bf7f99SWludzik, Jozef     }
126c2bf7f99SWludzik, Jozef     if (sensorType == "energy")
127c2bf7f99SWludzik, Jozef     {
128c2bf7f99SWludzik, Jozef         return "EnergyJoules";
129c2bf7f99SWludzik, Jozef     }
130c2bf7f99SWludzik, Jozef     return "";
131c2bf7f99SWludzik, Jozef }
132c2bf7f99SWludzik, Jozef 
133c2bf7f99SWludzik, Jozef inline const char* toReadingUnits(const std::string& sensorType)
134c2bf7f99SWludzik, Jozef {
135c2bf7f99SWludzik, Jozef     if (sensorType == "voltage")
136c2bf7f99SWludzik, Jozef     {
137c2bf7f99SWludzik, Jozef         return "V";
138c2bf7f99SWludzik, Jozef     }
139c2bf7f99SWludzik, Jozef     if (sensorType == "power")
140c2bf7f99SWludzik, Jozef     {
141c2bf7f99SWludzik, Jozef         return "W";
142c2bf7f99SWludzik, Jozef     }
143c2bf7f99SWludzik, Jozef     if (sensorType == "current")
144c2bf7f99SWludzik, Jozef     {
145c2bf7f99SWludzik, Jozef         return "A";
146c2bf7f99SWludzik, Jozef     }
147c2bf7f99SWludzik, Jozef     if (sensorType == "fan_tach")
148c2bf7f99SWludzik, Jozef     {
149c2bf7f99SWludzik, Jozef         return "RPM";
150c2bf7f99SWludzik, Jozef     }
151c2bf7f99SWludzik, Jozef     if (sensorType == "temperature")
152c2bf7f99SWludzik, Jozef     {
153c2bf7f99SWludzik, Jozef         return "Cel";
154c2bf7f99SWludzik, Jozef     }
1555deabed9SGunnar Mills     if (sensorType == "fan_pwm" || sensorType == "utilization" ||
1565deabed9SGunnar Mills         sensorType == "humidity")
157c2bf7f99SWludzik, Jozef     {
158c2bf7f99SWludzik, Jozef         return "%";
159c2bf7f99SWludzik, Jozef     }
160c2bf7f99SWludzik, Jozef     if (sensorType == "altitude")
161c2bf7f99SWludzik, Jozef     {
162c2bf7f99SWludzik, Jozef         return "m";
163c2bf7f99SWludzik, Jozef     }
164c2bf7f99SWludzik, Jozef     if (sensorType == "airflow")
165c2bf7f99SWludzik, Jozef     {
166c2bf7f99SWludzik, Jozef         return "cft_i/min";
167c2bf7f99SWludzik, Jozef     }
168c2bf7f99SWludzik, Jozef     if (sensorType == "energy")
169c2bf7f99SWludzik, Jozef     {
170c2bf7f99SWludzik, Jozef         return "J";
171c2bf7f99SWludzik, Jozef     }
172c2bf7f99SWludzik, Jozef     return "";
173a0ec28b6SAdrian Ambrożewicz }
174a0ec28b6SAdrian Ambrożewicz } // namespace sensors
175a0ec28b6SAdrian Ambrożewicz 
17608777fb0SLewanczyk, Dawid /**
177588c3f0dSKowalski, Kamil  * SensorsAsyncResp
17808777fb0SLewanczyk, Dawid  * Gathers data needed for response processing after async calls are done
17908777fb0SLewanczyk, Dawid  */
1801abe55efSEd Tanous class SensorsAsyncResp
1811abe55efSEd Tanous {
18208777fb0SLewanczyk, Dawid   public:
183a0ec28b6SAdrian Ambrożewicz     using DataCompleteCb = std::function<void(
184a0ec28b6SAdrian Ambrożewicz         const boost::beast::http::status status,
185*fe04d49cSNan Zhou         const std::map<std::string, std::string>& uriToDbus)>;
186a0ec28b6SAdrian Ambrożewicz 
187a0ec28b6SAdrian Ambrożewicz     struct SensorData
188a0ec28b6SAdrian Ambrożewicz     {
189a0ec28b6SAdrian Ambrożewicz         const std::string name;
190a0ec28b6SAdrian Ambrożewicz         std::string uri;
191a0ec28b6SAdrian Ambrożewicz         const std::string valueKey;
192a0ec28b6SAdrian Ambrożewicz         const std::string dbusPath;
193a0ec28b6SAdrian Ambrożewicz     };
194a0ec28b6SAdrian Ambrożewicz 
1958d1b46d7Szhanghch05     SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1968d1b46d7Szhanghch05                      const std::string& chassisIdIn,
19702da7c5aSEd Tanous                      std::span<std::string_view> typesIn,
19802da7c5aSEd Tanous                      std::string_view subNode) :
1998d1b46d7Szhanghch05         asyncResp(asyncResp),
200928fefb9SNan Zhou         chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
201928fefb9SNan Zhou         efficientExpand(false)
2021214b7e7SGunnar Mills     {}
20308777fb0SLewanczyk, Dawid 
204a0ec28b6SAdrian Ambrożewicz     // Store extra data about sensor mapping and return it in callback
2058d1b46d7Szhanghch05     SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2068d1b46d7Szhanghch05                      const std::string& chassisIdIn,
20702da7c5aSEd Tanous                      std::span<std::string_view> typesIn,
20802da7c5aSEd Tanous                      std::string_view subNode,
209a0ec28b6SAdrian Ambrożewicz                      DataCompleteCb&& creationComplete) :
2108d1b46d7Szhanghch05         asyncResp(asyncResp),
211928fefb9SNan Zhou         chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
212928fefb9SNan Zhou         efficientExpand(false), metadata{std::vector<SensorData>()},
213a0ec28b6SAdrian Ambrożewicz         dataComplete{std::move(creationComplete)}
214a0ec28b6SAdrian Ambrożewicz     {}
215a0ec28b6SAdrian Ambrożewicz 
216928fefb9SNan Zhou     // sensor collections expand
217928fefb9SNan Zhou     SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
218928fefb9SNan Zhou                      const std::string& chassisIdIn,
21902da7c5aSEd Tanous                      const std::span<std::string_view> typesIn,
220928fefb9SNan Zhou                      const std::string_view& subNode, bool efficientExpand) :
221928fefb9SNan Zhou         asyncResp(asyncResp),
222928fefb9SNan Zhou         chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
223928fefb9SNan Zhou         efficientExpand(efficientExpand)
224928fefb9SNan Zhou     {}
225928fefb9SNan Zhou 
2261abe55efSEd Tanous     ~SensorsAsyncResp()
2271abe55efSEd Tanous     {
2288d1b46d7Szhanghch05         if (asyncResp->res.result() ==
2298d1b46d7Szhanghch05             boost::beast::http::status::internal_server_error)
2301abe55efSEd Tanous         {
2311abe55efSEd Tanous             // Reset the json object to clear out any data that made it in
2321abe55efSEd Tanous             // before the error happened todo(ed) handle error condition with
2331abe55efSEd Tanous             // proper code
2348d1b46d7Szhanghch05             asyncResp->res.jsonValue = nlohmann::json::object();
23508777fb0SLewanczyk, Dawid         }
236a0ec28b6SAdrian Ambrożewicz 
237a0ec28b6SAdrian Ambrożewicz         if (dataComplete && metadata)
238a0ec28b6SAdrian Ambrożewicz         {
239*fe04d49cSNan Zhou             std::map<std::string, std::string> map;
2408d1b46d7Szhanghch05             if (asyncResp->res.result() == boost::beast::http::status::ok)
241a0ec28b6SAdrian Ambrożewicz             {
242a0ec28b6SAdrian Ambrożewicz                 for (auto& sensor : *metadata)
243a0ec28b6SAdrian Ambrożewicz                 {
244a0ec28b6SAdrian Ambrożewicz                     map.insert(std::make_pair(sensor.uri + sensor.valueKey,
245a0ec28b6SAdrian Ambrożewicz                                               sensor.dbusPath));
246a0ec28b6SAdrian Ambrożewicz                 }
247a0ec28b6SAdrian Ambrożewicz             }
2488d1b46d7Szhanghch05             dataComplete(asyncResp->res.result(), map);
249a0ec28b6SAdrian Ambrożewicz         }
25008777fb0SLewanczyk, Dawid     }
251588c3f0dSKowalski, Kamil 
252ecd6a3a2SEd Tanous     SensorsAsyncResp(const SensorsAsyncResp&) = delete;
253ecd6a3a2SEd Tanous     SensorsAsyncResp(SensorsAsyncResp&&) = delete;
254ecd6a3a2SEd Tanous     SensorsAsyncResp& operator=(const SensorsAsyncResp&) = delete;
255ecd6a3a2SEd Tanous     SensorsAsyncResp& operator=(SensorsAsyncResp&&) = delete;
256ecd6a3a2SEd Tanous 
257a0ec28b6SAdrian Ambrożewicz     void addMetadata(const nlohmann::json& sensorObject,
258a0ec28b6SAdrian Ambrożewicz                      const std::string& valueKey, const std::string& dbusPath)
259a0ec28b6SAdrian Ambrożewicz     {
260a0ec28b6SAdrian Ambrożewicz         if (metadata)
261a0ec28b6SAdrian Ambrożewicz         {
262a0ec28b6SAdrian Ambrożewicz             metadata->emplace_back(SensorData{sensorObject["Name"],
263a0ec28b6SAdrian Ambrożewicz                                               sensorObject["@odata.id"],
264a0ec28b6SAdrian Ambrożewicz                                               valueKey, dbusPath});
265a0ec28b6SAdrian Ambrożewicz         }
266a0ec28b6SAdrian Ambrożewicz     }
267a0ec28b6SAdrian Ambrożewicz 
268a0ec28b6SAdrian Ambrożewicz     void updateUri(const std::string& name, const std::string& uri)
269a0ec28b6SAdrian Ambrożewicz     {
270a0ec28b6SAdrian Ambrożewicz         if (metadata)
271a0ec28b6SAdrian Ambrożewicz         {
272a0ec28b6SAdrian Ambrożewicz             for (auto& sensor : *metadata)
273a0ec28b6SAdrian Ambrożewicz             {
274a0ec28b6SAdrian Ambrożewicz                 if (sensor.name == name)
275a0ec28b6SAdrian Ambrożewicz                 {
276a0ec28b6SAdrian Ambrożewicz                     sensor.uri = uri;
277a0ec28b6SAdrian Ambrożewicz                 }
278a0ec28b6SAdrian Ambrożewicz             }
279a0ec28b6SAdrian Ambrożewicz         }
280a0ec28b6SAdrian Ambrożewicz     }
281a0ec28b6SAdrian Ambrożewicz 
2828d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp> asyncResp;
283a0ec28b6SAdrian Ambrożewicz     const std::string chassisId;
28402da7c5aSEd Tanous     const std::span<std::string_view> types;
285a0ec28b6SAdrian Ambrożewicz     const std::string chassisSubNode;
286928fefb9SNan Zhou     const bool efficientExpand;
287a0ec28b6SAdrian Ambrożewicz 
288a0ec28b6SAdrian Ambrożewicz   private:
289a0ec28b6SAdrian Ambrożewicz     std::optional<std::vector<SensorData>> metadata;
290a0ec28b6SAdrian Ambrożewicz     DataCompleteCb dataComplete;
29108777fb0SLewanczyk, Dawid };
29208777fb0SLewanczyk, Dawid 
29308777fb0SLewanczyk, Dawid /**
294d500549bSAnthony Wilson  * Possible states for physical inventory leds
295d500549bSAnthony Wilson  */
296d500549bSAnthony Wilson enum class LedState
297d500549bSAnthony Wilson {
298d500549bSAnthony Wilson     OFF,
299d500549bSAnthony Wilson     ON,
300d500549bSAnthony Wilson     BLINK,
301d500549bSAnthony Wilson     UNKNOWN
302d500549bSAnthony Wilson };
303d500549bSAnthony Wilson 
304d500549bSAnthony Wilson /**
305adc4f0dbSShawn McCarney  * D-Bus inventory item associated with one or more sensors.
306adc4f0dbSShawn McCarney  */
307adc4f0dbSShawn McCarney class InventoryItem
308adc4f0dbSShawn McCarney {
309adc4f0dbSShawn McCarney   public:
310e05aec50SEd Tanous     InventoryItem(const std::string& objPath) : objectPath(objPath)
311adc4f0dbSShawn McCarney     {
312adc4f0dbSShawn McCarney         // Set inventory item name to last node of object path
31328aa8de5SGeorge Liu         sdbusplus::message::object_path path(objectPath);
31428aa8de5SGeorge Liu         name = path.filename();
31528aa8de5SGeorge Liu         if (name.empty())
316adc4f0dbSShawn McCarney         {
31728aa8de5SGeorge Liu             BMCWEB_LOG_ERROR << "Failed to find '/' in " << objectPath;
318adc4f0dbSShawn McCarney         }
319adc4f0dbSShawn McCarney     }
320adc4f0dbSShawn McCarney 
321adc4f0dbSShawn McCarney     std::string objectPath;
322adc4f0dbSShawn McCarney     std::string name;
323e05aec50SEd Tanous     bool isPresent = true;
324e05aec50SEd Tanous     bool isFunctional = true;
325e05aec50SEd Tanous     bool isPowerSupply = false;
326e05aec50SEd Tanous     int powerSupplyEfficiencyPercent = -1;
327adc4f0dbSShawn McCarney     std::string manufacturer;
328adc4f0dbSShawn McCarney     std::string model;
329adc4f0dbSShawn McCarney     std::string partNumber;
330adc4f0dbSShawn McCarney     std::string serialNumber;
331adc4f0dbSShawn McCarney     std::set<std::string> sensors;
332d500549bSAnthony Wilson     std::string ledObjectPath;
333e05aec50SEd Tanous     LedState ledState = LedState::UNKNOWN;
334adc4f0dbSShawn McCarney };
335adc4f0dbSShawn McCarney 
336adc4f0dbSShawn McCarney /**
337413961deSRichard Marian Thomaiyar  * @brief Get objects with connection necessary for sensors
338588c3f0dSKowalski, Kamil  * @param SensorsAsyncResp Pointer to object holding response data
33908777fb0SLewanczyk, Dawid  * @param sensorNames Sensors retrieved from chassis
34008777fb0SLewanczyk, Dawid  * @param callback Callback for processing gathered connections
34108777fb0SLewanczyk, Dawid  */
34208777fb0SLewanczyk, Dawid template <typename Callback>
343413961deSRichard Marian Thomaiyar void getObjectsWithConnection(
34481ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
345*fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& sensorNames,
3461abe55efSEd Tanous     Callback&& callback)
3471abe55efSEd Tanous {
348413961deSRichard Marian Thomaiyar     BMCWEB_LOG_DEBUG << "getObjectsWithConnection enter";
34903b5bae3SJames Feist     const std::string path = "/xyz/openbmc_project/sensors";
35008777fb0SLewanczyk, Dawid     const std::array<std::string, 1> interfaces = {
35108777fb0SLewanczyk, Dawid         "xyz.openbmc_project.Sensor.Value"};
35208777fb0SLewanczyk, Dawid 
35308777fb0SLewanczyk, Dawid     // Response handler for parsing objects subtree
354002d39b4SEd Tanous     auto respHandler =
355002d39b4SEd Tanous         [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
356002d39b4SEd Tanous          sensorNames](const boost::system::error_code ec,
357002d39b4SEd Tanous                       const dbus::utility::MapperGetSubTreeResponse& subtree) {
358413961deSRichard Marian Thomaiyar         BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler enter";
3591abe55efSEd Tanous         if (ec)
3601abe55efSEd Tanous         {
3618d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
362413961deSRichard Marian Thomaiyar             BMCWEB_LOG_ERROR
363413961deSRichard Marian Thomaiyar                 << "getObjectsWithConnection resp_handler: Dbus error " << ec;
36408777fb0SLewanczyk, Dawid             return;
36508777fb0SLewanczyk, Dawid         }
36608777fb0SLewanczyk, Dawid 
36755c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "Found " << subtree.size() << " subtrees";
36808777fb0SLewanczyk, Dawid 
36908777fb0SLewanczyk, Dawid         // Make unique list of connections only for requested sensor types and
37008777fb0SLewanczyk, Dawid         // found in the chassis
371*fe04d49cSNan Zhou         std::set<std::string> connections;
372413961deSRichard Marian Thomaiyar         std::set<std::pair<std::string, std::string>> objectsWithConnection;
37308777fb0SLewanczyk, Dawid 
37449c53ac9SJohnathan Mantey         BMCWEB_LOG_DEBUG << "sensorNames list count: " << sensorNames->size();
37549c53ac9SJohnathan Mantey         for (const std::string& tsensor : *sensorNames)
3761abe55efSEd Tanous         {
37755c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "Sensor to find: " << tsensor;
37808777fb0SLewanczyk, Dawid         }
37908777fb0SLewanczyk, Dawid 
38008777fb0SLewanczyk, Dawid         for (const std::pair<
38108777fb0SLewanczyk, Dawid                  std::string,
38208777fb0SLewanczyk, Dawid                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
3831abe55efSEd Tanous                  object : subtree)
3841abe55efSEd Tanous         {
38549c53ac9SJohnathan Mantey             if (sensorNames->find(object.first) != sensorNames->end())
3861abe55efSEd Tanous             {
38749c53ac9SJohnathan Mantey                 for (const std::pair<std::string, std::vector<std::string>>&
3881abe55efSEd Tanous                          objData : object.second)
3891abe55efSEd Tanous                 {
39049c53ac9SJohnathan Mantey                     BMCWEB_LOG_DEBUG << "Adding connection: " << objData.first;
39108777fb0SLewanczyk, Dawid                     connections.insert(objData.first);
392de629b6eSShawn McCarney                     objectsWithConnection.insert(
393de629b6eSShawn McCarney                         std::make_pair(object.first, objData.first));
39408777fb0SLewanczyk, Dawid                 }
39508777fb0SLewanczyk, Dawid             }
39608777fb0SLewanczyk, Dawid         }
39755c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "Found " << connections.size() << " connections";
398413961deSRichard Marian Thomaiyar         callback(std::move(connections), std::move(objectsWithConnection));
399413961deSRichard Marian Thomaiyar         BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler exit";
40008777fb0SLewanczyk, Dawid     };
40108777fb0SLewanczyk, Dawid     // Make call to ObjectMapper to find all sensors objects
40255c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
40355c7b7a2SEd Tanous         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
4041abe55efSEd Tanous         "/xyz/openbmc_project/object_mapper",
4051abe55efSEd Tanous         "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 2, interfaces);
406413961deSRichard Marian Thomaiyar     BMCWEB_LOG_DEBUG << "getObjectsWithConnection exit";
407413961deSRichard Marian Thomaiyar }
408413961deSRichard Marian Thomaiyar 
409413961deSRichard Marian Thomaiyar /**
410413961deSRichard Marian Thomaiyar  * @brief Create connections necessary for sensors
411413961deSRichard Marian Thomaiyar  * @param SensorsAsyncResp Pointer to object holding response data
412413961deSRichard Marian Thomaiyar  * @param sensorNames Sensors retrieved from chassis
413413961deSRichard Marian Thomaiyar  * @param callback Callback for processing gathered connections
414413961deSRichard Marian Thomaiyar  */
415413961deSRichard Marian Thomaiyar template <typename Callback>
416*fe04d49cSNan Zhou void getConnections(std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
417*fe04d49cSNan Zhou                     const std::shared_ptr<std::set<std::string>> sensorNames,
418413961deSRichard Marian Thomaiyar                     Callback&& callback)
419413961deSRichard Marian Thomaiyar {
420413961deSRichard Marian Thomaiyar     auto objectsWithConnectionCb =
421*fe04d49cSNan Zhou         [callback](const std::set<std::string>& connections,
422413961deSRichard Marian Thomaiyar                    const std::set<std::pair<std::string, std::string>>&
4233174e4dfSEd Tanous                    /*objectsWithConnection*/) { callback(connections); };
42481ce609eSEd Tanous     getObjectsWithConnection(sensorsAsyncResp, sensorNames,
425413961deSRichard Marian Thomaiyar                              std::move(objectsWithConnectionCb));
42608777fb0SLewanczyk, Dawid }
42708777fb0SLewanczyk, Dawid 
42808777fb0SLewanczyk, Dawid /**
42949c53ac9SJohnathan Mantey  * @brief Shrinks the list of sensors for processing
43049c53ac9SJohnathan Mantey  * @param SensorsAysncResp  The class holding the Redfish response
43149c53ac9SJohnathan Mantey  * @param allSensors  A list of all the sensors associated to the
43249c53ac9SJohnathan Mantey  * chassis element (i.e. baseboard, front panel, etc...)
43349c53ac9SJohnathan Mantey  * @param activeSensors A list that is a reduction of the incoming
43449c53ac9SJohnathan Mantey  * allSensors list.  Eliminate Thermal sensors when a Power request is
43549c53ac9SJohnathan Mantey  * made, and eliminate Power sensors when a Thermal request is made.
43649c53ac9SJohnathan Mantey  */
43723a21a1cSEd Tanous inline void reduceSensorList(
43881ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
43949c53ac9SJohnathan Mantey     const std::vector<std::string>* allSensors,
440*fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& activeSensors)
44149c53ac9SJohnathan Mantey {
44281ce609eSEd Tanous     if (sensorsAsyncResp == nullptr)
44349c53ac9SJohnathan Mantey     {
44449c53ac9SJohnathan Mantey         return;
44549c53ac9SJohnathan Mantey     }
44649c53ac9SJohnathan Mantey     if ((allSensors == nullptr) || (activeSensors == nullptr))
44749c53ac9SJohnathan Mantey     {
44849c53ac9SJohnathan Mantey         messages::resourceNotFound(
4498d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res, sensorsAsyncResp->chassisSubNode,
45081ce609eSEd Tanous             sensorsAsyncResp->chassisSubNode == sensors::node::thermal
451a0ec28b6SAdrian Ambrożewicz                 ? "Temperatures"
45249c53ac9SJohnathan Mantey                 : "Voltages");
45349c53ac9SJohnathan Mantey 
45449c53ac9SJohnathan Mantey         return;
45549c53ac9SJohnathan Mantey     }
45649c53ac9SJohnathan Mantey     if (allSensors->empty())
45749c53ac9SJohnathan Mantey     {
45849c53ac9SJohnathan Mantey         // Nothing to do, the activeSensors object is also empty
45949c53ac9SJohnathan Mantey         return;
46049c53ac9SJohnathan Mantey     }
46149c53ac9SJohnathan Mantey 
46202da7c5aSEd Tanous     for (std::string_view type : sensorsAsyncResp->types)
46349c53ac9SJohnathan Mantey     {
46449c53ac9SJohnathan Mantey         for (const std::string& sensor : *allSensors)
46549c53ac9SJohnathan Mantey         {
46649c53ac9SJohnathan Mantey             if (boost::starts_with(sensor, type))
46749c53ac9SJohnathan Mantey             {
46849c53ac9SJohnathan Mantey                 activeSensors->emplace(sensor);
46949c53ac9SJohnathan Mantey             }
47049c53ac9SJohnathan Mantey         }
47149c53ac9SJohnathan Mantey     }
47249c53ac9SJohnathan Mantey }
47349c53ac9SJohnathan Mantey 
47449c53ac9SJohnathan Mantey /**
4754bb3dc34SCarol Wang  * @brief Retrieves valid chassis path
4764bb3dc34SCarol Wang  * @param asyncResp   Pointer to object holding response data
4774bb3dc34SCarol Wang  * @param callback  Callback for next step to get valid chassis path
4784bb3dc34SCarol Wang  */
4794bb3dc34SCarol Wang template <typename Callback>
480b5a76932SEd Tanous void getValidChassisPath(const std::shared_ptr<SensorsAsyncResp>& asyncResp,
4814bb3dc34SCarol Wang                          Callback&& callback)
4824bb3dc34SCarol Wang {
4834bb3dc34SCarol Wang     BMCWEB_LOG_DEBUG << "checkChassisId enter";
4844bb3dc34SCarol Wang     const std::array<const char*, 2> interfaces = {
4854bb3dc34SCarol Wang         "xyz.openbmc_project.Inventory.Item.Board",
4864bb3dc34SCarol Wang         "xyz.openbmc_project.Inventory.Item.Chassis"};
4874bb3dc34SCarol Wang 
488b9d36b47SEd Tanous     auto respHandler = [callback{std::forward<Callback>(callback)}, asyncResp](
489b9d36b47SEd Tanous                            const boost::system::error_code ec,
490b9d36b47SEd Tanous                            const dbus::utility::MapperGetSubTreePathsResponse&
491b9d36b47SEd Tanous                                chassisPaths) mutable {
4924bb3dc34SCarol Wang         BMCWEB_LOG_DEBUG << "getValidChassisPath respHandler enter";
4934bb3dc34SCarol Wang         if (ec)
4944bb3dc34SCarol Wang         {
495b9d36b47SEd Tanous             BMCWEB_LOG_ERROR << "getValidChassisPath respHandler DBUS error: "
496b9d36b47SEd Tanous                              << ec;
4978d1b46d7Szhanghch05             messages::internalError(asyncResp->asyncResp->res);
4984bb3dc34SCarol Wang             return;
4994bb3dc34SCarol Wang         }
5004bb3dc34SCarol Wang 
5014bb3dc34SCarol Wang         std::optional<std::string> chassisPath;
5024bb3dc34SCarol Wang         std::string chassisName;
5034bb3dc34SCarol Wang         for (const std::string& chassis : chassisPaths)
5044bb3dc34SCarol Wang         {
50528aa8de5SGeorge Liu             sdbusplus::message::object_path path(chassis);
50628aa8de5SGeorge Liu             chassisName = path.filename();
50728aa8de5SGeorge Liu             if (chassisName.empty())
5084bb3dc34SCarol Wang             {
5094bb3dc34SCarol Wang                 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
5104bb3dc34SCarol Wang                 continue;
5114bb3dc34SCarol Wang             }
5124bb3dc34SCarol Wang             if (chassisName == asyncResp->chassisId)
5134bb3dc34SCarol Wang             {
5144bb3dc34SCarol Wang                 chassisPath = chassis;
5154bb3dc34SCarol Wang                 break;
5164bb3dc34SCarol Wang             }
5174bb3dc34SCarol Wang         }
5184bb3dc34SCarol Wang         callback(chassisPath);
5194bb3dc34SCarol Wang     };
5204bb3dc34SCarol Wang 
5214bb3dc34SCarol Wang     // Get the Chassis Collection
5224bb3dc34SCarol Wang     crow::connections::systemBus->async_method_call(
5234bb3dc34SCarol Wang         respHandler, "xyz.openbmc_project.ObjectMapper",
5244bb3dc34SCarol Wang         "/xyz/openbmc_project/object_mapper",
5254bb3dc34SCarol Wang         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
5264bb3dc34SCarol Wang         "/xyz/openbmc_project/inventory", 0, interfaces);
5274bb3dc34SCarol Wang     BMCWEB_LOG_DEBUG << "checkChassisId exit";
5284bb3dc34SCarol Wang }
5294bb3dc34SCarol Wang 
5304bb3dc34SCarol Wang /**
53108777fb0SLewanczyk, Dawid  * @brief Retrieves requested chassis sensors and redundancy data from DBus .
532588c3f0dSKowalski, Kamil  * @param SensorsAsyncResp   Pointer to object holding response data
53308777fb0SLewanczyk, Dawid  * @param callback  Callback for next step in gathered sensor processing
53408777fb0SLewanczyk, Dawid  */
53508777fb0SLewanczyk, Dawid template <typename Callback>
536b5a76932SEd Tanous void getChassis(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
5371abe55efSEd Tanous                 Callback&& callback)
5381abe55efSEd Tanous {
53955c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "getChassis enter";
540adc4f0dbSShawn McCarney     const std::array<const char*, 2> interfaces = {
54149c53ac9SJohnathan Mantey         "xyz.openbmc_project.Inventory.Item.Board",
542adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Item.Chassis"};
543002d39b4SEd Tanous     auto respHandler =
544002d39b4SEd Tanous         [callback{std::forward<Callback>(callback)}, sensorsAsyncResp](
54549c53ac9SJohnathan Mantey             const boost::system::error_code ec,
546002d39b4SEd Tanous             const dbus::utility::MapperGetSubTreePathsResponse& chassisPaths) {
54755c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "getChassis respHandler enter";
5481abe55efSEd Tanous         if (ec)
5491abe55efSEd Tanous         {
55055c7b7a2SEd Tanous             BMCWEB_LOG_ERROR << "getChassis respHandler DBUS error: " << ec;
5518d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
55208777fb0SLewanczyk, Dawid             return;
55308777fb0SLewanczyk, Dawid         }
55408777fb0SLewanczyk, Dawid 
55549c53ac9SJohnathan Mantey         const std::string* chassisPath = nullptr;
55649c53ac9SJohnathan Mantey         std::string chassisName;
55749c53ac9SJohnathan Mantey         for (const std::string& chassis : chassisPaths)
5581abe55efSEd Tanous         {
55928aa8de5SGeorge Liu             sdbusplus::message::object_path path(chassis);
56028aa8de5SGeorge Liu             chassisName = path.filename();
56128aa8de5SGeorge Liu             if (chassisName.empty())
5621abe55efSEd Tanous             {
56349c53ac9SJohnathan Mantey                 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
564daf36e2eSEd Tanous                 continue;
565daf36e2eSEd Tanous             }
56649c53ac9SJohnathan Mantey             if (chassisName == sensorsAsyncResp->chassisId)
5671abe55efSEd Tanous             {
56849c53ac9SJohnathan Mantey                 chassisPath = &chassis;
56949c53ac9SJohnathan Mantey                 break;
570daf36e2eSEd Tanous             }
57149c53ac9SJohnathan Mantey         }
57249c53ac9SJohnathan Mantey         if (chassisPath == nullptr)
5731abe55efSEd Tanous         {
5748d1b46d7Szhanghch05             messages::resourceNotFound(sensorsAsyncResp->asyncResp->res,
5758d1b46d7Szhanghch05                                        "Chassis", sensorsAsyncResp->chassisId);
57649c53ac9SJohnathan Mantey             return;
5771abe55efSEd Tanous         }
57808777fb0SLewanczyk, Dawid 
57949c53ac9SJohnathan Mantey         const std::string& chassisSubNode = sensorsAsyncResp->chassisSubNode;
580a0ec28b6SAdrian Ambrożewicz         if (chassisSubNode == sensors::node::power)
58149c53ac9SJohnathan Mantey         {
5828d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] =
58349c53ac9SJohnathan Mantey                 "#Power.v1_5_2.Power";
58449c53ac9SJohnathan Mantey         }
585a0ec28b6SAdrian Ambrożewicz         else if (chassisSubNode == sensors::node::thermal)
58649c53ac9SJohnathan Mantey         {
5878d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] =
58849c53ac9SJohnathan Mantey                 "#Thermal.v1_4_0.Thermal";
5898d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["Fans"] =
5908d1b46d7Szhanghch05                 nlohmann::json::array();
5918d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["Temperatures"] =
5924f9a2130SJennifer Lee                 nlohmann::json::array();
59349c53ac9SJohnathan Mantey         }
594a0ec28b6SAdrian Ambrożewicz         else if (chassisSubNode == sensors::node::sensors)
59595a3ecadSAnthony Wilson         {
5968d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] =
59795a3ecadSAnthony Wilson                 "#SensorCollection.SensorCollection";
5988d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["Description"] =
59995a3ecadSAnthony Wilson                 "Collection of Sensors for this Chassis";
6008d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["Members"] =
60195a3ecadSAnthony Wilson                 nlohmann::json::array();
6028d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["Members@odata.count"] =
6038d1b46d7Szhanghch05                 0;
60495a3ecadSAnthony Wilson         }
60595a3ecadSAnthony Wilson 
606a0ec28b6SAdrian Ambrożewicz         if (chassisSubNode != sensors::node::sensors)
60795a3ecadSAnthony Wilson         {
6088d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["Id"] = chassisSubNode;
60995a3ecadSAnthony Wilson         }
61095a3ecadSAnthony Wilson 
6118d1b46d7Szhanghch05         sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
61249c53ac9SJohnathan Mantey             "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId + "/" +
61349c53ac9SJohnathan Mantey             chassisSubNode;
6148d1b46d7Szhanghch05         sensorsAsyncResp->asyncResp->res.jsonValue["Name"] = chassisSubNode;
6158fb49dd6SShawn McCarney         // Get the list of all sensors for this Chassis element
6168fb49dd6SShawn McCarney         std::string sensorPath = *chassisPath + "/all_sensors";
6171e1e598dSJonathan Doman         sdbusplus::asio::getProperty<std::vector<std::string>>(
6181e1e598dSJonathan Doman             *crow::connections::systemBus, "xyz.openbmc_project.ObjectMapper",
6191e1e598dSJonathan Doman             sensorPath, "xyz.openbmc_project.Association", "endpoints",
620f94c4ecfSEd Tanous             [sensorsAsyncResp,
621f94c4ecfSEd Tanous              callback{std::forward<const Callback>(callback)}](
622271584abSEd Tanous                 const boost::system::error_code& e,
6231e1e598dSJonathan Doman                 const std::vector<std::string>& nodeSensorList) {
624271584abSEd Tanous             if (e)
62549c53ac9SJohnathan Mantey             {
626271584abSEd Tanous                 if (e.value() != EBADR)
62749c53ac9SJohnathan Mantey                 {
628002d39b4SEd Tanous                     messages::internalError(sensorsAsyncResp->asyncResp->res);
62949c53ac9SJohnathan Mantey                     return;
63049c53ac9SJohnathan Mantey                 }
63149c53ac9SJohnathan Mantey             }
632*fe04d49cSNan Zhou             const std::shared_ptr<std::set<std::string>> culledSensorList =
633*fe04d49cSNan Zhou                 std::make_shared<std::set<std::string>>();
6341e1e598dSJonathan Doman             reduceSensorList(sensorsAsyncResp, &nodeSensorList,
63549c53ac9SJohnathan Mantey                              culledSensorList);
63649c53ac9SJohnathan Mantey             callback(culledSensorList);
6371e1e598dSJonathan Doman             });
63849c53ac9SJohnathan Mantey     };
63949c53ac9SJohnathan Mantey 
64049c53ac9SJohnathan Mantey     // Get the Chassis Collection
64149c53ac9SJohnathan Mantey     crow::connections::systemBus->async_method_call(
64249c53ac9SJohnathan Mantey         respHandler, "xyz.openbmc_project.ObjectMapper",
64349c53ac9SJohnathan Mantey         "/xyz/openbmc_project/object_mapper",
64449c53ac9SJohnathan Mantey         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
645271584abSEd Tanous         "/xyz/openbmc_project/inventory", 0, interfaces);
64655c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "getChassis exit";
64708777fb0SLewanczyk, Dawid }
64808777fb0SLewanczyk, Dawid 
64908777fb0SLewanczyk, Dawid /**
650de629b6eSShawn McCarney  * @brief Finds all DBus object paths that implement ObjectManager.
651de629b6eSShawn McCarney  *
652de629b6eSShawn McCarney  * Creates a mapping from the associated connection name to the object path.
653de629b6eSShawn McCarney  *
654de629b6eSShawn McCarney  * Finds the object paths asynchronously.  Invokes callback when information has
655de629b6eSShawn McCarney  * been obtained.
656de629b6eSShawn McCarney  *
657de629b6eSShawn McCarney  * The callback must have the following signature:
658de629b6eSShawn McCarney  *   @code
659*fe04d49cSNan Zhou  *   callback(std::shared_ptr<std::map<std::string,std::string>> objectMgrPaths)
660de629b6eSShawn McCarney  *   @endcode
661de629b6eSShawn McCarney  *
66249c53ac9SJohnathan Mantey  * @param sensorsAsyncResp Pointer to object holding response data.
663de629b6eSShawn McCarney  * @param callback Callback to invoke when object paths obtained.
664de629b6eSShawn McCarney  */
665de629b6eSShawn McCarney template <typename Callback>
666b5a76932SEd Tanous void getObjectManagerPaths(
66781ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
668de629b6eSShawn McCarney     Callback&& callback)
669de629b6eSShawn McCarney {
670de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getObjectManagerPaths enter";
671de629b6eSShawn McCarney     const std::array<std::string, 1> interfaces = {
672de629b6eSShawn McCarney         "org.freedesktop.DBus.ObjectManager"};
673de629b6eSShawn McCarney 
674de629b6eSShawn McCarney     // Response handler for GetSubTree DBus method
675002d39b4SEd Tanous     auto respHandler =
676002d39b4SEd Tanous         [callback{std::forward<Callback>(callback)}, sensorsAsyncResp](
677b9d36b47SEd Tanous             const boost::system::error_code ec,
678002d39b4SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) {
679de629b6eSShawn McCarney         BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler enter";
680de629b6eSShawn McCarney         if (ec)
681de629b6eSShawn McCarney         {
6828d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
683de629b6eSShawn McCarney             BMCWEB_LOG_ERROR << "getObjectManagerPaths respHandler: DBus error "
684de629b6eSShawn McCarney                              << ec;
685de629b6eSShawn McCarney             return;
686de629b6eSShawn McCarney         }
687de629b6eSShawn McCarney 
688de629b6eSShawn McCarney         // Loop over returned object paths
689*fe04d49cSNan Zhou         std::shared_ptr<std::map<std::string, std::string>> objectMgrPaths =
690*fe04d49cSNan Zhou             std::make_shared<std::map<std::string, std::string>>();
691de629b6eSShawn McCarney         for (const std::pair<
692de629b6eSShawn McCarney                  std::string,
693de629b6eSShawn McCarney                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
694de629b6eSShawn McCarney                  object : subtree)
695de629b6eSShawn McCarney         {
696de629b6eSShawn McCarney             // Loop over connections for current object path
697de629b6eSShawn McCarney             const std::string& objectPath = object.first;
698de629b6eSShawn McCarney             for (const std::pair<std::string, std::vector<std::string>>&
699de629b6eSShawn McCarney                      objData : object.second)
700de629b6eSShawn McCarney             {
701de629b6eSShawn McCarney                 // Add mapping from connection to object path
702de629b6eSShawn McCarney                 const std::string& connection = objData.first;
7038fb49dd6SShawn McCarney                 (*objectMgrPaths)[connection] = objectPath;
704de629b6eSShawn McCarney                 BMCWEB_LOG_DEBUG << "Added mapping " << connection << " -> "
705de629b6eSShawn McCarney                                  << objectPath;
706de629b6eSShawn McCarney             }
707de629b6eSShawn McCarney         }
7088fb49dd6SShawn McCarney         callback(objectMgrPaths);
709de629b6eSShawn McCarney         BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler exit";
710de629b6eSShawn McCarney     };
711de629b6eSShawn McCarney 
712de629b6eSShawn McCarney     // Query mapper for all DBus object paths that implement ObjectManager
713de629b6eSShawn McCarney     crow::connections::systemBus->async_method_call(
714de629b6eSShawn McCarney         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
715de629b6eSShawn McCarney         "/xyz/openbmc_project/object_mapper",
716271584abSEd Tanous         "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0, interfaces);
717de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getObjectManagerPaths exit";
718de629b6eSShawn McCarney }
719de629b6eSShawn McCarney 
720de629b6eSShawn McCarney /**
721adc4f0dbSShawn McCarney  * @brief Returns the Redfish State value for the specified inventory item.
722adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with a sensor.
723adc4f0dbSShawn McCarney  * @return State value for inventory item.
72434dd179eSJames Feist  */
72523a21a1cSEd Tanous inline std::string getState(const InventoryItem* inventoryItem)
726adc4f0dbSShawn McCarney {
727adc4f0dbSShawn McCarney     if ((inventoryItem != nullptr) && !(inventoryItem->isPresent))
728adc4f0dbSShawn McCarney     {
729adc4f0dbSShawn McCarney         return "Absent";
730adc4f0dbSShawn McCarney     }
73134dd179eSJames Feist 
732adc4f0dbSShawn McCarney     return "Enabled";
733adc4f0dbSShawn McCarney }
734adc4f0dbSShawn McCarney 
735adc4f0dbSShawn McCarney /**
736adc4f0dbSShawn McCarney  * @brief Returns the Redfish Health value for the specified sensor.
737adc4f0dbSShawn McCarney  * @param sensorJson Sensor JSON object.
738adc4f0dbSShawn McCarney  * @param interfacesDict Map of all sensor interfaces.
739adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with the sensor.  Will
740adc4f0dbSShawn McCarney  * be nullptr if no associated inventory item was found.
741adc4f0dbSShawn McCarney  * @return Health value for sensor.
742adc4f0dbSShawn McCarney  */
743711ac7a9SEd Tanous inline std::string
744711ac7a9SEd Tanous     getHealth(nlohmann::json& sensorJson,
745711ac7a9SEd Tanous               const dbus::utility::DBusInteracesMap& interfacesDict,
746adc4f0dbSShawn McCarney               const InventoryItem* inventoryItem)
74734dd179eSJames Feist {
748adc4f0dbSShawn McCarney     // Get current health value (if any) in the sensor JSON object.  Some JSON
749adc4f0dbSShawn McCarney     // objects contain multiple sensors (such as PowerSupplies).  We want to set
750adc4f0dbSShawn McCarney     // the overall health to be the most severe of any of the sensors.
751adc4f0dbSShawn McCarney     std::string currentHealth;
752adc4f0dbSShawn McCarney     auto statusIt = sensorJson.find("Status");
753adc4f0dbSShawn McCarney     if (statusIt != sensorJson.end())
754adc4f0dbSShawn McCarney     {
755adc4f0dbSShawn McCarney         auto healthIt = statusIt->find("Health");
756adc4f0dbSShawn McCarney         if (healthIt != statusIt->end())
757adc4f0dbSShawn McCarney         {
758adc4f0dbSShawn McCarney             std::string* health = healthIt->get_ptr<std::string*>();
759adc4f0dbSShawn McCarney             if (health != nullptr)
760adc4f0dbSShawn McCarney             {
761adc4f0dbSShawn McCarney                 currentHealth = *health;
762adc4f0dbSShawn McCarney             }
763adc4f0dbSShawn McCarney         }
764adc4f0dbSShawn McCarney     }
765adc4f0dbSShawn McCarney 
766adc4f0dbSShawn McCarney     // If current health in JSON object is already Critical, return that.  This
767adc4f0dbSShawn McCarney     // should override the sensor health, which might be less severe.
768adc4f0dbSShawn McCarney     if (currentHealth == "Critical")
769adc4f0dbSShawn McCarney     {
770adc4f0dbSShawn McCarney         return "Critical";
771adc4f0dbSShawn McCarney     }
772adc4f0dbSShawn McCarney 
773adc4f0dbSShawn McCarney     // Check if sensor has critical threshold alarm
774711ac7a9SEd Tanous 
7759eb808c1SEd Tanous     for (const auto& [interface, values] : interfacesDict)
77634dd179eSJames Feist     {
777711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Sensor.Threshold.Critical")
77834dd179eSJames Feist         {
7799eb808c1SEd Tanous             for (const auto& [valueName, value] : values)
780711ac7a9SEd Tanous             {
781711ac7a9SEd Tanous                 if (valueName == "CriticalAlarmHigh" ||
782711ac7a9SEd Tanous                     valueName == "CriticalAlarmLow")
783711ac7a9SEd Tanous                 {
784711ac7a9SEd Tanous                     const bool* asserted = std::get_if<bool>(&value);
78534dd179eSJames Feist                     if (asserted == nullptr)
78634dd179eSJames Feist                     {
78734dd179eSJames Feist                         BMCWEB_LOG_ERROR << "Illegal sensor threshold";
78834dd179eSJames Feist                     }
78934dd179eSJames Feist                     else if (*asserted)
79034dd179eSJames Feist                     {
79134dd179eSJames Feist                         return "Critical";
79234dd179eSJames Feist                     }
79334dd179eSJames Feist                 }
79434dd179eSJames Feist             }
79534dd179eSJames Feist         }
79634dd179eSJames Feist     }
79734dd179eSJames Feist 
798adc4f0dbSShawn McCarney     // Check if associated inventory item is not functional
799adc4f0dbSShawn McCarney     if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional))
800adc4f0dbSShawn McCarney     {
801adc4f0dbSShawn McCarney         return "Critical";
802adc4f0dbSShawn McCarney     }
803adc4f0dbSShawn McCarney 
804adc4f0dbSShawn McCarney     // If current health in JSON object is already Warning, return that.  This
805adc4f0dbSShawn McCarney     // should override the sensor status, which might be less severe.
806adc4f0dbSShawn McCarney     if (currentHealth == "Warning")
807adc4f0dbSShawn McCarney     {
808adc4f0dbSShawn McCarney         return "Warning";
809adc4f0dbSShawn McCarney     }
810adc4f0dbSShawn McCarney 
811adc4f0dbSShawn McCarney     // Check if sensor has warning threshold alarm
8129eb808c1SEd Tanous     for (const auto& [interface, values] : interfacesDict)
81334dd179eSJames Feist     {
814711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Sensor.Threshold.Warning")
81534dd179eSJames Feist         {
8169eb808c1SEd Tanous             for (const auto& [valueName, value] : values)
817711ac7a9SEd Tanous             {
818711ac7a9SEd Tanous                 if (valueName == "WarningAlarmHigh" ||
819711ac7a9SEd Tanous                     valueName == "WarningAlarmLow")
820711ac7a9SEd Tanous                 {
821711ac7a9SEd Tanous                     const bool* asserted = std::get_if<bool>(&value);
82234dd179eSJames Feist                     if (asserted == nullptr)
82334dd179eSJames Feist                     {
82434dd179eSJames Feist                         BMCWEB_LOG_ERROR << "Illegal sensor threshold";
82534dd179eSJames Feist                     }
82634dd179eSJames Feist                     else if (*asserted)
82734dd179eSJames Feist                     {
828ebe4d91eSEd Tanous                         return "Warning";
82934dd179eSJames Feist                     }
83034dd179eSJames Feist                 }
83134dd179eSJames Feist             }
83234dd179eSJames Feist         }
83334dd179eSJames Feist     }
834adc4f0dbSShawn McCarney 
83534dd179eSJames Feist     return "OK";
83634dd179eSJames Feist }
83734dd179eSJames Feist 
83823a21a1cSEd Tanous inline void setLedState(nlohmann::json& sensorJson,
839d500549bSAnthony Wilson                         const InventoryItem* inventoryItem)
840d500549bSAnthony Wilson {
841d500549bSAnthony Wilson     if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty())
842d500549bSAnthony Wilson     {
843d500549bSAnthony Wilson         switch (inventoryItem->ledState)
844d500549bSAnthony Wilson         {
845d500549bSAnthony Wilson             case LedState::OFF:
846d500549bSAnthony Wilson                 sensorJson["IndicatorLED"] = "Off";
847d500549bSAnthony Wilson                 break;
848d500549bSAnthony Wilson             case LedState::ON:
849d500549bSAnthony Wilson                 sensorJson["IndicatorLED"] = "Lit";
850d500549bSAnthony Wilson                 break;
851d500549bSAnthony Wilson             case LedState::BLINK:
852d500549bSAnthony Wilson                 sensorJson["IndicatorLED"] = "Blinking";
853d500549bSAnthony Wilson                 break;
85423a21a1cSEd Tanous             case LedState::UNKNOWN:
855d500549bSAnthony Wilson                 break;
856d500549bSAnthony Wilson         }
857d500549bSAnthony Wilson     }
858d500549bSAnthony Wilson }
859d500549bSAnthony Wilson 
86034dd179eSJames Feist /**
86108777fb0SLewanczyk, Dawid  * @brief Builds a json sensor representation of a sensor.
86208777fb0SLewanczyk, Dawid  * @param sensorName  The name of the sensor to be built
863274fad5aSGunnar Mills  * @param sensorType  The type (temperature, fan_tach, etc) of the sensor to
86408777fb0SLewanczyk, Dawid  * build
865a0ec28b6SAdrian Ambrożewicz  * @param sensorsAsyncResp  Sensor metadata
86608777fb0SLewanczyk, Dawid  * @param interfacesDict  A dictionary of the interfaces and properties of said
86708777fb0SLewanczyk, Dawid  * interfaces to be built from
86808777fb0SLewanczyk, Dawid  * @param sensor_json  The json object to fill
869adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with the sensor.  Will
870adc4f0dbSShawn McCarney  * be nullptr if no associated inventory item was found.
87108777fb0SLewanczyk, Dawid  */
87223a21a1cSEd Tanous inline void objectInterfacesToJson(
87308777fb0SLewanczyk, Dawid     const std::string& sensorName, const std::string& sensorType,
874b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
875711ac7a9SEd Tanous     const dbus::utility::DBusInteracesMap& interfacesDict,
87681ce609eSEd Tanous     nlohmann::json& sensorJson, InventoryItem* inventoryItem)
8771abe55efSEd Tanous {
87808777fb0SLewanczyk, Dawid     // Assume values exist as is (10^0 == 1) if no scale exists
87908777fb0SLewanczyk, Dawid     int64_t scaleMultiplier = 0;
8809eb808c1SEd Tanous     for (const auto& [interface, values] : interfacesDict)
8811abe55efSEd Tanous     {
882711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Sensor.Value")
883711ac7a9SEd Tanous         {
8849eb808c1SEd Tanous             for (const auto& [valueName, value] : values)
885711ac7a9SEd Tanous             {
886711ac7a9SEd Tanous                 if (valueName == "Scale")
887711ac7a9SEd Tanous                 {
888711ac7a9SEd Tanous                     const int64_t* int64Value = std::get_if<int64_t>(&value);
8891abe55efSEd Tanous                     if (int64Value != nullptr)
8901abe55efSEd Tanous                     {
89108777fb0SLewanczyk, Dawid                         scaleMultiplier = *int64Value;
89208777fb0SLewanczyk, Dawid                     }
89308777fb0SLewanczyk, Dawid                 }
894711ac7a9SEd Tanous             }
895711ac7a9SEd Tanous         }
896711ac7a9SEd Tanous     }
89708777fb0SLewanczyk, Dawid 
898a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
899adc4f0dbSShawn McCarney     {
90095a3ecadSAnthony Wilson         // For sensors in SensorCollection we set Id instead of MemberId,
90195a3ecadSAnthony Wilson         // including power sensors.
90281ce609eSEd Tanous         sensorJson["Id"] = sensorName;
90381ce609eSEd Tanous         sensorJson["Name"] = boost::replace_all_copy(sensorName, "_", " ");
90495a3ecadSAnthony Wilson     }
90595a3ecadSAnthony Wilson     else if (sensorType != "power")
90695a3ecadSAnthony Wilson     {
90795a3ecadSAnthony Wilson         // Set MemberId and Name for non-power sensors.  For PowerSupplies and
90895a3ecadSAnthony Wilson         // PowerControl, those properties have more general values because
90995a3ecadSAnthony Wilson         // multiple sensors can be stored in the same JSON object.
91081ce609eSEd Tanous         sensorJson["MemberId"] = sensorName;
91181ce609eSEd Tanous         sensorJson["Name"] = boost::replace_all_copy(sensorName, "_", " ");
912adc4f0dbSShawn McCarney     }
913e742b6ccSEd Tanous 
91481ce609eSEd Tanous     sensorJson["Status"]["State"] = getState(inventoryItem);
91581ce609eSEd Tanous     sensorJson["Status"]["Health"] =
91681ce609eSEd Tanous         getHealth(sensorJson, interfacesDict, inventoryItem);
91708777fb0SLewanczyk, Dawid 
91808777fb0SLewanczyk, Dawid     // Parameter to set to override the type we get from dbus, and force it to
91908777fb0SLewanczyk, Dawid     // int, regardless of what is available.  This is used for schemas like fan,
92008777fb0SLewanczyk, Dawid     // that require integers, not floats.
92108777fb0SLewanczyk, Dawid     bool forceToInt = false;
92208777fb0SLewanczyk, Dawid 
9233929aca1SAnthony Wilson     nlohmann::json::json_pointer unit("/Reading");
924a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
92595a3ecadSAnthony Wilson     {
9262a4ba195SShounak Mitra         sensorJson["@odata.type"] = "#Sensor.v1_2_0.Sensor";
927c2bf7f99SWludzik, Jozef 
928c2bf7f99SWludzik, Jozef         const std::string& readingType = sensors::toReadingType(sensorType);
929c2bf7f99SWludzik, Jozef         if (readingType.empty())
93095a3ecadSAnthony Wilson         {
931c2bf7f99SWludzik, Jozef             BMCWEB_LOG_ERROR << "Redfish cannot map reading type for "
932c2bf7f99SWludzik, Jozef                              << sensorType;
93395a3ecadSAnthony Wilson         }
934c2bf7f99SWludzik, Jozef         else
93595a3ecadSAnthony Wilson         {
936c2bf7f99SWludzik, Jozef             sensorJson["ReadingType"] = readingType;
93795a3ecadSAnthony Wilson         }
938c2bf7f99SWludzik, Jozef 
939c2bf7f99SWludzik, Jozef         const std::string& readingUnits = sensors::toReadingUnits(sensorType);
940c2bf7f99SWludzik, Jozef         if (readingUnits.empty())
941f8ede15eSAdrian Ambrożewicz         {
942c2bf7f99SWludzik, Jozef             BMCWEB_LOG_ERROR << "Redfish cannot map reading unit for "
943c2bf7f99SWludzik, Jozef                              << sensorType;
944c2bf7f99SWludzik, Jozef         }
945c2bf7f99SWludzik, Jozef         else
946c2bf7f99SWludzik, Jozef         {
947c2bf7f99SWludzik, Jozef             sensorJson["ReadingUnits"] = readingUnits;
948f8ede15eSAdrian Ambrożewicz         }
94995a3ecadSAnthony Wilson     }
95095a3ecadSAnthony Wilson     else if (sensorType == "temperature")
9511abe55efSEd Tanous     {
9523929aca1SAnthony Wilson         unit = "/ReadingCelsius"_json_pointer;
95381ce609eSEd Tanous         sensorJson["@odata.type"] = "#Thermal.v1_3_0.Temperature";
95408777fb0SLewanczyk, Dawid         // TODO(ed) Documentation says that path should be type fan_tach,
95508777fb0SLewanczyk, Dawid         // implementation seems to implement fan
9561abe55efSEd Tanous     }
9571abe55efSEd Tanous     else if (sensorType == "fan" || sensorType == "fan_tach")
9581abe55efSEd Tanous     {
9593929aca1SAnthony Wilson         unit = "/Reading"_json_pointer;
96081ce609eSEd Tanous         sensorJson["ReadingUnits"] = "RPM";
96181ce609eSEd Tanous         sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
96281ce609eSEd Tanous         setLedState(sensorJson, inventoryItem);
96308777fb0SLewanczyk, Dawid         forceToInt = true;
9641abe55efSEd Tanous     }
9656f6d0d32SEd Tanous     else if (sensorType == "fan_pwm")
9666f6d0d32SEd Tanous     {
9673929aca1SAnthony Wilson         unit = "/Reading"_json_pointer;
96881ce609eSEd Tanous         sensorJson["ReadingUnits"] = "Percent";
96981ce609eSEd Tanous         sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
97081ce609eSEd Tanous         setLedState(sensorJson, inventoryItem);
9716f6d0d32SEd Tanous         forceToInt = true;
9726f6d0d32SEd Tanous     }
9731abe55efSEd Tanous     else if (sensorType == "voltage")
9741abe55efSEd Tanous     {
9753929aca1SAnthony Wilson         unit = "/ReadingVolts"_json_pointer;
97681ce609eSEd Tanous         sensorJson["@odata.type"] = "#Power.v1_0_0.Voltage";
9771abe55efSEd Tanous     }
9782474adfaSEd Tanous     else if (sensorType == "power")
9792474adfaSEd Tanous     {
98049c53ac9SJohnathan Mantey         std::string sensorNameLower =
98149c53ac9SJohnathan Mantey             boost::algorithm::to_lower_copy(sensorName);
98249c53ac9SJohnathan Mantey 
98355f79e6fSEd Tanous         if (sensorName == "total_power")
984028f7ebcSEddie James         {
98581ce609eSEd Tanous             sensorJson["@odata.type"] = "#Power.v1_0_0.PowerControl";
9867ab06f49SGunnar Mills             // Put multiple "sensors" into a single PowerControl, so have
9877ab06f49SGunnar Mills             // generic names for MemberId and Name. Follows Redfish mockup.
98881ce609eSEd Tanous             sensorJson["MemberId"] = "0";
98981ce609eSEd Tanous             sensorJson["Name"] = "Chassis Power Control";
9903929aca1SAnthony Wilson             unit = "/PowerConsumedWatts"_json_pointer;
991028f7ebcSEddie James         }
992028f7ebcSEddie James         else if (sensorNameLower.find("input") != std::string::npos)
99349c53ac9SJohnathan Mantey         {
9943929aca1SAnthony Wilson             unit = "/PowerInputWatts"_json_pointer;
99549c53ac9SJohnathan Mantey         }
99649c53ac9SJohnathan Mantey         else
99749c53ac9SJohnathan Mantey         {
9983929aca1SAnthony Wilson             unit = "/PowerOutputWatts"_json_pointer;
99949c53ac9SJohnathan Mantey         }
10002474adfaSEd Tanous     }
10011abe55efSEd Tanous     else
10021abe55efSEd Tanous     {
100355c7b7a2SEd Tanous         BMCWEB_LOG_ERROR << "Redfish cannot map object type for " << sensorName;
100408777fb0SLewanczyk, Dawid         return;
100508777fb0SLewanczyk, Dawid     }
100608777fb0SLewanczyk, Dawid     // Map of dbus interface name, dbus property name and redfish property_name
10073929aca1SAnthony Wilson     std::vector<
10083929aca1SAnthony Wilson         std::tuple<const char*, const char*, nlohmann::json::json_pointer>>
10093929aca1SAnthony Wilson         properties;
101008777fb0SLewanczyk, Dawid     properties.reserve(7);
101108777fb0SLewanczyk, Dawid 
101208777fb0SLewanczyk, Dawid     properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit);
1013de629b6eSShawn McCarney 
1014a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
10153929aca1SAnthony Wilson     {
10163929aca1SAnthony Wilson         properties.emplace_back(
10173929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh",
10183929aca1SAnthony Wilson             "/Thresholds/UpperCaution/Reading"_json_pointer);
10193929aca1SAnthony Wilson         properties.emplace_back(
10203929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow",
10213929aca1SAnthony Wilson             "/Thresholds/LowerCaution/Reading"_json_pointer);
10223929aca1SAnthony Wilson         properties.emplace_back(
10233929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh",
10243929aca1SAnthony Wilson             "/Thresholds/UpperCritical/Reading"_json_pointer);
10253929aca1SAnthony Wilson         properties.emplace_back(
10263929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow",
10273929aca1SAnthony Wilson             "/Thresholds/LowerCritical/Reading"_json_pointer);
10283929aca1SAnthony Wilson     }
10293929aca1SAnthony Wilson     else if (sensorType != "power")
1030de629b6eSShawn McCarney     {
103108777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
10323929aca1SAnthony Wilson                                 "WarningHigh",
10333929aca1SAnthony Wilson                                 "/UpperThresholdNonCritical"_json_pointer);
103408777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
10353929aca1SAnthony Wilson                                 "WarningLow",
10363929aca1SAnthony Wilson                                 "/LowerThresholdNonCritical"_json_pointer);
103708777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
10383929aca1SAnthony Wilson                                 "CriticalHigh",
10393929aca1SAnthony Wilson                                 "/UpperThresholdCritical"_json_pointer);
104008777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
10413929aca1SAnthony Wilson                                 "CriticalLow",
10423929aca1SAnthony Wilson                                 "/LowerThresholdCritical"_json_pointer);
1043de629b6eSShawn McCarney     }
104408777fb0SLewanczyk, Dawid 
10452474adfaSEd Tanous     // TODO Need to get UpperThresholdFatal and LowerThresholdFatal
10462474adfaSEd Tanous 
1047a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
104895a3ecadSAnthony Wilson     {
104995a3ecadSAnthony Wilson         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
10503929aca1SAnthony Wilson                                 "/ReadingRangeMin"_json_pointer);
105195a3ecadSAnthony Wilson         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
10523929aca1SAnthony Wilson                                 "/ReadingRangeMax"_json_pointer);
105395a3ecadSAnthony Wilson     }
105495a3ecadSAnthony Wilson     else if (sensorType == "temperature")
10551abe55efSEd Tanous     {
105608777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
10573929aca1SAnthony Wilson                                 "/MinReadingRangeTemp"_json_pointer);
105808777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
10593929aca1SAnthony Wilson                                 "/MaxReadingRangeTemp"_json_pointer);
10601abe55efSEd Tanous     }
1061adc4f0dbSShawn McCarney     else if (sensorType != "power")
10621abe55efSEd Tanous     {
106308777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
10643929aca1SAnthony Wilson                                 "/MinReadingRange"_json_pointer);
106508777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
10663929aca1SAnthony Wilson                                 "/MaxReadingRange"_json_pointer);
106708777fb0SLewanczyk, Dawid     }
106808777fb0SLewanczyk, Dawid 
10693929aca1SAnthony Wilson     for (const std::tuple<const char*, const char*,
10703929aca1SAnthony Wilson                           nlohmann::json::json_pointer>& p : properties)
10711abe55efSEd Tanous     {
107255f79e6fSEd Tanous         for (const auto& [interface, values] : interfacesDict)
10731abe55efSEd Tanous         {
1074711ac7a9SEd Tanous             if (interface != std::get<0>(p))
10751abe55efSEd Tanous             {
1076711ac7a9SEd Tanous                 continue;
1077711ac7a9SEd Tanous             }
107855f79e6fSEd Tanous             for (const auto& [valueName, valueVariant] : values)
1079711ac7a9SEd Tanous             {
1080711ac7a9SEd Tanous                 if (valueName != std::get<1>(p))
1081711ac7a9SEd Tanous                 {
1082711ac7a9SEd Tanous                     continue;
1083711ac7a9SEd Tanous                 }
10843929aca1SAnthony Wilson 
10853929aca1SAnthony Wilson                 // The property we want to set may be nested json, so use
10863929aca1SAnthony Wilson                 // a json_pointer for easy indexing into the json structure.
10873929aca1SAnthony Wilson                 const nlohmann::json::json_pointer& key = std::get<2>(p);
10883929aca1SAnthony Wilson 
108908777fb0SLewanczyk, Dawid                 // Attempt to pull the int64 directly
1090abf2add6SEd Tanous                 const int64_t* int64Value = std::get_if<int64_t>(&valueVariant);
109108777fb0SLewanczyk, Dawid 
1092abf2add6SEd Tanous                 const double* doubleValue = std::get_if<double>(&valueVariant);
1093028f7ebcSEddie James                 const uint32_t* uValue = std::get_if<uint32_t>(&valueVariant);
10946f6d0d32SEd Tanous                 double temp = 0.0;
10956f6d0d32SEd Tanous                 if (int64Value != nullptr)
10961abe55efSEd Tanous                 {
1097271584abSEd Tanous                     temp = static_cast<double>(*int64Value);
10986f6d0d32SEd Tanous                 }
10996f6d0d32SEd Tanous                 else if (doubleValue != nullptr)
11001abe55efSEd Tanous                 {
11016f6d0d32SEd Tanous                     temp = *doubleValue;
11021abe55efSEd Tanous                 }
1103028f7ebcSEddie James                 else if (uValue != nullptr)
1104028f7ebcSEddie James                 {
1105028f7ebcSEddie James                     temp = *uValue;
1106028f7ebcSEddie James                 }
11071abe55efSEd Tanous                 else
11081abe55efSEd Tanous                 {
11096f6d0d32SEd Tanous                     BMCWEB_LOG_ERROR
11106f6d0d32SEd Tanous                         << "Got value interface that wasn't int or double";
11116f6d0d32SEd Tanous                     continue;
111208777fb0SLewanczyk, Dawid                 }
11136f6d0d32SEd Tanous                 temp = temp * std::pow(10, scaleMultiplier);
11146f6d0d32SEd Tanous                 if (forceToInt)
11156f6d0d32SEd Tanous                 {
111681ce609eSEd Tanous                     sensorJson[key] = static_cast<int64_t>(temp);
11176f6d0d32SEd Tanous                 }
11186f6d0d32SEd Tanous                 else
11196f6d0d32SEd Tanous                 {
112081ce609eSEd Tanous                     sensorJson[key] = temp;
112108777fb0SLewanczyk, Dawid                 }
112208777fb0SLewanczyk, Dawid             }
112308777fb0SLewanczyk, Dawid         }
112408777fb0SLewanczyk, Dawid     }
1125a0ec28b6SAdrian Ambrożewicz 
112681ce609eSEd Tanous     sensorsAsyncResp->addMetadata(sensorJson, unit.to_string(),
1127a0ec28b6SAdrian Ambrożewicz                                   "/xyz/openbmc_project/sensors/" + sensorType +
1128a0ec28b6SAdrian Ambrożewicz                                       "/" + sensorName);
1129a0ec28b6SAdrian Ambrożewicz 
113055c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "Added sensor " << sensorName;
113108777fb0SLewanczyk, Dawid }
113208777fb0SLewanczyk, Dawid 
1133b5a76932SEd Tanous inline void populateFanRedundancy(
1134b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
11358bd25ccdSJames Feist {
11368bd25ccdSJames Feist     crow::connections::systemBus->async_method_call(
1137b9d36b47SEd Tanous         [sensorsAsyncResp](
1138b9d36b47SEd Tanous             const boost::system::error_code ec,
1139b9d36b47SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& resp) {
11408bd25ccdSJames Feist         if (ec)
11418bd25ccdSJames Feist         {
11428bd25ccdSJames Feist             return; // don't have to have this interface
11438bd25ccdSJames Feist         }
1144002d39b4SEd Tanous         for (const std::pair<
1145002d39b4SEd Tanous                  std::string,
1146002d39b4SEd Tanous                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
1147e278c18fSEd Tanous                  pathPair : resp)
11488bd25ccdSJames Feist         {
1149e278c18fSEd Tanous             const std::string& path = pathPair.first;
1150002d39b4SEd Tanous             const std::vector<std::pair<std::string, std::vector<std::string>>>&
1151002d39b4SEd Tanous                 objDict = pathPair.second;
11528bd25ccdSJames Feist             if (objDict.empty())
11538bd25ccdSJames Feist             {
11548bd25ccdSJames Feist                 continue; // this should be impossible
11558bd25ccdSJames Feist             }
11568bd25ccdSJames Feist 
11578bd25ccdSJames Feist             const std::string& owner = objDict.begin()->first;
11581e1e598dSJonathan Doman             sdbusplus::asio::getProperty<std::vector<std::string>>(
11591e1e598dSJonathan Doman                 *crow::connections::systemBus,
11601e1e598dSJonathan Doman                 "xyz.openbmc_project.ObjectMapper", path + "/chassis",
11611e1e598dSJonathan Doman                 "xyz.openbmc_project.Association", "endpoints",
1162002d39b4SEd Tanous                 [path, owner,
1163002d39b4SEd Tanous                  sensorsAsyncResp](const boost::system::error_code e,
11641e1e598dSJonathan Doman                                    const std::vector<std::string>& endpoints) {
1165271584abSEd Tanous                 if (e)
11668bd25ccdSJames Feist                 {
11678bd25ccdSJames Feist                     return; // if they don't have an association we
11688bd25ccdSJames Feist                             // can't tell what chassis is
11698bd25ccdSJames Feist                 }
1170002d39b4SEd Tanous                 auto found =
1171002d39b4SEd Tanous                     std::find_if(endpoints.begin(), endpoints.end(),
11728bd25ccdSJames Feist                                  [sensorsAsyncResp](const std::string& entry) {
1173002d39b4SEd Tanous                     return entry.find(sensorsAsyncResp->chassisId) !=
11748bd25ccdSJames Feist                            std::string::npos;
11758bd25ccdSJames Feist                     });
11768bd25ccdSJames Feist 
11771e1e598dSJonathan Doman                 if (found == endpoints.end())
11788bd25ccdSJames Feist                 {
11798bd25ccdSJames Feist                     return;
11808bd25ccdSJames Feist                 }
11818bd25ccdSJames Feist                 crow::connections::systemBus->async_method_call(
11828bd25ccdSJames Feist                     [path, sensorsAsyncResp](
1183271584abSEd Tanous                         const boost::system::error_code& err,
1184*fe04d49cSNan Zhou                         const std::map<std::string,
1185*fe04d49cSNan Zhou                                        dbus::utility::DbusVariantType>& ret) {
1186271584abSEd Tanous                     if (err)
11878bd25ccdSJames Feist                     {
11888bd25ccdSJames Feist                         return; // don't have to have this
11898bd25ccdSJames Feist                                 // interface
11908bd25ccdSJames Feist                     }
11918bd25ccdSJames Feist                     auto findFailures = ret.find("AllowedFailures");
11928bd25ccdSJames Feist                     auto findCollection = ret.find("Collection");
11938bd25ccdSJames Feist                     auto findStatus = ret.find("Status");
11948bd25ccdSJames Feist 
11958bd25ccdSJames Feist                     if (findFailures == ret.end() ||
1196002d39b4SEd Tanous                         findCollection == ret.end() || findStatus == ret.end())
11978bd25ccdSJames Feist                     {
1198002d39b4SEd Tanous                         BMCWEB_LOG_ERROR << "Invalid redundancy interface";
11998bd25ccdSJames Feist                         messages::internalError(
12008d1b46d7Szhanghch05                             sensorsAsyncResp->asyncResp->res);
12018bd25ccdSJames Feist                         return;
12028bd25ccdSJames Feist                     }
12038bd25ccdSJames Feist 
12049eb808c1SEd Tanous                     const uint8_t* allowedFailures =
1205002d39b4SEd Tanous                         std::get_if<uint8_t>(&(findFailures->second));
12069eb808c1SEd Tanous                     const std::vector<std::string>* collection =
12078bd25ccdSJames Feist                         std::get_if<std::vector<std::string>>(
12088bd25ccdSJames Feist                             &(findCollection->second));
12099eb808c1SEd Tanous                     const std::string* status =
1210002d39b4SEd Tanous                         std::get_if<std::string>(&(findStatus->second));
12118bd25ccdSJames Feist 
1212002d39b4SEd Tanous                     if (allowedFailures == nullptr || collection == nullptr ||
1213002d39b4SEd Tanous                         status == nullptr)
12148bd25ccdSJames Feist                     {
12158bd25ccdSJames Feist 
12168bd25ccdSJames Feist                         BMCWEB_LOG_ERROR
12170fda0f12SGeorge Liu                             << "Invalid redundancy interface types";
12188bd25ccdSJames Feist                         messages::internalError(
12198d1b46d7Szhanghch05                             sensorsAsyncResp->asyncResp->res);
12208bd25ccdSJames Feist                         return;
12218bd25ccdSJames Feist                     }
1222002d39b4SEd Tanous                     sdbusplus::message::object_path objectPath(path);
122328aa8de5SGeorge Liu                     std::string name = objectPath.filename();
122428aa8de5SGeorge Liu                     if (name.empty())
12258bd25ccdSJames Feist                     {
12268bd25ccdSJames Feist                         // this should be impossible
12278bd25ccdSJames Feist                         messages::internalError(
12288d1b46d7Szhanghch05                             sensorsAsyncResp->asyncResp->res);
12298bd25ccdSJames Feist                         return;
12308bd25ccdSJames Feist                     }
1231002d39b4SEd Tanous                     std::replace(name.begin(), name.end(), '_', ' ');
12328bd25ccdSJames Feist 
12338bd25ccdSJames Feist                     std::string health;
12348bd25ccdSJames Feist 
12358bd25ccdSJames Feist                     if (boost::ends_with(*status, "Full"))
12368bd25ccdSJames Feist                     {
12378bd25ccdSJames Feist                         health = "OK";
12388bd25ccdSJames Feist                     }
12398bd25ccdSJames Feist                     else if (boost::ends_with(*status, "Degraded"))
12408bd25ccdSJames Feist                     {
12418bd25ccdSJames Feist                         health = "Warning";
12428bd25ccdSJames Feist                     }
12438bd25ccdSJames Feist                     else
12448bd25ccdSJames Feist                     {
12458bd25ccdSJames Feist                         health = "Critical";
12468bd25ccdSJames Feist                     }
12471476687dSEd Tanous                     nlohmann::json::array_t redfishCollection;
12488bd25ccdSJames Feist                     const auto& fanRedfish =
1249002d39b4SEd Tanous                         sensorsAsyncResp->asyncResp->res.jsonValue["Fans"];
12508bd25ccdSJames Feist                     for (const std::string& item : *collection)
12518bd25ccdSJames Feist                     {
125228aa8de5SGeorge Liu                         sdbusplus::message::object_path path(item);
125328aa8de5SGeorge Liu                         std::string itemName = path.filename();
125428aa8de5SGeorge Liu                         if (itemName.empty())
125528aa8de5SGeorge Liu                         {
125628aa8de5SGeorge Liu                             continue;
125728aa8de5SGeorge Liu                         }
12588bd25ccdSJames Feist                         /*
12598bd25ccdSJames Feist                         todo(ed): merge patch that fixes the names
12608bd25ccdSJames Feist                         std::replace(itemName.begin(),
12618bd25ccdSJames Feist                                      itemName.end(), '_', ' ');*/
1262002d39b4SEd Tanous                         auto schemaItem =
1263002d39b4SEd Tanous                             std::find_if(fanRedfish.begin(), fanRedfish.end(),
12648bd25ccdSJames Feist                                          [itemName](const nlohmann::json& fan) {
12658bd25ccdSJames Feist                             return fan["MemberId"] == itemName;
12668bd25ccdSJames Feist                             });
12678bd25ccdSJames Feist                         if (schemaItem != fanRedfish.end())
12688bd25ccdSJames Feist                         {
12691476687dSEd Tanous                             nlohmann::json::object_t collection;
12701476687dSEd Tanous                             collection["@odata.id"] =
12711476687dSEd Tanous                                 (*schemaItem)["@odata.id"];
12721476687dSEd Tanous                             redfishCollection.emplace_back(
12731476687dSEd Tanous                                 std::move(collection));
12748bd25ccdSJames Feist                         }
12758bd25ccdSJames Feist                         else
12768bd25ccdSJames Feist                         {
1277002d39b4SEd Tanous                             BMCWEB_LOG_ERROR << "failed to find fan in schema";
12788bd25ccdSJames Feist                             messages::internalError(
12798d1b46d7Szhanghch05                                 sensorsAsyncResp->asyncResp->res);
12808bd25ccdSJames Feist                             return;
12818bd25ccdSJames Feist                         }
12828bd25ccdSJames Feist                     }
12838bd25ccdSJames Feist 
12843e9e72ebSKuiying Wang                     size_t minNumNeeded =
128526f6976fSEd Tanous                         collection->empty()
128626f6976fSEd Tanous                             ? 0
128726f6976fSEd Tanous                             : collection->size() - *allowedFailures;
1288002d39b4SEd Tanous                     nlohmann::json& jResp = sensorsAsyncResp->asyncResp->res
12898bd25ccdSJames Feist                                                 .jsonValue["Redundancy"];
12901476687dSEd Tanous 
12911476687dSEd Tanous                     nlohmann::json::object_t redundancy;
12921476687dSEd Tanous                     redundancy["@odata.id"] =
1293002d39b4SEd Tanous                         "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId +
1294002d39b4SEd Tanous                         "/" + sensorsAsyncResp->chassisSubNode +
1295002d39b4SEd Tanous                         "#/Redundancy/" + std::to_string(jResp.size());
1296002d39b4SEd Tanous                     redundancy["@odata.type"] = "#Redundancy.v1_3_2.Redundancy";
12971476687dSEd Tanous                     redundancy["MinNumNeeded"] = minNumNeeded;
12981476687dSEd Tanous                     redundancy["MemberId"] = name;
12991476687dSEd Tanous                     redundancy["Mode"] = "N+m";
13001476687dSEd Tanous                     redundancy["Name"] = name;
13011476687dSEd Tanous                     redundancy["RedundancySet"] = redfishCollection;
13021476687dSEd Tanous                     redundancy["Status"]["Health"] = health;
13031476687dSEd Tanous                     redundancy["Status"]["State"] = "Enabled";
13041476687dSEd Tanous 
13051476687dSEd Tanous                     jResp.push_back(std::move(redundancy));
13068bd25ccdSJames Feist                     },
1307002d39b4SEd Tanous                     owner, path, "org.freedesktop.DBus.Properties", "GetAll",
13088bd25ccdSJames Feist                     "xyz.openbmc_project.Control.FanRedundancy");
13091e1e598dSJonathan Doman                 });
13108bd25ccdSJames Feist         }
13118bd25ccdSJames Feist         },
13128bd25ccdSJames Feist         "xyz.openbmc_project.ObjectMapper",
13138bd25ccdSJames Feist         "/xyz/openbmc_project/object_mapper",
13148bd25ccdSJames Feist         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
13158bd25ccdSJames Feist         "/xyz/openbmc_project/control", 2,
13168bd25ccdSJames Feist         std::array<const char*, 1>{
13178bd25ccdSJames Feist             "xyz.openbmc_project.Control.FanRedundancy"});
13188bd25ccdSJames Feist }
13198bd25ccdSJames Feist 
1320b5a76932SEd Tanous inline void
132181ce609eSEd Tanous     sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
132249c53ac9SJohnathan Mantey {
13238d1b46d7Szhanghch05     nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue;
132449c53ac9SJohnathan Mantey     std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
132581ce609eSEd Tanous     if (sensorsAsyncResp->chassisSubNode == sensors::node::power)
132649c53ac9SJohnathan Mantey     {
132749c53ac9SJohnathan Mantey         sensorHeaders = {"Voltages", "PowerSupplies"};
132849c53ac9SJohnathan Mantey     }
132949c53ac9SJohnathan Mantey     for (const std::string& sensorGroup : sensorHeaders)
133049c53ac9SJohnathan Mantey     {
133149c53ac9SJohnathan Mantey         nlohmann::json::iterator entry = response.find(sensorGroup);
133249c53ac9SJohnathan Mantey         if (entry != response.end())
133349c53ac9SJohnathan Mantey         {
133449c53ac9SJohnathan Mantey             std::sort(entry->begin(), entry->end(),
133549c53ac9SJohnathan Mantey                       [](nlohmann::json& c1, nlohmann::json& c2) {
133649c53ac9SJohnathan Mantey                 return c1["Name"] < c2["Name"];
133749c53ac9SJohnathan Mantey             });
133849c53ac9SJohnathan Mantey 
133949c53ac9SJohnathan Mantey             // add the index counts to the end of each entry
134049c53ac9SJohnathan Mantey             size_t count = 0;
134149c53ac9SJohnathan Mantey             for (nlohmann::json& sensorJson : *entry)
134249c53ac9SJohnathan Mantey             {
134349c53ac9SJohnathan Mantey                 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
134449c53ac9SJohnathan Mantey                 if (odata == sensorJson.end())
134549c53ac9SJohnathan Mantey                 {
134649c53ac9SJohnathan Mantey                     continue;
134749c53ac9SJohnathan Mantey                 }
134849c53ac9SJohnathan Mantey                 std::string* value = odata->get_ptr<std::string*>();
134949c53ac9SJohnathan Mantey                 if (value != nullptr)
135049c53ac9SJohnathan Mantey                 {
135149c53ac9SJohnathan Mantey                     *value += std::to_string(count);
135249c53ac9SJohnathan Mantey                     count++;
135381ce609eSEd Tanous                     sensorsAsyncResp->updateUri(sensorJson["Name"], *value);
135449c53ac9SJohnathan Mantey                 }
135549c53ac9SJohnathan Mantey             }
135649c53ac9SJohnathan Mantey         }
135749c53ac9SJohnathan Mantey     }
135849c53ac9SJohnathan Mantey }
135949c53ac9SJohnathan Mantey 
136008777fb0SLewanczyk, Dawid /**
1361adc4f0dbSShawn McCarney  * @brief Finds the inventory item with the specified object path.
1362adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1363adc4f0dbSShawn McCarney  * @param invItemObjPath D-Bus object path of inventory item.
1364adc4f0dbSShawn McCarney  * @return Inventory item within vector, or nullptr if no match found.
13658fb49dd6SShawn McCarney  */
136623a21a1cSEd Tanous inline InventoryItem* findInventoryItem(
1367b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1368adc4f0dbSShawn McCarney     const std::string& invItemObjPath)
13698fb49dd6SShawn McCarney {
1370adc4f0dbSShawn McCarney     for (InventoryItem& inventoryItem : *inventoryItems)
13718fb49dd6SShawn McCarney     {
1372adc4f0dbSShawn McCarney         if (inventoryItem.objectPath == invItemObjPath)
13738fb49dd6SShawn McCarney         {
1374adc4f0dbSShawn McCarney             return &inventoryItem;
13758fb49dd6SShawn McCarney         }
13768fb49dd6SShawn McCarney     }
13778fb49dd6SShawn McCarney     return nullptr;
13788fb49dd6SShawn McCarney }
13798fb49dd6SShawn McCarney 
13808fb49dd6SShawn McCarney /**
1381adc4f0dbSShawn McCarney  * @brief Finds the inventory item associated with the specified sensor.
1382adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1383adc4f0dbSShawn McCarney  * @param sensorObjPath D-Bus object path of sensor.
1384adc4f0dbSShawn McCarney  * @return Inventory item within vector, or nullptr if no match found.
13858fb49dd6SShawn McCarney  */
138623a21a1cSEd Tanous inline InventoryItem* findInventoryItemForSensor(
1387b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1388adc4f0dbSShawn McCarney     const std::string& sensorObjPath)
1389adc4f0dbSShawn McCarney {
1390adc4f0dbSShawn McCarney     for (InventoryItem& inventoryItem : *inventoryItems)
1391adc4f0dbSShawn McCarney     {
1392adc4f0dbSShawn McCarney         if (inventoryItem.sensors.count(sensorObjPath) > 0)
1393adc4f0dbSShawn McCarney         {
1394adc4f0dbSShawn McCarney             return &inventoryItem;
1395adc4f0dbSShawn McCarney         }
1396adc4f0dbSShawn McCarney     }
1397adc4f0dbSShawn McCarney     return nullptr;
1398adc4f0dbSShawn McCarney }
1399adc4f0dbSShawn McCarney 
1400adc4f0dbSShawn McCarney /**
1401d500549bSAnthony Wilson  * @brief Finds the inventory item associated with the specified led path.
1402d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1403d500549bSAnthony Wilson  * @param ledObjPath D-Bus object path of led.
1404d500549bSAnthony Wilson  * @return Inventory item within vector, or nullptr if no match found.
1405d500549bSAnthony Wilson  */
1406d500549bSAnthony Wilson inline InventoryItem*
1407d500549bSAnthony Wilson     findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems,
1408d500549bSAnthony Wilson                             const std::string& ledObjPath)
1409d500549bSAnthony Wilson {
1410d500549bSAnthony Wilson     for (InventoryItem& inventoryItem : inventoryItems)
1411d500549bSAnthony Wilson     {
1412d500549bSAnthony Wilson         if (inventoryItem.ledObjectPath == ledObjPath)
1413d500549bSAnthony Wilson         {
1414d500549bSAnthony Wilson             return &inventoryItem;
1415d500549bSAnthony Wilson         }
1416d500549bSAnthony Wilson     }
1417d500549bSAnthony Wilson     return nullptr;
1418d500549bSAnthony Wilson }
1419d500549bSAnthony Wilson 
1420d500549bSAnthony Wilson /**
1421adc4f0dbSShawn McCarney  * @brief Adds inventory item and associated sensor to specified vector.
1422adc4f0dbSShawn McCarney  *
1423adc4f0dbSShawn McCarney  * Adds a new InventoryItem to the vector if necessary.  Searches for an
1424adc4f0dbSShawn McCarney  * existing InventoryItem with the specified object path.  If not found, one is
1425adc4f0dbSShawn McCarney  * added to the vector.
1426adc4f0dbSShawn McCarney  *
1427adc4f0dbSShawn McCarney  * Next, the specified sensor is added to the set of sensors associated with the
1428adc4f0dbSShawn McCarney  * InventoryItem.
1429adc4f0dbSShawn McCarney  *
1430adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1431adc4f0dbSShawn McCarney  * @param invItemObjPath D-Bus object path of inventory item.
1432adc4f0dbSShawn McCarney  * @param sensorObjPath D-Bus object path of sensor
1433adc4f0dbSShawn McCarney  */
1434b5a76932SEd Tanous inline void addInventoryItem(
1435b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1436b5a76932SEd Tanous     const std::string& invItemObjPath, const std::string& sensorObjPath)
1437adc4f0dbSShawn McCarney {
1438adc4f0dbSShawn McCarney     // Look for inventory item in vector
1439adc4f0dbSShawn McCarney     InventoryItem* inventoryItem =
1440adc4f0dbSShawn McCarney         findInventoryItem(inventoryItems, invItemObjPath);
1441adc4f0dbSShawn McCarney 
1442adc4f0dbSShawn McCarney     // If inventory item doesn't exist in vector, add it
1443adc4f0dbSShawn McCarney     if (inventoryItem == nullptr)
1444adc4f0dbSShawn McCarney     {
1445adc4f0dbSShawn McCarney         inventoryItems->emplace_back(invItemObjPath);
1446adc4f0dbSShawn McCarney         inventoryItem = &(inventoryItems->back());
1447adc4f0dbSShawn McCarney     }
1448adc4f0dbSShawn McCarney 
1449adc4f0dbSShawn McCarney     // Add sensor to set of sensors associated with inventory item
1450adc4f0dbSShawn McCarney     inventoryItem->sensors.emplace(sensorObjPath);
1451adc4f0dbSShawn McCarney }
1452adc4f0dbSShawn McCarney 
1453adc4f0dbSShawn McCarney /**
1454adc4f0dbSShawn McCarney  * @brief Stores D-Bus data in the specified inventory item.
1455adc4f0dbSShawn McCarney  *
1456adc4f0dbSShawn McCarney  * Finds D-Bus data in the specified map of interfaces.  Stores the data in the
1457adc4f0dbSShawn McCarney  * specified InventoryItem.
1458adc4f0dbSShawn McCarney  *
1459adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
1460adc4f0dbSShawn McCarney  * response.
1461adc4f0dbSShawn McCarney  *
1462adc4f0dbSShawn McCarney  * @param inventoryItem Inventory item where data will be stored.
1463adc4f0dbSShawn McCarney  * @param interfacesDict Map containing D-Bus interfaces and their properties
1464adc4f0dbSShawn McCarney  * for the specified inventory item.
1465adc4f0dbSShawn McCarney  */
146623a21a1cSEd Tanous inline void storeInventoryItemData(
1467adc4f0dbSShawn McCarney     InventoryItem& inventoryItem,
1468711ac7a9SEd Tanous     const dbus::utility::DBusInteracesMap& interfacesDict)
14698fb49dd6SShawn McCarney {
1470adc4f0dbSShawn McCarney     // Get properties from Inventory.Item interface
1471711ac7a9SEd Tanous 
14729eb808c1SEd Tanous     for (const auto& [interface, values] : interfacesDict)
14738fb49dd6SShawn McCarney     {
1474711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Item")
14758fb49dd6SShawn McCarney         {
14769eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
1477711ac7a9SEd Tanous             {
1478711ac7a9SEd Tanous                 if (name == "Present")
1479711ac7a9SEd Tanous                 {
1480711ac7a9SEd Tanous                     const bool* value = std::get_if<bool>(&dbusValue);
1481adc4f0dbSShawn McCarney                     if (value != nullptr)
14828fb49dd6SShawn McCarney                     {
1483adc4f0dbSShawn McCarney                         inventoryItem.isPresent = *value;
14848fb49dd6SShawn McCarney                     }
14858fb49dd6SShawn McCarney                 }
14868fb49dd6SShawn McCarney             }
1487711ac7a9SEd Tanous         }
1488adc4f0dbSShawn McCarney         // Check if Inventory.Item.PowerSupply interface is present
1489711ac7a9SEd Tanous 
1490711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Item.PowerSupply")
14918fb49dd6SShawn McCarney         {
1492adc4f0dbSShawn McCarney             inventoryItem.isPowerSupply = true;
14938fb49dd6SShawn McCarney         }
1494adc4f0dbSShawn McCarney 
1495adc4f0dbSShawn McCarney         // Get properties from Inventory.Decorator.Asset interface
1496711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
1497adc4f0dbSShawn McCarney         {
14989eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
1499711ac7a9SEd Tanous             {
1500711ac7a9SEd Tanous                 if (name == "Manufacturer")
1501adc4f0dbSShawn McCarney                 {
1502adc4f0dbSShawn McCarney                     const std::string* value =
1503711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
1504adc4f0dbSShawn McCarney                     if (value != nullptr)
1505adc4f0dbSShawn McCarney                     {
1506adc4f0dbSShawn McCarney                         inventoryItem.manufacturer = *value;
1507adc4f0dbSShawn McCarney                     }
1508adc4f0dbSShawn McCarney                 }
1509711ac7a9SEd Tanous                 if (name == "Model")
1510adc4f0dbSShawn McCarney                 {
1511adc4f0dbSShawn McCarney                     const std::string* value =
1512711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
1513adc4f0dbSShawn McCarney                     if (value != nullptr)
1514adc4f0dbSShawn McCarney                     {
1515adc4f0dbSShawn McCarney                         inventoryItem.model = *value;
1516adc4f0dbSShawn McCarney                     }
1517adc4f0dbSShawn McCarney                 }
1518711ac7a9SEd Tanous                 if (name == "SerialNumber")
1519adc4f0dbSShawn McCarney                 {
1520adc4f0dbSShawn McCarney                     const std::string* value =
1521711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
1522adc4f0dbSShawn McCarney                     if (value != nullptr)
1523adc4f0dbSShawn McCarney                     {
1524adc4f0dbSShawn McCarney                         inventoryItem.serialNumber = *value;
1525adc4f0dbSShawn McCarney                     }
1526adc4f0dbSShawn McCarney                 }
1527711ac7a9SEd Tanous                 if (name == "PartNumber")
1528711ac7a9SEd Tanous                 {
1529711ac7a9SEd Tanous                     const std::string* value =
1530711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
1531711ac7a9SEd Tanous                     if (value != nullptr)
1532711ac7a9SEd Tanous                     {
1533711ac7a9SEd Tanous                         inventoryItem.partNumber = *value;
1534711ac7a9SEd Tanous                     }
1535711ac7a9SEd Tanous                 }
1536711ac7a9SEd Tanous             }
1537adc4f0dbSShawn McCarney         }
1538adc4f0dbSShawn McCarney 
1539711ac7a9SEd Tanous         if (interface ==
1540711ac7a9SEd Tanous             "xyz.openbmc_project.State.Decorator.OperationalStatus")
1541adc4f0dbSShawn McCarney         {
15429eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
1543adc4f0dbSShawn McCarney             {
1544711ac7a9SEd Tanous                 if (name == "Functional")
1545711ac7a9SEd Tanous                 {
1546711ac7a9SEd Tanous                     const bool* value = std::get_if<bool>(&dbusValue);
1547adc4f0dbSShawn McCarney                     if (value != nullptr)
1548adc4f0dbSShawn McCarney                     {
1549adc4f0dbSShawn McCarney                         inventoryItem.isFunctional = *value;
15508fb49dd6SShawn McCarney                     }
15518fb49dd6SShawn McCarney                 }
15528fb49dd6SShawn McCarney             }
15538fb49dd6SShawn McCarney         }
1554711ac7a9SEd Tanous     }
1555711ac7a9SEd Tanous }
15568fb49dd6SShawn McCarney 
15578fb49dd6SShawn McCarney /**
1558adc4f0dbSShawn McCarney  * @brief Gets D-Bus data for inventory items associated with sensors.
15598fb49dd6SShawn McCarney  *
1560adc4f0dbSShawn McCarney  * Uses the specified connections (services) to obtain D-Bus data for inventory
1561adc4f0dbSShawn McCarney  * items associated with sensors.  Stores the resulting data in the
1562adc4f0dbSShawn McCarney  * inventoryItems vector.
15638fb49dd6SShawn McCarney  *
1564adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
1565adc4f0dbSShawn McCarney  * response.
1566adc4f0dbSShawn McCarney  *
1567adc4f0dbSShawn McCarney  * Finds the inventory item data asynchronously.  Invokes callback when data has
1568adc4f0dbSShawn McCarney  * been obtained.
1569adc4f0dbSShawn McCarney  *
1570adc4f0dbSShawn McCarney  * The callback must have the following signature:
1571adc4f0dbSShawn McCarney  *   @code
1572d500549bSAnthony Wilson  *   callback(void)
1573adc4f0dbSShawn McCarney  *   @endcode
1574adc4f0dbSShawn McCarney  *
1575adc4f0dbSShawn McCarney  * This function is called recursively, obtaining data asynchronously from one
1576adc4f0dbSShawn McCarney  * connection in each call.  This ensures the callback is not invoked until the
1577adc4f0dbSShawn McCarney  * last asynchronous function has completed.
15788fb49dd6SShawn McCarney  *
15798fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
1580adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1581adc4f0dbSShawn McCarney  * @param invConnections Connections that provide data for the inventory items.
15828fb49dd6SShawn McCarney  * @param objectMgrPaths Mappings from connection name to DBus object path that
15838fb49dd6SShawn McCarney  * implements ObjectManager.
1584adc4f0dbSShawn McCarney  * @param callback Callback to invoke when inventory data has been obtained.
1585adc4f0dbSShawn McCarney  * @param invConnectionsIndex Current index in invConnections.  Only specified
1586adc4f0dbSShawn McCarney  * in recursive calls to this function.
15878fb49dd6SShawn McCarney  */
1588adc4f0dbSShawn McCarney template <typename Callback>
1589adc4f0dbSShawn McCarney static void getInventoryItemsData(
15908fb49dd6SShawn McCarney     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1591adc4f0dbSShawn McCarney     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1592*fe04d49cSNan Zhou     std::shared_ptr<std::set<std::string>> invConnections,
1593*fe04d49cSNan Zhou     std::shared_ptr<std::map<std::string, std::string>> objectMgrPaths,
1594271584abSEd Tanous     Callback&& callback, size_t invConnectionsIndex = 0)
15958fb49dd6SShawn McCarney {
1596adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsData enter";
15978fb49dd6SShawn McCarney 
1598adc4f0dbSShawn McCarney     // If no more connections left, call callback
1599adc4f0dbSShawn McCarney     if (invConnectionsIndex >= invConnections->size())
16008fb49dd6SShawn McCarney     {
1601d500549bSAnthony Wilson         callback();
1602adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
1603adc4f0dbSShawn McCarney         return;
1604adc4f0dbSShawn McCarney     }
1605adc4f0dbSShawn McCarney 
1606adc4f0dbSShawn McCarney     // Get inventory item data from current connection
1607*fe04d49cSNan Zhou     auto it = invConnections->begin();
1608*fe04d49cSNan Zhou     std::advance(it, invConnectionsIndex);
1609adc4f0dbSShawn McCarney     if (it != invConnections->end())
1610adc4f0dbSShawn McCarney     {
1611adc4f0dbSShawn McCarney         const std::string& invConnection = *it;
1612adc4f0dbSShawn McCarney 
16138fb49dd6SShawn McCarney         // Response handler for GetManagedObjects
1614002d39b4SEd Tanous         auto respHandler =
1615002d39b4SEd Tanous             [sensorsAsyncResp, inventoryItems, invConnections, objectMgrPaths,
1616f94c4ecfSEd Tanous              callback{std::forward<Callback>(callback)},
1617002d39b4SEd Tanous              invConnectionsIndex](const boost::system::error_code ec,
1618711ac7a9SEd Tanous                                   dbus::utility::ManagedObjectType& resp) {
1619adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler enter";
16208fb49dd6SShawn McCarney             if (ec)
16218fb49dd6SShawn McCarney             {
16228fb49dd6SShawn McCarney                 BMCWEB_LOG_ERROR
1623adc4f0dbSShawn McCarney                     << "getInventoryItemsData respHandler DBus error " << ec;
16248d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
16258fb49dd6SShawn McCarney                 return;
16268fb49dd6SShawn McCarney             }
16278fb49dd6SShawn McCarney 
16288fb49dd6SShawn McCarney             // Loop through returned object paths
16298fb49dd6SShawn McCarney             for (const auto& objDictEntry : resp)
16308fb49dd6SShawn McCarney             {
16318fb49dd6SShawn McCarney                 const std::string& objPath =
16328fb49dd6SShawn McCarney                     static_cast<const std::string&>(objDictEntry.first);
16338fb49dd6SShawn McCarney 
1634adc4f0dbSShawn McCarney                 // If this object path is one of the specified inventory items
1635adc4f0dbSShawn McCarney                 InventoryItem* inventoryItem =
1636adc4f0dbSShawn McCarney                     findInventoryItem(inventoryItems, objPath);
1637adc4f0dbSShawn McCarney                 if (inventoryItem != nullptr)
16388fb49dd6SShawn McCarney                 {
1639adc4f0dbSShawn McCarney                     // Store inventory data in InventoryItem
1640adc4f0dbSShawn McCarney                     storeInventoryItemData(*inventoryItem, objDictEntry.second);
16418fb49dd6SShawn McCarney                 }
16428fb49dd6SShawn McCarney             }
16438fb49dd6SShawn McCarney 
1644adc4f0dbSShawn McCarney             // Recurse to get inventory item data from next connection
1645adc4f0dbSShawn McCarney             getInventoryItemsData(sensorsAsyncResp, inventoryItems,
1646adc4f0dbSShawn McCarney                                   invConnections, objectMgrPaths,
1647adc4f0dbSShawn McCarney                                   std::move(callback), invConnectionsIndex + 1);
1648adc4f0dbSShawn McCarney 
1649adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler exit";
16508fb49dd6SShawn McCarney         };
16518fb49dd6SShawn McCarney 
16528fb49dd6SShawn McCarney         // Find DBus object path that implements ObjectManager for the current
16538fb49dd6SShawn McCarney         // connection.  If no mapping found, default to "/".
16548fb49dd6SShawn McCarney         auto iter = objectMgrPaths->find(invConnection);
16558fb49dd6SShawn McCarney         const std::string& objectMgrPath =
16568fb49dd6SShawn McCarney             (iter != objectMgrPaths->end()) ? iter->second : "/";
16578fb49dd6SShawn McCarney         BMCWEB_LOG_DEBUG << "ObjectManager path for " << invConnection << " is "
16588fb49dd6SShawn McCarney                          << objectMgrPath;
16598fb49dd6SShawn McCarney 
16608fb49dd6SShawn McCarney         // Get all object paths and their interfaces for current connection
16618fb49dd6SShawn McCarney         crow::connections::systemBus->async_method_call(
16628fb49dd6SShawn McCarney             std::move(respHandler), invConnection, objectMgrPath,
16638fb49dd6SShawn McCarney             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
16648fb49dd6SShawn McCarney     }
16658fb49dd6SShawn McCarney 
1666adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
16678fb49dd6SShawn McCarney }
16688fb49dd6SShawn McCarney 
16698fb49dd6SShawn McCarney /**
1670adc4f0dbSShawn McCarney  * @brief Gets connections that provide D-Bus data for inventory items.
16718fb49dd6SShawn McCarney  *
1672adc4f0dbSShawn McCarney  * Gets the D-Bus connections (services) that provide data for the inventory
1673adc4f0dbSShawn McCarney  * items that are associated with sensors.
16748fb49dd6SShawn McCarney  *
16758fb49dd6SShawn McCarney  * Finds the connections asynchronously.  Invokes callback when information has
16768fb49dd6SShawn McCarney  * been obtained.
16778fb49dd6SShawn McCarney  *
16788fb49dd6SShawn McCarney  * The callback must have the following signature:
16798fb49dd6SShawn McCarney  *   @code
1680*fe04d49cSNan Zhou  *   callback(std::shared_ptr<std::set<std::string>> invConnections)
16818fb49dd6SShawn McCarney  *   @endcode
16828fb49dd6SShawn McCarney  *
16838fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
1684adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
16858fb49dd6SShawn McCarney  * @param callback Callback to invoke when connections have been obtained.
16868fb49dd6SShawn McCarney  */
16878fb49dd6SShawn McCarney template <typename Callback>
16888fb49dd6SShawn McCarney static void getInventoryItemsConnections(
1689b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1690b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
16918fb49dd6SShawn McCarney     Callback&& callback)
16928fb49dd6SShawn McCarney {
16938fb49dd6SShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsConnections enter";
16948fb49dd6SShawn McCarney 
16958fb49dd6SShawn McCarney     const std::string path = "/xyz/openbmc_project/inventory";
1696adc4f0dbSShawn McCarney     const std::array<std::string, 4> interfaces = {
16978fb49dd6SShawn McCarney         "xyz.openbmc_project.Inventory.Item",
1698adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Item.PowerSupply",
1699adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Decorator.Asset",
17008fb49dd6SShawn McCarney         "xyz.openbmc_project.State.Decorator.OperationalStatus"};
17018fb49dd6SShawn McCarney 
17028fb49dd6SShawn McCarney     // Response handler for parsing output from GetSubTree
1703002d39b4SEd Tanous     auto respHandler =
1704002d39b4SEd Tanous         [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
1705002d39b4SEd Tanous          inventoryItems](
1706b9d36b47SEd Tanous             const boost::system::error_code ec,
1707002d39b4SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) {
17088fb49dd6SShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler enter";
17098fb49dd6SShawn McCarney         if (ec)
17108fb49dd6SShawn McCarney         {
17118d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
17128fb49dd6SShawn McCarney             BMCWEB_LOG_ERROR
17138fb49dd6SShawn McCarney                 << "getInventoryItemsConnections respHandler DBus error " << ec;
17148fb49dd6SShawn McCarney             return;
17158fb49dd6SShawn McCarney         }
17168fb49dd6SShawn McCarney 
17178fb49dd6SShawn McCarney         // Make unique list of connections for desired inventory items
1718*fe04d49cSNan Zhou         std::shared_ptr<std::set<std::string>> invConnections =
1719*fe04d49cSNan Zhou             std::make_shared<std::set<std::string>>();
17208fb49dd6SShawn McCarney 
17218fb49dd6SShawn McCarney         // Loop through objects from GetSubTree
17228fb49dd6SShawn McCarney         for (const std::pair<
17238fb49dd6SShawn McCarney                  std::string,
17248fb49dd6SShawn McCarney                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
17258fb49dd6SShawn McCarney                  object : subtree)
17268fb49dd6SShawn McCarney         {
1727adc4f0dbSShawn McCarney             // Check if object path is one of the specified inventory items
17288fb49dd6SShawn McCarney             const std::string& objPath = object.first;
1729adc4f0dbSShawn McCarney             if (findInventoryItem(inventoryItems, objPath) != nullptr)
17308fb49dd6SShawn McCarney             {
17318fb49dd6SShawn McCarney                 // Store all connections to inventory item
17328fb49dd6SShawn McCarney                 for (const std::pair<std::string, std::vector<std::string>>&
17338fb49dd6SShawn McCarney                          objData : object.second)
17348fb49dd6SShawn McCarney                 {
17358fb49dd6SShawn McCarney                     const std::string& invConnection = objData.first;
17368fb49dd6SShawn McCarney                     invConnections->insert(invConnection);
17378fb49dd6SShawn McCarney                 }
17388fb49dd6SShawn McCarney             }
17398fb49dd6SShawn McCarney         }
1740d500549bSAnthony Wilson 
17418fb49dd6SShawn McCarney         callback(invConnections);
17428fb49dd6SShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler exit";
17438fb49dd6SShawn McCarney     };
17448fb49dd6SShawn McCarney 
17458fb49dd6SShawn McCarney     // Make call to ObjectMapper to find all inventory items
17468fb49dd6SShawn McCarney     crow::connections::systemBus->async_method_call(
17478fb49dd6SShawn McCarney         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
17488fb49dd6SShawn McCarney         "/xyz/openbmc_project/object_mapper",
17498fb49dd6SShawn McCarney         "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
17508fb49dd6SShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsConnections exit";
17518fb49dd6SShawn McCarney }
17528fb49dd6SShawn McCarney 
17538fb49dd6SShawn McCarney /**
1754adc4f0dbSShawn McCarney  * @brief Gets associations from sensors to inventory items.
17558fb49dd6SShawn McCarney  *
17568fb49dd6SShawn McCarney  * Looks for ObjectMapper associations from the specified sensors to related
1757d500549bSAnthony Wilson  * inventory items. Then finds the associations from those inventory items to
1758d500549bSAnthony Wilson  * their LEDs, if any.
17598fb49dd6SShawn McCarney  *
17608fb49dd6SShawn McCarney  * Finds the inventory items asynchronously.  Invokes callback when information
17618fb49dd6SShawn McCarney  * has been obtained.
17628fb49dd6SShawn McCarney  *
17638fb49dd6SShawn McCarney  * The callback must have the following signature:
17648fb49dd6SShawn McCarney  *   @code
1765adc4f0dbSShawn McCarney  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
17668fb49dd6SShawn McCarney  *   @endcode
17678fb49dd6SShawn McCarney  *
17688fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
17698fb49dd6SShawn McCarney  * @param sensorNames All sensors within the current chassis.
17708fb49dd6SShawn McCarney  * @param objectMgrPaths Mappings from connection name to DBus object path that
17718fb49dd6SShawn McCarney  * implements ObjectManager.
17728fb49dd6SShawn McCarney  * @param callback Callback to invoke when inventory items have been obtained.
17738fb49dd6SShawn McCarney  */
17748fb49dd6SShawn McCarney template <typename Callback>
1775adc4f0dbSShawn McCarney static void getInventoryItemAssociations(
1776b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1777*fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& sensorNames,
1778*fe04d49cSNan Zhou     const std::shared_ptr<std::map<std::string, std::string>>& objectMgrPaths,
17798fb49dd6SShawn McCarney     Callback&& callback)
17808fb49dd6SShawn McCarney {
1781adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemAssociations enter";
17828fb49dd6SShawn McCarney 
17838fb49dd6SShawn McCarney     // Response handler for GetManagedObjects
1784f94c4ecfSEd Tanous     auto respHandler = [callback{std::forward<Callback>(callback)},
1785f94c4ecfSEd Tanous                         sensorsAsyncResp,
17868fb49dd6SShawn McCarney                         sensorNames](const boost::system::error_code ec,
17878fb49dd6SShawn McCarney                                      dbus::utility::ManagedObjectType& resp) {
1788adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler enter";
17898fb49dd6SShawn McCarney         if (ec)
17908fb49dd6SShawn McCarney         {
1791adc4f0dbSShawn McCarney             BMCWEB_LOG_ERROR
1792adc4f0dbSShawn McCarney                 << "getInventoryItemAssociations respHandler DBus error " << ec;
17938d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
17948fb49dd6SShawn McCarney             return;
17958fb49dd6SShawn McCarney         }
17968fb49dd6SShawn McCarney 
1797adc4f0dbSShawn McCarney         // Create vector to hold list of inventory items
1798adc4f0dbSShawn McCarney         std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1799adc4f0dbSShawn McCarney             std::make_shared<std::vector<InventoryItem>>();
1800adc4f0dbSShawn McCarney 
18018fb49dd6SShawn McCarney         // Loop through returned object paths
18028fb49dd6SShawn McCarney         std::string sensorAssocPath;
18038fb49dd6SShawn McCarney         sensorAssocPath.reserve(128); // avoid memory allocations
18048fb49dd6SShawn McCarney         for (const auto& objDictEntry : resp)
18058fb49dd6SShawn McCarney         {
18068fb49dd6SShawn McCarney             const std::string& objPath =
18078fb49dd6SShawn McCarney                 static_cast<const std::string&>(objDictEntry.first);
18088fb49dd6SShawn McCarney 
18098fb49dd6SShawn McCarney             // If path is inventory association for one of the specified sensors
18108fb49dd6SShawn McCarney             for (const std::string& sensorName : *sensorNames)
18118fb49dd6SShawn McCarney             {
18128fb49dd6SShawn McCarney                 sensorAssocPath = sensorName;
18138fb49dd6SShawn McCarney                 sensorAssocPath += "/inventory";
18148fb49dd6SShawn McCarney                 if (objPath == sensorAssocPath)
18158fb49dd6SShawn McCarney                 {
18168fb49dd6SShawn McCarney                     // Get Association interface for object path
1817711ac7a9SEd Tanous                     for (const auto& [interface, values] : objDictEntry.second)
18188fb49dd6SShawn McCarney                     {
1819711ac7a9SEd Tanous                         if (interface == "xyz.openbmc_project.Association")
1820711ac7a9SEd Tanous                         {
1821711ac7a9SEd Tanous                             for (const auto& [valueName, value] : values)
1822711ac7a9SEd Tanous                             {
1823711ac7a9SEd Tanous                                 if (valueName == "endpoints")
18248fb49dd6SShawn McCarney                                 {
18258fb49dd6SShawn McCarney                                     const std::vector<std::string>* endpoints =
18268fb49dd6SShawn McCarney                                         std::get_if<std::vector<std::string>>(
1827711ac7a9SEd Tanous                                             &value);
1828711ac7a9SEd Tanous                                     if ((endpoints != nullptr) &&
1829711ac7a9SEd Tanous                                         !endpoints->empty())
18308fb49dd6SShawn McCarney                                     {
1831adc4f0dbSShawn McCarney                                         // Add inventory item to vector
1832adc4f0dbSShawn McCarney                                         const std::string& invItemPath =
1833adc4f0dbSShawn McCarney                                             endpoints->front();
1834711ac7a9SEd Tanous                                         addInventoryItem(inventoryItems,
1835711ac7a9SEd Tanous                                                          invItemPath,
1836adc4f0dbSShawn McCarney                                                          sensorName);
18378fb49dd6SShawn McCarney                                     }
18388fb49dd6SShawn McCarney                                 }
18398fb49dd6SShawn McCarney                             }
1840711ac7a9SEd Tanous                         }
1841711ac7a9SEd Tanous                     }
18428fb49dd6SShawn McCarney                     break;
18438fb49dd6SShawn McCarney                 }
18448fb49dd6SShawn McCarney             }
18458fb49dd6SShawn McCarney         }
18468fb49dd6SShawn McCarney 
1847d500549bSAnthony Wilson         // Now loop through the returned object paths again, this time to
1848d500549bSAnthony Wilson         // find the leds associated with the inventory items we just found
1849d500549bSAnthony Wilson         std::string inventoryAssocPath;
1850d500549bSAnthony Wilson         inventoryAssocPath.reserve(128); // avoid memory allocations
1851d500549bSAnthony Wilson         for (const auto& objDictEntry : resp)
1852d500549bSAnthony Wilson         {
1853d500549bSAnthony Wilson             const std::string& objPath =
1854d500549bSAnthony Wilson                 static_cast<const std::string&>(objDictEntry.first);
1855d500549bSAnthony Wilson 
1856d500549bSAnthony Wilson             for (InventoryItem& inventoryItem : *inventoryItems)
1857d500549bSAnthony Wilson             {
1858d500549bSAnthony Wilson                 inventoryAssocPath = inventoryItem.objectPath;
1859d500549bSAnthony Wilson                 inventoryAssocPath += "/leds";
1860d500549bSAnthony Wilson                 if (objPath == inventoryAssocPath)
1861d500549bSAnthony Wilson                 {
1862711ac7a9SEd Tanous                     for (const auto& [interface, values] : objDictEntry.second)
1863d500549bSAnthony Wilson                     {
1864711ac7a9SEd Tanous                         if (interface == "xyz.openbmc_project.Association")
1865711ac7a9SEd Tanous                         {
1866711ac7a9SEd Tanous                             for (const auto& [valueName, value] : values)
1867711ac7a9SEd Tanous                             {
1868711ac7a9SEd Tanous                                 if (valueName == "endpoints")
1869d500549bSAnthony Wilson                                 {
1870d500549bSAnthony Wilson                                     const std::vector<std::string>* endpoints =
1871d500549bSAnthony Wilson                                         std::get_if<std::vector<std::string>>(
1872711ac7a9SEd Tanous                                             &value);
1873711ac7a9SEd Tanous                                     if ((endpoints != nullptr) &&
1874711ac7a9SEd Tanous                                         !endpoints->empty())
1875d500549bSAnthony Wilson                                     {
1876711ac7a9SEd Tanous                                         // Add inventory item to vector
1877d500549bSAnthony Wilson                                         // Store LED path in inventory item
1878711ac7a9SEd Tanous                                         const std::string& ledPath =
1879711ac7a9SEd Tanous                                             endpoints->front();
1880d500549bSAnthony Wilson                                         inventoryItem.ledObjectPath = ledPath;
1881d500549bSAnthony Wilson                                     }
1882d500549bSAnthony Wilson                                 }
1883d500549bSAnthony Wilson                             }
1884711ac7a9SEd Tanous                         }
1885711ac7a9SEd Tanous                     }
1886711ac7a9SEd Tanous 
1887d500549bSAnthony Wilson                     break;
1888d500549bSAnthony Wilson                 }
1889d500549bSAnthony Wilson             }
1890d500549bSAnthony Wilson         }
1891adc4f0dbSShawn McCarney         callback(inventoryItems);
1892adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler exit";
18938fb49dd6SShawn McCarney     };
18948fb49dd6SShawn McCarney 
18958fb49dd6SShawn McCarney     // Find DBus object path that implements ObjectManager for ObjectMapper
18968fb49dd6SShawn McCarney     std::string connection = "xyz.openbmc_project.ObjectMapper";
18978fb49dd6SShawn McCarney     auto iter = objectMgrPaths->find(connection);
18988fb49dd6SShawn McCarney     const std::string& objectMgrPath =
18998fb49dd6SShawn McCarney         (iter != objectMgrPaths->end()) ? iter->second : "/";
19008fb49dd6SShawn McCarney     BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
19018fb49dd6SShawn McCarney                      << objectMgrPath;
19028fb49dd6SShawn McCarney 
19038fb49dd6SShawn McCarney     // Call GetManagedObjects on the ObjectMapper to get all associations
19048fb49dd6SShawn McCarney     crow::connections::systemBus->async_method_call(
19058fb49dd6SShawn McCarney         std::move(respHandler), connection, objectMgrPath,
19068fb49dd6SShawn McCarney         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
19078fb49dd6SShawn McCarney 
1908adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemAssociations exit";
19098fb49dd6SShawn McCarney }
19108fb49dd6SShawn McCarney 
19118fb49dd6SShawn McCarney /**
1912d500549bSAnthony Wilson  * @brief Gets D-Bus data for inventory item leds associated with sensors.
1913d500549bSAnthony Wilson  *
1914d500549bSAnthony Wilson  * Uses the specified connections (services) to obtain D-Bus data for inventory
1915d500549bSAnthony Wilson  * item leds associated with sensors.  Stores the resulting data in the
1916d500549bSAnthony Wilson  * inventoryItems vector.
1917d500549bSAnthony Wilson  *
1918d500549bSAnthony Wilson  * This data is later used to provide sensor property values in the JSON
1919d500549bSAnthony Wilson  * response.
1920d500549bSAnthony Wilson  *
1921d500549bSAnthony Wilson  * Finds the inventory item led data asynchronously.  Invokes callback when data
1922d500549bSAnthony Wilson  * has been obtained.
1923d500549bSAnthony Wilson  *
1924d500549bSAnthony Wilson  * The callback must have the following signature:
1925d500549bSAnthony Wilson  *   @code
192642cbe538SGunnar Mills  *   callback()
1927d500549bSAnthony Wilson  *   @endcode
1928d500549bSAnthony Wilson  *
1929d500549bSAnthony Wilson  * This function is called recursively, obtaining data asynchronously from one
1930d500549bSAnthony Wilson  * connection in each call.  This ensures the callback is not invoked until the
1931d500549bSAnthony Wilson  * last asynchronous function has completed.
1932d500549bSAnthony Wilson  *
1933d500549bSAnthony Wilson  * @param sensorsAsyncResp Pointer to object holding response data.
1934d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1935d500549bSAnthony Wilson  * @param ledConnections Connections that provide data for the inventory leds.
1936d500549bSAnthony Wilson  * @param callback Callback to invoke when inventory data has been obtained.
1937d500549bSAnthony Wilson  * @param ledConnectionsIndex Current index in ledConnections.  Only specified
1938d500549bSAnthony Wilson  * in recursive calls to this function.
1939d500549bSAnthony Wilson  */
1940d500549bSAnthony Wilson template <typename Callback>
1941d500549bSAnthony Wilson void getInventoryLedData(
1942d500549bSAnthony Wilson     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1943d500549bSAnthony Wilson     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1944*fe04d49cSNan Zhou     std::shared_ptr<std::map<std::string, std::string>> ledConnections,
1945d500549bSAnthony Wilson     Callback&& callback, size_t ledConnectionsIndex = 0)
1946d500549bSAnthony Wilson {
1947d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLedData enter";
1948d500549bSAnthony Wilson 
1949d500549bSAnthony Wilson     // If no more connections left, call callback
1950d500549bSAnthony Wilson     if (ledConnectionsIndex >= ledConnections->size())
1951d500549bSAnthony Wilson     {
195242cbe538SGunnar Mills         callback();
1953d500549bSAnthony Wilson         BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1954d500549bSAnthony Wilson         return;
1955d500549bSAnthony Wilson     }
1956d500549bSAnthony Wilson 
1957d500549bSAnthony Wilson     // Get inventory item data from current connection
1958*fe04d49cSNan Zhou     auto it = ledConnections->begin();
1959*fe04d49cSNan Zhou     std::advance(it, ledConnectionsIndex);
1960d500549bSAnthony Wilson     if (it != ledConnections->end())
1961d500549bSAnthony Wilson     {
1962d500549bSAnthony Wilson         const std::string& ledPath = (*it).first;
1963d500549bSAnthony Wilson         const std::string& ledConnection = (*it).second;
1964d500549bSAnthony Wilson         // Response handler for Get State property
19651e1e598dSJonathan Doman         auto respHandler =
19661e1e598dSJonathan Doman             [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
1967f94c4ecfSEd Tanous              callback{std::forward<Callback>(callback)}, ledConnectionsIndex](
19681e1e598dSJonathan Doman                 const boost::system::error_code ec, const std::string& state) {
1969d500549bSAnthony Wilson             BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler enter";
1970d500549bSAnthony Wilson             if (ec)
1971d500549bSAnthony Wilson             {
1972d500549bSAnthony Wilson                 BMCWEB_LOG_ERROR
1973d500549bSAnthony Wilson                     << "getInventoryLedData respHandler DBus error " << ec;
19748d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
1975d500549bSAnthony Wilson                 return;
1976d500549bSAnthony Wilson             }
1977d500549bSAnthony Wilson 
19781e1e598dSJonathan Doman             BMCWEB_LOG_DEBUG << "Led state: " << state;
1979d500549bSAnthony Wilson             // Find inventory item with this LED object path
1980d500549bSAnthony Wilson             InventoryItem* inventoryItem =
1981d500549bSAnthony Wilson                 findInventoryItemForLed(*inventoryItems, ledPath);
1982d500549bSAnthony Wilson             if (inventoryItem != nullptr)
1983d500549bSAnthony Wilson             {
1984d500549bSAnthony Wilson                 // Store LED state in InventoryItem
19851e1e598dSJonathan Doman                 if (boost::ends_with(state, "On"))
1986d500549bSAnthony Wilson                 {
1987d500549bSAnthony Wilson                     inventoryItem->ledState = LedState::ON;
1988d500549bSAnthony Wilson                 }
19891e1e598dSJonathan Doman                 else if (boost::ends_with(state, "Blink"))
1990d500549bSAnthony Wilson                 {
1991d500549bSAnthony Wilson                     inventoryItem->ledState = LedState::BLINK;
1992d500549bSAnthony Wilson                 }
19931e1e598dSJonathan Doman                 else if (boost::ends_with(state, "Off"))
1994d500549bSAnthony Wilson                 {
1995d500549bSAnthony Wilson                     inventoryItem->ledState = LedState::OFF;
1996d500549bSAnthony Wilson                 }
1997d500549bSAnthony Wilson                 else
1998d500549bSAnthony Wilson                 {
1999d500549bSAnthony Wilson                     inventoryItem->ledState = LedState::UNKNOWN;
2000d500549bSAnthony Wilson                 }
2001d500549bSAnthony Wilson             }
2002d500549bSAnthony Wilson 
2003d500549bSAnthony Wilson             // Recurse to get LED data from next connection
2004d500549bSAnthony Wilson             getInventoryLedData(sensorsAsyncResp, inventoryItems,
2005d500549bSAnthony Wilson                                 ledConnections, std::move(callback),
2006d500549bSAnthony Wilson                                 ledConnectionsIndex + 1);
2007d500549bSAnthony Wilson 
2008d500549bSAnthony Wilson             BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler exit";
2009d500549bSAnthony Wilson         };
2010d500549bSAnthony Wilson 
2011d500549bSAnthony Wilson         // Get the State property for the current LED
20121e1e598dSJonathan Doman         sdbusplus::asio::getProperty<std::string>(
20131e1e598dSJonathan Doman             *crow::connections::systemBus, ledConnection, ledPath,
20141e1e598dSJonathan Doman             "xyz.openbmc_project.Led.Physical", "State",
20151e1e598dSJonathan Doman             std::move(respHandler));
2016d500549bSAnthony Wilson     }
2017d500549bSAnthony Wilson 
2018d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
2019d500549bSAnthony Wilson }
2020d500549bSAnthony Wilson 
2021d500549bSAnthony Wilson /**
2022d500549bSAnthony Wilson  * @brief Gets LED data for LEDs associated with given inventory items.
2023d500549bSAnthony Wilson  *
2024d500549bSAnthony Wilson  * Gets the D-Bus connections (services) that provide LED data for the LEDs
2025d500549bSAnthony Wilson  * associated with the specified inventory items.  Then gets the LED data from
2026d500549bSAnthony Wilson  * each connection and stores it in the inventory item.
2027d500549bSAnthony Wilson  *
2028d500549bSAnthony Wilson  * This data is later used to provide sensor property values in the JSON
2029d500549bSAnthony Wilson  * response.
2030d500549bSAnthony Wilson  *
2031d500549bSAnthony Wilson  * Finds the LED data asynchronously.  Invokes callback when information has
2032d500549bSAnthony Wilson  * been obtained.
2033d500549bSAnthony Wilson  *
2034d500549bSAnthony Wilson  * The callback must have the following signature:
2035d500549bSAnthony Wilson  *   @code
203642cbe538SGunnar Mills  *   callback()
2037d500549bSAnthony Wilson  *   @endcode
2038d500549bSAnthony Wilson  *
2039d500549bSAnthony Wilson  * @param sensorsAsyncResp Pointer to object holding response data.
2040d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
2041d500549bSAnthony Wilson  * @param callback Callback to invoke when inventory items have been obtained.
2042d500549bSAnthony Wilson  */
2043d500549bSAnthony Wilson template <typename Callback>
2044d500549bSAnthony Wilson void getInventoryLeds(
2045d500549bSAnthony Wilson     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2046d500549bSAnthony Wilson     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2047d500549bSAnthony Wilson     Callback&& callback)
2048d500549bSAnthony Wilson {
2049d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLeds enter";
2050d500549bSAnthony Wilson 
2051d500549bSAnthony Wilson     const std::string path = "/xyz/openbmc_project";
2052d500549bSAnthony Wilson     const std::array<std::string, 1> interfaces = {
2053d500549bSAnthony Wilson         "xyz.openbmc_project.Led.Physical"};
2054d500549bSAnthony Wilson 
2055d500549bSAnthony Wilson     // Response handler for parsing output from GetSubTree
2056002d39b4SEd Tanous     auto respHandler =
2057002d39b4SEd Tanous         [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
2058002d39b4SEd Tanous          inventoryItems](
2059b9d36b47SEd Tanous             const boost::system::error_code ec,
2060002d39b4SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) {
2061d500549bSAnthony Wilson         BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler enter";
2062d500549bSAnthony Wilson         if (ec)
2063d500549bSAnthony Wilson         {
20648d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
2065d500549bSAnthony Wilson             BMCWEB_LOG_ERROR << "getInventoryLeds respHandler DBus error "
2066d500549bSAnthony Wilson                              << ec;
2067d500549bSAnthony Wilson             return;
2068d500549bSAnthony Wilson         }
2069d500549bSAnthony Wilson 
2070d500549bSAnthony Wilson         // Build map of LED object paths to connections
2071*fe04d49cSNan Zhou         std::shared_ptr<std::map<std::string, std::string>> ledConnections =
2072*fe04d49cSNan Zhou             std::make_shared<std::map<std::string, std::string>>();
2073d500549bSAnthony Wilson 
2074d500549bSAnthony Wilson         // Loop through objects from GetSubTree
2075d500549bSAnthony Wilson         for (const std::pair<
2076d500549bSAnthony Wilson                  std::string,
2077d500549bSAnthony Wilson                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
2078d500549bSAnthony Wilson                  object : subtree)
2079d500549bSAnthony Wilson         {
2080d500549bSAnthony Wilson             // Check if object path is LED for one of the specified inventory
2081d500549bSAnthony Wilson             // items
2082d500549bSAnthony Wilson             const std::string& ledPath = object.first;
2083d500549bSAnthony Wilson             if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr)
2084d500549bSAnthony Wilson             {
2085d500549bSAnthony Wilson                 // Add mapping from ledPath to connection
2086d500549bSAnthony Wilson                 const std::string& connection = object.second.begin()->first;
2087d500549bSAnthony Wilson                 (*ledConnections)[ledPath] = connection;
2088d500549bSAnthony Wilson                 BMCWEB_LOG_DEBUG << "Added mapping " << ledPath << " -> "
2089d500549bSAnthony Wilson                                  << connection;
2090d500549bSAnthony Wilson             }
2091d500549bSAnthony Wilson         }
2092d500549bSAnthony Wilson 
2093d500549bSAnthony Wilson         getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections,
2094d500549bSAnthony Wilson                             std::move(callback));
2095d500549bSAnthony Wilson         BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler exit";
2096d500549bSAnthony Wilson     };
2097d500549bSAnthony Wilson     // Make call to ObjectMapper to find all inventory items
2098d500549bSAnthony Wilson     crow::connections::systemBus->async_method_call(
2099d500549bSAnthony Wilson         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
2100d500549bSAnthony Wilson         "/xyz/openbmc_project/object_mapper",
2101d500549bSAnthony Wilson         "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
2102d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLeds exit";
2103d500549bSAnthony Wilson }
2104d500549bSAnthony Wilson 
2105d500549bSAnthony Wilson /**
210642cbe538SGunnar Mills  * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
210742cbe538SGunnar Mills  *
210842cbe538SGunnar Mills  * Uses the specified connections (services) (currently assumes just one) to
210942cbe538SGunnar Mills  * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
211042cbe538SGunnar Mills  * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
211142cbe538SGunnar Mills  *
211242cbe538SGunnar Mills  * This data is later used to provide sensor property values in the JSON
211342cbe538SGunnar Mills  * response.
211442cbe538SGunnar Mills  *
211542cbe538SGunnar Mills  * Finds the Power Supply Attributes data asynchronously.  Invokes callback
211642cbe538SGunnar Mills  * when data has been obtained.
211742cbe538SGunnar Mills  *
211842cbe538SGunnar Mills  * The callback must have the following signature:
211942cbe538SGunnar Mills  *   @code
212042cbe538SGunnar Mills  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
212142cbe538SGunnar Mills  *   @endcode
212242cbe538SGunnar Mills  *
212342cbe538SGunnar Mills  * @param sensorsAsyncResp Pointer to object holding response data.
212442cbe538SGunnar Mills  * @param inventoryItems D-Bus inventory items associated with sensors.
212542cbe538SGunnar Mills  * @param psAttributesConnections Connections that provide data for the Power
212642cbe538SGunnar Mills  *        Supply Attributes
212742cbe538SGunnar Mills  * @param callback Callback to invoke when data has been obtained.
212842cbe538SGunnar Mills  */
212942cbe538SGunnar Mills template <typename Callback>
213042cbe538SGunnar Mills void getPowerSupplyAttributesData(
2131b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
213242cbe538SGunnar Mills     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2133*fe04d49cSNan Zhou     const std::map<std::string, std::string>& psAttributesConnections,
213442cbe538SGunnar Mills     Callback&& callback)
213542cbe538SGunnar Mills {
213642cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData enter";
213742cbe538SGunnar Mills 
213842cbe538SGunnar Mills     if (psAttributesConnections.empty())
213942cbe538SGunnar Mills     {
214042cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "Can't find PowerSupplyAttributes, no connections!";
214142cbe538SGunnar Mills         callback(inventoryItems);
214242cbe538SGunnar Mills         return;
214342cbe538SGunnar Mills     }
214442cbe538SGunnar Mills 
214542cbe538SGunnar Mills     // Assuming just one connection (service) for now
2146*fe04d49cSNan Zhou     auto it = psAttributesConnections.begin();
214742cbe538SGunnar Mills 
214842cbe538SGunnar Mills     const std::string& psAttributesPath = (*it).first;
214942cbe538SGunnar Mills     const std::string& psAttributesConnection = (*it).second;
215042cbe538SGunnar Mills 
215142cbe538SGunnar Mills     // Response handler for Get DeratingFactor property
2152002d39b4SEd Tanous     auto respHandler =
2153002d39b4SEd Tanous         [sensorsAsyncResp, inventoryItems,
2154f94c4ecfSEd Tanous          callback{std::forward<Callback>(callback)}](
2155002d39b4SEd Tanous             const boost::system::error_code ec, const uint32_t value) {
215642cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler enter";
215742cbe538SGunnar Mills         if (ec)
215842cbe538SGunnar Mills         {
215942cbe538SGunnar Mills             BMCWEB_LOG_ERROR
216042cbe538SGunnar Mills                 << "getPowerSupplyAttributesData respHandler DBus error " << ec;
21618d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
216242cbe538SGunnar Mills             return;
216342cbe538SGunnar Mills         }
216442cbe538SGunnar Mills 
21651e1e598dSJonathan Doman         BMCWEB_LOG_DEBUG << "PS EfficiencyPercent value: " << value;
216642cbe538SGunnar Mills         // Store value in Power Supply Inventory Items
216742cbe538SGunnar Mills         for (InventoryItem& inventoryItem : *inventoryItems)
216842cbe538SGunnar Mills         {
216955f79e6fSEd Tanous             if (inventoryItem.isPowerSupply)
217042cbe538SGunnar Mills             {
217142cbe538SGunnar Mills                 inventoryItem.powerSupplyEfficiencyPercent =
21721e1e598dSJonathan Doman                     static_cast<int>(value);
217342cbe538SGunnar Mills             }
217442cbe538SGunnar Mills         }
217542cbe538SGunnar Mills 
217642cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler exit";
217742cbe538SGunnar Mills         callback(inventoryItems);
217842cbe538SGunnar Mills     };
217942cbe538SGunnar Mills 
218042cbe538SGunnar Mills     // Get the DeratingFactor property for the PowerSupplyAttributes
218142cbe538SGunnar Mills     // Currently only property on the interface/only one we care about
21821e1e598dSJonathan Doman     sdbusplus::asio::getProperty<uint32_t>(
21831e1e598dSJonathan Doman         *crow::connections::systemBus, psAttributesConnection, psAttributesPath,
21841e1e598dSJonathan Doman         "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor",
21851e1e598dSJonathan Doman         std::move(respHandler));
218642cbe538SGunnar Mills 
218742cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData exit";
218842cbe538SGunnar Mills }
218942cbe538SGunnar Mills 
219042cbe538SGunnar Mills /**
219142cbe538SGunnar Mills  * @brief Gets the Power Supply Attributes such as EfficiencyPercent
219242cbe538SGunnar Mills  *
219342cbe538SGunnar Mills  * Gets the D-Bus connection (service) that provides Power Supply Attributes
219442cbe538SGunnar Mills  * data. Then gets the Power Supply Attributes data from the connection
219542cbe538SGunnar Mills  * (currently just assumes 1 connection) and stores the data in the inventory
219642cbe538SGunnar Mills  * item.
219742cbe538SGunnar Mills  *
219842cbe538SGunnar Mills  * This data is later used to provide sensor property values in the JSON
219942cbe538SGunnar Mills  * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
220042cbe538SGunnar Mills  *
220142cbe538SGunnar Mills  * Finds the Power Supply Attributes data asynchronously. Invokes callback
220242cbe538SGunnar Mills  * when information has been obtained.
220342cbe538SGunnar Mills  *
220442cbe538SGunnar Mills  * The callback must have the following signature:
220542cbe538SGunnar Mills  *   @code
220642cbe538SGunnar Mills  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
220742cbe538SGunnar Mills  *   @endcode
220842cbe538SGunnar Mills  *
220942cbe538SGunnar Mills  * @param sensorsAsyncResp Pointer to object holding response data.
221042cbe538SGunnar Mills  * @param inventoryItems D-Bus inventory items associated with sensors.
221142cbe538SGunnar Mills  * @param callback Callback to invoke when data has been obtained.
221242cbe538SGunnar Mills  */
221342cbe538SGunnar Mills template <typename Callback>
221442cbe538SGunnar Mills void getPowerSupplyAttributes(
221542cbe538SGunnar Mills     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
221642cbe538SGunnar Mills     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
221742cbe538SGunnar Mills     Callback&& callback)
221842cbe538SGunnar Mills {
221942cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes enter";
222042cbe538SGunnar Mills 
222142cbe538SGunnar Mills     // Only need the power supply attributes when the Power Schema
2222a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode != sensors::node::power)
222342cbe538SGunnar Mills     {
222442cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit since not Power";
222542cbe538SGunnar Mills         callback(inventoryItems);
222642cbe538SGunnar Mills         return;
222742cbe538SGunnar Mills     }
222842cbe538SGunnar Mills 
222942cbe538SGunnar Mills     const std::array<std::string, 1> interfaces = {
223042cbe538SGunnar Mills         "xyz.openbmc_project.Control.PowerSupplyAttributes"};
223142cbe538SGunnar Mills 
223242cbe538SGunnar Mills     // Response handler for parsing output from GetSubTree
2233b9d36b47SEd Tanous     auto respHandler =
2234b9d36b47SEd Tanous         [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
2235b9d36b47SEd Tanous          inventoryItems](
2236b9d36b47SEd Tanous             const boost::system::error_code ec,
2237b9d36b47SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) {
223842cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler enter";
223942cbe538SGunnar Mills         if (ec)
224042cbe538SGunnar Mills         {
22418d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
224242cbe538SGunnar Mills             BMCWEB_LOG_ERROR
224342cbe538SGunnar Mills                 << "getPowerSupplyAttributes respHandler DBus error " << ec;
224442cbe538SGunnar Mills             return;
224542cbe538SGunnar Mills         }
224626f6976fSEd Tanous         if (subtree.empty())
224742cbe538SGunnar Mills         {
224842cbe538SGunnar Mills             BMCWEB_LOG_DEBUG << "Can't find Power Supply Attributes!";
224942cbe538SGunnar Mills             callback(inventoryItems);
225042cbe538SGunnar Mills             return;
225142cbe538SGunnar Mills         }
225242cbe538SGunnar Mills 
225342cbe538SGunnar Mills         // Currently we only support 1 power supply attribute, use this for
225442cbe538SGunnar Mills         // all the power supplies. Build map of object path to connection.
225542cbe538SGunnar Mills         // Assume just 1 connection and 1 path for now.
2256*fe04d49cSNan Zhou         std::map<std::string, std::string> psAttributesConnections;
225742cbe538SGunnar Mills 
225842cbe538SGunnar Mills         if (subtree[0].first.empty() || subtree[0].second.empty())
225942cbe538SGunnar Mills         {
226042cbe538SGunnar Mills             BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
226142cbe538SGunnar Mills             callback(inventoryItems);
226242cbe538SGunnar Mills             return;
226342cbe538SGunnar Mills         }
226442cbe538SGunnar Mills 
226542cbe538SGunnar Mills         const std::string& psAttributesPath = subtree[0].first;
226642cbe538SGunnar Mills         const std::string& connection = subtree[0].second.begin()->first;
226742cbe538SGunnar Mills 
226842cbe538SGunnar Mills         if (connection.empty())
226942cbe538SGunnar Mills         {
227042cbe538SGunnar Mills             BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
227142cbe538SGunnar Mills             callback(inventoryItems);
227242cbe538SGunnar Mills             return;
227342cbe538SGunnar Mills         }
227442cbe538SGunnar Mills 
227542cbe538SGunnar Mills         psAttributesConnections[psAttributesPath] = connection;
227642cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "Added mapping " << psAttributesPath << " -> "
227742cbe538SGunnar Mills                          << connection;
227842cbe538SGunnar Mills 
227942cbe538SGunnar Mills         getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
228042cbe538SGunnar Mills                                      psAttributesConnections,
228142cbe538SGunnar Mills                                      std::move(callback));
228242cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler exit";
228342cbe538SGunnar Mills     };
228442cbe538SGunnar Mills     // Make call to ObjectMapper to find the PowerSupplyAttributes service
228542cbe538SGunnar Mills     crow::connections::systemBus->async_method_call(
228642cbe538SGunnar Mills         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
228742cbe538SGunnar Mills         "/xyz/openbmc_project/object_mapper",
228842cbe538SGunnar Mills         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
228942cbe538SGunnar Mills         "/xyz/openbmc_project", 0, interfaces);
229042cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit";
229142cbe538SGunnar Mills }
229242cbe538SGunnar Mills 
229342cbe538SGunnar Mills /**
2294adc4f0dbSShawn McCarney  * @brief Gets inventory items associated with sensors.
22958fb49dd6SShawn McCarney  *
22968fb49dd6SShawn McCarney  * Finds the inventory items that are associated with the specified sensors.
2297adc4f0dbSShawn McCarney  * Then gets D-Bus data for the inventory items, such as presence and VPD.
22988fb49dd6SShawn McCarney  *
2299adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
2300adc4f0dbSShawn McCarney  * response.
23018fb49dd6SShawn McCarney  *
2302adc4f0dbSShawn McCarney  * Finds the inventory items asynchronously.  Invokes callback when the
2303adc4f0dbSShawn McCarney  * inventory items have been obtained.
2304adc4f0dbSShawn McCarney  *
2305adc4f0dbSShawn McCarney  * The callback must have the following signature:
2306adc4f0dbSShawn McCarney  *   @code
2307adc4f0dbSShawn McCarney  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2308adc4f0dbSShawn McCarney  *   @endcode
23098fb49dd6SShawn McCarney  *
23108fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
23118fb49dd6SShawn McCarney  * @param sensorNames All sensors within the current chassis.
23128fb49dd6SShawn McCarney  * @param objectMgrPaths Mappings from connection name to DBus object path that
23138fb49dd6SShawn McCarney  * implements ObjectManager.
2314adc4f0dbSShawn McCarney  * @param callback Callback to invoke when inventory items have been obtained.
23158fb49dd6SShawn McCarney  */
2316adc4f0dbSShawn McCarney template <typename Callback>
2317adc4f0dbSShawn McCarney static void getInventoryItems(
23188fb49dd6SShawn McCarney     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2319*fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>> sensorNames,
2320*fe04d49cSNan Zhou     std::shared_ptr<std::map<std::string, std::string>> objectMgrPaths,
2321adc4f0dbSShawn McCarney     Callback&& callback)
23228fb49dd6SShawn McCarney {
2323adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItems enter";
2324adc4f0dbSShawn McCarney     auto getInventoryItemAssociationsCb =
2325f94c4ecfSEd Tanous         [sensorsAsyncResp, objectMgrPaths,
2326f94c4ecfSEd Tanous          callback{std::forward<Callback>(callback)}](
2327adc4f0dbSShawn McCarney             std::shared_ptr<std::vector<InventoryItem>> inventoryItems) {
2328adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb enter";
23298fb49dd6SShawn McCarney         auto getInventoryItemsConnectionsCb =
2330adc4f0dbSShawn McCarney             [sensorsAsyncResp, inventoryItems, objectMgrPaths,
2331f94c4ecfSEd Tanous              callback{std::forward<const Callback>(callback)}](
2332*fe04d49cSNan Zhou                 std::shared_ptr<std::set<std::string>> invConnections) {
23338fb49dd6SShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb enter";
2334002d39b4SEd Tanous             auto getInventoryItemsDataCb = [sensorsAsyncResp, inventoryItems,
2335d500549bSAnthony Wilson                                             callback{std::move(callback)}]() {
2336d500549bSAnthony Wilson                 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb enter";
233742cbe538SGunnar Mills 
2338002d39b4SEd Tanous                 auto getInventoryLedsCb = [sensorsAsyncResp, inventoryItems,
2339002d39b4SEd Tanous                                            callback{std::move(callback)}]() {
234042cbe538SGunnar Mills                     BMCWEB_LOG_DEBUG << "getInventoryLedsCb enter";
234142cbe538SGunnar Mills                     // Find Power Supply Attributes and get the data
2342002d39b4SEd Tanous                     getPowerSupplyAttributes(sensorsAsyncResp, inventoryItems,
234342cbe538SGunnar Mills                                              std::move(callback));
234442cbe538SGunnar Mills                     BMCWEB_LOG_DEBUG << "getInventoryLedsCb exit";
234542cbe538SGunnar Mills                 };
234642cbe538SGunnar Mills 
2347d500549bSAnthony Wilson                 // Find led connections and get the data
2348d500549bSAnthony Wilson                 getInventoryLeds(sensorsAsyncResp, inventoryItems,
234942cbe538SGunnar Mills                                  std::move(getInventoryLedsCb));
2350d500549bSAnthony Wilson                 BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb exit";
2351d500549bSAnthony Wilson             };
23528fb49dd6SShawn McCarney 
2353adc4f0dbSShawn McCarney             // Get inventory item data from connections
2354adc4f0dbSShawn McCarney             getInventoryItemsData(sensorsAsyncResp, inventoryItems,
2355adc4f0dbSShawn McCarney                                   invConnections, objectMgrPaths,
2356d500549bSAnthony Wilson                                   std::move(getInventoryItemsDataCb));
23578fb49dd6SShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb exit";
23588fb49dd6SShawn McCarney         };
23598fb49dd6SShawn McCarney 
2360adc4f0dbSShawn McCarney         // Get connections that provide inventory item data
2361002d39b4SEd Tanous         getInventoryItemsConnections(sensorsAsyncResp, inventoryItems,
23628fb49dd6SShawn McCarney                                      std::move(getInventoryItemsConnectionsCb));
2363adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb exit";
23648fb49dd6SShawn McCarney     };
23658fb49dd6SShawn McCarney 
2366adc4f0dbSShawn McCarney     // Get associations from sensors to inventory items
2367adc4f0dbSShawn McCarney     getInventoryItemAssociations(sensorsAsyncResp, sensorNames, objectMgrPaths,
2368adc4f0dbSShawn McCarney                                  std::move(getInventoryItemAssociationsCb));
2369adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItems exit";
2370adc4f0dbSShawn McCarney }
2371adc4f0dbSShawn McCarney 
2372adc4f0dbSShawn McCarney /**
2373adc4f0dbSShawn McCarney  * @brief Returns JSON PowerSupply object for the specified inventory item.
2374adc4f0dbSShawn McCarney  *
2375adc4f0dbSShawn McCarney  * Searches for a JSON PowerSupply object that matches the specified inventory
2376adc4f0dbSShawn McCarney  * item.  If one is not found, a new PowerSupply object is added to the JSON
2377adc4f0dbSShawn McCarney  * array.
2378adc4f0dbSShawn McCarney  *
2379adc4f0dbSShawn McCarney  * Multiple sensors are often associated with one power supply inventory item.
2380adc4f0dbSShawn McCarney  * As a result, multiple sensor values are stored in one JSON PowerSupply
2381adc4f0dbSShawn McCarney  * object.
2382adc4f0dbSShawn McCarney  *
2383adc4f0dbSShawn McCarney  * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
2384adc4f0dbSShawn McCarney  * @param inventoryItem Inventory item for the power supply.
2385adc4f0dbSShawn McCarney  * @param chassisId Chassis that contains the power supply.
2386adc4f0dbSShawn McCarney  * @return JSON PowerSupply object for the specified inventory item.
2387adc4f0dbSShawn McCarney  */
238823a21a1cSEd Tanous inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
2389adc4f0dbSShawn McCarney                                       const InventoryItem& inventoryItem,
2390adc4f0dbSShawn McCarney                                       const std::string& chassisId)
2391adc4f0dbSShawn McCarney {
2392adc4f0dbSShawn McCarney     // Check if matching PowerSupply object already exists in JSON array
2393adc4f0dbSShawn McCarney     for (nlohmann::json& powerSupply : powerSupplyArray)
2394adc4f0dbSShawn McCarney     {
2395adc4f0dbSShawn McCarney         if (powerSupply["MemberId"] == inventoryItem.name)
2396adc4f0dbSShawn McCarney         {
2397adc4f0dbSShawn McCarney             return powerSupply;
2398adc4f0dbSShawn McCarney         }
2399adc4f0dbSShawn McCarney     }
2400adc4f0dbSShawn McCarney 
2401adc4f0dbSShawn McCarney     // Add new PowerSupply object to JSON array
2402adc4f0dbSShawn McCarney     powerSupplyArray.push_back({});
2403adc4f0dbSShawn McCarney     nlohmann::json& powerSupply = powerSupplyArray.back();
2404adc4f0dbSShawn McCarney     powerSupply["@odata.id"] =
2405adc4f0dbSShawn McCarney         "/redfish/v1/Chassis/" + chassisId + "/Power#/PowerSupplies/";
2406adc4f0dbSShawn McCarney     powerSupply["MemberId"] = inventoryItem.name;
2407adc4f0dbSShawn McCarney     powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " ");
2408adc4f0dbSShawn McCarney     powerSupply["Manufacturer"] = inventoryItem.manufacturer;
2409adc4f0dbSShawn McCarney     powerSupply["Model"] = inventoryItem.model;
2410adc4f0dbSShawn McCarney     powerSupply["PartNumber"] = inventoryItem.partNumber;
2411adc4f0dbSShawn McCarney     powerSupply["SerialNumber"] = inventoryItem.serialNumber;
2412d500549bSAnthony Wilson     setLedState(powerSupply, &inventoryItem);
2413adc4f0dbSShawn McCarney 
241442cbe538SGunnar Mills     if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
241542cbe538SGunnar Mills     {
241642cbe538SGunnar Mills         powerSupply["EfficiencyPercent"] =
241742cbe538SGunnar Mills             inventoryItem.powerSupplyEfficiencyPercent;
241842cbe538SGunnar Mills     }
241942cbe538SGunnar Mills 
242042cbe538SGunnar Mills     powerSupply["Status"]["State"] = getState(&inventoryItem);
2421adc4f0dbSShawn McCarney     const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
2422adc4f0dbSShawn McCarney     powerSupply["Status"]["Health"] = health;
2423adc4f0dbSShawn McCarney 
2424adc4f0dbSShawn McCarney     return powerSupply;
24258fb49dd6SShawn McCarney }
24268fb49dd6SShawn McCarney 
24278fb49dd6SShawn McCarney /**
2428de629b6eSShawn McCarney  * @brief Gets the values of the specified sensors.
2429de629b6eSShawn McCarney  *
2430de629b6eSShawn McCarney  * Stores the results as JSON in the SensorsAsyncResp.
2431de629b6eSShawn McCarney  *
2432de629b6eSShawn McCarney  * Gets the sensor values asynchronously.  Stores the results later when the
2433de629b6eSShawn McCarney  * information has been obtained.
2434de629b6eSShawn McCarney  *
2435adc4f0dbSShawn McCarney  * The sensorNames set contains all requested sensors for the current chassis.
2436de629b6eSShawn McCarney  *
2437de629b6eSShawn McCarney  * To minimize the number of DBus calls, the DBus method
2438de629b6eSShawn McCarney  * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
2439de629b6eSShawn McCarney  * values of all sensors provided by a connection (service).
2440de629b6eSShawn McCarney  *
2441de629b6eSShawn McCarney  * The connections set contains all the connections that provide sensor values.
2442de629b6eSShawn McCarney  *
2443de629b6eSShawn McCarney  * The objectMgrPaths map contains mappings from a connection name to the
2444de629b6eSShawn McCarney  * corresponding DBus object path that implements ObjectManager.
2445de629b6eSShawn McCarney  *
2446adc4f0dbSShawn McCarney  * The InventoryItem vector contains D-Bus inventory items associated with the
2447adc4f0dbSShawn McCarney  * sensors.  Inventory item data is needed for some Redfish sensor properties.
2448adc4f0dbSShawn McCarney  *
2449de629b6eSShawn McCarney  * @param SensorsAsyncResp Pointer to object holding response data.
2450adc4f0dbSShawn McCarney  * @param sensorNames All requested sensors within the current chassis.
2451de629b6eSShawn McCarney  * @param connections Connections that provide sensor values.
2452de629b6eSShawn McCarney  * @param objectMgrPaths Mappings from connection name to DBus object path that
2453de629b6eSShawn McCarney  * implements ObjectManager.
2454adc4f0dbSShawn McCarney  * @param inventoryItems Inventory items associated with the sensors.
2455de629b6eSShawn McCarney  */
245623a21a1cSEd Tanous inline void getSensorData(
245781ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2458*fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& sensorNames,
2459*fe04d49cSNan Zhou     const std::set<std::string>& connections,
2460*fe04d49cSNan Zhou     const std::shared_ptr<std::map<std::string, std::string>>& objectMgrPaths,
2461b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems)
2462de629b6eSShawn McCarney {
2463de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getSensorData enter";
2464de629b6eSShawn McCarney     // Get managed objects from all services exposing sensors
2465de629b6eSShawn McCarney     for (const std::string& connection : connections)
2466de629b6eSShawn McCarney     {
2467de629b6eSShawn McCarney         // Response handler to process managed objects
2468002d39b4SEd Tanous         auto getManagedObjectsCb =
2469002d39b4SEd Tanous             [sensorsAsyncResp, sensorNames,
2470002d39b4SEd Tanous              inventoryItems](const boost::system::error_code ec,
2471711ac7a9SEd Tanous                              dbus::utility::ManagedObjectType& resp) {
2472de629b6eSShawn McCarney             BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter";
2473de629b6eSShawn McCarney             if (ec)
2474de629b6eSShawn McCarney             {
2475de629b6eSShawn McCarney                 BMCWEB_LOG_ERROR << "getManagedObjectsCb DBUS error: " << ec;
24768d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
2477de629b6eSShawn McCarney                 return;
2478de629b6eSShawn McCarney             }
2479de629b6eSShawn McCarney             // Go through all objects and update response with sensor data
2480de629b6eSShawn McCarney             for (const auto& objDictEntry : resp)
2481de629b6eSShawn McCarney             {
2482de629b6eSShawn McCarney                 const std::string& objPath =
2483de629b6eSShawn McCarney                     static_cast<const std::string&>(objDictEntry.first);
2484de629b6eSShawn McCarney                 BMCWEB_LOG_DEBUG << "getManagedObjectsCb parsing object "
2485de629b6eSShawn McCarney                                  << objPath;
2486de629b6eSShawn McCarney 
2487de629b6eSShawn McCarney                 std::vector<std::string> split;
2488de629b6eSShawn McCarney                 // Reserve space for
2489de629b6eSShawn McCarney                 // /xyz/openbmc_project/sensors/<name>/<subname>
2490de629b6eSShawn McCarney                 split.reserve(6);
2491de629b6eSShawn McCarney                 boost::algorithm::split(split, objPath, boost::is_any_of("/"));
2492de629b6eSShawn McCarney                 if (split.size() < 6)
2493de629b6eSShawn McCarney                 {
2494de629b6eSShawn McCarney                     BMCWEB_LOG_ERROR << "Got path that isn't long enough "
2495de629b6eSShawn McCarney                                      << objPath;
2496de629b6eSShawn McCarney                     continue;
2497de629b6eSShawn McCarney                 }
2498de629b6eSShawn McCarney                 // These indexes aren't intuitive, as boost::split puts an empty
2499de629b6eSShawn McCarney                 // string at the beginning
2500de629b6eSShawn McCarney                 const std::string& sensorType = split[4];
2501de629b6eSShawn McCarney                 const std::string& sensorName = split[5];
2502de629b6eSShawn McCarney                 BMCWEB_LOG_DEBUG << "sensorName " << sensorName
2503de629b6eSShawn McCarney                                  << " sensorType " << sensorType;
250449c53ac9SJohnathan Mantey                 if (sensorNames->find(objPath) == sensorNames->end())
2505de629b6eSShawn McCarney                 {
2506accdbb2cSAndrew Geissler                     BMCWEB_LOG_DEBUG << sensorName << " not in sensor list ";
2507de629b6eSShawn McCarney                     continue;
2508de629b6eSShawn McCarney                 }
2509de629b6eSShawn McCarney 
2510adc4f0dbSShawn McCarney                 // Find inventory item (if any) associated with sensor
2511adc4f0dbSShawn McCarney                 InventoryItem* inventoryItem =
2512adc4f0dbSShawn McCarney                     findInventoryItemForSensor(inventoryItems, objPath);
2513adc4f0dbSShawn McCarney 
251495a3ecadSAnthony Wilson                 const std::string& sensorSchema =
251581ce609eSEd Tanous                     sensorsAsyncResp->chassisSubNode;
251695a3ecadSAnthony Wilson 
251795a3ecadSAnthony Wilson                 nlohmann::json* sensorJson = nullptr;
251895a3ecadSAnthony Wilson 
2519928fefb9SNan Zhou                 if (sensorSchema == sensors::node::sensors &&
2520928fefb9SNan Zhou                     !sensorsAsyncResp->efficientExpand)
252195a3ecadSAnthony Wilson                 {
25228d1b46d7Szhanghch05                     sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
252381ce609eSEd Tanous                         "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId +
252481ce609eSEd Tanous                         "/" + sensorsAsyncResp->chassisSubNode + "/" +
252595a3ecadSAnthony Wilson                         sensorName;
25268d1b46d7Szhanghch05                     sensorJson = &(sensorsAsyncResp->asyncResp->res.jsonValue);
252795a3ecadSAnthony Wilson                 }
252895a3ecadSAnthony Wilson                 else
252995a3ecadSAnthony Wilson                 {
2530271584abSEd Tanous                     std::string fieldName;
2531928fefb9SNan Zhou                     if (sensorsAsyncResp->efficientExpand)
2532928fefb9SNan Zhou                     {
2533928fefb9SNan Zhou                         fieldName = "Members";
2534928fefb9SNan Zhou                     }
2535928fefb9SNan Zhou                     else if (sensorType == "temperature")
2536de629b6eSShawn McCarney                     {
2537de629b6eSShawn McCarney                         fieldName = "Temperatures";
2538de629b6eSShawn McCarney                     }
2539de629b6eSShawn McCarney                     else if (sensorType == "fan" || sensorType == "fan_tach" ||
2540de629b6eSShawn McCarney                              sensorType == "fan_pwm")
2541de629b6eSShawn McCarney                     {
2542de629b6eSShawn McCarney                         fieldName = "Fans";
2543de629b6eSShawn McCarney                     }
2544de629b6eSShawn McCarney                     else if (sensorType == "voltage")
2545de629b6eSShawn McCarney                     {
2546de629b6eSShawn McCarney                         fieldName = "Voltages";
2547de629b6eSShawn McCarney                     }
2548de629b6eSShawn McCarney                     else if (sensorType == "power")
2549de629b6eSShawn McCarney                     {
255055f79e6fSEd Tanous                         if (sensorName == "total_power")
2551028f7ebcSEddie James                         {
2552028f7ebcSEddie James                             fieldName = "PowerControl";
2553028f7ebcSEddie James                         }
2554adc4f0dbSShawn McCarney                         else if ((inventoryItem != nullptr) &&
2555adc4f0dbSShawn McCarney                                  (inventoryItem->isPowerSupply))
2556028f7ebcSEddie James                         {
2557de629b6eSShawn McCarney                             fieldName = "PowerSupplies";
2558de629b6eSShawn McCarney                         }
2559adc4f0dbSShawn McCarney                         else
2560adc4f0dbSShawn McCarney                         {
2561adc4f0dbSShawn McCarney                             // Other power sensors are in SensorCollection
2562adc4f0dbSShawn McCarney                             continue;
2563adc4f0dbSShawn McCarney                         }
2564028f7ebcSEddie James                     }
2565de629b6eSShawn McCarney                     else
2566de629b6eSShawn McCarney                     {
2567de629b6eSShawn McCarney                         BMCWEB_LOG_ERROR << "Unsure how to handle sensorType "
2568de629b6eSShawn McCarney                                          << sensorType;
2569de629b6eSShawn McCarney                         continue;
2570de629b6eSShawn McCarney                     }
2571de629b6eSShawn McCarney 
2572de629b6eSShawn McCarney                     nlohmann::json& tempArray =
25738d1b46d7Szhanghch05                         sensorsAsyncResp->asyncResp->res.jsonValue[fieldName];
2574adc4f0dbSShawn McCarney                     if (fieldName == "PowerControl")
257549c53ac9SJohnathan Mantey                     {
2576adc4f0dbSShawn McCarney                         if (tempArray.empty())
25777ab06f49SGunnar Mills                         {
257895a3ecadSAnthony Wilson                             // Put multiple "sensors" into a single
257995a3ecadSAnthony Wilson                             // PowerControl. Follows MemberId naming and
258095a3ecadSAnthony Wilson                             // naming in power.hpp.
25811476687dSEd Tanous                             nlohmann::json::object_t power;
25821476687dSEd Tanous                             power["@odata.id"] =
2583adc4f0dbSShawn McCarney                                 "/redfish/v1/Chassis/" +
258481ce609eSEd Tanous                                 sensorsAsyncResp->chassisId + "/" +
258581ce609eSEd Tanous                                 sensorsAsyncResp->chassisSubNode + "#/" +
25861476687dSEd Tanous                                 fieldName + "/0";
25871476687dSEd Tanous                             tempArray.push_back(std::move(power));
2588adc4f0dbSShawn McCarney                         }
2589adc4f0dbSShawn McCarney                         sensorJson = &(tempArray.back());
2590adc4f0dbSShawn McCarney                     }
2591adc4f0dbSShawn McCarney                     else if (fieldName == "PowerSupplies")
2592adc4f0dbSShawn McCarney                     {
2593adc4f0dbSShawn McCarney                         if (inventoryItem != nullptr)
2594adc4f0dbSShawn McCarney                         {
2595adc4f0dbSShawn McCarney                             sensorJson =
2596adc4f0dbSShawn McCarney                                 &(getPowerSupply(tempArray, *inventoryItem,
259781ce609eSEd Tanous                                                  sensorsAsyncResp->chassisId));
2598adc4f0dbSShawn McCarney                         }
259949c53ac9SJohnathan Mantey                     }
2600928fefb9SNan Zhou                     else if (fieldName == "Members")
2601928fefb9SNan Zhou                     {
26021476687dSEd Tanous                         nlohmann::json::object_t member;
26031476687dSEd Tanous                         member["@odata.id"] =
2604928fefb9SNan Zhou                             "/redfish/v1/Chassis/" +
2605928fefb9SNan Zhou                             sensorsAsyncResp->chassisId + "/" +
26061476687dSEd Tanous                             sensorsAsyncResp->chassisSubNode + "/" + sensorName;
26071476687dSEd Tanous                         tempArray.push_back(std::move(member));
2608928fefb9SNan Zhou                         sensorJson = &(tempArray.back());
2609928fefb9SNan Zhou                     }
261049c53ac9SJohnathan Mantey                     else
261149c53ac9SJohnathan Mantey                     {
26121476687dSEd Tanous                         nlohmann::json::object_t member;
26131476687dSEd Tanous                         member["@odata.id"] = "/redfish/v1/Chassis/" +
26141476687dSEd Tanous                                               sensorsAsyncResp->chassisId +
26151476687dSEd Tanous                                               "/" +
26161476687dSEd Tanous                                               sensorsAsyncResp->chassisSubNode +
26171476687dSEd Tanous                                               "#/" + fieldName + "/";
26181476687dSEd Tanous                         tempArray.push_back(std::move(member));
2619adc4f0dbSShawn McCarney                         sensorJson = &(tempArray.back());
262049c53ac9SJohnathan Mantey                     }
262195a3ecadSAnthony Wilson                 }
2622de629b6eSShawn McCarney 
2623adc4f0dbSShawn McCarney                 if (sensorJson != nullptr)
2624adc4f0dbSShawn McCarney                 {
2625a0ec28b6SAdrian Ambrożewicz                     objectInterfacesToJson(
262681ce609eSEd Tanous                         sensorName, sensorType, sensorsAsyncResp,
2627a0ec28b6SAdrian Ambrożewicz                         objDictEntry.second, *sensorJson, inventoryItem);
2628adc4f0dbSShawn McCarney                 }
2629de629b6eSShawn McCarney             }
263081ce609eSEd Tanous             if (sensorsAsyncResp.use_count() == 1)
263149c53ac9SJohnathan Mantey             {
263281ce609eSEd Tanous                 sortJSONResponse(sensorsAsyncResp);
2633928fefb9SNan Zhou                 if (sensorsAsyncResp->chassisSubNode ==
2634928fefb9SNan Zhou                         sensors::node::sensors &&
2635928fefb9SNan Zhou                     sensorsAsyncResp->efficientExpand)
2636928fefb9SNan Zhou                 {
2637928fefb9SNan Zhou                     sensorsAsyncResp->asyncResp->res
2638928fefb9SNan Zhou                         .jsonValue["Members@odata.count"] =
2639928fefb9SNan Zhou                         sensorsAsyncResp->asyncResp->res.jsonValue["Members"]
2640928fefb9SNan Zhou                             .size();
2641928fefb9SNan Zhou                 }
2642928fefb9SNan Zhou                 else if (sensorsAsyncResp->chassisSubNode ==
2643928fefb9SNan Zhou                          sensors::node::thermal)
26448bd25ccdSJames Feist                 {
264581ce609eSEd Tanous                     populateFanRedundancy(sensorsAsyncResp);
26468bd25ccdSJames Feist                 }
264749c53ac9SJohnathan Mantey             }
2648de629b6eSShawn McCarney             BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit";
2649de629b6eSShawn McCarney         };
2650de629b6eSShawn McCarney 
2651de629b6eSShawn McCarney         // Find DBus object path that implements ObjectManager for the current
2652de629b6eSShawn McCarney         // connection.  If no mapping found, default to "/".
26538fb49dd6SShawn McCarney         auto iter = objectMgrPaths->find(connection);
2654de629b6eSShawn McCarney         const std::string& objectMgrPath =
26558fb49dd6SShawn McCarney             (iter != objectMgrPaths->end()) ? iter->second : "/";
2656de629b6eSShawn McCarney         BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
2657de629b6eSShawn McCarney                          << objectMgrPath;
2658de629b6eSShawn McCarney 
2659de629b6eSShawn McCarney         crow::connections::systemBus->async_method_call(
2660de629b6eSShawn McCarney             getManagedObjectsCb, connection, objectMgrPath,
2661de629b6eSShawn McCarney             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
266223a21a1cSEd Tanous     }
2663de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getSensorData exit";
2664de629b6eSShawn McCarney }
2665de629b6eSShawn McCarney 
2666*fe04d49cSNan Zhou inline void
2667*fe04d49cSNan Zhou     processSensorList(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2668*fe04d49cSNan Zhou                       const std::shared_ptr<std::set<std::string>>& sensorNames)
26691abe55efSEd Tanous {
2670*fe04d49cSNan Zhou     auto getConnectionCb = [sensorsAsyncResp, sensorNames](
2671*fe04d49cSNan Zhou                                const std::set<std::string>& connections) {
267255c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "getConnectionCb enter";
2673de629b6eSShawn McCarney         auto getObjectManagerPathsCb =
2674*fe04d49cSNan Zhou             [sensorsAsyncResp, sensorNames, connections](
2675*fe04d49cSNan Zhou                 const std::shared_ptr<std::map<std::string, std::string>>&
2676*fe04d49cSNan Zhou                     objectMgrPaths) {
2677de629b6eSShawn McCarney             BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb enter";
2678adc4f0dbSShawn McCarney             auto getInventoryItemsCb =
2679002d39b4SEd Tanous                 [sensorsAsyncResp, sensorNames, connections, objectMgrPaths](
2680f23b7296SEd Tanous                     const std::shared_ptr<std::vector<InventoryItem>>&
2681adc4f0dbSShawn McCarney                         inventoryItems) {
2682adc4f0dbSShawn McCarney                 BMCWEB_LOG_DEBUG << "getInventoryItemsCb enter";
268349c53ac9SJohnathan Mantey                 // Get sensor data and store results in JSON
2684002d39b4SEd Tanous                 getSensorData(sensorsAsyncResp, sensorNames, connections,
2685002d39b4SEd Tanous                               objectMgrPaths, inventoryItems);
2686adc4f0dbSShawn McCarney                 BMCWEB_LOG_DEBUG << "getInventoryItemsCb exit";
2687adc4f0dbSShawn McCarney             };
2688adc4f0dbSShawn McCarney 
2689adc4f0dbSShawn McCarney             // Get inventory items associated with sensors
2690002d39b4SEd Tanous             getInventoryItems(sensorsAsyncResp, sensorNames, objectMgrPaths,
2691adc4f0dbSShawn McCarney                               std::move(getInventoryItemsCb));
2692adc4f0dbSShawn McCarney 
2693de629b6eSShawn McCarney             BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb exit";
269408777fb0SLewanczyk, Dawid         };
2695de629b6eSShawn McCarney 
269649c53ac9SJohnathan Mantey         // Get mapping from connection names to the DBus object
269749c53ac9SJohnathan Mantey         // paths that implement the ObjectManager interface
269881ce609eSEd Tanous         getObjectManagerPaths(sensorsAsyncResp,
2699de629b6eSShawn McCarney                               std::move(getObjectManagerPathsCb));
270055c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "getConnectionCb exit";
270108777fb0SLewanczyk, Dawid     };
2702de629b6eSShawn McCarney 
2703de629b6eSShawn McCarney     // Get set of connections that provide sensor values
270481ce609eSEd Tanous     getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb));
270595a3ecadSAnthony Wilson }
270695a3ecadSAnthony Wilson 
270795a3ecadSAnthony Wilson /**
270895a3ecadSAnthony Wilson  * @brief Entry point for retrieving sensors data related to requested
270995a3ecadSAnthony Wilson  *        chassis.
271095a3ecadSAnthony Wilson  * @param SensorsAsyncResp   Pointer to object holding response data
271195a3ecadSAnthony Wilson  */
2712b5a76932SEd Tanous inline void
271381ce609eSEd Tanous     getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
271495a3ecadSAnthony Wilson {
271595a3ecadSAnthony Wilson     BMCWEB_LOG_DEBUG << "getChassisData enter";
271695a3ecadSAnthony Wilson     auto getChassisCb =
271781ce609eSEd Tanous         [sensorsAsyncResp](
2718*fe04d49cSNan Zhou             const std::shared_ptr<std::set<std::string>>& sensorNames) {
271995a3ecadSAnthony Wilson         BMCWEB_LOG_DEBUG << "getChassisCb enter";
272081ce609eSEd Tanous         processSensorList(sensorsAsyncResp, sensorNames);
272155c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "getChassisCb exit";
272208777fb0SLewanczyk, Dawid     };
2723928fefb9SNan Zhou     // SensorCollection doesn't contain the Redundancy property
2724928fefb9SNan Zhou     if (sensorsAsyncResp->chassisSubNode != sensors::node::sensors)
2725928fefb9SNan Zhou     {
27268d1b46d7Szhanghch05         sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] =
27278d1b46d7Szhanghch05             nlohmann::json::array();
2728928fefb9SNan Zhou     }
272926f03899SShawn McCarney     // Get set of sensors in chassis
273081ce609eSEd Tanous     getChassis(sensorsAsyncResp, std::move(getChassisCb));
273155c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "getChassisData exit";
2732271584abSEd Tanous }
273308777fb0SLewanczyk, Dawid 
2734413961deSRichard Marian Thomaiyar /**
273549c53ac9SJohnathan Mantey  * @brief Find the requested sensorName in the list of all sensors supplied by
273649c53ac9SJohnathan Mantey  * the chassis node
273749c53ac9SJohnathan Mantey  *
273849c53ac9SJohnathan Mantey  * @param sensorName   The sensor name supplied in the PATCH request
273949c53ac9SJohnathan Mantey  * @param sensorsList  The list of sensors managed by the chassis node
274049c53ac9SJohnathan Mantey  * @param sensorsModified  The list of sensors that were found as a result of
274149c53ac9SJohnathan Mantey  *                         repeated calls to this function
274249c53ac9SJohnathan Mantey  */
2743*fe04d49cSNan Zhou inline bool
2744*fe04d49cSNan Zhou     findSensorNameUsingSensorPath(std::string_view sensorName,
2745*fe04d49cSNan Zhou                                   std::set<std::string>& sensorsList,
2746*fe04d49cSNan Zhou                                   std::set<std::string>& sensorsModified)
274749c53ac9SJohnathan Mantey {
2748*fe04d49cSNan Zhou     for (const auto& chassisSensor : sensorsList)
274949c53ac9SJohnathan Mantey     {
275028aa8de5SGeorge Liu         sdbusplus::message::object_path path(chassisSensor);
2751b00dcc27SEd Tanous         std::string thisSensorName = path.filename();
275228aa8de5SGeorge Liu         if (thisSensorName.empty())
275349c53ac9SJohnathan Mantey         {
275449c53ac9SJohnathan Mantey             continue;
275549c53ac9SJohnathan Mantey         }
275649c53ac9SJohnathan Mantey         if (thisSensorName == sensorName)
275749c53ac9SJohnathan Mantey         {
275849c53ac9SJohnathan Mantey             sensorsModified.emplace(chassisSensor);
275949c53ac9SJohnathan Mantey             return true;
276049c53ac9SJohnathan Mantey         }
276149c53ac9SJohnathan Mantey     }
276249c53ac9SJohnathan Mantey     return false;
276349c53ac9SJohnathan Mantey }
276449c53ac9SJohnathan Mantey 
276549c53ac9SJohnathan Mantey /**
2766413961deSRichard Marian Thomaiyar  * @brief Entry point for overriding sensor values of given sensor
2767413961deSRichard Marian Thomaiyar  *
27688d1b46d7Szhanghch05  * @param sensorAsyncResp   response object
27694bb3dc34SCarol Wang  * @param allCollections   Collections extract from sensors' request patch info
2770413961deSRichard Marian Thomaiyar  * @param chassisSubNode   Chassis Node for which the query has to happen
2771413961deSRichard Marian Thomaiyar  */
277223a21a1cSEd Tanous inline void setSensorsOverride(
2773b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
27744bb3dc34SCarol Wang     std::unordered_map<std::string, std::vector<nlohmann::json>>&
2775397fd61fSjayaprakash Mutyala         allCollections)
2776413961deSRichard Marian Thomaiyar {
277770d1d0aaSjayaprakash Mutyala     BMCWEB_LOG_INFO << "setSensorsOverride for subNode"
27784bb3dc34SCarol Wang                     << sensorAsyncResp->chassisSubNode << "\n";
2779413961deSRichard Marian Thomaiyar 
2780543f4400SEd Tanous     const char* propertyValueName = nullptr;
2781f65af9e8SRichard Marian Thomaiyar     std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
2782413961deSRichard Marian Thomaiyar     std::string memberId;
2783543f4400SEd Tanous     double value = 0.0;
2784f65af9e8SRichard Marian Thomaiyar     for (auto& collectionItems : allCollections)
2785f65af9e8SRichard Marian Thomaiyar     {
2786f65af9e8SRichard Marian Thomaiyar         if (collectionItems.first == "Temperatures")
2787f65af9e8SRichard Marian Thomaiyar         {
2788f65af9e8SRichard Marian Thomaiyar             propertyValueName = "ReadingCelsius";
2789f65af9e8SRichard Marian Thomaiyar         }
2790f65af9e8SRichard Marian Thomaiyar         else if (collectionItems.first == "Fans")
2791f65af9e8SRichard Marian Thomaiyar         {
2792f65af9e8SRichard Marian Thomaiyar             propertyValueName = "Reading";
2793f65af9e8SRichard Marian Thomaiyar         }
2794f65af9e8SRichard Marian Thomaiyar         else
2795f65af9e8SRichard Marian Thomaiyar         {
2796f65af9e8SRichard Marian Thomaiyar             propertyValueName = "ReadingVolts";
2797f65af9e8SRichard Marian Thomaiyar         }
2798f65af9e8SRichard Marian Thomaiyar         for (auto& item : collectionItems.second)
2799f65af9e8SRichard Marian Thomaiyar         {
28008d1b46d7Szhanghch05             if (!json_util::readJson(item, sensorAsyncResp->asyncResp->res,
28018d1b46d7Szhanghch05                                      "MemberId", memberId, propertyValueName,
28028d1b46d7Szhanghch05                                      value))
2803413961deSRichard Marian Thomaiyar             {
2804413961deSRichard Marian Thomaiyar                 return;
2805413961deSRichard Marian Thomaiyar             }
2806f65af9e8SRichard Marian Thomaiyar             overrideMap.emplace(memberId,
2807f65af9e8SRichard Marian Thomaiyar                                 std::make_pair(value, collectionItems.first));
2808f65af9e8SRichard Marian Thomaiyar         }
2809f65af9e8SRichard Marian Thomaiyar     }
28104bb3dc34SCarol Wang 
2811002d39b4SEd Tanous     auto getChassisSensorListCb =
2812002d39b4SEd Tanous         [sensorAsyncResp, overrideMap](
2813*fe04d49cSNan Zhou             const std::shared_ptr<std::set<std::string>>& sensorsList) {
281449c53ac9SJohnathan Mantey         // Match sensor names in the PATCH request to those managed by the
281549c53ac9SJohnathan Mantey         // chassis node
2816*fe04d49cSNan Zhou         const std::shared_ptr<std::set<std::string>> sensorNames =
2817*fe04d49cSNan Zhou             std::make_shared<std::set<std::string>>();
2818f65af9e8SRichard Marian Thomaiyar         for (const auto& item : overrideMap)
2819413961deSRichard Marian Thomaiyar         {
2820f65af9e8SRichard Marian Thomaiyar             const auto& sensor = item.first;
282149c53ac9SJohnathan Mantey             if (!findSensorNameUsingSensorPath(sensor, *sensorsList,
282249c53ac9SJohnathan Mantey                                                *sensorNames))
2823f65af9e8SRichard Marian Thomaiyar             {
2824f65af9e8SRichard Marian Thomaiyar                 BMCWEB_LOG_INFO << "Unable to find memberId " << item.first;
28258d1b46d7Szhanghch05                 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2826f65af9e8SRichard Marian Thomaiyar                                            item.second.second, item.first);
2827413961deSRichard Marian Thomaiyar                 return;
2828413961deSRichard Marian Thomaiyar             }
2829f65af9e8SRichard Marian Thomaiyar         }
2830413961deSRichard Marian Thomaiyar         // Get the connection to which the memberId belongs
2831002d39b4SEd Tanous         auto getObjectsWithConnectionCb =
2832*fe04d49cSNan Zhou             [sensorAsyncResp,
2833*fe04d49cSNan Zhou              overrideMap](const std::set<std::string>& /*connections*/,
2834002d39b4SEd Tanous                           const std::set<std::pair<std::string, std::string>>&
2835413961deSRichard Marian Thomaiyar                               objectsWithConnection) {
2836f65af9e8SRichard Marian Thomaiyar             if (objectsWithConnection.size() != overrideMap.size())
2837413961deSRichard Marian Thomaiyar             {
2838413961deSRichard Marian Thomaiyar                 BMCWEB_LOG_INFO
2839f65af9e8SRichard Marian Thomaiyar                     << "Unable to find all objects with proper connection "
2840f65af9e8SRichard Marian Thomaiyar                     << objectsWithConnection.size() << " requested "
2841f65af9e8SRichard Marian Thomaiyar                     << overrideMap.size() << "\n";
28424f277b54SJayaprakash Mutyala                 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2843a0ec28b6SAdrian Ambrożewicz                                            sensorAsyncResp->chassisSubNode ==
2844a0ec28b6SAdrian Ambrożewicz                                                    sensors::node::thermal
2845413961deSRichard Marian Thomaiyar                                                ? "Temperatures"
2846413961deSRichard Marian Thomaiyar                                                : "Voltages",
2847f65af9e8SRichard Marian Thomaiyar                                            "Count");
2848f65af9e8SRichard Marian Thomaiyar                 return;
2849f65af9e8SRichard Marian Thomaiyar             }
2850f65af9e8SRichard Marian Thomaiyar             for (const auto& item : objectsWithConnection)
2851f65af9e8SRichard Marian Thomaiyar             {
285228aa8de5SGeorge Liu                 sdbusplus::message::object_path path(item.first);
285328aa8de5SGeorge Liu                 std::string sensorName = path.filename();
285428aa8de5SGeorge Liu                 if (sensorName.empty())
2855f65af9e8SRichard Marian Thomaiyar                 {
28564f277b54SJayaprakash Mutyala                     messages::internalError(sensorAsyncResp->asyncResp->res);
2857f65af9e8SRichard Marian Thomaiyar                     return;
2858f65af9e8SRichard Marian Thomaiyar                 }
2859f65af9e8SRichard Marian Thomaiyar 
2860f65af9e8SRichard Marian Thomaiyar                 const auto& iterator = overrideMap.find(sensorName);
2861f65af9e8SRichard Marian Thomaiyar                 if (iterator == overrideMap.end())
2862f65af9e8SRichard Marian Thomaiyar                 {
2863f65af9e8SRichard Marian Thomaiyar                     BMCWEB_LOG_INFO << "Unable to find sensor object"
2864f65af9e8SRichard Marian Thomaiyar                                     << item.first << "\n";
28654f277b54SJayaprakash Mutyala                     messages::internalError(sensorAsyncResp->asyncResp->res);
2866413961deSRichard Marian Thomaiyar                     return;
2867413961deSRichard Marian Thomaiyar                 }
2868413961deSRichard Marian Thomaiyar                 crow::connections::systemBus->async_method_call(
2869f65af9e8SRichard Marian Thomaiyar                     [sensorAsyncResp](const boost::system::error_code ec) {
2870413961deSRichard Marian Thomaiyar                     if (ec)
2871413961deSRichard Marian Thomaiyar                     {
28724f277b54SJayaprakash Mutyala                         if (ec.value() ==
28734f277b54SJayaprakash Mutyala                             boost::system::errc::permission_denied)
28744f277b54SJayaprakash Mutyala                         {
28754f277b54SJayaprakash Mutyala                             BMCWEB_LOG_WARNING
28764f277b54SJayaprakash Mutyala                                 << "Manufacturing mode is not Enabled...can't "
28774f277b54SJayaprakash Mutyala                                    "Override the sensor value. ";
28784f277b54SJayaprakash Mutyala 
28794f277b54SJayaprakash Mutyala                             messages::insufficientPrivilege(
28808d1b46d7Szhanghch05                                 sensorAsyncResp->asyncResp->res);
2881413961deSRichard Marian Thomaiyar                             return;
2882413961deSRichard Marian Thomaiyar                         }
28834f277b54SJayaprakash Mutyala                         BMCWEB_LOG_DEBUG
28844f277b54SJayaprakash Mutyala                             << "setOverrideValueStatus DBUS error: " << ec;
28854f277b54SJayaprakash Mutyala                         messages::internalError(
28864f277b54SJayaprakash Mutyala                             sensorAsyncResp->asyncResp->res);
28874f277b54SJayaprakash Mutyala                     }
2888413961deSRichard Marian Thomaiyar                     },
28894f277b54SJayaprakash Mutyala                     item.second, item.first, "org.freedesktop.DBus.Properties",
28904f277b54SJayaprakash Mutyala                     "Set", "xyz.openbmc_project.Sensor.Value", "Value",
2891168e20c1SEd Tanous                     dbus::utility::DbusVariantType(iterator->second.first));
2892f65af9e8SRichard Marian Thomaiyar             }
2893413961deSRichard Marian Thomaiyar         };
2894413961deSRichard Marian Thomaiyar         // Get object with connection for the given sensor name
2895413961deSRichard Marian Thomaiyar         getObjectsWithConnection(sensorAsyncResp, sensorNames,
2896413961deSRichard Marian Thomaiyar                                  std::move(getObjectsWithConnectionCb));
2897413961deSRichard Marian Thomaiyar     };
2898413961deSRichard Marian Thomaiyar     // get full sensor list for the given chassisId and cross verify the sensor.
2899413961deSRichard Marian Thomaiyar     getChassis(sensorAsyncResp, std::move(getChassisSensorListCb));
2900413961deSRichard Marian Thomaiyar }
2901413961deSRichard Marian Thomaiyar 
2902a0ec28b6SAdrian Ambrożewicz /**
2903a0ec28b6SAdrian Ambrożewicz  * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
2904a0ec28b6SAdrian Ambrożewicz  * path of the sensor.
2905a0ec28b6SAdrian Ambrożewicz  *
2906a0ec28b6SAdrian Ambrożewicz  * Function builds valid Redfish response for sensor query of given chassis and
2907a0ec28b6SAdrian Ambrożewicz  * node. It then builds metadata about Redfish<->D-Bus correlations and provides
2908a0ec28b6SAdrian Ambrożewicz  * it to caller in a callback.
2909a0ec28b6SAdrian Ambrożewicz  *
2910a0ec28b6SAdrian Ambrożewicz  * @param chassis   Chassis for which retrieval should be performed
2911a0ec28b6SAdrian Ambrożewicz  * @param node  Node (group) of sensors. See sensors::node for supported values
2912a0ec28b6SAdrian Ambrożewicz  * @param mapComplete   Callback to be called with retrieval result
2913a0ec28b6SAdrian Ambrożewicz  */
2914021d32cfSKrzysztof Grobelny inline void retrieveUriToDbusMap(const std::string& chassis,
2915021d32cfSKrzysztof Grobelny                                  const std::string& node,
2916a0ec28b6SAdrian Ambrożewicz                                  SensorsAsyncResp::DataCompleteCb&& mapComplete)
2917a0ec28b6SAdrian Ambrożewicz {
291802da7c5aSEd Tanous     decltype(sensors::paths)::const_iterator pathIt =
291902da7c5aSEd Tanous         std::find_if(sensors::paths.cbegin(), sensors::paths.cend(),
292002da7c5aSEd Tanous                      [&node](auto&& val) { return val.first == node; });
292102da7c5aSEd Tanous     if (pathIt == sensors::paths.cend())
2922a0ec28b6SAdrian Ambrożewicz     {
2923a0ec28b6SAdrian Ambrożewicz         BMCWEB_LOG_ERROR << "Wrong node provided : " << node;
2924a0ec28b6SAdrian Ambrożewicz         mapComplete(boost::beast::http::status::bad_request, {});
2925a0ec28b6SAdrian Ambrożewicz         return;
2926a0ec28b6SAdrian Ambrożewicz     }
2927d51e072fSKrzysztof Grobelny 
292872374eb7SNan Zhou     auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
2929*fe04d49cSNan Zhou     auto callback = [asyncResp, mapCompleteCb{std::move(mapComplete)}](
2930a0ec28b6SAdrian Ambrożewicz                         const boost::beast::http::status status,
2931*fe04d49cSNan Zhou                         const std::map<std::string, std::string>& uriToDbus) {
2932*fe04d49cSNan Zhou         mapCompleteCb(status, uriToDbus);
2933*fe04d49cSNan Zhou     };
2934a0ec28b6SAdrian Ambrożewicz 
2935a0ec28b6SAdrian Ambrożewicz     auto resp = std::make_shared<SensorsAsyncResp>(
2936d51e072fSKrzysztof Grobelny         asyncResp, chassis, pathIt->second, node, std::move(callback));
2937a0ec28b6SAdrian Ambrożewicz     getChassisData(resp);
2938a0ec28b6SAdrian Ambrożewicz }
2939a0ec28b6SAdrian Ambrożewicz 
2940bacb2162SNan Zhou namespace sensors
2941bacb2162SNan Zhou {
2942928fefb9SNan Zhou 
2943bacb2162SNan Zhou inline void getChassisCallback(
2944bacb2162SNan Zhou     const std::shared_ptr<SensorsAsyncResp>& asyncResp,
2945*fe04d49cSNan Zhou     const std::shared_ptr<std::set<std::string>>& sensorNames)
2946bacb2162SNan Zhou {
2947bacb2162SNan Zhou     BMCWEB_LOG_DEBUG << "getChassisCallback enter";
2948bacb2162SNan Zhou 
2949bacb2162SNan Zhou     nlohmann::json& entriesArray =
2950bacb2162SNan Zhou         asyncResp->asyncResp->res.jsonValue["Members"];
2951*fe04d49cSNan Zhou     for (const auto& sensor : *sensorNames)
2952bacb2162SNan Zhou     {
2953bacb2162SNan Zhou         BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor;
2954bacb2162SNan Zhou 
2955bacb2162SNan Zhou         sdbusplus::message::object_path path(sensor);
2956bacb2162SNan Zhou         std::string sensorName = path.filename();
2957bacb2162SNan Zhou         if (sensorName.empty())
2958bacb2162SNan Zhou         {
2959bacb2162SNan Zhou             BMCWEB_LOG_ERROR << "Invalid sensor path: " << sensor;
2960bacb2162SNan Zhou             messages::internalError(asyncResp->asyncResp->res);
2961bacb2162SNan Zhou             return;
2962bacb2162SNan Zhou         }
29631476687dSEd Tanous         nlohmann::json::object_t member;
29641476687dSEd Tanous         member["@odata.id"] = "/redfish/v1/Chassis/" + asyncResp->chassisId +
29651476687dSEd Tanous                               "/" + asyncResp->chassisSubNode + "/" +
29661476687dSEd Tanous                               sensorName;
29671476687dSEd Tanous         entriesArray.push_back(std::move(member));
2968bacb2162SNan Zhou     }
2969bacb2162SNan Zhou 
2970bacb2162SNan Zhou     asyncResp->asyncResp->res.jsonValue["Members@odata.count"] =
2971bacb2162SNan Zhou         entriesArray.size();
2972bacb2162SNan Zhou     BMCWEB_LOG_DEBUG << "getChassisCallback exit";
2973bacb2162SNan Zhou }
2974e6bd846dSNan Zhou 
2975de167a6fSNan Zhou inline void
2976de167a6fSNan Zhou     handleSensorCollectionGet(App& app, const crow::Request& req,
2977de167a6fSNan Zhou                               const std::shared_ptr<bmcweb::AsyncResp>& aResp,
2978de167a6fSNan Zhou                               const std::string& chassisId)
2979de167a6fSNan Zhou {
2980de167a6fSNan Zhou     query_param::QueryCapabilities capabilities = {
2981de167a6fSNan Zhou         .canDelegateExpandLevel = 1,
2982de167a6fSNan Zhou     };
2983de167a6fSNan Zhou     query_param::Query delegatedQuery;
29843ba00073SCarson Labrado     if (!redfish::setUpRedfishRouteWithDelegation(app, req, aResp,
2985de167a6fSNan Zhou                                                   delegatedQuery, capabilities))
2986de167a6fSNan Zhou     {
2987de167a6fSNan Zhou         return;
2988de167a6fSNan Zhou     }
2989de167a6fSNan Zhou 
2990de167a6fSNan Zhou     if (delegatedQuery.expandType != query_param::ExpandType::None)
2991de167a6fSNan Zhou     {
2992de167a6fSNan Zhou         // we perform efficient expand.
2993de167a6fSNan Zhou         auto asyncResp = std::make_shared<SensorsAsyncResp>(
2994de167a6fSNan Zhou             aResp, chassisId, sensors::dbus::sensorPaths,
2995de167a6fSNan Zhou             sensors::node::sensors,
2996de167a6fSNan Zhou             /*efficientExpand=*/true);
2997de167a6fSNan Zhou         getChassisData(asyncResp);
2998de167a6fSNan Zhou 
2999de167a6fSNan Zhou         BMCWEB_LOG_DEBUG
3000de167a6fSNan Zhou             << "SensorCollection doGet exit via efficient expand handler";
3001de167a6fSNan Zhou         return;
30020bad320cSEd Tanous     }
3003de167a6fSNan Zhou 
3004de167a6fSNan Zhou     // if there's no efficient expand available, we use the default
3005de167a6fSNan Zhou     // Query Parameters route
3006de167a6fSNan Zhou     auto asyncResp = std::make_shared<SensorsAsyncResp>(
3007de167a6fSNan Zhou         aResp, chassisId, sensors::dbus::sensorPaths, sensors::node::sensors);
3008de167a6fSNan Zhou 
3009de167a6fSNan Zhou     // We get all sensors as hyperlinkes in the chassis (this
3010de167a6fSNan Zhou     // implies we reply on the default query parameters handler)
3011de167a6fSNan Zhou     getChassis(asyncResp,
3012de167a6fSNan Zhou                std::bind_front(sensors::getChassisCallback, asyncResp));
3013de167a6fSNan Zhou     BMCWEB_LOG_DEBUG << "SensorCollection doGet exit";
3014de167a6fSNan Zhou }
3015de167a6fSNan Zhou 
3016e6bd846dSNan Zhou inline void handleSensorGet(App& app, const crow::Request& req,
3017e6bd846dSNan Zhou                             const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3018e6bd846dSNan Zhou                             const std::string& chassisId,
3019e6bd846dSNan Zhou                             const std::string& sensorName)
3020e6bd846dSNan Zhou {
30213ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, aResp))
3022e6bd846dSNan Zhou     {
3023e6bd846dSNan Zhou         return;
3024e6bd846dSNan Zhou     }
3025e6bd846dSNan Zhou     BMCWEB_LOG_DEBUG << "Sensor doGet enter";
3026e6bd846dSNan Zhou     std::shared_ptr<SensorsAsyncResp> asyncResp =
3027e6bd846dSNan Zhou         std::make_shared<SensorsAsyncResp>(aResp, chassisId,
3028e6bd846dSNan Zhou                                            std::span<std::string_view>(),
3029e6bd846dSNan Zhou                                            sensors::node::sensors);
3030e6bd846dSNan Zhou 
3031e6bd846dSNan Zhou     const std::array<const char*, 1> interfaces = {
3032e6bd846dSNan Zhou         "xyz.openbmc_project.Sensor.Value"};
3033e6bd846dSNan Zhou 
3034e6bd846dSNan Zhou     // Get a list of all of the sensors that implement Sensor.Value
3035e6bd846dSNan Zhou     // and get the path and service name associated with the sensor
3036e6bd846dSNan Zhou     crow::connections::systemBus->async_method_call(
3037e6bd846dSNan Zhou         [asyncResp,
3038e6bd846dSNan Zhou          sensorName](const boost::system::error_code ec,
3039e6bd846dSNan Zhou                      const ::dbus::utility::MapperGetSubTreeResponse& subtree) {
3040e6bd846dSNan Zhou         BMCWEB_LOG_DEBUG << "respHandler1 enter";
3041e6bd846dSNan Zhou         if (ec)
3042e6bd846dSNan Zhou         {
3043e6bd846dSNan Zhou             messages::internalError(asyncResp->asyncResp->res);
3044e6bd846dSNan Zhou             BMCWEB_LOG_ERROR << "Sensor getSensorPaths resp_handler: "
3045e6bd846dSNan Zhou                              << "Dbus error " << ec;
3046e6bd846dSNan Zhou             return;
3047e6bd846dSNan Zhou         }
3048e6bd846dSNan Zhou 
3049e6bd846dSNan Zhou         ::dbus::utility::MapperGetSubTreeResponse::const_iterator it =
3050e6bd846dSNan Zhou             std::find_if(
3051e6bd846dSNan Zhou                 subtree.begin(), subtree.end(),
3052e6bd846dSNan Zhou                 [sensorName](
3053e6bd846dSNan Zhou                     const std::pair<
3054e6bd846dSNan Zhou                         std::string,
3055e6bd846dSNan Zhou                         std::vector<std::pair<
3056e6bd846dSNan Zhou                             std::string, std::vector<std::string>>>>& object) {
3057e6bd846dSNan Zhou             sdbusplus::message::object_path path(object.first);
3058e6bd846dSNan Zhou             std::string name = path.filename();
3059e6bd846dSNan Zhou             if (name.empty())
3060e6bd846dSNan Zhou             {
3061e6bd846dSNan Zhou                 BMCWEB_LOG_ERROR << "Invalid sensor path: " << object.first;
3062e6bd846dSNan Zhou                 return false;
3063e6bd846dSNan Zhou             }
3064e6bd846dSNan Zhou 
3065e6bd846dSNan Zhou             return name == sensorName;
3066e6bd846dSNan Zhou                 });
3067e6bd846dSNan Zhou 
3068e6bd846dSNan Zhou         if (it == subtree.end())
3069e6bd846dSNan Zhou         {
3070e6bd846dSNan Zhou             BMCWEB_LOG_ERROR << "Could not find path for sensor: "
3071e6bd846dSNan Zhou                              << sensorName;
3072e6bd846dSNan Zhou             messages::resourceNotFound(asyncResp->asyncResp->res, "Sensor",
3073e6bd846dSNan Zhou                                        sensorName);
3074e6bd846dSNan Zhou             return;
3075e6bd846dSNan Zhou         }
3076e6bd846dSNan Zhou         std::string_view sensorPath = (*it).first;
3077e6bd846dSNan Zhou         BMCWEB_LOG_DEBUG << "Found sensor path for sensor '" << sensorName
3078e6bd846dSNan Zhou                          << "': " << sensorPath;
3079e6bd846dSNan Zhou 
3080*fe04d49cSNan Zhou         const std::shared_ptr<std::set<std::string>> sensorList =
3081*fe04d49cSNan Zhou             std::make_shared<std::set<std::string>>();
3082e6bd846dSNan Zhou 
3083e6bd846dSNan Zhou         sensorList->emplace(sensorPath);
3084e6bd846dSNan Zhou         processSensorList(asyncResp, sensorList);
3085e6bd846dSNan Zhou         BMCWEB_LOG_DEBUG << "respHandler1 exit";
3086e6bd846dSNan Zhou         },
3087e6bd846dSNan Zhou         "xyz.openbmc_project.ObjectMapper",
3088e6bd846dSNan Zhou         "/xyz/openbmc_project/object_mapper",
3089e6bd846dSNan Zhou         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
3090e6bd846dSNan Zhou         "/xyz/openbmc_project/sensors", 2, interfaces);
3091e6bd846dSNan Zhou }
3092e6bd846dSNan Zhou 
3093bacb2162SNan Zhou } // namespace sensors
3094bacb2162SNan Zhou 
30957e860f15SJohn Edward Broadbent inline void requestRoutesSensorCollection(App& app)
309695a3ecadSAnthony Wilson {
30977e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/")
3098ed398213SEd Tanous         .privileges(redfish::privileges::getSensorCollection)
3099002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
3100de167a6fSNan Zhou             std::bind_front(sensors::handleSensorCollectionGet, std::ref(app)));
310195a3ecadSAnthony Wilson }
310295a3ecadSAnthony Wilson 
31037e860f15SJohn Edward Broadbent inline void requestRoutesSensor(App& app)
310495a3ecadSAnthony Wilson {
31057e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/")
3106ed398213SEd Tanous         .privileges(redfish::privileges::getSensor)
3107002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
3108e6bd846dSNan Zhou             std::bind_front(sensors::handleSensorGet, std::ref(app)));
310995a3ecadSAnthony Wilson }
311095a3ecadSAnthony Wilson 
311108777fb0SLewanczyk, Dawid } // namespace redfish
3112