xref: /openbmc/bmcweb/features/redfish/lib/sensors.hpp (revision 7088690c98169f0a8710909560647a560287f8a6)
108777fb0SLewanczyk, Dawid /*
208777fb0SLewanczyk, Dawid // Copyright (c) 2018 Intel Corporation
308777fb0SLewanczyk, Dawid //
408777fb0SLewanczyk, Dawid // Licensed under the Apache License, Version 2.0 (the "License");
508777fb0SLewanczyk, Dawid // you may not use this file except in compliance with the License.
608777fb0SLewanczyk, Dawid // You may obtain a copy of the License at
708777fb0SLewanczyk, Dawid //
808777fb0SLewanczyk, Dawid //      http://www.apache.org/licenses/LICENSE-2.0
908777fb0SLewanczyk, Dawid //
1008777fb0SLewanczyk, Dawid // Unless required by applicable law or agreed to in writing, software
1108777fb0SLewanczyk, Dawid // distributed under the License is distributed on an "AS IS" BASIS,
1208777fb0SLewanczyk, Dawid // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1308777fb0SLewanczyk, Dawid // See the License for the specific language governing permissions and
1408777fb0SLewanczyk, Dawid // limitations under the License.
1508777fb0SLewanczyk, Dawid */
1608777fb0SLewanczyk, Dawid #pragma once
1708777fb0SLewanczyk, Dawid 
187e860f15SJohn Edward Broadbent #include <app.hpp>
1908777fb0SLewanczyk, Dawid #include <boost/algorithm/string/predicate.hpp>
2008777fb0SLewanczyk, Dawid #include <boost/algorithm/string/split.hpp>
2108777fb0SLewanczyk, Dawid #include <boost/container/flat_map.hpp>
2208777fb0SLewanczyk, Dawid #include <boost/range/algorithm/replace_copy_if.hpp>
231abe55efSEd Tanous #include <dbus_singleton.hpp>
24ed398213SEd Tanous #include <registries/privilege_registry.hpp>
25413961deSRichard Marian Thomaiyar #include <utils/json_utils.hpp>
261214b7e7SGunnar Mills 
271214b7e7SGunnar Mills #include <cmath>
28b5a76932SEd Tanous #include <utility>
29abf2add6SEd Tanous #include <variant>
3008777fb0SLewanczyk, Dawid 
311abe55efSEd Tanous namespace redfish
321abe55efSEd Tanous {
3308777fb0SLewanczyk, Dawid 
3408777fb0SLewanczyk, Dawid using GetSubTreeType = std::vector<
3508777fb0SLewanczyk, Dawid     std::pair<std::string,
3608777fb0SLewanczyk, Dawid               std::vector<std::pair<std::string, std::vector<std::string>>>>>;
3708777fb0SLewanczyk, Dawid 
38adc4f0dbSShawn McCarney using SensorVariant =
39adc4f0dbSShawn McCarney     std::variant<int64_t, double, uint32_t, bool, std::string>;
40aa2e59c1SEd Tanous 
4108777fb0SLewanczyk, Dawid using ManagedObjectsVectorType = std::vector<std::pair<
42aa2e59c1SEd Tanous     sdbusplus::message::object_path,
4308777fb0SLewanczyk, Dawid     boost::container::flat_map<
44aa2e59c1SEd Tanous         std::string, boost::container::flat_map<std::string, SensorVariant>>>>;
4508777fb0SLewanczyk, Dawid 
46a0ec28b6SAdrian Ambrożewicz namespace sensors
47a0ec28b6SAdrian Ambrożewicz {
48a0ec28b6SAdrian Ambrożewicz namespace node
49a0ec28b6SAdrian Ambrożewicz {
50a0ec28b6SAdrian Ambrożewicz static constexpr std::string_view power = "Power";
51a0ec28b6SAdrian Ambrożewicz static constexpr std::string_view sensors = "Sensors";
52a0ec28b6SAdrian Ambrożewicz static constexpr std::string_view thermal = "Thermal";
53a0ec28b6SAdrian Ambrożewicz } // namespace node
54a0ec28b6SAdrian Ambrożewicz 
55a0ec28b6SAdrian Ambrożewicz namespace dbus
56a0ec28b6SAdrian Ambrożewicz {
57c2bf7f99SWludzik, Jozef 
58a0ec28b6SAdrian Ambrożewicz static const boost::container::flat_map<std::string_view,
59a0ec28b6SAdrian Ambrożewicz                                         std::vector<const char*>>
60c2bf7f99SWludzik, Jozef     paths = {{node::power,
61a0ec28b6SAdrian Ambrożewicz               {"/xyz/openbmc_project/sensors/voltage",
62a0ec28b6SAdrian Ambrożewicz                "/xyz/openbmc_project/sensors/power"}},
63a0ec28b6SAdrian Ambrożewicz              {node::sensors,
64a0ec28b6SAdrian Ambrożewicz               {"/xyz/openbmc_project/sensors/power",
65a0ec28b6SAdrian Ambrożewicz                "/xyz/openbmc_project/sensors/current",
66*7088690cSBasheer Ahmed Muddebihal                "/xyz/openbmc_project/sensors/airflow",
67a0ec28b6SAdrian Ambrożewicz                "/xyz/openbmc_project/sensors/utilization"}},
68a0ec28b6SAdrian Ambrożewicz              {node::thermal,
69a0ec28b6SAdrian Ambrożewicz               {"/xyz/openbmc_project/sensors/fan_tach",
70a0ec28b6SAdrian Ambrożewicz                "/xyz/openbmc_project/sensors/temperature",
71a0ec28b6SAdrian Ambrożewicz                "/xyz/openbmc_project/sensors/fan_pwm"}}};
72c2bf7f99SWludzik, Jozef } // namespace dbus
73c2bf7f99SWludzik, Jozef 
74c2bf7f99SWludzik, Jozef inline const char* toReadingType(const std::string& sensorType)
75c2bf7f99SWludzik, Jozef {
76c2bf7f99SWludzik, Jozef     if (sensorType == "voltage")
77c2bf7f99SWludzik, Jozef     {
78c2bf7f99SWludzik, Jozef         return "Voltage";
79c2bf7f99SWludzik, Jozef     }
80c2bf7f99SWludzik, Jozef     if (sensorType == "power")
81c2bf7f99SWludzik, Jozef     {
82c2bf7f99SWludzik, Jozef         return "Power";
83c2bf7f99SWludzik, Jozef     }
84c2bf7f99SWludzik, Jozef     if (sensorType == "current")
85c2bf7f99SWludzik, Jozef     {
86c2bf7f99SWludzik, Jozef         return "Current";
87c2bf7f99SWludzik, Jozef     }
88c2bf7f99SWludzik, Jozef     if (sensorType == "fan_tach")
89c2bf7f99SWludzik, Jozef     {
90c2bf7f99SWludzik, Jozef         return "Rotational";
91c2bf7f99SWludzik, Jozef     }
92c2bf7f99SWludzik, Jozef     if (sensorType == "temperature")
93c2bf7f99SWludzik, Jozef     {
94c2bf7f99SWludzik, Jozef         return "Temperature";
95c2bf7f99SWludzik, Jozef     }
96c2bf7f99SWludzik, Jozef     if (sensorType == "fan_pwm" || sensorType == "utilization")
97c2bf7f99SWludzik, Jozef     {
98c2bf7f99SWludzik, Jozef         return "Percent";
99c2bf7f99SWludzik, Jozef     }
100c2bf7f99SWludzik, Jozef     if (sensorType == "altitude")
101c2bf7f99SWludzik, Jozef     {
102c2bf7f99SWludzik, Jozef         return "Altitude";
103c2bf7f99SWludzik, Jozef     }
104c2bf7f99SWludzik, Jozef     if (sensorType == "airflow")
105c2bf7f99SWludzik, Jozef     {
106c2bf7f99SWludzik, Jozef         return "AirFlow";
107c2bf7f99SWludzik, Jozef     }
108c2bf7f99SWludzik, Jozef     if (sensorType == "energy")
109c2bf7f99SWludzik, Jozef     {
110c2bf7f99SWludzik, Jozef         return "EnergyJoules";
111c2bf7f99SWludzik, Jozef     }
112c2bf7f99SWludzik, Jozef     return "";
113c2bf7f99SWludzik, Jozef }
114c2bf7f99SWludzik, Jozef 
115c2bf7f99SWludzik, Jozef inline const char* toReadingUnits(const std::string& sensorType)
116c2bf7f99SWludzik, Jozef {
117c2bf7f99SWludzik, Jozef     if (sensorType == "voltage")
118c2bf7f99SWludzik, Jozef     {
119c2bf7f99SWludzik, Jozef         return "V";
120c2bf7f99SWludzik, Jozef     }
121c2bf7f99SWludzik, Jozef     if (sensorType == "power")
122c2bf7f99SWludzik, Jozef     {
123c2bf7f99SWludzik, Jozef         return "W";
124c2bf7f99SWludzik, Jozef     }
125c2bf7f99SWludzik, Jozef     if (sensorType == "current")
126c2bf7f99SWludzik, Jozef     {
127c2bf7f99SWludzik, Jozef         return "A";
128c2bf7f99SWludzik, Jozef     }
129c2bf7f99SWludzik, Jozef     if (sensorType == "fan_tach")
130c2bf7f99SWludzik, Jozef     {
131c2bf7f99SWludzik, Jozef         return "RPM";
132c2bf7f99SWludzik, Jozef     }
133c2bf7f99SWludzik, Jozef     if (sensorType == "temperature")
134c2bf7f99SWludzik, Jozef     {
135c2bf7f99SWludzik, Jozef         return "Cel";
136c2bf7f99SWludzik, Jozef     }
137c2bf7f99SWludzik, Jozef     if (sensorType == "fan_pwm" || sensorType == "utilization")
138c2bf7f99SWludzik, Jozef     {
139c2bf7f99SWludzik, Jozef         return "%";
140c2bf7f99SWludzik, Jozef     }
141c2bf7f99SWludzik, Jozef     if (sensorType == "altitude")
142c2bf7f99SWludzik, Jozef     {
143c2bf7f99SWludzik, Jozef         return "m";
144c2bf7f99SWludzik, Jozef     }
145c2bf7f99SWludzik, Jozef     if (sensorType == "airflow")
146c2bf7f99SWludzik, Jozef     {
147c2bf7f99SWludzik, Jozef         return "cft_i/min";
148c2bf7f99SWludzik, Jozef     }
149c2bf7f99SWludzik, Jozef     if (sensorType == "energy")
150c2bf7f99SWludzik, Jozef     {
151c2bf7f99SWludzik, Jozef         return "J";
152c2bf7f99SWludzik, Jozef     }
153c2bf7f99SWludzik, Jozef     return "";
154a0ec28b6SAdrian Ambrożewicz }
155a0ec28b6SAdrian Ambrożewicz } // namespace sensors
156a0ec28b6SAdrian Ambrożewicz 
15708777fb0SLewanczyk, Dawid /**
158588c3f0dSKowalski, Kamil  * SensorsAsyncResp
15908777fb0SLewanczyk, Dawid  * Gathers data needed for response processing after async calls are done
16008777fb0SLewanczyk, Dawid  */
1611abe55efSEd Tanous class SensorsAsyncResp
1621abe55efSEd Tanous {
16308777fb0SLewanczyk, Dawid   public:
164a0ec28b6SAdrian Ambrożewicz     using DataCompleteCb = std::function<void(
165a0ec28b6SAdrian Ambrożewicz         const boost::beast::http::status status,
166a0ec28b6SAdrian Ambrożewicz         const boost::container::flat_map<std::string, std::string>& uriToDbus)>;
167a0ec28b6SAdrian Ambrożewicz 
168a0ec28b6SAdrian Ambrożewicz     struct SensorData
169a0ec28b6SAdrian Ambrożewicz     {
170a0ec28b6SAdrian Ambrożewicz         const std::string name;
171a0ec28b6SAdrian Ambrożewicz         std::string uri;
172a0ec28b6SAdrian Ambrożewicz         const std::string valueKey;
173a0ec28b6SAdrian Ambrożewicz         const std::string dbusPath;
174a0ec28b6SAdrian Ambrożewicz     };
175a0ec28b6SAdrian Ambrożewicz 
1768d1b46d7Szhanghch05     SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1778d1b46d7Szhanghch05                      const std::string& chassisIdIn,
178b5a76932SEd Tanous                      const std::vector<const char*>& typesIn,
179a0ec28b6SAdrian Ambrożewicz                      const std::string_view& subNode) :
1808d1b46d7Szhanghch05         asyncResp(asyncResp),
181271584abSEd Tanous         chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode)
1821214b7e7SGunnar Mills     {}
18308777fb0SLewanczyk, Dawid 
184a0ec28b6SAdrian Ambrożewicz     // Store extra data about sensor mapping and return it in callback
1858d1b46d7Szhanghch05     SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1868d1b46d7Szhanghch05                      const std::string& chassisIdIn,
187b5a76932SEd Tanous                      const std::vector<const char*>& typesIn,
188a0ec28b6SAdrian Ambrożewicz                      const std::string_view& subNode,
189a0ec28b6SAdrian Ambrożewicz                      DataCompleteCb&& creationComplete) :
1908d1b46d7Szhanghch05         asyncResp(asyncResp),
191a0ec28b6SAdrian Ambrożewicz         chassisId(chassisIdIn), types(typesIn),
192a0ec28b6SAdrian Ambrożewicz         chassisSubNode(subNode), metadata{std::vector<SensorData>()},
193a0ec28b6SAdrian Ambrożewicz         dataComplete{std::move(creationComplete)}
194a0ec28b6SAdrian Ambrożewicz     {}
195a0ec28b6SAdrian Ambrożewicz 
1961abe55efSEd Tanous     ~SensorsAsyncResp()
1971abe55efSEd Tanous     {
1988d1b46d7Szhanghch05         if (asyncResp->res.result() ==
1998d1b46d7Szhanghch05             boost::beast::http::status::internal_server_error)
2001abe55efSEd Tanous         {
2011abe55efSEd Tanous             // Reset the json object to clear out any data that made it in
2021abe55efSEd Tanous             // before the error happened todo(ed) handle error condition with
2031abe55efSEd Tanous             // proper code
2048d1b46d7Szhanghch05             asyncResp->res.jsonValue = nlohmann::json::object();
20508777fb0SLewanczyk, Dawid         }
206a0ec28b6SAdrian Ambrożewicz 
207a0ec28b6SAdrian Ambrożewicz         if (dataComplete && metadata)
208a0ec28b6SAdrian Ambrożewicz         {
209a0ec28b6SAdrian Ambrożewicz             boost::container::flat_map<std::string, std::string> map;
2108d1b46d7Szhanghch05             if (asyncResp->res.result() == boost::beast::http::status::ok)
211a0ec28b6SAdrian Ambrożewicz             {
212a0ec28b6SAdrian Ambrożewicz                 for (auto& sensor : *metadata)
213a0ec28b6SAdrian Ambrożewicz                 {
214a0ec28b6SAdrian Ambrożewicz                     map.insert(std::make_pair(sensor.uri + sensor.valueKey,
215a0ec28b6SAdrian Ambrożewicz                                               sensor.dbusPath));
216a0ec28b6SAdrian Ambrożewicz                 }
217a0ec28b6SAdrian Ambrożewicz             }
2188d1b46d7Szhanghch05             dataComplete(asyncResp->res.result(), map);
219a0ec28b6SAdrian Ambrożewicz         }
22008777fb0SLewanczyk, Dawid     }
221588c3f0dSKowalski, Kamil 
222a0ec28b6SAdrian Ambrożewicz     void addMetadata(const nlohmann::json& sensorObject,
223a0ec28b6SAdrian Ambrożewicz                      const std::string& valueKey, const std::string& dbusPath)
224a0ec28b6SAdrian Ambrożewicz     {
225a0ec28b6SAdrian Ambrożewicz         if (metadata)
226a0ec28b6SAdrian Ambrożewicz         {
227a0ec28b6SAdrian Ambrożewicz             metadata->emplace_back(SensorData{sensorObject["Name"],
228a0ec28b6SAdrian Ambrożewicz                                               sensorObject["@odata.id"],
229a0ec28b6SAdrian Ambrożewicz                                               valueKey, dbusPath});
230a0ec28b6SAdrian Ambrożewicz         }
231a0ec28b6SAdrian Ambrożewicz     }
232a0ec28b6SAdrian Ambrożewicz 
233a0ec28b6SAdrian Ambrożewicz     void updateUri(const std::string& name, const std::string& uri)
234a0ec28b6SAdrian Ambrożewicz     {
235a0ec28b6SAdrian Ambrożewicz         if (metadata)
236a0ec28b6SAdrian Ambrożewicz         {
237a0ec28b6SAdrian Ambrożewicz             for (auto& sensor : *metadata)
238a0ec28b6SAdrian Ambrożewicz             {
239a0ec28b6SAdrian Ambrożewicz                 if (sensor.name == name)
240a0ec28b6SAdrian Ambrożewicz                 {
241a0ec28b6SAdrian Ambrożewicz                     sensor.uri = uri;
242a0ec28b6SAdrian Ambrożewicz                 }
243a0ec28b6SAdrian Ambrożewicz             }
244a0ec28b6SAdrian Ambrożewicz         }
245a0ec28b6SAdrian Ambrożewicz     }
246a0ec28b6SAdrian Ambrożewicz 
2478d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp> asyncResp;
248a0ec28b6SAdrian Ambrożewicz     const std::string chassisId;
24908777fb0SLewanczyk, Dawid     const std::vector<const char*> types;
250a0ec28b6SAdrian Ambrożewicz     const std::string chassisSubNode;
251a0ec28b6SAdrian Ambrożewicz 
252a0ec28b6SAdrian Ambrożewicz   private:
253a0ec28b6SAdrian Ambrożewicz     std::optional<std::vector<SensorData>> metadata;
254a0ec28b6SAdrian Ambrożewicz     DataCompleteCb dataComplete;
25508777fb0SLewanczyk, Dawid };
25608777fb0SLewanczyk, Dawid 
25708777fb0SLewanczyk, Dawid /**
258d500549bSAnthony Wilson  * Possible states for physical inventory leds
259d500549bSAnthony Wilson  */
260d500549bSAnthony Wilson enum class LedState
261d500549bSAnthony Wilson {
262d500549bSAnthony Wilson     OFF,
263d500549bSAnthony Wilson     ON,
264d500549bSAnthony Wilson     BLINK,
265d500549bSAnthony Wilson     UNKNOWN
266d500549bSAnthony Wilson };
267d500549bSAnthony Wilson 
268d500549bSAnthony Wilson /**
269adc4f0dbSShawn McCarney  * D-Bus inventory item associated with one or more sensors.
270adc4f0dbSShawn McCarney  */
271adc4f0dbSShawn McCarney class InventoryItem
272adc4f0dbSShawn McCarney {
273adc4f0dbSShawn McCarney   public:
274adc4f0dbSShawn McCarney     InventoryItem(const std::string& objPath) :
275adc4f0dbSShawn McCarney         objectPath(objPath), name(), isPresent(true), isFunctional(true),
27642cbe538SGunnar Mills         isPowerSupply(false), powerSupplyEfficiencyPercent(-1), manufacturer(),
27742cbe538SGunnar Mills         model(), partNumber(), serialNumber(), sensors(), ledObjectPath(""),
278d500549bSAnthony Wilson         ledState(LedState::UNKNOWN)
279adc4f0dbSShawn McCarney     {
280adc4f0dbSShawn McCarney         // Set inventory item name to last node of object path
28128aa8de5SGeorge Liu         sdbusplus::message::object_path path(objectPath);
28228aa8de5SGeorge Liu         name = path.filename();
28328aa8de5SGeorge Liu         if (name.empty())
284adc4f0dbSShawn McCarney         {
28528aa8de5SGeorge Liu             BMCWEB_LOG_ERROR << "Failed to find '/' in " << objectPath;
286adc4f0dbSShawn McCarney         }
287adc4f0dbSShawn McCarney     }
288adc4f0dbSShawn McCarney 
289adc4f0dbSShawn McCarney     std::string objectPath;
290adc4f0dbSShawn McCarney     std::string name;
291adc4f0dbSShawn McCarney     bool isPresent;
292adc4f0dbSShawn McCarney     bool isFunctional;
293adc4f0dbSShawn McCarney     bool isPowerSupply;
29442cbe538SGunnar Mills     int powerSupplyEfficiencyPercent;
295adc4f0dbSShawn McCarney     std::string manufacturer;
296adc4f0dbSShawn McCarney     std::string model;
297adc4f0dbSShawn McCarney     std::string partNumber;
298adc4f0dbSShawn McCarney     std::string serialNumber;
299adc4f0dbSShawn McCarney     std::set<std::string> sensors;
300d500549bSAnthony Wilson     std::string ledObjectPath;
301d500549bSAnthony Wilson     LedState ledState;
302adc4f0dbSShawn McCarney };
303adc4f0dbSShawn McCarney 
304adc4f0dbSShawn McCarney /**
305413961deSRichard Marian Thomaiyar  * @brief Get objects with connection necessary for sensors
306588c3f0dSKowalski, Kamil  * @param SensorsAsyncResp Pointer to object holding response data
30708777fb0SLewanczyk, Dawid  * @param sensorNames Sensors retrieved from chassis
30808777fb0SLewanczyk, Dawid  * @param callback Callback for processing gathered connections
30908777fb0SLewanczyk, Dawid  */
31008777fb0SLewanczyk, Dawid template <typename Callback>
311413961deSRichard Marian Thomaiyar void getObjectsWithConnection(
31281ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
313b5a76932SEd Tanous     const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames,
3141abe55efSEd Tanous     Callback&& callback)
3151abe55efSEd Tanous {
316413961deSRichard Marian Thomaiyar     BMCWEB_LOG_DEBUG << "getObjectsWithConnection enter";
31703b5bae3SJames Feist     const std::string path = "/xyz/openbmc_project/sensors";
31808777fb0SLewanczyk, Dawid     const std::array<std::string, 1> interfaces = {
31908777fb0SLewanczyk, Dawid         "xyz.openbmc_project.Sensor.Value"};
32008777fb0SLewanczyk, Dawid 
32108777fb0SLewanczyk, Dawid     // Response handler for parsing objects subtree
32281ce609eSEd Tanous     auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp,
3231abe55efSEd Tanous                         sensorNames](const boost::system::error_code ec,
3241abe55efSEd Tanous                                      const GetSubTreeType& subtree) {
325413961deSRichard Marian Thomaiyar         BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler enter";
3261abe55efSEd Tanous         if (ec)
3271abe55efSEd Tanous         {
3288d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
329413961deSRichard Marian Thomaiyar             BMCWEB_LOG_ERROR
330413961deSRichard Marian Thomaiyar                 << "getObjectsWithConnection resp_handler: Dbus error " << ec;
33108777fb0SLewanczyk, Dawid             return;
33208777fb0SLewanczyk, Dawid         }
33308777fb0SLewanczyk, Dawid 
33455c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "Found " << subtree.size() << " subtrees";
33508777fb0SLewanczyk, Dawid 
33608777fb0SLewanczyk, Dawid         // Make unique list of connections only for requested sensor types and
33708777fb0SLewanczyk, Dawid         // found in the chassis
33808777fb0SLewanczyk, Dawid         boost::container::flat_set<std::string> connections;
339413961deSRichard Marian Thomaiyar         std::set<std::pair<std::string, std::string>> objectsWithConnection;
3401abe55efSEd Tanous         // Intrinsic to avoid malloc.  Most systems will have < 8 sensor
3411abe55efSEd Tanous         // producers
34208777fb0SLewanczyk, Dawid         connections.reserve(8);
34308777fb0SLewanczyk, Dawid 
34449c53ac9SJohnathan Mantey         BMCWEB_LOG_DEBUG << "sensorNames list count: " << sensorNames->size();
34549c53ac9SJohnathan Mantey         for (const std::string& tsensor : *sensorNames)
3461abe55efSEd Tanous         {
34755c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "Sensor to find: " << tsensor;
34808777fb0SLewanczyk, Dawid         }
34908777fb0SLewanczyk, Dawid 
35008777fb0SLewanczyk, Dawid         for (const std::pair<
35108777fb0SLewanczyk, Dawid                  std::string,
35208777fb0SLewanczyk, Dawid                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
3531abe55efSEd Tanous                  object : subtree)
3541abe55efSEd Tanous         {
35549c53ac9SJohnathan Mantey             if (sensorNames->find(object.first) != sensorNames->end())
3561abe55efSEd Tanous             {
35749c53ac9SJohnathan Mantey                 for (const std::pair<std::string, std::vector<std::string>>&
3581abe55efSEd Tanous                          objData : object.second)
3591abe55efSEd Tanous                 {
36049c53ac9SJohnathan Mantey                     BMCWEB_LOG_DEBUG << "Adding connection: " << objData.first;
36108777fb0SLewanczyk, Dawid                     connections.insert(objData.first);
362de629b6eSShawn McCarney                     objectsWithConnection.insert(
363de629b6eSShawn McCarney                         std::make_pair(object.first, objData.first));
36408777fb0SLewanczyk, Dawid                 }
36508777fb0SLewanczyk, Dawid             }
36608777fb0SLewanczyk, Dawid         }
36755c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "Found " << connections.size() << " connections";
368413961deSRichard Marian Thomaiyar         callback(std::move(connections), std::move(objectsWithConnection));
369413961deSRichard Marian Thomaiyar         BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler exit";
37008777fb0SLewanczyk, Dawid     };
37108777fb0SLewanczyk, Dawid     // Make call to ObjectMapper to find all sensors objects
37255c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
37355c7b7a2SEd Tanous         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
3741abe55efSEd Tanous         "/xyz/openbmc_project/object_mapper",
3751abe55efSEd Tanous         "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 2, interfaces);
376413961deSRichard Marian Thomaiyar     BMCWEB_LOG_DEBUG << "getObjectsWithConnection exit";
377413961deSRichard Marian Thomaiyar }
378413961deSRichard Marian Thomaiyar 
379413961deSRichard Marian Thomaiyar /**
380413961deSRichard Marian Thomaiyar  * @brief Create connections necessary for sensors
381413961deSRichard Marian Thomaiyar  * @param SensorsAsyncResp Pointer to object holding response data
382413961deSRichard Marian Thomaiyar  * @param sensorNames Sensors retrieved from chassis
383413961deSRichard Marian Thomaiyar  * @param callback Callback for processing gathered connections
384413961deSRichard Marian Thomaiyar  */
385413961deSRichard Marian Thomaiyar template <typename Callback>
38649c53ac9SJohnathan Mantey void getConnections(
38781ce609eSEd Tanous     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
38849c53ac9SJohnathan Mantey     const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
389413961deSRichard Marian Thomaiyar     Callback&& callback)
390413961deSRichard Marian Thomaiyar {
391413961deSRichard Marian Thomaiyar     auto objectsWithConnectionCb =
392413961deSRichard Marian Thomaiyar         [callback](const boost::container::flat_set<std::string>& connections,
393413961deSRichard Marian Thomaiyar                    const std::set<std::pair<std::string, std::string>>&
3943174e4dfSEd Tanous                    /*objectsWithConnection*/) { callback(connections); };
39581ce609eSEd Tanous     getObjectsWithConnection(sensorsAsyncResp, sensorNames,
396413961deSRichard Marian Thomaiyar                              std::move(objectsWithConnectionCb));
39708777fb0SLewanczyk, Dawid }
39808777fb0SLewanczyk, Dawid 
39908777fb0SLewanczyk, Dawid /**
40049c53ac9SJohnathan Mantey  * @brief Shrinks the list of sensors for processing
40149c53ac9SJohnathan Mantey  * @param SensorsAysncResp  The class holding the Redfish response
40249c53ac9SJohnathan Mantey  * @param allSensors  A list of all the sensors associated to the
40349c53ac9SJohnathan Mantey  * chassis element (i.e. baseboard, front panel, etc...)
40449c53ac9SJohnathan Mantey  * @param activeSensors A list that is a reduction of the incoming
40549c53ac9SJohnathan Mantey  * allSensors list.  Eliminate Thermal sensors when a Power request is
40649c53ac9SJohnathan Mantey  * made, and eliminate Power sensors when a Thermal request is made.
40749c53ac9SJohnathan Mantey  */
40823a21a1cSEd Tanous inline void reduceSensorList(
40981ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
41049c53ac9SJohnathan Mantey     const std::vector<std::string>* allSensors,
411b5a76932SEd Tanous     const std::shared_ptr<boost::container::flat_set<std::string>>&
412b5a76932SEd Tanous         activeSensors)
41349c53ac9SJohnathan Mantey {
41481ce609eSEd Tanous     if (sensorsAsyncResp == nullptr)
41549c53ac9SJohnathan Mantey     {
41649c53ac9SJohnathan Mantey         return;
41749c53ac9SJohnathan Mantey     }
41849c53ac9SJohnathan Mantey     if ((allSensors == nullptr) || (activeSensors == nullptr))
41949c53ac9SJohnathan Mantey     {
42049c53ac9SJohnathan Mantey         messages::resourceNotFound(
4218d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res, sensorsAsyncResp->chassisSubNode,
42281ce609eSEd Tanous             sensorsAsyncResp->chassisSubNode == sensors::node::thermal
423a0ec28b6SAdrian Ambrożewicz                 ? "Temperatures"
42449c53ac9SJohnathan Mantey                 : "Voltages");
42549c53ac9SJohnathan Mantey 
42649c53ac9SJohnathan Mantey         return;
42749c53ac9SJohnathan Mantey     }
42849c53ac9SJohnathan Mantey     if (allSensors->empty())
42949c53ac9SJohnathan Mantey     {
43049c53ac9SJohnathan Mantey         // Nothing to do, the activeSensors object is also empty
43149c53ac9SJohnathan Mantey         return;
43249c53ac9SJohnathan Mantey     }
43349c53ac9SJohnathan Mantey 
43481ce609eSEd Tanous     for (const char* type : sensorsAsyncResp->types)
43549c53ac9SJohnathan Mantey     {
43649c53ac9SJohnathan Mantey         for (const std::string& sensor : *allSensors)
43749c53ac9SJohnathan Mantey         {
43849c53ac9SJohnathan Mantey             if (boost::starts_with(sensor, type))
43949c53ac9SJohnathan Mantey             {
44049c53ac9SJohnathan Mantey                 activeSensors->emplace(sensor);
44149c53ac9SJohnathan Mantey             }
44249c53ac9SJohnathan Mantey         }
44349c53ac9SJohnathan Mantey     }
44449c53ac9SJohnathan Mantey }
44549c53ac9SJohnathan Mantey 
44649c53ac9SJohnathan Mantey /**
4474bb3dc34SCarol Wang  * @brief Retrieves valid chassis path
4484bb3dc34SCarol Wang  * @param asyncResp   Pointer to object holding response data
4494bb3dc34SCarol Wang  * @param callback  Callback for next step to get valid chassis path
4504bb3dc34SCarol Wang  */
4514bb3dc34SCarol Wang template <typename Callback>
452b5a76932SEd Tanous void getValidChassisPath(const std::shared_ptr<SensorsAsyncResp>& asyncResp,
4534bb3dc34SCarol Wang                          Callback&& callback)
4544bb3dc34SCarol Wang {
4554bb3dc34SCarol Wang     BMCWEB_LOG_DEBUG << "checkChassisId enter";
4564bb3dc34SCarol Wang     const std::array<const char*, 2> interfaces = {
4574bb3dc34SCarol Wang         "xyz.openbmc_project.Inventory.Item.Board",
4584bb3dc34SCarol Wang         "xyz.openbmc_project.Inventory.Item.Chassis"};
4594bb3dc34SCarol Wang 
4604bb3dc34SCarol Wang     auto respHandler =
4614bb3dc34SCarol Wang         [callback{std::move(callback)},
4624bb3dc34SCarol Wang          asyncResp](const boost::system::error_code ec,
4634bb3dc34SCarol Wang                     const std::vector<std::string>& chassisPaths) mutable {
4644bb3dc34SCarol Wang             BMCWEB_LOG_DEBUG << "getValidChassisPath respHandler enter";
4654bb3dc34SCarol Wang             if (ec)
4664bb3dc34SCarol Wang             {
4674bb3dc34SCarol Wang                 BMCWEB_LOG_ERROR
4684bb3dc34SCarol Wang                     << "getValidChassisPath respHandler DBUS error: " << ec;
4698d1b46d7Szhanghch05                 messages::internalError(asyncResp->asyncResp->res);
4704bb3dc34SCarol Wang                 return;
4714bb3dc34SCarol Wang             }
4724bb3dc34SCarol Wang 
4734bb3dc34SCarol Wang             std::optional<std::string> chassisPath;
4744bb3dc34SCarol Wang             std::string chassisName;
4754bb3dc34SCarol Wang             for (const std::string& chassis : chassisPaths)
4764bb3dc34SCarol Wang             {
47728aa8de5SGeorge Liu                 sdbusplus::message::object_path path(chassis);
47828aa8de5SGeorge Liu                 chassisName = path.filename();
47928aa8de5SGeorge Liu                 if (chassisName.empty())
4804bb3dc34SCarol Wang                 {
4814bb3dc34SCarol Wang                     BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
4824bb3dc34SCarol Wang                     continue;
4834bb3dc34SCarol Wang                 }
4844bb3dc34SCarol Wang                 if (chassisName == asyncResp->chassisId)
4854bb3dc34SCarol Wang                 {
4864bb3dc34SCarol Wang                     chassisPath = chassis;
4874bb3dc34SCarol Wang                     break;
4884bb3dc34SCarol Wang                 }
4894bb3dc34SCarol Wang             }
4904bb3dc34SCarol Wang             callback(chassisPath);
4914bb3dc34SCarol Wang         };
4924bb3dc34SCarol Wang 
4934bb3dc34SCarol Wang     // Get the Chassis Collection
4944bb3dc34SCarol Wang     crow::connections::systemBus->async_method_call(
4954bb3dc34SCarol Wang         respHandler, "xyz.openbmc_project.ObjectMapper",
4964bb3dc34SCarol Wang         "/xyz/openbmc_project/object_mapper",
4974bb3dc34SCarol Wang         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
4984bb3dc34SCarol Wang         "/xyz/openbmc_project/inventory", 0, interfaces);
4994bb3dc34SCarol Wang     BMCWEB_LOG_DEBUG << "checkChassisId exit";
5004bb3dc34SCarol Wang }
5014bb3dc34SCarol Wang 
5024bb3dc34SCarol Wang /**
50308777fb0SLewanczyk, Dawid  * @brief Retrieves requested chassis sensors and redundancy data from DBus .
504588c3f0dSKowalski, Kamil  * @param SensorsAsyncResp   Pointer to object holding response data
50508777fb0SLewanczyk, Dawid  * @param callback  Callback for next step in gathered sensor processing
50608777fb0SLewanczyk, Dawid  */
50708777fb0SLewanczyk, Dawid template <typename Callback>
508b5a76932SEd Tanous void getChassis(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
5091abe55efSEd Tanous                 Callback&& callback)
5101abe55efSEd Tanous {
51155c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "getChassis enter";
512adc4f0dbSShawn McCarney     const std::array<const char*, 2> interfaces = {
51349c53ac9SJohnathan Mantey         "xyz.openbmc_project.Inventory.Item.Board",
514adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Item.Chassis"};
51549c53ac9SJohnathan Mantey     auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp](
51649c53ac9SJohnathan Mantey                            const boost::system::error_code ec,
51749c53ac9SJohnathan Mantey                            const std::vector<std::string>& chassisPaths) {
51855c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "getChassis respHandler enter";
5191abe55efSEd Tanous         if (ec)
5201abe55efSEd Tanous         {
52155c7b7a2SEd Tanous             BMCWEB_LOG_ERROR << "getChassis respHandler DBUS error: " << ec;
5228d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
52308777fb0SLewanczyk, Dawid             return;
52408777fb0SLewanczyk, Dawid         }
52508777fb0SLewanczyk, Dawid 
52649c53ac9SJohnathan Mantey         const std::string* chassisPath = nullptr;
52749c53ac9SJohnathan Mantey         std::string chassisName;
52849c53ac9SJohnathan Mantey         for (const std::string& chassis : chassisPaths)
5291abe55efSEd Tanous         {
53028aa8de5SGeorge Liu             sdbusplus::message::object_path path(chassis);
53128aa8de5SGeorge Liu             chassisName = path.filename();
53228aa8de5SGeorge Liu             if (chassisName.empty())
5331abe55efSEd Tanous             {
53449c53ac9SJohnathan Mantey                 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
535daf36e2eSEd Tanous                 continue;
536daf36e2eSEd Tanous             }
53749c53ac9SJohnathan Mantey             if (chassisName == sensorsAsyncResp->chassisId)
5381abe55efSEd Tanous             {
53949c53ac9SJohnathan Mantey                 chassisPath = &chassis;
54049c53ac9SJohnathan Mantey                 break;
541daf36e2eSEd Tanous             }
54249c53ac9SJohnathan Mantey         }
54349c53ac9SJohnathan Mantey         if (chassisPath == nullptr)
5441abe55efSEd Tanous         {
5458d1b46d7Szhanghch05             messages::resourceNotFound(sensorsAsyncResp->asyncResp->res,
5468d1b46d7Szhanghch05                                        "Chassis", sensorsAsyncResp->chassisId);
54749c53ac9SJohnathan Mantey             return;
5481abe55efSEd Tanous         }
54908777fb0SLewanczyk, Dawid 
55049c53ac9SJohnathan Mantey         const std::string& chassisSubNode = sensorsAsyncResp->chassisSubNode;
551a0ec28b6SAdrian Ambrożewicz         if (chassisSubNode == sensors::node::power)
55249c53ac9SJohnathan Mantey         {
5538d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] =
55449c53ac9SJohnathan Mantey                 "#Power.v1_5_2.Power";
55549c53ac9SJohnathan Mantey         }
556a0ec28b6SAdrian Ambrożewicz         else if (chassisSubNode == sensors::node::thermal)
55749c53ac9SJohnathan Mantey         {
5588d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] =
55949c53ac9SJohnathan Mantey                 "#Thermal.v1_4_0.Thermal";
5608d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["Fans"] =
5618d1b46d7Szhanghch05                 nlohmann::json::array();
5628d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["Temperatures"] =
5634f9a2130SJennifer Lee                 nlohmann::json::array();
56449c53ac9SJohnathan Mantey         }
565a0ec28b6SAdrian Ambrożewicz         else if (chassisSubNode == sensors::node::sensors)
56695a3ecadSAnthony Wilson         {
5678d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] =
56895a3ecadSAnthony Wilson                 "#SensorCollection.SensorCollection";
5698d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["Description"] =
57095a3ecadSAnthony Wilson                 "Collection of Sensors for this Chassis";
5718d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["Members"] =
57295a3ecadSAnthony Wilson                 nlohmann::json::array();
5738d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["Members@odata.count"] =
5748d1b46d7Szhanghch05                 0;
57595a3ecadSAnthony Wilson         }
57695a3ecadSAnthony Wilson 
577a0ec28b6SAdrian Ambrożewicz         if (chassisSubNode != sensors::node::sensors)
57895a3ecadSAnthony Wilson         {
5798d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["Id"] = chassisSubNode;
58095a3ecadSAnthony Wilson         }
58195a3ecadSAnthony Wilson 
5828d1b46d7Szhanghch05         sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
58349c53ac9SJohnathan Mantey             "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId + "/" +
58449c53ac9SJohnathan Mantey             chassisSubNode;
5858d1b46d7Szhanghch05         sensorsAsyncResp->asyncResp->res.jsonValue["Name"] = chassisSubNode;
5868fb49dd6SShawn McCarney         // Get the list of all sensors for this Chassis element
5878fb49dd6SShawn McCarney         std::string sensorPath = *chassisPath + "/all_sensors";
58855c7b7a2SEd Tanous         crow::connections::systemBus->async_method_call(
58949c53ac9SJohnathan Mantey             [sensorsAsyncResp, callback{std::move(callback)}](
590271584abSEd Tanous                 const boost::system::error_code& e,
59149c53ac9SJohnathan Mantey                 const std::variant<std::vector<std::string>>&
59249c53ac9SJohnathan Mantey                     variantEndpoints) {
593271584abSEd Tanous                 if (e)
59449c53ac9SJohnathan Mantey                 {
595271584abSEd Tanous                     if (e.value() != EBADR)
59649c53ac9SJohnathan Mantey                     {
5978d1b46d7Szhanghch05                         messages::internalError(
5988d1b46d7Szhanghch05                             sensorsAsyncResp->asyncResp->res);
59949c53ac9SJohnathan Mantey                         return;
60049c53ac9SJohnathan Mantey                     }
60149c53ac9SJohnathan Mantey                 }
60249c53ac9SJohnathan Mantey                 const std::vector<std::string>* nodeSensorList =
60349c53ac9SJohnathan Mantey                     std::get_if<std::vector<std::string>>(&(variantEndpoints));
60449c53ac9SJohnathan Mantey                 if (nodeSensorList == nullptr)
60549c53ac9SJohnathan Mantey                 {
60649c53ac9SJohnathan Mantey                     messages::resourceNotFound(
6078d1b46d7Szhanghch05                         sensorsAsyncResp->asyncResp->res,
6088d1b46d7Szhanghch05                         sensorsAsyncResp->chassisSubNode,
609a0ec28b6SAdrian Ambrożewicz                         sensorsAsyncResp->chassisSubNode ==
610a0ec28b6SAdrian Ambrożewicz                                 sensors::node::thermal
61149c53ac9SJohnathan Mantey                             ? "Temperatures"
612a0ec28b6SAdrian Ambrożewicz                         : sensorsAsyncResp->chassisSubNode ==
613a0ec28b6SAdrian Ambrożewicz                                 sensors::node::power
61495a3ecadSAnthony Wilson                             ? "Voltages"
61595a3ecadSAnthony Wilson                             : "Sensors");
61649c53ac9SJohnathan Mantey                     return;
61749c53ac9SJohnathan Mantey                 }
61849c53ac9SJohnathan Mantey                 const std::shared_ptr<boost::container::flat_set<std::string>>
61949c53ac9SJohnathan Mantey                     culledSensorList = std::make_shared<
62049c53ac9SJohnathan Mantey                         boost::container::flat_set<std::string>>();
62149c53ac9SJohnathan Mantey                 reduceSensorList(sensorsAsyncResp, nodeSensorList,
62249c53ac9SJohnathan Mantey                                  culledSensorList);
62349c53ac9SJohnathan Mantey                 callback(culledSensorList);
62449c53ac9SJohnathan Mantey             },
62549c53ac9SJohnathan Mantey             "xyz.openbmc_project.ObjectMapper", sensorPath,
62649c53ac9SJohnathan Mantey             "org.freedesktop.DBus.Properties", "Get",
62749c53ac9SJohnathan Mantey             "xyz.openbmc_project.Association", "endpoints");
62849c53ac9SJohnathan Mantey     };
62949c53ac9SJohnathan Mantey 
63049c53ac9SJohnathan Mantey     // Get the Chassis Collection
63149c53ac9SJohnathan Mantey     crow::connections::systemBus->async_method_call(
63249c53ac9SJohnathan Mantey         respHandler, "xyz.openbmc_project.ObjectMapper",
63349c53ac9SJohnathan Mantey         "/xyz/openbmc_project/object_mapper",
63449c53ac9SJohnathan Mantey         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
635271584abSEd Tanous         "/xyz/openbmc_project/inventory", 0, interfaces);
63655c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "getChassis exit";
63708777fb0SLewanczyk, Dawid }
63808777fb0SLewanczyk, Dawid 
63908777fb0SLewanczyk, Dawid /**
640de629b6eSShawn McCarney  * @brief Finds all DBus object paths that implement ObjectManager.
641de629b6eSShawn McCarney  *
642de629b6eSShawn McCarney  * Creates a mapping from the associated connection name to the object path.
643de629b6eSShawn McCarney  *
644de629b6eSShawn McCarney  * Finds the object paths asynchronously.  Invokes callback when information has
645de629b6eSShawn McCarney  * been obtained.
646de629b6eSShawn McCarney  *
647de629b6eSShawn McCarney  * The callback must have the following signature:
648de629b6eSShawn McCarney  *   @code
6498fb49dd6SShawn McCarney  *   callback(std::shared_ptr<boost::container::flat_map<std::string,
6508fb49dd6SShawn McCarney  *                std::string>> objectMgrPaths)
651de629b6eSShawn McCarney  *   @endcode
652de629b6eSShawn McCarney  *
65349c53ac9SJohnathan Mantey  * @param sensorsAsyncResp Pointer to object holding response data.
654de629b6eSShawn McCarney  * @param callback Callback to invoke when object paths obtained.
655de629b6eSShawn McCarney  */
656de629b6eSShawn McCarney template <typename Callback>
657b5a76932SEd Tanous void getObjectManagerPaths(
65881ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
659de629b6eSShawn McCarney     Callback&& callback)
660de629b6eSShawn McCarney {
661de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getObjectManagerPaths enter";
662de629b6eSShawn McCarney     const std::array<std::string, 1> interfaces = {
663de629b6eSShawn McCarney         "org.freedesktop.DBus.ObjectManager"};
664de629b6eSShawn McCarney 
665de629b6eSShawn McCarney     // Response handler for GetSubTree DBus method
666de629b6eSShawn McCarney     auto respHandler = [callback{std::move(callback)},
66781ce609eSEd Tanous                         sensorsAsyncResp](const boost::system::error_code ec,
668de629b6eSShawn McCarney                                           const GetSubTreeType& subtree) {
669de629b6eSShawn McCarney         BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler enter";
670de629b6eSShawn McCarney         if (ec)
671de629b6eSShawn McCarney         {
6728d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
673de629b6eSShawn McCarney             BMCWEB_LOG_ERROR << "getObjectManagerPaths respHandler: DBus error "
674de629b6eSShawn McCarney                              << ec;
675de629b6eSShawn McCarney             return;
676de629b6eSShawn McCarney         }
677de629b6eSShawn McCarney 
678de629b6eSShawn McCarney         // Loop over returned object paths
6798fb49dd6SShawn McCarney         std::shared_ptr<boost::container::flat_map<std::string, std::string>>
6808fb49dd6SShawn McCarney             objectMgrPaths = std::make_shared<
6818fb49dd6SShawn McCarney                 boost::container::flat_map<std::string, std::string>>();
682de629b6eSShawn McCarney         for (const std::pair<
683de629b6eSShawn McCarney                  std::string,
684de629b6eSShawn McCarney                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
685de629b6eSShawn McCarney                  object : subtree)
686de629b6eSShawn McCarney         {
687de629b6eSShawn McCarney             // Loop over connections for current object path
688de629b6eSShawn McCarney             const std::string& objectPath = object.first;
689de629b6eSShawn McCarney             for (const std::pair<std::string, std::vector<std::string>>&
690de629b6eSShawn McCarney                      objData : object.second)
691de629b6eSShawn McCarney             {
692de629b6eSShawn McCarney                 // Add mapping from connection to object path
693de629b6eSShawn McCarney                 const std::string& connection = objData.first;
6948fb49dd6SShawn McCarney                 (*objectMgrPaths)[connection] = objectPath;
695de629b6eSShawn McCarney                 BMCWEB_LOG_DEBUG << "Added mapping " << connection << " -> "
696de629b6eSShawn McCarney                                  << objectPath;
697de629b6eSShawn McCarney             }
698de629b6eSShawn McCarney         }
6998fb49dd6SShawn McCarney         callback(objectMgrPaths);
700de629b6eSShawn McCarney         BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler exit";
701de629b6eSShawn McCarney     };
702de629b6eSShawn McCarney 
703de629b6eSShawn McCarney     // Query mapper for all DBus object paths that implement ObjectManager
704de629b6eSShawn McCarney     crow::connections::systemBus->async_method_call(
705de629b6eSShawn McCarney         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
706de629b6eSShawn McCarney         "/xyz/openbmc_project/object_mapper",
707271584abSEd Tanous         "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0, interfaces);
708de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getObjectManagerPaths exit";
709de629b6eSShawn McCarney }
710de629b6eSShawn McCarney 
711de629b6eSShawn McCarney /**
712adc4f0dbSShawn McCarney  * @brief Returns the Redfish State value for the specified inventory item.
713adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with a sensor.
714adc4f0dbSShawn McCarney  * @return State value for inventory item.
71534dd179eSJames Feist  */
71623a21a1cSEd Tanous inline std::string getState(const InventoryItem* inventoryItem)
717adc4f0dbSShawn McCarney {
718adc4f0dbSShawn McCarney     if ((inventoryItem != nullptr) && !(inventoryItem->isPresent))
719adc4f0dbSShawn McCarney     {
720adc4f0dbSShawn McCarney         return "Absent";
721adc4f0dbSShawn McCarney     }
72234dd179eSJames Feist 
723adc4f0dbSShawn McCarney     return "Enabled";
724adc4f0dbSShawn McCarney }
725adc4f0dbSShawn McCarney 
726adc4f0dbSShawn McCarney /**
727adc4f0dbSShawn McCarney  * @brief Returns the Redfish Health value for the specified sensor.
728adc4f0dbSShawn McCarney  * @param sensorJson Sensor JSON object.
729adc4f0dbSShawn McCarney  * @param interfacesDict Map of all sensor interfaces.
730adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with the sensor.  Will
731adc4f0dbSShawn McCarney  * be nullptr if no associated inventory item was found.
732adc4f0dbSShawn McCarney  * @return Health value for sensor.
733adc4f0dbSShawn McCarney  */
73423a21a1cSEd Tanous inline std::string getHealth(
735adc4f0dbSShawn McCarney     nlohmann::json& sensorJson,
73634dd179eSJames Feist     const boost::container::flat_map<
73734dd179eSJames Feist         std::string, boost::container::flat_map<std::string, SensorVariant>>&
738adc4f0dbSShawn McCarney         interfacesDict,
739adc4f0dbSShawn McCarney     const InventoryItem* inventoryItem)
74034dd179eSJames Feist {
741adc4f0dbSShawn McCarney     // Get current health value (if any) in the sensor JSON object.  Some JSON
742adc4f0dbSShawn McCarney     // objects contain multiple sensors (such as PowerSupplies).  We want to set
743adc4f0dbSShawn McCarney     // the overall health to be the most severe of any of the sensors.
744adc4f0dbSShawn McCarney     std::string currentHealth;
745adc4f0dbSShawn McCarney     auto statusIt = sensorJson.find("Status");
746adc4f0dbSShawn McCarney     if (statusIt != sensorJson.end())
747adc4f0dbSShawn McCarney     {
748adc4f0dbSShawn McCarney         auto healthIt = statusIt->find("Health");
749adc4f0dbSShawn McCarney         if (healthIt != statusIt->end())
750adc4f0dbSShawn McCarney         {
751adc4f0dbSShawn McCarney             std::string* health = healthIt->get_ptr<std::string*>();
752adc4f0dbSShawn McCarney             if (health != nullptr)
753adc4f0dbSShawn McCarney             {
754adc4f0dbSShawn McCarney                 currentHealth = *health;
755adc4f0dbSShawn McCarney             }
756adc4f0dbSShawn McCarney         }
757adc4f0dbSShawn McCarney     }
758adc4f0dbSShawn McCarney 
759adc4f0dbSShawn McCarney     // If current health in JSON object is already Critical, return that.  This
760adc4f0dbSShawn McCarney     // should override the sensor health, which might be less severe.
761adc4f0dbSShawn McCarney     if (currentHealth == "Critical")
762adc4f0dbSShawn McCarney     {
763adc4f0dbSShawn McCarney         return "Critical";
764adc4f0dbSShawn McCarney     }
765adc4f0dbSShawn McCarney 
766adc4f0dbSShawn McCarney     // Check if sensor has critical threshold alarm
76734dd179eSJames Feist     auto criticalThresholdIt =
76834dd179eSJames Feist         interfacesDict.find("xyz.openbmc_project.Sensor.Threshold.Critical");
76934dd179eSJames Feist     if (criticalThresholdIt != interfacesDict.end())
77034dd179eSJames Feist     {
77134dd179eSJames Feist         auto thresholdHighIt =
77234dd179eSJames Feist             criticalThresholdIt->second.find("CriticalAlarmHigh");
77334dd179eSJames Feist         auto thresholdLowIt =
77434dd179eSJames Feist             criticalThresholdIt->second.find("CriticalAlarmLow");
77534dd179eSJames Feist         if (thresholdHighIt != criticalThresholdIt->second.end())
77634dd179eSJames Feist         {
77734dd179eSJames Feist             const bool* asserted = std::get_if<bool>(&thresholdHighIt->second);
77834dd179eSJames Feist             if (asserted == nullptr)
77934dd179eSJames Feist             {
78034dd179eSJames Feist                 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
78134dd179eSJames Feist             }
78234dd179eSJames Feist             else if (*asserted)
78334dd179eSJames Feist             {
78434dd179eSJames Feist                 return "Critical";
78534dd179eSJames Feist             }
78634dd179eSJames Feist         }
78734dd179eSJames Feist         if (thresholdLowIt != criticalThresholdIt->second.end())
78834dd179eSJames Feist         {
78934dd179eSJames Feist             const bool* asserted = std::get_if<bool>(&thresholdLowIt->second);
79034dd179eSJames Feist             if (asserted == nullptr)
79134dd179eSJames Feist             {
79234dd179eSJames Feist                 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
79334dd179eSJames Feist             }
79434dd179eSJames Feist             else if (*asserted)
79534dd179eSJames Feist             {
79634dd179eSJames Feist                 return "Critical";
79734dd179eSJames Feist             }
79834dd179eSJames Feist         }
79934dd179eSJames Feist     }
80034dd179eSJames Feist 
801adc4f0dbSShawn McCarney     // Check if associated inventory item is not functional
802adc4f0dbSShawn McCarney     if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional))
803adc4f0dbSShawn McCarney     {
804adc4f0dbSShawn McCarney         return "Critical";
805adc4f0dbSShawn McCarney     }
806adc4f0dbSShawn McCarney 
807adc4f0dbSShawn McCarney     // If current health in JSON object is already Warning, return that.  This
808adc4f0dbSShawn McCarney     // should override the sensor status, which might be less severe.
809adc4f0dbSShawn McCarney     if (currentHealth == "Warning")
810adc4f0dbSShawn McCarney     {
811adc4f0dbSShawn McCarney         return "Warning";
812adc4f0dbSShawn McCarney     }
813adc4f0dbSShawn McCarney 
814adc4f0dbSShawn McCarney     // Check if sensor has warning threshold alarm
81534dd179eSJames Feist     auto warningThresholdIt =
81634dd179eSJames Feist         interfacesDict.find("xyz.openbmc_project.Sensor.Threshold.Warning");
81734dd179eSJames Feist     if (warningThresholdIt != interfacesDict.end())
81834dd179eSJames Feist     {
81934dd179eSJames Feist         auto thresholdHighIt =
82034dd179eSJames Feist             warningThresholdIt->second.find("WarningAlarmHigh");
82134dd179eSJames Feist         auto thresholdLowIt =
82234dd179eSJames Feist             warningThresholdIt->second.find("WarningAlarmLow");
82334dd179eSJames Feist         if (thresholdHighIt != warningThresholdIt->second.end())
82434dd179eSJames Feist         {
82534dd179eSJames Feist             const bool* asserted = std::get_if<bool>(&thresholdHighIt->second);
82634dd179eSJames Feist             if (asserted == nullptr)
82734dd179eSJames Feist             {
82834dd179eSJames Feist                 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
82934dd179eSJames Feist             }
83034dd179eSJames Feist             else if (*asserted)
83134dd179eSJames Feist             {
83234dd179eSJames Feist                 return "Warning";
83334dd179eSJames Feist             }
83434dd179eSJames Feist         }
83534dd179eSJames Feist         if (thresholdLowIt != warningThresholdIt->second.end())
83634dd179eSJames Feist         {
83734dd179eSJames Feist             const bool* asserted = std::get_if<bool>(&thresholdLowIt->second);
83834dd179eSJames Feist             if (asserted == nullptr)
83934dd179eSJames Feist             {
84034dd179eSJames Feist                 BMCWEB_LOG_ERROR << "Illegal sensor threshold";
84134dd179eSJames Feist             }
84234dd179eSJames Feist             else if (*asserted)
84334dd179eSJames Feist             {
84434dd179eSJames Feist                 return "Warning";
84534dd179eSJames Feist             }
84634dd179eSJames Feist         }
84734dd179eSJames Feist     }
848adc4f0dbSShawn McCarney 
84934dd179eSJames Feist     return "OK";
85034dd179eSJames Feist }
85134dd179eSJames Feist 
85223a21a1cSEd Tanous inline void setLedState(nlohmann::json& sensorJson,
853d500549bSAnthony Wilson                         const InventoryItem* inventoryItem)
854d500549bSAnthony Wilson {
855d500549bSAnthony Wilson     if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty())
856d500549bSAnthony Wilson     {
857d500549bSAnthony Wilson         switch (inventoryItem->ledState)
858d500549bSAnthony Wilson         {
859d500549bSAnthony Wilson             case LedState::OFF:
860d500549bSAnthony Wilson                 sensorJson["IndicatorLED"] = "Off";
861d500549bSAnthony Wilson                 break;
862d500549bSAnthony Wilson             case LedState::ON:
863d500549bSAnthony Wilson                 sensorJson["IndicatorLED"] = "Lit";
864d500549bSAnthony Wilson                 break;
865d500549bSAnthony Wilson             case LedState::BLINK:
866d500549bSAnthony Wilson                 sensorJson["IndicatorLED"] = "Blinking";
867d500549bSAnthony Wilson                 break;
86823a21a1cSEd Tanous             case LedState::UNKNOWN:
869d500549bSAnthony Wilson                 break;
870d500549bSAnthony Wilson         }
871d500549bSAnthony Wilson     }
872d500549bSAnthony Wilson }
873d500549bSAnthony Wilson 
87434dd179eSJames Feist /**
87508777fb0SLewanczyk, Dawid  * @brief Builds a json sensor representation of a sensor.
87608777fb0SLewanczyk, Dawid  * @param sensorName  The name of the sensor to be built
877274fad5aSGunnar Mills  * @param sensorType  The type (temperature, fan_tach, etc) of the sensor to
87808777fb0SLewanczyk, Dawid  * build
879a0ec28b6SAdrian Ambrożewicz  * @param sensorsAsyncResp  Sensor metadata
88008777fb0SLewanczyk, Dawid  * @param interfacesDict  A dictionary of the interfaces and properties of said
88108777fb0SLewanczyk, Dawid  * interfaces to be built from
88208777fb0SLewanczyk, Dawid  * @param sensor_json  The json object to fill
883adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with the sensor.  Will
884adc4f0dbSShawn McCarney  * be nullptr if no associated inventory item was found.
88508777fb0SLewanczyk, Dawid  */
88623a21a1cSEd Tanous inline void objectInterfacesToJson(
88708777fb0SLewanczyk, Dawid     const std::string& sensorName, const std::string& sensorType,
888b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
88908777fb0SLewanczyk, Dawid     const boost::container::flat_map<
890aa2e59c1SEd Tanous         std::string, boost::container::flat_map<std::string, SensorVariant>>&
89108777fb0SLewanczyk, Dawid         interfacesDict,
89281ce609eSEd Tanous     nlohmann::json& sensorJson, InventoryItem* inventoryItem)
8931abe55efSEd Tanous {
89408777fb0SLewanczyk, Dawid     // We need a value interface before we can do anything with it
89555c7b7a2SEd Tanous     auto valueIt = interfacesDict.find("xyz.openbmc_project.Sensor.Value");
8961abe55efSEd Tanous     if (valueIt == interfacesDict.end())
8971abe55efSEd Tanous     {
89855c7b7a2SEd Tanous         BMCWEB_LOG_ERROR << "Sensor doesn't have a value interface";
89908777fb0SLewanczyk, Dawid         return;
90008777fb0SLewanczyk, Dawid     }
90108777fb0SLewanczyk, Dawid 
90208777fb0SLewanczyk, Dawid     // Assume values exist as is (10^0 == 1) if no scale exists
90308777fb0SLewanczyk, Dawid     int64_t scaleMultiplier = 0;
90408777fb0SLewanczyk, Dawid 
90555c7b7a2SEd Tanous     auto scaleIt = valueIt->second.find("Scale");
90608777fb0SLewanczyk, Dawid     // If a scale exists, pull value as int64, and use the scaling.
9071abe55efSEd Tanous     if (scaleIt != valueIt->second.end())
9081abe55efSEd Tanous     {
909abf2add6SEd Tanous         const int64_t* int64Value = std::get_if<int64_t>(&scaleIt->second);
9101abe55efSEd Tanous         if (int64Value != nullptr)
9111abe55efSEd Tanous         {
91208777fb0SLewanczyk, Dawid             scaleMultiplier = *int64Value;
91308777fb0SLewanczyk, Dawid         }
91408777fb0SLewanczyk, Dawid     }
91508777fb0SLewanczyk, Dawid 
916a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
917adc4f0dbSShawn McCarney     {
91895a3ecadSAnthony Wilson         // For sensors in SensorCollection we set Id instead of MemberId,
91995a3ecadSAnthony Wilson         // including power sensors.
92081ce609eSEd Tanous         sensorJson["Id"] = sensorName;
92181ce609eSEd Tanous         sensorJson["Name"] = boost::replace_all_copy(sensorName, "_", " ");
92295a3ecadSAnthony Wilson     }
92395a3ecadSAnthony Wilson     else if (sensorType != "power")
92495a3ecadSAnthony Wilson     {
92595a3ecadSAnthony Wilson         // Set MemberId and Name for non-power sensors.  For PowerSupplies and
92695a3ecadSAnthony Wilson         // PowerControl, those properties have more general values because
92795a3ecadSAnthony Wilson         // multiple sensors can be stored in the same JSON object.
92881ce609eSEd Tanous         sensorJson["MemberId"] = sensorName;
92981ce609eSEd Tanous         sensorJson["Name"] = boost::replace_all_copy(sensorName, "_", " ");
930adc4f0dbSShawn McCarney     }
931e742b6ccSEd Tanous 
93281ce609eSEd Tanous     sensorJson["Status"]["State"] = getState(inventoryItem);
93381ce609eSEd Tanous     sensorJson["Status"]["Health"] =
93481ce609eSEd Tanous         getHealth(sensorJson, interfacesDict, inventoryItem);
93508777fb0SLewanczyk, Dawid 
93608777fb0SLewanczyk, Dawid     // Parameter to set to override the type we get from dbus, and force it to
93708777fb0SLewanczyk, Dawid     // int, regardless of what is available.  This is used for schemas like fan,
93808777fb0SLewanczyk, Dawid     // that require integers, not floats.
93908777fb0SLewanczyk, Dawid     bool forceToInt = false;
94008777fb0SLewanczyk, Dawid 
9413929aca1SAnthony Wilson     nlohmann::json::json_pointer unit("/Reading");
942a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
94395a3ecadSAnthony Wilson     {
94481ce609eSEd Tanous         sensorJson["@odata.type"] = "#Sensor.v1_0_0.Sensor";
945c2bf7f99SWludzik, Jozef 
946c2bf7f99SWludzik, Jozef         const std::string& readingType = sensors::toReadingType(sensorType);
947c2bf7f99SWludzik, Jozef         if (readingType.empty())
94895a3ecadSAnthony Wilson         {
949c2bf7f99SWludzik, Jozef             BMCWEB_LOG_ERROR << "Redfish cannot map reading type for "
950c2bf7f99SWludzik, Jozef                              << sensorType;
95195a3ecadSAnthony Wilson         }
952c2bf7f99SWludzik, Jozef         else
95395a3ecadSAnthony Wilson         {
954c2bf7f99SWludzik, Jozef             sensorJson["ReadingType"] = readingType;
95595a3ecadSAnthony Wilson         }
956c2bf7f99SWludzik, Jozef 
957c2bf7f99SWludzik, Jozef         const std::string& readingUnits = sensors::toReadingUnits(sensorType);
958c2bf7f99SWludzik, Jozef         if (readingUnits.empty())
959f8ede15eSAdrian Ambrożewicz         {
960c2bf7f99SWludzik, Jozef             BMCWEB_LOG_ERROR << "Redfish cannot map reading unit for "
961c2bf7f99SWludzik, Jozef                              << sensorType;
962c2bf7f99SWludzik, Jozef         }
963c2bf7f99SWludzik, Jozef         else
964c2bf7f99SWludzik, Jozef         {
965c2bf7f99SWludzik, Jozef             sensorJson["ReadingUnits"] = readingUnits;
966f8ede15eSAdrian Ambrożewicz         }
96795a3ecadSAnthony Wilson     }
96895a3ecadSAnthony Wilson     else if (sensorType == "temperature")
9691abe55efSEd Tanous     {
9703929aca1SAnthony Wilson         unit = "/ReadingCelsius"_json_pointer;
97181ce609eSEd Tanous         sensorJson["@odata.type"] = "#Thermal.v1_3_0.Temperature";
97208777fb0SLewanczyk, Dawid         // TODO(ed) Documentation says that path should be type fan_tach,
97308777fb0SLewanczyk, Dawid         // implementation seems to implement fan
9741abe55efSEd Tanous     }
9751abe55efSEd Tanous     else if (sensorType == "fan" || sensorType == "fan_tach")
9761abe55efSEd Tanous     {
9773929aca1SAnthony Wilson         unit = "/Reading"_json_pointer;
97881ce609eSEd Tanous         sensorJson["ReadingUnits"] = "RPM";
97981ce609eSEd Tanous         sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
98081ce609eSEd Tanous         setLedState(sensorJson, inventoryItem);
98108777fb0SLewanczyk, Dawid         forceToInt = true;
9821abe55efSEd Tanous     }
9836f6d0d32SEd Tanous     else if (sensorType == "fan_pwm")
9846f6d0d32SEd Tanous     {
9853929aca1SAnthony Wilson         unit = "/Reading"_json_pointer;
98681ce609eSEd Tanous         sensorJson["ReadingUnits"] = "Percent";
98781ce609eSEd Tanous         sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
98881ce609eSEd Tanous         setLedState(sensorJson, inventoryItem);
9896f6d0d32SEd Tanous         forceToInt = true;
9906f6d0d32SEd Tanous     }
9911abe55efSEd Tanous     else if (sensorType == "voltage")
9921abe55efSEd Tanous     {
9933929aca1SAnthony Wilson         unit = "/ReadingVolts"_json_pointer;
99481ce609eSEd Tanous         sensorJson["@odata.type"] = "#Power.v1_0_0.Voltage";
9951abe55efSEd Tanous     }
9962474adfaSEd Tanous     else if (sensorType == "power")
9972474adfaSEd Tanous     {
99849c53ac9SJohnathan Mantey         std::string sensorNameLower =
99949c53ac9SJohnathan Mantey             boost::algorithm::to_lower_copy(sensorName);
100049c53ac9SJohnathan Mantey 
1001028f7ebcSEddie James         if (!sensorName.compare("total_power"))
1002028f7ebcSEddie James         {
100381ce609eSEd Tanous             sensorJson["@odata.type"] = "#Power.v1_0_0.PowerControl";
10047ab06f49SGunnar Mills             // Put multiple "sensors" into a single PowerControl, so have
10057ab06f49SGunnar Mills             // generic names for MemberId and Name. Follows Redfish mockup.
100681ce609eSEd Tanous             sensorJson["MemberId"] = "0";
100781ce609eSEd Tanous             sensorJson["Name"] = "Chassis Power Control";
10083929aca1SAnthony Wilson             unit = "/PowerConsumedWatts"_json_pointer;
1009028f7ebcSEddie James         }
1010028f7ebcSEddie James         else if (sensorNameLower.find("input") != std::string::npos)
101149c53ac9SJohnathan Mantey         {
10123929aca1SAnthony Wilson             unit = "/PowerInputWatts"_json_pointer;
101349c53ac9SJohnathan Mantey         }
101449c53ac9SJohnathan Mantey         else
101549c53ac9SJohnathan Mantey         {
10163929aca1SAnthony Wilson             unit = "/PowerOutputWatts"_json_pointer;
101749c53ac9SJohnathan Mantey         }
10182474adfaSEd Tanous     }
10191abe55efSEd Tanous     else
10201abe55efSEd Tanous     {
102155c7b7a2SEd Tanous         BMCWEB_LOG_ERROR << "Redfish cannot map object type for " << sensorName;
102208777fb0SLewanczyk, Dawid         return;
102308777fb0SLewanczyk, Dawid     }
102408777fb0SLewanczyk, Dawid     // Map of dbus interface name, dbus property name and redfish property_name
10253929aca1SAnthony Wilson     std::vector<
10263929aca1SAnthony Wilson         std::tuple<const char*, const char*, nlohmann::json::json_pointer>>
10273929aca1SAnthony Wilson         properties;
102808777fb0SLewanczyk, Dawid     properties.reserve(7);
102908777fb0SLewanczyk, Dawid 
103008777fb0SLewanczyk, Dawid     properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit);
1031de629b6eSShawn McCarney 
1032a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
10333929aca1SAnthony Wilson     {
10343929aca1SAnthony Wilson         properties.emplace_back(
10353929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh",
10363929aca1SAnthony Wilson             "/Thresholds/UpperCaution/Reading"_json_pointer);
10373929aca1SAnthony Wilson         properties.emplace_back(
10383929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow",
10393929aca1SAnthony Wilson             "/Thresholds/LowerCaution/Reading"_json_pointer);
10403929aca1SAnthony Wilson         properties.emplace_back(
10413929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh",
10423929aca1SAnthony Wilson             "/Thresholds/UpperCritical/Reading"_json_pointer);
10433929aca1SAnthony Wilson         properties.emplace_back(
10443929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow",
10453929aca1SAnthony Wilson             "/Thresholds/LowerCritical/Reading"_json_pointer);
10463929aca1SAnthony Wilson     }
10473929aca1SAnthony Wilson     else if (sensorType != "power")
1048de629b6eSShawn McCarney     {
104908777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
10503929aca1SAnthony Wilson                                 "WarningHigh",
10513929aca1SAnthony Wilson                                 "/UpperThresholdNonCritical"_json_pointer);
105208777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
10533929aca1SAnthony Wilson                                 "WarningLow",
10543929aca1SAnthony Wilson                                 "/LowerThresholdNonCritical"_json_pointer);
105508777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
10563929aca1SAnthony Wilson                                 "CriticalHigh",
10573929aca1SAnthony Wilson                                 "/UpperThresholdCritical"_json_pointer);
105808777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
10593929aca1SAnthony Wilson                                 "CriticalLow",
10603929aca1SAnthony Wilson                                 "/LowerThresholdCritical"_json_pointer);
1061de629b6eSShawn McCarney     }
106208777fb0SLewanczyk, Dawid 
10632474adfaSEd Tanous     // TODO Need to get UpperThresholdFatal and LowerThresholdFatal
10642474adfaSEd Tanous 
1065a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
106695a3ecadSAnthony Wilson     {
106795a3ecadSAnthony Wilson         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
10683929aca1SAnthony Wilson                                 "/ReadingRangeMin"_json_pointer);
106995a3ecadSAnthony Wilson         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
10703929aca1SAnthony Wilson                                 "/ReadingRangeMax"_json_pointer);
107195a3ecadSAnthony Wilson     }
107295a3ecadSAnthony Wilson     else if (sensorType == "temperature")
10731abe55efSEd Tanous     {
107408777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
10753929aca1SAnthony Wilson                                 "/MinReadingRangeTemp"_json_pointer);
107608777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
10773929aca1SAnthony Wilson                                 "/MaxReadingRangeTemp"_json_pointer);
10781abe55efSEd Tanous     }
1079adc4f0dbSShawn McCarney     else if (sensorType != "power")
10801abe55efSEd Tanous     {
108108777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
10823929aca1SAnthony Wilson                                 "/MinReadingRange"_json_pointer);
108308777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
10843929aca1SAnthony Wilson                                 "/MaxReadingRange"_json_pointer);
108508777fb0SLewanczyk, Dawid     }
108608777fb0SLewanczyk, Dawid 
10873929aca1SAnthony Wilson     for (const std::tuple<const char*, const char*,
10883929aca1SAnthony Wilson                           nlohmann::json::json_pointer>& p : properties)
10891abe55efSEd Tanous     {
109008777fb0SLewanczyk, Dawid         auto interfaceProperties = interfacesDict.find(std::get<0>(p));
10911abe55efSEd Tanous         if (interfaceProperties != interfacesDict.end())
10921abe55efSEd Tanous         {
1093271584abSEd Tanous             auto thisValueIt = interfaceProperties->second.find(std::get<1>(p));
1094271584abSEd Tanous             if (thisValueIt != interfaceProperties->second.end())
10951abe55efSEd Tanous             {
1096271584abSEd Tanous                 const SensorVariant& valueVariant = thisValueIt->second;
10973929aca1SAnthony Wilson 
10983929aca1SAnthony Wilson                 // The property we want to set may be nested json, so use
10993929aca1SAnthony Wilson                 // a json_pointer for easy indexing into the json structure.
11003929aca1SAnthony Wilson                 const nlohmann::json::json_pointer& key = std::get<2>(p);
11013929aca1SAnthony Wilson 
110208777fb0SLewanczyk, Dawid                 // Attempt to pull the int64 directly
1103abf2add6SEd Tanous                 const int64_t* int64Value = std::get_if<int64_t>(&valueVariant);
110408777fb0SLewanczyk, Dawid 
1105abf2add6SEd Tanous                 const double* doubleValue = std::get_if<double>(&valueVariant);
1106028f7ebcSEddie James                 const uint32_t* uValue = std::get_if<uint32_t>(&valueVariant);
11076f6d0d32SEd Tanous                 double temp = 0.0;
11086f6d0d32SEd Tanous                 if (int64Value != nullptr)
11091abe55efSEd Tanous                 {
1110271584abSEd Tanous                     temp = static_cast<double>(*int64Value);
11116f6d0d32SEd Tanous                 }
11126f6d0d32SEd Tanous                 else if (doubleValue != nullptr)
11131abe55efSEd Tanous                 {
11146f6d0d32SEd Tanous                     temp = *doubleValue;
11151abe55efSEd Tanous                 }
1116028f7ebcSEddie James                 else if (uValue != nullptr)
1117028f7ebcSEddie James                 {
1118028f7ebcSEddie James                     temp = *uValue;
1119028f7ebcSEddie James                 }
11201abe55efSEd Tanous                 else
11211abe55efSEd Tanous                 {
11226f6d0d32SEd Tanous                     BMCWEB_LOG_ERROR
11236f6d0d32SEd Tanous                         << "Got value interface that wasn't int or double";
11246f6d0d32SEd Tanous                     continue;
112508777fb0SLewanczyk, Dawid                 }
11266f6d0d32SEd Tanous                 temp = temp * std::pow(10, scaleMultiplier);
11276f6d0d32SEd Tanous                 if (forceToInt)
11286f6d0d32SEd Tanous                 {
112981ce609eSEd Tanous                     sensorJson[key] = static_cast<int64_t>(temp);
11306f6d0d32SEd Tanous                 }
11316f6d0d32SEd Tanous                 else
11326f6d0d32SEd Tanous                 {
113381ce609eSEd Tanous                     sensorJson[key] = temp;
113408777fb0SLewanczyk, Dawid                 }
113508777fb0SLewanczyk, Dawid             }
113608777fb0SLewanczyk, Dawid         }
113708777fb0SLewanczyk, Dawid     }
1138a0ec28b6SAdrian Ambrożewicz 
113981ce609eSEd Tanous     sensorsAsyncResp->addMetadata(sensorJson, unit.to_string(),
1140a0ec28b6SAdrian Ambrożewicz                                   "/xyz/openbmc_project/sensors/" + sensorType +
1141a0ec28b6SAdrian Ambrożewicz                                       "/" + sensorName);
1142a0ec28b6SAdrian Ambrożewicz 
114355c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "Added sensor " << sensorName;
114408777fb0SLewanczyk, Dawid }
114508777fb0SLewanczyk, Dawid 
1146b5a76932SEd Tanous inline void populateFanRedundancy(
1147b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
11488bd25ccdSJames Feist {
11498bd25ccdSJames Feist     crow::connections::systemBus->async_method_call(
11508bd25ccdSJames Feist         [sensorsAsyncResp](const boost::system::error_code ec,
11518bd25ccdSJames Feist                            const GetSubTreeType& resp) {
11528bd25ccdSJames Feist             if (ec)
11538bd25ccdSJames Feist             {
11548bd25ccdSJames Feist                 return; // don't have to have this interface
11558bd25ccdSJames Feist             }
1156e278c18fSEd Tanous             for (const std::pair<std::string,
1157e278c18fSEd Tanous                                  std::vector<std::pair<
1158e278c18fSEd Tanous                                      std::string, std::vector<std::string>>>>&
1159e278c18fSEd Tanous                      pathPair : resp)
11608bd25ccdSJames Feist             {
1161e278c18fSEd Tanous                 const std::string& path = pathPair.first;
1162e278c18fSEd Tanous                 const std::vector<
1163e278c18fSEd Tanous                     std::pair<std::string, std::vector<std::string>>>& objDict =
1164e278c18fSEd Tanous                     pathPair.second;
11658bd25ccdSJames Feist                 if (objDict.empty())
11668bd25ccdSJames Feist                 {
11678bd25ccdSJames Feist                     continue; // this should be impossible
11688bd25ccdSJames Feist                 }
11698bd25ccdSJames Feist 
11708bd25ccdSJames Feist                 const std::string& owner = objDict.begin()->first;
11718bd25ccdSJames Feist                 crow::connections::systemBus->async_method_call(
11728bd25ccdSJames Feist                     [path, owner,
1173271584abSEd Tanous                      sensorsAsyncResp](const boost::system::error_code e,
11748bd25ccdSJames Feist                                        std::variant<std::vector<std::string>>
11758bd25ccdSJames Feist                                            variantEndpoints) {
1176271584abSEd Tanous                         if (e)
11778bd25ccdSJames Feist                         {
11788bd25ccdSJames Feist                             return; // if they don't have an association we
11798bd25ccdSJames Feist                                     // can't tell what chassis is
11808bd25ccdSJames Feist                         }
11818bd25ccdSJames Feist                         // verify part of the right chassis
11828bd25ccdSJames Feist                         auto endpoints = std::get_if<std::vector<std::string>>(
11838bd25ccdSJames Feist                             &variantEndpoints);
11848bd25ccdSJames Feist 
11858bd25ccdSJames Feist                         if (endpoints == nullptr)
11868bd25ccdSJames Feist                         {
11878bd25ccdSJames Feist                             BMCWEB_LOG_ERROR << "Invalid association interface";
11888d1b46d7Szhanghch05                             messages::internalError(
11898d1b46d7Szhanghch05                                 sensorsAsyncResp->asyncResp->res);
11908bd25ccdSJames Feist                             return;
11918bd25ccdSJames Feist                         }
11928bd25ccdSJames Feist 
11938bd25ccdSJames Feist                         auto found = std::find_if(
11948bd25ccdSJames Feist                             endpoints->begin(), endpoints->end(),
11958bd25ccdSJames Feist                             [sensorsAsyncResp](const std::string& entry) {
11968bd25ccdSJames Feist                                 return entry.find(
11978bd25ccdSJames Feist                                            sensorsAsyncResp->chassisId) !=
11988bd25ccdSJames Feist                                        std::string::npos;
11998bd25ccdSJames Feist                             });
12008bd25ccdSJames Feist 
12018bd25ccdSJames Feist                         if (found == endpoints->end())
12028bd25ccdSJames Feist                         {
12038bd25ccdSJames Feist                             return;
12048bd25ccdSJames Feist                         }
12058bd25ccdSJames Feist                         crow::connections::systemBus->async_method_call(
12068bd25ccdSJames Feist                             [path, sensorsAsyncResp](
1207271584abSEd Tanous                                 const boost::system::error_code& err,
12088bd25ccdSJames Feist                                 const boost::container::flat_map<
12098bd25ccdSJames Feist                                     std::string,
12108bd25ccdSJames Feist                                     std::variant<uint8_t,
12118bd25ccdSJames Feist                                                  std::vector<std::string>,
12128bd25ccdSJames Feist                                                  std::string>>& ret) {
1213271584abSEd Tanous                                 if (err)
12148bd25ccdSJames Feist                                 {
12158bd25ccdSJames Feist                                     return; // don't have to have this
12168bd25ccdSJames Feist                                             // interface
12178bd25ccdSJames Feist                                 }
12188bd25ccdSJames Feist                                 auto findFailures = ret.find("AllowedFailures");
12198bd25ccdSJames Feist                                 auto findCollection = ret.find("Collection");
12208bd25ccdSJames Feist                                 auto findStatus = ret.find("Status");
12218bd25ccdSJames Feist 
12228bd25ccdSJames Feist                                 if (findFailures == ret.end() ||
12238bd25ccdSJames Feist                                     findCollection == ret.end() ||
12248bd25ccdSJames Feist                                     findStatus == ret.end())
12258bd25ccdSJames Feist                                 {
12268bd25ccdSJames Feist                                     BMCWEB_LOG_ERROR
12278bd25ccdSJames Feist                                         << "Invalid redundancy interface";
12288bd25ccdSJames Feist                                     messages::internalError(
12298d1b46d7Szhanghch05                                         sensorsAsyncResp->asyncResp->res);
12308bd25ccdSJames Feist                                     return;
12318bd25ccdSJames Feist                                 }
12328bd25ccdSJames Feist 
12338bd25ccdSJames Feist                                 auto allowedFailures = std::get_if<uint8_t>(
12348bd25ccdSJames Feist                                     &(findFailures->second));
12358bd25ccdSJames Feist                                 auto collection =
12368bd25ccdSJames Feist                                     std::get_if<std::vector<std::string>>(
12378bd25ccdSJames Feist                                         &(findCollection->second));
12388bd25ccdSJames Feist                                 auto status = std::get_if<std::string>(
12398bd25ccdSJames Feist                                     &(findStatus->second));
12408bd25ccdSJames Feist 
12418bd25ccdSJames Feist                                 if (allowedFailures == nullptr ||
12428bd25ccdSJames Feist                                     collection == nullptr || status == nullptr)
12438bd25ccdSJames Feist                                 {
12448bd25ccdSJames Feist 
12458bd25ccdSJames Feist                                     BMCWEB_LOG_ERROR
12468bd25ccdSJames Feist                                         << "Invalid redundancy interface "
12478bd25ccdSJames Feist                                            "types";
12488bd25ccdSJames Feist                                     messages::internalError(
12498d1b46d7Szhanghch05                                         sensorsAsyncResp->asyncResp->res);
12508bd25ccdSJames Feist                                     return;
12518bd25ccdSJames Feist                                 }
125228aa8de5SGeorge Liu                                 sdbusplus::message::object_path objectPath(
125328aa8de5SGeorge Liu                                     path);
125428aa8de5SGeorge Liu                                 std::string name = objectPath.filename();
125528aa8de5SGeorge Liu                                 if (name.empty())
12568bd25ccdSJames Feist                                 {
12578bd25ccdSJames Feist                                     // this should be impossible
12588bd25ccdSJames Feist                                     messages::internalError(
12598d1b46d7Szhanghch05                                         sensorsAsyncResp->asyncResp->res);
12608bd25ccdSJames Feist                                     return;
12618bd25ccdSJames Feist                                 }
12628bd25ccdSJames Feist                                 std::replace(name.begin(), name.end(), '_',
12638bd25ccdSJames Feist                                              ' ');
12648bd25ccdSJames Feist 
12658bd25ccdSJames Feist                                 std::string health;
12668bd25ccdSJames Feist 
12678bd25ccdSJames Feist                                 if (boost::ends_with(*status, "Full"))
12688bd25ccdSJames Feist                                 {
12698bd25ccdSJames Feist                                     health = "OK";
12708bd25ccdSJames Feist                                 }
12718bd25ccdSJames Feist                                 else if (boost::ends_with(*status, "Degraded"))
12728bd25ccdSJames Feist                                 {
12738bd25ccdSJames Feist                                     health = "Warning";
12748bd25ccdSJames Feist                                 }
12758bd25ccdSJames Feist                                 else
12768bd25ccdSJames Feist                                 {
12778bd25ccdSJames Feist                                     health = "Critical";
12788bd25ccdSJames Feist                                 }
12798bd25ccdSJames Feist                                 std::vector<nlohmann::json> redfishCollection;
12808bd25ccdSJames Feist                                 const auto& fanRedfish =
12818d1b46d7Szhanghch05                                     sensorsAsyncResp->asyncResp->res
12828d1b46d7Szhanghch05                                         .jsonValue["Fans"];
12838bd25ccdSJames Feist                                 for (const std::string& item : *collection)
12848bd25ccdSJames Feist                                 {
128528aa8de5SGeorge Liu                                     sdbusplus::message::object_path path(item);
128628aa8de5SGeorge Liu                                     std::string itemName = path.filename();
128728aa8de5SGeorge Liu                                     if (itemName.empty())
128828aa8de5SGeorge Liu                                     {
128928aa8de5SGeorge Liu                                         continue;
129028aa8de5SGeorge Liu                                     }
12918bd25ccdSJames Feist                                     /*
12928bd25ccdSJames Feist                                     todo(ed): merge patch that fixes the names
12938bd25ccdSJames Feist                                     std::replace(itemName.begin(),
12948bd25ccdSJames Feist                                                  itemName.end(), '_', ' ');*/
12958bd25ccdSJames Feist                                     auto schemaItem = std::find_if(
12968bd25ccdSJames Feist                                         fanRedfish.begin(), fanRedfish.end(),
12978bd25ccdSJames Feist                                         [itemName](const nlohmann::json& fan) {
12988bd25ccdSJames Feist                                             return fan["MemberId"] == itemName;
12998bd25ccdSJames Feist                                         });
13008bd25ccdSJames Feist                                     if (schemaItem != fanRedfish.end())
13018bd25ccdSJames Feist                                     {
13028bd25ccdSJames Feist                                         redfishCollection.push_back(
13038bd25ccdSJames Feist                                             {{"@odata.id",
13048bd25ccdSJames Feist                                               (*schemaItem)["@odata.id"]}});
13058bd25ccdSJames Feist                                     }
13068bd25ccdSJames Feist                                     else
13078bd25ccdSJames Feist                                     {
13088bd25ccdSJames Feist                                         BMCWEB_LOG_ERROR
13098bd25ccdSJames Feist                                             << "failed to find fan in schema";
13108bd25ccdSJames Feist                                         messages::internalError(
13118d1b46d7Szhanghch05                                             sensorsAsyncResp->asyncResp->res);
13128bd25ccdSJames Feist                                         return;
13138bd25ccdSJames Feist                                     }
13148bd25ccdSJames Feist                                 }
13158bd25ccdSJames Feist 
13163e9e72ebSKuiying Wang                                 size_t minNumNeeded =
13173e9e72ebSKuiying Wang                                     collection->size() > 0
13183e9e72ebSKuiying Wang                                         ? collection->size() - *allowedFailures
13193e9e72ebSKuiying Wang                                         : 0;
1320271584abSEd Tanous                                 nlohmann::json& jResp =
13218d1b46d7Szhanghch05                                     sensorsAsyncResp->asyncResp->res
13228bd25ccdSJames Feist                                         .jsonValue["Redundancy"];
1323271584abSEd Tanous                                 jResp.push_back(
13248bd25ccdSJames Feist                                     {{"@odata.id",
1325717794d5SAppaRao Puli                                       "/redfish/v1/Chassis/" +
13268bd25ccdSJames Feist                                           sensorsAsyncResp->chassisId + "/" +
13278bd25ccdSJames Feist                                           sensorsAsyncResp->chassisSubNode +
13288bd25ccdSJames Feist                                           "#/Redundancy/" +
1329271584abSEd Tanous                                           std::to_string(jResp.size())},
13308bd25ccdSJames Feist                                      {"@odata.type",
13318bd25ccdSJames Feist                                       "#Redundancy.v1_3_2.Redundancy"},
13323e9e72ebSKuiying Wang                                      {"MinNumNeeded", minNumNeeded},
13338bd25ccdSJames Feist                                      {"MemberId", name},
13348bd25ccdSJames Feist                                      {"Mode", "N+m"},
13358bd25ccdSJames Feist                                      {"Name", name},
13368bd25ccdSJames Feist                                      {"RedundancySet", redfishCollection},
13378bd25ccdSJames Feist                                      {"Status",
13388bd25ccdSJames Feist                                       {{"Health", health},
13398bd25ccdSJames Feist                                        {"State", "Enabled"}}}});
13408bd25ccdSJames Feist                             },
13418bd25ccdSJames Feist                             owner, path, "org.freedesktop.DBus.Properties",
13428bd25ccdSJames Feist                             "GetAll",
13438bd25ccdSJames Feist                             "xyz.openbmc_project.Control.FanRedundancy");
13448bd25ccdSJames Feist                     },
134502e92e32SJames Feist                     "xyz.openbmc_project.ObjectMapper", path + "/chassis",
13468bd25ccdSJames Feist                     "org.freedesktop.DBus.Properties", "Get",
13478bd25ccdSJames Feist                     "xyz.openbmc_project.Association", "endpoints");
13488bd25ccdSJames Feist             }
13498bd25ccdSJames Feist         },
13508bd25ccdSJames Feist         "xyz.openbmc_project.ObjectMapper",
13518bd25ccdSJames Feist         "/xyz/openbmc_project/object_mapper",
13528bd25ccdSJames Feist         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
13538bd25ccdSJames Feist         "/xyz/openbmc_project/control", 2,
13548bd25ccdSJames Feist         std::array<const char*, 1>{
13558bd25ccdSJames Feist             "xyz.openbmc_project.Control.FanRedundancy"});
13568bd25ccdSJames Feist }
13578bd25ccdSJames Feist 
1358b5a76932SEd Tanous inline void
135981ce609eSEd Tanous     sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
136049c53ac9SJohnathan Mantey {
13618d1b46d7Szhanghch05     nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue;
136249c53ac9SJohnathan Mantey     std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
136381ce609eSEd Tanous     if (sensorsAsyncResp->chassisSubNode == sensors::node::power)
136449c53ac9SJohnathan Mantey     {
136549c53ac9SJohnathan Mantey         sensorHeaders = {"Voltages", "PowerSupplies"};
136649c53ac9SJohnathan Mantey     }
136749c53ac9SJohnathan Mantey     for (const std::string& sensorGroup : sensorHeaders)
136849c53ac9SJohnathan Mantey     {
136949c53ac9SJohnathan Mantey         nlohmann::json::iterator entry = response.find(sensorGroup);
137049c53ac9SJohnathan Mantey         if (entry != response.end())
137149c53ac9SJohnathan Mantey         {
137249c53ac9SJohnathan Mantey             std::sort(entry->begin(), entry->end(),
137349c53ac9SJohnathan Mantey                       [](nlohmann::json& c1, nlohmann::json& c2) {
137449c53ac9SJohnathan Mantey                           return c1["Name"] < c2["Name"];
137549c53ac9SJohnathan Mantey                       });
137649c53ac9SJohnathan Mantey 
137749c53ac9SJohnathan Mantey             // add the index counts to the end of each entry
137849c53ac9SJohnathan Mantey             size_t count = 0;
137949c53ac9SJohnathan Mantey             for (nlohmann::json& sensorJson : *entry)
138049c53ac9SJohnathan Mantey             {
138149c53ac9SJohnathan Mantey                 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
138249c53ac9SJohnathan Mantey                 if (odata == sensorJson.end())
138349c53ac9SJohnathan Mantey                 {
138449c53ac9SJohnathan Mantey                     continue;
138549c53ac9SJohnathan Mantey                 }
138649c53ac9SJohnathan Mantey                 std::string* value = odata->get_ptr<std::string*>();
138749c53ac9SJohnathan Mantey                 if (value != nullptr)
138849c53ac9SJohnathan Mantey                 {
138949c53ac9SJohnathan Mantey                     *value += std::to_string(count);
139049c53ac9SJohnathan Mantey                     count++;
139181ce609eSEd Tanous                     sensorsAsyncResp->updateUri(sensorJson["Name"], *value);
139249c53ac9SJohnathan Mantey                 }
139349c53ac9SJohnathan Mantey             }
139449c53ac9SJohnathan Mantey         }
139549c53ac9SJohnathan Mantey     }
139649c53ac9SJohnathan Mantey }
139749c53ac9SJohnathan Mantey 
139808777fb0SLewanczyk, Dawid /**
1399adc4f0dbSShawn McCarney  * @brief Finds the inventory item with the specified object path.
1400adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1401adc4f0dbSShawn McCarney  * @param invItemObjPath D-Bus object path of inventory item.
1402adc4f0dbSShawn McCarney  * @return Inventory item within vector, or nullptr if no match found.
14038fb49dd6SShawn McCarney  */
140423a21a1cSEd Tanous inline InventoryItem* findInventoryItem(
1405b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1406adc4f0dbSShawn McCarney     const std::string& invItemObjPath)
14078fb49dd6SShawn McCarney {
1408adc4f0dbSShawn McCarney     for (InventoryItem& inventoryItem : *inventoryItems)
14098fb49dd6SShawn McCarney     {
1410adc4f0dbSShawn McCarney         if (inventoryItem.objectPath == invItemObjPath)
14118fb49dd6SShawn McCarney         {
1412adc4f0dbSShawn McCarney             return &inventoryItem;
14138fb49dd6SShawn McCarney         }
14148fb49dd6SShawn McCarney     }
14158fb49dd6SShawn McCarney     return nullptr;
14168fb49dd6SShawn McCarney }
14178fb49dd6SShawn McCarney 
14188fb49dd6SShawn McCarney /**
1419adc4f0dbSShawn McCarney  * @brief Finds the inventory item associated with the specified sensor.
1420adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1421adc4f0dbSShawn McCarney  * @param sensorObjPath D-Bus object path of sensor.
1422adc4f0dbSShawn McCarney  * @return Inventory item within vector, or nullptr if no match found.
14238fb49dd6SShawn McCarney  */
142423a21a1cSEd Tanous inline InventoryItem* findInventoryItemForSensor(
1425b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1426adc4f0dbSShawn McCarney     const std::string& sensorObjPath)
1427adc4f0dbSShawn McCarney {
1428adc4f0dbSShawn McCarney     for (InventoryItem& inventoryItem : *inventoryItems)
1429adc4f0dbSShawn McCarney     {
1430adc4f0dbSShawn McCarney         if (inventoryItem.sensors.count(sensorObjPath) > 0)
1431adc4f0dbSShawn McCarney         {
1432adc4f0dbSShawn McCarney             return &inventoryItem;
1433adc4f0dbSShawn McCarney         }
1434adc4f0dbSShawn McCarney     }
1435adc4f0dbSShawn McCarney     return nullptr;
1436adc4f0dbSShawn McCarney }
1437adc4f0dbSShawn McCarney 
1438adc4f0dbSShawn McCarney /**
1439d500549bSAnthony Wilson  * @brief Finds the inventory item associated with the specified led path.
1440d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1441d500549bSAnthony Wilson  * @param ledObjPath D-Bus object path of led.
1442d500549bSAnthony Wilson  * @return Inventory item within vector, or nullptr if no match found.
1443d500549bSAnthony Wilson  */
1444d500549bSAnthony Wilson inline InventoryItem*
1445d500549bSAnthony Wilson     findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems,
1446d500549bSAnthony Wilson                             const std::string& ledObjPath)
1447d500549bSAnthony Wilson {
1448d500549bSAnthony Wilson     for (InventoryItem& inventoryItem : inventoryItems)
1449d500549bSAnthony Wilson     {
1450d500549bSAnthony Wilson         if (inventoryItem.ledObjectPath == ledObjPath)
1451d500549bSAnthony Wilson         {
1452d500549bSAnthony Wilson             return &inventoryItem;
1453d500549bSAnthony Wilson         }
1454d500549bSAnthony Wilson     }
1455d500549bSAnthony Wilson     return nullptr;
1456d500549bSAnthony Wilson }
1457d500549bSAnthony Wilson 
1458d500549bSAnthony Wilson /**
1459adc4f0dbSShawn McCarney  * @brief Adds inventory item and associated sensor to specified vector.
1460adc4f0dbSShawn McCarney  *
1461adc4f0dbSShawn McCarney  * Adds a new InventoryItem to the vector if necessary.  Searches for an
1462adc4f0dbSShawn McCarney  * existing InventoryItem with the specified object path.  If not found, one is
1463adc4f0dbSShawn McCarney  * added to the vector.
1464adc4f0dbSShawn McCarney  *
1465adc4f0dbSShawn McCarney  * Next, the specified sensor is added to the set of sensors associated with the
1466adc4f0dbSShawn McCarney  * InventoryItem.
1467adc4f0dbSShawn McCarney  *
1468adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1469adc4f0dbSShawn McCarney  * @param invItemObjPath D-Bus object path of inventory item.
1470adc4f0dbSShawn McCarney  * @param sensorObjPath D-Bus object path of sensor
1471adc4f0dbSShawn McCarney  */
1472b5a76932SEd Tanous inline void addInventoryItem(
1473b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1474b5a76932SEd Tanous     const std::string& invItemObjPath, const std::string& sensorObjPath)
1475adc4f0dbSShawn McCarney {
1476adc4f0dbSShawn McCarney     // Look for inventory item in vector
1477adc4f0dbSShawn McCarney     InventoryItem* inventoryItem =
1478adc4f0dbSShawn McCarney         findInventoryItem(inventoryItems, invItemObjPath);
1479adc4f0dbSShawn McCarney 
1480adc4f0dbSShawn McCarney     // If inventory item doesn't exist in vector, add it
1481adc4f0dbSShawn McCarney     if (inventoryItem == nullptr)
1482adc4f0dbSShawn McCarney     {
1483adc4f0dbSShawn McCarney         inventoryItems->emplace_back(invItemObjPath);
1484adc4f0dbSShawn McCarney         inventoryItem = &(inventoryItems->back());
1485adc4f0dbSShawn McCarney     }
1486adc4f0dbSShawn McCarney 
1487adc4f0dbSShawn McCarney     // Add sensor to set of sensors associated with inventory item
1488adc4f0dbSShawn McCarney     inventoryItem->sensors.emplace(sensorObjPath);
1489adc4f0dbSShawn McCarney }
1490adc4f0dbSShawn McCarney 
1491adc4f0dbSShawn McCarney /**
1492adc4f0dbSShawn McCarney  * @brief Stores D-Bus data in the specified inventory item.
1493adc4f0dbSShawn McCarney  *
1494adc4f0dbSShawn McCarney  * Finds D-Bus data in the specified map of interfaces.  Stores the data in the
1495adc4f0dbSShawn McCarney  * specified InventoryItem.
1496adc4f0dbSShawn McCarney  *
1497adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
1498adc4f0dbSShawn McCarney  * response.
1499adc4f0dbSShawn McCarney  *
1500adc4f0dbSShawn McCarney  * @param inventoryItem Inventory item where data will be stored.
1501adc4f0dbSShawn McCarney  * @param interfacesDict Map containing D-Bus interfaces and their properties
1502adc4f0dbSShawn McCarney  * for the specified inventory item.
1503adc4f0dbSShawn McCarney  */
150423a21a1cSEd Tanous inline void storeInventoryItemData(
1505adc4f0dbSShawn McCarney     InventoryItem& inventoryItem,
15068fb49dd6SShawn McCarney     const boost::container::flat_map<
15078fb49dd6SShawn McCarney         std::string, boost::container::flat_map<std::string, SensorVariant>>&
15088fb49dd6SShawn McCarney         interfacesDict)
15098fb49dd6SShawn McCarney {
1510adc4f0dbSShawn McCarney     // Get properties from Inventory.Item interface
1511adc4f0dbSShawn McCarney     auto interfaceIt =
1512adc4f0dbSShawn McCarney         interfacesDict.find("xyz.openbmc_project.Inventory.Item");
1513adc4f0dbSShawn McCarney     if (interfaceIt != interfacesDict.end())
15148fb49dd6SShawn McCarney     {
1515adc4f0dbSShawn McCarney         auto propertyIt = interfaceIt->second.find("Present");
1516adc4f0dbSShawn McCarney         if (propertyIt != interfaceIt->second.end())
15178fb49dd6SShawn McCarney         {
1518adc4f0dbSShawn McCarney             const bool* value = std::get_if<bool>(&propertyIt->second);
1519adc4f0dbSShawn McCarney             if (value != nullptr)
15208fb49dd6SShawn McCarney             {
1521adc4f0dbSShawn McCarney                 inventoryItem.isPresent = *value;
15228fb49dd6SShawn McCarney             }
15238fb49dd6SShawn McCarney         }
15248fb49dd6SShawn McCarney     }
15258fb49dd6SShawn McCarney 
1526adc4f0dbSShawn McCarney     // Check if Inventory.Item.PowerSupply interface is present
1527adc4f0dbSShawn McCarney     interfaceIt =
1528adc4f0dbSShawn McCarney         interfacesDict.find("xyz.openbmc_project.Inventory.Item.PowerSupply");
1529adc4f0dbSShawn McCarney     if (interfaceIt != interfacesDict.end())
15308fb49dd6SShawn McCarney     {
1531adc4f0dbSShawn McCarney         inventoryItem.isPowerSupply = true;
15328fb49dd6SShawn McCarney     }
1533adc4f0dbSShawn McCarney 
1534adc4f0dbSShawn McCarney     // Get properties from Inventory.Decorator.Asset interface
1535adc4f0dbSShawn McCarney     interfaceIt =
1536adc4f0dbSShawn McCarney         interfacesDict.find("xyz.openbmc_project.Inventory.Decorator.Asset");
1537adc4f0dbSShawn McCarney     if (interfaceIt != interfacesDict.end())
1538adc4f0dbSShawn McCarney     {
1539adc4f0dbSShawn McCarney         auto propertyIt = interfaceIt->second.find("Manufacturer");
1540adc4f0dbSShawn McCarney         if (propertyIt != interfaceIt->second.end())
1541adc4f0dbSShawn McCarney         {
1542adc4f0dbSShawn McCarney             const std::string* value =
1543adc4f0dbSShawn McCarney                 std::get_if<std::string>(&propertyIt->second);
1544adc4f0dbSShawn McCarney             if (value != nullptr)
1545adc4f0dbSShawn McCarney             {
1546adc4f0dbSShawn McCarney                 inventoryItem.manufacturer = *value;
1547adc4f0dbSShawn McCarney             }
1548adc4f0dbSShawn McCarney         }
1549adc4f0dbSShawn McCarney 
1550adc4f0dbSShawn McCarney         propertyIt = interfaceIt->second.find("Model");
1551adc4f0dbSShawn McCarney         if (propertyIt != interfaceIt->second.end())
1552adc4f0dbSShawn McCarney         {
1553adc4f0dbSShawn McCarney             const std::string* value =
1554adc4f0dbSShawn McCarney                 std::get_if<std::string>(&propertyIt->second);
1555adc4f0dbSShawn McCarney             if (value != nullptr)
1556adc4f0dbSShawn McCarney             {
1557adc4f0dbSShawn McCarney                 inventoryItem.model = *value;
1558adc4f0dbSShawn McCarney             }
1559adc4f0dbSShawn McCarney         }
1560adc4f0dbSShawn McCarney 
1561adc4f0dbSShawn McCarney         propertyIt = interfaceIt->second.find("PartNumber");
1562adc4f0dbSShawn McCarney         if (propertyIt != interfaceIt->second.end())
1563adc4f0dbSShawn McCarney         {
1564adc4f0dbSShawn McCarney             const std::string* value =
1565adc4f0dbSShawn McCarney                 std::get_if<std::string>(&propertyIt->second);
1566adc4f0dbSShawn McCarney             if (value != nullptr)
1567adc4f0dbSShawn McCarney             {
1568adc4f0dbSShawn McCarney                 inventoryItem.partNumber = *value;
1569adc4f0dbSShawn McCarney             }
1570adc4f0dbSShawn McCarney         }
1571adc4f0dbSShawn McCarney 
1572adc4f0dbSShawn McCarney         propertyIt = interfaceIt->second.find("SerialNumber");
1573adc4f0dbSShawn McCarney         if (propertyIt != interfaceIt->second.end())
1574adc4f0dbSShawn McCarney         {
1575adc4f0dbSShawn McCarney             const std::string* value =
1576adc4f0dbSShawn McCarney                 std::get_if<std::string>(&propertyIt->second);
1577adc4f0dbSShawn McCarney             if (value != nullptr)
1578adc4f0dbSShawn McCarney             {
1579adc4f0dbSShawn McCarney                 inventoryItem.serialNumber = *value;
1580adc4f0dbSShawn McCarney             }
1581adc4f0dbSShawn McCarney         }
1582adc4f0dbSShawn McCarney     }
1583adc4f0dbSShawn McCarney 
1584adc4f0dbSShawn McCarney     // Get properties from State.Decorator.OperationalStatus interface
1585adc4f0dbSShawn McCarney     interfaceIt = interfacesDict.find(
1586adc4f0dbSShawn McCarney         "xyz.openbmc_project.State.Decorator.OperationalStatus");
1587adc4f0dbSShawn McCarney     if (interfaceIt != interfacesDict.end())
1588adc4f0dbSShawn McCarney     {
1589adc4f0dbSShawn McCarney         auto propertyIt = interfaceIt->second.find("Functional");
1590adc4f0dbSShawn McCarney         if (propertyIt != interfaceIt->second.end())
1591adc4f0dbSShawn McCarney         {
1592adc4f0dbSShawn McCarney             const bool* value = std::get_if<bool>(&propertyIt->second);
1593adc4f0dbSShawn McCarney             if (value != nullptr)
1594adc4f0dbSShawn McCarney             {
1595adc4f0dbSShawn McCarney                 inventoryItem.isFunctional = *value;
15968fb49dd6SShawn McCarney             }
15978fb49dd6SShawn McCarney         }
15988fb49dd6SShawn McCarney     }
15998fb49dd6SShawn McCarney }
16008fb49dd6SShawn McCarney 
16018fb49dd6SShawn McCarney /**
1602adc4f0dbSShawn McCarney  * @brief Gets D-Bus data for inventory items associated with sensors.
16038fb49dd6SShawn McCarney  *
1604adc4f0dbSShawn McCarney  * Uses the specified connections (services) to obtain D-Bus data for inventory
1605adc4f0dbSShawn McCarney  * items associated with sensors.  Stores the resulting data in the
1606adc4f0dbSShawn McCarney  * inventoryItems vector.
16078fb49dd6SShawn McCarney  *
1608adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
1609adc4f0dbSShawn McCarney  * response.
1610adc4f0dbSShawn McCarney  *
1611adc4f0dbSShawn McCarney  * Finds the inventory item data asynchronously.  Invokes callback when data has
1612adc4f0dbSShawn McCarney  * been obtained.
1613adc4f0dbSShawn McCarney  *
1614adc4f0dbSShawn McCarney  * The callback must have the following signature:
1615adc4f0dbSShawn McCarney  *   @code
1616d500549bSAnthony Wilson  *   callback(void)
1617adc4f0dbSShawn McCarney  *   @endcode
1618adc4f0dbSShawn McCarney  *
1619adc4f0dbSShawn McCarney  * This function is called recursively, obtaining data asynchronously from one
1620adc4f0dbSShawn McCarney  * connection in each call.  This ensures the callback is not invoked until the
1621adc4f0dbSShawn McCarney  * last asynchronous function has completed.
16228fb49dd6SShawn McCarney  *
16238fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
1624adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1625adc4f0dbSShawn McCarney  * @param invConnections Connections that provide data for the inventory items.
16268fb49dd6SShawn McCarney  * @param objectMgrPaths Mappings from connection name to DBus object path that
16278fb49dd6SShawn McCarney  * implements ObjectManager.
1628adc4f0dbSShawn McCarney  * @param callback Callback to invoke when inventory data has been obtained.
1629adc4f0dbSShawn McCarney  * @param invConnectionsIndex Current index in invConnections.  Only specified
1630adc4f0dbSShawn McCarney  * in recursive calls to this function.
16318fb49dd6SShawn McCarney  */
1632adc4f0dbSShawn McCarney template <typename Callback>
1633adc4f0dbSShawn McCarney static void getInventoryItemsData(
16348fb49dd6SShawn McCarney     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1635adc4f0dbSShawn McCarney     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
16368fb49dd6SShawn McCarney     std::shared_ptr<boost::container::flat_set<std::string>> invConnections,
16378fb49dd6SShawn McCarney     std::shared_ptr<boost::container::flat_map<std::string, std::string>>
1638adc4f0dbSShawn McCarney         objectMgrPaths,
1639271584abSEd Tanous     Callback&& callback, size_t invConnectionsIndex = 0)
16408fb49dd6SShawn McCarney {
1641adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsData enter";
16428fb49dd6SShawn McCarney 
1643adc4f0dbSShawn McCarney     // If no more connections left, call callback
1644adc4f0dbSShawn McCarney     if (invConnectionsIndex >= invConnections->size())
16458fb49dd6SShawn McCarney     {
1646d500549bSAnthony Wilson         callback();
1647adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
1648adc4f0dbSShawn McCarney         return;
1649adc4f0dbSShawn McCarney     }
1650adc4f0dbSShawn McCarney 
1651adc4f0dbSShawn McCarney     // Get inventory item data from current connection
1652adc4f0dbSShawn McCarney     auto it = invConnections->nth(invConnectionsIndex);
1653adc4f0dbSShawn McCarney     if (it != invConnections->end())
1654adc4f0dbSShawn McCarney     {
1655adc4f0dbSShawn McCarney         const std::string& invConnection = *it;
1656adc4f0dbSShawn McCarney 
16578fb49dd6SShawn McCarney         // Response handler for GetManagedObjects
1658adc4f0dbSShawn McCarney         auto respHandler = [sensorsAsyncResp, inventoryItems, invConnections,
1659adc4f0dbSShawn McCarney                             objectMgrPaths, callback{std::move(callback)},
1660adc4f0dbSShawn McCarney                             invConnectionsIndex](
1661adc4f0dbSShawn McCarney                                const boost::system::error_code ec,
16628fb49dd6SShawn McCarney                                ManagedObjectsVectorType& resp) {
1663adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler enter";
16648fb49dd6SShawn McCarney             if (ec)
16658fb49dd6SShawn McCarney             {
16668fb49dd6SShawn McCarney                 BMCWEB_LOG_ERROR
1667adc4f0dbSShawn McCarney                     << "getInventoryItemsData respHandler DBus error " << ec;
16688d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
16698fb49dd6SShawn McCarney                 return;
16708fb49dd6SShawn McCarney             }
16718fb49dd6SShawn McCarney 
16728fb49dd6SShawn McCarney             // Loop through returned object paths
16738fb49dd6SShawn McCarney             for (const auto& objDictEntry : resp)
16748fb49dd6SShawn McCarney             {
16758fb49dd6SShawn McCarney                 const std::string& objPath =
16768fb49dd6SShawn McCarney                     static_cast<const std::string&>(objDictEntry.first);
16778fb49dd6SShawn McCarney 
1678adc4f0dbSShawn McCarney                 // If this object path is one of the specified inventory items
1679adc4f0dbSShawn McCarney                 InventoryItem* inventoryItem =
1680adc4f0dbSShawn McCarney                     findInventoryItem(inventoryItems, objPath);
1681adc4f0dbSShawn McCarney                 if (inventoryItem != nullptr)
16828fb49dd6SShawn McCarney                 {
1683adc4f0dbSShawn McCarney                     // Store inventory data in InventoryItem
1684adc4f0dbSShawn McCarney                     storeInventoryItemData(*inventoryItem, objDictEntry.second);
16858fb49dd6SShawn McCarney                 }
16868fb49dd6SShawn McCarney             }
16878fb49dd6SShawn McCarney 
1688adc4f0dbSShawn McCarney             // Recurse to get inventory item data from next connection
1689adc4f0dbSShawn McCarney             getInventoryItemsData(sensorsAsyncResp, inventoryItems,
1690adc4f0dbSShawn McCarney                                   invConnections, objectMgrPaths,
1691adc4f0dbSShawn McCarney                                   std::move(callback), invConnectionsIndex + 1);
1692adc4f0dbSShawn McCarney 
1693adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler exit";
16948fb49dd6SShawn McCarney         };
16958fb49dd6SShawn McCarney 
16968fb49dd6SShawn McCarney         // Find DBus object path that implements ObjectManager for the current
16978fb49dd6SShawn McCarney         // connection.  If no mapping found, default to "/".
16988fb49dd6SShawn McCarney         auto iter = objectMgrPaths->find(invConnection);
16998fb49dd6SShawn McCarney         const std::string& objectMgrPath =
17008fb49dd6SShawn McCarney             (iter != objectMgrPaths->end()) ? iter->second : "/";
17018fb49dd6SShawn McCarney         BMCWEB_LOG_DEBUG << "ObjectManager path for " << invConnection << " is "
17028fb49dd6SShawn McCarney                          << objectMgrPath;
17038fb49dd6SShawn McCarney 
17048fb49dd6SShawn McCarney         // Get all object paths and their interfaces for current connection
17058fb49dd6SShawn McCarney         crow::connections::systemBus->async_method_call(
17068fb49dd6SShawn McCarney             std::move(respHandler), invConnection, objectMgrPath,
17078fb49dd6SShawn McCarney             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
17088fb49dd6SShawn McCarney     }
17098fb49dd6SShawn McCarney 
1710adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
17118fb49dd6SShawn McCarney }
17128fb49dd6SShawn McCarney 
17138fb49dd6SShawn McCarney /**
1714adc4f0dbSShawn McCarney  * @brief Gets connections that provide D-Bus data for inventory items.
17158fb49dd6SShawn McCarney  *
1716adc4f0dbSShawn McCarney  * Gets the D-Bus connections (services) that provide data for the inventory
1717adc4f0dbSShawn McCarney  * items that are associated with sensors.
17188fb49dd6SShawn McCarney  *
17198fb49dd6SShawn McCarney  * Finds the connections asynchronously.  Invokes callback when information has
17208fb49dd6SShawn McCarney  * been obtained.
17218fb49dd6SShawn McCarney  *
17228fb49dd6SShawn McCarney  * The callback must have the following signature:
17238fb49dd6SShawn McCarney  *   @code
17248fb49dd6SShawn McCarney  *   callback(std::shared_ptr<boost::container::flat_set<std::string>>
17258fb49dd6SShawn McCarney  *            invConnections)
17268fb49dd6SShawn McCarney  *   @endcode
17278fb49dd6SShawn McCarney  *
17288fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
1729adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
17308fb49dd6SShawn McCarney  * @param callback Callback to invoke when connections have been obtained.
17318fb49dd6SShawn McCarney  */
17328fb49dd6SShawn McCarney template <typename Callback>
17338fb49dd6SShawn McCarney static void getInventoryItemsConnections(
1734b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1735b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
17368fb49dd6SShawn McCarney     Callback&& callback)
17378fb49dd6SShawn McCarney {
17388fb49dd6SShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsConnections enter";
17398fb49dd6SShawn McCarney 
17408fb49dd6SShawn McCarney     const std::string path = "/xyz/openbmc_project/inventory";
1741adc4f0dbSShawn McCarney     const std::array<std::string, 4> interfaces = {
17428fb49dd6SShawn McCarney         "xyz.openbmc_project.Inventory.Item",
1743adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Item.PowerSupply",
1744adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Decorator.Asset",
17458fb49dd6SShawn McCarney         "xyz.openbmc_project.State.Decorator.OperationalStatus"};
17468fb49dd6SShawn McCarney 
17478fb49dd6SShawn McCarney     // Response handler for parsing output from GetSubTree
17488fb49dd6SShawn McCarney     auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp,
1749adc4f0dbSShawn McCarney                         inventoryItems](const boost::system::error_code ec,
17508fb49dd6SShawn McCarney                                         const GetSubTreeType& subtree) {
17518fb49dd6SShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler enter";
17528fb49dd6SShawn McCarney         if (ec)
17538fb49dd6SShawn McCarney         {
17548d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
17558fb49dd6SShawn McCarney             BMCWEB_LOG_ERROR
17568fb49dd6SShawn McCarney                 << "getInventoryItemsConnections respHandler DBus error " << ec;
17578fb49dd6SShawn McCarney             return;
17588fb49dd6SShawn McCarney         }
17598fb49dd6SShawn McCarney 
17608fb49dd6SShawn McCarney         // Make unique list of connections for desired inventory items
17618fb49dd6SShawn McCarney         std::shared_ptr<boost::container::flat_set<std::string>>
17628fb49dd6SShawn McCarney             invConnections =
17638fb49dd6SShawn McCarney                 std::make_shared<boost::container::flat_set<std::string>>();
17648fb49dd6SShawn McCarney         invConnections->reserve(8);
17658fb49dd6SShawn McCarney 
17668fb49dd6SShawn McCarney         // Loop through objects from GetSubTree
17678fb49dd6SShawn McCarney         for (const std::pair<
17688fb49dd6SShawn McCarney                  std::string,
17698fb49dd6SShawn McCarney                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
17708fb49dd6SShawn McCarney                  object : subtree)
17718fb49dd6SShawn McCarney         {
1772adc4f0dbSShawn McCarney             // Check if object path is one of the specified inventory items
17738fb49dd6SShawn McCarney             const std::string& objPath = object.first;
1774adc4f0dbSShawn McCarney             if (findInventoryItem(inventoryItems, objPath) != nullptr)
17758fb49dd6SShawn McCarney             {
17768fb49dd6SShawn McCarney                 // Store all connections to inventory item
17778fb49dd6SShawn McCarney                 for (const std::pair<std::string, std::vector<std::string>>&
17788fb49dd6SShawn McCarney                          objData : object.second)
17798fb49dd6SShawn McCarney                 {
17808fb49dd6SShawn McCarney                     const std::string& invConnection = objData.first;
17818fb49dd6SShawn McCarney                     invConnections->insert(invConnection);
17828fb49dd6SShawn McCarney                 }
17838fb49dd6SShawn McCarney             }
17848fb49dd6SShawn McCarney         }
1785d500549bSAnthony Wilson 
17868fb49dd6SShawn McCarney         callback(invConnections);
17878fb49dd6SShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler exit";
17888fb49dd6SShawn McCarney     };
17898fb49dd6SShawn McCarney 
17908fb49dd6SShawn McCarney     // Make call to ObjectMapper to find all inventory items
17918fb49dd6SShawn McCarney     crow::connections::systemBus->async_method_call(
17928fb49dd6SShawn McCarney         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
17938fb49dd6SShawn McCarney         "/xyz/openbmc_project/object_mapper",
17948fb49dd6SShawn McCarney         "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
17958fb49dd6SShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsConnections exit";
17968fb49dd6SShawn McCarney }
17978fb49dd6SShawn McCarney 
17988fb49dd6SShawn McCarney /**
1799adc4f0dbSShawn McCarney  * @brief Gets associations from sensors to inventory items.
18008fb49dd6SShawn McCarney  *
18018fb49dd6SShawn McCarney  * Looks for ObjectMapper associations from the specified sensors to related
1802d500549bSAnthony Wilson  * inventory items. Then finds the associations from those inventory items to
1803d500549bSAnthony Wilson  * their LEDs, if any.
18048fb49dd6SShawn McCarney  *
18058fb49dd6SShawn McCarney  * Finds the inventory items asynchronously.  Invokes callback when information
18068fb49dd6SShawn McCarney  * has been obtained.
18078fb49dd6SShawn McCarney  *
18088fb49dd6SShawn McCarney  * The callback must have the following signature:
18098fb49dd6SShawn McCarney  *   @code
1810adc4f0dbSShawn McCarney  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
18118fb49dd6SShawn McCarney  *   @endcode
18128fb49dd6SShawn McCarney  *
18138fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
18148fb49dd6SShawn McCarney  * @param sensorNames All sensors within the current chassis.
18158fb49dd6SShawn McCarney  * @param objectMgrPaths Mappings from connection name to DBus object path that
18168fb49dd6SShawn McCarney  * implements ObjectManager.
18178fb49dd6SShawn McCarney  * @param callback Callback to invoke when inventory items have been obtained.
18188fb49dd6SShawn McCarney  */
18198fb49dd6SShawn McCarney template <typename Callback>
1820adc4f0dbSShawn McCarney static void getInventoryItemAssociations(
1821b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1822b5a76932SEd Tanous     const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames,
1823b5a76932SEd Tanous     const std::shared_ptr<boost::container::flat_map<std::string, std::string>>&
18248fb49dd6SShawn McCarney         objectMgrPaths,
18258fb49dd6SShawn McCarney     Callback&& callback)
18268fb49dd6SShawn McCarney {
1827adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemAssociations enter";
18288fb49dd6SShawn McCarney 
18298fb49dd6SShawn McCarney     // Response handler for GetManagedObjects
18308fb49dd6SShawn McCarney     auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp,
18318fb49dd6SShawn McCarney                         sensorNames](const boost::system::error_code ec,
18328fb49dd6SShawn McCarney                                      dbus::utility::ManagedObjectType& resp) {
1833adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler enter";
18348fb49dd6SShawn McCarney         if (ec)
18358fb49dd6SShawn McCarney         {
1836adc4f0dbSShawn McCarney             BMCWEB_LOG_ERROR
1837adc4f0dbSShawn McCarney                 << "getInventoryItemAssociations respHandler DBus error " << ec;
18388d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
18398fb49dd6SShawn McCarney             return;
18408fb49dd6SShawn McCarney         }
18418fb49dd6SShawn McCarney 
1842adc4f0dbSShawn McCarney         // Create vector to hold list of inventory items
1843adc4f0dbSShawn McCarney         std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1844adc4f0dbSShawn McCarney             std::make_shared<std::vector<InventoryItem>>();
1845adc4f0dbSShawn McCarney 
18468fb49dd6SShawn McCarney         // Loop through returned object paths
18478fb49dd6SShawn McCarney         std::string sensorAssocPath;
18488fb49dd6SShawn McCarney         sensorAssocPath.reserve(128); // avoid memory allocations
18498fb49dd6SShawn McCarney         for (const auto& objDictEntry : resp)
18508fb49dd6SShawn McCarney         {
18518fb49dd6SShawn McCarney             const std::string& objPath =
18528fb49dd6SShawn McCarney                 static_cast<const std::string&>(objDictEntry.first);
18538fb49dd6SShawn McCarney             const boost::container::flat_map<
18548fb49dd6SShawn McCarney                 std::string, boost::container::flat_map<
18558fb49dd6SShawn McCarney                                  std::string, dbus::utility::DbusVariantType>>&
18568fb49dd6SShawn McCarney                 interfacesDict = objDictEntry.second;
18578fb49dd6SShawn McCarney 
18588fb49dd6SShawn McCarney             // If path is inventory association for one of the specified sensors
18598fb49dd6SShawn McCarney             for (const std::string& sensorName : *sensorNames)
18608fb49dd6SShawn McCarney             {
18618fb49dd6SShawn McCarney                 sensorAssocPath = sensorName;
18628fb49dd6SShawn McCarney                 sensorAssocPath += "/inventory";
18638fb49dd6SShawn McCarney                 if (objPath == sensorAssocPath)
18648fb49dd6SShawn McCarney                 {
18658fb49dd6SShawn McCarney                     // Get Association interface for object path
18668fb49dd6SShawn McCarney                     auto assocIt =
18678fb49dd6SShawn McCarney                         interfacesDict.find("xyz.openbmc_project.Association");
18688fb49dd6SShawn McCarney                     if (assocIt != interfacesDict.end())
18698fb49dd6SShawn McCarney                     {
18708fb49dd6SShawn McCarney                         // Get inventory item from end point
18718fb49dd6SShawn McCarney                         auto endpointsIt = assocIt->second.find("endpoints");
18728fb49dd6SShawn McCarney                         if (endpointsIt != assocIt->second.end())
18738fb49dd6SShawn McCarney                         {
18748fb49dd6SShawn McCarney                             const std::vector<std::string>* endpoints =
18758fb49dd6SShawn McCarney                                 std::get_if<std::vector<std::string>>(
18768fb49dd6SShawn McCarney                                     &endpointsIt->second);
18778fb49dd6SShawn McCarney                             if ((endpoints != nullptr) && !endpoints->empty())
18788fb49dd6SShawn McCarney                             {
1879adc4f0dbSShawn McCarney                                 // Add inventory item to vector
1880adc4f0dbSShawn McCarney                                 const std::string& invItemPath =
1881adc4f0dbSShawn McCarney                                     endpoints->front();
1882adc4f0dbSShawn McCarney                                 addInventoryItem(inventoryItems, invItemPath,
1883adc4f0dbSShawn McCarney                                                  sensorName);
18848fb49dd6SShawn McCarney                             }
18858fb49dd6SShawn McCarney                         }
18868fb49dd6SShawn McCarney                     }
18878fb49dd6SShawn McCarney                     break;
18888fb49dd6SShawn McCarney                 }
18898fb49dd6SShawn McCarney             }
18908fb49dd6SShawn McCarney         }
18918fb49dd6SShawn McCarney 
1892d500549bSAnthony Wilson         // Now loop through the returned object paths again, this time to
1893d500549bSAnthony Wilson         // find the leds associated with the inventory items we just found
1894d500549bSAnthony Wilson         std::string inventoryAssocPath;
1895d500549bSAnthony Wilson         inventoryAssocPath.reserve(128); // avoid memory allocations
1896d500549bSAnthony Wilson         for (const auto& objDictEntry : resp)
1897d500549bSAnthony Wilson         {
1898d500549bSAnthony Wilson             const std::string& objPath =
1899d500549bSAnthony Wilson                 static_cast<const std::string&>(objDictEntry.first);
1900d500549bSAnthony Wilson             const boost::container::flat_map<
1901d500549bSAnthony Wilson                 std::string, boost::container::flat_map<
1902d500549bSAnthony Wilson                                  std::string, dbus::utility::DbusVariantType>>&
1903d500549bSAnthony Wilson                 interfacesDict = objDictEntry.second;
1904d500549bSAnthony Wilson 
1905d500549bSAnthony Wilson             for (InventoryItem& inventoryItem : *inventoryItems)
1906d500549bSAnthony Wilson             {
1907d500549bSAnthony Wilson                 inventoryAssocPath = inventoryItem.objectPath;
1908d500549bSAnthony Wilson                 inventoryAssocPath += "/leds";
1909d500549bSAnthony Wilson                 if (objPath == inventoryAssocPath)
1910d500549bSAnthony Wilson                 {
1911d500549bSAnthony Wilson                     // Get Association interface for object path
1912d500549bSAnthony Wilson                     auto assocIt =
1913d500549bSAnthony Wilson                         interfacesDict.find("xyz.openbmc_project.Association");
1914d500549bSAnthony Wilson                     if (assocIt != interfacesDict.end())
1915d500549bSAnthony Wilson                     {
1916d500549bSAnthony Wilson                         // Get inventory item from end point
1917d500549bSAnthony Wilson                         auto endpointsIt = assocIt->second.find("endpoints");
1918d500549bSAnthony Wilson                         if (endpointsIt != assocIt->second.end())
1919d500549bSAnthony Wilson                         {
1920d500549bSAnthony Wilson                             const std::vector<std::string>* endpoints =
1921d500549bSAnthony Wilson                                 std::get_if<std::vector<std::string>>(
1922d500549bSAnthony Wilson                                     &endpointsIt->second);
1923d500549bSAnthony Wilson                             if ((endpoints != nullptr) && !endpoints->empty())
1924d500549bSAnthony Wilson                             {
1925d500549bSAnthony Wilson                                 // Store LED path in inventory item
1926d500549bSAnthony Wilson                                 const std::string& ledPath = endpoints->front();
1927d500549bSAnthony Wilson                                 inventoryItem.ledObjectPath = ledPath;
1928d500549bSAnthony Wilson                             }
1929d500549bSAnthony Wilson                         }
1930d500549bSAnthony Wilson                     }
1931d500549bSAnthony Wilson                     break;
1932d500549bSAnthony Wilson                 }
1933d500549bSAnthony Wilson             }
1934d500549bSAnthony Wilson         }
1935adc4f0dbSShawn McCarney         callback(inventoryItems);
1936adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler exit";
19378fb49dd6SShawn McCarney     };
19388fb49dd6SShawn McCarney 
19398fb49dd6SShawn McCarney     // Find DBus object path that implements ObjectManager for ObjectMapper
19408fb49dd6SShawn McCarney     std::string connection = "xyz.openbmc_project.ObjectMapper";
19418fb49dd6SShawn McCarney     auto iter = objectMgrPaths->find(connection);
19428fb49dd6SShawn McCarney     const std::string& objectMgrPath =
19438fb49dd6SShawn McCarney         (iter != objectMgrPaths->end()) ? iter->second : "/";
19448fb49dd6SShawn McCarney     BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
19458fb49dd6SShawn McCarney                      << objectMgrPath;
19468fb49dd6SShawn McCarney 
19478fb49dd6SShawn McCarney     // Call GetManagedObjects on the ObjectMapper to get all associations
19488fb49dd6SShawn McCarney     crow::connections::systemBus->async_method_call(
19498fb49dd6SShawn McCarney         std::move(respHandler), connection, objectMgrPath,
19508fb49dd6SShawn McCarney         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
19518fb49dd6SShawn McCarney 
1952adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemAssociations exit";
19538fb49dd6SShawn McCarney }
19548fb49dd6SShawn McCarney 
19558fb49dd6SShawn McCarney /**
1956d500549bSAnthony Wilson  * @brief Gets D-Bus data for inventory item leds associated with sensors.
1957d500549bSAnthony Wilson  *
1958d500549bSAnthony Wilson  * Uses the specified connections (services) to obtain D-Bus data for inventory
1959d500549bSAnthony Wilson  * item leds associated with sensors.  Stores the resulting data in the
1960d500549bSAnthony Wilson  * inventoryItems vector.
1961d500549bSAnthony Wilson  *
1962d500549bSAnthony Wilson  * This data is later used to provide sensor property values in the JSON
1963d500549bSAnthony Wilson  * response.
1964d500549bSAnthony Wilson  *
1965d500549bSAnthony Wilson  * Finds the inventory item led data asynchronously.  Invokes callback when data
1966d500549bSAnthony Wilson  * has been obtained.
1967d500549bSAnthony Wilson  *
1968d500549bSAnthony Wilson  * The callback must have the following signature:
1969d500549bSAnthony Wilson  *   @code
197042cbe538SGunnar Mills  *   callback()
1971d500549bSAnthony Wilson  *   @endcode
1972d500549bSAnthony Wilson  *
1973d500549bSAnthony Wilson  * This function is called recursively, obtaining data asynchronously from one
1974d500549bSAnthony Wilson  * connection in each call.  This ensures the callback is not invoked until the
1975d500549bSAnthony Wilson  * last asynchronous function has completed.
1976d500549bSAnthony Wilson  *
1977d500549bSAnthony Wilson  * @param sensorsAsyncResp Pointer to object holding response data.
1978d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1979d500549bSAnthony Wilson  * @param ledConnections Connections that provide data for the inventory leds.
1980d500549bSAnthony Wilson  * @param callback Callback to invoke when inventory data has been obtained.
1981d500549bSAnthony Wilson  * @param ledConnectionsIndex Current index in ledConnections.  Only specified
1982d500549bSAnthony Wilson  * in recursive calls to this function.
1983d500549bSAnthony Wilson  */
1984d500549bSAnthony Wilson template <typename Callback>
1985d500549bSAnthony Wilson void getInventoryLedData(
1986d500549bSAnthony Wilson     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1987d500549bSAnthony Wilson     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1988d500549bSAnthony Wilson     std::shared_ptr<boost::container::flat_map<std::string, std::string>>
1989d500549bSAnthony Wilson         ledConnections,
1990d500549bSAnthony Wilson     Callback&& callback, size_t ledConnectionsIndex = 0)
1991d500549bSAnthony Wilson {
1992d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLedData enter";
1993d500549bSAnthony Wilson 
1994d500549bSAnthony Wilson     // If no more connections left, call callback
1995d500549bSAnthony Wilson     if (ledConnectionsIndex >= ledConnections->size())
1996d500549bSAnthony Wilson     {
199742cbe538SGunnar Mills         callback();
1998d500549bSAnthony Wilson         BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1999d500549bSAnthony Wilson         return;
2000d500549bSAnthony Wilson     }
2001d500549bSAnthony Wilson 
2002d500549bSAnthony Wilson     // Get inventory item data from current connection
2003d500549bSAnthony Wilson     auto it = ledConnections->nth(ledConnectionsIndex);
2004d500549bSAnthony Wilson     if (it != ledConnections->end())
2005d500549bSAnthony Wilson     {
2006d500549bSAnthony Wilson         const std::string& ledPath = (*it).first;
2007d500549bSAnthony Wilson         const std::string& ledConnection = (*it).second;
2008d500549bSAnthony Wilson         // Response handler for Get State property
2009d500549bSAnthony Wilson         auto respHandler =
2010d500549bSAnthony Wilson             [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
2011d500549bSAnthony Wilson              callback{std::move(callback)},
2012d500549bSAnthony Wilson              ledConnectionsIndex](const boost::system::error_code ec,
2013d500549bSAnthony Wilson                                   const std::variant<std::string>& ledState) {
2014d500549bSAnthony Wilson                 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler enter";
2015d500549bSAnthony Wilson                 if (ec)
2016d500549bSAnthony Wilson                 {
2017d500549bSAnthony Wilson                     BMCWEB_LOG_ERROR
2018d500549bSAnthony Wilson                         << "getInventoryLedData respHandler DBus error " << ec;
20198d1b46d7Szhanghch05                     messages::internalError(sensorsAsyncResp->asyncResp->res);
2020d500549bSAnthony Wilson                     return;
2021d500549bSAnthony Wilson                 }
2022d500549bSAnthony Wilson 
2023d500549bSAnthony Wilson                 const std::string* state = std::get_if<std::string>(&ledState);
2024d500549bSAnthony Wilson                 if (state != nullptr)
2025d500549bSAnthony Wilson                 {
2026d500549bSAnthony Wilson                     BMCWEB_LOG_DEBUG << "Led state: " << *state;
2027d500549bSAnthony Wilson                     // Find inventory item with this LED object path
2028d500549bSAnthony Wilson                     InventoryItem* inventoryItem =
2029d500549bSAnthony Wilson                         findInventoryItemForLed(*inventoryItems, ledPath);
2030d500549bSAnthony Wilson                     if (inventoryItem != nullptr)
2031d500549bSAnthony Wilson                     {
2032d500549bSAnthony Wilson                         // Store LED state in InventoryItem
2033d500549bSAnthony Wilson                         if (boost::ends_with(*state, "On"))
2034d500549bSAnthony Wilson                         {
2035d500549bSAnthony Wilson                             inventoryItem->ledState = LedState::ON;
2036d500549bSAnthony Wilson                         }
2037d500549bSAnthony Wilson                         else if (boost::ends_with(*state, "Blink"))
2038d500549bSAnthony Wilson                         {
2039d500549bSAnthony Wilson                             inventoryItem->ledState = LedState::BLINK;
2040d500549bSAnthony Wilson                         }
2041d500549bSAnthony Wilson                         else if (boost::ends_with(*state, "Off"))
2042d500549bSAnthony Wilson                         {
2043d500549bSAnthony Wilson                             inventoryItem->ledState = LedState::OFF;
2044d500549bSAnthony Wilson                         }
2045d500549bSAnthony Wilson                         else
2046d500549bSAnthony Wilson                         {
2047d500549bSAnthony Wilson                             inventoryItem->ledState = LedState::UNKNOWN;
2048d500549bSAnthony Wilson                         }
2049d500549bSAnthony Wilson                     }
2050d500549bSAnthony Wilson                 }
2051d500549bSAnthony Wilson                 else
2052d500549bSAnthony Wilson                 {
2053d500549bSAnthony Wilson                     BMCWEB_LOG_DEBUG << "Failed to find State data for LED: "
2054d500549bSAnthony Wilson                                      << ledPath;
2055d500549bSAnthony Wilson                 }
2056d500549bSAnthony Wilson 
2057d500549bSAnthony Wilson                 // Recurse to get LED data from next connection
2058d500549bSAnthony Wilson                 getInventoryLedData(sensorsAsyncResp, inventoryItems,
2059d500549bSAnthony Wilson                                     ledConnections, std::move(callback),
2060d500549bSAnthony Wilson                                     ledConnectionsIndex + 1);
2061d500549bSAnthony Wilson 
2062d500549bSAnthony Wilson                 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler exit";
2063d500549bSAnthony Wilson             };
2064d500549bSAnthony Wilson 
2065d500549bSAnthony Wilson         // Get the State property for the current LED
2066d500549bSAnthony Wilson         crow::connections::systemBus->async_method_call(
2067d500549bSAnthony Wilson             std::move(respHandler), ledConnection, ledPath,
2068d500549bSAnthony Wilson             "org.freedesktop.DBus.Properties", "Get",
2069d500549bSAnthony Wilson             "xyz.openbmc_project.Led.Physical", "State");
2070d500549bSAnthony Wilson     }
2071d500549bSAnthony Wilson 
2072d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
2073d500549bSAnthony Wilson }
2074d500549bSAnthony Wilson 
2075d500549bSAnthony Wilson /**
2076d500549bSAnthony Wilson  * @brief Gets LED data for LEDs associated with given inventory items.
2077d500549bSAnthony Wilson  *
2078d500549bSAnthony Wilson  * Gets the D-Bus connections (services) that provide LED data for the LEDs
2079d500549bSAnthony Wilson  * associated with the specified inventory items.  Then gets the LED data from
2080d500549bSAnthony Wilson  * each connection and stores it in the inventory item.
2081d500549bSAnthony Wilson  *
2082d500549bSAnthony Wilson  * This data is later used to provide sensor property values in the JSON
2083d500549bSAnthony Wilson  * response.
2084d500549bSAnthony Wilson  *
2085d500549bSAnthony Wilson  * Finds the LED data asynchronously.  Invokes callback when information has
2086d500549bSAnthony Wilson  * been obtained.
2087d500549bSAnthony Wilson  *
2088d500549bSAnthony Wilson  * The callback must have the following signature:
2089d500549bSAnthony Wilson  *   @code
209042cbe538SGunnar Mills  *   callback()
2091d500549bSAnthony Wilson  *   @endcode
2092d500549bSAnthony Wilson  *
2093d500549bSAnthony Wilson  * @param sensorsAsyncResp Pointer to object holding response data.
2094d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
2095d500549bSAnthony Wilson  * @param callback Callback to invoke when inventory items have been obtained.
2096d500549bSAnthony Wilson  */
2097d500549bSAnthony Wilson template <typename Callback>
2098d500549bSAnthony Wilson void getInventoryLeds(
2099d500549bSAnthony Wilson     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2100d500549bSAnthony Wilson     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2101d500549bSAnthony Wilson     Callback&& callback)
2102d500549bSAnthony Wilson {
2103d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLeds enter";
2104d500549bSAnthony Wilson 
2105d500549bSAnthony Wilson     const std::string path = "/xyz/openbmc_project";
2106d500549bSAnthony Wilson     const std::array<std::string, 1> interfaces = {
2107d500549bSAnthony Wilson         "xyz.openbmc_project.Led.Physical"};
2108d500549bSAnthony Wilson 
2109d500549bSAnthony Wilson     // Response handler for parsing output from GetSubTree
2110d500549bSAnthony Wilson     auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp,
2111d500549bSAnthony Wilson                         inventoryItems](const boost::system::error_code ec,
2112d500549bSAnthony Wilson                                         const GetSubTreeType& subtree) {
2113d500549bSAnthony Wilson         BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler enter";
2114d500549bSAnthony Wilson         if (ec)
2115d500549bSAnthony Wilson         {
21168d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
2117d500549bSAnthony Wilson             BMCWEB_LOG_ERROR << "getInventoryLeds respHandler DBus error "
2118d500549bSAnthony Wilson                              << ec;
2119d500549bSAnthony Wilson             return;
2120d500549bSAnthony Wilson         }
2121d500549bSAnthony Wilson 
2122d500549bSAnthony Wilson         // Build map of LED object paths to connections
2123d500549bSAnthony Wilson         std::shared_ptr<boost::container::flat_map<std::string, std::string>>
2124d500549bSAnthony Wilson             ledConnections = std::make_shared<
2125d500549bSAnthony Wilson                 boost::container::flat_map<std::string, std::string>>();
2126d500549bSAnthony Wilson 
2127d500549bSAnthony Wilson         // Loop through objects from GetSubTree
2128d500549bSAnthony Wilson         for (const std::pair<
2129d500549bSAnthony Wilson                  std::string,
2130d500549bSAnthony Wilson                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
2131d500549bSAnthony Wilson                  object : subtree)
2132d500549bSAnthony Wilson         {
2133d500549bSAnthony Wilson             // Check if object path is LED for one of the specified inventory
2134d500549bSAnthony Wilson             // items
2135d500549bSAnthony Wilson             const std::string& ledPath = object.first;
2136d500549bSAnthony Wilson             if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr)
2137d500549bSAnthony Wilson             {
2138d500549bSAnthony Wilson                 // Add mapping from ledPath to connection
2139d500549bSAnthony Wilson                 const std::string& connection = object.second.begin()->first;
2140d500549bSAnthony Wilson                 (*ledConnections)[ledPath] = connection;
2141d500549bSAnthony Wilson                 BMCWEB_LOG_DEBUG << "Added mapping " << ledPath << " -> "
2142d500549bSAnthony Wilson                                  << connection;
2143d500549bSAnthony Wilson             }
2144d500549bSAnthony Wilson         }
2145d500549bSAnthony Wilson 
2146d500549bSAnthony Wilson         getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections,
2147d500549bSAnthony Wilson                             std::move(callback));
2148d500549bSAnthony Wilson         BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler exit";
2149d500549bSAnthony Wilson     };
2150d500549bSAnthony Wilson     // Make call to ObjectMapper to find all inventory items
2151d500549bSAnthony Wilson     crow::connections::systemBus->async_method_call(
2152d500549bSAnthony Wilson         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
2153d500549bSAnthony Wilson         "/xyz/openbmc_project/object_mapper",
2154d500549bSAnthony Wilson         "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
2155d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLeds exit";
2156d500549bSAnthony Wilson }
2157d500549bSAnthony Wilson 
2158d500549bSAnthony Wilson /**
215942cbe538SGunnar Mills  * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
216042cbe538SGunnar Mills  *
216142cbe538SGunnar Mills  * Uses the specified connections (services) (currently assumes just one) to
216242cbe538SGunnar Mills  * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
216342cbe538SGunnar Mills  * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
216442cbe538SGunnar Mills  *
216542cbe538SGunnar Mills  * This data is later used to provide sensor property values in the JSON
216642cbe538SGunnar Mills  * response.
216742cbe538SGunnar Mills  *
216842cbe538SGunnar Mills  * Finds the Power Supply Attributes data asynchronously.  Invokes callback
216942cbe538SGunnar Mills  * when data has been obtained.
217042cbe538SGunnar Mills  *
217142cbe538SGunnar Mills  * The callback must have the following signature:
217242cbe538SGunnar Mills  *   @code
217342cbe538SGunnar Mills  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
217442cbe538SGunnar Mills  *   @endcode
217542cbe538SGunnar Mills  *
217642cbe538SGunnar Mills  * @param sensorsAsyncResp Pointer to object holding response data.
217742cbe538SGunnar Mills  * @param inventoryItems D-Bus inventory items associated with sensors.
217842cbe538SGunnar Mills  * @param psAttributesConnections Connections that provide data for the Power
217942cbe538SGunnar Mills  *        Supply Attributes
218042cbe538SGunnar Mills  * @param callback Callback to invoke when data has been obtained.
218142cbe538SGunnar Mills  */
218242cbe538SGunnar Mills template <typename Callback>
218342cbe538SGunnar Mills void getPowerSupplyAttributesData(
2184b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
218542cbe538SGunnar Mills     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
218642cbe538SGunnar Mills     const boost::container::flat_map<std::string, std::string>&
218742cbe538SGunnar Mills         psAttributesConnections,
218842cbe538SGunnar Mills     Callback&& callback)
218942cbe538SGunnar Mills {
219042cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData enter";
219142cbe538SGunnar Mills 
219242cbe538SGunnar Mills     if (psAttributesConnections.empty())
219342cbe538SGunnar Mills     {
219442cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "Can't find PowerSupplyAttributes, no connections!";
219542cbe538SGunnar Mills         callback(inventoryItems);
219642cbe538SGunnar Mills         return;
219742cbe538SGunnar Mills     }
219842cbe538SGunnar Mills 
219942cbe538SGunnar Mills     // Assuming just one connection (service) for now
220042cbe538SGunnar Mills     auto it = psAttributesConnections.nth(0);
220142cbe538SGunnar Mills 
220242cbe538SGunnar Mills     const std::string& psAttributesPath = (*it).first;
220342cbe538SGunnar Mills     const std::string& psAttributesConnection = (*it).second;
220442cbe538SGunnar Mills 
220542cbe538SGunnar Mills     // Response handler for Get DeratingFactor property
220642cbe538SGunnar Mills     auto respHandler = [sensorsAsyncResp, inventoryItems,
220742cbe538SGunnar Mills                         callback{std::move(callback)}](
220842cbe538SGunnar Mills                            const boost::system::error_code ec,
220942cbe538SGunnar Mills                            const std::variant<uint32_t>& deratingFactor) {
221042cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler enter";
221142cbe538SGunnar Mills         if (ec)
221242cbe538SGunnar Mills         {
221342cbe538SGunnar Mills             BMCWEB_LOG_ERROR
221442cbe538SGunnar Mills                 << "getPowerSupplyAttributesData respHandler DBus error " << ec;
22158d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
221642cbe538SGunnar Mills             return;
221742cbe538SGunnar Mills         }
221842cbe538SGunnar Mills 
221942cbe538SGunnar Mills         const uint32_t* value = std::get_if<uint32_t>(&deratingFactor);
222042cbe538SGunnar Mills         if (value != nullptr)
222142cbe538SGunnar Mills         {
222242cbe538SGunnar Mills             BMCWEB_LOG_DEBUG << "PS EfficiencyPercent value: " << *value;
222342cbe538SGunnar Mills             // Store value in Power Supply Inventory Items
222442cbe538SGunnar Mills             for (InventoryItem& inventoryItem : *inventoryItems)
222542cbe538SGunnar Mills             {
222642cbe538SGunnar Mills                 if (inventoryItem.isPowerSupply == true)
222742cbe538SGunnar Mills                 {
222842cbe538SGunnar Mills                     inventoryItem.powerSupplyEfficiencyPercent =
222942cbe538SGunnar Mills                         static_cast<int>(*value);
223042cbe538SGunnar Mills                 }
223142cbe538SGunnar Mills             }
223242cbe538SGunnar Mills         }
223342cbe538SGunnar Mills         else
223442cbe538SGunnar Mills         {
223542cbe538SGunnar Mills             BMCWEB_LOG_DEBUG
223642cbe538SGunnar Mills                 << "Failed to find EfficiencyPercent value for PowerSupplies";
223742cbe538SGunnar Mills         }
223842cbe538SGunnar Mills 
223942cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler exit";
224042cbe538SGunnar Mills         callback(inventoryItems);
224142cbe538SGunnar Mills     };
224242cbe538SGunnar Mills 
224342cbe538SGunnar Mills     // Get the DeratingFactor property for the PowerSupplyAttributes
224442cbe538SGunnar Mills     // Currently only property on the interface/only one we care about
224542cbe538SGunnar Mills     crow::connections::systemBus->async_method_call(
224642cbe538SGunnar Mills         std::move(respHandler), psAttributesConnection, psAttributesPath,
224742cbe538SGunnar Mills         "org.freedesktop.DBus.Properties", "Get",
224842cbe538SGunnar Mills         "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor");
224942cbe538SGunnar Mills 
225042cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData exit";
225142cbe538SGunnar Mills }
225242cbe538SGunnar Mills 
225342cbe538SGunnar Mills /**
225442cbe538SGunnar Mills  * @brief Gets the Power Supply Attributes such as EfficiencyPercent
225542cbe538SGunnar Mills  *
225642cbe538SGunnar Mills  * Gets the D-Bus connection (service) that provides Power Supply Attributes
225742cbe538SGunnar Mills  * data. Then gets the Power Supply Attributes data from the connection
225842cbe538SGunnar Mills  * (currently just assumes 1 connection) and stores the data in the inventory
225942cbe538SGunnar Mills  * item.
226042cbe538SGunnar Mills  *
226142cbe538SGunnar Mills  * This data is later used to provide sensor property values in the JSON
226242cbe538SGunnar Mills  * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
226342cbe538SGunnar Mills  *
226442cbe538SGunnar Mills  * Finds the Power Supply Attributes data asynchronously. Invokes callback
226542cbe538SGunnar Mills  * when information has been obtained.
226642cbe538SGunnar Mills  *
226742cbe538SGunnar Mills  * The callback must have the following signature:
226842cbe538SGunnar Mills  *   @code
226942cbe538SGunnar Mills  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
227042cbe538SGunnar Mills  *   @endcode
227142cbe538SGunnar Mills  *
227242cbe538SGunnar Mills  * @param sensorsAsyncResp Pointer to object holding response data.
227342cbe538SGunnar Mills  * @param inventoryItems D-Bus inventory items associated with sensors.
227442cbe538SGunnar Mills  * @param callback Callback to invoke when data has been obtained.
227542cbe538SGunnar Mills  */
227642cbe538SGunnar Mills template <typename Callback>
227742cbe538SGunnar Mills void getPowerSupplyAttributes(
227842cbe538SGunnar Mills     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
227942cbe538SGunnar Mills     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
228042cbe538SGunnar Mills     Callback&& callback)
228142cbe538SGunnar Mills {
228242cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes enter";
228342cbe538SGunnar Mills 
228442cbe538SGunnar Mills     // Only need the power supply attributes when the Power Schema
2285a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode != sensors::node::power)
228642cbe538SGunnar Mills     {
228742cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit since not Power";
228842cbe538SGunnar Mills         callback(inventoryItems);
228942cbe538SGunnar Mills         return;
229042cbe538SGunnar Mills     }
229142cbe538SGunnar Mills 
229242cbe538SGunnar Mills     const std::array<std::string, 1> interfaces = {
229342cbe538SGunnar Mills         "xyz.openbmc_project.Control.PowerSupplyAttributes"};
229442cbe538SGunnar Mills 
229542cbe538SGunnar Mills     // Response handler for parsing output from GetSubTree
229642cbe538SGunnar Mills     auto respHandler = [callback{std::move(callback)}, sensorsAsyncResp,
229742cbe538SGunnar Mills                         inventoryItems](const boost::system::error_code ec,
229842cbe538SGunnar Mills                                         const GetSubTreeType& subtree) {
229942cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler enter";
230042cbe538SGunnar Mills         if (ec)
230142cbe538SGunnar Mills         {
23028d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
230342cbe538SGunnar Mills             BMCWEB_LOG_ERROR
230442cbe538SGunnar Mills                 << "getPowerSupplyAttributes respHandler DBus error " << ec;
230542cbe538SGunnar Mills             return;
230642cbe538SGunnar Mills         }
230742cbe538SGunnar Mills         if (subtree.size() == 0)
230842cbe538SGunnar Mills         {
230942cbe538SGunnar Mills             BMCWEB_LOG_DEBUG << "Can't find Power Supply Attributes!";
231042cbe538SGunnar Mills             callback(inventoryItems);
231142cbe538SGunnar Mills             return;
231242cbe538SGunnar Mills         }
231342cbe538SGunnar Mills 
231442cbe538SGunnar Mills         // Currently we only support 1 power supply attribute, use this for
231542cbe538SGunnar Mills         // all the power supplies. Build map of object path to connection.
231642cbe538SGunnar Mills         // Assume just 1 connection and 1 path for now.
231742cbe538SGunnar Mills         boost::container::flat_map<std::string, std::string>
231842cbe538SGunnar Mills             psAttributesConnections;
231942cbe538SGunnar Mills 
232042cbe538SGunnar Mills         if (subtree[0].first.empty() || subtree[0].second.empty())
232142cbe538SGunnar Mills         {
232242cbe538SGunnar Mills             BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
232342cbe538SGunnar Mills             callback(inventoryItems);
232442cbe538SGunnar Mills             return;
232542cbe538SGunnar Mills         }
232642cbe538SGunnar Mills 
232742cbe538SGunnar Mills         const std::string& psAttributesPath = subtree[0].first;
232842cbe538SGunnar Mills         const std::string& connection = subtree[0].second.begin()->first;
232942cbe538SGunnar Mills 
233042cbe538SGunnar Mills         if (connection.empty())
233142cbe538SGunnar Mills         {
233242cbe538SGunnar Mills             BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
233342cbe538SGunnar Mills             callback(inventoryItems);
233442cbe538SGunnar Mills             return;
233542cbe538SGunnar Mills         }
233642cbe538SGunnar Mills 
233742cbe538SGunnar Mills         psAttributesConnections[psAttributesPath] = connection;
233842cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "Added mapping " << psAttributesPath << " -> "
233942cbe538SGunnar Mills                          << connection;
234042cbe538SGunnar Mills 
234142cbe538SGunnar Mills         getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
234242cbe538SGunnar Mills                                      psAttributesConnections,
234342cbe538SGunnar Mills                                      std::move(callback));
234442cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler exit";
234542cbe538SGunnar Mills     };
234642cbe538SGunnar Mills     // Make call to ObjectMapper to find the PowerSupplyAttributes service
234742cbe538SGunnar Mills     crow::connections::systemBus->async_method_call(
234842cbe538SGunnar Mills         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
234942cbe538SGunnar Mills         "/xyz/openbmc_project/object_mapper",
235042cbe538SGunnar Mills         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
235142cbe538SGunnar Mills         "/xyz/openbmc_project", 0, interfaces);
235242cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit";
235342cbe538SGunnar Mills }
235442cbe538SGunnar Mills 
235542cbe538SGunnar Mills /**
2356adc4f0dbSShawn McCarney  * @brief Gets inventory items associated with sensors.
23578fb49dd6SShawn McCarney  *
23588fb49dd6SShawn McCarney  * Finds the inventory items that are associated with the specified sensors.
2359adc4f0dbSShawn McCarney  * Then gets D-Bus data for the inventory items, such as presence and VPD.
23608fb49dd6SShawn McCarney  *
2361adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
2362adc4f0dbSShawn McCarney  * response.
23638fb49dd6SShawn McCarney  *
2364adc4f0dbSShawn McCarney  * Finds the inventory items asynchronously.  Invokes callback when the
2365adc4f0dbSShawn McCarney  * inventory items have been obtained.
2366adc4f0dbSShawn McCarney  *
2367adc4f0dbSShawn McCarney  * The callback must have the following signature:
2368adc4f0dbSShawn McCarney  *   @code
2369adc4f0dbSShawn McCarney  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2370adc4f0dbSShawn McCarney  *   @endcode
23718fb49dd6SShawn McCarney  *
23728fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
23738fb49dd6SShawn McCarney  * @param sensorNames All sensors within the current chassis.
23748fb49dd6SShawn McCarney  * @param objectMgrPaths Mappings from connection name to DBus object path that
23758fb49dd6SShawn McCarney  * implements ObjectManager.
2376adc4f0dbSShawn McCarney  * @param callback Callback to invoke when inventory items have been obtained.
23778fb49dd6SShawn McCarney  */
2378adc4f0dbSShawn McCarney template <typename Callback>
2379adc4f0dbSShawn McCarney static void getInventoryItems(
23808fb49dd6SShawn McCarney     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
23818fb49dd6SShawn McCarney     const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
23828fb49dd6SShawn McCarney     std::shared_ptr<boost::container::flat_map<std::string, std::string>>
2383adc4f0dbSShawn McCarney         objectMgrPaths,
2384adc4f0dbSShawn McCarney     Callback&& callback)
23858fb49dd6SShawn McCarney {
2386adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItems enter";
2387adc4f0dbSShawn McCarney     auto getInventoryItemAssociationsCb =
2388adc4f0dbSShawn McCarney         [sensorsAsyncResp, objectMgrPaths, callback{std::move(callback)}](
2389adc4f0dbSShawn McCarney             std::shared_ptr<std::vector<InventoryItem>> inventoryItems) {
2390adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb enter";
23918fb49dd6SShawn McCarney             auto getInventoryItemsConnectionsCb =
2392adc4f0dbSShawn McCarney                 [sensorsAsyncResp, inventoryItems, objectMgrPaths,
2393adc4f0dbSShawn McCarney                  callback{std::move(callback)}](
23948fb49dd6SShawn McCarney                     std::shared_ptr<boost::container::flat_set<std::string>>
23958fb49dd6SShawn McCarney                         invConnections) {
23968fb49dd6SShawn McCarney                     BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb enter";
2397d500549bSAnthony Wilson                     auto getInventoryItemsDataCb =
2398d500549bSAnthony Wilson                         [sensorsAsyncResp, inventoryItems,
2399d500549bSAnthony Wilson                          callback{std::move(callback)}]() {
2400d500549bSAnthony Wilson                             BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb enter";
240142cbe538SGunnar Mills 
240242cbe538SGunnar Mills                             auto getInventoryLedsCb = [sensorsAsyncResp,
240342cbe538SGunnar Mills                                                        inventoryItems,
240442cbe538SGunnar Mills                                                        callback{std::move(
240542cbe538SGunnar Mills                                                            callback)}]() {
240642cbe538SGunnar Mills                                 BMCWEB_LOG_DEBUG << "getInventoryLedsCb enter";
240742cbe538SGunnar Mills                                 // Find Power Supply Attributes and get the data
240842cbe538SGunnar Mills                                 getPowerSupplyAttributes(sensorsAsyncResp,
240942cbe538SGunnar Mills                                                          inventoryItems,
241042cbe538SGunnar Mills                                                          std::move(callback));
241142cbe538SGunnar Mills                                 BMCWEB_LOG_DEBUG << "getInventoryLedsCb exit";
241242cbe538SGunnar Mills                             };
241342cbe538SGunnar Mills 
2414d500549bSAnthony Wilson                             // Find led connections and get the data
2415d500549bSAnthony Wilson                             getInventoryLeds(sensorsAsyncResp, inventoryItems,
241642cbe538SGunnar Mills                                              std::move(getInventoryLedsCb));
2417d500549bSAnthony Wilson                             BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb exit";
2418d500549bSAnthony Wilson                         };
24198fb49dd6SShawn McCarney 
2420adc4f0dbSShawn McCarney                     // Get inventory item data from connections
2421adc4f0dbSShawn McCarney                     getInventoryItemsData(sensorsAsyncResp, inventoryItems,
2422adc4f0dbSShawn McCarney                                           invConnections, objectMgrPaths,
2423d500549bSAnthony Wilson                                           std::move(getInventoryItemsDataCb));
24248fb49dd6SShawn McCarney                     BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb exit";
24258fb49dd6SShawn McCarney                 };
24268fb49dd6SShawn McCarney 
2427adc4f0dbSShawn McCarney             // Get connections that provide inventory item data
24288fb49dd6SShawn McCarney             getInventoryItemsConnections(
2429adc4f0dbSShawn McCarney                 sensorsAsyncResp, inventoryItems,
24308fb49dd6SShawn McCarney                 std::move(getInventoryItemsConnectionsCb));
2431adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb exit";
24328fb49dd6SShawn McCarney         };
24338fb49dd6SShawn McCarney 
2434adc4f0dbSShawn McCarney     // Get associations from sensors to inventory items
2435adc4f0dbSShawn McCarney     getInventoryItemAssociations(sensorsAsyncResp, sensorNames, objectMgrPaths,
2436adc4f0dbSShawn McCarney                                  std::move(getInventoryItemAssociationsCb));
2437adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItems exit";
2438adc4f0dbSShawn McCarney }
2439adc4f0dbSShawn McCarney 
2440adc4f0dbSShawn McCarney /**
2441adc4f0dbSShawn McCarney  * @brief Returns JSON PowerSupply object for the specified inventory item.
2442adc4f0dbSShawn McCarney  *
2443adc4f0dbSShawn McCarney  * Searches for a JSON PowerSupply object that matches the specified inventory
2444adc4f0dbSShawn McCarney  * item.  If one is not found, a new PowerSupply object is added to the JSON
2445adc4f0dbSShawn McCarney  * array.
2446adc4f0dbSShawn McCarney  *
2447adc4f0dbSShawn McCarney  * Multiple sensors are often associated with one power supply inventory item.
2448adc4f0dbSShawn McCarney  * As a result, multiple sensor values are stored in one JSON PowerSupply
2449adc4f0dbSShawn McCarney  * object.
2450adc4f0dbSShawn McCarney  *
2451adc4f0dbSShawn McCarney  * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
2452adc4f0dbSShawn McCarney  * @param inventoryItem Inventory item for the power supply.
2453adc4f0dbSShawn McCarney  * @param chassisId Chassis that contains the power supply.
2454adc4f0dbSShawn McCarney  * @return JSON PowerSupply object for the specified inventory item.
2455adc4f0dbSShawn McCarney  */
245623a21a1cSEd Tanous inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
2457adc4f0dbSShawn McCarney                                       const InventoryItem& inventoryItem,
2458adc4f0dbSShawn McCarney                                       const std::string& chassisId)
2459adc4f0dbSShawn McCarney {
2460adc4f0dbSShawn McCarney     // Check if matching PowerSupply object already exists in JSON array
2461adc4f0dbSShawn McCarney     for (nlohmann::json& powerSupply : powerSupplyArray)
2462adc4f0dbSShawn McCarney     {
2463adc4f0dbSShawn McCarney         if (powerSupply["MemberId"] == inventoryItem.name)
2464adc4f0dbSShawn McCarney         {
2465adc4f0dbSShawn McCarney             return powerSupply;
2466adc4f0dbSShawn McCarney         }
2467adc4f0dbSShawn McCarney     }
2468adc4f0dbSShawn McCarney 
2469adc4f0dbSShawn McCarney     // Add new PowerSupply object to JSON array
2470adc4f0dbSShawn McCarney     powerSupplyArray.push_back({});
2471adc4f0dbSShawn McCarney     nlohmann::json& powerSupply = powerSupplyArray.back();
2472adc4f0dbSShawn McCarney     powerSupply["@odata.id"] =
2473adc4f0dbSShawn McCarney         "/redfish/v1/Chassis/" + chassisId + "/Power#/PowerSupplies/";
2474adc4f0dbSShawn McCarney     powerSupply["MemberId"] = inventoryItem.name;
2475adc4f0dbSShawn McCarney     powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " ");
2476adc4f0dbSShawn McCarney     powerSupply["Manufacturer"] = inventoryItem.manufacturer;
2477adc4f0dbSShawn McCarney     powerSupply["Model"] = inventoryItem.model;
2478adc4f0dbSShawn McCarney     powerSupply["PartNumber"] = inventoryItem.partNumber;
2479adc4f0dbSShawn McCarney     powerSupply["SerialNumber"] = inventoryItem.serialNumber;
2480d500549bSAnthony Wilson     setLedState(powerSupply, &inventoryItem);
2481adc4f0dbSShawn McCarney 
248242cbe538SGunnar Mills     if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
248342cbe538SGunnar Mills     {
248442cbe538SGunnar Mills         powerSupply["EfficiencyPercent"] =
248542cbe538SGunnar Mills             inventoryItem.powerSupplyEfficiencyPercent;
248642cbe538SGunnar Mills     }
248742cbe538SGunnar Mills 
248842cbe538SGunnar Mills     powerSupply["Status"]["State"] = getState(&inventoryItem);
2489adc4f0dbSShawn McCarney     const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
2490adc4f0dbSShawn McCarney     powerSupply["Status"]["Health"] = health;
2491adc4f0dbSShawn McCarney 
2492adc4f0dbSShawn McCarney     return powerSupply;
24938fb49dd6SShawn McCarney }
24948fb49dd6SShawn McCarney 
24958fb49dd6SShawn McCarney /**
2496de629b6eSShawn McCarney  * @brief Gets the values of the specified sensors.
2497de629b6eSShawn McCarney  *
2498de629b6eSShawn McCarney  * Stores the results as JSON in the SensorsAsyncResp.
2499de629b6eSShawn McCarney  *
2500de629b6eSShawn McCarney  * Gets the sensor values asynchronously.  Stores the results later when the
2501de629b6eSShawn McCarney  * information has been obtained.
2502de629b6eSShawn McCarney  *
2503adc4f0dbSShawn McCarney  * The sensorNames set contains all requested sensors for the current chassis.
2504de629b6eSShawn McCarney  *
2505de629b6eSShawn McCarney  * To minimize the number of DBus calls, the DBus method
2506de629b6eSShawn McCarney  * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
2507de629b6eSShawn McCarney  * values of all sensors provided by a connection (service).
2508de629b6eSShawn McCarney  *
2509de629b6eSShawn McCarney  * The connections set contains all the connections that provide sensor values.
2510de629b6eSShawn McCarney  *
2511de629b6eSShawn McCarney  * The objectMgrPaths map contains mappings from a connection name to the
2512de629b6eSShawn McCarney  * corresponding DBus object path that implements ObjectManager.
2513de629b6eSShawn McCarney  *
2514adc4f0dbSShawn McCarney  * The InventoryItem vector contains D-Bus inventory items associated with the
2515adc4f0dbSShawn McCarney  * sensors.  Inventory item data is needed for some Redfish sensor properties.
2516adc4f0dbSShawn McCarney  *
2517de629b6eSShawn McCarney  * @param SensorsAsyncResp Pointer to object holding response data.
2518adc4f0dbSShawn McCarney  * @param sensorNames All requested sensors within the current chassis.
2519de629b6eSShawn McCarney  * @param connections Connections that provide sensor values.
2520de629b6eSShawn McCarney  * @param objectMgrPaths Mappings from connection name to DBus object path that
2521de629b6eSShawn McCarney  * implements ObjectManager.
2522adc4f0dbSShawn McCarney  * @param inventoryItems Inventory items associated with the sensors.
2523de629b6eSShawn McCarney  */
252423a21a1cSEd Tanous inline void getSensorData(
252581ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2526b5a76932SEd Tanous     const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames,
2527de629b6eSShawn McCarney     const boost::container::flat_set<std::string>& connections,
2528b5a76932SEd Tanous     const std::shared_ptr<boost::container::flat_map<std::string, std::string>>&
2529adc4f0dbSShawn McCarney         objectMgrPaths,
2530b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems)
2531de629b6eSShawn McCarney {
2532de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getSensorData enter";
2533de629b6eSShawn McCarney     // Get managed objects from all services exposing sensors
2534de629b6eSShawn McCarney     for (const std::string& connection : connections)
2535de629b6eSShawn McCarney     {
2536de629b6eSShawn McCarney         // Response handler to process managed objects
253781ce609eSEd Tanous         auto getManagedObjectsCb = [sensorsAsyncResp, sensorNames,
2538adc4f0dbSShawn McCarney                                     inventoryItems](
2539de629b6eSShawn McCarney                                        const boost::system::error_code ec,
2540de629b6eSShawn McCarney                                        ManagedObjectsVectorType& resp) {
2541de629b6eSShawn McCarney             BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter";
2542de629b6eSShawn McCarney             if (ec)
2543de629b6eSShawn McCarney             {
2544de629b6eSShawn McCarney                 BMCWEB_LOG_ERROR << "getManagedObjectsCb DBUS error: " << ec;
25458d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
2546de629b6eSShawn McCarney                 return;
2547de629b6eSShawn McCarney             }
2548de629b6eSShawn McCarney             // Go through all objects and update response with sensor data
2549de629b6eSShawn McCarney             for (const auto& objDictEntry : resp)
2550de629b6eSShawn McCarney             {
2551de629b6eSShawn McCarney                 const std::string& objPath =
2552de629b6eSShawn McCarney                     static_cast<const std::string&>(objDictEntry.first);
2553de629b6eSShawn McCarney                 BMCWEB_LOG_DEBUG << "getManagedObjectsCb parsing object "
2554de629b6eSShawn McCarney                                  << objPath;
2555de629b6eSShawn McCarney 
2556de629b6eSShawn McCarney                 std::vector<std::string> split;
2557de629b6eSShawn McCarney                 // Reserve space for
2558de629b6eSShawn McCarney                 // /xyz/openbmc_project/sensors/<name>/<subname>
2559de629b6eSShawn McCarney                 split.reserve(6);
2560de629b6eSShawn McCarney                 boost::algorithm::split(split, objPath, boost::is_any_of("/"));
2561de629b6eSShawn McCarney                 if (split.size() < 6)
2562de629b6eSShawn McCarney                 {
2563de629b6eSShawn McCarney                     BMCWEB_LOG_ERROR << "Got path that isn't long enough "
2564de629b6eSShawn McCarney                                      << objPath;
2565de629b6eSShawn McCarney                     continue;
2566de629b6eSShawn McCarney                 }
2567de629b6eSShawn McCarney                 // These indexes aren't intuitive, as boost::split puts an empty
2568de629b6eSShawn McCarney                 // string at the beginning
2569de629b6eSShawn McCarney                 const std::string& sensorType = split[4];
2570de629b6eSShawn McCarney                 const std::string& sensorName = split[5];
2571de629b6eSShawn McCarney                 BMCWEB_LOG_DEBUG << "sensorName " << sensorName
2572de629b6eSShawn McCarney                                  << " sensorType " << sensorType;
257349c53ac9SJohnathan Mantey                 if (sensorNames->find(objPath) == sensorNames->end())
2574de629b6eSShawn McCarney                 {
2575de629b6eSShawn McCarney                     BMCWEB_LOG_ERROR << sensorName << " not in sensor list ";
2576de629b6eSShawn McCarney                     continue;
2577de629b6eSShawn McCarney                 }
2578de629b6eSShawn McCarney 
2579adc4f0dbSShawn McCarney                 // Find inventory item (if any) associated with sensor
2580adc4f0dbSShawn McCarney                 InventoryItem* inventoryItem =
2581adc4f0dbSShawn McCarney                     findInventoryItemForSensor(inventoryItems, objPath);
2582adc4f0dbSShawn McCarney 
258395a3ecadSAnthony Wilson                 const std::string& sensorSchema =
258481ce609eSEd Tanous                     sensorsAsyncResp->chassisSubNode;
258595a3ecadSAnthony Wilson 
258695a3ecadSAnthony Wilson                 nlohmann::json* sensorJson = nullptr;
258795a3ecadSAnthony Wilson 
2588a0ec28b6SAdrian Ambrożewicz                 if (sensorSchema == sensors::node::sensors)
258995a3ecadSAnthony Wilson                 {
25908d1b46d7Szhanghch05                     sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
259181ce609eSEd Tanous                         "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId +
259281ce609eSEd Tanous                         "/" + sensorsAsyncResp->chassisSubNode + "/" +
259395a3ecadSAnthony Wilson                         sensorName;
25948d1b46d7Szhanghch05                     sensorJson = &(sensorsAsyncResp->asyncResp->res.jsonValue);
259595a3ecadSAnthony Wilson                 }
259695a3ecadSAnthony Wilson                 else
259795a3ecadSAnthony Wilson                 {
2598271584abSEd Tanous                     std::string fieldName;
2599de629b6eSShawn McCarney                     if (sensorType == "temperature")
2600de629b6eSShawn McCarney                     {
2601de629b6eSShawn McCarney                         fieldName = "Temperatures";
2602de629b6eSShawn McCarney                     }
2603de629b6eSShawn McCarney                     else if (sensorType == "fan" || sensorType == "fan_tach" ||
2604de629b6eSShawn McCarney                              sensorType == "fan_pwm")
2605de629b6eSShawn McCarney                     {
2606de629b6eSShawn McCarney                         fieldName = "Fans";
2607de629b6eSShawn McCarney                     }
2608de629b6eSShawn McCarney                     else if (sensorType == "voltage")
2609de629b6eSShawn McCarney                     {
2610de629b6eSShawn McCarney                         fieldName = "Voltages";
2611de629b6eSShawn McCarney                     }
2612de629b6eSShawn McCarney                     else if (sensorType == "power")
2613de629b6eSShawn McCarney                     {
2614028f7ebcSEddie James                         if (!sensorName.compare("total_power"))
2615028f7ebcSEddie James                         {
2616028f7ebcSEddie James                             fieldName = "PowerControl";
2617028f7ebcSEddie James                         }
2618adc4f0dbSShawn McCarney                         else if ((inventoryItem != nullptr) &&
2619adc4f0dbSShawn McCarney                                  (inventoryItem->isPowerSupply))
2620028f7ebcSEddie James                         {
2621de629b6eSShawn McCarney                             fieldName = "PowerSupplies";
2622de629b6eSShawn McCarney                         }
2623adc4f0dbSShawn McCarney                         else
2624adc4f0dbSShawn McCarney                         {
2625adc4f0dbSShawn McCarney                             // Other power sensors are in SensorCollection
2626adc4f0dbSShawn McCarney                             continue;
2627adc4f0dbSShawn McCarney                         }
2628028f7ebcSEddie James                     }
2629de629b6eSShawn McCarney                     else
2630de629b6eSShawn McCarney                     {
2631de629b6eSShawn McCarney                         BMCWEB_LOG_ERROR << "Unsure how to handle sensorType "
2632de629b6eSShawn McCarney                                          << sensorType;
2633de629b6eSShawn McCarney                         continue;
2634de629b6eSShawn McCarney                     }
2635de629b6eSShawn McCarney 
2636de629b6eSShawn McCarney                     nlohmann::json& tempArray =
26378d1b46d7Szhanghch05                         sensorsAsyncResp->asyncResp->res.jsonValue[fieldName];
2638adc4f0dbSShawn McCarney                     if (fieldName == "PowerControl")
263949c53ac9SJohnathan Mantey                     {
2640adc4f0dbSShawn McCarney                         if (tempArray.empty())
26417ab06f49SGunnar Mills                         {
264295a3ecadSAnthony Wilson                             // Put multiple "sensors" into a single
264395a3ecadSAnthony Wilson                             // PowerControl. Follows MemberId naming and
264495a3ecadSAnthony Wilson                             // naming in power.hpp.
26457ab06f49SGunnar Mills                             tempArray.push_back(
2646adc4f0dbSShawn McCarney                                 {{"@odata.id",
2647adc4f0dbSShawn McCarney                                   "/redfish/v1/Chassis/" +
264881ce609eSEd Tanous                                       sensorsAsyncResp->chassisId + "/" +
264981ce609eSEd Tanous                                       sensorsAsyncResp->chassisSubNode + "#/" +
2650adc4f0dbSShawn McCarney                                       fieldName + "/0"}});
2651adc4f0dbSShawn McCarney                         }
2652adc4f0dbSShawn McCarney                         sensorJson = &(tempArray.back());
2653adc4f0dbSShawn McCarney                     }
2654adc4f0dbSShawn McCarney                     else if (fieldName == "PowerSupplies")
2655adc4f0dbSShawn McCarney                     {
2656adc4f0dbSShawn McCarney                         if (inventoryItem != nullptr)
2657adc4f0dbSShawn McCarney                         {
2658adc4f0dbSShawn McCarney                             sensorJson =
2659adc4f0dbSShawn McCarney                                 &(getPowerSupply(tempArray, *inventoryItem,
266081ce609eSEd Tanous                                                  sensorsAsyncResp->chassisId));
2661adc4f0dbSShawn McCarney                         }
266249c53ac9SJohnathan Mantey                     }
266349c53ac9SJohnathan Mantey                     else
266449c53ac9SJohnathan Mantey                     {
2665de629b6eSShawn McCarney                         tempArray.push_back(
266695a3ecadSAnthony Wilson                             {{"@odata.id",
266795a3ecadSAnthony Wilson                               "/redfish/v1/Chassis/" +
266881ce609eSEd Tanous                                   sensorsAsyncResp->chassisId + "/" +
266981ce609eSEd Tanous                                   sensorsAsyncResp->chassisSubNode + "#/" +
267095a3ecadSAnthony Wilson                                   fieldName + "/"}});
2671adc4f0dbSShawn McCarney                         sensorJson = &(tempArray.back());
267249c53ac9SJohnathan Mantey                     }
267395a3ecadSAnthony Wilson                 }
2674de629b6eSShawn McCarney 
2675adc4f0dbSShawn McCarney                 if (sensorJson != nullptr)
2676adc4f0dbSShawn McCarney                 {
2677a0ec28b6SAdrian Ambrożewicz                     objectInterfacesToJson(
267881ce609eSEd Tanous                         sensorName, sensorType, sensorsAsyncResp,
2679a0ec28b6SAdrian Ambrożewicz                         objDictEntry.second, *sensorJson, inventoryItem);
2680adc4f0dbSShawn McCarney                 }
2681de629b6eSShawn McCarney             }
268281ce609eSEd Tanous             if (sensorsAsyncResp.use_count() == 1)
268349c53ac9SJohnathan Mantey             {
268481ce609eSEd Tanous                 sortJSONResponse(sensorsAsyncResp);
268581ce609eSEd Tanous                 if (sensorsAsyncResp->chassisSubNode == sensors::node::thermal)
26868bd25ccdSJames Feist                 {
268781ce609eSEd Tanous                     populateFanRedundancy(sensorsAsyncResp);
26888bd25ccdSJames Feist                 }
268949c53ac9SJohnathan Mantey             }
2690de629b6eSShawn McCarney             BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit";
2691de629b6eSShawn McCarney         };
2692de629b6eSShawn McCarney 
2693de629b6eSShawn McCarney         // Find DBus object path that implements ObjectManager for the current
2694de629b6eSShawn McCarney         // connection.  If no mapping found, default to "/".
26958fb49dd6SShawn McCarney         auto iter = objectMgrPaths->find(connection);
2696de629b6eSShawn McCarney         const std::string& objectMgrPath =
26978fb49dd6SShawn McCarney             (iter != objectMgrPaths->end()) ? iter->second : "/";
2698de629b6eSShawn McCarney         BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
2699de629b6eSShawn McCarney                          << objectMgrPath;
2700de629b6eSShawn McCarney 
2701de629b6eSShawn McCarney         crow::connections::systemBus->async_method_call(
2702de629b6eSShawn McCarney             getManagedObjectsCb, connection, objectMgrPath,
2703de629b6eSShawn McCarney             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
270423a21a1cSEd Tanous     }
2705de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getSensorData exit";
2706de629b6eSShawn McCarney }
2707de629b6eSShawn McCarney 
270823a21a1cSEd Tanous inline void processSensorList(
270981ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2710b5a76932SEd Tanous     const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames)
27111abe55efSEd Tanous {
271295a3ecadSAnthony Wilson     auto getConnectionCb =
271381ce609eSEd Tanous         [sensorsAsyncResp, sensorNames](
271495a3ecadSAnthony Wilson             const boost::container::flat_set<std::string>& connections) {
271555c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "getConnectionCb enter";
2716de629b6eSShawn McCarney             auto getObjectManagerPathsCb =
271781ce609eSEd Tanous                 [sensorsAsyncResp, sensorNames,
2718b5a76932SEd Tanous                  connections](const std::shared_ptr<boost::container::flat_map<
2719b5a76932SEd Tanous                                   std::string, std::string>>& objectMgrPaths) {
2720de629b6eSShawn McCarney                     BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb enter";
2721adc4f0dbSShawn McCarney                     auto getInventoryItemsCb =
272281ce609eSEd Tanous                         [sensorsAsyncResp, sensorNames, connections,
2723adc4f0dbSShawn McCarney                          objectMgrPaths](
2724f23b7296SEd Tanous                             const std::shared_ptr<std::vector<InventoryItem>>&
2725adc4f0dbSShawn McCarney                                 inventoryItems) {
2726adc4f0dbSShawn McCarney                             BMCWEB_LOG_DEBUG << "getInventoryItemsCb enter";
272749c53ac9SJohnathan Mantey                             // Get sensor data and store results in JSON
272881ce609eSEd Tanous                             getSensorData(sensorsAsyncResp, sensorNames,
2729adc4f0dbSShawn McCarney                                           connections, objectMgrPaths,
2730f23b7296SEd Tanous                                           inventoryItems);
2731adc4f0dbSShawn McCarney                             BMCWEB_LOG_DEBUG << "getInventoryItemsCb exit";
2732adc4f0dbSShawn McCarney                         };
2733adc4f0dbSShawn McCarney 
2734adc4f0dbSShawn McCarney                     // Get inventory items associated with sensors
273581ce609eSEd Tanous                     getInventoryItems(sensorsAsyncResp, sensorNames,
2736adc4f0dbSShawn McCarney                                       objectMgrPaths,
2737adc4f0dbSShawn McCarney                                       std::move(getInventoryItemsCb));
2738adc4f0dbSShawn McCarney 
2739de629b6eSShawn McCarney                     BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb exit";
274008777fb0SLewanczyk, Dawid                 };
2741de629b6eSShawn McCarney 
274249c53ac9SJohnathan Mantey             // Get mapping from connection names to the DBus object
274349c53ac9SJohnathan Mantey             // paths that implement the ObjectManager interface
274481ce609eSEd Tanous             getObjectManagerPaths(sensorsAsyncResp,
2745de629b6eSShawn McCarney                                   std::move(getObjectManagerPathsCb));
274655c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "getConnectionCb exit";
274708777fb0SLewanczyk, Dawid         };
2748de629b6eSShawn McCarney 
2749de629b6eSShawn McCarney     // Get set of connections that provide sensor values
275081ce609eSEd Tanous     getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb));
275195a3ecadSAnthony Wilson }
275295a3ecadSAnthony Wilson 
275395a3ecadSAnthony Wilson /**
275495a3ecadSAnthony Wilson  * @brief Entry point for retrieving sensors data related to requested
275595a3ecadSAnthony Wilson  *        chassis.
275695a3ecadSAnthony Wilson  * @param SensorsAsyncResp   Pointer to object holding response data
275795a3ecadSAnthony Wilson  */
2758b5a76932SEd Tanous inline void
275981ce609eSEd Tanous     getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
276095a3ecadSAnthony Wilson {
276195a3ecadSAnthony Wilson     BMCWEB_LOG_DEBUG << "getChassisData enter";
276295a3ecadSAnthony Wilson     auto getChassisCb =
276381ce609eSEd Tanous         [sensorsAsyncResp](
2764f23b7296SEd Tanous             const std::shared_ptr<boost::container::flat_set<std::string>>&
276595a3ecadSAnthony Wilson                 sensorNames) {
276695a3ecadSAnthony Wilson             BMCWEB_LOG_DEBUG << "getChassisCb enter";
276781ce609eSEd Tanous             processSensorList(sensorsAsyncResp, sensorNames);
276855c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "getChassisCb exit";
276908777fb0SLewanczyk, Dawid         };
27708d1b46d7Szhanghch05     sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] =
27718d1b46d7Szhanghch05         nlohmann::json::array();
277208777fb0SLewanczyk, Dawid 
277326f03899SShawn McCarney     // Get set of sensors in chassis
277481ce609eSEd Tanous     getChassis(sensorsAsyncResp, std::move(getChassisCb));
277555c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "getChassisData exit";
2776271584abSEd Tanous }
277708777fb0SLewanczyk, Dawid 
2778413961deSRichard Marian Thomaiyar /**
277949c53ac9SJohnathan Mantey  * @brief Find the requested sensorName in the list of all sensors supplied by
278049c53ac9SJohnathan Mantey  * the chassis node
278149c53ac9SJohnathan Mantey  *
278249c53ac9SJohnathan Mantey  * @param sensorName   The sensor name supplied in the PATCH request
278349c53ac9SJohnathan Mantey  * @param sensorsList  The list of sensors managed by the chassis node
278449c53ac9SJohnathan Mantey  * @param sensorsModified  The list of sensors that were found as a result of
278549c53ac9SJohnathan Mantey  *                         repeated calls to this function
278649c53ac9SJohnathan Mantey  */
278723a21a1cSEd Tanous inline bool findSensorNameUsingSensorPath(
27880a86febdSRichard Marian Thomaiyar     std::string_view sensorName,
278949c53ac9SJohnathan Mantey     boost::container::flat_set<std::string>& sensorsList,
279049c53ac9SJohnathan Mantey     boost::container::flat_set<std::string>& sensorsModified)
279149c53ac9SJohnathan Mantey {
279228aa8de5SGeorge Liu     for (auto& chassisSensor : sensorsList)
279349c53ac9SJohnathan Mantey     {
279428aa8de5SGeorge Liu         sdbusplus::message::object_path path(chassisSensor);
2795b00dcc27SEd Tanous         std::string thisSensorName = path.filename();
279628aa8de5SGeorge Liu         if (thisSensorName.empty())
279749c53ac9SJohnathan Mantey         {
279849c53ac9SJohnathan Mantey             continue;
279949c53ac9SJohnathan Mantey         }
280049c53ac9SJohnathan Mantey         if (thisSensorName == sensorName)
280149c53ac9SJohnathan Mantey         {
280249c53ac9SJohnathan Mantey             sensorsModified.emplace(chassisSensor);
280349c53ac9SJohnathan Mantey             return true;
280449c53ac9SJohnathan Mantey         }
280549c53ac9SJohnathan Mantey     }
280649c53ac9SJohnathan Mantey     return false;
280749c53ac9SJohnathan Mantey }
280849c53ac9SJohnathan Mantey 
280949c53ac9SJohnathan Mantey /**
2810413961deSRichard Marian Thomaiyar  * @brief Entry point for overriding sensor values of given sensor
2811413961deSRichard Marian Thomaiyar  *
28128d1b46d7Szhanghch05  * @param sensorAsyncResp   response object
28134bb3dc34SCarol Wang  * @param allCollections   Collections extract from sensors' request patch info
2814413961deSRichard Marian Thomaiyar  * @param chassisSubNode   Chassis Node for which the query has to happen
2815413961deSRichard Marian Thomaiyar  */
281623a21a1cSEd Tanous inline void setSensorsOverride(
2817b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
28184bb3dc34SCarol Wang     std::unordered_map<std::string, std::vector<nlohmann::json>>&
2819397fd61fSjayaprakash Mutyala         allCollections)
2820413961deSRichard Marian Thomaiyar {
282170d1d0aaSjayaprakash Mutyala     BMCWEB_LOG_INFO << "setSensorsOverride for subNode"
28224bb3dc34SCarol Wang                     << sensorAsyncResp->chassisSubNode << "\n";
2823413961deSRichard Marian Thomaiyar 
2824f65af9e8SRichard Marian Thomaiyar     const char* propertyValueName;
2825f65af9e8SRichard Marian Thomaiyar     std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
2826413961deSRichard Marian Thomaiyar     std::string memberId;
2827413961deSRichard Marian Thomaiyar     double value;
2828f65af9e8SRichard Marian Thomaiyar     for (auto& collectionItems : allCollections)
2829f65af9e8SRichard Marian Thomaiyar     {
2830f65af9e8SRichard Marian Thomaiyar         if (collectionItems.first == "Temperatures")
2831f65af9e8SRichard Marian Thomaiyar         {
2832f65af9e8SRichard Marian Thomaiyar             propertyValueName = "ReadingCelsius";
2833f65af9e8SRichard Marian Thomaiyar         }
2834f65af9e8SRichard Marian Thomaiyar         else if (collectionItems.first == "Fans")
2835f65af9e8SRichard Marian Thomaiyar         {
2836f65af9e8SRichard Marian Thomaiyar             propertyValueName = "Reading";
2837f65af9e8SRichard Marian Thomaiyar         }
2838f65af9e8SRichard Marian Thomaiyar         else
2839f65af9e8SRichard Marian Thomaiyar         {
2840f65af9e8SRichard Marian Thomaiyar             propertyValueName = "ReadingVolts";
2841f65af9e8SRichard Marian Thomaiyar         }
2842f65af9e8SRichard Marian Thomaiyar         for (auto& item : collectionItems.second)
2843f65af9e8SRichard Marian Thomaiyar         {
28448d1b46d7Szhanghch05             if (!json_util::readJson(item, sensorAsyncResp->asyncResp->res,
28458d1b46d7Szhanghch05                                      "MemberId", memberId, propertyValueName,
28468d1b46d7Szhanghch05                                      value))
2847413961deSRichard Marian Thomaiyar             {
2848413961deSRichard Marian Thomaiyar                 return;
2849413961deSRichard Marian Thomaiyar             }
2850f65af9e8SRichard Marian Thomaiyar             overrideMap.emplace(memberId,
2851f65af9e8SRichard Marian Thomaiyar                                 std::make_pair(value, collectionItems.first));
2852f65af9e8SRichard Marian Thomaiyar         }
2853f65af9e8SRichard Marian Thomaiyar     }
28544bb3dc34SCarol Wang 
2855b5a76932SEd Tanous     auto getChassisSensorListCb = [sensorAsyncResp, overrideMap](
2856b5a76932SEd Tanous                                       const std::shared_ptr<
285749c53ac9SJohnathan Mantey                                           boost::container::flat_set<
2858b5a76932SEd Tanous                                               std::string>>& sensorsList) {
285949c53ac9SJohnathan Mantey         // Match sensor names in the PATCH request to those managed by the
286049c53ac9SJohnathan Mantey         // chassis node
286149c53ac9SJohnathan Mantey         const std::shared_ptr<boost::container::flat_set<std::string>>
286249c53ac9SJohnathan Mantey             sensorNames =
286349c53ac9SJohnathan Mantey                 std::make_shared<boost::container::flat_set<std::string>>();
2864f65af9e8SRichard Marian Thomaiyar         for (const auto& item : overrideMap)
2865413961deSRichard Marian Thomaiyar         {
2866f65af9e8SRichard Marian Thomaiyar             const auto& sensor = item.first;
286749c53ac9SJohnathan Mantey             if (!findSensorNameUsingSensorPath(sensor, *sensorsList,
286849c53ac9SJohnathan Mantey                                                *sensorNames))
2869f65af9e8SRichard Marian Thomaiyar             {
2870f65af9e8SRichard Marian Thomaiyar                 BMCWEB_LOG_INFO << "Unable to find memberId " << item.first;
28718d1b46d7Szhanghch05                 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2872f65af9e8SRichard Marian Thomaiyar                                            item.second.second, item.first);
2873413961deSRichard Marian Thomaiyar                 return;
2874413961deSRichard Marian Thomaiyar             }
2875f65af9e8SRichard Marian Thomaiyar         }
2876413961deSRichard Marian Thomaiyar         // Get the connection to which the memberId belongs
2877413961deSRichard Marian Thomaiyar         auto getObjectsWithConnectionCb =
2878f65af9e8SRichard Marian Thomaiyar             [sensorAsyncResp, overrideMap](
2879cb13a392SEd Tanous                 const boost::container::flat_set<std::string>& /*connections*/,
2880413961deSRichard Marian Thomaiyar                 const std::set<std::pair<std::string, std::string>>&
2881413961deSRichard Marian Thomaiyar                     objectsWithConnection) {
2882f65af9e8SRichard Marian Thomaiyar                 if (objectsWithConnection.size() != overrideMap.size())
2883413961deSRichard Marian Thomaiyar                 {
2884413961deSRichard Marian Thomaiyar                     BMCWEB_LOG_INFO
2885f65af9e8SRichard Marian Thomaiyar                         << "Unable to find all objects with proper connection "
2886f65af9e8SRichard Marian Thomaiyar                         << objectsWithConnection.size() << " requested "
2887f65af9e8SRichard Marian Thomaiyar                         << overrideMap.size() << "\n";
2888413961deSRichard Marian Thomaiyar                     messages::resourceNotFound(
28898d1b46d7Szhanghch05                         sensorAsyncResp->asyncResp->res,
2890a0ec28b6SAdrian Ambrożewicz                         sensorAsyncResp->chassisSubNode ==
2891a0ec28b6SAdrian Ambrożewicz                                 sensors::node::thermal
2892413961deSRichard Marian Thomaiyar                             ? "Temperatures"
2893413961deSRichard Marian Thomaiyar                             : "Voltages",
2894f65af9e8SRichard Marian Thomaiyar                         "Count");
2895f65af9e8SRichard Marian Thomaiyar                     return;
2896f65af9e8SRichard Marian Thomaiyar                 }
2897f65af9e8SRichard Marian Thomaiyar                 for (const auto& item : objectsWithConnection)
2898f65af9e8SRichard Marian Thomaiyar                 {
289928aa8de5SGeorge Liu                     sdbusplus::message::object_path path(item.first);
290028aa8de5SGeorge Liu                     std::string sensorName = path.filename();
290128aa8de5SGeorge Liu                     if (sensorName.empty())
2902f65af9e8SRichard Marian Thomaiyar                     {
29038d1b46d7Szhanghch05                         messages::internalError(
29048d1b46d7Szhanghch05                             sensorAsyncResp->asyncResp->res);
2905f65af9e8SRichard Marian Thomaiyar                         return;
2906f65af9e8SRichard Marian Thomaiyar                     }
2907f65af9e8SRichard Marian Thomaiyar 
2908f65af9e8SRichard Marian Thomaiyar                     const auto& iterator = overrideMap.find(sensorName);
2909f65af9e8SRichard Marian Thomaiyar                     if (iterator == overrideMap.end())
2910f65af9e8SRichard Marian Thomaiyar                     {
2911f65af9e8SRichard Marian Thomaiyar                         BMCWEB_LOG_INFO << "Unable to find sensor object"
2912f65af9e8SRichard Marian Thomaiyar                                         << item.first << "\n";
29138d1b46d7Szhanghch05                         messages::internalError(
29148d1b46d7Szhanghch05                             sensorAsyncResp->asyncResp->res);
2915413961deSRichard Marian Thomaiyar                         return;
2916413961deSRichard Marian Thomaiyar                     }
2917413961deSRichard Marian Thomaiyar                     crow::connections::systemBus->async_method_call(
2918f65af9e8SRichard Marian Thomaiyar                         [sensorAsyncResp](const boost::system::error_code ec) {
2919413961deSRichard Marian Thomaiyar                             if (ec)
2920413961deSRichard Marian Thomaiyar                             {
2921413961deSRichard Marian Thomaiyar                                 BMCWEB_LOG_DEBUG
2922f65af9e8SRichard Marian Thomaiyar                                     << "setOverrideValueStatus DBUS error: "
2923413961deSRichard Marian Thomaiyar                                     << ec;
29248d1b46d7Szhanghch05                                 messages::internalError(
29258d1b46d7Szhanghch05                                     sensorAsyncResp->asyncResp->res);
2926413961deSRichard Marian Thomaiyar                                 return;
2927413961deSRichard Marian Thomaiyar                             }
2928413961deSRichard Marian Thomaiyar                         },
2929f65af9e8SRichard Marian Thomaiyar                         item.second, item.first,
2930413961deSRichard Marian Thomaiyar                         "org.freedesktop.DBus.Properties", "Set",
2931413961deSRichard Marian Thomaiyar                         "xyz.openbmc_project.Sensor.Value", "Value",
293219bd78d9SPatrick Williams                         std::variant<double>(iterator->second.first));
2933f65af9e8SRichard Marian Thomaiyar                 }
2934413961deSRichard Marian Thomaiyar             };
2935413961deSRichard Marian Thomaiyar         // Get object with connection for the given sensor name
2936413961deSRichard Marian Thomaiyar         getObjectsWithConnection(sensorAsyncResp, sensorNames,
2937413961deSRichard Marian Thomaiyar                                  std::move(getObjectsWithConnectionCb));
2938413961deSRichard Marian Thomaiyar     };
2939413961deSRichard Marian Thomaiyar     // get full sensor list for the given chassisId and cross verify the sensor.
2940413961deSRichard Marian Thomaiyar     getChassis(sensorAsyncResp, std::move(getChassisSensorListCb));
2941413961deSRichard Marian Thomaiyar }
2942413961deSRichard Marian Thomaiyar 
2943a0ec28b6SAdrian Ambrożewicz /**
2944a0ec28b6SAdrian Ambrożewicz  * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
2945a0ec28b6SAdrian Ambrożewicz  * path of the sensor.
2946a0ec28b6SAdrian Ambrożewicz  *
2947a0ec28b6SAdrian Ambrożewicz  * Function builds valid Redfish response for sensor query of given chassis and
2948a0ec28b6SAdrian Ambrożewicz  * node. It then builds metadata about Redfish<->D-Bus correlations and provides
2949a0ec28b6SAdrian Ambrożewicz  * it to caller in a callback.
2950a0ec28b6SAdrian Ambrożewicz  *
2951a0ec28b6SAdrian Ambrożewicz  * @param chassis   Chassis for which retrieval should be performed
2952a0ec28b6SAdrian Ambrożewicz  * @param node  Node (group) of sensors. See sensors::node for supported values
2953a0ec28b6SAdrian Ambrożewicz  * @param mapComplete   Callback to be called with retrieval result
2954a0ec28b6SAdrian Ambrożewicz  */
295523a21a1cSEd Tanous inline void retrieveUriToDbusMap(const std::string& chassis,
295623a21a1cSEd Tanous                                  const std::string& node,
2957a0ec28b6SAdrian Ambrożewicz                                  SensorsAsyncResp::DataCompleteCb&& mapComplete)
2958a0ec28b6SAdrian Ambrożewicz {
2959c2bf7f99SWludzik, Jozef     auto pathIt = sensors::dbus::paths.find(node);
2960c2bf7f99SWludzik, Jozef     if (pathIt == sensors::dbus::paths.end())
2961a0ec28b6SAdrian Ambrożewicz     {
2962a0ec28b6SAdrian Ambrożewicz         BMCWEB_LOG_ERROR << "Wrong node provided : " << node;
2963a0ec28b6SAdrian Ambrożewicz         mapComplete(boost::beast::http::status::bad_request, {});
2964a0ec28b6SAdrian Ambrożewicz         return;
2965a0ec28b6SAdrian Ambrożewicz     }
2966d51e072fSKrzysztof Grobelny 
2967d51e072fSKrzysztof Grobelny     auto res = std::make_shared<crow::Response>();
2968d51e072fSKrzysztof Grobelny     auto asyncResp = std::make_shared<bmcweb::AsyncResp>(*res);
2969a0ec28b6SAdrian Ambrożewicz     auto callback =
2970d51e072fSKrzysztof Grobelny         [res, asyncResp, mapCompleteCb{std::move(mapComplete)}](
2971a0ec28b6SAdrian Ambrożewicz             const boost::beast::http::status status,
2972a0ec28b6SAdrian Ambrożewicz             const boost::container::flat_map<std::string, std::string>&
2973a0ec28b6SAdrian Ambrożewicz                 uriToDbus) { mapCompleteCb(status, uriToDbus); };
2974a0ec28b6SAdrian Ambrożewicz 
2975a0ec28b6SAdrian Ambrożewicz     auto resp = std::make_shared<SensorsAsyncResp>(
2976d51e072fSKrzysztof Grobelny         asyncResp, chassis, pathIt->second, node, std::move(callback));
2977a0ec28b6SAdrian Ambrożewicz     getChassisData(resp);
2978a0ec28b6SAdrian Ambrożewicz }
2979a0ec28b6SAdrian Ambrożewicz 
29807e860f15SJohn Edward Broadbent inline void requestRoutesSensorCollection(App& app)
298195a3ecadSAnthony Wilson {
29827e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/")
2983ed398213SEd Tanous         .privileges(redfish::privileges::getSensorCollection)
29847e860f15SJohn Edward Broadbent         .methods(
29857e860f15SJohn Edward Broadbent             boost::beast::http::verb::get)([](const crow::Request&,
29867e860f15SJohn Edward Broadbent                                               const std::shared_ptr<
29877e860f15SJohn Edward Broadbent                                                   bmcweb::AsyncResp>& aResp,
29887e860f15SJohn Edward Broadbent                                               const std::string& chassisId) {
298995a3ecadSAnthony Wilson             BMCWEB_LOG_DEBUG << "SensorCollection doGet enter";
29908d1b46d7Szhanghch05 
299195a3ecadSAnthony Wilson             std::shared_ptr<SensorsAsyncResp> asyncResp =
2992a0ec28b6SAdrian Ambrożewicz                 std::make_shared<SensorsAsyncResp>(
29938d1b46d7Szhanghch05                     aResp, chassisId,
29948d1b46d7Szhanghch05                     sensors::dbus::paths.at(sensors::node::sensors),
2995a0ec28b6SAdrian Ambrożewicz                     sensors::node::sensors);
299695a3ecadSAnthony Wilson 
299795a3ecadSAnthony Wilson             auto getChassisCb =
2998b5a76932SEd Tanous                 [asyncResp](
29997e860f15SJohn Edward Broadbent                     const std::shared_ptr<
30007e860f15SJohn Edward Broadbent                         boost::container::flat_set<std::string>>& sensorNames) {
300195a3ecadSAnthony Wilson                     BMCWEB_LOG_DEBUG << "getChassisCb enter";
300295a3ecadSAnthony Wilson 
300395a3ecadSAnthony Wilson                     nlohmann::json& entriesArray =
30048d1b46d7Szhanghch05                         asyncResp->asyncResp->res.jsonValue["Members"];
300595a3ecadSAnthony Wilson                     for (auto& sensor : *sensorNames)
300695a3ecadSAnthony Wilson                     {
300795a3ecadSAnthony Wilson                         BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor;
300895a3ecadSAnthony Wilson 
300928aa8de5SGeorge Liu                         sdbusplus::message::object_path path(sensor);
301028aa8de5SGeorge Liu                         std::string sensorName = path.filename();
301128aa8de5SGeorge Liu                         if (sensorName.empty())
301295a3ecadSAnthony Wilson                         {
30137e860f15SJohn Edward Broadbent                             BMCWEB_LOG_ERROR << "Invalid sensor path: "
30147e860f15SJohn Edward Broadbent                                              << sensor;
30158d1b46d7Szhanghch05                             messages::internalError(asyncResp->asyncResp->res);
301695a3ecadSAnthony Wilson                             return;
301795a3ecadSAnthony Wilson                         }
301895a3ecadSAnthony Wilson                         entriesArray.push_back(
30197e860f15SJohn Edward Broadbent                             {{"@odata.id", "/redfish/v1/Chassis/" +
30207e860f15SJohn Edward Broadbent                                                asyncResp->chassisId + "/" +
30217e860f15SJohn Edward Broadbent                                                asyncResp->chassisSubNode + "/" +
30227e860f15SJohn Edward Broadbent                                                sensorName}});
302395a3ecadSAnthony Wilson                     }
302495a3ecadSAnthony Wilson 
30258d1b46d7Szhanghch05                     asyncResp->asyncResp->res.jsonValue["Members@odata.count"] =
302695a3ecadSAnthony Wilson                         entriesArray.size();
302795a3ecadSAnthony Wilson                     BMCWEB_LOG_DEBUG << "getChassisCb exit";
302895a3ecadSAnthony Wilson                 };
302995a3ecadSAnthony Wilson 
303095a3ecadSAnthony Wilson             // Get set of sensors in chassis
303195a3ecadSAnthony Wilson             getChassis(asyncResp, std::move(getChassisCb));
303295a3ecadSAnthony Wilson             BMCWEB_LOG_DEBUG << "SensorCollection doGet exit";
30337e860f15SJohn Edward Broadbent         });
303495a3ecadSAnthony Wilson }
303595a3ecadSAnthony Wilson 
30367e860f15SJohn Edward Broadbent inline void requestRoutesSensor(App& app)
303795a3ecadSAnthony Wilson {
30387e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/")
3039ed398213SEd Tanous         .privileges(redfish::privileges::getSensor)
30407e860f15SJohn Edward Broadbent         .methods(
30417e860f15SJohn Edward Broadbent             boost::beast::http::verb::get)([](const crow::Request&,
30427e860f15SJohn Edward Broadbent                                               const std::shared_ptr<
30437e860f15SJohn Edward Broadbent                                                   bmcweb::AsyncResp>& aResp,
30447e860f15SJohn Edward Broadbent                                               const std::string& chassisId,
30457e860f15SJohn Edward Broadbent                                               const std::string& sensorName) {
304695a3ecadSAnthony Wilson             BMCWEB_LOG_DEBUG << "Sensor doGet enter";
304795a3ecadSAnthony Wilson             std::shared_ptr<SensorsAsyncResp> asyncResp =
30488d1b46d7Szhanghch05                 std::make_shared<SensorsAsyncResp>(aResp, chassisId,
3049a0ec28b6SAdrian Ambrożewicz                                                    std::vector<const char*>(),
3050a0ec28b6SAdrian Ambrożewicz                                                    sensors::node::sensors);
305195a3ecadSAnthony Wilson 
305295a3ecadSAnthony Wilson             const std::array<const char*, 1> interfaces = {
305395a3ecadSAnthony Wilson                 "xyz.openbmc_project.Sensor.Value"};
305495a3ecadSAnthony Wilson 
305595a3ecadSAnthony Wilson             // Get a list of all of the sensors that implement Sensor.Value
305695a3ecadSAnthony Wilson             // and get the path and service name associated with the sensor
305795a3ecadSAnthony Wilson             crow::connections::systemBus->async_method_call(
305895a3ecadSAnthony Wilson                 [asyncResp, sensorName](const boost::system::error_code ec,
305995a3ecadSAnthony Wilson                                         const GetSubTreeType& subtree) {
306095a3ecadSAnthony Wilson                     BMCWEB_LOG_DEBUG << "respHandler1 enter";
306195a3ecadSAnthony Wilson                     if (ec)
306295a3ecadSAnthony Wilson                     {
30638d1b46d7Szhanghch05                         messages::internalError(asyncResp->asyncResp->res);
30647e860f15SJohn Edward Broadbent                         BMCWEB_LOG_ERROR
30657e860f15SJohn Edward Broadbent                             << "Sensor getSensorPaths resp_handler: "
306695a3ecadSAnthony Wilson                             << "Dbus error " << ec;
306795a3ecadSAnthony Wilson                         return;
306895a3ecadSAnthony Wilson                     }
306995a3ecadSAnthony Wilson 
307095a3ecadSAnthony Wilson                     GetSubTreeType::const_iterator it = std::find_if(
307195a3ecadSAnthony Wilson                         subtree.begin(), subtree.end(),
307295a3ecadSAnthony Wilson                         [sensorName](
307395a3ecadSAnthony Wilson                             const std::pair<
307495a3ecadSAnthony Wilson                                 std::string,
30757e860f15SJohn Edward Broadbent                                 std::vector<std::pair<
30767e860f15SJohn Edward Broadbent                                     std::string, std::vector<std::string>>>>&
307795a3ecadSAnthony Wilson                                 object) {
307828aa8de5SGeorge Liu                             sdbusplus::message::object_path path(object.first);
307928aa8de5SGeorge Liu                             std::string name = path.filename();
308028aa8de5SGeorge Liu                             if (name.empty())
308195a3ecadSAnthony Wilson                             {
308295a3ecadSAnthony Wilson                                 BMCWEB_LOG_ERROR << "Invalid sensor path: "
308328aa8de5SGeorge Liu                                                  << object.first;
308495a3ecadSAnthony Wilson                                 return false;
308595a3ecadSAnthony Wilson                             }
308695a3ecadSAnthony Wilson 
308795a3ecadSAnthony Wilson                             return name == sensorName;
308895a3ecadSAnthony Wilson                         });
308995a3ecadSAnthony Wilson 
309095a3ecadSAnthony Wilson                     if (it == subtree.end())
309195a3ecadSAnthony Wilson                     {
309295a3ecadSAnthony Wilson                         BMCWEB_LOG_ERROR << "Could not find path for sensor: "
309395a3ecadSAnthony Wilson                                          << sensorName;
30948d1b46d7Szhanghch05                         messages::resourceNotFound(asyncResp->asyncResp->res,
30958d1b46d7Szhanghch05                                                    "Sensor", sensorName);
309695a3ecadSAnthony Wilson                         return;
309795a3ecadSAnthony Wilson                     }
309895a3ecadSAnthony Wilson                     std::string_view sensorPath = (*it).first;
309995a3ecadSAnthony Wilson                     BMCWEB_LOG_DEBUG << "Found sensor path for sensor '"
310095a3ecadSAnthony Wilson                                      << sensorName << "': " << sensorPath;
310195a3ecadSAnthony Wilson 
31027e860f15SJohn Edward Broadbent                     const std::shared_ptr<
31037e860f15SJohn Edward Broadbent                         boost::container::flat_set<std::string>>
310495a3ecadSAnthony Wilson                         sensorList = std::make_shared<
310595a3ecadSAnthony Wilson                             boost::container::flat_set<std::string>>();
310695a3ecadSAnthony Wilson 
310795a3ecadSAnthony Wilson                     sensorList->emplace(sensorPath);
310895a3ecadSAnthony Wilson                     processSensorList(asyncResp, sensorList);
310995a3ecadSAnthony Wilson                     BMCWEB_LOG_DEBUG << "respHandler1 exit";
311095a3ecadSAnthony Wilson                 },
311195a3ecadSAnthony Wilson                 "xyz.openbmc_project.ObjectMapper",
311295a3ecadSAnthony Wilson                 "/xyz/openbmc_project/object_mapper",
311395a3ecadSAnthony Wilson                 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
311495a3ecadSAnthony Wilson                 "/xyz/openbmc_project/sensors", 2, interfaces);
31157e860f15SJohn Edward Broadbent         });
311695a3ecadSAnthony Wilson }
311795a3ecadSAnthony Wilson 
311808777fb0SLewanczyk, Dawid } // namespace redfish
3119