xref: /openbmc/bmcweb/features/redfish/lib/sensors.hpp (revision 928fefb9a542b816d7c0418077def2b3874d1b0f)
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>
29*928fefb9SNan 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",
59e8204933SGeorge Liu #ifdef BMCWEB_NEW_POWERSUBSYSTEM_THERMALSUBSYSTEM
60e8204933SGeorge Liu                "/xyz/openbmc_project/sensors/voltage",
61e8204933SGeorge Liu                "/xyz/openbmc_project/sensors/fan_tach",
62e8204933SGeorge Liu                "/xyz/openbmc_project/sensors/temperature",
63e8204933SGeorge Liu                "/xyz/openbmc_project/sensors/fan_pwm",
64e8204933SGeorge Liu                "/xyz/openbmc_project/sensors/altitude",
65e8204933SGeorge Liu                "/xyz/openbmc_project/sensors/energy",
66e8204933SGeorge Liu #endif
67a0ec28b6SAdrian Ambrożewicz                "/xyz/openbmc_project/sensors/utilization"}},
68a0ec28b6SAdrian Ambrożewicz              {node::thermal,
69a0ec28b6SAdrian Ambrożewicz               {"/xyz/openbmc_project/sensors/fan_tach",
70a0ec28b6SAdrian Ambrożewicz                "/xyz/openbmc_project/sensors/temperature",
71a0ec28b6SAdrian Ambrożewicz                "/xyz/openbmc_project/sensors/fan_pwm"}}};
72c2bf7f99SWludzik, Jozef } // namespace dbus
73c2bf7f99SWludzik, Jozef 
74c2bf7f99SWludzik, Jozef inline const char* toReadingType(const std::string& sensorType)
75c2bf7f99SWludzik, Jozef {
76c2bf7f99SWludzik, Jozef     if (sensorType == "voltage")
77c2bf7f99SWludzik, Jozef     {
78c2bf7f99SWludzik, Jozef         return "Voltage";
79c2bf7f99SWludzik, Jozef     }
80c2bf7f99SWludzik, Jozef     if (sensorType == "power")
81c2bf7f99SWludzik, Jozef     {
82c2bf7f99SWludzik, Jozef         return "Power";
83c2bf7f99SWludzik, Jozef     }
84c2bf7f99SWludzik, Jozef     if (sensorType == "current")
85c2bf7f99SWludzik, Jozef     {
86c2bf7f99SWludzik, Jozef         return "Current";
87c2bf7f99SWludzik, Jozef     }
88c2bf7f99SWludzik, Jozef     if (sensorType == "fan_tach")
89c2bf7f99SWludzik, Jozef     {
90c2bf7f99SWludzik, Jozef         return "Rotational";
91c2bf7f99SWludzik, Jozef     }
92c2bf7f99SWludzik, Jozef     if (sensorType == "temperature")
93c2bf7f99SWludzik, Jozef     {
94c2bf7f99SWludzik, Jozef         return "Temperature";
95c2bf7f99SWludzik, Jozef     }
96c2bf7f99SWludzik, Jozef     if (sensorType == "fan_pwm" || sensorType == "utilization")
97c2bf7f99SWludzik, Jozef     {
98c2bf7f99SWludzik, Jozef         return "Percent";
99c2bf7f99SWludzik, Jozef     }
100c2bf7f99SWludzik, Jozef     if (sensorType == "altitude")
101c2bf7f99SWludzik, Jozef     {
102c2bf7f99SWludzik, Jozef         return "Altitude";
103c2bf7f99SWludzik, Jozef     }
104c2bf7f99SWludzik, Jozef     if (sensorType == "airflow")
105c2bf7f99SWludzik, Jozef     {
106c2bf7f99SWludzik, Jozef         return "AirFlow";
107c2bf7f99SWludzik, Jozef     }
108c2bf7f99SWludzik, Jozef     if (sensorType == "energy")
109c2bf7f99SWludzik, Jozef     {
110c2bf7f99SWludzik, Jozef         return "EnergyJoules";
111c2bf7f99SWludzik, Jozef     }
112c2bf7f99SWludzik, Jozef     return "";
113c2bf7f99SWludzik, Jozef }
114c2bf7f99SWludzik, Jozef 
115c2bf7f99SWludzik, Jozef inline const char* toReadingUnits(const std::string& sensorType)
116c2bf7f99SWludzik, Jozef {
117c2bf7f99SWludzik, Jozef     if (sensorType == "voltage")
118c2bf7f99SWludzik, Jozef     {
119c2bf7f99SWludzik, Jozef         return "V";
120c2bf7f99SWludzik, Jozef     }
121c2bf7f99SWludzik, Jozef     if (sensorType == "power")
122c2bf7f99SWludzik, Jozef     {
123c2bf7f99SWludzik, Jozef         return "W";
124c2bf7f99SWludzik, Jozef     }
125c2bf7f99SWludzik, Jozef     if (sensorType == "current")
126c2bf7f99SWludzik, Jozef     {
127c2bf7f99SWludzik, Jozef         return "A";
128c2bf7f99SWludzik, Jozef     }
129c2bf7f99SWludzik, Jozef     if (sensorType == "fan_tach")
130c2bf7f99SWludzik, Jozef     {
131c2bf7f99SWludzik, Jozef         return "RPM";
132c2bf7f99SWludzik, Jozef     }
133c2bf7f99SWludzik, Jozef     if (sensorType == "temperature")
134c2bf7f99SWludzik, Jozef     {
135c2bf7f99SWludzik, Jozef         return "Cel";
136c2bf7f99SWludzik, Jozef     }
137c2bf7f99SWludzik, Jozef     if (sensorType == "fan_pwm" || sensorType == "utilization")
138c2bf7f99SWludzik, Jozef     {
139c2bf7f99SWludzik, Jozef         return "%";
140c2bf7f99SWludzik, Jozef     }
141c2bf7f99SWludzik, Jozef     if (sensorType == "altitude")
142c2bf7f99SWludzik, Jozef     {
143c2bf7f99SWludzik, Jozef         return "m";
144c2bf7f99SWludzik, Jozef     }
145c2bf7f99SWludzik, Jozef     if (sensorType == "airflow")
146c2bf7f99SWludzik, Jozef     {
147c2bf7f99SWludzik, Jozef         return "cft_i/min";
148c2bf7f99SWludzik, Jozef     }
149c2bf7f99SWludzik, Jozef     if (sensorType == "energy")
150c2bf7f99SWludzik, Jozef     {
151c2bf7f99SWludzik, Jozef         return "J";
152c2bf7f99SWludzik, Jozef     }
153c2bf7f99SWludzik, Jozef     return "";
154a0ec28b6SAdrian Ambrożewicz }
155a0ec28b6SAdrian Ambrożewicz } // namespace sensors
156a0ec28b6SAdrian Ambrożewicz 
15708777fb0SLewanczyk, Dawid /**
158588c3f0dSKowalski, Kamil  * SensorsAsyncResp
15908777fb0SLewanczyk, Dawid  * Gathers data needed for response processing after async calls are done
16008777fb0SLewanczyk, Dawid  */
1611abe55efSEd Tanous class SensorsAsyncResp
1621abe55efSEd Tanous {
16308777fb0SLewanczyk, Dawid   public:
164a0ec28b6SAdrian Ambrożewicz     using DataCompleteCb = std::function<void(
165a0ec28b6SAdrian Ambrożewicz         const boost::beast::http::status status,
166a0ec28b6SAdrian Ambrożewicz         const boost::container::flat_map<std::string, std::string>& uriToDbus)>;
167a0ec28b6SAdrian Ambrożewicz 
168a0ec28b6SAdrian Ambrożewicz     struct SensorData
169a0ec28b6SAdrian Ambrożewicz     {
170a0ec28b6SAdrian Ambrożewicz         const std::string name;
171a0ec28b6SAdrian Ambrożewicz         std::string uri;
172a0ec28b6SAdrian Ambrożewicz         const std::string valueKey;
173a0ec28b6SAdrian Ambrożewicz         const std::string dbusPath;
174a0ec28b6SAdrian Ambrożewicz     };
175a0ec28b6SAdrian Ambrożewicz 
1768d1b46d7Szhanghch05     SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1778d1b46d7Szhanghch05                      const std::string& chassisIdIn,
178b5a76932SEd Tanous                      const std::vector<const char*>& typesIn,
179a0ec28b6SAdrian Ambrożewicz                      const std::string_view& subNode) :
1808d1b46d7Szhanghch05         asyncResp(asyncResp),
181*928fefb9SNan Zhou         chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
182*928fefb9SNan Zhou         efficientExpand(false)
1831214b7e7SGunnar Mills     {}
18408777fb0SLewanczyk, Dawid 
185a0ec28b6SAdrian Ambrożewicz     // Store extra data about sensor mapping and return it in callback
1868d1b46d7Szhanghch05     SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1878d1b46d7Szhanghch05                      const std::string& chassisIdIn,
188b5a76932SEd Tanous                      const std::vector<const char*>& typesIn,
189a0ec28b6SAdrian Ambrożewicz                      const std::string_view& subNode,
190a0ec28b6SAdrian Ambrożewicz                      DataCompleteCb&& creationComplete) :
1918d1b46d7Szhanghch05         asyncResp(asyncResp),
192*928fefb9SNan Zhou         chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
193*928fefb9SNan Zhou         efficientExpand(false), metadata{std::vector<SensorData>()},
194a0ec28b6SAdrian Ambrożewicz         dataComplete{std::move(creationComplete)}
195a0ec28b6SAdrian Ambrożewicz     {}
196a0ec28b6SAdrian Ambrożewicz 
197*928fefb9SNan Zhou     // sensor collections expand
198*928fefb9SNan Zhou     SensorsAsyncResp(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
199*928fefb9SNan Zhou                      const std::string& chassisIdIn,
200*928fefb9SNan Zhou                      const std::vector<const char*>& typesIn,
201*928fefb9SNan Zhou                      const std::string_view& subNode, bool efficientExpand) :
202*928fefb9SNan Zhou         asyncResp(asyncResp),
203*928fefb9SNan Zhou         chassisId(chassisIdIn), types(typesIn), chassisSubNode(subNode),
204*928fefb9SNan Zhou         efficientExpand(efficientExpand)
205*928fefb9SNan Zhou     {}
206*928fefb9SNan Zhou 
2071abe55efSEd Tanous     ~SensorsAsyncResp()
2081abe55efSEd Tanous     {
2098d1b46d7Szhanghch05         if (asyncResp->res.result() ==
2108d1b46d7Szhanghch05             boost::beast::http::status::internal_server_error)
2111abe55efSEd Tanous         {
2121abe55efSEd Tanous             // Reset the json object to clear out any data that made it in
2131abe55efSEd Tanous             // before the error happened todo(ed) handle error condition with
2141abe55efSEd Tanous             // proper code
2158d1b46d7Szhanghch05             asyncResp->res.jsonValue = nlohmann::json::object();
21608777fb0SLewanczyk, Dawid         }
217a0ec28b6SAdrian Ambrożewicz 
218a0ec28b6SAdrian Ambrożewicz         if (dataComplete && metadata)
219a0ec28b6SAdrian Ambrożewicz         {
220a0ec28b6SAdrian Ambrożewicz             boost::container::flat_map<std::string, std::string> map;
2218d1b46d7Szhanghch05             if (asyncResp->res.result() == boost::beast::http::status::ok)
222a0ec28b6SAdrian Ambrożewicz             {
223a0ec28b6SAdrian Ambrożewicz                 for (auto& sensor : *metadata)
224a0ec28b6SAdrian Ambrożewicz                 {
225a0ec28b6SAdrian Ambrożewicz                     map.insert(std::make_pair(sensor.uri + sensor.valueKey,
226a0ec28b6SAdrian Ambrożewicz                                               sensor.dbusPath));
227a0ec28b6SAdrian Ambrożewicz                 }
228a0ec28b6SAdrian Ambrożewicz             }
2298d1b46d7Szhanghch05             dataComplete(asyncResp->res.result(), map);
230a0ec28b6SAdrian Ambrożewicz         }
23108777fb0SLewanczyk, Dawid     }
232588c3f0dSKowalski, Kamil 
233ecd6a3a2SEd Tanous     SensorsAsyncResp(const SensorsAsyncResp&) = delete;
234ecd6a3a2SEd Tanous     SensorsAsyncResp(SensorsAsyncResp&&) = delete;
235ecd6a3a2SEd Tanous     SensorsAsyncResp& operator=(const SensorsAsyncResp&) = delete;
236ecd6a3a2SEd Tanous     SensorsAsyncResp& operator=(SensorsAsyncResp&&) = delete;
237ecd6a3a2SEd Tanous 
238a0ec28b6SAdrian Ambrożewicz     void addMetadata(const nlohmann::json& sensorObject,
239a0ec28b6SAdrian Ambrożewicz                      const std::string& valueKey, const std::string& dbusPath)
240a0ec28b6SAdrian Ambrożewicz     {
241a0ec28b6SAdrian Ambrożewicz         if (metadata)
242a0ec28b6SAdrian Ambrożewicz         {
243a0ec28b6SAdrian Ambrożewicz             metadata->emplace_back(SensorData{sensorObject["Name"],
244a0ec28b6SAdrian Ambrożewicz                                               sensorObject["@odata.id"],
245a0ec28b6SAdrian Ambrożewicz                                               valueKey, dbusPath});
246a0ec28b6SAdrian Ambrożewicz         }
247a0ec28b6SAdrian Ambrożewicz     }
248a0ec28b6SAdrian Ambrożewicz 
249a0ec28b6SAdrian Ambrożewicz     void updateUri(const std::string& name, const std::string& uri)
250a0ec28b6SAdrian Ambrożewicz     {
251a0ec28b6SAdrian Ambrożewicz         if (metadata)
252a0ec28b6SAdrian Ambrożewicz         {
253a0ec28b6SAdrian Ambrożewicz             for (auto& sensor : *metadata)
254a0ec28b6SAdrian Ambrożewicz             {
255a0ec28b6SAdrian Ambrożewicz                 if (sensor.name == name)
256a0ec28b6SAdrian Ambrożewicz                 {
257a0ec28b6SAdrian Ambrożewicz                     sensor.uri = uri;
258a0ec28b6SAdrian Ambrożewicz                 }
259a0ec28b6SAdrian Ambrożewicz             }
260a0ec28b6SAdrian Ambrożewicz         }
261a0ec28b6SAdrian Ambrożewicz     }
262a0ec28b6SAdrian Ambrożewicz 
2638d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp> asyncResp;
264a0ec28b6SAdrian Ambrożewicz     const std::string chassisId;
26508777fb0SLewanczyk, Dawid     const std::vector<const char*> types;
266a0ec28b6SAdrian Ambrożewicz     const std::string chassisSubNode;
267*928fefb9SNan Zhou     const bool efficientExpand;
268a0ec28b6SAdrian Ambrożewicz 
269a0ec28b6SAdrian Ambrożewicz   private:
270a0ec28b6SAdrian Ambrożewicz     std::optional<std::vector<SensorData>> metadata;
271a0ec28b6SAdrian Ambrożewicz     DataCompleteCb dataComplete;
27208777fb0SLewanczyk, Dawid };
27308777fb0SLewanczyk, Dawid 
27408777fb0SLewanczyk, Dawid /**
275d500549bSAnthony Wilson  * Possible states for physical inventory leds
276d500549bSAnthony Wilson  */
277d500549bSAnthony Wilson enum class LedState
278d500549bSAnthony Wilson {
279d500549bSAnthony Wilson     OFF,
280d500549bSAnthony Wilson     ON,
281d500549bSAnthony Wilson     BLINK,
282d500549bSAnthony Wilson     UNKNOWN
283d500549bSAnthony Wilson };
284d500549bSAnthony Wilson 
285d500549bSAnthony Wilson /**
286adc4f0dbSShawn McCarney  * D-Bus inventory item associated with one or more sensors.
287adc4f0dbSShawn McCarney  */
288adc4f0dbSShawn McCarney class InventoryItem
289adc4f0dbSShawn McCarney {
290adc4f0dbSShawn McCarney   public:
291e05aec50SEd Tanous     InventoryItem(const std::string& objPath) : objectPath(objPath)
292adc4f0dbSShawn McCarney     {
293adc4f0dbSShawn McCarney         // Set inventory item name to last node of object path
29428aa8de5SGeorge Liu         sdbusplus::message::object_path path(objectPath);
29528aa8de5SGeorge Liu         name = path.filename();
29628aa8de5SGeorge Liu         if (name.empty())
297adc4f0dbSShawn McCarney         {
29828aa8de5SGeorge Liu             BMCWEB_LOG_ERROR << "Failed to find '/' in " << objectPath;
299adc4f0dbSShawn McCarney         }
300adc4f0dbSShawn McCarney     }
301adc4f0dbSShawn McCarney 
302adc4f0dbSShawn McCarney     std::string objectPath;
303adc4f0dbSShawn McCarney     std::string name;
304e05aec50SEd Tanous     bool isPresent = true;
305e05aec50SEd Tanous     bool isFunctional = true;
306e05aec50SEd Tanous     bool isPowerSupply = false;
307e05aec50SEd Tanous     int powerSupplyEfficiencyPercent = -1;
308adc4f0dbSShawn McCarney     std::string manufacturer;
309adc4f0dbSShawn McCarney     std::string model;
310adc4f0dbSShawn McCarney     std::string partNumber;
311adc4f0dbSShawn McCarney     std::string serialNumber;
312adc4f0dbSShawn McCarney     std::set<std::string> sensors;
313d500549bSAnthony Wilson     std::string ledObjectPath;
314e05aec50SEd Tanous     LedState ledState = LedState::UNKNOWN;
315adc4f0dbSShawn McCarney };
316adc4f0dbSShawn McCarney 
317adc4f0dbSShawn McCarney /**
318413961deSRichard Marian Thomaiyar  * @brief Get objects with connection necessary for sensors
319588c3f0dSKowalski, Kamil  * @param SensorsAsyncResp Pointer to object holding response data
32008777fb0SLewanczyk, Dawid  * @param sensorNames Sensors retrieved from chassis
32108777fb0SLewanczyk, Dawid  * @param callback Callback for processing gathered connections
32208777fb0SLewanczyk, Dawid  */
32308777fb0SLewanczyk, Dawid template <typename Callback>
324413961deSRichard Marian Thomaiyar void getObjectsWithConnection(
32581ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
326b5a76932SEd Tanous     const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames,
3271abe55efSEd Tanous     Callback&& callback)
3281abe55efSEd Tanous {
329413961deSRichard Marian Thomaiyar     BMCWEB_LOG_DEBUG << "getObjectsWithConnection enter";
33003b5bae3SJames Feist     const std::string path = "/xyz/openbmc_project/sensors";
33108777fb0SLewanczyk, Dawid     const std::array<std::string, 1> interfaces = {
33208777fb0SLewanczyk, Dawid         "xyz.openbmc_project.Sensor.Value"};
33308777fb0SLewanczyk, Dawid 
33408777fb0SLewanczyk, Dawid     // Response handler for parsing objects subtree
335f94c4ecfSEd Tanous     auto respHandler = [callback{std::forward<Callback>(callback)},
336b9d36b47SEd Tanous                         sensorsAsyncResp, sensorNames](
337b9d36b47SEd Tanous                            const boost::system::error_code ec,
338b9d36b47SEd Tanous                            const dbus::utility::MapperGetSubTreeResponse&
339b9d36b47SEd Tanous                                subtree) {
340413961deSRichard Marian Thomaiyar         BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler enter";
3411abe55efSEd Tanous         if (ec)
3421abe55efSEd Tanous         {
3438d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
344413961deSRichard Marian Thomaiyar             BMCWEB_LOG_ERROR
345413961deSRichard Marian Thomaiyar                 << "getObjectsWithConnection resp_handler: Dbus error " << ec;
34608777fb0SLewanczyk, Dawid             return;
34708777fb0SLewanczyk, Dawid         }
34808777fb0SLewanczyk, Dawid 
34955c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "Found " << subtree.size() << " subtrees";
35008777fb0SLewanczyk, Dawid 
35108777fb0SLewanczyk, Dawid         // Make unique list of connections only for requested sensor types and
35208777fb0SLewanczyk, Dawid         // found in the chassis
35308777fb0SLewanczyk, Dawid         boost::container::flat_set<std::string> connections;
354413961deSRichard Marian Thomaiyar         std::set<std::pair<std::string, std::string>> objectsWithConnection;
3551abe55efSEd Tanous         // Intrinsic to avoid malloc.  Most systems will have < 8 sensor
3561abe55efSEd Tanous         // producers
35708777fb0SLewanczyk, Dawid         connections.reserve(8);
35808777fb0SLewanczyk, Dawid 
35949c53ac9SJohnathan Mantey         BMCWEB_LOG_DEBUG << "sensorNames list count: " << sensorNames->size();
36049c53ac9SJohnathan Mantey         for (const std::string& tsensor : *sensorNames)
3611abe55efSEd Tanous         {
36255c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "Sensor to find: " << tsensor;
36308777fb0SLewanczyk, Dawid         }
36408777fb0SLewanczyk, Dawid 
36508777fb0SLewanczyk, Dawid         for (const std::pair<
36608777fb0SLewanczyk, Dawid                  std::string,
36708777fb0SLewanczyk, Dawid                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
3681abe55efSEd Tanous                  object : subtree)
3691abe55efSEd Tanous         {
37049c53ac9SJohnathan Mantey             if (sensorNames->find(object.first) != sensorNames->end())
3711abe55efSEd Tanous             {
37249c53ac9SJohnathan Mantey                 for (const std::pair<std::string, std::vector<std::string>>&
3731abe55efSEd Tanous                          objData : object.second)
3741abe55efSEd Tanous                 {
37549c53ac9SJohnathan Mantey                     BMCWEB_LOG_DEBUG << "Adding connection: " << objData.first;
37608777fb0SLewanczyk, Dawid                     connections.insert(objData.first);
377de629b6eSShawn McCarney                     objectsWithConnection.insert(
378de629b6eSShawn McCarney                         std::make_pair(object.first, objData.first));
37908777fb0SLewanczyk, Dawid                 }
38008777fb0SLewanczyk, Dawid             }
38108777fb0SLewanczyk, Dawid         }
38255c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "Found " << connections.size() << " connections";
383413961deSRichard Marian Thomaiyar         callback(std::move(connections), std::move(objectsWithConnection));
384413961deSRichard Marian Thomaiyar         BMCWEB_LOG_DEBUG << "getObjectsWithConnection resp_handler exit";
38508777fb0SLewanczyk, Dawid     };
38608777fb0SLewanczyk, Dawid     // Make call to ObjectMapper to find all sensors objects
38755c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
38855c7b7a2SEd Tanous         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
3891abe55efSEd Tanous         "/xyz/openbmc_project/object_mapper",
3901abe55efSEd Tanous         "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 2, interfaces);
391413961deSRichard Marian Thomaiyar     BMCWEB_LOG_DEBUG << "getObjectsWithConnection exit";
392413961deSRichard Marian Thomaiyar }
393413961deSRichard Marian Thomaiyar 
394413961deSRichard Marian Thomaiyar /**
395413961deSRichard Marian Thomaiyar  * @brief Create connections necessary for sensors
396413961deSRichard Marian Thomaiyar  * @param SensorsAsyncResp Pointer to object holding response data
397413961deSRichard Marian Thomaiyar  * @param sensorNames Sensors retrieved from chassis
398413961deSRichard Marian Thomaiyar  * @param callback Callback for processing gathered connections
399413961deSRichard Marian Thomaiyar  */
400413961deSRichard Marian Thomaiyar template <typename Callback>
40149c53ac9SJohnathan Mantey void getConnections(
40281ce609eSEd Tanous     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
40349c53ac9SJohnathan Mantey     const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
404413961deSRichard Marian Thomaiyar     Callback&& callback)
405413961deSRichard Marian Thomaiyar {
406413961deSRichard Marian Thomaiyar     auto objectsWithConnectionCb =
407413961deSRichard Marian Thomaiyar         [callback](const boost::container::flat_set<std::string>& connections,
408413961deSRichard Marian Thomaiyar                    const std::set<std::pair<std::string, std::string>>&
4093174e4dfSEd Tanous                    /*objectsWithConnection*/) { callback(connections); };
41081ce609eSEd Tanous     getObjectsWithConnection(sensorsAsyncResp, sensorNames,
411413961deSRichard Marian Thomaiyar                              std::move(objectsWithConnectionCb));
41208777fb0SLewanczyk, Dawid }
41308777fb0SLewanczyk, Dawid 
41408777fb0SLewanczyk, Dawid /**
41549c53ac9SJohnathan Mantey  * @brief Shrinks the list of sensors for processing
41649c53ac9SJohnathan Mantey  * @param SensorsAysncResp  The class holding the Redfish response
41749c53ac9SJohnathan Mantey  * @param allSensors  A list of all the sensors associated to the
41849c53ac9SJohnathan Mantey  * chassis element (i.e. baseboard, front panel, etc...)
41949c53ac9SJohnathan Mantey  * @param activeSensors A list that is a reduction of the incoming
42049c53ac9SJohnathan Mantey  * allSensors list.  Eliminate Thermal sensors when a Power request is
42149c53ac9SJohnathan Mantey  * made, and eliminate Power sensors when a Thermal request is made.
42249c53ac9SJohnathan Mantey  */
42323a21a1cSEd Tanous inline void reduceSensorList(
42481ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
42549c53ac9SJohnathan Mantey     const std::vector<std::string>* allSensors,
426b5a76932SEd Tanous     const std::shared_ptr<boost::container::flat_set<std::string>>&
427b5a76932SEd Tanous         activeSensors)
42849c53ac9SJohnathan Mantey {
42981ce609eSEd Tanous     if (sensorsAsyncResp == nullptr)
43049c53ac9SJohnathan Mantey     {
43149c53ac9SJohnathan Mantey         return;
43249c53ac9SJohnathan Mantey     }
43349c53ac9SJohnathan Mantey     if ((allSensors == nullptr) || (activeSensors == nullptr))
43449c53ac9SJohnathan Mantey     {
43549c53ac9SJohnathan Mantey         messages::resourceNotFound(
4368d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res, sensorsAsyncResp->chassisSubNode,
43781ce609eSEd Tanous             sensorsAsyncResp->chassisSubNode == sensors::node::thermal
438a0ec28b6SAdrian Ambrożewicz                 ? "Temperatures"
43949c53ac9SJohnathan Mantey                 : "Voltages");
44049c53ac9SJohnathan Mantey 
44149c53ac9SJohnathan Mantey         return;
44249c53ac9SJohnathan Mantey     }
44349c53ac9SJohnathan Mantey     if (allSensors->empty())
44449c53ac9SJohnathan Mantey     {
44549c53ac9SJohnathan Mantey         // Nothing to do, the activeSensors object is also empty
44649c53ac9SJohnathan Mantey         return;
44749c53ac9SJohnathan Mantey     }
44849c53ac9SJohnathan Mantey 
44981ce609eSEd Tanous     for (const char* type : sensorsAsyncResp->types)
45049c53ac9SJohnathan Mantey     {
45149c53ac9SJohnathan Mantey         for (const std::string& sensor : *allSensors)
45249c53ac9SJohnathan Mantey         {
45349c53ac9SJohnathan Mantey             if (boost::starts_with(sensor, type))
45449c53ac9SJohnathan Mantey             {
45549c53ac9SJohnathan Mantey                 activeSensors->emplace(sensor);
45649c53ac9SJohnathan Mantey             }
45749c53ac9SJohnathan Mantey         }
45849c53ac9SJohnathan Mantey     }
45949c53ac9SJohnathan Mantey }
46049c53ac9SJohnathan Mantey 
46149c53ac9SJohnathan Mantey /**
4624bb3dc34SCarol Wang  * @brief Retrieves valid chassis path
4634bb3dc34SCarol Wang  * @param asyncResp   Pointer to object holding response data
4644bb3dc34SCarol Wang  * @param callback  Callback for next step to get valid chassis path
4654bb3dc34SCarol Wang  */
4664bb3dc34SCarol Wang template <typename Callback>
467b5a76932SEd Tanous void getValidChassisPath(const std::shared_ptr<SensorsAsyncResp>& asyncResp,
4684bb3dc34SCarol Wang                          Callback&& callback)
4694bb3dc34SCarol Wang {
4704bb3dc34SCarol Wang     BMCWEB_LOG_DEBUG << "checkChassisId enter";
4714bb3dc34SCarol Wang     const std::array<const char*, 2> interfaces = {
4724bb3dc34SCarol Wang         "xyz.openbmc_project.Inventory.Item.Board",
4734bb3dc34SCarol Wang         "xyz.openbmc_project.Inventory.Item.Chassis"};
4744bb3dc34SCarol Wang 
475b9d36b47SEd Tanous     auto respHandler = [callback{std::forward<Callback>(callback)}, asyncResp](
476b9d36b47SEd Tanous                            const boost::system::error_code ec,
477b9d36b47SEd Tanous                            const dbus::utility::MapperGetSubTreePathsResponse&
478b9d36b47SEd Tanous                                chassisPaths) mutable {
4794bb3dc34SCarol Wang         BMCWEB_LOG_DEBUG << "getValidChassisPath respHandler enter";
4804bb3dc34SCarol Wang         if (ec)
4814bb3dc34SCarol Wang         {
482b9d36b47SEd Tanous             BMCWEB_LOG_ERROR << "getValidChassisPath respHandler DBUS error: "
483b9d36b47SEd Tanous                              << ec;
4848d1b46d7Szhanghch05             messages::internalError(asyncResp->asyncResp->res);
4854bb3dc34SCarol Wang             return;
4864bb3dc34SCarol Wang         }
4874bb3dc34SCarol Wang 
4884bb3dc34SCarol Wang         std::optional<std::string> chassisPath;
4894bb3dc34SCarol Wang         std::string chassisName;
4904bb3dc34SCarol Wang         for (const std::string& chassis : chassisPaths)
4914bb3dc34SCarol Wang         {
49228aa8de5SGeorge Liu             sdbusplus::message::object_path path(chassis);
49328aa8de5SGeorge Liu             chassisName = path.filename();
49428aa8de5SGeorge Liu             if (chassisName.empty())
4954bb3dc34SCarol Wang             {
4964bb3dc34SCarol Wang                 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
4974bb3dc34SCarol Wang                 continue;
4984bb3dc34SCarol Wang             }
4994bb3dc34SCarol Wang             if (chassisName == asyncResp->chassisId)
5004bb3dc34SCarol Wang             {
5014bb3dc34SCarol Wang                 chassisPath = chassis;
5024bb3dc34SCarol Wang                 break;
5034bb3dc34SCarol Wang             }
5044bb3dc34SCarol Wang         }
5054bb3dc34SCarol Wang         callback(chassisPath);
5064bb3dc34SCarol Wang     };
5074bb3dc34SCarol Wang 
5084bb3dc34SCarol Wang     // Get the Chassis Collection
5094bb3dc34SCarol Wang     crow::connections::systemBus->async_method_call(
5104bb3dc34SCarol Wang         respHandler, "xyz.openbmc_project.ObjectMapper",
5114bb3dc34SCarol Wang         "/xyz/openbmc_project/object_mapper",
5124bb3dc34SCarol Wang         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
5134bb3dc34SCarol Wang         "/xyz/openbmc_project/inventory", 0, interfaces);
5144bb3dc34SCarol Wang     BMCWEB_LOG_DEBUG << "checkChassisId exit";
5154bb3dc34SCarol Wang }
5164bb3dc34SCarol Wang 
5174bb3dc34SCarol Wang /**
51808777fb0SLewanczyk, Dawid  * @brief Retrieves requested chassis sensors and redundancy data from DBus .
519588c3f0dSKowalski, Kamil  * @param SensorsAsyncResp   Pointer to object holding response data
52008777fb0SLewanczyk, Dawid  * @param callback  Callback for next step in gathered sensor processing
52108777fb0SLewanczyk, Dawid  */
52208777fb0SLewanczyk, Dawid template <typename Callback>
523b5a76932SEd Tanous void getChassis(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
5241abe55efSEd Tanous                 Callback&& callback)
5251abe55efSEd Tanous {
52655c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "getChassis enter";
527adc4f0dbSShawn McCarney     const std::array<const char*, 2> interfaces = {
52849c53ac9SJohnathan Mantey         "xyz.openbmc_project.Inventory.Item.Board",
529adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Item.Chassis"};
530f94c4ecfSEd Tanous     auto respHandler = [callback{std::forward<Callback>(callback)},
531f94c4ecfSEd Tanous                         sensorsAsyncResp](
53249c53ac9SJohnathan Mantey                            const boost::system::error_code ec,
533b9d36b47SEd Tanous                            const dbus::utility::MapperGetSubTreePathsResponse&
534b9d36b47SEd Tanous                                chassisPaths) {
53555c7b7a2SEd Tanous         BMCWEB_LOG_DEBUG << "getChassis respHandler enter";
5361abe55efSEd Tanous         if (ec)
5371abe55efSEd Tanous         {
53855c7b7a2SEd Tanous             BMCWEB_LOG_ERROR << "getChassis respHandler DBUS error: " << ec;
5398d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
54008777fb0SLewanczyk, Dawid             return;
54108777fb0SLewanczyk, Dawid         }
54208777fb0SLewanczyk, Dawid 
54349c53ac9SJohnathan Mantey         const std::string* chassisPath = nullptr;
54449c53ac9SJohnathan Mantey         std::string chassisName;
54549c53ac9SJohnathan Mantey         for (const std::string& chassis : chassisPaths)
5461abe55efSEd Tanous         {
54728aa8de5SGeorge Liu             sdbusplus::message::object_path path(chassis);
54828aa8de5SGeorge Liu             chassisName = path.filename();
54928aa8de5SGeorge Liu             if (chassisName.empty())
5501abe55efSEd Tanous             {
55149c53ac9SJohnathan Mantey                 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
552daf36e2eSEd Tanous                 continue;
553daf36e2eSEd Tanous             }
55449c53ac9SJohnathan Mantey             if (chassisName == sensorsAsyncResp->chassisId)
5551abe55efSEd Tanous             {
55649c53ac9SJohnathan Mantey                 chassisPath = &chassis;
55749c53ac9SJohnathan Mantey                 break;
558daf36e2eSEd Tanous             }
55949c53ac9SJohnathan Mantey         }
56049c53ac9SJohnathan Mantey         if (chassisPath == nullptr)
5611abe55efSEd Tanous         {
5628d1b46d7Szhanghch05             messages::resourceNotFound(sensorsAsyncResp->asyncResp->res,
5638d1b46d7Szhanghch05                                        "Chassis", sensorsAsyncResp->chassisId);
56449c53ac9SJohnathan Mantey             return;
5651abe55efSEd Tanous         }
56608777fb0SLewanczyk, Dawid 
56749c53ac9SJohnathan Mantey         const std::string& chassisSubNode = sensorsAsyncResp->chassisSubNode;
568a0ec28b6SAdrian Ambrożewicz         if (chassisSubNode == sensors::node::power)
56949c53ac9SJohnathan Mantey         {
5708d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] =
57149c53ac9SJohnathan Mantey                 "#Power.v1_5_2.Power";
57249c53ac9SJohnathan Mantey         }
573a0ec28b6SAdrian Ambrożewicz         else if (chassisSubNode == sensors::node::thermal)
57449c53ac9SJohnathan Mantey         {
5758d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] =
57649c53ac9SJohnathan Mantey                 "#Thermal.v1_4_0.Thermal";
5778d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["Fans"] =
5788d1b46d7Szhanghch05                 nlohmann::json::array();
5798d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["Temperatures"] =
5804f9a2130SJennifer Lee                 nlohmann::json::array();
58149c53ac9SJohnathan Mantey         }
582a0ec28b6SAdrian Ambrożewicz         else if (chassisSubNode == sensors::node::sensors)
58395a3ecadSAnthony Wilson         {
5848d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["@odata.type"] =
58595a3ecadSAnthony Wilson                 "#SensorCollection.SensorCollection";
5868d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["Description"] =
58795a3ecadSAnthony Wilson                 "Collection of Sensors for this Chassis";
5888d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["Members"] =
58995a3ecadSAnthony Wilson                 nlohmann::json::array();
5908d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["Members@odata.count"] =
5918d1b46d7Szhanghch05                 0;
59295a3ecadSAnthony Wilson         }
59395a3ecadSAnthony Wilson 
594a0ec28b6SAdrian Ambrożewicz         if (chassisSubNode != sensors::node::sensors)
59595a3ecadSAnthony Wilson         {
5968d1b46d7Szhanghch05             sensorsAsyncResp->asyncResp->res.jsonValue["Id"] = chassisSubNode;
59795a3ecadSAnthony Wilson         }
59895a3ecadSAnthony Wilson 
5998d1b46d7Szhanghch05         sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
60049c53ac9SJohnathan Mantey             "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId + "/" +
60149c53ac9SJohnathan Mantey             chassisSubNode;
6028d1b46d7Szhanghch05         sensorsAsyncResp->asyncResp->res.jsonValue["Name"] = chassisSubNode;
6038fb49dd6SShawn McCarney         // Get the list of all sensors for this Chassis element
6048fb49dd6SShawn McCarney         std::string sensorPath = *chassisPath + "/all_sensors";
6051e1e598dSJonathan Doman         sdbusplus::asio::getProperty<std::vector<std::string>>(
6061e1e598dSJonathan Doman             *crow::connections::systemBus, "xyz.openbmc_project.ObjectMapper",
6071e1e598dSJonathan Doman             sensorPath, "xyz.openbmc_project.Association", "endpoints",
608f94c4ecfSEd Tanous             [sensorsAsyncResp,
609f94c4ecfSEd Tanous              callback{std::forward<const Callback>(callback)}](
610271584abSEd Tanous                 const boost::system::error_code& e,
6111e1e598dSJonathan Doman                 const std::vector<std::string>& nodeSensorList) {
612271584abSEd Tanous                 if (e)
61349c53ac9SJohnathan Mantey                 {
614271584abSEd Tanous                     if (e.value() != EBADR)
61549c53ac9SJohnathan Mantey                     {
6168d1b46d7Szhanghch05                         messages::internalError(
6178d1b46d7Szhanghch05                             sensorsAsyncResp->asyncResp->res);
61849c53ac9SJohnathan Mantey                         return;
61949c53ac9SJohnathan Mantey                     }
62049c53ac9SJohnathan Mantey                 }
62149c53ac9SJohnathan Mantey                 const std::shared_ptr<boost::container::flat_set<std::string>>
62249c53ac9SJohnathan Mantey                     culledSensorList = std::make_shared<
62349c53ac9SJohnathan Mantey                         boost::container::flat_set<std::string>>();
6241e1e598dSJonathan Doman                 reduceSensorList(sensorsAsyncResp, &nodeSensorList,
62549c53ac9SJohnathan Mantey                                  culledSensorList);
62649c53ac9SJohnathan Mantey                 callback(culledSensorList);
6271e1e598dSJonathan Doman             });
62849c53ac9SJohnathan Mantey     };
62949c53ac9SJohnathan Mantey 
63049c53ac9SJohnathan Mantey     // Get the Chassis Collection
63149c53ac9SJohnathan Mantey     crow::connections::systemBus->async_method_call(
63249c53ac9SJohnathan Mantey         respHandler, "xyz.openbmc_project.ObjectMapper",
63349c53ac9SJohnathan Mantey         "/xyz/openbmc_project/object_mapper",
63449c53ac9SJohnathan Mantey         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
635271584abSEd Tanous         "/xyz/openbmc_project/inventory", 0, interfaces);
63655c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "getChassis exit";
63708777fb0SLewanczyk, Dawid }
63808777fb0SLewanczyk, Dawid 
63908777fb0SLewanczyk, Dawid /**
640de629b6eSShawn McCarney  * @brief Finds all DBus object paths that implement ObjectManager.
641de629b6eSShawn McCarney  *
642de629b6eSShawn McCarney  * Creates a mapping from the associated connection name to the object path.
643de629b6eSShawn McCarney  *
644de629b6eSShawn McCarney  * Finds the object paths asynchronously.  Invokes callback when information has
645de629b6eSShawn McCarney  * been obtained.
646de629b6eSShawn McCarney  *
647de629b6eSShawn McCarney  * The callback must have the following signature:
648de629b6eSShawn McCarney  *   @code
6498fb49dd6SShawn McCarney  *   callback(std::shared_ptr<boost::container::flat_map<std::string,
6508fb49dd6SShawn McCarney  *                std::string>> objectMgrPaths)
651de629b6eSShawn McCarney  *   @endcode
652de629b6eSShawn McCarney  *
65349c53ac9SJohnathan Mantey  * @param sensorsAsyncResp Pointer to object holding response data.
654de629b6eSShawn McCarney  * @param callback Callback to invoke when object paths obtained.
655de629b6eSShawn McCarney  */
656de629b6eSShawn McCarney template <typename Callback>
657b5a76932SEd Tanous void getObjectManagerPaths(
65881ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
659de629b6eSShawn McCarney     Callback&& callback)
660de629b6eSShawn McCarney {
661de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getObjectManagerPaths enter";
662de629b6eSShawn McCarney     const std::array<std::string, 1> interfaces = {
663de629b6eSShawn McCarney         "org.freedesktop.DBus.ObjectManager"};
664de629b6eSShawn McCarney 
665de629b6eSShawn McCarney     // Response handler for GetSubTree DBus method
666f94c4ecfSEd Tanous     auto respHandler = [callback{std::forward<Callback>(callback)},
667b9d36b47SEd Tanous                         sensorsAsyncResp](
668b9d36b47SEd Tanous                            const boost::system::error_code ec,
669b9d36b47SEd Tanous                            const dbus::utility::MapperGetSubTreeResponse&
670b9d36b47SEd Tanous                                subtree) {
671de629b6eSShawn McCarney         BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler enter";
672de629b6eSShawn McCarney         if (ec)
673de629b6eSShawn McCarney         {
6748d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
675de629b6eSShawn McCarney             BMCWEB_LOG_ERROR << "getObjectManagerPaths respHandler: DBus error "
676de629b6eSShawn McCarney                              << ec;
677de629b6eSShawn McCarney             return;
678de629b6eSShawn McCarney         }
679de629b6eSShawn McCarney 
680de629b6eSShawn McCarney         // Loop over returned object paths
6818fb49dd6SShawn McCarney         std::shared_ptr<boost::container::flat_map<std::string, std::string>>
6828fb49dd6SShawn McCarney             objectMgrPaths = std::make_shared<
6838fb49dd6SShawn McCarney                 boost::container::flat_map<std::string, std::string>>();
684de629b6eSShawn McCarney         for (const std::pair<
685de629b6eSShawn McCarney                  std::string,
686de629b6eSShawn McCarney                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
687de629b6eSShawn McCarney                  object : subtree)
688de629b6eSShawn McCarney         {
689de629b6eSShawn McCarney             // Loop over connections for current object path
690de629b6eSShawn McCarney             const std::string& objectPath = object.first;
691de629b6eSShawn McCarney             for (const std::pair<std::string, std::vector<std::string>>&
692de629b6eSShawn McCarney                      objData : object.second)
693de629b6eSShawn McCarney             {
694de629b6eSShawn McCarney                 // Add mapping from connection to object path
695de629b6eSShawn McCarney                 const std::string& connection = objData.first;
6968fb49dd6SShawn McCarney                 (*objectMgrPaths)[connection] = objectPath;
697de629b6eSShawn McCarney                 BMCWEB_LOG_DEBUG << "Added mapping " << connection << " -> "
698de629b6eSShawn McCarney                                  << objectPath;
699de629b6eSShawn McCarney             }
700de629b6eSShawn McCarney         }
7018fb49dd6SShawn McCarney         callback(objectMgrPaths);
702de629b6eSShawn McCarney         BMCWEB_LOG_DEBUG << "getObjectManagerPaths respHandler exit";
703de629b6eSShawn McCarney     };
704de629b6eSShawn McCarney 
705de629b6eSShawn McCarney     // Query mapper for all DBus object paths that implement ObjectManager
706de629b6eSShawn McCarney     crow::connections::systemBus->async_method_call(
707de629b6eSShawn McCarney         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
708de629b6eSShawn McCarney         "/xyz/openbmc_project/object_mapper",
709271584abSEd Tanous         "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0, interfaces);
710de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getObjectManagerPaths exit";
711de629b6eSShawn McCarney }
712de629b6eSShawn McCarney 
713de629b6eSShawn McCarney /**
714adc4f0dbSShawn McCarney  * @brief Returns the Redfish State value for the specified inventory item.
715adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with a sensor.
716adc4f0dbSShawn McCarney  * @return State value for inventory item.
71734dd179eSJames Feist  */
71823a21a1cSEd Tanous inline std::string getState(const InventoryItem* inventoryItem)
719adc4f0dbSShawn McCarney {
720adc4f0dbSShawn McCarney     if ((inventoryItem != nullptr) && !(inventoryItem->isPresent))
721adc4f0dbSShawn McCarney     {
722adc4f0dbSShawn McCarney         return "Absent";
723adc4f0dbSShawn McCarney     }
72434dd179eSJames Feist 
725adc4f0dbSShawn McCarney     return "Enabled";
726adc4f0dbSShawn McCarney }
727adc4f0dbSShawn McCarney 
728adc4f0dbSShawn McCarney /**
729adc4f0dbSShawn McCarney  * @brief Returns the Redfish Health value for the specified sensor.
730adc4f0dbSShawn McCarney  * @param sensorJson Sensor JSON object.
731adc4f0dbSShawn McCarney  * @param interfacesDict Map of all sensor interfaces.
732adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with the sensor.  Will
733adc4f0dbSShawn McCarney  * be nullptr if no associated inventory item was found.
734adc4f0dbSShawn McCarney  * @return Health value for sensor.
735adc4f0dbSShawn McCarney  */
736711ac7a9SEd Tanous inline std::string
737711ac7a9SEd Tanous     getHealth(nlohmann::json& sensorJson,
738711ac7a9SEd Tanous               const dbus::utility::DBusInteracesMap& interfacesDict,
739adc4f0dbSShawn McCarney               const InventoryItem* inventoryItem)
74034dd179eSJames Feist {
741adc4f0dbSShawn McCarney     // Get current health value (if any) in the sensor JSON object.  Some JSON
742adc4f0dbSShawn McCarney     // objects contain multiple sensors (such as PowerSupplies).  We want to set
743adc4f0dbSShawn McCarney     // the overall health to be the most severe of any of the sensors.
744adc4f0dbSShawn McCarney     std::string currentHealth;
745adc4f0dbSShawn McCarney     auto statusIt = sensorJson.find("Status");
746adc4f0dbSShawn McCarney     if (statusIt != sensorJson.end())
747adc4f0dbSShawn McCarney     {
748adc4f0dbSShawn McCarney         auto healthIt = statusIt->find("Health");
749adc4f0dbSShawn McCarney         if (healthIt != statusIt->end())
750adc4f0dbSShawn McCarney         {
751adc4f0dbSShawn McCarney             std::string* health = healthIt->get_ptr<std::string*>();
752adc4f0dbSShawn McCarney             if (health != nullptr)
753adc4f0dbSShawn McCarney             {
754adc4f0dbSShawn McCarney                 currentHealth = *health;
755adc4f0dbSShawn McCarney             }
756adc4f0dbSShawn McCarney         }
757adc4f0dbSShawn McCarney     }
758adc4f0dbSShawn McCarney 
759adc4f0dbSShawn McCarney     // If current health in JSON object is already Critical, return that.  This
760adc4f0dbSShawn McCarney     // should override the sensor health, which might be less severe.
761adc4f0dbSShawn McCarney     if (currentHealth == "Critical")
762adc4f0dbSShawn McCarney     {
763adc4f0dbSShawn McCarney         return "Critical";
764adc4f0dbSShawn McCarney     }
765adc4f0dbSShawn McCarney 
766adc4f0dbSShawn McCarney     // Check if sensor has critical threshold alarm
767711ac7a9SEd Tanous 
7689eb808c1SEd Tanous     for (const auto& [interface, values] : interfacesDict)
76934dd179eSJames Feist     {
770711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Sensor.Threshold.Critical")
77134dd179eSJames Feist         {
7729eb808c1SEd Tanous             for (const auto& [valueName, value] : values)
773711ac7a9SEd Tanous             {
774711ac7a9SEd Tanous                 if (valueName == "CriticalAlarmHigh" ||
775711ac7a9SEd Tanous                     valueName == "CriticalAlarmLow")
776711ac7a9SEd Tanous                 {
777711ac7a9SEd Tanous                     const bool* asserted = std::get_if<bool>(&value);
77834dd179eSJames Feist                     if (asserted == nullptr)
77934dd179eSJames Feist                     {
78034dd179eSJames Feist                         BMCWEB_LOG_ERROR << "Illegal sensor threshold";
78134dd179eSJames Feist                     }
78234dd179eSJames Feist                     else if (*asserted)
78334dd179eSJames Feist                     {
78434dd179eSJames Feist                         return "Critical";
78534dd179eSJames Feist                     }
78634dd179eSJames Feist                 }
78734dd179eSJames Feist             }
78834dd179eSJames Feist         }
78934dd179eSJames Feist     }
79034dd179eSJames Feist 
791adc4f0dbSShawn McCarney     // Check if associated inventory item is not functional
792adc4f0dbSShawn McCarney     if ((inventoryItem != nullptr) && !(inventoryItem->isFunctional))
793adc4f0dbSShawn McCarney     {
794adc4f0dbSShawn McCarney         return "Critical";
795adc4f0dbSShawn McCarney     }
796adc4f0dbSShawn McCarney 
797adc4f0dbSShawn McCarney     // If current health in JSON object is already Warning, return that.  This
798adc4f0dbSShawn McCarney     // should override the sensor status, which might be less severe.
799adc4f0dbSShawn McCarney     if (currentHealth == "Warning")
800adc4f0dbSShawn McCarney     {
801adc4f0dbSShawn McCarney         return "Warning";
802adc4f0dbSShawn McCarney     }
803adc4f0dbSShawn McCarney 
804adc4f0dbSShawn McCarney     // Check if sensor has warning threshold alarm
8059eb808c1SEd Tanous     for (const auto& [interface, values] : interfacesDict)
80634dd179eSJames Feist     {
807711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Sensor.Threshold.Warning")
80834dd179eSJames Feist         {
8099eb808c1SEd Tanous             for (const auto& [valueName, value] : values)
810711ac7a9SEd Tanous             {
811711ac7a9SEd Tanous                 if (valueName == "WarningAlarmHigh" ||
812711ac7a9SEd Tanous                     valueName == "WarningAlarmLow")
813711ac7a9SEd Tanous                 {
814711ac7a9SEd Tanous                     const bool* asserted = std::get_if<bool>(&value);
81534dd179eSJames Feist                     if (asserted == nullptr)
81634dd179eSJames Feist                     {
81734dd179eSJames Feist                         BMCWEB_LOG_ERROR << "Illegal sensor threshold";
81834dd179eSJames Feist                     }
81934dd179eSJames Feist                     else if (*asserted)
82034dd179eSJames Feist                     {
821ebe4d91eSEd Tanous                         return "Warning";
82234dd179eSJames Feist                     }
82334dd179eSJames Feist                 }
82434dd179eSJames Feist             }
82534dd179eSJames Feist         }
82634dd179eSJames Feist     }
827adc4f0dbSShawn McCarney 
82834dd179eSJames Feist     return "OK";
82934dd179eSJames Feist }
83034dd179eSJames Feist 
83123a21a1cSEd Tanous inline void setLedState(nlohmann::json& sensorJson,
832d500549bSAnthony Wilson                         const InventoryItem* inventoryItem)
833d500549bSAnthony Wilson {
834d500549bSAnthony Wilson     if (inventoryItem != nullptr && !inventoryItem->ledObjectPath.empty())
835d500549bSAnthony Wilson     {
836d500549bSAnthony Wilson         switch (inventoryItem->ledState)
837d500549bSAnthony Wilson         {
838d500549bSAnthony Wilson             case LedState::OFF:
839d500549bSAnthony Wilson                 sensorJson["IndicatorLED"] = "Off";
840d500549bSAnthony Wilson                 break;
841d500549bSAnthony Wilson             case LedState::ON:
842d500549bSAnthony Wilson                 sensorJson["IndicatorLED"] = "Lit";
843d500549bSAnthony Wilson                 break;
844d500549bSAnthony Wilson             case LedState::BLINK:
845d500549bSAnthony Wilson                 sensorJson["IndicatorLED"] = "Blinking";
846d500549bSAnthony Wilson                 break;
84723a21a1cSEd Tanous             case LedState::UNKNOWN:
848d500549bSAnthony Wilson                 break;
849d500549bSAnthony Wilson         }
850d500549bSAnthony Wilson     }
851d500549bSAnthony Wilson }
852d500549bSAnthony Wilson 
85334dd179eSJames Feist /**
85408777fb0SLewanczyk, Dawid  * @brief Builds a json sensor representation of a sensor.
85508777fb0SLewanczyk, Dawid  * @param sensorName  The name of the sensor to be built
856274fad5aSGunnar Mills  * @param sensorType  The type (temperature, fan_tach, etc) of the sensor to
85708777fb0SLewanczyk, Dawid  * build
858a0ec28b6SAdrian Ambrożewicz  * @param sensorsAsyncResp  Sensor metadata
85908777fb0SLewanczyk, Dawid  * @param interfacesDict  A dictionary of the interfaces and properties of said
86008777fb0SLewanczyk, Dawid  * interfaces to be built from
86108777fb0SLewanczyk, Dawid  * @param sensor_json  The json object to fill
862adc4f0dbSShawn McCarney  * @param inventoryItem D-Bus inventory item associated with the sensor.  Will
863adc4f0dbSShawn McCarney  * be nullptr if no associated inventory item was found.
86408777fb0SLewanczyk, Dawid  */
86523a21a1cSEd Tanous inline void objectInterfacesToJson(
86608777fb0SLewanczyk, Dawid     const std::string& sensorName, const std::string& sensorType,
867b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
868711ac7a9SEd Tanous     const dbus::utility::DBusInteracesMap& interfacesDict,
86981ce609eSEd Tanous     nlohmann::json& sensorJson, InventoryItem* inventoryItem)
8701abe55efSEd Tanous {
87108777fb0SLewanczyk, Dawid     // Assume values exist as is (10^0 == 1) if no scale exists
87208777fb0SLewanczyk, Dawid     int64_t scaleMultiplier = 0;
8739eb808c1SEd Tanous     for (const auto& [interface, values] : interfacesDict)
8741abe55efSEd Tanous     {
875711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Sensor.Value")
876711ac7a9SEd Tanous         {
8779eb808c1SEd Tanous             for (const auto& [valueName, value] : values)
878711ac7a9SEd Tanous             {
879711ac7a9SEd Tanous                 if (valueName == "Scale")
880711ac7a9SEd Tanous                 {
881711ac7a9SEd Tanous                     const int64_t* int64Value = std::get_if<int64_t>(&value);
8821abe55efSEd Tanous                     if (int64Value != nullptr)
8831abe55efSEd Tanous                     {
88408777fb0SLewanczyk, Dawid                         scaleMultiplier = *int64Value;
88508777fb0SLewanczyk, Dawid                     }
88608777fb0SLewanczyk, Dawid                 }
887711ac7a9SEd Tanous             }
888711ac7a9SEd Tanous         }
889711ac7a9SEd Tanous     }
89008777fb0SLewanczyk, Dawid 
891a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
892adc4f0dbSShawn McCarney     {
89395a3ecadSAnthony Wilson         // For sensors in SensorCollection we set Id instead of MemberId,
89495a3ecadSAnthony Wilson         // including power sensors.
89581ce609eSEd Tanous         sensorJson["Id"] = sensorName;
89681ce609eSEd Tanous         sensorJson["Name"] = boost::replace_all_copy(sensorName, "_", " ");
89795a3ecadSAnthony Wilson     }
89895a3ecadSAnthony Wilson     else if (sensorType != "power")
89995a3ecadSAnthony Wilson     {
90095a3ecadSAnthony Wilson         // Set MemberId and Name for non-power sensors.  For PowerSupplies and
90195a3ecadSAnthony Wilson         // PowerControl, those properties have more general values because
90295a3ecadSAnthony Wilson         // multiple sensors can be stored in the same JSON object.
90381ce609eSEd Tanous         sensorJson["MemberId"] = sensorName;
90481ce609eSEd Tanous         sensorJson["Name"] = boost::replace_all_copy(sensorName, "_", " ");
905adc4f0dbSShawn McCarney     }
906e742b6ccSEd Tanous 
90781ce609eSEd Tanous     sensorJson["Status"]["State"] = getState(inventoryItem);
90881ce609eSEd Tanous     sensorJson["Status"]["Health"] =
90981ce609eSEd Tanous         getHealth(sensorJson, interfacesDict, inventoryItem);
91008777fb0SLewanczyk, Dawid 
91108777fb0SLewanczyk, Dawid     // Parameter to set to override the type we get from dbus, and force it to
91208777fb0SLewanczyk, Dawid     // int, regardless of what is available.  This is used for schemas like fan,
91308777fb0SLewanczyk, Dawid     // that require integers, not floats.
91408777fb0SLewanczyk, Dawid     bool forceToInt = false;
91508777fb0SLewanczyk, Dawid 
9163929aca1SAnthony Wilson     nlohmann::json::json_pointer unit("/Reading");
917a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
91895a3ecadSAnthony Wilson     {
91981ce609eSEd Tanous         sensorJson["@odata.type"] = "#Sensor.v1_0_0.Sensor";
920c2bf7f99SWludzik, Jozef 
921c2bf7f99SWludzik, Jozef         const std::string& readingType = sensors::toReadingType(sensorType);
922c2bf7f99SWludzik, Jozef         if (readingType.empty())
92395a3ecadSAnthony Wilson         {
924c2bf7f99SWludzik, Jozef             BMCWEB_LOG_ERROR << "Redfish cannot map reading type for "
925c2bf7f99SWludzik, Jozef                              << sensorType;
92695a3ecadSAnthony Wilson         }
927c2bf7f99SWludzik, Jozef         else
92895a3ecadSAnthony Wilson         {
929c2bf7f99SWludzik, Jozef             sensorJson["ReadingType"] = readingType;
93095a3ecadSAnthony Wilson         }
931c2bf7f99SWludzik, Jozef 
932c2bf7f99SWludzik, Jozef         const std::string& readingUnits = sensors::toReadingUnits(sensorType);
933c2bf7f99SWludzik, Jozef         if (readingUnits.empty())
934f8ede15eSAdrian Ambrożewicz         {
935c2bf7f99SWludzik, Jozef             BMCWEB_LOG_ERROR << "Redfish cannot map reading unit for "
936c2bf7f99SWludzik, Jozef                              << sensorType;
937c2bf7f99SWludzik, Jozef         }
938c2bf7f99SWludzik, Jozef         else
939c2bf7f99SWludzik, Jozef         {
940c2bf7f99SWludzik, Jozef             sensorJson["ReadingUnits"] = readingUnits;
941f8ede15eSAdrian Ambrożewicz         }
94295a3ecadSAnthony Wilson     }
94395a3ecadSAnthony Wilson     else if (sensorType == "temperature")
9441abe55efSEd Tanous     {
9453929aca1SAnthony Wilson         unit = "/ReadingCelsius"_json_pointer;
94681ce609eSEd Tanous         sensorJson["@odata.type"] = "#Thermal.v1_3_0.Temperature";
94708777fb0SLewanczyk, Dawid         // TODO(ed) Documentation says that path should be type fan_tach,
94808777fb0SLewanczyk, Dawid         // implementation seems to implement fan
9491abe55efSEd Tanous     }
9501abe55efSEd Tanous     else if (sensorType == "fan" || sensorType == "fan_tach")
9511abe55efSEd Tanous     {
9523929aca1SAnthony Wilson         unit = "/Reading"_json_pointer;
95381ce609eSEd Tanous         sensorJson["ReadingUnits"] = "RPM";
95481ce609eSEd Tanous         sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
95581ce609eSEd Tanous         setLedState(sensorJson, inventoryItem);
95608777fb0SLewanczyk, Dawid         forceToInt = true;
9571abe55efSEd Tanous     }
9586f6d0d32SEd Tanous     else if (sensorType == "fan_pwm")
9596f6d0d32SEd Tanous     {
9603929aca1SAnthony Wilson         unit = "/Reading"_json_pointer;
96181ce609eSEd Tanous         sensorJson["ReadingUnits"] = "Percent";
96281ce609eSEd Tanous         sensorJson["@odata.type"] = "#Thermal.v1_3_0.Fan";
96381ce609eSEd Tanous         setLedState(sensorJson, inventoryItem);
9646f6d0d32SEd Tanous         forceToInt = true;
9656f6d0d32SEd Tanous     }
9661abe55efSEd Tanous     else if (sensorType == "voltage")
9671abe55efSEd Tanous     {
9683929aca1SAnthony Wilson         unit = "/ReadingVolts"_json_pointer;
96981ce609eSEd Tanous         sensorJson["@odata.type"] = "#Power.v1_0_0.Voltage";
9701abe55efSEd Tanous     }
9712474adfaSEd Tanous     else if (sensorType == "power")
9722474adfaSEd Tanous     {
97349c53ac9SJohnathan Mantey         std::string sensorNameLower =
97449c53ac9SJohnathan Mantey             boost::algorithm::to_lower_copy(sensorName);
97549c53ac9SJohnathan Mantey 
97655f79e6fSEd Tanous         if (sensorName == "total_power")
977028f7ebcSEddie James         {
97881ce609eSEd Tanous             sensorJson["@odata.type"] = "#Power.v1_0_0.PowerControl";
9797ab06f49SGunnar Mills             // Put multiple "sensors" into a single PowerControl, so have
9807ab06f49SGunnar Mills             // generic names for MemberId and Name. Follows Redfish mockup.
98181ce609eSEd Tanous             sensorJson["MemberId"] = "0";
98281ce609eSEd Tanous             sensorJson["Name"] = "Chassis Power Control";
9833929aca1SAnthony Wilson             unit = "/PowerConsumedWatts"_json_pointer;
984028f7ebcSEddie James         }
985028f7ebcSEddie James         else if (sensorNameLower.find("input") != std::string::npos)
98649c53ac9SJohnathan Mantey         {
9873929aca1SAnthony Wilson             unit = "/PowerInputWatts"_json_pointer;
98849c53ac9SJohnathan Mantey         }
98949c53ac9SJohnathan Mantey         else
99049c53ac9SJohnathan Mantey         {
9913929aca1SAnthony Wilson             unit = "/PowerOutputWatts"_json_pointer;
99249c53ac9SJohnathan Mantey         }
9932474adfaSEd Tanous     }
9941abe55efSEd Tanous     else
9951abe55efSEd Tanous     {
99655c7b7a2SEd Tanous         BMCWEB_LOG_ERROR << "Redfish cannot map object type for " << sensorName;
99708777fb0SLewanczyk, Dawid         return;
99808777fb0SLewanczyk, Dawid     }
99908777fb0SLewanczyk, Dawid     // Map of dbus interface name, dbus property name and redfish property_name
10003929aca1SAnthony Wilson     std::vector<
10013929aca1SAnthony Wilson         std::tuple<const char*, const char*, nlohmann::json::json_pointer>>
10023929aca1SAnthony Wilson         properties;
100308777fb0SLewanczyk, Dawid     properties.reserve(7);
100408777fb0SLewanczyk, Dawid 
100508777fb0SLewanczyk, Dawid     properties.emplace_back("xyz.openbmc_project.Sensor.Value", "Value", unit);
1006de629b6eSShawn McCarney 
1007a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
10083929aca1SAnthony Wilson     {
10093929aca1SAnthony Wilson         properties.emplace_back(
10103929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningHigh",
10113929aca1SAnthony Wilson             "/Thresholds/UpperCaution/Reading"_json_pointer);
10123929aca1SAnthony Wilson         properties.emplace_back(
10133929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Warning", "WarningLow",
10143929aca1SAnthony Wilson             "/Thresholds/LowerCaution/Reading"_json_pointer);
10153929aca1SAnthony Wilson         properties.emplace_back(
10163929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalHigh",
10173929aca1SAnthony Wilson             "/Thresholds/UpperCritical/Reading"_json_pointer);
10183929aca1SAnthony Wilson         properties.emplace_back(
10193929aca1SAnthony Wilson             "xyz.openbmc_project.Sensor.Threshold.Critical", "CriticalLow",
10203929aca1SAnthony Wilson             "/Thresholds/LowerCritical/Reading"_json_pointer);
10213929aca1SAnthony Wilson     }
10223929aca1SAnthony Wilson     else if (sensorType != "power")
1023de629b6eSShawn McCarney     {
102408777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
10253929aca1SAnthony Wilson                                 "WarningHigh",
10263929aca1SAnthony Wilson                                 "/UpperThresholdNonCritical"_json_pointer);
102708777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Warning",
10283929aca1SAnthony Wilson                                 "WarningLow",
10293929aca1SAnthony Wilson                                 "/LowerThresholdNonCritical"_json_pointer);
103008777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
10313929aca1SAnthony Wilson                                 "CriticalHigh",
10323929aca1SAnthony Wilson                                 "/UpperThresholdCritical"_json_pointer);
103308777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Threshold.Critical",
10343929aca1SAnthony Wilson                                 "CriticalLow",
10353929aca1SAnthony Wilson                                 "/LowerThresholdCritical"_json_pointer);
1036de629b6eSShawn McCarney     }
103708777fb0SLewanczyk, Dawid 
10382474adfaSEd Tanous     // TODO Need to get UpperThresholdFatal and LowerThresholdFatal
10392474adfaSEd Tanous 
1040a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode == sensors::node::sensors)
104195a3ecadSAnthony Wilson     {
104295a3ecadSAnthony Wilson         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
10433929aca1SAnthony Wilson                                 "/ReadingRangeMin"_json_pointer);
104495a3ecadSAnthony Wilson         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
10453929aca1SAnthony Wilson                                 "/ReadingRangeMax"_json_pointer);
104695a3ecadSAnthony Wilson     }
104795a3ecadSAnthony Wilson     else if (sensorType == "temperature")
10481abe55efSEd Tanous     {
104908777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
10503929aca1SAnthony Wilson                                 "/MinReadingRangeTemp"_json_pointer);
105108777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
10523929aca1SAnthony Wilson                                 "/MaxReadingRangeTemp"_json_pointer);
10531abe55efSEd Tanous     }
1054adc4f0dbSShawn McCarney     else if (sensorType != "power")
10551abe55efSEd Tanous     {
105608777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MinValue",
10573929aca1SAnthony Wilson                                 "/MinReadingRange"_json_pointer);
105808777fb0SLewanczyk, Dawid         properties.emplace_back("xyz.openbmc_project.Sensor.Value", "MaxValue",
10593929aca1SAnthony Wilson                                 "/MaxReadingRange"_json_pointer);
106008777fb0SLewanczyk, Dawid     }
106108777fb0SLewanczyk, Dawid 
10623929aca1SAnthony Wilson     for (const std::tuple<const char*, const char*,
10633929aca1SAnthony Wilson                           nlohmann::json::json_pointer>& p : properties)
10641abe55efSEd Tanous     {
106555f79e6fSEd Tanous         for (const auto& [interface, values] : interfacesDict)
10661abe55efSEd Tanous         {
1067711ac7a9SEd Tanous             if (interface != std::get<0>(p))
10681abe55efSEd Tanous             {
1069711ac7a9SEd Tanous                 continue;
1070711ac7a9SEd Tanous             }
107155f79e6fSEd Tanous             for (const auto& [valueName, valueVariant] : values)
1072711ac7a9SEd Tanous             {
1073711ac7a9SEd Tanous                 if (valueName != std::get<1>(p))
1074711ac7a9SEd Tanous                 {
1075711ac7a9SEd Tanous                     continue;
1076711ac7a9SEd Tanous                 }
10773929aca1SAnthony Wilson 
10783929aca1SAnthony Wilson                 // The property we want to set may be nested json, so use
10793929aca1SAnthony Wilson                 // a json_pointer for easy indexing into the json structure.
10803929aca1SAnthony Wilson                 const nlohmann::json::json_pointer& key = std::get<2>(p);
10813929aca1SAnthony Wilson 
108208777fb0SLewanczyk, Dawid                 // Attempt to pull the int64 directly
1083abf2add6SEd Tanous                 const int64_t* int64Value = std::get_if<int64_t>(&valueVariant);
108408777fb0SLewanczyk, Dawid 
1085abf2add6SEd Tanous                 const double* doubleValue = std::get_if<double>(&valueVariant);
1086028f7ebcSEddie James                 const uint32_t* uValue = std::get_if<uint32_t>(&valueVariant);
10876f6d0d32SEd Tanous                 double temp = 0.0;
10886f6d0d32SEd Tanous                 if (int64Value != nullptr)
10891abe55efSEd Tanous                 {
1090271584abSEd Tanous                     temp = static_cast<double>(*int64Value);
10916f6d0d32SEd Tanous                 }
10926f6d0d32SEd Tanous                 else if (doubleValue != nullptr)
10931abe55efSEd Tanous                 {
10946f6d0d32SEd Tanous                     temp = *doubleValue;
10951abe55efSEd Tanous                 }
1096028f7ebcSEddie James                 else if (uValue != nullptr)
1097028f7ebcSEddie James                 {
1098028f7ebcSEddie James                     temp = *uValue;
1099028f7ebcSEddie James                 }
11001abe55efSEd Tanous                 else
11011abe55efSEd Tanous                 {
11026f6d0d32SEd Tanous                     BMCWEB_LOG_ERROR
11036f6d0d32SEd Tanous                         << "Got value interface that wasn't int or double";
11046f6d0d32SEd Tanous                     continue;
110508777fb0SLewanczyk, Dawid                 }
11066f6d0d32SEd Tanous                 temp = temp * std::pow(10, scaleMultiplier);
11076f6d0d32SEd Tanous                 if (forceToInt)
11086f6d0d32SEd Tanous                 {
110981ce609eSEd Tanous                     sensorJson[key] = static_cast<int64_t>(temp);
11106f6d0d32SEd Tanous                 }
11116f6d0d32SEd Tanous                 else
11126f6d0d32SEd Tanous                 {
111381ce609eSEd Tanous                     sensorJson[key] = temp;
111408777fb0SLewanczyk, Dawid                 }
111508777fb0SLewanczyk, Dawid             }
111608777fb0SLewanczyk, Dawid         }
111708777fb0SLewanczyk, Dawid     }
1118a0ec28b6SAdrian Ambrożewicz 
111981ce609eSEd Tanous     sensorsAsyncResp->addMetadata(sensorJson, unit.to_string(),
1120a0ec28b6SAdrian Ambrożewicz                                   "/xyz/openbmc_project/sensors/" + sensorType +
1121a0ec28b6SAdrian Ambrożewicz                                       "/" + sensorName);
1122a0ec28b6SAdrian Ambrożewicz 
112355c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "Added sensor " << sensorName;
112408777fb0SLewanczyk, Dawid }
112508777fb0SLewanczyk, Dawid 
1126b5a76932SEd Tanous inline void populateFanRedundancy(
1127b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
11288bd25ccdSJames Feist {
11298bd25ccdSJames Feist     crow::connections::systemBus->async_method_call(
1130b9d36b47SEd Tanous         [sensorsAsyncResp](
1131b9d36b47SEd Tanous             const boost::system::error_code ec,
1132b9d36b47SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& resp) {
11338bd25ccdSJames Feist             if (ec)
11348bd25ccdSJames Feist             {
11358bd25ccdSJames Feist                 return; // don't have to have this interface
11368bd25ccdSJames Feist             }
1137e278c18fSEd Tanous             for (const std::pair<std::string,
1138e278c18fSEd Tanous                                  std::vector<std::pair<
1139e278c18fSEd Tanous                                      std::string, std::vector<std::string>>>>&
1140e278c18fSEd Tanous                      pathPair : resp)
11418bd25ccdSJames Feist             {
1142e278c18fSEd Tanous                 const std::string& path = pathPair.first;
1143e278c18fSEd Tanous                 const std::vector<
1144e278c18fSEd Tanous                     std::pair<std::string, std::vector<std::string>>>& objDict =
1145e278c18fSEd Tanous                     pathPair.second;
11468bd25ccdSJames Feist                 if (objDict.empty())
11478bd25ccdSJames Feist                 {
11488bd25ccdSJames Feist                     continue; // this should be impossible
11498bd25ccdSJames Feist                 }
11508bd25ccdSJames Feist 
11518bd25ccdSJames Feist                 const std::string& owner = objDict.begin()->first;
11521e1e598dSJonathan Doman                 sdbusplus::asio::getProperty<std::vector<std::string>>(
11531e1e598dSJonathan Doman                     *crow::connections::systemBus,
11541e1e598dSJonathan Doman                     "xyz.openbmc_project.ObjectMapper", path + "/chassis",
11551e1e598dSJonathan Doman                     "xyz.openbmc_project.Association", "endpoints",
11561e1e598dSJonathan Doman                     [path, owner, sensorsAsyncResp](
11571e1e598dSJonathan Doman                         const boost::system::error_code e,
11581e1e598dSJonathan Doman                         const std::vector<std::string>& endpoints) {
1159271584abSEd Tanous                         if (e)
11608bd25ccdSJames Feist                         {
11618bd25ccdSJames Feist                             return; // if they don't have an association we
11628bd25ccdSJames Feist                                     // can't tell what chassis is
11638bd25ccdSJames Feist                         }
11648bd25ccdSJames Feist                         auto found = std::find_if(
11651e1e598dSJonathan Doman                             endpoints.begin(), endpoints.end(),
11668bd25ccdSJames Feist                             [sensorsAsyncResp](const std::string& entry) {
11678bd25ccdSJames Feist                                 return entry.find(
11688bd25ccdSJames Feist                                            sensorsAsyncResp->chassisId) !=
11698bd25ccdSJames Feist                                        std::string::npos;
11708bd25ccdSJames Feist                             });
11718bd25ccdSJames Feist 
11721e1e598dSJonathan Doman                         if (found == endpoints.end())
11738bd25ccdSJames Feist                         {
11748bd25ccdSJames Feist                             return;
11758bd25ccdSJames Feist                         }
11768bd25ccdSJames Feist                         crow::connections::systemBus->async_method_call(
11778bd25ccdSJames Feist                             [path, sensorsAsyncResp](
1178271584abSEd Tanous                                 const boost::system::error_code& err,
11798bd25ccdSJames Feist                                 const boost::container::flat_map<
11808bd25ccdSJames Feist                                     std::string,
1181168e20c1SEd Tanous                                     dbus::utility::DbusVariantType>& ret) {
1182271584abSEd Tanous                                 if (err)
11838bd25ccdSJames Feist                                 {
11848bd25ccdSJames Feist                                     return; // don't have to have this
11858bd25ccdSJames Feist                                             // interface
11868bd25ccdSJames Feist                                 }
11878bd25ccdSJames Feist                                 auto findFailures = ret.find("AllowedFailures");
11888bd25ccdSJames Feist                                 auto findCollection = ret.find("Collection");
11898bd25ccdSJames Feist                                 auto findStatus = ret.find("Status");
11908bd25ccdSJames Feist 
11918bd25ccdSJames Feist                                 if (findFailures == ret.end() ||
11928bd25ccdSJames Feist                                     findCollection == ret.end() ||
11938bd25ccdSJames Feist                                     findStatus == ret.end())
11948bd25ccdSJames Feist                                 {
11958bd25ccdSJames Feist                                     BMCWEB_LOG_ERROR
11968bd25ccdSJames Feist                                         << "Invalid redundancy interface";
11978bd25ccdSJames Feist                                     messages::internalError(
11988d1b46d7Szhanghch05                                         sensorsAsyncResp->asyncResp->res);
11998bd25ccdSJames Feist                                     return;
12008bd25ccdSJames Feist                                 }
12018bd25ccdSJames Feist 
12029eb808c1SEd Tanous                                 const uint8_t* allowedFailures =
12039eb808c1SEd Tanous                                     std::get_if<uint8_t>(
12048bd25ccdSJames Feist                                         &(findFailures->second));
12059eb808c1SEd Tanous                                 const std::vector<std::string>* collection =
12068bd25ccdSJames Feist                                     std::get_if<std::vector<std::string>>(
12078bd25ccdSJames Feist                                         &(findCollection->second));
12089eb808c1SEd Tanous                                 const std::string* status =
12099eb808c1SEd Tanous                                     std::get_if<std::string>(
12108bd25ccdSJames Feist                                         &(findStatus->second));
12118bd25ccdSJames Feist 
12128bd25ccdSJames Feist                                 if (allowedFailures == nullptr ||
12138bd25ccdSJames Feist                                     collection == nullptr || status == nullptr)
12148bd25ccdSJames Feist                                 {
12158bd25ccdSJames Feist 
12168bd25ccdSJames Feist                                     BMCWEB_LOG_ERROR
12170fda0f12SGeorge Liu                                         << "Invalid redundancy interface types";
12188bd25ccdSJames Feist                                     messages::internalError(
12198d1b46d7Szhanghch05                                         sensorsAsyncResp->asyncResp->res);
12208bd25ccdSJames Feist                                     return;
12218bd25ccdSJames Feist                                 }
122228aa8de5SGeorge Liu                                 sdbusplus::message::object_path objectPath(
122328aa8de5SGeorge Liu                                     path);
122428aa8de5SGeorge Liu                                 std::string name = objectPath.filename();
122528aa8de5SGeorge Liu                                 if (name.empty())
12268bd25ccdSJames Feist                                 {
12278bd25ccdSJames Feist                                     // this should be impossible
12288bd25ccdSJames Feist                                     messages::internalError(
12298d1b46d7Szhanghch05                                         sensorsAsyncResp->asyncResp->res);
12308bd25ccdSJames Feist                                     return;
12318bd25ccdSJames Feist                                 }
12328bd25ccdSJames Feist                                 std::replace(name.begin(), name.end(), '_',
12338bd25ccdSJames Feist                                              ' ');
12348bd25ccdSJames Feist 
12358bd25ccdSJames Feist                                 std::string health;
12368bd25ccdSJames Feist 
12378bd25ccdSJames Feist                                 if (boost::ends_with(*status, "Full"))
12388bd25ccdSJames Feist                                 {
12398bd25ccdSJames Feist                                     health = "OK";
12408bd25ccdSJames Feist                                 }
12418bd25ccdSJames Feist                                 else if (boost::ends_with(*status, "Degraded"))
12428bd25ccdSJames Feist                                 {
12438bd25ccdSJames Feist                                     health = "Warning";
12448bd25ccdSJames Feist                                 }
12458bd25ccdSJames Feist                                 else
12468bd25ccdSJames Feist                                 {
12478bd25ccdSJames Feist                                     health = "Critical";
12488bd25ccdSJames Feist                                 }
12498bd25ccdSJames Feist                                 std::vector<nlohmann::json> redfishCollection;
12508bd25ccdSJames Feist                                 const auto& fanRedfish =
12518d1b46d7Szhanghch05                                     sensorsAsyncResp->asyncResp->res
12528d1b46d7Szhanghch05                                         .jsonValue["Fans"];
12538bd25ccdSJames Feist                                 for (const std::string& item : *collection)
12548bd25ccdSJames Feist                                 {
125528aa8de5SGeorge Liu                                     sdbusplus::message::object_path path(item);
125628aa8de5SGeorge Liu                                     std::string itemName = path.filename();
125728aa8de5SGeorge Liu                                     if (itemName.empty())
125828aa8de5SGeorge Liu                                     {
125928aa8de5SGeorge Liu                                         continue;
126028aa8de5SGeorge Liu                                     }
12618bd25ccdSJames Feist                                     /*
12628bd25ccdSJames Feist                                     todo(ed): merge patch that fixes the names
12638bd25ccdSJames Feist                                     std::replace(itemName.begin(),
12648bd25ccdSJames Feist                                                  itemName.end(), '_', ' ');*/
12658bd25ccdSJames Feist                                     auto schemaItem = std::find_if(
12668bd25ccdSJames Feist                                         fanRedfish.begin(), fanRedfish.end(),
12678bd25ccdSJames Feist                                         [itemName](const nlohmann::json& fan) {
12688bd25ccdSJames Feist                                             return fan["MemberId"] == itemName;
12698bd25ccdSJames Feist                                         });
12708bd25ccdSJames Feist                                     if (schemaItem != fanRedfish.end())
12718bd25ccdSJames Feist                                     {
12728bd25ccdSJames Feist                                         redfishCollection.push_back(
12738bd25ccdSJames Feist                                             {{"@odata.id",
12748bd25ccdSJames Feist                                               (*schemaItem)["@odata.id"]}});
12758bd25ccdSJames Feist                                     }
12768bd25ccdSJames Feist                                     else
12778bd25ccdSJames Feist                                     {
12788bd25ccdSJames Feist                                         BMCWEB_LOG_ERROR
12798bd25ccdSJames Feist                                             << "failed to find fan in schema";
12808bd25ccdSJames Feist                                         messages::internalError(
12818d1b46d7Szhanghch05                                             sensorsAsyncResp->asyncResp->res);
12828bd25ccdSJames Feist                                         return;
12838bd25ccdSJames Feist                                     }
12848bd25ccdSJames Feist                                 }
12858bd25ccdSJames Feist 
12863e9e72ebSKuiying Wang                                 size_t minNumNeeded =
128726f6976fSEd Tanous                                     collection->empty()
128826f6976fSEd Tanous                                         ? 0
128926f6976fSEd Tanous                                         : collection->size() - *allowedFailures;
1290271584abSEd Tanous                                 nlohmann::json& jResp =
12918d1b46d7Szhanghch05                                     sensorsAsyncResp->asyncResp->res
12928bd25ccdSJames Feist                                         .jsonValue["Redundancy"];
1293271584abSEd Tanous                                 jResp.push_back(
12948bd25ccdSJames Feist                                     {{"@odata.id",
1295717794d5SAppaRao Puli                                       "/redfish/v1/Chassis/" +
12968bd25ccdSJames Feist                                           sensorsAsyncResp->chassisId + "/" +
12978bd25ccdSJames Feist                                           sensorsAsyncResp->chassisSubNode +
12988bd25ccdSJames Feist                                           "#/Redundancy/" +
1299271584abSEd Tanous                                           std::to_string(jResp.size())},
13008bd25ccdSJames Feist                                      {"@odata.type",
13018bd25ccdSJames Feist                                       "#Redundancy.v1_3_2.Redundancy"},
13023e9e72ebSKuiying Wang                                      {"MinNumNeeded", minNumNeeded},
13038bd25ccdSJames Feist                                      {"MemberId", name},
13048bd25ccdSJames Feist                                      {"Mode", "N+m"},
13058bd25ccdSJames Feist                                      {"Name", name},
13068bd25ccdSJames Feist                                      {"RedundancySet", redfishCollection},
13078bd25ccdSJames Feist                                      {"Status",
13088bd25ccdSJames Feist                                       {{"Health", health},
13098bd25ccdSJames Feist                                        {"State", "Enabled"}}}});
13108bd25ccdSJames Feist                             },
13118bd25ccdSJames Feist                             owner, path, "org.freedesktop.DBus.Properties",
13128bd25ccdSJames Feist                             "GetAll",
13138bd25ccdSJames Feist                             "xyz.openbmc_project.Control.FanRedundancy");
13141e1e598dSJonathan Doman                     });
13158bd25ccdSJames Feist             }
13168bd25ccdSJames Feist         },
13178bd25ccdSJames Feist         "xyz.openbmc_project.ObjectMapper",
13188bd25ccdSJames Feist         "/xyz/openbmc_project/object_mapper",
13198bd25ccdSJames Feist         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
13208bd25ccdSJames Feist         "/xyz/openbmc_project/control", 2,
13218bd25ccdSJames Feist         std::array<const char*, 1>{
13228bd25ccdSJames Feist             "xyz.openbmc_project.Control.FanRedundancy"});
13238bd25ccdSJames Feist }
13248bd25ccdSJames Feist 
1325b5a76932SEd Tanous inline void
132681ce609eSEd Tanous     sortJSONResponse(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
132749c53ac9SJohnathan Mantey {
13288d1b46d7Szhanghch05     nlohmann::json& response = sensorsAsyncResp->asyncResp->res.jsonValue;
132949c53ac9SJohnathan Mantey     std::array<std::string, 2> sensorHeaders{"Temperatures", "Fans"};
133081ce609eSEd Tanous     if (sensorsAsyncResp->chassisSubNode == sensors::node::power)
133149c53ac9SJohnathan Mantey     {
133249c53ac9SJohnathan Mantey         sensorHeaders = {"Voltages", "PowerSupplies"};
133349c53ac9SJohnathan Mantey     }
133449c53ac9SJohnathan Mantey     for (const std::string& sensorGroup : sensorHeaders)
133549c53ac9SJohnathan Mantey     {
133649c53ac9SJohnathan Mantey         nlohmann::json::iterator entry = response.find(sensorGroup);
133749c53ac9SJohnathan Mantey         if (entry != response.end())
133849c53ac9SJohnathan Mantey         {
133949c53ac9SJohnathan Mantey             std::sort(entry->begin(), entry->end(),
134049c53ac9SJohnathan Mantey                       [](nlohmann::json& c1, nlohmann::json& c2) {
134149c53ac9SJohnathan Mantey                           return c1["Name"] < c2["Name"];
134249c53ac9SJohnathan Mantey                       });
134349c53ac9SJohnathan Mantey 
134449c53ac9SJohnathan Mantey             // add the index counts to the end of each entry
134549c53ac9SJohnathan Mantey             size_t count = 0;
134649c53ac9SJohnathan Mantey             for (nlohmann::json& sensorJson : *entry)
134749c53ac9SJohnathan Mantey             {
134849c53ac9SJohnathan Mantey                 nlohmann::json::iterator odata = sensorJson.find("@odata.id");
134949c53ac9SJohnathan Mantey                 if (odata == sensorJson.end())
135049c53ac9SJohnathan Mantey                 {
135149c53ac9SJohnathan Mantey                     continue;
135249c53ac9SJohnathan Mantey                 }
135349c53ac9SJohnathan Mantey                 std::string* value = odata->get_ptr<std::string*>();
135449c53ac9SJohnathan Mantey                 if (value != nullptr)
135549c53ac9SJohnathan Mantey                 {
135649c53ac9SJohnathan Mantey                     *value += std::to_string(count);
135749c53ac9SJohnathan Mantey                     count++;
135881ce609eSEd Tanous                     sensorsAsyncResp->updateUri(sensorJson["Name"], *value);
135949c53ac9SJohnathan Mantey                 }
136049c53ac9SJohnathan Mantey             }
136149c53ac9SJohnathan Mantey         }
136249c53ac9SJohnathan Mantey     }
136349c53ac9SJohnathan Mantey }
136449c53ac9SJohnathan Mantey 
136508777fb0SLewanczyk, Dawid /**
1366adc4f0dbSShawn McCarney  * @brief Finds the inventory item with the specified object path.
1367adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1368adc4f0dbSShawn McCarney  * @param invItemObjPath D-Bus object path of inventory item.
1369adc4f0dbSShawn McCarney  * @return Inventory item within vector, or nullptr if no match found.
13708fb49dd6SShawn McCarney  */
137123a21a1cSEd Tanous inline InventoryItem* findInventoryItem(
1372b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1373adc4f0dbSShawn McCarney     const std::string& invItemObjPath)
13748fb49dd6SShawn McCarney {
1375adc4f0dbSShawn McCarney     for (InventoryItem& inventoryItem : *inventoryItems)
13768fb49dd6SShawn McCarney     {
1377adc4f0dbSShawn McCarney         if (inventoryItem.objectPath == invItemObjPath)
13788fb49dd6SShawn McCarney         {
1379adc4f0dbSShawn McCarney             return &inventoryItem;
13808fb49dd6SShawn McCarney         }
13818fb49dd6SShawn McCarney     }
13828fb49dd6SShawn McCarney     return nullptr;
13838fb49dd6SShawn McCarney }
13848fb49dd6SShawn McCarney 
13858fb49dd6SShawn McCarney /**
1386adc4f0dbSShawn McCarney  * @brief Finds the inventory item associated with the specified sensor.
1387adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1388adc4f0dbSShawn McCarney  * @param sensorObjPath D-Bus object path of sensor.
1389adc4f0dbSShawn McCarney  * @return Inventory item within vector, or nullptr if no match found.
13908fb49dd6SShawn McCarney  */
139123a21a1cSEd Tanous inline InventoryItem* findInventoryItemForSensor(
1392b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1393adc4f0dbSShawn McCarney     const std::string& sensorObjPath)
1394adc4f0dbSShawn McCarney {
1395adc4f0dbSShawn McCarney     for (InventoryItem& inventoryItem : *inventoryItems)
1396adc4f0dbSShawn McCarney     {
1397adc4f0dbSShawn McCarney         if (inventoryItem.sensors.count(sensorObjPath) > 0)
1398adc4f0dbSShawn McCarney         {
1399adc4f0dbSShawn McCarney             return &inventoryItem;
1400adc4f0dbSShawn McCarney         }
1401adc4f0dbSShawn McCarney     }
1402adc4f0dbSShawn McCarney     return nullptr;
1403adc4f0dbSShawn McCarney }
1404adc4f0dbSShawn McCarney 
1405adc4f0dbSShawn McCarney /**
1406d500549bSAnthony Wilson  * @brief Finds the inventory item associated with the specified led path.
1407d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1408d500549bSAnthony Wilson  * @param ledObjPath D-Bus object path of led.
1409d500549bSAnthony Wilson  * @return Inventory item within vector, or nullptr if no match found.
1410d500549bSAnthony Wilson  */
1411d500549bSAnthony Wilson inline InventoryItem*
1412d500549bSAnthony Wilson     findInventoryItemForLed(std::vector<InventoryItem>& inventoryItems,
1413d500549bSAnthony Wilson                             const std::string& ledObjPath)
1414d500549bSAnthony Wilson {
1415d500549bSAnthony Wilson     for (InventoryItem& inventoryItem : inventoryItems)
1416d500549bSAnthony Wilson     {
1417d500549bSAnthony Wilson         if (inventoryItem.ledObjectPath == ledObjPath)
1418d500549bSAnthony Wilson         {
1419d500549bSAnthony Wilson             return &inventoryItem;
1420d500549bSAnthony Wilson         }
1421d500549bSAnthony Wilson     }
1422d500549bSAnthony Wilson     return nullptr;
1423d500549bSAnthony Wilson }
1424d500549bSAnthony Wilson 
1425d500549bSAnthony Wilson /**
1426adc4f0dbSShawn McCarney  * @brief Adds inventory item and associated sensor to specified vector.
1427adc4f0dbSShawn McCarney  *
1428adc4f0dbSShawn McCarney  * Adds a new InventoryItem to the vector if necessary.  Searches for an
1429adc4f0dbSShawn McCarney  * existing InventoryItem with the specified object path.  If not found, one is
1430adc4f0dbSShawn McCarney  * added to the vector.
1431adc4f0dbSShawn McCarney  *
1432adc4f0dbSShawn McCarney  * Next, the specified sensor is added to the set of sensors associated with the
1433adc4f0dbSShawn McCarney  * InventoryItem.
1434adc4f0dbSShawn McCarney  *
1435adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1436adc4f0dbSShawn McCarney  * @param invItemObjPath D-Bus object path of inventory item.
1437adc4f0dbSShawn McCarney  * @param sensorObjPath D-Bus object path of sensor
1438adc4f0dbSShawn McCarney  */
1439b5a76932SEd Tanous inline void addInventoryItem(
1440b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
1441b5a76932SEd Tanous     const std::string& invItemObjPath, const std::string& sensorObjPath)
1442adc4f0dbSShawn McCarney {
1443adc4f0dbSShawn McCarney     // Look for inventory item in vector
1444adc4f0dbSShawn McCarney     InventoryItem* inventoryItem =
1445adc4f0dbSShawn McCarney         findInventoryItem(inventoryItems, invItemObjPath);
1446adc4f0dbSShawn McCarney 
1447adc4f0dbSShawn McCarney     // If inventory item doesn't exist in vector, add it
1448adc4f0dbSShawn McCarney     if (inventoryItem == nullptr)
1449adc4f0dbSShawn McCarney     {
1450adc4f0dbSShawn McCarney         inventoryItems->emplace_back(invItemObjPath);
1451adc4f0dbSShawn McCarney         inventoryItem = &(inventoryItems->back());
1452adc4f0dbSShawn McCarney     }
1453adc4f0dbSShawn McCarney 
1454adc4f0dbSShawn McCarney     // Add sensor to set of sensors associated with inventory item
1455adc4f0dbSShawn McCarney     inventoryItem->sensors.emplace(sensorObjPath);
1456adc4f0dbSShawn McCarney }
1457adc4f0dbSShawn McCarney 
1458adc4f0dbSShawn McCarney /**
1459adc4f0dbSShawn McCarney  * @brief Stores D-Bus data in the specified inventory item.
1460adc4f0dbSShawn McCarney  *
1461adc4f0dbSShawn McCarney  * Finds D-Bus data in the specified map of interfaces.  Stores the data in the
1462adc4f0dbSShawn McCarney  * specified InventoryItem.
1463adc4f0dbSShawn McCarney  *
1464adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
1465adc4f0dbSShawn McCarney  * response.
1466adc4f0dbSShawn McCarney  *
1467adc4f0dbSShawn McCarney  * @param inventoryItem Inventory item where data will be stored.
1468adc4f0dbSShawn McCarney  * @param interfacesDict Map containing D-Bus interfaces and their properties
1469adc4f0dbSShawn McCarney  * for the specified inventory item.
1470adc4f0dbSShawn McCarney  */
147123a21a1cSEd Tanous inline void storeInventoryItemData(
1472adc4f0dbSShawn McCarney     InventoryItem& inventoryItem,
1473711ac7a9SEd Tanous     const dbus::utility::DBusInteracesMap& interfacesDict)
14748fb49dd6SShawn McCarney {
1475adc4f0dbSShawn McCarney     // Get properties from Inventory.Item interface
1476711ac7a9SEd Tanous 
14779eb808c1SEd Tanous     for (const auto& [interface, values] : interfacesDict)
14788fb49dd6SShawn McCarney     {
1479711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Item")
14808fb49dd6SShawn McCarney         {
14819eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
1482711ac7a9SEd Tanous             {
1483711ac7a9SEd Tanous                 if (name == "Present")
1484711ac7a9SEd Tanous                 {
1485711ac7a9SEd Tanous                     const bool* value = std::get_if<bool>(&dbusValue);
1486adc4f0dbSShawn McCarney                     if (value != nullptr)
14878fb49dd6SShawn McCarney                     {
1488adc4f0dbSShawn McCarney                         inventoryItem.isPresent = *value;
14898fb49dd6SShawn McCarney                     }
14908fb49dd6SShawn McCarney                 }
14918fb49dd6SShawn McCarney             }
1492711ac7a9SEd Tanous         }
1493adc4f0dbSShawn McCarney         // Check if Inventory.Item.PowerSupply interface is present
1494711ac7a9SEd Tanous 
1495711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Item.PowerSupply")
14968fb49dd6SShawn McCarney         {
1497adc4f0dbSShawn McCarney             inventoryItem.isPowerSupply = true;
14988fb49dd6SShawn McCarney         }
1499adc4f0dbSShawn McCarney 
1500adc4f0dbSShawn McCarney         // Get properties from Inventory.Decorator.Asset interface
1501711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
1502adc4f0dbSShawn McCarney         {
15039eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
1504711ac7a9SEd Tanous             {
1505711ac7a9SEd Tanous                 if (name == "Manufacturer")
1506adc4f0dbSShawn McCarney                 {
1507adc4f0dbSShawn McCarney                     const std::string* value =
1508711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
1509adc4f0dbSShawn McCarney                     if (value != nullptr)
1510adc4f0dbSShawn McCarney                     {
1511adc4f0dbSShawn McCarney                         inventoryItem.manufacturer = *value;
1512adc4f0dbSShawn McCarney                     }
1513adc4f0dbSShawn McCarney                 }
1514711ac7a9SEd Tanous                 if (name == "Model")
1515adc4f0dbSShawn McCarney                 {
1516adc4f0dbSShawn McCarney                     const std::string* value =
1517711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
1518adc4f0dbSShawn McCarney                     if (value != nullptr)
1519adc4f0dbSShawn McCarney                     {
1520adc4f0dbSShawn McCarney                         inventoryItem.model = *value;
1521adc4f0dbSShawn McCarney                     }
1522adc4f0dbSShawn McCarney                 }
1523711ac7a9SEd Tanous                 if (name == "SerialNumber")
1524adc4f0dbSShawn McCarney                 {
1525adc4f0dbSShawn McCarney                     const std::string* value =
1526711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
1527adc4f0dbSShawn McCarney                     if (value != nullptr)
1528adc4f0dbSShawn McCarney                     {
1529adc4f0dbSShawn McCarney                         inventoryItem.serialNumber = *value;
1530adc4f0dbSShawn McCarney                     }
1531adc4f0dbSShawn McCarney                 }
1532711ac7a9SEd Tanous                 if (name == "PartNumber")
1533711ac7a9SEd Tanous                 {
1534711ac7a9SEd Tanous                     const std::string* value =
1535711ac7a9SEd Tanous                         std::get_if<std::string>(&dbusValue);
1536711ac7a9SEd Tanous                     if (value != nullptr)
1537711ac7a9SEd Tanous                     {
1538711ac7a9SEd Tanous                         inventoryItem.partNumber = *value;
1539711ac7a9SEd Tanous                     }
1540711ac7a9SEd Tanous                 }
1541711ac7a9SEd Tanous             }
1542adc4f0dbSShawn McCarney         }
1543adc4f0dbSShawn McCarney 
1544711ac7a9SEd Tanous         if (interface ==
1545711ac7a9SEd Tanous             "xyz.openbmc_project.State.Decorator.OperationalStatus")
1546adc4f0dbSShawn McCarney         {
15479eb808c1SEd Tanous             for (const auto& [name, dbusValue] : values)
1548adc4f0dbSShawn McCarney             {
1549711ac7a9SEd Tanous                 if (name == "Functional")
1550711ac7a9SEd Tanous                 {
1551711ac7a9SEd Tanous                     const bool* value = std::get_if<bool>(&dbusValue);
1552adc4f0dbSShawn McCarney                     if (value != nullptr)
1553adc4f0dbSShawn McCarney                     {
1554adc4f0dbSShawn McCarney                         inventoryItem.isFunctional = *value;
15558fb49dd6SShawn McCarney                     }
15568fb49dd6SShawn McCarney                 }
15578fb49dd6SShawn McCarney             }
15588fb49dd6SShawn McCarney         }
1559711ac7a9SEd Tanous     }
1560711ac7a9SEd Tanous }
15618fb49dd6SShawn McCarney 
15628fb49dd6SShawn McCarney /**
1563adc4f0dbSShawn McCarney  * @brief Gets D-Bus data for inventory items associated with sensors.
15648fb49dd6SShawn McCarney  *
1565adc4f0dbSShawn McCarney  * Uses the specified connections (services) to obtain D-Bus data for inventory
1566adc4f0dbSShawn McCarney  * items associated with sensors.  Stores the resulting data in the
1567adc4f0dbSShawn McCarney  * inventoryItems vector.
15688fb49dd6SShawn McCarney  *
1569adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
1570adc4f0dbSShawn McCarney  * response.
1571adc4f0dbSShawn McCarney  *
1572adc4f0dbSShawn McCarney  * Finds the inventory item data asynchronously.  Invokes callback when data has
1573adc4f0dbSShawn McCarney  * been obtained.
1574adc4f0dbSShawn McCarney  *
1575adc4f0dbSShawn McCarney  * The callback must have the following signature:
1576adc4f0dbSShawn McCarney  *   @code
1577d500549bSAnthony Wilson  *   callback(void)
1578adc4f0dbSShawn McCarney  *   @endcode
1579adc4f0dbSShawn McCarney  *
1580adc4f0dbSShawn McCarney  * This function is called recursively, obtaining data asynchronously from one
1581adc4f0dbSShawn McCarney  * connection in each call.  This ensures the callback is not invoked until the
1582adc4f0dbSShawn McCarney  * last asynchronous function has completed.
15838fb49dd6SShawn McCarney  *
15848fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
1585adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
1586adc4f0dbSShawn McCarney  * @param invConnections Connections that provide data for the inventory items.
15878fb49dd6SShawn McCarney  * @param objectMgrPaths Mappings from connection name to DBus object path that
15888fb49dd6SShawn McCarney  * implements ObjectManager.
1589adc4f0dbSShawn McCarney  * @param callback Callback to invoke when inventory data has been obtained.
1590adc4f0dbSShawn McCarney  * @param invConnectionsIndex Current index in invConnections.  Only specified
1591adc4f0dbSShawn McCarney  * in recursive calls to this function.
15928fb49dd6SShawn McCarney  */
1593adc4f0dbSShawn McCarney template <typename Callback>
1594adc4f0dbSShawn McCarney static void getInventoryItemsData(
15958fb49dd6SShawn McCarney     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1596adc4f0dbSShawn McCarney     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
15978fb49dd6SShawn McCarney     std::shared_ptr<boost::container::flat_set<std::string>> invConnections,
15988fb49dd6SShawn McCarney     std::shared_ptr<boost::container::flat_map<std::string, std::string>>
1599adc4f0dbSShawn McCarney         objectMgrPaths,
1600271584abSEd Tanous     Callback&& callback, size_t invConnectionsIndex = 0)
16018fb49dd6SShawn McCarney {
1602adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsData enter";
16038fb49dd6SShawn McCarney 
1604adc4f0dbSShawn McCarney     // If no more connections left, call callback
1605adc4f0dbSShawn McCarney     if (invConnectionsIndex >= invConnections->size())
16068fb49dd6SShawn McCarney     {
1607d500549bSAnthony Wilson         callback();
1608adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
1609adc4f0dbSShawn McCarney         return;
1610adc4f0dbSShawn McCarney     }
1611adc4f0dbSShawn McCarney 
1612adc4f0dbSShawn McCarney     // Get inventory item data from current connection
1613adc4f0dbSShawn McCarney     auto it = invConnections->nth(invConnectionsIndex);
1614adc4f0dbSShawn McCarney     if (it != invConnections->end())
1615adc4f0dbSShawn McCarney     {
1616adc4f0dbSShawn McCarney         const std::string& invConnection = *it;
1617adc4f0dbSShawn McCarney 
16188fb49dd6SShawn McCarney         // Response handler for GetManagedObjects
1619adc4f0dbSShawn McCarney         auto respHandler = [sensorsAsyncResp, inventoryItems, invConnections,
1620f94c4ecfSEd Tanous                             objectMgrPaths,
1621f94c4ecfSEd Tanous                             callback{std::forward<Callback>(callback)},
1622adc4f0dbSShawn McCarney                             invConnectionsIndex](
1623adc4f0dbSShawn McCarney                                const boost::system::error_code ec,
1624711ac7a9SEd Tanous                                dbus::utility::ManagedObjectType& resp) {
1625adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler enter";
16268fb49dd6SShawn McCarney             if (ec)
16278fb49dd6SShawn McCarney             {
16288fb49dd6SShawn McCarney                 BMCWEB_LOG_ERROR
1629adc4f0dbSShawn McCarney                     << "getInventoryItemsData respHandler DBus error " << ec;
16308d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
16318fb49dd6SShawn McCarney                 return;
16328fb49dd6SShawn McCarney             }
16338fb49dd6SShawn McCarney 
16348fb49dd6SShawn McCarney             // Loop through returned object paths
16358fb49dd6SShawn McCarney             for (const auto& objDictEntry : resp)
16368fb49dd6SShawn McCarney             {
16378fb49dd6SShawn McCarney                 const std::string& objPath =
16388fb49dd6SShawn McCarney                     static_cast<const std::string&>(objDictEntry.first);
16398fb49dd6SShawn McCarney 
1640adc4f0dbSShawn McCarney                 // If this object path is one of the specified inventory items
1641adc4f0dbSShawn McCarney                 InventoryItem* inventoryItem =
1642adc4f0dbSShawn McCarney                     findInventoryItem(inventoryItems, objPath);
1643adc4f0dbSShawn McCarney                 if (inventoryItem != nullptr)
16448fb49dd6SShawn McCarney                 {
1645adc4f0dbSShawn McCarney                     // Store inventory data in InventoryItem
1646adc4f0dbSShawn McCarney                     storeInventoryItemData(*inventoryItem, objDictEntry.second);
16478fb49dd6SShawn McCarney                 }
16488fb49dd6SShawn McCarney             }
16498fb49dd6SShawn McCarney 
1650adc4f0dbSShawn McCarney             // Recurse to get inventory item data from next connection
1651adc4f0dbSShawn McCarney             getInventoryItemsData(sensorsAsyncResp, inventoryItems,
1652adc4f0dbSShawn McCarney                                   invConnections, objectMgrPaths,
1653adc4f0dbSShawn McCarney                                   std::move(callback), invConnectionsIndex + 1);
1654adc4f0dbSShawn McCarney 
1655adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemsData respHandler exit";
16568fb49dd6SShawn McCarney         };
16578fb49dd6SShawn McCarney 
16588fb49dd6SShawn McCarney         // Find DBus object path that implements ObjectManager for the current
16598fb49dd6SShawn McCarney         // connection.  If no mapping found, default to "/".
16608fb49dd6SShawn McCarney         auto iter = objectMgrPaths->find(invConnection);
16618fb49dd6SShawn McCarney         const std::string& objectMgrPath =
16628fb49dd6SShawn McCarney             (iter != objectMgrPaths->end()) ? iter->second : "/";
16638fb49dd6SShawn McCarney         BMCWEB_LOG_DEBUG << "ObjectManager path for " << invConnection << " is "
16648fb49dd6SShawn McCarney                          << objectMgrPath;
16658fb49dd6SShawn McCarney 
16668fb49dd6SShawn McCarney         // Get all object paths and their interfaces for current connection
16678fb49dd6SShawn McCarney         crow::connections::systemBus->async_method_call(
16688fb49dd6SShawn McCarney             std::move(respHandler), invConnection, objectMgrPath,
16698fb49dd6SShawn McCarney             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
16708fb49dd6SShawn McCarney     }
16718fb49dd6SShawn McCarney 
1672adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsData exit";
16738fb49dd6SShawn McCarney }
16748fb49dd6SShawn McCarney 
16758fb49dd6SShawn McCarney /**
1676adc4f0dbSShawn McCarney  * @brief Gets connections that provide D-Bus data for inventory items.
16778fb49dd6SShawn McCarney  *
1678adc4f0dbSShawn McCarney  * Gets the D-Bus connections (services) that provide data for the inventory
1679adc4f0dbSShawn McCarney  * items that are associated with sensors.
16808fb49dd6SShawn McCarney  *
16818fb49dd6SShawn McCarney  * Finds the connections asynchronously.  Invokes callback when information has
16828fb49dd6SShawn McCarney  * been obtained.
16838fb49dd6SShawn McCarney  *
16848fb49dd6SShawn McCarney  * The callback must have the following signature:
16858fb49dd6SShawn McCarney  *   @code
16868fb49dd6SShawn McCarney  *   callback(std::shared_ptr<boost::container::flat_set<std::string>>
16878fb49dd6SShawn McCarney  *            invConnections)
16888fb49dd6SShawn McCarney  *   @endcode
16898fb49dd6SShawn McCarney  *
16908fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
1691adc4f0dbSShawn McCarney  * @param inventoryItems D-Bus inventory items associated with sensors.
16928fb49dd6SShawn McCarney  * @param callback Callback to invoke when connections have been obtained.
16938fb49dd6SShawn McCarney  */
16948fb49dd6SShawn McCarney template <typename Callback>
16958fb49dd6SShawn McCarney static void getInventoryItemsConnections(
1696b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1697b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems,
16988fb49dd6SShawn McCarney     Callback&& callback)
16998fb49dd6SShawn McCarney {
17008fb49dd6SShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsConnections enter";
17018fb49dd6SShawn McCarney 
17028fb49dd6SShawn McCarney     const std::string path = "/xyz/openbmc_project/inventory";
1703adc4f0dbSShawn McCarney     const std::array<std::string, 4> interfaces = {
17048fb49dd6SShawn McCarney         "xyz.openbmc_project.Inventory.Item",
1705adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Item.PowerSupply",
1706adc4f0dbSShawn McCarney         "xyz.openbmc_project.Inventory.Decorator.Asset",
17078fb49dd6SShawn McCarney         "xyz.openbmc_project.State.Decorator.OperationalStatus"};
17088fb49dd6SShawn McCarney 
17098fb49dd6SShawn McCarney     // Response handler for parsing output from GetSubTree
1710f94c4ecfSEd Tanous     auto respHandler = [callback{std::forward<Callback>(callback)},
1711b9d36b47SEd Tanous                         sensorsAsyncResp, inventoryItems](
1712b9d36b47SEd Tanous                            const boost::system::error_code ec,
1713b9d36b47SEd Tanous                            const dbus::utility::MapperGetSubTreeResponse&
1714b9d36b47SEd Tanous                                subtree) {
17158fb49dd6SShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler enter";
17168fb49dd6SShawn McCarney         if (ec)
17178fb49dd6SShawn McCarney         {
17188d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
17198fb49dd6SShawn McCarney             BMCWEB_LOG_ERROR
17208fb49dd6SShawn McCarney                 << "getInventoryItemsConnections respHandler DBus error " << ec;
17218fb49dd6SShawn McCarney             return;
17228fb49dd6SShawn McCarney         }
17238fb49dd6SShawn McCarney 
17248fb49dd6SShawn McCarney         // Make unique list of connections for desired inventory items
17258fb49dd6SShawn McCarney         std::shared_ptr<boost::container::flat_set<std::string>>
17268fb49dd6SShawn McCarney             invConnections =
17278fb49dd6SShawn McCarney                 std::make_shared<boost::container::flat_set<std::string>>();
17288fb49dd6SShawn McCarney         invConnections->reserve(8);
17298fb49dd6SShawn McCarney 
17308fb49dd6SShawn McCarney         // Loop through objects from GetSubTree
17318fb49dd6SShawn McCarney         for (const std::pair<
17328fb49dd6SShawn McCarney                  std::string,
17338fb49dd6SShawn McCarney                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
17348fb49dd6SShawn McCarney                  object : subtree)
17358fb49dd6SShawn McCarney         {
1736adc4f0dbSShawn McCarney             // Check if object path is one of the specified inventory items
17378fb49dd6SShawn McCarney             const std::string& objPath = object.first;
1738adc4f0dbSShawn McCarney             if (findInventoryItem(inventoryItems, objPath) != nullptr)
17398fb49dd6SShawn McCarney             {
17408fb49dd6SShawn McCarney                 // Store all connections to inventory item
17418fb49dd6SShawn McCarney                 for (const std::pair<std::string, std::vector<std::string>>&
17428fb49dd6SShawn McCarney                          objData : object.second)
17438fb49dd6SShawn McCarney                 {
17448fb49dd6SShawn McCarney                     const std::string& invConnection = objData.first;
17458fb49dd6SShawn McCarney                     invConnections->insert(invConnection);
17468fb49dd6SShawn McCarney                 }
17478fb49dd6SShawn McCarney             }
17488fb49dd6SShawn McCarney         }
1749d500549bSAnthony Wilson 
17508fb49dd6SShawn McCarney         callback(invConnections);
17518fb49dd6SShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemsConnections respHandler exit";
17528fb49dd6SShawn McCarney     };
17538fb49dd6SShawn McCarney 
17548fb49dd6SShawn McCarney     // Make call to ObjectMapper to find all inventory items
17558fb49dd6SShawn McCarney     crow::connections::systemBus->async_method_call(
17568fb49dd6SShawn McCarney         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
17578fb49dd6SShawn McCarney         "/xyz/openbmc_project/object_mapper",
17588fb49dd6SShawn McCarney         "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
17598fb49dd6SShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemsConnections exit";
17608fb49dd6SShawn McCarney }
17618fb49dd6SShawn McCarney 
17628fb49dd6SShawn McCarney /**
1763adc4f0dbSShawn McCarney  * @brief Gets associations from sensors to inventory items.
17648fb49dd6SShawn McCarney  *
17658fb49dd6SShawn McCarney  * Looks for ObjectMapper associations from the specified sensors to related
1766d500549bSAnthony Wilson  * inventory items. Then finds the associations from those inventory items to
1767d500549bSAnthony Wilson  * their LEDs, if any.
17688fb49dd6SShawn McCarney  *
17698fb49dd6SShawn McCarney  * Finds the inventory items asynchronously.  Invokes callback when information
17708fb49dd6SShawn McCarney  * has been obtained.
17718fb49dd6SShawn McCarney  *
17728fb49dd6SShawn McCarney  * The callback must have the following signature:
17738fb49dd6SShawn McCarney  *   @code
1774adc4f0dbSShawn McCarney  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
17758fb49dd6SShawn McCarney  *   @endcode
17768fb49dd6SShawn McCarney  *
17778fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
17788fb49dd6SShawn McCarney  * @param sensorNames All sensors within the current chassis.
17798fb49dd6SShawn McCarney  * @param objectMgrPaths Mappings from connection name to DBus object path that
17808fb49dd6SShawn McCarney  * implements ObjectManager.
17818fb49dd6SShawn McCarney  * @param callback Callback to invoke when inventory items have been obtained.
17828fb49dd6SShawn McCarney  */
17838fb49dd6SShawn McCarney template <typename Callback>
1784adc4f0dbSShawn McCarney static void getInventoryItemAssociations(
1785b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
1786b5a76932SEd Tanous     const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames,
1787b5a76932SEd Tanous     const std::shared_ptr<boost::container::flat_map<std::string, std::string>>&
17888fb49dd6SShawn McCarney         objectMgrPaths,
17898fb49dd6SShawn McCarney     Callback&& callback)
17908fb49dd6SShawn McCarney {
1791adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemAssociations enter";
17928fb49dd6SShawn McCarney 
17938fb49dd6SShawn McCarney     // Response handler for GetManagedObjects
1794f94c4ecfSEd Tanous     auto respHandler = [callback{std::forward<Callback>(callback)},
1795f94c4ecfSEd Tanous                         sensorsAsyncResp,
17968fb49dd6SShawn McCarney                         sensorNames](const boost::system::error_code ec,
17978fb49dd6SShawn McCarney                                      dbus::utility::ManagedObjectType& resp) {
1798adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler enter";
17998fb49dd6SShawn McCarney         if (ec)
18008fb49dd6SShawn McCarney         {
1801adc4f0dbSShawn McCarney             BMCWEB_LOG_ERROR
1802adc4f0dbSShawn McCarney                 << "getInventoryItemAssociations respHandler DBus error " << ec;
18038d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
18048fb49dd6SShawn McCarney             return;
18058fb49dd6SShawn McCarney         }
18068fb49dd6SShawn McCarney 
1807adc4f0dbSShawn McCarney         // Create vector to hold list of inventory items
1808adc4f0dbSShawn McCarney         std::shared_ptr<std::vector<InventoryItem>> inventoryItems =
1809adc4f0dbSShawn McCarney             std::make_shared<std::vector<InventoryItem>>();
1810adc4f0dbSShawn McCarney 
18118fb49dd6SShawn McCarney         // Loop through returned object paths
18128fb49dd6SShawn McCarney         std::string sensorAssocPath;
18138fb49dd6SShawn McCarney         sensorAssocPath.reserve(128); // avoid memory allocations
18148fb49dd6SShawn McCarney         for (const auto& objDictEntry : resp)
18158fb49dd6SShawn McCarney         {
18168fb49dd6SShawn McCarney             const std::string& objPath =
18178fb49dd6SShawn McCarney                 static_cast<const std::string&>(objDictEntry.first);
18188fb49dd6SShawn McCarney 
18198fb49dd6SShawn McCarney             // If path is inventory association for one of the specified sensors
18208fb49dd6SShawn McCarney             for (const std::string& sensorName : *sensorNames)
18218fb49dd6SShawn McCarney             {
18228fb49dd6SShawn McCarney                 sensorAssocPath = sensorName;
18238fb49dd6SShawn McCarney                 sensorAssocPath += "/inventory";
18248fb49dd6SShawn McCarney                 if (objPath == sensorAssocPath)
18258fb49dd6SShawn McCarney                 {
18268fb49dd6SShawn McCarney                     // Get Association interface for object path
1827711ac7a9SEd Tanous                     for (const auto& [interface, values] : objDictEntry.second)
18288fb49dd6SShawn McCarney                     {
1829711ac7a9SEd Tanous                         if (interface == "xyz.openbmc_project.Association")
1830711ac7a9SEd Tanous                         {
1831711ac7a9SEd Tanous                             for (const auto& [valueName, value] : values)
1832711ac7a9SEd Tanous                             {
1833711ac7a9SEd Tanous                                 if (valueName == "endpoints")
18348fb49dd6SShawn McCarney                                 {
18358fb49dd6SShawn McCarney                                     const std::vector<std::string>* endpoints =
18368fb49dd6SShawn McCarney                                         std::get_if<std::vector<std::string>>(
1837711ac7a9SEd Tanous                                             &value);
1838711ac7a9SEd Tanous                                     if ((endpoints != nullptr) &&
1839711ac7a9SEd Tanous                                         !endpoints->empty())
18408fb49dd6SShawn McCarney                                     {
1841adc4f0dbSShawn McCarney                                         // Add inventory item to vector
1842adc4f0dbSShawn McCarney                                         const std::string& invItemPath =
1843adc4f0dbSShawn McCarney                                             endpoints->front();
1844711ac7a9SEd Tanous                                         addInventoryItem(inventoryItems,
1845711ac7a9SEd Tanous                                                          invItemPath,
1846adc4f0dbSShawn McCarney                                                          sensorName);
18478fb49dd6SShawn McCarney                                     }
18488fb49dd6SShawn McCarney                                 }
18498fb49dd6SShawn McCarney                             }
1850711ac7a9SEd Tanous                         }
1851711ac7a9SEd Tanous                     }
18528fb49dd6SShawn McCarney                     break;
18538fb49dd6SShawn McCarney                 }
18548fb49dd6SShawn McCarney             }
18558fb49dd6SShawn McCarney         }
18568fb49dd6SShawn McCarney 
1857d500549bSAnthony Wilson         // Now loop through the returned object paths again, this time to
1858d500549bSAnthony Wilson         // find the leds associated with the inventory items we just found
1859d500549bSAnthony Wilson         std::string inventoryAssocPath;
1860d500549bSAnthony Wilson         inventoryAssocPath.reserve(128); // avoid memory allocations
1861d500549bSAnthony Wilson         for (const auto& objDictEntry : resp)
1862d500549bSAnthony Wilson         {
1863d500549bSAnthony Wilson             const std::string& objPath =
1864d500549bSAnthony Wilson                 static_cast<const std::string&>(objDictEntry.first);
1865d500549bSAnthony Wilson 
1866d500549bSAnthony Wilson             for (InventoryItem& inventoryItem : *inventoryItems)
1867d500549bSAnthony Wilson             {
1868d500549bSAnthony Wilson                 inventoryAssocPath = inventoryItem.objectPath;
1869d500549bSAnthony Wilson                 inventoryAssocPath += "/leds";
1870d500549bSAnthony Wilson                 if (objPath == inventoryAssocPath)
1871d500549bSAnthony Wilson                 {
1872711ac7a9SEd Tanous                     for (const auto& [interface, values] : objDictEntry.second)
1873d500549bSAnthony Wilson                     {
1874711ac7a9SEd Tanous                         if (interface == "xyz.openbmc_project.Association")
1875711ac7a9SEd Tanous                         {
1876711ac7a9SEd Tanous                             for (const auto& [valueName, value] : values)
1877711ac7a9SEd Tanous                             {
1878711ac7a9SEd Tanous                                 if (valueName == "endpoints")
1879d500549bSAnthony Wilson                                 {
1880d500549bSAnthony Wilson                                     const std::vector<std::string>* endpoints =
1881d500549bSAnthony Wilson                                         std::get_if<std::vector<std::string>>(
1882711ac7a9SEd Tanous                                             &value);
1883711ac7a9SEd Tanous                                     if ((endpoints != nullptr) &&
1884711ac7a9SEd Tanous                                         !endpoints->empty())
1885d500549bSAnthony Wilson                                     {
1886711ac7a9SEd Tanous                                         // Add inventory item to vector
1887d500549bSAnthony Wilson                                         // Store LED path in inventory item
1888711ac7a9SEd Tanous                                         const std::string& ledPath =
1889711ac7a9SEd Tanous                                             endpoints->front();
1890d500549bSAnthony Wilson                                         inventoryItem.ledObjectPath = ledPath;
1891d500549bSAnthony Wilson                                     }
1892d500549bSAnthony Wilson                                 }
1893d500549bSAnthony Wilson                             }
1894711ac7a9SEd Tanous                         }
1895711ac7a9SEd Tanous                     }
1896711ac7a9SEd Tanous 
1897d500549bSAnthony Wilson                     break;
1898d500549bSAnthony Wilson                 }
1899d500549bSAnthony Wilson             }
1900d500549bSAnthony Wilson         }
1901adc4f0dbSShawn McCarney         callback(inventoryItems);
1902adc4f0dbSShawn McCarney         BMCWEB_LOG_DEBUG << "getInventoryItemAssociations respHandler exit";
19038fb49dd6SShawn McCarney     };
19048fb49dd6SShawn McCarney 
19058fb49dd6SShawn McCarney     // Find DBus object path that implements ObjectManager for ObjectMapper
19068fb49dd6SShawn McCarney     std::string connection = "xyz.openbmc_project.ObjectMapper";
19078fb49dd6SShawn McCarney     auto iter = objectMgrPaths->find(connection);
19088fb49dd6SShawn McCarney     const std::string& objectMgrPath =
19098fb49dd6SShawn McCarney         (iter != objectMgrPaths->end()) ? iter->second : "/";
19108fb49dd6SShawn McCarney     BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
19118fb49dd6SShawn McCarney                      << objectMgrPath;
19128fb49dd6SShawn McCarney 
19138fb49dd6SShawn McCarney     // Call GetManagedObjects on the ObjectMapper to get all associations
19148fb49dd6SShawn McCarney     crow::connections::systemBus->async_method_call(
19158fb49dd6SShawn McCarney         std::move(respHandler), connection, objectMgrPath,
19168fb49dd6SShawn McCarney         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
19178fb49dd6SShawn McCarney 
1918adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItemAssociations exit";
19198fb49dd6SShawn McCarney }
19208fb49dd6SShawn McCarney 
19218fb49dd6SShawn McCarney /**
1922d500549bSAnthony Wilson  * @brief Gets D-Bus data for inventory item leds associated with sensors.
1923d500549bSAnthony Wilson  *
1924d500549bSAnthony Wilson  * Uses the specified connections (services) to obtain D-Bus data for inventory
1925d500549bSAnthony Wilson  * item leds associated with sensors.  Stores the resulting data in the
1926d500549bSAnthony Wilson  * inventoryItems vector.
1927d500549bSAnthony Wilson  *
1928d500549bSAnthony Wilson  * This data is later used to provide sensor property values in the JSON
1929d500549bSAnthony Wilson  * response.
1930d500549bSAnthony Wilson  *
1931d500549bSAnthony Wilson  * Finds the inventory item led data asynchronously.  Invokes callback when data
1932d500549bSAnthony Wilson  * has been obtained.
1933d500549bSAnthony Wilson  *
1934d500549bSAnthony Wilson  * The callback must have the following signature:
1935d500549bSAnthony Wilson  *   @code
193642cbe538SGunnar Mills  *   callback()
1937d500549bSAnthony Wilson  *   @endcode
1938d500549bSAnthony Wilson  *
1939d500549bSAnthony Wilson  * This function is called recursively, obtaining data asynchronously from one
1940d500549bSAnthony Wilson  * connection in each call.  This ensures the callback is not invoked until the
1941d500549bSAnthony Wilson  * last asynchronous function has completed.
1942d500549bSAnthony Wilson  *
1943d500549bSAnthony Wilson  * @param sensorsAsyncResp Pointer to object holding response data.
1944d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
1945d500549bSAnthony Wilson  * @param ledConnections Connections that provide data for the inventory leds.
1946d500549bSAnthony Wilson  * @param callback Callback to invoke when inventory data has been obtained.
1947d500549bSAnthony Wilson  * @param ledConnectionsIndex Current index in ledConnections.  Only specified
1948d500549bSAnthony Wilson  * in recursive calls to this function.
1949d500549bSAnthony Wilson  */
1950d500549bSAnthony Wilson template <typename Callback>
1951d500549bSAnthony Wilson void getInventoryLedData(
1952d500549bSAnthony Wilson     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
1953d500549bSAnthony Wilson     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
1954d500549bSAnthony Wilson     std::shared_ptr<boost::container::flat_map<std::string, std::string>>
1955d500549bSAnthony Wilson         ledConnections,
1956d500549bSAnthony Wilson     Callback&& callback, size_t ledConnectionsIndex = 0)
1957d500549bSAnthony Wilson {
1958d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLedData enter";
1959d500549bSAnthony Wilson 
1960d500549bSAnthony Wilson     // If no more connections left, call callback
1961d500549bSAnthony Wilson     if (ledConnectionsIndex >= ledConnections->size())
1962d500549bSAnthony Wilson     {
196342cbe538SGunnar Mills         callback();
1964d500549bSAnthony Wilson         BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
1965d500549bSAnthony Wilson         return;
1966d500549bSAnthony Wilson     }
1967d500549bSAnthony Wilson 
1968d500549bSAnthony Wilson     // Get inventory item data from current connection
1969d500549bSAnthony Wilson     auto it = ledConnections->nth(ledConnectionsIndex);
1970d500549bSAnthony Wilson     if (it != ledConnections->end())
1971d500549bSAnthony Wilson     {
1972d500549bSAnthony Wilson         const std::string& ledPath = (*it).first;
1973d500549bSAnthony Wilson         const std::string& ledConnection = (*it).second;
1974d500549bSAnthony Wilson         // Response handler for Get State property
19751e1e598dSJonathan Doman         auto respHandler =
19761e1e598dSJonathan Doman             [sensorsAsyncResp, inventoryItems, ledConnections, ledPath,
1977f94c4ecfSEd Tanous              callback{std::forward<Callback>(callback)}, ledConnectionsIndex](
19781e1e598dSJonathan Doman                 const boost::system::error_code ec, const std::string& state) {
1979d500549bSAnthony Wilson                 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler enter";
1980d500549bSAnthony Wilson                 if (ec)
1981d500549bSAnthony Wilson                 {
1982d500549bSAnthony Wilson                     BMCWEB_LOG_ERROR
1983d500549bSAnthony Wilson                         << "getInventoryLedData respHandler DBus error " << ec;
19848d1b46d7Szhanghch05                     messages::internalError(sensorsAsyncResp->asyncResp->res);
1985d500549bSAnthony Wilson                     return;
1986d500549bSAnthony Wilson                 }
1987d500549bSAnthony Wilson 
19881e1e598dSJonathan Doman                 BMCWEB_LOG_DEBUG << "Led state: " << state;
1989d500549bSAnthony Wilson                 // Find inventory item with this LED object path
1990d500549bSAnthony Wilson                 InventoryItem* inventoryItem =
1991d500549bSAnthony Wilson                     findInventoryItemForLed(*inventoryItems, ledPath);
1992d500549bSAnthony Wilson                 if (inventoryItem != nullptr)
1993d500549bSAnthony Wilson                 {
1994d500549bSAnthony Wilson                     // Store LED state in InventoryItem
19951e1e598dSJonathan Doman                     if (boost::ends_with(state, "On"))
1996d500549bSAnthony Wilson                     {
1997d500549bSAnthony Wilson                         inventoryItem->ledState = LedState::ON;
1998d500549bSAnthony Wilson                     }
19991e1e598dSJonathan Doman                     else if (boost::ends_with(state, "Blink"))
2000d500549bSAnthony Wilson                     {
2001d500549bSAnthony Wilson                         inventoryItem->ledState = LedState::BLINK;
2002d500549bSAnthony Wilson                     }
20031e1e598dSJonathan Doman                     else if (boost::ends_with(state, "Off"))
2004d500549bSAnthony Wilson                     {
2005d500549bSAnthony Wilson                         inventoryItem->ledState = LedState::OFF;
2006d500549bSAnthony Wilson                     }
2007d500549bSAnthony Wilson                     else
2008d500549bSAnthony Wilson                     {
2009d500549bSAnthony Wilson                         inventoryItem->ledState = LedState::UNKNOWN;
2010d500549bSAnthony Wilson                     }
2011d500549bSAnthony Wilson                 }
2012d500549bSAnthony Wilson 
2013d500549bSAnthony Wilson                 // Recurse to get LED data from next connection
2014d500549bSAnthony Wilson                 getInventoryLedData(sensorsAsyncResp, inventoryItems,
2015d500549bSAnthony Wilson                                     ledConnections, std::move(callback),
2016d500549bSAnthony Wilson                                     ledConnectionsIndex + 1);
2017d500549bSAnthony Wilson 
2018d500549bSAnthony Wilson                 BMCWEB_LOG_DEBUG << "getInventoryLedData respHandler exit";
2019d500549bSAnthony Wilson             };
2020d500549bSAnthony Wilson 
2021d500549bSAnthony Wilson         // Get the State property for the current LED
20221e1e598dSJonathan Doman         sdbusplus::asio::getProperty<std::string>(
20231e1e598dSJonathan Doman             *crow::connections::systemBus, ledConnection, ledPath,
20241e1e598dSJonathan Doman             "xyz.openbmc_project.Led.Physical", "State",
20251e1e598dSJonathan Doman             std::move(respHandler));
2026d500549bSAnthony Wilson     }
2027d500549bSAnthony Wilson 
2028d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLedData exit";
2029d500549bSAnthony Wilson }
2030d500549bSAnthony Wilson 
2031d500549bSAnthony Wilson /**
2032d500549bSAnthony Wilson  * @brief Gets LED data for LEDs associated with given inventory items.
2033d500549bSAnthony Wilson  *
2034d500549bSAnthony Wilson  * Gets the D-Bus connections (services) that provide LED data for the LEDs
2035d500549bSAnthony Wilson  * associated with the specified inventory items.  Then gets the LED data from
2036d500549bSAnthony Wilson  * each connection and stores it in the inventory item.
2037d500549bSAnthony Wilson  *
2038d500549bSAnthony Wilson  * This data is later used to provide sensor property values in the JSON
2039d500549bSAnthony Wilson  * response.
2040d500549bSAnthony Wilson  *
2041d500549bSAnthony Wilson  * Finds the LED data asynchronously.  Invokes callback when information has
2042d500549bSAnthony Wilson  * been obtained.
2043d500549bSAnthony Wilson  *
2044d500549bSAnthony Wilson  * The callback must have the following signature:
2045d500549bSAnthony Wilson  *   @code
204642cbe538SGunnar Mills  *   callback()
2047d500549bSAnthony Wilson  *   @endcode
2048d500549bSAnthony Wilson  *
2049d500549bSAnthony Wilson  * @param sensorsAsyncResp Pointer to object holding response data.
2050d500549bSAnthony Wilson  * @param inventoryItems D-Bus inventory items associated with sensors.
2051d500549bSAnthony Wilson  * @param callback Callback to invoke when inventory items have been obtained.
2052d500549bSAnthony Wilson  */
2053d500549bSAnthony Wilson template <typename Callback>
2054d500549bSAnthony Wilson void getInventoryLeds(
2055d500549bSAnthony Wilson     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
2056d500549bSAnthony Wilson     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
2057d500549bSAnthony Wilson     Callback&& callback)
2058d500549bSAnthony Wilson {
2059d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLeds enter";
2060d500549bSAnthony Wilson 
2061d500549bSAnthony Wilson     const std::string path = "/xyz/openbmc_project";
2062d500549bSAnthony Wilson     const std::array<std::string, 1> interfaces = {
2063d500549bSAnthony Wilson         "xyz.openbmc_project.Led.Physical"};
2064d500549bSAnthony Wilson 
2065d500549bSAnthony Wilson     // Response handler for parsing output from GetSubTree
2066f94c4ecfSEd Tanous     auto respHandler = [callback{std::forward<Callback>(callback)},
2067b9d36b47SEd Tanous                         sensorsAsyncResp, inventoryItems](
2068b9d36b47SEd Tanous                            const boost::system::error_code ec,
2069b9d36b47SEd Tanous                            const dbus::utility::MapperGetSubTreeResponse&
2070b9d36b47SEd Tanous                                subtree) {
2071d500549bSAnthony Wilson         BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler enter";
2072d500549bSAnthony Wilson         if (ec)
2073d500549bSAnthony Wilson         {
20748d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
2075d500549bSAnthony Wilson             BMCWEB_LOG_ERROR << "getInventoryLeds respHandler DBus error "
2076d500549bSAnthony Wilson                              << ec;
2077d500549bSAnthony Wilson             return;
2078d500549bSAnthony Wilson         }
2079d500549bSAnthony Wilson 
2080d500549bSAnthony Wilson         // Build map of LED object paths to connections
2081d500549bSAnthony Wilson         std::shared_ptr<boost::container::flat_map<std::string, std::string>>
2082d500549bSAnthony Wilson             ledConnections = std::make_shared<
2083d500549bSAnthony Wilson                 boost::container::flat_map<std::string, std::string>>();
2084d500549bSAnthony Wilson 
2085d500549bSAnthony Wilson         // Loop through objects from GetSubTree
2086d500549bSAnthony Wilson         for (const std::pair<
2087d500549bSAnthony Wilson                  std::string,
2088d500549bSAnthony Wilson                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
2089d500549bSAnthony Wilson                  object : subtree)
2090d500549bSAnthony Wilson         {
2091d500549bSAnthony Wilson             // Check if object path is LED for one of the specified inventory
2092d500549bSAnthony Wilson             // items
2093d500549bSAnthony Wilson             const std::string& ledPath = object.first;
2094d500549bSAnthony Wilson             if (findInventoryItemForLed(*inventoryItems, ledPath) != nullptr)
2095d500549bSAnthony Wilson             {
2096d500549bSAnthony Wilson                 // Add mapping from ledPath to connection
2097d500549bSAnthony Wilson                 const std::string& connection = object.second.begin()->first;
2098d500549bSAnthony Wilson                 (*ledConnections)[ledPath] = connection;
2099d500549bSAnthony Wilson                 BMCWEB_LOG_DEBUG << "Added mapping " << ledPath << " -> "
2100d500549bSAnthony Wilson                                  << connection;
2101d500549bSAnthony Wilson             }
2102d500549bSAnthony Wilson         }
2103d500549bSAnthony Wilson 
2104d500549bSAnthony Wilson         getInventoryLedData(sensorsAsyncResp, inventoryItems, ledConnections,
2105d500549bSAnthony Wilson                             std::move(callback));
2106d500549bSAnthony Wilson         BMCWEB_LOG_DEBUG << "getInventoryLeds respHandler exit";
2107d500549bSAnthony Wilson     };
2108d500549bSAnthony Wilson     // Make call to ObjectMapper to find all inventory items
2109d500549bSAnthony Wilson     crow::connections::systemBus->async_method_call(
2110d500549bSAnthony Wilson         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
2111d500549bSAnthony Wilson         "/xyz/openbmc_project/object_mapper",
2112d500549bSAnthony Wilson         "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, 0, interfaces);
2113d500549bSAnthony Wilson     BMCWEB_LOG_DEBUG << "getInventoryLeds exit";
2114d500549bSAnthony Wilson }
2115d500549bSAnthony Wilson 
2116d500549bSAnthony Wilson /**
211742cbe538SGunnar Mills  * @brief Gets D-Bus data for Power Supply Attributes such as EfficiencyPercent
211842cbe538SGunnar Mills  *
211942cbe538SGunnar Mills  * Uses the specified connections (services) (currently assumes just one) to
212042cbe538SGunnar Mills  * obtain D-Bus data for Power Supply Attributes. Stores the resulting data in
212142cbe538SGunnar Mills  * the inventoryItems vector. Only stores data in Power Supply inventoryItems.
212242cbe538SGunnar Mills  *
212342cbe538SGunnar Mills  * This data is later used to provide sensor property values in the JSON
212442cbe538SGunnar Mills  * response.
212542cbe538SGunnar Mills  *
212642cbe538SGunnar Mills  * Finds the Power Supply Attributes data asynchronously.  Invokes callback
212742cbe538SGunnar Mills  * when data has been obtained.
212842cbe538SGunnar Mills  *
212942cbe538SGunnar Mills  * The callback must have the following signature:
213042cbe538SGunnar Mills  *   @code
213142cbe538SGunnar Mills  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
213242cbe538SGunnar Mills  *   @endcode
213342cbe538SGunnar Mills  *
213442cbe538SGunnar Mills  * @param sensorsAsyncResp Pointer to object holding response data.
213542cbe538SGunnar Mills  * @param inventoryItems D-Bus inventory items associated with sensors.
213642cbe538SGunnar Mills  * @param psAttributesConnections Connections that provide data for the Power
213742cbe538SGunnar Mills  *        Supply Attributes
213842cbe538SGunnar Mills  * @param callback Callback to invoke when data has been obtained.
213942cbe538SGunnar Mills  */
214042cbe538SGunnar Mills template <typename Callback>
214142cbe538SGunnar Mills void getPowerSupplyAttributesData(
2142b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
214342cbe538SGunnar Mills     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
214442cbe538SGunnar Mills     const boost::container::flat_map<std::string, std::string>&
214542cbe538SGunnar Mills         psAttributesConnections,
214642cbe538SGunnar Mills     Callback&& callback)
214742cbe538SGunnar Mills {
214842cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData enter";
214942cbe538SGunnar Mills 
215042cbe538SGunnar Mills     if (psAttributesConnections.empty())
215142cbe538SGunnar Mills     {
215242cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "Can't find PowerSupplyAttributes, no connections!";
215342cbe538SGunnar Mills         callback(inventoryItems);
215442cbe538SGunnar Mills         return;
215542cbe538SGunnar Mills     }
215642cbe538SGunnar Mills 
215742cbe538SGunnar Mills     // Assuming just one connection (service) for now
215842cbe538SGunnar Mills     auto it = psAttributesConnections.nth(0);
215942cbe538SGunnar Mills 
216042cbe538SGunnar Mills     const std::string& psAttributesPath = (*it).first;
216142cbe538SGunnar Mills     const std::string& psAttributesConnection = (*it).second;
216242cbe538SGunnar Mills 
216342cbe538SGunnar Mills     // Response handler for Get DeratingFactor property
216442cbe538SGunnar Mills     auto respHandler = [sensorsAsyncResp, inventoryItems,
2165f94c4ecfSEd Tanous                         callback{std::forward<Callback>(callback)}](
216642cbe538SGunnar Mills                            const boost::system::error_code ec,
21671e1e598dSJonathan Doman                            const uint32_t value) {
216842cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler enter";
216942cbe538SGunnar Mills         if (ec)
217042cbe538SGunnar Mills         {
217142cbe538SGunnar Mills             BMCWEB_LOG_ERROR
217242cbe538SGunnar Mills                 << "getPowerSupplyAttributesData respHandler DBus error " << ec;
21738d1b46d7Szhanghch05             messages::internalError(sensorsAsyncResp->asyncResp->res);
217442cbe538SGunnar Mills             return;
217542cbe538SGunnar Mills         }
217642cbe538SGunnar Mills 
21771e1e598dSJonathan Doman         BMCWEB_LOG_DEBUG << "PS EfficiencyPercent value: " << value;
217842cbe538SGunnar Mills         // Store value in Power Supply Inventory Items
217942cbe538SGunnar Mills         for (InventoryItem& inventoryItem : *inventoryItems)
218042cbe538SGunnar Mills         {
218155f79e6fSEd Tanous             if (inventoryItem.isPowerSupply)
218242cbe538SGunnar Mills             {
218342cbe538SGunnar Mills                 inventoryItem.powerSupplyEfficiencyPercent =
21841e1e598dSJonathan Doman                     static_cast<int>(value);
218542cbe538SGunnar Mills             }
218642cbe538SGunnar Mills         }
218742cbe538SGunnar Mills 
218842cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData respHandler exit";
218942cbe538SGunnar Mills         callback(inventoryItems);
219042cbe538SGunnar Mills     };
219142cbe538SGunnar Mills 
219242cbe538SGunnar Mills     // Get the DeratingFactor property for the PowerSupplyAttributes
219342cbe538SGunnar Mills     // Currently only property on the interface/only one we care about
21941e1e598dSJonathan Doman     sdbusplus::asio::getProperty<uint32_t>(
21951e1e598dSJonathan Doman         *crow::connections::systemBus, psAttributesConnection, psAttributesPath,
21961e1e598dSJonathan Doman         "xyz.openbmc_project.Control.PowerSupplyAttributes", "DeratingFactor",
21971e1e598dSJonathan Doman         std::move(respHandler));
219842cbe538SGunnar Mills 
219942cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributesData exit";
220042cbe538SGunnar Mills }
220142cbe538SGunnar Mills 
220242cbe538SGunnar Mills /**
220342cbe538SGunnar Mills  * @brief Gets the Power Supply Attributes such as EfficiencyPercent
220442cbe538SGunnar Mills  *
220542cbe538SGunnar Mills  * Gets the D-Bus connection (service) that provides Power Supply Attributes
220642cbe538SGunnar Mills  * data. Then gets the Power Supply Attributes data from the connection
220742cbe538SGunnar Mills  * (currently just assumes 1 connection) and stores the data in the inventory
220842cbe538SGunnar Mills  * item.
220942cbe538SGunnar Mills  *
221042cbe538SGunnar Mills  * This data is later used to provide sensor property values in the JSON
221142cbe538SGunnar Mills  * response. DeratingFactor on D-Bus is mapped to EfficiencyPercent on Redfish.
221242cbe538SGunnar Mills  *
221342cbe538SGunnar Mills  * Finds the Power Supply Attributes data asynchronously. Invokes callback
221442cbe538SGunnar Mills  * when information has been obtained.
221542cbe538SGunnar Mills  *
221642cbe538SGunnar Mills  * The callback must have the following signature:
221742cbe538SGunnar Mills  *   @code
221842cbe538SGunnar Mills  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
221942cbe538SGunnar Mills  *   @endcode
222042cbe538SGunnar Mills  *
222142cbe538SGunnar Mills  * @param sensorsAsyncResp Pointer to object holding response data.
222242cbe538SGunnar Mills  * @param inventoryItems D-Bus inventory items associated with sensors.
222342cbe538SGunnar Mills  * @param callback Callback to invoke when data has been obtained.
222442cbe538SGunnar Mills  */
222542cbe538SGunnar Mills template <typename Callback>
222642cbe538SGunnar Mills void getPowerSupplyAttributes(
222742cbe538SGunnar Mills     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
222842cbe538SGunnar Mills     std::shared_ptr<std::vector<InventoryItem>> inventoryItems,
222942cbe538SGunnar Mills     Callback&& callback)
223042cbe538SGunnar Mills {
223142cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes enter";
223242cbe538SGunnar Mills 
223342cbe538SGunnar Mills     // Only need the power supply attributes when the Power Schema
2234a0ec28b6SAdrian Ambrożewicz     if (sensorsAsyncResp->chassisSubNode != sensors::node::power)
223542cbe538SGunnar Mills     {
223642cbe538SGunnar Mills         BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit since not Power";
223742cbe538SGunnar Mills         callback(inventoryItems);
223842cbe538SGunnar Mills         return;
223942cbe538SGunnar Mills     }
224042cbe538SGunnar Mills 
224142cbe538SGunnar Mills     const std::array<std::string, 1> interfaces = {
224242cbe538SGunnar Mills         "xyz.openbmc_project.Control.PowerSupplyAttributes"};
224342cbe538SGunnar Mills 
224442cbe538SGunnar Mills     // Response handler for parsing output from GetSubTree
2245b9d36b47SEd Tanous     auto respHandler =
2246b9d36b47SEd Tanous         [callback{std::forward<Callback>(callback)}, sensorsAsyncResp,
2247b9d36b47SEd Tanous          inventoryItems](
2248b9d36b47SEd Tanous             const boost::system::error_code ec,
2249b9d36b47SEd Tanous             const dbus::utility::MapperGetSubTreeResponse& subtree) {
225042cbe538SGunnar Mills             BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler enter";
225142cbe538SGunnar Mills             if (ec)
225242cbe538SGunnar Mills             {
22538d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
225442cbe538SGunnar Mills                 BMCWEB_LOG_ERROR
225542cbe538SGunnar Mills                     << "getPowerSupplyAttributes respHandler DBus error " << ec;
225642cbe538SGunnar Mills                 return;
225742cbe538SGunnar Mills             }
225826f6976fSEd Tanous             if (subtree.empty())
225942cbe538SGunnar Mills             {
226042cbe538SGunnar Mills                 BMCWEB_LOG_DEBUG << "Can't find Power Supply Attributes!";
226142cbe538SGunnar Mills                 callback(inventoryItems);
226242cbe538SGunnar Mills                 return;
226342cbe538SGunnar Mills             }
226442cbe538SGunnar Mills 
226542cbe538SGunnar Mills             // Currently we only support 1 power supply attribute, use this for
226642cbe538SGunnar Mills             // all the power supplies. Build map of object path to connection.
226742cbe538SGunnar Mills             // Assume just 1 connection and 1 path for now.
226842cbe538SGunnar Mills             boost::container::flat_map<std::string, std::string>
226942cbe538SGunnar Mills                 psAttributesConnections;
227042cbe538SGunnar Mills 
227142cbe538SGunnar Mills             if (subtree[0].first.empty() || subtree[0].second.empty())
227242cbe538SGunnar Mills             {
227342cbe538SGunnar Mills                 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
227442cbe538SGunnar Mills                 callback(inventoryItems);
227542cbe538SGunnar Mills                 return;
227642cbe538SGunnar Mills             }
227742cbe538SGunnar Mills 
227842cbe538SGunnar Mills             const std::string& psAttributesPath = subtree[0].first;
227942cbe538SGunnar Mills             const std::string& connection = subtree[0].second.begin()->first;
228042cbe538SGunnar Mills 
228142cbe538SGunnar Mills             if (connection.empty())
228242cbe538SGunnar Mills             {
228342cbe538SGunnar Mills                 BMCWEB_LOG_DEBUG << "Power Supply Attributes mapper error!";
228442cbe538SGunnar Mills                 callback(inventoryItems);
228542cbe538SGunnar Mills                 return;
228642cbe538SGunnar Mills             }
228742cbe538SGunnar Mills 
228842cbe538SGunnar Mills             psAttributesConnections[psAttributesPath] = connection;
228942cbe538SGunnar Mills             BMCWEB_LOG_DEBUG << "Added mapping " << psAttributesPath << " -> "
229042cbe538SGunnar Mills                              << connection;
229142cbe538SGunnar Mills 
229242cbe538SGunnar Mills             getPowerSupplyAttributesData(sensorsAsyncResp, inventoryItems,
229342cbe538SGunnar Mills                                          psAttributesConnections,
229442cbe538SGunnar Mills                                          std::move(callback));
229542cbe538SGunnar Mills             BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes respHandler exit";
229642cbe538SGunnar Mills         };
229742cbe538SGunnar Mills     // Make call to ObjectMapper to find the PowerSupplyAttributes service
229842cbe538SGunnar Mills     crow::connections::systemBus->async_method_call(
229942cbe538SGunnar Mills         std::move(respHandler), "xyz.openbmc_project.ObjectMapper",
230042cbe538SGunnar Mills         "/xyz/openbmc_project/object_mapper",
230142cbe538SGunnar Mills         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
230242cbe538SGunnar Mills         "/xyz/openbmc_project", 0, interfaces);
230342cbe538SGunnar Mills     BMCWEB_LOG_DEBUG << "getPowerSupplyAttributes exit";
230442cbe538SGunnar Mills }
230542cbe538SGunnar Mills 
230642cbe538SGunnar Mills /**
2307adc4f0dbSShawn McCarney  * @brief Gets inventory items associated with sensors.
23088fb49dd6SShawn McCarney  *
23098fb49dd6SShawn McCarney  * Finds the inventory items that are associated with the specified sensors.
2310adc4f0dbSShawn McCarney  * Then gets D-Bus data for the inventory items, such as presence and VPD.
23118fb49dd6SShawn McCarney  *
2312adc4f0dbSShawn McCarney  * This data is later used to provide sensor property values in the JSON
2313adc4f0dbSShawn McCarney  * response.
23148fb49dd6SShawn McCarney  *
2315adc4f0dbSShawn McCarney  * Finds the inventory items asynchronously.  Invokes callback when the
2316adc4f0dbSShawn McCarney  * inventory items have been obtained.
2317adc4f0dbSShawn McCarney  *
2318adc4f0dbSShawn McCarney  * The callback must have the following signature:
2319adc4f0dbSShawn McCarney  *   @code
2320adc4f0dbSShawn McCarney  *   callback(std::shared_ptr<std::vector<InventoryItem>> inventoryItems)
2321adc4f0dbSShawn McCarney  *   @endcode
23228fb49dd6SShawn McCarney  *
23238fb49dd6SShawn McCarney  * @param sensorsAsyncResp Pointer to object holding response data.
23248fb49dd6SShawn McCarney  * @param sensorNames All sensors within the current chassis.
23258fb49dd6SShawn McCarney  * @param objectMgrPaths Mappings from connection name to DBus object path that
23268fb49dd6SShawn McCarney  * implements ObjectManager.
2327adc4f0dbSShawn McCarney  * @param callback Callback to invoke when inventory items have been obtained.
23288fb49dd6SShawn McCarney  */
2329adc4f0dbSShawn McCarney template <typename Callback>
2330adc4f0dbSShawn McCarney static void getInventoryItems(
23318fb49dd6SShawn McCarney     std::shared_ptr<SensorsAsyncResp> sensorsAsyncResp,
23328fb49dd6SShawn McCarney     const std::shared_ptr<boost::container::flat_set<std::string>> sensorNames,
23338fb49dd6SShawn McCarney     std::shared_ptr<boost::container::flat_map<std::string, std::string>>
2334adc4f0dbSShawn McCarney         objectMgrPaths,
2335adc4f0dbSShawn McCarney     Callback&& callback)
23368fb49dd6SShawn McCarney {
2337adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItems enter";
2338adc4f0dbSShawn McCarney     auto getInventoryItemAssociationsCb =
2339f94c4ecfSEd Tanous         [sensorsAsyncResp, objectMgrPaths,
2340f94c4ecfSEd Tanous          callback{std::forward<Callback>(callback)}](
2341adc4f0dbSShawn McCarney             std::shared_ptr<std::vector<InventoryItem>> inventoryItems) {
2342adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb enter";
23438fb49dd6SShawn McCarney             auto getInventoryItemsConnectionsCb =
2344adc4f0dbSShawn McCarney                 [sensorsAsyncResp, inventoryItems, objectMgrPaths,
2345f94c4ecfSEd Tanous                  callback{std::forward<const Callback>(callback)}](
23468fb49dd6SShawn McCarney                     std::shared_ptr<boost::container::flat_set<std::string>>
23478fb49dd6SShawn McCarney                         invConnections) {
23488fb49dd6SShawn McCarney                     BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb enter";
2349d500549bSAnthony Wilson                     auto getInventoryItemsDataCb =
2350d500549bSAnthony Wilson                         [sensorsAsyncResp, inventoryItems,
2351d500549bSAnthony Wilson                          callback{std::move(callback)}]() {
2352d500549bSAnthony Wilson                             BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb enter";
235342cbe538SGunnar Mills 
235442cbe538SGunnar Mills                             auto getInventoryLedsCb = [sensorsAsyncResp,
235542cbe538SGunnar Mills                                                        inventoryItems,
235642cbe538SGunnar Mills                                                        callback{std::move(
235742cbe538SGunnar Mills                                                            callback)}]() {
235842cbe538SGunnar Mills                                 BMCWEB_LOG_DEBUG << "getInventoryLedsCb enter";
235942cbe538SGunnar Mills                                 // Find Power Supply Attributes and get the data
236042cbe538SGunnar Mills                                 getPowerSupplyAttributes(sensorsAsyncResp,
236142cbe538SGunnar Mills                                                          inventoryItems,
236242cbe538SGunnar Mills                                                          std::move(callback));
236342cbe538SGunnar Mills                                 BMCWEB_LOG_DEBUG << "getInventoryLedsCb exit";
236442cbe538SGunnar Mills                             };
236542cbe538SGunnar Mills 
2366d500549bSAnthony Wilson                             // Find led connections and get the data
2367d500549bSAnthony Wilson                             getInventoryLeds(sensorsAsyncResp, inventoryItems,
236842cbe538SGunnar Mills                                              std::move(getInventoryLedsCb));
2369d500549bSAnthony Wilson                             BMCWEB_LOG_DEBUG << "getInventoryItemsDataCb exit";
2370d500549bSAnthony Wilson                         };
23718fb49dd6SShawn McCarney 
2372adc4f0dbSShawn McCarney                     // Get inventory item data from connections
2373adc4f0dbSShawn McCarney                     getInventoryItemsData(sensorsAsyncResp, inventoryItems,
2374adc4f0dbSShawn McCarney                                           invConnections, objectMgrPaths,
2375d500549bSAnthony Wilson                                           std::move(getInventoryItemsDataCb));
23768fb49dd6SShawn McCarney                     BMCWEB_LOG_DEBUG << "getInventoryItemsConnectionsCb exit";
23778fb49dd6SShawn McCarney                 };
23788fb49dd6SShawn McCarney 
2379adc4f0dbSShawn McCarney             // Get connections that provide inventory item data
23808fb49dd6SShawn McCarney             getInventoryItemsConnections(
2381adc4f0dbSShawn McCarney                 sensorsAsyncResp, inventoryItems,
23828fb49dd6SShawn McCarney                 std::move(getInventoryItemsConnectionsCb));
2383adc4f0dbSShawn McCarney             BMCWEB_LOG_DEBUG << "getInventoryItemAssociationsCb exit";
23848fb49dd6SShawn McCarney         };
23858fb49dd6SShawn McCarney 
2386adc4f0dbSShawn McCarney     // Get associations from sensors to inventory items
2387adc4f0dbSShawn McCarney     getInventoryItemAssociations(sensorsAsyncResp, sensorNames, objectMgrPaths,
2388adc4f0dbSShawn McCarney                                  std::move(getInventoryItemAssociationsCb));
2389adc4f0dbSShawn McCarney     BMCWEB_LOG_DEBUG << "getInventoryItems exit";
2390adc4f0dbSShawn McCarney }
2391adc4f0dbSShawn McCarney 
2392adc4f0dbSShawn McCarney /**
2393adc4f0dbSShawn McCarney  * @brief Returns JSON PowerSupply object for the specified inventory item.
2394adc4f0dbSShawn McCarney  *
2395adc4f0dbSShawn McCarney  * Searches for a JSON PowerSupply object that matches the specified inventory
2396adc4f0dbSShawn McCarney  * item.  If one is not found, a new PowerSupply object is added to the JSON
2397adc4f0dbSShawn McCarney  * array.
2398adc4f0dbSShawn McCarney  *
2399adc4f0dbSShawn McCarney  * Multiple sensors are often associated with one power supply inventory item.
2400adc4f0dbSShawn McCarney  * As a result, multiple sensor values are stored in one JSON PowerSupply
2401adc4f0dbSShawn McCarney  * object.
2402adc4f0dbSShawn McCarney  *
2403adc4f0dbSShawn McCarney  * @param powerSupplyArray JSON array containing Redfish PowerSupply objects.
2404adc4f0dbSShawn McCarney  * @param inventoryItem Inventory item for the power supply.
2405adc4f0dbSShawn McCarney  * @param chassisId Chassis that contains the power supply.
2406adc4f0dbSShawn McCarney  * @return JSON PowerSupply object for the specified inventory item.
2407adc4f0dbSShawn McCarney  */
240823a21a1cSEd Tanous inline nlohmann::json& getPowerSupply(nlohmann::json& powerSupplyArray,
2409adc4f0dbSShawn McCarney                                       const InventoryItem& inventoryItem,
2410adc4f0dbSShawn McCarney                                       const std::string& chassisId)
2411adc4f0dbSShawn McCarney {
2412adc4f0dbSShawn McCarney     // Check if matching PowerSupply object already exists in JSON array
2413adc4f0dbSShawn McCarney     for (nlohmann::json& powerSupply : powerSupplyArray)
2414adc4f0dbSShawn McCarney     {
2415adc4f0dbSShawn McCarney         if (powerSupply["MemberId"] == inventoryItem.name)
2416adc4f0dbSShawn McCarney         {
2417adc4f0dbSShawn McCarney             return powerSupply;
2418adc4f0dbSShawn McCarney         }
2419adc4f0dbSShawn McCarney     }
2420adc4f0dbSShawn McCarney 
2421adc4f0dbSShawn McCarney     // Add new PowerSupply object to JSON array
2422adc4f0dbSShawn McCarney     powerSupplyArray.push_back({});
2423adc4f0dbSShawn McCarney     nlohmann::json& powerSupply = powerSupplyArray.back();
2424adc4f0dbSShawn McCarney     powerSupply["@odata.id"] =
2425adc4f0dbSShawn McCarney         "/redfish/v1/Chassis/" + chassisId + "/Power#/PowerSupplies/";
2426adc4f0dbSShawn McCarney     powerSupply["MemberId"] = inventoryItem.name;
2427adc4f0dbSShawn McCarney     powerSupply["Name"] = boost::replace_all_copy(inventoryItem.name, "_", " ");
2428adc4f0dbSShawn McCarney     powerSupply["Manufacturer"] = inventoryItem.manufacturer;
2429adc4f0dbSShawn McCarney     powerSupply["Model"] = inventoryItem.model;
2430adc4f0dbSShawn McCarney     powerSupply["PartNumber"] = inventoryItem.partNumber;
2431adc4f0dbSShawn McCarney     powerSupply["SerialNumber"] = inventoryItem.serialNumber;
2432d500549bSAnthony Wilson     setLedState(powerSupply, &inventoryItem);
2433adc4f0dbSShawn McCarney 
243442cbe538SGunnar Mills     if (inventoryItem.powerSupplyEfficiencyPercent >= 0)
243542cbe538SGunnar Mills     {
243642cbe538SGunnar Mills         powerSupply["EfficiencyPercent"] =
243742cbe538SGunnar Mills             inventoryItem.powerSupplyEfficiencyPercent;
243842cbe538SGunnar Mills     }
243942cbe538SGunnar Mills 
244042cbe538SGunnar Mills     powerSupply["Status"]["State"] = getState(&inventoryItem);
2441adc4f0dbSShawn McCarney     const char* health = inventoryItem.isFunctional ? "OK" : "Critical";
2442adc4f0dbSShawn McCarney     powerSupply["Status"]["Health"] = health;
2443adc4f0dbSShawn McCarney 
2444adc4f0dbSShawn McCarney     return powerSupply;
24458fb49dd6SShawn McCarney }
24468fb49dd6SShawn McCarney 
24478fb49dd6SShawn McCarney /**
2448de629b6eSShawn McCarney  * @brief Gets the values of the specified sensors.
2449de629b6eSShawn McCarney  *
2450de629b6eSShawn McCarney  * Stores the results as JSON in the SensorsAsyncResp.
2451de629b6eSShawn McCarney  *
2452de629b6eSShawn McCarney  * Gets the sensor values asynchronously.  Stores the results later when the
2453de629b6eSShawn McCarney  * information has been obtained.
2454de629b6eSShawn McCarney  *
2455adc4f0dbSShawn McCarney  * The sensorNames set contains all requested sensors for the current chassis.
2456de629b6eSShawn McCarney  *
2457de629b6eSShawn McCarney  * To minimize the number of DBus calls, the DBus method
2458de629b6eSShawn McCarney  * org.freedesktop.DBus.ObjectManager.GetManagedObjects() is used to get the
2459de629b6eSShawn McCarney  * values of all sensors provided by a connection (service).
2460de629b6eSShawn McCarney  *
2461de629b6eSShawn McCarney  * The connections set contains all the connections that provide sensor values.
2462de629b6eSShawn McCarney  *
2463de629b6eSShawn McCarney  * The objectMgrPaths map contains mappings from a connection name to the
2464de629b6eSShawn McCarney  * corresponding DBus object path that implements ObjectManager.
2465de629b6eSShawn McCarney  *
2466adc4f0dbSShawn McCarney  * The InventoryItem vector contains D-Bus inventory items associated with the
2467adc4f0dbSShawn McCarney  * sensors.  Inventory item data is needed for some Redfish sensor properties.
2468adc4f0dbSShawn McCarney  *
2469de629b6eSShawn McCarney  * @param SensorsAsyncResp Pointer to object holding response data.
2470adc4f0dbSShawn McCarney  * @param sensorNames All requested sensors within the current chassis.
2471de629b6eSShawn McCarney  * @param connections Connections that provide sensor values.
2472de629b6eSShawn McCarney  * @param objectMgrPaths Mappings from connection name to DBus object path that
2473de629b6eSShawn McCarney  * implements ObjectManager.
2474adc4f0dbSShawn McCarney  * @param inventoryItems Inventory items associated with the sensors.
2475de629b6eSShawn McCarney  */
247623a21a1cSEd Tanous inline void getSensorData(
247781ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2478b5a76932SEd Tanous     const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames,
2479de629b6eSShawn McCarney     const boost::container::flat_set<std::string>& connections,
2480b5a76932SEd Tanous     const std::shared_ptr<boost::container::flat_map<std::string, std::string>>&
2481adc4f0dbSShawn McCarney         objectMgrPaths,
2482b5a76932SEd Tanous     const std::shared_ptr<std::vector<InventoryItem>>& inventoryItems)
2483de629b6eSShawn McCarney {
2484de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getSensorData enter";
2485de629b6eSShawn McCarney     // Get managed objects from all services exposing sensors
2486de629b6eSShawn McCarney     for (const std::string& connection : connections)
2487de629b6eSShawn McCarney     {
2488de629b6eSShawn McCarney         // Response handler to process managed objects
248981ce609eSEd Tanous         auto getManagedObjectsCb = [sensorsAsyncResp, sensorNames,
2490adc4f0dbSShawn McCarney                                     inventoryItems](
2491de629b6eSShawn McCarney                                        const boost::system::error_code ec,
2492711ac7a9SEd Tanous                                        dbus::utility::ManagedObjectType& resp) {
2493de629b6eSShawn McCarney             BMCWEB_LOG_DEBUG << "getManagedObjectsCb enter";
2494de629b6eSShawn McCarney             if (ec)
2495de629b6eSShawn McCarney             {
2496de629b6eSShawn McCarney                 BMCWEB_LOG_ERROR << "getManagedObjectsCb DBUS error: " << ec;
24978d1b46d7Szhanghch05                 messages::internalError(sensorsAsyncResp->asyncResp->res);
2498de629b6eSShawn McCarney                 return;
2499de629b6eSShawn McCarney             }
2500de629b6eSShawn McCarney             // Go through all objects and update response with sensor data
2501de629b6eSShawn McCarney             for (const auto& objDictEntry : resp)
2502de629b6eSShawn McCarney             {
2503de629b6eSShawn McCarney                 const std::string& objPath =
2504de629b6eSShawn McCarney                     static_cast<const std::string&>(objDictEntry.first);
2505de629b6eSShawn McCarney                 BMCWEB_LOG_DEBUG << "getManagedObjectsCb parsing object "
2506de629b6eSShawn McCarney                                  << objPath;
2507de629b6eSShawn McCarney 
2508de629b6eSShawn McCarney                 std::vector<std::string> split;
2509de629b6eSShawn McCarney                 // Reserve space for
2510de629b6eSShawn McCarney                 // /xyz/openbmc_project/sensors/<name>/<subname>
2511de629b6eSShawn McCarney                 split.reserve(6);
2512de629b6eSShawn McCarney                 boost::algorithm::split(split, objPath, boost::is_any_of("/"));
2513de629b6eSShawn McCarney                 if (split.size() < 6)
2514de629b6eSShawn McCarney                 {
2515de629b6eSShawn McCarney                     BMCWEB_LOG_ERROR << "Got path that isn't long enough "
2516de629b6eSShawn McCarney                                      << objPath;
2517de629b6eSShawn McCarney                     continue;
2518de629b6eSShawn McCarney                 }
2519de629b6eSShawn McCarney                 // These indexes aren't intuitive, as boost::split puts an empty
2520de629b6eSShawn McCarney                 // string at the beginning
2521de629b6eSShawn McCarney                 const std::string& sensorType = split[4];
2522de629b6eSShawn McCarney                 const std::string& sensorName = split[5];
2523de629b6eSShawn McCarney                 BMCWEB_LOG_DEBUG << "sensorName " << sensorName
2524de629b6eSShawn McCarney                                  << " sensorType " << sensorType;
252549c53ac9SJohnathan Mantey                 if (sensorNames->find(objPath) == sensorNames->end())
2526de629b6eSShawn McCarney                 {
2527accdbb2cSAndrew Geissler                     BMCWEB_LOG_DEBUG << sensorName << " not in sensor list ";
2528de629b6eSShawn McCarney                     continue;
2529de629b6eSShawn McCarney                 }
2530de629b6eSShawn McCarney 
2531adc4f0dbSShawn McCarney                 // Find inventory item (if any) associated with sensor
2532adc4f0dbSShawn McCarney                 InventoryItem* inventoryItem =
2533adc4f0dbSShawn McCarney                     findInventoryItemForSensor(inventoryItems, objPath);
2534adc4f0dbSShawn McCarney 
253595a3ecadSAnthony Wilson                 const std::string& sensorSchema =
253681ce609eSEd Tanous                     sensorsAsyncResp->chassisSubNode;
253795a3ecadSAnthony Wilson 
253895a3ecadSAnthony Wilson                 nlohmann::json* sensorJson = nullptr;
253995a3ecadSAnthony Wilson 
2540*928fefb9SNan Zhou                 if (sensorSchema == sensors::node::sensors &&
2541*928fefb9SNan Zhou                     !sensorsAsyncResp->efficientExpand)
254295a3ecadSAnthony Wilson                 {
25438d1b46d7Szhanghch05                     sensorsAsyncResp->asyncResp->res.jsonValue["@odata.id"] =
254481ce609eSEd Tanous                         "/redfish/v1/Chassis/" + sensorsAsyncResp->chassisId +
254581ce609eSEd Tanous                         "/" + sensorsAsyncResp->chassisSubNode + "/" +
254695a3ecadSAnthony Wilson                         sensorName;
25478d1b46d7Szhanghch05                     sensorJson = &(sensorsAsyncResp->asyncResp->res.jsonValue);
254895a3ecadSAnthony Wilson                 }
254995a3ecadSAnthony Wilson                 else
255095a3ecadSAnthony Wilson                 {
2551271584abSEd Tanous                     std::string fieldName;
2552*928fefb9SNan Zhou                     if (sensorsAsyncResp->efficientExpand)
2553*928fefb9SNan Zhou                     {
2554*928fefb9SNan Zhou                         fieldName = "Members";
2555*928fefb9SNan Zhou                     }
2556*928fefb9SNan Zhou                     else if (sensorType == "temperature")
2557de629b6eSShawn McCarney                     {
2558de629b6eSShawn McCarney                         fieldName = "Temperatures";
2559de629b6eSShawn McCarney                     }
2560de629b6eSShawn McCarney                     else if (sensorType == "fan" || sensorType == "fan_tach" ||
2561de629b6eSShawn McCarney                              sensorType == "fan_pwm")
2562de629b6eSShawn McCarney                     {
2563de629b6eSShawn McCarney                         fieldName = "Fans";
2564de629b6eSShawn McCarney                     }
2565de629b6eSShawn McCarney                     else if (sensorType == "voltage")
2566de629b6eSShawn McCarney                     {
2567de629b6eSShawn McCarney                         fieldName = "Voltages";
2568de629b6eSShawn McCarney                     }
2569de629b6eSShawn McCarney                     else if (sensorType == "power")
2570de629b6eSShawn McCarney                     {
257155f79e6fSEd Tanous                         if (sensorName == "total_power")
2572028f7ebcSEddie James                         {
2573028f7ebcSEddie James                             fieldName = "PowerControl";
2574028f7ebcSEddie James                         }
2575adc4f0dbSShawn McCarney                         else if ((inventoryItem != nullptr) &&
2576adc4f0dbSShawn McCarney                                  (inventoryItem->isPowerSupply))
2577028f7ebcSEddie James                         {
2578de629b6eSShawn McCarney                             fieldName = "PowerSupplies";
2579de629b6eSShawn McCarney                         }
2580adc4f0dbSShawn McCarney                         else
2581adc4f0dbSShawn McCarney                         {
2582adc4f0dbSShawn McCarney                             // Other power sensors are in SensorCollection
2583adc4f0dbSShawn McCarney                             continue;
2584adc4f0dbSShawn McCarney                         }
2585028f7ebcSEddie James                     }
2586de629b6eSShawn McCarney                     else
2587de629b6eSShawn McCarney                     {
2588de629b6eSShawn McCarney                         BMCWEB_LOG_ERROR << "Unsure how to handle sensorType "
2589de629b6eSShawn McCarney                                          << sensorType;
2590de629b6eSShawn McCarney                         continue;
2591de629b6eSShawn McCarney                     }
2592de629b6eSShawn McCarney 
2593de629b6eSShawn McCarney                     nlohmann::json& tempArray =
25948d1b46d7Szhanghch05                         sensorsAsyncResp->asyncResp->res.jsonValue[fieldName];
2595adc4f0dbSShawn McCarney                     if (fieldName == "PowerControl")
259649c53ac9SJohnathan Mantey                     {
2597adc4f0dbSShawn McCarney                         if (tempArray.empty())
25987ab06f49SGunnar Mills                         {
259995a3ecadSAnthony Wilson                             // Put multiple "sensors" into a single
260095a3ecadSAnthony Wilson                             // PowerControl. Follows MemberId naming and
260195a3ecadSAnthony Wilson                             // naming in power.hpp.
26027ab06f49SGunnar Mills                             tempArray.push_back(
2603adc4f0dbSShawn McCarney                                 {{"@odata.id",
2604adc4f0dbSShawn McCarney                                   "/redfish/v1/Chassis/" +
260581ce609eSEd Tanous                                       sensorsAsyncResp->chassisId + "/" +
260681ce609eSEd Tanous                                       sensorsAsyncResp->chassisSubNode + "#/" +
2607adc4f0dbSShawn McCarney                                       fieldName + "/0"}});
2608adc4f0dbSShawn McCarney                         }
2609adc4f0dbSShawn McCarney                         sensorJson = &(tempArray.back());
2610adc4f0dbSShawn McCarney                     }
2611adc4f0dbSShawn McCarney                     else if (fieldName == "PowerSupplies")
2612adc4f0dbSShawn McCarney                     {
2613adc4f0dbSShawn McCarney                         if (inventoryItem != nullptr)
2614adc4f0dbSShawn McCarney                         {
2615adc4f0dbSShawn McCarney                             sensorJson =
2616adc4f0dbSShawn McCarney                                 &(getPowerSupply(tempArray, *inventoryItem,
261781ce609eSEd Tanous                                                  sensorsAsyncResp->chassisId));
2618adc4f0dbSShawn McCarney                         }
261949c53ac9SJohnathan Mantey                     }
2620*928fefb9SNan Zhou                     else if (fieldName == "Members")
2621*928fefb9SNan Zhou                     {
2622*928fefb9SNan Zhou                         tempArray.push_back(
2623*928fefb9SNan Zhou                             {{"@odata.id",
2624*928fefb9SNan Zhou                               "/redfish/v1/Chassis/" +
2625*928fefb9SNan Zhou                                   sensorsAsyncResp->chassisId + "/" +
2626*928fefb9SNan Zhou                                   sensorsAsyncResp->chassisSubNode + "/" +
2627*928fefb9SNan Zhou                                   sensorName}});
2628*928fefb9SNan Zhou                         sensorJson = &(tempArray.back());
2629*928fefb9SNan Zhou                     }
263049c53ac9SJohnathan Mantey                     else
263149c53ac9SJohnathan Mantey                     {
2632de629b6eSShawn McCarney                         tempArray.push_back(
263395a3ecadSAnthony Wilson                             {{"@odata.id",
263495a3ecadSAnthony Wilson                               "/redfish/v1/Chassis/" +
263581ce609eSEd Tanous                                   sensorsAsyncResp->chassisId + "/" +
263681ce609eSEd Tanous                                   sensorsAsyncResp->chassisSubNode + "#/" +
263795a3ecadSAnthony Wilson                                   fieldName + "/"}});
2638adc4f0dbSShawn McCarney                         sensorJson = &(tempArray.back());
263949c53ac9SJohnathan Mantey                     }
264095a3ecadSAnthony Wilson                 }
2641de629b6eSShawn McCarney 
2642adc4f0dbSShawn McCarney                 if (sensorJson != nullptr)
2643adc4f0dbSShawn McCarney                 {
2644a0ec28b6SAdrian Ambrożewicz                     objectInterfacesToJson(
264581ce609eSEd Tanous                         sensorName, sensorType, sensorsAsyncResp,
2646a0ec28b6SAdrian Ambrożewicz                         objDictEntry.second, *sensorJson, inventoryItem);
2647adc4f0dbSShawn McCarney                 }
2648de629b6eSShawn McCarney             }
264981ce609eSEd Tanous             if (sensorsAsyncResp.use_count() == 1)
265049c53ac9SJohnathan Mantey             {
265181ce609eSEd Tanous                 sortJSONResponse(sensorsAsyncResp);
2652*928fefb9SNan Zhou                 if (sensorsAsyncResp->chassisSubNode ==
2653*928fefb9SNan Zhou                         sensors::node::sensors &&
2654*928fefb9SNan Zhou                     sensorsAsyncResp->efficientExpand)
2655*928fefb9SNan Zhou                 {
2656*928fefb9SNan Zhou                     sensorsAsyncResp->asyncResp->res
2657*928fefb9SNan Zhou                         .jsonValue["Members@odata.count"] =
2658*928fefb9SNan Zhou                         sensorsAsyncResp->asyncResp->res.jsonValue["Members"]
2659*928fefb9SNan Zhou                             .size();
2660*928fefb9SNan Zhou                 }
2661*928fefb9SNan Zhou                 else if (sensorsAsyncResp->chassisSubNode ==
2662*928fefb9SNan Zhou                          sensors::node::thermal)
26638bd25ccdSJames Feist                 {
266481ce609eSEd Tanous                     populateFanRedundancy(sensorsAsyncResp);
26658bd25ccdSJames Feist                 }
266649c53ac9SJohnathan Mantey             }
2667de629b6eSShawn McCarney             BMCWEB_LOG_DEBUG << "getManagedObjectsCb exit";
2668de629b6eSShawn McCarney         };
2669de629b6eSShawn McCarney 
2670de629b6eSShawn McCarney         // Find DBus object path that implements ObjectManager for the current
2671de629b6eSShawn McCarney         // connection.  If no mapping found, default to "/".
26728fb49dd6SShawn McCarney         auto iter = objectMgrPaths->find(connection);
2673de629b6eSShawn McCarney         const std::string& objectMgrPath =
26748fb49dd6SShawn McCarney             (iter != objectMgrPaths->end()) ? iter->second : "/";
2675de629b6eSShawn McCarney         BMCWEB_LOG_DEBUG << "ObjectManager path for " << connection << " is "
2676de629b6eSShawn McCarney                          << objectMgrPath;
2677de629b6eSShawn McCarney 
2678de629b6eSShawn McCarney         crow::connections::systemBus->async_method_call(
2679de629b6eSShawn McCarney             getManagedObjectsCb, connection, objectMgrPath,
2680de629b6eSShawn McCarney             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
268123a21a1cSEd Tanous     }
2682de629b6eSShawn McCarney     BMCWEB_LOG_DEBUG << "getSensorData exit";
2683de629b6eSShawn McCarney }
2684de629b6eSShawn McCarney 
268523a21a1cSEd Tanous inline void processSensorList(
268681ce609eSEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
2687b5a76932SEd Tanous     const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames)
26881abe55efSEd Tanous {
268995a3ecadSAnthony Wilson     auto getConnectionCb =
269081ce609eSEd Tanous         [sensorsAsyncResp, sensorNames](
269195a3ecadSAnthony Wilson             const boost::container::flat_set<std::string>& connections) {
269255c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "getConnectionCb enter";
2693de629b6eSShawn McCarney             auto getObjectManagerPathsCb =
269481ce609eSEd Tanous                 [sensorsAsyncResp, sensorNames,
2695b5a76932SEd Tanous                  connections](const std::shared_ptr<boost::container::flat_map<
2696b5a76932SEd Tanous                                   std::string, std::string>>& objectMgrPaths) {
2697de629b6eSShawn McCarney                     BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb enter";
2698adc4f0dbSShawn McCarney                     auto getInventoryItemsCb =
269981ce609eSEd Tanous                         [sensorsAsyncResp, sensorNames, connections,
2700adc4f0dbSShawn McCarney                          objectMgrPaths](
2701f23b7296SEd Tanous                             const std::shared_ptr<std::vector<InventoryItem>>&
2702adc4f0dbSShawn McCarney                                 inventoryItems) {
2703adc4f0dbSShawn McCarney                             BMCWEB_LOG_DEBUG << "getInventoryItemsCb enter";
270449c53ac9SJohnathan Mantey                             // Get sensor data and store results in JSON
270581ce609eSEd Tanous                             getSensorData(sensorsAsyncResp, sensorNames,
2706adc4f0dbSShawn McCarney                                           connections, objectMgrPaths,
2707f23b7296SEd Tanous                                           inventoryItems);
2708adc4f0dbSShawn McCarney                             BMCWEB_LOG_DEBUG << "getInventoryItemsCb exit";
2709adc4f0dbSShawn McCarney                         };
2710adc4f0dbSShawn McCarney 
2711adc4f0dbSShawn McCarney                     // Get inventory items associated with sensors
271281ce609eSEd Tanous                     getInventoryItems(sensorsAsyncResp, sensorNames,
2713adc4f0dbSShawn McCarney                                       objectMgrPaths,
2714adc4f0dbSShawn McCarney                                       std::move(getInventoryItemsCb));
2715adc4f0dbSShawn McCarney 
2716de629b6eSShawn McCarney                     BMCWEB_LOG_DEBUG << "getObjectManagerPathsCb exit";
271708777fb0SLewanczyk, Dawid                 };
2718de629b6eSShawn McCarney 
271949c53ac9SJohnathan Mantey             // Get mapping from connection names to the DBus object
272049c53ac9SJohnathan Mantey             // paths that implement the ObjectManager interface
272181ce609eSEd Tanous             getObjectManagerPaths(sensorsAsyncResp,
2722de629b6eSShawn McCarney                                   std::move(getObjectManagerPathsCb));
272355c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "getConnectionCb exit";
272408777fb0SLewanczyk, Dawid         };
2725de629b6eSShawn McCarney 
2726de629b6eSShawn McCarney     // Get set of connections that provide sensor values
272781ce609eSEd Tanous     getConnections(sensorsAsyncResp, sensorNames, std::move(getConnectionCb));
272895a3ecadSAnthony Wilson }
272995a3ecadSAnthony Wilson 
273095a3ecadSAnthony Wilson /**
273195a3ecadSAnthony Wilson  * @brief Entry point for retrieving sensors data related to requested
273295a3ecadSAnthony Wilson  *        chassis.
273395a3ecadSAnthony Wilson  * @param SensorsAsyncResp   Pointer to object holding response data
273495a3ecadSAnthony Wilson  */
2735b5a76932SEd Tanous inline void
273681ce609eSEd Tanous     getChassisData(const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp)
273795a3ecadSAnthony Wilson {
273895a3ecadSAnthony Wilson     BMCWEB_LOG_DEBUG << "getChassisData enter";
273995a3ecadSAnthony Wilson     auto getChassisCb =
274081ce609eSEd Tanous         [sensorsAsyncResp](
2741f23b7296SEd Tanous             const std::shared_ptr<boost::container::flat_set<std::string>>&
274295a3ecadSAnthony Wilson                 sensorNames) {
274395a3ecadSAnthony Wilson             BMCWEB_LOG_DEBUG << "getChassisCb enter";
274481ce609eSEd Tanous             processSensorList(sensorsAsyncResp, sensorNames);
274555c7b7a2SEd Tanous             BMCWEB_LOG_DEBUG << "getChassisCb exit";
274608777fb0SLewanczyk, Dawid         };
2747*928fefb9SNan Zhou     // SensorCollection doesn't contain the Redundancy property
2748*928fefb9SNan Zhou     if (sensorsAsyncResp->chassisSubNode != sensors::node::sensors)
2749*928fefb9SNan Zhou     {
27508d1b46d7Szhanghch05         sensorsAsyncResp->asyncResp->res.jsonValue["Redundancy"] =
27518d1b46d7Szhanghch05             nlohmann::json::array();
2752*928fefb9SNan Zhou     }
275326f03899SShawn McCarney     // Get set of sensors in chassis
275481ce609eSEd Tanous     getChassis(sensorsAsyncResp, std::move(getChassisCb));
275555c7b7a2SEd Tanous     BMCWEB_LOG_DEBUG << "getChassisData exit";
2756271584abSEd Tanous }
275708777fb0SLewanczyk, Dawid 
2758413961deSRichard Marian Thomaiyar /**
275949c53ac9SJohnathan Mantey  * @brief Find the requested sensorName in the list of all sensors supplied by
276049c53ac9SJohnathan Mantey  * the chassis node
276149c53ac9SJohnathan Mantey  *
276249c53ac9SJohnathan Mantey  * @param sensorName   The sensor name supplied in the PATCH request
276349c53ac9SJohnathan Mantey  * @param sensorsList  The list of sensors managed by the chassis node
276449c53ac9SJohnathan Mantey  * @param sensorsModified  The list of sensors that were found as a result of
276549c53ac9SJohnathan Mantey  *                         repeated calls to this function
276649c53ac9SJohnathan Mantey  */
276723a21a1cSEd Tanous inline bool findSensorNameUsingSensorPath(
27680a86febdSRichard Marian Thomaiyar     std::string_view sensorName,
276949c53ac9SJohnathan Mantey     boost::container::flat_set<std::string>& sensorsList,
277049c53ac9SJohnathan Mantey     boost::container::flat_set<std::string>& sensorsModified)
277149c53ac9SJohnathan Mantey {
277228aa8de5SGeorge Liu     for (auto& chassisSensor : sensorsList)
277349c53ac9SJohnathan Mantey     {
277428aa8de5SGeorge Liu         sdbusplus::message::object_path path(chassisSensor);
2775b00dcc27SEd Tanous         std::string thisSensorName = path.filename();
277628aa8de5SGeorge Liu         if (thisSensorName.empty())
277749c53ac9SJohnathan Mantey         {
277849c53ac9SJohnathan Mantey             continue;
277949c53ac9SJohnathan Mantey         }
278049c53ac9SJohnathan Mantey         if (thisSensorName == sensorName)
278149c53ac9SJohnathan Mantey         {
278249c53ac9SJohnathan Mantey             sensorsModified.emplace(chassisSensor);
278349c53ac9SJohnathan Mantey             return true;
278449c53ac9SJohnathan Mantey         }
278549c53ac9SJohnathan Mantey     }
278649c53ac9SJohnathan Mantey     return false;
278749c53ac9SJohnathan Mantey }
278849c53ac9SJohnathan Mantey 
278949c53ac9SJohnathan Mantey /**
2790413961deSRichard Marian Thomaiyar  * @brief Entry point for overriding sensor values of given sensor
2791413961deSRichard Marian Thomaiyar  *
27928d1b46d7Szhanghch05  * @param sensorAsyncResp   response object
27934bb3dc34SCarol Wang  * @param allCollections   Collections extract from sensors' request patch info
2794413961deSRichard Marian Thomaiyar  * @param chassisSubNode   Chassis Node for which the query has to happen
2795413961deSRichard Marian Thomaiyar  */
279623a21a1cSEd Tanous inline void setSensorsOverride(
2797b5a76932SEd Tanous     const std::shared_ptr<SensorsAsyncResp>& sensorAsyncResp,
27984bb3dc34SCarol Wang     std::unordered_map<std::string, std::vector<nlohmann::json>>&
2799397fd61fSjayaprakash Mutyala         allCollections)
2800413961deSRichard Marian Thomaiyar {
280170d1d0aaSjayaprakash Mutyala     BMCWEB_LOG_INFO << "setSensorsOverride for subNode"
28024bb3dc34SCarol Wang                     << sensorAsyncResp->chassisSubNode << "\n";
2803413961deSRichard Marian Thomaiyar 
2804543f4400SEd Tanous     const char* propertyValueName = nullptr;
2805f65af9e8SRichard Marian Thomaiyar     std::unordered_map<std::string, std::pair<double, std::string>> overrideMap;
2806413961deSRichard Marian Thomaiyar     std::string memberId;
2807543f4400SEd Tanous     double value = 0.0;
2808f65af9e8SRichard Marian Thomaiyar     for (auto& collectionItems : allCollections)
2809f65af9e8SRichard Marian Thomaiyar     {
2810f65af9e8SRichard Marian Thomaiyar         if (collectionItems.first == "Temperatures")
2811f65af9e8SRichard Marian Thomaiyar         {
2812f65af9e8SRichard Marian Thomaiyar             propertyValueName = "ReadingCelsius";
2813f65af9e8SRichard Marian Thomaiyar         }
2814f65af9e8SRichard Marian Thomaiyar         else if (collectionItems.first == "Fans")
2815f65af9e8SRichard Marian Thomaiyar         {
2816f65af9e8SRichard Marian Thomaiyar             propertyValueName = "Reading";
2817f65af9e8SRichard Marian Thomaiyar         }
2818f65af9e8SRichard Marian Thomaiyar         else
2819f65af9e8SRichard Marian Thomaiyar         {
2820f65af9e8SRichard Marian Thomaiyar             propertyValueName = "ReadingVolts";
2821f65af9e8SRichard Marian Thomaiyar         }
2822f65af9e8SRichard Marian Thomaiyar         for (auto& item : collectionItems.second)
2823f65af9e8SRichard Marian Thomaiyar         {
28248d1b46d7Szhanghch05             if (!json_util::readJson(item, sensorAsyncResp->asyncResp->res,
28258d1b46d7Szhanghch05                                      "MemberId", memberId, propertyValueName,
28268d1b46d7Szhanghch05                                      value))
2827413961deSRichard Marian Thomaiyar             {
2828413961deSRichard Marian Thomaiyar                 return;
2829413961deSRichard Marian Thomaiyar             }
2830f65af9e8SRichard Marian Thomaiyar             overrideMap.emplace(memberId,
2831f65af9e8SRichard Marian Thomaiyar                                 std::make_pair(value, collectionItems.first));
2832f65af9e8SRichard Marian Thomaiyar         }
2833f65af9e8SRichard Marian Thomaiyar     }
28344bb3dc34SCarol Wang 
2835b5a76932SEd Tanous     auto getChassisSensorListCb = [sensorAsyncResp, overrideMap](
2836b5a76932SEd Tanous                                       const std::shared_ptr<
283749c53ac9SJohnathan Mantey                                           boost::container::flat_set<
2838b5a76932SEd Tanous                                               std::string>>& sensorsList) {
283949c53ac9SJohnathan Mantey         // Match sensor names in the PATCH request to those managed by the
284049c53ac9SJohnathan Mantey         // chassis node
284149c53ac9SJohnathan Mantey         const std::shared_ptr<boost::container::flat_set<std::string>>
284249c53ac9SJohnathan Mantey             sensorNames =
284349c53ac9SJohnathan Mantey                 std::make_shared<boost::container::flat_set<std::string>>();
2844f65af9e8SRichard Marian Thomaiyar         for (const auto& item : overrideMap)
2845413961deSRichard Marian Thomaiyar         {
2846f65af9e8SRichard Marian Thomaiyar             const auto& sensor = item.first;
284749c53ac9SJohnathan Mantey             if (!findSensorNameUsingSensorPath(sensor, *sensorsList,
284849c53ac9SJohnathan Mantey                                                *sensorNames))
2849f65af9e8SRichard Marian Thomaiyar             {
2850f65af9e8SRichard Marian Thomaiyar                 BMCWEB_LOG_INFO << "Unable to find memberId " << item.first;
28518d1b46d7Szhanghch05                 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2852f65af9e8SRichard Marian Thomaiyar                                            item.second.second, item.first);
2853413961deSRichard Marian Thomaiyar                 return;
2854413961deSRichard Marian Thomaiyar             }
2855f65af9e8SRichard Marian Thomaiyar         }
2856413961deSRichard Marian Thomaiyar         // Get the connection to which the memberId belongs
28574f277b54SJayaprakash Mutyala         auto getObjectsWithConnectionCb = [sensorAsyncResp, overrideMap](
28584f277b54SJayaprakash Mutyala                                               const boost::container::flat_set<
28594f277b54SJayaprakash Mutyala                                                   std::string>& /*connections*/,
28604f277b54SJayaprakash Mutyala                                               const std::set<std::pair<
28614f277b54SJayaprakash Mutyala                                                   std::string, std::string>>&
2862413961deSRichard Marian Thomaiyar                                                   objectsWithConnection) {
2863f65af9e8SRichard Marian Thomaiyar             if (objectsWithConnection.size() != overrideMap.size())
2864413961deSRichard Marian Thomaiyar             {
2865413961deSRichard Marian Thomaiyar                 BMCWEB_LOG_INFO
2866f65af9e8SRichard Marian Thomaiyar                     << "Unable to find all objects with proper connection "
2867f65af9e8SRichard Marian Thomaiyar                     << objectsWithConnection.size() << " requested "
2868f65af9e8SRichard Marian Thomaiyar                     << overrideMap.size() << "\n";
28694f277b54SJayaprakash Mutyala                 messages::resourceNotFound(sensorAsyncResp->asyncResp->res,
2870a0ec28b6SAdrian Ambrożewicz                                            sensorAsyncResp->chassisSubNode ==
2871a0ec28b6SAdrian Ambrożewicz                                                    sensors::node::thermal
2872413961deSRichard Marian Thomaiyar                                                ? "Temperatures"
2873413961deSRichard Marian Thomaiyar                                                : "Voltages",
2874f65af9e8SRichard Marian Thomaiyar                                            "Count");
2875f65af9e8SRichard Marian Thomaiyar                 return;
2876f65af9e8SRichard Marian Thomaiyar             }
2877f65af9e8SRichard Marian Thomaiyar             for (const auto& item : objectsWithConnection)
2878f65af9e8SRichard Marian Thomaiyar             {
287928aa8de5SGeorge Liu                 sdbusplus::message::object_path path(item.first);
288028aa8de5SGeorge Liu                 std::string sensorName = path.filename();
288128aa8de5SGeorge Liu                 if (sensorName.empty())
2882f65af9e8SRichard Marian Thomaiyar                 {
28834f277b54SJayaprakash Mutyala                     messages::internalError(sensorAsyncResp->asyncResp->res);
2884f65af9e8SRichard Marian Thomaiyar                     return;
2885f65af9e8SRichard Marian Thomaiyar                 }
2886f65af9e8SRichard Marian Thomaiyar 
2887f65af9e8SRichard Marian Thomaiyar                 const auto& iterator = overrideMap.find(sensorName);
2888f65af9e8SRichard Marian Thomaiyar                 if (iterator == overrideMap.end())
2889f65af9e8SRichard Marian Thomaiyar                 {
2890f65af9e8SRichard Marian Thomaiyar                     BMCWEB_LOG_INFO << "Unable to find sensor object"
2891f65af9e8SRichard Marian Thomaiyar                                     << item.first << "\n";
28924f277b54SJayaprakash Mutyala                     messages::internalError(sensorAsyncResp->asyncResp->res);
2893413961deSRichard Marian Thomaiyar                     return;
2894413961deSRichard Marian Thomaiyar                 }
2895413961deSRichard Marian Thomaiyar                 crow::connections::systemBus->async_method_call(
2896f65af9e8SRichard Marian Thomaiyar                     [sensorAsyncResp](const boost::system::error_code ec) {
2897413961deSRichard Marian Thomaiyar                         if (ec)
2898413961deSRichard Marian Thomaiyar                         {
28994f277b54SJayaprakash Mutyala                             if (ec.value() ==
29004f277b54SJayaprakash Mutyala                                 boost::system::errc::permission_denied)
29014f277b54SJayaprakash Mutyala                             {
29024f277b54SJayaprakash Mutyala                                 BMCWEB_LOG_WARNING
29034f277b54SJayaprakash Mutyala                                     << "Manufacturing mode is not Enabled...can't "
29044f277b54SJayaprakash Mutyala                                        "Override the sensor value. ";
29054f277b54SJayaprakash Mutyala 
29064f277b54SJayaprakash Mutyala                                 messages::insufficientPrivilege(
29078d1b46d7Szhanghch05                                     sensorAsyncResp->asyncResp->res);
2908413961deSRichard Marian Thomaiyar                                 return;
2909413961deSRichard Marian Thomaiyar                             }
29104f277b54SJayaprakash Mutyala                             BMCWEB_LOG_DEBUG
29114f277b54SJayaprakash Mutyala                                 << "setOverrideValueStatus DBUS error: " << ec;
29124f277b54SJayaprakash Mutyala                             messages::internalError(
29134f277b54SJayaprakash Mutyala                                 sensorAsyncResp->asyncResp->res);
29144f277b54SJayaprakash Mutyala                         }
2915413961deSRichard Marian Thomaiyar                     },
29164f277b54SJayaprakash Mutyala                     item.second, item.first, "org.freedesktop.DBus.Properties",
29174f277b54SJayaprakash Mutyala                     "Set", "xyz.openbmc_project.Sensor.Value", "Value",
2918168e20c1SEd Tanous                     dbus::utility::DbusVariantType(iterator->second.first));
2919f65af9e8SRichard Marian Thomaiyar             }
2920413961deSRichard Marian Thomaiyar         };
2921413961deSRichard Marian Thomaiyar         // Get object with connection for the given sensor name
2922413961deSRichard Marian Thomaiyar         getObjectsWithConnection(sensorAsyncResp, sensorNames,
2923413961deSRichard Marian Thomaiyar                                  std::move(getObjectsWithConnectionCb));
2924413961deSRichard Marian Thomaiyar     };
2925413961deSRichard Marian Thomaiyar     // get full sensor list for the given chassisId and cross verify the sensor.
2926413961deSRichard Marian Thomaiyar     getChassis(sensorAsyncResp, std::move(getChassisSensorListCb));
2927413961deSRichard Marian Thomaiyar }
2928413961deSRichard Marian Thomaiyar 
2929a0ec28b6SAdrian Ambrożewicz /**
2930a0ec28b6SAdrian Ambrożewicz  * @brief Retrieves mapping of Redfish URIs to sensor value property to D-Bus
2931a0ec28b6SAdrian Ambrożewicz  * path of the sensor.
2932a0ec28b6SAdrian Ambrożewicz  *
2933a0ec28b6SAdrian Ambrożewicz  * Function builds valid Redfish response for sensor query of given chassis and
2934a0ec28b6SAdrian Ambrożewicz  * node. It then builds metadata about Redfish<->D-Bus correlations and provides
2935a0ec28b6SAdrian Ambrożewicz  * it to caller in a callback.
2936a0ec28b6SAdrian Ambrożewicz  *
2937a0ec28b6SAdrian Ambrożewicz  * @param chassis   Chassis for which retrieval should be performed
2938a0ec28b6SAdrian Ambrożewicz  * @param node  Node (group) of sensors. See sensors::node for supported values
2939a0ec28b6SAdrian Ambrożewicz  * @param mapComplete   Callback to be called with retrieval result
2940a0ec28b6SAdrian Ambrożewicz  */
2941021d32cfSKrzysztof Grobelny inline void retrieveUriToDbusMap(const std::string& chassis,
2942021d32cfSKrzysztof Grobelny                                  const std::string& node,
2943a0ec28b6SAdrian Ambrożewicz                                  SensorsAsyncResp::DataCompleteCb&& mapComplete)
2944a0ec28b6SAdrian Ambrożewicz {
2945c2bf7f99SWludzik, Jozef     auto pathIt = sensors::dbus::paths.find(node);
2946c2bf7f99SWludzik, Jozef     if (pathIt == sensors::dbus::paths.end())
2947a0ec28b6SAdrian Ambrożewicz     {
2948a0ec28b6SAdrian Ambrożewicz         BMCWEB_LOG_ERROR << "Wrong node provided : " << node;
2949a0ec28b6SAdrian Ambrożewicz         mapComplete(boost::beast::http::status::bad_request, {});
2950a0ec28b6SAdrian Ambrożewicz         return;
2951a0ec28b6SAdrian Ambrożewicz     }
2952d51e072fSKrzysztof Grobelny 
295372374eb7SNan Zhou     auto asyncResp = std::make_shared<bmcweb::AsyncResp>();
2954a0ec28b6SAdrian Ambrożewicz     auto callback =
295572374eb7SNan Zhou         [asyncResp, mapCompleteCb{std::move(mapComplete)}](
2956a0ec28b6SAdrian Ambrożewicz             const boost::beast::http::status status,
2957a0ec28b6SAdrian Ambrożewicz             const boost::container::flat_map<std::string, std::string>&
2958a0ec28b6SAdrian Ambrożewicz                 uriToDbus) { mapCompleteCb(status, uriToDbus); };
2959a0ec28b6SAdrian Ambrożewicz 
2960a0ec28b6SAdrian Ambrożewicz     auto resp = std::make_shared<SensorsAsyncResp>(
2961d51e072fSKrzysztof Grobelny         asyncResp, chassis, pathIt->second, node, std::move(callback));
2962a0ec28b6SAdrian Ambrożewicz     getChassisData(resp);
2963a0ec28b6SAdrian Ambrożewicz }
2964a0ec28b6SAdrian Ambrożewicz 
2965bacb2162SNan Zhou namespace sensors
2966bacb2162SNan Zhou {
2967*928fefb9SNan Zhou 
2968bacb2162SNan Zhou inline void getChassisCallback(
2969bacb2162SNan Zhou     const std::shared_ptr<SensorsAsyncResp>& asyncResp,
2970bacb2162SNan Zhou     const std::shared_ptr<boost::container::flat_set<std::string>>& sensorNames)
2971bacb2162SNan Zhou {
2972bacb2162SNan Zhou     BMCWEB_LOG_DEBUG << "getChassisCallback enter";
2973bacb2162SNan Zhou 
2974bacb2162SNan Zhou     nlohmann::json& entriesArray =
2975bacb2162SNan Zhou         asyncResp->asyncResp->res.jsonValue["Members"];
2976bacb2162SNan Zhou     for (auto& sensor : *sensorNames)
2977bacb2162SNan Zhou     {
2978bacb2162SNan Zhou         BMCWEB_LOG_DEBUG << "Adding sensor: " << sensor;
2979bacb2162SNan Zhou 
2980bacb2162SNan Zhou         sdbusplus::message::object_path path(sensor);
2981bacb2162SNan Zhou         std::string sensorName = path.filename();
2982bacb2162SNan Zhou         if (sensorName.empty())
2983bacb2162SNan Zhou         {
2984bacb2162SNan Zhou             BMCWEB_LOG_ERROR << "Invalid sensor path: " << sensor;
2985bacb2162SNan Zhou             messages::internalError(asyncResp->asyncResp->res);
2986bacb2162SNan Zhou             return;
2987bacb2162SNan Zhou         }
2988bacb2162SNan Zhou         entriesArray.push_back(
2989bacb2162SNan Zhou             {{"@odata.id", "/redfish/v1/Chassis/" + asyncResp->chassisId + "/" +
2990bacb2162SNan Zhou                                asyncResp->chassisSubNode + "/" + sensorName}});
2991bacb2162SNan Zhou     }
2992bacb2162SNan Zhou 
2993bacb2162SNan Zhou     asyncResp->asyncResp->res.jsonValue["Members@odata.count"] =
2994bacb2162SNan Zhou         entriesArray.size();
2995bacb2162SNan Zhou     BMCWEB_LOG_DEBUG << "getChassisCallback exit";
2996bacb2162SNan Zhou }
2997bacb2162SNan Zhou } // namespace sensors
2998bacb2162SNan Zhou 
29997e860f15SJohn Edward Broadbent inline void requestRoutesSensorCollection(App& app)
300095a3ecadSAnthony Wilson {
30017e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/")
3002ed398213SEd Tanous         .privileges(redfish::privileges::getSensorCollection)
3003*928fefb9SNan Zhou         .methods(
3004*928fefb9SNan Zhou             boost::beast::http::verb::get)([&app](
3005*928fefb9SNan Zhou                                                const crow::Request& req,
3006*928fefb9SNan Zhou                                                const std::shared_ptr<
3007*928fefb9SNan Zhou                                                    bmcweb::AsyncResp>& aResp,
30087e860f15SJohn Edward Broadbent                                                const std::string& chassisId) {
3009*928fefb9SNan Zhou             query_param::QueryCapabilities capabilities = {
3010*928fefb9SNan Zhou                 .canDelegateExpandLevel = 1,
3011*928fefb9SNan Zhou             };
3012*928fefb9SNan Zhou             query_param::Query delegatedQuery;
3013*928fefb9SNan Zhou             if (!redfish::setUpRedfishRouteWithDelegation(
3014*928fefb9SNan Zhou                     app, req, aResp->res, delegatedQuery, capabilities))
301545ca1b86SEd Tanous             {
301645ca1b86SEd Tanous                 return;
301745ca1b86SEd Tanous             }
301845ca1b86SEd Tanous 
3019*928fefb9SNan Zhou             if (delegatedQuery.expandType != query_param::ExpandType::None)
3020*928fefb9SNan Zhou             {
3021*928fefb9SNan Zhou                 // we perform efficient expand.
3022*928fefb9SNan Zhou                 auto asyncResp = std::make_shared<SensorsAsyncResp>(
3023*928fefb9SNan Zhou                     aResp, chassisId,
3024*928fefb9SNan Zhou                     sensors::dbus::paths.at(sensors::node::sensors),
3025*928fefb9SNan Zhou                     sensors::node::sensors,
3026*928fefb9SNan Zhou                     /*efficientExpand=*/true);
3027*928fefb9SNan Zhou                 getChassisData(asyncResp);
30288d1b46d7Szhanghch05 
3029*928fefb9SNan Zhou                 BMCWEB_LOG_DEBUG
3030*928fefb9SNan Zhou                     << "SensorCollection doGet exit via efficient expand handler";
3031*928fefb9SNan Zhou                 return;
3032*928fefb9SNan Zhou             };
3033*928fefb9SNan Zhou 
3034*928fefb9SNan Zhou             // if there's no efficient expand available, we use the default
3035*928fefb9SNan Zhou             // Query Parameters route
3036*928fefb9SNan Zhou             auto asyncResp = std::make_shared<SensorsAsyncResp>(
30378d1b46d7Szhanghch05                 aResp, chassisId,
30388d1b46d7Szhanghch05                 sensors::dbus::paths.at(sensors::node::sensors),
3039a0ec28b6SAdrian Ambrożewicz                 sensors::node::sensors);
3040*928fefb9SNan Zhou 
3041*928fefb9SNan Zhou             // We get all sensors as hyperlinkes in the chassis (this
3042*928fefb9SNan Zhou             // implies we reply on the default query parameters handler)
3043*928fefb9SNan Zhou             getChassis(asyncResp,
3044bacb2162SNan Zhou                        std::bind_front(sensors::getChassisCallback, asyncResp));
304595a3ecadSAnthony Wilson             BMCWEB_LOG_DEBUG << "SensorCollection doGet exit";
30467e860f15SJohn Edward Broadbent         });
304795a3ecadSAnthony Wilson }
304895a3ecadSAnthony Wilson 
30497e860f15SJohn Edward Broadbent inline void requestRoutesSensor(App& app)
305095a3ecadSAnthony Wilson {
30517e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Sensors/<str>/")
3052ed398213SEd Tanous         .privileges(redfish::privileges::getSensor)
30537e860f15SJohn Edward Broadbent         .methods(
305445ca1b86SEd Tanous             boost::beast::http::verb::get)([&app](
305545ca1b86SEd Tanous                                                const crow::Request& req,
30567e860f15SJohn Edward Broadbent                                                const std::shared_ptr<
30577e860f15SJohn Edward Broadbent                                                    bmcweb::AsyncResp>& aResp,
30587e860f15SJohn Edward Broadbent                                                const std::string& chassisId,
30597e860f15SJohn Edward Broadbent                                                const std::string& sensorName) {
306045ca1b86SEd Tanous             if (!redfish::setUpRedfishRoute(app, req, aResp->res))
306145ca1b86SEd Tanous             {
306245ca1b86SEd Tanous                 return;
306345ca1b86SEd Tanous             }
306495a3ecadSAnthony Wilson             BMCWEB_LOG_DEBUG << "Sensor doGet enter";
306595a3ecadSAnthony Wilson             std::shared_ptr<SensorsAsyncResp> asyncResp =
30668d1b46d7Szhanghch05                 std::make_shared<SensorsAsyncResp>(aResp, chassisId,
3067a0ec28b6SAdrian Ambrożewicz                                                    std::vector<const char*>(),
3068a0ec28b6SAdrian Ambrożewicz                                                    sensors::node::sensors);
306995a3ecadSAnthony Wilson 
307095a3ecadSAnthony Wilson             const std::array<const char*, 1> interfaces = {
307195a3ecadSAnthony Wilson                 "xyz.openbmc_project.Sensor.Value"};
307295a3ecadSAnthony Wilson 
307395a3ecadSAnthony Wilson             // Get a list of all of the sensors that implement Sensor.Value
307495a3ecadSAnthony Wilson             // and get the path and service name associated with the sensor
307595a3ecadSAnthony Wilson             crow::connections::systemBus->async_method_call(
3076b9d36b47SEd Tanous                 [asyncResp, sensorName](
3077b9d36b47SEd Tanous                     const boost::system::error_code ec,
3078b9d36b47SEd Tanous                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
307995a3ecadSAnthony Wilson                     BMCWEB_LOG_DEBUG << "respHandler1 enter";
308095a3ecadSAnthony Wilson                     if (ec)
308195a3ecadSAnthony Wilson                     {
30828d1b46d7Szhanghch05                         messages::internalError(asyncResp->asyncResp->res);
30837e860f15SJohn Edward Broadbent                         BMCWEB_LOG_ERROR
30847e860f15SJohn Edward Broadbent                             << "Sensor getSensorPaths resp_handler: "
308595a3ecadSAnthony Wilson                             << "Dbus error " << ec;
308695a3ecadSAnthony Wilson                         return;
308795a3ecadSAnthony Wilson                     }
308895a3ecadSAnthony Wilson 
3089b9d36b47SEd Tanous                     dbus::utility::MapperGetSubTreeResponse::const_iterator it =
3090b9d36b47SEd Tanous                         std::find_if(
309195a3ecadSAnthony Wilson                             subtree.begin(), subtree.end(),
309295a3ecadSAnthony Wilson                             [sensorName](
3093b9d36b47SEd Tanous                                 const std::pair<std::string,
30947e860f15SJohn Edward Broadbent                                                 std::vector<std::pair<
3095b9d36b47SEd Tanous                                                     std::string,
3096b9d36b47SEd Tanous                                                     std::vector<std::string>>>>&
309795a3ecadSAnthony Wilson                                     object) {
3098b9d36b47SEd Tanous                                 sdbusplus::message::object_path path(
3099b9d36b47SEd Tanous                                     object.first);
310028aa8de5SGeorge Liu                                 std::string name = path.filename();
310128aa8de5SGeorge Liu                                 if (name.empty())
310295a3ecadSAnthony Wilson                                 {
310395a3ecadSAnthony Wilson                                     BMCWEB_LOG_ERROR << "Invalid sensor path: "
310428aa8de5SGeorge Liu                                                      << object.first;
310595a3ecadSAnthony Wilson                                     return false;
310695a3ecadSAnthony Wilson                                 }
310795a3ecadSAnthony Wilson 
310895a3ecadSAnthony Wilson                                 return name == sensorName;
310995a3ecadSAnthony Wilson                             });
311095a3ecadSAnthony Wilson 
311195a3ecadSAnthony Wilson                     if (it == subtree.end())
311295a3ecadSAnthony Wilson                     {
311395a3ecadSAnthony Wilson                         BMCWEB_LOG_ERROR << "Could not find path for sensor: "
311495a3ecadSAnthony Wilson                                          << sensorName;
31158d1b46d7Szhanghch05                         messages::resourceNotFound(asyncResp->asyncResp->res,
31168d1b46d7Szhanghch05                                                    "Sensor", sensorName);
311795a3ecadSAnthony Wilson                         return;
311895a3ecadSAnthony Wilson                     }
311995a3ecadSAnthony Wilson                     std::string_view sensorPath = (*it).first;
312095a3ecadSAnthony Wilson                     BMCWEB_LOG_DEBUG << "Found sensor path for sensor '"
312195a3ecadSAnthony Wilson                                      << sensorName << "': " << sensorPath;
312295a3ecadSAnthony Wilson 
31237e860f15SJohn Edward Broadbent                     const std::shared_ptr<
31247e860f15SJohn Edward Broadbent                         boost::container::flat_set<std::string>>
312595a3ecadSAnthony Wilson                         sensorList = std::make_shared<
312695a3ecadSAnthony Wilson                             boost::container::flat_set<std::string>>();
312795a3ecadSAnthony Wilson 
312895a3ecadSAnthony Wilson                     sensorList->emplace(sensorPath);
312995a3ecadSAnthony Wilson                     processSensorList(asyncResp, sensorList);
313095a3ecadSAnthony Wilson                     BMCWEB_LOG_DEBUG << "respHandler1 exit";
313195a3ecadSAnthony Wilson                 },
313295a3ecadSAnthony Wilson                 "xyz.openbmc_project.ObjectMapper",
313395a3ecadSAnthony Wilson                 "/xyz/openbmc_project/object_mapper",
313495a3ecadSAnthony Wilson                 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
313595a3ecadSAnthony Wilson                 "/xyz/openbmc_project/sensors", 2, interfaces);
31367e860f15SJohn Edward Broadbent         });
313795a3ecadSAnthony Wilson }
313895a3ecadSAnthony Wilson 
313908777fb0SLewanczyk, Dawid } // namespace redfish
3140