xref: /openbmc/bmcweb/features/redfish/lib/sensors.hpp (revision 5deabed966f0ae5953dfb3a250a42b0046257ee8)
108777fb0SLewanczyk, Dawid /*
208777fb0SLewanczyk, Dawid // Copyright (c) 2018 Intel Corporation
308777fb0SLewanczyk, Dawid //
408777fb0SLewanczyk, Dawid // Licensed under the Apache License, Version 2.0 (the "License");
508777fb0SLewanczyk, Dawid // you may not use this file except in compliance with the License.
608777fb0SLewanczyk, Dawid // You may obtain a copy of the License at
708777fb0SLewanczyk, Dawid //
808777fb0SLewanczyk, Dawid //      http://www.apache.org/licenses/LICENSE-2.0
908777fb0SLewanczyk, Dawid //
1008777fb0SLewanczyk, Dawid // Unless required by applicable law or agreed to in writing, software
1108777fb0SLewanczyk, Dawid // distributed under the License is distributed on an "AS IS" BASIS,
1208777fb0SLewanczyk, Dawid // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1308777fb0SLewanczyk, Dawid // See the License for the specific language governing permissions and
1408777fb0SLewanczyk, Dawid // limitations under the License.
1508777fb0SLewanczyk, Dawid */
1608777fb0SLewanczyk, Dawid #pragma once
1708777fb0SLewanczyk, Dawid 
187e860f15SJohn Edward Broadbent #include <app.hpp>
1908777fb0SLewanczyk, Dawid #include <boost/algorithm/string/predicate.hpp>
2008777fb0SLewanczyk, Dawid #include <boost/algorithm/string/split.hpp>
2108777fb0SLewanczyk, Dawid #include <boost/container/flat_map.hpp>
2208777fb0SLewanczyk, Dawid #include <boost/range/algorithm/replace_copy_if.hpp>
231abe55efSEd Tanous #include <dbus_singleton.hpp>
24168e20c1SEd Tanous #include <dbus_utility.hpp>
2545ca1b86SEd Tanous #include <query.hpp>
26ed398213SEd Tanous #include <registries/privilege_registry.hpp>
271e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp>
28413961deSRichard Marian Thomaiyar #include <utils/json_utils.hpp>
29928fefb9SNan Zhou #include <utils/query_param.hpp>
301214b7e7SGunnar Mills 
311214b7e7SGunnar Mills #include <cmath>
32b5a76932SEd Tanous #include <utility>
33abf2add6SEd Tanous #include <variant>
3408777fb0SLewanczyk, Dawid 
351abe55efSEd Tanous namespace redfish
361abe55efSEd Tanous {
3708777fb0SLewanczyk, Dawid 
38a0ec28b6SAdrian Ambrożewicz namespace sensors
39a0ec28b6SAdrian Ambrożewicz {
40a0ec28b6SAdrian Ambrożewicz namespace node
41a0ec28b6SAdrian Ambrożewicz {
42a0ec28b6SAdrian Ambrożewicz static constexpr std::string_view power = "Power";
43a0ec28b6SAdrian Ambrożewicz static constexpr std::string_view sensors = "Sensors";
44a0ec28b6SAdrian Ambrożewicz static constexpr std::string_view thermal = "Thermal";
45a0ec28b6SAdrian Ambrożewicz } // namespace node
46a0ec28b6SAdrian Ambrożewicz 
47a0ec28b6SAdrian Ambrożewicz namespace dbus
48a0ec28b6SAdrian Ambrożewicz {
49c2bf7f99SWludzik, Jozef 
50a0ec28b6SAdrian Ambrożewicz static const boost::container::flat_map<std::string_view,
51a0ec28b6SAdrian Ambrożewicz                                         std::vector<const char*>>
52c2bf7f99SWludzik, Jozef     paths = {{node::power,
53a0ec28b6SAdrian Ambrożewicz               {"/xyz/openbmc_project/sensors/voltage",
54a0ec28b6SAdrian Ambrożewicz                "/xyz/openbmc_project/sensors/power"}},
55a0ec28b6SAdrian Ambrożewicz              {node::sensors,
56a0ec28b6SAdrian Ambrożewicz               {"/xyz/openbmc_project/sensors/power",
57a0ec28b6SAdrian Ambrożewicz                "/xyz/openbmc_project/sensors/current",
587088690cSBasheer Ahmed Muddebihal                "/xyz/openbmc_project/sensors/airflow",
59*5deabed9SGunnar Mills                "/xyz/openbmc_project/sensors/humidity",
60e8204933SGeorge Liu #ifdef BMCWEB_NEW_POWERSUBSYSTEM_THERMALSUBSYSTEM
61e8204933SGeorge Liu                "/xyz/openbmc_project/sensors/voltage",
62e8204933SGeorge Liu                "/xyz/openbmc_project/sensors/fan_tach",
63e8204933SGeorge Liu                "/xyz/openbmc_project/sensors/temperature",
64e8204933SGeorge Liu                "/xyz/openbmc_project/sensors/fan_pwm",
65e8204933SGeorge Liu                "/xyz/openbmc_project/sensors/altitude",
66e8204933SGeorge Liu                "/xyz/openbmc_project/sensors/energy",
67e8204933SGeorge Liu #endif
68a0ec28b6SAdrian Ambrożewicz                "/xyz/openbmc_project/sensors/utilization"}},
69a0ec28b6SAdrian Ambrożewicz              {node::thermal,
70a0ec28b6SAdrian Ambrożewicz               {"/xyz/openbmc_project/sensors/fan_tach",
71a0ec28b6SAdrian Ambrożewicz                "/xyz/openbmc_project/sensors/temperature",
72a0ec28b6SAdrian Ambrożewicz                "/xyz/openbmc_project/sensors/fan_pwm"}}};
73c2bf7f99SWludzik, Jozef } // namespace dbus
74c2bf7f99SWludzik, Jozef 
75c2bf7f99SWludzik, Jozef inline const char* toReadingType(const std::string& sensorType)
76c2bf7f99SWludzik, Jozef {
77c2bf7f99SWludzik, Jozef     if (sensorType == "voltage")
78c2bf7f99SWludzik, Jozef     {
79c2bf7f99SWludzik, Jozef         return "Voltage";
80c2bf7f99SWludzik, Jozef     }
81c2bf7f99SWludzik, Jozef     if (sensorType == "power")
82c2bf7f99SWludzik, Jozef     {
83c2bf7f99SWludzik, Jozef         return "Power";
84c2bf7f99SWludzik, Jozef     }
85c2bf7f99SWludzik, Jozef     if (sensorType == "current")
86c2bf7f99SWludzik, Jozef     {
87c2bf7f99SWludzik, Jozef         return "Current";
88c2bf7f99SWludzik, Jozef     }
89c2bf7f99SWludzik, Jozef     if (sensorType == "fan_tach")
90c2bf7f99SWludzik, Jozef     {
91c2bf7f99SWludzik, Jozef         return "Rotational";
92c2bf7f99SWludzik, Jozef     }
93c2bf7f99SWludzik, Jozef     if (sensorType == "temperature")
94c2bf7f99SWludzik, Jozef     {
95c2bf7f99SWludzik, Jozef         return "Temperature";
96c2bf7f99SWludzik, Jozef     }
97c2bf7f99SWludzik, Jozef     if (sensorType == "fan_pwm" || sensorType == "utilization")
98c2bf7f99SWludzik, Jozef     {
99c2bf7f99SWludzik, Jozef         return "Percent";
100c2bf7f99SWludzik, Jozef     }
101*5deabed9SGunnar Mills     if (sensorType == "humidity")
102*5deabed9SGunnar Mills     {
103*5deabed9SGunnar Mills         return "Humidity";
104*5deabed9SGunnar Mills     }
105c2bf7f99SWludzik, Jozef     if (sensorType == "altitude")
106c2bf7f99SWludzik, Jozef     {
107c2bf7f99SWludzik, Jozef         return "Altitude";
108c2bf7f99SWludzik, Jozef     }
109c2bf7f99SWludzik, Jozef     if (sensorType == "airflow")
110c2bf7f99SWludzik, Jozef     {
111c2bf7f99SWludzik, Jozef         return "AirFlow";
112c2bf7f99SWludzik, Jozef     }
113c2bf7f99SWludzik, Jozef     if (sensorType == "energy")
114c2bf7f99SWludzik, Jozef     {
115c2bf7f99SWludzik, Jozef         return "EnergyJoules";
116c2bf7f99SWludzik, Jozef     }
117c2bf7f99SWludzik, Jozef     return "";
118c2bf7f99SWludzik, Jozef }
119c2bf7f99SWludzik, Jozef 
120c2bf7f99SWludzik, Jozef inline const char* toReadingUnits(const std::string& sensorType)
121c2bf7f99SWludzik, Jozef {
122c2bf7f99SWludzik, Jozef     if (sensorType == "voltage")
123c2bf7f99SWludzik, Jozef     {
124c2bf7f99SWludzik, Jozef         return "V";
125c2bf7f99SWludzik, Jozef     }
126c2bf7f99SWludzik, Jozef     if (sensorType == "power")
127c2bf7f99SWludzik, Jozef     {
128c2bf7f99SWludzik, Jozef         return "W";
129c2bf7f99SWludzik, Jozef     }
130c2bf7f99SWludzik, Jozef     if (sensorType == "current")
131c2bf7f99SWludzik, Jozef     {
132c2bf7f99SWludzik, Jozef         return "A";
133c2bf7f99SWludzik, Jozef     }
134c2bf7f99SWludzik, Jozef     if (sensorType == "fan_tach")
135c2bf7f99SWludzik, Jozef     {
136c2bf7f99SWludzik, Jozef         return "RPM";
137c2bf7f99SWludzik, Jozef     }
138c2bf7f99SWludzik, Jozef     if (sensorType == "temperature")
139c2bf7f99SWludzik, Jozef     {
140c2bf7f99SWludzik, Jozef         return "Cel";
141c2bf7f99SWludzik, Jozef     }
142*5deabed9SGunnar Mills     if (sensorType == "fan_pwm" || sensorType == "utilization" ||
143*5deabed9SGunnar Mills         sensorType == "humidity")
144c2bf7f99SWludzik, Jozef     {
145c2bf7f99SWludzik, Jozef         return "%";
146c2bf7f99SWludzik, Jozef     }
147c2bf7f99SWludzik, Jozef     if (sensorType == "altitude")
148c2bf7f99SWludzik, Jozef     {
149c2bf7f99SWludzik, Jozef         return "m";
150c2bf7f99SWludzik, Jozef     }
151c2bf7f99SWludzik, Jozef     if (sensorType == "airflow")
152c2bf7f99SWludzik, Jozef     {
153c2bf7f99SWludzik, Jozef         return "cft_i/min";
154c2bf7f99SWludzik, Jozef     }
155c2bf7f99SWludzik, Jozef     if (sensorType == "energy")
156c2bf7f99SWludzik, Jozef     {
157c2bf7f99SWludzik, Jozef         return "J";
158c2bf7f99SWludzik, Jozef     }
159c2bf7f99SWludzik, Jozef     return "";
160a0ec28b6SAdrian Ambrożewicz }
161a0ec28b6SAdrian Ambrożewicz } // namespace sensors
162a0ec28b6SAdrian Ambrożewicz 
16308777fb0SLewanczyk, Dawid /**
164588c3f0dSKowalski, Kamil  * SensorsAsyncResp
16508777fb0SLewanczyk, Dawid  * Gathers data needed for response processing after async calls are done
16608777fb0SLewanczyk, Dawid  */
1671abe55efSEd Tanous class SensorsAsyncResp
1681abe55efSEd Tanous {
16908777fb0SLewanczyk, Dawid   public:
170a0ec28b6SAdrian Ambrożewicz     using DataCompleteCb = std::function<void(
171a0ec28b6SAdrian Ambrożewicz         const boost::beast::http::status status,
172a0ec28b6SAdrian Ambrożewicz         const boost::container::flat_map<std::string, std::string>& uriToDbus)>;
173a0ec28b6SAdrian Ambrożewicz 
174a0ec28b6SAdrian Ambrożewicz     struct SensorData
175a0ec28b6SAdrian Ambrożewicz     {
176a0ec28b6SAdrian Ambrożewicz         const std::string name;
177a0ec28b6SAdrian Ambrożewicz         std::string uri;
178a0ec28b6SAdrian Ambrożewicz         const std::string valueKey;
179a0ec28b6SAdrian Ambrożewicz         const std::string dbusPath;
180a0ec28b6SAdrian Ambrożewicz     };
181a0ec28b6SAdrian Ambrożewicz 
1828d1b46d7Szhanghch05     SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1838d1b46d7Szhanghch05                      const std::string& chassisIdIn,
184b5a76932SEd Tanous                      const std::vector<const char*>& typesIn,
185a0ec28b6SAdrian Ambrożewicz                      const std::string_view& subNode) :
1868d1b46d7Szhanghch05         asyncResp(asyncResp),
187928fefb9SNan Zhou         chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
188928fefb9SNan Zhou         efficientExpand(false)
1891214b7e7SGunnar Mills     {}
19008777fb0SLewanczyk, Dawid 
191a0ec28b6SAdrian Ambrożewicz     // Store extra data about sensor mapping and return it in callback
1928d1b46d7Szhanghch05     SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1938d1b46d7Szhanghch05                      const std::string& chassisIdIn,
194b5a76932SEd Tanous                      const std::vector<const char*>& typesIn,
195a0ec28b6SAdrian Ambrożewicz                      const std::string_view& subNode,
196a0ec28b6SAdrian Ambrożewicz                      DataCompleteCb&& creationComplete) :
1978d1b46d7Szhanghch05         asyncResp(asyncResp),
198928fefb9SNan Zhou         chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
199928fefb9SNan Zhou         efficientExpand(false), metadata{std::vector<SensorData>()},
200a0ec28b6SAdrian Ambrożewicz         dataComplete{std::move(creationComplete)}
201a0ec28b6SAdrian Ambrożewicz     {}
202a0ec28b6SAdrian Ambrożewicz 
203928fefb9SNan Zhou     // sensor collections expand
204928fefb9SNan Zhou     SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
205928fefb9SNan Zhou                      const std::string& chassisIdIn,
206928fefb9SNan Zhou                      const std::vector<const char*>& typesIn,
207928fefb9SNan Zhou                      const std::string_view& subNode, bool efficientExpand) :
208928fefb9SNan Zhou         asyncResp(asyncResp),
209928fefb9SNan Zhou         chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
210928fefb9SNan Zhou         efficientExpand(efficientExpand)
211928fefb9SNan Zhou     {}
212928fefb9SNan Zhou 
2131abe55efSEd Tanous     ~SensorsAsyncResp()
2141abe55efSEd Tanous     {
2158d1b46d7Szhanghch05         if (asyncResp->res.result() ==
2168d1b46d7Szhanghch05             boost::beast::http::status::internal_server_error)
2171abe55efSEd Tanous         {
2181abe55efSEd Tanous             // Reset the json object to clear out any data that made it in
2191abe55efSEd Tanous             // before the error happened todo(ed) handle error condition with
2201abe55efSEd Tanous             // proper code
2218d1b46d7Szhanghch05             asyncResp->res.jsonValue = nlohmann::json::object();
22208777fb0SLewanczyk, Dawid         }
223a0ec28b6SAdrian Ambrożewicz 
224a0ec28b6SAdrian Ambrożewicz         if (dataComplete && metadata)
225a0ec28b6SAdrian Ambrożewicz         {
226a0ec28b6SAdrian Ambrożewicz             boost::container::flat_map<std::string, std::string> map;
2278d1b46d7Szhanghch05             if (asyncResp->res.result() == boost::beast::http::status::ok)
228a0ec28b6SAdrian Ambrożewicz             {
229a0ec28b6SAdrian Ambrożewicz                 for (auto& sensor : *metadata)
230a0ec28b6SAdrian Ambrożewicz                 {
231a0ec28b6SAdrian Ambrożewicz                     map.insert(std::make_pair(sensor.uri + sensor.valueKey,
232a0ec28b6SAdrian Ambrożewicz                                               sensor.dbusPath));
233a0ec28b6SAdrian Ambrożewicz                 }
234a0ec28b6SAdrian Ambrożewicz             }
2358d1b46d7Szhanghch05             dataComplete(asyncResp->res.result(), map);
236a0ec28b6SAdrian Ambrożewicz         }
23708777fb0SLewanczyk, Dawid     }
238588c3f0dSKowalski, Kamil 
239ecd6a3a2SEd Tanous     SensorsAsyncResp(const SensorsAsyncResp&) = delete;
240ecd6a3a2SEd Tanous     SensorsAsyncResp(SensorsAsyncResp&&) = delete;
241ecd6a3a2SEd Tanous     SensorsAsyncResp& operator=(const SensorsAsyncResp&) = delete;
242ecd6a3a2SEd Tanous     SensorsAsyncResp& operator=(SensorsAsyncResp&&) = delete;
243ecd6a3a2SEd Tanous 
244a0ec28b6SAdrian Ambrożewicz     void addMetadata(const nlohmann::json& sensorObject,
245a0ec28b6SAdrian Ambrożewicz                      const std::string& valueKey, const std::string& dbusPath)
246a0ec28b6SAdrian Ambrożewicz     {
247a0ec28b6SAdrian Ambrożewicz         if (metadata)
248a0ec28b6SAdrian Ambrożewicz         {
249a0ec28b6SAdrian Ambrożewicz             metadata->emplace_back(SensorData{sensorObject["Name"],
250a0ec28b6SAdrian Ambrożewicz                                               sensorObject["@odata.id"],
251a0ec28b6SAdrian Ambrożewicz                                               valueKey, dbusPath});
252a0ec28b6SAdrian Ambrożewicz         }
253a0ec28b6SAdrian Ambrożewicz     }
254a0ec28b6SAdrian Ambrożewicz 
255a0ec28b6SAdrian Ambrożewicz     void updateUri(const std::string& name, const std::string& uri)
256a0ec28b6SAdrian Ambrożewicz     {
257a0ec28b6SAdrian Ambrożewicz         if (metadata)
258a0ec28b6SAdrian Ambrożewicz         {
259a0ec28b6SAdrian Ambrożewicz             for (auto& sensor : *metadata)
260a0ec28b6SAdrian Ambrożewicz             {
261a0ec28b6SAdrian Ambrożewicz                 if (sensor.name == name)
262a0ec28b6SAdrian Ambrożewicz                 {
263a0ec28b6SAdrian Ambrożewicz                     sensor.uri = uri;
264a0ec28b6SAdrian Ambrożewicz                 }
265a0ec28b6SAdrian Ambrożewicz             }
266a0ec28b6SAdrian Ambrożewicz         }
267a0ec28b6SAdrian Ambrożewicz     }
268a0ec28b6SAdrian Ambrożewicz 
2698d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp> asyncResp;
270a0ec28b6SAdrian Ambrożewicz     const std::string chassisId;
27108777fb0SLewanczyk, Dawid     const std::vector<const char*> types;
272a0ec28b6SAdrian Ambrożewicz     const std::string chassisSubNode;
273928fefb9SNan Zhou     const bool efficientExpand;
274a0ec28b6SAdrian Ambrożewicz 
275a0ec28b6SAdrian Ambrożewicz   private:
276a0ec28b6SAdrian Ambrożewicz     std::optional<std::vector<SensorData>> metadata;
277a0ec28b6SAdrian Ambrożewicz     DataCompleteCb dataComplete;
27808777fb0SLewanczyk, Dawid };
27908777fb0SLewanczyk, Dawid 
28008777fb0SLewanczyk, Dawid /**
281d500549bSAnthony Wilson  * Possible states for physical inventory leds
282d500549bSAnthony Wilson  */
283d500549bSAnthony Wilson enum class LedState
284d500549bSAnthony Wilson {
285d500549bSAnthony Wilson     OFF,
286d500549bSAnthony Wilson     ON,
287d500549bSAnthony Wilson     BLINK,
288d500549bSAnthony Wilson     UNKNOWN
289d500549bSAnthony Wilson };
290d500549bSAnthony Wilson 
291d500549bSAnthony Wilson /**
292adc4f0dbSShawn McCarney  * D-Bus inventory item associated with one or more sensors.
293adc4f0dbSShawn McCarney  */
294adc4f0dbSShawn McCarney class InventoryItem
295adc4f0dbSShawn McCarney {
296adc4f0dbSShawn McCarney   public:
297e05aec50SEd Tanous     InventoryItem(const std::string& objPath) : objectPath(objPath)
298adc4f0dbSShawn McCarney     {
299adc4f0dbSShawn McCarney         // Set inventory item name to last node of object path
30028aa8de5SGeorge Liu         sdbusplus::message::object_path path(objectPath);
30128aa8de5SGeorge Liu         name = path.filename();
30228aa8de5SGeorge Liu         if (name.empty())
303adc4f0dbSShawn McCarney         {
30428aa8de5SGeorge Liu             BMCWEB_LOG_ERROR << "Failed to find '/' in " << objectPath;
305adc4f0dbSShawn McCarney         }
306adc4f0dbSShawn McCarney     }
307adc4f0dbSShawn McCarney 
308adc4f0dbSShawn McCarney     std::string objectPath;
309adc4f0dbSShawn McCarney     std::string name;
310e05aec50SEd Tanous     bool isPresent = true;
311e05aec50SEd Tanous     bool isFunctional = true;
312e05aec50SEd Tanous     bool isPowerSupply = false;
313e05aec50SEd Tanous     int powerSupplyEfficiencyPercent = -1;
314adc4f0dbSShawn McCarney     std::string manufacturer;
315adc4f0dbSShawn McCarney     std::string model;
316adc4f0dbSShawn McCarney     std::string partNumber;
317adc4f0dbSShawn McCarney     std::string serialNumber;
318adc4f0dbSShawn McCarney     std::set<std::string> sensors;
319d500549bSAnthony Wilson     std::string ledObjectPath;
320e05aec50SEd Tanous     LedState ledState = LedState::UNKNOWN;
321adc4f0dbSShawn McCarney };
322adc4f0dbSShawn McCarney 
323adc4f0dbSShawn McCarney /**
324413961deSRichard Marian Thomaiyar  * @brief Get objects with connection necessary for sensors
325588c3f0dSKowalski, Kamil  * @param SensorsAsyncResp Pointer to object holding response data
32608777fb0SLewanczyk, Dawid  * @param sensorNames Sensors retrieved from chassis
32708777fb0SLewanczyk, Dawid  * @param callback Callback for processing gathered connections
32808777fb0SLewanczyk, Dawid  */
32908777fb0SLewanczyk, Dawid template <typename Callback>
330413961deSRichard Marian Thomaiyar void getObjectsWithConnection(
33181ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
332b5a76932SEd Tanous     const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames,
3331abe55efSEd Tanous     Callback&& callback)
3341abe55efSEd Tanous {
335413961deSRichard Marian Thomaiyar     BMCWEB_LOG_DEBUG << "getObjectsWithConnection enter";
33603b5bae3SJames Feist     const std::string path = "/xyz/openbmc_project/sensors";
33708777fb0SLewanczyk, Dawid     const std::array<std::string, 1> interfaces = {
33808777fb0SLewanczyk, Dawid         "xyz.openbmc_project.Sensor.Value"};
33908777fb0SLewanczyk, Dawid 
34008777fb0SLewanczyk, Dawid     // Response handler for parsing objects subtree
341f94c4ecfSEd Tanous     auto respHandler = [callback{std::forward<Callback>(callback)},
342b9d36b47SEd Tanous                         sensorsAsyncResp, sensorNames](
343b9d36b47SEd Tanous                            const boost::system::error_code ec,
344b9d36b47SEd Tanous                            const dbus::utility::MapperGetSubTreeResponse&
345b9d36b47SEd Tanous                                subtree) {
346413961deSRichard Marian Thomaiyar         BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler enter";
3471abe55efSEd Tanous         if (ec)
3481abe55efSEd Tanous         {
3498d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
350413961deSRichard Marian Thomaiyar             BMCWEB_LOG_ERROR
351413961deSRichard Marian Thomaiyar                 << "getObjectsWithConnection resp_handler: Dbus error " << ec;
35208777fb0SLewanczyk, Dawid             return;
35308777fb0SLewanczyk, Dawid         }
35408777fb0SLewanczyk, Dawid 
35555c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "Found " << subtree.size() << " subtrees";
35608777fb0SLewanczyk, Dawid 
35708777fb0SLewanczyk, Dawid         // Make unique list of connections only for requested sensor types and
35808777fb0SLewanczyk, Dawid         // found in the chassis
35908777fb0SLewanczyk, Dawid         boost::container::flat_set<std::string> connections;
360413961deSRichard Marian Thomaiyar         std::set<std::pair<std::string, std::string>> objectsWithConnection;
3611abe55efSEd Tanous         // Intrinsic to avoid malloc.  Most systems will have < 8 sensor
3621abe55efSEd Tanous         // producers
36308777fb0SLewanczyk, Dawid         connections.reserve(8);
36408777fb0SLewanczyk, Dawid 
36549c53ac9SJohnathan Mantey         BMCWEB_LOG_DEBUG << "sensorNames list count: " << sensorNames->size();
36649c53ac9SJohnathan Mantey         for (const std::string& tsensor : *sensorNames)
3671abe55efSEd Tanous         {
36855c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "Sensor to find: " << tsensor;
36908777fb0SLewanczyk, Dawid         }
37008777fb0SLewanczyk, Dawid 
37108777fb0SLewanczyk, Dawid         for (const std::pair<
37208777fb0SLewanczyk, Dawid                  std::string,
37308777fb0SLewanczyk, Dawid                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
3741abe55efSEd Tanous                  object : subtree)
3751abe55efSEd Tanous         {
37649c53ac9SJohnathan Mantey             if (sensorNames->find(object.first) != sensorNames->end())
3771abe55efSEd Tanous             {
37849c53ac9SJohnathan Mantey                 for (const std::pair<std::string, std::vector<std::string>>&
3791abe55efSEd Tanous                          objData : object.second)
3801abe55efSEd Tanous                 {
38149c53ac9SJohnathan Mantey                     BMCWEB_LOG_DEBUG << "Adding connection: " << objData.first;
38208777fb0SLewanczyk, Dawid                     connections.insert(objData.first);
383de629b6eSShawn McCarney                     objectsWithConnection.insert(
384de629b6eSShawn McCarney                         std::make_pair(object.first, objData.first));
38508777fb0SLewanczyk, Dawid                 }
38608777fb0SLewanczyk, Dawid             }
38708777fb0SLewanczyk, Dawid         }
38855c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "Found " << connections.size() << " connections";
389413961deSRichard Marian Thomaiyar         callback(std::move(connections), std::move(objectsWithConnection));
390413961deSRichard Marian Thomaiyar         BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler exit";
39108777fb0SLewanczyk, Dawid     };
39208777fb0SLewanczyk, Dawid     // Make call to ObjectMapper to find all sensors objects
39355c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
39455c7b7a2SEd Tanous         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
3951abe55efSEd Tanous         "/xyz/openbmc_project/object_mapper",
3961abe55efSEd Tanous         "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 2, interfaces);
397413961deSRichard Marian Thomaiyar     BMCWEB_LOG_DEBUG << "getObjectsWithConnection exit";
398413961deSRichard Marian Thomaiyar }
399413961deSRichard Marian Thomaiyar 
400413961deSRichard Marian Thomaiyar /**
401413961deSRichard Marian Thomaiyar  * @brief Create connections necessary for sensors
402413961deSRichard Marian Thomaiyar  * @param SensorsAsyncResp Pointer to object holding response data
403413961deSRichard Marian Thomaiyar  * @param sensorNames Sensors retrieved from chassis
404413961deSRichard Marian Thomaiyar  * @param callback Callback for processing gathered connections
405413961deSRichard Marian Thomaiyar  */
406413961deSRichard Marian Thomaiyar template <typename Callback>
40749c53ac9SJohnathan Mantey void getConnections(
40881ce609eSEd Tanous     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
40949c53ac9SJohnathan Mantey     const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
410413961deSRichard Marian Thomaiyar     Callback&& callback)
411413961deSRichard Marian Thomaiyar {
412413961deSRichard Marian Thomaiyar     auto objectsWithConnectionCb =
413413961deSRichard Marian Thomaiyar         [callback](const boost::container::flat_set<std::string>& connections,
414413961deSRichard Marian Thomaiyar                    const std::set<std::pair<std::string, std::string>>&
4153174e4dfSEd Tanous                    /*objectsWithConnection*/) { callback(connections); };
41681ce609eSEd Tanous     getObjectsWithConnection(sensorsAsyncResp, sensorNames,
417413961deSRichard Marian Thomaiyar                              std::move(objectsWithConnectionCb));
41808777fb0SLewanczyk, Dawid }
41908777fb0SLewanczyk, Dawid 
42008777fb0SLewanczyk, Dawid /**
42149c53ac9SJohnathan Mantey  * @brief Shrinks the list of sensors for processing
42249c53ac9SJohnathan Mantey  * @param SensorsAysncResp  The class holding the Redfish response
42349c53ac9SJohnathan Mantey  * @param allSensors  A list of all the sensors associated to the
42449c53ac9SJohnathan Mantey  * chassis element (i.e. baseboard, front panel, etc...)
42549c53ac9SJohnathan Mantey  * @param activeSensors A list that is a reduction of the incoming
42649c53ac9SJohnathan Mantey  * allSensors list.  Eliminate Thermal sensors when a Power request is
42749c53ac9SJohnathan Mantey  * made, and eliminate Power sensors when a Thermal request is made.
42849c53ac9SJohnathan Mantey  */
42923a21a1cSEd Tanous inline void reduceSensorList(
43081ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
43149c53ac9SJohnathan Mantey     const std::vector<std::string>* allSensors,
432b5a76932SEd Tanous     const std::shared_ptr<boost::container::flat_set<std::string>>&
433b5a76932SEd Tanous         activeSensors)
43449c53ac9SJohnathan Mantey {
43581ce609eSEd Tanous     if (sensorsAsyncResp == nullptr)
43649c53ac9SJohnathan Mantey     {
43749c53ac9SJohnathan Mantey         return;
43849c53ac9SJohnathan Mantey     }
43949c53ac9SJohnathan Mantey     if ((allSensors == nullptr) || (activeSensors == nullptr))
44049c53ac9SJohnathan Mantey     {
44149c53ac9SJohnathan Mantey         messages::resourceNotFound(
4428d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res, sensorsAsyncResp->chassisSubNode,
44381ce609eSEd Tanous             sensorsAsyncResp->chassisSubNode == sensors::node::thermal
444a0ec28b6SAdrian Ambrożewicz                 ? "Temperatures"
44549c53ac9SJohnathan Mantey                 : "Voltages");
44649c53ac9SJohnathan Mantey 
44749c53ac9SJohnathan Mantey         return;
44849c53ac9SJohnathan Mantey     }
44949c53ac9SJohnathan Mantey     if (allSensors->empty())
45049c53ac9SJohnathan Mantey     {
45149c53ac9SJohnathan Mantey         // Nothing to do, the activeSensors object is also empty
45249c53ac9SJohnathan Mantey         return;
45349c53ac9SJohnathan Mantey     }
45449c53ac9SJohnathan Mantey 
45581ce609eSEd Tanous     for (const char* type : sensorsAsyncResp->types)
45649c53ac9SJohnathan Mantey     {
45749c53ac9SJohnathan Mantey         for (const std::string& sensor : *allSensors)
45849c53ac9SJohnathan Mantey         {
45949c53ac9SJohnathan Mantey             if (boost::starts_with(sensor, type))
46049c53ac9SJohnathan Mantey             {
46149c53ac9SJohnathan Mantey                 activeSensors->emplace(sensor);
46249c53ac9SJohnathan Mantey             }
46349c53ac9SJohnathan Mantey         }
46449c53ac9SJohnathan Mantey     }
46549c53ac9SJohnathan Mantey }
46649c53ac9SJohnathan Mantey 
46749c53ac9SJohnathan Mantey /**
4684bb3dc34SCarol Wang  * @brief Retrieves valid chassis path
4694bb3dc34SCarol Wang  * @param asyncResp   Pointer to object holding response data
4704bb3dc34SCarol Wang  * @param callback  Callback for next step to get valid chassis path
4714bb3dc34SCarol Wang  */
4724bb3dc34SCarol Wang template <typename Callback>
473b5a76932SEd Tanous void getValidChassisPath(const std::shared_ptr<SensorsAsyncResp>& asyncResp,
4744bb3dc34SCarol Wang                          Callback&& callback)
4754bb3dc34SCarol Wang {
4764bb3dc34SCarol Wang     BMCWEB_LOG_DEBUG << "checkChassisId enter";
4774bb3dc34SCarol Wang     const std::array<const char*, 2> interfaces = {
4784bb3dc34SCarol Wang         "xyz.openbmc_project.Inventory.Item.Board",
4794bb3dc34SCarol Wang         "xyz.openbmc_project.Inventory.Item.Chassis"};
4804bb3dc34SCarol Wang 
481b9d36b47SEd Tanous     auto respHandler = [callback{std::forward<Callback>(callback)}, asyncResp](
482b9d36b47SEd Tanous                            const boost::system::error_code ec,
483b9d36b47SEd Tanous                            const dbus::utility::MapperGetSubTreePathsResponse&
484b9d36b47SEd Tanous                                chassisPaths) mutable {
4854bb3dc34SCarol Wang         BMCWEB_LOG_DEBUG << "getValidChassisPath respHandler enter";
4864bb3dc34SCarol Wang         if (ec)
4874bb3dc34SCarol Wang         {
488b9d36b47SEd Tanous             BMCWEB_LOG_ERROR << "getValidChassisPath respHandler DBUS error: "
489b9d36b47SEd Tanous                              << ec;
4908d1b46d7Szhanghch05             messages::internalError(asyncResp->asyncResp->res);
4914bb3dc34SCarol Wang             return;
4924bb3dc34SCarol Wang         }
4934bb3dc34SCarol Wang 
4944bb3dc34SCarol Wang         std::optional<std::string> chassisPath;
4954bb3dc34SCarol Wang         std::string chassisName;
4964bb3dc34SCarol Wang         for (const std::string& chassis : chassisPaths)
4974bb3dc34SCarol Wang         {
49828aa8de5SGeorge Liu             sdbusplus::message::object_path path(chassis);
49928aa8de5SGeorge Liu             chassisName = path.filename();
50028aa8de5SGeorge Liu             if (chassisName.empty())
5014bb3dc34SCarol Wang             {
5024bb3dc34SCarol Wang                 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
5034bb3dc34SCarol Wang                 continue;
5044bb3dc34SCarol Wang             }
5054bb3dc34SCarol Wang             if (chassisName == asyncResp->chassisId)
5064bb3dc34SCarol Wang             {
5074bb3dc34SCarol Wang                 chassisPath = chassis;
5084bb3dc34SCarol Wang                 break;
5094bb3dc34SCarol Wang             }
5104bb3dc34SCarol Wang         }
5114bb3dc34SCarol Wang         callback(chassisPath);
5124bb3dc34SCarol Wang     };
5134bb3dc34SCarol Wang 
5144bb3dc34SCarol Wang     // Get the Chassis Collection
5154bb3dc34SCarol Wang     crow::connections::systemBus->async_method_call(
5164bb3dc34SCarol Wang         respHandler, "xyz.openbmc_project.ObjectMapper",
5174bb3dc34SCarol Wang         "/xyz/openbmc_project/object_mapper",
5184bb3dc34SCarol Wang         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
5194bb3dc34SCarol Wang         "/xyz/openbmc_project/inventory", 0, interfaces);
5204bb3dc34SCarol Wang     BMCWEB_LOG_DEBUG << "checkChassisId exit";
5214bb3dc34SCarol Wang }
5224bb3dc34SCarol Wang 
5234bb3dc34SCarol Wang /**
52408777fb0SLewanczyk, Dawid  * @brief Retrieves requested chassis sensors and redundancy data from DBus .
525588c3f0dSKowalski, Kamil  * @param SensorsAsyncResp   Pointer to object holding response data
52608777fb0SLewanczyk, Dawid  * @param callback  Callback for next step in gathered sensor processing
52708777fb0SLewanczyk, Dawid  */
52808777fb0SLewanczyk, Dawid template <typename Callback>
529b5a76932SEd Tanous void getChassis(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
5301abe55efSEd Tanous                 Callback&& callback)
5311abe55efSEd Tanous {
53255c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "getChassis enter";
533adc4f0dbSShawn McCarney     const std::array<const char*, 2> interfaces = {
53449c53ac9SJohnathan Mantey         "xyz.openbmc_project.Inventory.Item.Board",
535adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Item.Chassis"};
536f94c4ecfSEd Tanous     auto respHandler = [callback{std::forward<Callback>(callback)},
537f94c4ecfSEd Tanous                         sensorsAsyncResp](
53849c53ac9SJohnathan Mantey                            const boost::system::error_code ec,
539b9d36b47SEd Tanous                            const dbus::utility::MapperGetSubTreePathsResponse&
540b9d36b47SEd Tanous                                chassisPaths) {
54155c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "getChassis respHandler enter";
5421abe55efSEd Tanous         if (ec)
5431abe55efSEd Tanous         {
54455c7b7a2SEd Tanous             BMCWEB_LOG_ERROR << "getChassis respHandler DBUS error: " << ec;
5458d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
54608777fb0SLewanczyk, Dawid             return;
54708777fb0SLewanczyk, Dawid         }
54808777fb0SLewanczyk, Dawid 
54949c53ac9SJohnathan Mantey         const std::string* chassisPath = nullptr;
55049c53ac9SJohnathan Mantey         std::string chassisName;
55149c53ac9SJohnathan Mantey         for (const std::string& chassis : chassisPaths)
5521abe55efSEd Tanous         {
55328aa8de5SGeorge Liu             sdbusplus::message::object_path path(chassis);
55428aa8de5SGeorge Liu             chassisName = path.filename();
55528aa8de5SGeorge Liu             if (chassisName.empty())
5561abe55efSEd Tanous             {
55749c53ac9SJohnathan Mantey                 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
558daf36e2eSEd Tanous                 continue;
559daf36e2eSEd Tanous             }
56049c53ac9SJohnathan Mantey             if (chassisName == sensorsAsyncResp->chassisId)
5611abe55efSEd Tanous             {
56249c53ac9SJohnathan Mantey                 chassisPath = &chassis;
56349c53ac9SJohnathan Mantey                 break;
564daf36e2eSEd Tanous             }
56549c53ac9SJohnathan Mantey         }
56649c53ac9SJohnathan Mantey         if (chassisPath == nullptr)
5671abe55efSEd Tanous         {
5688d1b46d7Szhanghch05             messages::resourceNotFound(sensorsAsyncResp->asyncResp->res,
5698d1b46d7Szhanghch05                                        "Chassis", sensorsAsyncResp->chassisId);
57049c53ac9SJohnathan Mantey             return;
5711abe55efSEd Tanous         }
57208777fb0SLewanczyk, Dawid 
57349c53ac9SJohnathan Mantey         const std::string& chassisSubNode = sensorsAsyncResp->chassisSubNode;
574a0ec28b6SAdrian Ambrożewicz         if (chassisSubNode == sensors::node::power)
57549c53ac9SJohnathan Mantey         {
5768d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] =
57749c53ac9SJohnathan Mantey                 "#Power.v1_5_2.Power";
57849c53ac9SJohnathan Mantey         }
579a0ec28b6SAdrian Ambrożewicz         else if (chassisSubNode == sensors::node::thermal)
58049c53ac9SJohnathan Mantey         {
5818d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] =
58249c53ac9SJohnathan Mantey                 "#Thermal.v1_4_0.Thermal";
5838d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["Fans"] =
5848d1b46d7Szhanghch05                 nlohmann::json::array();
5858d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["Temperatures"] =
5864f9a2130SJennifer Lee                 nlohmann::json::array();
58749c53ac9SJohnathan Mantey         }
588a0ec28b6SAdrian Ambrożewicz         else if (chassisSubNode == sensors::node::sensors)
58995a3ecadSAnthony Wilson         {
5908d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] =
59195a3ecadSAnthony Wilson                 "#SensorCollection.SensorCollection";
5928d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["Description"] =
59395a3ecadSAnthony Wilson                 "Collection of Sensors for this Chassis";
5948d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["Members"] =
59595a3ecadSAnthony Wilson                 nlohmann::json::array();
5968d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["Members@odata.count"] =
5978d1b46d7Szhanghch05                 0;
59895a3ecadSAnthony Wilson         }
59995a3ecadSAnthony Wilson 
600a0ec28b6SAdrian Ambrożewicz         if (chassisSubNode != sensors::node::sensors)
60195a3ecadSAnthony Wilson         {
6028d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["Id"] = chassisSubNode;
60395a3ecadSAnthony Wilson         }
60495a3ecadSAnthony Wilson 
6058d1b46d7Szhanghch05         sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
60649c53ac9SJohnathan Mantey             "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId + "/" +
60749c53ac9SJohnathan Mantey             chassisSubNode;
6088d1b46d7Szhanghch05         sensorsAsyncResp->asyncResp->res.jsonValue["Name"] = chassisSubNode;
6098fb49dd6SShawn McCarney         // Get the list of all sensors for this Chassis element
6108fb49dd6SShawn McCarney         std::string sensorPath = *chassisPath + "/all_sensors";
6111e1e598dSJonathan Doman         sdbusplus::asio::getProperty<std::vector<std::string>>(
6121e1e598dSJonathan Doman             *crow::connections::systemBus, "xyz.openbmc_project.ObjectMapper",
6131e1e598dSJonathan Doman             sensorPath, "xyz.openbmc_project.Association", "endpoints",
614f94c4ecfSEd Tanous             [sensorsAsyncResp,
615f94c4ecfSEd Tanous              callback{std::forward<const Callback>(callback)}](
616271584abSEd Tanous                 const boost::system::error_code& e,
6171e1e598dSJonathan Doman                 const std::vector<std::string>& nodeSensorList) {
618271584abSEd Tanous                 if (e)
61949c53ac9SJohnathan Mantey                 {
620271584abSEd Tanous                     if (e.value() != EBADR)
62149c53ac9SJohnathan Mantey                     {
6228d1b46d7Szhanghch05                         messages::internalError(
6238d1b46d7Szhanghch05                             sensorsAsyncResp->asyncResp->res);
62449c53ac9SJohnathan Mantey                         return;
62549c53ac9SJohnathan Mantey                     }
62649c53ac9SJohnathan Mantey                 }
62749c53ac9SJohnathan Mantey                 const std::shared_ptr<boost::container::flat_set<std::string>>
62849c53ac9SJohnathan Mantey                     culledSensorList = std::make_shared<
62949c53ac9SJohnathan Mantey                         boost::container::flat_set<std::string>>();
6301e1e598dSJonathan Doman                 reduceSensorList(sensorsAsyncResp, &nodeSensorList,
63149c53ac9SJohnathan Mantey                                  culledSensorList);
63249c53ac9SJohnathan Mantey                 callback(culledSensorList);
6331e1e598dSJonathan Doman             });
63449c53ac9SJohnathan Mantey     };
63549c53ac9SJohnathan Mantey 
63649c53ac9SJohnathan Mantey     // Get the Chassis Collection
63749c53ac9SJohnathan Mantey     crow::connections::systemBus->async_method_call(
63849c53ac9SJohnathan Mantey         respHandler, "xyz.openbmc_project.ObjectMapper",
63949c53ac9SJohnathan Mantey         "/xyz/openbmc_project/object_mapper",
64049c53ac9SJohnathan Mantey         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
641271584abSEd Tanous         "/xyz/openbmc_project/inventory", 0, interfaces);
64255c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "getChassis exit";
64308777fb0SLewanczyk, Dawid }
64408777fb0SLewanczyk, Dawid 
64508777fb0SLewanczyk, Dawid /**
646de629b6eSShawn McCarney  * @brief Finds all DBus object paths that implement ObjectManager.
647de629b6eSShawn McCarney  *
648de629b6eSShawn McCarney  * Creates a mapping from the associated connection name to the object path.
649de629b6eSShawn McCarney  *
650de629b6eSShawn McCarney  * Finds the object paths asynchronously.  Invokes callback when information has
651de629b6eSShawn McCarney  * been obtained.
652de629b6eSShawn McCarney  *
653de629b6eSShawn McCarney  * The callback must have the following signature:
654de629b6eSShawn McCarney  *   @code
6558fb49dd6SShawn McCarney  *   callback(std::shared_ptr<boost::container::flat_map<std::string,
6568fb49dd6SShawn McCarney  *                std::string>> objectMgrPaths)
657de629b6eSShawn McCarney  *   @endcode
658de629b6eSShawn McCarney  *
65949c53ac9SJohnathan Mantey  * @param sensorsAsyncResp Pointer to object holding response data.
660de629b6eSShawn McCarney  * @param callback Callback to invoke when object paths obtained.
661de629b6eSShawn McCarney  */
662de629b6eSShawn McCarney template <typename Callback>
663b5a76932SEd Tanous void getObjectManagerPaths(
66481ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
665de629b6eSShawn McCarney     Callback&& callback)
666de629b6eSShawn McCarney {
667de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getObjectManagerPaths enter";
668de629b6eSShawn McCarney     const std::array<std::string, 1> interfaces = {
669de629b6eSShawn McCarney         "org.freedesktop.DBus.ObjectManager"};
670de629b6eSShawn McCarney 
671de629b6eSShawn McCarney     // Response handler for GetSubTree DBus method
672f94c4ecfSEd Tanous     auto respHandler = [callback{std::forward<Callback>(callback)},
673b9d36b47SEd Tanous                         sensorsAsyncResp](
674b9d36b47SEd Tanous                            const boost::system::error_code ec,
675b9d36b47SEd Tanous                            const dbus::utility::MapperGetSubTreeResponse&
676b9d36b47SEd Tanous                                subtree) {
677de629b6eSShawn McCarney         BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler enter";
678de629b6eSShawn McCarney         if (ec)
679de629b6eSShawn McCarney         {
6808d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
681de629b6eSShawn McCarney             BMCWEB_LOG_ERROR << "getObjectManagerPaths respHandler: DBus error "
682de629b6eSShawn McCarney                              << ec;
683de629b6eSShawn McCarney             return;
684de629b6eSShawn McCarney         }
685de629b6eSShawn McCarney 
686de629b6eSShawn McCarney         // Loop over returned object paths
6878fb49dd6SShawn McCarney         std::shared_ptr<boost::container::flat_map<std::string, std::string>>
6888fb49dd6SShawn McCarney             objectMgrPaths = std::make_shared<
6898fb49dd6SShawn McCarney                 boost::container::flat_map<std::string, std::string>>();
690de629b6eSShawn McCarney         for (const std::pair<
691de629b6eSShawn McCarney                  std::string,
692de629b6eSShawn McCarney                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
693de629b6eSShawn McCarney                  object : subtree)
694de629b6eSShawn McCarney         {
695de629b6eSShawn McCarney             // Loop over connections for current object path
696de629b6eSShawn McCarney             const std::string& objectPath = object.first;
697de629b6eSShawn McCarney             for (const std::pair<std::string, std::vector<std::string>>&
698de629b6eSShawn McCarney                      objData : object.second)
699de629b6eSShawn McCarney             {
700de629b6eSShawn McCarney                 // Add mapping from connection to object path
701de629b6eSShawn McCarney                 const std::string& connection = objData.first;
7028fb49dd6SShawn McCarney                 (*objectMgrPaths)[connection] = objectPath;
703de629b6eSShawn McCarney                 BMCWEB_LOG_DEBUG << "Added mapping " << connection << " -> "
704de629b6eSShawn McCarney                                  << objectPath;
705de629b6eSShawn McCarney             }
706de629b6eSShawn McCarney         }
7078fb49dd6SShawn McCarney         callback(objectMgrPaths);
708de629b6eSShawn McCarney         BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler exit";
709de629b6eSShawn McCarney     };
710de629b6eSShawn McCarney 
711de629b6eSShawn McCarney     // Query mapper for all DBus object paths that implement ObjectManager
712de629b6eSShawn McCarney     crow::connections::systemBus->async_method_call(
713de629b6eSShawn McCarney         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
714de629b6eSShawn McCarney         "/xyz/openbmc_project/object_mapper",
715271584abSEd Tanous         "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0, interfaces);
716de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getObjectManagerPaths exit";
717de629b6eSShawn McCarney }
718de629b6eSShawn McCarney 
719de629b6eSShawn McCarney /**
720adc4f0dbSShawn McCarney  * @brief Returns the Redfish State value for the specified inventory item.
721adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with a sensor.
722adc4f0dbSShawn McCarney  * @return State value for inventory item.
72334dd179eSJames Feist  */
72423a21a1cSEd Tanous inline std::string getState(const InventoryItem* inventoryItem)
725adc4f0dbSShawn McCarney {
726adc4f0dbSShawn McCarney     if ((inventoryItem != nullptr) && !(inventoryItem->isPresent))
727adc4f0dbSShawn McCarney     {
728adc4f0dbSShawn McCarney         return "Absent";
729adc4f0dbSShawn McCarney     }
73034dd179eSJames Feist 
731adc4f0dbSShawn McCarney     return "Enabled";
732adc4f0dbSShawn McCarney }
733adc4f0dbSShawn McCarney 
734adc4f0dbSShawn McCarney /**
735adc4f0dbSShawn McCarney  * @brief Returns the Redfish Health value for the specified sensor.
736adc4f0dbSShawn McCarney  * @param sensorJson Sensor JSON object.
737adc4f0dbSShawn McCarney  * @param interfacesDict Map of all sensor interfaces.
738adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with the sensor.  Will
739adc4f0dbSShawn McCarney  * be nullptr if no associated inventory item was found.
740adc4f0dbSShawn McCarney  * @return Health value for sensor.
741adc4f0dbSShawn McCarney  */
742711ac7a9SEd Tanous inline std::string
743711ac7a9SEd Tanous     getHealth(nlohmann::json& sensorJson,
744711ac7a9SEd Tanous               const dbus::utility::DBusInteracesMap& interfacesDict,
745adc4f0dbSShawn McCarney               const InventoryItem* inventoryItem)
74634dd179eSJames Feist {
747adc4f0dbSShawn McCarney     // Get current health value (if any) in the sensor JSON object.  Some JSON
748adc4f0dbSShawn McCarney     // objects contain multiple sensors (such as PowerSupplies).  We want to set
749adc4f0dbSShawn McCarney     // the overall health to be the most severe of any of the sensors.
750adc4f0dbSShawn McCarney     std::string currentHealth;
751adc4f0dbSShawn McCarney     auto statusIt = sensorJson.find("Status");
752adc4f0dbSShawn McCarney     if (statusIt != sensorJson.end())
753adc4f0dbSShawn McCarney     {
754adc4f0dbSShawn McCarney         auto healthIt = statusIt->find("Health");
755adc4f0dbSShawn McCarney         if (healthIt != statusIt->end())
756adc4f0dbSShawn McCarney         {
757adc4f0dbSShawn McCarney             std::string* health = healthIt->get_ptr<std::string*>();
758adc4f0dbSShawn McCarney             if (health != nullptr)
759adc4f0dbSShawn McCarney             {
760adc4f0dbSShawn McCarney                 currentHealth = *health;
761adc4f0dbSShawn McCarney             }
762adc4f0dbSShawn McCarney         }
763adc4f0dbSShawn McCarney     }
764adc4f0dbSShawn McCarney 
765adc4f0dbSShawn McCarney     // If current health in JSON object is already Critical, return that.  This
766adc4f0dbSShawn McCarney     // should override the sensor health, which might be less severe.
767adc4f0dbSShawn McCarney     if (currentHealth == "Critical")
768adc4f0dbSShawn McCarney     {
769adc4f0dbSShawn McCarney         return "Critical";
770adc4f0dbSShawn McCarney     }
771adc4f0dbSShawn McCarney 
772adc4f0dbSShawn McCarney     // Check if sensor has critical threshold alarm
773711ac7a9SEd Tanous 
7749eb808c1SEd Tanous     for (const auto& [interface, values] : interfacesDict)
77534dd179eSJames Feist     {
776711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Sensor.Threshold.Critical")
77734dd179eSJames Feist         {
7789eb808c1SEd Tanous             for (const auto& [valueName, value] : values)
779711ac7a9SEd Tanous             {
780711ac7a9SEd Tanous                 if (valueName == "CriticalAlarmHigh" ||
781711ac7a9SEd Tanous                     valueName == "CriticalAlarmLow")
782711ac7a9SEd Tanous                 {
783711ac7a9SEd Tanous                     const bool* asserted = std::get_if<bool>(&value);
78434dd179eSJames Feist                     if (asserted == nullptr)
78534dd179eSJames Feist                     {
78634dd179eSJames Feist                         BMCWEB_LOG_ERROR << "Illegal sensor threshold";
78734dd179eSJames Feist                     }
78834dd179eSJames Feist                     else if (*asserted)
78934dd179eSJames Feist                     {
79034dd179eSJames Feist                         return "Critical";
79134dd179eSJames Feist                     }
79234dd179eSJames Feist                 }
79334dd179eSJames Feist             }
79434dd179eSJames Feist         }
79534dd179eSJames Feist     }
79634dd179eSJames Feist 
797adc4f0dbSShawn McCarney     // Check if associated inventory item is not functional
798adc4f0dbSShawn McCarney     if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional))
799adc4f0dbSShawn McCarney     {
800adc4f0dbSShawn McCarney         return "Critical";
801adc4f0dbSShawn McCarney     }
802adc4f0dbSShawn McCarney 
803adc4f0dbSShawn McCarney     // If current health in JSON object is already Warning, return that.  This
804adc4f0dbSShawn McCarney     // should override the sensor status, which might be less severe.
805adc4f0dbSShawn McCarney     if (currentHealth == "Warning")
806adc4f0dbSShawn McCarney     {
807adc4f0dbSShawn McCarney         return "Warning";
808adc4f0dbSShawn McCarney     }
809adc4f0dbSShawn McCarney 
810adc4f0dbSShawn McCarney     // Check if sensor has warning threshold alarm
8119eb808c1SEd Tanous     for (const auto& [interface, values] : interfacesDict)
81234dd179eSJames Feist     {
813711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Sensor.Threshold.Warning")
81434dd179eSJames Feist         {
8159eb808c1SEd Tanous             for (const auto& [valueName, value] : values)
816711ac7a9SEd Tanous             {
817711ac7a9SEd Tanous                 if (valueName == "WarningAlarmHigh" ||
818711ac7a9SEd Tanous                     valueName == "WarningAlarmLow")
819711ac7a9SEd Tanous                 {
820711ac7a9SEd Tanous                     const bool* asserted = std::get_if<bool>(&value);
82134dd179eSJames Feist                     if (asserted == nullptr)
82234dd179eSJames Feist                     {
82334dd179eSJames Feist                         BMCWEB_LOG_ERROR << "Illegal sensor threshold";
82434dd179eSJames Feist                     }
82534dd179eSJames Feist                     else if (*asserted)
82634dd179eSJames Feist                     {
827ebe4d91eSEd Tanous                         return "Warning";
82834dd179eSJames Feist                     }
82934dd179eSJames Feist                 }
83034dd179eSJames Feist             }
83134dd179eSJames Feist         }
83234dd179eSJames Feist     }
833adc4f0dbSShawn McCarney 
83434dd179eSJames Feist     return "OK";
83534dd179eSJames Feist }
83634dd179eSJames Feist 
83723a21a1cSEd Tanous inline void setLedState(nlohmann::json& sensorJson,
838d500549bSAnthony Wilson                         const InventoryItem* inventoryItem)
839d500549bSAnthony Wilson {
840d500549bSAnthony Wilson     if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty())
841d500549bSAnthony Wilson     {
842d500549bSAnthony Wilson         switch (inventoryItem->ledState)
843d500549bSAnthony Wilson         {
844d500549bSAnthony Wilson             case LedState::OFF:
845d500549bSAnthony Wilson                 sensorJson["IndicatorLED"] = "Off";
846d500549bSAnthony Wilson                 break;
847d500549bSAnthony Wilson             case LedState::ON:
848d500549bSAnthony Wilson                 sensorJson["IndicatorLED"] = "Lit";
849d500549bSAnthony Wilson                 break;
850d500549bSAnthony Wilson             case LedState::BLINK:
851d500549bSAnthony Wilson                 sensorJson["IndicatorLED"] = "Blinking";
852d500549bSAnthony Wilson                 break;
85323a21a1cSEd Tanous             case LedState::UNKNOWN:
854d500549bSAnthony Wilson                 break;
855d500549bSAnthony Wilson         }
856d500549bSAnthony Wilson     }
857d500549bSAnthony Wilson }
858d500549bSAnthony Wilson 
85934dd179eSJames Feist /**
86008777fb0SLewanczyk, Dawid  * @brief Builds a json sensor representation of a sensor.
86108777fb0SLewanczyk, Dawid  * @param sensorName  The name of the sensor to be built
862274fad5aSGunnar Mills  * @param sensorType  The type (temperature, fan_tach, etc) of the sensor to
86308777fb0SLewanczyk, Dawid  * build
864a0ec28b6SAdrian Ambrożewicz  * @param sensorsAsyncResp  Sensor metadata
86508777fb0SLewanczyk, Dawid  * @param interfacesDict  A dictionary of the interfaces and properties of said
86608777fb0SLewanczyk, Dawid  * interfaces to be built from
86708777fb0SLewanczyk, Dawid  * @param sensor_json  The json object to fill
868adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with the sensor.  Will
869adc4f0dbSShawn McCarney  * be nullptr if no associated inventory item was found.
87008777fb0SLewanczyk, Dawid  */
87123a21a1cSEd Tanous inline void objectInterfacesToJson(
87208777fb0SLewanczyk, Dawid     const std::string& sensorName, const std::string& sensorType,
873b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
874711ac7a9SEd Tanous     const dbus::utility::DBusInteracesMap& interfacesDict,
87581ce609eSEd Tanous     nlohmann::json& sensorJson, InventoryItem* inventoryItem)
8761abe55efSEd Tanous {
87708777fb0SLewanczyk, Dawid     // Assume values exist as is (10^0 == 1) if no scale exists
87808777fb0SLewanczyk, Dawid     int64_t scaleMultiplier = 0;
8799eb808c1SEd Tanous     for (const auto& [interface, values] : interfacesDict)
8801abe55efSEd Tanous     {
881711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Sensor.Value")
882711ac7a9SEd Tanous         {
8839eb808c1SEd Tanous             for (const auto& [valueName, value] : values)
884711ac7a9SEd Tanous             {
885711ac7a9SEd Tanous                 if (valueName == "Scale")
886711ac7a9SEd Tanous                 {
887711ac7a9SEd Tanous                     const int64_t* int64Value = std::get_if<int64_t>(&value);
8881abe55efSEd Tanous                     if (int64Value != nullptr)
8891abe55efSEd Tanous                     {
89008777fb0SLewanczyk, Dawid                         scaleMultiplier = *int64Value;
89108777fb0SLewanczyk, Dawid                     }
89208777fb0SLewanczyk, Dawid                 }
893711ac7a9SEd Tanous             }
894711ac7a9SEd Tanous         }
895711ac7a9SEd Tanous     }
89608777fb0SLewanczyk, Dawid 
897a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
898adc4f0dbSShawn McCarney     {
89995a3ecadSAnthony Wilson         // For sensors in SensorCollection we set Id instead of MemberId,
90095a3ecadSAnthony Wilson         // including power sensors.
90181ce609eSEd Tanous         sensorJson["Id"] = sensorName;
90281ce609eSEd Tanous         sensorJson["Name"] = boost::replace_all_copy(sensorName, "_", " ");
90395a3ecadSAnthony Wilson     }
90495a3ecadSAnthony Wilson     else if (sensorType != "power")
90595a3ecadSAnthony Wilson     {
90695a3ecadSAnthony Wilson         // Set MemberId and Name for non-power sensors.  For PowerSupplies and
90795a3ecadSAnthony Wilson         // PowerControl, those properties have more general values because
90895a3ecadSAnthony Wilson         // multiple sensors can be stored in the same JSON object.
90981ce609eSEd Tanous         sensorJson["MemberId"] = sensorName;
91081ce609eSEd Tanous         sensorJson["Name"] = boost::replace_all_copy(sensorName, "_", " ");
911adc4f0dbSShawn McCarney     }
912e742b6ccSEd Tanous 
91381ce609eSEd Tanous     sensorJson["Status"]["State"] = getState(inventoryItem);
91481ce609eSEd Tanous     sensorJson["Status"]["Health"] =
91581ce609eSEd Tanous         getHealth(sensorJson, interfacesDict, inventoryItem);
91608777fb0SLewanczyk, Dawid 
91708777fb0SLewanczyk, Dawid     // Parameter to set to override the type we get from dbus, and force it to
91808777fb0SLewanczyk, Dawid     // int, regardless of what is available.  This is used for schemas like fan,
91908777fb0SLewanczyk, Dawid     // that require integers, not floats.
92008777fb0SLewanczyk, Dawid     bool forceToInt = false;
92108777fb0SLewanczyk, Dawid 
9223929aca1SAnthony Wilson     nlohmann::json::json_pointer unit("/Reading");
923a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
92495a3ecadSAnthony Wilson     {
92581ce609eSEd Tanous         sensorJson["@odata.type"] = "#Sensor.v1_0_0.Sensor";
926c2bf7f99SWludzik, Jozef 
927c2bf7f99SWludzik, Jozef         const std::string& readingType = sensors::toReadingType(sensorType);
928c2bf7f99SWludzik, Jozef         if (readingType.empty())
92995a3ecadSAnthony Wilson         {
930c2bf7f99SWludzik, Jozef             BMCWEB_LOG_ERROR << "Redfish cannot map reading type for "
931c2bf7f99SWludzik, Jozef                              << sensorType;
93295a3ecadSAnthony Wilson         }
933c2bf7f99SWludzik, Jozef         else
93495a3ecadSAnthony Wilson         {
935c2bf7f99SWludzik, Jozef             sensorJson["ReadingType"] = readingType;
93695a3ecadSAnthony Wilson         }
937c2bf7f99SWludzik, Jozef 
938c2bf7f99SWludzik, Jozef         const std::string& readingUnits = sensors::toReadingUnits(sensorType);
939c2bf7f99SWludzik, Jozef         if (readingUnits.empty())
940f8ede15eSAdrian Ambrożewicz         {
941c2bf7f99SWludzik, Jozef             BMCWEB_LOG_ERROR << "Redfish cannot map reading unit for "
942c2bf7f99SWludzik, Jozef                              << sensorType;
943c2bf7f99SWludzik, Jozef         }
944c2bf7f99SWludzik, Jozef         else
945c2bf7f99SWludzik, Jozef         {
946c2bf7f99SWludzik, Jozef             sensorJson["ReadingUnits"] = readingUnits;
947f8ede15eSAdrian Ambrożewicz         }
94895a3ecadSAnthony Wilson     }
94995a3ecadSAnthony Wilson     else if (sensorType == "temperature")
9501abe55efSEd Tanous     {
9513929aca1SAnthony Wilson         unit = "/ReadingCelsius"_json_pointer;
95281ce609eSEd Tanous         sensorJson["@odata.type"] = "#Thermal.v1_3_0.Temperature";
95308777fb0SLewanczyk, Dawid         // TODO(ed) Documentation says that path should be type fan_tach,
95408777fb0SLewanczyk, Dawid         // implementation seems to implement fan
9551abe55efSEd Tanous     }
9561abe55efSEd Tanous     else if (sensorType == "fan" || sensorType == "fan_tach")
9571abe55efSEd Tanous     {
9583929aca1SAnthony Wilson         unit = "/Reading"_json_pointer;
95981ce609eSEd Tanous         sensorJson["ReadingUnits"] = "RPM";
96081ce609eSEd Tanous         sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
96181ce609eSEd Tanous         setLedState(sensorJson, inventoryItem);
96208777fb0SLewanczyk, Dawid         forceToInt = true;
9631abe55efSEd Tanous     }
9646f6d0d32SEd Tanous     else if (sensorType == "fan_pwm")
9656f6d0d32SEd Tanous     {
9663929aca1SAnthony Wilson         unit = "/Reading"_json_pointer;
96781ce609eSEd Tanous         sensorJson["ReadingUnits"] = "Percent";
96881ce609eSEd Tanous         sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
96981ce609eSEd Tanous         setLedState(sensorJson, inventoryItem);
9706f6d0d32SEd Tanous         forceToInt = true;
9716f6d0d32SEd Tanous     }
9721abe55efSEd Tanous     else if (sensorType == "voltage")
9731abe55efSEd Tanous     {
9743929aca1SAnthony Wilson         unit = "/ReadingVolts"_json_pointer;
97581ce609eSEd Tanous         sensorJson["@odata.type"] = "#Power.v1_0_0.Voltage";
9761abe55efSEd Tanous     }
9772474adfaSEd Tanous     else if (sensorType == "power")
9782474adfaSEd Tanous     {
97949c53ac9SJohnathan Mantey         std::string sensorNameLower =
98049c53ac9SJohnathan Mantey             boost::algorithm::to_lower_copy(sensorName);
98149c53ac9SJohnathan Mantey 
98255f79e6fSEd Tanous         if (sensorName == "total_power")
983028f7ebcSEddie James         {
98481ce609eSEd Tanous             sensorJson["@odata.type"] = "#Power.v1_0_0.PowerControl";
9857ab06f49SGunnar Mills             // Put multiple "sensors" into a single PowerControl, so have
9867ab06f49SGunnar Mills             // generic names for MemberId and Name. Follows Redfish mockup.
98781ce609eSEd Tanous             sensorJson["MemberId"] = "0";
98881ce609eSEd Tanous             sensorJson["Name"] = "Chassis Power Control";
9893929aca1SAnthony Wilson             unit = "/PowerConsumedWatts"_json_pointer;
990028f7ebcSEddie James         }
991028f7ebcSEddie James         else if (sensorNameLower.find("input") != std::string::npos)
99249c53ac9SJohnathan Mantey         {
9933929aca1SAnthony Wilson             unit = "/PowerInputWatts"_json_pointer;
99449c53ac9SJohnathan Mantey         }
99549c53ac9SJohnathan Mantey         else
99649c53ac9SJohnathan Mantey         {
9973929aca1SAnthony Wilson             unit = "/PowerOutputWatts"_json_pointer;
99849c53ac9SJohnathan Mantey         }
9992474adfaSEd Tanous     }
10001abe55efSEd Tanous     else
10011abe55efSEd Tanous     {
100255c7b7a2SEd Tanous         BMCWEB_LOG_ERROR << "Redfish cannot map object type for " << sensorName;
100308777fb0SLewanczyk, Dawid         return;
100408777fb0SLewanczyk, Dawid     }
100508777fb0SLewanczyk, Dawid     // Map of dbus interface name, dbus property name and redfish property_name
10063929aca1SAnthony Wilson     std::vector<
10073929aca1SAnthony Wilson         std::tuple<const char*, const char*, nlohmann::json::json_pointer>>
10083929aca1SAnthony Wilson         properties;
100908777fb0SLewanczyk, Dawid     properties.reserve(7);
101008777fb0SLewanczyk, Dawid 
101108777fb0SLewanczyk, Dawid     properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit);
1012de629b6eSShawn McCarney 
1013a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
10143929aca1SAnthony Wilson     {
10153929aca1SAnthony Wilson         properties.emplace_back(
10163929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh",
10173929aca1SAnthony Wilson             "/Thresholds/UpperCaution/Reading"_json_pointer);
10183929aca1SAnthony Wilson         properties.emplace_back(
10193929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow",
10203929aca1SAnthony Wilson             "/Thresholds/LowerCaution/Reading"_json_pointer);
10213929aca1SAnthony Wilson         properties.emplace_back(
10223929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh",
10233929aca1SAnthony Wilson             "/Thresholds/UpperCritical/Reading"_json_pointer);
10243929aca1SAnthony Wilson         properties.emplace_back(
10253929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow",
10263929aca1SAnthony Wilson             "/Thresholds/LowerCritical/Reading"_json_pointer);
10273929aca1SAnthony Wilson     }
10283929aca1SAnthony Wilson     else if (sensorType != "power")
1029de629b6eSShawn McCarney     {
103008777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
10313929aca1SAnthony Wilson                                 "WarningHigh",
10323929aca1SAnthony Wilson                                 "/UpperThresholdNonCritical"_json_pointer);
103308777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
10343929aca1SAnthony Wilson                                 "WarningLow",
10353929aca1SAnthony Wilson                                 "/LowerThresholdNonCritical"_json_pointer);
103608777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
10373929aca1SAnthony Wilson                                 "CriticalHigh",
10383929aca1SAnthony Wilson                                 "/UpperThresholdCritical"_json_pointer);
103908777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
10403929aca1SAnthony Wilson                                 "CriticalLow",
10413929aca1SAnthony Wilson                                 "/LowerThresholdCritical"_json_pointer);
1042de629b6eSShawn McCarney     }
104308777fb0SLewanczyk, Dawid 
10442474adfaSEd Tanous     // TODO Need to get UpperThresholdFatal and LowerThresholdFatal
10452474adfaSEd Tanous 
1046a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
104795a3ecadSAnthony Wilson     {
104895a3ecadSAnthony Wilson         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
10493929aca1SAnthony Wilson                                 "/ReadingRangeMin"_json_pointer);
105095a3ecadSAnthony Wilson         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
10513929aca1SAnthony Wilson                                 "/ReadingRangeMax"_json_pointer);
105295a3ecadSAnthony Wilson     }
105395a3ecadSAnthony Wilson     else if (sensorType == "temperature")
10541abe55efSEd Tanous     {
105508777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
10563929aca1SAnthony Wilson                                 "/MinReadingRangeTemp"_json_pointer);
105708777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
10583929aca1SAnthony Wilson                                 "/MaxReadingRangeTemp"_json_pointer);
10591abe55efSEd Tanous     }
1060adc4f0dbSShawn McCarney     else if (sensorType != "power")
10611abe55efSEd Tanous     {
106208777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
10633929aca1SAnthony Wilson                                 "/MinReadingRange"_json_pointer);
106408777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
10653929aca1SAnthony Wilson                                 "/MaxReadingRange"_json_pointer);
106608777fb0SLewanczyk, Dawid     }
106708777fb0SLewanczyk, Dawid 
10683929aca1SAnthony Wilson     for (const std::tuple<const char*, const char*,
10693929aca1SAnthony Wilson                           nlohmann::json::json_pointer>& p : properties)
10701abe55efSEd Tanous     {
107155f79e6fSEd Tanous         for (const auto& [interface, values] : interfacesDict)
10721abe55efSEd Tanous         {
1073711ac7a9SEd Tanous             if (interface != std::get<0>(p))
10741abe55efSEd Tanous             {
1075711ac7a9SEd Tanous                 continue;
1076711ac7a9SEd Tanous             }
107755f79e6fSEd Tanous             for (const auto& [valueName, valueVariant] : values)
1078711ac7a9SEd Tanous             {
1079711ac7a9SEd Tanous                 if (valueName != std::get<1>(p))
1080711ac7a9SEd Tanous                 {
1081711ac7a9SEd Tanous                     continue;
1082711ac7a9SEd Tanous                 }
10833929aca1SAnthony Wilson 
10843929aca1SAnthony Wilson                 // The property we want to set may be nested json, so use
10853929aca1SAnthony Wilson                 // a json_pointer for easy indexing into the json structure.
10863929aca1SAnthony Wilson                 const nlohmann::json::json_pointer& key = std::get<2>(p);
10873929aca1SAnthony Wilson 
108808777fb0SLewanczyk, Dawid                 // Attempt to pull the int64 directly
1089abf2add6SEd Tanous                 const int64_t* int64Value = std::get_if<int64_t>(&valueVariant);
109008777fb0SLewanczyk, Dawid 
1091abf2add6SEd Tanous                 const double* doubleValue = std::get_if<double>(&valueVariant);
1092028f7ebcSEddie James                 const uint32_t* uValue = std::get_if<uint32_t>(&valueVariant);
10936f6d0d32SEd Tanous                 double temp = 0.0;
10946f6d0d32SEd Tanous                 if (int64Value != nullptr)
10951abe55efSEd Tanous                 {
1096271584abSEd Tanous                     temp = static_cast<double>(*int64Value);
10976f6d0d32SEd Tanous                 }
10986f6d0d32SEd Tanous                 else if (doubleValue != nullptr)
10991abe55efSEd Tanous                 {
11006f6d0d32SEd Tanous                     temp = *doubleValue;
11011abe55efSEd Tanous                 }
1102028f7ebcSEddie James                 else if (uValue != nullptr)
1103028f7ebcSEddie James                 {
1104028f7ebcSEddie James                     temp = *uValue;
1105028f7ebcSEddie James                 }
11061abe55efSEd Tanous                 else
11071abe55efSEd Tanous                 {
11086f6d0d32SEd Tanous                     BMCWEB_LOG_ERROR
11096f6d0d32SEd Tanous                         << "Got value interface that wasn't int or double";
11106f6d0d32SEd Tanous                     continue;
111108777fb0SLewanczyk, Dawid                 }
11126f6d0d32SEd Tanous                 temp = temp * std::pow(10, scaleMultiplier);
11136f6d0d32SEd Tanous                 if (forceToInt)
11146f6d0d32SEd Tanous                 {
111581ce609eSEd Tanous                     sensorJson[key] = static_cast<int64_t>(temp);
11166f6d0d32SEd Tanous                 }
11176f6d0d32SEd Tanous                 else
11186f6d0d32SEd Tanous                 {
111981ce609eSEd Tanous                     sensorJson[key] = temp;
112008777fb0SLewanczyk, Dawid                 }
112108777fb0SLewanczyk, Dawid             }
112208777fb0SLewanczyk, Dawid         }
112308777fb0SLewanczyk, Dawid     }
1124a0ec28b6SAdrian Ambrożewicz 
112581ce609eSEd Tanous     sensorsAsyncResp->addMetadata(sensorJson, unit.to_string(),
1126a0ec28b6SAdrian Ambrożewicz                                   "/xyz/openbmc_project/sensors/" + sensorType +
1127a0ec28b6SAdrian Ambrożewicz                                       "/" + sensorName);
1128a0ec28b6SAdrian Ambrożewicz 
112955c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "Added sensor " << sensorName;
113008777fb0SLewanczyk, Dawid }
113108777fb0SLewanczyk, Dawid 
1132b5a76932SEd Tanous inline void populateFanRedundancy(
1133b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
11348bd25ccdSJames Feist {
11358bd25ccdSJames Feist     crow::connections::systemBus->async_method_call(
1136b9d36b47SEd Tanous         [sensorsAsyncResp](
1137b9d36b47SEd Tanous             const boost::system::error_code ec,
1138b9d36b47SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& resp) {
11398bd25ccdSJames Feist             if (ec)
11408bd25ccdSJames Feist             {
11418bd25ccdSJames Feist                 return; // don't have to have this interface
11428bd25ccdSJames Feist             }
1143e278c18fSEd Tanous             for (const std::pair<std::string,
1144e278c18fSEd Tanous                                  std::vector<std::pair<
1145e278c18fSEd Tanous                                      std::string, std::vector<std::string>>>>&
1146e278c18fSEd Tanous                      pathPair : resp)
11478bd25ccdSJames Feist             {
1148e278c18fSEd Tanous                 const std::string& path = pathPair.first;
1149e278c18fSEd Tanous                 const std::vector<
1150e278c18fSEd Tanous                     std::pair<std::string, std::vector<std::string>>>& objDict =
1151e278c18fSEd Tanous                     pathPair.second;
11528bd25ccdSJames Feist                 if (objDict.empty())
11538bd25ccdSJames Feist                 {
11548bd25ccdSJames Feist                     continue; // this should be impossible
11558bd25ccdSJames Feist                 }
11568bd25ccdSJames Feist 
11578bd25ccdSJames Feist                 const std::string& owner = objDict.begin()->first;
11581e1e598dSJonathan Doman                 sdbusplus::asio::getProperty<std::vector<std::string>>(
11591e1e598dSJonathan Doman                     *crow::connections::systemBus,
11601e1e598dSJonathan Doman                     "xyz.openbmc_project.ObjectMapper", path + "/chassis",
11611e1e598dSJonathan Doman                     "xyz.openbmc_project.Association", "endpoints",
11621e1e598dSJonathan Doman                     [path, owner, sensorsAsyncResp](
11631e1e598dSJonathan Doman                         const boost::system::error_code e,
11641e1e598dSJonathan Doman                         const std::vector<std::string>& endpoints) {
1165271584abSEd Tanous                         if (e)
11668bd25ccdSJames Feist                         {
11678bd25ccdSJames Feist                             return; // if they don't have an association we
11688bd25ccdSJames Feist                                     // can't tell what chassis is
11698bd25ccdSJames Feist                         }
11708bd25ccdSJames Feist                         auto found = std::find_if(
11711e1e598dSJonathan Doman                             endpoints.begin(), endpoints.end(),
11728bd25ccdSJames Feist                             [sensorsAsyncResp](const std::string& entry) {
11738bd25ccdSJames Feist                                 return entry.find(
11748bd25ccdSJames Feist                                            sensorsAsyncResp->chassisId) !=
11758bd25ccdSJames Feist                                        std::string::npos;
11768bd25ccdSJames Feist                             });
11778bd25ccdSJames Feist 
11781e1e598dSJonathan Doman                         if (found == endpoints.end())
11798bd25ccdSJames Feist                         {
11808bd25ccdSJames Feist                             return;
11818bd25ccdSJames Feist                         }
11828bd25ccdSJames Feist                         crow::connections::systemBus->async_method_call(
11838bd25ccdSJames Feist                             [path, sensorsAsyncResp](
1184271584abSEd Tanous                                 const boost::system::error_code& err,
11858bd25ccdSJames Feist                                 const boost::container::flat_map<
11868bd25ccdSJames Feist                                     std::string,
1187168e20c1SEd Tanous                                     dbus::utility::DbusVariantType>& ret) {
1188271584abSEd Tanous                                 if (err)
11898bd25ccdSJames Feist                                 {
11908bd25ccdSJames Feist                                     return; // don't have to have this
11918bd25ccdSJames Feist                                             // interface
11928bd25ccdSJames Feist                                 }
11938bd25ccdSJames Feist                                 auto findFailures = ret.find("AllowedFailures");
11948bd25ccdSJames Feist                                 auto findCollection = ret.find("Collection");
11958bd25ccdSJames Feist                                 auto findStatus = ret.find("Status");
11968bd25ccdSJames Feist 
11978bd25ccdSJames Feist                                 if (findFailures == ret.end() ||
11988bd25ccdSJames Feist                                     findCollection == ret.end() ||
11998bd25ccdSJames Feist                                     findStatus == ret.end())
12008bd25ccdSJames Feist                                 {
12018bd25ccdSJames Feist                                     BMCWEB_LOG_ERROR
12028bd25ccdSJames Feist                                         << "Invalid redundancy interface";
12038bd25ccdSJames Feist                                     messages::internalError(
12048d1b46d7Szhanghch05                                         sensorsAsyncResp->asyncResp->res);
12058bd25ccdSJames Feist                                     return;
12068bd25ccdSJames Feist                                 }
12078bd25ccdSJames Feist 
12089eb808c1SEd Tanous                                 const uint8_t* allowedFailures =
12099eb808c1SEd Tanous                                     std::get_if<uint8_t>(
12108bd25ccdSJames Feist                                         &(findFailures->second));
12119eb808c1SEd Tanous                                 const std::vector<std::string>* collection =
12128bd25ccdSJames Feist                                     std::get_if<std::vector<std::string>>(
12138bd25ccdSJames Feist                                         &(findCollection->second));
12149eb808c1SEd Tanous                                 const std::string* status =
12159eb808c1SEd Tanous                                     std::get_if<std::string>(
12168bd25ccdSJames Feist                                         &(findStatus->second));
12178bd25ccdSJames Feist 
12188bd25ccdSJames Feist                                 if (allowedFailures == nullptr ||
12198bd25ccdSJames Feist                                     collection == nullptr || status == nullptr)
12208bd25ccdSJames Feist                                 {
12218bd25ccdSJames Feist 
12228bd25ccdSJames Feist                                     BMCWEB_LOG_ERROR
12230fda0f12SGeorge Liu                                         << "Invalid redundancy interface types";
12248bd25ccdSJames Feist                                     messages::internalError(
12258d1b46d7Szhanghch05                                         sensorsAsyncResp->asyncResp->res);
12268bd25ccdSJames Feist                                     return;
12278bd25ccdSJames Feist                                 }
122828aa8de5SGeorge Liu                                 sdbusplus::message::object_path objectPath(
122928aa8de5SGeorge Liu                                     path);
123028aa8de5SGeorge Liu                                 std::string name = objectPath.filename();
123128aa8de5SGeorge Liu                                 if (name.empty())
12328bd25ccdSJames Feist                                 {
12338bd25ccdSJames Feist                                     // this should be impossible
12348bd25ccdSJames Feist                                     messages::internalError(
12358d1b46d7Szhanghch05                                         sensorsAsyncResp->asyncResp->res);
12368bd25ccdSJames Feist                                     return;
12378bd25ccdSJames Feist                                 }
12388bd25ccdSJames Feist                                 std::replace(name.begin(), name.end(), '_',
12398bd25ccdSJames Feist                                              ' ');
12408bd25ccdSJames Feist 
12418bd25ccdSJames Feist                                 std::string health;
12428bd25ccdSJames Feist 
12438bd25ccdSJames Feist                                 if (boost::ends_with(*status, "Full"))
12448bd25ccdSJames Feist                                 {
12458bd25ccdSJames Feist                                     health = "OK";
12468bd25ccdSJames Feist                                 }
12478bd25ccdSJames Feist                                 else if (boost::ends_with(*status, "Degraded"))
12488bd25ccdSJames Feist                                 {
12498bd25ccdSJames Feist                                     health = "Warning";
12508bd25ccdSJames Feist                                 }
12518bd25ccdSJames Feist                                 else
12528bd25ccdSJames Feist                                 {
12538bd25ccdSJames Feist                                     health = "Critical";
12548bd25ccdSJames Feist                                 }
12558bd25ccdSJames Feist                                 std::vector<nlohmann::json> redfishCollection;
12568bd25ccdSJames Feist                                 const auto& fanRedfish =
12578d1b46d7Szhanghch05                                     sensorsAsyncResp->asyncResp->res
12588d1b46d7Szhanghch05                                         .jsonValue["Fans"];
12598bd25ccdSJames Feist                                 for (const std::string& item : *collection)
12608bd25ccdSJames Feist                                 {
126128aa8de5SGeorge Liu                                     sdbusplus::message::object_path path(item);
126228aa8de5SGeorge Liu                                     std::string itemName = path.filename();
126328aa8de5SGeorge Liu                                     if (itemName.empty())
126428aa8de5SGeorge Liu                                     {
126528aa8de5SGeorge Liu                                         continue;
126628aa8de5SGeorge Liu                                     }
12678bd25ccdSJames Feist                                     /*
12688bd25ccdSJames Feist                                     todo(ed): merge patch that fixes the names
12698bd25ccdSJames Feist                                     std::replace(itemName.begin(),
12708bd25ccdSJames Feist                                                  itemName.end(), '_', ' ');*/
12718bd25ccdSJames Feist                                     auto schemaItem = std::find_if(
12728bd25ccdSJames Feist                                         fanRedfish.begin(), fanRedfish.end(),
12738bd25ccdSJames Feist                                         [itemName](const nlohmann::json& fan) {
12748bd25ccdSJames Feist                                             return fan["MemberId"] == itemName;
12758bd25ccdSJames Feist                                         });
12768bd25ccdSJames Feist                                     if (schemaItem != fanRedfish.end())
12778bd25ccdSJames Feist                                     {
12788bd25ccdSJames Feist                                         redfishCollection.push_back(
12798bd25ccdSJames Feist                                             {{"@odata.id",
12808bd25ccdSJames Feist                                               (*schemaItem)["@odata.id"]}});
12818bd25ccdSJames Feist                                     }
12828bd25ccdSJames Feist                                     else
12838bd25ccdSJames Feist                                     {
12848bd25ccdSJames Feist                                         BMCWEB_LOG_ERROR
12858bd25ccdSJames Feist                                             << "failed to find fan in schema";
12868bd25ccdSJames Feist                                         messages::internalError(
12878d1b46d7Szhanghch05                                             sensorsAsyncResp->asyncResp->res);
12888bd25ccdSJames Feist                                         return;
12898bd25ccdSJames Feist                                     }
12908bd25ccdSJames Feist                                 }
12918bd25ccdSJames Feist 
12923e9e72ebSKuiying Wang                                 size_t minNumNeeded =
129326f6976fSEd Tanous                                     collection->empty()
129426f6976fSEd Tanous                                         ? 0
129526f6976fSEd Tanous                                         : collection->size() - *allowedFailures;
1296271584abSEd Tanous                                 nlohmann::json& jResp =
12978d1b46d7Szhanghch05                                     sensorsAsyncResp->asyncResp->res
12988bd25ccdSJames Feist                                         .jsonValue["Redundancy"];
1299271584abSEd Tanous                                 jResp.push_back(
13008bd25ccdSJames Feist                                     {{"@odata.id",
1301717794d5SAppaRao Puli                                       "/redfish/v1/Chassis/" +
13028bd25ccdSJames Feist                                           sensorsAsyncResp->chassisId + "/" +
13038bd25ccdSJames Feist                                           sensorsAsyncResp->chassisSubNode +
13048bd25ccdSJames Feist                                           "#/Redundancy/" +
1305271584abSEd Tanous                                           std::to_string(jResp.size())},
13068bd25ccdSJames Feist                                      {"@odata.type",
13078bd25ccdSJames Feist                                       "#Redundancy.v1_3_2.Redundancy"},
13083e9e72ebSKuiying Wang                                      {"MinNumNeeded", minNumNeeded},
13098bd25ccdSJames Feist                                      {"MemberId", name},
13108bd25ccdSJames Feist                                      {"Mode", "N+m"},
13118bd25ccdSJames Feist                                      {"Name", name},
13128bd25ccdSJames Feist                                      {"RedundancySet", redfishCollection},
13138bd25ccdSJames Feist                                      {"Status",
13148bd25ccdSJames Feist                                       {{"Health", health},
13158bd25ccdSJames Feist                                        {"State", "Enabled"}}}});
13168bd25ccdSJames Feist                             },
13178bd25ccdSJames Feist                             owner, path, "org.freedesktop.DBus.Properties",
13188bd25ccdSJames Feist                             "GetAll",
13198bd25ccdSJames Feist                             "xyz.openbmc_project.Control.FanRedundancy");
13201e1e598dSJonathan Doman                     });
13218bd25ccdSJames Feist             }
13228bd25ccdSJames Feist         },
13238bd25ccdSJames Feist         "xyz.openbmc_project.ObjectMapper",
13248bd25ccdSJames Feist         "/xyz/openbmc_project/object_mapper",
13258bd25ccdSJames Feist         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
13268bd25ccdSJames Feist         "/xyz/openbmc_project/control", 2,
13278bd25ccdSJames Feist         std::array<const char*, 1>{
13288bd25ccdSJames Feist             "xyz.openbmc_project.Control.FanRedundancy"});
13298bd25ccdSJames Feist }
13308bd25ccdSJames Feist 
1331b5a76932SEd Tanous inline void
133281ce609eSEd Tanous     sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
133349c53ac9SJohnathan Mantey {
13348d1b46d7Szhanghch05     nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue;
133549c53ac9SJohnathan Mantey     std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
133681ce609eSEd Tanous     if (sensorsAsyncResp->chassisSubNode == sensors::node::power)
133749c53ac9SJohnathan Mantey     {
133849c53ac9SJohnathan Mantey         sensorHeaders = {"Voltages", "PowerSupplies"};
133949c53ac9SJohnathan Mantey     }
134049c53ac9SJohnathan Mantey     for (const std::string& sensorGroup : sensorHeaders)
134149c53ac9SJohnathan Mantey     {
134249c53ac9SJohnathan Mantey         nlohmann::json::iterator entry = response.find(sensorGroup);
134349c53ac9SJohnathan Mantey         if (entry != response.end())
134449c53ac9SJohnathan Mantey         {
134549c53ac9SJohnathan Mantey             std::sort(entry->begin(), entry->end(),
134649c53ac9SJohnathan Mantey                       [](nlohmann::json& c1, nlohmann::json& c2) {
134749c53ac9SJohnathan Mantey                           return c1["Name"] < c2["Name"];
134849c53ac9SJohnathan Mantey                       });
134949c53ac9SJohnathan Mantey 
135049c53ac9SJohnathan Mantey             // add the index counts to the end of each entry
135149c53ac9SJohnathan Mantey             size_t count = 0;
135249c53ac9SJohnathan Mantey             for (nlohmann::json& sensorJson : *entry)
135349c53ac9SJohnathan Mantey             {
135449c53ac9SJohnathan Mantey                 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
135549c53ac9SJohnathan Mantey                 if (odata == sensorJson.end())
135649c53ac9SJohnathan Mantey                 {
135749c53ac9SJohnathan Mantey                     continue;
135849c53ac9SJohnathan Mantey                 }
135949c53ac9SJohnathan Mantey                 std::string* value = odata->get_ptr<std::string*>();
136049c53ac9SJohnathan Mantey                 if (value != nullptr)
136149c53ac9SJohnathan Mantey                 {
136249c53ac9SJohnathan Mantey                     *value += std::to_string(count);
136349c53ac9SJohnathan Mantey                     count++;
136481ce609eSEd Tanous                     sensorsAsyncResp->updateUri(sensorJson["Name"], *value);
136549c53ac9SJohnathan Mantey                 }
136649c53ac9SJohnathan Mantey             }
136749c53ac9SJohnathan Mantey         }
136849c53ac9SJohnathan Mantey     }
136949c53ac9SJohnathan Mantey }
137049c53ac9SJohnathan Mantey 
137108777fb0SLewanczyk, Dawid /**
1372adc4f0dbSShawn McCarney  * @brief Finds the inventory item with the specified object path.
1373adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1374adc4f0dbSShawn McCarney  * @param invItemObjPath D-Bus object path of inventory item.
1375adc4f0dbSShawn McCarney  * @return Inventory item within vector, or nullptr if no match found.
13768fb49dd6SShawn McCarney  */
137723a21a1cSEd Tanous inline InventoryItem* findInventoryItem(
1378b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1379adc4f0dbSShawn McCarney     const std::string& invItemObjPath)
13808fb49dd6SShawn McCarney {
1381adc4f0dbSShawn McCarney     for (InventoryItem& inventoryItem : *inventoryItems)
13828fb49dd6SShawn McCarney     {
1383adc4f0dbSShawn McCarney         if (inventoryItem.objectPath == invItemObjPath)
13848fb49dd6SShawn McCarney         {
1385adc4f0dbSShawn McCarney             return &inventoryItem;
13868fb49dd6SShawn McCarney         }
13878fb49dd6SShawn McCarney     }
13888fb49dd6SShawn McCarney     return nullptr;
13898fb49dd6SShawn McCarney }
13908fb49dd6SShawn McCarney 
13918fb49dd6SShawn McCarney /**
1392adc4f0dbSShawn McCarney  * @brief Finds the inventory item associated with the specified sensor.
1393adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1394adc4f0dbSShawn McCarney  * @param sensorObjPath D-Bus object path of sensor.
1395adc4f0dbSShawn McCarney  * @return Inventory item within vector, or nullptr if no match found.
13968fb49dd6SShawn McCarney  */
139723a21a1cSEd Tanous inline InventoryItem* findInventoryItemForSensor(
1398b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1399adc4f0dbSShawn McCarney     const std::string& sensorObjPath)
1400adc4f0dbSShawn McCarney {
1401adc4f0dbSShawn McCarney     for (InventoryItem& inventoryItem : *inventoryItems)
1402adc4f0dbSShawn McCarney     {
1403adc4f0dbSShawn McCarney         if (inventoryItem.sensors.count(sensorObjPath) > 0)
1404adc4f0dbSShawn McCarney         {
1405adc4f0dbSShawn McCarney             return &inventoryItem;
1406adc4f0dbSShawn McCarney         }
1407adc4f0dbSShawn McCarney     }
1408adc4f0dbSShawn McCarney     return nullptr;
1409adc4f0dbSShawn McCarney }
1410adc4f0dbSShawn McCarney 
1411adc4f0dbSShawn McCarney /**
1412d500549bSAnthony Wilson  * @brief Finds the inventory item associated with the specified led path.
1413d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1414d500549bSAnthony Wilson  * @param ledObjPath D-Bus object path of led.
1415d500549bSAnthony Wilson  * @return Inventory item within vector, or nullptr if no match found.
1416d500549bSAnthony Wilson  */
1417d500549bSAnthony Wilson inline InventoryItem*
1418d500549bSAnthony Wilson     findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems,
1419d500549bSAnthony Wilson                             const std::string& ledObjPath)
1420d500549bSAnthony Wilson {
1421d500549bSAnthony Wilson     for (InventoryItem& inventoryItem : inventoryItems)
1422d500549bSAnthony Wilson     {
1423d500549bSAnthony Wilson         if (inventoryItem.ledObjectPath == ledObjPath)
1424d500549bSAnthony Wilson         {
1425d500549bSAnthony Wilson             return &inventoryItem;
1426d500549bSAnthony Wilson         }
1427d500549bSAnthony Wilson     }
1428d500549bSAnthony Wilson     return nullptr;
1429d500549bSAnthony Wilson }
1430d500549bSAnthony Wilson 
1431d500549bSAnthony Wilson /**
1432adc4f0dbSShawn McCarney  * @brief Adds inventory item and associated sensor to specified vector.
1433adc4f0dbSShawn McCarney  *
1434adc4f0dbSShawn McCarney  * Adds a new InventoryItem to the vector if necessary.  Searches for an
1435adc4f0dbSShawn McCarney  * existing InventoryItem with the specified object path.  If not found, one is
1436adc4f0dbSShawn McCarney  * added to the vector.
1437adc4f0dbSShawn McCarney  *
1438adc4f0dbSShawn McCarney  * Next, the specified sensor is added to the set of sensors associated with the
1439adc4f0dbSShawn McCarney  * InventoryItem.
1440adc4f0dbSShawn McCarney  *
1441adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1442adc4f0dbSShawn McCarney  * @param invItemObjPath D-Bus object path of inventory item.
1443adc4f0dbSShawn McCarney  * @param sensorObjPath D-Bus object path of sensor
1444adc4f0dbSShawn McCarney  */
1445b5a76932SEd Tanous inline void addInventoryItem(
1446b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1447b5a76932SEd Tanous     const std::string& invItemObjPath, const std::string& sensorObjPath)
1448adc4f0dbSShawn McCarney {
1449adc4f0dbSShawn McCarney     // Look for inventory item in vector
1450adc4f0dbSShawn McCarney     InventoryItem* inventoryItem =
1451adc4f0dbSShawn McCarney         findInventoryItem(inventoryItems, invItemObjPath);
1452adc4f0dbSShawn McCarney 
1453adc4f0dbSShawn McCarney     // If inventory item doesn't exist in vector, add it
1454adc4f0dbSShawn McCarney     if (inventoryItem == nullptr)
1455adc4f0dbSShawn McCarney     {
1456adc4f0dbSShawn McCarney         inventoryItems->emplace_back(invItemObjPath);
1457adc4f0dbSShawn McCarney         inventoryItem = &(inventoryItems->back());
1458adc4f0dbSShawn McCarney     }
1459adc4f0dbSShawn McCarney 
1460adc4f0dbSShawn McCarney     // Add sensor to set of sensors associated with inventory item
1461adc4f0dbSShawn McCarney     inventoryItem->sensors.emplace(sensorObjPath);
1462adc4f0dbSShawn McCarney }
1463adc4f0dbSShawn McCarney 
1464adc4f0dbSShawn McCarney /**
1465adc4f0dbSShawn McCarney  * @brief Stores D-Bus data in the specified inventory item.
1466adc4f0dbSShawn McCarney  *
1467adc4f0dbSShawn McCarney  * Finds D-Bus data in the specified map of interfaces.  Stores the data in the
1468adc4f0dbSShawn McCarney  * specified InventoryItem.
1469adc4f0dbSShawn McCarney  *
1470adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
1471adc4f0dbSShawn McCarney  * response.
1472adc4f0dbSShawn McCarney  *
1473adc4f0dbSShawn McCarney  * @param inventoryItem Inventory item where data will be stored.
1474adc4f0dbSShawn McCarney  * @param interfacesDict Map containing D-Bus interfaces and their properties
1475adc4f0dbSShawn McCarney  * for the specified inventory item.
1476adc4f0dbSShawn McCarney  */
147723a21a1cSEd Tanous inline void storeInventoryItemData(
1478adc4f0dbSShawn McCarney     InventoryItem& inventoryItem,
1479711ac7a9SEd Tanous     const dbus::utility::DBusInteracesMap& interfacesDict)
14808fb49dd6SShawn McCarney {
1481adc4f0dbSShawn McCarney     // Get properties from Inventory.Item interface
1482711ac7a9SEd Tanous 
14839eb808c1SEd Tanous     for (const auto& [interface, values] : interfacesDict)
14848fb49dd6SShawn McCarney     {
1485711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Item")
14868fb49dd6SShawn McCarney         {
14879eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
1488711ac7a9SEd Tanous             {
1489711ac7a9SEd Tanous                 if (name == "Present")
1490711ac7a9SEd Tanous                 {
1491711ac7a9SEd Tanous                     const bool* value = std::get_if<bool>(&dbusValue);
1492adc4f0dbSShawn McCarney                     if (value != nullptr)
14938fb49dd6SShawn McCarney                     {
1494adc4f0dbSShawn McCarney                         inventoryItem.isPresent = *value;
14958fb49dd6SShawn McCarney                     }
14968fb49dd6SShawn McCarney                 }
14978fb49dd6SShawn McCarney             }
1498711ac7a9SEd Tanous         }
1499adc4f0dbSShawn McCarney         // Check if Inventory.Item.PowerSupply interface is present
1500711ac7a9SEd Tanous 
1501711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Item.PowerSupply")
15028fb49dd6SShawn McCarney         {
1503adc4f0dbSShawn McCarney             inventoryItem.isPowerSupply = true;
15048fb49dd6SShawn McCarney         }
1505adc4f0dbSShawn McCarney 
1506adc4f0dbSShawn McCarney         // Get properties from Inventory.Decorator.Asset interface
1507711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
1508adc4f0dbSShawn McCarney         {
15099eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
1510711ac7a9SEd Tanous             {
1511711ac7a9SEd Tanous                 if (name == "Manufacturer")
1512adc4f0dbSShawn McCarney                 {
1513adc4f0dbSShawn McCarney                     const std::string* value =
1514711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
1515adc4f0dbSShawn McCarney                     if (value != nullptr)
1516adc4f0dbSShawn McCarney                     {
1517adc4f0dbSShawn McCarney                         inventoryItem.manufacturer = *value;
1518adc4f0dbSShawn McCarney                     }
1519adc4f0dbSShawn McCarney                 }
1520711ac7a9SEd Tanous                 if (name == "Model")
1521adc4f0dbSShawn McCarney                 {
1522adc4f0dbSShawn McCarney                     const std::string* value =
1523711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
1524adc4f0dbSShawn McCarney                     if (value != nullptr)
1525adc4f0dbSShawn McCarney                     {
1526adc4f0dbSShawn McCarney                         inventoryItem.model = *value;
1527adc4f0dbSShawn McCarney                     }
1528adc4f0dbSShawn McCarney                 }
1529711ac7a9SEd Tanous                 if (name == "SerialNumber")
1530adc4f0dbSShawn McCarney                 {
1531adc4f0dbSShawn McCarney                     const std::string* value =
1532711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
1533adc4f0dbSShawn McCarney                     if (value != nullptr)
1534adc4f0dbSShawn McCarney                     {
1535adc4f0dbSShawn McCarney                         inventoryItem.serialNumber = *value;
1536adc4f0dbSShawn McCarney                     }
1537adc4f0dbSShawn McCarney                 }
1538711ac7a9SEd Tanous                 if (name == "PartNumber")
1539711ac7a9SEd Tanous                 {
1540711ac7a9SEd Tanous                     const std::string* value =
1541711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
1542711ac7a9SEd Tanous                     if (value != nullptr)
1543711ac7a9SEd Tanous                     {
1544711ac7a9SEd Tanous                         inventoryItem.partNumber = *value;
1545711ac7a9SEd Tanous                     }
1546711ac7a9SEd Tanous                 }
1547711ac7a9SEd Tanous             }
1548adc4f0dbSShawn McCarney         }
1549adc4f0dbSShawn McCarney 
1550711ac7a9SEd Tanous         if (interface ==
1551711ac7a9SEd Tanous             "xyz.openbmc_project.State.Decorator.OperationalStatus")
1552adc4f0dbSShawn McCarney         {
15539eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
1554adc4f0dbSShawn McCarney             {
1555711ac7a9SEd Tanous                 if (name == "Functional")
1556711ac7a9SEd Tanous                 {
1557711ac7a9SEd Tanous                     const bool* value = std::get_if<bool>(&dbusValue);
1558adc4f0dbSShawn McCarney                     if (value != nullptr)
1559adc4f0dbSShawn McCarney                     {
1560adc4f0dbSShawn McCarney                         inventoryItem.isFunctional = *value;
15618fb49dd6SShawn McCarney                     }
15628fb49dd6SShawn McCarney                 }
15638fb49dd6SShawn McCarney             }
15648fb49dd6SShawn McCarney         }
1565711ac7a9SEd Tanous     }
1566711ac7a9SEd Tanous }
15678fb49dd6SShawn McCarney 
15688fb49dd6SShawn McCarney /**
1569adc4f0dbSShawn McCarney  * @brief Gets D-Bus data for inventory items associated with sensors.
15708fb49dd6SShawn McCarney  *
1571adc4f0dbSShawn McCarney  * Uses the specified connections (services) to obtain D-Bus data for inventory
1572adc4f0dbSShawn McCarney  * items associated with sensors.  Stores the resulting data in the
1573adc4f0dbSShawn McCarney  * inventoryItems vector.
15748fb49dd6SShawn McCarney  *
1575adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
1576adc4f0dbSShawn McCarney  * response.
1577adc4f0dbSShawn McCarney  *
1578adc4f0dbSShawn McCarney  * Finds the inventory item data asynchronously.  Invokes callback when data has
1579adc4f0dbSShawn McCarney  * been obtained.
1580adc4f0dbSShawn McCarney  *
1581adc4f0dbSShawn McCarney  * The callback must have the following signature:
1582adc4f0dbSShawn McCarney  *   @code
1583d500549bSAnthony Wilson  *   callback(void)
1584adc4f0dbSShawn McCarney  *   @endcode
1585adc4f0dbSShawn McCarney  *
1586adc4f0dbSShawn McCarney  * This function is called recursively, obtaining data asynchronously from one
1587adc4f0dbSShawn McCarney  * connection in each call.  This ensures the callback is not invoked until the
1588adc4f0dbSShawn McCarney  * last asynchronous function has completed.
15898fb49dd6SShawn McCarney  *
15908fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
1591adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1592adc4f0dbSShawn McCarney  * @param invConnections Connections that provide data for the inventory items.
15938fb49dd6SShawn McCarney  * @param objectMgrPaths Mappings from connection name to DBus object path that
15948fb49dd6SShawn McCarney  * implements ObjectManager.
1595adc4f0dbSShawn McCarney  * @param callback Callback to invoke when inventory data has been obtained.
1596adc4f0dbSShawn McCarney  * @param invConnectionsIndex Current index in invConnections.  Only specified
1597adc4f0dbSShawn McCarney  * in recursive calls to this function.
15988fb49dd6SShawn McCarney  */
1599adc4f0dbSShawn McCarney template <typename Callback>
1600adc4f0dbSShawn McCarney static void getInventoryItemsData(
16018fb49dd6SShawn McCarney     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1602adc4f0dbSShawn McCarney     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
16038fb49dd6SShawn McCarney     std::shared_ptr<boost::container::flat_set<std::string>> invConnections,
16048fb49dd6SShawn McCarney     std::shared_ptr<boost::container::flat_map<std::string, std::string>>
1605adc4f0dbSShawn McCarney         objectMgrPaths,
1606271584abSEd Tanous     Callback&& callback, size_t invConnectionsIndex = 0)
16078fb49dd6SShawn McCarney {
1608adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsData enter";
16098fb49dd6SShawn McCarney 
1610adc4f0dbSShawn McCarney     // If no more connections left, call callback
1611adc4f0dbSShawn McCarney     if (invConnectionsIndex >= invConnections->size())
16128fb49dd6SShawn McCarney     {
1613d500549bSAnthony Wilson         callback();
1614adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
1615adc4f0dbSShawn McCarney         return;
1616adc4f0dbSShawn McCarney     }
1617adc4f0dbSShawn McCarney 
1618adc4f0dbSShawn McCarney     // Get inventory item data from current connection
1619adc4f0dbSShawn McCarney     auto it = invConnections->nth(invConnectionsIndex);
1620adc4f0dbSShawn McCarney     if (it != invConnections->end())
1621adc4f0dbSShawn McCarney     {
1622adc4f0dbSShawn McCarney         const std::string& invConnection = *it;
1623adc4f0dbSShawn McCarney 
16248fb49dd6SShawn McCarney         // Response handler for GetManagedObjects
1625adc4f0dbSShawn McCarney         auto respHandler = [sensorsAsyncResp, inventoryItems, invConnections,
1626f94c4ecfSEd Tanous                             objectMgrPaths,
1627f94c4ecfSEd Tanous                             callback{std::forward<Callback>(callback)},
1628adc4f0dbSShawn McCarney                             invConnectionsIndex](
1629adc4f0dbSShawn McCarney                                const boost::system::error_code ec,
1630711ac7a9SEd Tanous                                dbus::utility::ManagedObjectType& resp) {
1631adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler enter";
16328fb49dd6SShawn McCarney             if (ec)
16338fb49dd6SShawn McCarney             {
16348fb49dd6SShawn McCarney                 BMCWEB_LOG_ERROR
1635adc4f0dbSShawn McCarney                     << "getInventoryItemsData respHandler DBus error " << ec;
16368d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
16378fb49dd6SShawn McCarney                 return;
16388fb49dd6SShawn McCarney             }
16398fb49dd6SShawn McCarney 
16408fb49dd6SShawn McCarney             // Loop through returned object paths
16418fb49dd6SShawn McCarney             for (const auto& objDictEntry : resp)
16428fb49dd6SShawn McCarney             {
16438fb49dd6SShawn McCarney                 const std::string& objPath =
16448fb49dd6SShawn McCarney                     static_cast<const std::string&>(objDictEntry.first);
16458fb49dd6SShawn McCarney 
1646adc4f0dbSShawn McCarney                 // If this object path is one of the specified inventory items
1647adc4f0dbSShawn McCarney                 InventoryItem* inventoryItem =
1648adc4f0dbSShawn McCarney                     findInventoryItem(inventoryItems, objPath);
1649adc4f0dbSShawn McCarney                 if (inventoryItem != nullptr)
16508fb49dd6SShawn McCarney                 {
1651adc4f0dbSShawn McCarney                     // Store inventory data in InventoryItem
1652adc4f0dbSShawn McCarney                     storeInventoryItemData(*inventoryItem, objDictEntry.second);
16538fb49dd6SShawn McCarney                 }
16548fb49dd6SShawn McCarney             }
16558fb49dd6SShawn McCarney 
1656adc4f0dbSShawn McCarney             // Recurse to get inventory item data from next connection
1657adc4f0dbSShawn McCarney             getInventoryItemsData(sensorsAsyncResp, inventoryItems,
1658adc4f0dbSShawn McCarney                                   invConnections, objectMgrPaths,
1659adc4f0dbSShawn McCarney                                   std::move(callback), invConnectionsIndex + 1);
1660adc4f0dbSShawn McCarney 
1661adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler exit";
16628fb49dd6SShawn McCarney         };
16638fb49dd6SShawn McCarney 
16648fb49dd6SShawn McCarney         // Find DBus object path that implements ObjectManager for the current
16658fb49dd6SShawn McCarney         // connection.  If no mapping found, default to "/".
16668fb49dd6SShawn McCarney         auto iter = objectMgrPaths->find(invConnection);
16678fb49dd6SShawn McCarney         const std::string& objectMgrPath =
16688fb49dd6SShawn McCarney             (iter != objectMgrPaths->end()) ? iter->second : "/";
16698fb49dd6SShawn McCarney         BMCWEB_LOG_DEBUG << "ObjectManager path for " << invConnection << " is "
16708fb49dd6SShawn McCarney                          << objectMgrPath;
16718fb49dd6SShawn McCarney 
16728fb49dd6SShawn McCarney         // Get all object paths and their interfaces for current connection
16738fb49dd6SShawn McCarney         crow::connections::systemBus->async_method_call(
16748fb49dd6SShawn McCarney             std::move(respHandler), invConnection, objectMgrPath,
16758fb49dd6SShawn McCarney             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
16768fb49dd6SShawn McCarney     }
16778fb49dd6SShawn McCarney 
1678adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
16798fb49dd6SShawn McCarney }
16808fb49dd6SShawn McCarney 
16818fb49dd6SShawn McCarney /**
1682adc4f0dbSShawn McCarney  * @brief Gets connections that provide D-Bus data for inventory items.
16838fb49dd6SShawn McCarney  *
1684adc4f0dbSShawn McCarney  * Gets the D-Bus connections (services) that provide data for the inventory
1685adc4f0dbSShawn McCarney  * items that are associated with sensors.
16868fb49dd6SShawn McCarney  *
16878fb49dd6SShawn McCarney  * Finds the connections asynchronously.  Invokes callback when information has
16888fb49dd6SShawn McCarney  * been obtained.
16898fb49dd6SShawn McCarney  *
16908fb49dd6SShawn McCarney  * The callback must have the following signature:
16918fb49dd6SShawn McCarney  *   @code
16928fb49dd6SShawn McCarney  *   callback(std::shared_ptr<boost::container::flat_set<std::string>>
16938fb49dd6SShawn McCarney  *            invConnections)
16948fb49dd6SShawn McCarney  *   @endcode
16958fb49dd6SShawn McCarney  *
16968fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
1697adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
16988fb49dd6SShawn McCarney  * @param callback Callback to invoke when connections have been obtained.
16998fb49dd6SShawn McCarney  */
17008fb49dd6SShawn McCarney template <typename Callback>
17018fb49dd6SShawn McCarney static void getInventoryItemsConnections(
1702b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1703b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
17048fb49dd6SShawn McCarney     Callback&& callback)
17058fb49dd6SShawn McCarney {
17068fb49dd6SShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsConnections enter";
17078fb49dd6SShawn McCarney 
17088fb49dd6SShawn McCarney     const std::string path = "/xyz/openbmc_project/inventory";
1709adc4f0dbSShawn McCarney     const std::array<std::string, 4> interfaces = {
17108fb49dd6SShawn McCarney         "xyz.openbmc_project.Inventory.Item",
1711adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Item.PowerSupply",
1712adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Decorator.Asset",
17138fb49dd6SShawn McCarney         "xyz.openbmc_project.State.Decorator.OperationalStatus"};
17148fb49dd6SShawn McCarney 
17158fb49dd6SShawn McCarney     // Response handler for parsing output from GetSubTree
1716f94c4ecfSEd Tanous     auto respHandler = [callback{std::forward<Callback>(callback)},
1717b9d36b47SEd Tanous                         sensorsAsyncResp, inventoryItems](
1718b9d36b47SEd Tanous                            const boost::system::error_code ec,
1719b9d36b47SEd Tanous                            const dbus::utility::MapperGetSubTreeResponse&
1720b9d36b47SEd Tanous                                subtree) {
17218fb49dd6SShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler enter";
17228fb49dd6SShawn McCarney         if (ec)
17238fb49dd6SShawn McCarney         {
17248d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
17258fb49dd6SShawn McCarney             BMCWEB_LOG_ERROR
17268fb49dd6SShawn McCarney                 << "getInventoryItemsConnections respHandler DBus error " << ec;
17278fb49dd6SShawn McCarney             return;
17288fb49dd6SShawn McCarney         }
17298fb49dd6SShawn McCarney 
17308fb49dd6SShawn McCarney         // Make unique list of connections for desired inventory items
17318fb49dd6SShawn McCarney         std::shared_ptr<boost::container::flat_set<std::string>>
17328fb49dd6SShawn McCarney             invConnections =
17338fb49dd6SShawn McCarney                 std::make_shared<boost::container::flat_set<std::string>>();
17348fb49dd6SShawn McCarney         invConnections->reserve(8);
17358fb49dd6SShawn McCarney 
17368fb49dd6SShawn McCarney         // Loop through objects from GetSubTree
17378fb49dd6SShawn McCarney         for (const std::pair<
17388fb49dd6SShawn McCarney                  std::string,
17398fb49dd6SShawn McCarney                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
17408fb49dd6SShawn McCarney                  object : subtree)
17418fb49dd6SShawn McCarney         {
1742adc4f0dbSShawn McCarney             // Check if object path is one of the specified inventory items
17438fb49dd6SShawn McCarney             const std::string& objPath = object.first;
1744adc4f0dbSShawn McCarney             if (findInventoryItem(inventoryItems, objPath) != nullptr)
17458fb49dd6SShawn McCarney             {
17468fb49dd6SShawn McCarney                 // Store all connections to inventory item
17478fb49dd6SShawn McCarney                 for (const std::pair<std::string, std::vector<std::string>>&
17488fb49dd6SShawn McCarney                          objData : object.second)
17498fb49dd6SShawn McCarney                 {
17508fb49dd6SShawn McCarney                     const std::string& invConnection = objData.first;
17518fb49dd6SShawn McCarney                     invConnections->insert(invConnection);
17528fb49dd6SShawn McCarney                 }
17538fb49dd6SShawn McCarney             }
17548fb49dd6SShawn McCarney         }
1755d500549bSAnthony Wilson 
17568fb49dd6SShawn McCarney         callback(invConnections);
17578fb49dd6SShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler exit";
17588fb49dd6SShawn McCarney     };
17598fb49dd6SShawn McCarney 
17608fb49dd6SShawn McCarney     // Make call to ObjectMapper to find all inventory items
17618fb49dd6SShawn McCarney     crow::connections::systemBus->async_method_call(
17628fb49dd6SShawn McCarney         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
17638fb49dd6SShawn McCarney         "/xyz/openbmc_project/object_mapper",
17648fb49dd6SShawn McCarney         "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
17658fb49dd6SShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsConnections exit";
17668fb49dd6SShawn McCarney }
17678fb49dd6SShawn McCarney 
17688fb49dd6SShawn McCarney /**
1769adc4f0dbSShawn McCarney  * @brief Gets associations from sensors to inventory items.
17708fb49dd6SShawn McCarney  *
17718fb49dd6SShawn McCarney  * Looks for ObjectMapper associations from the specified sensors to related
1772d500549bSAnthony Wilson  * inventory items. Then finds the associations from those inventory items to
1773d500549bSAnthony Wilson  * their LEDs, if any.
17748fb49dd6SShawn McCarney  *
17758fb49dd6SShawn McCarney  * Finds the inventory items asynchronously.  Invokes callback when information
17768fb49dd6SShawn McCarney  * has been obtained.
17778fb49dd6SShawn McCarney  *
17788fb49dd6SShawn McCarney  * The callback must have the following signature:
17798fb49dd6SShawn McCarney  *   @code
1780adc4f0dbSShawn McCarney  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
17818fb49dd6SShawn McCarney  *   @endcode
17828fb49dd6SShawn McCarney  *
17838fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
17848fb49dd6SShawn McCarney  * @param sensorNames All sensors within the current chassis.
17858fb49dd6SShawn McCarney  * @param objectMgrPaths Mappings from connection name to DBus object path that
17868fb49dd6SShawn McCarney  * implements ObjectManager.
17878fb49dd6SShawn McCarney  * @param callback Callback to invoke when inventory items have been obtained.
17888fb49dd6SShawn McCarney  */
17898fb49dd6SShawn McCarney template <typename Callback>
1790adc4f0dbSShawn McCarney static void getInventoryItemAssociations(
1791b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1792b5a76932SEd Tanous     const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames,
1793b5a76932SEd Tanous     const std::shared_ptr<boost::container::flat_map<std::string, std::string>>&
17948fb49dd6SShawn McCarney         objectMgrPaths,
17958fb49dd6SShawn McCarney     Callback&& callback)
17968fb49dd6SShawn McCarney {
1797adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemAssociations enter";
17988fb49dd6SShawn McCarney 
17998fb49dd6SShawn McCarney     // Response handler for GetManagedObjects
1800f94c4ecfSEd Tanous     auto respHandler = [callback{std::forward<Callback>(callback)},
1801f94c4ecfSEd Tanous                         sensorsAsyncResp,
18028fb49dd6SShawn McCarney                         sensorNames](const boost::system::error_code ec,
18038fb49dd6SShawn McCarney                                      dbus::utility::ManagedObjectType& resp) {
1804adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler enter";
18058fb49dd6SShawn McCarney         if (ec)
18068fb49dd6SShawn McCarney         {
1807adc4f0dbSShawn McCarney             BMCWEB_LOG_ERROR
1808adc4f0dbSShawn McCarney                 << "getInventoryItemAssociations respHandler DBus error " << ec;
18098d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
18108fb49dd6SShawn McCarney             return;
18118fb49dd6SShawn McCarney         }
18128fb49dd6SShawn McCarney 
1813adc4f0dbSShawn McCarney         // Create vector to hold list of inventory items
1814adc4f0dbSShawn McCarney         std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1815adc4f0dbSShawn McCarney             std::make_shared<std::vector<InventoryItem>>();
1816adc4f0dbSShawn McCarney 
18178fb49dd6SShawn McCarney         // Loop through returned object paths
18188fb49dd6SShawn McCarney         std::string sensorAssocPath;
18198fb49dd6SShawn McCarney         sensorAssocPath.reserve(128); // avoid memory allocations
18208fb49dd6SShawn McCarney         for (const auto& objDictEntry : resp)
18218fb49dd6SShawn McCarney         {
18228fb49dd6SShawn McCarney             const std::string& objPath =
18238fb49dd6SShawn McCarney                 static_cast<const std::string&>(objDictEntry.first);
18248fb49dd6SShawn McCarney 
18258fb49dd6SShawn McCarney             // If path is inventory association for one of the specified sensors
18268fb49dd6SShawn McCarney             for (const std::string& sensorName : *sensorNames)
18278fb49dd6SShawn McCarney             {
18288fb49dd6SShawn McCarney                 sensorAssocPath = sensorName;
18298fb49dd6SShawn McCarney                 sensorAssocPath += "/inventory";
18308fb49dd6SShawn McCarney                 if (objPath == sensorAssocPath)
18318fb49dd6SShawn McCarney                 {
18328fb49dd6SShawn McCarney                     // Get Association interface for object path
1833711ac7a9SEd Tanous                     for (const auto& [interface, values] : objDictEntry.second)
18348fb49dd6SShawn McCarney                     {
1835711ac7a9SEd Tanous                         if (interface == "xyz.openbmc_project.Association")
1836711ac7a9SEd Tanous                         {
1837711ac7a9SEd Tanous                             for (const auto& [valueName, value] : values)
1838711ac7a9SEd Tanous                             {
1839711ac7a9SEd Tanous                                 if (valueName == "endpoints")
18408fb49dd6SShawn McCarney                                 {
18418fb49dd6SShawn McCarney                                     const std::vector<std::string>* endpoints =
18428fb49dd6SShawn McCarney                                         std::get_if<std::vector<std::string>>(
1843711ac7a9SEd Tanous                                             &value);
1844711ac7a9SEd Tanous                                     if ((endpoints != nullptr) &&
1845711ac7a9SEd Tanous                                         !endpoints->empty())
18468fb49dd6SShawn McCarney                                     {
1847adc4f0dbSShawn McCarney                                         // Add inventory item to vector
1848adc4f0dbSShawn McCarney                                         const std::string& invItemPath =
1849adc4f0dbSShawn McCarney                                             endpoints->front();
1850711ac7a9SEd Tanous                                         addInventoryItem(inventoryItems,
1851711ac7a9SEd Tanous                                                          invItemPath,
1852adc4f0dbSShawn McCarney                                                          sensorName);
18538fb49dd6SShawn McCarney                                     }
18548fb49dd6SShawn McCarney                                 }
18558fb49dd6SShawn McCarney                             }
1856711ac7a9SEd Tanous                         }
1857711ac7a9SEd Tanous                     }
18588fb49dd6SShawn McCarney                     break;
18598fb49dd6SShawn McCarney                 }
18608fb49dd6SShawn McCarney             }
18618fb49dd6SShawn McCarney         }
18628fb49dd6SShawn McCarney 
1863d500549bSAnthony Wilson         // Now loop through the returned object paths again, this time to
1864d500549bSAnthony Wilson         // find the leds associated with the inventory items we just found
1865d500549bSAnthony Wilson         std::string inventoryAssocPath;
1866d500549bSAnthony Wilson         inventoryAssocPath.reserve(128); // avoid memory allocations
1867d500549bSAnthony Wilson         for (const auto& objDictEntry : resp)
1868d500549bSAnthony Wilson         {
1869d500549bSAnthony Wilson             const std::string& objPath =
1870d500549bSAnthony Wilson                 static_cast<const std::string&>(objDictEntry.first);
1871d500549bSAnthony Wilson 
1872d500549bSAnthony Wilson             for (InventoryItem& inventoryItem : *inventoryItems)
1873d500549bSAnthony Wilson             {
1874d500549bSAnthony Wilson                 inventoryAssocPath = inventoryItem.objectPath;
1875d500549bSAnthony Wilson                 inventoryAssocPath += "/leds";
1876d500549bSAnthony Wilson                 if (objPath == inventoryAssocPath)
1877d500549bSAnthony Wilson                 {
1878711ac7a9SEd Tanous                     for (const auto& [interface, values] : objDictEntry.second)
1879d500549bSAnthony Wilson                     {
1880711ac7a9SEd Tanous                         if (interface == "xyz.openbmc_project.Association")
1881711ac7a9SEd Tanous                         {
1882711ac7a9SEd Tanous                             for (const auto& [valueName, value] : values)
1883711ac7a9SEd Tanous                             {
1884711ac7a9SEd Tanous                                 if (valueName == "endpoints")
1885d500549bSAnthony Wilson                                 {
1886d500549bSAnthony Wilson                                     const std::vector<std::string>* endpoints =
1887d500549bSAnthony Wilson                                         std::get_if<std::vector<std::string>>(
1888711ac7a9SEd Tanous                                             &value);
1889711ac7a9SEd Tanous                                     if ((endpoints != nullptr) &&
1890711ac7a9SEd Tanous                                         !endpoints->empty())
1891d500549bSAnthony Wilson                                     {
1892711ac7a9SEd Tanous                                         // Add inventory item to vector
1893d500549bSAnthony Wilson                                         // Store LED path in inventory item
1894711ac7a9SEd Tanous                                         const std::string& ledPath =
1895711ac7a9SEd Tanous                                             endpoints->front();
1896d500549bSAnthony Wilson                                         inventoryItem.ledObjectPath = ledPath;
1897d500549bSAnthony Wilson                                     }
1898d500549bSAnthony Wilson                                 }
1899d500549bSAnthony Wilson                             }
1900711ac7a9SEd Tanous                         }
1901711ac7a9SEd Tanous                     }
1902711ac7a9SEd Tanous 
1903d500549bSAnthony Wilson                     break;
1904d500549bSAnthony Wilson                 }
1905d500549bSAnthony Wilson             }
1906d500549bSAnthony Wilson         }
1907adc4f0dbSShawn McCarney         callback(inventoryItems);
1908adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler exit";
19098fb49dd6SShawn McCarney     };
19108fb49dd6SShawn McCarney 
19118fb49dd6SShawn McCarney     // Find DBus object path that implements ObjectManager for ObjectMapper
19128fb49dd6SShawn McCarney     std::string connection = "xyz.openbmc_project.ObjectMapper";
19138fb49dd6SShawn McCarney     auto iter = objectMgrPaths->find(connection);
19148fb49dd6SShawn McCarney     const std::string& objectMgrPath =
19158fb49dd6SShawn McCarney         (iter != objectMgrPaths->end()) ? iter->second : "/";
19168fb49dd6SShawn McCarney     BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
19178fb49dd6SShawn McCarney                      << objectMgrPath;
19188fb49dd6SShawn McCarney 
19198fb49dd6SShawn McCarney     // Call GetManagedObjects on the ObjectMapper to get all associations
19208fb49dd6SShawn McCarney     crow::connections::systemBus->async_method_call(
19218fb49dd6SShawn McCarney         std::move(respHandler), connection, objectMgrPath,
19228fb49dd6SShawn McCarney         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
19238fb49dd6SShawn McCarney 
1924adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemAssociations exit";
19258fb49dd6SShawn McCarney }
19268fb49dd6SShawn McCarney 
19278fb49dd6SShawn McCarney /**
1928d500549bSAnthony Wilson  * @brief Gets D-Bus data for inventory item leds associated with sensors.
1929d500549bSAnthony Wilson  *
1930d500549bSAnthony Wilson  * Uses the specified connections (services) to obtain D-Bus data for inventory
1931d500549bSAnthony Wilson  * item leds associated with sensors.  Stores the resulting data in the
1932d500549bSAnthony Wilson  * inventoryItems vector.
1933d500549bSAnthony Wilson  *
1934d500549bSAnthony Wilson  * This data is later used to provide sensor property values in the JSON
1935d500549bSAnthony Wilson  * response.
1936d500549bSAnthony Wilson  *
1937d500549bSAnthony Wilson  * Finds the inventory item led data asynchronously.  Invokes callback when data
1938d500549bSAnthony Wilson  * has been obtained.
1939d500549bSAnthony Wilson  *
1940d500549bSAnthony Wilson  * The callback must have the following signature:
1941d500549bSAnthony Wilson  *   @code
194242cbe538SGunnar Mills  *   callback()
1943d500549bSAnthony Wilson  *   @endcode
1944d500549bSAnthony Wilson  *
1945d500549bSAnthony Wilson  * This function is called recursively, obtaining data asynchronously from one
1946d500549bSAnthony Wilson  * connection in each call.  This ensures the callback is not invoked until the
1947d500549bSAnthony Wilson  * last asynchronous function has completed.
1948d500549bSAnthony Wilson  *
1949d500549bSAnthony Wilson  * @param sensorsAsyncResp Pointer to object holding response data.
1950d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1951d500549bSAnthony Wilson  * @param ledConnections Connections that provide data for the inventory leds.
1952d500549bSAnthony Wilson  * @param callback Callback to invoke when inventory data has been obtained.
1953d500549bSAnthony Wilson  * @param ledConnectionsIndex Current index in ledConnections.  Only specified
1954d500549bSAnthony Wilson  * in recursive calls to this function.
1955d500549bSAnthony Wilson  */
1956d500549bSAnthony Wilson template <typename Callback>
1957d500549bSAnthony Wilson void getInventoryLedData(
1958d500549bSAnthony Wilson     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1959d500549bSAnthony Wilson     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1960d500549bSAnthony Wilson     std::shared_ptr<boost::container::flat_map<std::string, std::string>>
1961d500549bSAnthony Wilson         ledConnections,
1962d500549bSAnthony Wilson     Callback&& callback, size_t ledConnectionsIndex = 0)
1963d500549bSAnthony Wilson {
1964d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLedData enter";
1965d500549bSAnthony Wilson 
1966d500549bSAnthony Wilson     // If no more connections left, call callback
1967d500549bSAnthony Wilson     if (ledConnectionsIndex >= ledConnections->size())
1968d500549bSAnthony Wilson     {
196942cbe538SGunnar Mills         callback();
1970d500549bSAnthony Wilson         BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1971d500549bSAnthony Wilson         return;
1972d500549bSAnthony Wilson     }
1973d500549bSAnthony Wilson 
1974d500549bSAnthony Wilson     // Get inventory item data from current connection
1975d500549bSAnthony Wilson     auto it = ledConnections->nth(ledConnectionsIndex);
1976d500549bSAnthony Wilson     if (it != ledConnections->end())
1977d500549bSAnthony Wilson     {
1978d500549bSAnthony Wilson         const std::string& ledPath = (*it).first;
1979d500549bSAnthony Wilson         const std::string& ledConnection = (*it).second;
1980d500549bSAnthony Wilson         // Response handler for Get State property
19811e1e598dSJonathan Doman         auto respHandler =
19821e1e598dSJonathan Doman             [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
1983f94c4ecfSEd Tanous              callback{std::forward<Callback>(callback)}, ledConnectionsIndex](
19841e1e598dSJonathan Doman                 const boost::system::error_code ec, const std::string& state) {
1985d500549bSAnthony Wilson                 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler enter";
1986d500549bSAnthony Wilson                 if (ec)
1987d500549bSAnthony Wilson                 {
1988d500549bSAnthony Wilson                     BMCWEB_LOG_ERROR
1989d500549bSAnthony Wilson                         << "getInventoryLedData respHandler DBus error " << ec;
19908d1b46d7Szhanghch05                     messages::internalError(sensorsAsyncResp->asyncResp->res);
1991d500549bSAnthony Wilson                     return;
1992d500549bSAnthony Wilson                 }
1993d500549bSAnthony Wilson 
19941e1e598dSJonathan Doman                 BMCWEB_LOG_DEBUG << "Led state: " << state;
1995d500549bSAnthony Wilson                 // Find inventory item with this LED object path
1996d500549bSAnthony Wilson                 InventoryItem* inventoryItem =
1997d500549bSAnthony Wilson                     findInventoryItemForLed(*inventoryItems, ledPath);
1998d500549bSAnthony Wilson                 if (inventoryItem != nullptr)
1999d500549bSAnthony Wilson                 {
2000d500549bSAnthony Wilson                     // Store LED state in InventoryItem
20011e1e598dSJonathan Doman                     if (boost::ends_with(state, "On"))
2002d500549bSAnthony Wilson                     {
2003d500549bSAnthony Wilson                         inventoryItem->ledState = LedState::ON;
2004d500549bSAnthony Wilson                     }
20051e1e598dSJonathan Doman                     else if (boost::ends_with(state, "Blink"))
2006d500549bSAnthony Wilson                     {
2007d500549bSAnthony Wilson                         inventoryItem->ledState = LedState::BLINK;
2008d500549bSAnthony Wilson                     }
20091e1e598dSJonathan Doman                     else if (boost::ends_with(state, "Off"))
2010d500549bSAnthony Wilson                     {
2011d500549bSAnthony Wilson                         inventoryItem->ledState = LedState::OFF;
2012d500549bSAnthony Wilson                     }
2013d500549bSAnthony Wilson                     else
2014d500549bSAnthony Wilson                     {
2015d500549bSAnthony Wilson                         inventoryItem->ledState = LedState::UNKNOWN;
2016d500549bSAnthony Wilson                     }
2017d500549bSAnthony Wilson                 }
2018d500549bSAnthony Wilson 
2019d500549bSAnthony Wilson                 // Recurse to get LED data from next connection
2020d500549bSAnthony Wilson                 getInventoryLedData(sensorsAsyncResp, inventoryItems,
2021d500549bSAnthony Wilson                                     ledConnections, std::move(callback),
2022d500549bSAnthony Wilson                                     ledConnectionsIndex + 1);
2023d500549bSAnthony Wilson 
2024d500549bSAnthony Wilson                 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler exit";
2025d500549bSAnthony Wilson             };
2026d500549bSAnthony Wilson 
2027d500549bSAnthony Wilson         // Get the State property for the current LED
20281e1e598dSJonathan Doman         sdbusplus::asio::getProperty<std::string>(
20291e1e598dSJonathan Doman             *crow::connections::systemBus, ledConnection, ledPath,
20301e1e598dSJonathan Doman             "xyz.openbmc_project.Led.Physical", "State",
20311e1e598dSJonathan Doman             std::move(respHandler));
2032d500549bSAnthony Wilson     }
2033d500549bSAnthony Wilson 
2034d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
2035d500549bSAnthony Wilson }
2036d500549bSAnthony Wilson 
2037d500549bSAnthony Wilson /**
2038d500549bSAnthony Wilson  * @brief Gets LED data for LEDs associated with given inventory items.
2039d500549bSAnthony Wilson  *
2040d500549bSAnthony Wilson  * Gets the D-Bus connections (services) that provide LED data for the LEDs
2041d500549bSAnthony Wilson  * associated with the specified inventory items.  Then gets the LED data from
2042d500549bSAnthony Wilson  * each connection and stores it in the inventory item.
2043d500549bSAnthony Wilson  *
2044d500549bSAnthony Wilson  * This data is later used to provide sensor property values in the JSON
2045d500549bSAnthony Wilson  * response.
2046d500549bSAnthony Wilson  *
2047d500549bSAnthony Wilson  * Finds the LED data asynchronously.  Invokes callback when information has
2048d500549bSAnthony Wilson  * been obtained.
2049d500549bSAnthony Wilson  *
2050d500549bSAnthony Wilson  * The callback must have the following signature:
2051d500549bSAnthony Wilson  *   @code
205242cbe538SGunnar Mills  *   callback()
2053d500549bSAnthony Wilson  *   @endcode
2054d500549bSAnthony Wilson  *
2055d500549bSAnthony Wilson  * @param sensorsAsyncResp Pointer to object holding response data.
2056d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
2057d500549bSAnthony Wilson  * @param callback Callback to invoke when inventory items have been obtained.
2058d500549bSAnthony Wilson  */
2059d500549bSAnthony Wilson template <typename Callback>
2060d500549bSAnthony Wilson void getInventoryLeds(
2061d500549bSAnthony Wilson     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2062d500549bSAnthony Wilson     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2063d500549bSAnthony Wilson     Callback&& callback)
2064d500549bSAnthony Wilson {
2065d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLeds enter";
2066d500549bSAnthony Wilson 
2067d500549bSAnthony Wilson     const std::string path = "/xyz/openbmc_project";
2068d500549bSAnthony Wilson     const std::array<std::string, 1> interfaces = {
2069d500549bSAnthony Wilson         "xyz.openbmc_project.Led.Physical"};
2070d500549bSAnthony Wilson 
2071d500549bSAnthony Wilson     // Response handler for parsing output from GetSubTree
2072f94c4ecfSEd Tanous     auto respHandler = [callback{std::forward<Callback>(callback)},
2073b9d36b47SEd Tanous                         sensorsAsyncResp, inventoryItems](
2074b9d36b47SEd Tanous                            const boost::system::error_code ec,
2075b9d36b47SEd Tanous                            const dbus::utility::MapperGetSubTreeResponse&
2076b9d36b47SEd Tanous                                subtree) {
2077d500549bSAnthony Wilson         BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler enter";
2078d500549bSAnthony Wilson         if (ec)
2079d500549bSAnthony Wilson         {
20808d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
2081d500549bSAnthony Wilson             BMCWEB_LOG_ERROR << "getInventoryLeds respHandler DBus error "
2082d500549bSAnthony Wilson                              << ec;
2083d500549bSAnthony Wilson             return;
2084d500549bSAnthony Wilson         }
2085d500549bSAnthony Wilson 
2086d500549bSAnthony Wilson         // Build map of LED object paths to connections
2087d500549bSAnthony Wilson         std::shared_ptr<boost::container::flat_map<std::string, std::string>>
2088d500549bSAnthony Wilson             ledConnections = std::make_shared<
2089d500549bSAnthony Wilson                 boost::container::flat_map<std::string, std::string>>();
2090d500549bSAnthony Wilson 
2091d500549bSAnthony Wilson         // Loop through objects from GetSubTree
2092d500549bSAnthony Wilson         for (const std::pair<
2093d500549bSAnthony Wilson                  std::string,
2094d500549bSAnthony Wilson                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
2095d500549bSAnthony Wilson                  object : subtree)
2096d500549bSAnthony Wilson         {
2097d500549bSAnthony Wilson             // Check if object path is LED for one of the specified inventory
2098d500549bSAnthony Wilson             // items
2099d500549bSAnthony Wilson             const std::string& ledPath = object.first;
2100d500549bSAnthony Wilson             if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr)
2101d500549bSAnthony Wilson             {
2102d500549bSAnthony Wilson                 // Add mapping from ledPath to connection
2103d500549bSAnthony Wilson                 const std::string& connection = object.second.begin()->first;
2104d500549bSAnthony Wilson                 (*ledConnections)[ledPath] = connection;
2105d500549bSAnthony Wilson                 BMCWEB_LOG_DEBUG << "Added mapping " << ledPath << " -> "
2106d500549bSAnthony Wilson                                  << connection;
2107d500549bSAnthony Wilson             }
2108d500549bSAnthony Wilson         }
2109d500549bSAnthony Wilson 
2110d500549bSAnthony Wilson         getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections,
2111d500549bSAnthony Wilson                             std::move(callback));
2112d500549bSAnthony Wilson         BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler exit";
2113d500549bSAnthony Wilson     };
2114d500549bSAnthony Wilson     // Make call to ObjectMapper to find all inventory items
2115d500549bSAnthony Wilson     crow::connections::systemBus->async_method_call(
2116d500549bSAnthony Wilson         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
2117d500549bSAnthony Wilson         "/xyz/openbmc_project/object_mapper",
2118d500549bSAnthony Wilson         "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
2119d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLeds exit";
2120d500549bSAnthony Wilson }
2121d500549bSAnthony Wilson 
2122d500549bSAnthony Wilson /**
212342cbe538SGunnar Mills  * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
212442cbe538SGunnar Mills  *
212542cbe538SGunnar Mills  * Uses the specified connections (services) (currently assumes just one) to
212642cbe538SGunnar Mills  * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
212742cbe538SGunnar Mills  * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
212842cbe538SGunnar Mills  *
212942cbe538SGunnar Mills  * This data is later used to provide sensor property values in the JSON
213042cbe538SGunnar Mills  * response.
213142cbe538SGunnar Mills  *
213242cbe538SGunnar Mills  * Finds the Power Supply Attributes data asynchronously.  Invokes callback
213342cbe538SGunnar Mills  * when data has been obtained.
213442cbe538SGunnar Mills  *
213542cbe538SGunnar Mills  * The callback must have the following signature:
213642cbe538SGunnar Mills  *   @code
213742cbe538SGunnar Mills  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
213842cbe538SGunnar Mills  *   @endcode
213942cbe538SGunnar Mills  *
214042cbe538SGunnar Mills  * @param sensorsAsyncResp Pointer to object holding response data.
214142cbe538SGunnar Mills  * @param inventoryItems D-Bus inventory items associated with sensors.
214242cbe538SGunnar Mills  * @param psAttributesConnections Connections that provide data for the Power
214342cbe538SGunnar Mills  *        Supply Attributes
214442cbe538SGunnar Mills  * @param callback Callback to invoke when data has been obtained.
214542cbe538SGunnar Mills  */
214642cbe538SGunnar Mills template <typename Callback>
214742cbe538SGunnar Mills void getPowerSupplyAttributesData(
2148b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
214942cbe538SGunnar Mills     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
215042cbe538SGunnar Mills     const boost::container::flat_map<std::string, std::string>&
215142cbe538SGunnar Mills         psAttributesConnections,
215242cbe538SGunnar Mills     Callback&& callback)
215342cbe538SGunnar Mills {
215442cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData enter";
215542cbe538SGunnar Mills 
215642cbe538SGunnar Mills     if (psAttributesConnections.empty())
215742cbe538SGunnar Mills     {
215842cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "Can't find PowerSupplyAttributes, no connections!";
215942cbe538SGunnar Mills         callback(inventoryItems);
216042cbe538SGunnar Mills         return;
216142cbe538SGunnar Mills     }
216242cbe538SGunnar Mills 
216342cbe538SGunnar Mills     // Assuming just one connection (service) for now
216442cbe538SGunnar Mills     auto it = psAttributesConnections.nth(0);
216542cbe538SGunnar Mills 
216642cbe538SGunnar Mills     const std::string& psAttributesPath = (*it).first;
216742cbe538SGunnar Mills     const std::string& psAttributesConnection = (*it).second;
216842cbe538SGunnar Mills 
216942cbe538SGunnar Mills     // Response handler for Get DeratingFactor property
217042cbe538SGunnar Mills     auto respHandler = [sensorsAsyncResp, inventoryItems,
2171f94c4ecfSEd Tanous                         callback{std::forward<Callback>(callback)}](
217242cbe538SGunnar Mills                            const boost::system::error_code ec,
21731e1e598dSJonathan Doman                            const uint32_t value) {
217442cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler enter";
217542cbe538SGunnar Mills         if (ec)
217642cbe538SGunnar Mills         {
217742cbe538SGunnar Mills             BMCWEB_LOG_ERROR
217842cbe538SGunnar Mills                 << "getPowerSupplyAttributesData respHandler DBus error " << ec;
21798d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
218042cbe538SGunnar Mills             return;
218142cbe538SGunnar Mills         }
218242cbe538SGunnar Mills 
21831e1e598dSJonathan Doman         BMCWEB_LOG_DEBUG << "PS EfficiencyPercent value: " << value;
218442cbe538SGunnar Mills         // Store value in Power Supply Inventory Items
218542cbe538SGunnar Mills         for (InventoryItem& inventoryItem : *inventoryItems)
218642cbe538SGunnar Mills         {
218755f79e6fSEd Tanous             if (inventoryItem.isPowerSupply)
218842cbe538SGunnar Mills             {
218942cbe538SGunnar Mills                 inventoryItem.powerSupplyEfficiencyPercent =
21901e1e598dSJonathan Doman                     static_cast<int>(value);
219142cbe538SGunnar Mills             }
219242cbe538SGunnar Mills         }
219342cbe538SGunnar Mills 
219442cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler exit";
219542cbe538SGunnar Mills         callback(inventoryItems);
219642cbe538SGunnar Mills     };
219742cbe538SGunnar Mills 
219842cbe538SGunnar Mills     // Get the DeratingFactor property for the PowerSupplyAttributes
219942cbe538SGunnar Mills     // Currently only property on the interface/only one we care about
22001e1e598dSJonathan Doman     sdbusplus::asio::getProperty<uint32_t>(
22011e1e598dSJonathan Doman         *crow::connections::systemBus, psAttributesConnection, psAttributesPath,
22021e1e598dSJonathan Doman         "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor",
22031e1e598dSJonathan Doman         std::move(respHandler));
220442cbe538SGunnar Mills 
220542cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData exit";
220642cbe538SGunnar Mills }
220742cbe538SGunnar Mills 
220842cbe538SGunnar Mills /**
220942cbe538SGunnar Mills  * @brief Gets the Power Supply Attributes such as EfficiencyPercent
221042cbe538SGunnar Mills  *
221142cbe538SGunnar Mills  * Gets the D-Bus connection (service) that provides Power Supply Attributes
221242cbe538SGunnar Mills  * data. Then gets the Power Supply Attributes data from the connection
221342cbe538SGunnar Mills  * (currently just assumes 1 connection) and stores the data in the inventory
221442cbe538SGunnar Mills  * item.
221542cbe538SGunnar Mills  *
221642cbe538SGunnar Mills  * This data is later used to provide sensor property values in the JSON
221742cbe538SGunnar Mills  * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
221842cbe538SGunnar Mills  *
221942cbe538SGunnar Mills  * Finds the Power Supply Attributes data asynchronously. Invokes callback
222042cbe538SGunnar Mills  * when information has been obtained.
222142cbe538SGunnar Mills  *
222242cbe538SGunnar Mills  * The callback must have the following signature:
222342cbe538SGunnar Mills  *   @code
222442cbe538SGunnar Mills  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
222542cbe538SGunnar Mills  *   @endcode
222642cbe538SGunnar Mills  *
222742cbe538SGunnar Mills  * @param sensorsAsyncResp Pointer to object holding response data.
222842cbe538SGunnar Mills  * @param inventoryItems D-Bus inventory items associated with sensors.
222942cbe538SGunnar Mills  * @param callback Callback to invoke when data has been obtained.
223042cbe538SGunnar Mills  */
223142cbe538SGunnar Mills template <typename Callback>
223242cbe538SGunnar Mills void getPowerSupplyAttributes(
223342cbe538SGunnar Mills     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
223442cbe538SGunnar Mills     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
223542cbe538SGunnar Mills     Callback&& callback)
223642cbe538SGunnar Mills {
223742cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes enter";
223842cbe538SGunnar Mills 
223942cbe538SGunnar Mills     // Only need the power supply attributes when the Power Schema
2240a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode != sensors::node::power)
224142cbe538SGunnar Mills     {
224242cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit since not Power";
224342cbe538SGunnar Mills         callback(inventoryItems);
224442cbe538SGunnar Mills         return;
224542cbe538SGunnar Mills     }
224642cbe538SGunnar Mills 
224742cbe538SGunnar Mills     const std::array<std::string, 1> interfaces = {
224842cbe538SGunnar Mills         "xyz.openbmc_project.Control.PowerSupplyAttributes"};
224942cbe538SGunnar Mills 
225042cbe538SGunnar Mills     // Response handler for parsing output from GetSubTree
2251b9d36b47SEd Tanous     auto respHandler =
2252b9d36b47SEd Tanous         [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
2253b9d36b47SEd Tanous          inventoryItems](
2254b9d36b47SEd Tanous             const boost::system::error_code ec,
2255b9d36b47SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) {
225642cbe538SGunnar Mills             BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler enter";
225742cbe538SGunnar Mills             if (ec)
225842cbe538SGunnar Mills             {
22598d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
226042cbe538SGunnar Mills                 BMCWEB_LOG_ERROR
226142cbe538SGunnar Mills                     << "getPowerSupplyAttributes respHandler DBus error " << ec;
226242cbe538SGunnar Mills                 return;
226342cbe538SGunnar Mills             }
226426f6976fSEd Tanous             if (subtree.empty())
226542cbe538SGunnar Mills             {
226642cbe538SGunnar Mills                 BMCWEB_LOG_DEBUG << "Can't find Power Supply Attributes!";
226742cbe538SGunnar Mills                 callback(inventoryItems);
226842cbe538SGunnar Mills                 return;
226942cbe538SGunnar Mills             }
227042cbe538SGunnar Mills 
227142cbe538SGunnar Mills             // Currently we only support 1 power supply attribute, use this for
227242cbe538SGunnar Mills             // all the power supplies. Build map of object path to connection.
227342cbe538SGunnar Mills             // Assume just 1 connection and 1 path for now.
227442cbe538SGunnar Mills             boost::container::flat_map<std::string, std::string>
227542cbe538SGunnar Mills                 psAttributesConnections;
227642cbe538SGunnar Mills 
227742cbe538SGunnar Mills             if (subtree[0].first.empty() || subtree[0].second.empty())
227842cbe538SGunnar Mills             {
227942cbe538SGunnar Mills                 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
228042cbe538SGunnar Mills                 callback(inventoryItems);
228142cbe538SGunnar Mills                 return;
228242cbe538SGunnar Mills             }
228342cbe538SGunnar Mills 
228442cbe538SGunnar Mills             const std::string& psAttributesPath = subtree[0].first;
228542cbe538SGunnar Mills             const std::string& connection = subtree[0].second.begin()->first;
228642cbe538SGunnar Mills 
228742cbe538SGunnar Mills             if (connection.empty())
228842cbe538SGunnar Mills             {
228942cbe538SGunnar Mills                 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
229042cbe538SGunnar Mills                 callback(inventoryItems);
229142cbe538SGunnar Mills                 return;
229242cbe538SGunnar Mills             }
229342cbe538SGunnar Mills 
229442cbe538SGunnar Mills             psAttributesConnections[psAttributesPath] = connection;
229542cbe538SGunnar Mills             BMCWEB_LOG_DEBUG << "Added mapping " << psAttributesPath << " -> "
229642cbe538SGunnar Mills                              << connection;
229742cbe538SGunnar Mills 
229842cbe538SGunnar Mills             getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
229942cbe538SGunnar Mills                                          psAttributesConnections,
230042cbe538SGunnar Mills                                          std::move(callback));
230142cbe538SGunnar Mills             BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler exit";
230242cbe538SGunnar Mills         };
230342cbe538SGunnar Mills     // Make call to ObjectMapper to find the PowerSupplyAttributes service
230442cbe538SGunnar Mills     crow::connections::systemBus->async_method_call(
230542cbe538SGunnar Mills         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
230642cbe538SGunnar Mills         "/xyz/openbmc_project/object_mapper",
230742cbe538SGunnar Mills         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
230842cbe538SGunnar Mills         "/xyz/openbmc_project", 0, interfaces);
230942cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit";
231042cbe538SGunnar Mills }
231142cbe538SGunnar Mills 
231242cbe538SGunnar Mills /**
2313adc4f0dbSShawn McCarney  * @brief Gets inventory items associated with sensors.
23148fb49dd6SShawn McCarney  *
23158fb49dd6SShawn McCarney  * Finds the inventory items that are associated with the specified sensors.
2316adc4f0dbSShawn McCarney  * Then gets D-Bus data for the inventory items, such as presence and VPD.
23178fb49dd6SShawn McCarney  *
2318adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
2319adc4f0dbSShawn McCarney  * response.
23208fb49dd6SShawn McCarney  *
2321adc4f0dbSShawn McCarney  * Finds the inventory items asynchronously.  Invokes callback when the
2322adc4f0dbSShawn McCarney  * inventory items have been obtained.
2323adc4f0dbSShawn McCarney  *
2324adc4f0dbSShawn McCarney  * The callback must have the following signature:
2325adc4f0dbSShawn McCarney  *   @code
2326adc4f0dbSShawn McCarney  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2327adc4f0dbSShawn McCarney  *   @endcode
23288fb49dd6SShawn McCarney  *
23298fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
23308fb49dd6SShawn McCarney  * @param sensorNames All sensors within the current chassis.
23318fb49dd6SShawn McCarney  * @param objectMgrPaths Mappings from connection name to DBus object path that
23328fb49dd6SShawn McCarney  * implements ObjectManager.
2333adc4f0dbSShawn McCarney  * @param callback Callback to invoke when inventory items have been obtained.
23348fb49dd6SShawn McCarney  */
2335adc4f0dbSShawn McCarney template <typename Callback>
2336adc4f0dbSShawn McCarney static void getInventoryItems(
23378fb49dd6SShawn McCarney     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
23388fb49dd6SShawn McCarney     const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
23398fb49dd6SShawn McCarney     std::shared_ptr<boost::container::flat_map<std::string, std::string>>
2340adc4f0dbSShawn McCarney         objectMgrPaths,
2341adc4f0dbSShawn McCarney     Callback&& callback)
23428fb49dd6SShawn McCarney {
2343adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItems enter";
2344adc4f0dbSShawn McCarney     auto getInventoryItemAssociationsCb =
2345f94c4ecfSEd Tanous         [sensorsAsyncResp, objectMgrPaths,
2346f94c4ecfSEd Tanous          callback{std::forward<Callback>(callback)}](
2347adc4f0dbSShawn McCarney             std::shared_ptr<std::vector<InventoryItem>> inventoryItems) {
2348adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb enter";
23498fb49dd6SShawn McCarney             auto getInventoryItemsConnectionsCb =
2350adc4f0dbSShawn McCarney                 [sensorsAsyncResp, inventoryItems, objectMgrPaths,
2351f94c4ecfSEd Tanous                  callback{std::forward<const Callback>(callback)}](
23528fb49dd6SShawn McCarney                     std::shared_ptr<boost::container::flat_set<std::string>>
23538fb49dd6SShawn McCarney                         invConnections) {
23548fb49dd6SShawn McCarney                     BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb enter";
2355d500549bSAnthony Wilson                     auto getInventoryItemsDataCb =
2356d500549bSAnthony Wilson                         [sensorsAsyncResp, inventoryItems,
2357d500549bSAnthony Wilson                          callback{std::move(callback)}]() {
2358d500549bSAnthony Wilson                             BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb enter";
235942cbe538SGunnar Mills 
236042cbe538SGunnar Mills                             auto getInventoryLedsCb = [sensorsAsyncResp,
236142cbe538SGunnar Mills                                                        inventoryItems,
236242cbe538SGunnar Mills                                                        callback{std::move(
236342cbe538SGunnar Mills                                                            callback)}]() {
236442cbe538SGunnar Mills                                 BMCWEB_LOG_DEBUG << "getInventoryLedsCb enter";
236542cbe538SGunnar Mills                                 // Find Power Supply Attributes and get the data
236642cbe538SGunnar Mills                                 getPowerSupplyAttributes(sensorsAsyncResp,
236742cbe538SGunnar Mills                                                          inventoryItems,
236842cbe538SGunnar Mills                                                          std::move(callback));
236942cbe538SGunnar Mills                                 BMCWEB_LOG_DEBUG << "getInventoryLedsCb exit";
237042cbe538SGunnar Mills                             };
237142cbe538SGunnar Mills 
2372d500549bSAnthony Wilson                             // Find led connections and get the data
2373d500549bSAnthony Wilson                             getInventoryLeds(sensorsAsyncResp, inventoryItems,
237442cbe538SGunnar Mills                                              std::move(getInventoryLedsCb));
2375d500549bSAnthony Wilson                             BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb exit";
2376d500549bSAnthony Wilson                         };
23778fb49dd6SShawn McCarney 
2378adc4f0dbSShawn McCarney                     // Get inventory item data from connections
2379adc4f0dbSShawn McCarney                     getInventoryItemsData(sensorsAsyncResp, inventoryItems,
2380adc4f0dbSShawn McCarney                                           invConnections, objectMgrPaths,
2381d500549bSAnthony Wilson                                           std::move(getInventoryItemsDataCb));
23828fb49dd6SShawn McCarney                     BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb exit";
23838fb49dd6SShawn McCarney                 };
23848fb49dd6SShawn McCarney 
2385adc4f0dbSShawn McCarney             // Get connections that provide inventory item data
23868fb49dd6SShawn McCarney             getInventoryItemsConnections(
2387adc4f0dbSShawn McCarney                 sensorsAsyncResp, inventoryItems,
23888fb49dd6SShawn McCarney                 std::move(getInventoryItemsConnectionsCb));
2389adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb exit";
23908fb49dd6SShawn McCarney         };
23918fb49dd6SShawn McCarney 
2392adc4f0dbSShawn McCarney     // Get associations from sensors to inventory items
2393adc4f0dbSShawn McCarney     getInventoryItemAssociations(sensorsAsyncResp, sensorNames, objectMgrPaths,
2394adc4f0dbSShawn McCarney                                  std::move(getInventoryItemAssociationsCb));
2395adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItems exit";
2396adc4f0dbSShawn McCarney }
2397adc4f0dbSShawn McCarney 
2398adc4f0dbSShawn McCarney /**
2399adc4f0dbSShawn McCarney  * @brief Returns JSON PowerSupply object for the specified inventory item.
2400adc4f0dbSShawn McCarney  *
2401adc4f0dbSShawn McCarney  * Searches for a JSON PowerSupply object that matches the specified inventory
2402adc4f0dbSShawn McCarney  * item.  If one is not found, a new PowerSupply object is added to the JSON
2403adc4f0dbSShawn McCarney  * array.
2404adc4f0dbSShawn McCarney  *
2405adc4f0dbSShawn McCarney  * Multiple sensors are often associated with one power supply inventory item.
2406adc4f0dbSShawn McCarney  * As a result, multiple sensor values are stored in one JSON PowerSupply
2407adc4f0dbSShawn McCarney  * object.
2408adc4f0dbSShawn McCarney  *
2409adc4f0dbSShawn McCarney  * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
2410adc4f0dbSShawn McCarney  * @param inventoryItem Inventory item for the power supply.
2411adc4f0dbSShawn McCarney  * @param chassisId Chassis that contains the power supply.
2412adc4f0dbSShawn McCarney  * @return JSON PowerSupply object for the specified inventory item.
2413adc4f0dbSShawn McCarney  */
241423a21a1cSEd Tanous inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
2415adc4f0dbSShawn McCarney                                       const InventoryItem& inventoryItem,
2416adc4f0dbSShawn McCarney                                       const std::string& chassisId)
2417adc4f0dbSShawn McCarney {
2418adc4f0dbSShawn McCarney     // Check if matching PowerSupply object already exists in JSON array
2419adc4f0dbSShawn McCarney     for (nlohmann::json& powerSupply : powerSupplyArray)
2420adc4f0dbSShawn McCarney     {
2421adc4f0dbSShawn McCarney         if (powerSupply["MemberId"] == inventoryItem.name)
2422adc4f0dbSShawn McCarney         {
2423adc4f0dbSShawn McCarney             return powerSupply;
2424adc4f0dbSShawn McCarney         }
2425adc4f0dbSShawn McCarney     }
2426adc4f0dbSShawn McCarney 
2427adc4f0dbSShawn McCarney     // Add new PowerSupply object to JSON array
2428adc4f0dbSShawn McCarney     powerSupplyArray.push_back({});
2429adc4f0dbSShawn McCarney     nlohmann::json& powerSupply = powerSupplyArray.back();
2430adc4f0dbSShawn McCarney     powerSupply["@odata.id"] =
2431adc4f0dbSShawn McCarney         "/redfish/v1/Chassis/" + chassisId + "/Power#/PowerSupplies/";
2432adc4f0dbSShawn McCarney     powerSupply["MemberId"] = inventoryItem.name;
2433adc4f0dbSShawn McCarney     powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " ");
2434adc4f0dbSShawn McCarney     powerSupply["Manufacturer"] = inventoryItem.manufacturer;
2435adc4f0dbSShawn McCarney     powerSupply["Model"] = inventoryItem.model;
2436adc4f0dbSShawn McCarney     powerSupply["PartNumber"] = inventoryItem.partNumber;
2437adc4f0dbSShawn McCarney     powerSupply["SerialNumber"] = inventoryItem.serialNumber;
2438d500549bSAnthony Wilson     setLedState(powerSupply, &inventoryItem);
2439adc4f0dbSShawn McCarney 
244042cbe538SGunnar Mills     if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
244142cbe538SGunnar Mills     {
244242cbe538SGunnar Mills         powerSupply["EfficiencyPercent"] =
244342cbe538SGunnar Mills             inventoryItem.powerSupplyEfficiencyPercent;
244442cbe538SGunnar Mills     }
244542cbe538SGunnar Mills 
244642cbe538SGunnar Mills     powerSupply["Status"]["State"] = getState(&inventoryItem);
2447adc4f0dbSShawn McCarney     const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
2448adc4f0dbSShawn McCarney     powerSupply["Status"]["Health"] = health;
2449adc4f0dbSShawn McCarney 
2450adc4f0dbSShawn McCarney     return powerSupply;
24518fb49dd6SShawn McCarney }
24528fb49dd6SShawn McCarney 
24538fb49dd6SShawn McCarney /**
2454de629b6eSShawn McCarney  * @brief Gets the values of the specified sensors.
2455de629b6eSShawn McCarney  *
2456de629b6eSShawn McCarney  * Stores the results as JSON in the SensorsAsyncResp.
2457de629b6eSShawn McCarney  *
2458de629b6eSShawn McCarney  * Gets the sensor values asynchronously.  Stores the results later when the
2459de629b6eSShawn McCarney  * information has been obtained.
2460de629b6eSShawn McCarney  *
2461adc4f0dbSShawn McCarney  * The sensorNames set contains all requested sensors for the current chassis.
2462de629b6eSShawn McCarney  *
2463de629b6eSShawn McCarney  * To minimize the number of DBus calls, the DBus method
2464de629b6eSShawn McCarney  * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
2465de629b6eSShawn McCarney  * values of all sensors provided by a connection (service).
2466de629b6eSShawn McCarney  *
2467de629b6eSShawn McCarney  * The connections set contains all the connections that provide sensor values.
2468de629b6eSShawn McCarney  *
2469de629b6eSShawn McCarney  * The objectMgrPaths map contains mappings from a connection name to the
2470de629b6eSShawn McCarney  * corresponding DBus object path that implements ObjectManager.
2471de629b6eSShawn McCarney  *
2472adc4f0dbSShawn McCarney  * The InventoryItem vector contains D-Bus inventory items associated with the
2473adc4f0dbSShawn McCarney  * sensors.  Inventory item data is needed for some Redfish sensor properties.
2474adc4f0dbSShawn McCarney  *
2475de629b6eSShawn McCarney  * @param SensorsAsyncResp Pointer to object holding response data.
2476adc4f0dbSShawn McCarney  * @param sensorNames All requested sensors within the current chassis.
2477de629b6eSShawn McCarney  * @param connections Connections that provide sensor values.
2478de629b6eSShawn McCarney  * @param objectMgrPaths Mappings from connection name to DBus object path that
2479de629b6eSShawn McCarney  * implements ObjectManager.
2480adc4f0dbSShawn McCarney  * @param inventoryItems Inventory items associated with the sensors.
2481de629b6eSShawn McCarney  */
248223a21a1cSEd Tanous inline void getSensorData(
248381ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2484b5a76932SEd Tanous     const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames,
2485de629b6eSShawn McCarney     const boost::container::flat_set<std::string>& connections,
2486b5a76932SEd Tanous     const std::shared_ptr<boost::container::flat_map<std::string, std::string>>&
2487adc4f0dbSShawn McCarney         objectMgrPaths,
2488b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems)
2489de629b6eSShawn McCarney {
2490de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getSensorData enter";
2491de629b6eSShawn McCarney     // Get managed objects from all services exposing sensors
2492de629b6eSShawn McCarney     for (const std::string& connection : connections)
2493de629b6eSShawn McCarney     {
2494de629b6eSShawn McCarney         // Response handler to process managed objects
249581ce609eSEd Tanous         auto getManagedObjectsCb = [sensorsAsyncResp, sensorNames,
2496adc4f0dbSShawn McCarney                                     inventoryItems](
2497de629b6eSShawn McCarney                                        const boost::system::error_code ec,
2498711ac7a9SEd Tanous                                        dbus::utility::ManagedObjectType& resp) {
2499de629b6eSShawn McCarney             BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter";
2500de629b6eSShawn McCarney             if (ec)
2501de629b6eSShawn McCarney             {
2502de629b6eSShawn McCarney                 BMCWEB_LOG_ERROR << "getManagedObjectsCb DBUS error: " << ec;
25038d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
2504de629b6eSShawn McCarney                 return;
2505de629b6eSShawn McCarney             }
2506de629b6eSShawn McCarney             // Go through all objects and update response with sensor data
2507de629b6eSShawn McCarney             for (const auto& objDictEntry : resp)
2508de629b6eSShawn McCarney             {
2509de629b6eSShawn McCarney                 const std::string& objPath =
2510de629b6eSShawn McCarney                     static_cast<const std::string&>(objDictEntry.first);
2511de629b6eSShawn McCarney                 BMCWEB_LOG_DEBUG << "getManagedObjectsCb parsing object "
2512de629b6eSShawn McCarney                                  << objPath;
2513de629b6eSShawn McCarney 
2514de629b6eSShawn McCarney                 std::vector<std::string> split;
2515de629b6eSShawn McCarney                 // Reserve space for
2516de629b6eSShawn McCarney                 // /xyz/openbmc_project/sensors/<name>/<subname>
2517de629b6eSShawn McCarney                 split.reserve(6);
2518de629b6eSShawn McCarney                 boost::algorithm::split(split, objPath, boost::is_any_of("/"));
2519de629b6eSShawn McCarney                 if (split.size() < 6)
2520de629b6eSShawn McCarney                 {
2521de629b6eSShawn McCarney                     BMCWEB_LOG_ERROR << "Got path that isn't long enough "
2522de629b6eSShawn McCarney                                      << objPath;
2523de629b6eSShawn McCarney                     continue;
2524de629b6eSShawn McCarney                 }
2525de629b6eSShawn McCarney                 // These indexes aren't intuitive, as boost::split puts an empty
2526de629b6eSShawn McCarney                 // string at the beginning
2527de629b6eSShawn McCarney                 const std::string& sensorType = split[4];
2528de629b6eSShawn McCarney                 const std::string& sensorName = split[5];
2529de629b6eSShawn McCarney                 BMCWEB_LOG_DEBUG << "sensorName " << sensorName
2530de629b6eSShawn McCarney                                  << " sensorType " << sensorType;
253149c53ac9SJohnathan Mantey                 if (sensorNames->find(objPath) == sensorNames->end())
2532de629b6eSShawn McCarney                 {
2533accdbb2cSAndrew Geissler                     BMCWEB_LOG_DEBUG << sensorName << " not in sensor list ";
2534de629b6eSShawn McCarney                     continue;
2535de629b6eSShawn McCarney                 }
2536de629b6eSShawn McCarney 
2537adc4f0dbSShawn McCarney                 // Find inventory item (if any) associated with sensor
2538adc4f0dbSShawn McCarney                 InventoryItem* inventoryItem =
2539adc4f0dbSShawn McCarney                     findInventoryItemForSensor(inventoryItems, objPath);
2540adc4f0dbSShawn McCarney 
254195a3ecadSAnthony Wilson                 const std::string& sensorSchema =
254281ce609eSEd Tanous                     sensorsAsyncResp->chassisSubNode;
254395a3ecadSAnthony Wilson 
254495a3ecadSAnthony Wilson                 nlohmann::json* sensorJson = nullptr;
254595a3ecadSAnthony Wilson 
2546928fefb9SNan Zhou                 if (sensorSchema == sensors::node::sensors &&
2547928fefb9SNan Zhou                     !sensorsAsyncResp->efficientExpand)
254895a3ecadSAnthony Wilson                 {
25498d1b46d7Szhanghch05                     sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
255081ce609eSEd Tanous                         "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId +
255181ce609eSEd Tanous                         "/" + sensorsAsyncResp->chassisSubNode + "/" +
255295a3ecadSAnthony Wilson                         sensorName;
25538d1b46d7Szhanghch05                     sensorJson = &(sensorsAsyncResp->asyncResp->res.jsonValue);
255495a3ecadSAnthony Wilson                 }
255595a3ecadSAnthony Wilson                 else
255695a3ecadSAnthony Wilson                 {
2557271584abSEd Tanous                     std::string fieldName;
2558928fefb9SNan Zhou                     if (sensorsAsyncResp->efficientExpand)
2559928fefb9SNan Zhou                     {
2560928fefb9SNan Zhou                         fieldName = "Members";
2561928fefb9SNan Zhou                     }
2562928fefb9SNan Zhou                     else if (sensorType == "temperature")
2563de629b6eSShawn McCarney                     {
2564de629b6eSShawn McCarney                         fieldName = "Temperatures";
2565de629b6eSShawn McCarney                     }
2566de629b6eSShawn McCarney                     else if (sensorType == "fan" || sensorType == "fan_tach" ||
2567de629b6eSShawn McCarney                              sensorType == "fan_pwm")
2568de629b6eSShawn McCarney                     {
2569de629b6eSShawn McCarney                         fieldName = "Fans";
2570de629b6eSShawn McCarney                     }
2571de629b6eSShawn McCarney                     else if (sensorType == "voltage")
2572de629b6eSShawn McCarney                     {
2573de629b6eSShawn McCarney                         fieldName = "Voltages";
2574de629b6eSShawn McCarney                     }
2575de629b6eSShawn McCarney                     else if (sensorType == "power")
2576de629b6eSShawn McCarney                     {
257755f79e6fSEd Tanous                         if (sensorName == "total_power")
2578028f7ebcSEddie James                         {
2579028f7ebcSEddie James                             fieldName = "PowerControl";
2580028f7ebcSEddie James                         }
2581adc4f0dbSShawn McCarney                         else if ((inventoryItem != nullptr) &&
2582adc4f0dbSShawn McCarney                                  (inventoryItem->isPowerSupply))
2583028f7ebcSEddie James                         {
2584de629b6eSShawn McCarney                             fieldName = "PowerSupplies";
2585de629b6eSShawn McCarney                         }
2586adc4f0dbSShawn McCarney                         else
2587adc4f0dbSShawn McCarney                         {
2588adc4f0dbSShawn McCarney                             // Other power sensors are in SensorCollection
2589adc4f0dbSShawn McCarney                             continue;
2590adc4f0dbSShawn McCarney                         }
2591028f7ebcSEddie James                     }
2592de629b6eSShawn McCarney                     else
2593de629b6eSShawn McCarney                     {
2594de629b6eSShawn McCarney                         BMCWEB_LOG_ERROR << "Unsure how to handle sensorType "
2595de629b6eSShawn McCarney                                          << sensorType;
2596de629b6eSShawn McCarney                         continue;
2597de629b6eSShawn McCarney                     }
2598de629b6eSShawn McCarney 
2599de629b6eSShawn McCarney                     nlohmann::json& tempArray =
26008d1b46d7Szhanghch05                         sensorsAsyncResp->asyncResp->res.jsonValue[fieldName];
2601adc4f0dbSShawn McCarney                     if (fieldName == "PowerControl")
260249c53ac9SJohnathan Mantey                     {
2603adc4f0dbSShawn McCarney                         if (tempArray.empty())
26047ab06f49SGunnar Mills                         {
260595a3ecadSAnthony Wilson                             // Put multiple "sensors" into a single
260695a3ecadSAnthony Wilson                             // PowerControl. Follows MemberId naming and
260795a3ecadSAnthony Wilson                             // naming in power.hpp.
26087ab06f49SGunnar Mills                             tempArray.push_back(
2609adc4f0dbSShawn McCarney                                 {{"@odata.id",
2610adc4f0dbSShawn McCarney                                   "/redfish/v1/Chassis/" +
261181ce609eSEd Tanous                                       sensorsAsyncResp->chassisId + "/" +
261281ce609eSEd Tanous                                       sensorsAsyncResp->chassisSubNode + "#/" +
2613adc4f0dbSShawn McCarney                                       fieldName + "/0"}});
2614adc4f0dbSShawn McCarney                         }
2615adc4f0dbSShawn McCarney                         sensorJson = &(tempArray.back());
2616adc4f0dbSShawn McCarney                     }
2617adc4f0dbSShawn McCarney                     else if (fieldName == "PowerSupplies")
2618adc4f0dbSShawn McCarney                     {
2619adc4f0dbSShawn McCarney                         if (inventoryItem != nullptr)
2620adc4f0dbSShawn McCarney                         {
2621adc4f0dbSShawn McCarney                             sensorJson =
2622adc4f0dbSShawn McCarney                                 &(getPowerSupply(tempArray, *inventoryItem,
262381ce609eSEd Tanous                                                  sensorsAsyncResp->chassisId));
2624adc4f0dbSShawn McCarney                         }
262549c53ac9SJohnathan Mantey                     }
2626928fefb9SNan Zhou                     else if (fieldName == "Members")
2627928fefb9SNan Zhou                     {
2628928fefb9SNan Zhou                         tempArray.push_back(
2629928fefb9SNan Zhou                             {{"@odata.id",
2630928fefb9SNan Zhou                               "/redfish/v1/Chassis/" +
2631928fefb9SNan Zhou                                   sensorsAsyncResp->chassisId + "/" +
2632928fefb9SNan Zhou                                   sensorsAsyncResp->chassisSubNode + "/" +
2633928fefb9SNan Zhou                                   sensorName}});
2634928fefb9SNan Zhou                         sensorJson = &(tempArray.back());
2635928fefb9SNan Zhou                     }
263649c53ac9SJohnathan Mantey                     else
263749c53ac9SJohnathan Mantey                     {
2638de629b6eSShawn McCarney                         tempArray.push_back(
263995a3ecadSAnthony Wilson                             {{"@odata.id",
264095a3ecadSAnthony Wilson                               "/redfish/v1/Chassis/" +
264181ce609eSEd Tanous                                   sensorsAsyncResp->chassisId + "/" +
264281ce609eSEd Tanous                                   sensorsAsyncResp->chassisSubNode + "#/" +
264395a3ecadSAnthony Wilson                                   fieldName + "/"}});
2644adc4f0dbSShawn McCarney                         sensorJson = &(tempArray.back());
264549c53ac9SJohnathan Mantey                     }
264695a3ecadSAnthony Wilson                 }
2647de629b6eSShawn McCarney 
2648adc4f0dbSShawn McCarney                 if (sensorJson != nullptr)
2649adc4f0dbSShawn McCarney                 {
2650a0ec28b6SAdrian Ambrożewicz                     objectInterfacesToJson(
265181ce609eSEd Tanous                         sensorName, sensorType, sensorsAsyncResp,
2652a0ec28b6SAdrian Ambrożewicz                         objDictEntry.second, *sensorJson, inventoryItem);
2653adc4f0dbSShawn McCarney                 }
2654de629b6eSShawn McCarney             }
265581ce609eSEd Tanous             if (sensorsAsyncResp.use_count() == 1)
265649c53ac9SJohnathan Mantey             {
265781ce609eSEd Tanous                 sortJSONResponse(sensorsAsyncResp);
2658928fefb9SNan Zhou                 if (sensorsAsyncResp->chassisSubNode ==
2659928fefb9SNan Zhou                         sensors::node::sensors &&
2660928fefb9SNan Zhou                     sensorsAsyncResp->efficientExpand)
2661928fefb9SNan Zhou                 {
2662928fefb9SNan Zhou                     sensorsAsyncResp->asyncResp->res
2663928fefb9SNan Zhou                         .jsonValue["Members@odata.count"] =
2664928fefb9SNan Zhou                         sensorsAsyncResp->asyncResp->res.jsonValue["Members"]
2665928fefb9SNan Zhou                             .size();
2666928fefb9SNan Zhou                 }
2667928fefb9SNan Zhou                 else if (sensorsAsyncResp->chassisSubNode ==
2668928fefb9SNan Zhou                          sensors::node::thermal)
26698bd25ccdSJames Feist                 {
267081ce609eSEd Tanous                     populateFanRedundancy(sensorsAsyncResp);
26718bd25ccdSJames Feist                 }
267249c53ac9SJohnathan Mantey             }
2673de629b6eSShawn McCarney             BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit";
2674de629b6eSShawn McCarney         };
2675de629b6eSShawn McCarney 
2676de629b6eSShawn McCarney         // Find DBus object path that implements ObjectManager for the current
2677de629b6eSShawn McCarney         // connection.  If no mapping found, default to "/".
26788fb49dd6SShawn McCarney         auto iter = objectMgrPaths->find(connection);
2679de629b6eSShawn McCarney         const std::string& objectMgrPath =
26808fb49dd6SShawn McCarney             (iter != objectMgrPaths->end()) ? iter->second : "/";
2681de629b6eSShawn McCarney         BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
2682de629b6eSShawn McCarney                          << objectMgrPath;
2683de629b6eSShawn McCarney 
2684de629b6eSShawn McCarney         crow::connections::systemBus->async_method_call(
2685de629b6eSShawn McCarney             getManagedObjectsCb, connection, objectMgrPath,
2686de629b6eSShawn McCarney             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
268723a21a1cSEd Tanous     }
2688de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getSensorData exit";
2689de629b6eSShawn McCarney }
2690de629b6eSShawn McCarney 
269123a21a1cSEd Tanous inline void processSensorList(
269281ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2693b5a76932SEd Tanous     const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames)
26941abe55efSEd Tanous {
269595a3ecadSAnthony Wilson     auto getConnectionCb =
269681ce609eSEd Tanous         [sensorsAsyncResp, sensorNames](
269795a3ecadSAnthony Wilson             const boost::container::flat_set<std::string>& connections) {
269855c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "getConnectionCb enter";
2699de629b6eSShawn McCarney             auto getObjectManagerPathsCb =
270081ce609eSEd Tanous                 [sensorsAsyncResp, sensorNames,
2701b5a76932SEd Tanous                  connections](const std::shared_ptr<boost::container::flat_map<
2702b5a76932SEd Tanous                                   std::string, std::string>>& objectMgrPaths) {
2703de629b6eSShawn McCarney                     BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb enter";
2704adc4f0dbSShawn McCarney                     auto getInventoryItemsCb =
270581ce609eSEd Tanous                         [sensorsAsyncResp, sensorNames, connections,
2706adc4f0dbSShawn McCarney                          objectMgrPaths](
2707f23b7296SEd Tanous                             const std::shared_ptr<std::vector<InventoryItem>>&
2708adc4f0dbSShawn McCarney                                 inventoryItems) {
2709adc4f0dbSShawn McCarney                             BMCWEB_LOG_DEBUG << "getInventoryItemsCb enter";
271049c53ac9SJohnathan Mantey                             // Get sensor data and store results in JSON
271181ce609eSEd Tanous                             getSensorData(sensorsAsyncResp, sensorNames,
2712adc4f0dbSShawn McCarney                                           connections, objectMgrPaths,
2713f23b7296SEd Tanous                                           inventoryItems);
2714adc4f0dbSShawn McCarney                             BMCWEB_LOG_DEBUG << "getInventoryItemsCb exit";
2715adc4f0dbSShawn McCarney                         };
2716adc4f0dbSShawn McCarney 
2717adc4f0dbSShawn McCarney                     // Get inventory items associated with sensors
271881ce609eSEd Tanous                     getInventoryItems(sensorsAsyncResp, sensorNames,
2719adc4f0dbSShawn McCarney                                       objectMgrPaths,
2720adc4f0dbSShawn McCarney                                       std::move(getInventoryItemsCb));
2721adc4f0dbSShawn McCarney 
2722de629b6eSShawn McCarney                     BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb exit";
272308777fb0SLewanczyk, Dawid                 };
2724de629b6eSShawn McCarney 
272549c53ac9SJohnathan Mantey             // Get mapping from connection names to the DBus object
272649c53ac9SJohnathan Mantey             // paths that implement the ObjectManager interface
272781ce609eSEd Tanous             getObjectManagerPaths(sensorsAsyncResp,
2728de629b6eSShawn McCarney                                   std::move(getObjectManagerPathsCb));
272955c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "getConnectionCb exit";
273008777fb0SLewanczyk, Dawid         };
2731de629b6eSShawn McCarney 
2732de629b6eSShawn McCarney     // Get set of connections that provide sensor values
273381ce609eSEd Tanous     getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb));
273495a3ecadSAnthony Wilson }
273595a3ecadSAnthony Wilson 
273695a3ecadSAnthony Wilson /**
273795a3ecadSAnthony Wilson  * @brief Entry point for retrieving sensors data related to requested
273895a3ecadSAnthony Wilson  *        chassis.
273995a3ecadSAnthony Wilson  * @param SensorsAsyncResp   Pointer to object holding response data
274095a3ecadSAnthony Wilson  */
2741b5a76932SEd Tanous inline void
274281ce609eSEd Tanous     getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
274395a3ecadSAnthony Wilson {
274495a3ecadSAnthony Wilson     BMCWEB_LOG_DEBUG << "getChassisData enter";
274595a3ecadSAnthony Wilson     auto getChassisCb =
274681ce609eSEd Tanous         [sensorsAsyncResp](
2747f23b7296SEd Tanous             const std::shared_ptr<boost::container::flat_set<std::string>>&
274895a3ecadSAnthony Wilson                 sensorNames) {
274995a3ecadSAnthony Wilson             BMCWEB_LOG_DEBUG << "getChassisCb enter";
275081ce609eSEd Tanous             processSensorList(sensorsAsyncResp, sensorNames);
275155c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "getChassisCb exit";
275208777fb0SLewanczyk, Dawid         };
2753928fefb9SNan Zhou     // SensorCollection doesn't contain the Redundancy property
2754928fefb9SNan Zhou     if (sensorsAsyncResp->chassisSubNode != sensors::node::sensors)
2755928fefb9SNan Zhou     {
27568d1b46d7Szhanghch05         sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] =
27578d1b46d7Szhanghch05             nlohmann::json::array();
2758928fefb9SNan Zhou     }
275926f03899SShawn McCarney     // Get set of sensors in chassis
276081ce609eSEd Tanous     getChassis(sensorsAsyncResp, std::move(getChassisCb));
276155c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "getChassisData exit";
2762271584abSEd Tanous }
276308777fb0SLewanczyk, Dawid 
2764413961deSRichard Marian Thomaiyar /**
276549c53ac9SJohnathan Mantey  * @brief Find the requested sensorName in the list of all sensors supplied by
276649c53ac9SJohnathan Mantey  * the chassis node
276749c53ac9SJohnathan Mantey  *
276849c53ac9SJohnathan Mantey  * @param sensorName   The sensor name supplied in the PATCH request
276949c53ac9SJohnathan Mantey  * @param sensorsList  The list of sensors managed by the chassis node
277049c53ac9SJohnathan Mantey  * @param sensorsModified  The list of sensors that were found as a result of
277149c53ac9SJohnathan Mantey  *                         repeated calls to this function
277249c53ac9SJohnathan Mantey  */
277323a21a1cSEd Tanous inline bool findSensorNameUsingSensorPath(
27740a86febdSRichard Marian Thomaiyar     std::string_view sensorName,
277549c53ac9SJohnathan Mantey     boost::container::flat_set<std::string>& sensorsList,
277649c53ac9SJohnathan Mantey     boost::container::flat_set<std::string>& sensorsModified)
277749c53ac9SJohnathan Mantey {
277828aa8de5SGeorge Liu     for (auto& chassisSensor : sensorsList)
277949c53ac9SJohnathan Mantey     {
278028aa8de5SGeorge Liu         sdbusplus::message::object_path path(chassisSensor);
2781b00dcc27SEd Tanous         std::string thisSensorName = path.filename();
278228aa8de5SGeorge Liu         if (thisSensorName.empty())
278349c53ac9SJohnathan Mantey         {
278449c53ac9SJohnathan Mantey             continue;
278549c53ac9SJohnathan Mantey         }
278649c53ac9SJohnathan Mantey         if (thisSensorName == sensorName)
278749c53ac9SJohnathan Mantey         {
278849c53ac9SJohnathan Mantey             sensorsModified.emplace(chassisSensor);
278949c53ac9SJohnathan Mantey             return true;
279049c53ac9SJohnathan Mantey         }
279149c53ac9SJohnathan Mantey     }
279249c53ac9SJohnathan Mantey     return false;
279349c53ac9SJohnathan Mantey }
279449c53ac9SJohnathan Mantey 
279549c53ac9SJohnathan Mantey /**
2796413961deSRichard Marian Thomaiyar  * @brief Entry point for overriding sensor values of given sensor
2797413961deSRichard Marian Thomaiyar  *
27988d1b46d7Szhanghch05  * @param sensorAsyncResp   response object
27994bb3dc34SCarol Wang  * @param allCollections   Collections extract from sensors' request patch info
2800413961deSRichard Marian Thomaiyar  * @param chassisSubNode   Chassis Node for which the query has to happen
2801413961deSRichard Marian Thomaiyar  */
280223a21a1cSEd Tanous inline void setSensorsOverride(
2803b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
28044bb3dc34SCarol Wang     std::unordered_map<std::string, std::vector<nlohmann::json>>&
2805397fd61fSjayaprakash Mutyala         allCollections)
2806413961deSRichard Marian Thomaiyar {
280770d1d0aaSjayaprakash Mutyala     BMCWEB_LOG_INFO << "setSensorsOverride for subNode"
28084bb3dc34SCarol Wang                     << sensorAsyncResp->chassisSubNode << "\n";
2809413961deSRichard Marian Thomaiyar 
2810543f4400SEd Tanous     const char* propertyValueName = nullptr;
2811f65af9e8SRichard Marian Thomaiyar     std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
2812413961deSRichard Marian Thomaiyar     std::string memberId;
2813543f4400SEd Tanous     double value = 0.0;
2814f65af9e8SRichard Marian Thomaiyar     for (auto& collectionItems : allCollections)
2815f65af9e8SRichard Marian Thomaiyar     {
2816f65af9e8SRichard Marian Thomaiyar         if (collectionItems.first == "Temperatures")
2817f65af9e8SRichard Marian Thomaiyar         {
2818f65af9e8SRichard Marian Thomaiyar             propertyValueName = "ReadingCelsius";
2819f65af9e8SRichard Marian Thomaiyar         }
2820f65af9e8SRichard Marian Thomaiyar         else if (collectionItems.first == "Fans")
2821f65af9e8SRichard Marian Thomaiyar         {
2822f65af9e8SRichard Marian Thomaiyar             propertyValueName = "Reading";
2823f65af9e8SRichard Marian Thomaiyar         }
2824f65af9e8SRichard Marian Thomaiyar         else
2825f65af9e8SRichard Marian Thomaiyar         {
2826f65af9e8SRichard Marian Thomaiyar             propertyValueName = "ReadingVolts";
2827f65af9e8SRichard Marian Thomaiyar         }
2828f65af9e8SRichard Marian Thomaiyar         for (auto& item : collectionItems.second)
2829f65af9e8SRichard Marian Thomaiyar         {
28308d1b46d7Szhanghch05             if (!json_util::readJson(item, sensorAsyncResp->asyncResp->res,
28318d1b46d7Szhanghch05                                      "MemberId", memberId, propertyValueName,
28328d1b46d7Szhanghch05                                      value))
2833413961deSRichard Marian Thomaiyar             {
2834413961deSRichard Marian Thomaiyar                 return;
2835413961deSRichard Marian Thomaiyar             }
2836f65af9e8SRichard Marian Thomaiyar             overrideMap.emplace(memberId,
2837f65af9e8SRichard Marian Thomaiyar                                 std::make_pair(value, collectionItems.first));
2838f65af9e8SRichard Marian Thomaiyar         }
2839f65af9e8SRichard Marian Thomaiyar     }
28404bb3dc34SCarol Wang 
2841b5a76932SEd Tanous     auto getChassisSensorListCb = [sensorAsyncResp, overrideMap](
2842b5a76932SEd Tanous                                       const std::shared_ptr<
284349c53ac9SJohnathan Mantey                                           boost::container::flat_set<
2844b5a76932SEd Tanous                                               std::string>>& sensorsList) {
284549c53ac9SJohnathan Mantey         // Match sensor names in the PATCH request to those managed by the
284649c53ac9SJohnathan Mantey         // chassis node
284749c53ac9SJohnathan Mantey         const std::shared_ptr<boost::container::flat_set<std::string>>
284849c53ac9SJohnathan Mantey             sensorNames =
284949c53ac9SJohnathan Mantey                 std::make_shared<boost::container::flat_set<std::string>>();
2850f65af9e8SRichard Marian Thomaiyar         for (const auto& item : overrideMap)
2851413961deSRichard Marian Thomaiyar         {
2852f65af9e8SRichard Marian Thomaiyar             const auto& sensor = item.first;
285349c53ac9SJohnathan Mantey             if (!findSensorNameUsingSensorPath(sensor, *sensorsList,
285449c53ac9SJohnathan Mantey                                                *sensorNames))
2855f65af9e8SRichard Marian Thomaiyar             {
2856f65af9e8SRichard Marian Thomaiyar                 BMCWEB_LOG_INFO << "Unable to find memberId " << item.first;
28578d1b46d7Szhanghch05                 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2858f65af9e8SRichard Marian Thomaiyar                                            item.second.second, item.first);
2859413961deSRichard Marian Thomaiyar                 return;
2860413961deSRichard Marian Thomaiyar             }
2861f65af9e8SRichard Marian Thomaiyar         }
2862413961deSRichard Marian Thomaiyar         // Get the connection to which the memberId belongs
28634f277b54SJayaprakash Mutyala         auto getObjectsWithConnectionCb = [sensorAsyncResp, overrideMap](
28644f277b54SJayaprakash Mutyala                                               const boost::container::flat_set<
28654f277b54SJayaprakash Mutyala                                                   std::string>& /*connections*/,
28664f277b54SJayaprakash Mutyala                                               const std::set<std::pair<
28674f277b54SJayaprakash Mutyala                                                   std::string, std::string>>&
2868413961deSRichard Marian Thomaiyar                                                   objectsWithConnection) {
2869f65af9e8SRichard Marian Thomaiyar             if (objectsWithConnection.size() != overrideMap.size())
2870413961deSRichard Marian Thomaiyar             {
2871413961deSRichard Marian Thomaiyar                 BMCWEB_LOG_INFO
2872f65af9e8SRichard Marian Thomaiyar                     << "Unable to find all objects with proper connection "
2873f65af9e8SRichard Marian Thomaiyar                     << objectsWithConnection.size() << " requested "
2874f65af9e8SRichard Marian Thomaiyar                     << overrideMap.size() << "\n";
28754f277b54SJayaprakash Mutyala                 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2876a0ec28b6SAdrian Ambrożewicz                                            sensorAsyncResp->chassisSubNode ==
2877a0ec28b6SAdrian Ambrożewicz                                                    sensors::node::thermal
2878413961deSRichard Marian Thomaiyar                                                ? "Temperatures"
2879413961deSRichard Marian Thomaiyar                                                : "Voltages",
2880f65af9e8SRichard Marian Thomaiyar                                            "Count");
2881f65af9e8SRichard Marian Thomaiyar                 return;
2882f65af9e8SRichard Marian Thomaiyar             }
2883f65af9e8SRichard Marian Thomaiyar             for (const auto& item : objectsWithConnection)
2884f65af9e8SRichard Marian Thomaiyar             {
288528aa8de5SGeorge Liu                 sdbusplus::message::object_path path(item.first);
288628aa8de5SGeorge Liu                 std::string sensorName = path.filename();
288728aa8de5SGeorge Liu                 if (sensorName.empty())
2888f65af9e8SRichard Marian Thomaiyar                 {
28894f277b54SJayaprakash Mutyala                     messages::internalError(sensorAsyncResp->asyncResp->res);
2890f65af9e8SRichard Marian Thomaiyar                     return;
2891f65af9e8SRichard Marian Thomaiyar                 }
2892f65af9e8SRichard Marian Thomaiyar 
2893f65af9e8SRichard Marian Thomaiyar                 const auto& iterator = overrideMap.find(sensorName);
2894f65af9e8SRichard Marian Thomaiyar                 if (iterator == overrideMap.end())
2895f65af9e8SRichard Marian Thomaiyar                 {
2896f65af9e8SRichard Marian Thomaiyar                     BMCWEB_LOG_INFO << "Unable to find sensor object"
2897f65af9e8SRichard Marian Thomaiyar                                     << item.first << "\n";
28984f277b54SJayaprakash Mutyala                     messages::internalError(sensorAsyncResp->asyncResp->res);
2899413961deSRichard Marian Thomaiyar                     return;
2900413961deSRichard Marian Thomaiyar                 }
2901413961deSRichard Marian Thomaiyar                 crow::connections::systemBus->async_method_call(
2902f65af9e8SRichard Marian Thomaiyar                     [sensorAsyncResp](const boost::system::error_code ec) {
2903413961deSRichard Marian Thomaiyar                         if (ec)
2904413961deSRichard Marian Thomaiyar                         {
29054f277b54SJayaprakash Mutyala                             if (ec.value() ==
29064f277b54SJayaprakash Mutyala                                 boost::system::errc::permission_denied)
29074f277b54SJayaprakash Mutyala                             {
29084f277b54SJayaprakash Mutyala                                 BMCWEB_LOG_WARNING
29094f277b54SJayaprakash Mutyala                                     << "Manufacturing mode is not Enabled...can't "
29104f277b54SJayaprakash Mutyala                                        "Override the sensor value. ";
29114f277b54SJayaprakash Mutyala 
29124f277b54SJayaprakash Mutyala                                 messages::insufficientPrivilege(
29138d1b46d7Szhanghch05                                     sensorAsyncResp->asyncResp->res);
2914413961deSRichard Marian Thomaiyar                                 return;
2915413961deSRichard Marian Thomaiyar                             }
29164f277b54SJayaprakash Mutyala                             BMCWEB_LOG_DEBUG
29174f277b54SJayaprakash Mutyala                                 << "setOverrideValueStatus DBUS error: " << ec;
29184f277b54SJayaprakash Mutyala                             messages::internalError(
29194f277b54SJayaprakash Mutyala                                 sensorAsyncResp->asyncResp->res);
29204f277b54SJayaprakash Mutyala                         }
2921413961deSRichard Marian Thomaiyar                     },
29224f277b54SJayaprakash Mutyala                     item.second, item.first, "org.freedesktop.DBus.Properties",
29234f277b54SJayaprakash Mutyala                     "Set", "xyz.openbmc_project.Sensor.Value", "Value",
2924168e20c1SEd Tanous                     dbus::utility::DbusVariantType(iterator->second.first));
2925f65af9e8SRichard Marian Thomaiyar             }
2926413961deSRichard Marian Thomaiyar         };
2927413961deSRichard Marian Thomaiyar         // Get object with connection for the given sensor name
2928413961deSRichard Marian Thomaiyar         getObjectsWithConnection(sensorAsyncResp, sensorNames,
2929413961deSRichard Marian Thomaiyar                                  std::move(getObjectsWithConnectionCb));
2930413961deSRichard Marian Thomaiyar     };
2931413961deSRichard Marian Thomaiyar     // get full sensor list for the given chassisId and cross verify the sensor.
2932413961deSRichard Marian Thomaiyar     getChassis(sensorAsyncResp, std::move(getChassisSensorListCb));
2933413961deSRichard Marian Thomaiyar }
2934413961deSRichard Marian Thomaiyar 
2935a0ec28b6SAdrian Ambrożewicz /**
2936a0ec28b6SAdrian Ambrożewicz  * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
2937a0ec28b6SAdrian Ambrożewicz  * path of the sensor.
2938a0ec28b6SAdrian Ambrożewicz  *
2939a0ec28b6SAdrian Ambrożewicz  * Function builds valid Redfish response for sensor query of given chassis and
2940a0ec28b6SAdrian Ambrożewicz  * node. It then builds metadata about Redfish<->D-Bus correlations and provides
2941a0ec28b6SAdrian Ambrożewicz  * it to caller in a callback.
2942a0ec28b6SAdrian Ambrożewicz  *
2943a0ec28b6SAdrian Ambrożewicz  * @param chassis   Chassis for which retrieval should be performed
2944a0ec28b6SAdrian Ambrożewicz  * @param node  Node (group) of sensors. See sensors::node for supported values
2945a0ec28b6SAdrian Ambrożewicz  * @param mapComplete   Callback to be called with retrieval result
2946a0ec28b6SAdrian Ambrożewicz  */
2947021d32cfSKrzysztof Grobelny inline void retrieveUriToDbusMap(const std::string& chassis,
2948021d32cfSKrzysztof Grobelny                                  const std::string& node,
2949a0ec28b6SAdrian Ambrożewicz                                  SensorsAsyncResp::DataCompleteCb&& mapComplete)
2950a0ec28b6SAdrian Ambrożewicz {
2951c2bf7f99SWludzik, Jozef     auto pathIt = sensors::dbus::paths.find(node);
2952c2bf7f99SWludzik, Jozef     if (pathIt == sensors::dbus::paths.end())
2953a0ec28b6SAdrian Ambrożewicz     {
2954a0ec28b6SAdrian Ambrożewicz         BMCWEB_LOG_ERROR << "Wrong node provided : " << node;
2955a0ec28b6SAdrian Ambrożewicz         mapComplete(boost::beast::http::status::bad_request, {});
2956a0ec28b6SAdrian Ambrożewicz         return;
2957a0ec28b6SAdrian Ambrożewicz     }
2958d51e072fSKrzysztof Grobelny 
295972374eb7SNan Zhou     auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
2960a0ec28b6SAdrian Ambrożewicz     auto callback =
296172374eb7SNan Zhou         [asyncResp, mapCompleteCb{std::move(mapComplete)}](
2962a0ec28b6SAdrian Ambrożewicz             const boost::beast::http::status status,
2963a0ec28b6SAdrian Ambrożewicz             const boost::container::flat_map<std::string, std::string>&
2964a0ec28b6SAdrian Ambrożewicz                 uriToDbus) { mapCompleteCb(status, uriToDbus); };
2965a0ec28b6SAdrian Ambrożewicz 
2966a0ec28b6SAdrian Ambrożewicz     auto resp = std::make_shared<SensorsAsyncResp>(
2967d51e072fSKrzysztof Grobelny         asyncResp, chassis, pathIt->second, node, std::move(callback));
2968a0ec28b6SAdrian Ambrożewicz     getChassisData(resp);
2969a0ec28b6SAdrian Ambrożewicz }
2970a0ec28b6SAdrian Ambrożewicz 
2971bacb2162SNan Zhou namespace sensors
2972bacb2162SNan Zhou {
2973928fefb9SNan Zhou 
2974bacb2162SNan Zhou inline void getChassisCallback(
2975bacb2162SNan Zhou     const std::shared_ptr<SensorsAsyncResp>& asyncResp,
2976bacb2162SNan Zhou     const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames)
2977bacb2162SNan Zhou {
2978bacb2162SNan Zhou     BMCWEB_LOG_DEBUG << "getChassisCallback enter";
2979bacb2162SNan Zhou 
2980bacb2162SNan Zhou     nlohmann::json& entriesArray =
2981bacb2162SNan Zhou         asyncResp->asyncResp->res.jsonValue["Members"];
2982bacb2162SNan Zhou     for (auto& sensor : *sensorNames)
2983bacb2162SNan Zhou     {
2984bacb2162SNan Zhou         BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor;
2985bacb2162SNan Zhou 
2986bacb2162SNan Zhou         sdbusplus::message::object_path path(sensor);
2987bacb2162SNan Zhou         std::string sensorName = path.filename();
2988bacb2162SNan Zhou         if (sensorName.empty())
2989bacb2162SNan Zhou         {
2990bacb2162SNan Zhou             BMCWEB_LOG_ERROR << "Invalid sensor path: " << sensor;
2991bacb2162SNan Zhou             messages::internalError(asyncResp->asyncResp->res);
2992bacb2162SNan Zhou             return;
2993bacb2162SNan Zhou         }
2994bacb2162SNan Zhou         entriesArray.push_back(
2995bacb2162SNan Zhou             {{"@odata.id", "/redfish/v1/Chassis/" + asyncResp->chassisId + "/" +
2996bacb2162SNan Zhou                                asyncResp->chassisSubNode + "/" + sensorName}});
2997bacb2162SNan Zhou     }
2998bacb2162SNan Zhou 
2999bacb2162SNan Zhou     asyncResp->asyncResp->res.jsonValue["Members@odata.count"] =
3000bacb2162SNan Zhou         entriesArray.size();
3001bacb2162SNan Zhou     BMCWEB_LOG_DEBUG << "getChassisCallback exit";
3002bacb2162SNan Zhou }
3003bacb2162SNan Zhou } // namespace sensors
3004bacb2162SNan Zhou 
30057e860f15SJohn Edward Broadbent inline void requestRoutesSensorCollection(App& app)
300695a3ecadSAnthony Wilson {
30077e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/")
3008ed398213SEd Tanous         .privileges(redfish::privileges::getSensorCollection)
3009928fefb9SNan Zhou         .methods(
3010928fefb9SNan Zhou             boost::beast::http::verb::get)([&app](
3011928fefb9SNan Zhou                                                const crow::Request& req,
3012928fefb9SNan Zhou                                                const std::shared_ptr<
3013928fefb9SNan Zhou                                                    bmcweb::AsyncResp>& aResp,
30147e860f15SJohn Edward Broadbent                                                const std::string& chassisId) {
3015928fefb9SNan Zhou             query_param::QueryCapabilities capabilities = {
3016928fefb9SNan Zhou                 .canDelegateExpandLevel = 1,
3017928fefb9SNan Zhou             };
3018928fefb9SNan Zhou             query_param::Query delegatedQuery;
3019928fefb9SNan Zhou             if (!redfish::setUpRedfishRouteWithDelegation(
3020928fefb9SNan Zhou                     app, req, aResp->res, delegatedQuery, capabilities))
302145ca1b86SEd Tanous             {
302245ca1b86SEd Tanous                 return;
302345ca1b86SEd Tanous             }
302445ca1b86SEd Tanous 
3025928fefb9SNan Zhou             if (delegatedQuery.expandType != query_param::ExpandType::None)
3026928fefb9SNan Zhou             {
3027928fefb9SNan Zhou                 // we perform efficient expand.
3028928fefb9SNan Zhou                 auto asyncResp = std::make_shared<SensorsAsyncResp>(
3029928fefb9SNan Zhou                     aResp, chassisId,
3030928fefb9SNan Zhou                     sensors::dbus::paths.at(sensors::node::sensors),
3031928fefb9SNan Zhou                     sensors::node::sensors,
3032928fefb9SNan Zhou                     /*efficientExpand=*/true);
3033928fefb9SNan Zhou                 getChassisData(asyncResp);
30348d1b46d7Szhanghch05 
3035928fefb9SNan Zhou                 BMCWEB_LOG_DEBUG
3036928fefb9SNan Zhou                     << "SensorCollection doGet exit via efficient expand handler";
3037928fefb9SNan Zhou                 return;
3038928fefb9SNan Zhou             };
3039928fefb9SNan Zhou 
3040928fefb9SNan Zhou             // if there's no efficient expand available, we use the default
3041928fefb9SNan Zhou             // Query Parameters route
3042928fefb9SNan Zhou             auto asyncResp = std::make_shared<SensorsAsyncResp>(
30438d1b46d7Szhanghch05                 aResp, chassisId,
30448d1b46d7Szhanghch05                 sensors::dbus::paths.at(sensors::node::sensors),
3045a0ec28b6SAdrian Ambrożewicz                 sensors::node::sensors);
3046928fefb9SNan Zhou 
3047928fefb9SNan Zhou             // We get all sensors as hyperlinkes in the chassis (this
3048928fefb9SNan Zhou             // implies we reply on the default query parameters handler)
3049928fefb9SNan Zhou             getChassis(asyncResp,
3050bacb2162SNan Zhou                        std::bind_front(sensors::getChassisCallback, asyncResp));
305195a3ecadSAnthony Wilson             BMCWEB_LOG_DEBUG << "SensorCollection doGet exit";
30527e860f15SJohn Edward Broadbent         });
305395a3ecadSAnthony Wilson }
305495a3ecadSAnthony Wilson 
30557e860f15SJohn Edward Broadbent inline void requestRoutesSensor(App& app)
305695a3ecadSAnthony Wilson {
30577e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/")
3058ed398213SEd Tanous         .privileges(redfish::privileges::getSensor)
30597e860f15SJohn Edward Broadbent         .methods(
306045ca1b86SEd Tanous             boost::beast::http::verb::get)([&app](
306145ca1b86SEd Tanous                                                const crow::Request& req,
30627e860f15SJohn Edward Broadbent                                                const std::shared_ptr<
30637e860f15SJohn Edward Broadbent                                                    bmcweb::AsyncResp>& aResp,
30647e860f15SJohn Edward Broadbent                                                const std::string& chassisId,
30657e860f15SJohn Edward Broadbent                                                const std::string& sensorName) {
306645ca1b86SEd Tanous             if (!redfish::setUpRedfishRoute(app, req, aResp->res))
306745ca1b86SEd Tanous             {
306845ca1b86SEd Tanous                 return;
306945ca1b86SEd Tanous             }
307095a3ecadSAnthony Wilson             BMCWEB_LOG_DEBUG << "Sensor doGet enter";
307195a3ecadSAnthony Wilson             std::shared_ptr<SensorsAsyncResp> asyncResp =
30728d1b46d7Szhanghch05                 std::make_shared<SensorsAsyncResp>(aResp, chassisId,
3073a0ec28b6SAdrian Ambrożewicz                                                    std::vector<const char*>(),
3074a0ec28b6SAdrian Ambrożewicz                                                    sensors::node::sensors);
307595a3ecadSAnthony Wilson 
307695a3ecadSAnthony Wilson             const std::array<const char*, 1> interfaces = {
307795a3ecadSAnthony Wilson                 "xyz.openbmc_project.Sensor.Value"};
307895a3ecadSAnthony Wilson 
307995a3ecadSAnthony Wilson             // Get a list of all of the sensors that implement Sensor.Value
308095a3ecadSAnthony Wilson             // and get the path and service name associated with the sensor
308195a3ecadSAnthony Wilson             crow::connections::systemBus->async_method_call(
3082b9d36b47SEd Tanous                 [asyncResp, sensorName](
3083b9d36b47SEd Tanous                     const boost::system::error_code ec,
3084b9d36b47SEd Tanous                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
308595a3ecadSAnthony Wilson                     BMCWEB_LOG_DEBUG << "respHandler1 enter";
308695a3ecadSAnthony Wilson                     if (ec)
308795a3ecadSAnthony Wilson                     {
30888d1b46d7Szhanghch05                         messages::internalError(asyncResp->asyncResp->res);
30897e860f15SJohn Edward Broadbent                         BMCWEB_LOG_ERROR
30907e860f15SJohn Edward Broadbent                             << "Sensor getSensorPaths resp_handler: "
309195a3ecadSAnthony Wilson                             << "Dbus error " << ec;
309295a3ecadSAnthony Wilson                         return;
309395a3ecadSAnthony Wilson                     }
309495a3ecadSAnthony Wilson 
3095b9d36b47SEd Tanous                     dbus::utility::MapperGetSubTreeResponse::const_iterator it =
3096b9d36b47SEd Tanous                         std::find_if(
309795a3ecadSAnthony Wilson                             subtree.begin(), subtree.end(),
309895a3ecadSAnthony Wilson                             [sensorName](
3099b9d36b47SEd Tanous                                 const std::pair<std::string,
31007e860f15SJohn Edward Broadbent                                                 std::vector<std::pair<
3101b9d36b47SEd Tanous                                                     std::string,
3102b9d36b47SEd Tanous                                                     std::vector<std::string>>>>&
310395a3ecadSAnthony Wilson                                     object) {
3104b9d36b47SEd Tanous                                 sdbusplus::message::object_path path(
3105b9d36b47SEd Tanous                                     object.first);
310628aa8de5SGeorge Liu                                 std::string name = path.filename();
310728aa8de5SGeorge Liu                                 if (name.empty())
310895a3ecadSAnthony Wilson                                 {
310995a3ecadSAnthony Wilson                                     BMCWEB_LOG_ERROR << "Invalid sensor path: "
311028aa8de5SGeorge Liu                                                      << object.first;
311195a3ecadSAnthony Wilson                                     return false;
311295a3ecadSAnthony Wilson                                 }
311395a3ecadSAnthony Wilson 
311495a3ecadSAnthony Wilson                                 return name == sensorName;
311595a3ecadSAnthony Wilson                             });
311695a3ecadSAnthony Wilson 
311795a3ecadSAnthony Wilson                     if (it == subtree.end())
311895a3ecadSAnthony Wilson                     {
311995a3ecadSAnthony Wilson                         BMCWEB_LOG_ERROR << "Could not find path for sensor: "
312095a3ecadSAnthony Wilson                                          << sensorName;
31218d1b46d7Szhanghch05                         messages::resourceNotFound(asyncResp->asyncResp->res,
31228d1b46d7Szhanghch05                                                    "Sensor", sensorName);
312395a3ecadSAnthony Wilson                         return;
312495a3ecadSAnthony Wilson                     }
312595a3ecadSAnthony Wilson                     std::string_view sensorPath = (*it).first;
312695a3ecadSAnthony Wilson                     BMCWEB_LOG_DEBUG << "Found sensor path for sensor '"
312795a3ecadSAnthony Wilson                                      << sensorName << "': " << sensorPath;
312895a3ecadSAnthony Wilson 
31297e860f15SJohn Edward Broadbent                     const std::shared_ptr<
31307e860f15SJohn Edward Broadbent                         boost::container::flat_set<std::string>>
313195a3ecadSAnthony Wilson                         sensorList = std::make_shared<
313295a3ecadSAnthony Wilson                             boost::container::flat_set<std::string>>();
313395a3ecadSAnthony Wilson 
313495a3ecadSAnthony Wilson                     sensorList->emplace(sensorPath);
313595a3ecadSAnthony Wilson                     processSensorList(asyncResp, sensorList);
313695a3ecadSAnthony Wilson                     BMCWEB_LOG_DEBUG << "respHandler1 exit";
313795a3ecadSAnthony Wilson                 },
313895a3ecadSAnthony Wilson                 "xyz.openbmc_project.ObjectMapper",
313995a3ecadSAnthony Wilson                 "/xyz/openbmc_project/object_mapper",
314095a3ecadSAnthony Wilson                 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
314195a3ecadSAnthony Wilson                 "/xyz/openbmc_project/sensors", 2, interfaces);
31427e860f15SJohn Edward Broadbent         });
314395a3ecadSAnthony Wilson }
314495a3ecadSAnthony Wilson 
314508777fb0SLewanczyk, Dawid } // namespace redfish
3146